From 214e6e61f468bb7e58d24cfeb89432c32db8eae1 Mon Sep 17 00:00:00 2001 From: Chris3606 Date: Thu, 5 Jun 2025 18:58:13 -0500 Subject: [PATCH] temp --- GoRogue.UnitTests/Pathing/FleeMapTests.cs | 42 ++++++- GoRogue/GoRogue.csproj | 2 +- GoRogue/Pathing/AStar.cs | 121 ------------------ GoRogue/Pathing/Path.cs | 144 ++++++++++++++++++++++ 4 files changed, 186 insertions(+), 123 deletions(-) create mode 100644 GoRogue/Pathing/Path.cs diff --git a/GoRogue.UnitTests/Pathing/FleeMapTests.cs b/GoRogue.UnitTests/Pathing/FleeMapTests.cs index 004c4c03..d558be69 100644 --- a/GoRogue.UnitTests/Pathing/FleeMapTests.cs +++ b/GoRogue.UnitTests/Pathing/FleeMapTests.cs @@ -1,18 +1,25 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using GoRogue.Pathing; using GoRogue.UnitTests.Mocks; using SadRogue.Primitives; using SadRogue.Primitives.GridViews; using Xunit; +using Xunit.Abstractions; namespace GoRogue.UnitTests.Pathing { public class FleeMapTests { + private readonly ITestOutputHelper output; private const int _width = 40; private const int _height = 35; private static readonly Point _goal = (5, 5); + public FleeMapTests(ITestOutputHelper output) + { + this.output = output; + } [Fact] public void FleeMapDoesNotLeadToGoal() { @@ -61,5 +68,38 @@ public void OpenEdgedMapSupported() // TODO: Verify flee map leads away from goal } + + [Theory] + [InlineData(1.2)] //go right + [InlineData(1.6)] //go right + [InlineData(1.7)] //go left !? + public void test(double magnitude) + { + //map: + // # + // #C + // # + // Character (C) at (5,5) should flee threats (#) by going right + var threatsCells = new List + { + (5,4), + (4,5), + (5,6) + }; + + var gridView = new LambdaGridView(11, 11, cell => threatsCells.Contains(cell) ? GoalState.Goal : GoalState.Clear); + var goalMap = new GoalMap(gridView, Distance.Manhattan); + var fleeMap = new FleeMap(goalMap, magnitude); + var currentCell = new SadRogue.Primitives.Point(5, 5); + + + output.WriteLine(fleeMap.ToString(4)); + + //Direction direction = fleeMap.GetDirectionOfMinValue(currentCell, AdjacencyRule.Cardinals); + + + + //Assert.Equal(Direction.Right, direction); + } } } diff --git a/GoRogue/GoRogue.csproj b/GoRogue/GoRogue.csproj index adacd06f..57b62962 100644 --- a/GoRogue/GoRogue.csproj +++ b/GoRogue/GoRogue.csproj @@ -1,7 +1,7 @@  - netstandard2.1;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0 + netstandard2.1;netcoreapp3.1;net5.0;net6.0;net7.0 GoRogue Chris3606 Copyright © 2023 Christopher Ridley (Chris3606) diff --git a/GoRogue/Pathing/AStar.cs b/GoRogue/Pathing/AStar.cs index 0b181fbb..c72d012b 100644 --- a/GoRogue/Pathing/AStar.cs +++ b/GoRogue/Pathing/AStar.cs @@ -366,127 +366,6 @@ private bool CheckWalkability(Point pos, Point start, Point end, bool assumeEndp } } - /// - /// Encapsulates a path as returned by pathfinding algorithms like AStar. - /// - /// - /// Provides various functions to iterate through/access steps of the path, as well as - /// constant-time reversing functionality. - /// - [PublicAPI] - public class Path - { - private readonly IReadOnlyList _steps; - private bool _inOriginalOrder; - - /// - /// Creates a copy of the path, optionally reversing the path as it does so. - /// - /// Reversing is an O(1) operation, since it does not modify the list. - /// The path to copy. - /// Whether or not to reverse the path. Defaults to . - public Path(Path pathToCopy, bool reverse = false) - { - _steps = pathToCopy._steps; - _inOriginalOrder = reverse ? !pathToCopy._inOriginalOrder : pathToCopy._inOriginalOrder; - } - - // Create based on internal list - internal Path(IReadOnlyList steps) - { - _steps = steps; - _inOriginalOrder = true; - } - - /// - /// Ending point of the path. - /// - public Point End => _inOriginalOrder ? _steps[0] : _steps[^1]; - - /// - /// The length of the path, NOT including the starting point. - /// - public int Length => _steps.Count - 1; - - /// - /// The length of the path, INCLUDING the starting point. - /// - public int LengthWithStart => _steps.Count; - - /// - /// Starting point of the path. - /// - public Point Start => _inOriginalOrder ? _steps[^1] : _steps[0]; - - - /// - /// The coordinates that constitute the path (in order), NOT including the starting point. - /// These are the coordinates something might walk along to follow a path. - /// - public IEnumerable Steps - { - get - { - if (_inOriginalOrder) - for (var i = _steps.Count - 2; i >= 0; i--) - yield return _steps[i]; - else - for (var i = 1; i < _steps.Count; i++) - yield return _steps[i]; - } - } - - /// - /// The coordinates that constitute the path (in order), INCLUDING the starting point. - /// - public IEnumerable StepsWithStart - { - get - { - if (_inOriginalOrder) - for (var i = _steps.Count - 1; i >= 0; i--) - yield return _steps[i]; - else - foreach (var step in _steps) - yield return step; - } - } - - /// - /// Gets the nth step along the path, where 0 is the step AFTER the starting point. - /// - /// The (array-like index) of the step to get. - /// The coordinate constituting the step specified. - public Point GetStep(int stepNum) - { - if (_inOriginalOrder) - return _steps[_steps.Count - 2 - stepNum]; - - return _steps[stepNum + 1]; - } - - /// - /// Gets the nth step along the path, where 0 IS the starting point. - /// - /// The (array-like index) of the step to get. - /// The coordinate constituting the step specified. - public Point GetStepWithStart(int stepNum) => - // TODO: Revisit array-from-end syntax here - _inOriginalOrder ? _steps[_steps.Count - 1 - stepNum] : _steps[stepNum]; - - /// - /// Reverses the path, in constant time. - /// - public void Reverse() => _inOriginalOrder = !_inOriginalOrder; - - /// - /// Returns a string representation of all the steps in the path, including the start point, - /// eg. [(1, 2), (3, 4), (5, 6)]. - /// - /// A string representation of all steps in the path, including the start. - public override string ToString() => StepsWithStart.ExtendToString(); - } - // Node representing a grid position in AStar's priority queue internal class AStarNode : FastPriorityQueueNode { diff --git a/GoRogue/Pathing/Path.cs b/GoRogue/Pathing/Path.cs new file mode 100644 index 00000000..1003df41 --- /dev/null +++ b/GoRogue/Pathing/Path.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections.Generic; +using JetBrains.Annotations; +using SadRogue.Primitives; + +namespace GoRogue.Pathing +{ + /// + /// Encapsulates a path as returned by pathfinding algorithms like AStar. + /// + /// + /// Provides various functions to iterate through/access steps of the path, as well as + /// constant-time reversing functionality. + /// + [PublicAPI] + public class Path : IReadOnlyList // TODO: Need efficient enumerables; also maybe pull start out of path? + { + private readonly IReadOnlyList _steps; + private bool _inOriginalOrder; + + /// + /// Creates a copy of the path, optionally reversing the path as it does so. + /// + /// Reversing is an O(1) operation, since it does not modify the list. + /// The path to copy. + /// Whether or not to reverse the path. Defaults to . + public Path(Path pathToCopy, bool reverse = false) + { + _steps = pathToCopy._steps; + _inOriginalOrder = reverse ? !pathToCopy._inOriginalOrder : pathToCopy._inOriginalOrder; + } + + // Create based on internal list + internal Path(IReadOnlyList steps) + { + _steps = steps; + _inOriginalOrder = true; + } + + /// + /// Gets the nth step along the path, where 0 is the step AFTER the starting point. + /// + /// The (array-like index) of the step to get. + /// The coordinate constituting the step specified. + public Point this[int index] => _inOriginalOrder ? _steps[^(2 - index)] : _steps[index + 1]; + + /// + /// The length of the path, NOT including the starting point. + /// + public int Count => _steps.Count - 1; + + /// + /// Ending point of the path. + /// + public Point End => _inOriginalOrder ? _steps[0] : _steps[^1]; + + /// + /// The length of the path, NOT including the starting point. + /// + [Obsolete("Use Count property instead.")] + public int Length => _steps.Count - 1; + + /// + /// The length of the path, INCLUDING the starting point. + /// + [Obsolete("Use CountWithStart property instead.")] + public int LengthWithStart => _steps.Count; + + /// + /// Starting point of the path. + /// + public Point Start => _inOriginalOrder ? _steps[^1] : _steps[0]; + + + /// + /// The coordinates that constitute the path (in order), NOT including the starting point. + /// These are the coordinates something might walk along to follow a path. + /// + [Obsolete("Path now implements IReadOnlyList; use its indexers/enumerable implementation directly instead.")] + public IEnumerable Steps + { + get + { + if (_inOriginalOrder) + for (var i = _steps.Count - 2; i >= 0; i--) + yield return _steps[i]; + else + for (var i = 1; i < _steps.Count; i++) + yield return _steps[i]; + } + } + + /// + /// The coordinates that constitute the path (in order), INCLUDING the starting point. + /// + public IEnumerable StepsWithStart + { + get + { + if (_inOriginalOrder) + for (var i = _steps.Count - 1; i >= 0; i--) + yield return _steps[i]; + else + foreach (var step in _steps) + yield return step; + } + } + + /// + /// Gets the nth step along the path, where 0 is the step AFTER the starting point. + /// + /// The (array-like index) of the step to get. + /// The coordinate constituting the step specified. + [Obsolete("Use the indexers provided by Path instead.")] + public Point GetStep(int stepNum) + { + if (_inOriginalOrder) + return _steps[_steps.Count - 2 - stepNum]; + + return _steps[stepNum + 1]; + } + + /// + /// Gets the nth step along the path, where 0 IS the starting point. + /// + /// The (array-like index) of the step to get. + /// The coordinate constituting the step specified. + public Point GetStepWithStart(int stepNum) => + // TODO: Revisit array-from-end syntax here + _inOriginalOrder ? _steps[_steps.Count - 1 - stepNum] : _steps[stepNum]; + + /// + /// Reverses the path, in constant time. + /// + public void Reverse() => _inOriginalOrder = !_inOriginalOrder; + + /// + /// Returns a string representation of all the steps in the path, including the start point, + /// eg. [(1, 2), (3, 4), (5, 6)]. + /// + /// A string representation of all steps in the path, including the start. + public override string ToString() => StepsWithStart.ExtendToString(); + } +}