DarkflameServer/dGame/dComponents/BuffComponent.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

153 lines
4.6 KiB
C
Raw Normal View History

#ifndef BUFFCOMPONENT_H
#define BUFFCOMPONENT_H
#include "dCommonVars.h"
#include "RakNetTypes.h"
#include <vector>
#include <unordered_map>
#include <map>
#include "Component.h"
#include "eReplicaComponentType.h"
class Entity;
/**
* Extra information on effects to apply after applying a buff, for example whether to buff armor, imag or health and by how much
*/
struct BuffParameter {
int32_t buffId;
std::string name;
float value;
std::vector<float> values;
int32_t effectId;
};
/**
* Meta information about a buff that can be applied, e.g. how long it's applied, who applied it, etc.
*/
struct Buff {
int32_t id = 0;
float time = 0;
float tick = 0;
float tickTime = 0;
int32_t stacks = 0;
LWOOBJID source = 0;
int32_t behaviorID = 0;
bool cancelOnDamaged = false;
bool cancelOnDeath = false;
bool cancelOnLogout = false;
bool cancelOnRemoveBuff = false;
bool cancelOnUi = false;
bool cancelOnUnequip = false;
bool cancelOnZone = false;
bool applyOnTeammates = false;
uint32_t refCount = 0;
};
/**
* Allows for the application of buffs to the parent entity, altering health, armor and imagination.
*/
class BuffComponent : public Component {
public:
feat: Add isolated and simplified path to add components (#1204) * Components: Make ComponentType inline Prevents the next commits ODR violation * Components: Add new components * Entity: Add headers inline script component ComponentType * Components: Flip constructor argument order Entity comes first always * Entity: Add generic AddComponent Allows for much easier adding of components and is error proof by not allowing the user to add more than 1 of a specific component type to an Entity. * Entity: Migrate all component constructors Move all to the new variadic templates AddComponent function to reduce clutter and ways the component map is modified. The new function makes no assumptions. Component is assumed to not exist and is checked for with operator[]. This will construct a null component which will then be newed if the component didnt exist, or it will just get the current component if it does already exist. No new component will be allocated or constructed if the component already exists and the already existing pointer is returned instead. * Entity: Add placement new For the case where the component may already exist, use a placement new to construct the component again, it would be constructed again, but would not need to go through the allocator. * Entity: Add comments on likely new code * Tests: Fix tests * Update Entity.cpp * Update SGCannon.cpp * Entity: call destructor when re-constructing * Update Entity.cpp Update Entity.cpp --------- Co-authored-by: Aaron Kimbrell <aronwk.aaron@gmail.com>
2023-10-23 01:08:49 +00:00
inline static const eReplicaComponentType ComponentType = eReplicaComponentType::BUFF;
2022-07-28 13:39:57 +00:00
explicit BuffComponent(Entity* parent);
2022-07-28 13:39:57 +00:00
~BuffComponent();
2022-07-28 13:39:57 +00:00
Entity* GetParent() const;
2022-07-28 13:39:57 +00:00
void LoadFromXml(tinyxml2::XMLDocument* doc) override;
2022-07-28 13:39:57 +00:00
2022-04-05 12:11:06 +00:00
void UpdateXml(tinyxml2::XMLDocument* doc) override;
2022-07-28 13:39:57 +00:00
void Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate) override;
2022-07-28 13:39:57 +00:00
void Update(float deltaTime) override;
2022-07-28 13:39:57 +00:00
/**
* Applies a buff to the parent entity
* @param id the id of the buff to apply
* @param duration the duration of the buff in seconds
* @param source an optional source entity that cast the buff
* @param addImmunity client flag
* @param cancelOnDamaged client flag to indicate that the buff should disappear when damaged
* @param cancelOnDeath client flag to indicate that the buff should disappear when dying
* @param cancelOnLogout client flag to indicate that the buff should disappear when logging out
* @param cancelOnRemoveBuff client flag to indicate that the buff should disappear when a concrete GM to do so comes around
* @param cancelOnUi client flag to indicate that the buff should disappear when interacting with UI
* @param cancelOnUnequip client flag to indicate that the buff should disappear when the triggering item is unequipped
* @param cancelOnZone client flag to indicate that the buff should disappear when changing zones
*/
void ApplyBuff(int32_t id, float duration, LWOOBJID source, bool addImmunity = false, bool cancelOnDamaged = false,
bool cancelOnDeath = true, bool cancelOnLogout = false, bool cancelOnRemoveBuff = true,
bool cancelOnUi = false, bool cancelOnUnequip = false, bool cancelOnZone = false, bool applyOnTeammates = false);
void ApplyBuffFx(uint32_t buffId, const BuffParameter& buffName);
void RemoveBuffFx(uint32_t buffId, const BuffParameter& buffName);
2022-07-28 13:39:57 +00:00
/**
* Removes a buff from the parent entity, reversing its effects
* @param id the id of the buff to remove
* @param removeImmunity whether or not to remove immunity on removing the buff
*/
void RemoveBuff(int32_t id, bool fromUnEquip = false, bool removeImmunity = false, bool ignoreRefCount = false);
2022-07-28 13:39:57 +00:00
/**
* Returns whether or not the entity has a buff identified by `id`
* @param id the id of the buff to find
* @return whether or not the entity has a buff with the specified id active
*/
bool HasBuff(int32_t id);
2022-07-28 13:39:57 +00:00
/**
* Applies the effects of the buffs on the entity, e.g.: changing armor, health, imag, etc.
* @param id the id of the buff effects to apply
*/
void ApplyBuffEffect(int32_t id);
2022-07-28 13:39:57 +00:00
/**
* Reverses the effects of the applied buff
* @param id the id of the buff for which to remove the effects
*/
void RemoveBuffEffect(int32_t id);
2022-07-28 13:39:57 +00:00
/**
* Removes all buffs for the entity and reverses all of their effects
*/
void RemoveAllBuffs();
2022-07-28 13:39:57 +00:00
/**
* Removes all buffs for the entity and reverses all of their effects
*/
void Reset();
2022-07-28 13:39:57 +00:00
/**
* Applies all effects for all buffs, active or not, again
*/
void ReApplyBuffs();
2022-07-28 13:39:57 +00:00
/**
* Gets all the parameters (= effects), for the buffs that belong to this component
* @param buffId
* @return
*/
const std::vector<BuffParameter>& GetBuffParameters(int32_t buffId);
private:
/**
* The currently active buffs
*/
std::map<int32_t, Buff> m_Buffs;
2022-07-28 13:39:57 +00:00
2024-01-03 06:52:11 +00:00
// Buffs to remove at the end of the update frame.
std::vector<int32_t> m_BuffsToRemove;
/**
* Parameters (=effects) for each buff
*/
static std::unordered_map<int32_t, std::vector<BuffParameter>> m_Cache;
};
#endif // BUFFCOMPONENT_H