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.6 by root, Fri Jul 2 04:05:15 2010 UTC vs.
Revision 1.24 by root, Mon Jul 5 00:07:21 2010 UTC

23 23
24#include <global.h> 24#include <global.h>
25#include <random_map.h> 25#include <random_map.h>
26#include <rproto.h> 26#include <rproto.h>
27 27
28void noinline
29layout::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 size = (sizeof (cell *) + sizeof (cell) * h) * w;
40 data = (cell **)salloc<char> (size);
41
42 cell *p = (cell *)(data + w);
43
44 for (int x = 0; x < w; ++x)
45 data [x] = p + x * h;
46}
47
28Layout::Layout (int w, int h) 48layout::layout (int w, int h)
29: w(w), h(h)
30{ 49{
31 int size = (sizeof (char *) + sizeof (char) * h) * w; 50 alloc (w, h);
51}
32 52
53layout::layout (layout &copy)
54{
55 alloc (copy.w, copy.h);
56
57 memcpy (data [0], copy.data [0], sizeof (cell) * h * w);
58}
59
60layout::layout (layout &orig, int x1, int y1, int x2, int y2)
61{
62 w = x2 - x1;
63 h = y2 - y1;
64
65 // we only allocate space for the pointers
66 size = sizeof (cell *) * w;
33 col = (char **)salloc<char> (size); 67 data = (cell **)salloc<char> (size);
34 68
35 char *data = (char *)(col + w); 69 // and now we point back into the original layout
36
37 for (int x = w; x--; ) 70 for (int x = 0; x < w; ++x)
38 col [x] = data + x * h; 71 data [x] = orig.data [x + x1] + y1;
39} 72}
40 73
41Layout::~Layout () 74layout::~layout ()
42{ 75{
43 int size = (sizeof (char *) + sizeof (char) * h) * w;
44
45 sfree ((char *)col, size); 76 sfree ((char *)data, size);
77}
78
79void noinline
80layout::fill (char fill)
81{
82 //memset (data [0], fill, w * h); // only when contiguous :/
83 fill_rect (0, 0, w, h, fill);
84}
85
86void noinline
87layout::replace (char from, char to)
88{
89 for (int x = 0; x < w; ++x)
90 for (int y = 0; y < h; ++y)
91 if (data [x][y] == from)
92 data [x][y] = to;
93}
94
95void noinline
96layout::rect (int x1, int y1, int x2, int y2, char fill)
97{
98 --x2;
99
100 memset (data [x1] + y1, fill, y2 - y1);
101 memset (data [x2] + y1, fill, y2 - y1);
102
103 while (++x1 < x2)
104 data [x1][y1] = data [x1][y2 - 1] = fill;
105}
106
107void noinline
108layout::fill_rect (int x1, int y1, int x2, int y2, char fill)
109{
110 for (; x1 < x2; ++x1)
111 memset (data [x1] + y1, fill, y2 - y1);
46} 112}
47 113
48void 114void
49Layout::fill (char fill)
50{
51 memset (col [0], fill, w * h);
52}
53
54void
55Layout::rect (int x1, int y1, int x2, int y2, char fill)
56{
57 for (; x1 < x2; ++x1)
58 memset (col [x1] + y1, fill, y2 - y1);
59}
60
61void Layout::border (char fill) 115layout::border (char fill)
62{ 116{
63 for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; 117 rect (0, 0, w, h, fill);
64 for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill;
65} 118}
66 119
67void 120void noinline
68Layout::fill_rand (int percent) 121layout::fill_rand (int percent)
69{ 122{
70 percent = lerp (percent, 0, 100, 0, 256); 123 percent = lerp (percent, 0, 100, 0, 256);
71 124
72 for (int x = w - 1; --x > 0; ) 125 for (int x = 0; x < w; ++x)
73 for (int y = h - 1; --y > 0; ) 126 for (int y = 0; y < h; ++y)
74 col [x][y] = rmg_rndm (256) > percent ? 0 : '#'; 127 data [x][y] = rmg_rndm (256) > percent ? 0 : '#';
75} 128}
76 129
77///////////////////////////////////////////////////////////////////////////// 130/////////////////////////////////////////////////////////////////////////////
78 131
79// erode by cellular automata 132// erode by cellular automata
80void 133void noinline
81Layout::erode_1_2 (int c1, int c2, int repeat) 134layout::erode_1_2 (int c1, int c2, int repeat)
82{ 135{
83 Layout neu (w, h); 136 layout neu (w, h);
84 137
85 while (repeat--) 138 while (repeat--)
86 { 139 {
87 for (int x = 0; x < w; ++x) 140 for (int x = 0; x < w; ++x)
88 { 141 {
104 for (int i = array_length (dds); i--; ) 157 for (int i = array_length (dds); i--; )
105 { 158 {
106 int nx = x + dds [i][0]; 159 int nx = x + dds [i][0];
107 int ny = y + dds [i][1]; 160 int ny = y + dds [i][1];
108 161
109 if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !col [nx][ny]) 162 if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny])
110 { 163 {
111 n1 += dds [i][2]; 164 n1 += dds [i][2];
112 n2++; 165 n2++;
113 } 166 }
114 } 167 }
122} 175}
123 176
124///////////////////////////////////////////////////////////////////////////// 177/////////////////////////////////////////////////////////////////////////////
125 178
126void 179void
127Layout::print () const 180layout::print () const
128{ 181{
129 for (int y = 0; y < h; y++) 182 for (int y = 0; y < h; y++)
130 { 183 {
131 for (int x = 0; x < w; x++) 184 for (int x = 0; x < w; x++)
132 { 185 {
133 U8 c = (U8)col [x][y]; 186 U8 c = (U8)data [x][y];
134 187
135 if (!c) 188 if (!c)
136 c = ' '; 189 c = ' ';
137 else if (c < 10) 190 else if (c < 10)
138 c += '0'; 191 c += '0';
151///////////////////////////////////////////////////////////////////////////// 204/////////////////////////////////////////////////////////////////////////////
152// isolation remover - ensures single connected area 205// isolation remover - ensures single connected area
153 206
154typedef fixed_stack<point> pointlist; 207typedef fixed_stack<point> pointlist;
155 208
156static noinline void 209static void noinline
157push_flood_fill (Layout &dist, pointlist &seeds, int x, int y) 210push_flood_fill (layout &dist, pointlist &seeds, int x, int y)
158{ 211{
159 if (dist [x][y]) 212 if (dist [x][y])
160 return; 213 return;
161 214
162 while (y > 0 && !dist [x][y - 1]) 215 while (y > 0 && !dist [x][y - 1])
172 ++y; 225 ++y;
173 } 226 }
174 227
175 while (--y >= y0) 228 while (--y >= y0)
176 { 229 {
177 if (x > 0) push_flood_fill (dist, seeds, x - 1, y); 230 if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y);
178 if (x < dist.w - 1) push_flood_fill (dist, seeds, x + 1, y); 231 if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y);
179 } 232 }
180} 233}
181 234
182static inline void 235static void inline
183make_tunnel (Layout &dist, pointlist &seeds, int x, int y, U8 d) 236make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb)
184{ 237{
185 for (;;) 238 for (;;)
186 { 239 {
240 point neigh[4];
241 int ncnt = 0;
242
243 d += perturb > 1;
244
187 if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) 245 if (x > 0 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y);
188 --x;
189 else if (x < dist.w - 2 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) 246 if (x < dist.w - 1 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y);
190 ++x;
191 else if (y > 1 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) 247 if (y > 0 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1);
192 --y;
193 else if (y < dist.h - 2 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) 248 if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
194 ++y; 249
195 else 250 if (!ncnt)
196 break; 251 return;
252
253 point p = neigh [perturb ? rmg_rndm (ncnt) : 0];
254
255 seeds.push (p);
256
257 x = p.x;
258 y = p.y;
197 259
198 d = dist [x][y]; 260 d = dist [x][y];
199 dist [x][y] = 1; 261 dist [x][y] = 1;
200 seeds.push (point (x, y));
201 } 262 }
202} 263}
203 264
204static void inline 265static void inline
205maybe_push (Layout &dist, pointlist &seeds, int x, int y, U8 d) 266maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d)
206{ 267{
207 char &D = dist [x][y]; 268 char &D = dist [x][y];
208 269
209 if (U8 (D) > d) // if wall and higher distance, lower distance 270 if (U8 (D) > d) // if wall and higher distance, lower distance
210 D = d; 271 D = d;
212 return; 273 return;
213 274
214 seeds.push (point (x, y)); 275 seeds.push (point (x, y));
215} 276}
216 277
217void 278// isolation remover, works on a "distance" map
218Layout::isolation_remover (bool dirty) 279// the map must be initialised with 0 == rooms, 255 = walls
280static void noinline
281isolation_remover (layout &dist, unsigned int perturb = 2)
219{ 282{
220 Layout dist (w, h);
221
222 // dist contains 283 // dist contains
223 // 0 == invisited rooms 284 // 0 == invisited rooms
224 // 1 == visited rooms 285 // 1 == visited rooms
225 // 2+ shortest distance to random near room 286 // 2+ shortest distance to random near room
226 287
227 // phase 1, initialise dist array, find seed 288 clamp_it (perturb, 0, 2);
289
290 // phase 1, find seed
228 int cnt = 0; 291 int cnt = 0;
229 int x, y; 292 int x, y;
230 293
231 for (int i = 0; i < w; ++i) 294 for (int i = 0; i < dist.w; ++i)
232 for (int j = 0; j < h; ++j) 295 for (int j = 0; j < dist.h; ++j)
233 { 296 if (!dist [i][j] && !rmg_rndm (++cnt))
234 if (col [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; 297 x = i, y = j;
241 }
242 }
243 298
244 if (!cnt) 299 if (!cnt)
245 { 300 {
246 // map is completely massive, this is not good, 301 // map is completely massive, this is not good,
247 // so make it empty instead. 302 // so make it empty instead.
248 clear (); 303 dist.fill (1);
249 border ();
250 return; 304 return;
251 } 305 }
252 306
253 fixed_stack<point> seeds (w * h * 5); 307 fixed_stack<point> seeds (dist.w * dist.h * 5);
254 308
255 // found first free space - picking the first one gives 309 // found first free space - picking the first one gives
256 // us a slight bias for tunnels, but usually you won't 310 // us a slight bias for tunnels, but usually you won't
257 // notice that in-game 311 // notice that in-game
258 seeds.push (point (x, y)); 312 seeds.push (point (x, y));
270 y = p.y; 324 y = p.y;
271 325
272 if (!dist [x][y]) 326 if (!dist [x][y])
273 { 327 {
274 // found new isolated area, make tunnel 328 // found new isolated area, make tunnel
275 if (!dirty)
276 push_flood_fill (dist, seeds, x, y); 329 push_flood_fill (dist, seeds, x, y);
277
278 make_tunnel (dist, seeds, x, y, 255); 330 make_tunnel (dist, seeds, x, y, 254, perturb);
279
280 if (dirty)
281 push_flood_fill (dist, seeds, x, y);
282 } 331 }
283 else 332 else
284 { 333 {
285 // nothing here, continue to expand 334 // nothing here, continue to expand
286 U8 d = U8 (dist [x][y]) + 1; 335 U8 d = U8 (dist [x][y]) + 1;
289 if (x > 0) maybe_push (dist, seeds, x - 1, y, d); 338 if (x > 0) maybe_push (dist, seeds, x - 1, y, d);
290 if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); 339 if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d);
291 if (y > 0) maybe_push (dist, seeds, x, y - 1, d); 340 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
292 } 341 }
293 } 342 }
343}
344
345void
346layout::isolation_remover (int perturb)
347{
348 layout dist (w - 2, h - 2); // map without border
349
350 for (int x = 1; x < w - 1; ++x)
351 for (int y = 1; y < h - 1; ++y)
352 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
353
354 ::isolation_remover (dist, perturb);
294 355
295 // now copy the tunnels over 356 // now copy the tunnels over
296 for (int x = 0; x < w; ++x) 357 for (int x = 1; x < w - 1; ++x)
297 for (int y = 0; y < h; ++y) 358 for (int y = 1; y < h - 1; ++y)
298 if (col [x][y] == '#' && dist [x][y] == 1) 359 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
299 col [x][y] = 0; 360 data [x][y] = 0;
300} 361}
301 362
302///////////////////////////////////////////////////////////////////////////// 363/////////////////////////////////////////////////////////////////////////////
303 364
304// inspired mostly by http://www.jimrandomh.org/misc/caves.txt 365//+GPL
366
367/* puts doors at appropriate locations in a maze. */
305void 368void
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 () 369layout::doorify ()
349{ 370{
350 int ndoors = w * h / 60; /* reasonable number of doors. */ 371 int ndoors = w * h / 60; /* reasonable number of doors. */
351 int doorlocs = 0; /* # of available doorlocations */
352 372
353 uint16 *doorlist_x = salloc<uint16> (w * h); 373 coroapi::cede_to_tick ();
354 uint16 *doorlist_y = salloc<uint16> (w * h); 374
375 fixed_stack<point> doorloc (w * h);
355 376
356 /* make a list of possible door locations */ 377 /* make a list of possible door locations */
357 for (int i = 1; i < w - 1; i++) 378 for (int i = 1; i < w - 1; i++)
358 for (int j = 1; j < h - 1; j++) 379 for (int j = 1; j < h - 1; j++)
359 { 380 {
360 int sindex = surround_flag (*this, i, j); 381 int sindex = surround_flag (*this, i, j);
361 382
362 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 383 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
363 { 384 doorloc.push (point (i, j));
364 doorlist_x [doorlocs] = i;
365 doorlist_y [doorlocs] = j;
366 doorlocs++;
367 }
368 } 385 }
369 386
370 while (ndoors > 0 && doorlocs > 0) 387 while (ndoors && doorloc.size)
371 { 388 {
372 int di = rmg_rndm (doorlocs); 389 point p = doorloc.remove (rmg_rndm (doorloc.size));
373 int i = doorlist_x [di]; 390
374 int j = doorlist_y [di];
375 int sindex = surround_flag (*this, i, j); 391 int sindex = surround_flag (*this, p.x, p.y);
376 392
377 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 393 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
378 { 394 {
379 col [i][j] = 'D'; 395 data [p.x][p.y] = 'D';
380 ndoors--; 396 --ndoors;
381 } 397 }
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 } 398 }
388
389 sfree (doorlist_x, w * h);
390 sfree (doorlist_y, w * h);
391} 399}
392 400
393/* takes a map and makes it symmetric: adjusts Xsize and 401/* takes a map and makes it symmetric: adjusts Xsize and
394 * Ysize to produce a symmetric map. 402 * Ysize to produce a symmetric map.
395 */ 403 */
396void 404void
397Layout::symmetrize (int symmetry) 405layout::symmetrize (int symmetry)
398{ 406{
399 if (symmetry == SYMMETRY_NONE) 407 if (symmetry == SYMMETRY_NONE)
400 return; 408 return;
401 409
402 Layout sym_layout ( 410 layout sym_layout (
403 symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w, 411 symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w,
404 symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h 412 symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h
405 ); 413 );
406 414
407 if (symmetry == SYMMETRY_X) 415 if (symmetry == SYMMETRY_X)
408 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 416 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
409 for (int j = 0; j < sym_layout.h; j++) 417 for (int j = 0; j < sym_layout.h; j++)
410 { 418 {
411 sym_layout[i ][j] = 419 sym_layout[i ][j] =
412 sym_layout[sym_layout.w - i - 1][j] = col[i][j]; 420 sym_layout[sym_layout.w - i - 1][j] = data [i][j];
413 } 421 }
414 422
415 if (symmetry == SYMMETRY_Y) 423 if (symmetry == SYMMETRY_Y)
416 for (int i = 0; i < sym_layout.w; i++) 424 for (int i = 0; i < sym_layout.w; i++)
417 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 425 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
418 { 426 {
419 sym_layout[i][j ] = 427 sym_layout[i][j ] =
420 sym_layout[i][sym_layout.h - j - 1] = col[i][j]; 428 sym_layout[i][sym_layout.h - j - 1] = data [i][j];
421 } 429 }
422 430
423 if (symmetry == SYMMETRY_XY) 431 if (symmetry == SYMMETRY_XY)
424 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 432 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
425 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 433 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
426 { 434 {
427 sym_layout[i ][j ] = 435 sym_layout[i ][j ] =
428 sym_layout[i ][sym_layout.h - j - 1] = 436 sym_layout[i ][sym_layout.h - j - 1] =
429 sym_layout[sym_layout.w - i - 1][j ] = 437 sym_layout[sym_layout.w - i - 1][j ] =
430 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = col[i][j]; 438 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j];
431 } 439 }
432 440
433 /* need to run the isolation remover for some layouts */ 441 /* need to run the isolation remover for some layouts */
434#if 0 442#if 0
435 switch (RP->map_layout_style) 443 switch (RP->map_layout_style)
451} 459}
452 460
453//-GPL 461//-GPL
454 462
455void 463void
456Layout::rotate (int rotation) 464layout::rotate (int rotation)
457{ 465{
466 coroapi::cede_to_tick ();
467
458 switch (rotation & 3) 468 switch (rotation & 3)
459 { 469 {
460 case 2: /* a reflection */ 470 case 2: /* a reflection */
461 { 471 {
462 Layout new_layout (w, h); 472 layout new_layout (w, h);
463 473
464 for (int i = 0; i < w; i++) /* copy a reflection back */ 474 for (int i = 0; i < w; i++) /* copy a reflection back */
465 for (int j = 0; j < h; j++) 475 for (int j = 0; j < h; j++)
466 new_layout [i][j] = col [w - i - 1][h - j - 1]; 476 new_layout [i][j] = data [w - i - 1][h - j - 1];
467 477
468 swap (new_layout); 478 swap (new_layout);
469 } 479 }
470 break; 480 break;
471 481
472 case 1: 482 case 1:
473 case 3: 483 case 3:
474 { 484 {
475 Layout new_layout (h, w); 485 layout new_layout (h, w);
476 486
477 if (rotation == 1) /* swap x and y */ 487 if (rotation == 1) /* swap x and y */
478 for (int i = 0; i < w; i++) 488 for (int i = 0; i < w; i++)
479 for (int j = 0; j < h; j++) 489 for (int j = 0; j < h; j++)
480 new_layout [j][i] = col [i][j]; 490 new_layout [j][i] = data [i][j];
481 491
482 if (rotation == 3) /* swap x and y */ 492 if (rotation == 3) /* swap x and y */
483 for (int i = 0; i < w; i++) 493 for (int i = 0; i < w; i++)
484 for (int j = 0; j < h; j++) 494 for (int j = 0; j < h; j++)
485 new_layout [j][i] = col [w - i - 1][h - j - 1]; 495 new_layout [j][i] = data [w - i - 1][h - j - 1];
486 496
487 swap (new_layout); 497 swap (new_layout);
488 } 498 }
489 break; 499 break;
490 } 500 }
493///////////////////////////////////////////////////////////////////////////// 503/////////////////////////////////////////////////////////////////////////////
494 504
495//+GPL 505//+GPL
496 506
497/* 507/*
498 * Expands a layout by 2x in each dimension. 508 * Expands a maze by 2x in each dimension.
499 * H. S. Teoh 509 * H. S. Teoh
500 */ 510 */
501 511
502/* Copy the old tile X into the new one at location (i*2, j*2) and 512/* 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: 513 * fill up the rest of the 2x2 result with \0:
504 * X ---> X \0 514 * X ---> X \0
505 * \0 \0 515 * \0 \0
506 */ 516 */
507static void inline 517static void inline
508expand_misc (Layout &newlayout, int i, int j, Layout &layout) 518expand_misc (layout &newlayout, int i, int j, layout &maze)
509{ 519{
510 newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = layout[i][j]; 520 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 521 /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that
512 * for us.) */ 522 * for us.) */
513} 523}
514 524
515/* Returns a bitmap that represents which squares on the right and bottom 525/* Returns a bitmap that represents which squares on the right and bottom
518 * 2 match on (i, j+1) 528 * 2 match on (i, j+1)
519 * 4 match on (i+1, j+1) 529 * 4 match on (i+1, j+1)
520 * and the possible combinations thereof. 530 * and the possible combinations thereof.
521 */ 531 */
522static int noinline 532static int noinline
523calc_pattern (char ch, Layout &layout, int i, int j) 533calc_pattern (char ch, layout &maze, int i, int j)
524{ 534{
525 int pattern = 0; 535 int pattern = 0;
526 536
527 if (i + 1 < layout.w && layout[i + 1][j] == ch) 537 if (i + 1 < maze.w && maze[i + 1][j] == ch)
528 pattern |= 1; 538 pattern |= 1;
529 539
530 if (j + 1 < layout.h) 540 if (j + 1 < maze.h)
531 { 541 {
532 if (layout[i][j + 1] == ch) 542 if (maze[i][j + 1] == ch)
533 pattern |= 2; 543 pattern |= 2;
534 544
535 if (i + 1 < layout.w && layout[i + 1][j + 1] == ch) 545 if (i + 1 < maze.w && maze[i + 1][j + 1] == ch)
536 pattern |= 4; 546 pattern |= 4;
537 } 547 }
538 548
539 return pattern; 549 return pattern;
540} 550}
542/* Expand a wall. This function will try to sensibly connect the resulting 552/* 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 553 * wall to adjacent wall squares, so that the result won't have disconnected
544 * walls. 554 * walls.
545 */ 555 */
546static void inline 556static void inline
547expand_wall (Layout &newlayout, int i, int j, Layout &layout) 557expand_wall (layout &newlayout, int i, int j, layout &maze)
548{ 558{
549 int wall_pattern = calc_pattern ('#', layout, i, j); 559 int wall_pattern = calc_pattern ('#', maze, i, j);
550 int door_pattern = calc_pattern ('D', layout, i, j); 560 int door_pattern = calc_pattern ('D', maze, i, j);
551 int both_pattern = wall_pattern | door_pattern; 561 int both_pattern = wall_pattern | door_pattern;
552 562
553 newlayout[i * 2][j * 2] = '#'; 563 newlayout[i * 2][j * 2] = '#';
554 564
555 if (i + 1 < layout.w) 565 if (i + 1 < maze.w)
556 { 566 {
557 if (both_pattern & 1) 567 if (both_pattern & 1)
558 { /* join walls/doors to the right */ 568 { /* join walls/doors to the right */
559/* newlayout[i*2+1][j*2] = '#'; */ 569/* newlayout[i*2+1][j*2] = '#'; */
560 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; 570 newlayout[i * 2 + 1][j * 2] = maze[i + 1][j];
561 } 571 }
562 } 572 }
563 573
564 if (j + 1 < layout.h) 574 if (j + 1 < maze.h)
565 { 575 {
566 if (both_pattern & 2) 576 if (both_pattern & 2)
567 { /* join walls/doors to the bottom */ 577 { /* join walls/doors to the bottom */
568/* newlayout[i*2][j*2+1] = '#'; */ 578/* newlayout[i*2][j*2+1] = '#'; */
569 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; 579 newlayout[i * 2][j * 2 + 1] = maze[i][j + 1];
570 } 580 }
571 581
572 if (wall_pattern == 7) 582 if (wall_pattern == 7)
573 { /* if orig layout is a 2x2 wall block, 583 { /* if orig maze is a 2x2 wall block,
574 * we fill the result with walls. */ 584 * we fill the result with walls. */
575 newlayout[i * 2 + 1][j * 2 + 1] = '#'; 585 newlayout[i * 2 + 1][j * 2 + 1] = '#';
576 } 586 }
577 } 587 }
578} 588}
580/* This function will try to sensibly connect doors so that they meet up with 590/* 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 591 * adjacent walls. Note that it will also presumptuously delete (ignore) doors
582 * that it doesn't know how to correctly expand. 592 * that it doesn't know how to correctly expand.
583 */ 593 */
584static void inline 594static void inline
585expand_door (Layout &newlayout, int i, int j, Layout &layout) 595expand_door (layout &newlayout, int i, int j, layout &maze)
586{ 596{
587 int wall_pattern = calc_pattern ('#', layout, i, j); 597 int wall_pattern = calc_pattern ('#', maze, i, j);
588 int door_pattern = calc_pattern ('D', layout, i, j); 598 int door_pattern = calc_pattern ('D', maze, i, j);
589 int join_pattern; 599 int join_pattern;
590 600
591 /* Doors "like" to connect to walls more than other doors. If there is 601 /* 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 602 * a wall and another door, this door will connect to the wall and
593 * disconnect from the other door. */ 603 * disconnect from the other door. */
596 else 606 else
597 join_pattern = door_pattern; 607 join_pattern = door_pattern;
598 608
599 newlayout[i * 2][j * 2] = 'D'; 609 newlayout[i * 2][j * 2] = 'D';
600 610
601 if (i + 1 < layout.w) 611 if (i + 1 < maze.w)
602 if (join_pattern & 1) 612 if (join_pattern & 1)
603 /* there is a door/wall to the right */ 613 /* there is a door/wall to the right */
604 newlayout[i * 2 + 1][j * 2] = 'D'; 614 newlayout[i * 2 + 1][j * 2] = 'D';
605 615
606 if (j + 1 < layout.h) 616 if (j + 1 < maze.h)
607 if (join_pattern & 2) 617 if (join_pattern & 2)
608 /* there is a door/wall below */ 618 /* there is a door/wall below */
609 newlayout[i * 2][j * 2 + 1] = 'D'; 619 newlayout[i * 2][j * 2 + 1] = 'D';
610} 620}
611 621
612void 622void
613Layout::expand2x () 623layout::expand2x ()
614{ 624{
615 Layout new_layout (w * 2 - 1, h * 2 - 1); 625 layout new_layout (w * 2 - 1, h * 2 - 1);
616 626
617 new_layout.clear (); 627 new_layout.clear ();
628
629 coroapi::cede_to_tick ();
618 630
619 for (int i = 0; i < w; i++) 631 for (int i = 0; i < w; i++)
620 for (int j = 0; j < h; j++) 632 for (int j = 0; j < h; j++)
621 switch (col [i][j]) 633 switch (data [i][j])
622 { 634 {
623 case '#': expand_wall (new_layout, i, j, *this); break; 635 case '#': expand_wall (new_layout, i, j, *this); break;
624 case 'D': expand_door (new_layout, i, j, *this); break; 636 case 'D': expand_door (new_layout, i, j, *this); break;
625 default: expand_misc (new_layout, i, j, *this); break; 637 default: expand_misc (new_layout, i, j, *this); break;
626 } 638 }
628 swap (new_layout); 640 swap (new_layout);
629} 641}
630 642
631///////////////////////////////////////////////////////////////////////////// 643/////////////////////////////////////////////////////////////////////////////
632 644
633/* checks the layout to see if I can stick a horizontal(dir = 0) wall 645/* checks the maze to see if I can stick a horizontal(dir = 0) wall
634 (or vertical, dir == 1) 646 (or vertical, dir == 1)
635 here which ends up on other walls sensibly. */ 647 here which ends up on other walls sensibly. */
636static int 648static int
637can_make_wall (const Layout &maze, int dx, int dy, int dir) 649can_make_wall (const layout &maze, int dx, int dy, int dir)
638{ 650{
639 int i1; 651 int i1;
640 int length = 0; 652 int length = 0;
641 653
642 /* dont make walls if we're on the edge. */ 654 /* dont make walls if we're on the edge. */
705 717
706 return -1; 718 return -1;
707} 719}
708 720
709int 721int
710make_wall (char **maze, int x, int y, int dir) 722make_wall (layout &maze, int x, int y, int dir)
711{ 723{
712 maze[x][y] = 'D'; /* mark a door */ 724 maze[x][y] = 'D'; /* mark a door */
713 725
714 switch (dir) 726 switch (dir)
715 { 727 {
729 741
730 return 0; 742 return 0;
731} 743}
732 744
733void 745void
734Layout::roomify () 746layout::roomify ()
735{ 747{
736 int tries = w * h / 30; 748 int tries = w * h / 30;
749
750 coroapi::cede_to_tick ();
737 751
738 for (int ti = 0; ti < tries; ti++) 752 for (int ti = 0; ti < tries; ti++)
739 { 753 {
740 /* starting location for looking at creating a door */ 754 /* starting location for looking at creating a door */
741 int dx = rmg_rndm (w); 755 int dx = rmg_rndm (w);
764 else 778 else
765 make_wall (*this, dx, dy, 1); 779 make_wall (*this, dx, dy, 1);
766 } 780 }
767} 781}
768 782
783//-GPL
784
769///////////////////////////////////////////////////////////////////////////// 785/////////////////////////////////////////////////////////////////////////////
770 786
787// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
788void
789layout::gen_cave (int subtype)
790{
791 switch (subtype)
792 {
793 // a rough cave
794 case 0:
795 fill_rand (rmg_rndm (85, 97));
796 break;
797
798 // corridors
799 case 1:
800 fill_rand (rmg_rndm (5, 40));
801 erode_1_2 (5, 2, 10);
802 erode_1_2 (5, -1, 10);
803 erode_1_2 (5, 2, 1);
804 break;
805
806 // somewhat open, some room-like structures
807 case 2:
808 fill_rand (45);
809 erode_1_2 (5, 2, 4);
810 erode_1_2 (5, -1, 3);
811 break;
812
813 // wide open, roundish
814 case 3:
815 fill_rand (45);
816 erode_1_2 (5, 0, 5);
817 erode_1_2 (5, 1, 1);
818 break;
819 }
820
821 border ();
822 isolation_remover (1);
823}
824
825void
826layout::gen_castle ()
827{
828 fill ('#');
829
830 for (int n = w * h / 30 + 1; n--; )
831 {
832 int rw = rmg_rndm (6, 10);
833 int rh = rmg_rndm (6, 10);
834
835 if (rw > w || rh > h)
836 continue;
837
838 int rx = rmg_rndm (0, w - rw);
839 int ry = rmg_rndm (0, h - rh);
840
841 rect (rx, ry, rx + rw, ry + rh, '#');
842 fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0);
843 }
844
845 border ();
846 isolation_remover (0);
847}
848
849static void
850gen_mixed_ (layout &maze, random_map_params *RP)
851{
852 if (maze.w > maze.h && maze.w > 16)
853 {
854 int m = rmg_rndm (8, maze.w - 8);
855
856 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP);
857 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP);
858 }
859 else if (maze.h > 16)
860 {
861 int m = rmg_rndm (8, maze.h - 8);
862
863 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP);
864 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP);
865 }
866 else
867 {
868 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
869
870 if (RP->map_layout_style == LAYOUT_MULTIPLE)
871 ++RP->map_layout_style;
872
873 maze.generate (RP);
874 }
875
876 coroapi::cede_to_tick ();
877}
878
879// recursive subdivision with random sublayouts
880static void
881gen_mixed (layout &maze, random_map_params *RP)
882{
883 random_map_params &rp = *new random_map_params (RP);
884 gen_mixed_ (maze, &rp);
885 delete &rp;
886
887 maze.border ();
888
889 // exits currently do not work so well, as they
890 // are currently often found together, so nuke entrances
891 maze.replace ('<', ' ');
892
893 maze.isolation_remover (0);
894}
895
896//+GPL
897
771/* function selects the layout function and gives it whatever 898/* function selects the maze function and gives it whatever
772 arguments it needs. */ 899 arguments it needs. */
773void 900void
774Layout::generate (random_map_params *RP) 901layout::generate (random_map_params *RP)
775{ 902{
776 switch (RP->map_layout_style) 903 switch (RP->map_layout_style)
777 { 904 {
778 case LAYOUT_ONION: 905 case LAYOUT_ONION:
779 map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2); 906 map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2);
834 if (rmg_rndm (2)) 961 if (rmg_rndm (2))
835 doorify (); 962 doorify ();
836 963
837 break; 964 break;
838 965
966 case LAYOUT_CASTLE:
967 gen_castle ();
968
969 if (rmg_rndm (2))
970 doorify ();
971
972 break;
973
974 case LAYOUT_MULTIPLE:
975 gen_mixed (*this, RP);
976 break;
977
839 default: 978 default:
840 abort (); 979 abort ();
841 } 980 }
981}
842 982
843 /* rotate the layout randomly */ 983//-GPL
844 rotate (rmg_rndm (4));
845
846 symmetrize (RP->symmetry_used);
847 984
848#if 0 985#if 0
849 print ();//D 986static void
850#endif 987gen_village (layout &maze)
988{
989 maze.clear ();
990 maze.border ();
851 991
852 if (RP->expand2x) 992 for (int n = maze.w * maze.h / 200 + 1; n--; )
853 expand2x (); 993 {
854} 994 int rw = rmg_rndm (6, 10);
995 int rh = rmg_rndm (6, 10);
855 996
856//-GPL 997 int rx = rmg_rndm (2, maze.w - rw - 2);
998 int ry = rmg_rndm (2, maze.h - rh - 2);
857 999
858#if 0 1000 maze.rect (rx, ry, rx + rw, ry + rh, '#');
1001 }
1002
1003 maze.border ();
1004 maze.isolation_remover (2);
1005}
1006
859static struct demo 1007static struct demo
860{ 1008{
861 demo () 1009 demo ()
862 { 1010 {
863 rmg_rndm.seed (time (0)); 1011 rmg_rndm.seed (time (0));
864 1012
865 for(int i=1;i<10;i) 1013 for(int i=1;i<100;i++)
866 { 1014 {
867 Layout maze (10, 10); 1015 layout maze (40, 30);
868 maze.fill_rand (90); 1016 gen_village (maze);
869 maze.border ();
870 maze.isolation_remover ();
871 maze.doorify (); 1017 maze.doorify ();
872 maze.symmetrize (rmg_rndm (2, 4));
873 maze.rotate (rmg_rndm (4));
874 maze.expand2x ();
875 maze.print (); 1018 maze.print ();
1019 exit(0);
876 } 1020 }
877 1021
878 exit (1); 1022 exit (1);
879 } 1023 }
880} demo; 1024} demo;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines