fix(Scripts/Pet): Port TrinityCore snake trap AI to use CombatManager (#25232)

Co-authored-by: blinkysc <blinkysc@users.noreply.github.com>
Co-authored-by: Treeston <treeston.mmoc@gmail.com>
This commit is contained in:
blinkysc
2026-03-25 14:13:35 -05:00
committed by GitHub
parent 9c7cad84c6
commit 2f3a90e844
2 changed files with 269 additions and 74 deletions

View File

@@ -20,12 +20,15 @@
* Scriptnames of files in this file should be prefixed with "npc_pet_hun_".
*/
#include "CombatManager.h"
#include "Containers.h"
#include "CreatureScript.h"
#include "PetDefines.h"
#include "ScriptedCreature.h"
#include "SpellAuraEffects.h"
#include "SpellScript.h"
#include "SpellScriptLoader.h"
#include "TemporarySummon.h"
enum HunterSpells
{
@@ -50,99 +53,94 @@ enum PetSpellsMisc
struct npc_pet_hunter_snake_trap : public ScriptedAI
{
npc_pet_hunter_snake_trap(Creature* creature) : ScriptedAI(creature) { _init = false; }
npc_pet_hunter_snake_trap(Creature* creature) : ScriptedAI(creature), _isViper(false), _spellTimer(0) { }
void Reset() override
void JustEngagedWith(Unit* /*who*/) override { }
void InitializeAI() override
{
_spellTimer = urand(1500, 3000);
_isViper = me->GetEntry() == NPC_VIPER;
// Start attacking attacker of owner on first ai update after spawn - move in line of sight may choose better target
if (!me->GetVictim())
if (Unit* tgt = me->SelectNearestTarget(10.0f))
{
me->AddThreat(tgt, 100000.0f);
AttackStart(tgt);
}
me->SetMaxHealth(uint32(107 * (me->GetLevel() - 40) * 0.025f));
// Add delta to make them not all hit the same time
me->SetAttackTime(BASE_ATTACK, me->GetAttackTime(BASE_ATTACK) + urandms(0, 6));
if (!_isViper && !me->HasAura(SPELL_HUNTER_DEADLY_POISON_PASSIVE))
DoCast(me, SPELL_HUNTER_DEADLY_POISON_PASSIVE, true);
// Glyph of Snake Trap — apply AoE damage reduction scaling
if (Unit* owner = me->GetOwner())
if (owner->GetAuraEffectDummy(SPELL_HUNTER_GLYPH_OF_SNAKE_TRAP))
me->CastSpell(me, SPELL_HUNTER_PET_SCALING, true);
}
void EnterEvadeMode(EvadeReason /*why*/) override
{
// _EnterEvadeMode();
me->AddUnitState(UNIT_STATE_EVADE);
me->GetThreatMgr().ClearAllThreat();
me->CombatStop(true);
me->LoadCreaturesAddon(true);
me->SetLootRecipient(nullptr);
me->ResetPlayerDamageReq();
me->ClearLastLeashExtensionTimePtr();
me->GetMotionMaster()->MoveTargetedHome();
Reset();
}
//Redefined for random target selection:
void MoveInLineOfSight(Unit* who) override
{
if (!me->GetVictim() && who->isTargetableForAttack() && (me->IsHostileTo(who)) && who->isInAccessiblePlaceFor(me))
{
if (me->GetDistanceZ(who) > CREATURE_Z_ATTACK_RANGE)
return;
if (me->IsWithinDistInMap(who, 10.0f))
{
me->AddThreat(who, 100000.0f);
AttackStart(who);
}
}
}
// Redefined for random target selection:
void MoveInLineOfSight(Unit* /*who*/) override { }
void UpdateAI(uint32 diff) override
{
if (me->GetVictim() && me->GetVictim()->HasBreakableByDamageCrowdControlAura(me))
{
me->GetThreatMgr().ClearFixate();
me->InterruptNonMeleeSpells(false);
me->AttackStop();
return;
}
if (me->IsSummon() && !me->GetThreatMgr().GetFixateTarget())
{
Unit* summoner = me->ToTempSummon()->GetSummonerUnit();
if (summoner)
{
std::vector<Unit*> targets;
auto addTargetIfValid = [this, &targets, summoner](CombatReference* ref) mutable
{
Unit* enemy = ref->GetOther(summoner);
if (!enemy->HasBreakableByDamageCrowdControlAura(me) && me->CanCreatureAttack(enemy) && me->IsWithinDistInMap(enemy, me->GetAttackDistance(enemy)))
targets.push_back(enemy);
};
for (auto const& [guid, ref] : summoner->GetCombatManager().GetPvPCombatRefs())
addTargetIfValid(ref);
if (targets.empty())
for (auto const& [guid, ref] : summoner->GetCombatManager().GetPvECombatRefs())
addTargetIfValid(ref);
for (Unit* target : targets)
me->EngageWithTarget(target);
if (!targets.empty())
{
Unit* target = Acore::Containers::SelectRandomContainerElement(targets);
me->GetThreatMgr().FixateTarget(target);
}
}
}
if (!UpdateVictim())
return;
if (me->GetVictim()->HasBreakableByDamageCrowdControlAura(me))
// Viper
if (_isViper)
{
me->InterruptNonMeleeSpells(false);
return;
}
if (_spellTimer <= diff)
{
if (!urand(0, 2)) // 33% chance to cast
DoCastVictim(RAND(SPELL_HUNTER_MIND_NUMBING_POISON, SPELL_HUNTER_CRIPPLING_POISON));
if (!_init)
{
_init = true;
uint32 health = uint32(107 * (me->GetLevel() - 40) * 0.025f);
me->SetCreateHealth(health);
me->SetStatFlatModifier(UNIT_MOD_HEALTH, BASE_VALUE, (float)health);
me->SetMaxHealth(health);
//Add delta to make them not all hit the same time
uint32 delta = urand(0, 700);
me->SetAttackTime(BASE_ATTACK, me->GetAttackTime(BASE_ATTACK) + delta);
if (me->GetEntry() == NPC_VENOMOUS_SNAKE)
DoCastSelf(SPELL_HUNTER_DEADLY_POISON_PASSIVE, true);
// Glyph of Snake Trap
if (Unit* owner = me->GetOwner())
if (owner->GetAuraEffectDummy(SPELL_HUNTER_GLYPH_OF_SNAKE_TRAP))
me->CastSpell(me, SPELL_HUNTER_PET_SCALING, true);
}
_spellTimer += diff;
if (_spellTimer >= 3000)
{
if (urand(0, 2) == 0) // 33% chance to cast
DoCastVictim(RAND(SPELL_HUNTER_MIND_NUMBING_POISON, SPELL_HUNTER_CRIPPLING_POISON));
_spellTimer = 0;
_spellTimer = 3000;
}
else
_spellTimer -= diff;
}
DoMeleeAttackIfReady();
}
private:
bool _init;
bool _isViper;
uint32 _spellTimer;
};