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"
27 template<typename TRAVELLER
>
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);
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
));
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
>
65 DestinationHolder
<TRAVELLER
>::SetDestination(TRAVELLER
&traveller
, float dest_x
, float dest_y
, float dest_z
, bool sendMove
)
72 return StartTravel(traveller
, sendMove
);
75 template<typename TRAVELLER
>
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
);
88 traveller
.MoveTo(i_destX
, i_destY
, i_destZ
, i_totalTravelTime
);
89 return i_totalTravelTime
;
92 template<typename TRAVELLER
>
94 DestinationHolder
<TRAVELLER
>::UpdateTraveller(TRAVELLER
&traveller
, uint32 diff
, bool force_update
, bool micro_movement
)
98 i_tracker
.Update(diff
);
99 i_timeElapsed
+= diff
;
100 if( i_tracker
.Passed() || force_update
)
103 if(!i_destSet
) return true;
105 GetLocationNowNoMicroMovement(x
, y
, z
);
106 if( x
== -431602080 )
108 if( traveller
.GetTraveller().GetPositionX() != x
|| traveller
.GetTraveller().GetPositionY() != y
)
110 float ori
= traveller
.GetTraveller().GetAngle(x
, y
);
111 traveller
.Relocation(x
, y
, z
, ori
);
117 i_tracker
.Update(diff
);
118 i_timeElapsed
+= diff
;
119 if( i_tracker
.Passed() || force_update
)
122 if(!i_destSet
) return true;
125 if(!traveller
.GetTraveller().hasUnitState(UNIT_STAT_MOVING
| UNIT_STAT_IN_FLIGHT
))
128 if(traveller
.GetTraveller().hasUnitState(UNIT_STAT_IN_FLIGHT
))
129 GetLocationNow(traveller
.GetTraveller().GetMapId() ,x
, y
, z
, true); // Should repositione Object with right Coord, so I can bypass some Grid Relocation
131 GetLocationNow(traveller
.GetTraveller().GetMapId(), x
, y
, z
, false);
133 if( x
== -431602080 )
136 if( traveller
.GetTraveller().GetPositionX() != x
|| traveller
.GetTraveller().GetPositionY() != y
)
138 float ori
= traveller
.GetTraveller().GetAngle(x
, y
);
139 traveller
.Relocation(x
, y
, z
, ori
);
141 // Change movement computation to micro movement based on last tick coords, this makes system work
142 // even on multiple floors zones without hugh vmaps usage ;)
144 // Take care of underrun of uint32
145 if (i_totalTravelTime
>= i_timeElapsed
)
146 i_totalTravelTime
-= i_timeElapsed
; // Consider only the remaining part
148 i_totalTravelTime
= 0;
151 i_fromX
= x
; // and change origine
152 i_fromY
= y
; // then I take into account only micro movement
159 template<typename TRAVELLER
>
161 DestinationHolder
<TRAVELLER
>::GetLocationNow(uint32 mapid
, float &x
, float &y
, float &z
, bool is3D
) const
169 else if(HasDestination())
171 double percent_passed
= (double)i_timeElapsed
/ (double)i_totalTravelTime
;
172 const float distanceX
= ((i_destX
- i_fromX
) * percent_passed
);
173 const float distanceY
= ((i_destY
- i_fromY
) * percent_passed
);
174 const float distanceZ
= ((i_destZ
- i_fromZ
) * percent_passed
);
175 x
= i_fromX
+ distanceX
;
176 y
= i_fromY
+ distanceY
;
177 float z2
= i_fromZ
+ distanceZ
;
178 // All that is not finished but previous code neither... Traveller need be able to swim.
183 //That part is good for mob Walking on the floor. But the floor is not allways what we thought.
184 z
= MapManager::Instance().GetBaseMap(mapid
)->GetHeight(x
,y
,i_fromZ
,false); // Disable cave check
185 const float groundDist
= sqrt(distanceX
*distanceX
+ distanceY
*distanceY
);
186 const float zDist
= fabs(i_fromZ
- z
) + 0.000001f
;
187 const float slope
= groundDist
/ zDist
;
188 if(slope
< 1.0f
) // This prevents the ground returned by GetHeight to be used when in cave
189 z
= z2
; // a climb or jump of more than 45 is denied
194 template<typename TRAVELLER
>
196 DestinationHolder
<TRAVELLER
>::GetDistance2dFromDestSq(const WorldObject
&obj
) const
199 obj
.GetPosition(x
,y
,z
);
200 return (i_destX
-x
)*(i_destX
-x
)+(i_destY
-y
)*(i_destY
-y
);
203 template<typename TRAVELLER
>
205 DestinationHolder
<TRAVELLER
>::GetDestinationDiff(float x
, float y
, float z
) const
207 return sqrt(((x
-i_destX
)*(x
-i_destX
)) + ((y
-i_destY
)*(y
-i_destY
)) + ((z
-i_destZ
)*(z
-i_destZ
)));
210 template<typename TRAVELLER
>
212 DestinationHolder
<TRAVELLER
>::GetLocationNowNoMicroMovement(float &x
, float &y
, float &z
) const
222 double percent_passed
= (double)i_timeElapsed
/ (double)i_totalTravelTime
;
223 x
= i_fromX
+ ((i_destX
- i_fromX
) * percent_passed
);
224 y
= i_fromY
+ ((i_destY
- i_fromY
) * percent_passed
);
225 z
= i_fromZ
+ ((i_destZ
- i_fromZ
) * percent_passed
);