2 * Copyright (C) 2005,2006 MaNGOS <http://www.mangosproject.org/>
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
23 #include "MapManager.h"
24 #include "DestinationHolder.h"
28 template<typename TRAVELLER
>
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);
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
));
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
>
66 DestinationHolder
<TRAVELLER
>::SetDestination(TRAVELLER
&traveller
, float dest_x
, float dest_y
, float dest_z
, float offset
)
68 if (i_destX
== dest_x
&& i_destY
== dest_y
&& i_destZ
== dest_z
)
71 i_fromX
= traveller
.GetPositionX();
72 i_fromY
= traveller
.GetPositionY();
73 i_fromZ
= traveller
.GetPositionZ();
75 UpdateLocation(traveller
, dest_x
, dest_y
, dest_z
);
77 float dx
= dest_x
- i_fromX
;
78 float dy
= dest_y
- i_fromY
;
79 float dz
= dest_z
- i_fromZ
;
80 float dist
= sqrt((dx
*dx
) + (dy
*dy
) + (dz
*dz
));
81 double speed
= traveller
.Speed();
85 uint32 travel_time
= static_cast<uint32
>(dist
/ speed
+ 0.5);
86 traveller
.MoveTo(dest_x
, dest_y
, dest_z
, travel_time
);
90 template<typename TRAVELLER
>
92 DestinationHolder
<TRAVELLER
>::UpdateLocation(TRAVELLER
&traveller
, float dest_x
, float dest_y
, float dest_z
)
98 float dx
= i_destX
- i_fromX
;
99 float dy
= i_destY
- i_fromY
;
100 float dz
= i_destZ
- i_fromZ
;
101 double dist
= ::sqrt((dx
*dx
) + (dy
*dy
) + (dz
*dz
));
102 double speed
= traveller
.Speed();
105 speed
*= 0.001f
; // speed is in seconds so convert from second to millisecond
106 i_totalTravelTime
= static_cast<uint32
>( dist
/speed
+ 0.5 );
107 i_timeStarted
= getMSTime();
110 template<typename TRAVELLER
>
112 DestinationHolder
<TRAVELLER
>::UpdateTraveller(TRAVELLER
&traveller
, uint32 diff
, bool force_update
)
114 i_tracker
.Update(diff
);
115 if( i_tracker
.Passed() || force_update
)
119 GetLocationNow(x
, y
, z
);
120 if( x
== -431602080 )
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
);
132 template<typename TRAVELLER
>
134 DestinationHolder
<TRAVELLER
>::GetLocationNow(float &x
, float &y
, float &z
) const
136 uint32 time_elapsed
= getMSTime() - i_timeStarted
;
138 if( i_totalTravelTime
== 0 || time_elapsed
>= i_totalTravelTime
)
146 double percent_passed
= (double)time_elapsed
/ (double)i_totalTravelTime
;
147 x
= i_fromX
+ ((i_destX
- i_fromX
) * percent_passed
);
148 y
= i_fromY
+ ((i_destY
- i_fromY
) * percent_passed
);
149 z
= i_fromZ
+ ((i_destZ
- i_fromZ
) * percent_passed
);
153 template<typename TRAVELLER
>
155 DestinationHolder
<TRAVELLER
>::GetDistanceFromDestSq(const Object
&obj
) const
158 obj
.GetPosition(x
,y
,z
);
159 return (i_destX
-x
)*(i_destX
-x
)+(i_destY
-y
)*(i_destY
-y
)+(i_destZ
-z
)*(i_destZ
-z
);