public ProtoSubregion(int id, ProtoRegion region, ProtoRoom[,] MasterRoomList) { ID = id; ProtoRegion = region; Color = region.Color; Name = Statics.RandomRegionType(); int RoomCountX = MasterRoomList.GetLength(0); int RoomCountY = MasterRoomList.GetLength(1); // keep track of available adjacent roomss // list for random access; hashset for lookup by coordinates List<ProtoRoom> AvailableAdjacentRooms = new List<ProtoRoom>(); HashSet<PointInt> AvailableAdjacentCoordinates = new HashSet<PointInt>(); // grab random point as starting room int x = Statics.Random.Next(RoomCountX); int y = Statics.Random.Next(RoomCountY); while (!MasterRoomList[x, y].Available) { x = Statics.Random.Next(RoomCountX); y = Statics.Random.Next(RoomCountY); } // create starting room ProtoRoom startingRoom = MasterRoomList[x, y]; ProtoRooms.Add(startingRoom); startingRoom.Available = false; startingRoom.ProtoRegion = region; startingRoom.ProtoSubregion = this; UpdateAdjacentRooms(AvailableAdjacentRooms, AvailableAdjacentCoordinates, startingRoom, MasterRoomList); while (AvailableAdjacentRooms.Count > 0 && ((ProtoRooms.Count < Statics.MinimumRegionSize) || (Statics.Random.Next(100) < Statics.ProbabilityOfExpansion))) { // pick a random room from available neighbor set ProtoRoom randomNeighbor = AvailableAdjacentRooms.RandomListItem(); randomNeighbor.Available = false; randomNeighbor.ProtoRegion = region; randomNeighbor.ProtoSubregion = this; AvailableAdjacentRooms.Remove(randomNeighbor); AvailableAdjacentCoordinates.Remove(randomNeighbor.CoordinatesXY); ProtoRooms.Add(randomNeighbor); // add new room's available neighbors to the available set UpdateAdjacentRooms(AvailableAdjacentRooms, AvailableAdjacentCoordinates, randomNeighbor, MasterRoomList); } }
private void CreateProtoRegions() { // now we have a grid of available/unavailable [proto]rooms (tiles/pixels) // while we have available rooms, create regions, each with a single subregion int AvailableTileCount = Width * Height; while (AvailableTileCount > 0) { ProtoRegion protoRegion = new ProtoRegion(nNextRegionId++, MasterOvergroundRoomList); ProtoRegions.Add(protoRegion); AvailableTileCount -= protoRegion.RoomCount; } }