Core/Creatures: Moved autoattack handling from scripts to game
Port From (https://github.com/TrinityCore/TrinityCore/commit/605e5f94c0d71cad8e83fa5a07eaec4e6bed9cc3)
This commit is contained in:
@@ -71,8 +71,6 @@ namespace Game.AI
|
||||
if (info != null)
|
||||
_events.ScheduleEvent(spellId, info.cooldown, info.cooldown * 2);
|
||||
}
|
||||
else
|
||||
DoMeleeAttackIfReady();
|
||||
}
|
||||
|
||||
public override void SpellInterrupted(uint spellId, uint unTimeMs)
|
||||
@@ -87,10 +85,7 @@ namespace Game.AI
|
||||
|
||||
public override void UpdateAI(uint diff)
|
||||
{
|
||||
if (!UpdateVictim())
|
||||
return;
|
||||
|
||||
DoMeleeAttackIfReady();
|
||||
UpdateVictim();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,6 +188,7 @@ namespace Game.AI
|
||||
_minRange = spellInfo != null ? spellInfo.GetMinRange(false) : 0;
|
||||
creature.m_CombatDistance = spellInfo != null ? spellInfo.GetMaxRange(false) : 0;
|
||||
creature.m_SightDistance = creature.m_CombatDistance;
|
||||
creature.SetCanMelee(false);
|
||||
}
|
||||
|
||||
public override bool CanAIAttack(Unit victim)
|
||||
@@ -234,6 +230,7 @@ namespace Game.AI
|
||||
LoadConditions();
|
||||
_doDismiss = false;
|
||||
_dismissTimer = VEHICLE_DISMISS_TIME;
|
||||
me.SetCanMelee(false);
|
||||
}
|
||||
|
||||
public override void UpdateAI(uint diff)
|
||||
@@ -315,10 +312,7 @@ namespace Game.AI
|
||||
|
||||
public override void UpdateAI(uint diff)
|
||||
{
|
||||
if (!UpdateVictim())
|
||||
return;
|
||||
|
||||
DoMeleeAttackIfReady();
|
||||
UpdateVictim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,10 +12,7 @@ namespace Game.AI
|
||||
|
||||
public override void UpdateAI(uint diff)
|
||||
{
|
||||
if (!UpdateVictim())
|
||||
return;
|
||||
|
||||
DoMeleeAttackIfReady();
|
||||
UpdateVictim();
|
||||
}
|
||||
|
||||
public override bool CanSeeAlways(WorldObject obj)
|
||||
|
||||
@@ -11,7 +11,8 @@ namespace Game.AI
|
||||
{
|
||||
public PassiveAI(Creature creature) : base(creature)
|
||||
{
|
||||
creature.SetReactState(ReactStates.Passive);
|
||||
me.SetReactState(ReactStates.Passive);
|
||||
me.SetCanMelee(false);
|
||||
}
|
||||
|
||||
public override void UpdateAI(uint diff)
|
||||
@@ -43,8 +44,6 @@ namespace Game.AI
|
||||
{
|
||||
if (!me.IsValidAttackTarget(me.GetVictim()))
|
||||
me.AttackStop();
|
||||
else
|
||||
DoMeleeAttackIfReady();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,15 +50,6 @@ namespace Game.AI
|
||||
StopAttack();
|
||||
return;
|
||||
}
|
||||
|
||||
// Check before attacking to prevent pets from leaving stay position
|
||||
if (me.GetCharmInfo().HasCommandState(CommandStates.Stay))
|
||||
{
|
||||
if (me.GetCharmInfo().IsCommandAttack() || (me.GetCharmInfo().IsAtStay() && me.IsWithinMeleeRange(me.GetVictim())))
|
||||
DoMeleeAttackIfReady();
|
||||
}
|
||||
else
|
||||
DoMeleeAttackIfReady();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -52,41 +52,6 @@ namespace Game.AI
|
||||
targets.Sort(new ObjectDistanceOrderPred(me, ascending));
|
||||
}
|
||||
|
||||
public void DoMeleeAttackIfReady()
|
||||
{
|
||||
if (me.IsCreature() && !me.ToCreature().CanMelee())
|
||||
return;
|
||||
|
||||
if (me.HasUnitState(UnitState.Casting))
|
||||
{
|
||||
Spell channeledSpell = me.GetCurrentSpell(CurrentSpellTypes.Channeled);
|
||||
if (channeledSpell == null || !channeledSpell.GetSpellInfo().HasAttribute(SpellAttr5.AllowActionsDuringChannel))
|
||||
return;
|
||||
}
|
||||
|
||||
Unit victim = me.GetVictim();
|
||||
|
||||
if (!me.IsWithinMeleeRange(victim))
|
||||
return;
|
||||
|
||||
// Check that the victim is in front of the unit
|
||||
if (!me.HasInArc(2 * MathF.PI / 3, victim))
|
||||
return;
|
||||
|
||||
//Make sure our attack is ready and we aren't currently casting before checking distance
|
||||
if (me.IsAttackReady())
|
||||
{
|
||||
me.AttackerStateUpdate(victim);
|
||||
me.ResetAttackTimer();
|
||||
}
|
||||
|
||||
if (me.HaveOffhandWeapon() && me.IsAttackReady(WeaponAttackType.OffAttack))
|
||||
{
|
||||
me.AttackerStateUpdate(victim, WeaponAttackType.OffAttack);
|
||||
me.ResetAttackTimer(WeaponAttackType.OffAttack);
|
||||
}
|
||||
}
|
||||
|
||||
public bool DoSpellAttackIfReady(uint spellId)
|
||||
{
|
||||
if (me.HasUnitState(UnitState.Casting) || !me.IsAttackReady())
|
||||
|
||||
@@ -573,8 +573,6 @@ namespace Game.AI
|
||||
{
|
||||
if (IsRangedAttacker())
|
||||
DoRangedAttackIfReady();
|
||||
else
|
||||
DoMeleeAttackIfReady();
|
||||
}
|
||||
|
||||
public void CancelAllShapeshifts()
|
||||
|
||||
@@ -45,10 +45,7 @@ namespace Game.AI
|
||||
public override void UpdateAI(uint diff)
|
||||
{
|
||||
//Check if we have a current target
|
||||
if (!UpdateVictim())
|
||||
return;
|
||||
|
||||
DoMeleeAttackIfReady();
|
||||
UpdateVictim();
|
||||
}
|
||||
|
||||
//Start movement toward victim
|
||||
@@ -655,8 +652,6 @@ namespace Game.AI
|
||||
if (me.HasUnitState(UnitState.Casting))
|
||||
return;
|
||||
});
|
||||
|
||||
DoMeleeAttackIfReady();
|
||||
}
|
||||
|
||||
public void _DespawnAtEvade()
|
||||
@@ -766,8 +761,6 @@ namespace Game.AI
|
||||
if (me.HasUnitState(UnitState.Casting))
|
||||
return;
|
||||
});
|
||||
|
||||
DoMeleeAttackIfReady();
|
||||
}
|
||||
|
||||
// Hook used to execute events scheduled into EventMap without the need
|
||||
|
||||
@@ -261,10 +261,7 @@ namespace Game.AI
|
||||
|
||||
public virtual void UpdateEscortAI(uint diff)
|
||||
{
|
||||
if (!UpdateVictim())
|
||||
return;
|
||||
|
||||
DoMeleeAttackIfReady();
|
||||
UpdateVictim();
|
||||
}
|
||||
|
||||
public override void MovementInform(MovementGeneratorType moveType, uint Id)
|
||||
|
||||
@@ -158,10 +158,7 @@ namespace Game.AI
|
||||
|
||||
void UpdateFollowerAI(uint diff)
|
||||
{
|
||||
if (!UpdateVictim())
|
||||
return;
|
||||
|
||||
DoMeleeAttackIfReady();
|
||||
UpdateVictim();
|
||||
}
|
||||
|
||||
public void StartFollow(Player player, uint factionForFollower = 0, Quest quest = null)
|
||||
|
||||
@@ -310,21 +310,13 @@ namespace Game.AI
|
||||
|
||||
CheckConditions(diff);
|
||||
|
||||
bool hasVictim = UpdateVictim();
|
||||
UpdateVictim();
|
||||
|
||||
GetScript().OnUpdate(diff);
|
||||
|
||||
UpdatePath(diff);
|
||||
UpdateFollow(diff);
|
||||
UpdateDespawn(diff);
|
||||
|
||||
if (!IsAIControlled())
|
||||
return;
|
||||
|
||||
if (!hasVictim)
|
||||
return;
|
||||
|
||||
DoMeleeAttackIfReady();
|
||||
}
|
||||
|
||||
bool IsEscortInvokerInRange()
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user