-
Notifications
You must be signed in to change notification settings - Fork 0
/
placerooms.pl
2129 lines (2064 loc) · 80 KB
/
placerooms.pl
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
#!/usr/bin/perl -w
# -*- cperl -*-
# Inspired by this article:
# https://www.rockpapershotgun.com/2015/07/28/how-do-roguelikes-generate-levels/
use strict;
use utf8;
use Term::ANSIColor;
use Carp;
use open ':encoding(UTF-8)';
use open ":std";
$|=1;
my %arg = @ARGV;
my $debug = $arg{debug} || 0;
my $xmax = $arg{xmax} || $arg{COLNO} || 79;
my $ymax = $arg{ymax} || $arg{ROWNO} || 20;
my $unicode = $arg{unicode} ? "yes" : $arg{ascii} ? undef : "yes";
my $headcolor = $arg{headcolor} || "bold cyan";
my $placerand = $arg{placerand} || 30;
# Precalculate some ranges, as a minor optimization:
my @rndx = randomorder(1 .. $xmax);
my @rndy = randomorder(1 .. $ymax);
my @rndxpos = randomorder((-1 * $xmax) .. $xmax);
my @rndypos = randomorder((-1 * $ymax) .. $ymax);
my %wdir = ( E => +{ bit => 1, dx => 1, dy => 0, clockwise => 'S', },
N => +{ bit => 2, dx => 0, dy => -1, clockwise => 'E', },
W => +{ bit => 4, dx => -1, dy => 0, clockwise => 'N', },
S => +{ bit => 8, dx => 0, dy => 1, clockwise => 'W', },
);
my @dir_available = keys %wdir;
my @wallglyph = ($arg{isolatedwallchar} || $arg{wallchar} || "-", # 0 => does not connect
$arg{hwallcharwestend} || $arg{hwallchar} || $arg{wallchar} || ($unicode ? "─" : "-"), # 1 => connects east
$arg{vwallcharsouthend} || $arg{vwallchar} || $arg{wallchar} || ($unicode ? "│" : "|"), # 2 => connects north
$arg{swcorner} || $arg{wallchar} || ($unicode ? "└" : "-"), # 3 = 1 + 2 => connects east and north
$arg{hwallchareastend} || $arg{hwallchar} || $arg{wallchar} || ($unicode ? "─" : "-"), # 4 => connects west
$arg{hwallchar} || $arg{wallchar} || ($unicode ? "─" : "-"), # 5 = 1 + 4 => connects east and west
$arg{secorner} || $arg{wallchar} || ($unicode ? "┘" : "-"), # 6 = 2 + 4 => connects north and west
$arg{twallcharnorth} || $arg{wallchar} || ($unicode ? "┴" : "-"), # 7 = 1 + 2 + 4 => connects east, north, and west.
$arg{vwallcharnorthend} || $arg{vwallchar} || $arg{wallchar} || ($unicode ? "│" : "|"), # 8 => connects south
$arg{nwcorner} || $arg{wallchar} || ($unicode ? "┌" : "-"), # 9 = 1 + 8 => connects east and south
$arg{vwallchar} || $arg{wallchar} || ($unicode ? "│" : "|"), # 10 = 2 + 8 => connects north and south
$arg{twallcharwest} || $arg{wallchar} || ($unicode ? "├" : "|"), # 11 = 1 + 2 + 8 => connects east, north, and south
$arg{necorner} || $arg{wallchar} || ($unicode ? "┐" : "-"), # 12 = 4 + 8 => connects west and south
$arg{twallcharsouth} || $arg{wallchar} || ($unicode ? "┬" : "-"), # 13 = 1 + 4 + 8 => connects east, west, and south
$arg{twallchareast} || $arg{wallchar} || ($unicode ? "┤" : "|"), # 14 = 2 + 4 + 8 => connects north, west, and south
$arg{crosswallchar} || $arg{wallchar} || ($unicode ? "┼" : "-"), # 15 = 1 + 2 + 4 + 8 => connects all four directions.
);
my %walkable = map { $_ => "true" } qw(FLOOR CORR SCORR DOOR SDOOR SHALLOW);
my %solid = map { $_ => "true" } qw(STONE WALL);
my $roomno = 1;
my $roomcountgoal = int(5 + $xmax / 25) * int(2 + $ymax / 10);
if (($xmax >= 100) and ($ymax >= 30)) {
$roomcountgoal = int((($roomcountgoal + 2) / 3) + rand($roomcountgoal * 2 / 3));
}
my $level = +{
title => "First Room",
map => ((45 > int rand 100) ? generate_cavern($roomno, $xmax, $ymax) :
(10 > int rand 100) ? quadrangle_room($roomno) :
(85 > int rand 100) ? barbell_room($roomno) : generate_room($roomno)),
};
showlevel($level) if $debug =~ /placement/;
for (1 .. $roomcountgoal) {
print ":" if $debug =~ /dots/;
my $room = generate_room($roomno++);
my $newlev = add_room_to_level($level, $room);
if ($newlev) {
$level = $newlev;
$$level{title} = "Level After " . $roomno . " Rooms";
showlevel($level) if $debug =~ /placement/;
} elsif ($debug =~ /placement/) {
showlevel(+{ title => "Unplaced Room", map => $room })
if $debug =~ /unplaced/;
print "Could not place room $roomno.\n";
}
}
if ((90 > rand 100) or ($debug =~ /fixcorr/)) {
$$level{map} = fix_dead_corridors($$level{map});
if ($debug =~ /corr/) {
showlevel(+{title => "Fixed Dead Corridors", map => $$level{map}});
}
}
my $lakeprob = $arg{lakeprob} || (($debug =~ /lake/) ? 100 : 5);
if ($lakeprob >= rand 100) {
for my $lakenum (1 .. (($debug =~ /lake/) ? 2 : 0) + int rand(($xmax + $ymax) / 20)) {
if ((15 > int rand 100) or ($debug =~ /unconditional/)) {
# Do a small but completely unconditional lake; we rely on the
# fact that the lake itself has shallow water going all around the
# edge, meaning you can always circumnavigate it, to ensure that
# it does not make the level impossible to traverse. This works
# for unconditional lakes, because the entire lake is drawn,
# including the parts that pass through former walls and stone.
print "Unconditional lake.\n" if $debug =~ /lake/;
my ($lake, $deep, $shallow)
= generate_lake(2 + int rand($xmax / 10),
1 + int rand($ymax / 5));
my ($lxmin, $lymin, $lxmax, $lymax) = getextrema($lake);
my $lxsize = $lxmax - $lxmin + 1;
my $lysize = $lymax - $lymin + 1;
my $lxpos = 2 + int rand($xmax - $lxsize - 1);
my $lypos = 2 + int rand($ymax - $lysize - 1);
for my $x (0 .. ($lxsize - 1)) {
for my $y (0 .. ($lysize - 1)) {
if ($$lake[$lxmin + $x][$lymin + $y]{type} ne "UNDECIDED") {
$$level{map}[$lxpos + $x][$lypos + $y] = $$lake[$lxmin + $x][$lymin + $y];
}
}
}
} else {
# Try successively smaller lakes until we can position one in a way
# that doesn't block the player from traversing the level.
my $lxmax = 3 + int($xmax * 3 / 4);
my $lymax = 2 + int($ymax * 2 / 3);
my ($lxsize, $lysize) = ($lxmax, $lymax);
my $done = 0;
while ((not $done) and ($lxmax > 3) and ($lymax > 2)) {
my ($lake, $deep, $shallow) = generate_lake($lxmax, $lymax);
my ($lxa, $lya, $lxb, $lyb) = getextrema($lake);
if ($debug =~ /lake/) {
showlevel(+{ title => "Conditional Lake (Unplaced)", map => $lake});
print "Lake Extrema: ($lxa, $lya, $lxb, $lyb)\n";
}
my $tries = 20;
while ((not $done) and ($tries-- > 0)) {
$lxsize = $lxb - $lxa + 1;
$lysize = $lyb - $lya + 1;
my $lxpos = 2 + int rand($xmax - $lxsize - 4);
my $lypos = 1 + int rand($ymax - $lysize - 2);
my $combined = copy_map($$level{map});
my @edgespot;
for my $x (0 .. ($lxsize - 1)) {
for my $y (0 .. ($lysize - 1)) {
if ($$lake[$lxa + $x][$lya + $y]{type} ne "UNDECIDED") {
if (not (($solid{$$combined[$lxpos + $x][$lypos + $y]{type}}) or
($$combined[$lxpos + $x][$lypos + $y]{type} eq "UNDECIDED") or
($$combined[$lxpos + $x][$lypos + $y]{type} eq "DOOR") or
($$combined[$lxpos + $x][$lypos + $y]{type} eq "SDOOR"))) {
$$combined[$lxpos + $x][$lypos + $y] = $$lake[$lxa + $x][$lya + $y];
push @edgespot, [$x, $y] if $walkable{$$lake[$lxa + $x][$lya + $y]{type}};
}}}}
if ($debug =~ /lake/) {
print "Lake Size ($lxsize,$lysize); Position ($lxpos,$lypos).\n";
showlevel(+{ title => "Proposed Lake Position ($tries tries left)", map => $combined});
}
my ($ok, $i, $j) = (1, 0, 1);
while ($ok and ($i + 1 < scalar @edgespot)) {
my ($ix, $iy) = @{$edgespot[$i]};
my ($jx, $jy) = @{$edgespot[$j]};
my $d = distance_walking($combined, $ix + $lxpos, $iy + $lypos, $jx + $lxpos, $jy + $lypos);
if ($d > (($xmax + $ymax) * 4 / 3)) {
print "Excessive distance ($d) from ($ix+$lxpos,$iy+$lypos) to ($jx+$lxpos,$jy+$lypos)\n" if ($debug =~ /lake/);
$ok = undef;
} else {
$j++;
if ($j >= scalar @edgespot) {
$i++;
$j = $i + 1;
}}}
if ($ok) {
print "Lake position accepted.\n" if $debug =~ /lake/;
$$level{map} = $combined;
$done = 1;
}
}
# If we reach this point, give up on that lake and try a smaller one:
$lxmax = (7 * $lxsize + 1) / 10;
$lymax = (7 * $lysize + 1) / 10;
}
}
if ($debug =~ /lake|placement/) {
showlevel(+{title => "After Lake $lakenum", map => $$level{map}});
}
}
}
if ((($arg{negspaceprob} || 35) > rand 100) or ($debug =~ /negspace/)) {
for (1 .. 1 + int rand rand 4) {
my $mask = blankmap();
if (95 > rand 100) {
# Mask off the corner areas, we don't want those to be candidates:
cavern_paint_mask($$level{map}, $mask, 2, 2, qr/UNDECIDED/);
cavern_paint_mask($$level{map}, $mask, $xmax - 1, 2, qr/UNDECIDED/);
cavern_paint_mask($$level{map}, $mask, 2, $ymax - 1, qr/UNDECIDED/);
cavern_paint_mask($$level{map}, $mask, $xmax - 1, $ymax - 1, qr/UNDECIDED/);
}
my $count = 0;
if ($debug =~ /negspace/) {
showlevel(+{title => "Before Negative Space Calculation", map => $$level{map}});
}
my @candidate = ();
for my $x (randomorder(2 .. ($xmax - 1))) {
for my $y (randomorder(2 .. ($ymax - 1))) {
if (($$level{map}[$x][$y]{type} =~ /UNDECIDED/) and
$$mask[$x][$y]{type} eq "UNDECIDED") {
++$count;
if ($debug =~ /negspaces/) {
showlevel(+{title => "Negative Space Mask $count ($x,$y)", map => $mask});
pressenter();
}
my $tilecount = cavern_paint_mask($$level{map}, $mask, $x, $y, qr/UNDECIDED/);
push @candidate, [$x, $y, $tilecount + rand(1 + 2 * $tilecount / 3)];
}
}
}
if (25 > int rand 100) {
# Try to pick the biggest area:
@candidate = sort { $$b[2] <=> $$a[2] } @candidate;
} else {
@candidate = randomorder(@candidate);
}
if ($debug =~ /negspace/) {
showlevel(+{title => "Negative Space Mask", map => $mask});
for my $c (@candidate) {
print "Candidate: ($$c[0],$$c[1]), $$c[2] tiles\n";
}
}
my ($x, $y, $size) = @{$candidate[0]};
if ($size > 3) {
my $map = blankmap();
cavern_paint_mask($mask, $map, $x, $y);
if ($debug =~ /negspace/) {
showlevel(+{title => "Selected Negative Space Area", map => $map});
}
cavern_paint_mask($mask, $$level{map}, $x, $y);
if ($debug =~ /negspace/) {
showlevel(+{title => "Level Plus Negative Space Area", map => $$level{map}});
}
}
}}
for my $sdoornum (1 .. int(($xmax / 8) + rand($xmax / 14))) {
$$level{map} = fixwalls($$level{map});
my @c = map {
$$_[0]
} sort {
$$b[1] <=> $$a[1]
} map {
[ $_ => distance_around_wall($$level{map}, $$_[0], $$_[1]) + rand 7 ]
} grep {
my $c = $_;
($solid{$$level{map}[$$c[0]][$$c[1]]{type}} and
(# Could be made a north-to-south door:
($walkable{$$level{map}[$$c[0]][$$c[1]-1]{type}} and
$walkable{$$level{map}[$$c[0]][$$c[1]+1]{type}}) or
# Could be made an east-to-west door:
($walkable{$$level{map}[$$c[0]-1][$$c[1]]{type}} and
$walkable{$$level{map}[$$c[0]+1][$$c[1]]{type}})))
} map {
my $x = $_;
map {
[$x, $_]
} 2 .. ($ymax - 1);
} 2 .. ($xmax - 1);
my ($x, $y) = @{$c[0]};
my $d = distance_around_wall($$level{map}, $x, $y);
if ($d > (($xmax + $ymax) / 7)) {
$$level{map}[$x][$y] = terrain((40 > rand 100) ? "SDOOR" : "DOOR");
if ($debug =~ /secret/) {
showlevel(+{ title => "Added extra door $sdoornum (distance: $d)",
map => $$level{map},
});
}
} elsif ($debug =~ /secret/) {
print "Did not place door at ($x, $y), because distance around is only $d.\n";
}
}
$$level{title} = "Finalized Level";
$$level{map} = fixwalls(undecided_to_stone($$level{map}), checkstone => "yes");
showlevel($level);
exit 0; # Subroutines Follow.
sub is_well_connected {
my ($map) = @_;
my ($x, $y) = (1,1);
my $count = 0; # Counts number of walkable tiles.
while (($y <= $ymax) and not $walkable{$$map[$x][$y]{type}}) {
$x++; if ($x > $xmax) { $x = 1; $y++; }
}
if ($y > $ymax) {
print "is_well_connected() found no walkable terrain.\n" if $debug =~ /connected/;
return;
}
my $dist = distance_map($map, $x, $y);
while ($y <= $ymax) {
if ($walkable{$$map[$x][$y]{type}}) {
$count++;
return if $$dist[$x][$y] > ($xmax * $ymax);
}
$x++; if ($x > $xmax) { $x = 1; $y++; }
}
return $count;
}
sub distance_map {
my ($map, $ox, $oy) = @_;
my $infinity = ($xmax * $ymax) + 1;
my $dist = [ map { my $x = $_; [map { $infinity } 0 .. $ymax] } 0 .. $xmax];
$$dist[$ox][$oy] = 0;
my @nextgen = ([$ox, $oy]);
while (scalar @nextgen) {
my @lastgen = @nextgen;
@nextgen = ();
for my $coord (@lastgen) {
my ($x, $y) = @$coord;
my $newdist = $$dist[$x][$y] + 1;
for my $vector ([0, -1], [0, 1], [-1, 0], [1, 0]) {
my ($dx, $dy) = @$vector;
if (($x + $dx >= 1) and ($x + $dx <= $xmax) and
($y + $dy >= 1) and ($y + $dy <= $ymax) and
($walkable{$$map[$x + $dx][$y + $dy]{type}}) and
$$dist[$x + $dx][$y + $dy] > $newdist) {
$$dist[$x + $dx][$y + $dy] = $newdist;
push @nextgen, [$x + $dx, $y + $dy];
}}}}
if ($debug =~ /distmap/) {
my %bg = ( 0 => "on_black", 1 => "on_blue", 2 => "on_cyan", 3 => "on_green", 4 => "on_yellow", 5 => "on_red", 6 => "on_magenta", 7 => "on_white" );
showlevel(+{ title => "Distance Map",
map => [ map {
my $x = $_;
[map {
my $y = $_;
my $d = $$dist[$x][$y];
($d == $infinity) ? +{ type => "STONE",
char => "*",
fg => "bold white",
bg => "on_black",
}
: ($d < 70) ? +{ type => "FLOOR",
char => ($d % 10),
fg => "bold white",
bg => $bg{($d / 10)},
}
: +{ type => "FLOOR",
char => ($d % 10),
fg => "bold yellow",
bg => "on_black",
};
} 0 .. $ymax]
} 0 .. $xmax]})
}
return $dist;
}
sub distance_walking {
my ($map, $ox, $oy, $tx, $ty) = @_;
croak "Wat: distance_walking(@_)" if ((not $tx) or (not $ty) or (not $ox) or (not $oy));
my $infinity = ($xmax * $ymax) + 1; # Literally: worse than visiting every single tile on the level to get there.
my $dist = [ map {
my $x = $_;
[map {
$infinity
} 0 .. $ymax]
} 0 .. $xmax ];
$$dist[$ox][$oy] = 0; # Point of origin.
my @nextgen = ([$ox, $oy]);
while (scalar @nextgen) {
my @lastgen = @nextgen;
@nextgen = ();
for my $coord (@lastgen) {
my ($x, $y) = @$coord;
if ($$dist[$x][$y] < $infinity) {
my $newdist = $$dist[$x][$y] + 1;
if ($debug =~ /distance/) {
print "Distance from ($ox,$oy), generation $newdist, reached ($x,$y)\n";
}
for my $vector ([0, -1], [0, 1], [-1, 0], [1, 0]) {
my ($dx, $dy) = @$vector;
if (($x + $dx >= 1) and ($x + $dx <= $xmax) and
($y + $dy >= 1) and ($y + $dy <= $ymax) and
# it's possible to take that step:
($walkable{$$map[$x + $dx][$y + $dy]{type}}) and
# it's shorter than any previously known path to there:
$$dist[$x + $dx][$y + $dy] > $newdist) {
# With breath-first, this is now valid:
if (($x + $dx == $tx) and ($y + $dy == $ty)) {
return $newdist;
}
$$dist[$x + $dx][$y + $dy] = $newdist;
push @nextgen, [$x + $dx, $y + $dy];
}}
}}
}
return $$dist[$tx][$ty];
}
sub distance_around_wall {
my ($map, $cx, $cy) = @_;
my ($nsdist, $ewdist) = (0, 0);
if (($cy > 1) and ($cy < $ymax) and
($$map[$cx][$cy - 1]{type} =~ /FLOOR|CORR/) and
($$map[$cx][$cy + 1]{type} =~ /FLOOR|CORR/)) {
$nsdist = distance_walking($map, $cx, $cy - 1, $cx, $cy + 1);
}
if (($cx > 1) and ($cx < $xmax) and
($$map[$cx - 1][$cy]{type} =~ /FLOOR|CORR/) and
($$map[$cx + 1][$cy]{type} =~ /FLOOR|CORR/)) {
$ewdist = distance_walking($map, $cx - 1, $cy, $cx + 1, $cy);
}
return ($nsdist > $ewdist) ? $nsdist : $ewdist;
}
sub can_place_room {
my ($level, $room, $xoffset, $yoffset, $rxmin, $rymin, $rxmax, $rymax) = @_;
if (($rxmin + $xoffset < 1) or ($rxmax + $xoffset > $xmax) or
($rymin + $yoffset < 1) or ($rymax + $yoffset > $ymax)) {
return 0;
}
my @match;
for my $y ($rymin .. $rymax) {
for my $x ($rxmin .. $rxmax) {
if ($$room[$x][$y]{type} ne "UNDECIDED") {
if (($xoffset + $x < 1) or ($xoffset + $x > $xmax) or
($yoffset + $y < 1) or ($yoffset + $y > $ymax)) {
return 0;
}
if ($$room[$x][$y]{type} =~ /DOOR/) {
# Doors MUST match up:
if (not $$level{map}[$xoffset + $x][$yoffset + $y]{type} =~ /WALL|DOOR|STONE/) {
return 0;
}
} elsif ($$room[$x][$y]{type} =~ /WALL|STONE/) {
if ($$level{map}[$xoffset + $x][$yoffset + $y]{type} =~ /WALL|DOOR|STONE/) {
push @match, [$x, $y];
} elsif ($$level{map}[$xoffset + $x][$yoffset + $y]{type} ne "UNDECIDED") {
return 0;
}
} elsif ($$room[$x][$y]{type} =~ /CORR/) { # Corridor on corridor matches.
if ($$level{map}[$xoffset + $x][$yoffset + $y]{type} =~ /CORR/) {
push @match, [$x, $y];
} elsif ($$level{map}[$xoffset + $x][$yoffset + $y]{type} eq "STONE") {
# Accept, but don't count as a match.
} elsif ($$level{map}[$xoffset + $x][$yoffset + $y]{type} ne "UNDECIDED") {
return 0;
}
} else { # FLOOR or whatever else (water, lava, fountain, altar, etc.) has to go on undecided spots:
if ($$level{map}[$xoffset + $x][$yoffset + $y]{type} ne "UNDECIDED") {
return 0;
}
}
}
}
}
return @match;
}
sub getextrema {
my ($room) = @_;
my ($rxmin, $rymin, $rxmax, $rymax) = ($xmax, $ymax, 0, 0);
for my $x (1 .. $xmax) {
for my $y (1 .. $ymax) {
if ((not defined $$room[$x][$y]{type}) and ($debug =~ /extrema/)) {
croak "type undefined";
}
if ($$room[$x][$y]{type} ne "UNDECIDED") {
if ($x < $rxmin) {
$rxmin = $x;
}
if ($x > $rxmax) {
$rxmax = $x;
}
if ($y < $rymin) {
$rymin = $y;
}
if ($y > $rymax) {
$rymax = $y;
}
}
}
}
print "Extrema: $rxmin, $rymin, $rxmax, $rymax\n" if $debug =~ /extrema/;
return ($rxmin, $rymin, $rxmax, $rymax);
}
sub add_room_to_level {
my ($level, $room) = @_;
my ($possible, $bestx, $besty, $bestcount) = (0, 0, 0, 0);
croak "No room to add" if not ref $room;
my ($rxmin, $rymin, $rxmax, $rymax) = getextrema($room);
for my $xoffset (@rndxpos) {
if (($xoffset + $rxmin < 1) or ($xoffset + $rxmax > $xmax)) {
# Cannot place at this x position (column), no need to test the details.
} else {
for my $yoffset (@rndypos) {
if (($yoffset + $rymin < 0) or ($yoffset + $rymax > $ymax)) {
# Cannot place at this (x,y) position, no need to test further details.
} else {
my $wallmatchcount = can_place_room($level, $room, $xoffset, $yoffset, $rxmin, $rymin, $rxmax, $rymax);
if ($wallmatchcount > 0) {
$possible++;
$wallmatchcount = $wallmatchcount * ((100 - $placerand) + rand $placerand) / 100;
if ($wallmatchcount > $bestcount) {
$bestx = $xoffset;
$besty = $yoffset;
$bestcount = $wallmatchcount;
}
}
}
}
}
}
if ($possible) {
my ($xoffset, $yoffset) = ($bestx, $besty);
my $doorcount = 0;
my @matchpos;
$$level{map} = fixwalls($$level{map});
for my $x (1 .. $xmax) {
for my $y (1 .. $ymax) {
if (($$room[$x][$y]{type} =~ /WALL|STONE/) and
($$level{map}[$xoffset + $x][$yoffset + $y]{type} =~ /DOOR/)) {
$doorcount++;
} elsif (($$room[$x][$y]{type} =~ /STONE/) and
($$level{map}[$xoffset + $x][$yoffset + $y]{type} =~ /WALL/)) {
push @matchpos, [$x, $y];
} elsif ($$room[$x][$y]{type} ne "UNDECIDED") {
if ($$room[$x][$y]{type} eq "DOOR") {
$doorcount++;
} elsif ($$room[$x][$y]{type} =~ /WALL|STONE/ and
$$level{map}[$xoffset + $x][$yoffset + $y]{type} =~ /WALL|STONE/) {
push @matchpos, [$x, $y];
}
$$level{map}[$xoffset + $x][$yoffset + $y] = $$room[$x][$y];
}
}
}
@matchpos = grep { ($$_[0] > 1) and ($$_[0] < $xmax) and
($$_[1] > 1) and ($$_[1] < $ymax) } @matchpos;
if (not $doorcount) {
if (scalar @matchpos) {
my $coord = $matchpos[rand @matchpos];
my ($x, $y) = @$coord;
$$level{map}[$xoffset + $x][$yoffset + $y] = terrain("DOOR");
# TODO: if there are a lot of possible locations, maybe add a secret door at another one?
} elsif ($debug =~ /door/) {
showlevel(+{ title => "(Trying to add this room)", map => $room});
print color($arg{errorcolor} || "bold red") . "No place for door!" . color("reset");
showlevel($level);
pressenter("force");
}
}
return $level;
}
return;
}
sub undecided_to_stone {
my ($map) = @_;
return convert_terrain($map, qr/UNDECIDED/, terrain("STONE"));
}
sub convert_terrain {
my ($map, $match, $replacement, $decide) = @_;
$decide ||= sub { return 1; };
my $count = 0;
for my $x (1 .. $xmax) {
for my $y (1 .. $ymax) {
if ($$map[$x][$y]{type} =~ $match) {
if ($decide->($map, $x, $y, $count++)) {
$$map[$x][$y] = +{ %$replacement };
}
}
}
}
return $map;
}
sub extend_dead_corridor {
my ($map, $cx, $cy, $maxiter) = @_;
return if $maxiter < 1;
for my $vector ([0, -1], [0, 1], [1, 0], [-1, 0]) {
my ($dx, $dy) = @$vector;
if (($cx + $dx >= 1) and ($cx + $dx <= $xmax) and
($cy + $dy >= 1) and ($cy + $dy <= $ymax) and
($$map[$cx + $dx][$cy + $dy]{type} =~ /CORR/)) {
# This is the direction we're coming _from_.
# We want to extend in the opposite direction:
my $tx = $cx - $dx;
my $ty = $cy - $dy;
# But can we?
my $ttype = $$map[$tx][$ty]{type} || "ERROR";
if ($walkable{$ttype}) {
return "Success"; # Base case for success.
} elsif (($dx == 0) and
((($tx > 1) and (($walkable{$$map[$tx - 1][$ty]{type} || "ERROR"}))) or
(($tx + 2 < $xmax) and (($walkable{$$map[$tx + 1][$ty]{type} || "ERROR"}))) or
(($ty > 1) and (($walkable{$$map[$tx][$ty - 1]{type} || "ERROR"}))) or
(($ty + 2 < $ymax) and (($walkable{$$map[$tx][$ty + 1]{type} || "ERROR"}))))) {
# Lateral connection, good enough.
return "Success";
} elsif (($ttype eq "UNDECIDED") or ($solid{$ttype})) {
# Provisionally continue:
my $orig = $$map[$tx][$ty];
$$map[$tx][$ty] = terrain("CORR");
my $result = extend_dead_corridor($map, $tx, $ty, $maxiter - 1);
if ((($result || "") eq "Success") and ($ttype eq "WALL")) {
# Special case, the very last spot we opened up can become a
# door, if it was formerly a wall. (If this is wrong,
# fix_walls will correct it later.)
$$map[$tx][$ty] = terrain("DOOR");
return "Yes, but already did the door.";
} elsif ($result) {
return $result; # propagate our success back up the call chain.
} else {
# Failed, backtrack:
$$map[$tx][$ty] = $orig;
return; # propagate failure back up the call chain.
}
}
}
}
# If we didn't find a direction to extend, we fail:
return;
# This will happen for example if we hit the edge of the level, or
# run into terrain that is neither solid nor walkable (e.g., lava).
# If we've provisionally extended the corridor several tiles
# already, we'll backtrack and rip it all out.
}
sub fix_dead_corridors {
my ($map, $maxiter, $extendprob) = @_;
my $matchre = qr/FLOOR|CORR|SHALLOW|LAKE|DOOR/;
$maxiter ||= 300;
$extendprob ||= 25 + int rand 50;
my $didanything = 1;
while ($didanything and ($maxiter-- > 0)) {
$didanything = 0;
for my $x (2 .. ($xmax - 1)) {
for my $y (2 .. ($ymax - 1)) {
if (($$map[$x][$y]{type} =~ /CORR/) and
(countadjacent($map, $x, $y, $matchre) == 1)) {
$didanything++;
if (($extendprob > rand 100) and
extend_dead_corridor($map, $x, $y, int(($xmax + $ymax) / 2))) {
} elsif (countadjacent($map, $x, $y, qr/CORR/) == 1) {
$$map[$x][$y] = terrain("STONE");
}}}}}
return $map;
}
sub fixwalls {
my ($map, %arg) = @_;
# First, check for doors that aren't accessible enough:
for my $x (1 .. $xmax) {
for my $y (1 .. $ymax) {
if ($$map[$x][$y]{type} =~ /DOOR|SDOOR/) {
my $floorct = countadjacent($map, $x, $y, qr/FLOOR|SHALLOW|LAKE|TRAP/);
if ($floorct < 1) {
# Doors from one corridor to another should _usually_ be converted to secret corridor.
if (90 > rand 100) {
$$map[$x][$y] = terrain("SCORR");
}
}
if (($x > 1) and ($y > 1) and ($x < $xmax) and ($y < $ymax) and
(# Either it's a vertical door:
($$map[$x - 1][$y]{type} =~ /FLOOR|CORR|SHALLOW|LAKE|TRAP/ and
$$map[$x + 1][$y]{type} =~ /FLOOR|CORR|SHALLOW|LAKE|TRAP/ and
$$map[$x][$y - 1]{type} =~ /WALL|STONE/ and
$$map[$x][$y + 1]{type} =~ /WALL|STONE/) or
# Else it's a horizontal door
($$map[$x - 1][$y]{type} =~ /WALL|STONE/ and
$$map[$x + 1][$y]{type} =~ /WALL|STONE/ and
$$map[$x][$y - 1]{type} =~ /FLOOR|CORR|SHALLOW|LAKE|TRAP/ and
$$map[$x][$y + 1]{type} =~ /FLOOR|CORR|SHALLOW|LAKE|TRAP/))) {
# This door is okey dokey
} else {
if ($$map[$x][$y]{type} eq "SDOOR") {
# Failed secret door, plaster it over:
$$map[$x][$y] = terrain("WALL", bg => (($debug =~ /door/) ? "on_blue" : "on_black"));
} else {
# Failed regular door, just open it up:
for my $dx (-1 .. 1) {
for my $dy (-1 .. 1) {
if (($x + $dx > 1) and ($x + $dx < $xmax) and
($y + $dy > 1) and ($y + $dy < $ymax) and
(not $$map[$x][$y]{type} =~ /FLOOR|CORR|SHALLOW|LAKE|TRAP/) and
((not $dx) or (not $dy) or (50 > rand 100))) {
$$map[$x + $dx][$y + $dy] = terrain("FLOOR");
}}}
}
}
} elsif ($$map[$x][$y]{type} =~ /CORR/) {
# While we're at it, clean up any corridors that ended up in rooms:
my $floorct = countadjacent($map, $x, $y, qr/FLOOR|SHALLOW|LAKE|TRAP/);
my $corrct = countadjacent($map, $x, $y, qr/CORR/);
if (($floorct > 3) or (($floorct > 1) and not $corrct)) {
$$map[$x][$y] = terrain("FLOOR");
}
}
}
}
if ($arg{checkstone}) {
for my $x (1 .. $xmax) {
for my $y (1 .. $ymax) {
# Check for stone adjacent to floor, make it wall:
if ($$map[$x][$y]{type} =~ /STONE/) {
if (countadjacent($map, $x, $y, qr/FLOOR|SHALLOW|LAKE|TRAP/)) {
$$map[$x][$y] = terrain("WALL");
}
}
# Also check for wall surrounded by wall/stone, and make it stone:
if ($$map[$x][$y]{type} eq "WALL") {
if (countadjacent($map, $x, $y, qr/WALL|STONE/) == 8) {
$$map[$x][$y] = terrain("STONE");
}
}
}
}
}
# ais523 wall direction algorithm. We start by drawing a square around every
# open floor space, then remove the parts of the square that do not connect
# to other walls.
my %dirbit = ( EAST => 1,
NORTH => 2,
WEST => 4,
SOUTH => 8,
);
my @wmap = map { [map { 0 } 0 .. $ymax ] } 0 .. $xmax;
for my $x (2 .. ($xmax - 1)) {
for my $y (2 .. ($ymax - 1)) {
if ($$map[$x][$y]{type} =~ /FLOOR|SHALLOW|LAKE|TRAP|LAVA/) {
$wmap[$x+1][$y] |= $dirbit{NORTH} | $dirbit{SOUTH};
$wmap[$x-1][$y] |= $dirbit{NORTH} | $dirbit{SOUTH};
$wmap[$x][$y-1] |= $dirbit{EAST} | $dirbit{WEST};
$wmap[$x][$y+1] |= $dirbit{EAST} | $dirbit{WEST};
$wmap[$x+1][$y+1] |= $dirbit{NORTH} | $dirbit{WEST};
$wmap[$x-1][$y+1] |= $dirbit{NORTH} | $dirbit{EAST};
$wmap[$x+1][$y-1] |= $dirbit{SOUTH} | $dirbit{WEST};
$wmap[$x-1][$y-1] |= $dirbit{SOUTH} | $dirbit{EAST};
}
}
}
for my $x (1 .. $xmax) {
for my $y (1 .. $ymax) {
if (($x < $xmax) and not ($$map[$x+1][$y]{type} =~ /WALL|DOOR/)) {
$wmap[$x][$y] &= ~ $dirbit{EAST};
}
if (($x > 1) and not ($$map[$x-1][$y]{type} =~ /WALL|DOOR/)) {
$wmap[$x][$y] &= ~ $dirbit{WEST};
}
if (($y < $ymax) and not ($$map[$x][$y+1]{type} =~ /WALL|DOOR/)) {
$wmap[$x][$y] &= ~ $dirbit{SOUTH};
}
if (($y > 1) and not ($$map[$x][$y-1]{type} =~ /WALL|DOOR/)) {
$wmap[$x][$y] &= ~ $dirbit{NORTH};
}
if ($$map[$x][$y]{type} eq 'WALL') {
$$map[$x][$y]{char} = $wallglyph[$wmap[$x][$y]];
}
}
}
return $map;
}
sub blankmap {
my ($terrain) = @_;
$terrain ||= "UNDECIDED";
return [ map {
[map { terrain($terrain) } 0 .. $ymax]
} 0 .. $xmax];
}
sub copy_map {
my ($orig) = @_;
return [map {
my $x = $_;
[ map {
my $y = $_;
+{ %{$$orig[$x][$y]} }
} 0 .. $ymax]
} 0 .. $xmax];
}
sub walls_around_room {
my ($map, $re, $type) = @_;
$re ||= qr/FLOOR|SHALLOW|LAKE/;
# Convert any undecided tiles that are adjacent to floor into walls.
for my $x (1 .. $xmax) {
for my $y (1 .. $ymax) {
if ($$map[$x][$y]{type} eq "UNDECIDED") {
my $adjfloor = countadjacent($map, $x, $y, $re);
if ($adjfloor > 0) {
$$map[$x][$y] = terrain($type || "WALL");
}
}
}
}
return $map;
}
sub generate_lake {
my ($lxmax, $lymax, $deep, $shallow) = @_;
if (80 > rand 100) {
$deep ||= "LAKE";
$shallow ||= "SHALLOW";
} elsif (25 > rand 100) {
$deep ||= "STONE";
$shallow ||= "FLOOR";
} elsif (35 > rand 100) {
$deep ||= "LAVA";
$shallow ||= "FLOOR";
} else {
$deep ||= "LAKE";
$shallow ||= "FLOOR";
}
$lxmax ||= 3 + int($xmax / 2);
$lymax ||= 2 + int($ymax / 3);
my $room = walls_around_room(generate_room(undef, $lxmax, $lymax));
return (convert_terrain(convert_terrain($room, qr/FLOOR|CORRIDOR/, terrain($deep)),
qr/WALL|STONE/, terrain($shallow)),
$deep, $shallow);
}
sub generate_room {
my ($rno, $rxmax, $rymax, @arg) = @_;
$rxmax ||= $xmax;
$rymax ||= $ymax;
my $room = initiate_room($rno, $rxmax, $rymax, @arg);
my ($rxa, $rya, $rxb, $ryb) = getextrema($room);
my $xsize = ($rxb + 1 - $rxa);
my $ysize = ($ryb + 1 - $rya);
if (($xsize < 6) or ($ysize < 4) or (not defined $rno)) {
return $room;
}
my $island = initiate_room(undef,
int(($xsize / 2) + rand int($xsize / 4)),
int(($ysize * 2 / 3) + rand int($ysize / 4)),
@arg);
my $difference = smoothe_map(fixwalls(walls_around_room(subtract_room($room, $island)),
checkstone => "yes"));
if (is_well_connected($difference)) {
$room = $difference;
}
if (($arg{trapprob} || 45) > rand 100) {
my @place;
for my $x (1 .. $rxmax) {
for my $y (1 .. $rymax) {
if ($$room[$x][$y]{type} eq "FLOOR") {
push @place, [$x,$y];
}
}
}
@place = randomorder(@place);
my $maxtraps = $arg{maxtraps} || 5;
if (($rxmax < 10) or ($rymax < 4)) {
$maxtraps = int($maxtraps / 2);
}
my $tnum;
my @trap = map { randomtrap() } 1 .. int rand $maxtraps;
for my $trap (@trap) {
my $didtrap = 0;
while ((scalar @place) and not $didtrap) {
my ($cx, $cy) = @{shift @place};
my $orig = $$room[$cx][$cy];
$$room[$cx][$cy] = $trap;
my $ok = 1;
my @adj = grep {
my ($x,$y) = @$_;
($x >= 1) and ($x <= $xmax) and ($y >= 1) and ($y <= $ymax) and
$walkable{$$room[$x][$y]{type}}
} ([$cx + 1, $cy], [$cx - 1, $cy], [$cx, $cy + 1], [$cx, $cy - 1]);
my ($i, $j, $done) = (0, 1, 0);
while (($i + 1 < scalar @adj) and $ok) {
my ($ix, $iy) = @{$adj[$i]};
my ($jx, $jy) = @{$adj[$j]};
$ok = 0 if (distance_walking($room, $ix, $iy, $jx, $jy) > ($xmax * $ymax));
$j++; if ($j >= scalar @adj) { $i++; $j = $i + 1; }}
if ($ok) {
$didtrap = 1;
if ($debug =~ /trap/) {
$tnum++;
showlevel(+{ title => "Placed Trap $tnum ($$trap{name}) of " . scalar @trap . " ($maxtraps maximum).", map => $room });
}
} else {
$$room[$cx][$cy] = $orig;
}}}
}
return $room;
}
sub initiate_room {
my ($rno, @arg) = @_;
carp("generate_room(" . ((defined $rno) ? $rno : "undef") . ", @arg)") if ($debug =~ /carp/);
print "." if $debug =~ /dots/;
select undef, undef, undef, $arg{sleep} if $arg{sleep};
my @rtype =
(
[ 30 => sub { return organic_x_room(@_); } ],
[ 30 => sub { return elipseroom(@_); } ],
[ 30 => sub { return multirect_room(@_); } ],
[ 75 => sub { return cavern_room(@_); } ],
[ 30 => sub { return quadrilateral_room(@_); } ],
[ 30 => sub { return triangle_room(@_); } ],
[ 60 => sub { return lollipop_room(@_); } ],
[ 60 => sub { return intersection_room(@_); } ],
[ 30 => sub { return hexagonroom(@_); } ],
[ 10 => sub { return cyclic_corridor(@_); } ],
);
if (defined $rno) {
# These kinds should never be used for lakes, only for actual rooms:
push @rtype, [ 15 => sub { return vestibule(@_); } ];
push @rtype, [ 20 => sub { return dead_corridor(@_); } ];
push @rtype, [ 20 => sub { return rectangular_room(@_); } ];
# These kinds only ever fit if done pretty early on:
push @rtype, [ ((5 - $rno) * 10) => sub { return barbell_room(@_); } ] if $rno < 5;
push @rtype, [ ((3 - $rno) * 30) => sub { return quadrangle_room(@_); } ] if $rno < 3;
}
my $psum = 0;
$psum += $$_[0] for @rtype;
my $type = rand $psum;
my $sum = 0;
for my $rt (@rtype) {
$sum += $$rt[0];
if ($sum >= $type) {
my $room = $$rt[1]->($rno, @arg);
croak "Room creation failed (prob: prob $$rt[0]; sum: $sum (t $type of $psum))" if not $room;
return $room;
}
}
die "Failed to select a room type (wanted $type from $psum, only got to $sum)";
}
sub intersection_room {
my ($roomno, $rxmax, $rymax, @arg) = @_;
my $map = generate_room($roomno, int($rxmax * 2 / 5), int($rymax * 2 / 3), @arg);
my $two = generate_room($roomno, int($rxmax * 2 / 5), int($rymax * 2 / 3), @arg);
my ($xoffset, $yoffset) = (0, 0);
my ($mapxmin, $mapymin, $mapxmax, $mapymax) = getextrema($map);
my ($rxa, $rya, $rxb, $ryb) = getextrema($two);
if ($debug =~ /intersection/) {
showlevel(+{ title => "Intersection: Room A ($mapxmin,$mapymin), ($mapxmax,$mapymax)", map => $map });
showlevel(+{ title => "Intersection: Room B ($rxa,$rya), ($rxb,$ryb)", map => $two });
}
# What are the minimum and maximum offsets that will allow the second room to fit on the map?
my $minxoffset = 2 - $rxa;
my $maxxoffset = $xmax - 1 - $rxb;
my $minyoffset = 2 - $rya;
my $maxyoffset = $ymax - 1 - $ryb;
# We also want the second room to overlap with the first room. This may further restrict the offsets:
if ($minxoffset + $rxb < $mapxmin) { $minxoffset = $mapxmin - $rxb; }
if ($maxxoffset > $mapxmax) { $maxxoffset = $mapxmax; }
if ($minyoffset + $ryb < $mapymin) { $minyoffset = $mapymin - $ryb; }
if ($maxyoffset > $mapymax) { $maxyoffset = $mapymax; }
if ($debug =~ /intersection/) { print "Min offsets ($minxoffset,$minyoffset); max offsets ($maxxoffset,$maxyoffset)\n"; }
# With the limits established, try to pick specific offsets that actually work:
if (($maxxoffset > ($minxoffset + 1)) and
($maxyoffset > ($minyoffset + 1))) {
my $tries = 7;
while ($tries-- > 0) {
$xoffset = $minxoffset + int rand ($maxxoffset - $minxoffset);
$yoffset = $minyoffset + int rand ($maxyoffset - $minyoffset);
if ($debug =~ /intersection/) {
print "Testing offsets: ($xoffset,$yoffset)\n";
}
for my $x ($rxa .. $rxb) {
for my $y ($rya .. $ryb) {
if (($$two[$x][$y]{type} =~ /FLOOR|CORR/) and
($$map[$x + $xoffset][$y + $yoffset]{type} =~ /FLOOR|CORR/)) {
# This position works. Do the thing. The outer loops are
# now done, because we're going to return from the
# function this iteration. $xoffset and $yoffset are it.
if ($debug =~ /intersection/) { print "Selected offsets: ($xoffset,$yoffset)\n"; }
for my $x ($rxa .. $rxb) {
for my $y ($rya .. $ryb) {
if (($$two[$x][$y]{type} eq "FLOOR") or
($walkable{$$two[$x][$y]{type}} and not
$walkable{$$map[$x + $xoffset][$y + $yoffset]{type}}) or
($$map[$x + $xoffset][$y + $yoffset]{type} eq "UNDECIDED")) {
$$map[$x + $xoffset][$y + $yoffset] =
($$two[$x][$y]{type} eq "TRAP") ? $$two[$x][$y] : terrain($$two[$x][$y]{type});
}}}
$map = walls_around_room($map);
if ($debug =~ /intersection/) {
showlevel( +{ title => "Intersection (combined)", map => $map });
pressenter();
}
return $map;
}
}
}
}
if ($debug =~ /intersection/) {
warn "Cannot use offsets ($xoffset,$yoffset)\n";
}
}
# If all else fails, we at least have a room, even if it's not an intersection:
if ($debug =~ /intersection/) {
warn "Intersection failed: did not find working offsets. Returning Room A alone.";
}
return $map;