feat(Core/Movement): Improved pathfinding, collisions and movements (#4220)

Npc positioning
Implemented slope check to avoid unwanted climbing for some kind of movements (backwards, repositioning etc.)
Implemented backwards movement
Re-implemented circle repositioning algorithm (smartest than retail, but with the same feeling)
Fixed random position of summoned minions
Improved pet following movement. Also, they attack NPC from behind now. Thanks to @Footman

Swimming creatures
Fixed max_z coordinate for swimming creatures. Now only part of their body is allowed to be out of the water level
Fixed pathfinder for swimming creatures creating shortcuts for specific segments, now they swim underwater to reach the seashore instead of flying above the water level.
Creatures with water InhabitType but no swimming flag now, when not in combat, will walk on water depth instead of swimming. Thanks @jackpoz for the original code
UNIT_FLAG_SWIMMING in UpdateEnvironmentIfNeeded to show the swimming animation correctly when underwater
Implemented HasEnoughWater check to avoid swimming creatures to go where the water level is too low but also to properly enable swimming animation only when a creature has enough water to swim.

Walking creatures
Extended the DetourNavMeshQuery adding area cost based on walkability (slope angle + source height) to find better paths at runtime instead of completely remove them from mmaps
improve Z height in certain conditions (see #4205, #4203, #4247 )

Flying creatures
Rewriting of the hover system
Removed hacks and improved the UpdateEnvironmentIfNeeded. Now creatures can properly switch from flying to walk etc.
Spells
LOS on spell effect must be calculated on CollisionHeight and HitSpherePoint instead of position coords.
Improved position for object/creature spawned via spells
Improved checks for Fleeing movements (fear spells)

Other improvements
Implemented method to calculate the CollisionWidth from dbc (used by repositioning algorithm etc.)
Improved raycast and collision checks

Co-authored-by: Footman <p.alexej@freenet.de>
Co-authored-by: Helias <stefanoborzi32@gmail.com>
Co-authored-by: Francesco Borzì <borzifrancesco@gmail.com>
Co-authored-by: Kitzunu <24550914+Kitzunu@users.noreply.github.com>
This commit is contained in:
Yehonal
2021-02-01 01:34:27 +01:00
committed by GitHub
parent fcad2b56ae
commit c8f43d8584
79 changed files with 3025 additions and 2199 deletions

View File

@@ -47,8 +47,6 @@
#include "LuaEngine.h"
#endif
constexpr uint32 DEF_CANNOT_REACH = 5 * IN_MILLISECONDS; // this is when creatures are in los / no path to the target, 5s wait time then they return to spawn pos with evade
TrainerSpell const* TrainerSpellData::Find(uint32 spell_id) const
{
TrainerSpellMap::const_iterator itr = spellList.find(spell_id);
@@ -164,9 +162,10 @@ bool ForcedDespawnDelayEvent::Execute(uint64 /*e_time*/, uint32 /*p_time*/)
Creature::Creature(bool isWorldObject): Unit(isWorldObject), MovableMapObject(), m_groupLootTimer(0), lootingGroupLowGUID(0), m_PlayerDamageReq(0), m_lootRecipient(0), m_lootRecipientGroup(0),
m_corpseRemoveTime(0), m_respawnTime(0), m_respawnDelay(300), m_corpseDelay(60), m_wanderDistance(0.0f),
m_transportCheckTimer(1000), lootPickPocketRestoreTime(0), m_reactState(REACT_AGGRESSIVE), m_defaultMovementType(IDLE_MOTION_TYPE),
m_DBTableGuid(0), m_equipmentId(0), m_originalEquipmentId(0), m_AlreadyCallAssistance(false),
m_DBTableGuid(0), m_equipmentId(0), m_originalEquipmentId(0), m_originalAnimTier(UNIT_BYTE1_FLAG_GROUND), m_AlreadyCallAssistance(false),
m_AlreadySearchedAssistance(false), m_regenHealth(true), m_AI_locked(false), m_meleeDamageSchoolMask(SPELL_SCHOOL_MASK_NORMAL), m_originalEntry(0), m_moveInLineOfSightDisabled(false), m_moveInLineOfSightStrictlyDisabled(false),
m_homePosition(), m_transportHomePosition(), m_creatureInfo(nullptr), m_creatureData(nullptr), m_waypointID(0), m_path_id(0), m_formation(nullptr), _lastDamagedTime(0), m_cannotReachTarget(false), m_cannotReachTimer(0)
m_homePosition(), m_transportHomePosition(), m_creatureInfo(nullptr), m_creatureData(nullptr), m_waypointID(0), m_path_id(0), m_formation(nullptr), _lastDamagedTime(0), m_cannotReachTarget(false), m_cannotReachTimer(0),
_isMissingSwimmingFlagOutOfCombat(false)
{
m_regenTimer = CREATURE_REGEN_INTERVAL;
m_valuesCount = UNIT_END;
@@ -591,6 +590,35 @@ void Creature::Update(uint32 diff)
RemoveCharmAuras();
}
if (Unit *victim = GetVictim())
{
// If we are closer than 50% of the combat reach we are going to reposition the victim
if (diff >= m_moveBackwardsMovementTime)
{
float MaxRange = GetCollisionRadius() + GetVictim()->GetCollisionRadius();
if (IsInDist(victim, MaxRange))
AI()->MoveBackwardsChecks();
m_moveBackwardsMovementTime = urand(MOVE_BACKWARDS_CHECK_INTERVAL, MOVE_BACKWARDS_CHECK_INTERVAL * 3);
}
else
{
m_moveBackwardsMovementTime -= diff;
}
// Circling the target
if (diff >= m_moveCircleMovementTime)
{
AI()->MoveCircleChecks();
m_moveCircleMovementTime = urand(MOVE_CIRCLE_CHECK_INTERVAL, MOVE_CIRCLE_CHECK_INTERVAL * 2);
}
else
{
m_moveCircleMovementTime -= diff;
}
}
if (!IsInEvadeMode() && IsAIEnabled)
{
// do not allow the AI to be changed during update
@@ -607,8 +635,24 @@ void Creature::Update(uint32 diff)
m_regenTimer -= diff;
if (m_regenTimer <= 0)
{
if (!IsInEvadeMode() && (!IsInCombat() || IsPolymorphed())) // regenerate health if not in combat or if polymorphed
RegenerateHealth();
if (!IsInEvadeMode())
{
// regenerate health if not in combat or if polymorphed)
if (!IsInCombat() || IsPolymorphed())
RegenerateHealth();
else if (CanNotReachTarget())
{
// regenerate health if cannot reach the target and the setting is set to do so.
// this allows to disable the health regen of raid bosses if pathfinding has issues for whatever reason
if (sWorld->getBoolConfig(CONFIG_REGEN_HP_CANNOT_REACH_TARGET_IN_RAID) || !GetMap()->IsRaid())
{
RegenerateHealth();
sLog->outDebug(LOG_FILTER_UNITS, "RegenerateHealth() enabled because Creature cannot reach the target. Detail: %s", GetDebugInfo().c_str());
}
else
sLog->outDebug(LOG_FILTER_UNITS, "RegenerateHealth() disabled even if the Creature cannot reach the target. Detail: %s", GetDebugInfo().c_str());
}
}
if (getPowerType() == POWER_ENERGY)
Regenerate(POWER_ENERGY);
@@ -621,7 +665,7 @@ void Creature::Update(uint32 diff)
if (CanNotReachTarget() && !IsInEvadeMode() && !GetMap()->IsRaid())
{
m_cannotReachTimer += diff;
if (m_cannotReachTimer >= DEF_CANNOT_REACH && IsAIEnabled)
if (m_cannotReachTimer >= (sWorld->getIntConfig(CONFIG_NPC_EVADE_IF_NOT_REACHABLE)*IN_MILLISECONDS) && IsAIEnabled)
{
AI()->EnterEvadeMode();
}
@@ -660,6 +704,20 @@ void Creature::Update(uint32 diff)
}
}
bool Creature::IsFreeToMove()
{
uint32 moveFlags = m_movementInfo.GetMovementFlags();
// Do not reposition ourself when we are not allowed to move
if ((IsMovementPreventedByCasting() || isMoving() || !CanFreeMove()) &&
(GetMotionMaster()->GetCurrentMovementGeneratorType() != CHASE_MOTION_TYPE ||
moveFlags & MOVEMENTFLAG_SPLINE_ENABLED))
{
return false;
}
return true;
}
void Creature::Regenerate(Powers power)
{
uint32 curValue = GetPower(power);
@@ -900,6 +958,9 @@ bool Creature::Create(uint32 guidlow, Map* map, uint32 phaseMask, uint32 Entry,
SetByteValue(UNIT_FIELD_BYTES_0, 2, minfo->gender);
}
//! Need to be called after LoadCreaturesAddon - MOVEMENTFLAG_HOVER is set there
m_positionZ += GetHoverHeight();
LastUsedScriptID = GetCreatureTemplate()->ScriptID;
if (IsSpiritHealer() || IsSpiritGuide() || (GetCreatureTemplate()->flags_extra & CREATURE_FLAG_EXTRA_GHOST_VISIBILITY))
@@ -1524,8 +1585,10 @@ bool Creature::CanStartAttack(Unit const* who) const
// This set of checks is should be done only for creatures
if ((HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_NPC) && who->GetTypeId() != TYPEID_PLAYER) || // flag is valid only for non player characters
(HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_PC) && who->GetTypeId() == TYPEID_PLAYER)) // immune to PC and target is a player, return false
(HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_PC) && who->GetTypeId() == TYPEID_PLAYER)) // immune to PC and target is a player, return false
{
return false;
}
if (Unit* owner = who->GetOwner())
if (owner->GetTypeId() == TYPEID_PLAYER && HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_PC)) // immune to PC and target has player owner
@@ -1578,7 +1641,7 @@ void Creature::setDeathState(DeathState s, bool despawn)
SetTarget(0); // remove target selection in any cases (can be set at aura remove in Unit::setDeathState)
SetUInt32Value(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_NONE);
SetUInt32Value(UNIT_FIELD_MOUNTDISPLAYID, 0); // if creature is mounted on a virtual mount, remove it at death
Dismount(); // if creature is mounted on a virtual mount, remove it at death
setActive(false);
@@ -1597,7 +1660,11 @@ void Creature::setDeathState(DeathState s, bool despawn)
if (m_formation && m_formation->getLeader() == this)
m_formation->FormationReset(true);
if (!despawn && (CanFly() || IsFlying()) && !HasUnitMovementFlag(MOVEMENTFLAG_SWIMMING)) // pussywizard: added MOVEMENTFLAG_SWIMMING check because IsFlying() returns true when swimming creatures have MOVEMENTFLAG_DISABLE_GRAVITY
bool needsFalling = !despawn && (IsFlying() || IsHovering()) && !IsUnderWater();
SetHover(false, false);
SetDisableGravity(false, false);
if (needsFalling)
GetMotionMaster()->MoveFall(0, true);
Unit::setDeathState(CORPSE, despawn);
@@ -1609,6 +1676,7 @@ void Creature::setDeathState(DeathState s, bool despawn)
SetFullHealth();
SetLootRecipient(nullptr);
ResetPlayerDamageReq();
SetCannotReachTarget(false);
CreatureTemplate const* cinfo = GetCreatureTemplate();
// Xinef: npc run by default
//SetWalk(true);
@@ -2203,11 +2271,12 @@ bool Creature::LoadCreaturesAddon(bool reload)
// 2 StandFlags
// 3 StandMiscFlags
SetByteValue(UNIT_FIELD_BYTES_1, 0, uint8(cainfo->bytes1 & 0xFF));
//SetByteValue(UNIT_FIELD_BYTES_1, 1, uint8((cainfo->bytes1 >> 8) & 0xFF));
SetByteValue(UNIT_FIELD_BYTES_1, 1, 0);
SetByteValue(UNIT_FIELD_BYTES_1, 2, uint8((cainfo->bytes1 >> 16) & 0xFF));
SetByteValue(UNIT_FIELD_BYTES_1, 3, uint8((cainfo->bytes1 >> 24) & 0xFF));
SetByteValue(UNIT_FIELD_BYTES_1, UNIT_BYTES_1_OFFSET_STAND_STATE, uint8(cainfo->bytes1 & 0xFF));
//SetByteValue(UNIT_FIELD_BYTES_1, UNIT_BYTES_1_OFFSET_PET_TALENTS, uint8((cainfo->bytes1 >> 8) & 0xFF));
SetByteValue(UNIT_FIELD_BYTES_1, UNIT_BYTES_1_OFFSET_PET_TALENTS, 0);
SetByteValue(UNIT_FIELD_BYTES_1, UNIT_BYTES_1_OFFSET_VIS_FLAG, uint8((cainfo->bytes1 >> 16) & 0xFF));
m_originalAnimTier = uint8((cainfo->bytes1 >> 24) & 0xFF);
SetByteValue(UNIT_FIELD_BYTES_1, UNIT_BYTES_1_OFFSET_ANIM_TIER, m_originalAnimTier);
//! Suspected correlation between UNIT_FIELD_BYTES_1, offset 3, value 0x2:
//! If no inhabittype_fly (if no MovementFlag_DisableGravity or MovementFlag_CanFly flag found in sniffs)
@@ -2660,10 +2729,6 @@ bool Creature::SetDisableGravity(bool disable, bool packetOnly/*=false*/)
if (!movespline->Initialized())
return true;
// pussywizard: artificial disable_gravity to hovering npcs, don't send!
if (!disable && GetHoverHeight() >= 2.0f)
return true;
WorldPacket data(disable ? SMSG_SPLINE_MOVE_GRAVITY_DISABLE : SMSG_SPLINE_MOVE_GRAVITY_ENABLE, 9);
data.append(GetPackGUID());
SendMessageToSet(&data, false);
@@ -2684,6 +2749,43 @@ bool Creature::SetSwim(bool enable)
return true;
}
/**
* @brief This method check the current flag/status of a creature and its inhabit type
*
* Pets should swim by default to properly follow the player
* NOTE: You can set the UNIT_FLAG_CANNOT_SWIM temporary to deny a creature to swim
*
*/
bool Creature::CanSwim() const
{
if (Unit::CanSwim())
return true;
if (IsPet() || IS_PLAYER_GUID(GetOwnerGUID()))
return true;
return false;
}
bool Creature::CanEnterWater() const
{
if (CanSwim())
return true;
return GetCreatureTemplate()->InhabitType & INHABIT_WATER;
}
void Creature::RefreshSwimmingFlag(bool recheck)
{
if (!_isMissingSwimmingFlagOutOfCombat || recheck)
_isMissingSwimmingFlagOutOfCombat = !HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SWIMMING);
// Check if the creature has UNIT_FLAG_SWIMMING and add it if it's missing
// Creatures must be able to chase a target in water if they can enter water
if (_isMissingSwimmingFlagOutOfCombat && CanEnterWater())
SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SWIMMING);
}
bool Creature::SetCanFly(bool enable, bool /*packetOnly*/ /* = false */)
{
if (!Unit::SetCanFly(enable))
@@ -2698,6 +2800,14 @@ bool Creature::SetCanFly(bool enable, bool /*packetOnly*/ /* = false */)
return true;
}
bool Creature::CanFly() const
{
if (Unit::IsFlying())
return true;
return GetCreatureTemplate()->InhabitType & INHABIT_AIR;
}
bool Creature::SetWaterWalking(bool enable, bool packetOnly /* = false */)
{
if (!packetOnly && !Unit::SetWaterWalking(enable))
@@ -2733,9 +2843,9 @@ bool Creature::SetHover(bool enable, bool packetOnly /*= false*/)
//! Unconfirmed for players:
if (enable)
SetByteFlag(UNIT_FIELD_BYTES_1, 3, UNIT_BYTE1_FLAG_HOVER);
SetByteFlag(UNIT_FIELD_BYTES_1, UNIT_BYTES_1_OFFSET_ANIM_TIER, UNIT_BYTE1_FLAG_HOVER);
else
RemoveByteFlag(UNIT_FIELD_BYTES_1, 3, UNIT_BYTE1_FLAG_HOVER);
RemoveByteFlag(UNIT_FIELD_BYTES_1, UNIT_BYTES_1_OFFSET_ANIM_TIER, UNIT_BYTE1_FLAG_HOVER);
if (!movespline->Initialized())
return true;
@@ -2793,22 +2903,38 @@ void Creature::SetObjectScale(float scale)
{
Unit::SetObjectScale(scale);
float combatReach = DEFAULT_WORLD_OBJECT_SIZE;
if (CreatureModelInfo const* minfo = sObjectMgr->GetCreatureModelInfo(GetDisplayId()))
{
SetFloatValue(UNIT_FIELD_BOUNDINGRADIUS, (IsPet() ? 1.0f : minfo->bounding_radius) * scale);
SetFloatValue(UNIT_FIELD_COMBATREACH, (IsPet() ? DEFAULT_COMBAT_REACH : minfo->combat_reach) * scale);
if (minfo->combat_reach > 0)
combatReach = minfo->combat_reach;
}
if (IsPet())
combatReach = DEFAULT_COMBAT_REACH;
SetFloatValue(UNIT_FIELD_COMBATREACH, combatReach * GetFloatValue(OBJECT_FIELD_SCALE_X) * scale);
}
void Creature::SetDisplayId(uint32 modelId)
{
Unit::SetDisplayId(modelId);
float combatReach = DEFAULT_WORLD_OBJECT_SIZE;
if (CreatureModelInfo const* minfo = sObjectMgr->GetCreatureModelInfo(modelId))
{
SetFloatValue(UNIT_FIELD_BOUNDINGRADIUS, (IsPet() ? 1.0f : minfo->bounding_radius) * GetFloatValue(OBJECT_FIELD_SCALE_X));
SetFloatValue(UNIT_FIELD_COMBATREACH, (IsPet() ? DEFAULT_COMBAT_REACH : minfo->combat_reach) * GetFloatValue(OBJECT_FIELD_SCALE_X));
SetFloatValue(UNIT_FIELD_BOUNDINGRADIUS, (IsPet() ? 1.0f : minfo->bounding_radius) * GetObjectScale());
if (minfo->combat_reach > 0)
combatReach = minfo->combat_reach;
}
if (IsPet())
combatReach = DEFAULT_COMBAT_REACH;
SetFloatValue(UNIT_FIELD_COMBATREACH, combatReach * GetObjectScale());
}
void Creature::SetTarget(uint64 guid)
@@ -2835,6 +2961,16 @@ void Creature::FocusTarget(Spell const* focusSpell, WorldObject const* target)
SetInFront(target);
}
bool Creature::HasSpellFocus(Spell const* focusSpell) const
{
if (isDead()) // dead creatures cannot focus
{
return false;
}
return focusSpell ? (focusSpell == _spellFocusInfo.Spell) : (_spellFocusInfo.Spell || _spellFocusInfo.Delay);
}
void Creature::ReleaseFocus(Spell const* focusSpell)
{
// focused to something else
@@ -2892,3 +3028,36 @@ float Creature::GetAttackDistance(Unit const* player) const
return (retDistance * aggroRate);
}
bool Creature::IsMovementPreventedByCasting() const
{
Spell* spell = m_currentSpells[CURRENT_CHANNELED_SPELL];
// first check if currently a movement allowed channel is active and we're not casting
if (!!spell && spell->getState() != SPELL_STATE_FINISHED && spell->IsChannelActive() && spell->GetSpellInfo()->IsMoveAllowedChannel())
{
return false;
}
if (HasSpellFocus())
{
return true;
}
if (HasUnitState(UNIT_STATE_CASTING))
{
return true;
}
return false;
}
void Creature::SetCannotReachTarget(bool cannotReach)
{
if (cannotReach == m_cannotReachTarget)
return;
m_cannotReachTarget = cannotReach;
m_cannotReachTimer = 0;
if (cannotReach)
sLog->outDebug(LOG_FILTER_UNITS, "Creature::SetCannotReachTarget() called with true. Details: %s", GetDebugInfo().c_str());
}