Core/AI: Clean up charm AI handling.

Port From (https://github.com/TrinityCore/TrinityCore/commit/e4e8c1c59c8b37216814526b4d2551f23934f465)
This commit is contained in:
hondacrx
2021-10-31 13:55:26 -04:00
parent 7050fda482
commit 26940fa650
33 changed files with 472 additions and 443 deletions
+4 -3
View File
@@ -322,11 +322,12 @@ namespace Game.AI
public override void AttackStart(Unit victim) { }
public override void OnCharmed(bool apply)
public override void OnCharmed(bool isNew)
{
if (!me.GetVehicleKit().IsVehicleInUse() && !apply && _hasConditions)//was used and has conditions
bool charmed = me.IsCharmed();
if (!me.GetVehicleKit().IsVehicleInUse() && !charmed && _hasConditions)//was used and has conditions
_doDismiss = true;//needs reset
else if (apply)
else if (charmed)
_doDismiss = false;//in use again
_dismissTimer = VEHICLE_DISMISS_TIME;//reset timer
+11 -4
View File
@@ -44,13 +44,20 @@ namespace Game.AI
_moveInLineOfSightLocked = false;
}
public override void OnCharmed(bool apply)
public override void OnCharmed(bool isNew)
{
if (apply)
if (isNew && !me.IsCharmed() && !me.LastCharmerGUID.IsEmpty())
{
me.NeedChangeAI = true;
me.IsAIEnabled = false;
if (!me.HasReactState(ReactStates.Passive))
{
Unit lastCharmer = Global.ObjAccessor.GetUnit(me, me.LastCharmerGUID);
if (lastCharmer != null)
me.EngageWithTarget(lastCharmer);
}
me.LastCharmerGUID.Clear();
}
base.OnCharmed(isNew);
}
public void Talk(uint id, WorldObject whisperTarget = null)
+1 -7
View File
@@ -76,12 +76,6 @@ namespace Game.AI
me.RemoveDynamicFlag(UnitDynFlags.Lootable);
}
public override void OnCharmed(bool apply)
{
me.NeedChangeAI = true;
me.IsAIEnabled = false;
}
public override void MoveInLineOfSight(Unit who) { }
public override void EnterEvadeMode(EvadeReason why) { }
@@ -98,7 +92,7 @@ namespace Game.AI
public override void AttackStart(Unit unit) { }
public override void UpdateAI(uint diff) { }
public override void EnterEvadeMode(EvadeReason why) { }
public override void OnCharmed(bool apply) { }
public override void OnCharmed(bool isNew) { }
}
public class CritterAI : PassiveAI
-6
View File
@@ -601,12 +601,6 @@ namespace Game.AI
}
}
public override void OnCharmed(bool apply)
{
me.NeedChangeAI = true;
me.IsAIEnabled = false;
}
void ClearCharmInfoFlags()
{
// Quick access to set all flags to FALSE
+12 -1
View File
@@ -463,7 +463,18 @@ namespace Game.AI
public virtual void Reset() { }
public virtual void OnCharmed(bool apply) { }
/// <summary>
// Called when unit's charm state changes with isNew = false
// Implementation should call me->ScheduleAIChange() if AI replacement is desired
// If this call is made, AI will be replaced on the next tick
// When replacement is made, OnCharmed is called with isNew = true
/// </summary>
/// <param name="apply"></param>
public virtual void OnCharmed(bool isNew)
{
if (!isNew)
me.ScheduleAIChange();
}
public virtual bool ShouldSparWith(Unit target) { return false; }
+12 -5
View File
@@ -655,8 +655,6 @@ namespace Game.AI
return null;
}
public override void OnCharmed(bool apply) { }
// helper functions to determine player info
public bool IsHealer(Player who = null)
{
@@ -710,7 +708,14 @@ namespace Game.AI
{
Unit charmer = me.GetCharmer();
if (charmer)
return charmer.IsAIEnabled ? charmer.GetAI().SelectTarget(SelectAggroTarget.Random, 0, new ValidTargetSelectPredicate(this)) : charmer.GetVictim();
{
UnitAI charmerAI = charmer.GetAI();
if (charmerAI != null)
return charmerAI.SelectTarget(SelectAggroTarget.Random, 0, new ValidTargetSelectPredicate(this));
return charmer.GetVictim();
}
return null;
}
@@ -1344,9 +1349,9 @@ namespace Game.AI
}
}
public override void OnCharmed(bool apply)
public override void OnCharmed(bool isNew)
{
if (apply)
if (me.IsCharmed())
{
me.CastStop();
me.AttackStop();
@@ -1362,6 +1367,8 @@ namespace Game.AI
me.GetMotionMaster().Clear();
me.StopMoving();
}
base.OnCharmed(isNew);
}
}
+2 -2
View File
@@ -673,7 +673,7 @@ namespace Game.AI
foreach (var id in this)
{
Creature summon = ObjectAccessor.GetCreature(_me, id);
if (summon && summon.IsAIEnabled && (entry == 0 || summon.GetEntry() == entry))
if (summon && summon.IsAIEnabled() && (entry == 0 || summon.GetEntry() == entry))
{
summon.GetAI().DoZoneInCombat(null);
}
@@ -760,7 +760,7 @@ namespace Game.AI
foreach (var guid in summons)
{
Creature summon = ObjectAccessor.GetCreature(_me, guid);
if (summon && summon.IsAIEnabled)
if (summon && summon.IsAIEnabled())
summon.GetAI().DoAction(action);
}
}
+6 -5
View File
@@ -714,17 +714,18 @@ namespace Game.AI
GetScript().ProcessEventsFor(apply ? SmartEvents.PassengerBoarded : SmartEvents.PassengerRemoved, passenger, (uint)seatId, 0, apply);
}
public override void OnCharmed(bool apply)
public override void OnCharmed(bool isNew)
{
if (apply) // do this before we change charmed state, as charmed state might prevent these things from processing
bool charmed = me.IsCharmed();
if (charmed) // do this before we change charmed state, as charmed state might prevent these things from processing
{
if (HasEscortState(SmartEscortState.Escorting | SmartEscortState.Paused | SmartEscortState.Returning))
EndPath(true);
}
_isCharmed = apply;
_isCharmed = charmed;
if (!apply && !me.IsInEvadeMode())
if (!charmed && !me.IsInEvadeMode())
{
if (_repeatWaypointPath)
StartPath(_run, GetScript().GetPathId(), true);
@@ -736,7 +737,7 @@ namespace Game.AI
AttackStart(charmer);
}
GetScript().ProcessEventsFor(SmartEvents.Charmed, null, 0, 0, apply);
GetScript().ProcessEventsFor(SmartEvents.Charmed, null, 0, 0, charmed);
}
public override void DoAction(int param)
+1 -1
View File
@@ -958,7 +958,7 @@ namespace Game.AI
}
case SmartActions.SetInCombatWithZone:
{
if (_me != null && _me.IsAIEnabled)
if (_me != null && _me.IsAIEnabled())
{
_me.GetAI().DoZoneInCombat();
Log.outDebug(LogFilter.ScriptsAi, $"SmartScript.ProcessAction: SMART_ACTION_SET_IN_COMBAT_WITH_ZONE: Creature: {_me.GetGUID()}");
+1 -1
View File
@@ -86,7 +86,7 @@ namespace Game.Chat
return false;
Creature target = handler.GetSelectedCreature();
if (!target || !target.IsAIEnabled || target.GetAI() == null)
if (!target || !target.IsAIEnabled())
return false;
string fill_str = args.NextString();
+1 -1
View File
@@ -78,7 +78,7 @@ namespace Game.Chat
return false;
}
if (!creatureTarget.IsAIEnabled)
if (!creatureTarget.IsAIEnabled())
{
handler.SendSysMessage(CypherStrings.CreatureNotAiEnabled);
return false;
+26 -10
View File
@@ -16,6 +16,7 @@
*/
using Framework.Constants;
using Game.AI;
using Game.Entities;
using System.Collections.Generic;
using System.Linq;
@@ -209,8 +210,11 @@ namespace Game.Combat
pair.Value.Suppress(_owner);
if (UpdateOwnerCombatState())
if (_owner.IsAIEnabled)
_owner.GetAI().JustExitedCombat();
{
UnitAI ownerAI = _owner.GetAI();
if (ownerAI != null)
ownerAI.JustExitedCombat();
}
}
public void EndAllPvECombat()
@@ -230,8 +234,9 @@ namespace Game.Combat
public static void NotifyAICombat(Unit me, Unit other)
{
if (!me.IsAIEnabled)
if (!me.IsAIEnabled())
return;
me.GetAI().JustEnteredCombat(other);
Creature cMe = me.ToCreature();
@@ -334,10 +339,18 @@ namespace Game.Combat
bool needSecondAI = second.GetCombatManager().UpdateOwnerCombatState();
// ...and if that happened, also notify the AI of it...
if (needFirstAI && first.IsAIEnabled)
first.GetAI().JustExitedCombat();
if (needSecondAI && second.IsAIEnabled)
second.GetAI().JustExitedCombat();
if (needFirstAI)
{
UnitAI firstAI = first.GetAI();
if (firstAI != null)
firstAI.JustExitedCombat();
}
if (needSecondAI)
{
UnitAI secondAI = second.GetAI();
if (secondAI != null)
secondAI.JustExitedCombat();
}
}
public Unit GetOther(Unit me) { return (first == me) ? second : first; }
@@ -383,12 +396,15 @@ namespace Game.Combat
CombatManager.NotifyAICombat(second, first);
}
void SuppressFor(Unit who)
public void SuppressFor(Unit who)
{
Suppress(who);
if (who.GetCombatManager().UpdateOwnerCombatState())
if (who.IsAIEnabled)
who.GetAI().JustExitedCombat();
{
UnitAI ai = who.GetAI();
if (ai != null)
ai.JustExitedCombat();
}
}
// suppressed combat refs do not generate a combat state for one side of the relation
+6 -4
View File
@@ -16,6 +16,7 @@
*/
using Framework.Constants;
using Game.AI;
using Game.Entities;
using Game.Networking.Packets;
using Game.Spells;
@@ -198,7 +199,7 @@ namespace Game.Combat
static void SaveCreatureHomePositionIfNeed(Creature c)
{
MovementGeneratorType movetype = c.GetMotionMaster().GetCurrentMovementGeneratorType();
if (movetype == MovementGeneratorType.Waypoint || movetype == MovementGeneratorType.Point || (c.IsAIEnabled && c.GetAI().IsEscorted()))
if (movetype == MovementGeneratorType.Waypoint || movetype == MovementGeneratorType.Point || (c.IsAIEnabled() && c.GetAI().IsEscorted()))
c.SetHomePosition(c.GetPosition());
}
@@ -311,8 +312,9 @@ namespace Game.Combat
Creature cOwner = _owner.ToCreature();
Cypher.Assert(cOwner != null); // if we got here the owner can have a threat list, and must be a creature!
SaveCreatureHomePositionIfNeed(cOwner);
if (cOwner.IsAIEnabled)
cOwner.GetAI().JustEngagedWith(target);
CreatureAI ownerAI = cOwner.GetAI();
if (ownerAI != null)
ownerAI.JustEngagedWith(target);
}
}
@@ -835,7 +837,7 @@ namespace Game.Combat
if (!FlagsAllowFighting(_owner, _victim) || !FlagsAllowFighting(_victim, _owner))
return OnlineState.Offline;
if (_owner.IsAIEnabled && !_owner.GetAI().CanAIAttack(_victim))
if (_owner.IsAIEnabled() && !_owner.GetAI().CanAIAttack(_victim))
return OnlineState.Offline;
// next, check suppression (immunity to chosen melee attack school)
+28 -51
View File
@@ -58,13 +58,6 @@ namespace Game.Entities
_currentWaypointNodeInfo = new();
}
public override void Dispose()
{
i_AI = null;
base.Dispose();
}
public override void AddToWorld()
{
// Register the creature for guid lookup
@@ -170,8 +163,9 @@ namespace Game.Entities
//DestroyForNearbyPlayers(); // old UpdateObjectVisibility()
loot.Clear();
uint respawnDelay = m_respawnDelay;
if (IsAIEnabled)
GetAI().CorpseRemoved(respawnDelay);
CreatureAI ai = GetAI();
if (ai != null)
ai.CorpseRemoved(respawnDelay);
if (destroyForNearbyPlayers)
DestroyForNearbyPlayers();
@@ -429,7 +423,7 @@ namespace Game.Entities
public override void Update(uint diff)
{
if (IsAIEnabled && triggerJustAppeared && m_deathState != DeathState.Dead)
if (IsAIEnabled() && triggerJustAppeared && m_deathState != DeathState.Dead)
{
if (m_respawnCompatibilityMode && VehicleKit != null)
VehicleKit.Reset();
@@ -541,24 +535,8 @@ namespace Game.Entities
m_shouldReacquireTarget = false;
}
// if creature is charmed, switch to charmed AI (and back)
if (NeedChangeAI)
{
UpdateCharmAI();
NeedChangeAI = false;
IsAIEnabled = true;
if (!IsInEvadeMode() && !LastCharmerGUID.IsEmpty())
{
Unit charmer = Global.ObjAccessor.GetUnit(this, LastCharmerGUID);
if (charmer)
EngageWithTarget(charmer);
}
LastCharmerGUID.Clear();
}
// periodic check to see if the creature has passed an evade boundary
if (IsAIEnabled && !IsInEvadeMode() && IsEngaged())
if (IsAIEnabled() && !IsInEvadeMode() && IsEngaged())
{
if (diff >= m_boundaryCheckTime)
{
@@ -593,13 +571,10 @@ namespace Game.Entities
}
}
if (!IsInEvadeMode() && IsAIEnabled)
{
// do not allow the AI to be changed during update
m_AI_locked = true;
i_AI.UpdateAI(diff);
base.AIUpdateTick(diff);
m_AI_locked = false;
}
if (!IsAlive())
break;
@@ -633,8 +608,11 @@ namespace Game.Entities
{
m_cannotReachTimer += diff;
if (m_cannotReachTimer >= SharedConst.CreatureNoPathEvadeTime)
if (IsAIEnabled)
GetAI().EnterEvadeMode(EvadeReason.NoPath);
{
CreatureAI ai = GetAI();
if (ai != null)
ai.EnterEvadeMode(EvadeReason.NoPath);
}
}
break;
}
@@ -747,19 +725,15 @@ namespace Game.Entities
}
}
bool AIDestory()
bool DestoryAI()
{
if (m_AI_locked)
{
Log.outDebug(LogFilter.Scripts, "AIM_Destroy: failed to destroy, locked.");
Log.outDebug(LogFilter.Scripts, "DestroyAI: failed to destroy, locked.");
return false;
}
Cypher.Assert(i_disabledAI == null, "The disabled AI wasn't cleared!");
i_AI = null;
IsAIEnabled = false;
SetAI(null);
return true;
}
@@ -772,14 +746,15 @@ namespace Game.Entities
return false;
}
AIDestory();
if (ai == null)
ai = AISelector.SelectAI(this);
SetAI(ai);
InitializeMovementAI();
i_AI = ai ?? AISelector.SelectAI(this);
IsAIEnabled = true;
i_AI.InitializeAI();
// Initialize vehicle
if (GetVehicleKit() != null)
GetVehicleKit().Reset();
@@ -1108,10 +1083,11 @@ namespace Game.Entities
public bool IsEscortNPC(bool onlyIfActive = true)
{
if (!IsAIEnabled)
return false;
CreatureAI ai = GetAI();
if (ai != null)
return ai.IsEscortNPC(onlyIfActive);
return GetAI().IsEscortNPC(onlyIfActive);
return false;
}
public override bool IsMovementPreventedByCasting()
@@ -1695,7 +1671,7 @@ namespace Game.Entities
public override bool CanAlwaysSee(WorldObject obj)
{
if (IsAIEnabled && GetAI<CreatureAI>().CanSeeAlways(obj))
if (IsAIEnabled() && GetAI<CreatureAI>().CanSeeAlways(obj))
return true;
return false;
@@ -1936,8 +1912,9 @@ namespace Game.Entities
//Re-initialize reactstate that could be altered by movementgenerators
InitializeReactState();
if (IsAIEnabled) // reset the AI to be sure no dirty or uninitialized values will be used till next tick
GetAI().Reset();
UnitAI ai = GetAI();
if (ai != null) // reset the AI to be sure no dirty or uninitialized values will be used till next tick
ai.Reset();
triggerJustAppeared = true;
@@ -2299,7 +2276,7 @@ namespace Game.Entities
if (!victim.IsInAccessiblePlaceFor(this))
return false;
if (IsAIEnabled && !GetAI().CanAIAttack(victim))
if (IsAIEnabled() && !GetAI().CanAIAttack(victim))
return false;
// we cannot attack in evade mode
+2 -8
View File
@@ -390,14 +390,7 @@ namespace Game.Entities
aura.SetDuration(aura.GetSpellInfo().GetMaxDuration());
}
if (IsAIEnabled && GetAI() != null)
GetAI().UpdateAI(diff);
else if (NeedChangeAI)
{
UpdateCharmAI();
NeedChangeAI = false;
IsAIEnabled = GetAI() != null;
}
AIUpdateTick(diff);
// Update items that have just a limited lifetime
if (now > m_Last_tick)
@@ -5175,6 +5168,7 @@ namespace Game.Entities
if (guildId != 0)
{
SetUpdateFieldValue(m_values.ModifyValue(m_unitData).ModifyValue(m_unitData.GuildGUID), ObjectGuid.Create(HighGuid.Guild, guildId));
SetUpdateFieldValue(m_values.ModifyValue(m_activePlayerData).ModifyValue(m_activePlayerData.GuildClubMemberID), GetGUID().GetCounter());
AddPlayerFlag(PlayerFlags.GuildLevelEnabled);
}
else
+3 -3
View File
@@ -207,9 +207,9 @@ namespace Game.Entities
Unit owner = GetSummoner();
if (owner != null)
{
if (owner.IsTypeId(TypeId.Unit) && owner.ToCreature().IsAIEnabled)
if (owner.IsTypeId(TypeId.Unit) && owner.ToCreature().IsAIEnabled())
owner.ToCreature().GetAI().JustSummoned(this);
if (IsAIEnabled)
if (IsAIEnabled())
GetAI().IsSummonedBy(owner);
}
}
@@ -243,7 +243,7 @@ namespace Game.Entities
}
Unit owner = GetSummoner();
if (owner != null && owner.IsTypeId(TypeId.Unit) && owner.ToCreature().IsAIEnabled)
if (owner != null && owner.IsTypeId(TypeId.Unit) && owner.ToCreature().IsAIEnabled())
owner.ToCreature().GetAI().SummonedCreatureDespawn(this);
AddObjectToRemoveList();
+12 -8
View File
@@ -16,10 +16,10 @@
*/
using Framework.Constants;
using Game.AI;
using Game.BattleFields;
using Game.BattleGrounds;
using Game.Combat;
using Game.DataStorage;
using Game.Loots;
using Game.Maps;
using Game.Networking.Packets;
@@ -354,8 +354,11 @@ namespace Game.Entities
{
Creature cControlled = controlled.ToCreature();
if (cControlled != null)
if (cControlled.IsAIEnabled)
cControlled.GetAI().OwnerAttacked(victim);
{
CreatureAI controlledAI = cControlled.GetAI();
if (controlledAI != null)
controlledAI.OwnerAttacked(victim);
}
}
}
return true;
@@ -880,7 +883,7 @@ namespace Game.Entities
plrVictim.SendDurabilityLoss(plrVictim, loss);
}
// Call KilledUnit for creatures
if (attacker != null && attacker.IsCreature() && attacker.IsAIEnabled)
if (attacker != null && attacker.IsCreature() && attacker.IsAIEnabled())
attacker.ToCreature().GetAI().KilledUnit(victim);
// last damage from non duel opponent or opponent controlled creature
@@ -907,19 +910,20 @@ namespace Game.Entities
}
// Call KilledUnit for creatures, this needs to be called after the lootable flag is set
if (attacker != null && attacker.IsCreature() && attacker.IsAIEnabled)
if (attacker != null && attacker.IsCreature() && attacker.IsAIEnabled())
attacker.ToCreature().GetAI().KilledUnit(victim);
// Call creature just died function
if (creature.IsAIEnabled)
creature.GetAI().JustDied(attacker);
CreatureAI ai = creature.GetAI();
if (ai != null)
ai.JustDied(attacker);
TempSummon summon = creature.ToTempSummon();
if (summon != null)
{
Unit summoner = summon.GetSummoner();
if (summoner != null)
if (summoner.IsTypeId(TypeId.Unit) && summoner.IsAIEnabled)
if (summoner.IsTypeId(TypeId.Unit) && summoner.IsAIEnabled())
summoner.ToCreature().GetAI().SummonedCreatureDies(creature, attacker);
}
-2
View File
@@ -36,8 +36,6 @@ namespace Game.Entities
//AI
protected UnitAI i_AI;
protected UnitAI i_disabledAI;
public bool IsAIEnabled { get; set; }
public bool NeedChangeAI { get; set; }
//Movement
protected float[] m_speed_rate = new float[(int)UnitMoveType.Max];
+40 -84
View File
@@ -47,84 +47,49 @@ namespace Game.Entities
}
public void UpdateCharmAI()
{
switch (GetTypeId())
{
case TypeId.Unit:
if (i_disabledAI != null) // disabled AI must be primary AI
{
if (!IsCharmed())
{
i_AI = i_disabledAI;
i_disabledAI = null;
if (IsTypeId(TypeId.Unit))
ToCreature().GetAI().OnCharmed(false);
}
}
else
{
if (IsCharmed())
{
i_disabledAI = i_AI;
if (IsPossessed() || IsVehicle())
i_AI = new PossessedAI(ToCreature());
else
i_AI = new PetAI(ToCreature());
}
}
break;
case TypeId.Player:
{
if (IsCharmed()) // if we are currently being charmed, then we should apply charm AI
{
i_disabledAI = i_AI;
UnitAI newAI = null;
// first, we check if the creature's own AI specifies an override playerai for its owned players
if (IsPlayer())
{
Unit charmer = GetCharmer();
if (charmer)
if (charmer != null)
{
// first, we check if the creature's own AI specifies an override playerai for its owned players
Creature creatureCharmer = charmer.ToCreature();
if (creatureCharmer)
if (creatureCharmer != null)
{
PlayerAI charmAI = creatureCharmer.IsAIEnabled ? creatureCharmer.GetAI().GetAIForCharmedPlayer(ToPlayer()) : null;
if (charmAI != null)
newAI = charmAI;
CreatureAI charmerAI = creatureCharmer.GetAI();
if (charmerAI != null)
newAI = charmerAI.GetAIForCharmedPlayer(ToPlayer());
}
else
{
Log.outError(LogFilter.Misc, "Attempt to assign charm AI to player {0} who is charmed by non-creature {1}.", GetGUID().ToString(), GetCharmerGUID().ToString());
}
Log.outError(LogFilter.Misc, $"Attempt to assign charm AI to player {GetGUID()} who is charmed by non-creature {GetCharmerGUID()}.");
}
if (newAI == null) // otherwise, we default to the generic one
newAI = new SimpleCharmedPlayerAI(ToPlayer());
i_AI = newAI;
newAI.OnCharmed(true);
}
else
{
if (i_AI != null)
{
// we allow the charmed PlayerAI to clean up
i_AI.OnCharmed(false);
}
Cypher.Assert(IsCreature());
if (IsPossessed() || IsVehicle())
newAI = new PossessedAI(ToCreature());
else
{
Log.outError(LogFilter.Misc, "Attempt to remove charm AI from player {0} who doesn't currently have charm AI.", GetGUID().ToString());
}
// and restore our previous PlayerAI (if we had one)
i_AI = i_disabledAI;
i_disabledAI = null;
// IsAIEnabled gets handled in the caller
}
break;
}
default:
Log.outError(LogFilter.Misc, "Attempt to update charm AI for unit {0}, which is neither player nor creature.", GetGUID().ToString());
break;
newAI = new PetAI(ToCreature());
}
Cypher.Assert(newAI != null);
i_AI = newAI;
newAI.OnCharmed(true);
AIUpdateTick(0, true);
}
else
{
RestoreDisabledAI();
if (i_AI != null)
i_AI.OnCharmed(true);
}
}
public void SetMinion(Minion minion, bool apply)
@@ -365,7 +330,12 @@ namespace Game.Entities
StopMoving();
ToCreature().GetAI().OnCharmed(true);
// AI will schedule its own change if appropriate
UnitAI ai = GetAI();
if (ai != null)
ai.OnCharmed(false);
else
ScheduleAIChange();
}
else
{
@@ -378,12 +348,11 @@ namespace Game.Entities
if (charmer.IsTypeId(TypeId.Unit)) // we are charmed by a creature
{
// change AI to charmed AI on next Update tick
NeedChangeAI = true;
if (IsAIEnabled)
{
IsAIEnabled = false;
player.GetAI().OnCharmed(true);
}
UnitAI ai = GetAI();
if (ai != null)
ai.OnCharmed(false);
else
player.ScheduleAIChange();
}
player.SetClientControl(this, false);
@@ -482,17 +451,9 @@ namespace Game.Entities
///@todo Handle SLOT_IDLE motion resume
GetMotionMaster().InitializeDefault();
Creature creature = ToCreature();
if (creature)
{
// Creature will restore its old AI on next update
if (creature.GetAI() != null)
creature.GetAI().OnCharmed(false);
// Vehicle should not attack its passenger after he exists the seat
if (type != CharmType.Vehicle)
LastCharmerGUID = charmer ? charmer.GetGUID() : ObjectGuid.Empty;
}
LastCharmerGUID = charmer.GetGUID();
// If charmer still exists
if (!charmer)
@@ -539,17 +500,12 @@ namespace Game.Entities
}
}
Player player = ToPlayer();
if (player)
{
if (charmer.IsTypeId(TypeId.Unit)) // charmed by a creature, this means we had PlayerAI
{
NeedChangeAI = true;
IsAIEnabled = false;
}
if (!IsPlayer() || charmer.IsCreature())
GetAI().OnCharmed(false); // AI will potentially schedule a charm ai update
Player player = ToPlayer();
if (player != null)
player.SetClientControl(this, true);
}
// a guardian should always have charminfo
if (playerCharmer && this != charmer.GetFirstControlled())
+9 -9
View File
@@ -17,6 +17,7 @@
using Framework.Constants;
using Framework.Dynamic;
using Game.AI;
using Game.BattleGrounds;
using Game.Networking.Packets;
using Game.Spells;
@@ -1801,14 +1802,13 @@ namespace Game.Entities
Unit victim = healInfo.GetTarget();
uint addhealth = healInfo.GetHeal();
if (healer)
{
if (victim.IsAIEnabled)
victim.GetAI().HealReceived(healer, addhealth);
UnitAI victimAI = victim.GetAI();
if (victimAI != null)
victimAI.HealReceived(healer, addhealth);
if (healer.IsAIEnabled)
healer.GetAI().HealDone(victim, addhealth);
}
UnitAI healerAI = healer != null ? healer.GetAI() : null;
if (healerAI != null)
healerAI.HealDone(victim, addhealth);
if (addhealth != 0)
gain = (int)victim.ModifyHealth(addhealth);
@@ -2424,7 +2424,7 @@ namespace Game.Entities
spell.SetReferencedFromCurrent(false);
}
if (IsCreature() && IsAIEnabled)
if (IsCreature() && IsAIEnabled())
ToCreature().GetAI().OnSpellCastInterrupt(spell.GetSpellInfo());
}
}
@@ -2595,7 +2595,7 @@ namespace Game.Entities
}
Creature creature = ToCreature();
if (creature && creature.IsAIEnabled)
if (creature && creature.IsAIEnabled())
creature.GetAI().OnSpellClick(clicker, ref spellClickHandled);
}
+67 -15
View File
@@ -172,6 +172,9 @@ namespace Game.Entities
UpdateSplineMovement(diff);
GetMotionMaster().Update(diff);
if (i_AI == null && (!IsPlayer() || IsCharmed()))
UpdateCharmAI();
}
void _UpdateSpells(uint diff)
@@ -438,8 +441,9 @@ namespace Game.Entities
if (IsInWorld)
{
m_duringRemoveFromWorld = true;
if (IsAIEnabled)
GetAI().LeavingWorld();
UnitAI ai = GetAI();
if (ai != null)
ai.LeavingWorld();
if (IsVehicle())
RemoveVehicleKit(true);
@@ -537,14 +541,14 @@ namespace Game.Entities
public void _RegisterDynObject(DynamicObject dynObj)
{
m_dynObj.Add(dynObj);
if (IsTypeId(TypeId.Unit) && IsAIEnabled)
if (IsTypeId(TypeId.Unit) && IsAIEnabled())
ToCreature().GetAI().JustRegisteredDynObject(dynObj);
}
public void _UnregisterDynObject(DynamicObject dynObj)
{
m_dynObj.Remove(dynObj);
if (IsTypeId(TypeId.Unit) && IsAIEnabled)
if (IsTypeId(TypeId.Unit) && IsAIEnabled())
ToCreature().GetAI().JustUnregisteredDynObject(dynObj);
}
@@ -611,7 +615,7 @@ namespace Game.Entities
GetSpellHistory().StartCooldown(createBySpell, 0, null, true);
}
if (IsTypeId(TypeId.Unit) && ToCreature().IsAIEnabled)
if (IsTypeId(TypeId.Unit) && ToCreature().IsAIEnabled())
ToCreature().GetAI().JustSummonedGameobject(gameObj);
}
@@ -646,7 +650,7 @@ namespace Game.Entities
m_gameObj.Remove(gameObj);
if (IsTypeId(TypeId.Unit) && ToCreature().IsAIEnabled)
if (IsTypeId(TypeId.Unit) && ToCreature().IsAIEnabled())
ToCreature().GetAI().SummonedGameobjectDespawn(gameObj);
if (del)
@@ -694,14 +698,14 @@ namespace Game.Entities
public void _RegisterAreaTrigger(AreaTrigger areaTrigger)
{
m_areaTrigger.Add(areaTrigger);
if (IsTypeId(TypeId.Unit) && IsAIEnabled)
if (IsTypeId(TypeId.Unit) && IsAIEnabled())
ToCreature().GetAI().JustRegisteredAreaTrigger(areaTrigger);
}
public void _UnregisterAreaTrigger(AreaTrigger areaTrigger)
{
m_areaTrigger.Remove(areaTrigger);
if (IsTypeId(TypeId.Unit) && IsAIEnabled)
if (IsTypeId(TypeId.Unit) && IsAIEnabled())
ToCreature().GetAI().JustUnregisteredAreaTrigger(areaTrigger);
}
@@ -1160,8 +1164,51 @@ namespace Game.Entities
return m_vehicle != null && m_vehicle == vehicle.GetVehicleKit();
}
public bool IsAIEnabled() { return i_AI != null; }
public virtual UnitAI GetAI() { return i_AI; }
public void SetAI(UnitAI newAI) { i_AI = newAI; }
public void AIUpdateTick(uint diff, bool force = false)
{
if (diff == 0) // some places call with diff = 0, which does nothing (for now), see PR #22296
return;
UnitAI ai = GetAI();
if (ai != null)
ai.UpdateAI(diff);
}
public void SetAI(UnitAI newAI)
{
if (i_AI != null)
AIUpdateTick(0, true); // old AI gets a final tick if enabled
i_AI = newAI;
AIUpdateTick(0, true); // new AI gets its initial tick
}
public void ScheduleAIChange()
{
bool charmed = IsCharmed();
// if charm is applied, we can't have disabled AI already, and vice versa
if (charmed)
Cypher.Assert(i_disabledAI == null, "Attempt to schedule charm AI change on unit that already has disabled AI");
else if (!IsPlayer())
Cypher.Assert(i_disabledAI != null, "Attempt to schedule charm ID change on unit that doesn't have disabled AI");
if (charmed)
i_disabledAI = i_AI;
else
i_AI = null;
}
void RestoreDisabledAI()
{
Cypher.Assert(IsPlayer() || i_disabledAI != null, "Attempt to restore disabled AI on creature without disabled AI");
i_AI = i_disabledAI;
AIUpdateTick(0, true);
}
public bool IsPossessing()
{
@@ -2270,11 +2317,13 @@ namespace Game.Entities
public static uint DealDamage(Unit attacker, Unit victim, uint damage, CleanDamage cleanDamage = null, DamageEffectType damagetype = DamageEffectType.Direct, SpellSchoolMask damageSchoolMask = SpellSchoolMask.Normal, SpellInfo spellProto = null, bool durabilityLoss = true)
{
if (victim.IsAIEnabled)
victim.GetAI().DamageTaken(attacker, ref damage);
UnitAI victimAI = victim.GetAI();
if (victimAI != null)
victimAI.DamageTaken(attacker, ref damage);
if (attacker != null && attacker.IsAIEnabled)
attacker.GetAI().DamageDealt(victim, ref damage, damagetype);
UnitAI attackerAI = attacker ? attacker.GetAI() : null;
if (attackerAI != null)
attackerAI.DamageDealt(victim, ref damage, damagetype);
// Hook for OnDamage Event
Global.ScriptMgr.OnDamage(attacker, victim, ref damage);
@@ -2288,8 +2337,11 @@ namespace Game.Entities
{
Creature cControlled = controlled.ToCreature();
if (cControlled != null)
if (cControlled.IsAIEnabled)
cControlled.GetAI().OwnerAttackedBy(attacker);
{
CreatureAI controlledAI = cControlled.GetAI();
if (controlledAI != null)
controlledAI.OwnerAttackedBy(attacker);
}
}
}
+5 -3
View File
@@ -17,6 +17,7 @@
using Framework.Constants;
using Framework.Dynamic;
using Game.AI;
using Game.BattleGrounds;
using Game.DataStorage;
using Game.Movement;
@@ -361,7 +362,7 @@ namespace Game.Entities
if (unit.IsFlying())
_me.CastSpell(unit, SharedConst.VehicleSpellParachute, true);
if (_me.IsTypeId(TypeId.Unit) && _me.ToCreature().IsAIEnabled)
if (_me.IsTypeId(TypeId.Unit) && _me.ToCreature().IsAIEnabled())
_me.ToCreature().GetAI().PassengerBoarded(unit, seat.Key, false);
if (GetBase().IsTypeId(TypeId.Unit))
@@ -646,8 +647,9 @@ namespace Game.Entities
Creature creature = Target.GetBase().ToCreature();
if (creature != null)
{
if (creature.IsAIEnabled)
creature.GetAI().PassengerBoarded(Passenger, Seat.Key, true);
CreatureAI ai = creature.GetAI();
if (ai != null)
ai.PassengerBoarded(Passenger, Seat.Key, true);
Global.ScriptMgr.OnAddPassenger(Target, Passenger, Seat.Key);
+1 -1
View File
@@ -1749,7 +1749,7 @@ namespace Game
for (var i = 0; i < objs.Count; ++i)
{
Creature creature = objs[i];
if (creature.IsInWorld && creature.IsAIEnabled)
if (creature.IsInWorld && creature.IsAIEnabled())
creature.GetAI().OnGameEvent(_activate, _eventId);
}
}
+5 -5
View File
@@ -190,7 +190,7 @@ namespace Game
if (pet.GetVictim())
pet.AttackStop();
if (!pet.IsTypeId(TypeId.Player) && pet.ToCreature().IsAIEnabled)
if (!pet.IsTypeId(TypeId.Player) && pet.ToCreature().IsAIEnabled())
{
charmInfo.SetIsCommandAttack(true);
charmInfo.SetIsAtStay(false);
@@ -368,14 +368,14 @@ namespace Game
if (pet.GetVictim() != unit_target)
{
pet.GetMotionMaster().Clear();
if (pet.ToCreature().IsAIEnabled)
CreatureAI ai = pet.ToCreature().GetAI();
if (ai != null)
{
CreatureAI AI = pet.ToCreature().GetAI();
PetAI petAI = (PetAI)AI;
PetAI petAI = (PetAI)ai;
if (petAI != null)
petAI._AttackStart(unit_target); // force victim switch
else
AI.AttackStart(unit_target);
ai.AttackStart(unit_target);
}
}
}
+3 -4
View File
@@ -45,11 +45,11 @@ namespace Game.Maps
if (!c.HasUnitState(UnitState.Sightless))
{
if (c.IsAIEnabled && c.CanSeeOrDetect(u, false, true))
if (c.IsAIEnabled() && c.CanSeeOrDetect(u, false, true))
c.GetAI().MoveInLineOfSight_Safe(u);
else
{
if (u.IsTypeId(TypeId.Player) && u.HasStealthAura() && c.IsAIEnabled && c.CanSeeOrDetect(u, false, true, true))
if (u.IsTypeId(TypeId.Player) && u.HasStealthAura() && c.IsAIEnabled() && c.CanSeeOrDetect(u, false, true, true))
c.GetAI().TriggerAlert(u);
}
}
@@ -939,8 +939,7 @@ namespace Game.Maps
if (!u.IsWithinLOSInMap(i_enemy))
return;
if (u.GetAI() != null)
u.GetAI().AttackStart(i_enemy);
u.EngageWithTarget(i_enemy);
}
Unit i_funit;
+1 -1
View File
@@ -217,7 +217,7 @@ namespace Game.Maps
{
creature.CombatStop();
creature.GetThreatManager().ClearAllThreat();
if (creature.IsAIEnabled)
if (creature.IsAIEnabled())
creature.GetAI().EnterEvadeMode();
}
}
@@ -255,9 +255,9 @@ namespace Game.Movement
if (!owner.IsCreature())
return;
Creature creatureOwner = owner.ToCreature();
if (creatureOwner.IsAIEnabled && creatureOwner.GetAI() != null)
creatureOwner.GetAI().MovementInform(MovementGeneratorType.Chase, (uint)target.GetGUID().GetCounter());
CreatureAI ai = owner.ToCreature().GetAI();
if (ai != null)
ai.MovementInform(MovementGeneratorType.Chase, (uint)target.GetGUID().GetCounter());
}
public Unit GetTarget()
@@ -217,9 +217,9 @@ namespace Game.Movement
if (!owner.IsCreature())
return;
Creature creatureOwner = owner.ToCreature();
if (creatureOwner.IsAIEnabled && creatureOwner.GetAI() != null)
creatureOwner.GetAI().MovementInform(MovementGeneratorType.Follow, (uint)target.GetGUID().GetCounter());
CreatureAI ai = owner.ToCreature().GetAI();
if (ai != null)
ai.MovementInform(MovementGeneratorType.Follow, (uint)target.GetGUID().GetCounter());
}
}
}
@@ -17,6 +17,7 @@
using Framework.Constants;
using Framework.GameMath;
using Game.AI;
using Game.Entities;
using System;
using System.Collections.Generic;
@@ -164,9 +165,9 @@ namespace Game.Movement
if (movementInform && HasFlag(MovementGeneratorFlags.InformEnabled))
{
Creature ownerCreature = owner.ToCreature();
if (ownerCreature != null && ownerCreature.IsAIEnabled)
ownerCreature.GetAI().MovementInform(MovementGeneratorType.SplineChain, _id);
CreatureAI ai = owner.ToCreature().GetAI();
if (ai != null)
ai.MovementInform(MovementGeneratorType.SplineChain, _id);
}
}
@@ -16,6 +16,7 @@
*/
using Framework.Constants;
using Game.AI;
using Game.Entities;
using System.Collections.Generic;
using System.Linq;
@@ -116,8 +117,9 @@ namespace Game.Movement
_nextMoveTime.Reset(1000);
// inform AI
if (owner.IsAIEnabled)
owner.GetAI().WaypointPathStarted(_path.id);
CreatureAI ai = owner.GetAI();
if (ai != null)
ai.WaypointPathStarted(_path.id);
}
public override void DoReset(Creature owner)
@@ -230,8 +232,9 @@ namespace Game.Movement
void MovementInform(Creature owner)
{
if (owner.IsAIEnabled)
owner.GetAI().MovementInform(MovementGeneratorType.Waypoint, (uint)_currentNode);
CreatureAI ai = owner.GetAI();
if (ai != null)
ai.MovementInform(MovementGeneratorType.Waypoint, (uint)_currentNode);
}
void OnArrived(Creature owner)
@@ -255,10 +258,11 @@ namespace Game.Movement
}
// inform AI
if (owner.IsAIEnabled)
CreatureAI ai = owner.GetAI();
if (ai != null)
{
owner.GetAI().MovementInform(MovementGeneratorType.Waypoint, (uint)_currentNode);
owner.GetAI().WaypointReached(waypoint.id, _path.id);
ai.MovementInform(MovementGeneratorType.Waypoint, (uint)_currentNode);
ai.WaypointReached(waypoint.id, _path.id);
}
owner.UpdateCurrentWaypointInfo(waypoint.id, _path.id);
@@ -284,8 +288,9 @@ namespace Game.Movement
{
Cypher.Assert(_currentNode < _path.nodes.Count, $"WaypointMovementGenerator.StartMove: tried to reference a node id ({_currentNode}) which is not included in path ({_path.id})");
// inform AI
if (owner.IsAIEnabled)
owner.GetAI().WaypointStarted(_path.nodes[_currentNode].id, _path.id);
CreatureAI ai = owner.GetAI();
if (ai != null)
ai.WaypointStarted(_path.nodes[_currentNode].id, _path.id);
}
else
{
@@ -314,8 +319,9 @@ namespace Game.Movement
owner.UpdateCurrentWaypointInfo(0, 0);
// inform AI
if (owner.IsAIEnabled)
owner.GetAI().WaypointPathEnded(currentWaypoint.id, _path.id);
CreatureAI ai = owner.GetAI();
if (ai != null)
ai.WaypointPathEnded(currentWaypoint.id, _path.id);
return;
}
}
@@ -324,8 +330,9 @@ namespace Game.Movement
AddFlag(MovementGeneratorFlags.Initialized);
// inform AI
if (owner.IsAIEnabled)
owner.GetAI().WaypointStarted(_path.nodes[_currentNode].id, _path.id);
CreatureAI ai = owner.GetAI();
if (ai != null)
ai.WaypointStarted(_path.nodes[_currentNode].id, _path.id);
}
Cypher.Assert(_currentNode < _path.nodes.Count, $"WaypointMovementGenerator.StartMove: tried to reference a node id ({_currentNode}) which is not included in path ({_path.id})");
+15 -9
View File
@@ -17,6 +17,7 @@
using Framework.Constants;
using Framework.Dynamic;
using Game.AI;
using Game.BattleFields;
using Game.BattleGrounds;
using Game.Conditions;
@@ -2507,8 +2508,11 @@ namespace Game.Spells
{
Creature cControlled = controlled.ToCreature();
if (cControlled != null)
if (cControlled.IsAIEnabled)
cControlled.GetAI().OwnerAttacked(target);
{
CreatureAI controlledAI = cControlled.GetAI();
if (controlledAI != null)
controlledAI.OwnerAttacked(target);
}
}
}
}
@@ -2778,7 +2782,7 @@ namespace Game.Spells
// Call CreatureAI hook OnSuccessfulSpellCast
Creature caster = m_originalCaster.ToCreature();
if (caster)
if (caster.IsAIEnabled)
if (caster.IsAIEnabled())
caster.GetAI().OnSuccessfulSpellCast(GetSpellInfo());
}
@@ -8005,18 +8009,20 @@ namespace Game.Spells
if (_spellHitTarget)
{
//AI functions
if (_spellHitTarget.IsCreature())
Creature cHitTarget = _spellHitTarget.ToCreature();
if (cHitTarget != null)
{
if (_spellHitTarget.ToCreature().IsAIEnabled)
CreatureAI hitTargetAI = cHitTarget.GetAI();
if (hitTargetAI != null)
{
if (spell.GetCaster().IsGameObject())
_spellHitTarget.ToCreature().GetAI().SpellHit(spell.GetCaster().ToGameObject(), spell.m_spellInfo);
hitTargetAI.SpellHit(spell.GetCaster().ToGameObject(), spell.m_spellInfo);
else
_spellHitTarget.ToCreature().GetAI().SpellHit(spell.GetCaster().ToUnit(), spell.m_spellInfo);
hitTargetAI.SpellHit(spell.GetCaster().ToUnit(), spell.m_spellInfo);
}
}
if (spell.GetCaster().IsCreature() && spell.GetCaster().ToCreature().IsAIEnabled)
if (spell.GetCaster().IsCreature() && spell.GetCaster().ToCreature().IsAIEnabled())
spell.GetCaster().ToCreature().GetAI().SpellHitTarget(_spellHitTarget, spell.m_spellInfo);
else if (spell.GetCaster().IsGameObject() && spell.GetCaster().ToGameObject().GetAI() != null)
spell.GetCaster().ToGameObject().GetAI().SpellHitTarget(_spellHitTarget, spell.m_spellInfo);
@@ -8072,7 +8078,7 @@ namespace Game.Spells
go.GetAI().SpellHit(spell.GetCaster().ToUnit(), spell.m_spellInfo);
}
if (spell.GetCaster().IsCreature() && spell.GetCaster().ToCreature().IsAIEnabled)
if (spell.GetCaster().IsCreature() && spell.GetCaster().ToCreature().IsAIEnabled())
spell.GetCaster().ToCreature().GetAI().SpellHitTarget(go, spell.m_spellInfo);
else if (spell.GetCaster().IsGameObject() && spell.GetCaster().ToGameObject().GetAI() != null)
spell.GetCaster().ToGameObject().GetAI().SpellHitTarget(go, spell.m_spellInfo);
+1 -1
View File
@@ -1549,7 +1549,7 @@ namespace Scripts.Spells.Generic
GetUnitOwner().GetAllMinionsByEntry(minionList, CreatureIds.EtherealSoulTrader);
foreach (Creature minion in minionList)
{
if (minion.IsAIEnabled)
if (minion.IsAIEnabled())
{
minion.GetAI().Talk(TextIds.SayStealEssence);
minion.CastSpell(eventInfo.GetProcTarget(), SpellIds.StealEssenceVisual);
+2 -2
View File
@@ -327,8 +327,8 @@ namespace Scripts.Spells.Quest
if (!creatureTarget.IsPet() && creatureTarget.GetEntry() == _originalEntry)
{
creatureTarget.UpdateEntry(_newEntry);
if (_shouldAttack && creatureTarget.IsAIEnabled)
creatureTarget.GetAI().AttackStart(GetCaster());
if (_shouldAttack)
creatureTarget.EngageWithTarget(GetCaster());
if (_despawnTime != 0)
creatureTarget.DespawnOrUnsummon(_despawnTime);