Skip to content

Commit

Permalink
utils/graph: Added logic to only use the cache after consecutive nodes
Browse files Browse the repository at this point in the history
Instead of finding one node and using that, now it has to find 4 consecutive nodes for it to use the cache,
it yealds much more better results than before that was even going backwards
  • Loading branch information
xescugc committed Mar 19, 2024
1 parent 33a5fc4 commit bb151ea
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion utils/graph/astar.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (g *Graph) AStar(sx, sy int, d utils.Direction, tx, ty int, atScale bool) [
current.open = false
current.closed = true

if current.step.Node.ID == tn.ID || current.step.Node.NextStep != nil {
if current.step.Node.ID == tn.ID || checkConsecutiveSteps(current, 4) {
if current.step.Node.NextStep != nil {
current = &queueItem{
step: *current.step.Node.NextStep,
Expand Down Expand Up @@ -217,3 +217,17 @@ func (q *queue) Pop() interface{} {
*q = old[0 : n-1]
return item
}

// checkConsecutiveSteps will check in the queueItem tree if it has
// 'c' consecutive steps before returning true or false
func checkConsecutiveSteps(qi *queueItem, c int) bool {
if qi.parent != nil && qi.step.Node.NextStep != nil && qi.parent.step.Node.NextStep != nil && qi.parent.step.Node.NextStep.Node == qi.step.Node {
c--
if c == 0 {
return true
} else {
return checkConsecutiveSteps(qi.parent, c)
}
}
return false
}

0 comments on commit bb151ea

Please sign in to comment.