From 36b7778619b3765b04d0cb2453663fc461b467ff Mon Sep 17 00:00:00 2001 From: Simon O'Shea Date: Wed, 9 Aug 2023 10:10:21 -0400 Subject: [PATCH] animation works in logan GUI - terribly inefficient --- .../Assets/Scripts/Pathfinder.cs | 50 +++++++------------ .../Assets/UI Elements/UI/Scripts/nMain.cs | 31 +++++++++--- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs index d19ba73..229badc 100644 --- a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs +++ b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs @@ -80,7 +80,7 @@ public class Pathfinder Debug.Log("GO!"); open.Add(initialState); - int cap = 1000; + int cap = 2000; while (true && cap > 0) { if (!open.Any()) @@ -99,9 +99,6 @@ public class Pathfinder { Debug.Log("Gottem"); - - //Node n = FindLastSample(open); - while (currentNode.parent != null) { solution.Push(currentNode); @@ -115,10 +112,10 @@ public class Pathfinder List children = currentNode.expand(-1); if (heuristic == 0) - h0(children); - /* else if (heuristic.Equals("h1")) + h2(children);/* + else if (heuristic == 1) h1(children); - else if (heuristic.Equals("h2")) + else if (heuristic == 2) h2(children);*/ foreach (Node child in children) @@ -127,23 +124,10 @@ public class Pathfinder } cap--; } - + Debug.Log("too much work :("); return solution; } -/* private Node GetLowestNode(List NodeList) - { - Node lowest = NodeList[0]; - for (int i = 1; i < NodeList.Count; i++) - { - if (NodeList[i].fn < lowest.fn) - { - lowest = NodeList[i]; - } - } - return lowest; - }*/ - // HEURISTICS FOR ASTAR public static List h0(List children) { @@ -161,40 +145,40 @@ public class Pathfinder return sorted; } -/* public static List h1(List children) + public static List h1(List children) { - Queue sorted = new PriorityQueue<>(new NodeComparator()); + List sorted = new List(); - for (int i = 0; i < children.size(); i++) + for (int i = 0; i < children.Count; i++) { // estimated moves left: samples left - children.get(i).heuristic = children.get(i).samples.size(); - sorted.add(children.get(i)); + children[i].heuristic = children[i].samples.Count; + sorted.Add(children[i]); } return children; } - public static Queue h2(List children) + public static List h2(List children) { - Queue sorted = new PriorityQueue<>(new NodeComparator()); + List sorted = new List(); - if (children.size() == 0) + if (children.Count() == 0) return sorted; Node child; - for (int i = 0; i < children.size(); i++) + for (int i = 0; i < children.Count(); i++) { // estimated moves left: // distance to nearest sample + 1 (+1 to account for sample action) - child = children.get(i); + child = children[i]; child.heuristic = Node.distance(child.agent, child.nearestSample()) + 1; - sorted.add(child); + sorted.Add(child); } return sorted; - }*/ + } } public class Node diff --git a/Pathfinding Visualizer/Assets/UI Elements/UI/Scripts/nMain.cs b/Pathfinding Visualizer/Assets/UI Elements/UI/Scripts/nMain.cs index 30f3b49..f6bb494 100644 --- a/Pathfinding Visualizer/Assets/UI Elements/UI/Scripts/nMain.cs +++ b/Pathfinding Visualizer/Assets/UI Elements/UI/Scripts/nMain.cs @@ -27,9 +27,9 @@ public class nMain : MonoBehaviour void Start() { // Create world - int width = 32; - int height = 16; - float cellSize = 5f; + int width = 16; + int height = 8; + float cellSize = 10f; Vector3 origin = new Vector3(-77, -34); world = new LogicGrid(width, height, cellSize, origin); @@ -107,11 +107,15 @@ public class nMain : MonoBehaviour public void ResetGrid() { world.reset(); + agent = new Vector2Int(); + obstacles = new List(); + samples = new List(); } public void StartAlgorithm() { Vector2Int coord = new Vector2Int(); + bool agentFound = false; // Collect information about world for (int x = 0; x < world.GetWidth(); x++) @@ -120,7 +124,7 @@ public class nMain : MonoBehaviour int index = x * world.GetHeight() + y; int value = world.GetValue(x, y); - Debug.Log("x: " + x + " y: " + y + " | value: " + value); + //Debug.Log("x: " + x + " y: " + y + " | value: " + value); // Add coordinate to obstacle array if (value == 1) @@ -140,18 +144,33 @@ public class nMain : MonoBehaviour if (!samples.Contains(coord)) samples.Add(coord); } - // Add coordinate to obstacle array + // Add coordinate to agent array if (value == 3) { agent.x = x; agent.y = y; + agentFound = true; } } + if (!agentFound) + { + Debug.Log("NO AGENT!"); + return; + } + + if (samples.Count == 0) + { + Debug.Log("NO SAMPLES!"); + return; + } if (true) { - Pathfinder astar = new Pathfinder(world, agent, obstacles, samples, algorithm, 0); + Pathfinder path = new Pathfinder(world, agent, obstacles, samples, 0, 0); + Stack result = path.result; + Debug.Log("GO ANIMATE"); + cellMesh.Animate(result); } }