diff --git a/src/game/Maps/Map.cpp b/src/game/Maps/Map.cpp index d2863fd5b..02def4b91 100644 --- a/src/game/Maps/Map.cpp +++ b/src/game/Maps/Map.cpp @@ -3193,6 +3193,41 @@ bool Map::GetWalkHitPosition(GenericTransport* transport, float srcX, float srcY return true; } +// This will generate a random point to all directions in water for the provided point in radius range. +bool Map::GetSwimRandomPosition(float& x, float& y, float& z, float radius, GridMapLiquidData& liquid_status, bool randomRange/* = true*/) const +{ + const float angle = rand_norm_f() * (M_PI_F * 2.0f); + const float range = (randomRange ? rand_norm_f() : 1.f) * radius; + + float i_x = x + range * cos(angle); + float i_y = y + range * sin(angle); + + // get real ground of new point + // the code consider cylinder instead of sphere for possible z + float ground = GetHeight(i_x, i_y, z); + if (ground > INVALID_HEIGHT) // GetHeight can fail + { + float min_z = z - 0.7f * radius; // 0.7 to have a bit a "flat" cylinder, TODO which value looks nicest + if (min_z < ground) + min_z = ground + 0.5f; // Get some space to prevent under map + + float liquidLevel = liquid_status.level - 2.0f; // just to make the generated point is in water and not on surface or a bit above + + // if not enough space to fit the creature better is to return from here + if (min_z > liquidLevel) + return false; + + // Mobs underwater do not move along Z axis + //float max_z = std::max(z + 0.7f * radius, min_z); + //max_z = std::min(max_z, liquidLevel); + x = i_x; + y = i_y; + if (min_z > z) + z = min_z; + return true; + } + return false; +} bool Map::GetWalkRandomPosition(GenericTransport* transport, float &x, float &y, float &z, float maxRadius, uint32 moveAllowedFlags) const { diff --git a/src/game/Maps/Map.h b/src/game/Maps/Map.h index 9bedb07ed..0b8ac0ca4 100644 --- a/src/game/Maps/Map.h +++ b/src/game/Maps/Map.h @@ -555,6 +555,7 @@ class Map : public GridRefManager bool GetWalkHitPosition(GenericTransport* t, float srcX, float srcY, float srcZ, float& destX, float& destY, float& destZ, uint32 moveAllowedFlags = 0xF /*NAV_GROUND | NAV_WATER | NAV_MAGMA | NAV_SLIME*/, float zSearchDist = 20.0f, bool locatedOnSteepSlope = true) const; bool GetWalkRandomPosition(GenericTransport* t, float &x, float &y, float &z, float maxRadius, uint32 moveAllowedFlags = 0xF) const; + bool GetSwimRandomPosition(float& x, float& y, float& z, float radius, GridMapLiquidData& liquid_status, bool randomRange = true) const; VMAP::ModelInstance* FindCollisionModel(float x1, float y1, float z1, float x2, float y2, float z2); void Balance() { _dynamicTree.balance(); } diff --git a/src/game/Movement/RandomMovementGenerator.cpp b/src/game/Movement/RandomMovementGenerator.cpp index 333bff5c7..723553f5a 100644 --- a/src/game/Movement/RandomMovementGenerator.cpp +++ b/src/game/Movement/RandomMovementGenerator.cpp @@ -24,10 +24,6 @@ void RandomMovementGenerator::_setRandomLocation(Creature &creature) { - // Don't move if invalid coordinates have been set somehow. - if (i_positionX == 0.0f && i_positionY == 0.0f) - return; - if (creature.CanFly()) { //typedef std::vector PointsArray; @@ -35,7 +31,7 @@ void RandomMovementGenerator::_setRandomLocation(Creature &creature) uint32 ptsPerCycle = ceil(i_wanderDistance * 2); static uint32 const nbCyclesPerPacket = 1; for (uint32 i = 0; i <= nbCyclesPerPacket * ptsPerCycle; ++i) - path.push_back(Vector3(i_positionX + i_wanderDistance * cos(i * 2 * M_PI / ptsPerCycle), i_positionY + i_wanderDistance * sin(i * 2 * M_PI / ptsPerCycle), i_positionZ)); + path.push_back(Vector3(i_startPosition.x + i_wanderDistance * cos(i * 2 * M_PI / ptsPerCycle), i_startPosition.y + i_wanderDistance * sin(i * 2 * M_PI / ptsPerCycle), i_startPosition.z)); Movement::MoveSplineInit init(creature, "RandomMovementGenerator (CanFly)"); init.SetFly(); init.SetWalk(false); @@ -46,13 +42,14 @@ void RandomMovementGenerator::_setRandomLocation(Creature &creature) return; } - float destX, destY, destZ; - if (!creature.GetRandomPoint(i_positionX, i_positionY, i_positionZ, i_wanderDistance, destX, destY, destZ)) + if (i_randomPoints.empty()) return; + G3D::Vector3 const& dest = SelectRandomContainerElement(i_randomPoints); + creature.AddUnitState(UNIT_STAT_ROAMING_MOVE); Movement::MoveSplineInit init(creature, "RandomMovementGenerator"); - init.MoveTo(destX, destY, destZ, MOVE_PATHFINDING | MOVE_EXCLUDE_STEEP_SLOPES); + init.MoveTo(dest.x, dest.y, dest.z, MOVE_PATHFINDING | MOVE_EXCLUDE_STEEP_SLOPES); init.SetWalk(!creature.HasExtraFlag(CREATURE_FLAG_EXTRA_ALWAYS_RUN)); init.Launch(); @@ -69,11 +66,27 @@ void RandomMovementGenerator::_setRandomLocation(Creature &creature) } } +#define MAX_RANDOM_POINTS 10 + void RandomMovementGenerator::Initialize(Creature &creature) { if (!creature.IsAlive()) return; + i_randomPoints.clear(); + i_randomPoints.reserve(MAX_RANDOM_POINTS + 1); + for (uint32 i = 0; (i < MAX_RANDOM_POINTS * 3) && (i_randomPoints.size() < MAX_RANDOM_POINTS); ++i) + { + G3D::Vector3 point; + if (creature.GetRandomPoint(i_startPosition.x, i_startPosition.y, i_startPosition.z, i_wanderDistance, point.x, point.y, point.z)) + i_randomPoints.push_back(point); + } + + if (i_randomPoints.empty()) + sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "Failed to generate random points for %s!", creature.GetGuidStr().c_str()); + else + i_randomPoints.push_back(i_startPosition); + creature.AddUnitState(UNIT_STAT_ROAMING | UNIT_STAT_ROAMING_MOVE); i_nextMoveTime.Reset(1000); } @@ -134,13 +147,13 @@ void RandomMovementGenerator::UpdateAsync(Creature &creature, uint32 diff) bool RandomMovementGenerator::GetResetPosition(Creature& c, float& x, float& y, float& z) { // use current if in range - if (c.IsWithinDist2d(i_positionX, i_positionY, i_wanderDistance)) + if (c.IsWithinDist2d(i_startPosition.x, i_startPosition.y, i_wanderDistance)) c.GetPosition(x, y, z); else { - x = i_positionX; - y = i_positionY; - z = i_positionZ; + x = i_startPosition.x; + y = i_startPosition.y; + z = i_startPosition.z; } return true; diff --git a/src/game/Movement/RandomMovementGenerator.h b/src/game/Movement/RandomMovementGenerator.h index 0b0c1bd48..a579e22ac 100644 --- a/src/game/Movement/RandomMovementGenerator.h +++ b/src/game/Movement/RandomMovementGenerator.h @@ -25,12 +25,13 @@ class RandomMovementGenerator : public MovementGeneratorMedium< Creature, RandomMovementGenerator > { public: - explicit RandomMovementGenerator(Creature const& creature, bool use_current_position = false, float wander_distance = 0.0f, uint32 expire_time = 0) : i_nextMoveTime(1000), i_positionX(0.0f), i_positionY(0.0f), i_positionZ(0.0f), i_wanderDistance(5.0f), i_expireTime(expire_time), i_wanderSteps(0) + explicit RandomMovementGenerator(Creature const& creature, bool use_current_position = false, float wander_distance = 0.0f, uint32 expire_time = 0) : + i_nextMoveTime(1000), i_wanderDistance(5.0f), i_expireTime(expire_time), i_wanderSteps(0) { if (use_current_position) - creature.GetPosition(i_positionX, i_positionY, i_positionZ); + creature.GetPosition(i_startPosition.x, i_startPosition.y, i_startPosition.z); else - creature.GetRespawnCoord(i_positionX, i_positionY, i_positionZ, nullptr, &i_wanderDistance); + creature.GetRespawnCoord(i_startPosition.x, i_startPosition.y, i_startPosition.z, nullptr, &i_wanderDistance); if (wander_distance > 0.0f) i_wanderDistance = wander_distance; @@ -54,12 +55,11 @@ class RandomMovementGenerator private: ShortTimeTracker i_nextMoveTime; - float i_positionX; - float i_positionY; - float i_positionZ; + G3D::Vector3 i_startPosition; float i_wanderDistance; uint32 i_expireTime; uint8 i_wanderSteps; + std::vector i_randomPoints; }; #endif diff --git a/src/game/Objects/Object.cpp b/src/game/Objects/Object.cpp index cef82fb2b..75ba2828c 100644 --- a/src/game/Objects/Object.cpp +++ b/src/game/Objects/Object.cpp @@ -1848,13 +1848,12 @@ bool WorldObject::GetRandomPoint(float x, float y, float z, float distance, floa rand_z = z; return true; } - ASSERT(FindMap()); - Map const* map = GetMap(); + + Map const* pMap = GetMap(); + Unit const* pUnit = ToUnit(); - bool is_air_ok = isType(TYPEMASK_UNIT) ? ((Unit*)this)->CanFly() : false; - - // 1er cas on peut voler => Position en l'air, facile. - if (is_air_ok) + // 1st case we can fly => Position in the air, easy. + if (pUnit && pUnit->CanFly()) { float randAngle1 = rand_norm_f() * 2 * M_PI; float randAngle2 = rand_norm_f() * 2 * M_PI; @@ -1865,25 +1864,44 @@ bool WorldObject::GetRandomPoint(float x, float y, float z, float distance, floa // May happen in the border of the map if (!MaNGOS::IsValidMapCoord(x, y, z) || !MaNGOS::IsValidMapCoord(rand_x, rand_y, rand_z)) return false; - map->GetLosHitPosition(x, y, z, rand_x, rand_y, rand_z, -0.5f); + pMap->GetLosHitPosition(x, y, z, rand_x, rand_y, rand_z, -0.5f); return true; } - else + + // Get swimming position using just vmaps. + if (pUnit && pUnit->CanSwim() && pUnit->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_USE_SWIM_ANIMATION)) { - // Sinon, on trouve une position au sol, ou dans l'eau, ou dans la lave (pas pour les joueurs) + GridMapLiquidData liquid_status; + GridMapLiquidStatus res = pMap->GetTerrain()->getLiquidStatus(x, y, z, MAP_ALL_LIQUIDS, &liquid_status); + if (res & (LIQUID_MAP_UNDER_WATER | LIQUID_MAP_IN_WATER)) + { + rand_x = x; + rand_y = y; + rand_z = z; + if (pMap->GetSwimRandomPosition(rand_x, rand_y, rand_z, distance, liquid_status, true)) + { + pMap->GetLosHitPosition(x, y, z, rand_x, rand_y, rand_z, -0.5f); + return true; + } + } + } + + { + // Otherwise, we find a position on the ground, or in water, or in lava (not for players) uint32 moveAllowed = NAV_GROUND | NAV_WATER; if (GetTypeId() != TYPEID_PLAYER) moveAllowed |= NAV_MAGMA | NAV_SLIME; rand_x = x; rand_y = y; rand_z = z; - if (map->GetWalkRandomPosition(GetTransport(), rand_x, rand_y, rand_z, distance, moveAllowed)) + if (pMap->GetWalkRandomPosition(GetTransport(), rand_x, rand_y, rand_z, distance, moveAllowed)) { // Giant type creatures walk underwater - if ((isType(TYPEMASK_UNIT) && !ToUnit()->CanSwim()) || - (IsCreature() && ToCreature()->GetCreatureInfo()->type == CREATURE_TYPE_GIANT)) + if ((pUnit && !pUnit->CanSwim()) || + (IsCreature() && !pUnit->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_USE_SWIM_ANIMATION))) return true; - // La position renvoyee par le pathfinding est tout au fond de l'eau. On randomise ca un peu ... + + // The position returned by the pathfinding is at the bottom of the water. We're randomizing it a bit... float ground = 0.0f; float waterSurface = GetTerrain()->GetWaterLevel(x, y, z, &ground); if (waterSurface == VMAP_INVALID_HEIGHT_VALUE) @@ -1893,7 +1911,7 @@ bool WorldObject::GetRandomPoint(float x, float y, float z, float distance, floa rand_z += rand_norm_f() * distance / 2.0f; if (rand_z < ground) rand_z = ground; - // Ici 'is_air_ok' = false, donc on reste SOUS l'eau. + // Flying case checked before that, so we stay UNDER water. if (rand_z > waterSurface) rand_z = waterSurface; return true;