--- deliantra/server/random_maps/random_map.C 2006/12/31 20:48:27 1.15 +++ deliantra/server/random_maps/random_map.C 2007/01/27 02:19:37 1.27 @@ -1,26 +1,26 @@ - /* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 2001 Mark Wedel & Crossfire Development Team - Copyright (C) 1992 Frank Tore Johansen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - The authors can be reached via e-mail at -*/ + * CrossFire, A Multiplayer game for X-windows + * + * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team + * Copyright (C) 2001 Mark Wedel & Crossfire Development Team + * Copyright (C) 1992 Frank Tore Johansen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * The authors can be reached via e-mail at + */ #include #include @@ -31,6 +31,8 @@ #include #include +#define CEDE coroapi::cede (); rndm.seed (RP->random_seed + __LINE__); + void dump_layout (char **layout, random_map_params *RP) { @@ -53,15 +55,20 @@ printf ("\n"); } -maptile * -generate_random_map (const char *OutFileName, random_map_params *RP) +bool +maptile::generate_random_map (random_map_params *RP) { - char **layout, buf[HUGE_BUF]; - maptile *theMap; + char **layout, buf[16384]; int i; + RP->Xsize = RP->xsize; + RP->Ysize = RP->ysize; + /* pick a random seed, or use the one from the input file */ - SRANDOM (RP->random_seed ? RP->random_seed + RP->dungeon_level : time (0)); + RP->random_seed = RP->random_seed + ? RP->random_seed + RP->dungeon_level + : time (0); + CEDE; write_map_parameters_to_string (buf, RP); @@ -79,10 +86,21 @@ RP->difficulty_given = 1; if (RP->Xsize < MIN_RANDOM_MAP_SIZE) - RP->Xsize = MIN_RANDOM_MAP_SIZE + RANDOM () % 25 + 5; + RP->Xsize = MIN_RANDOM_MAP_SIZE + rndm (25) + 5; if (RP->Ysize < MIN_RANDOM_MAP_SIZE) - RP->Ysize = MIN_RANDOM_MAP_SIZE + RANDOM () % 25 + 5; + RP->Ysize = MIN_RANDOM_MAP_SIZE + rndm (25) + 5; + + if (RP->symmetry == SYMMETRY_RANDOM) + RP->symmetry_used = rndm (SYMMETRY_XY) + 1; + else + RP->symmetry_used = RP->symmetry; + + if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) + RP->Ysize = RP->Ysize / 2 + 1; + + if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) + RP->Xsize = RP->Xsize / 2 + 1; if (RP->expand2x > 0) { @@ -90,6 +108,35 @@ RP->Ysize /= 2; } + RP->map_layout_style = LAYOUT_NONE; + + /* Redo this - there was a lot of redundant code of checking for preset + * layout style and then random layout style. Instead, figure out + * the numeric layoutstyle, so there is only one area that actually + * calls the code to make the maps. + */ + if (strstr (RP->layoutstyle, "onion")) + RP->map_layout_style = LAYOUT_ONION; + + if (strstr (RP->layoutstyle, "maze")) + RP->map_layout_style = LAYOUT_MAZE; + + if (strstr (RP->layoutstyle, "spiral")) + RP->map_layout_style = LAYOUT_SPIRAL; + + if (strstr (RP->layoutstyle, "rogue")) + RP->map_layout_style = LAYOUT_ROGUELIKE; + + if (strstr (RP->layoutstyle, "snake")) + RP->map_layout_style = LAYOUT_SNAKE; + + if (strstr (RP->layoutstyle, "squarespiral")) + RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; + + /* No style found - choose one randomly */ + if (RP->map_layout_style == LAYOUT_NONE) + RP->map_layout_style = rndm (NROFLAYOUTS - 1) + 1; + layout = layoutgen (RP); #ifdef RMAP_DEBUG @@ -102,65 +149,70 @@ /* difficulty+=1; */ /* rotate the layout randomly */ - layout = rotate_layout (layout, RANDOM () % 4, RP); + layout = rotate_layout (layout, rndm (4), RP); #ifdef RMAP_DEBUG dump_layout (layout, RP); #endif /* allocate the map and set the floor */ - theMap = make_map_floor (layout, RP->floorstyle, RP); - - /* set the name of the map. */ - theMap->path = OutFileName; + make_map_floor (layout, RP->floorstyle, RP); /* set region */ - theMap->region = RP->region; + default_region = RP->region; + + CEDE; - coroapi::cede (); /* create walls unless the wallstyle is "none" */ if (strcmp (RP->wallstyle, "none")) { - make_map_walls (theMap, layout, RP->wallstyle, RP); + make_map_walls (this, layout, RP->wallstyle, RP); /* place doors unless doorstyle or wallstyle is "none" */ if (strcmp (RP->doorstyle, "none")) - put_doors (theMap, layout, RP->doorstyle, RP); - + put_doors (this, layout, RP->doorstyle, RP); } - coroapi::cede (); + CEDE; + /* create exits unless the exitstyle is "none" */ if (strcmp (RP->exitstyle, "none")) - place_exits (theMap, layout, RP->exitstyle, RP->orientation, RP); + place_exits (this, layout, RP->exitstyle, RP->orientation, RP); - coroapi::cede (); - place_specials_in_map (theMap, layout, RP); + CEDE; + + place_specials_in_map (this, layout, RP); + + CEDE; - coroapi::cede (); /* create monsters unless the monsterstyle is "none" */ if (strcmp (RP->monsterstyle, "none")) - place_monsters (theMap, RP->monsterstyle, RP->difficulty, RP); + place_monsters (this, RP->monsterstyle, RP->difficulty, RP); + + CEDE; - coroapi::cede (); /* treasures needs to have a proper difficulty set for the map. */ - theMap->difficulty = theMap->estimate_difficulty (); + difficulty = estimate_difficulty (); + + CEDE; - coroapi::cede (); /* create treasure unless the treasurestyle is "none" */ if (strcmp (RP->treasurestyle, "none")) - place_treasure (theMap, layout, RP->treasurestyle, RP->treasureoptions, RP); + place_treasure (this, layout, RP->treasurestyle, RP->treasureoptions, RP); + + CEDE; - coroapi::cede (); /* create decor unless the decorstyle is "none" */ if (strcmp (RP->decorstyle, "none")) - put_decor (theMap, layout, RP->decorstyle, RP->decoroptions, RP); + put_decor (this, layout, RP->decorstyle, RP->decoroptions, RP); + + CEDE; - coroapi::cede (); /* generate treasures, etc. */ - theMap->fix_auto_apply (); + fix_auto_apply (); - coroapi::cede (); - unblock_exits (theMap, layout, RP); + CEDE; + + unblock_exits (this, layout, RP); /* free the layout */ for (i = 0; i < RP->Xsize; i++) @@ -168,10 +220,12 @@ free (layout); - theMap->msg = strdup (buf); - theMap->in_memory = MAP_IN_MEMORY; + msg = strdup (buf); + in_memory = MAP_IN_MEMORY; + + CEDE; - return theMap; + return 1; } /* function selects the layout function and gives it whatever @@ -182,66 +236,23 @@ char **maze = 0; int oxsize = RP->Xsize, oysize = RP->Ysize; - if (RP->symmetry == SYMMETRY_RANDOM) - RP->symmetry_used = (RANDOM () % (SYMMETRY_XY)) + 1; - else - RP->symmetry_used = RP->symmetry; - - if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) - RP->Ysize = RP->Ysize / 2 + 1; - if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) - RP->Xsize = RP->Xsize / 2 + 1; - - if (RP->Xsize < MIN_RANDOM_MAP_SIZE) - RP->Xsize = MIN_RANDOM_MAP_SIZE + RANDOM () % 5; - if (RP->Ysize < MIN_RANDOM_MAP_SIZE) - RP->Ysize = MIN_RANDOM_MAP_SIZE + RANDOM () % 5; - RP->map_layout_style = 0; - - /* Redo this - there was a lot of redundant code of checking for preset - * layout style and then random layout style. Instead, figure out - * the numeric layoutstyle, so there is only one area that actually - * calls the code to make the maps. - */ - if (strstr (RP->layoutstyle, "onion")) - RP->map_layout_style = LAYOUT_ONION; - - if (strstr (RP->layoutstyle, "maze")) - RP->map_layout_style = LAYOUT_MAZE; - - if (strstr (RP->layoutstyle, "spiral")) - RP->map_layout_style = LAYOUT_SPIRAL; - - if (strstr (RP->layoutstyle, "rogue")) - RP->map_layout_style = LAYOUT_ROGUELIKE; - - if (strstr (RP->layoutstyle, "snake")) - RP->map_layout_style = LAYOUT_SNAKE; - - if (strstr (RP->layoutstyle, "squarespiral")) - RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; - - /* No style found - choose one ranomdly */ - if (RP->map_layout_style == LAYOUT_NONE) - RP->map_layout_style = (RANDOM () % (NROFLAYOUTS - 1)) + 1; - switch (RP->map_layout_style) { case LAYOUT_ONION: maze = map_gen_onion (RP->Xsize, RP->Ysize, RP->layoutoptions1, RP->layoutoptions2); - if (!(RANDOM () % 3) && !(RP->layoutoptions1 & RMOPT_WALLS_ONLY)) + if (!(rndm (3)) && !(RP->layoutoptions1 & RMOPT_WALLS_ONLY)) roomify_layout (maze, RP); break; case LAYOUT_MAZE: - maze = maze_gen (RP->Xsize, RP->Ysize, RANDOM () % 2); - if (!(RANDOM () % 2)) + maze = maze_gen (RP->Xsize, RP->Ysize, rndm (2)); + if (!(rndm (2))) doorify_layout (maze, RP); break; case LAYOUT_SPIRAL: maze = map_gen_spiral (RP->Xsize, RP->Ysize, RP->layoutoptions1); - if (!(RANDOM () % 2)) + if (!(rndm (2))) doorify_layout (maze, RP); break; @@ -262,13 +273,13 @@ case LAYOUT_SNAKE: maze = make_snake_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); - if (RANDOM () % 2) + if (rndm (2)) roomify_layout (maze, RP); break; case LAYOUT_SQUARE_SPIRAL: maze = make_square_spiral_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); - if (RANDOM () % 2) + if (rndm (2)) roomify_layout (maze, RP); break; } @@ -441,21 +452,25 @@ int dx, dy; /* starting location for looking at creating a door */ int cx, cy; /* results of checking on creating walls. */ - dx = RANDOM () % RP->Xsize; - dy = RANDOM () % RP->Ysize; + dx = rndm (RP->Xsize); + dy = rndm (RP->Ysize); + cx = can_make_wall (maze, dx, dy, 0, RP); /* horizontal */ cy = can_make_wall (maze, dx, dy, 1, RP); /* vertical */ if (cx == -1) { if (cy != -1) make_wall (maze, dx, dy, 1); + continue; } + if (cy == -1) { make_wall (maze, dx, dy, 0); continue; } + if (cx < cy) make_wall (maze, dx, dy, 0); else @@ -611,15 +626,17 @@ int di; int sindex; - di = RANDOM () % doorlocs; + di = rndm (doorlocs); i = doorlist_x[di]; j = doorlist_y[di]; sindex = surround_flag (maze, i, j, RP); + if (sindex == 3 || sindex == 12) /* these are possible door sindex */ { maze[i][j] = 'D'; ndoors--; } + /* reduce the size of the list */ doorlocs--; doorlist_x[di] = doorlist_x[doorlocs]; @@ -633,9 +650,9 @@ void write_map_parameters_to_string (char *buf, random_map_params *RP) { - char small_buf[2048]; + char small_buf[16384]; - sprintf (buf, "xsize %d\nysize %d\n", RP->Xsize, RP->Ysize); + sprintf (buf, "xsize %d\nysize %d\n", RP->xsize, RP->ysize); if (RP->wallstyle[0]) { @@ -778,22 +795,21 @@ strcat (buf, small_buf); } - if (RP->random_seed) + if (RP->treasureoptions) { - /* Add one so that the next map is a bit different */ - sprintf (small_buf, "random_seed %d\n", RP->random_seed + 1); + sprintf (small_buf, "treasureoptions %d\n", RP->treasureoptions); strcat (buf, small_buf); } - if (RP->treasureoptions) + if (RP->random_seed) { - sprintf (small_buf, "treasureoptions %d\n", RP->treasureoptions); + sprintf (small_buf, "random_seed %u\n", RP->random_seed); strcat (buf, small_buf); } - if (RP->random_seed) + if (RP->custom) { - sprintf (small_buf, "random_seed %d\n", RP->random_seed); + sprintf (small_buf, "custom %s\n", RP->custom); strcat (buf, small_buf); } } @@ -823,9 +839,9 @@ int difficulty_given_n, int decoroptions_n, int orientation_n, - int origin_x_n, int origin_y_n, int random_seed_n, int treasureoptions_n, float difficulty_increase) + int origin_x_n, int origin_y_n, uint32_t random_seed_n, int treasureoptions_n, float difficulty_increase) { - char small_buf[2048]; + char small_buf[16384]; sprintf (buf, "xsize %d\nysize %d\n", xsize_n, ysize_n); @@ -969,7 +985,7 @@ if (random_seed_n) { /* Add one so that the next map is a bit different */ - sprintf (small_buf, "random_seed %d\n", random_seed_n + 1); + sprintf (small_buf, "random_seed %u\n", random_seed_n + 1); strcat (buf, small_buf); }