Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
val antonini committed Apr 28, 2024
1 parent 7ac71d5 commit 087da14
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions astar.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ func NewPathfinder(weights Grid[int]) Pathfinder {

// Find returns a path from start to end. If no path is found, an empty slice.
func (p Pathfinder) Find(start, end Vec2) []Vec2 {
open := newMinHeap(p.weights.Width, p.weights.Height)
searchSpace := newSearchSpace(p.weights) // tracks the open, closed and f values of each node
searchSpace := newSearchSpace(p.weights) // tracks the open, closed and f values of each node
open := newMinHeap(searchSpace.Width, searchSpace.Height) // prioritised queue of f

origin := searchSpace.Get(start)
origin.f = 0
origin.open = true

open.push(heapNode{pos: start})
open.push(heapNode{pos: start, f: origin.f})
searchSpace.Set(start, origin)

for open.len() > 0 {
Expand Down Expand Up @@ -59,22 +60,24 @@ func (p Pathfinder) Find(start, end Vec2) []Vec2 {
return path
}

// check if more optimal path to successor was already encountered
existingSuccessor := searchSpace.Get(succPos)

if existingSuccessor.open && existingSuccessor.f < successor.f {
continue
}

if existingSuccessor.closed && existingSuccessor.f < successor.f {
continue
}

searchSpace.Set(succPos, successor)
open.push(heapNode{pos: succPos, f: successor.f})
}

q.closed = true
searchSpace.Set(qPos, q)
}

// not found
return []Vec2{}
}

Expand Down

0 comments on commit 087da14

Please sign in to comment.