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

Comparing deliantra/server/random_maps/random_map.C (file contents):
Revision 1.56 by root, Wed Jun 30 20:51:02 2010 UTC vs.
Revision 1.60 by root, Fri Jul 2 03:40:14 2010 UTC

28#include <random_map.h> 28#include <random_map.h>
29#include <rproto.h> 29#include <rproto.h>
30#include <sproto.h> 30#include <sproto.h>
31 31
32#define CEDE coroapi::cede_to_tick () 32#define CEDE coroapi::cede_to_tick ()
33
34static void symmetrize_layout (Layout maze, random_map_params *RP);
35static void rotate_layout (Layout maze, int rotation);
36 33
37noinline SV * 34noinline SV *
38random_map_params::get_sv (const char *option) const 35random_map_params::get_sv (const char *option) const
39{ 36{
40 SV **he = hv_fetch (hv, option, strlen (option), 0); 37 SV **he = hv_fetch (hv, option, strlen (option), 0);
169random_map_params::~random_map_params () 166random_map_params::~random_map_params ()
170{ 167{
171 SvREFCNT_dec (hv); 168 SvREFCNT_dec (hv);
172} 169}
173 170
174/* takes a map and makes it symmetric: adjusts Xsize and
175 * Ysize to produce a symmetric map.
176 */
177static void
178symmetrize_layout (Layout layout, random_map_params *RP)
179{
180 if (RP->symmetry_used == SYMMETRY_NONE)
181 return;
182
183 Layout sym_layout (
184 RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY ? layout->w * 2 - 3 : layout->w,
185 RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY ? layout->h * 2 - 3 : layout->h
186 );
187
188 if (RP->symmetry_used == SYMMETRY_X)
189 for (int i = 0; i < sym_layout->w / 2 + 1; i++)
190 for (int j = 0; j < sym_layout->h; j++)
191 {
192 sym_layout[i ][j] =
193 sym_layout[sym_layout->w - i - 1][j] = layout[i][j];
194 }
195
196 if (RP->symmetry_used == SYMMETRY_Y)
197 for (int i = 0; i < sym_layout->w; i++)
198 for (int j = 0; j < sym_layout->h / 2 + 1; j++)
199 {
200 sym_layout[i][j ] =
201 sym_layout[i][sym_layout->h - j - 1] = layout[i][j];
202 }
203
204 if (RP->symmetry_used == SYMMETRY_XY)
205 for (int i = 0; i < sym_layout->w / 2 + 1; i++)
206 for (int j = 0; j < sym_layout->h / 2 + 1; j++)
207 {
208 sym_layout[i ][j ] =
209 sym_layout[i ][sym_layout->h - j - 1] =
210 sym_layout[sym_layout->w - i - 1][j ] =
211 sym_layout[sym_layout->w - i - 1][sym_layout->h - j - 1] = layout[i][j];
212 }
213
214 layout.swap (sym_layout);
215 sym_layout.free ();
216
217 /* reconnect disjointed spirals */
218 /* reconnect disjointed nethacklayouts: the routine for
219 spirals will do the trick? */
220 if (RP->map_layout_style == LAYOUT_SPIRAL
221 || RP->map_layout_style == LAYOUT_ROGUELIKE)
222 connect_spirals (layout->w, layout->h, RP->symmetry_used, layout);
223}
224
225/* takes a map and rotates it. This completes the
226 onion layouts, making them possibly centered on any wall.
227 It'll modify Xsize and Ysize if they're swapped.
228*/
229static void
230rotate_layout (Layout layout, int rotation)
231{
232 int w = layout->w;
233 int h = layout->h;
234
235 switch (rotation)
236 {
237 case 2: /* a reflection */
238 {
239 Layout new_layout (w, h);
240
241 for (int i = 0; i < w; i++) /* copy a reflection back */
242 for (int j = 0; j < h; j++)
243 new_layout[i][j] = layout[w - i - 1][h - j - 1];
244
245 layout.swap (new_layout);
246 new_layout.free ();
247 }
248 break;
249
250 case 1:
251 case 3:
252 {
253 Layout new_layout (h, w);
254
255 if (rotation == 1) /* swap x and y */
256 for (int i = 0; i < w; i++)
257 for (int j = 0; j < h; j++)
258 new_layout[j][i] = layout[i][j];
259
260 if (rotation == 3) /* swap x and y */
261 for (int i = 0; i < w; i++)
262 for (int j = 0; j < h; j++)
263 new_layout[j][i] = layout[w - i - 1][h - j - 1];
264
265 layout.swap (new_layout);
266 new_layout.free ();
267 }
268 break;
269 }
270}
271
272/* checks the layout to see if I can stick a horizontal(dir = 0) wall
273 (or vertical, dir == 1)
274 here which ends up on other walls sensibly. */
275static int
276can_make_wall (char **maze, int dx, int dy, int dir, random_map_params *RP)
277{
278 int i1;
279 int length = 0;
280
281 /* dont make walls if we're on the edge. */
282 if (dx == 0 || dx == (RP->Xsize - 1) || dy == 0 || dy == (RP->Ysize - 1))
283 return -1;
284
285 /* don't make walls if we're ON a wall. */
286 if (maze[dx][dy] != 0)
287 return -1;
288
289 if (dir == 0) /* horizontal */
290 {
291 int y = dy;
292
293 for (i1 = dx - 1; i1 > 0; i1--)
294 {
295 int sindex = surround_flag2 (maze, i1, y, RP);
296
297 if (sindex == 1)
298 break;
299 if (sindex != 0)
300 return -1; /* can't make horiz. wall here */
301 if (maze[i1][y] != 0)
302 return -1; /* can't make horiz. wall here */
303 length++;
304 }
305
306 for (i1 = dx + 1; i1 < RP->Xsize - 1; i1++)
307 {
308 int sindex = surround_flag2 (maze, i1, y, RP);
309
310 if (sindex == 2)
311 break;
312 if (sindex != 0)
313 return -1; /* can't make horiz. wall here */
314 if (maze[i1][y] != 0)
315 return -1; /* can't make horiz. wall here */
316 length++;
317 }
318 return length;
319 }
320 else
321 { /* vertical */
322 int x = dx;
323
324 for (i1 = dy - 1; i1 > 0; i1--)
325 {
326 int sindex = surround_flag2 (maze, x, i1, RP);
327
328 if (sindex == 4)
329 break;
330 if (sindex != 0)
331 return -1; /* can't make vert. wall here */
332 if (maze[x][i1] != 0)
333 return -1; /* can't make horiz. wall here */
334 length++;
335 }
336
337 for (i1 = dy + 1; i1 < RP->Ysize - 1; i1++)
338 {
339 int sindex = surround_flag2 (maze, x, i1, RP);
340
341 if (sindex == 8)
342 break;
343 if (sindex != 0)
344 return -1; /* can't make verti. wall here */
345 if (maze[x][i1] != 0)
346 return -1; /* can't make horiz. wall here */
347 length++;
348 }
349
350 return length;
351 }
352
353 return -1;
354}
355
356/* take a layout and make some rooms in it.
357 --works best on onions.*/
358static void
359roomify_layout (char **maze, random_map_params *RP)
360{
361 int tries = RP->Xsize * RP->Ysize / 30;
362
363 for (int ti = 0; ti < tries; ti++)
364 {
365 /* starting location for looking at creating a door */
366 int dx = rmg_rndm (RP->Xsize);
367 int dy = rmg_rndm (RP->Ysize);
368
369 /* results of checking on creating walls. */
370 int cx = can_make_wall (maze, dx, dy, 0, RP); /* horizontal */
371 int cy = can_make_wall (maze, dx, dy, 1, RP); /* vertical */
372
373 if (cx == -1)
374 {
375 if (cy != -1)
376 make_wall (maze, dx, dy, 1);
377
378 continue;
379 }
380
381 if (cy == -1)
382 {
383 make_wall (maze, dx, dy, 0);
384 continue;
385 }
386
387 if (cx < cy)
388 make_wall (maze, dx, dy, 0);
389 else
390 make_wall (maze, dx, dy, 1);
391 }
392}
393
394int
395make_wall (char **maze, int x, int y, int dir)
396{
397 maze[x][y] = 'D'; /* mark a door */
398
399 switch (dir)
400 {
401 case 0: /* horizontal */
402 {
403 for (int i1 = x - 1; maze[i1][y] == 0; --i1) maze[i1][y] = '#';
404 for (int i1 = x + 1; maze[i1][y] == 0; ++i1) maze[i1][y] = '#';
405 break;
406 }
407 case 1: /* vertical */
408 {
409 for (int i1 = y - 1; maze[x][i1] == 0; --i1) maze[x][i1] = '#';
410 for (int i1 = y + 1; maze[x][i1] == 0; ++i1) maze[x][i1] = '#';
411 break;
412 }
413 }
414
415 return 0;
416}
417
418/* puts doors at appropriate locations in a layout. */
419static void
420doorify_layout (char **maze, random_map_params *RP)
421{
422 int ndoors = RP->Xsize * RP->Ysize / 60; /* reasonable number of doors. */
423 int doorlocs = 0; /* # of available doorlocations */
424
425 uint16 *doorlist_x = salloc<uint16> (RP->Xsize * RP->Ysize);
426 uint16 *doorlist_y = salloc<uint16> (RP->Xsize * RP->Ysize);
427
428 /* make a list of possible door locations */
429 for (int i = 1; i < RP->Xsize - 1; i++)
430 for (int j = 1; j < RP->Ysize - 1; j++)
431 {
432 int sindex = surround_flag (maze, i, j, RP);
433
434 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
435 {
436 doorlist_x[doorlocs] = i;
437 doorlist_y[doorlocs] = j;
438 doorlocs++;
439 }
440 }
441
442 while (ndoors > 0 && doorlocs > 0)
443 {
444 int di = rmg_rndm (doorlocs);
445 int i = doorlist_x[di];
446 int j = doorlist_y[di];
447 int sindex = surround_flag (maze, i, j, RP);
448
449 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
450 {
451 maze[i][j] = 'D';
452 ndoors--;
453 }
454
455 /* reduce the size of the list */
456 doorlocs--;
457 doorlist_x[di] = doorlist_x[doorlocs];
458 doorlist_y[di] = doorlist_y[doorlocs];
459 }
460
461 sfree (doorlist_x, RP->Xsize * RP->Ysize);
462 sfree (doorlist_y, RP->Xsize * RP->Ysize);
463}
464
465/* function selects the layout function and gives it whatever
466 arguments it needs. */
467static Layout
468layoutgen (random_map_params *RP)
469{
470 Layout layout (RP);
471
472 switch (RP->map_layout_style)
473 {
474 case LAYOUT_ONION:
475 map_gen_onion (layout, RP->layoutoptions1, RP->layoutoptions2);
476
477 if (!(rmg_rndm (3)) && !(RP->layoutoptions1 & (RMOPT_WALLS_ONLY | RMOPT_WALL_OFF)))
478 roomify_layout (layout, RP);
479
480 break;
481
482 case LAYOUT_MAZE:
483 maze_gen (layout, RP->get_iv ("maze_type", rmg_rndm (4)));
484
485 if (!(rmg_rndm (2)))
486 doorify_layout (layout, RP);
487
488 break;
489
490 case LAYOUT_SPIRAL:
491 map_gen_spiral (layout, RP->layoutoptions1);
492
493 if (!(rmg_rndm (2)))
494 doorify_layout (layout, RP);
495
496 break;
497
498 case LAYOUT_ROGUELIKE:
499 /* Don't put symmetry in rogue maps. There isn't much reason to
500 * do so in the first place (doesn't make it any more interesting),
501 * but more importantly, the symmetry code presumes we are symmetrizing
502 * spirals, or maps with lots of passages - making a symmetric rogue
503 * map fails because its likely that the passages the symmetry process
504 * creates may not connect the rooms.
505 */
506 RP->symmetry_used = SYMMETRY_NONE;
507 roguelike_layout_gen (layout, RP->layoutoptions1);
508 /* no doorifying... done already */
509 break;
510
511 case LAYOUT_SNAKE:
512 make_snake_layout (layout, RP->layoutoptions1);
513
514 if (rmg_rndm (2))
515 roomify_layout (layout, RP);
516
517 break;
518
519 case LAYOUT_SQUARE_SPIRAL:
520 make_square_spiral_layout (layout, RP->layoutoptions1);
521
522 if (rmg_rndm (2))
523 roomify_layout (layout, RP);
524
525 break;
526
527 default:
528 abort ();
529 }
530
531 /* rotate the layout randomly */
532 rotate_layout (layout, rmg_rndm (4));
533
534 symmetrize_layout (layout, RP);
535
536#ifdef RMAP_DEBUG
537 dump_layout (layout);
538#endif
539
540 if (RP->expand2x)
541 expand2x (layout);
542
543 return layout;
544}
545
546bool 171bool
547maptile::generate_random_map (random_map_params *RP) 172maptile::generate_random_map (random_map_params *RP)
548{ 173{
549 RP->Xsize = RP->xsize; 174 RP->Xsize = RP->xsize;
550 RP->Ysize = RP->ysize; 175 RP->Ysize = RP->ysize;
576 RP->Xsize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; 201 RP->Xsize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5;
577 202
578 if (RP->Ysize < MIN_RANDOM_MAP_SIZE) 203 if (RP->Ysize < MIN_RANDOM_MAP_SIZE)
579 RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; 204 RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5;
580 205
206 min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE);
207 min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE);
208
581 if (RP->symmetry == SYMMETRY_RANDOM) 209 if (RP->symmetry == SYMMETRY_RANDOM)
582 RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1; 210 RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1;
583 else 211 else
584 RP->symmetry_used = RP->symmetry; 212 RP->symmetry_used = RP->symmetry;
585 213
593 { 221 {
594 RP->Xsize /= 2; 222 RP->Xsize /= 2;
595 RP->Ysize /= 2; 223 RP->Ysize /= 2;
596 } 224 }
597 225
598 RP->map_layout_style = LAYOUT_NONE;
599
600 /* Redo this - there was a lot of redundant code of checking for preset
601 * layout style and then random layout style. Instead, figure out
602 * the numeric layoutstyle, so there is only one area that actually
603 * calls the code to make the maps.
604 */
605 if (strstr (RP->layoutstyle, "onion")) 226 if (strstr (RP->layoutstyle, "onion"))
606 RP->map_layout_style = LAYOUT_ONION; 227 RP->map_layout_style = LAYOUT_ONION;
607 else if (strstr (RP->layoutstyle, "maze")) 228 else if (strstr (RP->layoutstyle, "maze"))
608 RP->map_layout_style = LAYOUT_MAZE; 229 RP->map_layout_style = LAYOUT_MAZE;
609 else if (strstr (RP->layoutstyle, "spiral")) 230 else if (strstr (RP->layoutstyle, "spiral"))
612 RP->map_layout_style = LAYOUT_ROGUELIKE; 233 RP->map_layout_style = LAYOUT_ROGUELIKE;
613 else if (strstr (RP->layoutstyle, "snake")) 234 else if (strstr (RP->layoutstyle, "snake"))
614 RP->map_layout_style = LAYOUT_SNAKE; 235 RP->map_layout_style = LAYOUT_SNAKE;
615 else if (strstr (RP->layoutstyle, "squarespiral")) 236 else if (strstr (RP->layoutstyle, "squarespiral"))
616 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; 237 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL;
238 else if (strstr (RP->layoutstyle, "cave"))
617 else if (RP->map_layout_style == LAYOUT_NONE) 239 RP->map_layout_style = LAYOUT_CAVE;
240 else
618 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */ 241 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */
619 else
620 abort ();
621 242
622 Layout layout = layoutgen (RP); 243 Layout layout (RP->Xsize, RP->Ysize);
623 244 layout.generate (RP);
624#ifdef RMAP_DEBUG
625 dump_layout (layout);
626#endif
627 245
628 /* increment these for the current map */ 246 /* increment these for the current map */
629 ++RP->dungeon_level; 247 ++RP->dungeon_level;
630 248
631 // need to patch RP becasue following code doesn't use the Layout object 249 // need to patch RP becasue following code doesn't use the Layout object
632 RP->Xsize = layout->w; 250 RP->Xsize = layout.w;
633 RP->Ysize = layout->h; 251 RP->Ysize = layout.h;
634 252
635 /* allocate the map and set the floor */ 253 /* allocate the map and set the floor */
636 make_map_floor (layout, RP->get_str ("floorstyle", ""), RP); 254 make_map_floor (layout, RP->get_str ("floorstyle", ""), RP);
637 255
638 /* set region */ 256 /* set region */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines