/// <summary> /// Retrieve List of passable adjacent Tiles (occupation check included) /// </summary> /// <param name="map"></param> /// <param name="x"></param> /// <param name="z"></param> /// <param name="y"></param> /// <returns></returns> public static List <Tile> GetAdjTilesWithOccCheck(Map map, int x, int z, int y) { List <Tile> result = new List <Tile>(); string basepointHash = MapUtils.GetTileHash(x, z, y); Tile basePointTile; if (map.mapTileDict.TryGetValue(basepointHash, out basePointTile)) { for (int i = 0; i < 14; i++) { int[] indecies2 = map.GetTileIndexesByAdjIndex(i, x, z, y); string adjPointHash = GetTileHash(indecies2[0], indecies2[1], indecies2[2]); string adjDictHash = MapUtils.FormMapHash(x, y, z, indecies2[0], indecies2[2], indecies2[1]); Tile adjPoint; bool hit = false; if (map.mapTileDict.TryGetValue(adjPointHash, out adjPoint)) { if (map.mapAdjDict.TryGetValue(adjDictHash, out hit)) { // Check if something is standing on tile if (!adjPoint.occupied) { result.Add(adjPoint); } } } } } return(result); }
public static List <Tile> FindPath(Map map, Tile start, Tile goal) { List <Tile> path = new List <Tile>(); SimplePriorityQueue <Tile> frontier = new SimplePriorityQueue <Tile>(); Dictionary <Tile, Tile> cameFrom = new Dictionary <Tile, Tile>(); Dictionary <Tile, int> costSoFar = new Dictionary <Tile, int>(); frontier.Enqueue(start, 0); cameFrom[start] = start; costSoFar[start] = 0; Tile currentTile = new Tile(); while (frontier.Count > 0) { currentTile = frontier.Dequeue(); if (currentTile.Equals(goal)) { break; } foreach (Tile next in MapUtils.GetAdjTilesWithOccCheck(map, currentTile.x, currentTile.z, currentTile.y)) { int newCost; newCost = costSoFar[currentTile] + 1; if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next]) { costSoFar[next] = newCost; int priority = newCost + ManhattanDistance(next, goal); frontier.Enqueue(next, priority); cameFrom[next] = currentTile; } // if (costSoFar.TryGetValue(currentTile, out costSoFarCurrent)) // { // newCost = costSoFarCurrent + 1; // +1 because cost to move between all tiles is 1 // if (true) //change! // { // costSoFar.Add(next, newCost); // int priority = newCost + ManhattanDistance(next, goal); // frontier.Enqueue(next, priority); // cameFrom.Add(next, currentTile); // } // } // int newCost = } } while (!currentTile.Equals(start)) { path.Add(currentTile); currentTile = cameFrom[currentTile]; } path.Add(start); path.Reverse(); path.RemoveAt(0); return(path); }