--- deliantra/server/random_maps/treasure.C 2010/04/13 02:39:53 1.53 +++ deliantra/server/random_maps/treasure.C 2011/05/01 16:58:16 1.61 @@ -1,7 +1,7 @@ /* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * - * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2005,2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team * Copyright (©) 2001 Mark Wedel & Crossfire Development Team * Copyright (©) 1992 Frank Tore Johansen * @@ -25,7 +25,7 @@ /* placing treasure in maps, where appropriate. */ #include -#include +#include #include /* some defines for various options which can be set. */ @@ -43,15 +43,14 @@ #define NO_PASS_DOORS 0 #define PASS_DOORS 1 -static object *find_closest_monster (maptile *map, int x, int y, random_map_params *RP); -static object *find_monster_in_room (maptile *map, int x, int y, random_map_params *RP); -static void find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP); -static void find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky, random_map_params *RP); +static object *find_closest_monster (maptile *map, int x, int y); +static object *find_monster_in_room (maptile *map, int x, int y); +static void find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky); static object *place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params *RP); -static object **find_doors_in_room (maptile *map, int x, int y, random_map_params *RP); +static object **find_doors_in_room (maptile *map, int x, int y); static void lock_and_hide_doors (object **doorlist, maptile *map, int opts, random_map_params *RP); -static void find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP); -static object **surround_by_doors (maptile *map, char **layout, int x, int y, int opts); +static void find_enclosed_spot (maptile *map, int *cx, int *cy); +static object **surround_by_doors (maptile *map, char **maze, int x, int y, int opts); /* a macro to get a strongly centered random distribution, from 0 to x, centered at x/2 */ @@ -81,7 +80,7 @@ sure a key is placed on both sides of the door. */ static int -keyplace (maptile *map, int x, int y, const shstr &keycode, int door_flag, int n_keys, random_map_params *RP) +keyplace (maptile *map, int x, int y, const shstr &keycode, int door_flag, int n_keys) { int i, j; int kx = 0, ky = 0; @@ -95,10 +94,10 @@ the_keymaster = 0; while (tries < 15 && !the_keymaster) { - i = rmg_rndm (RP->Xsize - 2) + 1; - j = rmg_rndm (RP->Ysize - 2) + 1; + i = rmg_rndm (map->width - 2) + 1; + j = rmg_rndm (map->height - 2) + 1; tries++; - the_keymaster = find_closest_monster (map, i, j, RP); + the_keymaster = find_closest_monster (map, i, j); } /* if we don't find a good keymaster, drop the key on the ground. */ @@ -109,8 +108,8 @@ freeindex = -1; for (tries = 0; tries < 15 && freeindex == -1; tries++) { - kx = rmg_rndm (RP->Xsize - 2) + 1; - ky = rmg_rndm (RP->Ysize - 2) + 1; + kx = rmg_rndm (map->width - 2) + 1; + ky = rmg_rndm (map->height - 2) + 1; freeindex = rmg_find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1); } @@ -134,26 +133,26 @@ return 0; } - the_keymaster = find_monster_in_room (map, x, y, RP); + the_keymaster = find_monster_in_room (map, x, y); if (!the_keymaster) /* if fail, find a spot to drop the key. */ - find_spot_in_room (map, x, y, &kx, &ky, RP); + find_spot_in_room (map, x, y, &kx, &ky); } else { int sum = 0; /* count how many keys we actually place */ /* I'm lazy, so just try to place in all 4 directions. */ - sum += keyplace (map, x + 1, y, keycode, NO_PASS_DOORS, 1, RP); - sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1, RP); - sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1, RP); - sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1, RP); + sum += keyplace (map, x + 1, y, keycode, NO_PASS_DOORS, 1); + sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1); + sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1); + sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1); if (sum < 2) /* we might have made a disconnected map-place more keys. */ { /* diagonally this time. */ - keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); - keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); - keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1, RP); - keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1, RP); + keyplace (map, x + 1, y + 1, keycode, NO_PASS_DOORS, 1); + keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1); + keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1); + keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1); } the_key->destroy (); @@ -189,12 +188,12 @@ /* place treasures in the map, given the map, (required) -layout, (required) +maze, (required) treasure style (may be empty or NULL, or "none" to cause no treasure.) treasureoptions (may be 0 for random choices or positive) */ void -place_treasure (maptile *map, char **layout, char *treasure_style, int treasureoptions, random_map_params *RP) +place_treasure (maptile *map, layout &maze, const char *treasure_style, int treasureoptions, random_map_params *RP) { int num_treasures; @@ -252,7 +251,7 @@ { for (j = 0; j < RP->Ysize; j++) { - if (layout[i][j] == 'C' || layout[i][j] == '>') + if (maze[i][j] == 'C' || maze[i][j] == '>') { int tdiv = RP->symmetry_used; object *chest; @@ -268,7 +267,7 @@ if (treasureoptions & (DOORED | HIDDEN)) { - object **doorlist = find_doors_in_room (map, i, j, RP); + object **doorlist = find_doors_in_room (map, i, j); lock_and_hide_doors (doorlist, map, treasureoptions, RP); free (doorlist); } @@ -290,7 +289,8 @@ { i = rmg_rndm (RP->Xsize - 2) + 1; j = rmg_rndm (RP->Ysize - 2) + 1; - find_enclosed_spot (map, &i, &j, RP); + + find_enclosed_spot (map, &i, &j); if (wall_blocked (map, i, j)) i = -1; @@ -307,7 +307,7 @@ j = chest->y; if (treasureoptions & (DOORED | HIDDEN)) { - doorlist = surround_by_doors (map, layout, i, j, treasureoptions); + doorlist = surround_by_doors (map, maze, i, j, treasureoptions); lock_and_hide_doors (doorlist, map, treasureoptions, RP); free (doorlist); } @@ -315,7 +315,7 @@ } } else - { /* DIFFUSE treasure layout */ + { /* DIFFUSE treasure maze */ int ti, i, j; for (ti = 0; ti < num_treasures; ti++) @@ -407,7 +407,7 @@ if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) { the_chest->slaying = format ("RMG-%d-%d", (int)rmg_rndm (1000000000), (int)rmg_rndm (1000000000)); - keyplace (map, x, y, the_chest->slaying, PASS_DOORS, 1, RP); + keyplace (map, x, y, the_chest->slaying, PASS_DOORS, 1); } /* actually place the chest. */ @@ -419,7 +419,7 @@ /* finds the closest monster and returns him, regardless of doors or walls */ static object * -find_closest_monster (maptile *map, int x, int y, random_map_params *RP) +find_closest_monster (maptile *map, int x, int y) { int i; @@ -430,13 +430,15 @@ lx = x + freearr_x[i]; ly = y + freearr_y[i]; /* boundscheck */ - if (lx >= 0 && ly >= 0 && lx < RP->Xsize && ly < RP->Ysize) + if (lx >= 0 && ly >= 0 && lx < map->width && ly < map->height) /* don't bother searching this square unless the map says life exists. */ if (GET_MAP_FLAGS (map, lx, ly) & P_IS_ALIVE) { object *the_monster = GET_MAP_OB (map, lx, ly); - for (; the_monster != NULL && (!the_monster->flag [FLAG_MONSTER]); the_monster = the_monster->above); + for (; the_monster && !the_monster->flag [FLAG_MONSTER]; the_monster = the_monster->above) + ; + if (the_monster && the_monster->flag [FLAG_MONSTER]) return the_monster; } @@ -449,9 +451,9 @@ static object *theMonsterToFind; /* a recursive routine which will return a monster, eventually,if there is one. - it does a check-off on the layout, converting 0's to 1's */ + it does a check-off on the maze, converting 0's to 1's */ static object * -find_monster_in_room_recursive (char **layout, maptile *map, int x, int y, random_map_params *RP) +find_monster_in_room_recursive (layout &maze, maptile *map, int x, int y) { int i, j; @@ -460,22 +462,22 @@ return theMonsterToFind; /* bounds check x and y */ - if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) + if (!(x >= 0 && y >= 0 && x < maze.w && y < maze.h)) return theMonsterToFind; /* if the square is blocked or searched already, leave */ - if (layout[x][y] != 0) + if (maze[x][y] != 0) return theMonsterToFind; /* might be NULL, that's fine. */ /* check the current square for a monster. If there is one, set theMonsterToFind and return it. */ - layout[x][y] = 1; + maze[x][y] = 1; if (GET_MAP_FLAGS (map, x, y) & P_IS_ALIVE) { object *the_monster = GET_MAP_OB (map, x, y); /* check off this point */ - for (; the_monster != NULL && (!the_monster->flag [FLAG_ALIVE]); the_monster = the_monster->above); + for (; the_monster && (!the_monster->flag [FLAG_ALIVE]); the_monster = the_monster->above); if (the_monster && the_monster->flag [FLAG_ALIVE]) { theMonsterToFind = the_monster; @@ -484,10 +486,10 @@ } /* now search all the 8 squares around recursively for a monster,in random order */ - for (i = rmg_rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) + for (i = rmg_rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++) { - theMonsterToFind = find_monster_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); - if (theMonsterToFind != NULL) + theMonsterToFind = find_monster_in_room_recursive (maze, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1]); + if (theMonsterToFind) return theMonsterToFind; } @@ -497,107 +499,77 @@ /* sets up some data structures: the _recursive form does the real work. */ static object * -find_monster_in_room (maptile *map, int x, int y, random_map_params *RP) +find_monster_in_room (maptile *map, int x, int y) { - Layout layout2 (RP); - - layout2->clear (); + layout layout2 (map->width, map->height); - /* allocate and copy the layout, converting C to 0. */ - for (int i = 0; i < layout2->w; i++) - for (int j = 0; j < layout2->h; j++) - if (wall_blocked (map, i, j)) - layout2[i][j] = '#'; + // find walls + for (int i = 0; i < layout2.w; i++) + for (int j = 0; j < layout2.h; j++) + layout2[i][j] = wall_blocked (map, i, j) ? '#' : 0; theMonsterToFind = 0; - theMonsterToFind = find_monster_in_room_recursive (layout2, map, x, y, RP); - - layout2.free (); + theMonsterToFind = find_monster_in_room_recursive (layout2, map, x, y); return theMonsterToFind; } -/* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */ -static int *room_free_spots_x; -static int *room_free_spots_y; -static int number_of_free_spots_in_room; - /* the workhorse routine, which finds the free spots in a room: a datastructure of free points is set up, and a position chosen from that datastructure. */ static void -find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP) +find_spot_in_room_recursive (layout &maze, fixed_stack &spots, int x, int y) { - int i, j; - /* bounds check x and y */ - if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) + if (!(x >= 0 && y >= 0 && x < maze.w && y < maze.h)) return; /* if the square is blocked or searched already, leave */ - if (layout[x][y] != 0) + if (maze[x][y] != 0) return; /* set the current square as checked, and add it to the list. set theMonsterToFind and return it. */ /* check off this point */ - layout[x][y] = 1; - room_free_spots_x[number_of_free_spots_in_room] = x; - room_free_spots_y[number_of_free_spots_in_room] = y; - number_of_free_spots_in_room++; + maze[x][y] = 1; + spots.push (point (x, y)); /* now search all the 8 squares around recursively for free spots,in random order */ - for (i = rmg_rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) - find_spot_in_room_recursive (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); + for (int i = rmg_rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++) + find_spot_in_room_recursive (maze, spots, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1]); } /* find a random non-blocked spot in this room to drop a key. */ static void -find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky, random_map_params *RP) +find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky) { - char **layout2; - int i, j; + fixed_stack spots (map->width * map->height); - number_of_free_spots_in_room = 0; - room_free_spots_x = (int *) calloc (sizeof (int), RP->Xsize * RP->Ysize); - room_free_spots_y = (int *) calloc (sizeof (int), RP->Xsize * RP->Ysize); - - layout2 = (char **) calloc (sizeof (char *), RP->Xsize); - /* allocate and copy the layout, converting C to 0. */ - for (i = 0; i < RP->Xsize; i++) - { - layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); - for (j = 0; j < RP->Ysize; j++) - if (wall_blocked (map, i, j)) - layout2[i][j] = '#'; - } + layout layout2 (map->width, map->height); + + /* allocate and copy the maze, converting C to 0. */ + for (int i = 0; i < map->width; i++) + for (int j = 0; j < map->height; j++) + layout2 [i][j] = wall_blocked (map, i, j) ? '#' : 0; /* setup num_free_spots and room_free_spots */ - find_spot_in_room_recursive (layout2, x, y, RP); + find_spot_in_room_recursive (layout2, spots, x, y); - if (number_of_free_spots_in_room > 0) + if (spots.size) { - i = rmg_rndm (number_of_free_spots_in_room); - *kx = room_free_spots_x[i]; - *ky = room_free_spots_y[i]; - } - - /* deallocate the temp. layout */ - for (i = 0; i < RP->Xsize; i++) - free (layout2[i]); + point p = spots [rmg_rndm (spots.size)]; - free (layout2); - free (room_free_spots_x); - free (room_free_spots_y); + *kx = p.x; + *ky = p.y; + } } - /* searches the map for a spot with walls around it. The more walls the better, but it'll settle for 1 wall, or even 0, but it'll return 0 if no FREE spots are found.*/ static void -find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP) +find_enclosed_spot (maptile *map, int *cx, int *cy) { int x, y; int i; @@ -611,7 +583,7 @@ lx = x + freearr_x[i]; ly = y + freearr_y[i]; - sindex = surround_flag3 (map, lx, ly, RP); + sindex = surround_flag3 (map, lx, ly); /* if it's blocked on 3 sides, it's enclosed */ if (sindex == 7 || sindex == 11 || sindex == 13 || sindex == 14) { @@ -629,7 +601,7 @@ lx = x + freearr_x[i]; ly = y + freearr_y[i]; - sindex = surround_flag3 (map, lx, ly, RP); + sindex = surround_flag3 (map, lx, ly); /* if it's blocked on 3 sides, it's enclosed */ if (sindex == 3 || sindex == 5 || sindex == 9 || sindex == 6 || sindex == 10 || sindex == 12) { @@ -646,7 +618,7 @@ lx = x + freearr_x[i]; ly = y + freearr_y[i]; - sindex = surround_flag3 (map, lx, ly, RP); + sindex = surround_flag3 (map, lx, ly); /* if it's blocked on 3 sides, it's enclosed */ if (sindex) { @@ -689,7 +661,7 @@ a chest. It only goes as far as the 8 squares surrounding, and it'll remove any monsters it finds.*/ static object ** -surround_by_doors (maptile *map, char **layout, int x, int y, int opts) +surround_by_doors (maptile *map, char **maze, int x, int y, int opts) { int i; const char *doors[2]; @@ -714,11 +686,11 @@ { int x1 = x + freearr_x[i], y1 = y + freearr_y[i]; - if (!wall_blocked (map, x1, y1) && layout[x1][y1] == '>') + if (!wall_blocked (map, x1, y1) && maze[x1][y1] == '>') { /* place a door */ remove_monsters (x1, y1, map); - object *new_door = get_archetype (freearr_x[i] == 0 ? doors[1] : doors[0]); + object *new_door = archetype::get (freearr_x[i] == 0 ? doors[1] : doors[0]); map->insert (new_door, x1, y1); doorlist[ndoors_made] = new_door; ndoors_made++; @@ -741,23 +713,23 @@ /* the workhorse routine, which finds the doors in a room */ static void -find_doors_in_room_recursive (char **layout, maptile *map, int x, int y, object **doorlist, int *ndoors, random_map_params *RP) +find_doors_in_room_recursive (layout &maze, maptile *map, int x, int y, object **doorlist, int *ndoors) { int i, j; object *door; /* bounds check x and y */ - if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) + if (!(x >= 0 && y >= 0 && x < maze.w && y < maze.h)) return; /* if the square is blocked or searched already, leave */ - if (layout[x][y] == 1) + if (maze[x][y] == 1) return; /* check off this point */ - if (layout[x][y] == '#') + if (maze[x][y] == '#') { /* there could be a door here */ - layout[x][y] = 1; + maze[x][y] = 1; door = door_in_square (map, x, y); if (door) { @@ -774,35 +746,35 @@ } else { - layout[x][y] = 1; + maze[x][y] = 1; /* now search all the 8 squares around recursively for free spots,in random order */ for (i = rmg_rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++) - find_doors_in_room_recursive (layout, map, + find_doors_in_room_recursive (maze, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], - doorlist, ndoors, RP); + doorlist, ndoors); } } /* find a random non-blocked spot in this room to drop a key. */ static object ** -find_doors_in_room (maptile *map, int x, int y, random_map_params *RP) +find_doors_in_room (maptile *map, int x, int y) { int i, j; int ndoors = 0; object **doorlist = (object **)calloc (sizeof (int), 1024); - LayoutData layout2 (RP->Xsize, RP->Ysize); + layout layout2 (map->width, map->height); layout2.clear (); - /* allocate and copy the layout, converting C to 0. */ - for (i = 0; i < RP->Xsize; i++) - for (j = 0; j < RP->Ysize; j++) + /* allocate and copy the maze, converting C to 0. */ + for (i = 0; i < map->width; i++) + for (j = 0; j < map->height; j++) layout2[i][j] = wall_blocked (map, i, j) ? '#' : 0; /* setup num_free_spots and room_free_spots */ - find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors, RP); + find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors); return doorlist; } @@ -821,7 +793,7 @@ { for (i = 0, door = doorlist[0]; doorlist[i]; i++) { - object *new_door = get_archetype (shstr_locked_door1); + object *new_door = archetype::get (shstr_locked_door1); door = doorlist[i]; new_door->face = door->face; @@ -831,7 +803,7 @@ doorlist[i] = new_door; insert_ob_in_map (new_door, map, NULL, 0); new_door->slaying = format ("RMG-%d-%d", (int)rmg_rndm (1000000000), (int)rmg_rndm (1000000000)); - keyplace (map, new_door->x, new_door->y, new_door->slaying, NO_PASS_DOORS, 2, RP); + keyplace (map, new_door->x, new_door->y, new_door->slaying, NO_PASS_DOORS, 2); } }