diff --git a/Game1/Sources/Pathing/A-Star/Astar.cs b/Game1/Sources/Pathing/A-Star/Astar.cs index e046110..ca41ca7 100644 --- a/Game1/Sources/Pathing/A-Star/Astar.cs +++ b/Game1/Sources/Pathing/A-Star/Astar.cs @@ -67,6 +67,7 @@ class Astar Path path = new Path(); PriorityQueue openList = new PriorityQueue(); PriorityQueue closedList = new PriorityQueue(); + Nodes target = new Nodes(targetPos); Nodes startPos = new Nodes (tractorPos); Nodes current = null; int g = 0; @@ -79,7 +80,7 @@ class Astar closedList.Enqueue(current); openList.Dequeue(); - if (closedList.Peek().getCords() == targetPos) + if (closedList.Peek().getCords() == target.getCords()) break; var adjacentNodes = GetAdjacentNodes(current.getCords()); @@ -92,7 +93,7 @@ class Astar if (!(openList.Exists(adjacentNode.getCords()))) { adjacentNode.setG(g); - adjacentNode.setH(ComputeHScore(adjacentNode.getCords(), targetPos)); + adjacentNode.setH(ComputeHScore(adjacentNode.getCords(), target.getCords())); adjacentNode.calculateF(); adjacentNode.setParent(current); openList.Enqueue(adjacentNode); @@ -109,7 +110,7 @@ class Astar } } - while (current.getParent() != null) + while (current != null) { path.AddNode(current); current = current.getParent(); diff --git a/Game1/Sources/Pathing/A-Star/PathSaver/PriorityQueue.cs b/Game1/Sources/Pathing/A-Star/PathSaver/PriorityQueue.cs index 603d552..6dfa0a4 100644 --- a/Game1/Sources/Pathing/A-Star/PathSaver/PriorityQueue.cs +++ b/Game1/Sources/Pathing/A-Star/PathSaver/PriorityQueue.cs @@ -41,8 +41,9 @@ class PriorityQueue { Nodes min = Peek(); Nodes root = list[Count - 1]; - list.RemoveAt(Count - 1); + list.RemoveAt(0); + int i = 0; while (i * 2 + 1 < Count) { @@ -56,6 +57,7 @@ class PriorityQueue } if (Count > 0) list[i] = root; + } public Nodes Peek() diff --git a/Game1/Sources/Smart/SmartTractor.cs b/Game1/Sources/Smart/SmartTractor.cs index 5046641..ea35145 100644 --- a/Game1/Sources/Smart/SmartTractor.cs +++ b/Game1/Sources/Smart/SmartTractor.cs @@ -20,7 +20,6 @@ class SmartTractor //What to do next public Path returnChoice(int task) { - astar.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target); if (task == 0) { //To the house @@ -31,6 +30,7 @@ class SmartTractor //To the fields getTargetPosition(r.Next(0, (int)Size.X), r.Next(0, (int)Size.Y)); } + astar.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target/(tileSize+Spacing)); //astar.FindPath(); return astar.FindPath(); }