--- deliantra/server/random_maps/treasure.C 2007/01/06 14:42:30 1.14 +++ deliantra/server/random_maps/treasure.C 2007/01/19 21:49:58 1.22 @@ -1,32 +1,29 @@ - /* - 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 -*/ + * 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 @@ -51,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; } @@ -69,7 +64,6 @@ 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) { @@ -82,13 +76,14 @@ if (treasure_style) if (!strcmp (treasure_style, "none")) return; + if (treasureoptions <= 0) treasureoptions = RANDOM () % (2 * LAST_OPTION); /* filter out the mutually exclusive options */ if ((treasureoptions & RICH) && (treasureoptions & SPARSE)) { - if (RANDOM () % 2) + if (rndm (2)) treasureoptions -= 1; else treasureoptions -= 2; @@ -214,6 +209,7 @@ the_chest->destroy (); return NULL; } + xl = x + freearr_x[i]; yl = y + freearr_y[i]; @@ -334,12 +330,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; @@ -351,16 +346,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; @@ -369,12 +365,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]; } } } @@ -386,8 +384,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 @@ -399,18 +398,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; @@ -465,7 +466,7 @@ } /* now search all the 8 squares around recursively for a monster,in random order */ - for (i = RANDOM () % 8, j = 0; j < 8 && theMonsterToFind == NULL; i++, j++) + for (i = 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); if (theMonsterToFind != NULL) @@ -508,9 +509,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; @@ -519,7 +517,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) { @@ -540,11 +537,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); - } + for (i = 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); } @@ -565,10 +561,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 */ @@ -583,9 +577,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); @@ -595,7 +588,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) { @@ -656,24 +648,27 @@ } } /* 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; + } } - void remove_monsters (int x, int y, maptile *map) { object *tmp; - for (tmp = GET_MAP_OB (map, x, y); tmp != NULL; tmp = tmp->above) + for (tmp = GET_MAP_OB (map, x, y); tmp; tmp = tmp->above) if (QUERY_FLAG (tmp, FLAG_ALIVE)) { if (tmp->head) @@ -686,11 +681,9 @@ }; } - /* 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) { @@ -717,18 +710,17 @@ { 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) && layout[x1][y1] == '>') { /* place a door */ - object *new_door = get_archetype ((freearr_x[i] == 0) ? doors[1] : doors[0]); + remove_monsters (x1, y1, map); - new_door->x = x + freearr_x[i]; - new_door->y = y + freearr_y[i]; - remove_monsters (new_door->x, new_door->y, map); - insert_ob_in_map (new_door, map, NULL, 0); + object *new_door = get_archetype (freearr_x[i] == 0 ? doors[1] : doors[0]); + map->insert (new_door, x1, y1); doorlist[ndoors_made] = new_door; ndoors_made++; } } + return doorlist; } @@ -782,7 +774,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++) + for (i = rndm (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); } }