[8483] Implement glyph 43361.
[getmangos.git] / src / game / DestinationHolderImp.h
blob1340e0d1bf1d712128e8240ed1486a1807ae2f7f
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 "MapManager.h"
23 #include "DestinationHolder.h"
25 #include <cmath>
27 template<typename TRAVELLER>
28 void
29 DestinationHolder<TRAVELLER>::_findOffSetPoint(float x1, float y1, float x2, float y2, float offset, float &x, float &y)
31 /* given the point (x1, y1) and (x2, y2).. need to find the point (x,y) on the same line
32 * such that the distance from (x, y) to (x2, y2) is offset.
33 * Let the distance of p1 to p2 = d.. then the ratio of offset/d = (x2-x)/(x2-x1)
34 * hence x = x2 - (offset/d)*(x2-x1)
35 * like wise offset/d = (y2-y)/(y2-y1);
37 if( offset == 0 )
39 x = x2;
40 y = y2;
42 else
44 double x_diff = double(x2 - x1);
45 double y_diff = double(y2 - y1);
46 double distance_d = (double)((x_diff*x_diff) + (y_diff * y_diff));
47 if(distance_d == 0)
49 x = x2;
50 y = y2;
52 else
54 distance_d = ::sqrt(distance_d); // starting distance
55 double distance_ratio = (double)(distance_d - offset)/(double)distance_d;
56 // line above has revised formula which is more correct, I think
57 x = (float)(x1 + (distance_ratio*x_diff));
58 y = (float)(y1 + (distance_ratio*y_diff));
63 template<typename TRAVELLER>
64 uint32
65 DestinationHolder<TRAVELLER>::SetDestination(TRAVELLER &traveller, float dest_x, float dest_y, float dest_z, bool sendMove)
67 i_destSet = true;
68 i_destX = dest_x;
69 i_destY = dest_y;
70 i_destZ = dest_z;
72 return StartTravel(traveller, sendMove);
75 template<typename TRAVELLER>
76 uint32
77 DestinationHolder<TRAVELLER>::StartTravel(TRAVELLER &traveller, bool sendMove)
79 if(!i_destSet) return 0;
81 i_fromX = traveller.GetPositionX();
82 i_fromY = traveller.GetPositionY();
83 i_fromZ = traveller.GetPositionZ();
85 i_totalTravelTime = traveller.GetTotalTrevelTimeTo(i_destX,i_destY,i_destZ);
86 i_timeElapsed = 0;
87 if(sendMove)
88 traveller.MoveTo(i_destX, i_destY, i_destZ, i_totalTravelTime);
89 return i_totalTravelTime;
92 template<typename TRAVELLER>
93 bool
94 DestinationHolder<TRAVELLER>::UpdateTraveller(TRAVELLER &traveller, uint32 diff, bool force_update, bool micro_movement)
96 if (!micro_movement)
98 i_tracker.Update(diff);
99 i_timeElapsed += diff;
100 if (i_tracker.Passed() || force_update)
102 ResetUpdate();
103 if (!i_destSet) return true;
105 float x,y,z;
106 GetLocationNowNoMicroMovement(x, y, z);
108 if (traveller.GetTraveller().GetPositionX() != x || traveller.GetTraveller().GetPositionY() != y || traveller.GetTraveller().GetPositionZ() != z)
110 float ori = traveller.GetTraveller().GetAngle(x, y);
111 traveller.Relocation(x, y, z, ori);
113 return true;
115 return false;
117 i_tracker.Update(diff);
118 i_timeElapsed += diff;
119 if (i_tracker.Passed() || force_update)
121 ResetUpdate();
122 if (!i_destSet) return true;
124 if (!traveller.GetTraveller().hasUnitState(UNIT_STAT_MOVING | UNIT_STAT_IN_FLIGHT))
125 return true;
127 float x,y,z;
128 if (traveller.GetTraveller().hasUnitState(UNIT_STAT_IN_FLIGHT))
129 GetLocationNow(traveller.GetTraveller().GetBaseMap() ,x, y, z, true); // Should reposition Object with right Coord, so I can bypass some Grid Relocation
130 else
131 GetLocationNow(traveller.GetTraveller().GetBaseMap(), x, y, z, false);
133 if (traveller.GetTraveller().GetPositionX() != x || traveller.GetTraveller().GetPositionY() != y || traveller.GetTraveller().GetPositionZ() != z)
135 float ori = traveller.GetTraveller().GetAngle(x, y);
136 traveller.Relocation(x, y, z, ori);
138 // Change movement computation to micro movement based on last tick coords, this makes system work
139 // even on multiple floors zones without hugh vmaps usage ;)
141 // Take care of underrun of uint32
142 if (i_totalTravelTime >= i_timeElapsed)
143 i_totalTravelTime -= i_timeElapsed; // Consider only the remaining part
144 else
145 i_totalTravelTime = 0;
147 i_timeElapsed = 0;
148 i_fromX = x; // and change origine
149 i_fromY = y; // then I take into account only micro movement
150 i_fromZ = z;
151 return true;
153 return false;
156 template<typename TRAVELLER>
157 void
158 DestinationHolder<TRAVELLER>::GetLocationNow(const Map * map, float &x, float &y, float &z, bool is3D) const
160 if (HasArrived())
162 x = i_destX;
163 y = i_destY;
164 z = i_destZ;
166 else if (HasDestination())
168 double percent_passed = (double)i_timeElapsed / (double)i_totalTravelTime;
169 const float distanceX = ((i_destX - i_fromX) * percent_passed);
170 const float distanceY = ((i_destY - i_fromY) * percent_passed);
171 const float distanceZ = ((i_destZ - i_fromZ) * percent_passed);
172 x = i_fromX + distanceX;
173 y = i_fromY + distanceY;
174 float z2 = i_fromZ + distanceZ;
175 // All that is not finished but previous code neither... Traveller need be able to swim.
176 if (is3D)
177 z = z2;
178 else
180 //That part is good for mob Walking on the floor. But the floor is not always what we thought.
181 z = map->GetHeight(x,y,i_fromZ,false); // Disable cave check
182 const float groundDist = sqrt(distanceX*distanceX + distanceY*distanceY);
183 const float zDist = fabs(i_fromZ - z) + 0.000001f;
184 const float slope = groundDist / zDist;
185 if(slope < 1.0f) // This prevents the ground returned by GetHeight to be used when in cave
186 z = z2; // a climb or jump of more than 45 is denied
191 template<typename TRAVELLER>
192 float
193 DestinationHolder<TRAVELLER>::GetDistance3dFromDestSq(const WorldObject &obj) const
195 float x,y,z;
196 obj.GetPosition(x,y,z);
197 return (i_destX-x)*(i_destX-x)+(i_destY-y)*(i_destY-y)+(i_destZ-z)*(i_destZ-z);
200 template<typename TRAVELLER>
201 float
202 DestinationHolder<TRAVELLER>::GetDestinationDiff(float x, float y, float z) const
204 return sqrt(((x-i_destX)*(x-i_destX)) + ((y-i_destY)*(y-i_destY)) + ((z-i_destZ)*(z-i_destZ)));
207 template<typename TRAVELLER>
208 void
209 DestinationHolder<TRAVELLER>::GetLocationNowNoMicroMovement(float &x, float &y, float &z) const
211 if (HasArrived())
213 x = i_destX;
214 y = i_destY;
215 z = i_destZ;
217 else
219 double percent_passed = (double)i_timeElapsed / (double)i_totalTravelTime;
220 x = i_fromX + ((i_destX - i_fromX) * percent_passed);
221 y = i_fromY + ((i_destY - i_fromY) * percent_passed);
222 z = i_fromZ + ((i_destZ - i_fromZ) * percent_passed);
226 #endif