public Dijkstra(int gridSize, Map map) { Name = "Dijkstra"; profile = new Profile("Dijkstra Path Time"); GridSize = gridSize; nodes = new NodeCollection(gridSize); path = new List<Coord2>(); this.map = map; }
/// <summary> /// Create a new Dijkstra pathfinding object. /// </summary> /// <param name="gridSize">The size of the grid being searched for paths.</param> /// <param name="map">The map being used.</param> public Dijkstra(int gridSize, Map map) { Name = "Dijkstra"; GridSize = gridSize; nodes = new NodeCollection(gridSize); path = new List<Coord2>(); pathConnectors = new List<Line>(); this.map = map; }
public void Build(Coord2 startPos, Coord2 targetPos) { if(InputHandler.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.Enter)) { this.startPos = startPos; this.targetPos = targetPos; path = new List<Coord2>(); nodes = new NodeCollection(GridSize); // Initialize bot position nodes.Get(startPos.X, startPos.Y).cost = 0; bool firstLoop = true; while (nodes.Get(targetPos.X, targetPos.Y).closed == false) { if (firstLoop) { currentLowestPos = startPos; firstLoop = false; } else FindLowestCost(); // Find lowest cost // Mark lowest cost as closed nodes.Get(currentLowestPos.X, currentLowestPos.Y).closed = true; // Find the neigbour positions Coord2[] neighbours = GetNeighbours(currentLowestPos); // Recalculate Costs RecalculateCosts(neighbours, currentLowestPos); } // Trace the completed path TracePath(); } }
public AStar(int gridSize, Map map) : base(gridSize, map) { nodes = new NodeCollection(gridSize); Name = "A Star"; }
/// <summary> /// Builds a new path between two given locations. /// </summary> /// <param name="startPos">The starting position.</param> /// <param name="targetPos">The target position.</param> /// <param name="testMode">Whether or not the algorithm is being run in testing mode, or if it is live within a map visualization.</param> public override void Build(Coord2 startPos, Coord2 targetPos, bool testMode = false) { path = new List<Coord2>(); nodes = new NodeCollection(GridSize); searchedNodes = 0; this.start = nodes.Get(startPos); this.target = nodes.Get(targetPos); // Initialize bot position nodes.Get(startPos).cost = 0; bool firstLoop = true; while (!target.closed) { if (firstLoop) { currentLowest = start; firstLoop = false; } else FindLowestCost(); // Find lowest cost // Mark lowest cost as closed currentLowest.closed = true; map.SetRenderColor(currentLowest.position, CLOSED_COLOR); // Find the neigbour positions List<Node> neighbours = GetNeighours(currentLowest); // Update visualization UpdateVisualization(neighbours); // Recalculate Costs RecalculateCosts(neighbours, currentLowest); // Update number of searched nodes searchedNodes++; } // Trace the completed path, if target has been found if(target.parent != null) TracePath(); }