From b713b948418fd64909319667ab0a455b94bc6a7b Mon Sep 17 00:00:00 2001 From: EmosewaMC <39972741+EmosewaMC@users.noreply.github.com> Date: Tue, 26 Apr 2022 21:44:35 -0700 Subject: [PATCH] Added Loot Buff Behavior Added functions and methods to support the pickup radius. Functionality includes: Corrected serialization for ControllablePhysicsComponent which correctly serializes the pickup radius to the client. A method to add the pickup radius to the list of active pickup radii the component has. A method to remove and re-calculate the largest active radii the component currently has. Tested equipping all variations of the LootBuff behavior (passive skills, items, item skills) and all functioned as intended. Tested equipping multiple items with a loot buff and then unequipping them in different orders. Tested adding pickup radii of different values and the server correctly adjusted the pickup radius to the largest one currently equipped. --- dGame/dBehaviors/AndBehavior.cpp | 6 +++ dGame/dBehaviors/AndBehavior.h | 2 + dGame/dBehaviors/Behavior.cpp | 5 ++- dGame/dBehaviors/BehaviorContext.cpp | 4 +- dGame/dBehaviors/LootBuffBehavior.cpp | 38 ++++++++++++++++++ dGame/dBehaviors/LootBuffBehavior.h | 32 +++++++++++++++ .../ControllablePhysicsComponent.cpp | 40 ++++++++++++++++++- .../ControllablePhysicsComponent.h | 33 +++++++++++++++ 8 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 dGame/dBehaviors/LootBuffBehavior.cpp create mode 100644 dGame/dBehaviors/LootBuffBehavior.h diff --git a/dGame/dBehaviors/AndBehavior.cpp b/dGame/dBehaviors/AndBehavior.cpp index 231b39c9..5fc1e113 100644 --- a/dGame/dBehaviors/AndBehavior.cpp +++ b/dGame/dBehaviors/AndBehavior.cpp @@ -19,6 +19,12 @@ void AndBehavior::Calculate(BehaviorContext* context, RakNet::BitStream* bitStre } } +void AndBehavior::UnCast(BehaviorContext* context, const BehaviorBranchContext branch) { + for (auto behavior : this->m_behaviors) { + behavior->UnCast(context, branch); + } +} + void AndBehavior::Load() { const auto parameters = GetParameterNames(); diff --git a/dGame/dBehaviors/AndBehavior.h b/dGame/dBehaviors/AndBehavior.h index 2b7d95e6..9cbce569 100644 --- a/dGame/dBehaviors/AndBehavior.h +++ b/dGame/dBehaviors/AndBehavior.h @@ -20,5 +20,7 @@ public: void Calculate(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) override; + void UnCast(BehaviorContext* context, BehaviorBranchContext branch) override; + void Load() override; }; diff --git a/dGame/dBehaviors/Behavior.cpp b/dGame/dBehaviors/Behavior.cpp index 74153c11..db5bbf2f 100644 --- a/dGame/dBehaviors/Behavior.cpp +++ b/dGame/dBehaviors/Behavior.cpp @@ -18,6 +18,7 @@ #include "AreaOfEffectBehavior.h" #include "DurationBehavior.h" #include "TacArcBehavior.h" +#include "LootBuffBehavior.h" #include "AttackDelayBehavior.h" #include "BasicAttackBehavior.h" #include "ChainBehavior.h" @@ -172,7 +173,9 @@ Behavior* Behavior::CreateBehavior(const uint32_t behaviorId) behavior = new SpeedBehavior(behaviorId); break; case BehaviorTemplates::BEHAVIOR_DARK_INSPIRATION: break; - case BehaviorTemplates::BEHAVIOR_LOOT_BUFF: break; + case BehaviorTemplates::BEHAVIOR_LOOT_BUFF: + behavior = new LootBuffBehavior(behaviorId); + break; case BehaviorTemplates::BEHAVIOR_VENTURE_VISION: break; case BehaviorTemplates::BEHAVIOR_SPAWN_OBJECT: behavior = new SpawnBehavior(behaviorId); diff --git a/dGame/dBehaviors/BehaviorContext.cpp b/dGame/dBehaviors/BehaviorContext.cpp index fc67a82d..c7bf912f 100644 --- a/dGame/dBehaviors/BehaviorContext.cpp +++ b/dGame/dBehaviors/BehaviorContext.cpp @@ -64,8 +64,8 @@ void BehaviorContext::RegisterSyncBehavior(const uint32_t syncId, Behavior* beha void BehaviorContext::RegisterTimerBehavior(Behavior* behavior, const BehaviorBranchContext& branchContext, const LWOOBJID second) { - BehaviorTimerEntry entry -; + BehaviorTimerEntry entry; + entry.time = branchContext.duration; entry.behavior = behavior; entry.branchContext = branchContext; diff --git a/dGame/dBehaviors/LootBuffBehavior.cpp b/dGame/dBehaviors/LootBuffBehavior.cpp new file mode 100644 index 00000000..fe46f7bb --- /dev/null +++ b/dGame/dBehaviors/LootBuffBehavior.cpp @@ -0,0 +1,38 @@ +#include "LootBuffBehavior.h" + +void LootBuffBehavior::Handle(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) { + auto target = EntityManager::Instance()->GetEntity(context->caster); + if (!target) return; + + auto controllablePhysicsComponent = target->GetComponent(); + if (!controllablePhysicsComponent) return; + + controllablePhysicsComponent->AddPickupRadiusScale(m_Scale); + EntityManager::Instance()->SerializeEntity(target); + + if (branch.duration > 0) context->RegisterTimerBehavior(this, branch); + +} + +void LootBuffBehavior::Calculate(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) { + Handle(context, bitStream, branch); +} + +void LootBuffBehavior::UnCast(BehaviorContext* context, BehaviorBranchContext branch) { + auto target = EntityManager::Instance()->GetEntity(context->caster); + if (!target) return; + + auto controllablePhysicsComponent = target->GetComponent(); + if (!controllablePhysicsComponent) return; + + controllablePhysicsComponent->RemovePickupRadiusScale(m_Scale); + EntityManager::Instance()->SerializeEntity(target); +} + +void LootBuffBehavior::Timer(BehaviorContext* context, BehaviorBranchContext branch, LWOOBJID second) { + UnCast(context, branch); +} + +void LootBuffBehavior::Load() { + this->m_Scale = GetFloat("scale"); +} \ No newline at end of file diff --git a/dGame/dBehaviors/LootBuffBehavior.h b/dGame/dBehaviors/LootBuffBehavior.h new file mode 100644 index 00000000..0f85b3b0 --- /dev/null +++ b/dGame/dBehaviors/LootBuffBehavior.h @@ -0,0 +1,32 @@ +#pragma once +#include "Behavior.h" +#include "BehaviorBranchContext.h" +#include "BehaviorContext.h" +#include "ControllablePhysicsComponent.h" + +/** + * @brief This is the behavior class to be used for all Loot Buff behavior nodes in the Behavior tree. + * + */ +class LootBuffBehavior final : public Behavior +{ +public: + + float m_Scale; + + /* + * Inherited + */ + + explicit LootBuffBehavior(const uint32_t behaviorId) : Behavior(behaviorId) {} + + void Handle(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) override; + + void Calculate(BehaviorContext* context, RakNet::BitStream* bitStream, BehaviorBranchContext branch) override; + + void UnCast(BehaviorContext* context, BehaviorBranchContext branch) override; + + void Timer(BehaviorContext* context, BehaviorBranchContext branch, LWOOBJID second) override; + + void Load() override; +}; diff --git a/dGame/dComponents/ControllablePhysicsComponent.cpp b/dGame/dComponents/ControllablePhysicsComponent.cpp index 922ae55d..648b1471 100644 --- a/dGame/dComponents/ControllablePhysicsComponent.cpp +++ b/dGame/dComponents/ControllablePhysicsComponent.cpp @@ -29,6 +29,8 @@ ControllablePhysicsComponent::ControllablePhysicsComponent(Entity* entity) : Com m_GravityScale = 1; m_DirtyCheats = false; m_IgnoreMultipliers = false; + m_PickupRadius = 0.0f; + m_DirtyPickupRadiusScale = true; if (entity->GetLOT() != 1) // Other physics entities we care about will be added by BaseCombatAI return; @@ -85,7 +87,13 @@ void ControllablePhysicsComponent::Serialize(RakNet::BitStream* outBitStream, bo m_DirtyCheats = false; } - outBitStream->Write0(); + outBitStream->Write(m_DirtyPickupRadiusScale); + if (m_DirtyPickupRadiusScale) { + outBitStream->Write(m_PickupRadius); + outBitStream->Write0(); //No clue what this is so im leaving it false. + m_DirtyPickupRadiusScale = false; + } + outBitStream->Write0(); outBitStream->Write(m_DirtyPosition || bIsInitialUpdate); @@ -230,4 +238,32 @@ void ControllablePhysicsComponent::SetDirtyVelocity(bool val) { void ControllablePhysicsComponent::SetDirtyAngularVelocity(bool val) { m_DirtyAngularVelocity = val; -} \ No newline at end of file +} + +void ControllablePhysicsComponent::AddPickupRadiusScale(float value) { + m_ActivePickupRadiusScales.push_back(value); + if (value > m_PickupRadius) { + m_PickupRadius = value; + m_DirtyPickupRadiusScale = true; + } +} + +void ControllablePhysicsComponent::RemovePickupRadiusScale(float value) { + // Attempt to remove pickup radius from active radii + const auto pos = std::find(m_ActivePickupRadiusScales.begin(), m_ActivePickupRadiusScales.end(), value); + if (pos != m_ActivePickupRadiusScales.end()) { + m_ActivePickupRadiusScales.erase(pos); + } else { + Game::logger->Log("ControllablePhysicsComponent", "Warning: Could not find pickup radius %f in list of active radii. List has %i active radii.\n", value, m_ActivePickupRadiusScales.size()); + return; + } + + // Recalculate pickup radius since we removed one by now + m_PickupRadius = 0.0f; + m_DirtyPickupRadiusScale = true; + for (uint32_t i = 0; i < m_ActivePickupRadiusScales.size(); i++) { + auto candidateRadius = m_ActivePickupRadiusScales[i]; + if (m_PickupRadius < candidateRadius) m_PickupRadius = candidateRadius; + } + EntityManager::Instance()->SerializeEntity(m_Parent); +} diff --git a/dGame/dComponents/ControllablePhysicsComponent.h b/dGame/dComponents/ControllablePhysicsComponent.h index f0fe7461..c7acec62 100644 --- a/dGame/dComponents/ControllablePhysicsComponent.h +++ b/dGame/dComponents/ControllablePhysicsComponent.h @@ -227,6 +227,24 @@ public: dpEntity* GetdpEntity() const { return m_dpEntity; } + /** + * I store this in a vector because if I have 2 separate pickup radii being applied to the player, I dont know which one is correctly active. + * This method adds the pickup radius to the vector of active radii and if its larger than the current one, is applied as the new pickup radius. + */ + void AddPickupRadiusScale(float value) ; + + /** + * Removes the provided pickup radius scale from our list of buffs + * The recalculates what our pickup radius is. + */ + void RemovePickupRadiusScale(float value) ; + + /** + * The pickup radii of this component. + * @return All active radii scales for this component. + */ + std::vector GetActivePickupRadiusScales() { return m_ActivePickupRadiusScales; }; + private: /** * The entity that owns this component @@ -322,6 +340,21 @@ private: * Whether this entity is static, making it unable to move */ bool m_Static; + + /** + * Whether the pickup scale is dirty. + */ + bool m_DirtyPickupRadiusScale; + + /** + * The list of pickup radius scales for this entity + */ + std::vector m_ActivePickupRadiusScales; + + /** + * The active pickup radius for this entity + */ + float m_PickupRadius; }; #endif // CONTROLLABLEPHYSICSCOMPONENT_H