~ruther/NosSmooth

89dac16560c8617e42c13e40983134d37a89ea1b — Rutherther 2 years ago f151cbf
fix(pathfinding): update pathfinding to work with same position, small distances...
M Extensions/NosSmooth.Extensions.Pathfinding/Path.cs => Extensions/NosSmooth.Extensions.Pathfinding/Path.cs +8 -3
@@ 85,10 85,15 @@ public class Path
    /// <returns>A position to walk to.</returns>
    public (short X, short Y) TakeForwardPath()
    {
        if (ReachedEnd || CurrentPartIndex + 2 >= Parts.Count)
        if (ReachedEnd)
        {
            return (TargetX, TargetY);
        }
        if (CurrentPartIndex + 2 >= Parts.Count)
        {
            CurrentPartIndex++;
            return (TargetX, TargetY);
        }

        var zeroTile = (CurrentX, CurrentY);
        var firstTile = Parts[++CurrentPartIndex];


@@ 129,11 134,11 @@ public class Path
        if (xFirstDiff != 0)
        {
            var xRatio = xCurrentDiff / (float)xFirstDiff;
            return (yFirstDiff * xRatio) - yCurrentDiff < float.Epsilon * 10;
            return Math.Abs((yFirstDiff * xRatio) - yCurrentDiff) < float.Epsilon;
        }

        var yRatio = yCurrentDiff / (float)yFirstDiff;
        return (xFirstDiff * yRatio) - xCurrentDiff < float.Epsilon * 10;
        return Math.Abs((xFirstDiff * yRatio) - xCurrentDiff) < float.Epsilon;
    }

    /// <summary>

M Extensions/NosSmooth.Extensions.Pathfinding/Pathfinder.cs => Extensions/NosSmooth.Extensions.Pathfinding/Pathfinder.cs +6 -1
@@ 90,11 90,16 @@ public class Pathfinder
        short targetY
    )
    {
        if (!mapInfo.IsWalkable((short)targetX, (short)targetY))
        if (!mapInfo.IsWalkable(targetX, targetY))
        {
            return new NotFoundError("The requested target is not walkable, path cannot be found.");
        }

        if (x == targetX && y == targetY)
        {
            return new Path(mapInfo.Id, x, y, targetX, targetY, Array.Empty<(short X, short Y)>());
        }

        var target = (targetX, targetY);
        var visited = new HashSet<(short X, short Y)>();
        var offsets = new (short X, short Y)[] { (0, 1), (1, 0), (1, 1), (0, -1), (-1, 0), (-1, -1), (1, -1), (-1, 1) };

M Extensions/NosSmooth.Extensions.Pathfinding/WalkManager.cs => Extensions/NosSmooth.Extensions.Pathfinding/WalkManager.cs +10 -0
@@ 53,6 53,11 @@ public class WalkManager
            return Result.FromError(pathResult);
        }

        if (pathResult.Entity.Parts.Count == 0)
        {
            return Result.FromSuccess();
        }

        var path = pathResult.Entity;
        while (!path.ReachedEnd)
        {


@@ 71,6 76,11 @@ public class WalkManager
                    return Result.FromSuccess();
                }

                if (_state.X == x && _state.Y == y)
                {
                    return Result.FromSuccess();
                }

                return walkResult;
            }
        }