From bb151ea2fca7c0f30686ab0350f72992d032fd2d Mon Sep 17 00:00:00 2001 From: xescugc Date: Tue, 19 Mar 2024 14:04:05 +0100 Subject: [PATCH] utils/graph: Added logic to only use the cache after consecutive nodes 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 --- utils/graph/astar.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/utils/graph/astar.go b/utils/graph/astar.go index b95f2ca..ae23d5f 100644 --- a/utils/graph/astar.go +++ b/utils/graph/astar.go @@ -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, @@ -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 +}