Fixed entrance rooms starting off of the boarder and improved clarity of printed map.
This commit is contained in:
parent
64fbb67397
commit
024e0ae514
3 changed files with 65 additions and 42 deletions
|
|
@ -24,7 +24,8 @@ namespace DungeonGenerator
|
||||||
_yLength = 28;
|
_yLength = 28;
|
||||||
|
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
_entrancePoints = GenerateEntrancePoints(_xLength, _yLength, random.Next(1,4));
|
List<Room> entranceRooms = GenerateEntranceRooms(_xLength, _yLength, random.Next(1,4));
|
||||||
|
_entrancePoints = entranceRooms.SelectMany(room => room.GetPointsInRoom()).ToList();
|
||||||
|
|
||||||
EvenDisperser disperser = new EvenDisperser(_xLength, _yLength, _entrancePoints); //TODO calculate L and W from length
|
EvenDisperser disperser = new EvenDisperser(_xLength, _yLength, _entrancePoints); //TODO calculate L and W from length
|
||||||
int numberOfMonsterRooms = 7; // TODO: Calculate from ratio
|
int numberOfMonsterRooms = 7; // TODO: Calculate from ratio
|
||||||
|
|
@ -41,6 +42,7 @@ namespace DungeonGenerator
|
||||||
|
|
||||||
public void PrintMap()
|
public void PrintMap()
|
||||||
{
|
{
|
||||||
|
List<Point> monsterRoomPoints = _monsterRooms.SelectMany(room => room.GetPointsInRoom()).ToList();
|
||||||
char[,] mapMatrix = new Char[_xLength, _yLength];
|
char[,] mapMatrix = new Char[_xLength, _yLength];
|
||||||
for (int x = 0; x < _xLength; x++)
|
for (int x = 0; x < _xLength; x++)
|
||||||
{
|
{
|
||||||
|
|
@ -50,13 +52,13 @@ namespace DungeonGenerator
|
||||||
{
|
{
|
||||||
mapMatrix[x, y] = '*';
|
mapMatrix[x, y] = '*';
|
||||||
}
|
}
|
||||||
else if (_monsterRooms.Any(room => room.PositionOfTopLeft.Equals(new Point(x,y))))
|
else if (monsterRoomPoints.Contains(new Point(x,y)))
|
||||||
{
|
{
|
||||||
mapMatrix[x, y] = 'X';
|
mapMatrix[x, y] = 'X';
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mapMatrix[x, y] = 'O';
|
mapMatrix[x, y] = '-';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -86,15 +88,15 @@ namespace DungeonGenerator
|
||||||
Left
|
Left
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Point> GenerateEntrancePoints(int xLengthOfDungeon, int yLengthOfDungeon, int numberOfEntranceLines)
|
private List<Room> GenerateEntranceRooms(int xLengthOfDungeon, int yLengthOfDungeon, int numberOfEntranceLines)
|
||||||
{
|
{
|
||||||
if (numberOfEntranceLines > 4)
|
if (numberOfEntranceLines > 4)
|
||||||
{
|
{
|
||||||
throw new Exception("Number of entrance lines cannot be greater than 4");
|
throw new Exception("Number of entrance lines cannot be greater than 4");
|
||||||
}
|
}
|
||||||
|
|
||||||
const int entranceSpaces = 12;
|
const int numEntranceRooms = 12;
|
||||||
List<Point> entrancePoints = new List<Point>();
|
List<Room> entranceRooms = new List<Room>();
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
Dictionary<Side, int> sidesToEnterOn = new()
|
Dictionary<Side, int> sidesToEnterOn = new()
|
||||||
{
|
{
|
||||||
|
|
@ -118,30 +120,31 @@ namespace DungeonGenerator
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int pointsPerLine = entranceSpaces / numberOfEntranceLines;
|
int roomsPerLine = numEntranceRooms / numberOfEntranceLines;
|
||||||
|
int bottom = yLengthOfDungeon - SIDE_LENGTH_OF_NORMAL;
|
||||||
|
int oneRoomFromBottom = bottom - SIDE_LENGTH_OF_NORMAL;
|
||||||
|
int right = xLengthOfDungeon - SIDE_LENGTH_OF_NORMAL;
|
||||||
// Generate entrance lines on each side
|
// Generate entrance lines on each side
|
||||||
switch (sidesToEnterOn[Side.Top])
|
switch (sidesToEnterOn[Side.Top])
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
// Add half of points for this entrance line to the top left side
|
// Add half of points for this entrance line to the top left side
|
||||||
for (var i = 0; i < pointsPerLine; i++)
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point(i,0));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(i,0)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the rest of the points for this entrance line to the top right side.
|
// Add the rest of the points for this entrance line to the top right side.
|
||||||
int pointsRemaining = pointsPerLine;
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
for (var i = 0; i < pointsRemaining; i++)
|
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point((xLengthOfDungeon - 1) - i,0));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(right - i,0)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
int midpoint = xLengthOfDungeon / 2;
|
int startOfLine = GetStartOfCenteredLine(xLengthOfDungeon, roomsPerLine, SIDE_LENGTH_OF_NORMAL);
|
||||||
int startOfLine = midpoint - (pointsPerLine / 2);
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
for (var i = 0; i < pointsPerLine; i++)
|
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point(startOfLine + i, 0));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL,new Point(startOfLine + i, 0)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -150,23 +153,22 @@ namespace DungeonGenerator
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
// Add points for this entrance line to the top right side
|
// Add points for this entrance line to the top right side
|
||||||
for (var i = 0; i < pointsPerLine; i++)
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point(xLengthOfDungeon - 1, i + 1));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(right, i + SIDE_LENGTH_OF_NORMAL)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the rest of the points for this entrance line to the bottom right side.
|
// Add the rest of the points for this entrance line to the bottom right side.
|
||||||
for (var i = 0; i < pointsPerLine; i++)
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point(xLengthOfDungeon - 1, (yLengthOfDungeon - 2) - i));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(right, oneRoomFromBottom - i)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
int midpoint = yLengthOfDungeon / 2;
|
int startOfLine = GetStartOfCenteredLine(xLengthOfDungeon, roomsPerLine, SIDE_LENGTH_OF_NORMAL);
|
||||||
int startOfLine = midpoint - (pointsPerLine / 2);
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
for (int i = 0; i < pointsPerLine; i++)
|
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point(xLengthOfDungeon - 1, startOfLine + i));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(right, startOfLine + i)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -175,23 +177,22 @@ namespace DungeonGenerator
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
// Add half of points for this entrance line to the bottom left side
|
// Add half of points for this entrance line to the bottom left side
|
||||||
for (var i = 0; i < pointsPerLine; i++)
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point(i, yLengthOfDungeon - 1));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(i, bottom)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the rest of the points for this entrance line to the bottom right side.
|
// Add the rest of the points for this entrance line to the bottom right side.
|
||||||
for (var i = 0; i < pointsPerLine; i++)
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point((xLengthOfDungeon - 1) - i, yLengthOfDungeon - 1));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point((xLengthOfDungeon - 1) - i, bottom)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
int midpoint = xLengthOfDungeon / 2;
|
int startOfLine = GetStartOfCenteredLine(xLengthOfDungeon, roomsPerLine, SIDE_LENGTH_OF_NORMAL);
|
||||||
int startOfLine = midpoint - (pointsPerLine / 2);
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
for (var i = 0; i < pointsPerLine; i++)
|
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point(startOfLine + i, yLengthOfDungeon - 1));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(startOfLine + i, bottom)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -200,28 +201,33 @@ namespace DungeonGenerator
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
// Add half of points for this entrance line to the top left side
|
// Add half of points for this entrance line to the top left side
|
||||||
for (var i = 0; i < pointsPerLine; i++)
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point(0, i + 1));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(0, i + SIDE_LENGTH_OF_NORMAL)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the rest of the points for this entrance line to the bottom left side.
|
// Add the rest of the points for this entrance line to the bottom left side.
|
||||||
for (var i = 0; i < pointsPerLine; i++)
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point(0, (yLengthOfDungeon - 1) - i));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(0, oneRoomFromBottom - i)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
int midpoint = yLengthOfDungeon / 2;
|
int startOfLine = GetStartOfCenteredLine(xLengthOfDungeon, roomsPerLine, SIDE_LENGTH_OF_NORMAL);
|
||||||
int startOfLine = midpoint - (pointsPerLine / 2);
|
for (var i = 0; i < roomsPerLine * SIDE_LENGTH_OF_NORMAL; i += SIDE_LENGTH_OF_NORMAL)
|
||||||
for (int i = 0; i < pointsPerLine; i++)
|
|
||||||
{
|
{
|
||||||
entrancePoints.Add(new Point(0, startOfLine + i));
|
entranceRooms.Add(new Room(RoomType.Entrance, SIDE_LENGTH_OF_NORMAL, SIDE_LENGTH_OF_NORMAL, new Point(0, startOfLine + i)));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return entrancePoints;
|
return entranceRooms;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int GetStartOfCenteredLine(int length, int numberOfRooms, int sizeOfRoom)
|
||||||
|
{
|
||||||
|
int midpoint = length / 2;
|
||||||
|
return midpoint - (numberOfRooms * sizeOfRoom / 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -37,7 +37,7 @@ namespace DungeonGenerator
|
||||||
{
|
{
|
||||||
int numCandidates = 50; // Increasing improves results but greatly effects performance.
|
int numCandidates = 50; // Increasing improves results but greatly effects performance.
|
||||||
Random rnd = new Random();
|
Random rnd = new Random();
|
||||||
int randomIndex = rnd.Next(_availablePoints.Count);
|
int randomIndex = rnd.Next(0, _availablePoints.Count);
|
||||||
Point bestCandidate = new Point(Int32.MaxValue, Int32.MaxValue);
|
Point bestCandidate = new Point(Int32.MaxValue, Int32.MaxValue);
|
||||||
int bestDistance = 0;
|
int bestDistance = 0;
|
||||||
for (var i = 0; i < numCandidates; i++)
|
for (var i = 0; i < numCandidates; i++)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace DungeonGenerator
|
namespace DungeonGenerator
|
||||||
{
|
{
|
||||||
public enum RoomType
|
public enum RoomType
|
||||||
{
|
{
|
||||||
Normal,
|
Normal,
|
||||||
|
Entrance,
|
||||||
Monster,
|
Monster,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -20,5 +23,19 @@ namespace DungeonGenerator
|
||||||
public int Height { get; set; }
|
public int Height { get; set; }
|
||||||
public int Width { get; set; }
|
public int Width { get; set; }
|
||||||
public Point PositionOfTopLeft { get; set; }
|
public Point PositionOfTopLeft { get; set; }
|
||||||
|
|
||||||
|
public List<Point> GetPointsInRoom()
|
||||||
|
{
|
||||||
|
List<Point> points = new List<Point>();
|
||||||
|
for (int i = 0; i < Width; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < Height; j++)
|
||||||
|
{
|
||||||
|
points.Add(new Point(PositionOfTopLeft.X + i, PositionOfTopLeft.Y + j));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return points;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue