--- deliantra/server/random_maps/treasure.C 2008/07/24 20:35:37 1.41 +++ deliantra/server/random_maps/treasure.C 2010/07/02 15:03:57 1.57 @@ -1,22 +1,23 @@ /* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * - * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team - * Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team - * Copyright (©) 1992,2007 Frank Tore Johansen + * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2001 Mark Wedel & Crossfire Development Team + * Copyright (©) 1992 Frank Tore Johansen * - * Deliantra 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 3 of the License, or - * (at your option) any later version. + * Deliantra is free software: you can redistribute it and/or modify it under + * the terms of the Affero GNU General Public License as published by the + * Free Software Foundation, either version 3 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, see . + * You should have received a copy of the Affero GNU General Public License + * and the GNU General Public License along with this program. If not, see + * . * * The authors can be reached via e-mail to */ @@ -42,6 +43,16 @@ #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 **maze, 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 *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 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 **maze, int x, int y, int opts); + /* a macro to get a strongly centered random distribution, from 0 to x, centered at x/2 */ static int @@ -100,7 +111,7 @@ { kx = rmg_rndm (RP->Xsize - 2) + 1; ky = rmg_rndm (RP->Ysize - 2) + 1; - freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1); + freeindex = rmg_find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1); } // can freeindex ever be < 0? @@ -150,7 +161,6 @@ } } - LOG(llevError, "key %d,%d %p\n", the_keymaster ? the_keymaster->x : x, the_keymaster ? the_keymaster->y : y, the_keymaster+0);//D if (the_keymaster) the_keymaster->head_ ()->insert (the_key); else @@ -179,16 +189,13 @@ /* 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, char **maze, const char *treasure_style, int treasureoptions, random_map_params *RP) { - char styledirname[1024]; - char stylefilepath[1024]; - maptile *style_map = 0; int num_treasures; /* bail out if treasure isn't wanted. */ @@ -220,13 +227,11 @@ return; /* get the style map */ - sprintf (styledirname, "%s", "/styles/treasurestyles"); - sprintf (stylefilepath, "%s/%s", styledirname, treasure_style); - style_map = find_style (styledirname, treasure_style, -1); + maptile *style_map = find_style ("/styles/treasurestyles", treasure_style, RP->difficulty); if (!style_map) { - LOG (llevError, "unable to load style map %s %s.\n", styledirname, treasure_style); + LOG (llevError, "unable to load style map %s %s.\n", "/styles/treasurestyles", treasure_style); return; } @@ -247,7 +252,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; @@ -302,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); } @@ -310,7 +315,7 @@ } } else - { /* DIFFUSE treasure layout */ + { /* DIFFUSE treasure maze */ int ti, i, j; for (ti = 0; ti < num_treasures; ti++) @@ -325,7 +330,7 @@ /* put a chest into the map, near x and y, with the treasure style determined (may be null, or may be a treasure list from lib/treasures, if the global variable "treasurestyle" is set to that treasure list's name */ -object * +static object * place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params *RP) { object *the_chest = archetype::get (shstr_chest); /* was "chest_2" */ @@ -358,7 +363,7 @@ { /* use the treasure list */ object *new_treasure = style_map->pick_random_object (rmg_rndm); - insert_ob_in_ob (arch_to_object (new_treasure->arch), the_chest); + insert_ob_in_ob (new_treasure->arch->instance (), the_chest); } else { /* use the style map */ @@ -376,7 +381,7 @@ /* stick a trap in the chest if required */ if (treasureoptions & TRAPPED) { - maptile *trap_map = find_style ("/styles/trapstyles", "traps", -1); + maptile *trap_map = find_style ("/styles/trapstyles", "traps", RP->difficulty); if (trap_map) { @@ -412,10 +417,8 @@ return the_chest; } - -/* finds the closest monster and returns him, regardless of doors - or walls */ -object * +/* 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) { int i; @@ -433,8 +436,8 @@ { object *the_monster = GET_MAP_OB (map, lx, ly); - for (; the_monster != NULL && (!QUERY_FLAG (the_monster, FLAG_MONSTER)); the_monster = the_monster->above); - if (the_monster && QUERY_FLAG (the_monster, FLAG_MONSTER)) + for (; the_monster != NULL && (!the_monster->flag [FLAG_MONSTER]); the_monster = the_monster->above); + if (the_monster && the_monster->flag [FLAG_MONSTER]) return the_monster; } } @@ -443,13 +446,12 @@ /* both find_monster_in_room routines need to have access to this. */ -object *theMonsterToFind; +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 */ - -object * -find_monster_in_room_recursive (char **layout, maptile *map, int x, int y, random_map_params *RP) + it does a check-off on the maze, converting 0's to 1's */ +static object * +find_monster_in_room_recursive (char **maze, maptile *map, int x, int y, random_map_params *RP) { int i, j; @@ -462,19 +464,19 @@ 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 && (!QUERY_FLAG (the_monster, FLAG_ALIVE)); the_monster = the_monster->above); - if (the_monster && QUERY_FLAG (the_monster, FLAG_ALIVE)) + for (; the_monster != NULL && (!the_monster->flag [FLAG_ALIVE]); the_monster = the_monster->above); + if (the_monster && the_monster->flag [FLAG_ALIVE]) { theMonsterToFind = the_monster; return theMonsterToFind; @@ -484,7 +486,7 @@ /* 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++) { - theMonsterToFind = find_monster_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); + theMonsterToFind = find_monster_in_room_recursive (maze, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); if (theMonsterToFind != NULL) return theMonsterToFind; } @@ -494,37 +496,32 @@ /* sets up some data structures: the _recursive form does the real work. */ -object * +static object * find_monster_in_room (maptile *map, int x, int y, random_map_params *RP) { - Layout layout2 (RP); + layout layout2 (map->width, map->height); - layout2->clear (); - - /* 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 (); - return theMonsterToFind; } /* a datastructure needed by find_spot_in_room and find_spot_in_room_recursive */ -int *room_free_spots_x; -int *room_free_spots_y; -int number_of_free_spots_in_room; +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. */ -void -find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP) +static void +find_spot_in_room_recursive (char **maze, int x, int y, random_map_params *RP) { int i, j; @@ -533,25 +530,25 @@ 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; + maze[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++; /* 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); + find_spot_in_room_recursive (maze, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); } /* find a random non-blocked spot in this room to drop a key. */ -void +static void find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky, random_map_params *RP) { char **layout2; @@ -562,7 +559,7 @@ 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. */ + /* allocate and copy the maze, converting C to 0. */ for (i = 0; i < RP->Xsize; i++) { layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); @@ -581,7 +578,7 @@ *ky = room_free_spots_y[i]; } - /* deallocate the temp. layout */ + /* deallocate the temp. maze */ for (i = 0; i < RP->Xsize; i++) free (layout2[i]); @@ -594,7 +591,7 @@ /* 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.*/ -void +static void find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP) { int x, y; @@ -609,7 +606,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) { @@ -627,7 +624,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) { @@ -644,7 +641,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) { @@ -654,7 +651,7 @@ } } /* give up and return the closest free spot. */ - i = find_free_spot (archetype::find (shstr_chest), map, x, y, 1, SIZEOFFREE1 + 1); + i = rmg_find_free_spot (archetype::find (shstr_chest), map, x, y, 1, SIZEOFFREE1 + 1); if (i != -1) { @@ -669,29 +666,25 @@ } } -void +static void remove_monsters (int x, int y, maptile *map) { - object *tmp; + for (object *tmp = GET_MAP_OB (map, x, y); tmp; ) + { + object *next = tmp->above; + + if (tmp->flag [FLAG_ALIVE]) + tmp->head_ ()->destroy (); - for (tmp = GET_MAP_OB (map, x, y); tmp; tmp = tmp->above) - if (QUERY_FLAG (tmp, FLAG_ALIVE)) - { - if (tmp->head) - tmp = tmp->head; - tmp->remove (); - tmp->destroy (); - tmp = GET_MAP_OB (map, x, y); - if (tmp == NULL) - break; - }; + tmp = next; + } } /* surrounds the point x,y by doors, so as to enclose something, like a chest. It only goes as far as the 8 squares surrounding, and it'll remove any monsters it finds.*/ -object ** -surround_by_doors (maptile *map, char **layout, int x, int y, int opts) +static object ** +surround_by_doors (maptile *map, char **maze, int x, int y, int opts) { int i; const char *doors[2]; @@ -716,7 +709,7 @@ { 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); @@ -730,22 +723,20 @@ return doorlist; } - /* returns the first door in this square, or NULL if there isn't a door. */ -object * +static object * door_in_square (maptile *map, int x, int y) { - object *tmp; - - for (tmp = GET_MAP_OB (map, x, y); tmp != NULL; tmp = tmp->above) + for (object *tmp = GET_MAP_OB (map, x, y); tmp; tmp = tmp->above) if (tmp->type == DOOR || tmp->type == LOCKED_DOOR) return tmp; + return NULL; } /* the workhorse routine, which finds the doors in a room */ -void -find_doors_in_room_recursive (char **layout, maptile *map, int x, int y, object **doorlist, int *ndoors, random_map_params *RP) +static void +find_doors_in_room_recursive (char **maze, maptile *map, int x, int y, object **doorlist, int *ndoors, random_map_params *RP) { int i, j; object *door; @@ -755,13 +746,13 @@ 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) { @@ -778,18 +769,18 @@ } 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); } } /* find a random non-blocked spot in this room to drop a key. */ -object ** +static object ** find_doors_in_room (maptile *map, int x, int y, random_map_params *RP) { int i, j; @@ -797,10 +788,10 @@ object **doorlist = (object **)calloc (sizeof (int), 1024); - LayoutData layout2 (RP->Xsize, RP->Ysize); + layout layout2 (RP->Xsize, RP->Ysize); layout2.clear (); - /* allocate and copy the layout, converting C to 0. */ + /* allocate and copy the maze, converting C to 0. */ for (i = 0; i < RP->Xsize; i++) for (j = 0; j < RP->Ysize; j++) layout2[i][j] = wall_blocked (map, i, j) ? '#' : 0; @@ -813,7 +804,7 @@ /* locks and/or hides all the doors in doorlist, or does nothing if opts doesn't say to lock/hide doors. */ -void +static void lock_and_hide_doors (object **doorlist, maptile *map, int opts, random_map_params *RP) { object *door; @@ -823,7 +814,7 @@ if (opts & DOORED) { - for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) + for (i = 0, door = doorlist[0]; doorlist[i]; i++) { object *new_door = get_archetype (shstr_locked_door1); @@ -831,7 +822,6 @@ new_door->face = door->face; new_door->x = door->x; new_door->y = door->y; - door->remove (); door->destroy (); doorlist[i] = new_door; insert_ob_in_map (new_door, map, NULL, 0); @@ -858,9 +848,6 @@ door->face = wallface->face; - if (!QUERY_FLAG (wallface, FLAG_REMOVED)) - wallface->remove (); - wallface->destroy (); } }