1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef TRINITY_DESTINATIONHOLDERIMP_H |
---|
22 | #define TRINITY_DESTINATIONHOLDERIMP_H |
---|
23 | |
---|
24 | #include "Creature.h" |
---|
25 | #include "MapManager.h" |
---|
26 | #include "DestinationHolder.h" |
---|
27 | |
---|
28 | #include <cmath> |
---|
29 | |
---|
30 | template<typename TRAVELLER> |
---|
31 | void |
---|
32 | DestinationHolder<TRAVELLER>::_findOffSetPoint(float x1, float y1, float x2, float y2, float offset, float &x, float &y) |
---|
33 | { |
---|
34 | /* given the point (x1, y1) and (x2, y2).. need to find the point (x,y) on the same line |
---|
35 | * such that the distance from (x, y) to (x2, y2) is offset. |
---|
36 | * Let the distance of p1 to p2 = d.. then the ratio of offset/d = (x2-x)/(x2-x1) |
---|
37 | * hence x = x2 - (offset/d)*(x2-x1) |
---|
38 | * like wise offset/d = (y2-y)/(y2-y1); |
---|
39 | */ |
---|
40 | if( offset == 0 ) |
---|
41 | { |
---|
42 | x = x2; |
---|
43 | y = y2; |
---|
44 | } |
---|
45 | else |
---|
46 | { |
---|
47 | double x_diff = double(x2 - x1); |
---|
48 | double y_diff = double(y2 - y1); |
---|
49 | double distance_d = (double)((x_diff*x_diff) + (y_diff * y_diff)); |
---|
50 | if(distance_d == 0) |
---|
51 | { |
---|
52 | x = x2; |
---|
53 | y = y2; |
---|
54 | } |
---|
55 | else |
---|
56 | { |
---|
57 | distance_d = ::sqrt(distance_d); // starting distance |
---|
58 | double distance_ratio = (double)(distance_d - offset)/(double)distance_d; |
---|
59 | // line above has revised formula which is more correct, I think |
---|
60 | x = (float)(x1 + (distance_ratio*x_diff)); |
---|
61 | y = (float)(y1 + (distance_ratio*y_diff)); |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | template<typename TRAVELLER> |
---|
67 | uint32 |
---|
68 | DestinationHolder<TRAVELLER>::SetDestination(TRAVELLER &traveller, float dest_x, float dest_y, float dest_z, bool sendMove) |
---|
69 | { |
---|
70 | i_destSet = true; |
---|
71 | i_destX = dest_x; |
---|
72 | i_destY = dest_y; |
---|
73 | i_destZ = dest_z; |
---|
74 | |
---|
75 | return StartTravel(traveller, sendMove); |
---|
76 | } |
---|
77 | |
---|
78 | template<typename TRAVELLER> |
---|
79 | uint32 |
---|
80 | DestinationHolder<TRAVELLER>::StartTravel(TRAVELLER &traveller, bool sendMove) |
---|
81 | { |
---|
82 | if(!i_destSet) return 0; |
---|
83 | |
---|
84 | i_fromX = traveller.GetPositionX(); |
---|
85 | i_fromY = traveller.GetPositionY(); |
---|
86 | i_fromZ = traveller.GetPositionZ(); |
---|
87 | |
---|
88 | float dx = i_destX - i_fromX; |
---|
89 | float dy = i_destY - i_fromY; |
---|
90 | float dz = i_destZ - i_fromZ; |
---|
91 | |
---|
92 | float dist; |
---|
93 | //Should be for Creature Flying and Swimming. |
---|
94 | if(traveller.GetTraveller().hasUnitState(UNIT_STAT_IN_FLIGHT)) |
---|
95 | dist = sqrt((dx*dx) + (dy*dy) + (dz*dz)); |
---|
96 | else //Walking on the ground |
---|
97 | dist = sqrt((dx*dx) + (dy*dy)); |
---|
98 | float speed = traveller.Speed(); |
---|
99 | |
---|
100 | speed *= 0.001f; // speed is in seconds so convert from second to millisecond |
---|
101 | i_totalTravelTime = static_cast<uint32>(dist/speed); |
---|
102 | i_timeElapsed = 0; |
---|
103 | if(sendMove) |
---|
104 | traveller.MoveTo(i_destX, i_destY, i_destZ, i_totalTravelTime); |
---|
105 | return i_totalTravelTime; |
---|
106 | } |
---|
107 | |
---|
108 | template<typename TRAVELLER> |
---|
109 | bool |
---|
110 | DestinationHolder<TRAVELLER>::UpdateTraveller(TRAVELLER &traveller, uint32 diff, bool force_update, bool micro_movement) |
---|
111 | { |
---|
112 | if(!micro_movement) |
---|
113 | { |
---|
114 | i_tracker.Update(diff); |
---|
115 | i_timeElapsed += diff; |
---|
116 | if( i_tracker.Passed() || force_update ) |
---|
117 | { |
---|
118 | ResetUpdate(); |
---|
119 | if(!i_destSet) return true; |
---|
120 | float x,y,z; |
---|
121 | GetLocationNowNoMicroMovement(x, y, z); |
---|
122 | if( x == -431602080 ) |
---|
123 | return false; |
---|
124 | if( traveller.GetTraveller().GetPositionX() != x || traveller.GetTraveller().GetPositionY() != y ) |
---|
125 | { |
---|
126 | float ori = traveller.GetTraveller().GetAngle(x, y); |
---|
127 | traveller.Relocation(x, y, z, ori); |
---|
128 | } |
---|
129 | return true; |
---|
130 | } |
---|
131 | return false; |
---|
132 | } |
---|
133 | i_tracker.Update(diff); |
---|
134 | i_timeElapsed += diff; |
---|
135 | if( i_tracker.Passed() || force_update ) |
---|
136 | { |
---|
137 | ResetUpdate(); |
---|
138 | if(!i_destSet) return true; |
---|
139 | float x,y,z; |
---|
140 | |
---|
141 | if(!traveller.GetTraveller().hasUnitState(UNIT_STAT_MOVING | UNIT_STAT_IN_FLIGHT)) |
---|
142 | return true; |
---|
143 | |
---|
144 | if(traveller.GetTraveller().hasUnitState(UNIT_STAT_IN_FLIGHT)) |
---|
145 | GetLocationNow(traveller.GetTraveller().GetMapId() ,x, y, z, true); // Should repositione Object with right Coord, so I can bypass some Grid Relocation |
---|
146 | else |
---|
147 | GetLocationNow(traveller.GetTraveller().GetMapId(), x, y, z, false); |
---|
148 | |
---|
149 | if( x == -431602080 ) |
---|
150 | return false; |
---|
151 | |
---|
152 | if( traveller.GetTraveller().GetPositionX() != x || traveller.GetTraveller().GetPositionY() != y ) |
---|
153 | { |
---|
154 | float ori = traveller.GetTraveller().GetAngle(x, y); |
---|
155 | traveller.Relocation(x, y, z, ori); |
---|
156 | } |
---|
157 | // Change movement computation to micro movement based on last tick coords, this makes system work |
---|
158 | // even on multiple floors zones without hugh vmaps usage ;) |
---|
159 | |
---|
160 | // Take care of underrun of uint32 |
---|
161 | if (i_totalTravelTime >= i_timeElapsed) |
---|
162 | i_totalTravelTime -= i_timeElapsed; // Consider only the remaining part |
---|
163 | else |
---|
164 | i_totalTravelTime = 0; |
---|
165 | |
---|
166 | i_timeElapsed = 0; |
---|
167 | i_fromX = x; // and change origine |
---|
168 | i_fromY = y; // then I take into account only micro movement |
---|
169 | i_fromZ = z; |
---|
170 | return true; |
---|
171 | } |
---|
172 | return false; |
---|
173 | } |
---|
174 | |
---|
175 | template<typename TRAVELLER> |
---|
176 | void |
---|
177 | DestinationHolder<TRAVELLER>::GetLocationNow(uint32 mapid, float &x, float &y, float &z, bool is3D) const |
---|
178 | { |
---|
179 | if( HasArrived() ) |
---|
180 | { |
---|
181 | x = i_destX; |
---|
182 | y = i_destY; |
---|
183 | z = i_destZ; |
---|
184 | } |
---|
185 | else if(HasDestination()) |
---|
186 | { |
---|
187 | double percent_passed = (double)i_timeElapsed / (double)i_totalTravelTime; |
---|
188 | const float distanceX = ((i_destX - i_fromX) * percent_passed); |
---|
189 | const float distanceY = ((i_destY - i_fromY) * percent_passed); |
---|
190 | const float distanceZ = ((i_destZ - i_fromZ) * percent_passed); |
---|
191 | x = i_fromX + distanceX; |
---|
192 | y = i_fromY + distanceY; |
---|
193 | float z2 = i_fromZ + distanceZ; |
---|
194 | // All that is not finished but previous code neither... Traveller need be able to swim. |
---|
195 | if(is3D) |
---|
196 | z = z2; |
---|
197 | else |
---|
198 | { |
---|
199 | //That part is good for mob Walking on the floor. But the floor is not allways what we thought. |
---|
200 | z = MapManager::Instance().GetBaseMap(mapid)->GetHeight(x,y,i_fromZ,false); // Disable cave check |
---|
201 | const float groundDist = sqrt(distanceX*distanceX + distanceY*distanceY); |
---|
202 | const float zDist = fabs(i_fromZ - z) + 0.000001f; |
---|
203 | const float slope = groundDist / zDist; |
---|
204 | if(slope < 1.0f) // This prevents the ground returned by GetHeight to be used when in cave |
---|
205 | z = z2; // a climb or jump of more than 45 is denied |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | template<typename TRAVELLER> |
---|
211 | float |
---|
212 | DestinationHolder<TRAVELLER>::GetDistance2dFromDestSq(const WorldObject &obj) const |
---|
213 | { |
---|
214 | float x,y,z; |
---|
215 | obj.GetPosition(x,y,z); |
---|
216 | return (i_destX-x)*(i_destX-x)+(i_destY-y)*(i_destY-y); |
---|
217 | } |
---|
218 | |
---|
219 | template<typename TRAVELLER> |
---|
220 | float |
---|
221 | DestinationHolder<TRAVELLER>::GetDestinationDiff(float x, float y, float z) const |
---|
222 | { |
---|
223 | return sqrt(((x-i_destX)*(x-i_destX)) + ((y-i_destY)*(y-i_destY)) + ((z-i_destZ)*(z-i_destZ))); |
---|
224 | } |
---|
225 | |
---|
226 | template<typename TRAVELLER> |
---|
227 | void |
---|
228 | DestinationHolder<TRAVELLER>::GetLocationNowNoMicroMovement(float &x, float &y, float &z) const |
---|
229 | { |
---|
230 | if( HasArrived() ) |
---|
231 | { |
---|
232 | x = i_destX; |
---|
233 | y = i_destY; |
---|
234 | z = i_destZ; |
---|
235 | } |
---|
236 | else |
---|
237 | { |
---|
238 | double percent_passed = (double)i_timeElapsed / (double)i_totalTravelTime; |
---|
239 | x = i_fromX + ((i_destX - i_fromX) * percent_passed); |
---|
240 | y = i_fromY + ((i_destY - i_fromY) * percent_passed); |
---|
241 | z = i_fromZ + ((i_destZ - i_fromZ) * percent_passed); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | #endif |
---|