--- deliantra/server/random_maps/treasure.C 2008/05/04 14:12:38 1.38 +++ deliantra/server/random_maps/treasure.C 2008/05/08 11:39:24 1.39 @@ -50,6 +50,121 @@ return (rmg_rndm (x) + rmg_rndm (x) + rmg_rndm (x)) / 3; } +static object * +gen_key (const shstr &keycode) +{ + /* get a key and set its keycode */ + object *key = archetype::get (shstr_key2); + + key->slaying = keycode; + key->stats.food = 100; + key->speed_left = -1.f; + key->flag [FLAG_IS_USED_UP] = true; + key->set_speed (1.f / 300.f); + + return key; +} + +/* places keys in the map, preferably in something alive. + keycode is the key's code, + door_flag is either PASS_DOORS or NO_PASS_DOORS. + NO_PASS_DOORS won't cross doors or walls to keyplace, PASS_DOORS will. + if n_keys is 1, it will place 1 key. if n_keys >1, it will place 2-4 keys: + it will place 2-4 keys regardless of what nkeys is provided nkeys > 1. + + 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. +*/ +static int +keyplace (maptile *map, int x, int y, const shstr &keycode, int door_flag, int n_keys, random_map_params *RP) +{ + int i, j; + int kx = 0, ky = 0; + object *the_keymaster; /* the monster that gets the key. */ + + if (door_flag == PASS_DOORS) + { + int tries = 0; + + the_keymaster = 0; + while (tries < 15 && !the_keymaster) + { + i = rmg_rndm (RP->Xsize - 2) + 1; + j = rmg_rndm (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) + { + int freeindex; + + freeindex = -1; + for (tries = 0; tries < 15 && freeindex == -1; tries++) + { + kx = rmg_rndm (RP->Xsize - 2) + 1; + ky = rmg_rndm (RP->Ysize - 2) + 1; + freeindex = find_free_spot (gen_key (keycode), map, kx, ky, 1, SIZEOFFREE1 + 1); + } + + // can freeindex ever be < 0? + if (freeindex >= 0) + { + kx += freearr_x [freeindex]; + ky += freearr_y [freeindex]; + } + } + } + else + { /* NO_PASS_DOORS --we have to work harder. */ + /* don't try to keyplace if we're sitting on a blocked square and + NO_PASS_DOORS is set. */ + if (n_keys == 1) + { + if (wall_blocked (map, x, y)) + return 0; + + the_keymaster = find_monster_in_room (map, x, y, RP); + if (!the_keymaster) /* if fail, find a spot to drop the key. */ + find_spot_in_room (map, x, y, &kx, &ky, RP); + } + 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); + + 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); + } + + return 1; + } + } + + object *the_key = gen_key (keycode); + + if (the_keymaster) + the_keymaster->head_ ()->insert (the_key); + else + { + the_key->x = kx; + the_key->y = ky; + insert_ob_in_map (the_key, map, NULL, 0); + } + + return 1; +} + /* returns true if square x,y has P_NO_PASS set, which is true for walls * and doors but not monsters. * This function is not map tile aware. @@ -288,11 +403,8 @@ there's only 1 treasure.... */ if ((treasureoptions & KEYREQUIRED) && n_treasures > 1) { - char keybuf[1024]; - - sprintf (keybuf, "%d", rmg_rndm (1000000000)); - the_chest->slaying = keybuf; - keyplace (map, x, y, keybuf, PASS_DOORS, 1, RP); + 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); } /* actually place the chest. */ @@ -331,112 +443,6 @@ return NULL; } - - -/* places keys in the map, preferably in something alive. - keycode is the key's code, - door_flag is either PASS_DOORS or NO_PASS_DOORS. - NO_PASS_DOORS won't cross doors or walls to keyplace, PASS_DOORS will. - if n_keys is 1, it will place 1 key. if n_keys >1, it will place 2-4 keys: - it will place 2-4 keys regardless of what nkeys is provided nkeys > 1. - - 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 = 0, ky = 0; - object *the_keymaster; /* the monster that gets the key. */ - - /* get a key and set its keycode */ - object *the_key = archetype::get (shstr_key2); - the_key->slaying = keycode; - - if (door_flag == PASS_DOORS) - { - int tries = 0; - - the_keymaster = 0; - while (tries < 15 && !the_keymaster) - { - i = rmg_rndm (RP->Xsize - 2) + 1; - j = rmg_rndm (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) - { - int freeindex; - - freeindex = -1; - for (tries = 0; tries < 15 && freeindex == -1; tries++) - { - 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); - } - - // can freeindex ever be < 0? - if (freeindex >= 0) - { - kx += freearr_x [freeindex]; - ky += freearr_y [freeindex]; - } - } - } - else - { /* NO_PASS_DOORS --we have to work harder. */ - /* don't try to keyplace if we're sitting on a blocked square and - NO_PASS_DOORS is set. */ - if (n_keys == 1) - { - if (wall_blocked (map, x, y)) - return 0; - - the_keymaster = find_monster_in_room (map, x, y, RP); - if (!the_keymaster) /* if fail, find a spot to drop the key. */ - find_spot_in_room (map, x, y, &kx, &ky, RP); - } - 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); - - 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); - } - - return 1; - } - } - - if (!the_keymaster) - { - the_key->x = kx; - the_key->y = ky; - insert_ob_in_map (the_key, map, NULL, 0); - return 1; - } - - insert_ob_in_ob (the_key, the_keymaster->head_ ()); - return 1; -} - - - /* both find_monster_in_room routines need to have access to this. */ object *theMonsterToFind; @@ -822,7 +828,6 @@ for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) { object *new_door = get_archetype ("locked_door1"); - char keybuf[1024]; door = doorlist[i]; new_door->face = door->face; @@ -832,9 +837,8 @@ door->destroy (); doorlist[i] = new_door; insert_ob_in_map (new_door, map, NULL, 0); - sprintf (keybuf, "%d", rmg_rndm (1000000000)); - new_door->slaying = keybuf; - keyplace (map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP); + 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); } }