--- deliantra/server/random_maps/treasure.C 2008/04/15 03:00:24 1.36
+++ deliantra/server/random_maps/treasure.C 2009/11/06 12:49:19 1.46
@@ -5,18 +5,19 @@
* Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team
* Copyright (©) 1992,2007 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,12 +43,134 @@
#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 *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 **layout, 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
bc_random (int x)
{
- return (rndm (x) + rndm (x) + rndm (x)) / 3;
+ 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_key_random_map);
+ key->slaying = keycode;
+ 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. */
+ object *the_key = gen_key (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))
+ {
+ the_key->destroy ();
+ 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);
+ }
+
+ the_key->destroy ();
+ return 1;
+ }
+ }
+
+ 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
@@ -84,12 +207,12 @@
return;
if (treasureoptions <= 0)
- treasureoptions = rndm (2 * LAST_OPTION);
+ treasureoptions = rmg_rndm (2 * LAST_OPTION);
/* filter out the mutually exclusive options */
if ((treasureoptions & RICH) && (treasureoptions & SPARSE))
{
- if (rndm (2))
+ if (rmg_rndm (2))
treasureoptions -= 1;
else
treasureoptions -= 2;
@@ -159,6 +282,7 @@
}
break;
}
+
default:
{
int i, j, tries;
@@ -169,8 +293,8 @@
tries = 0;
while (i == -1 && tries < 100)
{
- i = rndm (RP->Xsize - 2) + 1;
- j = rndm (RP->Ysize - 2) + 1;
+ i = rmg_rndm (RP->Xsize - 2) + 1;
+ j = rmg_rndm (RP->Ysize - 2) + 1;
find_enclosed_spot (map, &i, &j, RP);
if (wall_blocked (map, i, j))
@@ -201,8 +325,8 @@
for (ti = 0; ti < num_treasures; ti++)
{
- i = rndm (RP->Xsize - 2) + 1;
- j = rndm (RP->Ysize - 2) + 1;
+ i = rmg_rndm (RP->Xsize - 2) + 1;
+ j = rmg_rndm (RP->Ysize - 2) + 1;
place_chest (treasureoptions, i, j, map, style_map, 1, RP);
}
}
@@ -214,21 +338,18 @@
object *
place_chest (int treasureoptions, int x, int y, maptile *map, maptile *style_map, int n_treasures, random_map_params *RP)
{
- object *the_chest;
- int i, xl, yl;
-
- the_chest = get_archetype ("chest"); /* was "chest_2" */
+ object *the_chest = archetype::get (shstr_chest); /* was "chest_2" */
/* first, find a place to put the chest. */
- i = find_first_free_spot (the_chest, map, x, y);
+ int i = find_first_free_spot (the_chest, map, x, y); // this call uses the main rng
if (i == -1)
{
the_chest->destroy ();
return NULL;
}
- xl = x + freearr_x[i];
- yl = y + freearr_y[i];
+ int xl = x + freearr_x[i];
+ int yl = y + freearr_y[i];
/* if the placement is blocked, return a fail. */
if (wall_blocked (map, xl, yl))
@@ -245,7 +366,7 @@
if (tlist != NULL)
for (ti = 0; ti < n_treasures; ti++)
{ /* use the treasure list */
- object *new_treasure = style_map->pick_random_object ();
+ object *new_treasure = style_map->pick_random_object (rmg_rndm);
insert_ob_in_ob (arch_to_object (new_treasure->arch), the_chest);
}
@@ -266,19 +387,18 @@
if (treasureoptions & TRAPPED)
{
maptile *trap_map = find_style ("/styles/trapstyles", "traps", -1);
- object *the_trap;
if (trap_map)
{
- the_trap = trap_map->pick_random_object ();
+ object *the_trap = trap_map->pick_random_object (rmg_rndm);
+
the_trap->stats.Cha = 10 + RP->difficulty;
the_trap->level = bc_random ((3 * RP->difficulty) / 2);
+
if (the_trap)
{
- object *new_trap;
+ object *new_trap = the_trap->arch->instance ();//TODO: why not clone?
- new_trap = arch_to_object (the_trap->arch);
- new_trap->copy_to (the_trap);
new_trap->x = x;
new_trap->y = y;
insert_ob_in_ob (new_trap, the_chest);
@@ -291,11 +411,8 @@
there's only 1 treasure.... */
if ((treasureoptions & KEYREQUIRED) && n_treasures > 1)
{
- char keybuf[1024];
-
- sprintf (keybuf, "%d", 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. */
@@ -305,10 +422,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;
@@ -334,121 +449,13 @@
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. */
- object *the_key;
-
- /* get a key and set its keycode */
- the_key = get_archetype ("key2");
- the_key->slaying = keycode;
-
- if (door_flag == PASS_DOORS)
- {
- int tries = 0;
-
- the_keymaster = 0;
- while (tries < 15 && !the_keymaster)
- {
- i = rndm (RP->Xsize - 2) + 1;
- j = 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 = rndm (RP->Xsize - 2) + 1;
- ky = 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;
+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 *
+static object *
find_monster_in_room_recursive (char **layout, maptile *map, int x, int y, random_map_params *RP)
{
int i, j;
@@ -482,7 +489,7 @@
}
/* now search all the 8 squares around recursively for a monster,in random order */
- for (i = rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++)
+ 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);
if (theMonsterToFind != NULL)
@@ -516,14 +523,14 @@
}
/* 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
+static void
find_spot_in_room_recursive (char **layout, int x, int y, random_map_params *RP)
{
int i, j;
@@ -545,13 +552,13 @@
number_of_free_spots_in_room++;
/* now search all the 8 squares around recursively for free spots,in random order */
- for (i = rndm (8), j = 0; j < 8 && theMonsterToFind == NULL; i++, j++)
+ 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 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;
@@ -576,7 +583,7 @@
if (number_of_free_spots_in_room > 0)
{
- i = rndm (number_of_free_spots_in_room);
+ i = rmg_rndm (number_of_free_spots_in_room);
*kx = room_free_spots_x[i];
*ky = room_free_spots_y[i];
}
@@ -594,7 +601,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;
@@ -654,7 +661,7 @@
}
}
/* give up and return the closest free spot. */
- i = find_free_spot (archetype::find ("chest"), map, x, y, 1, SIZEOFFREE1 + 1);
+ i = find_free_spot (archetype::find (shstr_chest), map, x, y, 1, SIZEOFFREE1 + 1);
if (i != -1)
{
@@ -672,25 +679,21 @@
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 **
+static object **
surround_by_doors (maptile *map, char **layout, int x, int y, int opts)
{
int i;
@@ -730,21 +733,19 @@
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
+static void
find_doors_in_room_recursive (char **layout, maptile *map, int x, int y, object **doorlist, int *ndoors, random_map_params *RP)
{
int i, j;
@@ -781,7 +782,7 @@
layout[x][y] = 1;
/* now search all the 8 squares around recursively for free spots,in random order */
- for (i = rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++)
+ for (i = rmg_rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++)
find_doors_in_room_recursive (layout, map,
x + freearr_x[i % 8 + 1], y + freearr_y[i % 8 + 1],
doorlist, ndoors, RP);
@@ -789,7 +790,7 @@
}
/* 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;
@@ -813,7 +814,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,22 +824,19 @@
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 ("locked_door1");
- char keybuf[1024];
+ object *new_door = get_archetype (shstr_locked_door1);
door = doorlist[i];
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);
- sprintf (keybuf, "%d", 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);
}
}
@@ -860,9 +858,6 @@
door->face = wallface->face;
- if (!QUERY_FLAG (wallface, FLAG_REMOVED))
- wallface->remove ();
-
wallface->destroy ();
}
}