[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / DestinationHolderImp.h
blob911f9d2be0eba707eca38060f2b13f5a990bf14e
1 /*
2 * Copyright (C) 2005-2009 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 "Creature.h"
23 #include "MapManager.h"
24 #include "DestinationHolder.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 float dx = i_destX - i_fromX;
87 float dy = i_destY - i_fromY;
88 float dz = i_destZ - i_fromZ;
90 float dist;
91 //Should be for Creature Flying and Swimming.
92 if(traveller.GetTraveller().hasUnitState(UNIT_STAT_IN_FLIGHT))
93 dist = sqrt((dx*dx) + (dy*dy) + (dz*dz));
94 else //Walking on the ground
95 dist = sqrt((dx*dx) + (dy*dy));
96 float speed = traveller.Speed();
98 speed *= 0.001f; // speed is in seconds so convert from second to millisecond
99 i_totalTravelTime = static_cast<uint32>(dist/speed);
100 i_timeElapsed = 0;
101 if(sendMove)
102 traveller.MoveTo(i_destX, i_destY, i_destZ, i_totalTravelTime);
103 return i_totalTravelTime;
106 template<typename TRAVELLER>
107 bool
108 DestinationHolder<TRAVELLER>::UpdateTraveller(TRAVELLER &traveller, uint32 diff, bool force_update, bool micro_movement)
110 if(!micro_movement)
112 i_tracker.Update(diff);
113 i_timeElapsed += diff;
114 if( i_tracker.Passed() || force_update )
116 ResetUpdate();
117 if(!i_destSet) return true;
118 float x,y,z;
119 GetLocationNowNoMicroMovement(x, y, z);
120 if( x == -431602080 )
121 return false;
122 if( traveller.GetTraveller().GetPositionX() != x || traveller.GetTraveller().GetPositionY() != y )
124 float ori = traveller.GetTraveller().GetAngle(x, y);
125 traveller.Relocation(x, y, z, ori);
127 return true;
129 return false;
131 i_tracker.Update(diff);
132 i_timeElapsed += diff;
133 if( i_tracker.Passed() || force_update )
135 ResetUpdate();
136 if(!i_destSet) return true;
137 float x,y,z;
139 if(!traveller.GetTraveller().hasUnitState(UNIT_STAT_MOVING | UNIT_STAT_IN_FLIGHT))
140 return true;
142 if(traveller.GetTraveller().hasUnitState(UNIT_STAT_IN_FLIGHT))
143 GetLocationNow(traveller.GetTraveller().GetMapId() ,x, y, z, true); // Should repositione Object with right Coord, so I can bypass some Grid Relocation
144 else
145 GetLocationNow(traveller.GetTraveller().GetMapId(), x, y, z, false);
147 if( x == -431602080 )
148 return false;
150 if( traveller.GetTraveller().GetPositionX() != x || traveller.GetTraveller().GetPositionY() != y )
152 float ori = traveller.GetTraveller().GetAngle(x, y);
153 traveller.Relocation(x, y, z, ori);
155 // Change movement computation to micro movement based on last tick coords, this makes system work
156 // even on multiple floors zones without hugh vmaps usage ;)
158 // Take care of underrun of uint32
159 if (i_totalTravelTime >= i_timeElapsed)
160 i_totalTravelTime -= i_timeElapsed; // Consider only the remaining part
161 else
162 i_totalTravelTime = 0;
164 i_timeElapsed = 0;
165 i_fromX = x; // and change origine
166 i_fromY = y; // then I take into account only micro movement
167 i_fromZ = z;
168 return true;
170 return false;
173 template<typename TRAVELLER>
174 void
175 DestinationHolder<TRAVELLER>::GetLocationNow(uint32 mapid, float &x, float &y, float &z, bool is3D) const
177 if( HasArrived() )
179 x = i_destX;
180 y = i_destY;
181 z = i_destZ;
183 else if(HasDestination())
185 double percent_passed = (double)i_timeElapsed / (double)i_totalTravelTime;
186 const float distanceX = ((i_destX - i_fromX) * percent_passed);
187 const float distanceY = ((i_destY - i_fromY) * percent_passed);
188 const float distanceZ = ((i_destZ - i_fromZ) * percent_passed);
189 x = i_fromX + distanceX;
190 y = i_fromY + distanceY;
191 float z2 = i_fromZ + distanceZ;
192 // All that is not finished but previous code neither... Traveller need be able to swim.
193 if(is3D)
194 z = z2;
195 else
197 //That part is good for mob Walking on the floor. But the floor is not allways what we thought.
198 z = MapManager::Instance().GetBaseMap(mapid)->GetHeight(x,y,i_fromZ,false); // Disable cave check
199 const float groundDist = sqrt(distanceX*distanceX + distanceY*distanceY);
200 const float zDist = fabs(i_fromZ - z) + 0.000001f;
201 const float slope = groundDist / zDist;
202 if(slope < 1.0f) // This prevents the ground returned by GetHeight to be used when in cave
203 z = z2; // a climb or jump of more than 45 is denied
208 template<typename TRAVELLER>
209 float
210 DestinationHolder<TRAVELLER>::GetDistance2dFromDestSq(const WorldObject &obj) const
212 float x,y,z;
213 obj.GetPosition(x,y,z);
214 return (i_destX-x)*(i_destX-x)+(i_destY-y)*(i_destY-y);
217 template<typename TRAVELLER>
218 float
219 DestinationHolder<TRAVELLER>::GetDestinationDiff(float x, float y, float z) const
221 return sqrt(((x-i_destX)*(x-i_destX)) + ((y-i_destY)*(y-i_destY)) + ((z-i_destZ)*(z-i_destZ)));
224 template<typename TRAVELLER>
225 void
226 DestinationHolder<TRAVELLER>::GetLocationNowNoMicroMovement(float &x, float &y, float &z) const
228 if( HasArrived() )
230 x = i_destX;
231 y = i_destY;
232 z = i_destZ;
234 else
236 double percent_passed = (double)i_timeElapsed / (double)i_totalTravelTime;
237 x = i_fromX + ((i_destX - i_fromX) * percent_passed);
238 y = i_fromY + ((i_destY - i_fromY) * percent_passed);
239 z = i_fromZ + ((i_destZ - i_fromZ) * percent_passed);
243 #endif