/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 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 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 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
*/
/* placing treasure in maps, where appropriate. */
#include
#include
#include
/* some defines for various options which can be set. */
#define CONCENTRATED 1 /* all the treasure is at the C's for onions. */
#define HIDDEN 2 /* doors to treasure are hidden. */
#define KEYREQUIRED 4 /* chest has a key, which is placed randomly in the map. */
#define DOORED 8 /* treasure has doors around it. */
#define TRAPPED 16 /* trap dropped in same location as chest. */
#define SPARSE 32 /* 1/2 as much treasure as default */
#define RICH 64 /* 2x as much treasure as default */
#define FILLED 128 /* Fill/tile the entire map with treasure */
#define LAST_OPTION 64 /* set this to the last real option, for random */
#define NO_PASS_DOORS 0
#define PASS_DOORS 1
static object *find_closest_monster (maptile *map, int x, int y);
static object *find_monster_in_room (maptile *map, int x, int y);
static void find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky);
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);
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);
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
bc_random (int x)
{
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)
{
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 (map->width - 2) + 1;
j = rmg_rndm (map->height - 2) + 1;
tries++;
the_keymaster = find_closest_monster (map, i, j);
}
/* 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 (map->width - 2) + 1;
ky = rmg_rndm (map->height - 2) + 1;
freeindex = rmg_find_free_spot (the_key, map, kx, ky, 1, SIZEOFFREE1 + 1);
}
// can freeindex ever be < 0?
if (freeindex >= 0)
{
kx += DIRX (freeindex);
ky += DIRY (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);
if (!the_keymaster) /* if fail, find a spot to drop the key. */
find_spot_in_room (map, x, y, &kx, &ky);
}
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);
sum += keyplace (map, x, y + 1, keycode, NO_PASS_DOORS, 1);
sum += keyplace (map, x - 1, y, keycode, NO_PASS_DOORS, 1);
sum += keyplace (map, x, y - 1, keycode, NO_PASS_DOORS, 1);
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);
keyplace (map, x + 1, y - 1, keycode, NO_PASS_DOORS, 1);
keyplace (map, x - 1, y + 1, keycode, NO_PASS_DOORS, 1);
keyplace (map, x - 1, y - 1, keycode, NO_PASS_DOORS, 1);
}
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
* and doors but not monsters.
* This function is not map tile aware.
*/
int
wall_blocked (maptile *m, int x, int y)
{
if (OUT_OF_REAL_MAP (m, x, y))
return 1;
m->at (x, y).update ();
return GET_MAP_MOVE_BLOCK (m, x, y) & MOVE_WALK;
}
/* place treasures in the map, given the
map, (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, layout &maze, const char *treasure_style, int treasureoptions, random_map_params *RP)
{
int num_treasures;
/* bail out if treasure isn't wanted. */
if (treasure_style)
if (!strcmp (treasure_style, "none"))
return;
if (treasureoptions <= 0)
treasureoptions = rmg_rndm (2 * LAST_OPTION);
/* filter out the mutually exclusive options */
if ((treasureoptions & RICH) && (treasureoptions & SPARSE))
{
if (rmg_rndm (2))
treasureoptions -= 1;
else
treasureoptions -= 2;
}
/* pick the number of treasures */
if (treasureoptions & SPARSE)
num_treasures = bc_random (RP->total_map_hp / 600 + RP->difficulty / 2 + 1);
else if (treasureoptions & RICH)
num_treasures = bc_random (RP->total_map_hp / 150 + 2 * RP->difficulty + 1);
else
num_treasures = bc_random (RP->total_map_hp / 300 + RP->difficulty + 1);
if (num_treasures <= 0)
return;
/* get the style map */
maptile *style_map = find_style ("/styles/treasurestyles", treasure_style, RP->difficulty);
if (!style_map)
{
LOG (llevError, "unable to load style map %s %s.\n", "/styles/treasurestyles", treasure_style);
return;
}
/* all the treasure at one spot in the map. */
if (treasureoptions & CONCENTRATED)
{
/* map_layout_style global, and is previously set */
switch (RP->map_layout_style)
{
case LAYOUT_ONION:
case LAYOUT_SPIRAL:
case LAYOUT_SQUARE_SPIRAL:
{
int i, j;
/* search the onion for C's or '>', and put treasure there. */
for (i = 0; i < RP->Xsize; i++)
{
for (j = 0; j < RP->Ysize; j++)
{
if (maze[i][j] == 'C' || maze[i][j] == '>')
{
int tdiv = RP->symmetry_used;
object *chest;
if (tdiv == 3)
tdiv = 2; /* this symmetry uses a divisor of 2 */
/* don't put a chest on an exit. */
chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures / tdiv, RP);
if (!chest)
continue; /* if no chest was placed NEXT */
if (treasureoptions & (DOORED | HIDDEN))
{
object **doorlist = find_doors_in_room (map, i, j);
lock_and_hide_doors (doorlist, map, treasureoptions, RP);
free (doorlist);
}
}
}
}
break;
}
default:
{
int i, j, tries;
object *chest;
object **doorlist;
i = j = -1;
tries = 0;
while (i == -1 && tries < 100)
{
i = rmg_rndm (RP->Xsize - 2) + 1;
j = rmg_rndm (RP->Ysize - 2) + 1;
find_enclosed_spot (map, &i, &j);
if (wall_blocked (map, i, j))
i = -1;
tries++;
}
chest = place_chest (treasureoptions, i, j, map, style_map, num_treasures, RP);
if (!chest)
return;
i = chest->x;
j = chest->y;
if (treasureoptions & (DOORED | HIDDEN))
{
doorlist = surround_by_doors (map, maze, i, j, treasureoptions);
lock_and_hide_doors (doorlist, map, treasureoptions, RP);
free (doorlist);
}
}
}
}
else
{ /* DIFFUSE treasure maze */
int ti, i, j;
for (ti = 0; ti < num_treasures; ti++)
{
i = rmg_rndm (RP->Xsize - 2) + 1;
j = rmg_rndm (RP->Ysize - 2) + 1;
place_chest (treasureoptions, i, j, map, style_map, 1, RP);
}
}
}
/* 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 */
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" */
/* first, find a place to put the chest. */
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;
}
int xl = x + DIRX (i);
int yl = y + DIRY (i);
/* if the placement is blocked, return a fail. */
if (wall_blocked (map, xl, yl))
return 0;
/* put the treasures in the chest. */
/* if(style_map) { */
#if 0 /* don't use treasure style maps for now! */
int ti;
/* if treasurestyle lists a treasure list, use it. */
treasurelist *tlist = find_treasurelist (RP->treasurestyle);
if (tlist != NULL)
for (ti = 0; ti < n_treasures; ti++)
{ /* use the treasure list */
object *new_treasure = style_map->pick_random_object (rmg_rndm);
insert_ob_in_ob (new_treasure->arch->instance (), the_chest);
}
else
{ /* use the style map */
the_chest->randomitems = tlist;
the_chest->stats.hp = n_treasures;
}
#endif
{ /* neither style_map no treasure list given */
treasurelist *tlist = treasurelist::find ("chest");
the_chest->randomitems = tlist;
the_chest->stats.hp = n_treasures;
}
/* stick a trap in the chest if required */
if (treasureoptions & TRAPPED)
{
maptile *trap_map = find_style ("/styles/trapstyles", "traps", RP->difficulty);
if (trap_map)
{
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 = the_trap->arch->instance ();//TODO: why not clone?
new_trap->x = x;
new_trap->y = y;
insert_ob_in_ob (new_trap, the_chest);
}
}
}
/* set the chest lock code, and call the keyplacer routine with
the lockcode. It's not worth bothering to lock the chest if
there's only 1 treasure.... */
if ((treasureoptions & KEYREQUIRED) && n_treasures > 1)
{
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);
}
/* actually place the chest. */
the_chest->x = xl;
the_chest->y = yl;
insert_ob_in_map (the_chest, map, NULL, 0);
return the_chest;
}
/* finds the closest monster and returns him, regardless of doors or walls */
static object *
find_closest_monster (maptile *map, int x, int y)
{
int i;
for (i = 0; i < SIZEOFFREE; i++)
{
int lx, ly;
lx = x + DIRX (i);
ly = y + DIRY (i);
/* boundscheck */
if (lx >= 0 && ly >= 0 && lx < map->width && ly < map->height)
/* don't bother searching this square unless the map says life exists. */
if (GET_MAP_FLAGS (map, lx, ly) & P_IS_ALIVE)
{
object *the_monster = GET_MAP_OB (map, lx, ly);
for (; the_monster && !the_monster->flag [FLAG_MONSTER]; the_monster = the_monster->above)
;
if (the_monster && the_monster->flag [FLAG_MONSTER])
return the_monster;
}
}
return NULL;
}
/* both find_monster_in_room routines need to have access to this. */
static object *theMonsterToFind;
/* a recursive routine which will return a monster, eventually,if there is one.
it does a check-off on the maze, converting 0's to 1's */
static object *
find_monster_in_room_recursive (layout &maze, maptile *map, int x, int y)
{
int i, j;
/* if we've found a monster already, leave */
if (theMonsterToFind != NULL)
return theMonsterToFind;
/* bounds check x and y */
if (!(x >= 0 && y >= 0 && x < maze.w && y < maze.h))
return theMonsterToFind;
/* if the square is blocked or searched already, leave */
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. */
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 && (!the_monster->flag [FLAG_ALIVE]); the_monster = the_monster->above);
if (the_monster && the_monster->flag [FLAG_ALIVE])
{
theMonsterToFind = the_monster;
return theMonsterToFind;
}
}
/* now search all the 8 squares around recursively for a monster,in random order */
for (i = rmg_rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++)
{
theMonsterToFind = find_monster_in_room_recursive (maze, map, x + DIRX (i % 8 + 1), y + DIRY (i % 8 + 1));
if (theMonsterToFind)
return theMonsterToFind;
}
return theMonsterToFind;
}
/* sets up some data structures: the _recursive form does the
real work. */
static object *
find_monster_in_room (maptile *map, int x, int y)
{
layout layout2 (map->width, map->height);
// 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);
return theMonsterToFind;
}
/* 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. */
static void
find_spot_in_room_recursive (layout &maze, fixed_stack &spots, int x, int y)
{
/* bounds check x and y */
if (!(x >= 0 && y >= 0 && x < maze.w && y < maze.h))
return;
/* if the square is blocked or searched already, leave */
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 */
maze[x][y] = 1;
spots.push (point (x, y));
/* now search all the 8 squares around recursively for free spots,in random order */
for (int i = rmg_rndm (8), j = 0; j < 8 && !theMonsterToFind; i++, j++)
find_spot_in_room_recursive (maze, spots, x + DIRX (i % 8 + 1), y + DIRY (i % 8 + 1));
}
/* find a random non-blocked spot in this room to drop a key. */
static void
find_spot_in_room (maptile *map, int x, int y, int *kx, int *ky)
{
fixed_stack spots (map->width * map->height);
layout layout2 (map->width, map->height);
/* allocate and copy the maze, converting C to 0. */
for (int i = 0; i < map->width; i++)
for (int j = 0; j < map->height; j++)
layout2 [i][j] = wall_blocked (map, i, j) ? '#' : 0;
/* setup num_free_spots and room_free_spots */
find_spot_in_room_recursive (layout2, spots, x, y);
if (spots.size)
{
point p = spots [rmg_rndm (spots.size)];
*kx = p.x;
*ky = p.y;
}
}
/* 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.*/
static void
find_enclosed_spot (maptile *map, int *cx, int *cy)
{
int x, y;
int i;
x = *cx;
y = *cy;
for (i = 0; i <= SIZEOFFREE1; i++)
{
int lx, ly, sindex;
lx = x + DIRX (i);
ly = y + DIRY (i);
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)
{
*cx = lx;
*cy = ly;
return;
}
}
/* OK, if we got here, we're obviously someplace where there's no enclosed
spots--try to find someplace which is 2x enclosed. */
for (i = 0; i <= SIZEOFFREE1; i++)
{
int lx, ly, sindex;
lx = x + DIRX (i);
ly = y + DIRY (i);
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)
{
*cx = lx;
*cy = ly;
return;
}
}
/* settle for one surround point */
for (i = 0; i <= SIZEOFFREE1; i++)
{
int lx, ly, sindex;
lx = x + DIRX (i);
ly = y + DIRY (i);
sindex = surround_flag3 (map, lx, ly);
/* if it's blocked on 3 sides, it's enclosed */
if (sindex)
{
*cx = lx;
*cy = ly;
return;
}
}
/* give up and return the closest free spot. */
i = rmg_find_free_spot (archetype::find (shstr_chest), map, x, y, 1, SIZEOFFREE1 + 1);
if (i != -1)
{
*cx = x + DIRX (i);
*cy = y + DIRY (i);
}
else
{
/* indicate failure */
*cx = -1;
*cy = -1;
}
}
static void
remove_monsters (int x, int y, maptile *map)
{
for (object *tmp = GET_MAP_OB (map, x, y); tmp; )
{
object *next = tmp->above;
if (tmp->flag [FLAG_ALIVE])
tmp->head_ ()->destroy ();
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.*/
static object **
surround_by_doors (maptile *map, char **maze, int x, int y, int opts)
{
int i;
const char *doors[2];
object **doorlist;
int ndoors_made = 0;
doorlist = (object **) calloc (9, sizeof (object *)); /* 9 doors so we can hold termination null */
/* this is a list we pick from, for horizontal and vertical doors */
if (opts & DOORED)
{
doors[0] = "locked_door2";
doors[1] = "locked_door1";
}
else
{
doors[0] = "door_1";
doors[1] = "door_2";
}
/* place doors in all the 8 adjacent unblocked squares. */
for (i = 1; i < 9; i++)
{
int x1 = x + DIRX (i), y1 = y + DIRY (i);
if (!wall_blocked (map, x1, y1) && maze[x1][y1] == '>')
{ /* place a door */
remove_monsters (x1, y1, map);
object *new_door = archetype::get (DIRX (i) == 0 ? doors[1] : doors[0]);
map->insert (new_door, x1, y1);
doorlist[ndoors_made] = new_door;
ndoors_made++;
}
}
return doorlist;
}
/* returns the first door in this square, or NULL if there isn't a door. */
static object *
door_in_square (maptile *map, int x, int y)
{
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 */
static void
find_doors_in_room_recursive (layout &maze, maptile *map, int x, int y, object **doorlist, int *ndoors)
{
int i, j;
object *door;
/* bounds check x and y */
if (!(x >= 0 && y >= 0 && x < maze.w && y < maze.h))
return;
/* if the square is blocked or searched already, leave */
if (maze[x][y] == 1)
return;
/* check off this point */
if (maze[x][y] == '#')
{ /* there could be a door here */
maze[x][y] = 1;
door = door_in_square (map, x, y);
if (door)
{
doorlist[*ndoors] = door;
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;
}
}
else
{
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 (maze, map,
x + DIRX (i % 8 + 1), y + DIRY (i % 8 + 1),
doorlist, ndoors);
}
}
/* find a random non-blocked spot in this room to drop a key. */
static object **
find_doors_in_room (maptile *map, int x, int y)
{
int i, j;
int ndoors = 0;
object **doorlist = (object **)calloc (sizeof (int), 1024);
layout layout2 (map->width, map->height);
layout2.clear ();
/* allocate and copy the maze, converting C to 0. */
for (i = 0; i < map->width; i++)
for (j = 0; j < map->height; j++)
layout2[i][j] = wall_blocked (map, i, j) ? '#' : 0;
/* setup num_free_spots and room_free_spots */
find_doors_in_room_recursive (layout2, map, x, y, doorlist, &ndoors);
return doorlist;
}
/* locks and/or hides all the doors in doorlist, or does nothing if
opts doesn't say to lock/hide doors. */
static void
lock_and_hide_doors (object **doorlist, maptile *map, int opts, random_map_params *RP)
{
object *door;
int i;
/* lock the doors and hide the keys. */
if (opts & DOORED)
{
for (i = 0, door = doorlist[0]; doorlist[i]; i++)
{
object *new_door = archetype::get (shstr_locked_door1);
door = doorlist[i];
new_door->face = door->face;
new_door->x = door->x;
new_door->y = door->y;
door->destroy ();
doorlist[i] = new_door;
insert_ob_in_map (new_door, map, NULL, 0);
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);
}
}
/* change the faces of the doors and surrounding walls to hide them. */
if (opts & HIDDEN)
{
for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++)
{
object *wallface;
door = doorlist[i];
wallface = retrofit_joined_wall (map, door->x, door->y, 1, RP);
if (wallface != NULL)
{
retrofit_joined_wall (map, door->x - 1, door->y, 0, RP);
retrofit_joined_wall (map, door->x + 1, door->y, 0, RP);
retrofit_joined_wall (map, door->x, door->y - 1, 0, RP);
retrofit_joined_wall (map, door->x, door->y + 1, 0, RP);
door->face = wallface->face;
wallface->destroy ();
}
}
}
}