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.12 by root, Sat Jul 3 01:49:18 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) 28void
27: w(w), h(h) 29layout::alloc (int w, int h)
28{ 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!)
29 int size = (sizeof (char *) + sizeof (char) * h) * w; 39 int size = (sizeof (cell *) + sizeof (cell) * h) * w;
30 40
31 col = (char **)salloc<char> (size); 41 data = (cell **)salloc<char> (size);
32 42
33 char *data = (char *)(col + w); 43 cell *p = (cell *)(data + w);
34 44
35 for (int x = w; x--; ) 45 for (int x = w; x--; )
36 col [x] = data + x * h; 46 data [x] = p + x * h;
37} 47}
38 48
39LayoutData::~LayoutData () 49layout::layout (int w, int h)
40{ 50{
51 alloc (w, h);
52}
53
54layout::layout (layout &copy)
55{
56 alloc (copy.w, copy.h);
57
58 memcpy (data [0], copy.data [0], sizeof (cell) * h * w);
59}
60
61layout::~layout ()
62{
41 int size = (sizeof (char *) + sizeof (char) * h) * w; 63 int size = (sizeof (cell *) + sizeof (cell) * h) * w;
42 64
43 sfree ((char *)col, size); 65 sfree ((char *)data, size);
44} 66}
45 67
46void 68void
47LayoutData::fill (char fill) 69layout::fill (char fill)
48{ 70{
49 memset (col [0], fill, w * h); 71 memset (data [0], fill, w * h);
50} 72}
51 73
52void 74void
53LayoutData::rect (int x1, int y1, int x2, int y2, char fill) 75layout::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
86void
87layout::fill_rect (int x1, int y1, int x2, int y2, char fill)
54{ 88{
55 for (; x1 < x2; ++x1) 89 for (; x1 < x2; ++x1)
56 memset (col [x1] + y1, fill, y2 - y1); 90 memset (data [x1] + y1, fill, y2 - y1);
57} 91}
58 92
59void LayoutData::border (char fill) 93void layout::border (char fill)
60{ 94{
61 for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; 95 rect (0, 0, w, h, fill);
62 for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill;
63} 96}
64 97
65void 98void
66LayoutData::fill_rand (int percent) 99layout::fill_rand (int percent)
67{ 100{
68 percent = lerp (percent, 0, 100, 0, 256); 101 percent = lerp (percent, 0, 100, 0, 256);
69 102
70 for (int x = w - 1; --x > 0; ) 103 for (int x = w - 1; --x > 0; )
71 for (int y = h - 1; --y > 0; ) 104 for (int y = h - 1; --y > 0; )
72 col [x][y] = rmg_rndm (256) > percent ? 0 : '#'; 105 data [x][y] = rmg_rndm (256) > percent ? 0 : '#';
73} 106}
74 107
75///////////////////////////////////////////////////////////////////////////// 108/////////////////////////////////////////////////////////////////////////////
76 109
77// erode by cellular automata 110// erode by cellular automata
78void 111void
79LayoutData::erode_1_2 (int c1, int c2, int repeat) 112layout::erode_1_2 (int c1, int c2, int repeat)
80{ 113{
81 LayoutData neu (w, h); 114 layout neu (w, h);
82 115
83 while (repeat--) 116 while (repeat--)
84 { 117 {
85 for (int x = 0; x < w; ++x) 118 for (int x = 0; x < w; ++x)
86 { 119 {
90 { 123 {
91 int n1 = 0, n2 = 0; 124 int n1 = 0, n2 = 0;
92 125
93 // a 5x5 area, dx, dy, distance (1 == <= 1, 0 <= 2) 126 // a 5x5 area, dx, dy, distance (1 == <= 1, 0 <= 2)
94 static I8 dds[][3] = { 127 static I8 dds[][3] = {
95 { -2, -1, 0 }, 128 { -2, -1, 0 }, { -2, 0, 0 }, { -2, 1, 0 },
96 { -2, 0, 0 }, 129 { -1, -2, 0 }, { -1, -1, 1 }, { -1, 0, 1 }, { -1, 1, 1 }, { -1, 2, 0 },
97 { -2, 1, 0 }, 130 { 0, -2, 0 }, { 0, -1, 1 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 2, 0 },
98 131 { 1, -2, 0 }, { 1, -1, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 1, 2, 0 },
99 { -1, -2, 0 }, 132 { 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 }; 133 };
121 134
122 for (int i = array_length (dds); i--; ) 135 for (int i = array_length (dds); i--; )
123 { 136 {
124 int nx = x + dds [i][0]; 137 int nx = x + dds [i][0];
125 int ny = y + dds [i][1]; 138 int ny = y + dds [i][1];
126 139
127 if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !col [nx][ny]) 140 if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny])
128 { 141 {
129 n1 += dds [i][2]; 142 n1 += dds [i][2];
130 n2++; 143 n2++;
131 } 144 }
132 } 145 }
140} 153}
141 154
142///////////////////////////////////////////////////////////////////////////// 155/////////////////////////////////////////////////////////////////////////////
143 156
144void 157void
145LayoutData::print () 158layout::print () const
146{ 159{
147 for (int y = 0; y < h; y++) 160 for (int y = 0; y < h; y++)
148 { 161 {
149 for (int x = 0; x < w; x++) 162 for (int x = 0; x < w; x++)
150 { 163 {
151 U8 c = (U8)col [x][y]; 164 U8 c = (U8)data [x][y];
152 165
153 if (!c) 166 if (!c)
154 c = ' '; 167 c = ' ';
155 else if (c < 10) 168 else if (c < 10)
156 c += '0'; 169 c += '0';
169///////////////////////////////////////////////////////////////////////////// 182/////////////////////////////////////////////////////////////////////////////
170// isolation remover - ensures single connected area 183// isolation remover - ensures single connected area
171 184
172typedef fixed_stack<point> pointlist; 185typedef fixed_stack<point> pointlist;
173 186
174static noinline void 187static void noinline
175push_flood_fill (LayoutData &maze, LayoutData &dist, pointlist &seeds, int x, int y) 188push_flood_fill (layout &dist, pointlist &seeds, int x, int y)
176{ 189{
177 if (maze [x][y]) 190 if (dist [x][y])
178 return; 191 return;
179 192
180 while (y > 0 && !maze [x][y - 1]) 193 while (y > 0 && !dist [x][y - 1])
181 --y; 194 --y;
182 195
183 int y0 = y; 196 int y0 = y;
184 197
185 while (y < maze.h && !maze [x][y]) 198 while (y < dist.h && !dist [x][y])
186 { 199 {
187 seeds.push (point (x, y)); 200 seeds.push (point (x, y));
188 201
189 maze [x][y] = 1;
190 dist [x][y] = 1; 202 dist [x][y] = 1;
191 ++y; 203 ++y;
192 } 204 }
193 205
194 while (--y >= y0) 206 while (--y >= y0)
195 { 207 {
196 if (x > 0) push_flood_fill (maze, dist, seeds, x - 1, y); 208 if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y);
197 if (x < maze.w - 1) push_flood_fill (maze, dist, seeds, x + 1, y); 209 if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y);
198 } 210 }
199} 211}
200 212
201static inline void 213static inline void
202make_tunnel (LayoutData &maze, LayoutData &dist, pointlist &seeds, int x, int y, U8 d) 214make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d)
203{ 215{
204 for (;;) 216 for (;;)
205 { 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
206 seeds.push (point (x, y)); 233 seeds.push (p);
207 234
208 maze [x][y] = 1; 235 x = p.x;
236 y = p.y;
237
238 d = dist [x][y];
209 dist [x][y] = 1; 239 dist [x][y] = 1;
240 }
241}
210 242
211 if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) 243static void inline
244maybe_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
258static void noinline
259isolation_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])
212 --x; 304 {
213 else if (x < maze.w - 2 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) 305 // found new isolated area, make tunnel
306 push_flood_fill (dist, seeds, x, y);
307 make_tunnel (dist, seeds, x, y, 255);
214 ++x; 308 }
215 else if (y > 1 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1)
216 --y;
217 else if (y < maze.h - 2 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1)
218 ++y;
219 else 309 else
220 break;
221
222 d = dist [x][y];
223 }
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));
244
245 // phase 2, while we have seeds, if
246 // seed is empty, floodfill, else grow
247
248 while (seeds.size)
249 { 310 {
250 coroapi::cede_to_tick (); 311 // nothing here, continue to expand
251
252 point p = seeds.remove (rmg_rndm (seeds.size));
253
254 x = p.x;
255 y = p.y;
256
257 if (!maze [x][y])
258 {
259 // found new isolated area, make tunnel?
260 if (!dirty)
261 push_flood_fill (maze, dist, seeds, x, y);
262
263 make_tunnel (maze, dist, seeds, x, y, 255);
264
265 if (dirty)
266 push_flood_fill (maze, dist, seeds, x, y);
267 }
268 else
269 {
270 U8 d = U8 (dist [x][y]) + 1; 312 U8 d = U8 (dist [x][y]) + 1;
271 313
272 if (x < maze.w - 1 && U8 (dist [x + 1][y]) > d) 314 if (x < dist.w - 2) maybe_push (dist, seeds, x + 1, y, d);
273 { 315 if (x > 1) maybe_push (dist, seeds, x - 1, y, d);
274 dist [x + 1][y] = d; 316 if (y < dist.h - 2) maybe_push (dist, seeds, x, y + 1, d);
275 seeds.push (point (x + 1, y)); 317 if (y > 1) 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 } 318 }
319 }
320}
297 321
298 goto success; 322void
299 } 323layout::isolation_remover ()
324{
325 layout dist (w, h);
300 326
301success:
302
303 // we mark free but visited floors as 1, undo this here
304 for (int x = 0; x < maze.w; ++x) 327 for (int x = 1; x < w - 1; ++x)
305 for (int y = 0; y < maze.h; ++y) 328 for (int y = 1; y < h - 1; ++y)
306 if (maze [x][y] == 1) 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)
307 maze [x][y] = 0; 337 data [x][y] = 0;
308}
309
310void
311LayoutData::isolation_remover (bool dirty)
312{
313 ::isolation_remover (*this, dirty);
314} 338}
315 339
316///////////////////////////////////////////////////////////////////////////// 340/////////////////////////////////////////////////////////////////////////////
317 341
318// inspired mostly by http://www.jimrandomh.org/misc/caves.txt 342// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
319void 343void
320LayoutData::gen_cave (int subtype) 344layout::gen_cave (int subtype)
321{ 345{
322 switch (subtype) 346 switch (subtype)
323 { 347 {
324 // a rough cave 348 // a rough cave
325 case 0: 349 case 0:
326 fill_rand (rmg_rndm (80, 95)); 350 fill_rand (rmg_rndm (85, 97));
327 break; 351 break;
328 352
329 // corridors 353 // corridors
330 case 1: 354 case 1:
331 fill_rand (rmg_rndm (5, 40)); 355 fill_rand (rmg_rndm (5, 40));
351 375
352 border (); 376 border ();
353 isolation_remover (); 377 isolation_remover ();
354} 378}
355 379
380/////////////////////////////////////////////////////////////////////////////
381
382//+GPL
383
384/* puts doors at appropriate locations in a maze. */
385void
386layout::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 */
434void
435layout::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
493void
494layout::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 */
545static void inline
546expand_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 */
560static int noinline
561calc_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 */
584static void inline
585expand_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 */
622static void inline
623expand_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
650void
651layout::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. */
674static int
675can_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
747int
748make_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
771void
772layout::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. */
811void
812layout::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
356#if 0 896#if 0
357static struct demo 897static struct demo
358{ 898{
359 demo () 899 demo ()
360 { 900 {
361 Layout maze (40, 25);
362 rmg_rndm.seed (time (0)); 901 rmg_rndm.seed (time (0));
363 902
364 for(int i=1;i<10;i) 903 for(int i=1;i<100;i++)
365 { 904 {
366 maze->gen_cave (3); 905 layout maze (40, 25);
906 maze.fill_rand (85);
907 maze.border ();
908 maze.isolation_remover ();
367 maze->print (); 909 maze.print ();
368 } 910 }
369 911
370 exit (1); 912 exit (1);
371 } 913 }
372} demo; 914} demo;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines