[9290] Some cleanups in realmd, no functional changes
[getmangos.git] / src / game / MotionMaster.cpp
blobd483d32445804a2db24b4ee62f8f96022f9cf184
1 /*
2 * Copyright (C) 2005-2010 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 "MotionMaster.h"
20 #include "CreatureAISelector.h"
21 #include "Creature.h"
22 #include "Traveller.h"
24 #include "ConfusedMovementGenerator.h"
25 #include "FleeingMovementGenerator.h"
26 #include "HomeMovementGenerator.h"
27 #include "IdleMovementGenerator.h"
28 #include "PointMovementGenerator.h"
29 #include "TargetedMovementGenerator.h"
30 #include "WaypointMovementGenerator.h"
32 #include <cassert>
34 inline bool isStatic(MovementGenerator *mv)
36 return (mv == &si_idleMovement);
39 void
40 MotionMaster::Initialize()
42 // clear ALL movement generators (including default)
43 while(!empty())
45 MovementGenerator *curr = top();
46 pop();
47 curr->Finalize(*i_owner);
48 if( !isStatic( curr ) )
49 delete curr;
52 // set new default movement generator
53 if(i_owner->GetTypeId() == TYPEID_UNIT)
55 MovementGenerator* movement = FactorySelector::selectMovementGenerator((Creature*)i_owner);
56 push( movement == NULL ? &si_idleMovement : movement );
57 top()->Initialize(*i_owner);
59 else
60 push(&si_idleMovement);
63 MotionMaster::~MotionMaster()
65 // clear ALL movement generators (including default)
66 while(!empty())
68 MovementGenerator *curr = top();
69 pop();
70 curr->Finalize(*i_owner);
71 if( !isStatic( curr ) )
72 delete curr;
76 void
77 MotionMaster::UpdateMotion(uint32 diff)
79 if( i_owner->hasUnitState(UNIT_STAT_CAN_NOT_MOVE) )
80 return;
81 assert( !empty() );
82 m_cleanFlag |= MMCF_UPDATE;
83 if (!top()->Update(*i_owner, diff))
85 m_cleanFlag &= ~MMCF_UPDATE;
86 MovementExpired();
88 else
89 m_cleanFlag &= ~MMCF_UPDATE;
91 if (m_expList)
93 for (size_t i = 0; i < m_expList->size(); ++i)
95 MovementGenerator* mg = (*m_expList)[i];
96 if (!isStatic(mg))
97 delete mg;
100 delete m_expList;
101 m_expList = NULL;
103 if (empty())
104 Initialize();
106 if (m_cleanFlag & MMCF_RESET)
108 top()->Reset(*i_owner);
109 m_cleanFlag &= ~MMCF_RESET;
114 void
115 MotionMaster::DirectClean(bool reset)
117 while( !empty() && size() > 1 )
119 MovementGenerator *curr = top();
120 pop();
121 curr->Finalize(*i_owner);
122 if( !isStatic( curr ) )
123 delete curr;
126 if (reset)
128 assert( !empty() );
129 top()->Reset(*i_owner);
133 void
134 MotionMaster::DelayedClean()
136 if (empty() || size() == 1)
137 return;
139 if(!m_expList)
140 m_expList = new ExpireList();
142 while( !empty() && size() > 1 )
144 MovementGenerator *curr = top();
145 pop();
146 curr->Finalize(*i_owner);
147 if( !isStatic( curr ) )
148 m_expList->push_back(curr);
152 void
153 MotionMaster::DirectExpire(bool reset)
155 if( empty() || size() == 1 )
156 return;
158 MovementGenerator *curr = top();
159 pop();
161 // also drop stored under top() targeted motions
162 while (!empty() && (top()->GetMovementGeneratorType() == CHASE_MOTION_TYPE || top()->GetMovementGeneratorType() == FOLLOW_MOTION_TYPE))
164 MovementGenerator *temp = top();
165 pop();
166 temp ->Finalize(*i_owner);
167 delete temp;
170 // it can add another motions instead
171 curr->Finalize(*i_owner);
173 if( !isStatic(curr) )
174 delete curr;
176 if( empty() )
177 Initialize();
179 if (reset) top()->Reset(*i_owner);
182 void
183 MotionMaster::DelayedExpire()
185 if( empty() || size() == 1 )
186 return;
188 MovementGenerator *curr = top();
189 pop();
191 if(!m_expList)
192 m_expList = new ExpireList();
194 // also drop stored under top() targeted motions
195 while (!empty() && (top()->GetMovementGeneratorType() == CHASE_MOTION_TYPE || top()->GetMovementGeneratorType() == FOLLOW_MOTION_TYPE))
197 MovementGenerator *temp = top();
198 pop();
199 temp ->Finalize(*i_owner);
200 m_expList->push_back(temp );
203 curr->Finalize(*i_owner);
205 if( !isStatic(curr) )
206 m_expList->push_back(curr);
209 void MotionMaster::MoveIdle()
211 if( empty() || !isStatic( top() ) )
212 push( &si_idleMovement );
215 void
216 MotionMaster::MoveTargetedHome()
218 if(i_owner->hasUnitState(UNIT_STAT_FLEEING))
219 return;
221 Clear(false);
223 if(i_owner->GetTypeId()==TYPEID_UNIT && !((Creature*)i_owner)->GetCharmerOrOwnerGUID())
225 DEBUG_LOG("Creature (Entry: %u GUID: %u) targeted home", i_owner->GetEntry(), i_owner->GetGUIDLow());
226 Mutate(new HomeMovementGenerator<Creature>());
228 else if(i_owner->GetTypeId()==TYPEID_UNIT && ((Creature*)i_owner)->GetCharmerOrOwnerGUID())
230 DEBUG_LOG("Pet or controlled creature (Entry: %u GUID: %u) targeting home",
231 i_owner->GetEntry(), i_owner->GetGUIDLow() );
232 Unit *target = ((Creature*)i_owner)->GetCharmerOrOwner();
233 if(target)
235 DEBUG_LOG("Following %s (GUID: %u)",
236 target->GetTypeId()==TYPEID_PLAYER ? "player" : "creature",
237 target->GetTypeId()==TYPEID_PLAYER ? target->GetGUIDLow() : ((Creature*)target)->GetDBTableGUIDLow() );
238 Mutate(new FollowMovementGenerator<Creature>(*target,PET_FOLLOW_DIST,PET_FOLLOW_ANGLE));
241 else
243 sLog.outError("Player (GUID: %u) attempt targeted home", i_owner->GetGUIDLow() );
247 void
248 MotionMaster::MoveConfused()
250 if(i_owner->GetTypeId()==TYPEID_PLAYER)
252 DEBUG_LOG("Player (GUID: %u) move confused", i_owner->GetGUIDLow() );
253 Mutate(new ConfusedMovementGenerator<Player>());
255 else
257 DEBUG_LOG("Creature (Entry: %u GUID: %u) move confused",
258 i_owner->GetEntry(), i_owner->GetGUIDLow() );
259 Mutate(new ConfusedMovementGenerator<Creature>());
263 void
264 MotionMaster::MoveChase(Unit* target, float dist, float angle)
266 // ignore movement request if target not exist
267 if(!target)
268 return;
270 if(i_owner->GetTypeId()==TYPEID_PLAYER)
272 DEBUG_LOG("Player (GUID: %u) chase to %s (GUID: %u)",
273 i_owner->GetGUIDLow(),
274 target->GetTypeId()==TYPEID_PLAYER ? "player" : "creature",
275 target->GetTypeId()==TYPEID_PLAYER ? i_owner->GetGUIDLow() : ((Creature*)i_owner)->GetDBTableGUIDLow() );
276 Mutate(new ChaseMovementGenerator<Player>(*target,dist,angle));
278 else
280 DEBUG_LOG("Creature (Entry: %u GUID: %u) chase to %s (GUID: %u)",
281 i_owner->GetEntry(), i_owner->GetGUIDLow(),
282 target->GetTypeId()==TYPEID_PLAYER ? "player" : "creature",
283 target->GetTypeId()==TYPEID_PLAYER ? target->GetGUIDLow() : ((Creature*)target)->GetDBTableGUIDLow() );
284 Mutate(new ChaseMovementGenerator<Creature>(*target,dist,angle));
288 void
289 MotionMaster::MoveFollow(Unit* target, float dist, float angle)
291 Clear();
293 // ignore movement request if target not exist
294 if(!target)
295 return;
297 if(i_owner->GetTypeId()==TYPEID_PLAYER)
299 DEBUG_LOG("Player (GUID: %u) follow to %s (GUID: %u)", i_owner->GetGUIDLow(),
300 target->GetTypeId()==TYPEID_PLAYER ? "player" : "creature",
301 target->GetTypeId()==TYPEID_PLAYER ? i_owner->GetGUIDLow() : ((Creature*)i_owner)->GetDBTableGUIDLow() );
302 Mutate(new FollowMovementGenerator<Player>(*target,dist,angle));
304 else
306 DEBUG_LOG("Creature (Entry: %u GUID: %u) follow to %s (GUID: %u)",
307 i_owner->GetEntry(), i_owner->GetGUIDLow(),
308 target->GetTypeId()==TYPEID_PLAYER ? "player" : "creature",
309 target->GetTypeId()==TYPEID_PLAYER ? target->GetGUIDLow() : ((Creature*)target)->GetDBTableGUIDLow() );
310 Mutate(new FollowMovementGenerator<Creature>(*target,dist,angle));
314 void
315 MotionMaster::MovePoint(uint32 id, float x, float y, float z)
317 if(i_owner->GetTypeId()==TYPEID_PLAYER)
319 DEBUG_LOG("Player (GUID: %u) targeted point (Id: %u X: %f Y: %f Z: %f)", i_owner->GetGUIDLow(), id, x, y, z );
320 Mutate(new PointMovementGenerator<Player>(id,x,y,z));
322 else
324 DEBUG_LOG("Creature (Entry: %u GUID: %u) targeted point (ID: %u X: %f Y: %f Z: %f)",
325 i_owner->GetEntry(), i_owner->GetGUIDLow(), id, x, y, z );
326 Mutate(new PointMovementGenerator<Creature>(id,x,y,z));
330 void
331 MotionMaster::MoveSeekAssistance(float x, float y, float z)
333 if(i_owner->GetTypeId()==TYPEID_PLAYER)
335 sLog.outError("Player (GUID: %u) attempt to seek assistance",i_owner->GetGUIDLow());
337 else
339 DEBUG_LOG("Creature (Entry: %u GUID: %u) seek assistance (X: %f Y: %f Z: %f)",
340 i_owner->GetEntry(), i_owner->GetGUIDLow(), x, y, z );
341 Mutate(new AssistanceMovementGenerator(x,y,z));
345 void
346 MotionMaster::MoveSeekAssistanceDistract(uint32 time)
348 if(i_owner->GetTypeId()==TYPEID_PLAYER)
350 sLog.outError("Player (GUID: %u) attempt to call distract after assistance",i_owner->GetGUIDLow());
352 else
354 DEBUG_LOG("Creature (Entry: %u GUID: %u) is distracted after assistance call (Time: %u)",
355 i_owner->GetEntry(), i_owner->GetGUIDLow(), time );
356 Mutate(new AssistanceDistractMovementGenerator(time));
360 void
361 MotionMaster::MoveFleeing(Unit* enemy, uint32 time)
363 if(!enemy)
364 return;
366 if(i_owner->GetTypeId()==TYPEID_PLAYER)
368 DEBUG_LOG("Player (GUID: %u) flee from %s (GUID: %u)", i_owner->GetGUIDLow(),
369 enemy->GetTypeId()==TYPEID_PLAYER ? "player" : "creature",
370 enemy->GetTypeId()==TYPEID_PLAYER ? enemy->GetGUIDLow() : ((Creature*)enemy)->GetDBTableGUIDLow() );
371 Mutate(new FleeingMovementGenerator<Player>(enemy->GetGUID()));
373 else
375 DEBUG_LOG("Creature (Entry: %u GUID: %u) flee from %s (GUID: %u)%s",
376 i_owner->GetEntry(), i_owner->GetGUIDLow(),
377 enemy->GetTypeId()==TYPEID_PLAYER ? "player" : "creature",
378 enemy->GetTypeId()==TYPEID_PLAYER ? enemy->GetGUIDLow() : ((Creature*)enemy)->GetDBTableGUIDLow(),
379 time ? " for a limited time" : "");
380 if (time)
381 Mutate(new TimedFleeingMovementGenerator(enemy->GetGUID(), time));
382 else
383 Mutate(new FleeingMovementGenerator<Creature>(enemy->GetGUID()));
387 void
388 MotionMaster::MoveTaxiFlight(uint32 path, uint32 pathnode)
390 if(i_owner->GetTypeId()==TYPEID_PLAYER)
392 DEBUG_LOG("Player (GUID: %u) taxi to (Path %u node %u)", i_owner->GetGUIDLow(), path, pathnode);
393 FlightPathMovementGenerator* mgen = new FlightPathMovementGenerator(path,pathnode);
394 Mutate(mgen);
396 else
398 sLog.outError("Creature (Entry: %u GUID: %u) attempt taxi to (Path %u node %u)",
399 i_owner->GetEntry(), i_owner->GetGUIDLow(), path, pathnode );
403 void
404 MotionMaster::MoveDistract(uint32 timer)
406 if(i_owner->GetTypeId()==TYPEID_PLAYER)
408 DEBUG_LOG("Player (GUID: %u) distracted (timer: %u)", i_owner->GetGUIDLow(), timer);
410 else
412 DEBUG_LOG("Creature (Entry: %u GUID: %u) (timer: %u)",
413 i_owner->GetEntry(), i_owner->GetGUIDLow(), timer);
416 DistractMovementGenerator* mgen = new DistractMovementGenerator(timer);
417 Mutate(mgen);
420 void MotionMaster::Mutate(MovementGenerator *m)
422 if (!empty())
424 switch(top()->GetMovementGeneratorType())
426 // HomeMovement is not that important, delete it if meanwhile a new comes
427 case HOME_MOTION_TYPE:
428 // DistractMovement interrupted by any other movement
429 case DISTRACT_MOTION_TYPE:
430 MovementExpired(false);
431 default:
432 break;
435 if (!empty())
436 top()->Interrupt(*i_owner);
438 m->Initialize(*i_owner);
439 push(m);
442 void MotionMaster::propagateSpeedChange()
444 Impl::container_type::iterator it = Impl::c.begin();
445 for ( ;it != end(); ++it)
447 (*it)->unitSpeedChanged();
451 MovementGeneratorType MotionMaster::GetCurrentMovementGeneratorType() const
453 if(empty())
454 return IDLE_MOTION_TYPE;
456 return top()->GetMovementGeneratorType();
459 bool MotionMaster::GetDestination(float &x, float &y, float &z)
461 if(empty())
462 return false;
464 return top()->GetDestination(x,y,z);
467 void MotionMaster::UpdateFinalDistanceToTarget(float fDistance)
469 if (!empty())
470 top()->UpdateFinalDistance(fDistance);