[9143] Typos
[getmangos.git] / src / game / DestinationHolderImp.h
blobfc622623f497080cf422d9eb5d6a7c03d2962abc
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 #ifndef MANGOS_DESTINATIONHOLDERIMP_H
20 #define MANGOS_DESTINATIONHOLDERIMP_H
22 #include "MapManager.h"
23 #include "DestinationHolder.h"
24 #include "Unit.h"
26 #include <cmath>
28 template<typename TRAVELLER>
29 void
30 DestinationHolder<TRAVELLER>::_findOffSetPoint(float x1, float y1, float x2, float y2, float offset, float &x, float &y)
32 /* given the point (x1, y1) and (x2, y2).. need to find the point (x,y) on the same line
33 * such that the distance from (x, y) to (x2, y2) is offset.
34 * Let the distance of p1 to p2 = d.. then the ratio of offset/d = (x2-x)/(x2-x1)
35 * hence x = x2 - (offset/d)*(x2-x1)
36 * like wise offset/d = (y2-y)/(y2-y1);
38 if( offset == 0 )
40 x = x2;
41 y = y2;
43 else
45 double x_diff = double(x2 - x1);
46 double y_diff = double(y2 - y1);
47 double distance_d = (double)((x_diff*x_diff) + (y_diff * y_diff));
48 if(distance_d == 0)
50 x = x2;
51 y = y2;
53 else
55 distance_d = ::sqrt(distance_d); // starting distance
56 double distance_ratio = (double)(distance_d - offset)/(double)distance_d;
57 // line above has revised formula which is more correct, I think
58 x = (float)(x1 + (distance_ratio*x_diff));
59 y = (float)(y1 + (distance_ratio*y_diff));
64 template<typename TRAVELLER>
65 uint32
66 DestinationHolder<TRAVELLER>::SetDestination(TRAVELLER &traveller, float dest_x, float dest_y, float dest_z, bool sendMove)
68 i_destSet = true;
69 i_destX = dest_x;
70 i_destY = dest_y;
71 i_destZ = dest_z;
73 return StartTravel(traveller, sendMove);
76 template<typename TRAVELLER>
77 uint32
78 DestinationHolder<TRAVELLER>::StartTravel(TRAVELLER &traveller, bool sendMove)
80 if(!i_destSet) return 0;
82 i_fromX = traveller.GetPositionX();
83 i_fromY = traveller.GetPositionY();
84 i_fromZ = traveller.GetPositionZ();
86 i_totalTravelTime = traveller.GetTotalTrevelTimeTo(i_destX,i_destY,i_destZ);
87 i_timeElapsed = 0;
88 if(sendMove)
89 traveller.MoveTo(i_destX, i_destY, i_destZ, i_totalTravelTime);
90 return i_totalTravelTime;
93 template<typename TRAVELLER>
94 bool
95 DestinationHolder<TRAVELLER>::UpdateTraveller(TRAVELLER &traveller, uint32 diff, bool force_update, bool micro_movement)
97 if (!micro_movement)
99 i_tracker.Update(diff);
100 i_timeElapsed += diff;
101 if (i_tracker.Passed() || force_update)
103 ResetUpdate();
104 if (!i_destSet) return true;
106 float x,y,z;
107 GetLocationNowNoMicroMovement(x, y, z);
109 if (traveller.GetTraveller().GetPositionX() != x || traveller.GetTraveller().GetPositionY() != y || traveller.GetTraveller().GetPositionZ() != z)
111 float ori = traveller.GetTraveller().GetAngle(x, y);
112 traveller.Relocation(x, y, z, ori);
114 return true;
116 return false;
118 i_tracker.Update(diff);
119 i_timeElapsed += diff;
120 if (i_tracker.Passed() || force_update)
122 ResetUpdate();
123 if (!i_destSet) return true;
125 if (!traveller.GetTraveller().hasUnitState(UNIT_STAT_MOVING | UNIT_STAT_IN_FLIGHT))
126 return true;
128 float x,y,z;
129 if (traveller.GetTraveller().hasUnitState(UNIT_STAT_IN_FLIGHT))
130 GetLocationNow(traveller.GetTraveller().GetBaseMap() ,x, y, z, true); // Should reposition Object with right Coord, so I can bypass some Grid Relocation
131 else
132 GetLocationNow(traveller.GetTraveller().GetBaseMap(), x, y, z, false);
134 if (traveller.GetTraveller().GetPositionX() != x || traveller.GetTraveller().GetPositionY() != y || traveller.GetTraveller().GetPositionZ() != z)
136 float ori = traveller.GetTraveller().GetAngle(x, y);
137 traveller.Relocation(x, y, z, ori);
139 // Change movement computation to micro movement based on last tick coords, this makes system work
140 // even on multiple floors zones without hugh vmaps usage ;)
142 // Take care of underrun of uint32
143 if (i_totalTravelTime >= i_timeElapsed)
144 i_totalTravelTime -= i_timeElapsed; // Consider only the remaining part
145 else
146 i_totalTravelTime = 0;
148 i_timeElapsed = 0;
149 i_fromX = x; // and change origine
150 i_fromY = y; // then I take into account only micro movement
151 i_fromZ = z;
152 return true;
154 return false;
157 template<typename TRAVELLER>
158 void
159 DestinationHolder<TRAVELLER>::GetLocationNow(const Map * map, float &x, float &y, float &z, bool is3D) const
161 if (HasArrived())
163 x = i_destX;
164 y = i_destY;
165 z = i_destZ;
167 else if (HasDestination())
169 double percent_passed = (double)i_timeElapsed / (double)i_totalTravelTime;
170 const float distanceX = ((i_destX - i_fromX) * percent_passed);
171 const float distanceY = ((i_destY - i_fromY) * percent_passed);
172 const float distanceZ = ((i_destZ - i_fromZ) * percent_passed);
173 x = i_fromX + distanceX;
174 y = i_fromY + distanceY;
175 float z2 = i_fromZ + distanceZ;
176 // All that is not finished but previous code neither... Traveller need be able to swim.
177 if (is3D)
178 z = z2;
179 else
181 //That part is good for mob Walking on the floor. But the floor is not always what we thought.
182 z = map->GetHeight(x,y,i_fromZ,false); // Disable cave check
183 const float groundDist = sqrt(distanceX*distanceX + distanceY*distanceY);
184 const float zDist = fabs(i_fromZ - z) + 0.000001f;
185 const float slope = groundDist / zDist;
186 if(slope < 1.0f) // This prevents the ground returned by GetHeight to be used when in cave
187 z = z2; // a climb or jump of more than 45 is denied
192 template<typename TRAVELLER>
193 float
194 DestinationHolder<TRAVELLER>::GetDistance3dFromDestSq(const WorldObject &obj) const
196 float x,y,z;
197 obj.GetPosition(x,y,z);
198 return (i_destX-x)*(i_destX-x)+(i_destY-y)*(i_destY-y)+(i_destZ-z)*(i_destZ-z);
201 template<typename TRAVELLER>
202 float
203 DestinationHolder<TRAVELLER>::GetDestinationDiff(float x, float y, float z) const
205 return sqrt(((x-i_destX)*(x-i_destX)) + ((y-i_destY)*(y-i_destY)) + ((z-i_destZ)*(z-i_destZ)));
208 template<typename TRAVELLER>
209 void
210 DestinationHolder<TRAVELLER>::GetLocationNowNoMicroMovement(float &x, float &y, float &z) const
212 if (HasArrived())
214 x = i_destX;
215 y = i_destY;
216 z = i_destZ;
218 else
220 double percent_passed = (double)i_timeElapsed / (double)i_totalTravelTime;
221 x = i_fromX + ((i_destX - i_fromX) * percent_passed);
222 y = i_fromY + ((i_destY - i_fromY) * percent_passed);
223 z = i_fromZ + ((i_destZ - i_fromZ) * percent_passed);
227 #endif