simplify passing data around

This commit is contained in:
Aaron Kimbre 2022-12-24 14:25:31 -06:00
parent 3f3c5d9215
commit 6bf45cad39
6 changed files with 81 additions and 92 deletions

View File

@ -16,11 +16,23 @@ void ApplyBuffBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitS
if (buffComponent == nullptr) return;
buffComponent->ApplyBuff(m_BuffId, m_Duration, context->originator,
m_AddImmunity, m_ApplyOnTeammates,
m_CancelOnDamaged, m_CancelOnDeath, m_CancelOnLogout, m_CancelOnRemoveBuff,
m_CancelOnUi, m_CancelOnUnequip, m_CancelOnZone, m_CancelOnDamageAbsDone,
m_UseRefCount);
Buff buff;
buff.id = m_BuffId;
buff.duration = m_Duration;
buff.source = context->originator;
buff.addImmunity = m_AddImmunity;
buff.applyOnTeammates = m_ApplyOnTeammates;
buff.cancelOnDamaged = m_CancelOnDamaged;
buff.cancelOnDeath = m_CancelOnDeath;
buff.cancelOnLogout = m_CancelOnLogout;
buff.cancelOnRemoveBuff = m_CancelOnRemoveBuff;
buff.cancelOnUi = m_CancelOnUi;
buff.cancelOnUnequip = m_CancelOnUnequip;
buff.cancelOnZone = m_CancelOnZone;
buff.useRefCount = m_UseRefCount;
buff.cancelOnDamageAbsDone = m_CancelOnDamageAbsDone;
buffComponent->ApplyBuff(buff);
}
void ApplyBuffBehavior::UnCast(BehaviorContext* context, BehaviorBranchContext branch) {

View File

@ -20,11 +20,13 @@ BuffComponent::~BuffComponent() {
}
void BuffComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUpdate, unsigned int& flags) {
// apply buffs from previous sessions that are stored in the charxml
if (bIsInitialUpdate) {
// buffs
if (m_Buffs.empty()) {
outBitStream->Write0();
outBitStream->Write0(); // no buffs
} else {
outBitStream->Write1();
outBitStream->Write1(); // we have some
outBitStream->Write<uint32_t>(m_Buffs.size());
for (const auto& buff : m_Buffs) {
@ -38,17 +40,18 @@ void BuffComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUp
outBitStream->Write(buff.second.cancelOnLogout);
outBitStream->Write(buff.second.cancelOnUnequip);
outBitStream->Write(buff.second.cancelOnDamageAbsDone);
outBitStream->Write0(); // outBitStream->Write(addedByTeammate)
outBitStream->Write(buff.second.source != m_Parent->GetObjectID());
outBitStream->Write(buff.second.applyOnTeammates);
// if (addedByTeammate) outBitStream->Write(team mate lwoobjid);
if (buff.second.source != m_Parent->GetObjectID()) outBitStream->Write(buff.second.source);
outBitStream->Write(buff.second.refcount);
}
}
// buff imunities
if (m_BuffImmunities.empty()) {
outBitStream->Write0();
outBitStream->Write0(); // no buff immunities
} else {
outBitStream->Write1();
outBitStream->Write1(); // we have some
outBitStream->Write<uint32_t>(m_BuffImmunities.size());
for (const auto& buffImmunity : m_BuffImmunities) {
@ -62,9 +65,9 @@ void BuffComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsInitialUp
outBitStream->Write(buffImmunity.second.cancelOnLogout);
outBitStream->Write(buffImmunity.second.cancelOnUnequip);
outBitStream->Write(buffImmunity.second.cancelOnDamageAbsDone);
outBitStream->Write0(); // outBitStream->Write(addedByTeammate)
outBitStream->Write(buffImmunity.second.source != m_Parent->GetObjectID());
outBitStream->Write(buffImmunity.second.applyOnTeammates);
// if (addedByTeammate) outBitStream->Write(team mate lwoobjid);
if (buffImmunity.second.source != m_Parent->GetObjectID()) outBitStream->Write(buffImmunity.second.source);
outBitStream->Write(buffImmunity.second.refcount);
}
}
@ -104,22 +107,17 @@ void BuffComponent::Update(float deltaTime) {
}
}
void BuffComponent::ApplyBuff(const int32_t id, const float duration, const LWOOBJID source,
bool addImmunity, bool applyOnTeammates,
bool cancelOnDamaged, bool cancelOnDeath, bool cancelOnLogout, bool cancelOnRemoveBuff,
bool cancelOnUi, bool cancelOnUnequip, bool cancelOnZone, bool cancelOnDamageAbsDone,
bool useRefCount) {
void BuffComponent::ApplyBuff(Buff buff) {
GameMessages::SendAddBuff(const_cast<LWOOBJID&>(m_Parent->GetObjectID()), source, (uint32_t)id,
(uint32_t)duration * 1000, addImmunity, applyOnTeammates, cancelOnDamaged, cancelOnDeath,
cancelOnLogout, cancelOnRemoveBuff, cancelOnUi, cancelOnUnequip, cancelOnZone, cancelOnDamageAbsDone,
useRefCount);
if (HasBuff(buff.id) && HasBuffImmunity(buff.id)) return;
GameMessages::SendAddBuff(const_cast<LWOOBJID&>(m_Parent->GetObjectID()), buff);
float tick = 0;
float stacks = 0;
int32_t behaviorID = 0;
const auto& parameters = GetBuffParameters(id);
const auto& parameters = GetBuffParameters(buff.id);
for (const auto& parameter : parameters) {
if (parameter.name == "overtime") {
auto* behaviorTemplateTable = CDClientManager::Instance()->GetTable<CDSkillBehaviorTable>("SkillBehavior");
@ -131,31 +129,16 @@ void BuffComponent::ApplyBuff(const int32_t id, const float duration, const LWOO
}
}
ApplyBuffEffect(id);
Buff buff;
buff.id = id;
buff.time = duration;
ApplyBuffEffect(buff.id);
buff.tick = tick;
buff.tickTime = tick;
buff.stacks = stacks;
buff.source = source;
buff.behaviorID = behaviorID;
buff.addImmunity = addImmunity;
buff.applyOnTeammates = applyOnTeammates;
buff.cancelOnDamaged = cancelOnDamaged;
buff.cancelOnDeath = cancelOnDeath;
buff.cancelOnLogout = cancelOnLogout;
buff.cancelOnRemoveBuff = cancelOnRemoveBuff;
buff.cancelOnUi = cancelOnUi;
buff.cancelOnUnequip = cancelOnUnequip;
buff.cancelOnZone = cancelOnZone;
buff.useRefCount = useRefCount;
buff.cancelOnDamageAbsDone = cancelOnDamageAbsDone;
buff.refcount = 0;
m_Buffs.emplace(id, buff);
if (buff.addImmunity) {
m_BuffImmunities.emplace(buff.id, buff);
} else m_Buffs.emplace(buff.id, buff);
}
void BuffComponent::RemoveBuff(int32_t id, bool fromUnEquip, bool removeImmunity) {
@ -176,6 +159,10 @@ bool BuffComponent::HasBuff(int32_t id) {
return m_Buffs.find(id) != m_Buffs.end();
}
bool BuffComponent::HasBuffImmunity(int32_t id) {
return m_BuffImmunities.find(id) != m_BuffImmunities.end();
}
void BuffComponent::ApplyBuffEffect(int32_t id) {
const auto& parameters = GetBuffParameters(id);
for (const auto& parameter : parameters) {

View File

@ -28,6 +28,7 @@ struct BuffParameter
struct Buff
{
int32_t id = 0;
float duration = 0;
float time = 0;
float tick = 0;
float tickTime = 0;
@ -73,25 +74,9 @@ public:
/**
* 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 applyOnTeammates should the buff apply on teammates
* @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
* @param cancelOnDamageAbsDone cancel if we have done damage absorbtion
* @param useRefCount client flag to indicate that the stacks
* @param buff the Buff to apply
*/
void ApplyBuff(int32_t id, float duration, LWOOBJID source, bool addImmunity = false, bool applyOnTeammates = false,
bool cancelOnDamaged = false, bool cancelOnDeath = true, bool cancelOnLogout = false, bool cancelOnRemoveBuff = true,
bool cancelOnUi = false, bool cancelOnUnequip = false, bool cancelOnZone = false, bool cancelOnDamageAbsDone = false,
bool useRefCount = false);
void ApplyBuff(Buff buff);
/**
* Removes a buff from the parent entity, reversing its effects
@ -107,6 +92,14 @@ public:
*/
bool HasBuff(int32_t id);
/**
* Returns whether or not the entity has a buff immunity identified by `id`
* @param id the id of the buff immunity to find
* @return whether or not the entity has a buff with the specified id active
*/
bool HasBuffImmunity(int32_t id);
/**
* 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

View File

@ -61,6 +61,7 @@
#include "RacingControlComponent.h"
#include "RailActivatorComponent.h"
#include "LevelProgressionComponent.h"
#include "BuffComponent.h"
// Message includes:
#include "dZoneManager.h"
@ -4437,39 +4438,34 @@ void GameMessages::SendVehicleNotifyFinishedRace(LWOOBJID objectId, const System
SEND_PACKET;
}
void GameMessages::SendAddBuff(LWOOBJID& objectID, const LWOOBJID& casterID, uint32_t buffID, uint32_t msDuration,
bool addImmunity, bool applyOnTeammates,
bool cancelOnDamaged, bool cancelOnDeath, bool cancelOnLogout, bool cancelOnRemoveBuff,
bool cancelOnUi, bool cancelOnUnequip, bool cancelOnZone, bool cancelOnDamageAbsDone,
bool useRefCount,
const SystemAddress& sysAddr) {
void GameMessages::SendAddBuff(LWOOBJID& target, Buff buff, const SystemAddress& sysAddr) {
CBITSTREAM;
CMSGHEADER;
bitStream.Write(objectID);
bitStream.Write(target);
bitStream.Write(GAME_MSG::GAME_MSG_ADD_BUFF);
bitStream.Write(false); // Added by teammate
bitStream.Write(applyOnTeammates);
bitStream.Write(cancelOnDamageAbsDone);
bitStream.Write(cancelOnDamaged);
bitStream.Write(cancelOnDeath);
bitStream.Write(cancelOnLogout);
bitStream.Write(buff.source != target); // If we were added by a teammate/someone else
bitStream.Write(buff.applyOnTeammates);
bitStream.Write(buff.cancelOnDamageAbsDone);
bitStream.Write(buff.cancelOnDamaged);
bitStream.Write(buff.cancelOnDeath);
bitStream.Write(buff.cancelOnLogout);
bitStream.Write(false); // Cancel on move
bitStream.Write(cancelOnRemoveBuff);
bitStream.Write(buff.cancelOnRemoveBuff);
bitStream.Write(cancelOnUi);
bitStream.Write(cancelOnUnequip);
bitStream.Write(cancelOnZone);
bitStream.Write(buff.cancelOnUi);
bitStream.Write(buff.cancelOnUnequip);
bitStream.Write(buff.cancelOnZone);
bitStream.Write(false); // Ignore immunities
bitStream.Write(addImmunity);
bitStream.Write(useRefCount);
bitStream.Write(buff.addImmunity);
bitStream.Write(buff.useRefCount);
bitStream.Write(buffID);
bitStream.Write(msDuration);
bitStream.Write(buff.id);
bitStream.Write(buff.duration * 1000); // needs to be in MS
bitStream.Write(casterID);
bitStream.Write(casterID);
bitStream.Write(buff.source);
bitStream.Write(buff.source);
if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) SEND_PACKET_BROADCAST;
SEND_PACKET;

View File

@ -22,6 +22,7 @@ class NiPoint3;
enum class eUnequippableActiveType;
enum eInventoryType : uint32_t;
class Item;
struct Buff;
namespace GameMessages {
class PropertyDataMessage;
@ -183,12 +184,7 @@ namespace GameMessages {
// The success or failure response sent back to the client will preserve the same value for localID.
void SendBBBSaveResponse(const LWOOBJID& objectId, const LWOOBJID& localID, unsigned char* buffer, uint32_t bufferSize, const SystemAddress& sysAddr);
void SendAddBuff(LWOOBJID& objectID, const LWOOBJID& casterID, uint32_t buffID, uint32_t msDuration,
bool addImmunity = false, bool applyOnTeammates = false,
bool cancelOnDamaged = false, bool cancelOnDeath = true,bool cancelOnLogout = false, bool cancelOnRemoveBuff = true,
bool cancelOnUi = false, bool cancelOnUnequip = false, bool cancelOnZone = false, bool cancelOnDamageAbsDone = false,
bool useRefCount = false,
const SystemAddress& sysAddr = UNASSIGNED_SYSTEM_ADDRESS);
void SendAddBuff(LWOOBJID& target, Buff buff, const SystemAddress& sysAddr = UNASSIGNED_SYSTEM_ADDRESS);
void SendToggleGMInvis(LWOOBJID objectId, bool enabled, const SystemAddress& sysAddr);

View File

@ -1463,6 +1463,10 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
if (chatCommand == "buff" && args.size() >= 2 && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER) {
auto* buffComponent = entity->GetComponent<BuffComponent>();
if (!buffComponent) {
ChatPackets::SendSystemMessage(sysAddr, u"Error, no buff component.");
return;
}
int32_t id = 0;
int32_t duration = 0;
@ -1477,9 +1481,10 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
return;
}
if (buffComponent != nullptr) {
buffComponent->ApplyBuff(id, duration, entity->GetObjectID());
}
Buff buff;
buff.id = id;
buff.duration = duration;
buffComponent->ApplyBuff(buff);
return;
}