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.1 by root, Wed Jun 30 20:51:02 2010 UTC vs.
Revision 1.5 by root, Fri Jul 2 03:40:14 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
67void
68Layout::fill_rand (int percent)
69{
70 percent = lerp (percent, 0, 100, 0, 256);
71
72 for (int x = w - 1; --x > 0; )
73 for (int y = h - 1; --y > 0; )
74 col [x][y] = rmg_rndm (256) > percent ? 0 : '#';
75}
76
65///////////////////////////////////////////////////////////////////////////// 77/////////////////////////////////////////////////////////////////////////////
66 78
79// erode by cellular automata
67void 80void
81Layout::erode_1_2 (int c1, int c2, int repeat)
82{
83 Layout neu (w, h);
84
85 while (repeat--)
86 {
87 for (int x = 0; x < w; ++x)
88 {
89 coroapi::cede_to_tick ();
90
91 for (int y = 0; y < h; ++y)
92 {
93 int n1 = 0, n2 = 0;
94
95 // a 5x5 area, dx, dy, distance (1 == <= 1, 0 <= 2)
96 static I8 dds[][3] = {
97 { -2, -1, 0 }, { -2, 0, 0 }, { -2, 1, 0 },
98 { -1, -2, 0 }, { -1, -1, 1 }, { -1, 0, 1 }, { -1, 1, 1 }, { -1, 2, 0 },
99 { 0, -2, 0 }, { 0, -1, 1 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 2, 0 },
100 { 1, -2, 0 }, { 1, -1, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 1, 2, 0 },
101 { 2, -1, 0 }, { 2, 0, 0 }, { 2, 1, 0 },
102 };
103
104 for (int i = array_length (dds); i--; )
105 {
106 int nx = x + dds [i][0];
107 int ny = y + dds [i][1];
108
109 if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !col [nx][ny])
110 {
111 n1 += dds [i][2];
112 n2++;
113 }
114 }
115
116 neu [x][y] = n1 >= c1 || n2 <= c2 ? '#' : 0;
117 }
118 }
119
120 swap (neu);
121 }
122}
123
124/////////////////////////////////////////////////////////////////////////////
125
126void
68Layout::print () 127Layout::print () const
69{ 128{
70 for (int y = 0; y < ptr->h; y++) 129 for (int y = 0; y < h; y++)
71 { 130 {
72 for (int x = 0; x < ptr->w; x++) 131 for (int x = 0; x < w; x++)
73 { 132 {
74 U8 c = (U8)ptr->col[x][y]; 133 U8 c = (U8)col [x][y];
75 134
76 if (!c) 135 if (!c)
77 c = ' '; 136 c = ' ';
78 else if (c < 10) 137 else if (c < 10)
79 c += '0'; 138 c += '0';
92///////////////////////////////////////////////////////////////////////////// 151/////////////////////////////////////////////////////////////////////////////
93// isolation remover - ensures single connected area 152// isolation remover - ensures single connected area
94 153
95typedef fixed_stack<point> pointlist; 154typedef fixed_stack<point> pointlist;
96 155
97static void
98room (Layout &layout, int xc, int yc)
99{
100 layout->rect (xc - 2, yc - 2, xc + 3, yc + 3, 0);
101}
102
103static noinline void 156static noinline void
104push_flood_fill (Layout maze, Layout dist, pointlist &seeds, int x, int y) 157push_flood_fill (Layout &dist, pointlist &seeds, int x, int y)
105{ 158{
106 if (maze [x][y]) 159 if (dist [x][y])
107 return; 160 return;
108 161
109 while (y > 0 && !maze [x][y - 1]) 162 while (y > 0 && !dist [x][y - 1])
110 --y; 163 --y;
111 164
112 int y0 = y; 165 int y0 = y;
113 166
114 while (y < maze->h && !maze [x][y]) 167 while (y < dist.h && !dist [x][y])
115 { 168 {
116 seeds.push (point (x, y)); 169 seeds.push (point (x, y));
117 170
118 maze [x][y] = 1;
119 dist [x][y] = 1; 171 dist [x][y] = 1;
120 ++y; 172 ++y;
121 } 173 }
122 174
123 while (--y >= y0) 175 while (--y >= y0)
124 { 176 {
125 if (x > 0) push_flood_fill (maze, dist, seeds, x - 1, y); 177 if (x > 0) push_flood_fill (dist, seeds, x - 1, y);
126 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);
127 } 179 }
128} 180}
129 181
130static inline void 182static inline void
131make_tunnel (Layout maze, Layout dist, pointlist &seeds, int x, int y, U8 d) 183make_tunnel (Layout &dist, pointlist &seeds, int x, int y, U8 d)
132{ 184{
133 for (;;) 185 for (;;)
134 { 186 {
135 seeds.push (point (x, y));
136
137 maze [x][y] = 1;
138 dist [x][y] = 1;
139
140 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)
141 --x; 188 --x;
142 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)
143 ++x; 190 ++x;
144 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)
145 --y; 192 --y;
146 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)
147 ++y; 194 ++y;
148 else 195 else
149 break; 196 break;
150 197
151 d = dist [x][y]; 198 d = dist [x][y];
199 dist [x][y] = 1;
200 seeds.push (point (x, y));
152 } 201 }
202}
203
204static void inline
205maybe_push (Layout &dist, pointlist &seeds, int x, int y, U8 d)
206{
207 char &D = dist [x][y];
208
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));
153} 215}
154 216
155static void 217static void
156isolation_remover (Layout maze, bool dirty) 218isolation_remover (Layout &maze, bool dirty)
157{ 219{
158 Layout dist (maze->w, maze->h); 220 Layout dist (maze.w, maze.h);
159 221
160 dist->fill (255); 222 // dist contains
223 // 0 == invisited rooms
224 // 1 == visited rooms
225 // 2+ shortest distance to random near room
161 226
227 // phase 1, initialise dist array, find seed
228 int cnt = 0;
229 int x, y;
230
231 for (int i = 0; i < maze.w; ++i)
232 for (int j = 0; j < maze.h; ++j)
233 {
234 if (maze [i][j] == '#')
235 dist [i][j] = U8 (255);
236 else
237 {
238 dist [i][j] = 0;
239 if (!rmg_rndm (++cnt))
240 x = i, y = j;
241 }
242 }
243
244 if (!cnt)
245 return;
246
162 fixed_stack<point> seeds (maze->w * maze->h * 4); 247 fixed_stack<point> seeds (maze.w * maze.h * 5);
163 248
164 // phase 1, find seed 249 // found first free space - picking the first one gives
165 for (int x = 1; x < maze->w; ++x) 250 // us a slight bias for tunnels, but usually you won't
166 for (int y = 1; y < maze->h; ++y) 251 // notice that in-game
167 if (!maze [x][y])
168 {
169 seeds.push (point (x, y)); 252 seeds.push (point (x, y));
170 253
171 // phase 2, while we have seeds, if 254 // phase 2, while we have seeds, if
172 // seed is empty, floodfill, else grow 255 // seed is empty, floodfill, else grow
173 256
174 while (seeds.size) 257 while (seeds.size)
175 { 258 {
259 coroapi::cede_to_tick ();
260
176 point p = seeds.remove (rmg_rndm (seeds.size)); 261 point p = seeds.remove (rmg_rndm (seeds.size));
177 262
178 x = p.x; 263 x = p.x;
179 y = p.y; 264 y = p.y;
180 265
181 if (!maze [x][y]) 266 if (!dist [x][y])
182 { 267 {
183 // found new isolated area, make tunnel? 268 // found new isolated area, make tunnel
184 if (!dirty) 269 if (!dirty)
185 push_flood_fill (maze, dist, seeds, x, y); 270 push_flood_fill (dist, seeds, x, y);
186 271
187 make_tunnel (maze, dist, seeds, x, y, 255); 272 make_tunnel (dist, seeds, x, y, 255);
188 273
189 if (dirty) 274 if (dirty)
190 push_flood_fill (maze, dist, seeds, x, y); 275 push_flood_fill (dist, seeds, x, y);
191 } 276 }
192 else 277 else
193 { 278 {
279 // nothing here, continue to expand
194 U8 d = U8 (dist [x][y]) + 1; 280 U8 d = U8 (dist [x][y]) + 1;
195 281
196 if (x < maze->w - 1 && U8 (dist [x + 1][y]) > d) 282 if (x < dist.w - 1) maybe_push (dist, seeds, x + 1, y, d);
197 { 283 if (x > 0) maybe_push (dist, seeds, x - 1, y, d);
198 dist [x + 1][y] = d; 284 if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d);
199 seeds.push (point (x + 1, y)); 285 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
200 }
201
202 if (x > 0 && U8 (dist [x - 1][y]) > d)
203 {
204 dist [x - 1][y] = d;
205 seeds.push (point (x - 1, y));
206 }
207
208 if (y < maze->h - 1 && U8 (dist [x][y + 1]) > d)
209 {
210 dist [x][y + 1] = d;
211 seeds.push (point (x, y + 1));
212 }
213
214 if (y > 0 && U8 (dist [x][y - 1]) > d)
215 {
216 dist [x][y - 1] = d;
217 seeds.push (point (x, y - 1));
218 }
219 }
220 } 286 }
287 }
221 288
222 goto success; 289 // now copy the tunnels over
223 }
224
225success:
226 dist.free ();
227
228 // we mark free but visited floors as 1, undo this here
229 for (int x = 0; x < maze->w; ++x) 290 for (int x = 0; x < maze.w; ++x)
230 for (int y = 0; y < maze->h; ++y) 291 for (int y = 0; y < maze.h; ++y)
231 if (maze [x][y] == 1) 292 if (maze [x][y] == '#' && dist [x][y] == 1)
232 maze [x][y] = 0; 293 maze [x][y] = 0;
233} 294}
234 295
235void 296void
236LayoutData::isolation_remover (bool dirty) 297Layout::isolation_remover (bool dirty)
237{ 298{
238 Layout maze;
239 maze.ptr = this;
240 ::isolation_remover (maze, dirty); 299 ::isolation_remover (*this, dirty);
241} 300}
301
302/////////////////////////////////////////////////////////////////////////////
303
304// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
305void
306Layout::gen_cave (int subtype)
307{
308 switch (subtype)
309 {
310 // a rough cave
311 case 0:
312 fill_rand (rmg_rndm (80, 95));
313 break;
314
315 // corridors
316 case 1:
317 fill_rand (rmg_rndm (5, 40));
318 erode_1_2 (5, 2, 10);
319 erode_1_2 (5, -1, 10);
320 erode_1_2 (5, 2, 1);
321 break;
322
323 // somewhat open, roundish
324 case 2:
325 fill_rand (45);
326 erode_1_2 (5, 0, 5);
327 erode_1_2 (5, 1, 1);
328 break;
329
330 // wide open, some room-like structures
331 case 3:
332 fill_rand (45);
333 erode_1_2 (5, 2, 4);
334 erode_1_2 (5, -1, 3);
335 break;
336 }
337
338 border ();
339 isolation_remover ();
340}
341
342/////////////////////////////////////////////////////////////////////////////
343
344//+GPL
345
346/* puts doors at appropriate locations in a layout. */
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 layout 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 &layout)
509{
510 newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = layout[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 &layout, int i, int j)
524{
525 int pattern = 0;
526
527 if (i + 1 < layout.w && layout[i + 1][j] == ch)
528 pattern |= 1;
529
530 if (j + 1 < layout.h)
531 {
532 if (layout[i][j + 1] == ch)
533 pattern |= 2;
534
535 if (i + 1 < layout.w && layout[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 &layout)
548{
549 int wall_pattern = calc_pattern ('#', layout, i, j);
550 int door_pattern = calc_pattern ('D', layout, i, j);
551 int both_pattern = wall_pattern | door_pattern;
552
553 newlayout[i * 2][j * 2] = '#';
554
555 if (i + 1 < layout.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] = layout[i + 1][j];
561 }
562 }
563
564 if (j + 1 < layout.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] = layout[i][j + 1];
570 }
571
572 if (wall_pattern == 7)
573 { /* if orig layout 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 &layout)
586{
587 int wall_pattern = calc_pattern ('#', layout, i, j);
588 int door_pattern = calc_pattern ('D', layout, 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 < layout.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 < layout.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 layout 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 layout 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 layout 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
242 857
243#if 0 858#if 0
244static struct demo 859static struct demo
245{ 860{
246 demo () 861 demo ()
247 { 862 {
248 Layout maze (40, 25); 863 Layout maze (40, 25);
249 rmg_rndm.seed (time (0)); 864 rmg_rndm.seed (time (0));
250 865
251 for (int p = 80; p < 100; p += 1) {
252 maze->fill ('#');
253
254#if 1
255 for (int x = 1; x < maze->w - 1; ++x)
256 for (int y = 1; y < maze->h - 1; ++y)
257 maze [x][y] = rmg_rndm(100) < p ? '#' : 0;
258#else
259 room (maze, 5, 5);
260 room (maze, 30,20);
261 room (maze, 10,20);
262 room (maze, 20,10);
263#endif
264
265 isolation_remover (maze, 1);
266 maze.print ();
267 }
268
269#if 0
270 for(int i=1;i<10;i) 866 for(int i=1;i<10;i)
271 { 867 {
272 maze_gen (maze, 1); 868 maze.fill_rand (97);
869 maze.border ();
870 maze.isolation_remover ();
273 maze.print (); 871 maze.print ();
274 } 872 }
275#endif 873
276 exit (1); 874 exit (1);
277 } 875 }
278} demo; 876} demo;
279#endif 877#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines