-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
755 lines (651 loc) · 25.3 KB
/
Program.cs
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
using System;
class Dungeon
{
private static int seed = Environment.TickCount;
private static System.Random random = new Random(seed);
private static float splitVariance = .5f; //used to determine variance in bsp splits
private static float sizeVariance = .5f; //used to determine variance in room size
private static float doorVariance = .5f; //used to determine variance in door location
private static int minSize = 4; //minimum size for rooms in any dimension
private static int depth = 4; //depth of recursion for bsp algorithm
private static int width = 80;
private static int height = 80;
private static List<Room> rooms = new List<Room>();
private static char currID = 'A'; //used to assign unique id to each room
static void Main(string[] args)
{
//parse any command line inputs, if there are any
if(args.Length > 0)
{
//if ParseInput() returns 1, then the program must exit
//this happens if the "-help" parameter is included anywhere or if there is an error
//in the users parameters
if(ParseInput(args) == 1)
{
return;
}
}
Console.WriteLine("Parameters (copy these parameters to save your dungeon):");
Console.WriteLine("Size:\t\t\t" + width + "x" + height);
Console.WriteLine("Depth:\t\t\t" + depth);
Console.WriteLine("Seed:\t\t\t" + seed);
Console.WriteLine("Minsize:\t\t" + minSize);
Console.WriteLine("Split variance:\t" + splitVariance);
Console.WriteLine("Size variance:\t" + sizeVariance);
Console.WriteLine("Door variance:\t" + doorVariance);
Console.WriteLine("");
Console.WriteLine("OR, better yet, just copy the arguments directly:");
Console.WriteLine("-size " + width + " " + height + " -depth " + depth + " -minsize " + minSize +
" -splitvar " + splitVariance + " -sizevar " + sizeVariance + " -doorvar " + doorVariance + " -seed " + seed);
Console.WriteLine("");
Console.WriteLine("Here's your dungeon:");
Console.WriteLine("");
//this is absolutely the stupidest way to do it, but i must've screwed up somewhere and
//gotten width and height mixed up (sigh). so basically, we're just gonna swap 'em and
//act like nothing's wrong.
int temp = width;
width = height;
height = temp;
//we start with a room that's the size of the entire area. it will be subdivided
Room startRoom = new Room(0, width - 1, 0, height - 1);
BinarySpacePartition(startRoom, depth);
Shrink();
Draw();
}
static int ParseInput(string[] args)
{
int index = 0;
while(index < args.Length)
{
switch(args[index])
{
case "-help":
Console.WriteLine("Dungeon Generator v1.0 Help:");
Console.WriteLine("");
Console.WriteLine("Arg Params : Description");
Console.WriteLine("");
Console.WriteLine("-size x y : sets width to x and height to y (default 80 80)");
Console.WriteLine("-depth d : sets recursive depth to d (default 4)");
Console.WriteLine(" depth determines how many rooms are generated. there will");
Console.WriteLine(" be 2^depth rooms");
Console.WriteLine("-seed s : sets seed to s (defaults to a random value)");
Console.WriteLine("-minsize ms : sets minimum room size (on either axis) to ms (default 4)");
Console.WriteLine("");
Console.WriteLine("For the following commands, both decimal and string values are accepted.");
Console.WriteLine("If a decimal value between 0 and 1 is provided, it will be used. Otherwise,");
Console.WriteLine("you can use 'none', 'low', 'med', 'high', and 'max'.");
Console.WriteLine("The default for all of these is 'med'.");
Console.WriteLine("");
Console.WriteLine("-splitvar spv : sets the split variance to spv");
Console.WriteLine("-sizevar szv : sets the size variance to szv");
Console.WriteLine("-doorvar dv : sets the door variance to dv");
Console.WriteLine("");
Console.WriteLine("Note: It is best to redirect your output to a file!");
Console.WriteLine("Use the '>' operator after the executable, and enter a filename to write to.");
return 1;
case "-size":
try
{
width = Int32.Parse(args[index + 1]);
height = Int32.Parse(args[index + 2]);
index++; //must increment index one additional amount
}
catch(FormatException)
{
InvalidInput(args[index], "", false);
return 1;
}
break;
case "-depth":
try
{
depth = Int32.Parse(args[index + 1]);
}
catch(FormatException)
{
InvalidInput(args[index], args[index + 1], false);
return 1;
}
break;
case "-seed":
try
{
seed = Int32.Parse(args[index + 1]);
random = new Random(seed);
}
catch(FormatException)
{
InvalidInput(args[index], args[index + 1], false);
return 1;
}
break;
case "-minsize":
try
{
minSize = Int32.Parse(args[index + 1]);
}
catch(FormatException)
{
InvalidInput(args[index], args[index + 1], false);
return 1;
}
break;
case "-splitvar":
if(args[index + 1] == "none")
{
splitVariance = 0f;
}
else if(args[index + 1] == "low")
{
splitVariance = .25f;
}
else if(args[index + 1] == "med")
{
splitVariance = .5f;
}
else if(args[index + 1] == "high")
{
splitVariance = .75f;
}
else if(args[index + 1] == "max")
{
splitVariance = 1f;
}
else
{
try
{
splitVariance = float.Parse(args[index + 1]);
if(splitVariance < 0 || splitVariance > 1)
{
throw new FormatException();
}
}
catch(FormatException)
{
InvalidInput(args[index], args[index + 1], false);
return 1;
}
}
break;
case "-sizevar":
if(args[index + 1] == "none")
{
sizeVariance = 0f;
}
else if(args[index + 1] == "low")
{
sizeVariance = .25f;
}
else if(args[index + 1] == "med")
{
sizeVariance = .5f;
}
else if(args[index + 1] == "high")
{
sizeVariance = .75f;
}
else if(args[index + 1] == "max")
{
sizeVariance = 1f;
}
else
{
try
{
sizeVariance = float.Parse(args[index + 1]);
if(sizeVariance < 0 || sizeVariance > 1)
{
throw new FormatException();
}
}
catch(FormatException)
{
InvalidInput(args[index], args[index + 1], false);
return 1;
}
}
break;
case "-doorvar":
if(args[index + 1] == "none")
{
doorVariance = 0f;
}
else if(args[index + 1] == "low")
{
doorVariance = .25f;
}
else if(args[index + 1] == "med")
{
doorVariance = .5f;
}
else if(args[index + 1] == "high")
{
doorVariance = .75f;
}
else if(args[index + 1] == "max")
{
doorVariance = 1f;
}
else
{
try
{
doorVariance = float.Parse(args[index + 1]);
if(doorVariance < 0 || doorVariance > 1)
{
throw new FormatException();
}
}
catch(FormatException)
{
InvalidInput(args[index], args[index + 1], false);
return 1;
}
}
break;
default:
InvalidInput(args[index], "", true);
return 1;
}
index += 2;
}
return 0;
}
static void InvalidInput(String arg, String value, bool noVal)
{
TextWriter errorWriter = Console.Error;
if(noVal)
{
errorWriter.WriteLine("ERROR: Invalid argument '" + arg + "'");
}
//slightly different error message for -size
else if(arg == "-size")
{
errorWriter.WriteLine("ERROR: Invalid parameters for '-size'");
}
else
{
errorWriter.WriteLine("ERROR: Parameter '" + value + "' is not valid for arg '" + arg + "'");
}
return;
}
static void BinarySpacePartition(Room room, int levelsToGo)
{
//check base cases first
//stop when we've reached the desired depth, or if the current room reached the min size
if(levelsToGo == 0 || room.rightWall - room.leftWall < minSize || room.topWall - room.bottomWall < minSize)
{
room.id = currID++;
rooms.Add(room);
return;
}
levelsToGo--;
//determine the direction upon which we are going to split. whichever wall is longer is
//the one being split
if(room.rightWall - room.leftWall > room.topWall - room.bottomWall)
{
float splitPoint = Split(room.leftWall, room.rightWall, splitVariance);
//using the split point, make two new rooms split down the line along that point
Room leftRoom = new Room(room.leftWall, splitPoint, room.bottomWall, room.topWall);
Room rightRoom = new Room(splitPoint, room.rightWall, room.bottomWall, room.topWall);
//since this room started as one big room, we need to make sure that doors end up in
//the appropriate room. any doors that were on the left side of the split should be a
//part of the left room, and vice versa
foreach(Door doorAt in room.doors)
{
Room toGetDoor;
if(doorAt.x < splitPoint)
{
toGetDoor = leftRoom;
}
else
{
toGetDoor = rightRoom;
}
toGetDoor.AddDoor(doorAt);
//since we're splitting vertically, and there is a specific allowed variance for
//door locations, we want to relocate a door so it is within the allowed variance.
//for example, with 0 variance, doors are always in the center of a room. vertical
//doors, however, would no longer be because we've split along the vertical axis.
//fortunately, we can use the same split function in order to move the door within
//the appropriate bounds
if(!doorAt.horizontal)
{
doorAt.x = Split(toGetDoor.leftWall, toGetDoor.rightWall, doorVariance);
}
}
//after any necessary doors have been added and relocated, we need to add a new set of
//doors between the two rooms we've just created. we've split vertically, so the doors
//are going to be horizontal. in both cases, their location along the y axis will be
//based on the door variance using the split function
Door leftDoor = new Door(splitPoint, Split(room.bottomWall, room.topWall, doorVariance), true);
Door rightDoor = new Door(splitPoint, Split(room.bottomWall, room.topWall, doorVariance), true);
leftDoor.AssignOtherDoor(rightDoor);
rightDoor.AssignOtherDoor(leftDoor);
leftRoom.doors.Add(leftDoor);
rightRoom.doors.Add(rightDoor);
//now recursively split these subrooms
BinarySpacePartition(leftRoom, levelsToGo);
BinarySpacePartition(rightRoom, levelsToGo);
}
else
{
//split across the opposite dimension. the code here is largely the same as above, just
//doing things in the x direction instead of y
float splitPoint = Split(room.bottomWall, room.topWall, splitVariance);
Room bottomRoom = new Room(room.leftWall, room.rightWall, room.bottomWall, splitPoint);
Room topRoom = new Room(room.leftWall, room.rightWall, splitPoint, room.topWall);
foreach(Door doorAt in room.doors)
{
Room toGetDoor;
if(doorAt.y < splitPoint)
{
toGetDoor = bottomRoom;
}
else
{
toGetDoor = topRoom;
}
toGetDoor.AddDoor(doorAt);
if(doorAt.horizontal)
{
doorAt.y = Split(toGetDoor.bottomWall, toGetDoor.topWall, doorVariance);
}
}
Door bottomDoor = new Door(Split(room.leftWall, room.rightWall, doorVariance), splitPoint, false);
Door topDoor = new Door(Split(room.leftWall, room.rightWall, doorVariance), splitPoint, false);
bottomDoor.AssignOtherDoor(topDoor);
topDoor.AssignOtherDoor(bottomDoor);
bottomRoom.AddDoor(bottomDoor);
topRoom.AddDoor(topDoor);
BinarySpacePartition(bottomRoom, levelsToGo);
BinarySpacePartition(topRoom, levelsToGo);
}
}
static float Split(float min, float max, float variance)
{
//get our random variance value, the amount from which we're deviating from the center
//with no variance, we will always split right down the middle of a room.
//the variance value will always be between -1 and 1. -1 inclusive, 1 exclusive
float randomVariance = (float) (random.NextDouble() * 2 - 1);
float midPoint = (min + max) / 2;
//we're using the variance that was randomly generated as well as the given varaince
//value, which will depend on the purposes for which the function is being used
midPoint += randomVariance * variance * (max - min) / 2;
return midPoint;
}
//function for drawing the map to the console
//
//'.' = void space
//' ' = open space
//'+' = room corner
//'O' = tunnel
//'H' = vertical door
//'I' = horizontal door
//'|' = vertical wall
//'-' = horizontal wall
static void Draw()
{
char[,] map = new char[width, height];
//begin by filling the entire space with void.
//the requisite parts will be drawn over later
for(int col = 0; col < width; ++col)
{
for(int row = 0; row < height; ++row)
{
map[col, row] = '.';
}
}
foreach(Room room in rooms)
{
int left = (int) room.leftWall;
int right = (int) room.rightWall;
int bottom = (int) room.bottomWall;
int top = (int) room.topWall;
//place horizontal lines for tops and bottoms of rooms, ignoring corners
for(int col = left + 1; col < right; ++col)
{
map[col, bottom] = '|';
map[col, top] = '|';
}
//place vertical lines for sides of rooms, ignoring corners
for(int row = bottom + 1; row < top; ++row)
{
map[left, row] = '-';
map[right, row] = '-';
}
//fill rooms with empty space
for(int col = left + 1; col < right; ++col)
{
for(int row = bottom + 1; row < top; ++row)
{
map[col, row] = ' ';
}
}
}
//apply corner tile for each room, as well as room name in the middle
foreach(Room room in rooms)
{
int left = (int) room.leftWall;
int right = (int) room.rightWall;
int bottom = (int) room.bottomWall;
int top = (int) room.topWall;
map[left, bottom] = '+';
map[right, bottom] = '+';
map[left, top] = '+';
map[right, top] = '+';
map[(left + right) / 2, (bottom + top) / 2] = room.id;
}
foreach(Room room in rooms)
{
foreach(Door door in room.doors)
{
int col = (int) door.x;
int row = (int) door.y;
if(door.horizontal)
{
map[col, row] = 'I';
}
else
{
map[col, row] = 'H';
}
ConnectDoors(map, door);
}
}
//now to actually print it to the console
for(int col = 0; col < width; ++col)
{
for(int row = 0; row < height; ++row)
{
Console.Write(map[col, row]);
}
Console.WriteLine();
}
}
static void ConnectDoors(char[,] map, Door door)
{
if(door.horizontal)
{
int midPoint = (int) door.divDim;
int fromCol = (int) door.x;
int toCol = (int) door.GetOtherDoor().x;
//for whatever reason, horizontal doors are always one row off
int fromRow = (int) door.y - 1;
int toRow = (int) door.GetOtherDoor().y - 1;
if(fromCol > toCol)
{
int temp = fromCol;
fromCol = toCol;
toCol = temp;
temp = fromRow;
fromRow = toRow;
toRow = temp;
}
fromRow++;
toRow++;
for(int col = fromCol + 1; col <= midPoint; ++col)
{
map[col, fromRow] = 'O';
}
for(int col = midPoint; col < toCol; ++col)
{
map[col, toRow] = 'O';
}
if(fromRow > toRow)
{
int temp = fromRow;
fromRow = toRow;
toRow = temp;
}
for(int row = fromRow + 1; row < toRow; ++row)
{
map[midPoint, row] = 'O';
}
}
else
{
int mid = (int) door.divDim;
//vertical doors are always one column off
int fromCol = (int) door.x - 1;
int toCol = (int) door.GetOtherDoor().x - 1;
int fromRow = (int) door.y;
int toRow = (int) door.GetOtherDoor().y;
if (fromRow > toRow)
{
int temp = fromCol;
fromCol = toCol;
toCol = temp;
temp = fromRow;
fromRow = toRow;
toRow = temp;
}
fromCol++;
toCol++;
for (int row = fromRow + 1; row <= mid; ++row)
{
map[fromCol, row] = 'O';
}
for (int row = mid; row < toRow; ++row)
{
map[toCol, row] = 'O';
}
if (fromCol > toCol)
{
int temp = fromCol;
fromCol = toCol;
toCol = temp;
}
for (int col = fromCol + 1; col < toCol; ++col)
{
map[col, mid] = 'O';
}
}
}
static void Shrink()
{
//for each room, shrink it horizontally and vertically by some amount, based on
//sizeVariance
foreach(Room room in rooms)
{
//shrink horizontally first
float horizScale = (float) (1 - sizeVariance * random.NextDouble());
float width = room.rightWall - room.leftWall;
float newWidth = horizScale * width;
//at minimum, each room will be made 2 units smaller, to create minimum space between
//each room
if (width - newWidth < 2)
{
newWidth = width - 2;
}
//but at the same time, rooms must conform to the minimum size
if (newWidth < minSize)
{
newWidth = minSize;
}
float centerWidth = (room.rightWall + room.leftWall) / 2;
room.rightWall = (centerWidth + newWidth / 2);
room.leftWall = (centerWidth - newWidth / 2);
//now do the same process, but vertically
float vertScale = (float)(1 - sizeVariance * random.NextDouble());
float height = room.topWall - room.bottomWall;
float newHeight = vertScale * height;
if (height - newHeight < 2)
{
newHeight = height - 2;
}
if (newHeight < minSize)
{
newHeight = minSize;
}
float centerHeight = (room.topWall + room.bottomWall) / 2;
room.topWall = (centerHeight + newHeight / 2);
room.bottomWall = (centerHeight - newHeight / 2);
horizScale = newWidth / width;
vertScale = newHeight / height;
//need to also adjust the doors so they're always inside a wall
foreach (Door door in room.doors)
{
float doorOffsetX = door.x - centerWidth;
door.x = horizScale * doorOffsetX + centerWidth;
float doorOffsetY = door.y - centerHeight;
door.y = vertScale * doorOffsetY + centerHeight;
}
}
}
}
class Room
{
public float leftWall;
public float rightWall;
public float topWall;
public float bottomWall;
public char id;
public List<Door> doors { get; }
public Room(float leftWall, float rightWall, float bottomWall, float topWall)
{
this.leftWall = leftWall;
this.rightWall = rightWall;
this.topWall = topWall;
this.bottomWall = bottomWall;
doors = new List<Door>();
}
public void AddDoor(Door door)
{
doors.Add(door);
}
}
class Door
{
//door variables are completely public because they made need to be changed
public float x;
public float y;
public float divDim;
public bool horizontal { get; }
private Door? other; //a door always must connect to another door
public Door(float x, float y, bool horizontal)
{
this.x = x;
this.y = y;
this.horizontal = horizontal;
if(horizontal)
{
divDim = x;
}
else
{
divDim = y;
}
}
public void AssignOtherDoor(Door other)
{
this.other = other;
}
public Door GetOtherDoor()
{
#pragma warning disable CS8603 //ignore the potential null return
return other;
#pragma warning restore CS8603
}
}