From 3cd0d1ec3df09b1cc741cd333760dd94a11c276b Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Fri, 10 Feb 2023 02:30:17 -0600 Subject: [PATCH] Make wrapper for casting skills (#987) * Make wrapper for casting skills this is to reduce magic numbers in the code base Only updated one use of this to demo that this works. Will be do more in a sepearate PR. Also, inadvertantly fix damage stacking and self-damage in the teslapack * add skill<->behavior caching * explicit by reference * address emo's feedback --- dGame/dComponents/SkillComponent.cpp | 26 +++++++++++++++++++ dGame/dComponents/SkillComponent.h | 14 ++++++++++ .../EquipmentTriggers/CoilBackpackBase.cpp | 4 +-- dScripts/EquipmentTriggers/CoilBackpackBase.h | 4 +-- dScripts/EquipmentTriggers/GemPack.h | 3 +-- dScripts/EquipmentTriggers/ShardArmor.h | 3 +-- dScripts/EquipmentTriggers/TeslaPack.h | 3 +-- 7 files changed, 46 insertions(+), 11 deletions(-) diff --git a/dGame/dComponents/SkillComponent.cpp b/dGame/dComponents/SkillComponent.cpp index 562c284a..2391dc3b 100644 --- a/dGame/dComponents/SkillComponent.cpp +++ b/dGame/dComponents/SkillComponent.cpp @@ -22,10 +22,13 @@ #include "EchoStartSkill.h" #include "dMessageIdentifiers.h" #include "DoClientProjectileImpact.h" +#include "CDClientManager.h" ProjectileSyncEntry::ProjectileSyncEntry() { } +std::unordered_map SkillComponent::m_skillBehaviorCache = {}; + bool SkillComponent::CastPlayerSkill(const uint32_t behaviorId, const uint32_t skillUid, RakNet::BitStream* bitStream, const LWOOBJID target, uint32_t skillID) { auto* context = new BehaviorContext(this->m_Parent->GetObjectID()); @@ -210,6 +213,29 @@ void SkillComponent::RegisterCalculatedProjectile(const LWOOBJID projectileId, B this->m_managedProjectiles.push_back(entry); } +bool SkillComponent::CastSkill(const uint32_t skillId, LWOOBJID target, const LWOOBJID optionalOriginatorID){ + uint32_t behaviorId = -1; + // try to find it via the cache + const auto& pair = m_skillBehaviorCache.find(skillId); + + // if it's not in the cache look it up and cache it + if (pair == m_skillBehaviorCache.end()) { + auto skillTable = CDClientManager::Instance()->GetTable("SkillBehavior"); + behaviorId = skillTable->GetSkillByID(skillId).behaviorID; + m_skillBehaviorCache.insert_or_assign(skillId, behaviorId); + } else { + behaviorId = pair->second; + } + + // check to see if we got back a valid behavior + if (behaviorId == -1) { + Game::logger->LogDebug("SkillComponent", "Tried to cast skill %i but found no behavior", skillId); + return false; + } + + return CalculateBehavior(skillId, behaviorId, target, false, false, optionalOriginatorID).success; +} + SkillExecutionResult SkillComponent::CalculateBehavior(const uint32_t skillId, const uint32_t behaviorId, const LWOOBJID target, const bool ignoreTarget, const bool clientInitalized, const LWOOBJID originatorOverride) { auto* bitStream = new RakNet::BitStream(); diff --git a/dGame/dComponents/SkillComponent.h b/dGame/dComponents/SkillComponent.h index f43276f1..2bdcb88f 100644 --- a/dGame/dComponents/SkillComponent.h +++ b/dGame/dComponents/SkillComponent.h @@ -119,6 +119,15 @@ public: */ void RegisterPlayerProjectile(LWOOBJID projectileId, BehaviorContext* context, const BehaviorBranchContext& branch, LOT lot); + /** + * Wrapper for CalculateBehavior that mimics the call structure in scripts and helps reduce magic numbers + * @param skillId the skill to cast + * @param target the target of the skill + * @param optionalOriginatorID change the originator of the skill + * @return if the case succeeded + */ + bool CastSkill(const uint32_t skillId, LWOOBJID target = LWOOBJID_EMPTY, const LWOOBJID optionalOriginatorID = LWOOBJID_EMPTY); + /** * Initializes a server-side skill calculation. * @param skillId the skill ID @@ -190,6 +199,11 @@ private: */ uint32_t m_skillUid; + /** + * Cache for looking up a behavior id via a skill ID + */ + static std::unordered_map m_skillBehaviorCache; + /** * Sync a server-side projectile calculation. * @param entry the projectile information diff --git a/dScripts/EquipmentTriggers/CoilBackpackBase.cpp b/dScripts/EquipmentTriggers/CoilBackpackBase.cpp index d3102e0e..4e323a08 100644 --- a/dScripts/EquipmentTriggers/CoilBackpackBase.cpp +++ b/dScripts/EquipmentTriggers/CoilBackpackBase.cpp @@ -14,10 +14,10 @@ void CoilBackpackBase::NotifyHitOrHealResult(Entity* self, Entity* attacker, int if (self->GetVar(u"coilCount") > 4) { auto* skillComponent = self->GetComponent(); if (!skillComponent) return; - skillComponent->CalculateBehavior(m_SkillId, m_BehaviorId, self->GetObjectID()); + skillComponent->CastSkill(m_SkillId); self->SetVar(u"coilCount", 0); } - } + } } void CoilBackpackBase::OnFactionTriggerItemUnequipped(Entity* itemOwner, LWOOBJID itemObjId) { diff --git a/dScripts/EquipmentTriggers/CoilBackpackBase.h b/dScripts/EquipmentTriggers/CoilBackpackBase.h index 290c6c0f..2d641346 100644 --- a/dScripts/EquipmentTriggers/CoilBackpackBase.h +++ b/dScripts/EquipmentTriggers/CoilBackpackBase.h @@ -5,9 +5,8 @@ class CoilBackpackBase: public CppScripts::Script { public: - CoilBackpackBase(uint32_t skillId, uint32_t behaviorId) { + CoilBackpackBase(uint32_t skillId) { m_SkillId = skillId; - m_BehaviorId = behaviorId; }; void OnFactionTriggerItemEquipped(Entity* itemOwner, LWOOBJID itemObjId) override; @@ -15,7 +14,6 @@ public: void OnFactionTriggerItemUnequipped(Entity* itemOwner, LWOOBJID itemObjId) override; private: uint32_t m_SkillId = 0; - uint32_t m_BehaviorId = 0; }; #endif //!__GemPackBase__H__ diff --git a/dScripts/EquipmentTriggers/GemPack.h b/dScripts/EquipmentTriggers/GemPack.h index 13bf7b0b..d71181ab 100644 --- a/dScripts/EquipmentTriggers/GemPack.h +++ b/dScripts/EquipmentTriggers/GemPack.h @@ -5,10 +5,9 @@ class GemPack : public CoilBackpackBase { public: - GemPack() : CoilBackpackBase(skillId, behaviorId) {}; + GemPack() : CoilBackpackBase(skillId) {}; private: static const uint32_t skillId = 1488; - static const uint32_t behaviorId = 36779; }; #endif //!__GEMPACK__H__ diff --git a/dScripts/EquipmentTriggers/ShardArmor.h b/dScripts/EquipmentTriggers/ShardArmor.h index 01d2fe33..5486db54 100644 --- a/dScripts/EquipmentTriggers/ShardArmor.h +++ b/dScripts/EquipmentTriggers/ShardArmor.h @@ -5,10 +5,9 @@ class ShardArmor : public CoilBackpackBase { public: - ShardArmor() : CoilBackpackBase(skillId, behaviorId) {}; + ShardArmor() : CoilBackpackBase(skillId) {}; private: static const uint32_t skillId = 1249; - static const uint32_t behaviorId = 29086; }; #endif //!__SHARDARMOR__H__ diff --git a/dScripts/EquipmentTriggers/TeslaPack.h b/dScripts/EquipmentTriggers/TeslaPack.h index 6e8bc9a4..3ba09f5c 100644 --- a/dScripts/EquipmentTriggers/TeslaPack.h +++ b/dScripts/EquipmentTriggers/TeslaPack.h @@ -5,10 +5,9 @@ class TeslaPack : public CoilBackpackBase { public: - TeslaPack() : CoilBackpackBase(skillId, behaviorId) {}; + TeslaPack() : CoilBackpackBase(skillId) {}; private: static const uint32_t skillId = 1001; - static const uint32_t behaviorId = 20917; }; #endif //!__TESLAPACK__H__