public static List<Tile> GetAdjacentLegalTiles(Map map, Tile currentTile) { List<Tile> adjacentTiles = new List<Tile>(); int x = currentTile.X; int y = currentTile.Y; if (x - 1 >= 0) { Tile left = map.map[x - 1][y]; if (!left.IsImpassible) { adjacentTiles.Add(left); } } if (x + 1 < map.Size[0]) { Tile right = map.map[x + 1][y]; if(!right.IsImpassible) { adjacentTiles.Add(right); } } if(y - 1 >= 0) { Tile up = map.map[x][y - 1]; if(!up.IsImpassible) adjacentTiles.Add(up); } if(y + 1 < map.Size[1]) { Tile down = map.map[x][y + 1]; if(!down.IsImpassible) { adjacentTiles.Add(down); } } return adjacentTiles; }
public static int GetFCost(Map map, Tile tile, Tile destination) { int GCost = 0; bool root = false; Tile test = tile; while (!root) { GCost += test.MoveCost; if (test.IsRoot) { root = true; } else { test = test.parentTile; } } int HCost = (GetManhattanDistance(tile, destination)); return GCost + HCost; }
public Battle(Map newMap) { BattleMap = newMap; Units = new List<Unit>(); TurnCount = 1; MoveMode = false; AttackMode = false; MoveEnabled = true; AttackEnabled = true; for (int x = 0; x < newMap.Size[0]; x++) { for (int y = 0; y < newMap.Size[1]; y++) { if (newMap.map[x][y].hasUnit) { Units.Add(newMap.map[x][y].TileUnit); } } } }
//MAP FILE: A PRIMER //Map files are plaintext files encoded with data from which we can load maps //The format for the map file is as follows (omit the <> when writing): //[Start of File] //<x size> //<y size> //<tile set number> //<music number> //<x coordinate>,<y coordinate>,<height>,<unit number (see below)>,<player number[0 or 1]>,<impassible[0 or 1]>,<move cost>,<type> //[repeat for each tile] //[end of file] public static Map LoadMap(string filename) { StreamReader sr = new StreamReader(filename); string file = sr.ReadToEnd(); string[] lines = file.Split(new string[]{Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries); int sizeX = Int32.Parse(lines[0]); int sizeY = Int32.Parse(lines[1]); int tileSet = Int32.Parse(lines[2]); int music = Int32.Parse(lines[3]); int linecount = 4; Map finalMap = new Map(new int[]{sizeX, sizeY}, tileSet, music); for (int i = 4; i < lines.Length; i++) { linecount++; Tile newTile = new Tile(); string[] tileData = lines[i].Split(','); int X = Int32.Parse(tileData[0]); int y = Int32.Parse(tileData[1]); int height = Int32.Parse(tileData[2]); int unit = Int32.Parse(tileData[3]); int PlayerUnit = Int32.Parse(tileData[4]); int impassible = Int32.Parse(tileData[5]); int moveCost = Int32.Parse(tileData[6]); newTile.MoveCost = moveCost; string type = tileData[7]; newTile.type = type; newTile.X = X; newTile.Y = y; newTile.Height = height; if (impassible == 0) { newTile.IsImpassible = false; } else { newTile.IsImpassible = true; } Unit tileUnit; newTile.hasUnit = true; //the following is a list of unit codes, use -1 for no unit switch (unit) { case 0: tileUnit = new Soldier(); break; case 1: tileUnit = new Defender(); break; case 2: tileUnit = new Cavalry(); break; case 3: tileUnit = new Ranger(); break; case 4: tileUnit = new Mage(); break; case 5: tileUnit = new Artillery(); break; case 6: tileUnit = new Scout(); break; case 7: tileUnit = new Bomber(); break; case 8: tileUnit = new Fighter(); break; default: tileUnit = null; newTile.hasUnit = false; break; } if (newTile.hasUnit) { newTile.IsImpassible = true; if (PlayerUnit == 0) { tileUnit.isPlayerUnit = true; } else { tileUnit.isPlayerUnit = false; } newTile.TileUnit = tileUnit; newTile.IsImpassible = true; tileUnit.Location[0] = newTile.X; tileUnit.Location[1] = newTile.Y; } else { newTile.TileUnit = null; } newTile.AssignFileName(); finalMap.map[X][y] = newTile; if (newTile.type == "water") { newTile.IsImpassible = true; } } sr.Close(); return finalMap; }
//runs path algorithm and checks if there is //path between two points public static bool PathExists(Tile startTile, Tile endTile, Map map, List<Tile> rangeSet, int speed) { //HOLY SHIT, A WORKING A* ALGORITHM startTile.IsRoot = true; List<Tile> open = new List<Tile>(); List<Tile> closed = new List<Tile>(); open.Add(startTile); if (startTile == endTile) { return false; } Tile current = new Tile(); while (open.Count > 0) { int LowestFScore = 10000; for (int i = 0; i < open.Count; i++ ) { if (open.ElementAt(i).FScore < LowestFScore) { LowestFScore = open.ElementAt(i).FScore; current = open.ElementAt(i); } } open.Remove(current); closed.Add(current); List<Tile> adjacent = GetAdjacentLegalTiles(map, current); List<Tile> adjacentTemp = new List<Tile>(); foreach (Tile adj in adjacent) { //if(rangeSet.Contains(adj)) //{ adjacentTemp.Add(adj); //} if (closed.Contains(adj)) { adjacentTemp.Remove(adj); } } adjacent = adjacentTemp; foreach (Tile tile in adjacent) { if (!open.Contains(tile)) { open.Add(tile); } tile.parentTile = current; tile.GScore = GetGScore(tile); tile.HScore = GetManhattanDistance(current, endTile); tile.FScore = tile.GScore + tile.HScore; if (open.Contains(tile)) { Tile test = new Tile(); foreach (Tile o in open) { if (o == tile) { test = o; } } if (tile.GScore < test.GScore) { test.parentTile = current; test.GScore = GetGScore(test); test.FScore = test.GScore + test.HScore; } } } if(closed.Contains(endTile)) { List<Tile> path = BuildPath(endTile); if (Reachable(path, speed)) { return true; } else { return false; } } } return false; }
//same as above, only returns the path public static List<Tile> GetPath(Tile startTile, Tile endTile, Map map) { //HOLY SHIT, ANOTHER WORKING A* ALGORITHM startTile.IsRoot = true; List<Tile> open = new List<Tile>(); List<Tile> closed = new List<Tile>(); open.Add(startTile); Tile current = new Tile(); while (open.Count > 0) { int LowestFScore = 10000; for (int i = 0; i < open.Count; i++ ) { if (open.ElementAt(i).FScore < LowestFScore) { LowestFScore = open.ElementAt(i).FScore; current = open.ElementAt(i); } } open.Remove(current); closed.Add(current); List<Tile> adjacent = GetAdjacentLegalTiles(map, current); List<Tile> adjacentTemp = new List<Tile>(); foreach (Tile adj in adjacent) { adjacentTemp.Add(adj); if (closed.Contains(adj)) { adjacentTemp.Remove(adj); } } adjacent = adjacentTemp; foreach (Tile tile in adjacent) { if (!open.Contains(tile)) { open.Add(tile); } tile.parentTile = current; tile.GScore = GetGScore(tile); tile.HScore = GetManhattanDistance(current, endTile); tile.FScore = tile.GScore + tile.HScore; if (open.Contains(tile)) { Tile test = new Tile(); foreach (Tile o in open) { if (o == tile) { test = o; } } if (tile.GScore < test.GScore) { test.parentTile = current; test.GScore = GetGScore(test); test.FScore = test.GScore + test.HScore; } } } if(closed.Contains(endTile)) { List<Tile> path = BuildPath(endTile); return path; } } return new List<Tile>(); }