root/trunk/src/game/DestinationHolderImp.h @ 39

Revision 2, 8.2 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef MANGOS_DESTINATIONHOLDERIMP_H
20#define MANGOS_DESTINATIONHOLDERIMP_H
21
22#include "Creature.h"
23#include "MapManager.h"
24#include "DestinationHolder.h"
25
26#include <cmath>
27
28template<typename TRAVELLER>
29void
30DestinationHolder<TRAVELLER>::_findOffSetPoint(float x1, float y1, float x2, float y2, float offset, float &x, float &y)
31{
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);
37     */
38    if( offset == 0 )
39    {
40        x = x2;
41        y = y2;
42    }
43    else
44    {
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));
48        if(distance_d == 0)
49        {
50            x = x2;
51            y = y2;
52        }
53        else
54        {
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));
60        }
61    }
62}
63
64template<typename TRAVELLER>
65uint32
66DestinationHolder<TRAVELLER>::SetDestination(TRAVELLER &traveller, float dest_x, float dest_y, float dest_z, bool sendMove)
67{
68    i_destSet = true;
69    i_destX = dest_x;
70    i_destY = dest_y;
71    i_destZ = dest_z;
72
73    return StartTravel(traveller, sendMove);
74}
75
76template<typename TRAVELLER>
77uint32
78DestinationHolder<TRAVELLER>::StartTravel(TRAVELLER &traveller, bool sendMove)
79{
80    if(!i_destSet) return 0;
81
82    i_fromX = traveller.GetPositionX();
83    i_fromY = traveller.GetPositionY();
84    i_fromZ = traveller.GetPositionZ();
85
86    float dx = i_destX - i_fromX;
87    float dy = i_destY - i_fromY;
88    float dz = i_destZ - i_fromZ;
89
90    float dist;
91    //Should be for Creature Flying and Swimming.
92    if(traveller.GetTraveller().hasUnitState(UNIT_STAT_IN_FLIGHT))
93        dist = sqrt((dx*dx) + (dy*dy) + (dz*dz));
94    else                                                    //Walking on the ground
95        dist = sqrt((dx*dx) + (dy*dy));
96    float speed = traveller.Speed();
97
98    speed *=  0.001f;                                       // speed is in seconds so convert from second to millisecond
99    i_totalTravelTime = static_cast<uint32>(dist/speed);
100    i_timeElapsed = 0;
101    if(sendMove)
102        traveller.MoveTo(i_destX, i_destY, i_destZ, i_totalTravelTime);
103    return i_totalTravelTime;
104}
105
106template<typename TRAVELLER>
107bool
108DestinationHolder<TRAVELLER>::UpdateTraveller(TRAVELLER &traveller, uint32 diff, bool force_update, bool micro_movement)
109{
110    if(!micro_movement)
111    {
112        i_tracker.Update(diff);
113        i_timeElapsed += diff;
114        if( i_tracker.Passed() || force_update )
115        {
116            ResetUpdate();
117            if(!i_destSet) return true;
118            float x,y,z;
119            GetLocationNowNoMicroMovement(x, y, z);
120            if( x == -431602080 )
121                return false;
122            if( traveller.GetTraveller().GetPositionX() != x || traveller.GetTraveller().GetPositionY() != y )
123            {
124                float ori = traveller.GetTraveller().GetAngle(x, y);
125                traveller.Relocation(x, y, z, ori);
126            }
127            return true;
128        }
129        return false;
130    }
131    i_tracker.Update(diff);
132    i_timeElapsed += diff;
133    if( i_tracker.Passed() || force_update )
134    {
135        ResetUpdate();
136        if(!i_destSet) return true;
137        float x,y,z;
138
139        if(!traveller.GetTraveller().hasUnitState(UNIT_STAT_MOVING | UNIT_STAT_IN_FLIGHT))
140            return true;
141
142        if(traveller.GetTraveller().hasUnitState(UNIT_STAT_IN_FLIGHT))
143            GetLocationNow(traveller.GetTraveller().GetMapId() ,x, y, z, true);                  // Should repositione Object with right Coord, so I can bypass some Grid Relocation
144        else
145            GetLocationNow(traveller.GetTraveller().GetMapId(), x, y, z, false);
146
147        if( x == -431602080 )
148            return false;
149
150        if( traveller.GetTraveller().GetPositionX() != x || traveller.GetTraveller().GetPositionY() != y )
151        {
152            float ori = traveller.GetTraveller().GetAngle(x, y);
153            traveller.Relocation(x, y, z, ori);
154        }
155        // Change movement computation to micro movement based on last tick coords, this makes system work
156        // even on multiple floors zones without hugh vmaps usage ;)
157       
158        // Take care of underrun of uint32
159        if (i_totalTravelTime >= i_timeElapsed)
160            i_totalTravelTime -= i_timeElapsed;     // Consider only the remaining part
161        else
162            i_totalTravelTime = 0;
163       
164        i_timeElapsed = 0;
165        i_fromX = x;                            // and change origine
166        i_fromY = y;                            // then I take into account only micro movement
167        i_fromZ = z;
168        return true;
169    }
170    return false;
171}
172
173template<typename TRAVELLER>
174void
175DestinationHolder<TRAVELLER>::GetLocationNow(uint32 mapid, float &x, float &y, float &z, bool is3D) const
176{
177    if( HasArrived() )
178    {
179        x = i_destX;
180        y = i_destY;
181        z = i_destZ;
182    }
183    else if(HasDestination())
184    {
185        double percent_passed = (double)i_timeElapsed / (double)i_totalTravelTime;
186        const float distanceX = ((i_destX - i_fromX) * percent_passed);
187        const float distanceY = ((i_destY - i_fromY) * percent_passed);
188        const float distanceZ = ((i_destZ - i_fromZ) * percent_passed);
189        x = i_fromX + distanceX;
190        y = i_fromY + distanceY;
191        float z2 = i_fromZ + distanceZ;
192        // All that is not finished but previous code neither... Traveller need be able to swim.
193        if(is3D)
194            z = z2;
195        else
196        {
197            //That part is good for mob Walking on the floor. But the floor is not allways what we thought.
198            z = MapManager::Instance().GetBaseMap(mapid)->GetHeight(x,y,i_fromZ,false); // Disable cave check
199            const float groundDist = sqrt(distanceX*distanceX + distanceY*distanceY);
200            const float zDist = fabs(i_fromZ - z) + 0.000001f;
201            const float slope = groundDist / zDist;
202            if(slope < 1.0f)  // This prevents the ground returned by GetHeight to be used when in cave
203                z = z2; // a climb or jump of more than 45 is denied
204        }
205    }
206}
207
208template<typename TRAVELLER>
209float
210DestinationHolder<TRAVELLER>::GetDistance2dFromDestSq(const WorldObject &obj) const
211{
212    float x,y,z;
213    obj.GetPosition(x,y,z);
214    return (i_destX-x)*(i_destX-x)+(i_destY-y)*(i_destY-y);
215}
216
217template<typename TRAVELLER>
218float
219DestinationHolder<TRAVELLER>::GetDestinationDiff(float x, float y, float z) const
220{
221    return sqrt(((x-i_destX)*(x-i_destX)) + ((y-i_destY)*(y-i_destY)) + ((z-i_destZ)*(z-i_destZ)));
222}
223
224template<typename TRAVELLER>
225void
226DestinationHolder<TRAVELLER>::GetLocationNowNoMicroMovement(float &x, float &y, float &z) const
227{
228    if( HasArrived() )
229    {
230        x = i_destX;
231        y = i_destY;
232        z = i_destZ;
233    }
234    else
235    {
236        double percent_passed = (double)i_timeElapsed / (double)i_totalTravelTime;
237        x = i_fromX + ((i_destX - i_fromX) * percent_passed);
238        y = i_fromY + ((i_destY - i_fromY) * percent_passed);
239        z = i_fromZ + ((i_destZ - i_fromZ) * percent_passed);
240    }
241}
242
243#endif
Note: See TracBrowser for help on using the browser.