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

@@ -153,9 +153,28 @@ ProcEventInfo::ProcEventInfo(Unit* actor, Unit* actionTarget, Unit* procTarget,
#pragma warning(disable:4355)
#endif
Unit::Unit(bool isWorldObject) : WorldObject(isWorldObject),
m_movedByPlayer(nullptr), m_lastSanctuaryTime(0), IsAIEnabled(false), NeedChangeAI(false),
m_ControlledByPlayer(false), m_CreatedByPlayer(false), movespline(new Movement::MoveSpline()), i_AI(nullptr), i_disabledAI(nullptr), m_realRace(0), m_race(0), m_AutoRepeatFirstCast(false), m_procDeep(0), m_removedAurasCount(0),
i_motionMaster(new MotionMaster(this)), m_regenTimer(0), m_ThreatManager(this), m_vehicle(nullptr), m_vehicleKit(nullptr), m_unitTypeMask(UNIT_MASK_NONE), m_HostileRefManager(this)
m_movedByPlayer(nullptr),
m_lastSanctuaryTime(0),
IsAIEnabled(false),
NeedChangeAI(false),
m_ControlledByPlayer(false),
m_CreatedByPlayer(false),
movespline(new Movement::MoveSpline()),
i_AI(nullptr),
i_disabledAI(nullptr),
m_realRace(0),
m_race(0),
m_AutoRepeatFirstCast(false),
m_procDeep(0),
m_removedAurasCount(0),
i_motionMaster(new MotionMaster(this)),
m_regenTimer(0),
m_ThreatManager(this),
m_vehicle(nullptr),
m_vehicleKit(nullptr),
m_unitTypeMask(UNIT_MASK_NONE),
m_HostileRefManager(this),
m_comboTarget(nullptr)
{
#ifdef _MSC_VER
#pragma warning(default:4355)
@@ -246,7 +265,6 @@ Unit::Unit(bool isWorldObject) : WorldObject(isWorldObject),
m_serverSideVisibility.SetValue(SERVERSIDE_VISIBILITY_GHOST, GHOST_VISIBILITY_ALIVE);
m_mmapNotAcceptableStartTime = 0;
m_last_notify_position.Relocate(-5000.0f, -5000.0f, -5000.0f, 0.0f);
m_last_notify_mstime = 0;
m_delayed_unit_relocation_timer = 0;
@@ -445,7 +463,7 @@ void Unit::SendMonsterMove(float NewPosX, float NewPosY, float NewPosZ, uint32 T
data.append(GetPackGUID());
data << uint8(0); // new in 3.1
data << GetPositionX() << GetPositionY() << GetPositionZ() + GetHoverHeight();
data << GetPositionX() << GetPositionY() << GetPositionZ();
data << World::GetGameTimeMS();
data << uint8(0);
data << uint32(sf);
@@ -571,12 +589,32 @@ bool Unit::IsWithinMeleeRange(const Unit* obj, float dist) const
float dz = GetPositionZ() - obj->GetPositionZ();
float distsq = dx * dx + dy * dy + dz * dz;
float sizefactor = GetMeleeReach() + obj->GetMeleeReach();
float maxdist = dist + sizefactor;
float maxdist = dist + GetMeleeRange(obj);
return distsq < maxdist * maxdist;
}
float Unit::GetMeleeRange(Unit const* target) const
{
float range = GetCombatReach() + target->GetCombatReach() + 4.0f / 3.0f;
return std::max(range, NOMINAL_MELEE_RANGE);
}
bool Unit::IsWithinRange(Unit const* obj, float dist) const
{
if (!obj || !IsInMap(obj) || !InSamePhase(obj))
{
return false;
}
auto dx = GetPositionX() - obj->GetPositionX();
auto dy = GetPositionY() - obj->GetPositionY();
auto dz = GetPositionZ() - obj->GetPositionZ();
auto distsq = dx * dx + dy * dy + dz * dz;
return distsq <= dist * dist;
}
bool Unit::GetRandomContactPoint(const Unit* obj, float& x, float& y, float& z, bool force) const
{
float combat_reach = GetCombatReach();
@@ -595,14 +633,14 @@ bool Unit::GetRandomContactPoint(const Unit* obj, float& x, float& y, float& z,
GetAngle(obj) + (attacker_number ? (static_cast<float>(M_PI / 2) - static_cast<float>(M_PI) * (float)rand_norm()) * float(attacker_number) / combat_reach * 0.3f : 0));
// pussywizard
if (fabs(this->GetPositionZ() - z) > 3.0f || !IsWithinLOS(x, y, z))
if (fabs(this->GetPositionZ() - z) > this->GetCollisionHeight() || !IsWithinLOS(x, y, z))
{
x = this->GetPositionX();
y = this->GetPositionY();
z = this->GetPositionZ();
obj->UpdateAllowedPositionZ(x, y, z);
}
float maxDist = MELEE_RANGE + GetMeleeReach() + obj->GetMeleeReach();
float maxDist = GetMeleeRange(obj);
if (GetExactDistSq(x, y, z) >= maxDist * maxDist)
{
if (force)
@@ -2141,7 +2179,7 @@ void Unit::CalcHealAbsorb(Unit const* victim, const SpellInfo* healSpell, uint32
healAmount = RemainingHeal;
}
void Unit::AttackerStateUpdate (Unit* victim, WeaponAttackType attType, bool extra)
void Unit::AttackerStateUpdate(Unit* victim, WeaponAttackType attType, bool extra)
{
if (HasUnitState(UNIT_STATE_CANNOT_AUTOATTACK) || HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_PACIFIED))
return;
@@ -2188,6 +2226,95 @@ void Unit::AttackerStateUpdate (Unit* victim, WeaponAttackType attType, bool ext
}
}
Position* Unit::GetMeleeAttackPoint(Unit* attacker)
{
if (!attacker)
{
return nullptr;
}
AttackerSet attackers = getAttackers();
if (attackers.size() <= 1) // if the attackers are not more than one
{
return nullptr;
}
float meleeReach = GetExactDist2d(attacker);
if (meleeReach <= 0)
{
return nullptr;
}
float minAngle = 0;
Unit *refUnit = nullptr;
uint32 validAttackers = 0;
double attackerSize = attacker->GetCollisionRadius();
for (const auto& otherAttacker: attackers)
{
// if the otherAttacker is not valid, skip
if (!otherAttacker ||
otherAttacker->GetGUID() == attacker->GetGUID() ||
!otherAttacker->IsWithinMeleeRange(this) ||
otherAttacker->isMoving()
)
{
continue;
}
float curretAngle = atan(attacker->GetExactDist2d(otherAttacker) / meleeReach);
if (minAngle == 0 || curretAngle < minAngle)
{
minAngle = curretAngle;
refUnit = otherAttacker;
}
validAttackers++;
}
if (!validAttackers || !refUnit)
return nullptr;
float contactDist = attackerSize + refUnit->GetCollisionRadius();
float requiredAngle = atan(contactDist / meleeReach);
float attackersAngle = atan(attacker->GetExactDist2d(refUnit) / meleeReach);
// in instance: the more attacker there are, the higher will be the tollerance
// outside: creatures should not intersecate
float angleTollerance = attacker->GetMap()->IsDungeon() ? requiredAngle - requiredAngle * tanh(validAttackers / 5.0f) : requiredAngle;
if (attackersAngle > angleTollerance)
{
return nullptr;
}
double angle = atan(contactDist / meleeReach);
float angularRadius = frand(0.1f, 0.3f) + angle;
int8 direction = (urand(0, 1) ? -1 : 1);
float currentAngle = GetAngle(refUnit);
float absAngle = currentAngle + angularRadius * direction;
float x, y, z;
float distance = meleeReach - GetObjectSize();
GetNearPoint(attacker, x, y, z, distance, 0.0f, absAngle);
if (!GetMap()->CanReachPositionAndGetValidCoords(this, x, y, z, true, true))
{
GetNearPoint(attacker, x, y, z, distance, 0.0f, absAngle * -1); // try the other side
if (!GetMap()->CanReachPositionAndGetValidCoords(this, x, y, z, true, true))
{
return nullptr;
}
}
return new Position(x,y,z);
}
void Unit::HandleProcExtraAttackFor(Unit* victim)
{
while (m_extraAttacks)
@@ -3514,7 +3641,7 @@ bool Unit::isInAccessiblePlaceFor(Creature const* c) const
}
if (IsInWater())
return IsUnderWater() ? c->CanSwim() : (c->CanSwim() || c->CanFly());
return IsUnderWater() ? c->CanEnterWater() : (c->CanEnterWater() || c->CanFly());
else
return c->CanWalk() || c->CanFly() || (c->CanSwim() && IsInWater(true));
}
@@ -3534,33 +3661,38 @@ void Unit::UpdateEnvironmentIfNeeded(const uint8 option)
return;
}
if (option <= 1 && GetExactDistSq(&m_last_environment_position) < 2.5f * 2.5f)
// run environment checks everytime the unit moves
// more than it's average radius
// TODO: find better solution here
float radiusWidth = GetCollisionRadius();
float radiusHeight = GetCollisionHeight() / 2;
float radiusAvg = (radiusWidth + radiusHeight) / 2;
if (option <= 1 && GetExactDistSq(&m_last_environment_position) < radiusAvg*radiusAvg)
return;
m_last_environment_position.Relocate(GetPositionX(), GetPositionY(), GetPositionZ());
m_staticFloorZ = GetMap()->GetHeight(GetPhaseMask(), GetPositionX(), GetPositionY(), GetPositionZ());
m_is_updating_environment = true;
bool changed = false;
Creature* c = this->ToCreature();
Map* baseMap = const_cast<Map*>(GetBaseMap());
Creature* c = this->ToCreature();
if (!c || !baseMap)
{
m_is_updating_environment = false;
return;
}
bool canChangeFlying = option == 3 || ((c->GetScriptId() == 0 || GetInstanceId() == 0) && GetMotionMaster()->GetMotionSlotType(MOTION_SLOT_CONTROLLED) == NULL_MOTION_TYPE);
bool canChangeFlying = option == 3 || GetMotionMaster()->GetCurrentMovementGeneratorType() != WAYPOINT_MOTION_TYPE;
bool canFallGround = option == 0 && canChangeFlying && GetInstanceId() == 0 && !IsInCombat() && !GetVehicle() && !GetTransport() && !HasUnitMovementFlag(MOVEMENTFLAG_ONTRANSPORT) && !c->IsTrigger() && !c->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NOT_SELECTABLE) && GetMotionMaster()->GetCurrentMovementGeneratorType() <= RANDOM_MOTION_TYPE && !HasUnitState(UNIT_STATE_EVADE) && !IsControlledByPlayer();
float x = GetPositionX(), y = GetPositionY(), z = GetPositionZ();
bool isInAir = true;
float ground_z = z;
LiquidData liquidData;
liquidData.level = INVALID_HEIGHT;
ZLiquidStatus liquidStatus = baseMap->getLiquidStatus(x, y, z, MAP_ALL_LIQUIDS, &liquidData);
// IsInWater
bool enoughWater = (liquidData.level > INVALID_HEIGHT && liquidData.level > liquidData.depth_level && liquidData.level - liquidData.depth_level >= 1.5f); // also check if theres enough water - at least 2yd
bool enoughWater = baseMap->HasEnoughWater(this, liquidData);
m_last_isinwater_status = (liquidStatus & (LIQUID_MAP_IN_WATER | LIQUID_MAP_UNDER_WATER)) && enoughWater;
m_last_islittleabovewater_status = (liquidData.level > INVALID_HEIGHT && liquidData.level > liquidData.depth_level && liquidData.level <= z + 3.0f && liquidData.level > z - 1.0f);
@@ -3615,12 +3747,12 @@ void Unit::UpdateEnvironmentIfNeeded(const uint8 option)
// Refresh being in water
if (m_last_isinwater_status)
{
if (!c->CanFly() || z < liquidData.level - 2.0f)
if (!c->CanFly() || enoughWater)
{
if (!HasUnitState(UNIT_STATE_NO_ENVIRONMENT_UPD) && c->CanSwim() && (!HasUnitMovementFlag(MOVEMENTFLAG_SWIMMING) || !HasUnitMovementFlag(MOVEMENTFLAG_DISABLE_GRAVITY)))
{
SetSwim(true);
SetDisableGravity(true);
// SetDisableGravity(true);
changed = true;
}
isInAir = false;
@@ -3648,11 +3780,12 @@ void Unit::UpdateEnvironmentIfNeeded(const uint8 option)
{
if (GetMap()->GetGrid(x, y))
{
float temp = GetMap()->GetHeight(GetPhaseMask(), x, y, z, true, 100.0f);
float temp = GetFloorZ();
if (temp > INVALID_HEIGHT)
{
ground_z = (c->CanSwim() && liquidData.level > INVALID_HEIGHT) ? liquidData.level : temp;
isInAir = flyingBarelyInWater || G3D::fuzzyGt(z, ground_z + 0.75f) || G3D::fuzzyLt(z, ground_z - 0.5f);
bool canHover = c->CanHover();
isInAir = flyingBarelyInWater || (G3D::fuzzyGt(GetPositionZ(), ground_z + (canHover ? GetFloatValue(UNIT_FIELD_HOVERHEIGHT) : 0.0f) + GROUND_HEIGHT_TOLERANCE) || G3D::fuzzyLt(GetPositionZ(), ground_z - GROUND_HEIGHT_TOLERANCE)); // Can be underground too, prevent the falling
}
else
isInAir = true;
@@ -3685,24 +3818,26 @@ void Unit::UpdateEnvironmentIfNeeded(const uint8 option)
}
else if (c->CanFly() && isInAir)
{
if (!c->IsFalling() && (!HasUnitMovementFlag(MOVEMENTFLAG_CAN_FLY) || !HasUnitMovementFlag(MOVEMENTFLAG_DISABLE_GRAVITY) /*|| !HasUnitMovementFlag(MOVEMENTFLAG_HOVER)*/))
if (!c->IsFalling() && (!HasUnitMovementFlag(MOVEMENTFLAG_CAN_FLY) || !HasUnitMovementFlag(MOVEMENTFLAG_DISABLE_GRAVITY) || !HasUnitMovementFlag(MOVEMENTFLAG_HOVER)))
{
SetCanFly(true);
SetDisableGravity(true);
//SetHover(true);
if (IsAlive() && (c->CanHover() || HasAuraType(SPELL_AURA_HOVER)))
SetHover(true);
changed = true;
}
}
else
{
if (HasUnitMovementFlag(MOVEMENTFLAG_CAN_FLY) || HasUnitMovementFlag(MOVEMENTFLAG_FLYING) /*|| HasUnitMovementFlag(MOVEMENTFLAG_HOVER)*/)
if (HasUnitMovementFlag(MOVEMENTFLAG_CAN_FLY) || HasUnitMovementFlag(MOVEMENTFLAG_FLYING) || HasUnitMovementFlag(MOVEMENTFLAG_HOVER))
{
SetCanFly(false);
RemoveUnitMovementFlag(MOVEMENTFLAG_FLYING);
//SetHover(false);
if (!HasAuraType(SPELL_AURA_HOVER))
SetHover(false);
changed = true;
}
if (HasUnitMovementFlag(MOVEMENTFLAG_DISABLE_GRAVITY) && !HasUnitMovementFlag(MOVEMENTFLAG_SWIMMING) /*&& !HasUnitMovementFlag(MOVEMENTFLAG_HOVER)*/)
if (HasUnitMovementFlag(MOVEMENTFLAG_DISABLE_GRAVITY) && !HasUnitMovementFlag(MOVEMENTFLAG_SWIMMING) && !HasUnitMovementFlag(MOVEMENTFLAG_HOVER))
{
SetDisableGravity(false);
changed = true;
@@ -9570,6 +9705,13 @@ bool Unit::Attack(Unit* victim, bool meleeAttack)
if (GetTypeId() == TYPEID_PLAYER && IsMounted())
return false;
// creatures cannot attack while evading
Creature* creature = ToCreature();
if (creature && creature->IsInEvadeMode())
{
return false;
}
//if (HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_PACIFIED)) // pussywizard: wtf? why having this flag prevents from entering combat? it should just prevent melee attack
// return false;
@@ -9638,7 +9780,7 @@ bool Unit::Attack(Unit* victim, bool meleeAttack)
//if (GetTypeId() == TYPEID_UNIT)
// ToCreature()->SetCombatStartPosition(GetPositionX(), GetPositionY(), GetPositionZ());
if (GetTypeId() == TYPEID_UNIT && !IsPet())
if (creature && !IsPet())
{
// should not let player enter combat by right clicking target - doesn't helps
SetInCombatWith(victim);
@@ -9646,8 +9788,8 @@ bool Unit::Attack(Unit* victim, bool meleeAttack)
victim->SetInCombatWith(this);
AddThreat(victim, 0.0f);
ToCreature()->SendAIReaction(AI_REACTION_HOSTILE);
ToCreature()->CallAssistance();
creature->SendAIReaction(AI_REACTION_HOSTILE);
creature->CallAssistance();
}
// delay offhand weapon attack to next attack time
@@ -9659,7 +9801,7 @@ bool Unit::Attack(Unit* victim, bool meleeAttack)
// Let the pet know we've started attacking someting. Handles melee attacks only
// Spells such as auto-shot and others handled in WorldSession::HandleCastSpellOpcode
if (this->GetTypeId() == TYPEID_PLAYER && !m_Controlled.empty())
if (GetTypeId() == TYPEID_PLAYER && !m_Controlled.empty())
for (Unit::ControlSet::iterator itr = m_Controlled.begin(); itr != m_Controlled.end(); ++itr)
if (Unit* pet = *itr)
if (pet->IsAlive() && pet->GetTypeId() == TYPEID_UNIT)
@@ -12546,7 +12688,7 @@ void Unit::Mount(uint32 mount, uint32 VehicleId, uint32 creatureEntry)
WorldPacket data(SMSG_MOVE_SET_COLLISION_HGT, GetPackGUID().size() + 4 + 4);
data.append(GetPackGUID());
data << uint32(sWorld->GetGameTime()); // Packet counter
data << player->GetCollisionHeight(true);
data << player->GetCollisionHeight();
player->GetSession()->SendPacket(&data);
}
@@ -12566,7 +12708,7 @@ void Unit::Dismount()
WorldPacket data(SMSG_MOVE_SET_COLLISION_HGT, GetPackGUID().size() + 4 + 4);
data.append(GetPackGUID());
data << uint32(sWorld->GetGameTime()); // Packet counter
data << thisPlayer->GetCollisionHeight(false);
data << thisPlayer->GetCollisionHeight();
thisPlayer->GetSession()->SendPacket(&data);
}
@@ -12728,7 +12870,6 @@ void Unit::SetInCombatState(bool PvP, Unit* enemy, uint32 duration)
if (Creature* creature = ToCreature())
{
creature->m_targetsNotAcceptable.clear();
creature->UpdateEnvironmentIfNeeded(2);
// Set home position at place of engaging combat for escorted creatures
@@ -12746,6 +12887,8 @@ void Unit::SetInCombatState(bool PvP, Unit* enemy, uint32 duration)
creature->GetFormation()->MemberAttackStart(creature, enemy);
}
creature->RefreshSwimmingFlag();
if (IsPet())
{
UpdateSpeed(MOVE_RUN, true);
@@ -12880,7 +13023,7 @@ bool Unit::_IsValidAttackTarget(Unit const* target, SpellInfo const* bySpell, Wo
return false;
}
// check flags
if (target->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE | UNIT_FLAG_TAXI_FLIGHT | UNIT_FLAG_NOT_ATTACKABLE_1 | UNIT_FLAG_UNK_16)
if (target->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE | UNIT_FLAG_TAXI_FLIGHT | UNIT_FLAG_NOT_ATTACKABLE_1 | UNIT_FLAG_NON_ATTACKABLE_2)
|| (!HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE) && target->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_NPC))
|| (!target->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE) && HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_NPC))
|| (HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE) && target->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_IMMUNE_TO_PC))
@@ -13748,33 +13891,15 @@ Unit* Creature::SelectVictim()
if (target && _CanDetectFeignDeathOf(target) && CanCreatureAttack(target))
{
if (m_mmapNotAcceptableStartTime) m_mmapNotAcceptableStartTime = 0; // pussywizard: finding any valid target resets timer
SetInFront(target);
return target;
}
// pussywizard: if victim is not acceptable only due to mmaps, it may be for example a knockback, wait for a few secs before evading
if (!target && !isWorldBoss() && !GetInstanceId() && IsAlive() && (!CanHaveThreatList() || !getThreatManager().isThreatListEmpty()))
if (Unit* v = GetVictim())
if (isTargetNotAcceptableByMMaps(v->GetGUID(), sWorld->GetGameTime(), v))
if (_CanDetectFeignDeathOf(v) && CanCreatureAttack(v))
{
if (m_mmapNotAcceptableStartTime)
{
if (sWorld->GetGameTime() <= m_mmapNotAcceptableStartTime + 4)
return nullptr;
}
else
{
m_mmapNotAcceptableStartTime = sWorld->GetGameTime();
return nullptr;
}
}
// pussywizard: not sure why it's here
// pussywizard: if some npc (not player pet) is attacking us and we can't fight back - don't evade o_O
// last case when creature must not go to evade mode:
// it in combat but attacker not make any damage and not enter to aggro radius to have record in threat list
// Note: creature does not have targeted movement generator but has attacker in this case
for (AttackerSet::const_iterator itr = m_attackers.begin(); itr != m_attackers.end(); ++itr)
if ((*itr) && !CanCreatureAttack(*itr) && (*itr)->GetTypeId() != TYPEID_PLAYER && !(*itr)->ToCreature()->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN))
if ((*itr) && CanCreatureAttack(*itr) && (*itr)->GetTypeId() != TYPEID_PLAYER && !(*itr)->ToCreature()->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN))
return nullptr;
if (GetVehicle())
@@ -15812,7 +15937,7 @@ bool Unit::IsStandState() const
void Unit::SetStandState(uint8 state)
{
SetByteValue(UNIT_FIELD_BYTES_1, 0, state);
SetByteValue(UNIT_FIELD_BYTES_1, UNIT_BYTES_1_OFFSET_STAND_STATE, state);
if (IsStandState())
RemoveAurasWithInterruptFlags(AURA_INTERRUPT_FLAG_NOT_SEATED);
@@ -17442,6 +17567,9 @@ bool Unit::SetCharmedBy(Unit* charmer, CharmType type, AuraApplication const* au
CombatStop();
DeleteThreatList();
if (Creature* creature = ToCreature())
creature->RefreshSwimmingFlag();
if (GetTypeId() == TYPEID_PLAYER)
sScriptMgr->OnPlayerBeingCharmed(ToPlayer(), charmer, _oldFactionId, charmer->getFaction());
@@ -18634,7 +18762,7 @@ void Unit::_ExitVehicle(Position const* exitPosition)
{
float x = pos.GetPositionX() + 2.0f * cos(pos.GetOrientation() - M_PI / 2.0f);
float y = pos.GetPositionY() + 2.0f * sin(pos.GetOrientation() - M_PI / 2.0f);
float z = GetMap()->GetHeight(GetPhaseMask(), x, y, pos.GetPositionZ()) + 0.1f;
float z = GetMapHeight(x, y, pos.GetPositionZ());
if (z > INVALID_HEIGHT)
pos.Relocate(x, y, z);
}
@@ -18718,15 +18846,12 @@ void Unit::_ExitVehicle(Position const* exitPosition)
void Unit::BuildMovementPacket(ByteBuffer* data) const
{
if (GetTypeId() == TYPEID_UNIT && GetHoverHeight() >= 2.0f && !HasUnitMovementFlag(MOVEMENTFLAG_FALLING) && !movespline->isFalling()) // pussywizard: add disable gravity to hover (artifically, not to spoil speed and other things)
*data << uint32(GetUnitMovementFlags() | MOVEMENTFLAG_DISABLE_GRAVITY); // movement flags
else
*data << uint32(GetUnitMovementFlags()); // movement flags
*data << uint32(GetUnitMovementFlags()); // movement flags
*data << uint16(GetExtraUnitMovementFlags()); // 2.3.0
*data << uint32(World::GetGameTimeMS()); // time / counter
*data << GetPositionX();
*data << GetPositionY();
*data << GetPositionZ() + GetHoverHeight();
*data << GetPositionZ();
*data << GetOrientation();
// 0x00000200
@@ -18776,6 +18901,13 @@ bool Unit::IsFalling() const
return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_FALLING | MOVEMENTFLAG_FALLING_FAR) || movespline->isFalling();
}
/**
* @brief this method checks the current flag of a unit
*
* These flags can be set within the database or dynamically changed at runtime
* UNIT_FLAG_SWIMMING must be updated when a unit enters a swimmable area
*
*/
bool Unit::CanSwim() const
{
// Mirror client behavior, if this method returns false then client will not use swimming animation and for players will apply gravity as if there was no water
@@ -18787,7 +18919,7 @@ bool Unit::CanSwim() const
return false;
if (IsPet() && HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_PET_IN_COMBAT))
return true;
return HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_RENAME | UNIT_FLAG_UNK_15);
return HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_RENAME | UNIT_FLAG_SWIMMING);
}
void Unit::NearTeleportTo(float x, float y, float z, float orientation, bool casting /*= false*/, bool vehicleTeleport /*= false*/, bool withPet /*= false*/, bool removeTransport /*= false*/)
@@ -19496,9 +19628,15 @@ bool Unit::SetSwim(bool enable)
return false;
if (enable)
{
AddUnitMovementFlag(MOVEMENTFLAG_SWIMMING);
SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SWIMMING);
}
else
{
RemoveUnitMovementFlag(MOVEMENTFLAG_SWIMMING);
RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SWIMMING);
}
return true;
}
@@ -19570,13 +19708,23 @@ bool Unit::SetHover(bool enable, bool /*packetOnly = false*/)
if (enable == HasUnitMovementFlag(MOVEMENTFLAG_HOVER))
return false;
float hoverHeight = GetFloatValue(UNIT_FIELD_HOVERHEIGHT);
if (enable)
{
AddUnitMovementFlag(MOVEMENTFLAG_HOVER);
if (hoverHeight && GetPositionZ() - GetFloorZ() < hoverHeight)
UpdateHeight(GetPositionZ() + hoverHeight);
}
else
{
RemoveUnitMovementFlag(MOVEMENTFLAG_HOVER);
if (hoverHeight && (!isDying() || GetTypeId() != TYPEID_UNIT))
{
float newZ = std::max<float>(GetFloorZ(), GetPositionZ() - hoverHeight);
UpdateAllowedPositionZ(GetPositionX(), GetPositionY(), newZ);
UpdateHeight(newZ);
}
SendMovementFlagUpdate(); // pussywizard: needed for falling after death (instead of falling onto air at hover height)
}
@@ -19835,3 +19983,78 @@ bool Unit::IsInCombatWith(Unit const* who) const
// Nothing found, false.
return false;
}
/**
* @brief this method gets the diameter of a Unit by DB if any value is defined, otherwise it gets the value by the DBC
*
* If the player is mounted the diameter also takes in consideration the mount size
*
* @return float The diameter of a unit
*/
float Unit::GetCollisionWidth() const
{
if (GetTypeId() == TYPEID_PLAYER)
return GetObjectSize();
float scaleMod = GetObjectScale(); // 99% sure about this
float objectSize = GetObjectSize();
float defaultSize = DEFAULT_WORLD_OBJECT_SIZE * scaleMod;
//! Dismounting case - use basic default model data
CreatureDisplayInfoEntry const* displayInfo = sCreatureDisplayInfoStore.AssertEntry(GetNativeDisplayId());
CreatureModelDataEntry const* modelData = sCreatureModelDataStore.AssertEntry(displayInfo->ModelId);
if (IsMounted())
{
if (CreatureDisplayInfoEntry const* mountDisplayInfo = sCreatureDisplayInfoStore.LookupEntry(GetUInt32Value(UNIT_FIELD_MOUNTDISPLAYID)))
{
if (CreatureModelDataEntry const* mountModelData = sCreatureModelDataStore.LookupEntry(mountDisplayInfo->ModelId))
{
if (G3D::fuzzyGt(mountModelData->CollisionWidth, modelData->CollisionWidth))
modelData = mountModelData;
}
}
}
float collisionWidth = scaleMod * modelData->CollisionWidth * modelData->Scale * displayInfo->scale * 2;
// if the objectSize is the default value or the creature is mounted and we have a DBC value, then we can retrieve DBC value instead
return G3D::fuzzyGt(collisionWidth, 0.0f) && (G3D::fuzzyEq(objectSize,defaultSize) || IsMounted()) ? collisionWidth : objectSize;
}
/**
* @brief this method gets the radius of a Unit by DB if any value is defined, otherwise it gets the value by the DBC
*
* If the player is mounted the radius also takes in consideration the mount size
*
* @return float The radius of a unit
*/
float Unit::GetCollisionRadius() const
{
return GetCollisionWidth() / 2;
}
//! Return collision height sent to client
float Unit::GetCollisionHeight() const
{
float scaleMod = GetObjectScale(); // 99% sure about this
float defaultHeight = DEFAULT_WORLD_OBJECT_SIZE * scaleMod;
CreatureDisplayInfoEntry const* displayInfo = sCreatureDisplayInfoStore.AssertEntry(GetNativeDisplayId());
CreatureModelDataEntry const* modelData = sCreatureModelDataStore.AssertEntry(displayInfo->ModelId);
float collisionHeight = 0.0f;
if (IsMounted())
{
if (CreatureDisplayInfoEntry const* mountDisplayInfo = sCreatureDisplayInfoStore.LookupEntry(GetUInt32Value(UNIT_FIELD_MOUNTDISPLAYID)))
{
if (CreatureModelDataEntry const* mountModelData = sCreatureModelDataStore.LookupEntry(mountDisplayInfo->ModelId))
{
collisionHeight = scaleMod * (mountModelData->MountHeight + modelData->CollisionHeight * modelData->Scale * displayInfo->scale * 0.5f);
}
}
}
else
collisionHeight = scaleMod * modelData->CollisionHeight * modelData->Scale * displayInfo->scale;
return collisionHeight == 0.0f ? defaultHeight : collisionHeight;
}

