Core/Creatures: Moved autoattack handling from scripts to game

Port From (https://github.com/TrinityCore/TrinityCore/commit/605e5f94c0d71cad8e83fa5a07eaec4e6bed9cc3)
This commit is contained in:
hondacrx
2024-02-05 18:18:30 -05:00
parent aa696fb176
commit 437fe2d8fd
94 changed files with 192 additions and 358 deletions
@@ -554,6 +554,10 @@ namespace Game.Entities
AIUpdateTick(diff);
DoMeleeAttackIfReady();
// creature can be dead after UpdateAI call
// CORPSE/DEAD state will processed at next tick (in other case death timer will be updated unexpectedly)
if (!IsAlive())
break;
+1 -1
View File
@@ -144,7 +144,7 @@ namespace Game.Entities
uint m_titanGripPenaltySpellId;
uint m_deathTimer;
long m_deathExpireTime;
byte m_swingErrorMsg;
AttackSwingErr? m_swingErrorMsg;
DateTime m_regenInterruptTimestamp;
uint m_regenTimerCount;
uint m_foodEmoteTimerCount;
+14 -68
View File
@@ -421,69 +421,7 @@ namespace Game.Entities
m_achievementSys.UpdateTimedCriteria(TimeSpan.FromMilliseconds(diff));
if (HasUnitState(UnitState.MeleeAttacking) && !HasUnitState(UnitState.Casting | UnitState.Charging))
{
Unit victim = GetVictim();
if (victim != null)
{
// default combat reach 10
// TODO add weapon, skill check
if (IsAttackReady(WeaponAttackType.BaseAttack))
{
if (!IsWithinMeleeRange(victim))
{
SetAttackTimer(WeaponAttackType.BaseAttack, 100);
if (m_swingErrorMsg != 1) // send single time (client auto repeat)
{
SendAttackSwingNotInRange();
m_swingErrorMsg = 1;
}
}
//120 degrees of radiant range, if player is not in boundary radius
else if (!IsWithinBoundaryRadius(victim) && !HasInArc(2 * MathFunctions.PI / 3, victim))
{
SetAttackTimer(WeaponAttackType.BaseAttack, 100);
if (m_swingErrorMsg != 2) // send single time (client auto repeat)
{
SendAttackSwingBadFacingAttack();
m_swingErrorMsg = 2;
}
}
else
{
m_swingErrorMsg = 0; // reset swing error state
// prevent base and off attack in same time, delay attack at 0.2 sec
if (HaveOffhandWeapon())
if (GetAttackTimer(WeaponAttackType.OffAttack) < SharedConst.AttackDisplayDelay)
SetAttackTimer(WeaponAttackType.OffAttack, SharedConst.AttackDisplayDelay);
// do attack
AttackerStateUpdate(victim, WeaponAttackType.BaseAttack);
ResetAttackTimer(WeaponAttackType.BaseAttack);
}
}
if (!IsInFeralForm() && HaveOffhandWeapon() && IsAttackReady(WeaponAttackType.OffAttack))
{
if (!IsWithinMeleeRange(victim))
SetAttackTimer(WeaponAttackType.OffAttack, 100);
else if (!IsWithinBoundaryRadius(victim) && !HasInArc(2 * MathFunctions.PI / 3, victim))
SetAttackTimer(WeaponAttackType.BaseAttack, 100);
else
{
// prevent base and off attack in same time, delay attack at 0.2 sec
if (GetAttackTimer(WeaponAttackType.BaseAttack) < SharedConst.AttackDisplayDelay)
SetAttackTimer(WeaponAttackType.BaseAttack, SharedConst.AttackDisplayDelay);
// do attack
AttackerStateUpdate(victim, WeaponAttackType.OffAttack);
ResetAttackTimer(WeaponAttackType.OffAttack);
}
}
}
}
DoMeleeAttackIfReady();
if (HasPlayerFlag(PlayerFlags.Resting))
_restMgr.Update(diff);
@@ -7707,11 +7645,19 @@ namespace Game.Entities
public bool CanTameExoticPets() { return IsGameMaster() || HasAuraType(AuraType.AllowTamePetType); }
void SendAttackSwingCantAttack() { SendPacket(new AttackSwingError(AttackSwingErr.CantAttack)); }
public void SendAttackSwingCancelAttack() { SendPacket(new CancelCombat()); }
void SendAttackSwingDeadTarget() { SendPacket(new AttackSwingError(AttackSwingErr.DeadTarget)); }
public void SendAttackSwingNotInRange() { SendPacket(new AttackSwingError(AttackSwingErr.NotInRange)); }
void SendAttackSwingBadFacingAttack() { SendPacket(new AttackSwingError(AttackSwingErr.BadFacing)); }
public void SendAttackSwingCancelAttack()
{
SendPacket(new CancelCombat());
}
public void SetAttackSwingError(AttackSwingErr? err)
{
if (err.HasValue && err.Value != m_swingErrorMsg)
SendPacket(new AttackSwingError(err.Value));
m_swingErrorMsg = err;
}
public void SendAutoRepeatCancel(Unit target)
{
CancelAutoRepeat cancelAutoRepeat = new();
+72 -11
View File
@@ -281,14 +281,8 @@ namespace Game.Entities
Creature creature = ToCreature();
// creatures cannot attack while evading
if (creature != null)
{
if (creature.IsInEvadeMode())
return false;
if (creature.CanMelee())
meleeAttack = false;
}
if (creature != null && creature.IsInEvadeMode())
return false;
// nobody can attack GM in GM-mode
if (victim.IsTypeId(TypeId.Player))
@@ -522,12 +516,15 @@ namespace Game.Entities
return m_baseAttackSpeed[(int)att];
}
public void AttackerStateUpdate(Unit victim, WeaponAttackType attType = WeaponAttackType.BaseAttack, bool extra = false)
public void DoMeleeAttackIfReady()
{
if (HasUnitFlag(UnitFlags.Pacified))
if (!HasUnitState(UnitState.MeleeAttacking))
return;
if (HasUnitState(UnitState.CannotAutoattack) && !extra)
if (HasUnitState(UnitState.Charging))
return;
if (IsCreature() && !ToCreature().CanMelee())
return;
if (HasUnitState(UnitState.Casting))
@@ -537,6 +534,70 @@ namespace Game.Entities
return;
}
Unit victim = GetVictim();
if (victim == null)
return;
AttackSwingErr? getAutoAttackError()
{
if (!IsWithinMeleeRange(victim))
return AttackSwingErr.NotInRange;
//120 degrees of radiant range, if player is not in boundary radius
if (!IsWithinBoundaryRadius(victim) && !HasInArc(2 * MathF.PI / 3, victim))
return AttackSwingErr.BadFacing;
return null;
};
if (IsAttackReady(WeaponAttackType.BaseAttack))
{
AttackSwingErr? autoAttackError = getAutoAttackError();
if (!autoAttackError.HasValue)
{
// prevent base and off attack in same time, delay attack at 0.2 sec
if (HaveOffhandWeapon())
if (GetAttackTimer(WeaponAttackType.OffAttack) < SharedConst.AttackDisplayDelay)
SetAttackTimer(WeaponAttackType.OffAttack, SharedConst.AttackDisplayDelay);
// do attack
AttackerStateUpdate(victim, WeaponAttackType.BaseAttack);
ResetAttackTimer(WeaponAttackType.BaseAttack);
}
else
SetAttackTimer(WeaponAttackType.BaseAttack, 100);
Player attackerPlayer = ToPlayer();
if (attackerPlayer != null)
attackerPlayer.SetAttackSwingError(autoAttackError);
}
if (!IsInFeralForm() && HaveOffhandWeapon() && IsAttackReady(WeaponAttackType.OffAttack))
{
AttackSwingErr? autoAttackError = getAutoAttackError();
if (!autoAttackError.HasValue)
{
// prevent base and off attack in same time, delay attack at 0.2 sec
if (GetAttackTimer(WeaponAttackType.BaseAttack) < SharedConst.AttackDisplayDelay)
SetAttackTimer(WeaponAttackType.BaseAttack, SharedConst.AttackDisplayDelay);
// do attack
AttackerStateUpdate(victim, WeaponAttackType.OffAttack);
ResetAttackTimer(WeaponAttackType.OffAttack);
}
else
SetAttackTimer(WeaponAttackType.OffAttack, 100);
}
}
public void AttackerStateUpdate(Unit victim, WeaponAttackType attType = WeaponAttackType.BaseAttack, bool extra = false)
{
if (HasUnitFlag(UnitFlags.Pacified))
return;
if (HasUnitState(UnitState.CannotAutoattack) && !extra)
return;
if (HasAuraType(AuraType.DisableAttackingExceptAbilities))
return;
@@ -1236,7 +1236,6 @@ namespace Game.Entities
case UnitState.Confused:
if (!HasUnitState(UnitState.Stunned))
{
ClearUnitState(UnitState.MeleeAttacking);
SendMeleeAttackStop();
// SendAutoRepeatCancel ?
SetConfused(true);
@@ -1245,7 +1244,6 @@ namespace Game.Entities
case UnitState.Fleeing:
if (!HasUnitState(UnitState.Stunned | UnitState.Confused))
{
ClearUnitState(UnitState.MeleeAttacking);
SendMeleeAttackStop();
// SendAutoRepeatCancel ?
SetFeared(true);
+1 -1
View File
@@ -672,7 +672,7 @@ namespace Game.Entities
public void RemoveAllDynObjects()
{
while (!m_dynObj.Empty())
m_dynObj.First().Remove();
m_dynObj.Last().Remove();
}
public GameObject GetGameObject(uint spellId)