1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
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. |
---|
8 | * |
---|
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. |
---|
13 | * |
---|
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 |
---|
17 | */ |
---|
18 | |
---|
19 | #include "ByteBuffer.h" |
---|
20 | #include "TargetedMovementGenerator.h" |
---|
21 | #include "Errors.h" |
---|
22 | #include "Creature.h" |
---|
23 | #include "MapManager.h" |
---|
24 | #include "DestinationHolderImp.h" |
---|
25 | #include "World.h" |
---|
26 | |
---|
27 | #define SMALL_ALPHA 0.05f |
---|
28 | |
---|
29 | #include <cmath> |
---|
30 | /* |
---|
31 | struct StackCleaner |
---|
32 | { |
---|
33 | Creature &i_creature; |
---|
34 | StackCleaner(Creature &creature) : i_creature(creature) {} |
---|
35 | void Done(void) { i_creature.StopMoving(); } |
---|
36 | ~StackCleaner() |
---|
37 | { |
---|
38 | i_creature->Clear(); |
---|
39 | } |
---|
40 | }; |
---|
41 | */ |
---|
42 | |
---|
43 | template<class T> |
---|
44 | void |
---|
45 | TargetedMovementGenerator<T>::_setTargetLocation(T &owner) |
---|
46 | { |
---|
47 | if( !i_target.isValid() || !&owner ) |
---|
48 | return; |
---|
49 | |
---|
50 | if( owner.hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNNED | UNIT_STAT_DISTRACTED) ) |
---|
51 | return; |
---|
52 | |
---|
53 | // prevent redundant micro-movement for pets, other followers. |
---|
54 | if(i_offset && i_target->IsWithinDistInMap(&owner,2*i_offset)) |
---|
55 | return; |
---|
56 | |
---|
57 | float x, y, z; |
---|
58 | if(!i_offset) |
---|
59 | { |
---|
60 | // to nearest contact position |
---|
61 | i_target->GetContactPoint( &owner, x, y, z ); |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | // to at i_offset distance from target and i_angle from target facing |
---|
66 | i_target->GetClosePoint(x,y,z,owner.GetObjectSize(),i_offset,i_angle); |
---|
67 | } |
---|
68 | |
---|
69 | /* |
---|
70 | We MUST not check the distance difference and avoid setting the new location for smaller distances. |
---|
71 | By that we risk having far too many GetContactPoint() calls freezing the whole system. |
---|
72 | In TargetedMovementGenerator<T>::Update() we check the distance to the target and at |
---|
73 | some range we calculate a new position. The calculation takes some processor cycles due to vmaps. |
---|
74 | If the distance to the target it too large to ignore, |
---|
75 | but the distance to the new contact point is short enough to be ignored, |
---|
76 | we will calculate a new contact point each update loop, but will never move to it. |
---|
77 | The system will freeze. |
---|
78 | ralf |
---|
79 | |
---|
80 | //We don't update Mob Movement, if the difference between New destination and last destination is < BothObjectSize |
---|
81 | float bothObjectSize = i_target->GetObjectSize() + owner.GetObjectSize() + CONTACT_DISTANCE; |
---|
82 | if( i_destinationHolder.HasDestination() && i_destinationHolder.GetDestinationDiff(x,y,z) < bothObjectSize ) |
---|
83 | return; |
---|
84 | */ |
---|
85 | Traveller<T> traveller(owner); |
---|
86 | i_destinationHolder.SetDestination(traveller, x, y, z); |
---|
87 | owner.addUnitState(UNIT_STAT_CHASE); |
---|
88 | if (owner.GetTypeId() == TYPEID_UNIT && ((Creature*)&owner)->canFly()) |
---|
89 | owner.AddUnitMovementFlag(MOVEMENTFLAG_FLYING2); |
---|
90 | } |
---|
91 | |
---|
92 | template<class T> |
---|
93 | void |
---|
94 | TargetedMovementGenerator<T>::Initialize(T &owner) |
---|
95 | { |
---|
96 | if(!&owner) |
---|
97 | return; |
---|
98 | owner.RemoveUnitMovementFlag(MOVEMENTFLAG_WALK_MODE); |
---|
99 | |
---|
100 | if (owner.GetTypeId() == TYPEID_UNIT && ((Creature*)&owner)->canFly()) |
---|
101 | owner.AddUnitMovementFlag(MOVEMENTFLAG_FLYING2); |
---|
102 | |
---|
103 | _setTargetLocation(owner); |
---|
104 | } |
---|
105 | |
---|
106 | template<class T> |
---|
107 | void |
---|
108 | TargetedMovementGenerator<T>::Finalize(T &owner) |
---|
109 | { |
---|
110 | owner.clearUnitState(UNIT_STAT_CHASE); |
---|
111 | } |
---|
112 | |
---|
113 | template<class T> |
---|
114 | void |
---|
115 | TargetedMovementGenerator<T>::Reset(T &owner) |
---|
116 | { |
---|
117 | Initialize(owner); |
---|
118 | } |
---|
119 | |
---|
120 | template<class T> |
---|
121 | bool |
---|
122 | TargetedMovementGenerator<T>::Update(T &owner, const uint32 & time_diff) |
---|
123 | { |
---|
124 | if(!i_target.isValid()) |
---|
125 | return false; |
---|
126 | |
---|
127 | if( !&owner || !owner.isAlive()) |
---|
128 | return true; |
---|
129 | |
---|
130 | if( owner.hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNNED | UNIT_STAT_FLEEING | UNIT_STAT_DISTRACTED) ) |
---|
131 | return true; |
---|
132 | |
---|
133 | // prevent movement while casting spells with cast time or channel time |
---|
134 | if ( owner.IsNonMeleeSpellCasted(false, false, true)) |
---|
135 | { |
---|
136 | if (!owner.IsStopped()) |
---|
137 | owner.StopMoving(); |
---|
138 | return true; |
---|
139 | } |
---|
140 | |
---|
141 | // prevent crash after creature killed pet |
---|
142 | if (!owner.hasUnitState(UNIT_STAT_FOLLOW) && owner.getVictim() != i_target.getTarget()) |
---|
143 | return true; |
---|
144 | |
---|
145 | Traveller<T> traveller(owner); |
---|
146 | |
---|
147 | if( !i_destinationHolder.HasDestination() ) |
---|
148 | _setTargetLocation(owner); |
---|
149 | if( owner.IsStopped() && !i_destinationHolder.HasArrived() ) |
---|
150 | { |
---|
151 | owner.addUnitState(UNIT_STAT_CHASE); |
---|
152 | if (owner.GetTypeId() == TYPEID_UNIT && ((Creature*)&owner)->canFly()) |
---|
153 | owner.AddUnitMovementFlag(MOVEMENTFLAG_FLYING2); |
---|
154 | |
---|
155 | i_destinationHolder.StartTravel(traveller); |
---|
156 | return true; |
---|
157 | } |
---|
158 | |
---|
159 | if (i_destinationHolder.UpdateTraveller(traveller, time_diff, false)) |
---|
160 | { |
---|
161 | // put targeted movement generators on a higher priority |
---|
162 | if (owner.GetObjectSize()) |
---|
163 | i_destinationHolder.ResetUpdate(50); |
---|
164 | |
---|
165 | float dist = i_target->GetObjectSize() + owner.GetObjectSize() + sWorld.getRate(RATE_TARGET_POS_RECALCULATION_RANGE); |
---|
166 | |
---|
167 | //More distance let have better performance, less distance let have more sensitive reaction at target move. |
---|
168 | |
---|
169 | // try to counter precision differences |
---|
170 | if( i_destinationHolder.GetDistance2dFromDestSq(*i_target.getTarget()) >= dist * dist) |
---|
171 | { |
---|
172 | owner.SetInFront(i_target.getTarget()); // Set new Angle For Map:: |
---|
173 | _setTargetLocation(owner); //Calculate New Dest and Send data To Player |
---|
174 | } |
---|
175 | // Update the Angle of the target only for Map::, no need to send packet for player |
---|
176 | else if ( !i_angle && !owner.HasInArc( 0.01f, i_target.getTarget() ) ) |
---|
177 | owner.SetInFront(i_target.getTarget()); |
---|
178 | |
---|
179 | if(( owner.IsStopped() && !i_destinationHolder.HasArrived() ) || i_recalculateTravel ) |
---|
180 | { |
---|
181 | i_recalculateTravel = false; |
---|
182 | //Angle update will take place into owner.StopMoving() |
---|
183 | owner.SetInFront(i_target.getTarget()); |
---|
184 | |
---|
185 | owner.StopMoving(); |
---|
186 | if(owner.canReachWithAttack(i_target.getTarget()) && !owner.hasUnitState(UNIT_STAT_FOLLOW)) |
---|
187 | owner.Attack(i_target.getTarget(),true); |
---|
188 | } |
---|
189 | } |
---|
190 | return true; |
---|
191 | } |
---|
192 | |
---|
193 | template<class T> |
---|
194 | Unit* |
---|
195 | TargetedMovementGenerator<T>::GetTarget() const |
---|
196 | { |
---|
197 | return i_target.getTarget(); |
---|
198 | } |
---|
199 | |
---|
200 | template void TargetedMovementGenerator<Player>::_setTargetLocation(Player &); |
---|
201 | template void TargetedMovementGenerator<Creature>::_setTargetLocation(Creature &); |
---|
202 | template void TargetedMovementGenerator<Player>::Initialize(Player &); |
---|
203 | template void TargetedMovementGenerator<Creature>::Initialize(Creature &); |
---|
204 | template void TargetedMovementGenerator<Player>::Finalize(Player &); |
---|
205 | template void TargetedMovementGenerator<Creature>::Finalize(Creature &); |
---|
206 | template void TargetedMovementGenerator<Player>::Reset(Player &); |
---|
207 | template void TargetedMovementGenerator<Creature>::Reset(Creature &); |
---|
208 | template bool TargetedMovementGenerator<Player>::Update(Player &, const uint32 &); |
---|
209 | template bool TargetedMovementGenerator<Creature>::Update(Creature &, const uint32 &); |
---|
210 | template Unit* TargetedMovementGenerator<Player>::GetTarget() const; |
---|
211 | template Unit* TargetedMovementGenerator<Creature>::GetTarget() const; |
---|