using System; using System.Collections.Generic; using System.Linq; namespace GwaRL { public class DungeonMap : DungeonSpace { public int[] terrain; public int[] decals; public int[] distancemap; public DungeonMap() { this.size = new int[] { 60, 60 }; this.terrain = new int[3600]; for (int i = 0; i < this.terrain.Length; i++) { this.terrain[i] = 1; } this.decals = new int[3600]; this.distancemap = new int[3600]; } public DungeonMap(int[] size) { this.size = size; this.terrain = new int[size[0]*size[1]]; for (int i = 0; i < this.terrain.Length; i++) { this.terrain[i] = 1; } this.decals = new int[size[0] * size[1]]; this.distancemap = new int[size[0] * size[1]]; } public DungeonMap(int[] size, int[] terrain) { this.size = size; this.terrain = terrain; this.decals = new int[size[0] * size[1]]; this.distancemap = new int[size[0] * size[1]]; } public DungeonMap(int[] size, int[] terrain,int[] decals) { this.size = size; this.terrain = terrain; this.decals = decals; this.distancemap = new int[size[0] * size[1]]; } public string gettile(int position) { string tile; switch (this.terrain[position]) { case 0: tile = "floor"; break; case 1: tile = "wall"; break; case 2: tile = "stairup"; break; case 3: tile = "stairdown"; break; default: tile = "none"; break; } return tile; } public void settile(int position, string tile) { switch (tile) { case "floor": this.terrain[position] = 0; break; case "wall": this.terrain[position] = 1; break; case "stairup": this.terrain[position] = 2; break; case "stairdown": this.terrain[position] = 3; break; default: this.terrain[position] = 0; break; } } public string[] getarea(int[] positions) { string[] tiles = new string[positions.Length]; for (int i=0;i getlineofsightfrom(int position) { List neartiles = this.getnearindices(position,9); List visible = new List(); for (int i=0;i 9) los = false; for (int i = 0; i < line.distance; i++) { if (line.npaths == 2) { if (this.gettile(line.path[0][i]) == "wall" && this.gettile(line.path[1][i]) == "wall") los = false; } else if (this.gettile(line.path[0][i]) == "wall") los = false; } return los; } //djikstra map public void builddistancemap(int goal) { //prepare the array. -1 denotes untreated square. for (int i = 0; i < this.terrain.Length; i++) { this.distancemap[i] = -1; } this.distancemap[goal] = 0; //take all the squares surrounding the goal/origin List edge = new List(this.getnearindices(goal,1)); int step = 1; while (edge.Count>0) { int discard = 0; int edgecount = edge.Count; for (int i = 0; i < edgecount; i++) { if (this.terrain[edge[i-discard]] != 1 && this.distancemap[edge[i-discard]] < 0) { this.distancemap[edge[i-discard]] = step; } else { edge.RemoveAt(i - discard); discard++; } } //record this here to stop it continuing to loop over the new elements it adds edgecount = edge.Count; for (int i = 0; i < edgecount; i++) { List neartiles = this.getnearindices(edge[i], 1); foreach (int tileindex in neartiles) { if (this.terrain[tileindex] != 1 && this.distancemap[tileindex] < 0 && !edge.Contains(tileindex)) { edge.Add(tileindex); } } } step++; } } public List buildpath(int origin) { int goal = new List(this.distancemap).IndexOf(0); List path = new List(); bool nopath = false; int stepsleft = this.distancemap[origin]; int currenttile = origin; if (stepsleft > 0) while (stepsleft > 0 && nopath == false) { List adjacenttiles = new List(this.getnearindices(currenttile,1)); adjacenttiles.RemoveAt(4); List tiles = new List(); List directions = new List(); List moduli = new List(); for (int i = 0; i < adjacenttiles.Count; i++) { if (this.distancemap[adjacenttiles[i]]>=0&&this.distancemap[adjacenttiles[i]] 0) { int minimum = moduli.IndexOf(moduli.Min()); currenttile = tiles[minimum]; path.Add(directions[minimum]); stepsleft--; } else nopath = true; } return path; } public List reversepath(List path) { List newpath = new List(); for (int i = path.Count-1; i >= 0; i--) { if (path[i]>0&&path[i]<10) newpath.Add(10 - path[i]); } return newpath; } } }