--- deliantra/server/random_maps/random_map.C 2006/12/13 21:27:09 1.8 +++ deliantra/server/random_maps/random_map.C 2006/12/31 20:48:27 1.15 @@ -1,3 +1,4 @@ + /* CrossFire, A Multiplayer game for X-windows @@ -31,7 +32,7 @@ #include void -dump_layout (char **layout, RMParms * RP) +dump_layout (char **layout, random_map_params *RP) { { int i, j; @@ -52,20 +53,15 @@ printf ("\n"); } -extern FILE *logfile; - maptile * -generate_random_map (const char *OutFileName, RMParms * RP) +generate_random_map (const char *OutFileName, random_map_params *RP) { char **layout, buf[HUGE_BUF]; maptile *theMap; int i; /* pick a random seed, or use the one from the input file */ - if (RP->random_seed == 0) - RP->random_seed = time (0); - - SRANDOM (RP->random_seed); + SRANDOM (RP->random_seed ? RP->random_seed + RP->dungeon_level : time (0)); write_map_parameters_to_string (buf, RP); @@ -115,11 +111,12 @@ theMap = make_map_floor (layout, RP->floorstyle, RP); /* set the name of the map. */ - strcpy (theMap->path, OutFileName); + theMap->path = OutFileName; /* set region */ theMap->region = RP->region; + coroapi::cede (); /* create walls unless the wallstyle is "none" */ if (strcmp (RP->wallstyle, "none")) { @@ -131,38 +128,48 @@ } + coroapi::cede (); /* create exits unless the exitstyle is "none" */ if (strcmp (RP->exitstyle, "none")) place_exits (theMap, layout, RP->exitstyle, RP->orientation, RP); + coroapi::cede (); place_specials_in_map (theMap, layout, RP); + coroapi::cede (); /* create monsters unless the monsterstyle is "none" */ if (strcmp (RP->monsterstyle, "none")) place_monsters (theMap, RP->monsterstyle, RP->difficulty, RP); + coroapi::cede (); /* treasures needs to have a proper difficulty set for the map. */ - theMap->difficulty = calculate_difficulty (theMap); + theMap->difficulty = theMap->estimate_difficulty (); + coroapi::cede (); /* create treasure unless the treasurestyle is "none" */ if (strcmp (RP->treasurestyle, "none")) place_treasure (theMap, layout, RP->treasurestyle, RP->treasureoptions, RP); + coroapi::cede (); /* create decor unless the decorstyle is "none" */ if (strcmp (RP->decorstyle, "none")) put_decor (theMap, layout, RP->decorstyle, RP->decoroptions, RP); + coroapi::cede (); /* generate treasures, etc. */ - fix_auto_apply (theMap); + theMap->fix_auto_apply (); + coroapi::cede (); unblock_exits (theMap, layout, RP); /* free the layout */ for (i = 0; i < RP->Xsize; i++) free (layout[i]); + free (layout); - theMap->msg = strdup_local (buf); + theMap->msg = strdup (buf); + theMap->in_memory = MAP_IN_MEMORY; return theMap; } @@ -170,19 +177,19 @@ /* function selects the layout function and gives it whatever arguments it needs. */ char ** -layoutgen (RMParms * RP) +layoutgen (random_map_params *RP) { char **maze = 0; int oxsize = RP->Xsize, oysize = RP->Ysize; - if (RP->symmetry == RANDOM_SYM) - RP->symmetry_used = (RANDOM () % (XY_SYM)) + 1; + if (RP->symmetry == SYMMETRY_RANDOM) + RP->symmetry_used = (RANDOM () % (SYMMETRY_XY)) + 1; else RP->symmetry_used = RP->symmetry; - if (RP->symmetry_used == Y_SYM || RP->symmetry_used == XY_SYM) + if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) RP->Ysize = RP->Ysize / 2 + 1; - if (RP->symmetry_used == X_SYM || RP->symmetry_used == XY_SYM) + if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) RP->Xsize = RP->Xsize / 2 + 1; if (RP->Xsize < MIN_RANDOM_MAP_SIZE) @@ -197,108 +204,95 @@ * calls the code to make the maps. */ if (strstr (RP->layoutstyle, "onion")) - { - RP->map_layout_style = ONION_LAYOUT; - } + RP->map_layout_style = LAYOUT_ONION; if (strstr (RP->layoutstyle, "maze")) - { - RP->map_layout_style = MAZE_LAYOUT; - } + RP->map_layout_style = LAYOUT_MAZE; if (strstr (RP->layoutstyle, "spiral")) - { - RP->map_layout_style = SPIRAL_LAYOUT; - } + RP->map_layout_style = LAYOUT_SPIRAL; if (strstr (RP->layoutstyle, "rogue")) - { - RP->map_layout_style = ROGUELIKE_LAYOUT; - } + RP->map_layout_style = LAYOUT_ROGUELIKE; if (strstr (RP->layoutstyle, "snake")) - { - RP->map_layout_style = SNAKE_LAYOUT; - } + RP->map_layout_style = LAYOUT_SNAKE; if (strstr (RP->layoutstyle, "squarespiral")) - { - RP->map_layout_style = SQUARE_SPIRAL_LAYOUT; - } + RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; + /* No style found - choose one ranomdly */ - if (RP->map_layout_style == 0) - { - RP->map_layout_style = (RANDOM () % NROFLAYOUTS) + 1; - } + if (RP->map_layout_style == LAYOUT_NONE) + RP->map_layout_style = (RANDOM () % (NROFLAYOUTS - 1)) + 1; switch (RP->map_layout_style) { - - case ONION_LAYOUT: - maze = map_gen_onion (RP->Xsize, RP->Ysize, RP->layoutoptions1, RP->layoutoptions2); - if (!(RANDOM () % 3) && !(RP->layoutoptions1 & OPT_WALLS_ONLY)) - roomify_layout (maze, RP); - break; - - case MAZE_LAYOUT: - maze = maze_gen (RP->Xsize, RP->Ysize, RANDOM () % 2); - if (!(RANDOM () % 2)) - doorify_layout (maze, RP); - break; - - case SPIRAL_LAYOUT: - maze = map_gen_spiral (RP->Xsize, RP->Ysize, RP->layoutoptions1); - if (!(RANDOM () % 2)) - doorify_layout (maze, RP); - break; - - case ROGUELIKE_LAYOUT: - /* Don't put symmetry in rogue maps. There isn't much reason to - * do so in the first place (doesn't make it any more interesting), - * but more importantly, the symmetry code presumes we are symmetrizing - * spirals, or maps with lots of passages - making a symmetric rogue - * map fails because its likely that the passages the symmetry process - * creates may not connect the rooms. - */ - RP->symmetry_used = NO_SYM; - RP->Ysize = oysize; - RP->Xsize = oxsize; - maze = roguelike_layout_gen (RP->Xsize, RP->Ysize, RP->layoutoptions1); - /* no doorifying... done already */ - break; - - case SNAKE_LAYOUT: - maze = make_snake_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); - if (RANDOM () % 2) - roomify_layout (maze, RP); - break; - - case SQUARE_SPIRAL_LAYOUT: - maze = make_square_spiral_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); - if (RANDOM () % 2) - roomify_layout (maze, RP); - break; + case LAYOUT_ONION: + maze = map_gen_onion (RP->Xsize, RP->Ysize, RP->layoutoptions1, RP->layoutoptions2); + if (!(RANDOM () % 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)) + doorify_layout (maze, RP); + break; + + case LAYOUT_SPIRAL: + maze = map_gen_spiral (RP->Xsize, RP->Ysize, RP->layoutoptions1); + if (!(RANDOM () % 2)) + doorify_layout (maze, RP); + break; + + case LAYOUT_ROGUELIKE: + /* Don't put symmetry in rogue maps. There isn't much reason to + * do so in the first place (doesn't make it any more interesting), + * but more importantly, the symmetry code presumes we are symmetrizing + * spirals, or maps with lots of passages - making a symmetric rogue + * map fails because its likely that the passages the symmetry process + * creates may not connect the rooms. + */ + RP->symmetry_used = SYMMETRY_NONE; + RP->Ysize = oysize; + RP->Xsize = oxsize; + maze = roguelike_layout_gen (RP->Xsize, RP->Ysize, RP->layoutoptions1); + /* no doorifying... done already */ + break; + + case LAYOUT_SNAKE: + maze = make_snake_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); + if (RANDOM () % 2) + roomify_layout (maze, RP); + break; + + case LAYOUT_SQUARE_SPIRAL: + maze = make_square_spiral_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); + if (RANDOM () % 2) + roomify_layout (maze, RP); + break; } maze = symmetrize_layout (maze, RP->symmetry_used, RP); + #ifdef RMAP_DEBUG dump_layout (maze, RP); #endif + if (RP->expand2x) { maze = expand2x (maze, RP->Xsize, RP->Ysize); RP->Xsize = RP->Xsize * 2 - 1; RP->Ysize = RP->Ysize * 2 - 1; } + return maze; } - /* takes a map and makes it symmetric: adjusts Xsize and Ysize to produce a symmetric map. */ - char ** -symmetrize_layout (char **maze, int sym, RMParms * RP) +symmetrize_layout (char **maze, int sym, random_map_params *RP) { int i, j; char **sym_maze; @@ -307,35 +301,35 @@ Xsize_orig = RP->Xsize; Ysize_orig = RP->Ysize; RP->symmetry_used = sym; /* tell everyone else what sort of symmetry is used. */ - if (sym == NO_SYM) + if (sym == SYMMETRY_NONE) { RP->Xsize = Xsize_orig; RP->Ysize = Ysize_orig; return maze; } /* pick new sizes */ - RP->Xsize = ((sym == X_SYM || sym == XY_SYM) ? RP->Xsize * 2 - 3 : RP->Xsize); - RP->Ysize = ((sym == Y_SYM || sym == XY_SYM) ? RP->Ysize * 2 - 3 : RP->Ysize); + RP->Xsize = ((sym == SYMMETRY_X || sym == SYMMETRY_XY) ? RP->Xsize * 2 - 3 : RP->Xsize); + RP->Ysize = ((sym == SYMMETRY_Y || sym == SYMMETRY_XY) ? RP->Ysize * 2 - 3 : RP->Ysize); sym_maze = (char **) calloc (sizeof (char *), RP->Xsize); for (i = 0; i < RP->Xsize; i++) sym_maze[i] = (char *) calloc (sizeof (char), RP->Ysize); - if (sym == X_SYM) + if (sym == SYMMETRY_X) for (i = 0; i < RP->Xsize / 2 + 1; i++) for (j = 0; j < RP->Ysize; j++) { sym_maze[i][j] = maze[i][j]; sym_maze[RP->Xsize - i - 1][j] = maze[i][j]; }; - if (sym == Y_SYM) + if (sym == SYMMETRY_Y) for (i = 0; i < RP->Xsize; i++) for (j = 0; j < RP->Ysize / 2 + 1; j++) { sym_maze[i][j] = maze[i][j]; sym_maze[i][RP->Ysize - j - 1] = maze[i][j]; } - if (sym == XY_SYM) + if (sym == SYMMETRY_XY) for (i = 0; i < RP->Xsize / 2 + 1; i++) for (j = 0; j < RP->Ysize / 2 + 1; j++) { @@ -349,11 +343,11 @@ free (maze[i]); free (maze); /* reconnect disjointed spirals */ - if (RP->map_layout_style == SPIRAL_LAYOUT) + if (RP->map_layout_style == LAYOUT_SPIRAL) connect_spirals (RP->Xsize, RP->Ysize, sym, sym_maze); /* reconnect disjointed nethackmazes: the routine for spirals will do the trick? */ - if (RP->map_layout_style == ROGUELIKE_LAYOUT) + if (RP->map_layout_style == LAYOUT_ROGUELIKE) connect_spirals (RP->Xsize, RP->Ysize, sym, sym_maze); return sym_maze; @@ -366,70 +360,70 @@ */ char ** -rotate_layout (char **maze, int rotation, RMParms * RP) +rotate_layout (char **maze, int rotation, random_map_params *RP) { char **new_maze; int i, j; switch (rotation) { - case 0: + case 0: + return maze; + break; + case 2: /* a reflection */ + { + char *newmaze = (char *) malloc (sizeof (char) * RP->Xsize * RP->Ysize); + + for (i = 0; i < RP->Xsize; i++) + { /* make a copy */ + for (j = 0; j < RP->Ysize; j++) + { + newmaze[i * RP->Ysize + j] = maze[i][j]; + } + } + for (i = 0; i < RP->Xsize; i++) + { /* copy a reflection back */ + for (j = 0; j < RP->Ysize; j++) + { + maze[i][j] = newmaze[(RP->Xsize - i - 1) * RP->Ysize + RP->Ysize - j - 1]; + } + } + free (newmaze); return maze; break; - case 2: /* a reflection */ - { - char *newmaze = (char *) malloc (sizeof (char) * RP->Xsize * RP->Ysize); - - for (i = 0; i < RP->Xsize; i++) - { /* make a copy */ - for (j = 0; j < RP->Ysize; j++) - { - newmaze[i * RP->Ysize + j] = maze[i][j]; - } - } + } + case 1: + case 3: + { + int swap; + new_maze = (char **) calloc (sizeof (char *), RP->Ysize); + for (i = 0; i < RP->Ysize; i++) + { + new_maze[i] = (char *) calloc (sizeof (char), RP->Xsize); + } + if (rotation == 1) /* swap x and y */ for (i = 0; i < RP->Xsize; i++) - { /* copy a reflection back */ - for (j = 0; j < RP->Ysize; j++) - { - maze[i][j] = newmaze[(RP->Xsize - i - 1) * RP->Ysize + RP->Ysize - j - 1]; - } - } - free (newmaze); - return maze; - break; - } - case 1: - case 3: - { - int swap; - new_maze = (char **) calloc (sizeof (char *), RP->Ysize); - for (i = 0; i < RP->Ysize; i++) - { - new_maze[i] = (char *) calloc (sizeof (char), RP->Xsize); - } - if (rotation == 1) /* swap x and y */ + for (j = 0; j < RP->Ysize; j++) + new_maze[j][i] = maze[i][j]; + + if (rotation == 3) + { /* swap x and y */ for (i = 0; i < RP->Xsize; i++) for (j = 0; j < RP->Ysize; j++) - new_maze[j][i] = maze[i][j]; - - if (rotation == 3) - { /* swap x and y */ - for (i = 0; i < RP->Xsize; i++) - for (j = 0; j < RP->Ysize; j++) - new_maze[j][i] = maze[RP->Xsize - i - 1][RP->Ysize - j - 1]; - } + new_maze[j][i] = maze[RP->Xsize - i - 1][RP->Ysize - j - 1]; + } - /* delete the old layout */ - for (i = 0; i < RP->Xsize; i++) - free (maze[i]); - free (maze); - - swap = RP->Ysize; - RP->Ysize = RP->Xsize; - RP->Xsize = swap; - return new_maze; - break; - } + /* delete the old layout */ + for (i = 0; i < RP->Xsize; i++) + free (maze[i]); + free (maze); + + swap = RP->Ysize; + RP->Ysize = RP->Xsize; + RP->Xsize = swap; + return new_maze; + break; + } } return NULL; } @@ -437,7 +431,7 @@ /* take a layout and make some rooms in it. --works best on onions.*/ void -roomify_layout (char **maze, RMParms * RP) +roomify_layout (char **maze, random_map_params *RP) { int tries = RP->Xsize * RP->Ysize / 30; int ti; @@ -474,7 +468,7 @@ here which ends up on other walls sensibly. */ int -can_make_wall (char **maze, int dx, int dy, int dir, RMParms * RP) +can_make_wall (char **maze, int dx, int dy, int dir, random_map_params *RP) { int i1; int length = 0; @@ -559,35 +553,34 @@ maze[x][y] = 'D'; /* mark a door */ switch (dir) { - case 0: /* horizontal */ - { - int i1; + case 0: /* horizontal */ + { + int i1; - for (i1 = x - 1; maze[i1][y] == 0; i1--) - maze[i1][y] = '#'; - for (i1 = x + 1; maze[i1][y] == 0; i1++) - maze[i1][y] = '#'; - break; - } - case 1: /* vertical */ - { - int i1; + for (i1 = x - 1; maze[i1][y] == 0; i1--) + maze[i1][y] = '#'; + for (i1 = x + 1; maze[i1][y] == 0; i1++) + maze[i1][y] = '#'; + break; + } + case 1: /* vertical */ + { + int i1; - for (i1 = y - 1; maze[x][i1] == 0; i1--) - maze[x][i1] = '#'; - for (i1 = y + 1; maze[x][i1] == 0; i1++) - maze[x][i1] = '#'; - break; - } + for (i1 = y - 1; maze[x][i1] == 0; i1--) + maze[x][i1] = '#'; + for (i1 = y + 1; maze[x][i1] == 0; i1++) + maze[x][i1] = '#'; + break; + } } return 0; } /* puts doors at appropriate locations in a layout. */ - void -doorify_layout (char **maze, RMParms * RP) +doorify_layout (char **maze, random_map_params *RP) { int ndoors = RP->Xsize * RP->Ysize / 60; /* reasonable number of doors. */ char *doorlist_x; @@ -612,6 +605,7 @@ doorlocs++; } } + while (ndoors > 0 && doorlocs > 0) { int di; @@ -631,16 +625,15 @@ doorlist_x[di] = doorlist_x[doorlocs]; doorlist_y[di] = doorlist_y[doorlocs]; } + free (doorlist_x); free (doorlist_y); } - void -write_map_parameters_to_string (char *buf, RMParms * RP) +write_map_parameters_to_string (char *buf, random_map_params *RP) { - - char small_buf[256]; + char small_buf[2048]; sprintf (buf, "xsize %d\nysize %d\n", RP->Xsize, RP->Ysize); @@ -692,9 +685,9 @@ strcat (buf, small_buf); } - if (RP->final_map[0]) + if (RP->final_map.length ()) { - sprintf (small_buf, "final_map %s\n", RP->final_map); + sprintf (small_buf, "final_map %s\n", &RP->final_map); strcat (buf, small_buf); } @@ -704,9 +697,9 @@ strcat (buf, small_buf); } - if (RP->this_map[0]) + if (RP->this_map.length ()) { - sprintf (small_buf, "origin_map %s\n", RP->this_map); + sprintf (small_buf, "origin_map %s\n", &RP->this_map); strcat (buf, small_buf); } @@ -722,14 +715,12 @@ strcat (buf, small_buf); } - if (RP->layoutoptions2) { sprintf (small_buf, "layoutoptions2 %d\n", RP->layoutoptions2); strcat (buf, small_buf); } - if (RP->layoutoptions3) { sprintf (small_buf, "layoutoptions3 %d\n", RP->layoutoptions3); @@ -742,7 +733,6 @@ strcat (buf, small_buf); } - if (RP->difficulty && RP->difficulty_given) { sprintf (small_buf, "difficulty %d\n", RP->difficulty); @@ -787,6 +777,7 @@ sprintf (small_buf, "origin_y %d\n", RP->origin_y); strcat (buf, small_buf); } + if (RP->random_seed) { /* Add one so that the next map is a bit different */ @@ -800,7 +791,11 @@ strcat (buf, small_buf); } - + if (RP->random_seed) + { + sprintf (small_buf, "random_seed %d\n", RP->random_seed); + strcat (buf, small_buf); + } } void @@ -830,8 +825,7 @@ int orientation_n, int origin_x_n, int origin_y_n, int random_seed_n, int treasureoptions_n, float difficulty_increase) { - - char small_buf[256]; + char small_buf[2048]; sprintf (buf, "xsize %d\nysize %d\n", xsize_n, ysize_n); @@ -907,7 +901,6 @@ strcat (buf, small_buf); } - if (layoutoptions2_n) { sprintf (small_buf, "layoutoptions2 %d\n", layoutoptions2_n); @@ -972,6 +965,7 @@ sprintf (small_buf, "origin_y %d\n", origin_y_n); strcat (buf, small_buf); } + if (random_seed_n) { /* Add one so that the next map is a bit different */ @@ -984,8 +978,6 @@ sprintf (small_buf, "treasureoptions %d\n", treasureoptions_n); strcat (buf, small_buf); } - - } /* copy an object with an inventory... i.e., duplicate the inv too. */ @@ -996,9 +988,10 @@ src_ob->copy_to (dest_ob); - for (walk = src_ob->inv; walk != NULL; walk = walk->below) + for (walk = src_ob->inv; walk; walk = walk->below) { tmp = object::create (); + walk->copy_to (tmp); insert_ob_in_ob (tmp, dest_ob); }