From 9e22de63da5c39ac363e9c7b78aebb352859f708 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Mon, 31 Mar 2025 02:20:13 +0200 Subject: [PATCH] Split 'MoveTo' into separate classes, part 2 --- .../Controller/Steps/Movement/LandExecutor.cs | 64 +++++++++++++ .../Controller/Steps/Movement/LandTask.cs | 7 ++ .../Controller/Steps/Movement/MoveTo.cs | 96 +------------------ .../Steps/Movement/WaitForNearDataId.cs | 7 ++ .../Movement/WaitForNearDataIdExecutor.cs | 26 +++++ Questionable/QuestionablePlugin.cs | 4 +- 6 files changed, 107 insertions(+), 97 deletions(-) create mode 100644 Questionable/Controller/Steps/Movement/LandExecutor.cs create mode 100644 Questionable/Controller/Steps/Movement/LandTask.cs create mode 100644 Questionable/Controller/Steps/Movement/WaitForNearDataId.cs create mode 100644 Questionable/Controller/Steps/Movement/WaitForNearDataIdExecutor.cs diff --git a/Questionable/Controller/Steps/Movement/LandExecutor.cs b/Questionable/Controller/Steps/Movement/LandExecutor.cs new file mode 100644 index 00000000..3e2be167 --- /dev/null +++ b/Questionable/Controller/Steps/Movement/LandExecutor.cs @@ -0,0 +1,64 @@ +using System; +using Dalamud.Game.ClientState.Conditions; +using Dalamud.Plugin.Services; +using FFXIVClientStructs.FFXIV.Client.Game; +using FFXIVClientStructs.FFXIV.Client.Game.Character; +using Microsoft.Extensions.Logging; + +namespace Questionable.Controller.Steps.Movement; + +internal sealed class LandExecutor(IClientState clientState, ICondition condition, ILogger logger) + : TaskExecutor +{ + private bool _landing; + private DateTime _continueAt; + + protected override bool Start() + { + if (!condition[ConditionFlag.InFlight]) + { + logger.LogInformation("Not flying, not attempting to land"); + return false; + } + + _landing = AttemptLanding(); + _continueAt = DateTime.Now.AddSeconds(0.25); + return true; + } + + public override ETaskResult Update() + { + if (DateTime.Now < _continueAt) + return ETaskResult.StillRunning; + + if (condition[ConditionFlag.InFlight]) + { + if (!_landing) + { + _landing = AttemptLanding(); + _continueAt = DateTime.Now.AddSeconds(0.25); + } + + return ETaskResult.StillRunning; + } + + return ETaskResult.TaskComplete; + } + + private unsafe bool AttemptLanding() + { + var character = (Character*)(clientState.LocalPlayer?.Address ?? 0); + if (character != null) + { + if (ActionManager.Instance()->GetActionStatus(ActionType.GeneralAction, 23) == 0) + { + logger.LogInformation("Attempting to land"); + return ActionManager.Instance()->UseAction(ActionType.GeneralAction, 23); + } + } + + return false; + } + + public override bool ShouldInterruptOnDamage() => false; +} diff --git a/Questionable/Controller/Steps/Movement/LandTask.cs b/Questionable/Controller/Steps/Movement/LandTask.cs new file mode 100644 index 00000000..ada55b87 --- /dev/null +++ b/Questionable/Controller/Steps/Movement/LandTask.cs @@ -0,0 +1,7 @@ +namespace Questionable.Controller.Steps.Movement; + +internal sealed class LandTask : ITask +{ + public bool ShouldRedoOnInterrupt() => true; + public override string ToString() => "Land"; +} diff --git a/Questionable/Controller/Steps/Movement/MoveTo.cs b/Questionable/Controller/Steps/Movement/MoveTo.cs index 05d8684e..a44bee9a 100644 --- a/Questionable/Controller/Steps/Movement/MoveTo.cs +++ b/Questionable/Controller/Steps/Movement/MoveTo.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Numerics; -using Dalamud.Game.ClientState.Conditions; -using Dalamud.Game.ClientState.Objects.Types; using Dalamud.Plugin.Services; -using FFXIVClientStructs.FFXIV.Client.Game; -using FFXIVClientStructs.FFXIV.Client.Game.Character; using Microsoft.Extensions.Logging; using Questionable.Controller.Steps.Common; using Questionable.Data; -using Questionable.Functions; using Questionable.Model; using Questionable.Model.Questing; @@ -67,92 +61,4 @@ internal static class MoveTo yield return new LandTask(); } } - - internal sealed record WaitForNearDataId(uint DataId, float StopDistance) : ITask - { - public bool ShouldRedoOnInterrupt() => true; - } - - internal sealed class WaitForNearDataIdExecutor( - GameFunctions gameFunctions, - IClientState clientState) : TaskExecutor - { - protected override bool Start() => true; - - public override ETaskResult Update() - { - IGameObject? gameObject = gameFunctions.FindObjectByDataId(Task.DataId); - if (gameObject == null || - (gameObject.Position - clientState.LocalPlayer!.Position).Length() > Task.StopDistance) - { - throw new TaskException("Object not found or too far away, no position so we can't move"); - } - - return ETaskResult.TaskComplete; - } - - public override bool ShouldInterruptOnDamage() => false; - } - - internal sealed class LandTask : ITask - { - public bool ShouldRedoOnInterrupt() => true; - public override string ToString() => "Land"; - } - - internal sealed class LandExecutor(IClientState clientState, ICondition condition, ILogger logger) - : TaskExecutor - { - private bool _landing; - private DateTime _continueAt; - - protected override bool Start() - { - if (!condition[ConditionFlag.InFlight]) - { - logger.LogInformation("Not flying, not attempting to land"); - return false; - } - - _landing = AttemptLanding(); - _continueAt = DateTime.Now.AddSeconds(0.25); - return true; - } - - public override ETaskResult Update() - { - if (DateTime.Now < _continueAt) - return ETaskResult.StillRunning; - - if (condition[ConditionFlag.InFlight]) - { - if (!_landing) - { - _landing = AttemptLanding(); - _continueAt = DateTime.Now.AddSeconds(0.25); - } - - return ETaskResult.StillRunning; - } - - return ETaskResult.TaskComplete; - } - - private unsafe bool AttemptLanding() - { - var character = (Character*)(clientState.LocalPlayer?.Address ?? 0); - if (character != null) - { - if (ActionManager.Instance()->GetActionStatus(ActionType.GeneralAction, 23) == 0) - { - logger.LogInformation("Attempting to land"); - return ActionManager.Instance()->UseAction(ActionType.GeneralAction, 23); - } - } - - return false; - } - - public override bool ShouldInterruptOnDamage() => false; - } } diff --git a/Questionable/Controller/Steps/Movement/WaitForNearDataId.cs b/Questionable/Controller/Steps/Movement/WaitForNearDataId.cs new file mode 100644 index 00000000..a08c8a99 --- /dev/null +++ b/Questionable/Controller/Steps/Movement/WaitForNearDataId.cs @@ -0,0 +1,7 @@ + +namespace Questionable.Controller.Steps.Movement; + +internal sealed record WaitForNearDataId(uint DataId, float StopDistance) : ITask +{ + public bool ShouldRedoOnInterrupt() => true; +} diff --git a/Questionable/Controller/Steps/Movement/WaitForNearDataIdExecutor.cs b/Questionable/Controller/Steps/Movement/WaitForNearDataIdExecutor.cs new file mode 100644 index 00000000..a958a2ed --- /dev/null +++ b/Questionable/Controller/Steps/Movement/WaitForNearDataIdExecutor.cs @@ -0,0 +1,26 @@ +using Dalamud.Game.ClientState.Objects.Types; +using Dalamud.Plugin.Services; +using Questionable.Functions; + +namespace Questionable.Controller.Steps.Movement; + +internal sealed class WaitForNearDataIdExecutor( + GameFunctions gameFunctions, + IClientState clientState) : TaskExecutor +{ + protected override bool Start() => true; + + public override ETaskResult Update() + { + IGameObject? gameObject = gameFunctions.FindObjectByDataId(Task.DataId); + if (gameObject == null || + (gameObject.Position - clientState.LocalPlayer!.Position).Length() > Task.StopDistance) + { + throw new TaskException("Object not found or too far away, no position so we can't move"); + } + + return ETaskResult.TaskComplete; + } + + public override bool ShouldInterruptOnDamage() => false; +} diff --git a/Questionable/QuestionablePlugin.cs b/Questionable/QuestionablePlugin.cs index ef153105..82d3e40e 100644 --- a/Questionable/QuestionablePlugin.cs +++ b/Questionable/QuestionablePlugin.cs @@ -176,8 +176,8 @@ public sealed class QuestionablePlugin : IDalamudPlugin serviceCollection .AddTaskFactoryAndExecutor(); serviceCollection.AddTaskFactoryAndExecutor(); - serviceCollection.AddTaskExecutor(); - serviceCollection.AddTaskExecutor(); + serviceCollection.AddTaskExecutor(); + serviceCollection.AddTaskExecutor(); serviceCollection .AddTaskFactoryAndExecutor(); -- 2.30.2