root/trunk/src/game/RandomMovementGenerator.cpp @ 17

Revision 2, 5.8 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#include "Creature.h"
20#include "MapManager.h"
21#include "RandomMovementGenerator.h"
22#include "DestinationHolderImp.h"
23#include "Map.h"
24#include "Util.h"
25
26#define RUNNING_CHANCE_RANDOMMV 20                                  //will be "1 / RUNNING_CHANCE_RANDOMMV"
27
28template<>
29void
30RandomMovementGenerator<Creature>::_setRandomLocation(Creature &creature)
31{
32    float X,Y,Z,z,nx,ny,nz,wander_distance,ori,dist;
33
34    creature.GetRespawnCoord(X, Y, Z);
35    creature.GetRespawnCoord(X, Y, Z, &ori, &wander_distance);
36
37    z = creature.GetPositionZ();
38    uint32 mapid=creature.GetMapId();
39    Map const* map = MapManager::Instance().GetBaseMap(mapid);
40
41    // For 2D/3D system selection
42    bool is_land_ok  = creature.canWalk();
43    bool is_water_ok = creature.canSwim();
44    bool is_air_ok   = creature.canFly();
45
46    const float angle = rand_norm()*(M_PI*2);
47    const float range = rand_norm()*wander_distance;
48    const float distanceX = range * cos(angle);
49    const float distanceY = range * sin(angle);
50
51    nx = X + distanceX;
52    ny = Y + distanceY;
53    dist = distanceX*distanceX + distanceY*distanceY;
54
55    if (is_air_ok) // 3D system above ground and above water (flying mode)
56    {
57        const float distanceZ = rand_norm() * sqrtf(dist)/2; // Limit height change
58        nz = Z + distanceZ;
59        float tz = map->GetHeight(nx, ny, nz-2.0f, false); // Map check only, vmap needed here but need to alter vmaps checks for height.
60        float wz = map->GetWaterLevel(nx, ny);
61        if (tz >= nz || wz >= nz)
62            return; // Problem here, we must fly above the ground and water, not under. Let's try on next tick
63    }
64    //else if (is_water_ok) // 3D system under water and above ground (swimming mode)
65    else // 2D only
66    {
67        dist = dist>=100.0f ? 10.0f : sqrtf(dist); // 10.0 is the max that vmap high can check (MAX_CAN_FALL_DISTANCE)
68        // The fastest way to get an accurate result 90% of the time.
69        // Better result can be obtained like 99% accuracy with a ray light, but the cost is too high and the code is too long.
70        nz = map->GetHeight(nx,ny,Z+dist-2.0f,false); // Map check
71        if (fabs(nz-Z)>dist)
72        {
73            nz = map->GetHeight(nx,ny,Z-2.0f,true); // Vmap Horizontal or above
74            if (fabs(nz-Z)>dist)
75            {
76                nz = map->GetHeight(nx,ny,Z+dist-2.0f,true); // Vmap Higher
77                if (fabs(nz-Z)>dist)
78                    return; // let's forget this bad coords where a z cannot be find and retry at next tick
79            }
80        }
81    }
82
83    Traveller<Creature> traveller(creature);
84    creature.SetOrientation(creature.GetAngle(nx,ny));
85    i_destinationHolder.SetDestination(traveller, nx, ny, nz);
86    creature.addUnitState(UNIT_STAT_ROAMING);
87    if (is_air_ok)
88    {
89        i_nextMoveTime.Reset(i_destinationHolder.GetTotalTravelTime());
90        creature.SetUnitMovementFlags(MOVEMENTFLAG_FLYING2);
91    }
92    //else if (is_water_ok) // Swimming mode to be done with more than this check
93    else
94    {
95        i_nextMoveTime.Reset(urand(500+i_destinationHolder.GetTotalTravelTime(),5000+i_destinationHolder.GetTotalTravelTime()));
96        creature.SetUnitMovementFlags(MOVEMENTFLAG_WALK_MODE);
97    }
98}
99
100template<>
101void
102RandomMovementGenerator<Creature>::Initialize(Creature &creature)
103{
104    if(!creature.isAlive())
105        return;
106
107    if (creature.canFly())
108        creature.SetUnitMovementFlags(MOVEMENTFLAG_FLYING2);
109    else
110        creature.SetUnitMovementFlags(irand(0,RUNNING_CHANCE_RANDOMMV) > 0 ? MOVEMENTFLAG_WALK_MODE : MOVEMENTFLAG_NONE );
111    _setRandomLocation(creature);
112}
113
114template<>
115void
116RandomMovementGenerator<Creature>::Reset(Creature &creature)
117{
118    Initialize(creature);
119}
120
121template<>
122bool
123RandomMovementGenerator<Creature>::Update(Creature &creature, const uint32 &diff)
124{
125    if(creature.hasUnitState(UNIT_STAT_ROOT | UNIT_STAT_STUNDED | UNIT_STAT_DISTRACTED))
126    {
127        i_nextMoveTime.Update(i_nextMoveTime.GetExpiry());    // Expire the timer
128        creature.clearUnitState(UNIT_STAT_ROAMING);
129        return true;
130    }
131
132    i_nextMoveTime.Update(diff);
133
134    if(i_destinationHolder.HasArrived() && !creature.IsStopped() && !creature.canFly())
135        creature.clearUnitState(UNIT_STAT_ROAMING);
136
137    if(!i_destinationHolder.HasArrived() && creature.IsStopped())
138        creature.addUnitState(UNIT_STAT_ROAMING);
139
140    CreatureTraveller traveller(creature);
141
142    if( i_destinationHolder.UpdateTraveller(traveller, diff, false, true) )
143    {
144        if(i_nextMoveTime.Passed())
145        {
146            if (creature.canFly())
147                creature.SetUnitMovementFlags(MOVEMENTFLAG_FLYING2);
148            else
149                creature.SetUnitMovementFlags(irand(0,RUNNING_CHANCE_RANDOMMV) > 0 ? MOVEMENTFLAG_WALK_MODE : MOVEMENTFLAG_NONE);
150            _setRandomLocation(creature);
151        }
152        else if(creature.isPet() && creature.GetOwner() && creature.GetDistance(creature.GetOwner()) > PET_FOLLOW_DIST+2.5f)
153        {
154           creature.SetUnitMovementFlags(MOVEMENTFLAG_NONE);
155           _setRandomLocation(creature);
156        }
157    }
158    return true;
159}
Note: See TracBrowser for help on using the browser.