Skip to content

Commit

Permalink
refactor(Core/BossAI): Improve the ScheduleHealthCheck() code (azerot…
Browse files Browse the repository at this point in the history
…hcore#19743)

* refactor(Core/BossAI): Improve the ScheduleHealthCheck() code

* Update ScriptedCreature.cpp

---------

Co-authored-by: sudlud <[email protected]>
  • Loading branch information
Nyeriah and sudlud authored Aug 26, 2024
1 parent 4ed1815 commit 83f8871
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
22 changes: 10 additions & 12 deletions src/server/game/AI/ScriptedAI/ScriptedCreature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,18 @@ void BossAI::DamageTaken(Unit* /*attacker*/, uint32& damage, DamageEffectType /*
{
if (!_healthCheckEvents.empty())
{
for (auto& check : _healthCheckEvents)
{
if (check._valid && me->HealthBelowPctDamaged(check._healthPct, damage))
{
check._exec();
check._valid = false;
}
}

_healthCheckEvents.remove_if([&](HealthCheckEventData data) -> bool
{
return _ProccessHealthCheckEvent(data._healthPct, damage, data._exec);
return !data._valid;
});
}
}
Expand All @@ -746,17 +755,6 @@ void BossAI::ScheduleHealthCheckEvent(std::initializer_list<uint8> healthPct, st
}
}

bool BossAI::_ProccessHealthCheckEvent(uint8 healthPct, uint32 damage, std::function<void()> exec) const
{
if (me->HealthBelowPctDamaged(healthPct, damage))
{
exec();
return true;
}

return false;
}

// WorldBossAI - for non-instanced bosses

WorldBossAI::WorldBossAI(Creature* creature) :
Expand Down
4 changes: 2 additions & 2 deletions src/server/game/AI/ScriptedAI/ScriptedCreature.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,11 @@ struct ScriptedAI : public CreatureAI

struct HealthCheckEventData
{
HealthCheckEventData(uint8 healthPct, std::function<void()> exec) : _healthPct(healthPct), _exec(exec) { };
HealthCheckEventData(uint8 healthPct, std::function<void()> exec, bool valid = true) : _healthPct(healthPct), _exec(exec), _valid(valid) { };

uint8 _healthPct;
std::function<void()> _exec;
bool _valid;
};

class BossAI : public ScriptedAI
Expand Down Expand Up @@ -497,7 +498,6 @@ class BossAI : public ScriptedAI
void _JustDied();
void _JustReachedHome() { me->setActive(false); }
void _EnterEvadeMode(EvadeReason why = EVADE_REASON_OTHER);
[[nodiscard]] bool _ProccessHealthCheckEvent(uint8 healthPct, uint32 damage, std::function<void()> exec) const;

void TeleportCheaters();

Expand Down

0 comments on commit 83f8871

Please sign in to comment.