aboutsummaryrefslogtreecommitdiff
path: root/src/game/RandomMovementGenerator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game/RandomMovementGenerator.cpp')
-rw-r--r--src/game/RandomMovementGenerator.cpp194
1 files changed, 75 insertions, 119 deletions
diff --git a/src/game/RandomMovementGenerator.cpp b/src/game/RandomMovementGenerator.cpp
index 14dae3c663f..cb748883772 100644
--- a/src/game/RandomMovementGenerator.cpp
+++ b/src/game/RandomMovementGenerator.cpp
@@ -22,7 +22,10 @@
#include "MapManager.h"
#include "RandomMovementGenerator.h"
#include "Traveller.h"
+#include "ObjectAccessor.h"
#include "DestinationHolderImp.h"
+#include "Map.h"
+#include "Util.h"
#define RUNNING_CHANCE_RANDOMMV 20 //will be "1 / RUNNING_CHANCE_RANDOMMV"
@@ -31,9 +34,9 @@ bool
RandomMovementGenerator<Creature>::GetDestination(float &x, float &y, float &z) const
{
if(i_destinationHolder.HasArrived())
- return false;
-
- i_destinationHolder.GetDestination(x, y, z);
+ return false;
+
+ i_destinationHolder.GetDestination(x, y, z);
return true;
}
@@ -41,96 +44,76 @@ template<>
void
RandomMovementGenerator<Creature>::_setRandomLocation(Creature &creature)
{
- float i_x,i_y,i_z,n_x,n_y,n_z,ori,dist;
- creature.GetHomePosition(i_x,i_y,i_z,ori);
+ float X,Y,Z,z,nx,ny,nz,ori,dist;
- const float angle = 2*M_PI*rand_norm();
- const float range = rand_norm()*wander_dist;
- const float distX = range*cos(angle);
- const float distY = range*sin(angle);
+ creature.GetHomePosition(X, Y, Z, ori);
- n_x = i_x + distX;
- n_y = i_y + distY;
+ z = creature.GetPositionZ();
+ uint32 mapid=creature.GetMapId();
+ Map const* map = MapManager::Instance().GetBaseMap(mapid);
- Trinity::NormalizeMapCoord(n_x);
- Trinity::NormalizeMapCoord(n_y);
+ // For 2D/3D system selection
+ bool is_land_ok = creature.canWalk();
+ bool is_water_ok = creature.canSwim();
+ bool is_air_ok = creature.canFly();
- Map const* map = MapManager::Instance().GetBaseMap(creature.GetMapId());
+ const float angle = rand_norm()*(M_PI*2);
+ const float range = rand_norm()*wander_distance;
+ const float distanceX = range * cos(angle);
+ const float distanceY = range * sin(angle);
- switch(_InhabitType)
- {
- case INHABIT_AIR:
+ nx = X + distanceX;
+ ny = Y + distanceY;
+
+ // prevent invalid coordinates generation
+ Trinity::NormalizeMapCoord(nx);
+ Trinity::NormalizeMapCoord(ny);
+
+ dist = (nx - X)*(nx - X) + (ny - Y)*(ny - Y);
+
+ if (is_air_ok) // 3D system above ground and above water (flying mode)
{
- //don't use normalized n_x, n_y because difference is too small.. and this is random movement
- dist = sqrtf(distX*distX + distY*distY);
- const float distanceZ = rand_norm() * dist/2; // Limit height change
- n_z = i_z + distanceZ;
- float tz = map->GetHeight(n_x,n_y,n_z - 2.0f,false);
- if (tz >= n_z || ground >= n_z)
- return;
- //creature.AddUnitMovementFlag(MOVEMENTFLAG_FLYING2);
- break;
+ const float distanceZ = rand_norm() * sqrtf(dist)/2; // Limit height change
+ nz = Z + distanceZ;
+ float tz = map->GetHeight(nx, ny, nz-2.0f, false); // Map check only, vmap needed here but need to alter vmaps checks for height.
+ float wz = map->GetWaterLevel(nx, ny);
+ if (tz >= nz || wz >= nz)
+ return; // Problem here, we must fly above the ground and water, not under. Let's try on next tick
}
- case INHABIT_WATER:
+ //else if (is_water_ok) // 3D system under water and above ground (swimming mode)
+ else // 2D only
{
- //some small creatures can swim above water level
- float water = map->GetWaterLevel(n_x,n_y) - 1.8f;
- float floor = map->GetHeight(n_x,n_y,i_z,false) + 0.2f;
- if (water - floor < 0)
- return;
- float z_diff = 2 * 1.192*creature.GetDistance2d(n_x,n_y); //1.192 = tan(50)
- //if(water - floor > z_diff){
- n_z = z_diff*(1 - 2*rand_norm()) + creature.GetPositionZ();
- n_z = n_z > water ? water : n_z;
- n_z = n_z < floor ? floor : n_z;
- //}else
- //n_z = floor + rand_norm()*(water - floor);
- //creature.AddUnitMovementFlag(MOVEMENTFLAG_SWIMMING);
- break;
- }
- case INHABIT_GROUND:
- {
- dist = (distX*distX + distY*distY);
dist = dist>=100.0f ? 10.0f : sqrtf(dist); // 10.0 is the max that vmap high can check (MAX_CAN_FALL_DISTANCE)
- n_z = map->GetHeight(n_x,n_y,i_z,false);
- if (fabs(n_z - i_z) > dist)
+ // The fastest way to get an accurate result 90% of the time.
+ // Better result can be obtained like 99% accuracy with a ray light, but the cost is too high and the code is too long.
+ nz = map->GetHeight(nx,ny,Z+dist-2.0f,false); // Map check
+ if (fabs(nz-Z)>dist)
{
- n_z = map->GetHeight(n_x,n_y,i_z,true); // Vmap Horizontal or above
- if (fabs(n_z - i_z) > dist)
+ nz = map->GetHeight(nx,ny,Z-2.0f,true); // Vmap Horizontal or above
+ if (fabs(nz-Z)>dist)
{
- n_z = map->GetHeight(n_x,n_y,i_z + 8.0f,true); // Vmap Higher
- if (fabs(n_z - i_z) > dist)
- return;
+ nz = map->GetHeight(nx,ny,Z+dist-2.0f,true); // Vmap Higher
+ if (fabs(nz-Z)>dist)
+ return; // let's forget this bad coords where a z cannot be find and retry at next tick
}
}
- if (!irand(0,RUNNING_CHANCE_RANDOMMV))
- creature.RemoveUnitMovementFlag(MOVEMENTFLAG_WALK_MODE);
- else
- creature.AddUnitMovementFlag(MOVEMENTFLAG_WALK_MODE);
- break;
- }
- default:
- n_z = i_z;
- break;
}
Traveller<Creature> traveller(creature);
- creature.SetOrientation(creature.GetAngle(n_x,n_y));
- i_destinationHolder.SetDestination(traveller,n_x,n_y,n_z);
+ creature.SetOrientation(creature.GetAngle(nx,ny));
+ i_destinationHolder.SetDestination(traveller, nx, ny, nz);
creature.addUnitState(UNIT_STAT_ROAMING);
-
- switch(_InhabitType)
- {
- case INHABIT_AIR:
- i_nextMoveTime.Reset(i_destinationHolder.GetTotalTravelTime());
- break;
- case INHABIT_WATER:
- i_nextMoveTime.Reset(i_destinationHolder.GetTotalTravelTime() + urand(500,3000));
- break;
- default:
- i_nextMoveTime.Reset(i_destinationHolder.GetTotalTravelTime() + urand(500,5000));
- break;
- }
+ if (is_air_ok)
+ {
+ i_nextMoveTime.Reset(i_destinationHolder.GetTotalTravelTime());
+ creature.AddUnitMovementFlag(MOVEMENTFLAG_FLYING2);
+ }
+ //else if (is_water_ok) // Swimming mode to be done with more than this check
+ else
+ {
+ i_nextMoveTime.Reset(urand(500+i_destinationHolder.GetTotalTravelTime(),5000+i_destinationHolder.GetTotalTravelTime()));
+ creature.SetUnitMovementFlags(MOVEMENTFLAG_WALK_MODE);
+ }
}
template<>
@@ -140,47 +123,13 @@ RandomMovementGenerator<Creature>::Initialize(Creature &creature)
if(!creature.isAlive())
return;
- wander_dist = creature.GetRespawnRadius();
- Map const* map = MapManager::Instance().GetBaseMap(creature.GetMapId());
-
- _InhabitType = creature.GetCreatureInfo()->InhabitType;
-
- // Let's select only one movement type
- // TODO: make this check more correct
- float water;
- if(_InhabitType & (INHABIT_WATER | INHABIT_AIR))
- water = map->GetWaterLevel(creature.GetPositionX(),creature.GetPositionY());
- if(_InhabitType & INHABIT_AIR)
- {
- float floor = map->GetHeight(creature.GetPositionX(),creature.GetPositionY(),creature.GetPositionZ(),true);
- ground = floor > water ? floor : water;
- if(ground + 2.0f < creature.GetPositionZ())
- {
- _InhabitType = INHABIT_AIR;
- creature.addUnitState(UNIT_STAT_IN_FLIGHT);
- creature.AddUnitMovementFlag(MOVEMENTFLAG_FLYING2);
- i_nextMoveTime.Reset(urand(0,1000)); //to make them call _setRandomLocation not in one time
- //_setRandomLocation(creature);
- return;
- }else
- _InhabitType &= ~INHABIT_AIR;
- }
- if(_InhabitType & INHABIT_WATER)
- {
- if (water > creature.GetPositionZ()) //don't use "+2.0f" because under-water creatures already has height <= -1.9(if water level is 0)
- {
- _InhabitType = INHABIT_WATER;
- creature.addUnitState(UNIT_STAT_IN_FLIGHT);
- creature.AddUnitMovementFlag(MOVEMENTFLAG_SWIMMING);
- i_nextMoveTime.Reset(urand(0,1000));
- //_setRandomLocation(creature);
- return;
- }else
- _InhabitType &= ~INHABIT_WATER;
- }
- if(_InhabitType & INHABIT_GROUND)
- i_nextMoveTime.Reset(urand(0,1000));
- //_setRandomLocation(creature);
+ wander_distance = creature.GetRespawnRadius();
+
+ if (creature.canFly())
+ creature.AddUnitMovementFlag(MOVEMENTFLAG_FLYING2);
+ else
+ creature.SetUnitMovementFlags(irand(0,RUNNING_CHANCE_RANDOMMV) > 0 ? MOVEMENTFLAG_WALK_MODE : MOVEMENTFLAG_NONE );
+ _setRandomLocation(creature);
}
template<>
@@ -207,7 +156,7 @@ RandomMovementGenerator<Creature>::Update(Creature &creature, const uint32 &diff
i_nextMoveTime.Update(diff);
- if(i_destinationHolder.HasArrived() && !creature.IsStopped() && !(_InhabitType & (INHABIT_WATER | INHABIT_AIR)))
+ if(i_destinationHolder.HasArrived() && !creature.IsStopped() && !creature.canFly())
creature.clearUnitState(UNIT_STAT_ROAMING);
if(!i_destinationHolder.HasArrived() && creature.IsStopped())
@@ -217,8 +166,14 @@ RandomMovementGenerator<Creature>::Update(Creature &creature, const uint32 &diff
if( i_destinationHolder.UpdateTraveller(traveller, diff, false, true) )
{
- if(i_nextMoveTime.Passed())
+ if(i_nextMoveTime.Passed())
+ {
+ if (creature.canFly())
+ creature.AddUnitMovementFlag(MOVEMENTFLAG_FLYING2);
+ else
+ creature.SetUnitMovementFlags(irand(0,RUNNING_CHANCE_RANDOMMV) > 0 ? MOVEMENTFLAG_WALK_MODE : MOVEMENTFLAG_NONE);
_setRandomLocation(creature);
+ }
else if(creature.isPet() && creature.GetOwner() && creature.GetDistance(creature.GetOwner()) > PET_FOLLOW_DIST+2.5f)
{
creature.SetUnitMovementFlags(MOVEMENTFLAG_NONE);
@@ -227,3 +182,4 @@ RandomMovementGenerator<Creature>::Update(Creature &creature, const uint32 &diff
}
return true;
}
+