mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2024-11-06 18:27:20 -05:00
dd21809288
Copied go-astar into d2common/d2astar, made a few optimizations. Runs roughly 30% faster according to my benchmarking. Added a `maxCost` param to prevent searching the entire map for a path. This probably needs tweaked a bit, but follows the original game more closely. Co-authored-by: Nicholas Eden <neden@zigzagame.com>
36 lines
664 B
Go
36 lines
664 B
Go
package d2astar
|
|
|
|
// A priorityQueue implements heap.Interface and holds Nodes. The
|
|
// priorityQueue is used to track open nodes by rank.
|
|
type priorityQueue []*node
|
|
|
|
func (pq priorityQueue) Len() int {
|
|
return len(pq)
|
|
}
|
|
|
|
func (pq priorityQueue) Less(i, j int) bool {
|
|
return pq[i].rank < pq[j].rank
|
|
}
|
|
|
|
func (pq priorityQueue) Swap(i, j int) {
|
|
pq[i], pq[j] = pq[j], pq[i]
|
|
pq[i].index = i
|
|
pq[j].index = j
|
|
}
|
|
|
|
func (pq *priorityQueue) Push(x interface{}) {
|
|
n := len(*pq)
|
|
no := x.(*node)
|
|
no.index = n
|
|
*pq = append(*pq, no)
|
|
}
|
|
|
|
func (pq *priorityQueue) Pop() interface{} {
|
|
old := *pq
|
|
n := len(old)
|
|
no := old[n-1]
|
|
no.index = -1
|
|
*pq = old[0 : n-1]
|
|
return no
|
|
}
|