-
Notifications
You must be signed in to change notification settings - Fork 1
/
DungeonGenerator.h
519 lines (448 loc) · 18.8 KB
/
DungeonGenerator.h
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#ifndef PCG_DUNGEON_H
#define PCG_DUNGEON_H
#include <queue>
#include <map>
#include <algorithm>
#include <random>
#include <string>
#include <vector>
#include <cmath>
#include <iostream>
enum TILE_TYPE { Empty=0, Floor=1, Corridor=2, Entrance=3, Exit=4, Door=5, Treasure=6, Monster=7, Trap=8 };
/*! Struct representing a point or vector in 2d space.
*/
typedef struct _Vec2 {
/* Data */
int x;
int y;
/* Constructors and Operators */
_Vec2() {
x=0;
y=0;
}
_Vec2(int _x, int _y) {
x=_x;
y=_y;
}
_Vec2(const _Vec2& v) {
x=v.x;
y=v.y;
}
bool operator==(const _Vec2& rhs) const {
return (x == rhs.x && y == rhs.y);
}
bool operator!=(const _Vec2& rhs) const {
return (x != rhs.x || y != rhs.y);
}
} Vec2;
/*! Class for Axis-Aligned Bounding Box
* Instances of this class are used to represent
* a region in 2d space.
*/
class AABB {
public:
AABB() {
mPosition.x = 0;
mPosition.y = 0;
mSize.x = 0;
mSize.y = 0;
}
AABB(int x, int y, int w, int h){
mPosition.x = x;
mPosition.y = y;
mSize.x = w;
mSize.y = h;
}
~AABB(){};
private:
Vec2 mPosition;
Vec2 mSize;
public:
int getWidth() const{
return mSize.x;
}
int getHeight() const{
return mSize.y;
}
int getVolume() const{
return mSize.x * mSize.y;
}
Vec2 getCenter() {
return Vec2(mPosition.x + mSize.x/2, mPosition.y + mSize.y/2);
}
int X() const{
return mPosition.x;
}
int Y() const {
return mPosition.y;
}
void setPosition(int x, int y){
mPosition.x = x;
mPosition.y = y;
}
void setSize(int w, int h){
mSize.x = w;
mSize.y = h;
}
bool isInside(Vec2 &p) const{
return ( p.x >= mPosition.x && p.x <= (mPosition.x+mSize.x) &&
p.y >= mPosition.y && p.y <= (mPosition.y+mSize.y) );
}
};
#include <unordered_set>
template<typename T>
class Node {
public:
Node(Node<T>* parent, T data){
mpParent = parent;
mpLeft = nullptr;
mpRight = nullptr;
mData = data;
}
~Node(){
if(mpLeft != nullptr)
delete mpLeft;
if(mpRight != nullptr)
delete mpRight;
}
private:
T mData;
Node* mpParent;
Node* mpLeft;
Node* mpRight;
public: // Public Methods
T GetData(void) const{
return mData;
}
Node* GetParent(void) const{
return mpParent;
}
void MakeLeftChild(T data) {
mpLeft = new Node(this, data);
}
Node* GetLeftChild(void) const{
return mpLeft;
}
void MakeRightChild(T data){
mpRight = new Node(this, data);
}
Node* GetRightChild(void) const{
return mpRight;
}
public: // Public inner types
class NodeIterator {
public:
NodeIterator(Node* parent){
mpRoot = parent;
mpCurrent = parent;
}
private:
std::unordered_set<Node*> mVisited;
Node* mpRoot;
Node* mpCurrent;
public:
bool Next(){
if( mpCurrent->GetLeftChild() != 0 && mVisited.find(mpCurrent->GetLeftChild()) == mVisited.end() ) {
//mpCurrent->SetLeftVisited();
mVisited.insert(mpCurrent->GetLeftChild());
mpCurrent = mpCurrent->GetLeftChild();
return true;
}
if( mpCurrent->GetRightChild() != 0 && mVisited.find(mpCurrent->GetRightChild()) == mVisited.end() ) {
//mpCurrent->SetRightVisited();
mVisited.insert( mpCurrent->GetRightChild() );
mpCurrent = mpCurrent->GetRightChild();
return true;
}
if( mpCurrent->GetParent() != 0 ) {
mpCurrent = mpCurrent->GetParent();
return true;
} else {
return false;
}
}
T GetData() const{
return mpCurrent->mData;
}
Node* GetNode() const{
return mpCurrent;
}
bool IsLeaf() const{
if( mpCurrent->GetLeftChild() == 0 && mpCurrent->GetRightChild() == 0 )
return true;
else
return false;
}
void Reset(){
mVisited.clear();
mpCurrent = mpRoot;
}
};
};
typedef AABB Room;
typedef std::vector< Vec2 > Path;
typedef std::vector<unsigned int> GridLine;
typedef std::vector< std::vector<unsigned int> > Grid;
#define RAND_GEN_PERCENTAGE (float)mUniDistr(mRandGen)
#define DEBUG 1
/*! Class representing a randomly generated dungeon
*/
class DungeonGenerator {
public:
DungeonGenerator(std::string seed, int width, int height) : mWidth(width), mHeight(height), mRootNode(nullptr, AABB(0,0,width,height)) {
// init grid
mGrid = std::vector< std::vector< unsigned int > >(mHeight, std::vector< unsigned int >(mWidth, TILE_TYPE::Empty));
mRandGen = std::mt19937( std::random_device()() );
mUniDistr = std::uniform_real_distribution<float>(0.0f, 1.0f);
}
~DungeonGenerator(){}
//enum TILE_TYPE { Empty=0, Floor=1, Corridor=2, Entrance=3, Exit=4, Door=5, Treasure=6, Monster=7, Trap=8 };
private:
int mWidth;
int mHeight;
int mUnitSquare;
std::mt19937 mRandGen;
std::uniform_real_distribution<float> mUniDistr;
std::vector< Room > mRooms;
std::vector< Path > mCorridors;
std::vector< Vec2 > mTreasures;
std::vector< Vec2 > mMonsters;
std::vector< Vec2 > mTraps;
Vec2 mEntrance;
Vec2 mExit;
Grid mGrid;
Node<AABB> mRootNode;
public:
void Generate() {
// Clean-up if already generated
mRootNode = Node<AABB>(nullptr, AABB(0, 0, mWidth, mHeight));
mRooms.clear();
mCorridors.clear();
mTreasures.clear();
mMonsters.clear();
mTraps.clear();
ClearGrid();
// Generate dungeon parts
SplitSpace(&mRootNode);
FindRoomsDigCorridors();
BakeFloor();
PlaceEntranceAndExit();
PlaceDoors();
PlaceTreasureAndMonsters();
BakeDetails();
std::cout << "Dungeon Generation complete!" << std::endl;
#ifdef DEBUG
for(int i = 0; i<mGrid.size(); i++) {
for(int j = 0; j<mGrid[i].size(); j++) {
std::cout << mGrid[i][j];
}
std::cout << std::endl;
}
#endif
}
Grid GetGrid(void){
return mGrid;
}
// Function that returns a list of points for each neighbouring grid cell of the function's center parameter
std::vector<Vec2> GetNeighbours(Vec2 ¢er) {
std::vector<Vec2> neighbours;
neighbours.push_back( Vec2(center.x+1, center.y) );
neighbours.push_back( Vec2(center.x-1, center.y) );
neighbours.push_back( Vec2(center.x, center.y+1) );
neighbours.push_back( Vec2(center.x, center.y-1) );
return neighbours;
}
// Simplistic grid-based pathfinding
Path FindPath(Vec2 begin, Vec2 end) {
// Create needed variables and init
// with the starting point.
Path result;
result.push_back(begin);
Vec2 current = begin;
do {
// Get neighbours
Path neighbours = GetNeighbours(current);
// Find nearest
int nearestIndex;
int nearestRange=1000;
for(int i=0; i<4; ++i) {
if( (std::abs(neighbours[i].x - end.x) + std::abs(neighbours[i].y - end.y)) < nearestRange ) {
nearestRange = (std::abs(neighbours[i].x - end.x) + std::abs(neighbours[i].y - end.y));
nearestIndex = i;
}
}
// save up and continue...
current = neighbours[nearestIndex];
result.push_back(current);
} while (current != end); //... until we reach the end point
return result;
}
private:
void ClearGrid(){
mGrid.clear();
mGrid = std::vector< std::vector< unsigned int > >(mHeight, std::vector< unsigned int >(mWidth, TILE_TYPE::Empty));
}
void SplitSpace(Node<AABB>* node){
// Choose how and where to split
float ratio = (float)node->GetData().getWidth() / node->GetData().getHeight();
bool splitVertical = true;
if(ratio < 1.0f)
splitVertical = false;
float split = RAND_GEN_PERCENTAGE;
do {
split = RAND_GEN_PERCENTAGE;
} while (split < 0.4f || split > 0.6f);
// Create and calculate the 2 subspaces
// of the splitted one.
AABB subspaceA, subspaceB;
if( splitVertical ) {
int splitX = node->GetData().X() + (int)(split * node->GetData().getWidth());
subspaceA = AABB( node->GetData().X(), node->GetData().Y(),
(int)(split * node->GetData().getWidth()), node->GetData().getHeight() );
subspaceB = AABB( splitX, node->GetData().Y(),
(int)((1-split) * node->GetData().getWidth()), node->GetData().getHeight() );
} else {
int splitY = node->GetData().Y() + (int)(split * node->GetData().getHeight());
subspaceA = AABB( node->GetData().X(), node->GetData().Y(),
node->GetData().getWidth(), (int)(split * node->GetData().getHeight()) );
subspaceB = AABB( node->GetData().X(), splitY,
node->GetData().getWidth(), (int)((1-split) * node->GetData().getHeight()) );
}
#ifdef DEBUG
std::cout << "Splitting [" << node->GetData().X() << ", " << node->GetData().Y() << ", " << node->GetData().getWidth() << ", " << node->GetData().getHeight() << "] into:" << std::endl;
std::cout << "Space A: [" << subspaceA.X() << ", " << subspaceA.Y() << ", " << subspaceA.getWidth() << ", " << subspaceA.getHeight() << "]" << std::endl;
std::cout << "Space B: [" << subspaceB.X() << ", " << subspaceB.Y() << ", " << subspaceB.getWidth() << ", " << subspaceB.getHeight() << "]" << std::endl;
std::cout << std::endl;
#endif
// Add subspaces to the current node
node->MakeLeftChild(subspaceA);
node->MakeRightChild(subspaceB);
// Decide if we need to split more
// and continue recursion.
if( subspaceA.getWidth() > 7 && subspaceA.getHeight() > 6 )
SplitSpace(node->GetLeftChild());
if( subspaceB.getWidth() > 7 && subspaceB.getHeight() > 6 )
SplitSpace(node->GetRightChild());
}
void FindRoomsDigCorridors(){
Node<AABB>::NodeIterator it(&mRootNode);
// Iterate over bsp-tree and add Rooms that
// adhere to the minimum size required
while( it.Next() != false ) {
if( it.IsLeaf() == true && it.GetData().getWidth() > 3 && it.GetData().getHeight() > 3 ) {
mRooms.push_back( AABB(it.GetData().X()+1, it.GetData().Y()+1, it.GetData().getWidth()-2, it.GetData().getHeight()-2) );
#ifdef DEBUG
std::cout << "Added [" << it.GetData().X() << ", " << it.GetData().Y() << ", " << it.GetData().getWidth() << ", " << it.GetData().getHeight() << "]" << std::endl;
#endif
}
}
it.Reset();
// Re-iterate over bsp-tree and create
// corridors using pathfind function (grid-based)
while( it.Next() != false ) {
if( !it.IsLeaf() ) {
Path corridor = FindPath( it.GetNode()->GetLeftChild()->GetData().getCenter(),
it.GetNode()->GetRightChild()->GetData().getCenter() );
mCorridors.push_back(corridor);
#ifdef DEBUG
std::cout << "Corridor points from [" << it.GetNode()->GetLeftChild()->GetData().getCenter().x << ", " << it.GetNode()->GetLeftChild()->GetData().getCenter().y << "]" << std::endl;
std::cout << " to [" << it.GetNode()->GetRightChild()->GetData().getCenter().x << ", " << it.GetNode()->GetRightChild()->GetData().getCenter().y << "]" << std::endl;
for(Path::iterator itP = corridor.begin(); itP != corridor.end(); itP++) {
std::cout << itP->x << ", " << itP->y << std::endl;
}
std::cout << std::endl;
#endif
}
}
}
void PlaceEntranceAndExit(){
int i, j; // i is the index of the room with the entrance
// j is the index of the room with the exit
i = j = 0;
// There are N rooms, choose if entrance will be in one
// of the rooms of the first half (0 to N/2) or on the
// second (N/2 to N). Exit will be on the other.
if( RAND_GEN_PERCENTAGE > 0.5f ) {
i = floorf( RAND_GEN_PERCENTAGE * (mRooms.size() / 2.0f) );
j = floorf( (mRooms.size() / 2.0f) + RAND_GEN_PERCENTAGE * (mRooms.size() / 2.0f) );
} else {
j = floorf( RAND_GEN_PERCENTAGE * (mRooms.size() / 2.0f) );
i = floorf( (mRooms.size() / 2.0f) + RAND_GEN_PERCENTAGE * (mRooms.size() / 2.0f) );
}
// Set the center of the rooms as entrance and exit
mEntrance = mRooms[i].getCenter();
mExit = mRooms[j].getCenter();
#ifdef DEBUG
std::cout << "Entrance: [" << mEntrance.x << ", " << mEntrance.y << "]" << std::endl;
std::cout << "Exit: [" << mExit.x << ", " << mExit.y << "]" << std::endl;
#endif
}
void BakeFloor(){
std::cout << std::endl << "Baking data on mGrid..." << std::endl;
// Fill rooms on the grid with the proper id (floor=1)
for(std::vector< Room >::iterator it = mRooms.begin(); it != mRooms.end(); ++it) {
for(int i = it->Y(); i < it->Y()+it->getHeight(); i++) {
for(int j = it->X(); j < it->X()+it->getWidth(); j++) {
mGrid[i][j] = TILE_TYPE::Floor;
}
}
}
// Fill corridors on the grid with the proper id (corridor=2)
for(std::vector< Path >::iterator it = mCorridors.begin(); it != mCorridors.end(); ++it) {
for(Path::iterator pathIt = it->begin(); pathIt != it->end(); ++pathIt) {
if(mGrid[pathIt->y][pathIt->x] != 1)
mGrid[pathIt->y][pathIt->x] = TILE_TYPE::Corridor;
}
}
}
void PlaceDoors(){
// Detects corridor-room crossings and
// places doors (door=5)
// *fix* weird door placement sometimes
for(int i = 1; i < mGrid.size()-1; i++) {
for(int j = 1; j < mGrid[i].size()-1; j++) {
if( mGrid[i][j] == 2 && (mGrid[i+1][j] == 1 || mGrid[i-1][j] == 1 || mGrid[i][j+1] == 1 || mGrid[i][j-1] == 1) &&
(mGrid[i+1][j] != 5 && mGrid[i-1][j] != 5 && mGrid[i][j+1] != 5 && mGrid[i][j-1] != 5) &&
((mGrid[i+1][j] == 0 && mGrid[i-1][j] == 0) || (mGrid[i][j+1] == 0 && mGrid[i][j-1] == 0)) )
{
mGrid[i][j] = TILE_TYPE::Door;
}
}
}
}
void PlaceTreasureAndMonsters(){
// Iterate rooms and place treasure, monsters and traps
for(std::vector< Room >::iterator it = mRooms.begin(); it != mRooms.end(); ++it) {
int scale = (int)((float)it->getVolume()/8.0f);
// Monsters
for(int i = 0; i<scale; i++)
mMonsters.push_back( Vec2(it->X() + (int)(it->getWidth() * RAND_GEN_PERCENTAGE), it->Y() + (int)(it->getHeight() * RAND_GEN_PERCENTAGE)) );
// Treasures
if( scale > 3) {
mTreasures.push_back( Vec2(it->X() + (int)(it->getWidth() * RAND_GEN_PERCENTAGE), it->Y() + (int)(it->getHeight() * RAND_GEN_PERCENTAGE)) );
mTreasures.push_back( Vec2(it->X() + (int)(it->getWidth() * RAND_GEN_PERCENTAGE), it->Y() + (int)(it->getHeight() * RAND_GEN_PERCENTAGE)) );
} else if (scale >= 1)
mTreasures.push_back( Vec2(it->X() + (int)(it->getWidth() * RAND_GEN_PERCENTAGE), it->Y() + (int)(it->getHeight() * RAND_GEN_PERCENTAGE)) );
// Traps
if( RAND_GEN_PERCENTAGE > 0.35f )
mTraps.push_back( Vec2(it->X() + (int)(it->getWidth() * RAND_GEN_PERCENTAGE), it->Y() + (int)(it->getHeight() * RAND_GEN_PERCENTAGE)) );
}
}
void BakeDetails(){
// Write the details of the dungeon to its grid.
// Details = Entrance/Exit, Monsters, Treasure and Traps
// Entrance and exit...
mGrid[mEntrance.y][mEntrance.x] = TILE_TYPE::Entrance;
mGrid[mExit.y][mExit.x] = TILE_TYPE::Exit;
// Monsters...
for(std::vector< Vec2 >::iterator it = mMonsters.begin(); it != mMonsters.end(); ++it)
mGrid[it->y][it->x] = TILE_TYPE::Monster;
// Treasure...
for(std::vector< Vec2 >::iterator it = mTreasures.begin(); it != mTreasures.end(); ++it)
mGrid[it->y][it->x] = TILE_TYPE::Treasure;
// Traps...
for(std::vector< Vec2 >::iterator it = mTraps.begin(); it != mTraps.end(); ++it)
mGrid[it->y][it->x] = TILE_TYPE::Trap;
}
};
#endif