From d297d6c047dc46f40a1edee5160d36cc01c47473 Mon Sep 17 00:00:00 2001 From: Jasson McMorris Date: Fri, 6 Sep 2024 19:50:43 -0700 Subject: [PATCH] Always recalculate a battlefields group deathCount --- src/map/battlefield.cpp | 86 ++++++++++++++++++----------------------- src/map/battlefield.h | 1 - 2 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/map/battlefield.cpp b/src/map/battlefield.cpp index 02200b003d2..bb49f69f90f 100644 --- a/src/map/battlefield.cpp +++ b/src/map/battlefield.cpp @@ -902,72 +902,62 @@ void CBattlefield::handleDeath(CBaseEntity* PEntity) return; } - for (auto& group : m_groups) + // Create a copy of groups since m_groups may change during the callbacks + auto groups(m_groups); + + for (auto& group : groups) { - for (uint32 mobId : group.mobIds) + + // Calculate the total mobs that are dead for this group + uint8 deathCount = 0; + + for (uint32 mobID : group.mobIds) { - if (mobId == PEntity->id) + CMobEntity* PMob = dynamic_cast(zoneutils::GetEntity(mobID, TYPE_MOB | TYPE_PET)); + if (PMob == nullptr || PMob->isDead()) { - ++group.deathCount; - - break; + ++deathCount; } } - } - auto groups(m_groups); - - for (auto& group : groups) - { for (uint32 mobId : group.mobIds) { - if (mobId == PEntity->id) + if (mobId != PEntity->id) + { + continue; + } + + if (group.deathCallback.valid()) { - if (group.deathCallback.valid()) + auto result = group.deathCallback(CLuaBattlefield(this), CLuaBaseEntity(PEntity), deathCount); + if (!result.valid()) { - auto result = group.deathCallback(CLuaBattlefield(this), CLuaBaseEntity(PEntity), group.deathCount); - if (!result.valid()) - { - sol::error err = result; - ShowError("Error in battlefield %s group.death: %s", this->GetName(), err.what()); - } + sol::error err = result; + ShowError("Error in battlefield %s group.death: %s", this->GetName(), err.what()); } + } - if (group.allDeathCallback.valid() && group.deathCount >= group.mobIds.size()) + if (group.allDeathCallback.valid() && deathCount >= group.mobIds.size()) + { + auto result = group.allDeathCallback(CLuaBattlefield(this), CLuaBaseEntity(PEntity)); + if (!result.valid()) { - // Validate all mobs in the group are dead since they may have been revived - uint16 deathCount = 0; - for (auto& deathMobId : group.mobIds) - { - CMobEntity* PMob = (CMobEntity*)zoneutils::GetEntity(deathMobId, TYPE_MOB | TYPE_PET); - if (PMob != nullptr && PMob->isDead()) - { - ++deathCount; - } - } - - if (deathCount == group.mobIds.size()) - { - auto result = group.allDeathCallback(CLuaBattlefield(this), CLuaBaseEntity(PEntity)); - if (!result.valid()) - { - sol::error err = result; - ShowError("Error in battlefield %s group.allDeath: %s", this->GetName(), err.what()); - } - } + sol::error err = result; + ShowError("Error in battlefield %s group.allDeath: %s", this->GetName(), err.what()); } + } - if (group.randomDeathCallback.valid() && mobId == group.randomMobId) + if (group.randomDeathCallback.valid() && mobId == group.randomMobId) + { + auto result = group.randomDeathCallback(CLuaBattlefield(this), CLuaBaseEntity(PEntity)); + if (!result.valid()) { - auto result = group.randomDeathCallback(CLuaBattlefield(this), CLuaBaseEntity(PEntity)); - if (!result.valid()) - { - sol::error err = result; - ShowError("Error in battlefield %s group.randomDeath: %s", this->GetName(), err.what()); - } + sol::error err = result; + ShowError("Error in battlefield %s group.randomDeath: %s", this->GetName(), err.what()); } - break; } + + break; } } } diff --git a/src/map/battlefield.h b/src/map/battlefield.h index e1dd14f97d9..ae648c11016 100644 --- a/src/map/battlefield.h +++ b/src/map/battlefield.h @@ -123,7 +123,6 @@ struct BattlefieldGroup sol::function randomDeathCallback; sol::function allDeathCallback; sol::function setupCallback; - uint8 deathCount = 0; uint32 randomMobId = 0; };