-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom-pack.pl
286 lines (273 loc) · 9.96 KB
/
room-pack.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
#!/usr/bin/perl
use Term::ANSIColor;
my ($ROWNO, $COLNO) = (21, 79);
my (@rowpos, @colpos, @grid, @cell, @map,
$rowcount, $colcount, $cellcount, $cellcount);
my ($x, $y, $trycount);
my ($trymax, $xpandprob) = (7000, 60);
my $targetpct = 60 + int rand 30;
$x = 0;
while (++$x + 6 < $COLNO) {
$colcount++;
$colpos[$colcount - 1] = $x;
$x += 2 + int rand 4;
}
$colpos[$colcount] = $COLNO - 2;
$y = 0;
while (++$y + 4 < $ROWNO) {
$rowcount++;
$rowpos[$rowcount - 1] = $y;
$y += 2 + int rand 2;
}
$rowpos[$rowcount] = $ROWNO - 2;
$cellmax = $rowcount * $colcount;
for $x (1 .. $colcount) {
for $y (1 .. $rowcount) {
$grid[$x - 1][$y - 1] = 0;
}
}
while ((($gridcellcount * 100 / $cellmax) < $targetpct) and
($trycount < $trymax)) {
$x = int rand $colcount;
$y = int rand $rowcount;
if ($grid[$x][$y]) {
$trycount++;
} else {
my ($minx, $maxx, $miny, $maxy) = ($x, $x, $y, $y);
$cellcount++;
$gridcellcount++;
$grid[$x][$y] = $cellcount;
my $trytoexpand = sub {
my ($xdir, $ydir) = @_;
print " Attempting to expand ($xdir,$ydir) ";
while ($xpandprob > int rand 100) {
my $canexpand = 1;
if ($xdir != 0) {
for $y ($miny .. $maxy) {
if (($maxx + $xdir >= $colcount) or
($minx + $xdir < 0) or
$grid[$maxx + $xdir][$y] or
$grid[$minx + $xdir][$y]) {
$canexpand = 0;
}
}
if ($canexpand) {
print "x";
if ($xdir > 0) {
$maxx += $xdir;
} else {
$minx += $xdir;
}
for $y ($miny .. $maxy) {
$grid[$minx][$y] = $cellcount;
$grid[$maxx][$y] = $cellcount;
$gridcellcount++;
}
} else {
print "0";
}
} else {
for $x ($minx .. $maxx) {
if (($maxy + $ydir >= $rowcount) or
($miny + $ydir < 0) or
$grid[$x][$miny + $ydir] or
$grid[$x][$maxy + $ydir]) {
$canexpand = 0;
}
}
if ($canexpand) {
print "y";
if ($ydir > 0) {
$maxy += $ydir;
} else {
$miny += $ydir;
}
for $x ($minx .. $maxx) {
$grid[$x][$miny] = $cellcount;
$grid[$x][$maxy] = $cellcount;
$gridcellcount++;
}
} else {
print "0";
}
}
}
print "\n";
};
# Try to expand...
$trytoexpand->(1, 0); # to the east
$trytoexpand->(-1,0); # to the west
$trytoexpand->(0,-1); # to the north
$trytoexpand->(0, 1); # to the south
# And now save the cell:
$cell[$cellcount - 1] =
+{ minx => $minx,
miny => $miny,
maxx => $maxx,
maxy => $maxy,
join => +{ map { $_ => ((60 > int rand 100) ? "open" : "wall") } qw(n s e w) },
num => $cellcount,
#door => +{ (map { $colpos[$minx] + 1 + int rand($colpos[$maxx + 1] - $colpos[$minx] - 2) } qw(n s)),
# (map { $rowpos[$miny] + 1 + int rand($rowpos[$maxy + 1] - $rowpos[$miny] - 2) } qw(e w)) },
};
print "Used $gridcellcount of $cellmax grid cells (" .
(sprintf("%0.1f", ($gridcellcount * 100 / $cellmax))) . '%)' . "\n";
#print "Press Enter to show map...\n";
#<STDIN>;
#showmap();
}
}
print "Placed $cellcount cells.\n";
showmap();
sub showmap {
print color "reset";
print "\n**********************************************\n\n";
@map = ();
for $cell (1 .. $cellcount) {
my $c = $cell[$cell - 1];
my $minx = $colpos[$$c{minx}];
my $maxx = $colpos[$$c{maxx} + 1] - 1;
my $miny = $rowpos[$$c{miny}];
my $maxy = $rowpos[$$c{maxy} + 1] - 1;
print " cell $$c{num} [$$c{minx},$$c{miny},$$c{maxx},$$c{maxy}] => ($minx,$miny,$maxx,$maxy)\n";
my $gridx = 0;
for $x ($minx .. $maxx) {
while ($colpos[$gridx + 1] <= $x) {
$gridx++;
}
my $gridy = 0;
for $y ($miny .. $maxy) {
while ($rowpos[$gridy + 1] <= $y) {
$gridy++;
}
$map[$x][$y] =
+{ char => (#(($y == $miny and $x == $$c{door}{n}) ||
# ($y == $maxy and $x == $$c{door}{s}) ||
# ($x == $minx and $y == $$c{door}{w}) ||
# ($x == $maxx and $y == $$c{door}{e})) ? "+" :
(($y == $miny) and not ($gridy > 0 and $$c{join}{n} eq "open"
and $grid[$gridx][$gridy - 1]
and $cell[$grid[$gridx][$gridy - 1] - 1]{join}{s} eq "open"
and $x > $colpos[$cell[$grid[$gridx][$gridy - 1] - 1]{minx}]
and $x < $colpos[$cell[$grid[$gridx][$gridy - 1] - 1]{maxx}]
)) ? "-" :
(($y == $maxy) and not ($gridy < $rowcount and $$c{join}{s} eq "open"
and $grid[$gridx][$gridy + 1]
and $cell[$grid[$gridx][$gridy + 1] - 1]{join}{n} eq "open"
and $x > $colpos[$cell[$grid[$gridx][$gridy + 1] - 1]{minx}]
and $x < $colpos[$cell[$grid[$gridx][$gridy + 1] - 1]{maxx}]
)) ? "-" :
(($x == $minx) and not ($gridx > 0 and $$c{join}{w} eq "open"
and $grid[$gridx - 1][$gridy]
and $cell[$grid[$gridx - 1][$gridy] - 1]{join}{e} eq "open"
and $y > $rowpos[$cell[$grid[$gridx - 1][$gridy] - 1]{miny}]
and $y < $rowpos[$cell[$grid[$gridx - 1][$gridy] - 1]{maxy}]
)) ? "|" :
(($x == $maxx) and not ($gridx < $colcount and $$c{join}{e} eq "open"
and $grid[$gridx + 1][$gridy]
and $cell[$grid[$gridx + 1][$gridy] - 1]{join}{w} eq "open"
and $y > $rowpos[$cell[$grid[$gridx + 1][$gridy] - 1]{miny}]
and $y < $rowpos[$cell[$grid[$gridx + 1][$gridy] - 1]{maxy}]
)) ? "|" :
'.'#($$c{num} % 10)
),
};
}
}
}
# Now add in some corridors...
my @visited;
for my $cnum (1 .. $cellcount) {
my $c = $cell[$cnum - 1];
my $minx = $colpos[$$c{minx}];
my $maxx = $colpos[$$c{maxx} + 1] - 1;
my $miny = $rowpos[$$c{miny}];
my $maxy = $rowpos[$$c{maxy} + 1] - 1;
my $x = $minx + 1 + int rand($maxx - $minx - 2);
my $y = $miny + 1 + int rand($maxy - $miny - 2);
my @dir = ([-1, 0], [1, 0], [0, -1], [0, 1]); # gridbug path
my $dir = $dir[int rand @dir];
$visited[$x][$y] = $cnum;
my $turncount = 0;
while ($x >= $minx and $x <= $maxx and $y >= $miny and $y <= $maxy) {
$visited[$x][$y] ||= $cnum;
$map[$x][$y]{fg} = 'blue'; # For testing
if ($map[$x][$y]{char} =~ /[-|]/) {
$map[$x][$y]{char} = '+';
$map[$x][$y]{fg} = 'yellow';
}
$x += $$dir[0];
$y += $$dir[1];
}
$visited[$x][$y] ||= $cnum;
$map[$x][$y]{fg} = 'cyan'; # For testing
$map[$x][$y]{char} = '#' if (($map[$x][$y]{char} || ' ') eq ' ');
if ($map[$x][$y]{char} =~ /[-|]/) {
$map[$x][$y]{char} = '.';
$map[$x][$y]{fg} = 'yellow';
}
$x += $$dir[0];
$y += $$dir[1];
while ($x >= 0 and $x < $COLNO and $y >= 0 and $y < $ROWNO and
not $visited[$x][$y]) {
$visited[$x][$y] = $cnum;
if (($map[$x][$y]{char} || ' ') eq ' ') {
$map[$x][$y]{fg} = 'green'; # For testing
$map[$x][$y]{char} = '#';
my $origdir = $dir;
if ($turncount == 0 and not int rand 5) {
# Turn a right angle
while (($$dir[0] == $$origdir[0] and $$dir[1] == $$origdir[1]) or
($$dir[0] == -1 * $$origdir[0] and $$dir[1] == -1 * $$origdir[1])) {
$dir = $dir[int rand @dir];
}
$turncount++;
} else {
my $nextx = $x + $$dir[0];
my $nexty = $y + $$dir[1];
# Try not to run end-on into a wall:
if ((($$dir[0] and ($map[$nextx][$nexty]{char} eq '-')) or
($$dir[1] and ($map[$nextx][$nexty]{char} eq '|')))) {
if ($turncount < 6) {
# Turn a right angle
my $olddir = $dir;
while (($$dir[0] == $$olddir[0] and $$dir[1] == $$olddir[1]) or
($$dir[0] == $$origdir[0] and $$dir[1] == $$origdir[1]) or
($$dir[0] == -1 * $$origdir[0] and $$dir[1] == -1 * $$origdir[1])) {
$dir = $dir[int rand @dir];
}
$turncount++;
} else {
# Give up.
$map[$nextx][$nexty]{fg} = 'magenta';
$visited[$nextx][$nexty] = 1;
}
}
}
} elsif ($map[$x][$y]{char} =~ /[-|]/) {
$map[$x][$y]{fg} = 'yellow';
$map[$x][$y]{char} = '.';
}
$x = $x + $$dir[0];
$y = $y + $$dir[1];
if ($map[$x][$y]{char} =~ /[0-9.]/) {
# Finish condition.
$map[$x][$y]{fg} = 'red'; # for testing
$visited[$x][$y] = 1; # this stops the loop
}
}
}
for $y (0 .. $ROWNO - 1) {
for $x (0 .. $COLNO - 1) {
my $tile = $map[$x][$y] || +{ char => ' ' };
$$tile{char} ||= 'X';
$$tile{fg} ||= "white";
$$tile{bg} ||= "on_black";
print color $$tile{bg};
print color $$tile{fg};
print $$tile{char};
}
print color "reset";
print "!\n";
}
}