/// <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(); } }
/// <summary> /// Updates the visualization of the algorithm. /// </summary> private void UpdateVisualization() { int highestVal = HighestValue(); int lowestVal = LowestValue(); for (int x = 0; x < gridSize; x++) { for (int y = 0; y < gridSize; y++) { map.SetRenderColor(new Coord2(x, y), Color.Lerp(Color.White, scentColor, (float)buffer1.data[x, y] / (float)highestVal)); } } }