ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/layout.C
(Generate patch)

Comparing deliantra/server/random_maps/layout.C (file contents):
Revision 1.2 by root, Wed Jun 30 23:03:40 2010 UTC vs.
Revision 1.7 by root, Fri Jul 2 15:03:57 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines