From f1682ef22657b6c73a98d2de81dadcd30785d9c8 Mon Sep 17 00:00:00 2001 From: Xian55 <367101+Xian55@users.noreply.github.com> Date: Sun, 15 Dec 2024 14:31:48 +0100 Subject: [PATCH] Core: FleeGoal: Added a way to custom define when the goal should be opt-in. Core: Refactor a little bit. --- Core/ClassConfig/ClassConfiguration.cs | 1 + Core/Goals/FleeGoal.cs | 37 ++++++++++++------------ Core/GoalsComponent/Navigation.cs | 8 ++--- Core/GoalsComponent/SafeSpotCollector.cs | 35 ++++++++++++++++++---- Core/GoalsFactory/GoalFactory.cs | 11 +++++-- Core/Path/Simplify/PathSimplify.cs | 7 +++-- README.md | 29 +++++++++++++++++++ 7 files changed, 94 insertions(+), 34 deletions(-) diff --git a/Core/ClassConfig/ClassConfiguration.cs b/Core/ClassConfig/ClassConfiguration.cs index c6eaab7ce..77641ce3a 100644 --- a/Core/ClassConfig/ClassConfiguration.cs +++ b/Core/ClassConfig/ClassConfiguration.cs @@ -69,6 +69,7 @@ public sealed partial class ClassConfiguration public Dictionary IntVariables { get; } = new(); public KeyActions Pull { get; } = new(); + public KeyActions Flee { get; } = new(); public KeyActions Combat { get; } = new(); public KeyActions Adhoc { get; } = new(); public KeyActions Parallel { get; } = new(); diff --git a/Core/Goals/FleeGoal.cs b/Core/Goals/FleeGoal.cs index 8d0742151..19930e309 100644 --- a/Core/Goals/FleeGoal.cs +++ b/Core/Goals/FleeGoal.cs @@ -2,6 +2,8 @@ using Microsoft.Extensions.Logging; +using SharedLib.Extensions; + using System; using System.Buffers; using System.Numerics; @@ -19,18 +21,15 @@ public sealed class FleeGoal : GoapGoal, IRouteProvider private readonly PlayerReader playerReader; private readonly Navigation navigation; private readonly AddonBits bits; - private readonly CombatLog combatLog; private readonly SafeSpotCollector safeSpotCollector; private Vector3[] MapPoints = []; - public int MOB_COUNT = 1; - public FleeGoal(ILogger logger, ConfigurableInput input, Wait wait, PlayerReader playerReader, AddonBits bits, ClassConfiguration classConfiguration, Navigation playerNavigation, - ClassConfiguration classConfig, CombatLog combatLog, + ClassConfiguration classConfig, SafeSpotCollector safeSpotCollector) : base(nameof(FleeGoal)) { @@ -41,15 +40,13 @@ public FleeGoal(ILogger logger, ConfigurableInput input, this.playerReader = playerReader; this.navigation = playerNavigation; this.bits = bits; - this.combatLog = combatLog; this.classConfig = classConfig; AddPrecondition(GoapKey.incombat, true); - Keys = classConfiguration.Combat.Sequence; + Keys = classConfiguration.Flee.Sequence; - // this will ensure that the component is created this.safeSpotCollector = safeSpotCollector; } @@ -79,28 +76,32 @@ public Vector3 NextMapPoint() public override bool CanRun() { return - safeSpotCollector.Locations.Count > 0 && - combatLog.DamageTakenCount() > MOB_COUNT; + safeSpotCollector.MapLocations.Count > 0 && + Keys.Length > 0 && Keys[0].CanRun(); } public override void OnEnter() { - // TODO: might have to do some pre processing like - // straightening the path like - // PathSimplify.SimplifyPath(MapPoints); - var count = safeSpotCollector.Locations.Count; - MapPoints = new Vector3[count]; + int count = safeSpotCollector.MapLocations.Count; + + ArrayPool pooler = ArrayPool.Shared; + Vector3[] array = pooler.Rent(count); + var span = array.AsSpan(); - safeSpotCollector.Locations.CopyTo(MapPoints, 0); + safeSpotCollector.MapLocations.CopyTo(array, 0); - navigation.SetWayPoints(MapPoints.AsSpan(0, count)); + Span simplified = PathSimplify.Simplify(array.AsSpan()[..count], PathSimplify.HALF, true); + MapPoints = simplified.ToArray(); + + navigation.SetWayPoints(simplified); navigation.ResetStuckParameters(); + + pooler.Return(array); } public override void OnExit() { - // TODO: there might be better options here to dont clear all of them - safeSpotCollector.Locations.Clear(); + safeSpotCollector.Reduce(playerReader.MapPosNoZ); navigation.Stop(); navigation.StopMovement(); diff --git a/Core/GoalsComponent/Navigation.cs b/Core/GoalsComponent/Navigation.cs index 403093705..9cceaa052 100644 --- a/Core/GoalsComponent/Navigation.cs +++ b/Core/GoalsComponent/Navigation.cs @@ -419,14 +419,10 @@ private float ReachedDistance(float minDistance) private void ReduceByDistance(Vector3 playerW, float minDistance) { - float worldDistance = playerW.WorldDistanceXYTo(routeToNextWaypoint.Peek()); - while (worldDistance < ReachedDistance(minDistance) && routeToNextWaypoint.Count > 0) + while (routeToNextWaypoint.Count > 0 && + playerW.WorldDistanceXYTo(routeToNextWaypoint.Peek()) < ReachedDistance(minDistance)) { routeToNextWaypoint.Pop(); - if (routeToNextWaypoint.Count > 0) - { - worldDistance = playerW.WorldDistanceXYTo(routeToNextWaypoint.Peek()); - } } } diff --git a/Core/GoalsComponent/SafeSpotCollector.cs b/Core/GoalsComponent/SafeSpotCollector.cs index 0bf0d6498..2f47a346e 100644 --- a/Core/GoalsComponent/SafeSpotCollector.cs +++ b/Core/GoalsComponent/SafeSpotCollector.cs @@ -1,4 +1,4 @@ -using Core.GOAP; +using SharedLib.Extensions; using System; using System.Collections.Generic; @@ -14,7 +14,7 @@ public sealed class SafeSpotCollector : IDisposable private readonly Timer timer; - public Stack Locations { get; } = new(); + public Stack MapLocations { get; } = new(); public SafeSpotCollector( PlayerReader playerReader, @@ -36,11 +36,34 @@ public void Update(object? obj) if (bits.Combat()) return; - if (Locations.TryPeek(out var lastPos) && - lastPos == playerReader.MapPosNoZ) + if (MapLocations.TryPeek(out Vector3 lastMapPos) && + lastMapPos == playerReader.MapPosNoZ) return; - // TODO: might be a good idea to check distance to last location - Locations.Push(playerReader.MapPosNoZ); + MapLocations.Push(playerReader.MapPosNoZ); + } + + public void Reduce(Vector3 mapPosition) + { + lock (MapLocations) + { + Vector3 closestM = default; + float distanceM = float.MaxValue; + + foreach (Vector3 p in MapLocations) + { + float d = mapPosition.MapDistanceXYTo(p); + if (d < distanceM) + { + closestM = p; + distanceM = d; + } + } + + while (MapLocations.TryPeek(out var p) && p != closestM) + { + MapLocations.Pop(); + } + } } } diff --git a/Core/GoalsFactory/GoalFactory.cs b/Core/GoalsFactory/GoalFactory.cs index fa6fbd1e7..a5da1286e 100644 --- a/Core/GoalsFactory/GoalFactory.cs +++ b/Core/GoalsFactory/GoalFactory.cs @@ -84,7 +84,6 @@ public static IServiceProvider Create( { services.AddScoped(); services.AddScoped(); - services.AddScoped(); services.AddScoped(); services.AddScoped(); ResolveFollowRouteGoal(services, classConfig); @@ -138,7 +137,7 @@ public static IServiceProvider Create( services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); + AddFleeGoal(services, classConfig); services.AddScoped(); if (classConfig.WrongZone.ZoneId > 0) @@ -294,6 +293,14 @@ public static void ResolveFollowRouteGoal(IServiceCollection services, } } + public static void AddFleeGoal(IServiceCollection services, ClassConfiguration classConfig) + { + if (classConfig.Flee.Sequence.Length == 0) + return; + + services.AddScoped(); + } + private static string RelativeFilePath(DataConfig dataConfig, string path) { return !path.Contains(dataConfig.Path) diff --git a/Core/Path/Simplify/PathSimplify.cs b/Core/Path/Simplify/PathSimplify.cs index 83f175bbf..a196a915e 100644 --- a/Core/Path/Simplify/PathSimplify.cs +++ b/Core/Path/Simplify/PathSimplify.cs @@ -7,6 +7,9 @@ namespace Core; public static class PathSimplify { + public const float DEFAULT = 0.3f; + public const float HALF = 0.15f; + // square distance from a Vector3 to a segment private static float GetSquareSegmentDistance(in Vector3 p, in Vector3 p1, in Vector3 p2) { @@ -129,10 +132,10 @@ private static Span DouglasPeucker(Span points, float sqTolera /// Tolerance tolerance in the same measurement as the Vector3 coordinates /// Enable highest quality for using Douglas-Peucker, set false for Radial-Distance algorithm /// Simplified list of Vector3 - public static Span Simplify(Span points, float tolerance = 0.3f, bool highestQuality = false) + public static Span Simplify(Span points, float tolerance = DEFAULT, bool highestQuality = false) { if (points.Length == 0) - return Array.Empty(); + return []; float sqTolerance = tolerance * tolerance; diff --git a/README.md b/README.md index 3bcb887bc..24997abdf 100644 --- a/README.md +++ b/README.md @@ -482,6 +482,7 @@ Your class file probably exists and just needs to be edited to set the pathing f | `"IntVariables"` | List of user defined `integer` variables | true | `[]` | | --- | --- | --- | --- | | `"Pull"` | [KeyActions](#keyactions) to execute upon [Pull Goal](#pull-goal) | true | `{}` | +| `"Flee"` | [KeyActions](#keyactions) to execute upon [Flee Goal](#flee-goal). Currently only the first one is considered for the custom logic. | true | `{}` | | `"Combat"` | [KeyActions](#keyactions) to execute upon [Combat Goal](#combat-goal) | **false** | `{}` | | `"AssistFocus"` | [KeyActions](#keyactions) to execute upon [Assist Focus Goal](#assist-focus-goal) | **false** | `{}` | | `"Adhoc"` | [KeyActions](#keyactions) to execute upon [Adhoc Goals](#adhoc-goals) | true | `{}` | @@ -900,6 +901,33 @@ e.g. of a Balance Druid }, ``` +### Flee Goal + +Its an opt-in goal. + +Can define custom rules when the character should try to run away from an encounter which seems to be impossible to survive. + +The goal will be executed while the player is in combat and the first KeyAction custom [Requirement(s)](#requirement) are met. + +While the goal is active +* the player going to retrace the past locations which were deemed to be safe. +* Clears the current target if have any. + +The path will be simplifed to ensure straight line of movement. + +To opt-in the goal execution you have to define the following the [Class Configuration](#12-class-configuration) + +```json +"Flee": { + "Sequence": [ + { + "Name": "Flee", + "Requirement": "MobCount > 1 && Health% < 50" + } + ] +}, +``` + ### Combat Goal The `Sequence` of [KeyAction(s)](#keyaction) that are used when in combat and trying to kill a mob. @@ -1931,6 +1959,7 @@ e.g. Rogue_20.json Every [KeyAction](#keyaction) has individual Interrupt(s) condition(s) which are [Requirement(s)](#requirement) to stop execution before fully finishing it. As of now every [Goal groups](#goal-groups) has a default Interrupt. +* [Flee Goal](#flee-goal) based [KeyAction(s)](#keyaction) interrupted once the player left combat. * [Combat Goal](#combat-goal) based [KeyAction(s)](#keyaction) interrupted once the target dies and the player loses the target. * [Parallel Goal](#parallel-goals) based [KeyAction(s)](#keyaction) has **No** interrupt conditions. * [Adhoc Goals](#adhoc-goals) based [KeyAction(s)](#keyaction) depends on `KeyAction.InCombat` flag.