Skip to content

Commit

Permalink
Don't leave map bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit committed Jan 16, 2025
1 parent 748afb6 commit e3da82a
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions OpenDreamRuntime/Map/DreamMapManager.Pathfinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,24 @@ public IEnumerable<AtomDirection> CalculateSteps((int X, int Y, int Z) loc, (int

toExplore.Enqueue(PathFindNode.GetNode(loc.X, loc.Y));

void Explore(PathFindNode current, int offsetX, int offsetY, int currentStep) {
var next = PathFindNode.GetNode(current.X + offsetX, current.Y + offsetY);
void Explore(PathFindNode current, int offsetX, int offsetY) {
var nextX = current.X + offsetX;
var nextY = current.Y + offsetY;
if (nextX < 1 || nextX > Size.X || nextY < 1 || nextY > Size.Y)
return; // This is outside of map bounds

var next = PathFindNode.GetNode(nextX, nextY);
if (explored.Contains(next))
return;
if (!TryGetCellAt(new(next.X, next.Y), z, out var cell) || cell.Turf.IsDense)
return;

if (!toExplore.Contains(next))
toExplore.Enqueue(next);
else if (next.NeededSteps >= currentStep + 1)
else if (next.NeededSteps >= current.NeededSteps + 1)
return;

next.NeededSteps = currentStep + 1;
next.NeededSteps = current.NeededSteps + 1;
next.Parent = current;
}

Expand All @@ -87,14 +92,14 @@ void Explore(PathFindNode current, int offsetX, int offsetY, int currentStep) {
}

explored.Add(node);
Explore(node, 1, 0, node.NeededSteps);
Explore(node, 1, 1, node.NeededSteps);
Explore(node, 0, 1, node.NeededSteps);
Explore(node, -1, 1, node.NeededSteps);
Explore(node, -1, 0, node.NeededSteps);
Explore(node, -1, -1, node.NeededSteps);
Explore(node, 0, -1, node.NeededSteps);
Explore(node, 1, -1, node.NeededSteps);
Explore(node, 1, 0);
Explore(node, 1, 1);
Explore(node, 0, 1);
Explore(node, -1, 1);
Explore(node, -1, 0);
Explore(node, -1, -1);
Explore(node, 0, -1);
Explore(node, 1, -1);
}

foreach (var node in explored)
Expand Down

0 comments on commit e3da82a

Please sign in to comment.