Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Queue<Vector2Int> getPathToDestination(Vector2Int destinationCoordinates) {
- //Prepare array of tile costs for pathfinding; set default value to maximum
- int uncheckedTileValue = int.MaxValue;
- int[][] tileCosts = new int[Map._Instance.Width][];
- for (int x = 0; x < Map._Instance.Width; x++) {
- tileCosts[x] = new int[Map._Instance.Height];
- for (int y = 0; y < Map._Instance.Height; y++) {
- tileCosts[x][y] = uncheckedTileValue;
- }
- }
- //Prepare to begin pathfinding: starting with unit's current location, calculate out the movement costs for each tile
- tileCosts[location.x][location.y] = 0;
- Queue<Vector2Int> currentNeighbors = new Queue<Vector2Int>();
- Queue<Vector2Int> nextNeighbors = new Queue<Vector2Int>();
- //Initialize cycle with starting neighbors
- Vector2Int[] startNeighborCoords = MapCoordinateUtility.getNeighborCoordsOfLocation(destinationCoordinates);
- foreach (Vector2Int startNeighbor in startNeighborCoords) {
- if (canAccessTile(startNeighbor)) {
- Debug.Log("Start Neighbor: "+startNeighbor.ToString());
- nextNeighbors.Enqueue(startNeighbor);
- }
- }
- int currentDifficulty = 1;
- while (nextNeighbors.Count > 0) {
- Debug.Log("Calculating difficulties for step: "+currentDifficulty);
- //Check for break condition: path to destination already found
- if (tileCosts[destinationCoordinates.x][destinationCoordinates.y] != uncheckedTileValue) {
- Debug.Log("Difficulty for destination has been found: "+ tileCosts[destinationCoordinates.x][destinationCoordinates.y]);
- break;
- }
- //Shift next neighbors over to current neighbors
- currentNeighbors = nextNeighbors;
- nextNeighbors = new Queue<Vector2Int>();
- while (currentNeighbors.Count > 0) {
- //Notate current search's difficulty
- Vector2Int currentSearchLocation = currentNeighbors.Dequeue();
- Debug.Log("Notating difficulty for " + currentSearchLocation.ToString());
- if (tileCosts[currentSearchLocation.x][currentSearchLocation.y] == uncheckedTileValue) {
- tileCosts[currentSearchLocation.x][currentSearchLocation.y] = currentDifficulty;
- }
- //Check for adjacent unchecked tiles
- Vector2Int[] foundNextNeighbors = MapCoordinateUtility.getNeighborCoordsOfLocation(currentSearchLocation);
- foreach (Vector2Int nextNeighbor in foundNextNeighbors) {
- if (!canAccessTile(nextNeighbor)) {
- //Tile not navigable; ignore
- continue;
- } else if (tileCosts[nextNeighbor.x][nextNeighbor.y] != uncheckedTileValue) {
- //Value was already visited; do not add to queue
- continue;
- } else {
- nextNeighbors.Enqueue(nextNeighbor);
- }
- }
- }
- //Increase the difficulty of future notations before proceeding to the next search ring
- currentDifficulty++;
- }
- //Check if destination has a real difficulty; return null if path not found
- if (tileCosts[destinationCoordinates.x][destinationCoordinates.y] == uncheckedTileValue) {
- return null;
- }
- //Construct path, searching from the destination back to the source
- LinkedList<Vector2Int> path = new LinkedList<Vector2Int>();
- path.AddFirst(destinationCoordinates);
- while (!location.Equals(path.First.Value)) {
- int currentPathDistance = tileCosts[path.First.Value.x][path.First.Value.y];
- Vector2Int[] potentialNextSteps = MapCoordinateUtility.getNeighborCoordsOfLocation(path.First.Value);
- foreach (Vector2Int potentialNextStep in potentialNextSteps) {
- if (!Map._Instance.isTileInMap(potentialNextStep)) {
- //Neighbor location is not within the map; skip
- continue;
- } else if (tileCosts[potentialNextStep.x][potentialNextStep.y] == uncheckedTileValue) {
- //This tile was not on the list of potential paths; skip
- continue;
- } else if (tileCosts[potentialNextStep.x][potentialNextStep.y] < currentPathDistance) {
- path.AddFirst(potentialNextStep);
- break;
- }
- }
- }
- //Load path into queue
- Queue<Vector2Int> foundPath = new Queue<Vector2Int>();
- foreach (Vector2Int step in path) {
- foundPath.Enqueue(step);
- }
- return foundPath;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement