-
Notifications
You must be signed in to change notification settings - Fork 37
/
DungeonBuilder.cs
1702 lines (1615 loc) · 81.5 KB
/
DungeonBuilder.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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI;
using StardewValley;
using xTile;
using xTile.Dimensions;
using xTile.Layers;
using xTile.Tiles;
namespace Entoarox.DynamicDungeons
{
internal class DungeonBuilder
{
/*********
** Properties
*********/
private const float ChanceToStartAlive = 0.4f;
private const int DeathLimit = 3;
private const int BirthLimit = 4;
private const int SimulationSteps = 30;
private static readonly Random Random = new Random();
private Color[,] CMap;
private readonly double Difficulty;
private readonly int Floor;
private int Height;
private bool[,] Map;
private Random Seeder;
private int Width;
/*********
** Accessors
*********/
public int Seed { get; private set; }
/*********
** Public methods
*********/
public DungeonBuilder(double difficulty, int floor)
{
this.Difficulty = difficulty;
this.Floor = floor;
this.Seed = DungeonBuilder.Random.Next();
this.GenerateMap();
}
public DungeonBuilder(double difficulty, int floor, int seed)
{
this.Difficulty = difficulty;
this.Floor = floor;
this.Seed = seed;
this.GenerateMap();
}
public Texture2D GetMiniMap()
{
Color[] mapping = new Color[this.Width * this.Height];
for (int y = 0; y < this.Height; y++)
{
for (int x = 0; x < this.Width; x++)
{
if (this.CMap[x, y] == Color.White)
mapping[y * this.Width + x] = new Color(221, 148, 84);
else if (this.CMap[x, y] == Color.Black)
mapping[y * this.Width + x] = new Color(34, 17, 34);
else
mapping[y * this.Width + x] = this.CMap[x, y];
}
}
Texture2D texture = new Texture2D(Game1.graphics.GraphicsDevice, this.Width, this.Height);
texture.SetData(mapping);
return texture;
}
public Map GetMap()
{
this.Report("Generating map data...");
int[] floorTiles = { 137, 139, 140, 153, 154, 155, 169, 170, 171 };
Map map = new Map("DynamicDungeons_" + this.Seed.ToString("X8"));
map.Properties.Add("ViewportFollowPlayer", "True");
map.Properties.Add("Outdoors", "True");
TileSheet sheet = new TileSheet("VanillaSheet", map, "Mines\\mine", new Size(16, 18), new Size(16, 16));
sheet.TileIndexProperties[165].Add("Diggable", "T");
sheet.TileIndexProperties[181].Add("Diggable", "T");
sheet.TileIndexProperties[183].Add("Diggable", "T");
sheet.TileIndexProperties[165].Add("Type", "Dirt");
sheet.TileIndexProperties[181].Add("Type", "Dirt");
sheet.TileIndexProperties[183].Add("Type", "Dirt");
sheet.TileIndexProperties[275].Add("Water", "T");
sheet.TileIndexProperties[276].Add("Water", "T");
sheet.TileIndexProperties[277].Add("Water", "T");
sheet.TileIndexProperties[260].Add("Water", "T");
sheet.TileIndexProperties[240].Add("Type", "Wood");
sheet.TileIndexProperties[241].Add("Type", "Wood");
sheet.TileIndexProperties[242].Add("Type", "Wood");
sheet.TileIndexProperties[256].Add("Type", "Wood");
sheet.TileIndexProperties[257].Add("Type", "Wood");
sheet.TileIndexProperties[258].Add("Type", "Wood");
sheet.TileIndexProperties[272].Add("Type", "Wood");
sheet.TileIndexProperties[273].Add("Type", "Wood");
sheet.TileIndexProperties[274].Add("Type", "Wood");
map.AddTileSheet(sheet);
// Overlay tilesheet
TileSheet isheet = new TileSheet("InfoSheet", map, ModEntry.SHelper.Content.GetActualAssetKey("assets/sheet_info.png"), new Size(8, 1), new Size(16, 16));
map.AddTileSheet(isheet);
// Custom tilesheet
//var mysheet = new TileSheet("CustomSheet", map, DynamicDungeonsMod.SHelper.Content.GetActualAssetKey("assets/sheet_info.png"), new Size(8, 1), new Size(16, 16));
//map.AddTileSheet(isheet);
TileSheet bsheet = new TileSheet(map, ModEntry.SHelper.Content.GetActualAssetKey("assets/sheet_boulder.png"), new Size(6, 3), new Size(16, 16));
map.AddTileSheet(bsheet);
TileSheet csheet = new TileSheet(map, ModEntry.SHelper.Content.GetActualAssetKey("assets/sheet_cavein.png"), new Size(2, 2), new Size(16, 16));
map.AddTileSheet(csheet);
TileSheet s1sheet = new TileSheet(map, ModEntry.SHelper.Content.GetActualAssetKey("assets/sheet_stalagmite1.png"), new Size(1, 2), new Size(16, 16));
map.AddTileSheet(s1sheet);
TileSheet s2sheet = new TileSheet(map, ModEntry.SHelper.Content.GetActualAssetKey("assets/sheet_stalagmite2.png"), new Size(2, 3), new Size(16, 16));
map.AddTileSheet(s2sheet);
TileSheet vsheet = new TileSheet(map, ModEntry.SHelper.Content.GetActualAssetKey("assets/sheet_dwarfvendor.png"), new Size(8, 3), new Size(16, 16));
map.AddTileSheet(vsheet);
TileSheet sWalls = new TileSheet(map, ModEntry.SHelper.Content.GetActualAssetKey("assets/sheet_walls.png"), new Size(9, 24), new Size(16, 16));
map.AddTileSheet(sWalls);
TileSheet rsheet = new TileSheet(map, ModEntry.SHelper.Content.GetActualAssetKey("assets/sheet_tracks.png"), new Size(10, 3), new Size(16, 16));
rsheet.TileIndexProperties[3].Add("Passable", "T");
rsheet.TileIndexProperties[4].Add("Passable", "T");
rsheet.TileIndexProperties[5].Add("Passable", "T");
rsheet.TileIndexProperties[6].Add("Passable", "T");
rsheet.TileIndexProperties[7].Add("Passable", "T");
rsheet.TileIndexProperties[8].Add("Passable", "T");
rsheet.TileIndexProperties[9].Add("Passable", "T");
rsheet.TileIndexProperties[13].Add("Passable", "T");
rsheet.TileIndexProperties[14].Add("Passable", "T");
rsheet.TileIndexProperties[15].Add("Passable", "T");
rsheet.TileIndexProperties[16].Add("Passable", "T");
rsheet.TileIndexProperties[17].Add("Passable", "T");
rsheet.TileIndexProperties[18].Add("Passable", "T");
rsheet.TileIndexProperties[19].Add("Passable", "T");
rsheet.TileIndexProperties[27].Add("Passable", "T");
rsheet.TileIndexProperties[28].Add("Passable", "T");
rsheet.TileIndexProperties[29].Add("Passable", "T");
map.AddTileSheet(rsheet);
Layer floor = new Layer("Back", map, new Size(this.Width, this.Height), new Size(64, 64));
map.AddLayer(floor);
Layer wall = new Layer("Buildings", map, new Size(this.Width, this.Height), new Size(64, 64));
map.AddLayer(wall);
Layer front = new Layer("Front", map, new Size(this.Width, this.Height), new Size(64, 64));
map.AddLayer(front);
Layer afront = new Layer("AlwaysFront", map, new Size(this.Width, this.Height), new Size(64, 64));
map.AddLayer(afront);
Layer info = new Layer("MapInfo", map, new Size(this.Width, this.Height), new Size(64, 64));
map.AddLayer(info);
this.Report("Filling in floors and walls...");
for (int x = 0; x < this.Width; x++)
{
for (int y = 0; y < this.Height; y++)
{
// If not a void tile
if (this.CMap[x, y] != Color.Transparent)
{
if (floor.Tiles[x, y] == null)
floor.Tiles[x, y] = new StaticTile(floor, sheet, BlendMode.Additive, 138);
// If a wall tile
if (this.CMap[x, y] == Color.Black)
{
info.Tiles[x, y] = new StaticTile(info, isheet, BlendMode.Additive, 0);
byte wallState = this.GetSurroundingWalls(x, y);
switch (wallState)
{
// Bottom straight wall
case 0b000_11_111:
case 0b100_11_111:
case 0b001_11_111:
case 0b101_11_111:
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 77);
front.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 77);
front.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 214 + x % 2);
break;
// Top straight wall
case 0b111_11_000:
case 0b111_11_100:
case 0b111_11_001:
case 0b111_11_101:
if (this.Seeder.NextDouble() < 0.1)
{
if ((x + y) % 5 == 0)
{
front.Tiles[x, y - 3] = new StaticTile(wall, sheet, BlendMode.Additive, 73 + x % 3);
front.Tiles[x, y - 2] = new StaticTile(wall, sheet, BlendMode.Additive, 98);
front.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 114);
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 121 + x % 3);
}
else
{
front.Tiles[x, y - 3] = new StaticTile(wall, sheet, BlendMode.Additive, 7 + x % 2);
front.Tiles[x, y - 2] = new StaticTile(wall, sheet, BlendMode.Additive, 23 + x % 2);
front.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 39 + x % 2);
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 55 + x % 2);
}
}
else if (this.Seeder.NextDouble() < 0.3)
{
if (this.Seeder.NextDouble() < this.Difficulty / 25)
{
front.Tiles[x, y - 3] = new AnimatedTile(wall, new[]
{
new StaticTile(wall, sWalls, BlendMode.Additive, 108 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 144 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 180 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 144 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 108 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 108 + x % 9)
}, 250);
front.Tiles[x, y - 2] = new AnimatedTile(wall, new[]
{
new StaticTile(wall, sWalls, BlendMode.Additive, 117 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 153 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 189 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 153 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 117 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 117 + x % 9)
}, 250);
front.Tiles[x, y - 1] = new AnimatedTile(wall, new[]
{
new StaticTile(wall, sWalls, BlendMode.Additive, 126 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 162 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 198 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 162 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 126 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 126 + x % 9)
}, 250);
wall.Tiles[x, y] = new AnimatedTile(wall, new[]
{
new StaticTile(wall, sWalls, BlendMode.Additive, 135 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 171 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 207 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 171 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 135 + x % 9),
new StaticTile(wall, sWalls, BlendMode.Additive, 135 + x % 9)
}, 250);
}
else if (this.Seeder.NextDouble() < Math.Min(0.6, 0.05 + this.Difficulty / 18))
{
front.Tiles[x, y - 3] = new StaticTile(wall, sWalls, BlendMode.Additive, 72 + x % 9);
front.Tiles[x, y - 2] = new StaticTile(wall, sWalls, BlendMode.Additive, 81 + x % 9);
front.Tiles[x, y - 1] = new StaticTile(wall, sWalls, BlendMode.Additive, 90 + x % 9);
wall.Tiles[x, y] = new StaticTile(wall, sWalls, BlendMode.Additive, 99 + x % 9);
}
else if (this.Seeder.NextDouble() < Math.Min(0.8, 0.1 + this.Difficulty / 14))
{
front.Tiles[x, y - 3] = new StaticTile(wall, sWalls, BlendMode.Additive, 36 + x % 9);
front.Tiles[x, y - 2] = new StaticTile(wall, sWalls, BlendMode.Additive, 45 + x % 9);
front.Tiles[x, y - 1] = new StaticTile(wall, sWalls, BlendMode.Additive, 54 + x % 9);
wall.Tiles[x, y] = new StaticTile(wall, sWalls, BlendMode.Additive, 63 + x % 9);
}
else
{
front.Tiles[x, y - 3] = new StaticTile(wall, sWalls, BlendMode.Additive, 0 + x % 9);
front.Tiles[x, y - 2] = new StaticTile(wall, sWalls, BlendMode.Additive, 9 + x % 9);
front.Tiles[x, y - 1] = new StaticTile(wall, sWalls, BlendMode.Additive, 18 + x % 9);
wall.Tiles[x, y] = new StaticTile(wall, sWalls, BlendMode.Additive, 27 + x % 9);
}
}
else
{
front.Tiles[x, y - 3] = new StaticTile(wall, sheet, BlendMode.Additive, 73 + x % 3);
front.Tiles[x, y - 2] = new StaticTile(wall, sheet, BlendMode.Additive, 89 + x % 3);
front.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 105 + x % 3);
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 121 + x % 3);
}
floor.Tiles[x, y] = new StaticTile(floor, sheet, BlendMode.Additive, 218);
floor.Tiles[x, y + 1] = new StaticTile(floor, sheet, BlendMode.Additive, 234);
info.Tiles[x, y + 1] = new StaticTile(info, isheet, BlendMode.Additive, 4);
break;
// Top-left outer corner wall
case 0b111_11_110:
front.Tiles[x, y - 3] = new StaticTile(wall, sheet, BlendMode.Additive, 68);
front.Tiles[x, y - 2] = new StaticTile(wall, sheet, BlendMode.Additive, 84);
wall.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 100);
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 116);
floor.Tiles[x, y] = new StaticTile(floor, sheet, BlendMode.Additive, 218);
floor.Tiles[x, y + 1] = new StaticTile(floor, sheet, BlendMode.Additive, 234);
info.Tiles[x, y + 1] = new StaticTile(info, isheet, BlendMode.Additive, 4);
break;
// Top-right outer corner wall
case 0b111_11_011:
front.Tiles[x, y - 3] = new StaticTile(wall, sheet, BlendMode.Additive, 111);
front.Tiles[x, y - 2] = new StaticTile(wall, sheet, BlendMode.Additive, 127);
wall.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 143);
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 159);
break;
// Right straight wall
case 0b011_01_011:
case 0b011_01_111:
case 0b111_01_111:
case 0b111_01_011:
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 175 + 16 * (y % 4));
floor.Tiles[x, y] = new StaticTile(floor, sheet, BlendMode.Additive, 218);
floor.Tiles[x - 1, y] = new StaticTile(floor, sheet, BlendMode.Additive, 217);
if (wallState == 0b111_01_011)
floor.Tiles[x - 1, y] = new StaticTile(floor, sheet, BlendMode.Additive, 185);
info.Tiles[x - 1, y] = new StaticTile(info, isheet, BlendMode.Additive, 4);
break;
// Left straight wall
case 0b110_10_110:
case 0b110_10_111:
case 0b111_10_110:
case 0b111_10_111:
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 132 + 16 * (y % 4));
break;
// Bottom-right inner corner wall
case 0b110_1_0_000:
case 0b110_1_0_001:
front.Tiles[x, y - 3] = new StaticTile(wall, sheet, BlendMode.Additive, 71);
front.Tiles[x, y - 2] = new StaticTile(wall, sheet, BlendMode.Additive, 87);
front.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 103);
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 119);
floor.Tiles[x, y] = new StaticTile(floor, sheet, BlendMode.Additive, 219);
floor.Tiles[x, y + 1] = new StaticTile(floor, sheet, BlendMode.Additive, 235);
info.Tiles[x, y + 1] = new StaticTile(info, isheet, BlendMode.Additive, 4);
break;
// Bottom-left inner corner wall
case 0b011_0_1_000:
case 0b011_0_1_001:
front.Tiles[x, y - 3] = new StaticTile(wall, sheet, BlendMode.Additive, 72);
front.Tiles[x, y - 2] = new StaticTile(wall, sheet, BlendMode.Additive, 88);
front.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 104);
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 120);
floor.Tiles[x, y] = new StaticTile(floor, sheet, BlendMode.Additive, 218);
floor.Tiles[x - 1, y] = new StaticTile(floor, sheet, BlendMode.Additive, 217);
floor.Tiles[x - 1, y + 1] = new StaticTile(floor, sheet, BlendMode.Additive, 233);
floor.Tiles[x, y + 1] = new StaticTile(floor, sheet, BlendMode.Additive, 234);
info.Tiles[x, y + 1] = new StaticTile(info, isheet, BlendMode.Additive, 4);
info.Tiles[x - 1, y + 1] = new StaticTile(info, isheet, BlendMode.Additive, 4);
info.Tiles[x - 1, y] = new StaticTile(info, isheet, BlendMode.Additive, 4);
break;
// Top-left inner corner wall
case 0b000_0_1_011:
case 0b100_0_1_011:
front.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 220);
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 236);
floor.Tiles[x - 1, y] = new StaticTile(floor, sheet, BlendMode.Additive, 201);
floor.Tiles[x, y] = new StaticTile(floor, sheet, BlendMode.Additive, 202);
info.Tiles[x - 1, y] = new StaticTile(info, isheet, BlendMode.Additive, 4);
break;
// Top-right inner corner wall
case 0b000_1_0_110:
case 0b001_1_0_110:
front.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 197);
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 148);
break;
// Top-left outer corner wall
case 0b110_1_1_111:
front.Tiles[x, y - 2] = new StaticTile(wall, sheet, BlendMode.Additive, 180);
front.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 196);
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 77);
break;
// Top-right outer corner wall
case 0b011_1_1_111:
front.Tiles[x, y - 2] = new StaticTile(wall, sheet, BlendMode.Additive, 223);
front.Tiles[x, y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 206);
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 77);
break;
// Undefined wall type
default:
wall.Tiles[x, y] = new StaticTile(wall, sheet, BlendMode.Additive, 275);
break;
}
}
}
else
info.Tiles[x, y] = new StaticTile(info, isheet, BlendMode.Additive, 6);
}
}
this.Report("Spawning in structures....");
// Try to spawn structures
this.BuildStructure(ref map, "DirtPatch", 9, 9, 50, 2, 4, () => this.GetRandomDirt(ref floor, ref wall, ref sheet));
this.BuildStructure(ref map, "WaterPatch", 9, 9, 50, 1, 2, () => this.GetRandomWater(ref floor, ref wall, ref sheet));
this.BuildStructure(ref map, "WoodPatch", 7, 7, 50, 1, 2, () => this.GetRandomWood(ref floor, ref wall, ref sheet));
this.BuildStructure(ref map, "CobblePatch", 7, 7, 50, 1, 2, () => this.GetRandomCobble(ref floor, ref wall, ref sheet));
this.BuildStructure(ref map, "CoalCart", 7, 7, 8, 2, 3, new ITile[]
{
new STile(2, 0, front, sheet, 205),
new STile(3, 0, front, sheet, 215),
new STile(4, 0, front, sheet, 215),
new STile(5, 0, front, sheet, 197),
new STile(2, 1, wall, sheet, 72),
new STile(3, 1, wall, sheet, 73),
new STile(4, 1, wall, sheet, 74),
new STile(5, 1, wall, sheet, 71),
new STile(2, 2, wall, sheet, 88),
new STile(3, 2, wall, sheet, 130),
new STile(4, 2, wall, sheet, 131),
new STile(5, 2, wall, sheet, 87),
new STile(0, 3, floor, sheet, 193),
new STile(1, 3, floor, sheet, 226),
new STile(2, 3, wall, sheet, 104),
new STile(3, 3, wall, sheet, 146),
new STile(4, 3, wall, sheet, 147),
new STile(5, 3, wall, sheet, 103),
new STile(1, 4, floor, sheet, 228),
new STile(2, 4, wall, sheet, 120),
new STile(2, 4, front, sheet, 179),
new STile(3, 4, wall, sheet, 162),
new STile(3, 4, front, sheet, 208),
new STile(4, 4, wall, sheet, 163),
new STile(5, 4, wall, sheet, 119),
new STile(1, 5, floor, sheet, 227),
new STile(2, 5, floor, sheet, 210),
new STile(2, 5, wall, sheet, 195),
new STile(3, 5, floor, sheet, 210),
new STile(3, 5, wall, sheet, 224),
new STile(4, 5, floor, sheet, 210)
});
this.BuildStructure(ref map, "SmallHole", 4, 4, 10, 3, 5, new ITile[]
{
new STile(1, 1, floor, sheet, 77),
new STile(1, 2, floor, sheet, 77),
new STile(2, 1, floor, sheet, 77),
new STile(2, 2, floor, sheet, 77),
new STile(1, 1, wall, sheet, 243),
new STile(2, 1, wall, sheet, 245),
new STile(1, 2, wall, sheet, 278),
new STile(2, 2, wall, sheet, 280)
});
this.BuildStructure(ref map, "SmallStalagmite", 3, 3, 10, 4, 8, new ITile[]
{
new STile(1, 0, front, s1sheet, 0),
new STile(1, 1, wall, s1sheet, 1)
});
this.BuildStructure(ref map, "LargeStalagmite", 4, 3, 10, 3, 6, new ITile[]
{
new STile(1, -1, afront, s2sheet, 0),
new STile(2, -1, afront, s2sheet, 1),
new STile(1, 0, front, s2sheet, 2),
new STile(2, 0, front, s2sheet, 3),
new STile(1, 1, wall, s2sheet, 4),
new STile(2, 1, wall, s2sheet, 5)
});
this.BuildStructure(ref map, "LootChest", 3, 3, 5, 1, 2, new ITile[]
{
new STile(0, 0, floor, sheet, 240),
new STile(1, 0, floor, sheet, 241),
new STile(2, 0, floor, sheet, 242),
new STile(0, 1, floor, sheet, 256),
new STile(1, 1, floor, sheet, 257),
new STile(2, 1, floor, sheet, 258),
new STile(0, 2, floor, sheet, 272),
new STile(1, 2, floor, sheet, 273),
new STile(2, 2, floor, sheet, 274),
new STile(1, 1, wall, sheet, 237),
new PTile(1, 1, wall, "Action", "DDLoot General 3 true 0.3 Mimic")
});
this.BuildStructure(ref map, "CavedSkeleton", 4, 4, 5, 1, 2, new ITile[]
{
new STile(1, 1, wall, csheet, 0),
new STile(2, 1, wall, csheet, 1),
new STile(1, 2, wall, csheet, 2),
new STile(2, 2, wall, csheet, 3),
new PTile(1, 2, wall, "Action", "DDLoot Supplies 2 0.1 Skeleton"),
new PTile(2, 2, wall, "Action", "DDLoot Supplies 2 0.1 Skeleton")
});
this.BuildStructure(ref map, "GemRock", 5, 4, 3, 0, 1, new ITile[]
{
new ATile(1, 0, front, new[]
{
new STile(0, 0, front, bsheet, 0),
new STile(0, 0, front, bsheet, 3)
}, 500),
new ATile(2, 0, front, new[]
{
new STile(0, 0, front, bsheet, 1),
new STile(0, 0, front, bsheet, 4)
}, 500),
new ATile(3, 0, front, new[]
{
new STile(0, 0, front, bsheet, 2),
new STile(0, 0, front, bsheet, 5)
}, 500),
new ATile(1, 1, wall, new[]
{
new STile(0, 0, wall, bsheet, 6),
new STile(0, 0, wall, bsheet, 9)
}, 500),
new ATile(2, 1, wall, new[]
{
new STile(0, 0, wall, bsheet, 7),
new STile(0, 0, wall, bsheet, 10)
}, 500),
new ATile(3, 1, wall, new[]
{
new STile(0, 0, wall, bsheet, 8),
new STile(0, 0, wall, bsheet, 11)
}, 500),
new ATile(1, 2, wall, new[]
{
new STile(0, 0, wall, bsheet, 12),
new STile(0, 0, wall, bsheet, 15)
}, 500),
new ATile(2, 2, wall, new[]
{
new STile(0, 0, wall, bsheet, 13),
new STile(0, 0, wall, bsheet, 16)
}, 500),
new ATile(3, 2, wall, new[]
{
new STile(0, 0, wall, bsheet, 14),
new STile(0, 0, wall, bsheet, 17)
}, 500),
new PTile(1, 2, wall, "Action", "DDLoot Gems 1"),
new PTile(2, 2, wall, "Action", "DDLoot Gems 1"),
new PTile(3, 2, wall, "Action", "DDLoot Gems 1")
});
if (this.Floor % 3 == 0)
{
this.BuildStructure(ref map, "DwardVendor", 4, 3, 50, 1, 1, new ITile[]
{
new ATile(0, -1, afront, new[]
{
new STile(0, 0, afront, vsheet, 0),
new STile(0, 0, afront, vsheet, 2),
new STile(0, 0, afront, vsheet, 10),
new STile(0, 0, afront, vsheet, 18),
new STile(0, 0, afront, vsheet, 4),
new STile(0, 0, afront, vsheet, 12),
new STile(0, 0, afront, vsheet, 20),
new STile(0, 0, afront, vsheet, 6),
new STile(0, 0, afront, vsheet, 14),
new STile(0, 0, afront, vsheet, 22)
}, 500),
new ATile(1, -1, afront, new[]
{
new STile(0, 0, afront, vsheet, 1),
new STile(0, 0, afront, vsheet, 3),
new STile(0, 0, afront, vsheet, 11),
new STile(0, 0, afront, vsheet, 19),
new STile(0, 0, afront, vsheet, 5),
new STile(0, 0, afront, vsheet, 13),
new STile(0, 0, afront, vsheet, 21),
new STile(0, 0, afront, vsheet, 7),
new STile(0, 0, afront, vsheet, 15),
new STile(0, 0, afront, vsheet, 23)
}, 500),
new STile(0, 0, front, vsheet, 8),
new STile(1, 0, front, vsheet, 9),
new STile(0, 1, wall, vsheet, 16),
new STile(1, 1, wall, vsheet, 17),
new PTile(0, 1, wall, "Action", "DDShop DwarfVendor"),
new PTile(1, 1, wall, "Action", "DDShop DwarfVendor")
});
}
for (int c = 0; c < this.Seeder.Next(3, 7); c++)
this.GenerateTrack(map, rsheet);
// Randomise floor
for (int x = 0; x < this.Width; x++)
{
for (int y = 0; y < this.Height; y++)
{
if (floor.Tiles[x, y] != null && floor.Tiles[x, y].TileIndex == 138)
{
if (info.Tiles[x, y] == null)
info.Tiles[x, y] = new StaticTile(info, isheet, BlendMode.Additive, 1);
if (this.Seeder.NextDouble() < 0.05)
floor.Tiles[x, y].TileIndex = floorTiles[x * y % floorTiles.Length];
}
}
}
Point entry = this.GetFloorPoint();
front.Tiles[entry.X, entry.Y - 4] = new StaticTile(wall, sheet, BlendMode.Additive, 67);
front.Tiles[entry.X, entry.Y - 3] = new StaticTile(wall, sheet, BlendMode.Additive, 83);
front.Tiles[entry.X, entry.Y - 2] = new StaticTile(wall, sheet, BlendMode.Additive, 99);
wall.Tiles[entry.X, entry.Y - 1] = new StaticTile(wall, sheet, BlendMode.Additive, 115);
wall.Tiles[entry.X, entry.Y - 1].Properties.Add("Action", "DDExit");
floor.Tiles[entry.X, entry.Y] = new StaticTile(floor, sheet, BlendMode.Additive, 204);
info.Tiles[entry.X, entry.Y] = new StaticTile(info, isheet, BlendMode.Additive, 7);
return map;
}
public Point GetFloorPoint()
{
for (int y = 0; y < this.Height; y++)
{
for (int x = 0; x < this.Width; x++)
{
if (this.CMap[x, y] == Color.White)
return new Point(x, y);
}
}
return default;
}
/*********
** Protected methods
*********/
private void Report(string message)
{
ModEntry.SMonitor.Log("DDGenerator: " + message, LogLevel.Info);
}
private void ColorConvert()
{
this.CMap = new Color[this.Width, this.Height];
for (int x = 0; x < this.Width; x++)
{
for (int y = 0; y < this.Height; y++)
this.CMap[x, y] = this.Map[x, y] ? Color.Black : Color.White;
}
}
private void GenerateMap()
{
this.Report("Trying to generate from seed: " + this.Seed.ToString("X8"));
// Setup the seeder that this map will be using
this.Seeder = new Random(this.Seed);
// Define the width & height
this.Width = this.Seeder.Next(40, 48);
this.Height = this.Seeder.Next(40, 48);
// Setup the map
this.Map = new bool[this.Width, this.Height];
// Seed the initial automaton state
this.Report("Creating seed map...");
for (int x = 0; x < this.Width; x++)
{
for (int y = 0; y < this.Height; y++)
{
if (this.Seeder.NextDouble() < DungeonBuilder.ChanceToStartAlive)
this.Map[x, y] = true;
}
}
this.Report("Simulating map...");
this.Simulate(1);
}
private void Simulate(int attempt)
{
this.Report("Attempt " + attempt + ": Simulation part 1...");
for (int s = 0; s < DungeonBuilder.SimulationSteps; s++)
this.NextStep();
this.Report("Attempt " + attempt + ": Simulation part 2...");
for (int s = 0; s < DungeonBuilder.SimulationSteps * 4; s++)
this.NextClean();
this.Report("Attempt " + attempt + ": Converting structure...");
this.ColorConvert();
// Perform flood filling to find & isolate a large dungeon region
this.Report("Attempt " + attempt + ": Trying to validate map...");
if (!this.FloodFill())
{
if (attempt < 3)
{
this.Report("Attempt " + attempt + " failed to validate, trying again...");
this.Simulate(attempt + 1);
}
else
{
this.Report("Attempt " + attempt + " failed to validate, retry limit reached, discarding map and starting over...");
this.GenerateMap();
}
}
else
{
this.Report("Attempt " + attempt + ": Validation success!");
this.Report("Y-Scaling...");
this.Upscale();
this.Report("Encapsulating...");
this.Encapsulate();
this.Report("Removing wall-locked tiles...");
this.WipeBounds();
this.Report("Map generation complete");
}
}
private Color GetTileColor(int x, int y)
{
return x < 0 || y < 0 || x >= this.Width || y >= this.Height
? Color.Black
: this.CMap[x, y];
}
private int GetColorCount(Color color)
{
int count = 0;
for (int x = 0; x < this.Width; x++)
{
for (int y = 0; y < this.Height; y++)
{
if (this.CMap[x, y] == color)
count++;
}
}
return count;
}
private Color GetNeighborsColor(int x, int y)
{
return x < 0 || y < 0 || x >= this.Width || y >= this.Height
? Color.Transparent
: this.CMap[x, y];
}
private int GetNeighborCountColor(int x, int y)
{
int count = 0;
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
{
if (i == 0 && j == 0)
continue;
if (this.GetTileColor(x + i, y + j) != Color.White)
count++;
}
return count;
}
private bool GetNeighbor(int x, int y)
{
return x < 0 || y < 0 || x >= this.Width || y >= this.Height || this.Map[x, y];
}
private int GetNeighborCount(int x, int y)
{
int count = 0;
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++)
{
if (i == 0 && j == 0)
continue;
if (this.GetNeighbor(x + i, y + j))
count++;
}
return count;
}
private int GetNeighborCountX(int x, int y)
{
int count = 0;
if (this.GetNeighbor(x - 1, y))
count++;
if (this.GetNeighbor(x + 1, y))
count++;
return count;
}
private int GetNeighborCountY(int x, int y)
{
int count = 0;
if (this.GetNeighbor(x, y - 1))
count++;
if (this.GetNeighbor(x, y + 2))
count++;
return count;
}
private void NextStep()
{
bool[,] updated = new bool[this.Width, this.Height];
Parallel.For(0, this.Width, x =>
{
Parallel.For(0, this.Height, y =>
{
int count = this.GetNeighborCount(x, y);
if (this.Map[x, y])
updated[x, y] = count >= DungeonBuilder.DeathLimit;
else
updated[x, y] = count > DungeonBuilder.BirthLimit;
});
});
this.Map = updated;
}
private void NextClean()
{
bool[,] updated = new bool[this.Width, this.Height];
Parallel.For(0, this.Width, x =>
{
Parallel.For(0, this.Height, y =>
{
if (this.Map[x, y])
{
int xc = this.GetNeighborCountX(x, y);
int yc = this.GetNeighborCountY(x, y);
updated[x, y] = xc + yc >= 3 || (xc != 0 && yc != 0);
}
else
updated[x, y] = false;
});
});
this.Map = updated;
}
private bool FloodFill()
{
for (int x = 0; x < this.Width; x++)
for (int y = 0; y < this.Height; y++)
if (this.CMap[x, y] == Color.White)
{
int floods = this.Flood(x, y);
if (floods > this.Width * this.Height / 3)
{
this.FloodAway(Color.White);
this.FloodAway(Color.Red, Color.White);
this.Report("Validation found a valid region: " + floods);
return true;
}
this.Report("Validation failed to find a valid region, retrying...");
this.FloodAway(Color.Red);
return this.FloodFill();
}
this.Report("Validation attempt failed, returning control to the map generator...");
this.Seed = this.Seeder.Next();
return false;
}
private void FloodAway(Color color, Color? replacement = null)
{
Color outColor = replacement ?? Color.Black;
Color[,] updated = new Color[this.Width, this.Height];
Parallel.For(0, this.Width, x =>
{
Parallel.For(0, this.Height, y =>
{
if (this.CMap[x, y] == color)
updated[x, y] = outColor;
else
updated[x, y] = this.CMap[x, y];
});
});
this.CMap = updated;
}
private int Flood(int x, int y)
{
this.CMap[x, y] = Color.Red;
int floods = 1;
if (this.GetTileColor(x - 1, y) == Color.White)
floods += this.Flood(x - 1, y);
if (this.GetTileColor(x + 1, y) == Color.White)
floods += this.Flood(x + 1, y);
if (this.GetTileColor(x, y - 1) == Color.White)
floods += this.Flood(x, y - 1);
if (this.GetTileColor(x, y + 1) == Color.White)
floods += this.Flood(x, y + 1);
return floods;
}
private void Encapsulate()
{
Color[,] updated = new Color[this.Width + 8, this.Height + 8];
Parallel.For(0, this.Width + 8, x =>
{
Parallel.For(0, this.Height + 8, y =>
{
updated[x, y] = Color.Black;
});
});
Parallel.For(0, this.Width, x =>
{
Parallel.For(0, this.Height, y =>
{
updated[x + 4, y + 4] = this.CMap[x, y];
});
});
this.Width += 8;
this.Height += 8;
this.CMap = updated;
}
private void WipeBounds()
{
Color[,] updated = new Color[this.Width, this.Height];
Parallel.For(0, this.Width, x =>
{
Parallel.For(0, this.Height, y =>
{
if (this.CMap[x, y] != Color.White && this.GetNeighborCountColor(x, y) == 8)
updated[x, y] = Color.Transparent;
else
updated[x, y] = this.CMap[x, y];
});
});
this.CMap = updated;
}
private void Upscale()
{
Color[,] updated = new Color[this.Width * 2, this.Height * 2];
Parallel.For(0, this.Width * 2, x =>
{
Parallel.For(0, this.Height * 2, y =>
{
updated[x, y] = this.CMap[(int)Math.Floor(x / 2f), (int)Math.Floor(y / 2f)];
});
});
this.Height *= 2;
this.Width *= 2;
this.CMap = updated;
}
private byte GetSurroundingWalls(int x, int y)
{
byte count = 0;
byte add = 128;
for (int j = -1; j < 2; j++)
for (int i = -1; i < 2; i++)
{
if (i == 0 && j == 0)
continue;
if (this.GetTileColor(x + i, y + j) != Color.White)
count += add;
add /= 2;
}
return count;
}
private Point? GetRandomGroundPoint(ref Layer floor, ref Layer wall)
{
for (int x = this.Seeder.Next(0, this.Width - 1); x < this.Width; x++)
{
for (int y = this.Seeder.Next(0, this.Height - 1); y < this.Height; y++)
{
if (floor.Tiles[x, y] != null && floor.Tiles[x, y].TileIndex == 138 && wall.Tiles[x, y] == null)
return new Point(x, y);
}
}
return null;
}
private bool FindRegionOfSize(ref Map map, int width, int height, int weight, out Point point)
{
point = default;
Layer floor = map.GetLayer("Back");
Layer wall = map.GetLayer("Buildings");
for (float t = 0; t <= weight; t++)
{
Point? origin = this.GetRandomGroundPoint(ref floor, ref wall);
if (origin != null)
return this.FindRegionOfSize(ref map, width, height, weight, (Point)origin, out point);
}
return false;
}
private bool FindRegionOfSize(ref Map map, int width, int height, int weight, Point start, out Point point)
{
point = default;
Layer floor = map.GetLayer("Back");
Layer wall = map.GetLayer("Buildings");
bool invalid = false;
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
{
if (floor.Tiles[start.X + i, start.Y + j] == null || floor.Tiles[start.X + i, start.Y + j].TileIndex != 138 || wall.Tiles[start.X + i, start.Y + j] != null)
invalid = true;
if (invalid)
break;
}
if (!invalid)
{
point = start;
return true;
}
return false;
}
private void BuildStructure(Map map, string name, int width, int height, int weight, int minSpawn, int maxSpawn, Tiles.Tile[] tiles)
{
for (int c = minSpawn; c < maxSpawn; c++)
if (this.FindRegionOfSize(ref map, width, height, weight, out Point point))
{
this.Report("Structure spawned: " + name);
foreach (Tiles.Tile tile in tiles)
tile.Apply(point.X, point.Y, map);
}
else
this.Report("Spawn attempt failed: " + name);
}
private void BuildStructure(ref Map map, string name, int width, int height, int weight, int minSpawn, int maxSpawn, ITile[] tiles)
{
this.BuildStructure(ref map, name, width, height, weight, minSpawn, maxSpawn, () => tiles);
}
private void BuildStructure(ref Map map, string name, int width, int height, int weight, int minSpawn, int maxSpawn, Func<ITile[]> tiles)
{