View File

@@ -135,6 +135,14 @@ enum SpellFacingFlags
#define BASE_MAXDAMAGE 2.0f
#define BASE_ATTACK_TIME 2000
enum UnitBytes1Offsets : uint8
{
UNIT_BYTES_1_OFFSET_STAND_STATE = 0,
UNIT_BYTES_1_OFFSET_PET_TALENTS = 1,
UNIT_BYTES_1_OFFSET_VIS_FLAG = 2,
UNIT_BYTES_1_OFFSET_ANIM_TIER = 3
};
// byte value (UNIT_FIELD_BYTES_1, 0)
enum UnitStandStateType
{
@@ -164,9 +172,11 @@ enum UnitStandFlags
// byte flags value (UNIT_FIELD_BYTES_1, 3)
enum UnitBytes1_Flags
{
UNIT_BYTE1_FLAG_GROUND = 0x00,
UNIT_BYTE1_FLAG_ALWAYS_STAND = 0x01,
UNIT_BYTE1_FLAG_HOVER = 0x02,
UNIT_BYTE1_FLAG_UNK_3 = 0x04,
UNIT_BYTE1_FLAG_FLY = 0x03,
UNIT_BYTE1_FLAG_SUBMERGED = 0x04,
UNIT_BYTE1_FLAG_ALL = 0xFF
};
@@ -586,8 +596,8 @@ enum UnitFlags
UNIT_FLAG_PVP = 0x00001000, // changed in 3.0.3
UNIT_FLAG_SILENCED = 0x00002000, // silenced, 2.1.1
UNIT_FLAG_CANNOT_SWIM = 0x00004000, // 2.0.8
UNIT_FLAG_UNK_15 = 0x00008000,
UNIT_FLAG_UNK_16 = 0x00010000,
UNIT_FLAG_SWIMMING = 0x00008000, // shows swim animation in water
UNIT_FLAG_NON_ATTACKABLE_2 = 0x00010000, // removes attackable icon, if on yourself, cannot assist self but can cast TARGET_SELF spells - added by SPELL_AURA_MOD_UNATTACKABLE
UNIT_FLAG_PACIFIED = 0x00020000, // 3.0.3 ok
UNIT_FLAG_STUNNED = 0x00040000, // 3.0.3 ok
UNIT_FLAG_IN_COMBAT = 0x00080000,
@@ -602,7 +612,7 @@ enum UnitFlags
UNIT_FLAG_UNK_28 = 0x10000000,
UNIT_FLAG_UNK_29 = 0x20000000, // used in Feing Death spell
UNIT_FLAG_SHEATHE = 0x40000000,
UNIT_FLAG_UNK_31 = 0x80000000
UNIT_FLAG_IMMUNE = 0x80000000, // Immune to damage
};
// Value masks for UNIT_FIELD_FLAGS_2
@@ -1254,6 +1264,26 @@ private:
GlobalCooldownMgr _GlobalCooldownMgr;
};
struct AttackPosition {
AttackPosition(Position pos) : _pos(pos), _taken(false) {}
bool operator==(const int val)
{
return !val;
};
int operator=(const int val)
{
if (!val)
{
// _pos = NULL;
_taken = false;
return 0; // NULL
}
return 0; // NULL
};
Position _pos;
bool _taken;
};
// for clearing special attacks
#define REACTIVE_TIMER_START 5000
@@ -1404,8 +1434,10 @@ public:
virtual void SetCanDualWield(bool value) { m_canDualWield = value; }
[[nodiscard]] float GetCombatReach() const override { return m_floatValues[UNIT_FIELD_COMBATREACH]; }
[[nodiscard]] float GetMeleeReach() const { float reach = m_floatValues[UNIT_FIELD_COMBATREACH]; return reach > MIN_MELEE_REACH ? reach : MIN_MELEE_REACH; }
[[nodiscard]] bool IsWithinRange(Unit const* obj, float dist) const;
bool IsWithinCombatRange(const Unit* obj, float dist2compare) const;
bool IsWithinMeleeRange(const Unit* obj, float dist = MELEE_RANGE) const;
bool IsWithinMeleeRange(const Unit* obj, float dist = 0.f) const;
float GetMeleeRange(Unit const* target) const;
bool GetRandomContactPoint(const Unit* target, float& x, float& y, float& z, bool force = false) const;
uint32 m_extraAttacks;
bool m_canDualWield;
@@ -1433,6 +1465,7 @@ public:
bool AttackStop();
void RemoveAllAttackers();
[[nodiscard]] AttackerSet const& getAttackers() const { return m_attackers; }
[[nodiscard]] Position* GetMeleeAttackPoint(Unit* attacker);
[[nodiscard]] bool isAttackingPlayer() const;
[[nodiscard]] Unit* GetVictim() const { return m_attacking; }
@@ -1570,8 +1603,8 @@ public:
[[nodiscard]] bool IsStandState() const;
void SetStandState(uint8 state);
void SetStandFlags(uint8 flags) { SetByteFlag(UNIT_FIELD_BYTES_1, 2, flags); }
void RemoveStandFlags(uint8 flags) { RemoveByteFlag(UNIT_FIELD_BYTES_1, 2, flags); }
void SetStandFlags(uint8 flags) { SetByteFlag(UNIT_FIELD_BYTES_1, UNIT_BYTES_1_OFFSET_VIS_FLAG, flags); }
void RemoveStandFlags(uint8 flags) { RemoveByteFlag(UNIT_FIELD_BYTES_1, UNIT_BYTES_1_OFFSET_VIS_FLAG, flags); }
[[nodiscard]] bool IsMounted() const { return HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_MOUNT); }
[[nodiscard]] uint32 GetMountID() const { return GetUInt32Value(UNIT_FIELD_MOUNTDISPLAYID); }
@@ -1789,8 +1822,6 @@ public:
//void SendMonsterMove(float NewPosX, float NewPosY, float NewPosZ, uint8 type, uint32 MovementFlags, uint32 Time, Player* player = nullptr);
void SendMovementFlagUpdate(bool self = false);
[[nodiscard]] bool IsLevitating() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_DISABLE_GRAVITY); }
[[nodiscard]] bool IsWalking() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_WALKING); }
virtual bool SetWalk(bool enable);
virtual bool SetDisableGravity(bool disable, bool packetOnly = false);
virtual bool SetSwim(bool enable);
@@ -2173,7 +2204,7 @@ public:
uint32 GetDisplayId() { return GetUInt32Value(UNIT_FIELD_DISPLAYID); }
virtual void SetDisplayId(uint32 modelId);
uint32 GetNativeDisplayId() { return GetUInt32Value(UNIT_FIELD_NATIVEDISPLAYID); }
[[nodiscard]] uint32 GetNativeDisplayId() const { return GetUInt32Value(UNIT_FIELD_NATIVEDISPLAYID); }
void RestoreDisplayId();
void SetNativeDisplayId(uint32 modelId) { SetUInt32Value(UNIT_FIELD_NATIVEDISPLAYID, modelId); }
void setTransForm(uint32 spellid) { m_transform = spellid;}
@@ -2365,6 +2396,8 @@ public:
void BuildMovementPacket(ByteBuffer* data) const;
[[nodiscard]] virtual bool CanSwim() const;
[[nodiscard]] bool IsLevitating() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_DISABLE_GRAVITY); }
[[nodiscard]] bool IsWalking() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_WALKING); }
[[nodiscard]] bool isMoving() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_MASK_MOVING); }
[[nodiscard]] bool isTurning() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_MASK_TURNING); }
[[nodiscard]] bool IsHovering() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_HOVER); }
@@ -2373,6 +2406,7 @@ public:
[[nodiscard]] bool IsFlying() const { return m_movementInfo.HasMovementFlag(MOVEMENTFLAG_FLYING | MOVEMENTFLAG_DISABLE_GRAVITY); }
[[nodiscard]] bool IsFalling() const;
[[nodiscard]] float GetHoverHeight() const { return IsHovering() ? GetFloatValue(UNIT_FIELD_HOVERHEIGHT) : 0.0f; }
[[nodiscard]] virtual bool CanEnterWater() const = 0;
void RewardRage(uint32 damage, uint32 weaponSpeedHitFactor, bool attacker);
@@ -2387,11 +2421,6 @@ public:
TempSummon* ToTempSummon() { if (IsSummon()) return reinterpret_cast<TempSummon*>(this); else return nullptr; }
[[nodiscard]] const TempSummon* ToTempSummon() const { if (IsSummon()) return reinterpret_cast<const TempSummon*>(this); else return nullptr; }
// pussywizard:
// MMaps
std::map<uint64, MMapTargetData> m_targetsNotAcceptable;
bool isTargetNotAcceptableByMMaps(uint64 guid, uint32 currTime, const Position* t = nullptr) const { std::map<uint64, MMapTargetData>::const_iterator itr = m_targetsNotAcceptable.find(guid); if (itr != m_targetsNotAcceptable.end() && (itr->second._endTime >= currTime || (t && !itr->second.PosChanged(*this, *t)))) return true; return false; }
uint32 m_mmapNotAcceptableStartTime;
// Safe mover
std::set<SafeUnitPointer*> SafeUnitPointerSet;
void AddPointedBy(SafeUnitPointer* sup) { SafeUnitPointerSet.insert(sup); }
@@ -2450,6 +2479,10 @@ public:
// Movement info
Movement::MoveSpline* movespline;
[[nodiscard]] float GetCollisionHeight() const override;
[[nodiscard]] float GetCollisionWidth() const override;
[[nodiscard]] float GetCollisionRadius() const override;
protected:
explicit Unit (bool isWorldObject);
@@ -2565,6 +2598,7 @@ private:
HostileRefManager m_HostileRefManager;
FollowerRefManager m_FollowingRefManager;
Unit* m_comboTarget;
ComboPointHolderSet m_ComboPointHolders;