Skip to content

Commit b1c784b

Browse files
authored
Merge pull request #131 from NosCoreIO/FixException
Fix #130
2 parents b73023c + c3d78cc commit b1c784b

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/NosCore.PathFinder/NosCore.PathFinder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<RepositoryUrl>https://github.com/NosCoreIO/NosCore.PathFinder.git</RepositoryUrl>
1313
<PackageIconUrl></PackageIconUrl>
1414
<PackageTags>nostale, noscore, nostale private server source, nostale emulator</PackageTags>
15-
<Version>0.0.12</Version>
15+
<Version>0.0.13</Version>
1616
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
1717
<Description>NosCore's PathFinder</Description>
1818
<PackageLicenseExpression></PackageLicenseExpression>

src/NosCore.PathFinder/Pathfinder/GoalBasedPathfinder.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,27 @@ public GoalBasedPathfinder(IMapGrid mapGrid, IHeuristic heuristic)
2525
_heuristic = heuristic;
2626
}
2727

28+
public GoalBasedPathfinder(IMapGrid mapGrid, IHeuristic heuristic, BrushFire brushfire) : this(mapGrid, heuristic)
29+
{
30+
CacheBrushFire(brushfire, brushfire.Origin);
31+
}
32+
33+
2834
private BrushFire CacheBrushFire(BrushFire brushFire, (short X, short Y) start)
2935
{
3036
Node? GetParent((short X, short Y) currentnode)
3137
{
32-
var neighbor = _mapGrid.GetNeighbors(currentnode).Select(s => new Node((s.X, s.Y), brushFire.Grid[(s.X, s.Y)]?.Value ?? 0)).OrderBy(s => s.Value).FirstOrDefault();
38+
var neighbor = _mapGrid.GetNeighbors(currentnode).Select(s => new Node((s.X, s.Y), brushFire.Grid.ContainsKey((s.X, s.Y)) ? brushFire.Grid[(s.X, s.Y)]?.Value ?? 0 : 0)).OrderBy(s => s.Value).FirstOrDefault();
3339
if (!(neighbor is { } neighborCell))
3440
{
3541
return null;
3642
}
3743

44+
if (!brushFire.Grid.ContainsKey((neighborCell.Position.X, neighborCell.Position.Y)))
45+
{
46+
brushFire.Grid.Add((neighborCell.Position.X, neighborCell.Position.Y), new Node(neighborCell.Position, null));
47+
}
48+
3849
brushFire.Grid[(neighborCell.Position.X, neighborCell.Position.Y)] ??= new Node(neighborCell.Position, null);
3950

4051
if (neighborCell.Value > 0 && brushFire.Grid[(neighborCell.Position.X, neighborCell.Position.Y)]!.Closed == false)
@@ -59,10 +70,10 @@ private BrushFire CacheBrushFire(BrushFire brushFire, (short X, short Y) start)
5970

6071
public IEnumerable<(short X, short Y)> FindPath((short X, short Y) start, (short X, short Y) end)
6172
{
62-
List<(short X, short Y)> list = new List<(short X, short Y)>();
73+
List<(short X, short Y)> list = new();
6374
BrushFirecache.TryGetValue(end, out BrushFire? brushFireOut);
6475

65-
if (!_mapGrid.IsWalkable(start.X, start.Y) || !_mapGrid.IsWalkable(end.X, end.Y))
76+
if (!_mapGrid.IsWalkable(start.X, start.Y) || !_mapGrid.IsWalkable(end.X, end.Y) || (brushFireOut != null && !brushFireOut.Value.Grid.ContainsKey((start.X, start.Y))))
6677
{
6778
return list;
6879
}

test/NosCore.PathFinder.Tests/GoalBasedPathfinderTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,16 @@ public void Test_GoalBasedPathfinder()
8383

8484
TestHelper.VerifyFile("goal-based-pathfinder.png", bitmap, listPixel, "Goal Based Pathfinder");
8585
}
86+
87+
88+
[TestMethod]
89+
public void Test_GoalBasedPathfinder_OutOfDistance_ShouldNotReturnPath()
90+
{
91+
(short X, short Y) characterPosition = (6, 10);
92+
var brushFire = _map.LoadBrushFire(characterPosition, new OctileDistanceHeuristic(), 2);
93+
var goalPathfinder = new GoalBasedPathfinder(_map, new OctileDistanceHeuristic(), brushFire);
94+
var path = goalPathfinder.FindPath((2, 2), characterPosition);
95+
Assert.AreEqual(0, path.Count());
96+
}
8697
}
8798
}

0 commit comments

Comments
 (0)