Skip to content

Commit

Permalink
Always recalculate a battlefields group deathCount
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcmorris committed Sep 7, 2024
1 parent d79a273 commit f6ee490
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
85 changes: 37 additions & 48 deletions src/map/battlefield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,72 +902,61 @@ 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 = (CMobEntity*)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;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/map/battlefield.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ struct BattlefieldGroup
sol::function randomDeathCallback;
sol::function allDeathCallback;
sol::function setupCallback;
uint8 deathCount = 0;
uint32 randomMobId = 0;
};

Expand Down

0 comments on commit f6ee490

Please sign in to comment.