public void GenerateRooms(MT19337 rng) { // this function generates all the rooms and hallways for this BSPMapNode and all of its children. if (LeftChild == null && RightChild == null) { if (WalkableSpace != null) { return; } // the room can be between 3 x 3 tiles to the size of the leaf - 2. var roomWidth = rng.Between(BSPTreeEngine.MIN_ROOM_WIDTH, Width - 2); var roomHeight = rng.Between(BSPTreeEngine.MIN_ROOM_HEIGHT, Height - 2); // place the room within the BSPMapNode, but don't put it right // against the side of the BSPMapNode (that would merge rooms together) var roomStartX = rng.Between(1, Width - roomWidth - 1); var roomStartY = rng.Between(1, Height - roomHeight - 1); WalkableSpace = new Rectangle(X + roomStartX, Y + roomStartY, roomWidth, roomHeight); return; } // subleafs: LeftChild?.GenerateRooms(rng); RightChild?.GenerateRooms(rng); // both leafs exist. we know their GetRandomRoom shouldn't return null, because we just called GenerateRooms. if (LeftChild != null && RightChild != null) { MakeHallway(rng, (Rectangle)LeftChild.GetRandomRoom(rng), (Rectangle)RightChild.GetRandomRoom(rng)); } }
public CompleteMap Generate(MT19337 rng, MapRequirements reqs) { var map = new Map((byte)reqs.Barrier); GenerateTree(rng); foreach (var roomspec in reqs.Rooms) { // randomly descend rooms until you find one that fits // var leaf_that_fits = RandomLeafWhere(rng, leaf => leaf.Width >= roomspec.Width && leaf.Height >= roomspec.Height); // if (leaf_that_fits == null) // return null; // leaf_that_fits.WalkableSpace = Rectangle.Empty; // don't generate one // TODO something } root.GenerateRooms(rng); IEnumerable <Rectangle> all_the_rectangles = all_nodes. Where(leaf => leaf.WalkableSpace != null). Select(leaf => (Rectangle)leaf.WalkableSpace). Concat( all_nodes. Where(leaf => leaf.Hallways != null). SelectMany(leaf => leaf.Hallways) ); foreach (var rect in all_the_rectangles) { // TODO something smarter map.Fill((rect.Left, rect.Top), (rect.Width, rect.Height), reqs.Floor); } var smoothIterations = 0; while (MapHelper.SmoothFilter(map, reqs.Barrier, reqs.Floor)) { smoothIterations++; } Point entranceLocation = PointInAnyRandomRoom(rng); map[entranceLocation.Y, entranceLocation.X] = (byte)Tile.WarpUp; return(new CompleteMap { Map = map, Entrance = new Coordinate((byte)entranceLocation.X, (byte)entranceLocation.Y, CoordinateLocale.Standard), Requirements = reqs }); }