From c9929f1c1e0cb1ee68d245c7e885d745a2906d58 Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Fri, 2 Oct 2026 09:56:05 -0500 Subject: [PATCH] fix(skill): a start with a skill handle still in progress is dropped, as the client LWOSkillComponent::msgStartSkill (0x00d45cb0) logs and ignores a StartSkill whose skill handle is still waiting on behavior syncs. The server ran it a second time: the hits, the imagination cost and the mission progress twice. Live captures hold 22 byte-identical StartSkill resends with the same handle a few packets apart. Co-Authored-By: Claude Opus 5.5 --- dGame/dComponents/SkillComponent.cpp | 12 ++++ dGame/dComponents/SkillComponent.h | 5 ++ dGame/dGameMessages/SkillMessages.cpp | 4 ++ .../dComponentsTests/CMakeLists.txt | 1 + .../dComponentsTests/SkillComponentTests.cpp | 62 +++++++++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 tests/dGameTests/dComponentsTests/SkillComponentTests.cpp diff --git a/dGame/dComponents/SkillComponent.cpp b/dGame/dComponents/SkillComponent.cpp index c8b0c5feb..c600e93da 100644 --- a/dGame/dComponents/SkillComponent.cpp +++ b/dGame/dComponents/SkillComponent.cpp @@ -7,6 +7,7 @@ #include "GeneralUtils.h" #include "tinyxml2.h" +#include #include #include @@ -34,7 +35,18 @@ ProjectileSyncEntry::ProjectileSyncEntry() { std::unordered_map SkillComponent::m_skillBehaviorCache = {}; +bool SkillComponent::IsSkillPending(const uint32_t skillUid) const { + const auto [begin, end] = m_managedBehaviors.equal_range(skillUid); + return std::any_of(begin, end, [](const auto& pair) { return pair.second && !pair.second->syncEntries.empty(); }); +} + bool SkillComponent::CastPlayerSkill(const uint32_t behaviorId, const uint32_t skillUid, RakNet::BitStream& bitStream, const LWOOBJID target, uint32_t skillID) { + // A skill handle still waiting on syncs is taken: the client logs and drops a second start with it + if (IsSkillPending(skillUid)) { + LOG("StartSkill with existing skill handle %u for %llu", skillUid, m_Parent->GetObjectID()); + return false; + } + auto* context = new BehaviorContext(this->m_Parent->GetObjectID()); context->caster = m_Parent->GetObjectID(); diff --git a/dGame/dComponents/SkillComponent.h b/dGame/dComponents/SkillComponent.h index ebc8ad935..bfda7708c 100644 --- a/dGame/dComponents/SkillComponent.h +++ b/dGame/dComponents/SkillComponent.h @@ -126,6 +126,11 @@ public: */ bool HasSkill(uint32_t skillUid) const { return m_managedBehaviors.contains(skillUid); } + /** + * @return whether a skill with that handle still waits for the client to sync one of its behaviors + */ + bool IsSkillPending(uint32_t skillUid) const; + /** * Starts a player skill. Should only be called when the server receives a start skill message from the client. * @param behaviorId the root behavior ID of the skill diff --git a/dGame/dGameMessages/SkillMessages.cpp b/dGame/dGameMessages/SkillMessages.cpp index dc0a72f5c..98f826d7e 100644 --- a/dGame/dGameMessages/SkillMessages.cpp +++ b/dGame/dGameMessages/SkillMessages.cpp @@ -137,6 +137,10 @@ namespace GameMessages { Entity* entity = &entityRef; if (skillID == 1561 || skillID == 1562 || skillID == 1541) return; + // LWOSkillComponent::msgStartSkill (0x00d45cb0): a start with a skill handle still in progress is dropped + const auto* const castingSkills = entity->GetComponent(); + if (castingSkills && castingSkills->IsSkillPending(uiSkillHandle)) return; + MissionComponent* comp = entity->GetComponent(); if (comp) { comp->Progress(eMissionTaskType::USE_SKILL, skillID); diff --git a/tests/dGameTests/dComponentsTests/CMakeLists.txt b/tests/dGameTests/dComponentsTests/CMakeLists.txt index 57665c224..6bc704a03 100644 --- a/tests/dGameTests/dComponentsTests/CMakeLists.txt +++ b/tests/dGameTests/dComponentsTests/CMakeLists.txt @@ -25,6 +25,7 @@ set(DCOMPONENTS_TESTS "UseItemResultTests.cpp" "LootMetricsTests.cpp" "SpawnerComponentTests.cpp" + "SkillComponentTests.cpp" ) # Get the folder name and prepend it to the files above diff --git a/tests/dGameTests/dComponentsTests/SkillComponentTests.cpp b/tests/dGameTests/dComponentsTests/SkillComponentTests.cpp new file mode 100644 index 000000000..d7473d158 --- /dev/null +++ b/tests/dGameTests/dComponentsTests/SkillComponentTests.cpp @@ -0,0 +1,62 @@ +#include "GameDependencies.h" + +#include "Behavior.h" +#include "BehaviorBranchContext.h" +#include "BehaviorContext.h" +#include "Entity.h" +#include "SkillComponent.h" + +#include + +namespace { + // A behavior that waits for one client sync (handle 7) and counts the syncs it gets + class WaitsForSync final : public Behavior { + public: + static constexpr uint32_t SYNC_HANDLE = 7; + int syncs = 0; + + explicit WaitsForSync(const uint32_t behaviorId) : Behavior(behaviorId) {} + + void Handle(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) override { + context->RegisterSyncBehavior(SYNC_HANDLE, this, branch, 10.0f); + } + + void Sync(BehaviorContext* context, RakNet::BitStream& bitStream, BehaviorBranchContext branch) override { ++syncs; } + }; +} + +class SkillComponentTests : public GameDependenciesTest { +protected: + static constexpr uint32_t BEHAVIOR_ID = 990001; + WaitsForSync* behavior = nullptr; + + void SetUp() override { + SetUpDependencies(); + behavior = new WaitsForSync(BEHAVIOR_ID); + } + + void TearDown() override { + Behavior::Cache.erase(BEHAVIOR_ID); + delete behavior; + TearDownDependencies(); + } +}; + +// LWOSkillComponent::msgStartSkill (0x00d45cb0): a start whose skill handle still waits on syncs is logged and dropped; +// once its behaviors are synced the handle can start a skill again. +TEST_F(SkillComponentTests, StartWithAPendingSkillHandleIsDropped) { + Entity caster(30, info); + auto* const skills = caster.AddComponent(-1); + RakNet::BitStream empty; + + EXPECT_TRUE(skills->CastPlayerSkill(BEHAVIOR_ID, 5, empty, LWOOBJID_EMPTY)); + EXPECT_TRUE(skills->IsSkillPending(5)); + EXPECT_FALSE(skills->CastPlayerSkill(BEHAVIOR_ID, 5, empty, LWOOBJID_EMPTY)); + EXPECT_TRUE(skills->CastPlayerSkill(BEHAVIOR_ID, 6, empty, LWOOBJID_EMPTY)); // another handle is free + + skills->SyncPlayerSkill(5, WaitsForSync::SYNC_HANDLE, empty); + EXPECT_EQ(behavior->syncs, 1); // the dropped start registered no second sync + EXPECT_FALSE(skills->IsSkillPending(5)); + EXPECT_TRUE(skills->CastPlayerSkill(BEHAVIOR_ID, 5, empty, LWOOBJID_EMPTY)); +} +