-
Notifications
You must be signed in to change notification settings - Fork 0
/
DunGen.cpp
759 lines (611 loc) · 19.7 KB
/
DunGen.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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#include "DunGen.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <SFML/Graphics.hpp>
DunGen::DunGen(int xCap,int yCap,int difficulty)
{
Generate(xCap,yCap,difficulty);
}
void DunGen::Generate(int xCap,int yCap,int difficulty)
{
srand(time(NULL));
//blankTexture.loadFromFile("sprites/dungeon_sprites/emptytile.png");
//lvlTexture.loadFromFile("sprites/dungeon_sprites/room.png");
//stairTexture.loadFromFile("sprites/dungeon_sprites/dstair.png");
ResetLevel();
Tunnels(xCap,yCap,difficulty,3);
Rooms(xCap,yCap,difficulty,4);
PlaceChest(difficulty);
SpriteGenerate();
}
void DunGen::Tunnels(int xCap,int yCap,int difficulty,int roomType)
{
//To do: Add an extra
lineLength = 20;
//Decides the size of the tunnel. This is actually one OVER what it should be (0-9, which is 10 digits.). Replace with a formula later.
tunX = rand() % (xCap+1);
tunY = rand() % (yCap+1);
levelXCap = xCap;
levelYCap = yCap;
level[tunX][tunY] = roomType;
tunLocX[0] = tunX;
tunLocY[0] = tunY;
firstTunX = tunX;
firstTunY = tunY;
tunCount = 0;
//while (tunCount<lineLength)
for (int t=1;t<lineLength;t++)
//Hm. I might need to make a long, elaborate scan to make sure it
{
direction = rand()% 3;
if (prevDir == 5)
{
direction = rand() % 4;
if (direction == 0)
{
std::cout << "Start 0" << std::endl;
TunUp();
}
else if (direction == 1)
{
std::cout << "Start 1" << std::endl;
TunRight();
}
else if (direction == 2)
{
std::cout << "Start 2" << std::endl;
TunDown();
}
else if (direction == 3)
{
std::cout << "Start 3" << std::endl;
TunLeft();
}
}
else if (prevDir == 0)
{
std::cout << "Previous Direction 0: ";
if (direction == 0)
{
std::cout << "Standard Up" << std::endl;
TunUp();
}
else if (direction == 1)
{
std::cout << "Standard Right" << std::endl;
TunRight();
}
else if (direction == 2)
{
std::cout << "Standard Left" << std::endl;
TunLeft();
}
}
else if (prevDir == 1)
{
std::cout << "Previous Direction 1: ";
if (direction == 0)
{
std::cout << "Standard Up" << std::endl;
TunUp();
}
else if (direction == 1)
{
std::cout << "Standard Right" << std::endl;
TunRight();
}
else if (direction == 2)
{
std::cout << "Standard Down" << std::endl;
TunDown();
}
}
else if (prevDir == 2)
{
std::cout << "Previous Direction 2: ";
if (direction == 0)
{
std::cout << "Standard Right" << std::endl;
TunRight();
}
else if (direction == 1)
{
std::cout << "Standard Down" << std::endl;
TunDown();
}
else if (direction == 2)
{
std::cout << "Standard Left" << std::endl;
TunLeft();
}
}
else if (prevDir == 3)
{
std::cout << "Previous Direction 3: ";
if (direction == 0)
{
std::cout << "Standard Up" << std::endl;
TunUp();
}
else if (direction == 1)
{
std::cout << "Standard Down" << std::endl;
TunDown();
}
else if (direction == 2)
{
std::cout << "Standard Left" << std::endl;
TunLeft();
}
}
if (roomType > level[tunX][tunY])
{
tunCount++;
level[tunX][tunY] = roomType;
tunLocX[t] = tunX;
tunLocY[t] = tunY;
}
if (direction != prevDir)
{
//This process will create some extra space, between -1 to 2 spots.
t++;
std::cout << "Double Down: ";
if (direction == 0)
{
std::cout << "Up" << std::endl;
TunUp();
prevDir = 0;
}
else if (direction == 1)
{
std::cout << "Right" << std::endl;
TunRight();
prevDir = 1;
}
else if (direction == 2)
{
std::cout << "Down" << std::endl;
TunDown();
prevDir = 2;
}
else if (direction == 3)
{
std::cout << "Left" << std::endl;
TunLeft();
prevDir = 3;
}
if (roomType > level[tunX][tunY])
{
tunCount++;
level[tunX][tunY] = roomType;
tunLocX[t] = tunX;
tunLocY[t] = tunY;
}
}
}
lastTunX = tunX;
lastTunY = tunY;
prevDir = 5;
}
void DunGen::Rooms(int xCap,int yCap,int difficulty,int roomType)
{
std::cout << "INITIATE ROOM CREATION" << std::endl;
roomTotal = 4;
//So the formula probably has something to do with rand and difficulty, plus a flat 2 at the end (Beginning and end)
//Again, is actually 1 over intended.
rWidth = 3;
rHeight = 3;
//Again, will be random for now. But this will do.
startXDist = rand() % (rWidth);
startYDist = rand() % (rHeight);
//For this, 0-2. Figures out the distance between the upper left of the room and the tunnel.
roomFX = firstTunX - startXDist;
roomFY = firstTunY - startYDist;
roomLX = roomFX + rWidth;
roomLY = roomFY + rHeight;
//These four make sure rooms are never off the map.
RoomBoundsCheck(xCap,yCap);
firstRoomULX = roomFX;
firstRoomULY = roomFY;
firstRoomLRX = roomLX;
firstRoomLRY = roomLY;
for (int rx=roomFX;rx<roomLX;rx++)
{
for (int ry=roomFY;ry<roomLY;ry++)
{
if (level[rx][ry] < roomType)
{
level[rx][ry] = roomType;
}
}
}
//Last room
rWidth = 3;
rHeight = 3;
//Again, will be random for now. But this will do.
startXDist = rand() % (rWidth);
startYDist = rand() % (rHeight);
//For this, 0-2. Figures out the distance between the upper left of the room and the tunnel.
roomFX = lastTunX - startXDist;
roomFY = lastTunY - startYDist;
roomLX = roomFX + rWidth;
roomLY = roomFY + rHeight;
//These four make sure rooms are never off the map.
//To do: Dedicate this map to the array.
/*
It seems it's not fond of me throwing double nested loops.
Or maybe it's the override it doesn't like?
//No, scratch all of that. It overrides just fine, it loops a nested loop fine. What seems to be
//the problem is that the initial variable can't equal another variable? And a while loop causes a crash.
//Hm.
*/
RoomBoundsCheck(xCap,yCap);
lastRoomULX = roomFX;
lastRoomULY = roomFY;
lastRoomLRX = roomLX;
lastRoomLRY = roomLY;
for (int rx=roomFX;rx<roomLX;rx++)
{
for (int ry=roomFY;ry<roomLY;ry++)
{
if (level[rx][ry] < roomType)
{
level[rx][ry] = roomType;
}
}
}
for(int r=1;r<roomTotal;r++)
{
//Makes the rest of the rooms.
rWidth = 3;
rHeight = 3;
//Will be random later.
startXDist = rand()%rWidth;
startYDist = rand()%rHeight;
//Should have a count of the amount of the tunnels.
roomXInv = rand()%2;
roomYInv = rand()%2;
if (roomXInv == 1)
{
startXDist = (startXDist)*-1;
}
if (roomYInv == 1)
{
startYDist = (startYDist)*-1;
}
roomFX = tunLocX[rand()%(tunCount-1)+1] + startXDist;
roomFY = tunLocY[rand()%(tunCount-1)+1] + startYDist;
//So any room but the first and last.
roomLX = roomFX + rWidth;
roomLY = roomFX + rHeight;
RoomBoundsCheck(xCap,yCap);
for (int rx=roomFX;rx<roomLX;rx++)
{
for (int ry=roomFY;ry<roomLY;ry++)
{
if (level[rx][ry] < roomType)
{
level[rx][ry] = roomType;
}
}
}
}
//Makes stairs (Spawn point for the player)
startStairX = rand()%(firstRoomLRX - firstRoomULX)+firstRoomULX;
startStairY = rand()%(firstRoomLRY - firstRoomULY)+firstRoomULY;
level[startStairX][startStairY] = 5;
endStairX = rand()%(lastRoomLRX - lastRoomULX)+lastRoomULX;
endStairY = rand()%(lastRoomLRY - lastRoomULY)+lastRoomULY;
level[endStairX][endStairY] = 6;
}
void DunGen::PlaceChest(int difficulty)
{
int chesttotal = rand()%(difficulty) + 1;
//The highest difficulty is about 20, I think, so the most you could have is 5.
int chestX = rand()%levelXCap;
int chestY = rand()%levelYCap;
for(int c=0;c<chesttotal;c++)
{
chestTexture[c].loadFromFile("sprites/dungeon_sprites/chest.png");
while (level[chestX][chestY] != 4 and level[chestX][chestY] == false)
{
//So to leave this loop, the point has to be a 4 and the occupation has to be false.
//Make sure it's on 4 a and doesn't overlap with other chests.
chestX = rand()%levelXCap;
chestY = rand()%levelYCap;
}
chestLocX[c] = chestX;
chestLocY[c] = chestY;
chestsActive++;
chestSprite[c].setTexture(chestTexture[c],false);
chestSprite[c].setPosition(chestX*50,chestY*50);
chestOccupied[chestX][chestY] = true;
}
}
void DunGen::Load(int xCap,int yCap)
{
ResetLevel();
levelXCap = xCap;
levelYCap = yCap;
//Also load from file
//Scratched. Just here for historical purposes.
}
void DunGen::ResetLevel()
{
for (int x=0;x<=100;x++)
{
for (int y=0;y<=100;y++)
{
level[x][y] = 0;
levelOccupied[x][y] = false;
//chestOccupied[x][y] = false;
}
}
/*chest0Created = false;
chest1Created = false;
chest2Created = false;
chest3Created = false;
chest4Created = false;*/
for (int l=0;l<levelSize;l++)
{
level_sprite[l] = sf::Vector2f(0,0);
}
}
void DunGen::TunUp()
{
if (tunY <= 0 or (level[tunX][tunY-1] == 3 and (level[tunX+1][tunY] != 3 or level[tunX-1][tunY] != 3)))
{
altDir = rand() % 2;
if (altDir == 0)
{
TunRight();
}
else if (altDir == 1)
{
TunLeft();
}
}
else
{
tunY -= 1;
direction = 0;
}
}
void DunGen::TunRight()
{
if (tunX >= levelXCap or (level[tunX+1][tunY] == 3 and (level[tunX][tunY+1] != 3 or level[tunX][tunY-1] != 3)))
{
altDir = rand() % 2;
if (altDir == 0)
{
TunUp();
}
else if (altDir == 1)
{
TunDown();
}
}
else
{
tunX += 1;
direction = 1;
}
}
void DunGen::TunDown()
{
if (tunY >= levelYCap or (level[tunX][tunY + 1] == 3 and (level[tunX+1][tunY] != 3 or level[tunX-1][tunY] != 3)))
{
altDir = rand() % 2;
if (altDir == 0)
{
TunRight();
}
else if (altDir == 1)
{
TunLeft();
}
}
else
{
tunY += 1;
direction = 2;
}
}
void DunGen::TunLeft()
{
if (tunX == 0 or (level[tunX - 1][tunY] == 3 and (level[tunX][tunY+1] != 3 or level[tunX][tunY-1] != 3)))
{
//First part checks the cap, second checks if the spot is occupied by a tile and if the spot is
altDir = rand() % 2;
if (altDir == 0)
{
TunUp();
}
else if (altDir == 1)
{
TunDown();
}
}
else
{
tunX -= 1;
direction = 3;
}
}
void DunGen::RoomBoundsCheck(int xCap,int yCap)
{
if (roomFX < 0)
{
roomLX -= roomFX;
roomFX -= roomFX;
//So -15 - (-15) = 0
}
if (roomLX > xCap)
{
roomFX -= roomLX - xCap;
roomLX -= roomLX - xCap;
}
if (roomFY < 0)
{
roomLY -= roomFY;
roomFY -= roomFY;
//So like, -5 - -5 would be 0.
}
if (roomLY > yCap)
{
roomFY -= roomLY - yCap;
roomLY -= roomLY - yCap;
}
}
void DunGen::SpriteGenerate()
{
for (int y=0;y<=levelYCap;y++)
{
for (int x=0;x<=levelXCap;x++)
{
std::cout << level[x][y] << ", ";
if (level[x][y] == 0)
{
//sprite[x][y].setTexture(blankTexture,false);
level_sprite[levelSize] = sf::Vector2f(x*50,y*50);
level_sprite[levelSize].color = sf::Color::Green;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50));
level_sprite[levelSize].color = sf::Color::Green;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50)+50);
level_sprite[levelSize].color = sf::Color::Green;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50),(y*50)+50);
level_sprite[levelSize].color = sf::Color::Green;
levelSize++;
//That pseudo code should do it.
//Oh boy. This'll be about 5000 pixels. Well, camera's easy enough to do with the player. Maybe I'll put the
//the player in the first 3 or 4 space I can find. Or last. Whatever.
//And let's see. Maybe make a color code? 2 and 3 don't exist, player is blue, 0 is green 4 is red, 3 is...whatever's left. White?
//I suppose the game loop will check if the player can or can't go to a certain spot.
}
/*
else if (level[x][y] == 1)
{
level_sprite[levelSize] = sf::Vector2f(x*50,y*50);
level_sprite[levelSize].color = sf::Color::Black;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50));
level_sprite[levelSize].color = sf::Color::Black;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50)+50);
level_sprite[levelSize].color = sf::Color::Black;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50),(y*50)+50);
level_sprite[levelSize].color = sf::Color::Black;
levelSize++;
}
else if (level[x][y] == 2)
{
level_sprite[levelSize] = sf::Vector2f(x*50,y*50);
level_sprite[levelSize].color = sf::Color::Cyan;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50));
level_sprite[levelSize].color = sf::Color::Cyan;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50)+50);
level_sprite[levelSize].color = sf::Color::Cyan;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50),(y*50)+50);
level_sprite[levelSize].color = sf::Color::Cyan;
levelSize++;
}*/
else if (level[x][y] == 3)
{
//sprite[x][y].setTexture(lvlTexture,false);
level_sprite[levelSize] = sf::Vector2f(x*50,y*50);
level_sprite[levelSize].color = sf::Color::Yellow;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50));
level_sprite[levelSize].color = sf::Color::Yellow;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50)+50);
level_sprite[levelSize].color = sf::Color::Yellow;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50),(y*50)+50);
level_sprite[levelSize].color = sf::Color::Yellow;
levelSize++;
}
else if (level[x][y] == 4)
{
//sprite[x][y].setTexture(lvlTexture,false);
level_sprite[levelSize] = sf::Vector2f(x*50,y*50);
level_sprite[levelSize].color = sf::Color::Red;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50));
level_sprite[levelSize].color = sf::Color::Red;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50)+50);
level_sprite[levelSize].color = sf::Color::Red;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50),(y*50)+50);
level_sprite[levelSize].color = sf::Color::Red;
levelSize++;
}
else if (level[x][y] == 5)
{
//sprite[x][y].setTexture(lvlTexture,false);
level_sprite[levelSize] = sf::Vector2f(x*50,y*50);
level_sprite[levelSize].color = sf::Color::Blue;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50));
level_sprite[levelSize].color = sf::Color::Blue;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50)+50);
level_sprite[levelSize].color = sf::Color::Blue;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50),(y*50)+50);
level_sprite[levelSize].color = sf::Color::Blue;
levelSize++;
}
else if (level[x][y] == 6)
{
//sprite[x][y].setTexture(stairTexture,false);
level_sprite[levelSize] = sf::Vector2f(x*50,y*50);
level_sprite[levelSize].color = sf::Color::Magenta;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50));
level_sprite[levelSize].color = sf::Color::Magenta;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50)+50,(y*50)+50);
level_sprite[levelSize].color = sf::Color::Magenta;
levelSize++;
level_sprite[levelSize] = sf::Vector2f((x*50),(y*50)+50);
level_sprite[levelSize].color = sf::Color::Magenta;
levelSize++;
}
/*if (chestOccupied[x][y] == true)
{
//Draw a chest in that area
if (chest0Created == false)
{
chest0Created = true;
}
else if (chest1Created == false)
{
chest1Created = true;
}
else if (chest2Created == false)
{
chest2Created = true;
}
else if (chest3Created == false)
{
chest3Created = true;
}
else if (chest4Created == false)
{
chest4Created = true;
}
}*/
}
std::cout << std::endl;
}
}