[7717] Use more safe code in EventAI.
[AHbot.git] / src / game / ReactorAI.cpp
blob0fd68dfe3ec2c29f9a651ed119d7091c783cc4c6
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "ByteBuffer.h"
20 #include "ReactorAI.h"
21 #include "Errors.h"
22 #include "Creature.h"
23 #include "Log.h"
24 #include "ObjectAccessor.h"
26 #define REACTOR_VISIBLE_RANGE (26.46f)
28 int
29 ReactorAI::Permissible(const Creature *creature)
31 if( creature->isCivilian() || creature->IsNeutralToAll() )
32 return PERMIT_BASE_REACTIVE;
34 return PERMIT_BASE_NO;
37 void
38 ReactorAI::MoveInLineOfSight(Unit *)
42 void
43 ReactorAI::AttackStart(Unit *p)
45 if(!p)
46 return;
48 if(m_creature->Attack(p,true))
50 DEBUG_LOG("Tag unit GUID: %u (TypeId: %u) as a victim", p->GetGUIDLow(), p->GetTypeId());
51 m_creature->SetInCombatWith(p);
52 p->SetInCombatWith(m_creature);
54 m_creature->AddThreat(p, 0.0f);
55 i_victimGuid = p->GetGUID();
56 m_creature->GetMotionMaster()->MoveChase(p);
60 bool
61 ReactorAI::IsVisible(Unit *) const
63 return false;
66 void
67 ReactorAI::UpdateAI(const uint32 /*time_diff*/)
69 // update i_victimGuid if i_creature.getVictim() !=0 and changed
70 if(!m_creature->SelectHostilTarget() || !m_creature->getVictim())
71 return;
73 i_victimGuid = m_creature->getVictim()->GetGUID();
75 if( m_creature->isAttackReady() )
77 if( m_creature->IsWithinDistInMap(m_creature->getVictim(), ATTACK_DISTANCE))
79 m_creature->AttackerStateUpdate(m_creature->getVictim());
80 m_creature->resetAttackTimer();
85 void
86 ReactorAI::EnterEvadeMode()
88 if( !m_creature->isAlive() )
90 DEBUG_LOG("Creature stoped attacking cuz his dead [guid=%u]", m_creature->GetGUIDLow());
91 m_creature->GetMotionMaster()->MovementExpired();
92 m_creature->GetMotionMaster()->MoveIdle();
93 i_victimGuid = 0;
94 m_creature->CombatStop(true);
95 m_creature->DeleteThreatList();
96 return;
99 Unit* victim = ObjectAccessor::GetUnit(*m_creature, i_victimGuid );
101 if( !victim )
103 DEBUG_LOG("Creature stopped attacking because victim is non exist [guid=%u]", m_creature->GetGUIDLow());
105 else if( victim->HasStealthAura() )
107 DEBUG_LOG("Creature stopped attacking cuz his victim is stealth [guid=%u]", m_creature->GetGUIDLow());
109 else if( victim->isInFlight() )
111 DEBUG_LOG("Creature stopped attacking cuz his victim is fly away [guid=%u]", m_creature->GetGUIDLow());
113 else
115 DEBUG_LOG("Creature stopped attacking due to target %s [guid=%u]", victim->isAlive() ? "out run him" : "is dead", m_creature->GetGUIDLow());
118 m_creature->RemoveAllAuras();
119 m_creature->DeleteThreatList();
120 i_victimGuid = 0;
121 m_creature->CombatStop(true);
122 m_creature->SetLootRecipient(NULL);
124 // Remove TargetedMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
125 if( m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE )
126 m_creature->GetMotionMaster()->MoveTargetedHome();