Core/Scripts: Integrate new ActionResultSetter with movement generators and spells

Port From (https://github.com/TrinityCore/TrinityCore/commit/b265c49977235dea5e7e69d7b6fb93f4943bf87a)
This commit is contained in:
Hondacrx
2024-08-05 14:12:00 -04:00
parent 22fbfd8360
commit 708df58712
13 changed files with 170 additions and 58 deletions
@@ -5,6 +5,7 @@ using Framework.Constants;
using Game.AI;
using Game.Entities;
using System;
using System.Threading.Tasks;
namespace Game.Movement
{
@@ -13,15 +14,16 @@ namespace Game.Movement
public const float MIN_QUIET_DISTANCE = 28.0f;
public const float MAX_QUIET_DISTANCE = 43.0f;
public FleeingMovementGenerator(ObjectGuid fright)
public FleeingMovementGenerator(ObjectGuid fleeTargetGUID, TaskCompletionSource<MovementStopReason> scriptResult = null)
{
_fleeTargetGUID = fright;
_fleeTargetGUID = fleeTargetGUID;
_timer = new TimeTracker();
Mode = MovementGeneratorMode.Default;
Priority = MovementGeneratorPriority.Highest;
Flags = MovementGeneratorFlags.InitializationPending;
BaseUnitState = UnitState.Fleeing;
ScriptResult = scriptResult;
}
public override void Initialize(Unit owner)
@@ -90,6 +92,9 @@ namespace Game.Movement
else if (owner.IsPlayer())
owner.StopMoving();
}
if (movementInform)
SetScriptResult(MovementStopReason.Finished);
}
void SetTargetLocation(Unit owner)
@@ -192,7 +197,7 @@ namespace Game.Movement
public class TimedFleeingMovementGenerator : FleeingMovementGenerator
{
public TimedFleeingMovementGenerator(ObjectGuid fright, TimeSpan time) : base(fright)
public TimedFleeingMovementGenerator(ObjectGuid fleeTargetGUID, TimeSpan time, TaskCompletionSource<MovementStopReason> scriptResult = null) : base(fleeTargetGUID, scriptResult)
{
_totalFleeTime = new TimeTracker(time);
}
@@ -228,6 +233,8 @@ namespace Game.Movement
if (movementInform)
{
SetScriptResult(MovementStopReason.Finished);
Creature ownerCreature = owner.ToCreature();
CreatureAI ai = ownerCreature != null ? ownerCreature.GetAI() : null;
if (ai != null)
@@ -9,6 +9,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
namespace Game.Movement
{
@@ -24,13 +25,14 @@ namespace Game.Movement
int _currentNode;
List<TaxiNodeChangeInfo> _pointsForPathSwitch = new(); //! node indexes and costs where TaxiPath changes
public FlightPathMovementGenerator(float? speed)
public FlightPathMovementGenerator(float? speed, TaskCompletionSource<MovementStopReason> scriptResult)
{
_speed = speed;
Mode = MovementGeneratorMode.Default;
Priority = MovementGeneratorPriority.Highest;
Flags = MovementGeneratorFlags.InitializationPending;
BaseUnitState = UnitState.InFlight;
ScriptResult = scriptResult;
}
public override void DoInitialize(Player owner)
@@ -157,6 +159,9 @@ namespace Game.Movement
}
owner.RemovePlayerFlag(PlayerFlags.TaxiBenchmark);
if (movementInform)
SetScriptResult(MovementStopReason.Finished);
}
uint GetPathAtMapEnd()
@@ -5,6 +5,7 @@ using Framework.Constants;
using Game.AI;
using Game.Entities;
using System;
using System.Threading.Tasks;
namespace Game.Movement
{
@@ -23,7 +24,7 @@ namespace Game.Movement
AbstractFollower _abstractFollower;
public FollowMovementGenerator(Unit target, float range, ChaseAngle angle, TimeSpan? duration)
public FollowMovementGenerator(Unit target, float range, ChaseAngle angle, TimeSpan? duration, TaskCompletionSource<MovementStopReason> scriptResult = null)
{
_abstractFollower = new AbstractFollower(target);
_range = range;
@@ -33,6 +34,7 @@ namespace Game.Movement
Priority = MovementGeneratorPriority.Normal;
Flags = MovementGeneratorFlags.InitializationPending;
BaseUnitState = UnitState.Follow;
ScriptResult = scriptResult;
if (duration.HasValue)
_duration = new(duration.Value);
@@ -186,6 +188,8 @@ namespace Game.Movement
{
owner.ClearUnitState(UnitState.FollowMove);
UpdatePetSpeed(owner);
if (movementInform)
SetScriptResult(MovementStopReason.Finished);
}
}
@@ -4,6 +4,7 @@
using Framework.Constants;
using Game.Entities;
using System;
using System.Threading.Tasks;
namespace Game.Movement
{
@@ -38,6 +39,8 @@ namespace Game.Movement
_duration = new(args.Duration.Value);
_durationTracksSpline = false;
}
ScriptResult = args.ScriptResult;
}
public override void Initialize(Unit owner)
@@ -99,6 +102,8 @@ namespace Game.Movement
if (_arrivalSpellId != 0)
owner.CastSpell(Global.ObjAccessor.GetUnit(owner, _arrivalSpellTargetGuid), _arrivalSpellId, true);
SetScriptResult(MovementStopReason.Finished);
Creature creature = owner.ToCreature();
if (creature != null && creature.GetAI() != null)
creature.GetAI().MovementInform(_type, _pointId);
@@ -112,5 +117,6 @@ namespace Game.Movement
public uint? ArrivalSpellId;
public ObjectGuid? ArrivalSpellTarget;
public TimeSpan? Duration;
public TaskCompletionSource<MovementStopReason> ScriptResult;
}
}
@@ -4,6 +4,7 @@
using Framework.Constants;
using Game.Entities;
using System;
using System.Threading.Tasks;
namespace Game.Movement
{
@@ -56,7 +57,7 @@ namespace Game.Movement
float? _totalTurnAngle;
uint _diffSinceLastUpdate;
public RotateMovementGenerator(uint id, RotateDirection direction, TimeSpan? duration, float? turnSpeed, float? totalTurnAngle)
public RotateMovementGenerator(uint id, RotateDirection direction, TimeSpan? duration, float? turnSpeed, float? totalTurnAngle, TaskCompletionSource<MovementStopReason> scriptResult)
{
_id = id;
_direction = direction;
@@ -70,6 +71,7 @@ namespace Game.Movement
Priority = MovementGeneratorPriority.Normal;
Flags = MovementGeneratorFlags.InitializationPending;
BaseUnitState = UnitState.Rotating;
ScriptResult = scriptResult;
}
public override void Initialize(Unit owner)
@@ -142,8 +144,12 @@ namespace Game.Movement
{
AddFlag(MovementGeneratorFlags.Finalized);
if (movementInform && owner.IsCreature())
owner.ToCreature().GetAI().MovementInform(MovementGeneratorType.Rotate, _id);
if (movementInform)
{
SetScriptResult(MovementStopReason.Finished);
if (owner.IsCreature())
owner.ToCreature().GetAI().MovementInform(MovementGeneratorType.Rotate, _id);
}
}
public override MovementGeneratorType GetMovementGeneratorType() { return MovementGeneratorType.Rotate; }
@@ -5,6 +5,7 @@ using Framework.Constants;
using Game.AI;
using Game.Entities;
using System;
using System.Threading.Tasks;
namespace Game.Movement
{
@@ -14,6 +15,7 @@ namespace Game.Movement
public MovementGeneratorPriority Priority;
public MovementGeneratorFlags Flags;
public UnitState BaseUnitState;
public TaskCompletionSource<MovementStopReason> ScriptResult;
// on top first update
public virtual void Initialize(Unit owner) { }
@@ -68,8 +70,17 @@ namespace Game.Movement
{
return $"Mode: {Mode} Priority: {Priority} Flags: {Flags} BaseUniteState: {BaseUnitState}";
}
public void SetScriptResult(MovementStopReason reason)
{
if (ScriptResult != null)
{
ScriptResult.SetResult(reason);
ScriptResult = null;
}
}
}
public abstract class MovementGeneratorMedium<T> : MovementGenerator where T : Unit
{
public override void Initialize(Unit owner)
@@ -4,6 +4,7 @@
using Framework.Constants;
using Game.Entities;
using System;
using System.Threading.Tasks;
namespace Game.Movement
{
@@ -21,7 +22,7 @@ namespace Game.Movement
float? _closeEnoughDistance;
public PointMovementGenerator(uint id, float x, float y, float z, bool generatePath, float? speed = null, float? finalOrient = null, Unit faceTarget = null, SpellEffectExtraData spellEffectExtraData = null,
MovementWalkRunSpeedSelectionMode speedSelectionMode = MovementWalkRunSpeedSelectionMode.Default, float? closeEnoughDistance = null)
MovementWalkRunSpeedSelectionMode speedSelectionMode = MovementWalkRunSpeedSelectionMode.Default, float? closeEnoughDistance = null, TaskCompletionSource<MovementStopReason> scriptResult = null)
{
_movementId = id;
_destination = new Position(x, y, z);
@@ -37,6 +38,7 @@ namespace Game.Movement
Priority = MovementGeneratorPriority.Normal;
Flags = MovementGeneratorFlags.InitializationPending;
BaseUnitState = UnitState.Roaming;
ScriptResult = scriptResult;
}
public override void Initialize(Unit owner)
@@ -192,6 +194,8 @@ namespace Game.Movement
public void MovementInform(Unit owner)
{
SetScriptResult(MovementStopReason.Finished);
// deliver EVENT_CHARGE to scripts, EVENT_CHARGE_PREPATH is just internal implementation detail of this movement generator
uint movementId = _movementId == EventId.ChargePrepath ? EventId.Charge : _movementId;
owner.ToCreature()?.GetAI()?.MovementInform(MovementGeneratorType.Point, movementId);
@@ -4,6 +4,7 @@
using Framework.Constants;
using Game.Entities;
using System;
using System.Threading.Tasks;
namespace Game.Movement
{
@@ -16,7 +17,7 @@ namespace Game.Movement
float _wanderDistance;
uint _wanderSteps;
public RandomMovementGenerator(float spawnDist = 0.0f, TimeSpan? duration = null)
public RandomMovementGenerator(float spawnDist = 0.0f, TimeSpan? duration = null, TaskCompletionSource<MovementStopReason> scriptResult = null)
{
_timer = new TimeTracker();
_reference = new();
@@ -26,6 +27,8 @@ namespace Game.Movement
Priority = MovementGeneratorPriority.Normal;
Flags = MovementGeneratorFlags.InitializationPending;
BaseUnitState = UnitState.Roaming;
ScriptResult = scriptResult;
if (duration.HasValue)
_duration = new TimeTracker(duration.Value);
}
@@ -112,8 +115,11 @@ namespace Game.Movement
}
if (movementInform && HasFlag(MovementGeneratorFlags.InformEnabled))
{
SetScriptResult(MovementStopReason.Finished);
if (owner.IsAIEnabled())
owner.GetAI().MovementInform(MovementGeneratorType.Random, 0);
}
}
public override void Pause(uint timer)
@@ -7,6 +7,7 @@ using Game.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Game.Movement
{
@@ -30,8 +31,7 @@ namespace Game.Movement
bool _generatePath;
public WaypointMovementGenerator(uint pathId = 0, bool repeating = true, TimeSpan? duration = null, float? speed = null, MovementWalkRunSpeedSelectionMode speedSelectionMode = MovementWalkRunSpeedSelectionMode.Default,
(TimeSpan min, TimeSpan max)? waitTimeRangeAtPathEnd = null, float? wanderDistanceAtPathEnds = null, bool? followPathBackwardsFromEndToStart = null, bool generatePath = true)
(TimeSpan min, TimeSpan max)? waitTimeRangeAtPathEnd = null, float? wanderDistanceAtPathEnds = null, bool? followPathBackwardsFromEndToStart = null, bool generatePath = true, TaskCompletionSource<MovementStopReason> scriptResult = null)
{
_nextMoveTime = new TimeTracker(0);
_pathId = pathId;
@@ -49,13 +49,14 @@ namespace Game.Movement
Priority = MovementGeneratorPriority.Normal;
Flags = MovementGeneratorFlags.InitializationPending;
BaseUnitState = UnitState.Roaming;
ScriptResult = scriptResult;
if (duration.HasValue)
_duration = new(duration.Value);
}
public WaypointMovementGenerator(WaypointPath path, bool repeating = true, TimeSpan? duration = null, float? speed = null, MovementWalkRunSpeedSelectionMode speedSelectionMode = MovementWalkRunSpeedSelectionMode.Default,
(TimeSpan min, TimeSpan max)? waitTimeRangeAtPathEnd = null, float? wanderDistanceAtPathEnds = null, bool? followPathBackwardsFromEndToStart = null, bool generatePath = true)
(TimeSpan min, TimeSpan max)? waitTimeRangeAtPathEnd = null, float? wanderDistanceAtPathEnds = null, bool? followPathBackwardsFromEndToStart = null, bool generatePath = true, TaskCompletionSource<MovementStopReason> scriptResult = null)
{
_nextMoveTime = new TimeTracker(0);
_repeating = repeating;
@@ -72,6 +73,7 @@ namespace Game.Movement
Priority = MovementGeneratorPriority.Normal;
Flags = MovementGeneratorFlags.InitializationPending;
BaseUnitState = UnitState.Roaming;
ScriptResult = scriptResult;
if (duration.HasValue)
_duration = new(duration.Value);
@@ -176,6 +178,9 @@ namespace Game.Movement
{
RemoveFlag(MovementGeneratorFlags.Transitory);
AddFlag(MovementGeneratorFlags.InformEnabled);
AddFlag(MovementGeneratorFlags.Finalized);
owner.UpdateCurrentWaypointInfo(0, 0);
SetScriptResult(MovementStopReason.Finished);
return false;
}
}
@@ -268,6 +273,9 @@ namespace Game.Movement
// TODO: Research if this modification is needed, which most likely isnt
owner.SetWalk(false);
}
if (movementInform)
SetScriptResult(MovementStopReason.Finished);
}
public void MovementInform(Creature owner)
@@ -367,6 +375,8 @@ namespace Game.Movement
CreatureAI ai = owner.GetAI();
if (ai != null)
ai.WaypointPathEnded(currentWaypoint.Id, _path.Id);
SetScriptResult(MovementStopReason.Finished);
return;
}
}
@@ -406,7 +416,7 @@ namespace Game.Movement
init.SetAnimation(AnimTier.Ground);
break;
case WaypointMoveType.Takeoff:
init.SetAnimation(AnimTier.Hover);
init.SetAnimation(AnimTier.Fly);
break;
case WaypointMoveType.Run:
init.SetWalk(false);