[9143] Typos
[getmangos.git] / src / game / TargetedMovementGenerator.cpp
blob10f2597074232c91b039b9669c2ba7cfe34afae2
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 "ByteBuffer.h"
20 #include "TargetedMovementGenerator.h"
21 #include "Errors.h"
22 #include "Creature.h"
23 #include "DestinationHolderImp.h"
24 #include "World.h"
26 #define SMALL_ALPHA 0.05f
28 #include <cmath>
30 struct StackCleaner
32 Creature &i_creature;
33 StackCleaner(Creature &creature) : i_creature(creature) {}
34 void Done(void) { i_creature.StopMoving(); }
35 ~StackCleaner()
37 i_creature->Clear();
42 template<class T>
43 void
44 TargetedMovementGenerator<T>::_setTargetLocation(T &owner)
46 if (!i_target.isValid() || !i_target->IsInWorld())
47 return;
49 if (owner.hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNNED | UNIT_STAT_DISTRACTED | UNIT_STAT_DIED))
50 return;
52 // prevent redundant micro-movement for pets, other followers.
53 if (i_offset && i_target->IsWithinDistInMap(&owner,2*i_offset))
54 return;
56 float x, y, z;
57 if (!i_offset)
59 // to nearest contact position
60 i_target->GetContactPoint( &owner, x, y, z );
62 else
64 // to at i_offset distance from target and i_angle from target facing
65 i_target->GetClosePoint(x,y,z,owner.GetObjectSize(),i_offset,i_angle);
69 We MUST not check the distance difference and avoid setting the new location for smaller distances.
70 By that we risk having far too many GetContactPoint() calls freezing the whole system.
71 In TargetedMovementGenerator<T>::Update() we check the distance to the target and at
72 some range we calculate a new position. The calculation takes some processor cycles due to vmaps.
73 If the distance to the target it too large to ignore,
74 but the distance to the new contact point is short enough to be ignored,
75 we will calculate a new contact point each update loop, but will never move to it.
76 The system will freeze.
77 ralf
79 //We don't update Mob Movement, if the difference between New destination and last destination is < BothObjectSize
80 float bothObjectSize = i_target->GetObjectSize() + owner.GetObjectSize() + CONTACT_DISTANCE;
81 if( i_destinationHolder.HasDestination() && i_destinationHolder.GetDestinationDiff(x,y,z) < bothObjectSize )
82 return;
84 Traveller<T> traveller(owner);
85 i_destinationHolder.SetDestination(traveller, x, y, z);
86 owner.addUnitState(UNIT_STAT_CHASE);
87 if (owner.GetTypeId() == TYPEID_UNIT && ((Creature*)&owner)->canFly())
88 ((Creature&)owner).AddMonsterMoveFlag(MONSTER_MOVE_FLY);
91 template<>
92 void TargetedMovementGenerator<Creature>::Initialize(Creature &owner)
94 if (owner.HasSearchedAssistance())
95 owner.AddMonsterMoveFlag(MONSTER_MOVE_WALK);
96 else
97 owner.RemoveMonsterMoveFlag(MONSTER_MOVE_WALK);
99 if (((Creature*)&owner)->canFly())
100 owner.AddMonsterMoveFlag(MONSTER_MOVE_FLY);
102 _setTargetLocation(owner);
105 template<>
106 void TargetedMovementGenerator<Player>::UpdateFinalDistance(float fDistance)
108 // nothing to do for Player
111 template<>
112 void TargetedMovementGenerator<Creature>::UpdateFinalDistance(float fDistance)
114 i_offset = fDistance;
115 i_recalculateTravel = true;
118 template<>
119 void TargetedMovementGenerator<Player>::Initialize(Player &owner)
121 _setTargetLocation(owner);
124 template<class T>
125 void
126 TargetedMovementGenerator<T>::Finalize(T &owner)
128 owner.clearUnitState(UNIT_STAT_CHASE);
131 template<class T>
132 void
133 TargetedMovementGenerator<T>::Reset(T &owner)
135 Initialize(owner);
138 template<class T>
139 bool
140 TargetedMovementGenerator<T>::Update(T &owner, const uint32 & time_diff)
142 if (!i_target.isValid() || !i_target->IsInWorld())
143 return false;
145 if (!owner.isAlive())
146 return true;
148 if (owner.hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNNED | UNIT_STAT_FLEEING | UNIT_STAT_DISTRACTED | UNIT_STAT_DIED))
149 return true;
151 // prevent movement while casting spells with cast time or channel time
152 if (owner.IsNonMeleeSpellCasted(false, false, true))
154 if (!owner.IsStopped())
155 owner.StopMoving();
156 return true;
159 // prevent crash after creature killed pet
160 if (!owner.hasUnitState(UNIT_STAT_FOLLOW) && owner.getVictim() != i_target.getTarget())
161 return true;
163 Traveller<T> traveller(owner);
165 if (!i_destinationHolder.HasDestination())
166 _setTargetLocation(owner);
167 if (owner.IsStopped() && !i_destinationHolder.HasArrived())
169 owner.addUnitState(UNIT_STAT_CHASE);
170 if (owner.GetTypeId() == TYPEID_UNIT && ((Creature*)&owner)->canFly())
171 ((Creature&)owner).AddMonsterMoveFlag(MONSTER_MOVE_FLY);
173 i_destinationHolder.StartTravel(traveller);
174 return true;
177 if (i_destinationHolder.UpdateTraveller(traveller, time_diff, false))
179 // put targeted movement generators on a higher priority
180 if (owner.GetObjectSize())
181 i_destinationHolder.ResetUpdate(50);
183 float dist = i_target->GetObjectSize() + owner.GetObjectSize() + sWorld.getRate(RATE_TARGET_POS_RECALCULATION_RANGE);
185 //More distance let have better performance, less distance let have more sensitive reaction at target move.
187 // try to counter precision differences
188 if (i_destinationHolder.GetDistance3dFromDestSq(*i_target.getTarget()) >= dist * dist)
190 owner.SetInFront(i_target.getTarget()); // Set new Angle For Map::
191 _setTargetLocation(owner); //Calculate New Dest and Send data To Player
193 // Update the Angle of the target only for Map::, no need to send packet for player
194 else if (!i_angle && !owner.HasInArc(0.01f, i_target.getTarget()))
195 owner.SetInFront(i_target.getTarget());
197 if ((owner.IsStopped() && !i_destinationHolder.HasArrived()) || i_recalculateTravel)
199 i_recalculateTravel = false;
200 //Angle update will take place into owner.StopMoving()
201 owner.SetInFront(i_target.getTarget());
203 owner.StopMoving();
204 if(owner.canReachWithAttack(i_target.getTarget()) && !owner.hasUnitState(UNIT_STAT_FOLLOW))
205 owner.Attack(i_target.getTarget(),true);
208 return true;
211 template<class T>
212 Unit*
213 TargetedMovementGenerator<T>::GetTarget() const
215 return i_target.getTarget();
218 template void TargetedMovementGenerator<Player>::_setTargetLocation(Player &);
219 template void TargetedMovementGenerator<Creature>::_setTargetLocation(Creature &);
220 template void TargetedMovementGenerator<Player>::Finalize(Player &);
221 template void TargetedMovementGenerator<Creature>::Finalize(Creature &);
222 template void TargetedMovementGenerator<Player>::Reset(Player &);
223 template void TargetedMovementGenerator<Creature>::Reset(Creature &);
224 template bool TargetedMovementGenerator<Player>::Update(Player &, const uint32 &);
225 template bool TargetedMovementGenerator<Creature>::Update(Creature &, const uint32 &);
226 template Unit* TargetedMovementGenerator<Player>::GetTarget() const;
227 template Unit* TargetedMovementGenerator<Creature>::GetTarget() const;