[7921] Set pet and for creature owner in Spell::EffectSummon similar other summon...
[getmangos.git] / src / game / DestinationHolderImp.h
blob0db69fdde8db56dd45d7e225515afcf550d472dc
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;
104 float x,y,z;
105 GetLocationNowNoMicroMovement(x, y, z);
106 if( x == -431602080 )
107 return false;
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);
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;
123 float x,y,z;
125 if(!traveller.GetTraveller().hasUnitState(UNIT_STAT_MOVING | UNIT_STAT_IN_FLIGHT))
126 return true;
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
130 else
131 GetLocationNow(traveller.GetTraveller().GetMapId(), x, y, z, false);
133 if( x == -431602080 )
134 return false;
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
147 else
148 i_totalTravelTime = 0;
150 i_timeElapsed = 0;
151 i_fromX = x; // and change origine
152 i_fromY = y; // then I take into account only micro movement
153 i_fromZ = z;
154 return true;
156 return false;
159 template<typename TRAVELLER>
160 void
161 DestinationHolder<TRAVELLER>::GetLocationNow(uint32 mapid, float &x, float &y, float &z, bool is3D) const
163 if( HasArrived() )
165 x = i_destX;
166 y = i_destY;
167 z = i_destZ;
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.
179 if(is3D)
180 z = z2;
181 else
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>
195 float
196 DestinationHolder<TRAVELLER>::GetDistance2dFromDestSq(const WorldObject &obj) const
198 float x,y,z;
199 obj.GetPosition(x,y,z);
200 return (i_destX-x)*(i_destX-x)+(i_destY-y)*(i_destY-y);
203 template<typename TRAVELLER>
204 float
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>
211 void
212 DestinationHolder<TRAVELLER>::GetLocationNowNoMicroMovement(float &x, float &y, float &z) const
214 if( HasArrived() )
216 x = i_destX;
217 y = i_destY;
218 z = i_destZ;
220 else
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);
229 #endif