/
RoomFirstGenerator.cpp
213 lines (173 loc) · 6.98 KB
/
RoomFirstGenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "DungeonGenerator.h"
using namespace ProceduralGeneration;
RoomFirstGenerator::RoomFirstGenerator(int dungeonWidth, int dungeonHeight, Coord startPosition,
int minRoomWidth, int minRoomHeight, int offset, bool randomWalkRooms, RandomWalkParameters paramaters) :
RandomWalkGenerator(dungeonWidth, dungeonHeight, startPosition, paramaters.iterations, paramaters.walkLength, paramaters.startRandomly),
m_minRoomWidth(minRoomWidth), m_minRoomHeight(minRoomHeight), m_offset(offset), m_randomWalkRooms(randomWalkRooms)
{
}
// Generates a series of bounding boxes using Binary space partitioning,
// creates rooms from the boxes then draws them
void RoomFirstGenerator::Generate()
{
// creates a new list of bounding boxes by running the binary space partitioning algorithm
var roomsList = Algorithms::BinarySpacePartitioning(Boundary(m_startPosition, { m_width, m_height }),
m_minRoomWidth, m_minRoomHeight, m_random);
// creates an empty list for coordinates for the floor then creates rooms
CoordList floorPositions;
if (m_randomWalkRooms) //create non square rooms if this is turned on
{
floorPositions = CreateRandomWalkRooms(roomsList);
}
else {
floorPositions = CreateSimpleRooms(roomsList);
}
// creates a list of coordinates for the center of each room and then adds them all
CoordList roomCenters;
for(var room : roomsList)
{
roomCenters.Add(room.Center());
}
// creates corridors by connecting the rooms and then adds them to the floor tiles
CoordList corridors = ConnectRooms(roomCenters);
// adds the corridors to the floors
floorPositions.insert(floorPositions.end(), corridors.begin(), corridors.end());
// get wall tiles
var wallPositions = Algorithms::GetWalls(floorPositions, Coord(m_width, m_height));
// add tiles to the map
AddToDungeon(&floorPositions, &wallPositions);
}
// Creates a set of rooms from the bounding boxes
CoordList RoomFirstGenerator::CreateSimpleRooms(vector<Boundary> rooms)
{
CoordList floor;
for(var room : rooms)
{
// iterates through all columns from the left offset
// to the width of the room minus the right offset
for (int col = m_offset; col < room.Size().x - m_offset; col++)
{
// iterates through all rows from the bottom offset
// to the height of the room minus the top offset
for (int row = m_offset; row < room.Size().y - m_offset; row++)
{
Coord position = room.min + Coord(col, row);
floor.Add(position);
}
}
}
return floor;
}
// Generates a series of rooms in the binary space partition that are not rectangles
// by using the random walk algorithm
CoordList RoomFirstGenerator::CreateRandomWalkRooms(vector<Boundary> rooms)
{
CoordList floor;
for (int i = 0; i < rooms.size(); i++)
{
//if (i == 16)
//cout << "i = 16";
var roomBounds = rooms[i]; // the boundary area of the next room
var roomCenter = Coord(roomBounds.Center().x, roomBounds.Center().y);
var roomFloor = RunRandomWalk(roomCenter);
//for(var position : roomFloor)
for(int j=0; j < roomFloor.size(); j++)
{
/*
if (i == 15 && j == 187)
{
cout << "i = 15, j = 187";
}*/
var position = roomFloor[j];
// checks that the position is inside the boundary and if it is adds it to the floors
if (position.x >= (roomBounds.min.x + m_offset) && position.x <= (roomBounds.max.x - m_offset) &&
position.y >= (roomBounds.min.y - m_offset) && position.y <= (roomBounds.max.y - m_offset))
{
floor.Add(position);
}
}
}
return floor;
}
// Connects all the rooms together by creating corridors from the center or the rooms
CoordList RoomFirstGenerator::ConnectRooms(CoordList roomCenters)
{
CoordList corridors;
CoordList::iterator it;
PseudoRandom random(0, (int) roomCenters.size() - 1, m_seed);
// randomly select a room centre from the list then remove it form the list
int randomIndex = random.GetIntValue();
var currentRoomCenter = roomCenters[randomIndex];
roomCenters.erase(roomCenters.begin() + randomIndex);
// while there are still room centers ro be romove
while (roomCenters.size() > 0)
{
// finds the closest room and remove it
Coord closest = FindClosestPointTo(currentRoomCenter, roomCenters);
CoordList::iterator index = std::find(roomCenters.begin(), roomCenters.end(), closest);
roomCenters.erase(index);
// creats a new corridor from the closest point and makes it the new room centre
CoordList newCorridor = CreateCorridor(currentRoomCenter, closest);
currentRoomCenter = closest;
// adds the new corridor to the existing ones
corridors.insert(corridors.end(), newCorridor.begin(), newCorridor.end());
}
return corridors;
}
// Connects a corridor from the current room to the nearest one
CoordList RoomFirstGenerator::CreateCorridor(Coord currentRoomCenter, Coord destination)
{
CoordList corridor;
var position = currentRoomCenter;
corridor.Add(position); // add the center tile to the corridor tiles
// keeps adding new corridor positions until
// it matches up with the destinations y position
while (position.y != destination.y)
{
// if the destination is above the position move up
if (destination.y > position.y)
{
position += Direction2D::Up();
}
// if the destination is below the position move down
else if (destination.y < position.y)
{
position += Direction2D::Down();
}
corridor.Add(position);
}
// keeps adding new corridor positions until
// it matches up with the destinations x position
while (position.x != destination.x)
{
if (destination.x > position.x)
{
position += Direction2D::Right();
}
else if (destination.x < position.x)
{
position += Direction2D::Left();
}
corridor.Add(position);
}
return corridor;
}
// Finds the closet room to the current room and returns the coordinates of the centre of that room
Coord RoomFirstGenerator::FindClosestPointTo(Coord currentRoomCenter, CoordList roomCenters)
{
Coord closest = {0, 0};
float distance = FLT_MAX;
// calculates the distance from each room centre to the current room center
for(var position : roomCenters)
{
float currentDistance = Coord::DistanceBetween(position, currentRoomCenter);
// if the distance is less than the smallest distance then
// set the smallest distance to the current one
if (currentDistance < distance)
{
distance = currentDistance;
closest = position;
}
}
return closest;
}