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