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.28 by root, Sat Apr 23 04:56:52 2011 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,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) Crossfire Development Team (restored, original file without copyright notice) 5 * Copyright (©) 1994-2004 Crossfire Development Team (restored, original file without copyright notice)
6 * 6 *
7 * 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
8 * 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
9 * 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
10 * option) any later version. 10 * option) any later version.
20 * 20 *
21 * 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>
22 */ 22 */
23 23
24#include <global.h> 24#include <global.h>
25#include <random_map.h> 25#include <rmg.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));
259 313
260 // phase 2, while we have seeds, if 314 // phase 2, while we have seeds, if
261 // seed is empty, floodfill, else grow 315 // seed is empty, floodfill, else grow
262 316
317 int rem_index = 0; // used to remove "somewhat ordered"
318
263 while (seeds.size) 319 while (seeds.size)
264 { 320 {
265 coroapi::cede_to_tick (); 321 coroapi::cede_to_tick ();
266 322
323 int i = perturb
324 ? rmg_rndm (max (0, seeds.size - 8), seeds.size - 1)
325 : rem_index ++ % seeds.size;
326
267 point p = seeds.remove (rmg_rndm (seeds.size)); 327 point p = seeds.remove (i);
268 328
269 x = p.x; 329 x = p.x;
270 y = p.y; 330 y = p.y;
271 331
272 if (!dist [x][y]) 332 if (!dist [x][y])
273 { 333 {
274 // found new isolated area, make tunnel 334 // found new isolated area, make tunnel
275 if (!dirty)
276 push_flood_fill (dist, seeds, x, y); 335 push_flood_fill (dist, seeds, x, y);
277
278 make_tunnel (dist, seeds, x, y, 255); 336 make_tunnel (dist, seeds, x, y, 254, perturb);
279
280 if (dirty)
281 push_flood_fill (dist, seeds, x, y);
282 } 337 }
283 else 338 else
284 { 339 {
285 // nothing here, continue to expand 340 // nothing here, continue to expand
286 U8 d = U8 (dist [x][y]) + 1; 341 U8 d = U8 (dist [x][y]) + 1;
289 if (x > 0) maybe_push (dist, seeds, x - 1, y, d); 344 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); 345 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); 346 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
292 } 347 }
293 } 348 }
349}
350
351void
352layout::isolation_remover (int perturb)
353{
354 layout dist (w - 2, h - 2); // map without border
355
356 for (int x = 1; x < w - 1; ++x)
357 for (int y = 1; y < h - 1; ++y)
358 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
359
360 ::isolation_remover (dist, perturb);
294 361
295 // now copy the tunnels over 362 // now copy the tunnels over
296 for (int x = 0; x < w; ++x) 363 for (int x = 1; x < w - 1; ++x)
297 for (int y = 0; y < h; ++y) 364 for (int y = 1; y < h - 1; ++y)
298 if (col [x][y] == '#' && dist [x][y] == 1) 365 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
299 col [x][y] = 0; 366 data [x][y] = 0;
300} 367}
301 368
302///////////////////////////////////////////////////////////////////////////// 369/////////////////////////////////////////////////////////////////////////////
303 370
304// inspired mostly by http://www.jimrandomh.org/misc/caves.txt 371//+GPL
372
373/* puts doors at appropriate locations in a maze. */
305void 374void
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 () 375layout::doorify ()
349{ 376{
350 int ndoors = w * h / 60; /* reasonable number of doors. */ 377 int ndoors = w * h / 60; /* reasonable number of doors. */
351 int doorlocs = 0; /* # of available doorlocations */
352 378
353 uint16 *doorlist_x = salloc<uint16> (w * h); 379 coroapi::cede_to_tick ();
354 uint16 *doorlist_y = salloc<uint16> (w * h); 380
381 fixed_stack<point> doorloc (w * h);
355 382
356 /* make a list of possible door locations */ 383 /* make a list of possible door locations */
357 for (int i = 1; i < w - 1; i++) 384 for (int i = 1; i < w - 1; i++)
358 for (int j = 1; j < h - 1; j++) 385 for (int j = 1; j < h - 1; j++)
359 { 386 {
360 int sindex = surround_flag (*this, i, j); 387 int sindex = surround_flag (*this, i, j);
361 388
362 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 389 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
363 { 390 doorloc.push (point (i, j));
364 doorlist_x [doorlocs] = i;
365 doorlist_y [doorlocs] = j;
366 doorlocs++;
367 }
368 } 391 }
369 392
370 while (ndoors > 0 && doorlocs > 0) 393 while (ndoors && doorloc.size)
371 { 394 {
372 int di = rmg_rndm (doorlocs); 395 point p = doorloc.remove (rmg_rndm (doorloc.size));
373 int i = doorlist_x [di]; 396
374 int j = doorlist_y [di];
375 int sindex = surround_flag (*this, i, j); 397 int sindex = surround_flag (*this, p.x, p.y);
376 398
377 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 399 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
378 { 400 {
379 col [i][j] = 'D'; 401 data [p.x][p.y] = 'D';
380 ndoors--; 402 --ndoors;
381 } 403 }
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 } 404 }
388
389 sfree (doorlist_x, w * h);
390 sfree (doorlist_y, w * h);
391} 405}
392 406
393/* takes a map and makes it symmetric: adjusts Xsize and 407/* takes a map and makes it symmetric: adjusts Xsize and
394 * Ysize to produce a symmetric map. 408 * Ysize to produce a symmetric map.
395 */ 409 */
396void 410void
397Layout::symmetrize (int symmetry) 411layout::symmetrize (int symmetry)
398{ 412{
399 if (symmetry == SYMMETRY_NONE) 413 if (symmetry == SYMMETRY_NONE)
400 return; 414 return;
401 415
402 Layout sym_layout ( 416 layout sym_layout (
403 symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w, 417 symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w,
404 symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h 418 symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h
405 ); 419 );
406 420
407 if (symmetry == SYMMETRY_X) 421 if (symmetry == SYMMETRY_X)
408 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 422 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
409 for (int j = 0; j < sym_layout.h; j++) 423 for (int j = 0; j < sym_layout.h; j++)
410 { 424 {
411 sym_layout[i ][j] = 425 sym_layout[i ][j] =
412 sym_layout[sym_layout.w - i - 1][j] = col[i][j]; 426 sym_layout[sym_layout.w - i - 1][j] = data [i][j];
413 } 427 }
414 428
415 if (symmetry == SYMMETRY_Y) 429 if (symmetry == SYMMETRY_Y)
416 for (int i = 0; i < sym_layout.w; i++) 430 for (int i = 0; i < sym_layout.w; i++)
417 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 431 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
418 { 432 {
419 sym_layout[i][j ] = 433 sym_layout[i][j ] =
420 sym_layout[i][sym_layout.h - j - 1] = col[i][j]; 434 sym_layout[i][sym_layout.h - j - 1] = data [i][j];
421 } 435 }
422 436
423 if (symmetry == SYMMETRY_XY) 437 if (symmetry == SYMMETRY_XY)
424 for (int i = 0; i < sym_layout.w / 2 + 1; i++) 438 for (int i = 0; i < sym_layout.w / 2 + 1; i++)
425 for (int j = 0; j < sym_layout.h / 2 + 1; j++) 439 for (int j = 0; j < sym_layout.h / 2 + 1; j++)
426 { 440 {
427 sym_layout[i ][j ] = 441 sym_layout[i ][j ] =
428 sym_layout[i ][sym_layout.h - j - 1] = 442 sym_layout[i ][sym_layout.h - j - 1] =
429 sym_layout[sym_layout.w - i - 1][j ] = 443 sym_layout[sym_layout.w - i - 1][j ] =
430 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = col[i][j]; 444 sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j];
431 } 445 }
432 446
433 /* need to run the isolation remover for some layouts */ 447 /* need to run the isolation remover for some layouts */
434#if 0 448#if 0
435 switch (RP->map_layout_style) 449 switch (RP->map_layout_style)
451} 465}
452 466
453//-GPL 467//-GPL
454 468
455void 469void
456Layout::rotate (int rotation) 470layout::rotate (int rotation)
457{ 471{
472 coroapi::cede_to_tick ();
473
458 switch (rotation & 3) 474 switch (rotation & 3)
459 { 475 {
460 case 2: /* a reflection */ 476 case 2: /* a reflection */
461 { 477 {
462 Layout new_layout (w, h); 478 layout new_layout (w, h);
463 479
464 for (int i = 0; i < w; i++) /* copy a reflection back */ 480 for (int i = 0; i < w; i++) /* copy a reflection back */
465 for (int j = 0; j < h; j++) 481 for (int j = 0; j < h; j++)
466 new_layout [i][j] = col [w - i - 1][h - j - 1]; 482 new_layout [i][j] = data [w - i - 1][h - j - 1];
467 483
468 swap (new_layout); 484 swap (new_layout);
469 } 485 }
470 break; 486 break;
471 487
472 case 1: 488 case 1:
473 case 3: 489 case 3:
474 { 490 {
475 Layout new_layout (h, w); 491 layout new_layout (h, w);
476 492
477 if (rotation == 1) /* swap x and y */ 493 if (rotation == 1) /* swap x and y */
478 for (int i = 0; i < w; i++) 494 for (int i = 0; i < w; i++)
479 for (int j = 0; j < h; j++) 495 for (int j = 0; j < h; j++)
480 new_layout [j][i] = col [i][j]; 496 new_layout [j][i] = data [i][j];
481 497
482 if (rotation == 3) /* swap x and y */ 498 if (rotation == 3) /* swap x and y */
483 for (int i = 0; i < w; i++) 499 for (int i = 0; i < w; i++)
484 for (int j = 0; j < h; j++) 500 for (int j = 0; j < h; j++)
485 new_layout [j][i] = col [w - i - 1][h - j - 1]; 501 new_layout [j][i] = data [w - i - 1][h - j - 1];
486 502
487 swap (new_layout); 503 swap (new_layout);
488 } 504 }
489 break; 505 break;
490 } 506 }
493///////////////////////////////////////////////////////////////////////////// 509/////////////////////////////////////////////////////////////////////////////
494 510
495//+GPL 511//+GPL
496 512
497/* 513/*
498 * Expands a layout by 2x in each dimension. 514 * Expands a maze by 2x in each dimension.
499 * H. S. Teoh 515 * H. S. Teoh
500 */ 516 */
501 517
502/* Copy the old tile X into the new one at location (i*2, j*2) and 518/* 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: 519 * fill up the rest of the 2x2 result with \0:
504 * X ---> X \0 520 * X ---> X \0
505 * \0 \0 521 * \0 \0
506 */ 522 */
507static void inline 523static void inline
508expand_misc (Layout &newlayout, int i, int j, Layout &layout) 524expand_misc (layout &newlayout, int i, int j, layout &maze)
509{ 525{
510 newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = layout[i][j]; 526 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 527 /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that
512 * for us.) */ 528 * for us.) */
513} 529}
514 530
515/* Returns a bitmap that represents which squares on the right and bottom 531/* Returns a bitmap that represents which squares on the right and bottom
518 * 2 match on (i, j+1) 534 * 2 match on (i, j+1)
519 * 4 match on (i+1, j+1) 535 * 4 match on (i+1, j+1)
520 * and the possible combinations thereof. 536 * and the possible combinations thereof.
521 */ 537 */
522static int noinline 538static int noinline
523calc_pattern (char ch, Layout &layout, int i, int j) 539calc_pattern (char ch, layout &maze, int i, int j)
524{ 540{
525 int pattern = 0; 541 int pattern = 0;
526 542
527 if (i + 1 < layout.w && layout[i + 1][j] == ch) 543 if (i + 1 < maze.w && maze[i + 1][j] == ch)
528 pattern |= 1; 544 pattern |= 1;
529 545
530 if (j + 1 < layout.h) 546 if (j + 1 < maze.h)
531 { 547 {
532 if (layout[i][j + 1] == ch) 548 if (maze[i][j + 1] == ch)
533 pattern |= 2; 549 pattern |= 2;
534 550
535 if (i + 1 < layout.w && layout[i + 1][j + 1] == ch) 551 if (i + 1 < maze.w && maze[i + 1][j + 1] == ch)
536 pattern |= 4; 552 pattern |= 4;
537 } 553 }
538 554
539 return pattern; 555 return pattern;
540} 556}
542/* Expand a wall. This function will try to sensibly connect the resulting 558/* 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 559 * wall to adjacent wall squares, so that the result won't have disconnected
544 * walls. 560 * walls.
545 */ 561 */
546static void inline 562static void inline
547expand_wall (Layout &newlayout, int i, int j, Layout &layout) 563expand_wall (layout &newlayout, int i, int j, layout &maze)
548{ 564{
549 int wall_pattern = calc_pattern ('#', layout, i, j); 565 int wall_pattern = calc_pattern ('#', maze, i, j);
550 int door_pattern = calc_pattern ('D', layout, i, j); 566 int door_pattern = calc_pattern ('D', maze, i, j);
551 int both_pattern = wall_pattern | door_pattern; 567 int both_pattern = wall_pattern | door_pattern;
552 568
553 newlayout[i * 2][j * 2] = '#'; 569 newlayout[i * 2][j * 2] = '#';
554 570
555 if (i + 1 < layout.w) 571 if (i + 1 < maze.w)
556 { 572 {
557 if (both_pattern & 1) 573 if (both_pattern & 1)
558 { /* join walls/doors to the right */ 574 { /* join walls/doors to the right */
559/* newlayout[i*2+1][j*2] = '#'; */ 575/* newlayout[i*2+1][j*2] = '#'; */
560 newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; 576 newlayout[i * 2 + 1][j * 2] = maze[i + 1][j];
561 } 577 }
562 } 578 }
563 579
564 if (j + 1 < layout.h) 580 if (j + 1 < maze.h)
565 { 581 {
566 if (both_pattern & 2) 582 if (both_pattern & 2)
567 { /* join walls/doors to the bottom */ 583 { /* join walls/doors to the bottom */
568/* newlayout[i*2][j*2+1] = '#'; */ 584/* newlayout[i*2][j*2+1] = '#'; */
569 newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; 585 newlayout[i * 2][j * 2 + 1] = maze[i][j + 1];
570 } 586 }
571 587
572 if (wall_pattern == 7) 588 if (wall_pattern == 7)
573 { /* if orig layout is a 2x2 wall block, 589 { /* if orig maze is a 2x2 wall block,
574 * we fill the result with walls. */ 590 * we fill the result with walls. */
575 newlayout[i * 2 + 1][j * 2 + 1] = '#'; 591 newlayout[i * 2 + 1][j * 2 + 1] = '#';
576 } 592 }
577 } 593 }
578} 594}
580/* This function will try to sensibly connect doors so that they meet up with 596/* 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 597 * adjacent walls. Note that it will also presumptuously delete (ignore) doors
582 * that it doesn't know how to correctly expand. 598 * that it doesn't know how to correctly expand.
583 */ 599 */
584static void inline 600static void inline
585expand_door (Layout &newlayout, int i, int j, Layout &layout) 601expand_door (layout &newlayout, int i, int j, layout &maze)
586{ 602{
587 int wall_pattern = calc_pattern ('#', layout, i, j); 603 int wall_pattern = calc_pattern ('#', maze, i, j);
588 int door_pattern = calc_pattern ('D', layout, i, j); 604 int door_pattern = calc_pattern ('D', maze, i, j);
589 int join_pattern; 605 int join_pattern;
590 606
591 /* Doors "like" to connect to walls more than other doors. If there is 607 /* 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 608 * a wall and another door, this door will connect to the wall and
593 * disconnect from the other door. */ 609 * disconnect from the other door. */
596 else 612 else
597 join_pattern = door_pattern; 613 join_pattern = door_pattern;
598 614
599 newlayout[i * 2][j * 2] = 'D'; 615 newlayout[i * 2][j * 2] = 'D';
600 616
601 if (i + 1 < layout.w) 617 if (i + 1 < maze.w)
602 if (join_pattern & 1) 618 if (join_pattern & 1)
603 /* there is a door/wall to the right */ 619 /* there is a door/wall to the right */
604 newlayout[i * 2 + 1][j * 2] = 'D'; 620 newlayout[i * 2 + 1][j * 2] = 'D';
605 621
606 if (j + 1 < layout.h) 622 if (j + 1 < maze.h)
607 if (join_pattern & 2) 623 if (join_pattern & 2)
608 /* there is a door/wall below */ 624 /* there is a door/wall below */
609 newlayout[i * 2][j * 2 + 1] = 'D'; 625 newlayout[i * 2][j * 2 + 1] = 'D';
610} 626}
611 627
612void 628void
613Layout::expand2x () 629layout::expand2x ()
614{ 630{
615 Layout new_layout (w * 2 - 1, h * 2 - 1); 631 layout new_layout (w * 2 - 1, h * 2 - 1);
616 632
617 new_layout.clear (); 633 new_layout.clear ();
634
635 coroapi::cede_to_tick ();
618 636
619 for (int i = 0; i < w; i++) 637 for (int i = 0; i < w; i++)
620 for (int j = 0; j < h; j++) 638 for (int j = 0; j < h; j++)
621 switch (col [i][j]) 639 switch (data [i][j])
622 { 640 {
623 case '#': expand_wall (new_layout, i, j, *this); break; 641 case '#': expand_wall (new_layout, i, j, *this); break;
624 case 'D': expand_door (new_layout, i, j, *this); break; 642 case 'D': expand_door (new_layout, i, j, *this); break;
625 default: expand_misc (new_layout, i, j, *this); break; 643 default: expand_misc (new_layout, i, j, *this); break;
626 } 644 }
628 swap (new_layout); 646 swap (new_layout);
629} 647}
630 648
631///////////////////////////////////////////////////////////////////////////// 649/////////////////////////////////////////////////////////////////////////////
632 650
633/* checks the layout to see if I can stick a horizontal(dir = 0) wall 651/* checks the maze to see if I can stick a horizontal(dir = 0) wall
634 (or vertical, dir == 1) 652 (or vertical, dir == 1)
635 here which ends up on other walls sensibly. */ 653 here which ends up on other walls sensibly. */
636static int 654static int
637can_make_wall (const Layout &maze, int dx, int dy, int dir) 655can_make_wall (const layout &maze, int dx, int dy, int dir)
638{ 656{
639 int i1; 657 int i1;
640 int length = 0; 658 int length = 0;
641 659
642 /* dont make walls if we're on the edge. */ 660 /* dont make walls if we're on the edge. */
705 723
706 return -1; 724 return -1;
707} 725}
708 726
709int 727int
710make_wall (char **maze, int x, int y, int dir) 728make_wall (layout &maze, int x, int y, int dir)
711{ 729{
712 maze[x][y] = 'D'; /* mark a door */ 730 maze[x][y] = 'D'; /* mark a door */
713 731
714 switch (dir) 732 switch (dir)
715 { 733 {
729 747
730 return 0; 748 return 0;
731} 749}
732 750
733void 751void
734Layout::roomify () 752layout::roomify ()
735{ 753{
736 int tries = w * h / 30; 754 int tries = w * h / 30;
755
756 coroapi::cede_to_tick ();
737 757
738 for (int ti = 0; ti < tries; ti++) 758 for (int ti = 0; ti < tries; ti++)
739 { 759 {
740 /* starting location for looking at creating a door */ 760 /* starting location for looking at creating a door */
741 int dx = rmg_rndm (w); 761 int dx = rmg_rndm (w);
764 else 784 else
765 make_wall (*this, dx, dy, 1); 785 make_wall (*this, dx, dy, 1);
766 } 786 }
767} 787}
768 788
789//-GPL
790
769///////////////////////////////////////////////////////////////////////////// 791/////////////////////////////////////////////////////////////////////////////
770 792
793// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
794void
795layout::gen_cave (int subtype)
796{
797 switch (subtype)
798 {
799 // a rough cave
800 case 0:
801 fill_rand (rmg_rndm (85, 97));
802 break;
803
804 // corridors
805 case 1:
806 fill_rand (rmg_rndm (5, 40));
807 erode_1_2 (5, 2, 10);
808 erode_1_2 (5, -1, 10);
809 erode_1_2 (5, 2, 1);
810 break;
811
812 // somewhat open, some room-like structures
813 case 2:
814 fill_rand (45);
815 erode_1_2 (5, 2, 4);
816 erode_1_2 (5, -1, 3);
817 break;
818
819 // wide open, roundish
820 case 3:
821 fill_rand (45);
822 erode_1_2 (5, 0, 5);
823 erode_1_2 (5, 1, 1);
824 break;
825 }
826
827 border ();
828 isolation_remover (1);
829}
830
831void
832layout::gen_castle ()
833{
834 fill ('#');
835
836 for (int n = w * h / 30 + 1; n--; )
837 {
838 int rw = rmg_rndm (6, 10);
839 int rh = rmg_rndm (6, 10);
840
841 if (rw > w || rh > h)
842 continue;
843
844 int rx = rmg_rndm (0, w - rw);
845 int ry = rmg_rndm (0, h - rh);
846
847 rect (rx, ry, rx + rw, ry + rh, '#');
848 fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0);
849 }
850
851 border ();
852 isolation_remover (0);
853}
854
855static void
856gen_mixed_ (layout &maze, random_map_params *RP)
857{
858 if (maze.w > maze.h && maze.w > 16)
859 {
860 int m = rmg_rndm (8, maze.w - 8);
861
862 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP);
863 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP);
864 }
865 else if (maze.h > 16)
866 {
867 int m = rmg_rndm (8, maze.h - 8);
868
869 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP);
870 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP);
871 }
872 else
873 {
874 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
875
876 if (RP->map_layout_style == LAYOUT_MULTIPLE)
877 ++RP->map_layout_style;
878
879 maze.generate (RP);
880 }
881
882 coroapi::cede_to_tick ();
883}
884
885// recursive subdivision with random sublayouts
886static void
887gen_mixed (layout &maze, random_map_params *RP)
888{
889 random_map_params &rp = *new random_map_params (RP);
890 gen_mixed_ (maze, &rp);
891 delete &rp;
892
893 maze.border ();
894
895 // exits currently do not work so well, as they
896 // are currently often found together, so nuke entrances
897 maze.replace ('<', ' ');
898
899 maze.isolation_remover (0);
900}
901
902//+GPL
903
771/* function selects the layout function and gives it whatever 904/* function selects the maze function and gives it whatever
772 arguments it needs. */ 905 arguments it needs. */
773void 906void
774Layout::generate (random_map_params *RP) 907layout::generate (random_map_params *RP)
775{ 908{
776 switch (RP->map_layout_style) 909 switch (RP->map_layout_style)
777 { 910 {
778 case LAYOUT_ONION: 911 case LAYOUT_ONION:
779 map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2); 912 map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2);
834 if (rmg_rndm (2)) 967 if (rmg_rndm (2))
835 doorify (); 968 doorify ();
836 969
837 break; 970 break;
838 971
972 case LAYOUT_CASTLE:
973 gen_castle ();
974
975 if (rmg_rndm (2))
976 doorify ();
977
978 break;
979
980 case LAYOUT_MULTIPLE:
981 gen_mixed (*this, RP);
982 break;
983
839 default: 984 default:
840 abort (); 985 abort ();
841 } 986 }
987}
842 988
843 /* rotate the layout randomly */ 989//-GPL
844 rotate (rmg_rndm (4));
845
846 symmetrize (RP->symmetry_used);
847 990
848#if 0 991#if 0
849 print ();//D 992static void
850#endif 993gen_village (layout &maze)
994{
995 maze.clear ();
996 maze.border ();
851 997
852 if (RP->expand2x) 998 for (int n = maze.w * maze.h / 200 + 1; n--; )
853 expand2x (); 999 {
854} 1000 int rw = rmg_rndm (6, 10);
1001 int rh = rmg_rndm (6, 10);
855 1002
856//-GPL 1003 int rx = rmg_rndm (2, maze.w - rw - 2);
1004 int ry = rmg_rndm (2, maze.h - rh - 2);
857 1005
858#if 0 1006 maze.rect (rx, ry, rx + rw, ry + rh, '#');
1007 }
1008
1009 maze.border ();
1010 maze.isolation_remover (2);
1011}
1012
859static struct demo 1013static struct demo
860{ 1014{
861 demo () 1015 demo ()
862 { 1016 {
863 rmg_rndm.seed (time (0)); 1017 rmg_rndm.seed (time (0));
1018 extern void hack();hack ();
864 1019
865 for(int i=1;i<10;i) 1020 for(int i=1;i<100;i++)
866 { 1021 {
867 Layout maze (10, 10); 1022 layout maze (40, 30);
868 maze.fill_rand (90); 1023 maze.fill_rand (99);
869 maze.border (); 1024 maze.border ();
870 maze.isolation_remover (); 1025 maze.isolation_remover (2);
871 maze.doorify ();
872 maze.symmetrize (rmg_rndm (2, 4));
873 maze.rotate (rmg_rndm (4));
874 maze.expand2x ();
875 maze.print (); 1026 maze.print ();
876 } 1027 }
877 1028
878 exit (1); 1029 exit (1);
879 } 1030 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines