public static void parseMap(Dictionary<Object, Object> serverData) { try { if (!(serverData.ContainsKey("map"))) mainProcessor.doDie("serverData does not contain map data. Cannot parse."); //make new map object for new data. Dictionary<Tuple<int, int>, tile> newMapData = new Dictionary<Tuple<int,int>,tile>(); Dictionary<Object, Object> mapData = JsonConvert.DeserializeObject<Dictionary<Object, Object>>(Convert.ToString(serverData["map"])); int yLength = Convert.ToInt32(mapData["j-length"]); int xLength = Convert.ToInt32(mapData["k-length"]); string actualMapData = Convert.ToString(mapData["data"]); //parse through the raw map data received from the server. int splitIndex = 0; for (int currColumn = 1; currColumn < (yLength + 1); currColumn++) { for (int currRow = 1; currRow < (xLength + 1); currRow++) { splitIndex++; //get current tile string tileType = mainProcessor.Split(mainProcessor.Split(actualMapData, "\"", splitIndex), "\"", 0); tile currentTile = new tile(currColumn, currRow, tiles.tileTypes[tileType]); //add tile to new map Tuple<int, int> tileCoordinates = new Tuple<int, int>(currColumn, currRow); newMapData.Add(tileCoordinates, currentTile); splitIndex++; //jump to correct split point } } //map is now parsed. mainProcessor.game_map = new map(xLength, yLength, newMapData); mainProcessor.sendInfo("Map has been parsed."); } catch (Exception ex) { mainProcessor.doDie("Unable to parse map. " + ex.Message); } }
public void pathFinding_process(map processMap, tile initPos, tile targetPos) { path_currentTile = initPos.tile_pos; path_lastTile = path_currentTile; Boolean path_found = false; Boolean dokill = false; List<movements> availableActions = new List<movements>() { movements.UP, movements.DOWN, movements.RIGHT_UP, movements.RIGHT_DOWN, movements.LEFT_UP, movements.LEFT_DOWN}; /* Movement costs: * up * 14 10 14 * \ / * | * / \ * 14 10 14 * down */ while ((!path_found) && (!dokill)) { Dictionary<movements, Tuple<int, int>> availableDirections = new Dictionary<movements, Tuple<int, int>>() { {movements.UP, new Tuple<int, int>(path_currentTile.Item1 - 1, path_currentTile.Item2)}, {movements.DOWN, new Tuple<int, int>(path_currentTile.Item1 + 1, path_currentTile.Item2)}, {movements.LEFT_UP, new Tuple<int, int>(path_currentTile.Item1 + 1, path_currentTile.Item2 + 1)}, {movements.LEFT_DOWN, new Tuple<int, int>(path_currentTile.Item1 - 1, path_currentTile.Item2 + 1)}, {movements.RIGHT_DOWN, new Tuple<int, int>(path_currentTile.Item1 + 1, path_currentTile.Item2 - 1)}, {movements.RIGHT_DOWN, new Tuple<int, int>(path_currentTile.Item1 - 1, path_currentTile.Item2 - 1)} }; //remove invalid tiles foreach (KeyValuePair<movements, Tuple<int, int>> pair in availableDirections) { //remove invalid tiles if (!processMap.map_data.ContainsKey(pair.Value)) { path_blackList.Add(pair.Value); availableActions.Remove(pair.Key); Console.WriteLine("[PathFinder] - Removed invalid tile at (" + Convert.ToString(pair.Value.Item1) + "," + Convert.ToString(pair.Value.Item2) + ")"); } //remove blacklisted tiles if (path_blackList.Contains(pair.Value)) { availableActions.Remove(pair.Key); Console.WriteLine("[PathFinder] - Removed blacklisted tile at (" + Convert.ToString(pair.Value.Item1) + "," + Convert.ToString(pair.Value.Item2) + ")"); } //remove unwalkable tiles tile analyzeTile = processMap.map_data[pair.Value]; if ((analyzeTile.tile_type == tiles.tileType.VOID) || (analyzeTile.tile_type == tiles.tileType.SPAWN) || (analyzeTile.tile_type == tiles.tileType.ROCK)) { availableActions.Remove(pair.Key); Console.WriteLine("[PathFinder] - Removed unwalkable tile at (" + Convert.ToString(pair.Value.Item1) + "," + Convert.ToString(pair.Value.Item2) + ")"); } } /* FIND TILES */ //prioritize y axis before x axis. Boolean foundYTile = true; //is set to false if not Boolean foundXTile = false; if (targetPos.tile_pos.Item1 > path_currentTile.Item1) { //target y axis is greater. //following moves should be made: up, left_up, right_up if (availableDirections.ContainsKey(movements.UP)) { Console.WriteLine("[PathFinder] - Found walkable UP tile (" + Convert.ToString(path_currentTile.Item1 - 1) + "," + Convert.ToString(path_currentTile.Item2) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 - 1, path_currentTile.Item2); } else if (availableDirections.ContainsKey(movements.LEFT_UP)) { Console.WriteLine("[PathFinder] - Found walkable LEFT UP tile (" + Convert.ToString(path_currentTile.Item1 - 1) + "," + Convert.ToString(path_currentTile.Item2 + 1) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 - 1, path_currentTile.Item2 + 1); } else if (availableDirections.ContainsKey(movements.RIGHT_UP)) { Console.WriteLine("[PathFinder] - Found walkable RIGHT UP tile (" + Convert.ToString(path_currentTile.Item1 - 1) + "," + Convert.ToString(path_currentTile.Item2 - 1) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 - 1, path_currentTile.Item2 - 1); } else { foundYTile = false; } path_current.Add(path_currentTile); path_lastTile = path_currentTile; } else if (targetPos.tile_pos.Item1 < path_currentTile.Item1) { //target y axis is lower. //following moves should be made: down, left_down, right_down if (availableDirections.ContainsKey(movements.DOWN)) { Console.WriteLine("[PathFinder] - Found walkable DOWN tile (" + Convert.ToString(path_currentTile.Item1 + 1) + "," + Convert.ToString(path_currentTile.Item2) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 + 1, path_currentTile.Item2); } else if (availableDirections.ContainsKey(movements.LEFT_UP)) { Console.WriteLine("[PathFinder] - Found walkable LEFT DOWN tile (" + Convert.ToString(path_currentTile.Item1 + 1) + "," + Convert.ToString(path_currentTile.Item2 + 1) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 + 1, path_currentTile.Item2 + 1); } else if (availableDirections.ContainsKey(movements.RIGHT_UP)) { Console.WriteLine("[PathFinder] - Found walkable RIGHT DOWN tile (" + Convert.ToString(path_currentTile.Item1 + 1) + "," + Convert.ToString(path_currentTile.Item2 - 1) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 + 1, path_currentTile.Item2 - 1); } else { foundYTile = false; } path_current.Add(path_currentTile); path_lastTile = path_currentTile; } if (!foundYTile) { //try to find XTILE if (targetPos.tile_pos.Item2 > path_currentTile.Item2) { //target x axis is greater. //following moves should be made: left up, left down, down, up if (availableDirections.ContainsKey(movements.LEFT_UP)) { Console.WriteLine("[PathFinder] - Found walkable LEFT UP tile (" + Convert.ToString(path_currentTile.Item1 - 1) + "," + Convert.ToString(path_currentTile.Item2 + 1) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 - 1, path_currentTile.Item2 + 1); } else if (availableDirections.ContainsKey(movements.LEFT_DOWN)) { Console.WriteLine("[PathFinder] - Found walkable LEFT DOWN tile (" + Convert.ToString(path_currentTile.Item1 + 1) + "," + Convert.ToString(path_currentTile.Item2 + 1) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 + 1, path_currentTile.Item2 + 1); } else if (availableDirections.ContainsKey(movements.RIGHT_UP)) { Console.WriteLine("[PathFinder] - Found walkable RIGHT UP tile (" + Convert.ToString(path_currentTile.Item1 - 1) + "," + Convert.ToString(path_currentTile.Item2 - 1) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 - 1, path_currentTile.Item2 - 1); } else { foundXTile = false; } path_current.Add(path_currentTile); path_lastTile = path_currentTile; } else if (targetPos.tile_pos.Item2 < path_currentTile.Item2) { //target x axis is lower.right up, right down, down, up if (availableDirections.ContainsKey(movements.RIGHT_UP)) { Console.WriteLine("[PathFinder] - Found walkable RIGHT UP tile (" + Convert.ToString(path_currentTile.Item1 - 1) + "," + Convert.ToString(path_currentTile.Item2 - 1) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 - 1, path_currentTile.Item2 - 1); } else if (availableDirections.ContainsKey(movements.RIGHT_DOWN)) { Console.WriteLine("[PathFinder] - Found walkable RIGHT DOWN tile (" + Convert.ToString(path_currentTile.Item1 + 1) + "," + Convert.ToString(path_currentTile.Item2 - 1) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 + 1, path_currentTile.Item2 - 1); } else if (availableDirections.ContainsKey(movements.DOWN)) { Console.WriteLine("[PathFinder] - Found walkable DOWN tile (" + Convert.ToString(path_currentTile.Item1 + 1) + "," + Convert.ToString(path_currentTile.Item2) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 + 1, path_currentTile.Item2); } else if (availableDirections.ContainsKey(movements.UP)) { Console.WriteLine("[PathFinder] - Found walkable UP tile (" + Convert.ToString(path_currentTile.Item1 - 1) + "," + Convert.ToString(path_currentTile.Item2) + ")"); path_currentTile = new Tuple<int, int>(path_currentTile.Item1 - 1, path_currentTile.Item2); } else { foundXTile = false; } path_current.Add(path_currentTile); path_lastTile = path_currentTile; } } if ((!foundYTile) && (!foundXTile)) { if (path_current.Contains(path_currentTile)) path_current.Remove(path_currentTile); path_blackList.Add(path_currentTile); path_currentTile = path_lastTile; } } }