// Remove all rooms that we were unable to link to by corridor private void RemoveUnConnectedRooms() { foreach (var room in rooms) { if (!LinkedRooms.Contains(room)) { foreach (var corridor in room.Doors) { if (corridor != null) { corridor.RemoveFailedCorridor(corridor.StartX, corridor.StartY); } } for (var y1 = room.Top - 1; y1 < room.Top + room.Height + 1; y1++) { for (var x1 = room.Left - 1; x1 < room.Left + room.Width + 1; x1++) { if (Atts.IsOnMap(x1, y1) && (map[x1, y1].LocationType == Atts.LocationType.WallPoint || map[x1, y1].LocationType == Atts.LocationType.RoomSpace || map[x1, y1].LocationType == Atts.LocationType.DoorPoint)) { map[x1, y1].LocationType = Atts.LocationType.Empty; } } } } } }
// tries to create rect or round room public bool CreateRoom(DungeonLocation[,] map) { _map = map; if (Atts.IsFirstLevel) { if (Atts.RandPc(100) < 70) { CreateRectangularRoom(true); } else { CreateCircularRoom(); } } else { var d100 = Atts.RandPc(100); if (d100 < 85) { CreateRectangularRoom(true); } else if (d100 < 95) { CreateCircularRoom(); } else { CreateRectangularRoom(true); } } return(fitsMap); }
// pick new direction to go in, return -1 if not possible private int PickDirection(int xdoor, int ydoor) { bool[] canMove = new[] { true, true, true, true }; while (true) { var iDir = 0; var AreMoves = canMove.Any(b => b); // are there any canMove[] true? if (!AreMoves) { break; } do { iDir = Atts.R.Next(4); } while (!canMove[iDir]); var x = xdoor + Atts.OffSetDirX[iDir]; var y = ydoor + Atts.OffSetDirY[iDir]; if (Atts.IsOnMap(x, y) && cmap[x, y].LocationType == Atts.LocationType.Empty) { return(iDir); } canMove[iDir] = false; } ; return(-1); }
// saves out a char map private void DumpTextMap(int mapnum) { return; var mapDumpFile = String.Format(@"map{0}.txt", mapnum); using (var sw = new StreamWriter(mapDumpFile)) { for (var x1 = 0; x1 < Atts.MaxLocsX; x1++) { if (x1 % 5 == 0) { sw.Write(x1.ToString("000 ")); } } sw.WriteLine(); for (var y1 = 0; y1 < Atts.MaxLocsY; y1++) { sw.Write(y1.ToString("000 ")); for (var x1 = 0; x1 < Atts.MaxLocsX; x1++) { var locType = map[x1, y1].LocationType; var c = Atts.MapChar(locType, map[x1, y1].GridLine); sw.Write(c); } sw.WriteLine(y1.ToString("000 ")); } for (var x1 = 0; x1 < Atts.MaxLocsX; x1++) { if (x1 % 5 == 0) { sw.Write(x1.ToString("000 ")); } } sw.WriteLine(); sw.Close(); } }
private void FixRoomDoors(bool isCircular) { // we've created lots of tempdoor positions but only want up to 4 doors // first count how many temp doors. // For rectangular rooms, top,left,height and width are ok. // or circular, Width is 0, Height = Radius and Top, Left are the circle centre so need to transform var top = Top; var left = Left; var height = Height; var width = Width; if (isCircular) { top -= width; if (top < 0) { top = 0; } left -= width; if (left < 0) { left = 0; } width = width * 2; // 2 x radius height = width; } var x1 = 0; var y1 = 0; var countTempDoors = 0; for (y1 = top; y1 < top + height; y1++) { for (x1 = left; x1 < left + width; x1++) { if (Atts.IsOnMap(x1, y1) && _map[x1, y1].LocationType == Atts.LocationType.TempDoor) { countTempDoors++; } } } if (countTempDoors > 8) { countTempDoors = 8; } while (countTempDoors > 0) { do { y1 = Atts.R.Next(top + height); x1 = Atts.R.Next(left + width); } while (_map[x1, y1].LocationType != Atts.LocationType.TempDoor); _map[x1, y1].LocationType = Atts.LocationType.DoorPoint; countTempDoors--; } // remove the rest for (y1 = top - 1; y1 < top + height + 1; y1++) { for (x1 = left - 1; x1 < left + width + 1; x1++) { if (Atts.IsOnMap(x1, y1) && _map[x1, y1].LocationType == Atts.LocationType.TempDoor) { _map[x1, y1].LocationType = Atts.LocationType.WallPoint; } } } }
// try to grow corridor public bool GrowCorridor(ref int xdoor, ref int ydoor) { var done = false; var x = xdoor; var y = ydoor; var seglen = Atts.R.Next(4) + 3; var iDir = PickDirection(x, y); if (iDir == -1) { return(false); } var xOffset = Atts.OffSetDirX[iDir]; var yOffset = Atts.OffSetDirY[iDir]; while (!done) { x += xOffset; y += yOffset; if (!Atts.IsOnMap(x, y)) { RemoveFailedCorridor(xdoor, ydoor); return(false); } var locType = cmap[x, y].LocationType; if (locType == Atts.LocationType.Empty) { cmap[x, y].LocationType = Atts.LocationType.TempCorridor; if (seglen > 0) { seglen--; } else if (cmap[x, y].GridLine) // Only change direction where { iDir = NewDirection(iDir); xOffset = Atts.OffSetDirX[iDir]; yOffset = Atts.OffSetDirY[iDir]; seglen = Atts.R.Next(4) + 3; } continue; } // hit something - door or Corridor if (locType == Atts.LocationType.Corridor) { var metCorridor = cmap[x, y].Corridor; if (IsNewCorridor(metCorridor)) { MeetANewCorridor(metCorridor); done = true; } else { RemoveFailedCorridor(xdoor, ydoor); return(false); } } if (locType == Atts.LocationType.DoorPoint) { var room = cmap[x, y].aRoom; if (!room.IsSameRoom(StartRoom) && !room.ConnectedRooms.Contains(StartRoom) && room.HasSpareExit()) { EndRoom = room; room.AddExit(this, Atts.FromDir[iDir]); LinkTwoRooms(room, StartRoom); done = true; } else { RemoveFailedCorridor(xdoor, ydoor); return(false); } } /* if (locType == Atts.LocationType.WallPoint) * { * RemoveFailedCorridor(xdoor, ydoor); * return false; * }*/ } // Finalize the TempDoors and TempCorridors if (cmap[xdoor, ydoor].LocationType == Atts.LocationType.TempDoor) { cmap[xdoor, ydoor].LocationType = Atts.LocationType.DoorPoint; } for (var yp = 0; yp < Atts.MaxLocsY; yp++) { for (var xp = 0; xp < Atts.MaxLocsX; xp++) { if (Atts.IsOnMap(xp, yp) && cmap[xp, yp].LocationType == Atts.LocationType.TempCorridor) { cmap[xp, yp].LocationType = Atts.LocationType.Corridor; cmap[xp, yp].Corridor = this; } } } return(true); }