--- deliantra/server/random_maps/treasure.C 2006/12/31 19:02:24 1.12 +++ deliantra/server/random_maps/treasure.C 2007/01/18 00:06:56 1.20 @@ -1,31 +1,29 @@ - /* - 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 + * + * 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 + */ /* placing treasure in maps, where appropriate. */ - - #include #include #include @@ -50,15 +48,13 @@ * and doors but not monsters. * This function is not map tile aware. */ - int wall_blocked (maptile *m, int x, int y) { - int r; - if (OUT_OF_REAL_MAP (m, x, y)) return 1; - r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT; + + int r = GET_MAP_MOVE_BLOCK (m, x, y) & ~MOVE_BLOCK_DEFAULT; return r; } @@ -68,12 +64,11 @@ 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) { - char styledirname[256]; - char stylefilepath[256]; + char styledirname[1024]; + char stylefilepath[1024]; maptile *style_map = 0; int num_treasures; @@ -213,6 +208,7 @@ the_chest->destroy (); return NULL; } + xl = x + freearr_x[i]; yl = y + freearr_y[i]; @@ -278,7 +274,7 @@ if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) { - char keybuf[256]; + char keybuf[1024]; sprintf (keybuf, "%d", (int) RANDOM ()); the_chest->slaying = keybuf; @@ -333,12 +329,11 @@ The idea is that you call keyplace on x,y where a door is, and it'll make sure a key is placed on both sides of the door. */ - int keyplace (maptile *map, int x, int y, char *keycode, int door_flag, int n_keys, random_map_params *RP) { int i, j; - int kx, ky; + int kx = 0, ky = 0; object *the_keymaster; /* the monster that gets the key. */ object *the_key; @@ -350,16 +345,17 @@ { int tries = 0; - the_keymaster = NULL; - while (tries < 15 && the_keymaster == NULL) + the_keymaster = 0; + while (tries < 15 && !the_keymaster) { i = (RANDOM () % (RP->Xsize - 2)) + 1; j = (RANDOM () % (RP->Ysize - 2)) + 1; tries++; the_keymaster = find_closest_monster (map, i, j, RP); } + /* if we don't find a good keymaster, drop the key on the ground. */ - if (the_keymaster == NULL) + if (!the_keymaster) { int freeindex; @@ -368,12 +364,14 @@ { kx = (RANDOM () % (RP->Xsize - 2)) + 1; ky = (RANDOM () % (RP->Ysize - 2)) + 1; - freeindex = find_first_free_spot (the_key, map, kx, ky); + freeindex = find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1); } - if (freeindex != -1) + + // can freeindex ever be < 0? + if (freeindex >= 0) { - kx += freearr_x[freeindex]; - ky += freearr_y[freeindex]; + kx += freearr_x [freeindex]; + ky += freearr_y [freeindex]; } } } @@ -385,8 +383,9 @@ { if (wall_blocked (map, x, y)) return 0; + the_keymaster = find_monster_in_room (map, x, y, RP); - if (the_keymaster == NULL) /* if fail, find a spot to drop the key. */ + if (!the_keymaster) /* if fail, find a spot to drop the key. */ find_spot_in_room (map, x, y, &kx, &ky, RP); } else @@ -398,18 +397,20 @@ 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); + if (sum < 2) /* we might have made a disconnected map-place more keys. */ - { /* diagnoally this time. */ + { /* 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); } + return 1; } } - if (the_keymaster == NULL) + if (!the_keymaster) { the_key->x = kx; the_key->y = ky; @@ -507,9 +508,6 @@ 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; @@ -518,7 +516,6 @@ /* 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) { @@ -539,11 +536,10 @@ 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 = RANDOM () % 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 (layout, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], RP); } @@ -564,10 +560,8 @@ { layout2[i] = (char *) calloc (sizeof (char), RP->Ysize); for (j = 0; j < RP->Ysize; j++) - { - if (wall_blocked (map, i, j)) - layout2[i][j] = '#'; - } + if (wall_blocked (map, i, j)) + layout2[i][j] = '#'; } /* setup num_free_spots and room_free_spots */ @@ -582,9 +576,8 @@ /* deallocate the temp. layout */ for (i = 0; i < RP->Xsize; i++) - { - free (layout2[i]); - } + free (layout2[i]); + free (layout2); free (room_free_spots_x); free (room_free_spots_y); @@ -594,7 +587,6 @@ /* 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 find_enclosed_spot (maptile *map, int *cx, int *cy, random_map_params *RP) { @@ -655,15 +647,19 @@ } } /* give up and return the closest free spot. */ - i = find_first_free_spot (&archetype::find ("chest")->clone, map, x, y); - if (i != -1 && i <= SIZEOFFREE1) + i = find_free_spot (&archetype::find ("chest")->clone, map, x, y, 1, SIZEOFFREE1 + 1); + + if (i != -1) { *cx = x + freearr_x[i]; *cy = y + freearr_y[i]; - return; } - /* indicate failure */ - *cx = *cy = -1; + else + { + /* indicate failure */ + *cx = -1; + *cy = -1; + } } @@ -765,14 +761,15 @@ { /* there could be a door here */ layout[x][y] = 1; door = door_in_square (map, x, y); - if (door != NULL) + if (door) { doorlist[*ndoors] = door; - if (*ndoors > 254) /* eek! out of memory */ + if (*ndoors > 1022) /* eek! out of memory */ { LOG (llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n"); return; } + *ndoors = *ndoors + 1; } } @@ -781,9 +778,7 @@ layout[x][y] = 1; /* now search all the 8 squares around recursively for free spots,in random order */ for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) - { - find_doors_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], doorlist, ndoors, RP); - } + find_doors_in_room_recursive (layout, map, x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1], doorlist, ndoors, RP); } } @@ -796,8 +791,7 @@ int i, j; int ndoors = 0; - doorlist = (object **) calloc (sizeof (int), 256); - + doorlist = (object **) calloc (sizeof (int), 1024); layout2 = (char **) calloc (sizeof (char *), RP->Xsize); /* allocate and copy the layout, converting C to 0. */ @@ -816,9 +810,8 @@ /* deallocate the temp. layout */ for (i = 0; i < RP->Xsize; i++) - { - free (layout2[i]); - } + free (layout2[i]); + free (layout2); return doorlist; } @@ -841,7 +834,7 @@ for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) { object *new_door = get_archetype ("locked_door1"); - char keybuf[256]; + char keybuf[1024]; door = doorlist[i]; new_door->face = door->face;