public List <Cell> FindPath(Vector2Int start, Vector2Int goal) { Cell startCell = _cells[start.x, start.y]; Cell goalCell = _cells[goal.x, goal.y]; return(_pathfinder.AStar(startCell, goalCell)); }
private void AdvanceStage() { if (_stage == Stage.Start) { _stage = Stage.PlottingBlock; ResetEditor(); } else if (_stage == Stage.PlottingBlock) { _stage = Stage.PlottingStart; } else if (_stage == Stage.PlottingStart) { _stage = Stage.PlottingEnd; } else if (_stage == Stage.PlottingEnd) { _stage = Stage.Run; } else if (_stage == Stage.Run) { _stage = Stage.Start; AdvanceStage(); return; } switchStage: switch (_stage) { case Stage.PlottingBlock: helper.text = "Click on any square to toggle it (If you create a block path, the creator will force you to repeat)\nPress space to plot a starting point"; break; case Stage.PlottingStart: helper.text = "Click on any square to place a start position\nPress space to plot an ending point"; break; case Stage.PlottingEnd: helper.text = "Click on any square to place an end position\nPress space to run the pathfinder"; break; case Stage.Run: _path = _pather.AStar(_tempStart, _tempEnd); if (_path.Count == 0) { _stage = Stage.PlottingBlock; goto switchStage; } helper.text = "Press Enter to Play\nPress Space to Repath"; if (_path.Count > 0) { _line.positionCount = _path.Count; StartCoroutine(RenderLine()); } break; } }
// Update is called once per frame void Update() { _validConnections.Clear(); var cities = GetCities(); var generators = Tiles.Where(t => t is Generator).ToList(); foreach (var generator in generators) { foreach (var city in cities) { List <Tile> possiblePath; List <Tile> excludeTiles = new List <Tile>(); do { possiblePath = Pathfinder.AStar(Tiles, generator, city, Heuristic, GetNeighbors, excludeTiles); if (possiblePath != null) { _validConnections.Add(possiblePath); excludeTiles.AddRange(possiblePath.GetRange(1, possiblePath.Count - 1)); } } while (possiblePath != null); } } }
private void Update() { if (canSetWalls == true) { SetBlocks(); } else { GetNeighbors(); } if (canSetStartAndGoal == true) { SetStartAndGoal(); } if (startPathfinder == true) { if (pathfinder.pathfinderMode == Mode.BreadthFirstSearch) { StartCoroutine(pathfinder.BFS(nodes[yStart, xStart], nodes[yGoal, xGoal])); } else if (pathfinder.pathfinderMode == Mode.Dijkstra) { StartCoroutine(pathfinder.Dijkstra(nodes[yStart, xStart], nodes[yGoal, xGoal])); } else if (pathfinder.pathfinderMode == Mode.AStar) { StartCoroutine(pathfinder.AStar(nodes[yStart, xStart], nodes[yGoal, xGoal])); } startPathfinder = false; } }
static public void AStarPathCanBeRun() { NodeGraph graph = new NodeGraph(); ushort start = graph.AddNode(); ushort mid = graph.AddNode(); ushort end = graph.AddNode(); graph.AddEdge(start, mid); graph.AddEdge(mid, start); graph.AddEdge(end, mid); graph.AddEdge(mid, end); graph.OptimizeEdgeOrder(); NodePath path = new NodePath(); bool bFoundPath = Pathfinder.AStar(graph, ref path, start, end); Assert.IsTrue(bFoundPath); }
private void DrawRange() { rangeOverlayMap.ClearAllTiles(); Vector3Int start = pathfinder.GetPath()[0]; int length = pathfinder.maxLength; if (length == 0) { List <Vector3Int> attackingTiles = unitController.GetAttackableTiles(unitController.GetSelectedUnit().position); DisplayCurrentAttackRange(attackingTiles); showingRange = true; return; } Dictionary <Vector3Int, RangeStatus> highlightStatuses = new Dictionary <Vector3Int, RangeStatus>(); List <Unit> unitsInRange = new List <Unit>(); for (int i = length * -1; i <= length; i++) { for (int j = length * -1; j <= length; j++) { Vector3Int dest = new Vector3Int(start.x + i, start.y + j, 0); if ((Mathf.Abs(i) + Mathf.Abs(j)) > length || unitController.CheckForUnit(dest) != null) //if true, always out of range even if there would be a direct path { continue; } if (movementGrid.GetMovementTile(dest) != null) { List <Vector3Int> testPath = pathfinder.AStar(start, dest, new List <Vector3Int> { start }); if (testPath == null) //pathfinding escaped, no path { List <Vector3Int> attackingTiles = unitController.GetAttackableTiles(unitController.GetSelectedUnit().position); DisplayCurrentAttackRange(attackingTiles); } else if (testPath.Count > 0 && testPath[testPath.Count - 1] == dest) //there is a valid path to the coordinates { List <Vector3Int> attackingTiles = unitController.GetAttackableTiles(testPath[testPath.Count - 1]); foreach (Vector3Int location in attackingTiles) { if (movementGrid.GetMovementTile(location) == null) { Debug.Log("Attack location out of bounds: " + location); continue; } //Debug.Log("Enter Attack Highlight for location: " + location); if (!highlightStatuses.ContainsKey(location)) { Unit unitAtLocation = unitController.CheckForUnit(location); if (unitAtLocation != null) { if (unitAtLocation.teamNumber == unitController.GetSelectedUnit().teamNumber) { //Debug.Log("Marked blocked by ally"); highlightStatuses.Add(location, RangeStatus.BlockedByAlly); } else { //Debug.Log("Marked blocked by enemy"); highlightStatuses.Add(location, RangeStatus.BlockedByEnemy); unitsInRange.Add(unitAtLocation); rangeOverlayMap.SetTile(location, attackTileHighlight); } } else { //Debug.Log("Marked attacking"); highlightStatuses.Add(location, RangeStatus.Attack); rangeOverlayMap.SetTile(location, attackTileHighlight); } } } foreach (Vector3Int location in testPath) { if (highlightStatuses.ContainsKey(location)) { if (highlightStatuses[location] == RangeStatus.Movement || highlightStatuses[location] == RangeStatus.BlockedByAlly || highlightStatuses[location] == RangeStatus.BlockedByEnemy) { continue; } else { highlightStatuses[location] = RangeStatus.Movement; rangeOverlayMap.SetTile(location, movementTileHighlight); } } else if (unitController.CheckForUnit(location) != null) { if (unitController.CheckForUnit(location).teamNumber == unitController.GetSelectedUnit().teamNumber) { highlightStatuses.Add(location, RangeStatus.BlockedByAlly); } } else { highlightStatuses.Add(location, RangeStatus.Movement); rangeOverlayMap.SetTile(location, movementTileHighlight); } } } } } } unitController.SetUnitsInAttackRange(unitsInRange); showingRange = true; }
private void OnNodeClick(ushort inNodeId) { Stopwatch timer = Stopwatch.StartNew(); Pathfinder.AStar(m_NodeGraph, ref m_PathBuffer, m_CurrentNode, inNodeId, Pathfinder.ManhattanDistanceHeuristic, default, Pathfinder.Flags.ReturnClosestPath);