-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathais-caverns.pl
766 lines (708 loc) · 24.7 KB
/
ais-caverns.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
#!/usr/bin/perl
# This is Jonadab's version, modified to be easier for a Perl
# programmer to understand. I intend to get rid of all the bitwise
# arithmetic and stuff until I actually can follow what is going on.
# Otherwise I'll never be able to translate it into working C code.
# We have an 80x21 space. Each square in it starts out as undecided.
# Then we mark all the squares around the outside as wall.
# Then we visit all the other squares in a random order, converting
# them to wall or to floor. A square is a floor if it has two walls
# 8-next to it, which don't have a continuous 4-path of wall squares
# between them; and a wall if there are any adjacent walls
# otherwise. The remaining case is no walls; in that case, we choose
# at random.
# Afterward, a few areas of floor are converted into water or lava.
# We can also embed the Wizard's Tower or Fake Tower.
use warnings;
use strict;
use Term::ANSIColor;
my $width = 79;
my $height = 20;
use constant FLOOR => 0;
use constant RIGHT => 1;
use constant UP => 2;
use constant LEFT => 4;
use constant DOWN => 8;
use constant CORRIDOR => 16;
use constant SOLID => 32;
use constant EDGE => 64;
use constant UNDECIDED => 128;
use constant WATER => 256;
use constant LAVA => 512;
use constant STAIR => 1024;
my @map;
my @coords;
my ($upstair, $dnstair);
my %arg;
my $depth;
my $liqcount = 0;
my $river = 0;
my $liquid;
my $dohtml = 0;
# Wall direction constants are 1=east, 2=north, 4=west, 8=south, and these
# are added together as appropriate to get combinations, so that for example
# if there are walls to the north and west, that's 6.
# E N W S
# 1 1 1 1 1 1
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
# that is to say, reading each column below as a binary number:
# south: 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
# west: 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
# north: 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
# east: 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
my @walls = qw/! ─ │ └ ─ ─ ┘ ┴ │ ┌ │ ├ ┐ ┬ ┤ ┼
! ═ ║ ╚ ═ ═ ╝ ╩ ║ ╔ ║ ╠ ╗ ╦ ╣ ╬/;
# (The second batch, with the CORRIDOR bit (16) also set, are doors?)
$walls[0] = ' ';
while (@ARGV) {
my $arg = shift @ARGV;
if ($arg =~ /^([0-9]+)$/) {
$depth = $arg; # This is an integer now, from 0 to 100.
} elsif ($arg =~ /^(wiz|fake)tower\d|specialroom$/) {
$arg{embed} = $arg;
} elsif ($arg =~ /html/) {
$dohtml = 1;
} else {
warn "Did not understand argument: $arg\n";
}
}
sub randomdepth {
my $d = int rand 100;
print "Random Depth: $d\n";
return $d;
}
generate_level();
show_level();
exit 0; # Subroutines follow.
# Returns the neighbours of a square.
sub neighbours {
my ($x, $y) = @_;
return (
$map[$x+1][$y], $map[$x+1][$y+1], $map[$x][$y+1], $map[$x-1][$y+1],
$map[$x-1][$y], $map[$x-1][$y-1], $map[$x][$y-1], $map[$x+1][$y-1]
);
}
# Places a wall on a square, with side effects:
# - Dead ends are recursively removed;
# - Squares that do not form part of a 2x2 square become corridor.
sub block_point {
my ($x, $y, $check, $mark_corridors) = @_;
if ($x == 0 or $y == 0 or ($x == $width - 1) or ($y == $height - 1)) {
return;
}
if ($check and ($map[$x][$y] eq SOLID)) {
return;
}
my $wallcount =
(($map[$x+1][$y] eq SOLID) ? 1 : 0) +
(($map[$x][$y+1] eq SOLID) ? 1 : 0) +
(($map[$x-1][$y] eq SOLID) ? 1 : 0) +
(($map[$x][$y-1] eq SOLID) ? 1 : 0);
if ($wallcount == 3 or not $check) {
$map[$x][$y] = SOLID;
block_point($x+1, $y, 1, $mark_corridors);
block_point($x, $y+1, 1, $mark_corridors);
block_point($x-1, $y, 1, $mark_corridors);
block_point($x, $y-1, 1, $mark_corridors);
if ($mark_corridors) {
block_point($x+1, $y+1, 1, $mark_corridors);
block_point($x+1, $y-1, 1, $mark_corridors);
block_point($x-1, $y+1, 1, $mark_corridors);
block_point($x-1, $y-1, 1, $mark_corridors);
}
}
if ($mark_corridors
and ($map[$x][$y] ne SOLID)
and ($map[$x][$y] ne CORRIDOR)) {
my @neighbours = neighbours($x, $y);
$map[$x][$y] = CORRIDOR;
for my $nidx (0, 2, 4, 6) {
if (($map[$x][$y] eq CORRIDOR)
and not (($neighbours[$nidx] eq SOLID) or
($neighbours[($nidx + 1) % 8] eq SOLID) or
($neighbours[($nidx + 2) % 8] eq SOLID))) {
$map[$x][$y] = FLOOR;
}
}
}
}
sub markwalldirs {
my ($x, $y, $add) = @_;
if ($map[$x][$y] ne FLOOR) {
# FLOOR is the only exception because CORRIDOR can mean door.
for my $dir (@$add) {
$map[$x][$y] |= $dir;
}
}
}
sub unmarkwalldir {
my ($x, $y, $subtract) = @_;
if ($map[$x][$y] ne FLOOR) {
# FLOOR is the only exception because CORRIDOR can mean door.
for my $dir (@$subtract) {
$map[$x][$y] &= ~$dir;
}
}
}
sub placestairs {
my ($trycount);
++$trycount;
my $stairx = 1;
my @shuffy = (1 .. ($height - 1));
# Base stair-placement probabability. High values tend to give you
# stairs very near the left and right edges, so you have to cross
# the whole map. Low values can give you stairs pretty much
# anywhere. We want each of these scenarios to happen frequently.
my $baseprob = int(rand(5) * rand(6));
warn "baseprob: $baseprob\n";
my $prob = $baseprob;
for my $i (0 .. ($height - 2)) {
my $swapi = int rand($height - 2);
my $swap = $shuffy[$i];
$shuffy[$i] = $shuffy[$swapi];
$shuffy[$swapi] = $swap;
}
while ($prob++ <= 2000 and not $upstair) {
while ($stairx < $width and not $upstair) {
for my $stairy (@shuffy) {
if (($map[$stairx][$stairy] eq FLOOR) and not $upstair
and ($prob >= rand 1000)) {
$map[$stairx][$stairy] = STAIR;
$upstair = +{ x => $stairx, y => $stairy};
}
}
$stairx++;
}
}
# Reshuffle the y order for doing the other stairs:
for my $i (0 .. ($height - 2)) {
my $swapi = int rand($height - 2);
my $swap = $shuffy[$i];
$shuffy[$i] = $shuffy[$swapi];
$shuffy[$swapi] = $swap;
}
$stairx = $width - 1;
$prob = $baseprob;
while ($prob++ <= 2000 and not $dnstair) {
while (($stairx > 0) and not $dnstair) {
for my $stairy (@shuffy) {
if (($map[$stairx][$stairy] eq FLOOR) and not $dnstair
and ($prob >= rand 1000)) {
$map[$stairx][$stairy] = STAIR;
$dnstair = +{ x => $stairx, y => $stairy };
}
}
$stairx--;
}
}
if ($trycount < 1000 and not ($upstair and $dnstair)) {
placestairs($trycount);
}
}
sub generate_level {
$depth = randomdepth() if not defined $depth;
# Initialize to undecided...
for my $x (0 .. ($width - 1)) {
for my $y (0 .. ($height - 1)) {
if ($x == 0 || $y == 0 || ($x == $width - 1) || ($y == $height - 1)) {
$map[$x][$y] = SOLID;
} else {
$map[$x][$y] = UNDECIDED;
push @coords, [$x, $y];
}
}
}
# Shuffle the coordinates.
my @shuffled_coords;
while (@coords) {
my $index = int(rand(@coords));
push @shuffled_coords, splice @coords, $index, 1;
}
doembed($arg{embed}) if $arg{embed};
for my $cpair (@shuffled_coords) {
my ($x, $y) = @$cpair;
if ($map[$x][$y] eq UNDECIDED) {
my @neighbours = neighbours($x, $y);
# To reduce diagonal chokepoints, we treat any diagonally adjacent spot
# between two walls as a floor.
for my $nidx (0, 2, 4, 6) {
if (($neighbours[$nidx] eq SOLID) and
($neighbours[($nidx + 2) % 8] eq SOLID) and
($neighbours[$nidx + 1] eq SOLID)) {
$neighbours[$nidx + 1] = FLOOR;
}
}
my $transitioncount = 0;
for my $nidx (0 .. 7) {
$transitioncount += (($neighbours[$nidx] eq SOLID) xor
($neighbours[($nidx+1) % 8] eq SOLID));
}
my $newval = $transitioncount > 2 ? FLOOR :
$transitioncount == 2 ? SOLID :
((rand 100) < ($depth * $depth * $depth
/ (100 * 100))) ? SOLID : FLOOR;
# In order to get larger blocks of walls, if we just created a dead end,
# we mark that cell as # even if it was previously ., because we know
# that doing that cannot block connectivity.
$map[$x][$y] = $newval;
if ($newval eq SOLID) {
block_point($x, $y, 0, 0);
}
}
}
# Now remove orphaned walls from the map.
for my $x (1 .. ($width - 2)) {
for my $y (1 .. ($height - 2)) {
if (($map[$x+1][$y] ne SOLID) and
($map[$x-1][$y] ne SOLID) and
($map[$x][$y+1] ne SOLID) and
($map[$x][$y-1] ne SOLID) and
($map[$x][$y] eq SOLID)) {
$map[$x][$y] = FLOOR;
}
}
}
# Mark corridors on the map.
for my $x (1 .. ($width - 2)) {
for my $y (1 .. ($height - 2)) {
block_point $x, $y, 1, 1;
}
}
if (rand(100) > 50) {
# To produce longer corridors, we block any squares that are diagonally
# adjacent to a corridor, but not orthogonally adjacent to a corridor or
# which have both squares a knight's move from the corridor open.
my $anychanges = 1;
while ($anychanges) {
$anychanges = 0;
for my $cpair (@shuffled_coords) {
my ($x, $y) = @$cpair;
my $orthocorridor = 0;
my @neighbours = neighbours($x, $y);
next if (($map[$x][$y] eq SOLID) or
($map[$x][$y] eq CORRIDOR));
for my $nidx (0, 2, 4, 6) {
if ($neighbours[$nidx] eq CORRIDOR) {
$orthocorridor++;
}
}
if ($orthocorridor == 0) {
for my $nidx (1, 3, 5, 7) {
if (($neighbours[$nidx] eq CORRIDOR) and
(($neighbours[($nidx + 3) % 8] eq SOLID) or
($neighbours[($nidx + 3) % 8] eq CORRIDOR) or
($neighbours[($nidx + 5) % 8] eq SOLID) or
($neighbours[($nidx + 5) % 8] eq CORRIDOR))) {
($anychanges = 1);
block_point($x, $y, 0, 1);
}
}
}
}
}
}
# If a corridor has a length of exactly 2, convert it back to room squares.
# This looks neater than the alternative, although the effect is minor.
for my $x (1 .. ($width - 2)) {
for my $y (1 .. ($height - 2)) {
if ($map[$x][$y] ne CORRIDOR) {
for my $d ([0,+1],[0,-1],[+1,0],[-1,0]) {
my ($d1, $d2) = @$d;
if (not ((($x == ($width - 2)) and ($d1 == 1)) or
(($y == ($height - 2)) and ($d2 == 1)) or
(($x == 1) and ($d1 == -1)) or
(($y == 1) and ($d2 == -1))) and
not ($map[$x+$d1][$y+$d2] eq CORRIDOR) and
not (($map[$x+2*$d1][$y+2*$d2] eq CORRIDOR) or
($map[$x+$d1+$d2][$y+$d1+$d2] eq CORRIDOR) or
($map[$x+$d1-$d2][$y-$d1+$d2] eq CORRIDOR) or
($map[$x+$d2][$y+$d1] eq CORRIDOR) or
($map[$x-$d2][$y-$d1] eq CORRIDOR) or
($map[$x-$d1][$y-$d2] eq CORRIDOR))) {
if ($map[$x][$y] eq CORRIDOR) {
$map[$x][$y] = FLOOR;
}
if ($map[$x+$d1][$y+$d2] eq CORRIDOR) {
$map[$x+$d1][$y+$d2] = FLOOR;
}
}
}
}
}
}
# Repeat the embed, undoing any of the above changes in that area.
doembed($arg{embed}) if $arg{embed};
# Work out where walls should be. 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, and then each piece only gets drawn if it's on a solid tile.
# I am leaving some bitwise arithmetic in this section of the code
# because unlike with all that gratuitous &= ~CORRIDOR garbage
# earlier, here there is an actual reason for it to be this way.
# Wall directions:
# Add the wall dirs needed so that floor areas are surrounded:
for my $x (1 .. ($width - 2)) {
for my $y (1 .. ($height - 2)) {
if ($map[$x][$y] eq FLOOR) {
markwalldirs($x+1, $y, [UP, DOWN]);
markwalldirs($x-1, $y, [UP, DOWN]);
markwalldirs($x, $y+1, [LEFT, RIGHT]);
markwalldirs($x, $y-1, [LEFT, RIGHT]);
markwalldirs($x+1, $y+1, [UP, LEFT]);
markwalldirs($x-1, $y+1, [UP, RIGHT]);
markwalldirs($x+1, $y-1, [DOWN, LEFT]);
markwalldirs($x-1, $y-1, [DOWN, RIGHT]);
}
}
}
# Wall directions:
# Subtract wall dirs that would point straight into floor areas:
for my $x (0 .. ($width - 1)) {
for my $y (0 .. ($height - 1)) {
if (($x < $width - 1) and not
($map[$x+1][$y] & (SOLID | CORRIDOR))) {
unmarkwalldir($x, $y, [RIGHT]);
}
if (($x > 0) and not
($map[$x-1][$y] & (SOLID | CORRIDOR))) {
unmarkwalldir($x, $y, [LEFT]);
}
if (($y < $height - 1) and not
($map[$x][$y+1] & (SOLID | CORRIDOR))) {
unmarkwalldir($x, $y, [DOWN]);
}
if (($y > 0) and not
($map[$x][$y-1] & (SOLID | CORRIDOR))) {
unmarkwalldir($x, $y, [UP]);
}
}
}
# Make corridors on walls secret if it doesn't create an obvious dead
# end (it usually does). The exception is dug-out corner squares,
# which are converted to diagonal chokepoints instead.
for my $cpair (@shuffled_coords) {
my ($x, $y) = @$cpair;
if ($map[$x][$y] & CORRIDOR) {
my @neighbours = neighbours $x, $y;
my $checklonely = 1;
for my $nidx (0, 2, 4, 6) {
if ($neighbours[$nidx] & CORRIDOR) {
$checklonely = 0;
}
}
if ($checklonely) {
$map[$x][$y] |= SOLID;
if ((($map[$x][$y] & UP) or
($map[$x][$y] & DOWN)) and
(($map[$x][$y] & LEFT) or
($map[$x][$y] & RIGHT))) {
if ($map[$x][$y] & CORRIDOR) {
# This almost never happens.
$map[$x][$y] = FLOOR;
}
}
}
}
}
## Meh, this part isn't actually needed.
## # Also make the entire corridor secret if it doesn't branch and ends cleanly
## # at each end.
## sub cleanly_ending_corridor {
## my ($x, $y) = @_;
## if (not $map[$x][$y] & CORRIDOR) {
## return;
## }
## if ($map[$x][$y] & SOLID) {
## # It's a secret door. That counts as a clean end.
## return 1;
## }
## my @neighbours = neighbours($x, $y);
## my $corridorcount = 0;
## for my $nidx (0, 2, 4, 6) {
## if ($neighbours[$nidx] & CORRIDOR) {
## $corridorcount++;
## }
## }
## return if $corridorcount != 1;
## return if $map[$x][$y] & LEFT && $map[$x-1][$y] & CORRIDOR;
## return if $map[$x][$y] & RIGHT && $map[$x+1][$y] & CORRIDOR;
## return if $map[$x][$y] & UP && $map[$x][$y-1] & CORRIDOR;
## return if $map[$x][$y] & DOWN && $map[$x][$y+1] & CORRIDOR;
## return 1;
## }
## sub mark_corridor_secret {
## my ($x, $y, $ox, $oy) = @_;
## $map[$x][$y] & CORRIDOR or return 0;
## $map[$x][$y] & SOLID and return 0;
## my @neighbours = neighbours $x, $y;
## my $opencount = 0;
## $neighbours[$_] & SOLID or $opencount++ for (0, 2, 4, 6);
## $opencount > 2 and return 0;
## if ($ox != -1 && cleanly_ending_corridor $x, $y) {
## $map[$x][$y] |= SOLID;
## return 1;
## }
## my $a = 0;
## $a += mark_corridor_secret($x+1, $y, $x, $y)
## unless $ox == $x+1 && $oy == $y or $a;
## $a += mark_corridor_secret($x-1, $y, $x, $y)
## unless $ox == $x-1 && $oy == $y or $a;
## $a += mark_corridor_secret($x, $y+1, $x, $y)
## unless $ox == $x && $oy == $y+1 or $a;
## $a += mark_corridor_secret($x, $y-1, $x, $y)
## unless $ox == $x && $oy == $y-1 or $a;
## return $a;
## }
## for my $x (0 .. ($width - 1)) {
## for my $y (0 .. ($height - 1)) {
## next unless cleanly_ending_corridor $x, $y;
## if (mark_corridor_secret $x, $y, -1, -1) {
## $map[$x][$y] |= SOLID;
## }
## }
## }
# Before laying down liquids, reserve space for the stairs...
placestairs(0);
$liquid = ($depth > (rand(1000) / 9)) ? LAVA : WATER;
if (2 > int rand $depth) {
$river = 1;
my $minbreadth = 1 + rand 1;
my $cx = ($width / 4) + rand($width / 2);
my ($cy, $cydir, $cxdir) = (0, 1, 0);
if (50 < int rand 100) {
($cy, $cydir) = ($height - 1, -1);
}
for (0 .. $height) {
my $hflex = $minbreadth * 1.5;
if ($cx / $width > 0.9) {
$cx += $hflex - rand $hflex;
$cxdir = -2;
} elsif ($cx / $width < 0.1) {
$cx += (rand $hflex) - $hflex;
$cxdir = 2;
} elsif (abs($cxdir) > $minbreadth * 2) {
$cxdir = $cxdir * rand 1;
$cx += $cxdir;
} else {
$cxdir += $hflex - rand($hflex * 2);
$cx += $cxdir;
}
dopool($cx, $cy, $minbreadth + rand $minbreadth, 0);
$cy += $cydir;
}
} else {
for (1 .. int((rand(6) * rand($depth * 8) - 150) / 100)) {
$liqcount++;
my $cx = int rand $width;
my $cy = int rand $height;
my $radius = 2 + rand rand rand 7;
dopool($cx, $cy, $radius);
}}
}
sub show_level {
my $plural = ($liqcount > 1) ? "s" : '';
my $liquidcount = $river ? "(river of " . ($liquid eq LAVA ? "lava" : "water") . ")"
: $liqcount ? "($liqcount pool$plural of " . ($liquid eq LAVA ? "lava" : "water") . ")"
: "(No liquid)";
open HTML, ">>", "gehennom-maps.html" if $dohtml;
print HTML qq[<div class="dungeon">
<div class="dungeondepth">Depth: $depth</div>\n] if $dohtml;
print HTML qq[<div class="liquidcount">$liquidcount</div>\n] if $dohtml;
print $liquidcount . "\n";
print HTML qq[<div class="dungeonmap">\n] if $dohtml;
for my $y (0 .. ($height - 1)) {
print HTML qq[ <div class="dungeonrow">] if $dohtml;
for my $x (0 .. ($width - 1)) {
if ($map[$x][$y] & STAIR) {
print HTML qq[<span class="stair">] if $dohtml;
print color 'bold white on_red';
} elsif (($map[$x][$y] & (SOLID)) and ($map[$x][$y] & (CORRIDOR))) {
print HTML qq[<span class="secretcorr">] if $dohtml;
print color 'blue';
} elsif ($map[$x][$y] & (SOLID)) {
print HTML qq[<span class="wall">] if $dohtml;
print color 'red';
} elsif ($map[$x][$y] & LAVA) {
print HTML qq[<span class="lava">] if $dohtml;
print color 'bold yellow on_red';
} elsif ($map[$x][$y] & WATER) {
print HTML qq[<span class="water">] if $dohtml;
print color 'bold cyan on_blue';
} elsif (($map[$x][$y] & CORRIDOR) and
($map[$x][$y] & SOLID)) {
print HTML qq[<span class="secretcorridor">] if $dohtml;
print color 'blue';
} elsif ($map[$x][$y] & (CORRIDOR)) {
print HTML qq[<span class="corridor">] if $dohtml;
print color 'bold black';
} else {
print HTML qq[<span class="floor">] if $dohtml;
print color 'white' ;
}
print(
(($map[$x][$y] & (SOLID)) and ($map[$x][$y] & (CORRIDOR))) ? "#" :
(($x == $$upstair{x}) and ($y == $$upstair{y})) ? "<" :
(($x == $$dnstair{x}) and ($y == $$dnstair{y})) ? ">" :
$map[$x][$y] & SOLID ? $walls[$map[$x][$y] & 31] :
$map[$x][$y] & (WATER | LAVA) ? '}' :
$map[$x][$y] & CORRIDOR ? '#' : '.');
print color 'reset';
my $wallchar = $walls[$map[$x][$y] & 31]; $wallchar =~ s/ / /;
if ($dohtml) {
print HTML (
(($map[$x][$y] & (SOLID)) and ($map[$x][$y] & (CORRIDOR))) ? "#" :
(($x == $$upstair{x}) and ($y == $$upstair{y})) ? "<" :
(($x == $$dnstair{x}) and ($y == $$dnstair{y})) ? ">" :
$map[$x][$y] & SOLID ? $wallchar :
$map[$x][$y] & (WATER | LAVA) ? '}' :
$map[$x][$y] & CORRIDOR ? '#' : '.');
print HTML "</span>";
}
}
print HTML "</div>\n" if $dohtml;
print "\n";
}
print HTML "</div></div>\n\n" if $dohtml;
}
sub dopool {
my ($cx, $cy, $radius, $jitter) = @_;
my $stretch = 1 + ((rand 50) / 100);
my $eatrockpct ||= 0;
$jitter ||= (rand 50) / 20;
for my $x (($cx - $radius * $stretch) .. ($cx + $radius * $stretch)) {
if (($x > 0) and ($x + 1 < $width)) {
for my $y (($cy - $radius) .. ($cy + $radius)) {
if (($y > 0) and ($y + 1 < $height)) {
my $distance = sqrt((($x - $cx) / $stretch)**2 + ($y - $cy) ** 2) + rand $jitter;
if (($distance <= $radius) and
(not ($map[int $x][int $y] & STAIR)) and
((not ($map[int $x][int $y] & SOLID))
and (not ($map[int $x][int $y] & CORRIDOR)))) {
$map[int $x][int $y] = $liquid;
}}}}}
}
sub embedcenter {
my (@e) = @_; # The embed is assumed to be rectangular. (But, it can contain undecided terrain.)
my $x = int(($width - scalar @{$e[0]}) / 2);
my $y = int(($height - scalar @e) / 2);
return ($x, $y);
}
sub placeembed {
my ($e, $xoffset, $yoffset) = @_;
my $dy = 0;
for my $line (@$e) {
my $dx = 0;
for my $tile (@$line) {
$map[$xoffset + $dx][$yoffset + $dy] = $tile;
$dx++;
}
$dy++;
}
}
sub parseembed {
return map {
[ map {
my $char = $_;
my $tile = UNDECIDED;
if ($char =~ /[-|]/) {
$tile = SOLID;
} elsif ($char eq ' ') {
$tile = SOLID;
} elsif ($char eq '.') {
$tile = FLOOR;
} elsif ($char eq 'S') {
$tile = (SOLID | CORRIDOR);
} elsif ($char eq '?') {
$tile = UNDECIDED;
} elsif ($char eq 'L') {
$tile = LAVA;
} elsif ($char eq 'W') {
$tile = WATER;
}
$tile;
} split //, $_]
} @_;
}
sub doembed {
my ($special) = @_;
my @embed = ([]);
if ($special eq 'wiztower1') {
print "Wizard's Tower, Bottom Level\n";
@embed = parseembed(' --------------------- ',
' |...................| ',
' |--S------------....| ',
' |....|.WWWWWWW.|--S-| ',
' |....|.WW WW.|....| ',
' |....|.W . W.|....| ',
' |-S--|.W ... W.|....| ',
' |....|.W . W.|....| ',
' |....S.WW WW.|....| ',
' |....|.WWWWWWW.|....| ',
' --------------------- ');
} elsif ($special eq 'wiztower2') {
print "Wizard's Tower, Middle Level\n";
@embed = parseembed('---------------------',
'|....|..............|',
'|-S--|-S----------S-|',
'|..|.|.........|....|',
'|..S.|.........|-S--|',
'|..|.|.........|....|',
'|S--S|.........|....|',
'|....|.........|--S-|',
'|....S.........|....|',
'|....|.........|....|',
'---------------------');
} elsif ($special eq 'wiztower3') {
print "Wizard's Tower, Top Level\n";
@embed = parseembed('---------------------',
'|....S.......S......|',
'|....|-----------S--|',
'|....|WWWWWWW|......|',
'|....|WW---WW|......|',
'|....|W--.--W|......|',
'|--S-|W|...|W|----S-|',
'|....|W--.--W|......|',
'|....|WW---WWS......|',
'|....|WWWWWWW|......|',
'---------------------');
} elsif ($special eq 'faketower1') {
print "Fake Tower One\n";
@embed = parseembed('WWWWWWW',
'WW---WW',
'W--.--W',
'W|...|W',
'W--.--W',
'WW---WW',
'WWWWWWW');
} elsif ($special eq 'faketower2') {
print "Fake Tower Two\n";
@embed = parseembed('WWWWWWW',
'WW---WW',
'W--.--W',
'W|...|W',
'W--.--W',
'WW---WW',
'WWWWWWW');
}
elsif ($special eq 'specialroom') {
# Several things about this don't work right.
my $width = 5 + int rand 7;
my $height = 4 + int rand 3;
# Note: these width and height figures include the walls.
@embed = map {
my $y = $_;
[ map {
my $x = $_;
my $tile = UNDECIDED;
if (($x == 0) or ($y == 0) or ($x == $width) or ($y == $height)) {
$tile = SOLID;
} else {
$tile = FLOOR;
}
$tile;
} 1 .. $width
];
} 1 .. $height;
}
my ($xoffset, $yoffset) = embedcenter(@embed);
placeembed([@embed], $xoffset, $yoffset);
}