/// <summary> /// Link adjacent seed rooms /// </summary> private void LinkSeeds(Tile[,] map, Point p) { if (p.x < 0 || p.y < 0 || p.x >= map.GetLength(0) || p.y >= map.GetLength(1)) { return; } if (map.t(p).type != Tile.Type.ROOM) { return; } int group_id = map.t(p).group_id; Point[] p2 = GetAdjacentTiles(map, p); foreach (var pp in p2) { if (pp.x < 0 || pp.x >= map.GetLength(0) || pp.y < 0 || pp.y >= map.GetLength(1)) { continue; } if (map.t(pp).type == Tile.Type.ROOM && map.t(pp).group_id != group_id) { AllowPassage(map, p, pp); map.t(pp).group_id = group_id; LinkSeeds(map, pp); } } }
public static bool CheckConnectivity(Tile[,] map, Point source, Point target) { if (map.t(source).type == Tile.Type.IMPASSABLE) { return(false); } if (map.t(target).type == Tile.Type.IMPASSABLE) { return(false); } List <Point> visited = new List <Point>(); Queue <Point> unvisited = new Queue <Point>(); unvisited.Enqueue(source); while (unvisited.Count > 0) { Point p = unvisited.Dequeue(); visited.Add(p); if (p == target) { return(true); } Point[] adj = GetAdjacentTiles(map, p); foreach (Point n in adj) { if (map.t(n).type == Tile.Type.IMPASSABLE) { continue; } if (!visited.Contains(n)) { visited.Add(n); unvisited.Enqueue(n); } } } return(false); }
/// <summary> /// Tries to grow into the target position. Returns TRUE if seeding was successful /// </summary> private bool TrySeed(Tile[,] map, Point target, float chance) { if (target.x < 0 || target.y < 0 || target.x >= map.GetLength(0) || target.y >= map.GetLength(1)) { return(false); } if (map.t(target).type != Tile.Type.IMPASSABLE) { return(false); } if (_r.NextDouble() < chance) { map.t(target).type = Tile.Type.ROOM; return(true); } return(false); }
/// <summary> /// Grows seeded room tiles /// </summary> private void BuildRooms(Tile[,] map) { Queue <Point> seeds = new Queue <Point>(); int x = map.GetLength(0); int y = map.GetLength(1); // find seed locations for (int a = 0; a < x; a++) { for (int b = 0; b < y; b++) { if (map[a, b].type == Tile.Type.ROOM) { seeds.Enqueue(new MapGenerator.Point() { x = a, y = b }); } } } int current_generation = 0; int generation_members = seeds.Count; while (seeds.Count > 0) { float chance = inflationBias - inflationDecay * current_generation; Point p = seeds.Dequeue(); // attempt growth in all directions Point[] targets = GetAdjacentTiles(map, p); foreach (var target in targets) { if (TrySeed(map, target, chance)) { AllowPassage(map, p, target); map.t(target).group_id = map[p.x, p.y].group_id; seeds.Enqueue(target); } } // check for generation advancement if (--generation_members <= 0) { current_generation++; generation_members = seeds.Count; } } }
private void AllowPassage(Tile[,] map, Point p1, Point p2) { int dx = p1.x - p2.x; int dy = p1.y - p2.y; if (Math.Abs(dx) > 1 || Math.Abs(dy) > 1) { Console.WriteLine(p1 + " and " + p2 + " are not adjacent!"); return; } if (dx != 0 ^ dy == 0) { Console.WriteLine(p1 + " and " + p2 + " are not adjacent!"); return; } if (dx < 0) { map.t(p1).SetPassage(MapDirection.EAST, true); map.t(p2).SetPassage(MapDirection.WEST, true); } else if (dx > 0) { map.t(p1).SetPassage(MapDirection.WEST, true); map.t(p2).SetPassage(MapDirection.EAST, true); } else if (dy < 0) { map.t(p1).SetPassage(MapDirection.SOUTH, true); map.t(p2).SetPassage(MapDirection.NORTH, true); } else if (dy > 0) { map.t(p1).SetPassage(MapDirection.NORTH, true); map.t(p2).SetPassage(MapDirection.SOUTH, true); } }
public Tile this[PT pt] { get { return(map.t(pt)); } set { map[pt.x, pt.y] = value; } }