[2771] Applied MaNGOS coding style (see trunk/bcpp.cfg).
[mangos-git.git] / src / game / AggressorAI.cpp
blobd08defa36506f68fe100be3183c3c61d43e54567
1 /*
2 * Copyright (C) 2005,2006 MaNGOS <http://www.mangosproject.org/>
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 "AggressorAI.h"
20 #include "Errors.h"
21 #include "Creature.h"
22 #include "Player.h"
23 #include "Utilities.h"
24 #include "TargetedMovementGenerator.h"
25 #include "Database/DBCStores.h"
26 #include <list>
28 int
29 AggressorAI::Permissible(const Creature *creature)
31 // have some hostile factions, it will be selected by IsHostileTo check at MoveInLineOfSight
32 if( !creature->IsNeutralToAll() )
33 return PERMIT_BASE_PROACTIVE;
35 return PERMIT_BASE_NO;
38 AggressorAI::AggressorAI(Creature &c) : i_creature(c), i_victimGuid(0), i_state(STATE_NORMAL), i_tracker(TIME_INTERVAL_LOOK)
42 void
43 AggressorAI::MoveInLineOfSight(Unit *u)
45 if( !i_creature.getVictim() && u->isTargetableForAttack() && IsVisible(u) && u->isInAccessablePlaceFor(&i_creature))
47 float attackRadius = i_creature.GetAttackDistance(u);
48 if(i_creature.IsWithinDist(u, attackRadius) && i_creature.GetDistanceZ(u) <= CREATURE_Z_ATTACK_RANGE)
50 if( i_creature.IsHostileTo( u ) )
52 AttackStart(u);
53 if(u->HasStealthAura())
54 u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
60 void
61 AggressorAI::HealBy(Unit *healer, uint32 amount_healed)
65 void
66 AggressorAI::DamageInflict(Unit *healer, uint32 amount_healed)
70 bool
71 AggressorAI::_needToStop() const
73 if( !i_creature.isAlive() || !i_creature.getVictim() || !i_creature.getVictim()->isTargetableForAttack() )
74 return true;
76 if(!i_creature.getVictim()->isInAccessablePlaceFor(&i_creature))
77 return true;
79 float rx,ry,rz;
80 i_creature.GetRespawnCoord(rx, ry, rz);
81 float spawndist=i_creature.GetDistanceSq(rx,ry,rz);
82 float length = i_creature.GetDistanceSq(i_creature.getVictim());
83 float hostillen=i_creature.GetHostilityDistance( i_creature.getVictim()->GetGUID() );
84 return (( length > (10.0f + hostillen) * (10.0f + hostillen) && spawndist > 6400.0f )
85 || ( length > (20.0f + hostillen) * (20.0f + hostillen) && spawndist > 2500.0f )
86 || ( length > (30.0f + hostillen) * (30.0f + hostillen) ));
89 void AggressorAI::AttackStop(Unit *)
93 void AggressorAI::_stopAttack()
95 if( !i_creature.isAlive() )
97 DEBUG_LOG("Creature stopped attacking cuz his dead [guid=%u]", i_creature.GetGUIDLow());
98 i_victimGuid = 0;
99 i_creature.CombatStop();
100 return;
103 assert( i_victimGuid );
105 Unit* victim = ObjectAccessor::Instance().GetUnit(i_creature, i_victimGuid );
107 if( !victim )
109 DEBUG_LOG("Creature stopped attacking because victim is non exist [guid=%u]", i_creature.GetGUIDLow());
111 else if( !victim->isAlive() )
113 DEBUG_LOG("Creature stopped attacking cuz his victim is dead [guid=%u]", i_creature.GetGUIDLow());
115 else if( victim->HasStealthAura() )
117 DEBUG_LOG("Creature stopped attacking cuz his victim is stealth [guid=%u]", i_creature.GetGUIDLow());
119 else if( victim->isInFlight() )
121 DEBUG_LOG("Creature stopped attacking cuz his victim is fly away [guid=%u]", i_creature.GetGUIDLow());
123 else
125 DEBUG_LOG("Creature stopped attacking due to target out run him [guid=%u]", i_creature.GetGUIDLow());
126 //i_state = STATE_LOOK_AT_VICTIM;
127 //i_tracker.Reset(TIME_INTERVAL_LOOK);
130 i_victimGuid = 0;
131 i_creature.AttackStop();
133 // Remove TargetedMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
134 if( i_creature->top()->GetMovementGeneratorType() == TARGETED_MOTION_TYPE )
135 i_creature->TargetedHome();
138 void
139 AggressorAI::UpdateAI(const uint32 diff)
141 // update i_victimGuid if i_creature.getVictim() !=0 and changed
142 if(i_creature.getVictim())
143 i_victimGuid = i_creature.getVictim()->GetGUID();
145 // i_creature.getVictim() can't be used for check in case stop fighting, i_creature.getVictim() clearóâ at Unit death etc.
146 if( i_victimGuid )
148 if( _needToStop() )
150 DEBUG_LOG("Aggressor AI stoped attacking [guid=%u]", i_creature.GetGUIDLow());
151 _stopAttack(); // i_victimGuid == 0 && i_creature.getVictim() == NULL now
152 return;
155 assert((i_victimGuid != 0) == (i_creature.getVictim() != NULL) && "i_victimGuid and i_creature.getVictim() not synchronized.");
157 //switch( i_state )
159 /*case STATE_LOOK_AT_VICTIM:
161 if( IsVisible(i_pVictim) )
163 DEBUG_LOG("Victim %u re-enters creature's aggro radius fater stop attacking", i_pVictim->GetGUIDLow());
164 i_state = STATE_NORMAL;
165 i_creature->MovementExpired();
166 break; // move on
167 // back to the cat and mice game if you move back in range
170 i_tracker.Update(diff);
171 if( i_tracker.Passed() )
173 i_creature->MovementExpired();
174 DEBUG_LOG("Creature running back home [guid=%u]", i_creature.GetGUIDLow());
175 if( i_creature->top()->GetMovementGeneratorType() == MovementGenerator::TARGETED_MOTION_TYPE )
176 static_cast<TargetedMovementGenerator *>(i_creature->top())->TargetedHome(i_creature);
177 i_state = STATE_NORMAL;
178 i_pVictim = NULL;
180 else
183 float dx = i_pVictim->GetPositionX() - i_creature.GetPositionX();
184 float dy = i_pVictim->GetPositionY() - i_creature.GetPositionY();
185 float orientation = (float)atan2((double)dy, (double)dx);
186 i_creature.Relocate(i_pVictim->GetPositionX(), i_pVictim->GetPositionY(), i_pVictim->GetPositionZ(), orientation);
189 break;
191 //case STATE_NORMAL:
193 if( i_creature.IsStopped() )
195 if( i_creature.isAttackReady() )
197 Unit* newtarget = i_creature.SelectHostilTarget();
198 if(newtarget)
199 AttackStart(newtarget);
201 if(!i_creature.canReachWithAttack(i_creature.getVictim()))
202 return;
203 i_creature.AttackerStateUpdate(i_creature.getVictim());
204 i_creature.resetAttackTimer();
206 if ( !i_creature.getVictim() )
207 return;
209 if( _needToStop() )
210 _stopAttack();
213 // break;
215 //default:
216 // break;
221 bool
222 AggressorAI::IsVisible(Unit *pl) const
224 bool seestealth = true;
225 uint32 sight = sWorld.getConfig(CONFIG_SIGHT_MONSTER);
226 float dist = i_creature.GetDistanceSq(pl);
227 if(pl->HasStealthAura())
229 int32 seevaluse;
230 int notfront = i_creature.isInFront(pl, sight) ? 0 : 1;
231 seevaluse = 5 + i_creature.getLevel() * 5 + i_creature.m_detectStealth - pl->m_stealthvalue - (uint32)sqrt(dist/100) - 50 * notfront;
232 if(seevaluse<0)
233 seestealth = false;
234 else if(seevaluse>=int32(urand(0,30)))
235 seestealth = true;
236 else seestealth = false;
238 // offset=1.0
239 return seestealth && (dist * 1.0 <= sight) ;
242 void
243 AggressorAI::AttackStart(Unit *u)
245 if( i_creature.getVictim() || !u )
246 return;
247 // DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", i_creature.GetName(), u->GetGUIDLow());
248 i_victimGuid = u->GetGUID();
249 i_creature.Attack(u);
250 i_creature.resetAttackTimer();
251 i_creature->Mutate(new TargetedMovementGenerator(*u));
252 if (u->GetTypeId() == TYPEID_PLAYER)
253 i_creature.SetLootRecipient((Player*)u);