1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
* Copyright (©) Crossfire Development Team (restored, original file without copyright notice) |
6 |
* |
7 |
* Deliantra is free software: you can redistribute it and/or modify it under |
8 |
* the terms of the Affero GNU General Public License as published by the |
9 |
* Free Software Foundation, either version 3 of the License, or (at your |
10 |
* option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the Affero GNU General Public License |
18 |
* and the GNU General Public License along with this program. If not, see |
19 |
* <http://www.gnu.org/licenses/>. |
20 |
* |
21 |
* The authors can be reached via e-mail to <support@deliantra.net> |
22 |
*/ |
23 |
|
24 |
#include <global.h> |
25 |
#include <random_map.h> |
26 |
#include <rproto.h> |
27 |
|
28 |
void |
29 |
layout::alloc (int w, int h) |
30 |
{ |
31 |
assert (sizeof (cell) == 1); |
32 |
|
33 |
this->w = w; |
34 |
this->h = h; |
35 |
|
36 |
// we store the layout in a single contiguous memory layout |
37 |
// first part consists of pointers to each column, followed |
38 |
// by the actual columns (not rows!) |
39 |
int size = (sizeof (cell *) + sizeof (cell) * h) * w; |
40 |
|
41 |
data = (cell **)salloc<char> (size); |
42 |
|
43 |
cell *p = (cell *)(data + w); |
44 |
|
45 |
for (int x = w; x--; ) |
46 |
data [x] = p + x * h; |
47 |
} |
48 |
|
49 |
layout::layout (int w, int h) |
50 |
{ |
51 |
alloc (w, h); |
52 |
} |
53 |
|
54 |
layout::layout (layout ©) |
55 |
{ |
56 |
alloc (copy.w, copy.h); |
57 |
|
58 |
memcpy (data [0], copy.data [0], sizeof (cell) * h * w); |
59 |
} |
60 |
|
61 |
layout::~layout () |
62 |
{ |
63 |
int size = (sizeof (cell *) + sizeof (cell) * h) * w; |
64 |
|
65 |
sfree ((char *)data, size); |
66 |
} |
67 |
|
68 |
void |
69 |
layout::fill (char fill) |
70 |
{ |
71 |
memset (data [0], fill, w * h); |
72 |
} |
73 |
|
74 |
void |
75 |
layout::rect (int x1, int y1, int x2, int y2, char fill) |
76 |
{ |
77 |
--x2; |
78 |
|
79 |
memset (data [x1] + y1, fill, y2 - y1); |
80 |
memset (data [x2] + y1, fill, y2 - y1); |
81 |
|
82 |
while (++x1 < x2) |
83 |
data [x1][y1] = data [x1][y2 - 1] = fill; |
84 |
} |
85 |
|
86 |
void |
87 |
layout::fill_rect (int x1, int y1, int x2, int y2, char fill) |
88 |
{ |
89 |
for (; x1 < x2; ++x1) |
90 |
memset (data [x1] + y1, fill, y2 - y1); |
91 |
} |
92 |
|
93 |
void layout::border (char fill) |
94 |
{ |
95 |
rect (0, 0, w, h, fill); |
96 |
} |
97 |
|
98 |
void |
99 |
layout::fill_rand (int percent) |
100 |
{ |
101 |
percent = lerp (percent, 0, 100, 0, 256); |
102 |
|
103 |
for (int x = w - 1; --x > 0; ) |
104 |
for (int y = h - 1; --y > 0; ) |
105 |
data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
106 |
} |
107 |
|
108 |
///////////////////////////////////////////////////////////////////////////// |
109 |
|
110 |
// erode by cellular automata |
111 |
void |
112 |
layout::erode_1_2 (int c1, int c2, int repeat) |
113 |
{ |
114 |
layout neu (w, h); |
115 |
|
116 |
while (repeat--) |
117 |
{ |
118 |
for (int x = 0; x < w; ++x) |
119 |
{ |
120 |
coroapi::cede_to_tick (); |
121 |
|
122 |
for (int y = 0; y < h; ++y) |
123 |
{ |
124 |
int n1 = 0, n2 = 0; |
125 |
|
126 |
// a 5x5 area, dx, dy, distance (1 == <= 1, 0 <= 2) |
127 |
static I8 dds[][3] = { |
128 |
{ -2, -1, 0 }, { -2, 0, 0 }, { -2, 1, 0 }, |
129 |
{ -1, -2, 0 }, { -1, -1, 1 }, { -1, 0, 1 }, { -1, 1, 1 }, { -1, 2, 0 }, |
130 |
{ 0, -2, 0 }, { 0, -1, 1 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 2, 0 }, |
131 |
{ 1, -2, 0 }, { 1, -1, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 1, 2, 0 }, |
132 |
{ 2, -1, 0 }, { 2, 0, 0 }, { 2, 1, 0 }, |
133 |
}; |
134 |
|
135 |
for (int i = array_length (dds); i--; ) |
136 |
{ |
137 |
int nx = x + dds [i][0]; |
138 |
int ny = y + dds [i][1]; |
139 |
|
140 |
if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny]) |
141 |
{ |
142 |
n1 += dds [i][2]; |
143 |
n2++; |
144 |
} |
145 |
} |
146 |
|
147 |
neu [x][y] = n1 >= c1 || n2 <= c2 ? '#' : 0; |
148 |
} |
149 |
} |
150 |
|
151 |
swap (neu); |
152 |
} |
153 |
} |
154 |
|
155 |
///////////////////////////////////////////////////////////////////////////// |
156 |
|
157 |
void |
158 |
layout::print () const |
159 |
{ |
160 |
for (int y = 0; y < h; y++) |
161 |
{ |
162 |
for (int x = 0; x < w; x++) |
163 |
{ |
164 |
U8 c = (U8)data [x][y]; |
165 |
|
166 |
if (!c) |
167 |
c = ' '; |
168 |
else if (c < 10) |
169 |
c += '0'; |
170 |
else if (c < 32) |
171 |
c += 'a' - 10; |
172 |
|
173 |
putc ((char)c, stdout); |
174 |
} |
175 |
|
176 |
putc ('\n', stdout); |
177 |
} |
178 |
|
179 |
putc ('\n', stdout); |
180 |
} |
181 |
|
182 |
///////////////////////////////////////////////////////////////////////////// |
183 |
// isolation remover - ensures single connected area |
184 |
|
185 |
typedef fixed_stack<point> pointlist; |
186 |
|
187 |
static void noinline |
188 |
push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
189 |
{ |
190 |
if (dist [x][y]) |
191 |
return; |
192 |
|
193 |
while (y > 0 && !dist [x][y - 1]) |
194 |
--y; |
195 |
|
196 |
int y0 = y; |
197 |
|
198 |
while (y < dist.h && !dist [x][y]) |
199 |
{ |
200 |
seeds.push (point (x, y)); |
201 |
|
202 |
dist [x][y] = 1; |
203 |
++y; |
204 |
} |
205 |
|
206 |
while (--y >= y0) |
207 |
{ |
208 |
if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y); |
209 |
if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y); |
210 |
} |
211 |
} |
212 |
|
213 |
static inline void |
214 |
make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) |
215 |
{ |
216 |
for (;;) |
217 |
{ |
218 |
point neigh[4]; |
219 |
int ncnt = 0; |
220 |
|
221 |
if (x > 1 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); |
222 |
if (x < dist.w - 2 && U8 (dist [x + 1][y]) <= d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y); |
223 |
if (y > 1 && U8 (dist [x][y - 1]) <= d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1); |
224 |
if (y < dist.h - 2 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
225 |
|
226 |
printf ("tunnel %d+%d ncnt %d\n", x, y, ncnt);//D |
227 |
|
228 |
if (!ncnt) |
229 |
return; |
230 |
|
231 |
point &p = neigh [rmg_rndm (ncnt)]; |
232 |
|
233 |
seeds.push (p); |
234 |
|
235 |
x = p.x; |
236 |
y = p.y; |
237 |
|
238 |
d = dist [x][y]; |
239 |
dist [x][y] = 1; |
240 |
} |
241 |
} |
242 |
|
243 |
static void inline |
244 |
maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) |
245 |
{ |
246 |
char &D = dist [x][y]; |
247 |
|
248 |
if (U8 (D) > d) // if wall and higher distance, lower distance |
249 |
D = d; |
250 |
else if (D) // otherwise, if it's no room, this space is uninteresting |
251 |
return; |
252 |
|
253 |
seeds.push (point (x, y)); |
254 |
} |
255 |
|
256 |
// isolation remover, works on a "distance" map |
257 |
// the map must be initialised with 0 == rooms, 255 = walls |
258 |
static void noinline |
259 |
isolation_remover (layout &dist) |
260 |
{ |
261 |
// dist contains |
262 |
// 0 == invisited rooms |
263 |
// 1 == visited rooms |
264 |
// 2+ shortest distance to random near room |
265 |
|
266 |
// phase 1, find seed |
267 |
int cnt = 0; |
268 |
int x, y; |
269 |
|
270 |
for (int i = 0; i < dist.w; ++i) |
271 |
for (int j = 0; j < dist.h; ++j) |
272 |
if (!dist [i][j] && !rmg_rndm (++cnt)) |
273 |
x = i, y = j; |
274 |
|
275 |
if (!cnt) |
276 |
{ |
277 |
// map is completely massive, this is not good, |
278 |
// so make it empty instead. |
279 |
dist.fill (1); |
280 |
dist.border (255); |
281 |
return; |
282 |
} |
283 |
|
284 |
fixed_stack<point> seeds (dist.w * dist.h * 5); |
285 |
|
286 |
// found first free space - picking the first one gives |
287 |
// us a slight bias for tunnels, but usually you won't |
288 |
// notice that in-game |
289 |
seeds.push (point (x, y)); |
290 |
|
291 |
// phase 2, while we have seeds, if |
292 |
// seed is empty, floodfill, else grow |
293 |
|
294 |
while (seeds.size) |
295 |
{ |
296 |
coroapi::cede_to_tick (); |
297 |
|
298 |
point p = seeds.remove (rmg_rndm (seeds.size)); |
299 |
|
300 |
x = p.x; |
301 |
y = p.y; |
302 |
|
303 |
if (!dist [x][y]) |
304 |
{ |
305 |
// found new isolated area, make tunnel |
306 |
push_flood_fill (dist, seeds, x, y); |
307 |
make_tunnel (dist, seeds, x, y, 255); |
308 |
} |
309 |
else |
310 |
{ |
311 |
// nothing here, continue to expand |
312 |
U8 d = U8 (dist [x][y]) + 1; |
313 |
|
314 |
if (x < dist.w - 2) maybe_push (dist, seeds, x + 1, y, d); |
315 |
if (x > 1) maybe_push (dist, seeds, x - 1, y, d); |
316 |
if (y < dist.h - 2) maybe_push (dist, seeds, x, y + 1, d); |
317 |
if (y > 1) maybe_push (dist, seeds, x, y - 1, d); |
318 |
} |
319 |
} |
320 |
} |
321 |
|
322 |
void |
323 |
layout::isolation_remover () |
324 |
{ |
325 |
layout dist (w, h); |
326 |
|
327 |
for (int x = 1; x < w - 1; ++x) |
328 |
for (int y = 1; y < h - 1; ++y) |
329 |
dist [x][y] = data [x][y] == '#' ? U8 (255) : 0; |
330 |
|
331 |
::isolation_remover (dist); |
332 |
|
333 |
// now copy the tunnels over |
334 |
for (int x = 1; x < w - 1; ++x) |
335 |
for (int y = 1; y < h - 1; ++y) |
336 |
if (data [x][y] == '#' && dist [x][y] == 1) |
337 |
data [x][y] = 0; |
338 |
} |
339 |
|
340 |
///////////////////////////////////////////////////////////////////////////// |
341 |
|
342 |
// inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
343 |
void |
344 |
layout::gen_cave (int subtype) |
345 |
{ |
346 |
switch (subtype) |
347 |
{ |
348 |
// a rough cave |
349 |
case 0: |
350 |
fill_rand (rmg_rndm (85, 97)); |
351 |
break; |
352 |
|
353 |
// corridors |
354 |
case 1: |
355 |
fill_rand (rmg_rndm (5, 40)); |
356 |
erode_1_2 (5, 2, 10); |
357 |
erode_1_2 (5, -1, 10); |
358 |
erode_1_2 (5, 2, 1); |
359 |
break; |
360 |
|
361 |
// somewhat open, roundish |
362 |
case 2: |
363 |
fill_rand (45); |
364 |
erode_1_2 (5, 0, 5); |
365 |
erode_1_2 (5, 1, 1); |
366 |
break; |
367 |
|
368 |
// wide open, some room-like structures |
369 |
case 3: |
370 |
fill_rand (45); |
371 |
erode_1_2 (5, 2, 4); |
372 |
erode_1_2 (5, -1, 3); |
373 |
break; |
374 |
} |
375 |
|
376 |
border (); |
377 |
isolation_remover (); |
378 |
} |
379 |
|
380 |
///////////////////////////////////////////////////////////////////////////// |
381 |
|
382 |
//+GPL |
383 |
|
384 |
/* puts doors at appropriate locations in a maze. */ |
385 |
void |
386 |
layout::doorify () |
387 |
{ |
388 |
int ndoors = w * h / 60; /* reasonable number of doors. */ |
389 |
int doorlocs = 0; /* # of available doorlocations */ |
390 |
|
391 |
uint16 *doorlist_x = salloc<uint16> (w * h); |
392 |
uint16 *doorlist_y = salloc<uint16> (w * h); |
393 |
|
394 |
/* make a list of possible door locations */ |
395 |
for (int i = 1; i < w - 1; i++) |
396 |
for (int j = 1; j < h - 1; j++) |
397 |
{ |
398 |
int sindex = surround_flag (*this, i, j); |
399 |
|
400 |
if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
401 |
{ |
402 |
doorlist_x [doorlocs] = i; |
403 |
doorlist_y [doorlocs] = j; |
404 |
doorlocs++; |
405 |
} |
406 |
} |
407 |
|
408 |
while (ndoors > 0 && doorlocs > 0) |
409 |
{ |
410 |
int di = rmg_rndm (doorlocs); |
411 |
int i = doorlist_x [di]; |
412 |
int j = doorlist_y [di]; |
413 |
int sindex = surround_flag (*this, i, j); |
414 |
|
415 |
if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
416 |
{ |
417 |
data [i][j] = 'D'; |
418 |
ndoors--; |
419 |
} |
420 |
|
421 |
/* reduce the size of the list */ |
422 |
doorlocs--; |
423 |
doorlist_x[di] = doorlist_x [doorlocs]; |
424 |
doorlist_y[di] = doorlist_y [doorlocs]; |
425 |
} |
426 |
|
427 |
sfree (doorlist_x, w * h); |
428 |
sfree (doorlist_y, w * h); |
429 |
} |
430 |
|
431 |
/* takes a map and makes it symmetric: adjusts Xsize and |
432 |
* Ysize to produce a symmetric map. |
433 |
*/ |
434 |
void |
435 |
layout::symmetrize (int symmetry) |
436 |
{ |
437 |
if (symmetry == SYMMETRY_NONE) |
438 |
return; |
439 |
|
440 |
layout sym_layout ( |
441 |
symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w, |
442 |
symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h |
443 |
); |
444 |
|
445 |
if (symmetry == SYMMETRY_X) |
446 |
for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
447 |
for (int j = 0; j < sym_layout.h; j++) |
448 |
{ |
449 |
sym_layout[i ][j] = |
450 |
sym_layout[sym_layout.w - i - 1][j] = data [i][j]; |
451 |
} |
452 |
|
453 |
if (symmetry == SYMMETRY_Y) |
454 |
for (int i = 0; i < sym_layout.w; i++) |
455 |
for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
456 |
{ |
457 |
sym_layout[i][j ] = |
458 |
sym_layout[i][sym_layout.h - j - 1] = data [i][j]; |
459 |
} |
460 |
|
461 |
if (symmetry == SYMMETRY_XY) |
462 |
for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
463 |
for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
464 |
{ |
465 |
sym_layout[i ][j ] = |
466 |
sym_layout[i ][sym_layout.h - j - 1] = |
467 |
sym_layout[sym_layout.w - i - 1][j ] = |
468 |
sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j]; |
469 |
} |
470 |
|
471 |
/* need to run the isolation remover for some layouts */ |
472 |
#if 0 |
473 |
switch (RP->map_layout_style) |
474 |
{ |
475 |
case LAYOUT_ONION: |
476 |
case LAYOUT_SNAKE: |
477 |
case LAYOUT_SQUARE_SPIRAL: |
478 |
// safe |
479 |
break; |
480 |
|
481 |
default: |
482 |
sym_layout.isolation_remover (); |
483 |
break; |
484 |
} |
485 |
#endif |
486 |
sym_layout.isolation_remover (); |
487 |
|
488 |
swap (sym_layout); |
489 |
} |
490 |
|
491 |
//-GPL |
492 |
|
493 |
void |
494 |
layout::rotate (int rotation) |
495 |
{ |
496 |
switch (rotation & 3) |
497 |
{ |
498 |
case 2: /* a reflection */ |
499 |
{ |
500 |
layout new_layout (w, h); |
501 |
|
502 |
for (int i = 0; i < w; i++) /* copy a reflection back */ |
503 |
for (int j = 0; j < h; j++) |
504 |
new_layout [i][j] = data [w - i - 1][h - j - 1]; |
505 |
|
506 |
swap (new_layout); |
507 |
} |
508 |
break; |
509 |
|
510 |
case 1: |
511 |
case 3: |
512 |
{ |
513 |
layout new_layout (h, w); |
514 |
|
515 |
if (rotation == 1) /* swap x and y */ |
516 |
for (int i = 0; i < w; i++) |
517 |
for (int j = 0; j < h; j++) |
518 |
new_layout [j][i] = data [i][j]; |
519 |
|
520 |
if (rotation == 3) /* swap x and y */ |
521 |
for (int i = 0; i < w; i++) |
522 |
for (int j = 0; j < h; j++) |
523 |
new_layout [j][i] = data [w - i - 1][h - j - 1]; |
524 |
|
525 |
swap (new_layout); |
526 |
} |
527 |
break; |
528 |
} |
529 |
} |
530 |
|
531 |
///////////////////////////////////////////////////////////////////////////// |
532 |
|
533 |
//+GPL |
534 |
|
535 |
/* |
536 |
* Expands a maze by 2x in each dimension. |
537 |
* H. S. Teoh |
538 |
*/ |
539 |
|
540 |
/* Copy the old tile X into the new one at location (i*2, j*2) and |
541 |
* fill up the rest of the 2x2 result with \0: |
542 |
* X ---> X \0 |
543 |
* \0 \0 |
544 |
*/ |
545 |
static void inline |
546 |
expand_misc (layout &newlayout, int i, int j, layout &maze) |
547 |
{ |
548 |
newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = maze[i][j]; |
549 |
/* (Note: no need to reset rest of 2x2 area to \0 because calloc does that |
550 |
* for us.) */ |
551 |
} |
552 |
|
553 |
/* Returns a bitmap that represents which squares on the right and bottom |
554 |
* edges of a square (i,j) match the given character: |
555 |
* 1 match on (i+1, j) |
556 |
* 2 match on (i, j+1) |
557 |
* 4 match on (i+1, j+1) |
558 |
* and the possible combinations thereof. |
559 |
*/ |
560 |
static int noinline |
561 |
calc_pattern (char ch, layout &maze, int i, int j) |
562 |
{ |
563 |
int pattern = 0; |
564 |
|
565 |
if (i + 1 < maze.w && maze[i + 1][j] == ch) |
566 |
pattern |= 1; |
567 |
|
568 |
if (j + 1 < maze.h) |
569 |
{ |
570 |
if (maze[i][j + 1] == ch) |
571 |
pattern |= 2; |
572 |
|
573 |
if (i + 1 < maze.w && maze[i + 1][j + 1] == ch) |
574 |
pattern |= 4; |
575 |
} |
576 |
|
577 |
return pattern; |
578 |
} |
579 |
|
580 |
/* Expand a wall. This function will try to sensibly connect the resulting |
581 |
* wall to adjacent wall squares, so that the result won't have disconnected |
582 |
* walls. |
583 |
*/ |
584 |
static void inline |
585 |
expand_wall (layout &newlayout, int i, int j, layout &maze) |
586 |
{ |
587 |
int wall_pattern = calc_pattern ('#', maze, i, j); |
588 |
int door_pattern = calc_pattern ('D', maze, i, j); |
589 |
int both_pattern = wall_pattern | door_pattern; |
590 |
|
591 |
newlayout[i * 2][j * 2] = '#'; |
592 |
|
593 |
if (i + 1 < maze.w) |
594 |
{ |
595 |
if (both_pattern & 1) |
596 |
{ /* join walls/doors to the right */ |
597 |
/* newlayout[i*2+1][j*2] = '#'; */ |
598 |
newlayout[i * 2 + 1][j * 2] = maze[i + 1][j]; |
599 |
} |
600 |
} |
601 |
|
602 |
if (j + 1 < maze.h) |
603 |
{ |
604 |
if (both_pattern & 2) |
605 |
{ /* join walls/doors to the bottom */ |
606 |
/* newlayout[i*2][j*2+1] = '#'; */ |
607 |
newlayout[i * 2][j * 2 + 1] = maze[i][j + 1]; |
608 |
} |
609 |
|
610 |
if (wall_pattern == 7) |
611 |
{ /* if orig maze is a 2x2 wall block, |
612 |
* we fill the result with walls. */ |
613 |
newlayout[i * 2 + 1][j * 2 + 1] = '#'; |
614 |
} |
615 |
} |
616 |
} |
617 |
|
618 |
/* This function will try to sensibly connect doors so that they meet up with |
619 |
* adjacent walls. Note that it will also presumptuously delete (ignore) doors |
620 |
* that it doesn't know how to correctly expand. |
621 |
*/ |
622 |
static void inline |
623 |
expand_door (layout &newlayout, int i, int j, layout &maze) |
624 |
{ |
625 |
int wall_pattern = calc_pattern ('#', maze, i, j); |
626 |
int door_pattern = calc_pattern ('D', maze, i, j); |
627 |
int join_pattern; |
628 |
|
629 |
/* Doors "like" to connect to walls more than other doors. If there is |
630 |
* a wall and another door, this door will connect to the wall and |
631 |
* disconnect from the other door. */ |
632 |
if (wall_pattern & 3) |
633 |
join_pattern = wall_pattern; |
634 |
else |
635 |
join_pattern = door_pattern; |
636 |
|
637 |
newlayout[i * 2][j * 2] = 'D'; |
638 |
|
639 |
if (i + 1 < maze.w) |
640 |
if (join_pattern & 1) |
641 |
/* there is a door/wall to the right */ |
642 |
newlayout[i * 2 + 1][j * 2] = 'D'; |
643 |
|
644 |
if (j + 1 < maze.h) |
645 |
if (join_pattern & 2) |
646 |
/* there is a door/wall below */ |
647 |
newlayout[i * 2][j * 2 + 1] = 'D'; |
648 |
} |
649 |
|
650 |
void |
651 |
layout::expand2x () |
652 |
{ |
653 |
layout new_layout (w * 2 - 1, h * 2 - 1); |
654 |
|
655 |
new_layout.clear (); |
656 |
|
657 |
for (int i = 0; i < w; i++) |
658 |
for (int j = 0; j < h; j++) |
659 |
switch (data [i][j]) |
660 |
{ |
661 |
case '#': expand_wall (new_layout, i, j, *this); break; |
662 |
case 'D': expand_door (new_layout, i, j, *this); break; |
663 |
default: expand_misc (new_layout, i, j, *this); break; |
664 |
} |
665 |
|
666 |
swap (new_layout); |
667 |
} |
668 |
|
669 |
///////////////////////////////////////////////////////////////////////////// |
670 |
|
671 |
/* checks the maze to see if I can stick a horizontal(dir = 0) wall |
672 |
(or vertical, dir == 1) |
673 |
here which ends up on other walls sensibly. */ |
674 |
static int |
675 |
can_make_wall (const layout &maze, int dx, int dy, int dir) |
676 |
{ |
677 |
int i1; |
678 |
int length = 0; |
679 |
|
680 |
/* dont make walls if we're on the edge. */ |
681 |
if (dx == 0 || dx == (maze.w - 1) || dy == 0 || dy == (maze.h - 1)) |
682 |
return -1; |
683 |
|
684 |
/* don't make walls if we're ON a wall. */ |
685 |
if (maze [dx][dy] != 0) |
686 |
return -1; |
687 |
|
688 |
if (dir == 0) /* horizontal */ |
689 |
{ |
690 |
int y = dy; |
691 |
|
692 |
for (i1 = dx - 1; i1 > 0; i1--) |
693 |
{ |
694 |
int sindex = surround_flag2 (maze, i1, y); |
695 |
|
696 |
if (sindex == 1) break; |
697 |
if (sindex != 0) return -1; /* can't make horiz. wall here */ |
698 |
if (maze[i1][y] != 0) return -1; /* can't make horiz. wall here */ |
699 |
|
700 |
length++; |
701 |
} |
702 |
|
703 |
for (i1 = dx + 1; i1 < maze.w - 1; i1++) |
704 |
{ |
705 |
int sindex = surround_flag2 (maze, i1, y); |
706 |
|
707 |
if (sindex == 2) break; |
708 |
if (sindex != 0) return -1; /* can't make horiz. wall here */ |
709 |
if (maze[i1][y] != 0) return -1; /* can't make horiz. wall here */ |
710 |
|
711 |
length++; |
712 |
} |
713 |
return length; |
714 |
} |
715 |
else |
716 |
{ /* vertical */ |
717 |
int x = dx; |
718 |
|
719 |
for (i1 = dy - 1; i1 > 0; i1--) |
720 |
{ |
721 |
int sindex = surround_flag2 (maze, x, i1); |
722 |
|
723 |
if (sindex == 4) break; |
724 |
if (sindex != 0) return -1; /* can't make vert. wall here */ |
725 |
if (maze[x][i1] != 0) return -1; /* can't make horiz. wall here */ |
726 |
|
727 |
length++; |
728 |
} |
729 |
|
730 |
for (i1 = dy + 1; i1 < maze.h - 1; i1++) |
731 |
{ |
732 |
int sindex = surround_flag2 (maze, x, i1); |
733 |
|
734 |
if (sindex == 8) break; |
735 |
if (sindex != 0) return -1; /* can't make verti. wall here */ |
736 |
if (maze[x][i1] != 0) return -1; /* can't make horiz. wall here */ |
737 |
|
738 |
length++; |
739 |
} |
740 |
|
741 |
return length; |
742 |
} |
743 |
|
744 |
return -1; |
745 |
} |
746 |
|
747 |
int |
748 |
make_wall (char **maze, int x, int y, int dir) |
749 |
{ |
750 |
maze[x][y] = 'D'; /* mark a door */ |
751 |
|
752 |
switch (dir) |
753 |
{ |
754 |
case 0: /* horizontal */ |
755 |
{ |
756 |
for (int i1 = x - 1; maze[i1][y] == 0; --i1) maze[i1][y] = '#'; |
757 |
for (int i1 = x + 1; maze[i1][y] == 0; ++i1) maze[i1][y] = '#'; |
758 |
break; |
759 |
} |
760 |
case 1: /* vertical */ |
761 |
{ |
762 |
for (int i1 = y - 1; maze[x][i1] == 0; --i1) maze[x][i1] = '#'; |
763 |
for (int i1 = y + 1; maze[x][i1] == 0; ++i1) maze[x][i1] = '#'; |
764 |
break; |
765 |
} |
766 |
} |
767 |
|
768 |
return 0; |
769 |
} |
770 |
|
771 |
void |
772 |
layout::roomify () |
773 |
{ |
774 |
int tries = w * h / 30; |
775 |
|
776 |
for (int ti = 0; ti < tries; ti++) |
777 |
{ |
778 |
/* starting location for looking at creating a door */ |
779 |
int dx = rmg_rndm (w); |
780 |
int dy = rmg_rndm (h); |
781 |
|
782 |
/* results of checking on creating walls. */ |
783 |
int cx = can_make_wall (*this, dx, dy, 0); /* horizontal */ |
784 |
int cy = can_make_wall (*this, dx, dy, 1); /* vertical */ |
785 |
|
786 |
if (cx == -1) |
787 |
{ |
788 |
if (cy != -1) |
789 |
make_wall (*this, dx, dy, 1); |
790 |
|
791 |
continue; |
792 |
} |
793 |
|
794 |
if (cy == -1) |
795 |
{ |
796 |
make_wall (*this, dx, dy, 0); |
797 |
continue; |
798 |
} |
799 |
|
800 |
if (cx < cy) |
801 |
make_wall (*this, dx, dy, 0); |
802 |
else |
803 |
make_wall (*this, dx, dy, 1); |
804 |
} |
805 |
} |
806 |
|
807 |
///////////////////////////////////////////////////////////////////////////// |
808 |
|
809 |
/* function selects the maze function and gives it whatever |
810 |
arguments it needs. */ |
811 |
void |
812 |
layout::generate (random_map_params *RP) |
813 |
{ |
814 |
switch (RP->map_layout_style) |
815 |
{ |
816 |
case LAYOUT_ONION: |
817 |
map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2); |
818 |
|
819 |
if (!(rmg_rndm (3)) && !(RP->layoutoptions1 & (RMOPT_WALLS_ONLY | RMOPT_WALL_OFF))) |
820 |
roomify (); |
821 |
|
822 |
break; |
823 |
|
824 |
case LAYOUT_MAZE: |
825 |
maze_gen (*this, RP->get_iv ("maze_type", rmg_rndm (4))); |
826 |
|
827 |
if (rmg_rndm (2)) |
828 |
doorify (); |
829 |
|
830 |
break; |
831 |
|
832 |
case LAYOUT_SPIRAL: |
833 |
map_gen_spiral (*this, RP->layoutoptions1); |
834 |
|
835 |
if (rmg_rndm (2)) |
836 |
doorify (); |
837 |
|
838 |
break; |
839 |
|
840 |
case LAYOUT_ROGUELIKE: |
841 |
/* Don't put symmetry in rogue maps. There isn't much reason to |
842 |
* do so in the first place (doesn't make it any more interesting), |
843 |
* but more importantly, the symmetry code presumes we are symmetrizing |
844 |
* spirals, or maps with lots of passages - making a symmetric rogue |
845 |
* map fails because its likely that the passages the symmetry process |
846 |
* creates may not connect the rooms. |
847 |
*/ |
848 |
RP->symmetry_used = SYMMETRY_NONE; |
849 |
roguelike_layout_gen (*this, RP->layoutoptions1); |
850 |
/* no doorifying... done already */ |
851 |
break; |
852 |
|
853 |
case LAYOUT_SNAKE: |
854 |
make_snake_layout (*this, RP->layoutoptions1); |
855 |
|
856 |
if (rmg_rndm (2)) |
857 |
roomify (); |
858 |
|
859 |
break; |
860 |
|
861 |
case LAYOUT_SQUARE_SPIRAL: |
862 |
make_square_spiral_layout (*this, RP->layoutoptions1); |
863 |
|
864 |
if (rmg_rndm (2)) |
865 |
roomify (); |
866 |
|
867 |
break; |
868 |
|
869 |
case LAYOUT_CAVE: |
870 |
gen_cave (RP->get_iv ("cave_type", rmg_rndm (4))); |
871 |
|
872 |
if (rmg_rndm (2)) |
873 |
doorify (); |
874 |
|
875 |
break; |
876 |
|
877 |
default: |
878 |
abort (); |
879 |
} |
880 |
|
881 |
/* rotate the maze randomly */ |
882 |
rotate (rmg_rndm (4)); |
883 |
|
884 |
symmetrize (RP->symmetry_used); |
885 |
|
886 |
#if 0 |
887 |
print ();//D |
888 |
#endif |
889 |
|
890 |
if (RP->expand2x) |
891 |
expand2x (); |
892 |
} |
893 |
|
894 |
//-GPL |
895 |
|
896 |
#if 0 |
897 |
static struct demo |
898 |
{ |
899 |
demo () |
900 |
{ |
901 |
rmg_rndm.seed (time (0)); |
902 |
|
903 |
for(int i=1;i<100;i++) |
904 |
{ |
905 |
layout maze (40, 25); |
906 |
maze.fill_rand (85); |
907 |
maze.border (); |
908 |
maze.isolation_remover (); |
909 |
maze.print (); |
910 |
} |
911 |
|
912 |
exit (1); |
913 |
} |
914 |
} demo; |
915 |
#endif |