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"
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"
34 inline bool isStatic(MovementGenerator
*mv
)
36 return (mv
== &si_idleMovement
);
40 MotionMaster::Initialize()
42 // clear ALL movement generators (including default)
45 MovementGenerator
*curr
= top();
47 curr
->Finalize(*i_owner
);
48 if( !isStatic( 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
);
60 push(&si_idleMovement
);
63 MotionMaster::~MotionMaster()
65 // clear ALL movement generators (including default)
68 MovementGenerator
*curr
= top();
70 curr
->Finalize(*i_owner
);
71 if( !isStatic( curr
) )
77 MotionMaster::UpdateMotion(uint32 diff
)
79 if( i_owner
->hasUnitState(UNIT_STAT_CAN_NOT_MOVE
) )
82 m_cleanFlag
|= MMCF_UPDATE
;
83 if (!top()->Update(*i_owner
, diff
))
85 m_cleanFlag
&= ~MMCF_UPDATE
;
89 m_cleanFlag
&= ~MMCF_UPDATE
;
93 for (size_t i
= 0; i
< m_expList
->size(); ++i
)
95 MovementGenerator
* mg
= (*m_expList
)[i
];
106 if (m_cleanFlag
& MMCF_RESET
)
108 top()->Reset(*i_owner
);
109 m_cleanFlag
&= ~MMCF_RESET
;
115 MotionMaster::DirectClean(bool reset
)
117 while( !empty() && size() > 1 )
119 MovementGenerator
*curr
= top();
121 curr
->Finalize(*i_owner
);
122 if( !isStatic( curr
) )
129 top()->Reset(*i_owner
);
134 MotionMaster::DelayedClean()
136 if (empty() || size() == 1)
140 m_expList
= new ExpireList();
142 while( !empty() && size() > 1 )
144 MovementGenerator
*curr
= top();
146 curr
->Finalize(*i_owner
);
147 if( !isStatic( curr
) )
148 m_expList
->push_back(curr
);
153 MotionMaster::DirectExpire(bool reset
)
155 if( empty() || size() == 1 )
158 MovementGenerator
*curr
= top();
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();
166 temp
->Finalize(*i_owner
);
170 // it can add another motions instead
171 curr
->Finalize(*i_owner
);
173 if( !isStatic(curr
) )
179 if (reset
) top()->Reset(*i_owner
);
183 MotionMaster::DelayedExpire()
185 if( empty() || size() == 1 )
188 MovementGenerator
*curr
= top();
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();
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
);
216 MotionMaster::MoveTargetedHome()
218 if(i_owner
->hasUnitState(UNIT_STAT_FLEEING
))
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();
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
));
243 sLog
.outError("Player (GUID: %u) attempt targeted home", i_owner
->GetGUIDLow() );
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
>());
257 DEBUG_LOG("Creature (Entry: %u GUID: %u) move confused",
258 i_owner
->GetEntry(), i_owner
->GetGUIDLow() );
259 Mutate(new ConfusedMovementGenerator
<Creature
>());
264 MotionMaster::MoveChase(Unit
* target
, float dist
, float angle
)
266 // ignore movement request if target not exist
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
));
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
));
289 MotionMaster::MoveFollow(Unit
* target
, float dist
, float angle
)
293 // ignore movement request if target not exist
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
));
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
));
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
));
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
));
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());
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
));
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());
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
));
361 MotionMaster::MoveFleeing(Unit
* enemy
, uint32 time
)
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()));
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" : "");
381 Mutate(new TimedFleeingMovementGenerator(enemy
->GetGUID(), time
));
383 Mutate(new FleeingMovementGenerator
<Creature
>(enemy
->GetGUID()));
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
);
398 sLog
.outError("Creature (Entry: %u GUID: %u) attempt taxi to (Path %u node %u)",
399 i_owner
->GetEntry(), i_owner
->GetGUIDLow(), path
, pathnode
);
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
);
412 DEBUG_LOG("Creature (Entry: %u GUID: %u) (timer: %u)",
413 i_owner
->GetEntry(), i_owner
->GetGUIDLow(), timer
);
416 DistractMovementGenerator
* mgen
= new DistractMovementGenerator(timer
);
420 void MotionMaster::Mutate(MovementGenerator
*m
)
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);
436 top()->Interrupt(*i_owner
);
438 m
->Initialize(*i_owner
);
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
454 return IDLE_MOTION_TYPE
;
456 return top()->GetMovementGeneratorType();
459 bool MotionMaster::GetDestination(float &x
, float &y
, float &z
)
464 return top()->GetDestination(x
,y
,z
);
467 void MotionMaster::UpdateFinalDistanceToTarget(float fDistance
)
470 top()->UpdateFinalDistance(fDistance
);