|
| 1 | +/* Copyright (C) 2006 - 2010 ScriptDev2 <https://scriptdev2.svn.sourceforge.net/> |
| 2 | + * This program is free software licensed under GPL version 2 |
| 3 | + * Please see the included DOCS/LICENSE.TXT for more information */ |
| 4 | + |
| 5 | +/* ScriptData |
| 6 | +SDName: ScriptedPetAI |
| 7 | +SD%Complete: 50 |
| 8 | +SDComment: Intended to be used with Guardian/Protector/Minipets. Little/no control over when pet enter/leave combat. Must be considered to be under development. |
| 9 | +SDCategory: Npc |
| 10 | +EndScriptData */ |
| 11 | + |
| 12 | +#include "precompiled.h" |
| 13 | +#include "pet_ai.h" |
| 14 | + |
| 15 | +ScriptedPetAI::ScriptedPetAI(Creature* pCreature) : CreatureAI(pCreature) |
| 16 | +{} |
| 17 | + |
| 18 | +void ScriptedPetAI::MoveInLineOfSight(Unit* pWho) |
| 19 | +{ |
| 20 | + if (m_creature->getVictim()) |
| 21 | + return; |
| 22 | + |
| 23 | + if (!m_creature->GetCharmInfo() || !m_creature->GetCharmInfo()->HasReactState(REACT_AGGRESSIVE)) |
| 24 | + return; |
| 25 | + |
| 26 | + if (m_creature->CanInitiateAttack() && pWho->isTargetableForAttack() && |
| 27 | + m_creature->IsHostileTo(pWho) && pWho->isInAccessablePlaceFor(m_creature)) |
| 28 | + { |
| 29 | + if (!m_creature->CanFly() && m_creature->GetDistanceZ(pWho) > CREATURE_Z_ATTACK_RANGE) |
| 30 | + return; |
| 31 | + |
| 32 | + if (m_creature->IsWithinDistInMap(pWho, m_creature->GetAttackDistance(pWho)) && m_creature->IsWithinLOSInMap(pWho)) |
| 33 | + { |
| 34 | + pWho->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH); |
| 35 | + AttackStart(pWho); |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +void ScriptedPetAI::AttackStart(Unit* pWho) |
| 41 | +{ |
| 42 | + if (pWho && m_creature->Attack(pWho, true)) |
| 43 | + m_creature->GetMotionMaster()->MoveChase(pWho); |
| 44 | +} |
| 45 | + |
| 46 | +void ScriptedPetAI::AttackedBy(Unit* pAttacker) |
| 47 | +{ |
| 48 | + if (m_creature->getVictim()) |
| 49 | + return; |
| 50 | + |
| 51 | + if (m_creature->GetCharmInfo() && !m_creature->GetCharmInfo()->HasReactState(REACT_PASSIVE) && |
| 52 | + m_creature->canReachWithAttack(pAttacker)) |
| 53 | + AttackStart(pAttacker); |
| 54 | +} |
| 55 | + |
| 56 | +void ScriptedPetAI::ResetPetCombat() |
| 57 | +{ |
| 58 | + Unit* pOwner = m_creature->GetCharmerOrOwner(); |
| 59 | + |
| 60 | + if (pOwner && m_creature->GetCharmInfo() && m_creature->GetCharmInfo()->HasCommandState(COMMAND_FOLLOW)) |
| 61 | + { |
| 62 | + m_creature->GetMotionMaster()->MoveFollow(pOwner, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + m_creature->GetMotionMaster()->Clear(false); |
| 67 | + m_creature->GetMotionMaster()->MoveIdle(); |
| 68 | + } |
| 69 | + |
| 70 | + m_creature->AttackStop(); |
| 71 | + |
| 72 | + debug_log("SD2: ScriptedPetAI reset pet combat and stop attack."); |
| 73 | + Reset(); |
| 74 | +} |
| 75 | + |
| 76 | +void ScriptedPetAI::DoMeleeAttackIfReady() |
| 77 | +{ |
| 78 | + if (m_creature->isAttackReady()) |
| 79 | + { |
| 80 | + if (m_creature->IsWithinDistInMap(m_creature->getVictim(), ATTACK_DISTANCE)) |
| 81 | + { |
| 82 | + m_creature->AttackerStateUpdate(m_creature->getVictim()); |
| 83 | + m_creature->resetAttackTimer(); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void ScriptedPetAI::UpdatePetAI(const uint32 uiDiff) |
| 89 | +{ |
| 90 | + DoMeleeAttackIfReady(); |
| 91 | +} |
| 92 | + |
| 93 | +void ScriptedPetAI::UpdateAI(const uint32 uiDiff) |
| 94 | +{ |
| 95 | + if (!m_creature->isAlive()) // should not be needed, isAlive is checked in mangos before calling UpdateAI |
| 96 | + return; |
| 97 | + |
| 98 | + // UpdateAllies() is done in the generic PetAI in Mangos, but we can't do this from script side. |
| 99 | + // Unclear what side effects this has, but is something to be resolved from Mangos. |
| 100 | + |
| 101 | + if (m_creature->getVictim()) // in combat |
| 102 | + { |
| 103 | + if (!m_creature->getVictim()->isTargetableForAttack()) |
| 104 | + { |
| 105 | + // target no longer valid for pet, so either attack stops or new target are selected |
| 106 | + // doesn't normally reach this, because of how petAi is designed in Mangos. CombatStop |
| 107 | + // are called before this update diff, and then pet will already have no victim. |
| 108 | + ResetPetCombat(); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + // update when in combat |
| 113 | + UpdatePetAI(uiDiff); |
| 114 | + } |
| 115 | + else if (m_creature->GetCharmInfo()) |
| 116 | + { |
| 117 | + Unit* pOwner = m_creature->GetCharmerOrOwner(); |
| 118 | + |
| 119 | + if (!pOwner) |
| 120 | + return; |
| 121 | + |
| 122 | + if (pOwner->isInCombat() && !m_creature->GetCharmInfo()->HasReactState(REACT_PASSIVE)) |
| 123 | + { |
| 124 | + // Not correct in all cases. |
| 125 | + // When mob initiate attack by spell, pet should not start attack before spell landed. |
| 126 | + AttackStart(pOwner->getAttackerForHelper()); |
| 127 | + } |
| 128 | + else if (m_creature->GetCharmInfo()->HasCommandState(COMMAND_FOLLOW)) |
| 129 | + { |
| 130 | + // not following, so start follow |
| 131 | + if (!m_creature->hasUnitState(UNIT_STAT_FOLLOW)) |
| 132 | + m_creature->GetMotionMaster()->MoveFollow(pOwner, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE); |
| 133 | + |
| 134 | + // update when not in combat |
| 135 | + UpdatePetOOCAI(uiDiff); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments