Skip to content

Commit

Permalink
fix(Core/Scripts): Further improve the ScheduleHealthCheck() function (
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyeriah authored Oct 9, 2024
1 parent 0fa770d commit 31cb757
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/server/game/AI/ScriptedAI/ScriptedCreature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,8 @@ Player* ScriptedAI::SelectTargetFromPlayerList(float maxdist, uint32 excludeAura
BossAI::BossAI(Creature* creature, uint32 bossId) : ScriptedAI(creature),
instance(creature->GetInstanceScript()),
summons(creature),
_bossId(bossId)
_bossId(bossId),
_nextHealthCheck(0, { }, false)
{
callForHelpRange = 0.0f;
if (instance)
Expand Down Expand Up @@ -733,22 +734,20 @@ void BossAI::UpdateAI(uint32 diff)

void BossAI::DamageTaken(Unit* /*attacker*/, uint32& damage, DamageEffectType /*damagetype*/, SpellSchoolMask /*damageSchoolMask*/)
{
if (!_healthCheckEvents.empty())
{
for (auto& check : _healthCheckEvents)
if (_nextHealthCheck._valid)
if (me->HealthBelowPctDamaged(_nextHealthCheck._healthPct, damage))
{
if (check._valid && me->HealthBelowPctDamaged(check._healthPct, damage))
_nextHealthCheck._exec();
_nextHealthCheck._valid = false;

_healthCheckEvents.remove_if([&](HealthCheckEventData data) -> bool
{
check._exec();
check._valid = false;
}
}
return data._healthPct == _nextHealthCheck._healthPct;
});

_healthCheckEvents.remove_if([&](HealthCheckEventData data) -> bool
{
return !data._valid;
});
}
if (!_healthCheckEvents.empty())
_nextHealthCheck = _healthCheckEvents.front();
}
}

/**
Expand All @@ -760,6 +759,7 @@ void BossAI::DamageTaken(Unit* /*attacker*/, uint32& damage, DamageEffectType /*
void BossAI::ScheduleHealthCheckEvent(uint32 healthPct, std::function<void()> exec)
{
_healthCheckEvents.push_back(HealthCheckEventData(healthPct, exec));
_nextHealthCheck = _healthCheckEvents.front();
};

void BossAI::ScheduleHealthCheckEvent(std::initializer_list<uint8> healthPct, std::function<void()> exec)
Expand All @@ -768,6 +768,8 @@ void BossAI::ScheduleHealthCheckEvent(std::initializer_list<uint8> healthPct, st
{
_healthCheckEvents.push_back(HealthCheckEventData(checks, exec));
}

_nextHealthCheck = _healthCheckEvents.front();
}

// WorldBossAI - for non-instanced bosses
Expand Down
1 change: 1 addition & 0 deletions src/server/game/AI/ScriptedAI/ScriptedCreature.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ class BossAI : public ScriptedAI
private:
uint32 const _bossId;
std::list<HealthCheckEventData> _healthCheckEvents;
HealthCheckEventData _nextHealthCheck;
};

class WorldBossAI : public ScriptedAI
Expand Down

0 comments on commit 31cb757

Please sign in to comment.