/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
* Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
* Copyright (©) 2002,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.
*
* 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 .
*
* The authors can be reached via e-mail to
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
playervec players;
void
display_motd (const object *op)
{
char buf[MAX_BUF];
char motd[HUGE_BUF];
FILE *fp;
int comp;
int size;
sprintf (buf, "%s/%s", settings.confdir, settings.motd);
if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL)
return;
motd[0] = '\0';
size = 0;
while (fgets (buf, MAX_BUF, fp))
{
if (*buf == '#')
continue;
strncat (motd + size, buf, HUGE_BUF - size);
size += strlen (buf);
}
draw_ext_info (NDI_UNIQUE | NDI_GREEN, 0, op, MSG_TYPE_MOTD, MSG_SUBTYPE_NONE, motd, NULL);
close_and_delete (fp, comp);
}
void
send_rules (const object *op)
{
char buf[MAX_BUF];
char rules[HUGE_BUF];
FILE *fp;
int comp;
int size;
sprintf (buf, "%s/%s", settings.confdir, settings.rules);
if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL)
return;
rules[0] = '\0';
size = 0;
while (fgets (buf, MAX_BUF, fp))
{
if (*buf == '#')
continue;
if (size + strlen (buf) >= HUGE_BUF)
{
LOG (llevDebug, "Warning, rules size is > %d bytes.\n", HUGE_BUF);
break;
}
strncat (rules + size, buf, HUGE_BUF - size);
size += strlen (buf);
}
draw_ext_info (NDI_UNIQUE | NDI_GREEN, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_RULES, rules, NULL);
close_and_delete (fp, comp);
}
void
send_news (const object *op)
{
char buf[MAX_BUF];
char news[HUGE_BUF];
char subject[MAX_BUF];
FILE *fp;
int comp;
int size;
sprintf (buf, "%s/%s", settings.confdir, settings.news);
if ((fp = open_and_uncompress (buf, 0, &comp)) == NULL)
return;
news[0] = '\0';
subject[0] = '\0';
size = 0;
while (fgets (buf, MAX_BUF, fp))
{
if (*buf == '#')
continue;
if (*buf == '%')
{ /* send one news */
if (size > 0)
draw_ext_info_format (NDI_UNIQUE | NDI_GREEN, 0, op, MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_NEWS, "INFORMATION: %s\n%s", (char *)"%s\n%s", subject, news); /*send previously read news */
strcpy (subject, buf + 1);
strip_endline (subject);
size = 0;
news[0] = '\0';
}
else
{
if (size + strlen (buf) >= HUGE_BUF)
{
LOG (llevDebug, "Warning, one news item has size > %d bytes.\n", HUGE_BUF);
break;
}
strncat (news + size, buf, HUGE_BUF - size);
size += strlen (buf);
}
}
draw_ext_info_format (NDI_UNIQUE | NDI_GREEN, 0, op,
MSG_TYPE_ADMIN, MSG_TYPE_ADMIN_NEWS, "INFORMATION: %s\n%s\n", (char *)"%s\n%s", subject, news);
close_and_delete (fp, comp);
}
/* This loads the first map an puts the player on it. */
static void
set_first_map (object *op)
{
op->contr->maplevel = first_map_path;
op->x = -1;
op->y = -1;
}
void
player::activate ()
{
if (active)
return;
players.insert (this);
ob->remove ();
ob->map = 0;
ob->activate_recursive ();
CLEAR_FLAG (ob, FLAG_FRIENDLY);
add_friendly_object (ob);
}
void
player::deactivate ()
{
if (!active)
return;
terminate_all_pets (ob);
remove_friendly_object (ob);
ob->deactivate_recursive ();
if (ob->map)
maplevel = ob->map->path;
ob->remove ();
ob->enemy = 0; // sometimes keeps an extra refcount on itself
ob->map = 0;
party = 0;
combat_ob = ranged_ob = 0; //TODO, should be special marker, non-refcounted, not this
players.erase (this);
}
// connect the player with a specific client
// also changes, rationalises, and fixes some incorrect settings
void
player::connect (client *ns)
{
this->ns = ns;
ns->pl = this;
run_on = 0;
fire_on = 0;
ob->close_container (); //TODO: client-specific
ns->update_look = 0;
ns->look_position = 0;
clear_los (this);
ns->reset_stats ();
/* make sure he's a player -- needed because of class change. */
ob->type = PLAYER; // we are paranoid
ob->race = ob->arch->race;
ob->update_weight ();
link_player_skills (ob);
CLEAR_FLAG (ob, FLAG_NO_FIX_PLAYER);
assign (title, ob->arch->object::name);
/* if it's a dragon player, set the correct title here */
if (is_dragon_pl (ob))
{
object *tmp, *abil = 0, *skin = 0;
for (tmp = ob->inv; tmp; tmp = tmp->below)
if (tmp->type == FORCE)
if (tmp->arch->archname == shstr_dragon_ability_force)
abil = tmp;
else if (tmp->arch->archname == shstr_dragon_skin_force)
skin = tmp;
set_dragon_name (ob, abil, skin);
}
new_draw_info (NDI_UNIQUE, 0, ob, "Welcome Back!");
esrv_new_player (this, ob->weight + ob->carrying);
ob->update_stats ();
ns->floorbox_update ();
esrv_send_inventory (ob, ob);
esrv_add_spells (this, 0);
activate ();
send_rules (ob);
send_news (ob);
display_motd (ob);
INVOKE_PLAYER (CONNECT, this);
INVOKE_PLAYER (LOGIN, this);
}
void
player::disconnect ()
{
if (ob)
{
ob->close_container (); //TODO: client-specific
ob->drop_unpaid_items ();
}
if (ns)
{
if (active)
INVOKE_PLAYER (LOGOUT, this, ARG_INT (0));
INVOKE_PLAYER (DISCONNECT, this);
ns->reset_stats ();
ns->pl = 0;
ns = 0;
}
observe = ob;
deactivate ();
}
// the need for this function can be explained
// by load_object not returning the object
void
player::set_object (object *op)
{
ob = observe = op;
ob->contr = this; /* this aren't yet in archetype */
ob->speed = 1.0f;
ob->speed_left = 0.5f;
ob->direction = 5; /* So player faces south */
ob->flag [FLAG_READY_WEAPON] = false;
ob->flag [FLAG_READY_SKILL] = false;
ob->flag [FLAG_READY_BOW] = false;
for (object *op = ob->inv; op; op = op->below)
if (op->flag [FLAG_APPLIED])
switch (op->type)
{
case SKILL:
ob->flag [FLAG_APPLIED] = false;
break;
case WAND:
case ROD:
case HORN:
case BOW:
ranged_ob = op;
break;
case WEAPON:
combat_ob = op;
break;
}
ob->change_weapon (combat_ob ? combat_ob : ranged_ob);
ob->deactivate (); // change_Weapon activates, fix this better
}
void
player::set_observe (object *op)
{
observe = op ? op : ob;
do_los = 1;
}
player::player ()
{
/* There are some elements we want initialised to non zero value -
* we deal with that below this point.
*/
outputs_sync = 4;
outputs_count = 4;
unapply = unapply_nochoice;
savebed_map = first_map_path; /* Init. respawn position */
gen_sp_armour = 10;
bowtype = bow_normal;
petmode = pet_normal;
listening = 10;
usekeys = containers;
peaceful = 1; /* default peaceful */
do_los = 1;
weapon_sp = 1.0f;
weapon_sp_left = 0.5f;
}
void
player::do_destroy ()
{
disconnect ();
attachable::do_destroy ();
if (ob)
{
ob->destroy_inv (false);
ob->destroy ();
}
ob = observe = 0;
}
player::~player ()
{
/* Clear item stack */
free (stack_items);
}
/* Tries to add player on the connection passed in ns.
* All we can really get in this is some settings like host and display
* mode.
*/
player *
player::create ()
{
player *pl = new player;
pl->set_object (arch_to_object (get_player_archetype (0)));
pl->ob->roll_stats ();
pl->ob->stats.wc = 2;
pl->ob->run_away = 25; /* Then we panick... */
set_first_map (pl->ob);
return pl;
}
/*
* get_player_archetype() return next player archetype from archetype
* list. Not very efficient routine, but used only creating new players.
* Note: there MUST be at least one player archetype!
*/
archetype *
get_player_archetype (archetype *at)
{
// archetypes could have been reloaded
archetype *nat = at ? archetype::find (at->archname) : archetypes [0];
if (!nat)
return at;
archvec::iterator i = archetypes.find (nat);
for (;;)
{
if (++i == archetypes.end ())
i = archetypes.begin ();
else if (*i == at)
cleanup ("not a single player archetype found");
if ((*i)->type == PLAYER)
return *i;
}
}
object *
get_nearest_player (object *mon)
{
object *op = NULL;
objectlink *ol;
unsigned lastdist;
rv_vector rv;
for (ol = first_friendly_object, lastdist = 1000; ol; ol = ol->next)
{
if (!can_detect_enemy (mon, ol->ob, &rv))
continue;
if (lastdist > rv.distance)
{
op = ol->ob;
lastdist = rv.distance;
}
}
for_all_players (pl)
if (can_detect_enemy (mon, pl->ob, &rv))
if (lastdist > rv.distance)
{
op = pl->ob;
lastdist = rv.distance;
}
#if 0
LOG (llevDebug, "get_nearest_player() finds player: %s\n", op ? &op->name : "(null)");
#endif
return op;
}
/* I believe this can safely go to 2, 3 is questionable, 4 will likely
* result in a monster paths backtracking. It basically determines how large a
* detour a monster will take from the direction path when looking
* for a path to the player. The values are in the amount of direction
* the deviation is
*/
#define DETOUR_AMOUNT 2
/* This is used to prevent infinite loops. Consider a case where the
* player is in a chamber (with gate closed), and monsters are outside.
* with DETOUR_AMOUNT==2, the function will turn each corner, trying to
* find a path into the chamber. This is a good thing, but since there
* is no real path, it will just keep circling the chamber for
* ever (this could be a nice effect for monsters, but not for the function
* to get stuck in. I think for the monsters, if max is reached and
* we return the first direction the creature could move would result in the
* circling behaviour. Unfortunately, this function is also used to determined
* if the creature should cast a spell, so returning a direction in that case
* is probably not a good thing.
*/
#define MAX_SPACES 50
/*
* Returns the direction to the player, if valid. Returns 0 otherwise.
* modified to verify there is a path to the player. Does this by stepping towards
* player and if path is blocked then see if blockage is close enough to player that
* direction to player is changed (ie zig or zag). Continue zig zag until either
* reach player or path is blocked. Thus, will only return true if there is a free
* path to player. Though path may not be a straight line. Note that it will find
* player hiding along a corridor at right angles to the corridor with the monster.
*
* Modified by MSW 2001-08-06 to handle tiled maps. Various notes:
* 1) With DETOUR_AMOUNT being 2, it should still go and find players hiding
* down corriders.
* 2) I think the old code was broken if the first direction the monster
* should move was blocked - the code would store the first direction without
* verifying that the player can actually move in that direction. The new
* code does not store anything in firstdir until we have verified that the
* monster can in fact move one space in that direction.
* 3) I'm not sure how good this code will be for moving multipart monsters,
* since only simple checks to blocked are being called, which could mean the monster
* is blocking itself.
*/
int
path_to_player (object *mon, object *pl, unsigned mindiff)
{
rv_vector rv;
sint16 x, y;
int lastx, lasty, dir, i, diff, firstdir = 0, lastdir, max = MAX_SPACES, mflags, blocked;
maptile *m, *lastmap;
get_rangevector (mon, pl, &rv, 0);
if (rv.distance < mindiff)
return 0;
x = mon->x;
y = mon->y;
m = mon->map;
dir = rv.direction;
lastdir = firstdir = rv.direction; /* perhaps we stand next to pl, init firstdir too */
diff = ::max (abs (rv.distance_x), abs (rv.distance_y));
/* If we can't solve it within the search distance, return now. */
if (diff > max)
return 0;
while (diff > 1 && max > 0)
{
lastx = x;
lasty = y;
lastmap = m;
x = lastx + freearr_x[dir];
y = lasty + freearr_y[dir];
mflags = get_map_flags (m, &m, x, y, &x, &y);
blocked = (mflags & P_OUT_OF_MAP) ? MOVE_ALL : GET_MAP_MOVE_BLOCK (m, x, y);
/* Space is blocked - try changing direction a little */
if ((mflags & P_OUT_OF_MAP) || ((OB_TYPE_MOVE_BLOCK (mon, blocked) || (mflags & P_BLOCKSVIEW))
&& (m == mon->map && blocked_link (mon, m, x, y))))
{
/* recalculate direction from last good location. Possible
* we were not traversing ideal location before.
*/
get_rangevector_from_mapcoord (lastmap, lastx, lasty, pl, &rv, 0);
if (rv.direction != dir)
{
/* OK - says direction should be different - lets reset the
* the values so it will try again.
*/
x = lastx;
y = lasty;
m = lastmap;
dir = firstdir = rv.direction;
}
else
{
/* direct path is blocked - try taking a side step to
* either the left or right.
* Note increase the values in the loop below to be
* more than -1/1 respectively will mean the monster takes
* bigger detour. Have to be careful about these values getting
* too big (3 or maybe 4 or higher) as the monster may just try
* stepping back and forth
*/
for (i = -DETOUR_AMOUNT; i <= DETOUR_AMOUNT; i++)
{
if (i == 0)
continue; /* already did this, so skip it */
/* Use lastdir here - otherwise,
* since the direction that the creature should move in
* may change, you could get infinite loops.
* ie, player is northwest, but monster can only
* move west, so it does that. It goes some distance,
* gets blocked, finds that it should move north,
* can't do that, but now finds it can move east, and
* gets back to its original point. lastdir contains
* the last direction the creature has successfully
* moved.
*/
x = lastx + freearr_x[absdir (lastdir + i)];
y = lasty + freearr_y[absdir (lastdir + i)];
m = lastmap;
mflags = get_map_flags (m, &m, x, y, &x, &y);
if (mflags & P_OUT_OF_MAP)
continue;
blocked = GET_MAP_MOVE_BLOCK (m, x, y);
if (OB_TYPE_MOVE_BLOCK (mon, blocked))
continue;
if (mflags & P_BLOCKSVIEW)
continue;
if (m == mon->map && blocked_link (mon, m, x, y))
break;
}
/* go through entire loop without finding a valid
* sidestep to take - thus, no valid path.
*/
if (i == (DETOUR_AMOUNT + 1))
return 0;
diff--;
lastdir = dir;
max--;
if (!firstdir)
firstdir = dir + i;
} /* else check alternate directions */
} /* if blocked */
else
{
/* we moved towards creature, so diff is less */
diff--;
max--;
lastdir = dir;
if (!firstdir)
firstdir = dir;
}
if (diff <= 1)
{
/* Recalculate diff (distance) because we may not have actually
* headed toward player for entire distance.
*/
get_rangevector_from_mapcoord (m, x, y, pl, &rv, 0);
diff = ::max (abs (rv.distance_x), abs (rv.distance_y));
}
if (diff > max)
return 0;
}
/* If we reached the max, didn't find a direction in time */
if (!max)
return 0;
return firstdir;
}
void
give_initial_items (object *pl, treasurelist *items)
{
if (pl->randomitems)
create_treasure (items, pl, GT_STARTEQUIP | GT_ONLY_GOOD, 1, 0);
for (object *next, *op = pl->inv; op; op = next)
{
next = op->below;
/* Forces get applied per default, unless they have the
* flag "neutral" set. Sorry but I can't think of a better way
*/
if (op->type == FORCE && !QUERY_FLAG (op, FLAG_NEUTRAL))
SET_FLAG (op, FLAG_APPLIED);
/* we never give weapons/armour if these cannot be used
* by this player due to race restrictions
*/
if (pl->type == PLAYER)
{
if ((!QUERY_FLAG (pl, FLAG_USE_ARMOUR)
&&
(op->type == ARMOUR || op->type == BOOTS
|| op->type == CLOAK || op->type == HELMET
|| op->type == SHIELD || op->type == GLOVES
|| op->type == BRACERS || op->type == GIRDLE))
|| (!QUERY_FLAG (pl, FLAG_USE_WEAPON) && op->type == WEAPON))
{
op->destroy ();
continue;
}
}
/* This really needs to be better - we should really give
* a substitute spellbook. The problem is that we don't really
* have a good idea what to replace it with (need something like
* a first level treasurelist for each skill.)
* remove duplicate skills also
*/
if (op->type == SPELLBOOK || op->type == SKILL)
{
object *tmp;
for (tmp = op->below; tmp; tmp = tmp->below)
if (tmp->type == op->type && tmp->name == op->name)
break;
if (tmp)
{
op->destroy ();
LOG (llevError, "give_initial_items: Removing duplicate object %s\n", &tmp->name);
continue;
}
if (op->nrof > 1)
op->nrof = 1;
}
if (op->type == SPELLBOOK && op->inv)
CLEAR_FLAG (op->inv, FLAG_STARTEQUIP);
/* Give starting characters identified, uncursed, and undamned
* items. Just don't identify gold or silver, or it won't be
* merged properly.
*/
if (need_identify (op))
{
SET_FLAG (op, FLAG_IDENTIFIED);
CLEAR_FLAG (op, FLAG_CURSED);
CLEAR_FLAG (op, FLAG_DAMNED);
}
if (op->type == SPELL)
{
op->destroy ();
continue;
}
else if (op->type == SKILL)
{
SET_FLAG (op, FLAG_CAN_USE_SKILL);
op->stats.exp = 0;
op->level = 1;
}
else /* lock all 'normal items by default */
SET_FLAG (op, FLAG_INV_LOCKED);
} /* for loop of objects in player inv */
/* Need to set up the skill pointers */
link_player_skills (pl);
}
void
get_party_password (object *op, partylist *party)
{
if (party == NULL)
{
LOG (llevError, "get_party_password(): tried to make player %s join a NULL party", &op->name);
return;
}
op->contr->write_buf[0] = '\0';
op->contr->ns->state = ST_GET_PARTY_PASSWORD;
op->contr->party_to_join = party;
send_query (op->contr->ns, CS_QUERY_HIDEINPUT, "What is the password?\n:");
}
/* This rolls four 1-6 rolls and sums the best 3 of the 4. */
static int
roll_stat (void)
{
int a[4], i, j, k;
for (i = 0; i < 4; i++)
a[i] = (int) rndm (6) + 1;
for (i = 0, j = 0, k = 7; i < 4; i++)
if (a[i] < k)
k = a[i], j = i;
for (i = 0, k = 0; i < 4; i++)
if (i != j)
k += a[i];
return k;
}
void
object::roll_stats ()
{
int statsort [NUM_STATS];
for (;;)
{
int sum = 0;
for (int i = NUM_STATS; i--; )
sum += statsort [i] = roll_stat ();
if (sum >= 82 && sum <= 116)
break;
}
// Sort the stats so that rerolling is easier...
std::sort (statsort, statsort + NUM_STATS, std::greater());
for (int i = 0; i < NUM_STATS; ++i)
stats.stat (i) = statsort [i];
stats.exp = 0;
stats.ac = 0;
stats.hp = stats.maxhp;
stats.sp = stats.maxsp;
stats.grace = stats.maxgrace;
if (contr)
{
contr->levhp[1] = 9;
contr->levsp[1] = 6;
contr->levgrace[1] = 3;
contr->orig_stats = stats;
}
}
void
object::swap_stats (int a, int b)
{
swap (contr->orig_stats.stat (a), contr->orig_stats.stat (b));
for (int i = 0; i < NUM_STATS; ++i)
stats.stat (i) = contr->orig_stats.stat (i);
//TODO: the following code looks so borked and should, at the very least,
// be merged with the similar code in roll_stats
stats.ac = 0;
level = 1;
stats.exp = 0;
stats.ac = 0;
stats.hp = stats.maxhp;
stats.sp = stats.maxsp;
stats.grace = stats.maxgrace;
if (contr)
{
contr->levhp[1] = 9;
contr->levsp[1] = 6;
contr->levgrace[1] = 3;
contr->orig_stats = stats;
}
}
static void
start_info (object *op)
{
char buf[MAX_BUF];
sprintf (buf, "Welcome to Deliantra v%s!", VERSION);
new_draw_info (NDI_UNIQUE, 0, op, buf);
}
/* This function takes the key that is passed, and does the
* appropriate action with it (change race, or other things).
* The function name is for historical reasons - now we have
* separate race and class; this actually changes the RACE,
* not the class.
*/
void
player::chargen_race_done ()
{
/* this must before then initial items are given */
esrv_new_player (ob->contr, ob->weight + ob->carrying);
treasurelist *tl = treasurelist::find ("starting_wealth");
if (tl)
create_treasure (tl, ob, 0, 0, 0);
INVOKE_PLAYER (BIRTH, ob->contr);
INVOKE_PLAYER (LOGIN, ob->contr);
ob->contr->ns->state = ST_PLAYING;
if (ob->msg)
ob->msg = 0;
/* We create this now because some of the unique maps will need it
* to save here.
*/
{
char buf[MAX_BUF];
sprintf (buf, "%s/%s/%s", settings.localdir, settings.playerdir, &ob->name);
make_path_to_file (buf);
}
start_info (ob);
CLEAR_FLAG (ob, FLAG_WIZ);
give_initial_items (ob, ob->randomitems);
link_player_skills (ob);
esrv_send_inventory (ob, ob);
ob->update_stats ();
/* This moves the player to a different start map, if there
* is one for this race
*/
if (*first_map_ext_path)
{
object *tmp;
char mapname[MAX_BUF];
snprintf (mapname, MAX_BUF - 1, "%s/%s", &first_map_ext_path, &ob->arch->archname);
tmp = object::create ();
EXIT_PATH (tmp) = mapname;
EXIT_X (tmp) = ob->x;
EXIT_Y (tmp) = ob->y;
ob->enter_exit (tmp); /* we don't really care if it succeeded;
* if the map isn't there, then stay on the
* default initial map */
tmp->destroy ();
}
else
LOG (llevDebug, "first_map_ext_path not set\n");
}
void
player::chargen_race_next ()
{
/* Following actually changes the race - this is the default command
* if we don't match with one of the options above.
*/
do
{
shstr name = ob->name;
int x = ob->x, y = ob->y;
ob->remove_statbonus ();
ob->remove ();
ob->arch = get_player_archetype (ob->arch);
ob->arch->copy_to (ob);
ob->instantiate ();
ob->stats = ob->contr->orig_stats;
ob->name = ob->name_pl = name;
ob->x = x;
ob->y = y;
SET_ANIMATION (ob, 2); /* So player faces south */
insert_ob_in_map (ob, ob->map, ob, 0);
assign (ob->contr->title, ob->arch->object::name);
ob->add_statbonus ();
}
while (!allowed_class (ob));
update_object (ob, UP_OBJ_FACE);
esrv_update_item (UPD_FACE, ob, ob);
ob->update_stats ();
ob->stats.hp = ob->stats.maxhp;
ob->stats.sp = ob->stats.maxsp;
ob->stats.grace = 0;
}
void
flee_player (object *op)
{
int dir, diff;
rv_vector rv;
if (op->stats.hp < 0)
{
LOG (llevDebug, "Fleeing player is dead.\n");
CLEAR_FLAG (op, FLAG_SCARED);
return;
}
if (op->enemy == NULL)
{
LOG (llevDebug, "Fleeing player had no enemy.\n");
CLEAR_FLAG (op, FLAG_SCARED);
return;
}
/* Seen some crashes here. Since we don't store an
* op->enemy_count, it is possible that something destroys the
* actual enemy, and the object is recycled.
*/
if (op->enemy->map == NULL)
{
CLEAR_FLAG (op, FLAG_SCARED);
op->enemy = NULL;
return;
}
if (!(random_roll (0, 4, op, PREFER_LOW)) && did_make_save (op, op->level, 0))
{
op->enemy = NULL;
CLEAR_FLAG (op, FLAG_SCARED);
return;
}
get_rangevector (op, op->enemy, &rv, 0);
dir = absdir (4 + rv.direction);
for (diff = 0; diff < 3; diff++)
{
int m = 1 - (RANDOM () & 2);
if (move_ob (op, absdir (dir + diff * m), op) || (diff == 0 && move_ob (op, absdir (dir - diff * m), op)))
return;
}
/* Cornered, get rid of scared */
CLEAR_FLAG (op, FLAG_SCARED);
op->enemy = NULL;
}
/* check_pick sees if there is stuff to be picked up/picks up stuff.
* It returns 1 if the player should keep on moving, 0 if he should
* stop.
*/
int
check_pick (object *op)
{
object *tmp, *next;
int stop = 0;
int wvratio;
char putstring[128];
/* if you're flying, you cna't pick up anything */
if (op->move_type & MOVE_FLYING)
return 1;
next = op->below;
int cnt = MAX_ITEM_PER_DROP;
#define CHK_PICK_PICKUP do { pick_up (op, tmp); cnt--; } while (0)
/* loop while there are items on the floor that are not marked as
* destroyed */
while (next && !next->destroyed ())
{
tmp = next;
next = tmp->below;
if (cnt <= 0)
{
op->failmsg ("Couldn't pickup all items at once.");
return 0;
}
if (op->destroyed ())
return 0;
if (!can_pick (op, tmp))
continue;
if (op->contr->search_str[0] != '\0' && settings.search_items == TRUE)
{
if (item_matched_string (op, tmp, op->contr->search_str))
CHK_PICK_PICKUP;
continue;
}
/* high not bit set? We're using the old autopickup model */
if (!(op->contr->mode & PU_NEWMODE))
{
switch (op->contr->mode)
{
case 0:
return 1; /* don't pick up */
case 1:
CHK_PICK_PICKUP;
return 1;
case 2:
CHK_PICK_PICKUP;
return 0;
case 3:
return 0; /* stop before pickup */
case 4:
CHK_PICK_PICKUP;
break;
case 5:
CHK_PICK_PICKUP;
stop = 1;
break;
case 6:
if (QUERY_FLAG (tmp, FLAG_KNOWN_MAGICAL)
&& !QUERY_FLAG (tmp, FLAG_KNOWN_CURSED))
CHK_PICK_PICKUP;
break;
case 7:
if (tmp->type == MONEY || tmp->type == GEM)
CHK_PICK_PICKUP;
break;
default:
/* use value density */
if (!QUERY_FLAG (tmp, FLAG_UNPAID)
&& (query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * MAX (tmp->nrof, 1))) >= op->contr->mode)
CHK_PICK_PICKUP;
}
}
else
{ /* old model */
/* NEW pickup handling */
if (op->contr->mode & PU_DEBUG)
{
/* some debugging code to figure out item information */
if (tmp->name != NULL)
sprintf (putstring, "item name: %s item type: %d weight/value: %d",
&tmp->name, tmp->type, (int) (query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * MAX (tmp->nrof, 1))));
else
sprintf (putstring, "item name: %s item type: %d weight/value: %d",
&tmp->arch->archname, tmp->type, (int) (query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * MAX (tmp->nrof, 1))));
new_draw_info (NDI_UNIQUE, 0, op, putstring);
}
/* philosophy:
* It's easy to grab an item type from a pile, as long as it's
* generic. This takes no game-time. For more detailed pickups
* and selections, select-items should be used. This is a
* grab-as-you-run type mode that's really useful for arrows for
* example.
* The drawback: right now it has no frontend, so you need to
* stick the bits you want into a calculator in hex mode and then
* convert to decimal and then 'pickup <#>
*/
/* the first two modes are exclusive: if NOTHING we return, if
* STOP then we stop. All the rest are applied sequentially,
* meaning if any test passes, the item gets picked up. */
/* if mode is set to pick nothing up, return */
if (op->contr->mode & PU_NOTHING)
return 1;
/* if mode is set to stop when encountering objects, return */
/* take STOP before INHIBIT since it doesn't actually pick
* anything up */
if (op->contr->mode & PU_STOP)
return 0;
/* useful for going into stores and not losing your settings... */
/* and for battles wher you don't want to get loaded down while
* fighting */
if (op->contr->mode & PU_INHIBIT)
return 1;
/* prevent us from turning into auto-thieves :) */
if (QUERY_FLAG (tmp, FLAG_UNPAID))
continue;
/* ignore known cursed objects */
if (QUERY_FLAG (tmp, FLAG_KNOWN_CURSED) && op->contr->mode & PU_NOT_CURSED)
continue;
/* all food and drink if desired */
/* question: don't pick up known-poisonous stuff? */
if (op->contr->mode & PU_FOOD)
if (tmp->type == FOOD)
{
CHK_PICK_PICKUP;
continue;
}
if (op->contr->mode & PU_DRINK)
if (tmp->type == DRINK || (tmp->type == POISON && !QUERY_FLAG (tmp, FLAG_KNOWN_CURSED)))
{
CHK_PICK_PICKUP;
continue;
}
if (op->contr->mode & PU_POTION)
if (tmp->type == POTION)
{
CHK_PICK_PICKUP;
continue;
}
/* spellbooks, skillscrolls and normal books/scrolls */
if (op->contr->mode & PU_SPELLBOOK)
if (tmp->type == SPELLBOOK)
{
CHK_PICK_PICKUP;
continue;
}
if (op->contr->mode & PU_SKILLSCROLL)
if (tmp->type == SKILLSCROLL)
{
CHK_PICK_PICKUP;
continue;
}
if (op->contr->mode & PU_READABLES)
if (tmp->type == BOOK || tmp->type == SCROLL || tmp->type == INSCRIBABLE)
{
CHK_PICK_PICKUP;
continue;
}
/* wands/staves/rods/horns */
if (op->contr->mode & PU_MAGIC_DEVICE)
if (tmp->type == WAND || tmp->type == ROD || tmp->type == HORN)
{
CHK_PICK_PICKUP;
continue;
}
/* pick up all magical items */
if (op->contr->mode & PU_MAGICAL)
if (QUERY_FLAG (tmp, FLAG_KNOWN_MAGICAL) && !QUERY_FLAG (tmp, FLAG_KNOWN_CURSED))
{
CHK_PICK_PICKUP;
continue;
}
if (op->contr->mode & PU_VALUABLES)
{
if (tmp->type == MONEY || tmp->type == GEM)
{
CHK_PICK_PICKUP;
continue;
}
}
/* rings & amulets - talismans seems to be typed AMULET */
if (op->contr->mode & PU_JEWELS)
if (tmp->type == RING || tmp->type == AMULET)
{
CHK_PICK_PICKUP;
continue;
}
/* we don't forget dragon food */
if (op->contr->mode & PU_FLESH)
if (tmp->type == FLESH)
{
CHK_PICK_PICKUP;
continue;
}
/* bows and arrows. Bows are good for selling! */
if (op->contr->mode & PU_BOW)
if (tmp->type == BOW)
{
CHK_PICK_PICKUP;
continue;
}
if (op->contr->mode & PU_ARROW)
if (tmp->type == ARROW)
{
CHK_PICK_PICKUP;
continue;
}
/* all kinds of armor etc. */
if (op->contr->mode & PU_ARMOUR)
if (tmp->type == ARMOUR)
{
CHK_PICK_PICKUP;
continue;
}
if (op->contr->mode & PU_HELMET)
if (tmp->type == HELMET)
{
CHK_PICK_PICKUP;
continue;
}
if (op->contr->mode & PU_SHIELD)
if (tmp->type == SHIELD)
{
CHK_PICK_PICKUP;
continue;
}
if (op->contr->mode & PU_BOOTS)
if (tmp->type == BOOTS)
{
CHK_PICK_PICKUP;
continue;
}
if (op->contr->mode & PU_GLOVES)
if (tmp->type == GLOVES)
{
CHK_PICK_PICKUP;
continue;
}
if (op->contr->mode & PU_CLOAK)
if (tmp->type == CLOAK)
{
CHK_PICK_PICKUP;
continue;
}
/* hoping to catch throwing daggers here */
if (op->contr->mode & PU_MISSILEWEAPON)
if (tmp->type == WEAPON && QUERY_FLAG (tmp, FLAG_IS_THROWN))
{
CHK_PICK_PICKUP;
continue;
}
/* careful: chairs and tables are weapons! */
if (op->contr->mode & PU_ALLWEAPON)
{
if (tmp->type == WEAPON && tmp->name != NULL)
{
if (strstr (tmp->name, "table") == NULL && strstr (tmp->arch->archname, "table") == NULL &&
strstr (tmp->name, "chair") && strstr (tmp->arch->archname, "chair") == NULL)
{
CHK_PICK_PICKUP;
continue;
}
}
if (tmp->type == WEAPON && tmp->name == NULL)
{
if (strstr (tmp->arch->archname, "table") == NULL && strstr (tmp->arch->archname, "chair") == NULL)
{
CHK_PICK_PICKUP;
continue;
}
}
}
/* misc stuff that's useful */
if (op->contr->mode & PU_KEY)
if (tmp->type == KEY || tmp->type == SPECIAL_KEY)
{
CHK_PICK_PICKUP;
continue;
}
/* any of the last 4 bits set means we use the ratio for value
* pickups */
if (op->contr->mode & PU_RATIO)
{
/* use value density to decide what else to grab */
/* >=7 was >= op->contr->mode */
/* >=7 is the old standard setting. Now we take the last 4 bits
* and multiply them by 5, giving 0..15*5== 5..75 */
wvratio = (op->contr->mode & PU_RATIO) * 5;
if ((query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * MAX (tmp->nrof, 1))) >= (unsigned int) wvratio)
{
CHK_PICK_PICKUP;
#if 0
fprintf (stderr, "HIGH WEIGHT/VALUE [");
if (tmp->name != NULL)
{
fprintf (stderr, "%s", tmp->name);
}
else
fprintf (stderr, "%s", tmp->arch->archname);
fprintf (stderr, ",%d] = ", tmp->type);
fprintf (stderr, "%d\n", (int) (query_cost (tmp, op, F_TRUE) * 100 / (tmp->weight * MAX (tmp->nrof, 1))));
#endif
continue;
}
}
} /* the new pickup model */
}
return !stop;
}
/*
* Find an arrow in the inventory and after that
* in the right type container (quiver). Pointer to the
* found object is returned.
*/
object *
find_arrow (object *op, const char *type)
{
for (object *tmp = op->inv; tmp; tmp = tmp->below)
if (tmp->type == ARROW && !strcmp (&tmp->race, type))
return splay (tmp);
for (object *tmp = op->inv; tmp; tmp = tmp->below)
if (tmp->type == CONTAINER && QUERY_FLAG (tmp, FLAG_APPLIED) && !strcmp (&tmp->race, type))
if (object *arrow = find_arrow (tmp, type))
{
splay (tmp);
return arrow;
}
return 0;
}
/*
* Similar to find_arrow, but looks for (roughly) the best arrow to use
* against the target. A full test is not performed, simply a basic test
* of resistances. The archer is making a quick guess at what he sees down
* the hall. Failing that it does it's best to pick the highest plus arrow.
*/
object *
find_better_arrow (object *op, object *target, const char *type, int *better)
{
object *tmp = NULL, *arrow, *ntmp;
int attacknum, attacktype, betterby = 0, i;
if (!type)
return NULL;
for (arrow = op->inv; arrow; arrow = arrow->below)
{
if (arrow->type == CONTAINER && arrow->race == type && QUERY_FLAG (arrow, FLAG_APPLIED))
{
i = 0;
ntmp = find_better_arrow (arrow, target, type, &i);
if (i > betterby)
{
tmp = ntmp;
betterby = i;
}
}
else if (arrow->type == ARROW && arrow->race == type)
{
/* allways prefer assasination/slaying */
if (target->race != NULL && arrow->slaying != NULL && strstr (arrow->slaying, target->race))
{
if (arrow->attacktype & AT_DEATH)
{
*better = 100;
return arrow;
}
else
{
tmp = arrow;
betterby = (arrow->magic + arrow->stats.dam) * 2;
}
}
else
{
for (attacknum = 0; attacknum < NROFATTACKS; attacknum++)
{
attacktype = 1 << attacknum;
if ((arrow->attacktype & attacktype) && (target->arch->resist[attacknum]) < 0)
if (((arrow->magic + arrow->stats.dam) * (100 - target->arch->resist[attacknum]) / 100) > betterby)
{
tmp = arrow;
betterby = (arrow->magic + arrow->stats.dam) * (100 - target->arch->resist[attacknum]) / 100;
}
}
if ((2 + arrow->magic + arrow->stats.dam) > betterby)
{
tmp = arrow;
betterby = 2 + arrow->magic + arrow->stats.dam;
}
if (arrow->title && (1 + arrow->magic + arrow->stats.dam) > betterby)
{
tmp = arrow;
betterby = 1 + arrow->magic + arrow->stats.dam;
}
}
}
}
if (tmp == NULL && arrow == NULL)
return find_arrow (op, type);
*better = betterby;
return tmp;
}
/* looks in a given direction, finds the first valid target, and calls
* find_better_arrow to find a decent arrow to use.
* op = the shooter
* type = bow->race
* dir = fire direction
*/
object *
pick_arrow_target (object *op, const char *type, int dir)
{
object *tmp = NULL;
maptile *m;
int i, mflags, found, number;
sint16 x, y;
if (op->map == NULL)
return find_arrow (op, type);
/* do a dex check */
number = (die_roll (2, 40, op, PREFER_LOW) - 2) / 2;
if (number > (op->stats.Dex + (op->chosen_skill ? op->chosen_skill->level : op->level)))
return find_arrow (op, type);
m = op->map;
x = op->x;
y = op->y;
/* find the first target */
for (i = 0, found = 0; i < 20; i++)
{
x += freearr_x[dir];
y += freearr_y[dir];
mflags = get_map_flags (m, &m, x, y, &x, &y);
if (mflags & P_OUT_OF_MAP || mflags & P_BLOCKSVIEW)
{
tmp = NULL;
break;
}
else if (GET_MAP_MOVE_BLOCK (m, x, y) == MOVE_FLY_LOW)
{
/* This block presumes arrows and the like are MOVE_FLY_SLOW -
* perhaps a bad assumption.
*/
tmp = NULL;
break;
}
if (mflags & P_IS_ALIVE)
{
for (tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
if (QUERY_FLAG (tmp, FLAG_ALIVE))
{
found++;
break;
}
if (found)
break;
}
}
if (tmp == NULL)
return find_arrow (op, type);
if (tmp->head)
tmp = tmp->head;
return find_better_arrow (op, tmp, type, &i);
}
/*
* Creature fires a bow - op can be monster or player. Returns
* 1 if bow was actually fired, 0 otherwise.
* op is the object firing the bow.
* part is for multipart creatures - the part firing the bow.
* dir is the direction of fire.
* wc_mod is any special modifier to give (used in special player fire modes)
* sx, sy are coordinates to fire arrow from - also used in some of the special
* player fire modes.
*/
int
fire_bow (object *op, object *part, object *arrow, int dir, int wc_mod, sint16 sx, sint16 sy)
{
object *left, *bow;
int mflags;
maptile *m;
if (!dir)
{
new_draw_info (NDI_UNIQUE, 0, op, "You can't shoot yourself!");
return 0;
}
if (op->contr)
bow = op->current_weapon;
else
{
for (bow = op->inv; bow; bow = bow->below)
/* Don't check for applied - monsters don't apply bows - in that way, they
* don't need to switch back and forth between bows and weapons.
*/
if (bow->type == BOW)
break;
if (!bow)
{
LOG (llevError, "Range: bow without activated bow (%s).\n", &op->name);
return 0;
}
// optimisation: move object to top so we will find it quickly again
if (bow->below)
{
bow->remove ();
op->insert (bow);
}
}
if (!bow->race || !bow->skill)
{
new_draw_info_format (NDI_UNIQUE, 0, op, "Your %s is broken.", &bow->name);
return 0;
}
if (arrow == NULL)
{
if ((arrow = find_arrow (op, bow->race)) == NULL)
{
if (op->type == PLAYER)
new_draw_info_format (NDI_UNIQUE, 0, op, "You have no %s left.", &bow->race);
/* FLAG_READY_BOW will get reset if the monsters picks up some arrows */
else
CLEAR_FLAG (op, FLAG_READY_BOW);
return 0;
}
}
mflags = get_map_flags (op->map, &m, sx, sy, &sx, &sy);
if (mflags & P_OUT_OF_MAP)
return 0;
if (GET_MAP_MOVE_BLOCK (m, sx, sy) == MOVE_FLY_LOW)
{
new_draw_info (NDI_UNIQUE, 0, op, "Something is in the way.");
return 0;
}
/* this should not happen, but sometimes does */
if (arrow->nrof == 0)
{
arrow->destroy ();
return 0;
}
left = arrow; /* these are arrows left to the player */
arrow = get_split_ob (arrow, 1);
if (!arrow)
{
new_draw_info_format (NDI_UNIQUE, 0, op, "You have no %s left.", &bow->race);
return 0;
}
arrow->set_owner (op);
arrow->skill = bow->skill;
arrow->direction = dir;
arrow->stats.sp = arrow->stats.wc; /* save original wc, dam, attacktype and slaying */
arrow->stats.hp = arrow->stats.dam;
arrow->stats.grace = arrow->attacktype;
if (arrow->slaying)
arrow->spellarg = strdup (arrow->slaying);
#if 0
if (player *pl = op->contr)
{
float speed = pl->weapon_sp;
/* penalize ROF for bestarrow */
if (pl->bowtype == bow_bestarrow)
speed *= .9f;
else
speed *= 1.f + dex_bonus[op->stats.Dex] * .2f;
op->speed_left += speed - op->speed;
}
#endif
SET_ANIMATION (arrow, arrow->direction);
/* update the speed */
arrow->speed = ((bow->flag [FLAG_NO_STRENGTH] ? 0 : dam_bonus[op->stats.Str]) + bow->magic + arrow->magic) / 5.f
+ bow->stats.dam / 7.f;
arrow->set_speed (max (arrow->speed, 2.f));
arrow->speed_left = 0;
int wc = op->stats.wc + wc_mod - arrow->magic - arrow->stats.wc;
if (op->type == PLAYER)
{
arrow->level = op->chosen_skill ? op->chosen_skill->level : op->level;
wc -= dex_bonus[op->stats.Dex];
if (!arrow->slaying)
arrow->slaying = op->slaying;
arrow->attacktype |= op->attacktype;
}
else
{
arrow->level = op->level;
arrow->stats.wc -= bow->magic;
if (!arrow->slaying)
arrow->slaying = bow->slaying;
arrow->attacktype |= bow->attacktype;
}
wc -= arrow->level;
arrow->stats.dam = clamp (arrow->stats.dam + op->stats.dam + arrow->magic, MIN_DAM, MAX_DAM);
arrow->stats.wc = clamp (wc, MIN_WC, MAX_WC);
arrow->move_type = MOVE_FLY_LOW;
arrow->move_on = MOVE_FLY_LOW | MOVE_WALK;
op->play_sound (sound_find ("fire_arrow"));
m->insert (arrow, sx, sy, op);
if (!arrow->destroyed ())
move_arrow (arrow);
if (op->type == PLAYER)
{
if (left->destroyed ())
esrv_del_item (op->contr, left->count);
else
esrv_send_item (op, left);
}
return 1;
}
/* Special fire code for players - this takes into
* account the special fire modes players can have
* but monsters can't. Putting that code here
* makes the fire_bow code much cleaner.
* this function should only be called if 'op' is a player,
* hence the function name.
*/
int
player_fire_bow (object *op, int dir)
{
int ret = 0, wcmod = 0;
if (op->contr->bowtype == bow_bestarrow)
{
ret = fire_bow (op, op, pick_arrow_target (op, op->contr->ranged_ob->race, dir), dir, 0, op->x, op->y);
}
else if (op->contr->bowtype >= bow_n && op->contr->bowtype <= bow_nw)
{
if (!similar_direction (dir, op->contr->bowtype - bow_n + 1))
wcmod = -1;
ret = fire_bow (op, op, NULL, op->contr->bowtype - bow_n + 1, wcmod, op->x, op->y);
}
else if (op->contr->bowtype == bow_threewide)
{
ret = fire_bow (op, op, NULL, dir, 0, op->x, op->y);
ret |= fire_bow (op, op, NULL, dir, -5, op->x + freearr_x[absdir (dir + 2)], op->y + freearr_y[absdir (dir + 2)]);
ret |= fire_bow (op, op, NULL, dir, -5, op->x + freearr_x[absdir (dir - 2)], op->y + freearr_y[absdir (dir - 2)]);
}
else if (op->contr->bowtype == bow_spreadshot)
{
ret |= fire_bow (op, op, NULL, dir, 0, op->x, op->y);
ret |= fire_bow (op, op, NULL, absdir (dir - 1), -5, op->x, op->y);
ret |= fire_bow (op, op, NULL, absdir (dir + 1), -5, op->x, op->y);
}
else
{
/* Simple case */
ret = fire_bow (op, op, NULL, dir, 0, op->x, op->y);
}
return ret;
}
/* Fires a misc (wand/rod/horn) object in 'dir'.
* Broken apart from 'fire' to keep it more readable.
*/
void
fire_misc_object (object *op, int dir)
{
object *item = op->contr->ranged_ob;
if (!item)
{
new_draw_info (NDI_UNIQUE, 0, op, "You have range item readied.");
return;
}
if (!item->inv)
{
LOG (llevError, "Object %s lacks a spell\n", &item->name);
return;
}
if (!op->change_weapon (item))
return;
if (item->type == WAND)
{
if (item->stats.food <= 0)
{
op->contr->play_sound (sound_find ("wand_poof"));
new_draw_info_format (NDI_UNIQUE, 0, op, "The %s goes poof.", query_base_name (item, 0));
return;
}
}
else if (item->type == ROD || item->type == HORN)
{
if (item->stats.hp < MAX (item->inv->stats.sp, item->inv->stats.grace))
{
op->contr->play_sound (sound_find ("wand_poof"));
if (item->type == ROD)
new_draw_info_format (NDI_UNIQUE, 0, op, "The %s whines for a while, but nothing happens.", query_base_name (item, 0));
else
new_draw_info_format (NDI_UNIQUE, 0, op, "The %s needs more time to charge.", query_base_name (item, 0));
return;
}
}
if (cast_spell (op, item, dir, item->inv, NULL))
{
SET_FLAG (op, FLAG_BEEN_APPLIED); /* You now know something about it */
if (item->type == WAND)
{
if (!(--item->stats.food))
{
object *tmp;
if (item->arch)
{
CLEAR_FLAG (item, FLAG_ANIMATE);
item->face = item->arch->face;
item->set_speed (0);
}
if ((tmp = item->in_player ()))
esrv_update_item (UPD_ANIM, tmp, item);
}
}
else if (item->type == ROD || item->type == HORN)
drain_rod_charge (item);
}
}
/* Received a fire command for the player - go and do it.
*/
bool
fire (object *op, int dir)
{
int spellcost = 0;
/* check for loss of invisiblity/hide */
if (action_makes_visible (op))
make_visible (op);
player *pl = op->contr;
if (pl->golem)
{
control_golem (op->contr->golem, dir);
return false;
}
object *ob = pl->ranged_ob;
if (!ob)
return false;
if (!op->change_weapon (ob))
return false;
if (op->speed_left > 0.f)
--op->speed_left;
else
return false;
switch (ob->type)
{
case BOW:
player_fire_bow (op, dir);
break;
case SPELL:
spellcost = cast_spell (op, op, dir, ob, pl->spellparam[0] ? pl->spellparam : 0);
break;
case BUILDER:
apply_map_builder (op, dir);
break;
case SKILL:
do_skill (op, op, ob, dir, 0);
break;
default:
fire_misc_object (op, dir);
break;
}
return true;
}
/* find_key
* We try to find a key for the door as passed. If we find a key
* and successfully use it, we return the key, otherwise NULL
* This function merges both normal and locked door, since the logic
* for both is the same - just the specific key is different.
* pl is the player,
* inv is the objects inventory to searched
* door is the door we are trying to match against.
* This function can be called recursively to search containers.
*/
object *
find_key (object *pl, object *container, object *door)
{
object *tmp, *key;
/* Should not happen, but sanity checking is never bad */
if (!container->inv)
return 0;
/* First, lets try to find a key in the top level inventory */
for (tmp = container->inv; tmp; tmp = tmp->below)
{
if (door->type == DOOR && tmp->type == KEY)
break;
/* For sanity, we should really check door type, but other stuff
* (like containers) can be locked with special keys
*/
if (tmp->slaying && tmp->type == SPECIAL_KEY && tmp->slaying == door->slaying)
break;
}
/* No key found - lets search inventories now */
/* If we find and use a key in an inventory, return at that time.
* otherwise, if we search all the inventories and still don't find
* a key, return
*/
if (!tmp)
{
for (tmp = container->inv; tmp; tmp = tmp->below)
{
/* No reason to search empty containers */
if (tmp->type == CONTAINER && tmp->inv)
{
if ((key = find_key (pl, tmp, door)))
return key;
}
}
if (!tmp)
return NULL;
}
/* We get down here if we have found a key. Now if its in a container,
* see if we actually want to use it
*/
if (pl != container)
{
/* Only let players use keys in containers */
if (!pl->contr)
return NULL;
/* cases where this fails:
* If we only search the player inventory, return now since we
* are not in the players inventory.
* If the container is not active, return now since only active
* containers can be used.
* If we only search keyrings and the container does not have
* a race/isn't a keyring.
* No checking for all containers - to fall through past here,
* inv must have been an container and must have been active.
*
* Change the color so that the message doesn't disappear with
* all the others.
*/
if (pl->contr->usekeys == key_inventory ||
!QUERY_FLAG (container, FLAG_APPLIED) ||
(pl->contr->usekeys == keyrings && (!container->race || strcmp (container->race, "keys"))))
{
new_draw_info_format (NDI_UNIQUE | NDI_BROWN, 0, pl,
"The %s in your %s vibrates as you approach the door", query_name (tmp), query_name (container));
return NULL;
}
}
return tmp;
}
/* moved door processing out of move_player_attack.
* returns 1 if player has opened the door with a key
* such that the caller should not do anything more,
* 0 otherwise
*/
static int
player_attack_door (object *op, object *door)
{
/* If its a door, try to find a key. If we do destroy the door,
* might as well return immediately as there is nothing more to do -
* otherwise, we fall through to the rest of the code.
*/
object *key = find_key (op, op, door);
/* If we found a key, do some extra work */
if (key)
{
object *container = key->env;
if (action_makes_visible (op))
make_visible (op);
if (door->inv && (door->inv->type == RUNE || door->inv->type == TRAP))
spring_trap (door->inv, op);
if (door->type == DOOR)
hit_player (door, 9998, op, AT_PHYSICAL, 1); /* Break through the door */
else if (door->type == LOCKED_DOOR)
{
op->statusmsg (format ("You open the door with the %s", query_short_name (key)), NDI_BROWN);
remove_door2 (door); /* remove door without violence ;-) */
}
/* Do this after we print the message */
key->decrease (); /* Use up one of the keys */
/* Need to update the weight the container the key was in */
if (container != op)
esrv_update_item (UPD_WEIGHT, op, container);
return 1; /* Nothing more to do below */
}
else if (door->type == LOCKED_DOOR)
{
/* Might as well return now - no other way to open this */
op->failmsg (door->msg ? &door->msg : "Hmm, you miss the certain something to open this door...");
return 1;
}
return 0;
}
/* This function is just part of a breakup from move_player.
* It should keep the code cleaner.
* When this is called, the players direction has been updated
* (taking into account confusion.) The player is also actually
* going to try and move (not fire weapons).
*/
bool
move_player_attack (object *op, int dir)
{
int on_battleground;
sint16 nx = freearr_x[dir] + op->x;
sint16 ny = freearr_y[dir] + op->y;
on_battleground = op_on_battleground (op, 0, 0);
if (out_of_map (op->map, nx, ny))
return false;
if (!op->contr->braced && op->speed_left > 0.f && move_ob (op, dir, op))
{
--op->speed_left;
return true;
}
/* If braced, or can't move to the square, and it is not out of the
* map, attack it. Note order of if statement is important - don't
* want to be calling move_ob if braced, because move_ob will move the
* player. This is a pretty nasty hack, because if we could
* move to some space, it then means that if we are braced, we should
* do nothing at all. As it is, if we are braced, we go through
* quite a bit of processing. However, it probably is less than what
* move_ob uses.
*/
maptile *m = op->map->xy_find (nx, ny);
/* Go through all the objects, and find ones of interest. Only stop if
* we find a monster - that is something we know we want to attack.
* if its a door or barrel (can roll) see if there may be monsters
* on the space
*/
object *mon;
for (mon = m->at (nx, ny).bot; mon; mon = mon->above)
{
if ((mon->flag [FLAG_ALIVE]
|| mon->type == LOCKED_DOOR
|| mon->flag [FLAG_CAN_ROLL])
&& mon != op)
break;
}
if (!mon) /* This happens anytime the player tries to move */
return false; /* into a wall */
mon = mon->head_ ();
if ((mon->type == DOOR && mon->stats.hp >= 0) || (mon->type == LOCKED_DOOR))
if (op->contr->weapon_sp_left > 0.f)
if (player_attack_door (op, mon))
{
--op->contr->weapon_sp_left;
return true;
}
/* The following deals with possibly attacking peaceful
* or friendly creatures. Basically, all players are considered
* unaggressive. If the moving player has peaceful set, then the
* object should be pushed instead of attacked. It is assumed that
* if you are braced, you will not attack friends accidently,
* and thus will not push them.
*/
/* If the creature is a pet, push it even if the player is not
* peaceful. Our assumption is the creature is a pet if the
* player owns it and it is either friendly or unagressive.
*/
if (op->type == PLAYER
&& ((mon->owner && mon->owner->contr
&& same_party (mon->owner->contr->party, op->contr->party))
|| mon->owner == op)
&& (QUERY_FLAG (mon, FLAG_UNAGGRESSIVE) || QUERY_FLAG (mon, FLAG_FRIENDLY)))
{
/* If we're braced, we don't want to switch places with it */
if (op->contr->braced)
return false;
if (op->speed_left > 0.f)
{
--op->speed_left;
op->play_sound (sound_find ("push_player"));
push_ob (mon, dir, op);
if (op->contr->tmp_invis || op->hide)
make_visible (op);
return true;
}
else
return false;
}
/* in certain circumstances, you shouldn't attack friendly
* creatures. Note that if you are braced, you can't push
* someone, but put it inside this loop so that you won't
* attack them either.
*/
if ((mon->type == PLAYER || mon->enemy != op)
&& (mon->type == PLAYER || QUERY_FLAG (mon, FLAG_UNAGGRESSIVE) || QUERY_FLAG (mon, FLAG_FRIENDLY))
&& ((op->contr->peaceful
|| (mon->type == PLAYER && mon->contr->peaceful))
&& !on_battleground))
{
if (op->speed_left > 0.f)
{
--op->speed_left;
if (!op->contr->braced)
{
op->play_sound (sound_find ("push_player"));
push_ob (mon, dir, op);
}
else
op->statusmsg ("You withhold your attack");
if (op->contr->tmp_invis || op->hide)
make_visible (op);
return true;
}
}
/* If the object is a boulder or other rollable object, then
* roll it if not braced. You can't roll it if you are braced.
*/
else if (QUERY_FLAG (mon, FLAG_CAN_ROLL) && (!op->contr->braced))
{
if (op->speed_left > 0.f)
{
--op->speed_left;
recursive_roll (mon, dir, op);
if (action_makes_visible (op))
make_visible (op);
return true;
}
}
/* Any generic living creature. Including things like doors.
* Way it works is like this: First, it must have some hit points
* and be living. Then, it must be one of the following:
* 1) Not a player, 2) A player, but of a different party. Note
* that party_number -1 is no party, so attacks can still happen.
*/
else if ((mon->stats.hp >= 0) && QUERY_FLAG (mon, FLAG_ALIVE) &&
((mon->type != PLAYER || op->contr->party == NULL || op->contr->party != mon->contr->party)))
{
if (op->contr->weapon_sp_left > 0.f && !op->flag [FLAG_WIZPASS])
{
--op->contr->weapon_sp_left;
skill_attack (mon, op, 0, 0, 0);
if (action_makes_visible (op))
make_visible (op);
return true;
}
}
return false;
}
bool
move_player (object *op, int dir)
{
int pick;
if (!op->map || op->map->in_memory != MAP_ACTIVE)
return 0;
/* Sanity check: make sure dir is valid */
if ((dir < 0) || (dir >= 9))
{
LOG (llevError, "move_player: invalid direction %d\n", dir);
return 0;
}
/* peterm: added following line */
if (QUERY_FLAG (op, FLAG_CONFUSED) && dir)
dir = absdir (dir + rndm (3) + rndm (3) - 2);
op->facing = dir;
if (op->hide)
do_hidden_move (op);
bool retval;
if (INVOKE_PLAYER (MOVE, op->contr, ARG_INT (dir)))
retval = RESULT_INT (0);
else if (op->contr->fire_on)
retval = fire (op, dir);
else
{
retval = move_player_attack (op, dir);
pick = check_pick (op);
}
/* Add special check for newcs players and fire on - this way, the
* server can handle repeat firing.
*/
if (op->contr->fire_on || (op->contr->run_on && pick != 0))
op->direction = dir;
else
op->direction = 0;
/* Update how the player looks. Use the facing, so direction may
* get reset to zero. This allows for full animation capabilities
* for players.
*/
animate_object (op, op->facing);
return retval;
}
/* This is similar to handle_player, below, but is only used by the
* new client/server stuff.
* This is sort of special, in that the new client/server actually uses
* the new speed values for commands.
*
* Returns true if there are more actions we can do. Should not do
* many actions in a row, as that would be too unfair to other
* players.
*/
bool
handle_newcs_player (object *op)
{
if (QUERY_FLAG (op, FLAG_SCARED))
{
if (op->speed_left > 0.f)
{
--op->speed_left;
flee_player (op);
return true;
}
else
return false;
}
/* call this here - we also will call this in do_ericserver, but
* the players time has been increased when doericserver has been
* called, so we recheck it here.
*/
if (op->contr->ns->handle_command ())
return true;
if (op->direction && (op->contr->run_on || op->contr->fire_on))
return move_player (op, op->direction);
return false;
}
int
save_life (object *op)
{
if (!QUERY_FLAG (op, FLAG_LIFESAVE))
return 0;
for (object *tmp = op->inv; tmp; tmp = tmp->below)
if (QUERY_FLAG (tmp, FLAG_APPLIED) && QUERY_FLAG (tmp, FLAG_LIFESAVE))
{
op->play_sound (sound_find ("ob_evaporate"));
new_draw_info_format (NDI_UNIQUE, 0, op, "Your %s vibrates violently, then evaporates.", query_name (tmp));
if (op->contr)
esrv_del_item (op->contr, tmp->count);
tmp->destroy ();
CLEAR_FLAG (op, FLAG_LIFESAVE);
if (op->stats.hp < 0)
op->stats.hp = op->stats.maxhp;
if (op->stats.food < 0)
op->stats.food = 999;
op->update_stats ();
return 1;
}
LOG (llevError, "Error: LIFESAVE set without applied object.\n");
CLEAR_FLAG (op, FLAG_LIFESAVE);
enter_player_savebed (op); /* bring him home. */
return 0;
}
/* This goes throws the inventory and removes unpaid objects, and puts them
* back in the map (location and map determined by values of env). This
* function will descend into containers. op is the object to start the search
* from.
*/
static void
drop_unpaid_items (object *op, object *env)
{
while (op)
{
object *next = op->below; /* Make sure we have a good value, in case we remove object 'op' */
if (QUERY_FLAG (op, FLAG_UNPAID))
{
if (env->type == PLAYER)
esrv_del_item (env->contr, op->count);
op->insert_at (env);
}
else if (op->inv)
drop_unpaid_items (op->inv, env);
op = next;
}
}
void
object::drop_unpaid_items ()
{
if (!flag [FLAG_REMOVED])
::drop_unpaid_items (inv, this);
}
/*
* Returns pointer a static string containing gravestone text
* Moved from apply.c to player.c - player.c is what
* actually uses this function. player.c may not be quite the
* best, a misc file for object actions is probably better,
* but there isn't one in the server directory.
*/
char *
gravestone_text (object *op)
{
static char buf2[MAX_BUF];
char buf[MAX_BUF];
time_t now = time (NULL);
strcpy (buf2, " R.I.P.\n\n");
if (op->type == PLAYER)
sprintf (buf, "%s the %s\n", &op->name, op->contr->title);
else
sprintf (buf, "%s\n", &op->name);
strncat (buf2, " ", 20 - strlen (buf) / 2);
strcat (buf2, buf);
if (op->type == PLAYER)
sprintf (buf, "who was in level %d when killed\n", op->level);
else
sprintf (buf, "who was in level %d when died.\n\n", op->level);
strncat (buf2, " ", 20 - strlen (buf) / 2);
strcat (buf2, buf);
if (op->type == PLAYER)
{
sprintf (buf, "by %s.\n\n", op->contr->killer);
strncat (buf2, " ", 21 - strlen (buf) / 2);
strcat (buf2, buf);
}
strftime (buf, MAX_BUF, "%b %d %Y\n", localtime (&now));
strncat (buf2, " ", 20 - strlen (buf) / 2);
strcat (buf2, buf);
return buf2;
}
void
do_some_living (object *op)
{
int last_food = op->stats.food;
int gen_hp, gen_sp, gen_grace;
int over_hp, over_sp, over_grace;
int i;
int rate_hp = 1200;
int rate_sp = 2500;
int rate_grace = 2000;
const int max_hp = 1;
const int max_sp = 1;
const int max_grace = 1;
if (op->contr->hidden)
{
op->invisible = 1000;
/* the socket code flashes the player visible/invisible
* depending on the value of invisible, so we need to
* alternate it here for it to work correctly.
*/
if (pticks & 2)
op->invisible--;
}
else if (op->invisible && !(QUERY_FLAG (op, FLAG_MAKE_INVIS)))
{
if (!op->invisible--)
{
make_visible (op);
new_draw_info (NDI_UNIQUE, 0, op, "Your invisibility spell runs out.");
}
}
if (op->contr->ns->state == ST_PLAYING)
{
/* these next three if clauses make it possible to SLOW DOWN
hp/grace/spellpoint regeneration. */
if (op->contr->gen_hp >= 0)
gen_hp = (op->contr->gen_hp + 1) * op->stats.maxhp;
else
{
gen_hp = op->stats.maxhp;
rate_hp -= rate_hp / 2 * op->contr->gen_hp;
}
if (op->contr->gen_sp >= 0)
gen_sp = (op->contr->gen_sp + 1) * op->stats.maxsp;
else
{
gen_sp = op->stats.maxsp;
rate_sp -= rate_sp / 2 * op->contr->gen_sp;
}
if (op->contr->gen_grace >= 0)
gen_grace = (op->contr->gen_grace + 1) * op->stats.maxgrace;
else
{
gen_grace = op->stats.maxgrace;
rate_grace -= rate_grace / 2 * op->contr->gen_grace;
}
/* Regenerate Grace */
/* I altered this a little - maximum grace is ony achieved through prayer -b.t. */
if (--op->last_grace < 0)
{
if (op->stats.grace < op->stats.maxgrace / 2)
op->stats.grace++; /* no penalty in food for regaining grace */
if (max_grace > 1)
{
over_grace = (gen_grace < 20 ? 30 : gen_grace + 10) / rate_grace;
if (over_grace > 0)
{
op->stats.sp += over_grace
+ (random_roll (0, rate_grace - 1, op, PREFER_HIGH) > ((gen_grace < 20 ? 30 : gen_grace + 10) % rate_grace)) ? -1 : 0;
op->last_grace = 0;
}
else
{
op->last_grace = rate_grace / (gen_grace < 20 ? 30 : gen_grace + 10);
}
}
else
{
op->last_grace = rate_grace / (gen_grace < 20 ? 30 : gen_grace + 10);
}
/* wearing stuff doesn't detract from grace generation. */
}
if (op->stats.food > 0)
{
/* Regenerate Spell Points */
if (!op->contr->golem && --op->last_sp < 0)
{
gen_sp = gen_sp * 10 / (op->contr->gen_sp_armour < 10 ? 10 : op->contr->gen_sp_armour);
if (op->stats.sp < op->stats.maxsp)
{
op->stats.sp++;
/* dms do not consume food */
if (!QUERY_FLAG (op, FLAG_WIZ))
{
op->stats.food--;
if (op->contr->digestion < 0)
op->stats.food += op->contr->digestion;
else if (op->contr->digestion > 0 && random_roll (0, op->contr->digestion, op, PREFER_HIGH))
op->stats.food = last_food;
}
}
if (max_sp > 1)
{
over_sp = (gen_sp + 10) / rate_sp;
if (over_sp > 0)
{
if (op->stats.sp < op->stats.maxsp)
{
op->stats.sp += over_sp > max_sp ? max_sp : over_sp;
if (random_roll (0, rate_sp - 1, op, PREFER_LOW) > ((gen_sp + 10) % rate_sp))
op->stats.sp--;
if (op->stats.sp > op->stats.maxsp)
op->stats.sp = op->stats.maxsp;
}
op->last_sp = 0;
}
else
op->last_sp = rate_sp / (gen_sp < 20 ? 30 : gen_sp + 10);
}
else
op->last_sp = rate_sp / (gen_sp < 20 ? 30 : gen_sp + 10);
}
/* Regenerate Hit Points */
if (--op->last_heal < 0)
{
if (op->stats.hp < op->stats.maxhp)
{
op->stats.hp++;
/* dms do not consume food */
if (!QUERY_FLAG (op, FLAG_WIZ))
{
op->stats.food--;
if (op->contr->digestion < 0)
op->stats.food += op->contr->digestion;
else if (op->contr->digestion > 0 && random_roll (0, op->contr->digestion, op, PREFER_HIGH))
op->stats.food = last_food;
}
}
if (max_hp > 1)
{
over_hp = (gen_hp < 20 ? 30 : gen_hp + 10) / rate_hp;
if (over_hp > 0)
{
op->stats.sp += over_hp + (RANDOM () % rate_hp > ((gen_hp < 20 ? 30 : gen_hp + 10) % rate_hp)) ? -1 : 0;
op->last_heal = 0;
}
else
op->last_heal = rate_hp / (gen_hp < 20 ? 30 : gen_hp + 10);
}
else
op->last_heal = rate_hp / (gen_hp < 20 ? 30 : gen_hp + 10);
}
}
/* Digestion */
if (--op->last_eat < 0)
{
int bonus = max (0, op->contr->digestion),
penalty = max (0, -op->contr->digestion);
op->last_eat = 25 * (1 + bonus) / (max (0, op->contr->gen_hp) + penalty + 1);
/* dms do not consume food */
if (!QUERY_FLAG (op, FLAG_WIZ))
op->stats.food--;
}
if (op->stats.food < 0 && op->stats.hp >= 0)
{
object *tmp, *flesh = 0;
for (tmp = op->inv; tmp; tmp = tmp->below)
{
if (!QUERY_FLAG (tmp, FLAG_UNPAID))
{
if (tmp->type == FOOD || tmp->type == DRINK || tmp->type == POISON)
{
new_draw_info (NDI_UNIQUE, 0, op, "You blindly grab for a bite of food.");
manual_apply (op, tmp, 0);
if (op->stats.food >= 0 || op->stats.hp < 0)
break;
}
else if (tmp->type == FLESH)
flesh = tmp;
} /* End if paid for object */
} /* end of for loop */
/* If player is still starving, it means they don't have any food, so
* eat flesh instead.
*/
if (op->stats.food < 0 && op->stats.hp >= 0 && flesh)
{
new_draw_info (NDI_UNIQUE, 0, op, "You blindly grab for a bite of food.");
manual_apply (op, flesh, 0);
}
}
if (op->stats.food < 0)
{
op->stats.hp += op->stats.food;
op->stats.food = 0;
}
if (op->stats.hp < 0 && !QUERY_FLAG (op, FLAG_WIZ))
kill_player (op);
}
}
/* If the player should die (lack of hp, food, etc), we call this.
* op is the player in jeopardy. If the player can not be saved (not
* permadeath, no lifesave), this will take care of removing the player
* file.
*/
void
kill_player (object *op)
{
int x, y;
char buf[MAX_BUF];
maptile *map; /* this is for resurrection */
int will_kill_again;
archetype *at;
object *tmp;
if (save_life (op))
return;
/* If player dies on BATTLEGROUND, no stat/exp loss! For Combat-Arenas
* in cities ONLY!!! It is very important that this doesn't get abused.
* Look at op_on_battleground() for more info --AndreasV
*/
if (op_on_battleground (op, &x, &y))
{
new_draw_info (NDI_UNIQUE | NDI_NAVY, 0, op, "You have been defeated in combat!");
new_draw_info (NDI_UNIQUE | NDI_NAVY, 0, op, "Local medics have saved your life...");
/* restore player */
at = archetype::find ("poisoning");
if (object *tmp = present_arch_in_ob (at, op))
{
tmp->destroy ();
new_draw_info (NDI_UNIQUE, 0, op, "Your body feels cleansed");
}
at = archetype::find ("confusion");
if (object *tmp = present_arch_in_ob (at, op))
{
tmp->destroy ();
new_draw_info (NDI_UNIQUE, 0, tmp, "Your mind feels clearer");
}
cure_disease (op, 0, 0); /* remove any disease */
op->stats.hp = op->stats.maxhp;
if (op->stats.food <= 0)
op->stats.food = 999;
/* create a bodypart-trophy to make the winner happy */
if (object *tmp = arch_to_object (archetype::find ("finger")))
{
tmp->name = format ("%s's finger" , &op->name);
tmp->name_pl = format ("%s's fingers", &op->name);
tmp->msg = format (
"This finger has been cut off of %s the %s, when he was defeated at level %d by %s.\n",
&op->name, op->contr->title, (int) (op->level), op->contr->killer
);
tmp->value = 0, tmp->type = 0;
tmp->materialname = "organics";
tmp->insert_at (op, tmp);
}
/* teleport defeated player to new destination */
transfer_ob (op, x, y, 0, NULL);
op->contr->braced = 0;
return;
}
INVOKE_PLAYER (DEATH, op->contr);
command_kill_pets (op, 0);
if (op->stats.food < 0)
strcpy (op->contr->killer, "starvation");
op->contr->play_sound (sound_find ("player_dies"));
/* save the map location for corpse, gravestone */
x = op->x;
y = op->y;
map = op->map;
/* NOT_PERMADEATH code. This basically brings the character back to
* life if they are dead - it takes some exp and a random stat.
* See the config.h file for a little more in depth detail about this.
*/
/* Basically two ways to go - remove a stat permanently, or just
* make it depletion. This bunch of code deals with that aspect
* of death.
*/
#ifndef COZY_SERVER
if (settings.balanced_stat_loss)
{
/* If stat loss is permanent, lose one stat only. */
/* Lower level chars don't lose as many stats because they suffer
more if they do. */
/* Higher level characters can afford things such as potions of
restoration, or better, stat potions. So we slug them that
little bit harder. */
/* GD */
if (settings.stat_loss_on_death)
num_stats_lose = 1;
else
num_stats_lose = 1 + op->level / BALSL_NUMBER_LOSSES_RATIO;
}
else
num_stats_lose = 1;
lost_a_stat = 0;
for (z = 0; z < num_stats_lose; z++)
{
i = RANDOM () % NUM_STATS;
if (settings.stat_loss_on_death)
{
/* Pick a random stat and take a point off it. Tell the player
* what he lost.
*/
change_attr_value (&(op->stats), i, -1);
check_stat_bounds (&(op->stats));
change_attr_value (&(op->contr->orig_stats), i, -1);
check_stat_bounds (&(op->contr->orig_stats));
new_draw_info (NDI_UNIQUE, 0, op, lose_msg[i]);
lost_a_stat = 1;
}
else
{
/* deplete a stat */
archetype *deparch = archetype::find ("depletion");
object *dep;
dep = present_arch_in_ob (deparch, op);
if (!dep)
{
dep = arch_to_object (deparch);
insert_ob_in_ob (dep, op);
}
lose_this_stat = 1;
if (settings.balanced_stat_loss)
{
/* GD */
/* Get the stat that we're about to deplete. */
this_stat = get_attr_value (&(dep->stats), i);
if (this_stat < 0)
{
int loss_chance = 1 + op->level / BALSL_LOSS_CHANCE_RATIO;
int keep_chance = this_stat * this_stat;
/* Yes, I am paranoid. Sue me. */
if (keep_chance < 1)
keep_chance = 1;
/* There is a maximum depletion total per level. */
if (this_stat < -1 - op->level / BALSL_MAX_LOSS_RATIO)
{
lose_this_stat = 0;
/* Take loss chance vs keep chance to see if we
retain the stat. */
}
else
{
if (random_roll (0, loss_chance + keep_chance - 1, op, PREFER_LOW) < keep_chance)
lose_this_stat = 0;
/* LOG(llevDebug, "Determining stat loss. Stat: %d Keep: %d Lose: %d Result: %s.\n",
this_stat, keep_chance, loss_chance,
lose_this_stat?"LOSE":"KEEP"); */
}
}
}
if (lose_this_stat)
{
this_stat = get_attr_value (&(dep->stats), i);
/* We could try to do something clever like find another
* stat to reduce if this fails. But chances are, if
* stats have been depleted to -50, all are pretty low
* and should be roughly the same, so it shouldn't make a
* difference.
*/
if (this_stat >= -50)
{
change_attr_value (&(dep->stats), i, -1);
SET_FLAG (dep, FLAG_APPLIED);
new_draw_info (NDI_UNIQUE, 0, op, lose_msg[i]);
op->update_stats ();
lost_a_stat = 1;
}
}
}
}
/* If no stat lost, tell the player. */
if (!lost_a_stat)
{
/* determine_god() seems to not work sometimes... why is this?
Should I be using something else? GD */
const char *god = determine_god (op);
if (god && (strcmp (god, "none")))
new_draw_info_format (NDI_UNIQUE, 0, op, "For a brief moment you feel the holy presence of %s protecting" " you.", god);
else
new_draw_info (NDI_UNIQUE, 0, op, "For a brief moment you feel a holy presence protecting you.");
}
#else
new_draw_info (NDI_UNIQUE, 0, op, "For a brief moment you" " feel a holy presence protecting you from losing yourself completely.");
#endif
/* Put a gravestone up where the character 'almost' died. List the
* exp loss on the stone.
*/
tmp = arch_to_object (archetype::find ("gravestone"));
sprintf (buf, "%s's gravestone", &op->name);
tmp->name = buf;
sprintf (buf, "%s's gravestones", &op->name);
tmp->name_pl = buf;
sprintf (buf, "RIP\nHere rests the hero %s the %s,\n" "who was killed\n" "by %s.\n", &op->name, op->contr->title, op->contr->killer);
tmp->msg = buf;
tmp->x = op->x, tmp->y = op->y;
insert_ob_in_map (tmp, op->map, NULL, 0);
/**************************************/
/* */
/* Subtract the experience points, */
/* if we died cause of food, give us */
/* food, and reset HP's... */
/* */
/**************************************/
/* remove any poisoning and confusion the character may be suffering. */
/* restore player */
at = archetype::find ("poisoning");
tmp = present_arch_in_ob (at, op);
if (tmp)
{
tmp->destroy ();
new_draw_info (NDI_UNIQUE, 0, op, "Your body feels cleansed");
}
at = archetype::find ("confusion");
tmp = present_arch_in_ob (at, op);
if (tmp)
{
tmp->destroy ();
new_draw_info (NDI_UNIQUE, 0, tmp, "Your mind feels clearer");
}
cure_disease (op, 0, 0); /* remove any disease */
/*add_exp(op, (op->stats.exp * -0.20)); */
apply_death_exp_penalty (op);
if (op->stats.food < 100)
op->stats.food = 900;
op->stats.hp = op->stats.maxhp;
op->stats.sp = MAX (op->stats.sp, op->stats.maxsp);
op->stats.grace = MAX (op->stats.grace, op->stats.maxgrace);
/*
* Check to see if the player has any unpaid items. If so, remove them
* and put them back in the map.
*/
op->drop_unpaid_items ();
/****************************************/
/* */
/* Move player to his current respawn- */
/* position (usually last savebed) */
/* */
/****************************************/
enter_player_savebed (op);
op->contr->braced = 0;
/* it is possible that the player has blown something up
* at his savebed location, and that can have long lasting
* spell effects. So first see if there is a spell effect
* on the space that might harm the player.
*/
will_kill_again = 0;
for (tmp = GET_MAP_OB (op->map, op->x, op->y); tmp; tmp = tmp->above)
if (tmp->type == SPELL_EFFECT)
will_kill_again |= tmp->attacktype;
if (will_kill_again)
{
object *force;
int at;
force = get_archetype (FORCE_NAME);
/* 50 ticks should be enough time for the spell to abate */
force->speed = 0.1f;
force->speed_left = -5.f;
SET_FLAG (force, FLAG_APPLIED);
for (at = 0; at < NROFATTACKS; at++)
if (will_kill_again & (1 << at))
force->resist[at] = 100;
insert_ob_in_ob (force, op);
op->update_stats ();
}
new_draw_info (NDI_UNIQUE, 0, op, "YOU HAVE DIED.");
}
void
loot_object (object *op)
{ /* Grab and destroy some treasure */
object *tmp, *tmp2, *next;
op->close_container (); /* close open sack first */
for (tmp = op->inv; tmp; tmp = next)
{
next = tmp->below;
if (tmp->invisible)
continue;
tmp->remove ();
tmp->x = op->x, tmp->y = op->y;
if (tmp->type == CONTAINER)
loot_object (tmp); /* empty container to ground */
if (!QUERY_FLAG (tmp, FLAG_UNIQUE) && (QUERY_FLAG (tmp, FLAG_STARTEQUIP) || QUERY_FLAG (tmp, FLAG_NO_DROP) || !(rndm (3))))
{
if (tmp->nrof > 1)
{
tmp2 = get_split_ob (tmp, 1 + RANDOM () % (tmp->nrof - 1));
tmp2->destroy ();
insert_ob_in_map (tmp, op->map, NULL, 0);
}
else
tmp->destroy ();
}
else
insert_ob_in_map (tmp, op->map, NULL, 0);
}
}
/*
* fix_weight(): Check recursively the weight of all players, and fix
* what needs to be fixed. Refresh windows and fix speed if anything
* was changed.
*/
void
fix_weight (void)
{
for_all_players (pl)
{
sint32 old = pl->ob->carrying;
pl->ob->update_weight ();
if (old != pl->ob->carrying)
{
pl->ob->update_stats ();
LOG (llevDebug, "Fixed inventory in %s (%d -> %d)\n", &pl->ob->name, (int)old, (int)pl->ob->carrying);
}
}
}
void
fix_luck (void)
{
for_all_players (pl)
if (!pl->ob->contr->ns->state)
pl->ob->change_luck (0);
}
/* cast_dust() - handles op throwing objects of type 'DUST'.
* This is much simpler in the new spell code - we basically
* just treat this as any other spell casting object.
*/
void
cast_dust (object *op, object *throw_ob, int dir)
{
object *skop, *spob;
skop = find_skill_by_name (op, throw_ob->skill);
/* casting POTION 'dusts' is really a use_magic_item skill */
if (op->type == PLAYER && throw_ob->type == POTION && !skop)
{
LOG (llevError, "Player %s lacks critical skill use_magic_item!\n", &op->name);
return;
}
spob = throw_ob->inv;
// elmex Tue Aug 15 17:19:46 CEST 2006: Added this check to
// not pass NULL to cast_spell (which did indeed check itself, but
// errors should be reported as early as possible IMHO)
if (!spob)
{
LOG (llevError, "cast_dust: thrown object %s (by %s) had no spell in it!", &throw_ob->name, &op->name);
return;
}
if (op->type == PLAYER)
new_draw_info_format (NDI_UNIQUE, 0, op, "You cast %s.", &spob->name);
cast_spell (op, throw_ob, dir, spob, NULL);
throw_ob->destroy ();
}
void
make_visible (object *op)
{
op->hide = 0;
op->invisible = 0;
if (op->type == PLAYER)
{
op->contr->tmp_invis = 0;
op->contr->invis_race = 0;
}
update_object (op, UP_OBJ_CHANGE);
}
int
is_true_undead (object *op)
{
if (QUERY_FLAG (op->arch, FLAG_UNDEAD))
return 1;
return 0;
}
/* look at the surrounding terrain to determine
* the hideability of this object. Positive levels
* indicate greater hideability.
*/
int
hideability (object *ob)
{
int i, level = 0, mflag;
sint16 x, y;
if (!ob || !ob->map)
return 0;
/* so, on normal lighted maps, its hard to hide */
level = ob->map->darkness - 2;
/* this also picks up whether the object is glowing.
* If you carry a light on a non-dark map, its not
* as bad as carrying a light on a pitch dark map */
if (has_carried_lights (ob))
level = -(10 + (2 * ob->map->darkness));
/* scan through all nearby squares for terrain to hide in */
for (i = 0, x = ob->x, y = ob->y;
i <= SIZEOFFREE1;
i++, x = ob->x + freearr_x[i], y = ob->y + freearr_y[i])
{
mflag = get_map_flags (ob->map, NULL, x, y, NULL, NULL);
if (mflag & P_OUT_OF_MAP)
continue;
if (mflag & P_BLOCKSVIEW) /* something to hide near! */
level += 2;
else /* open terrain! */
level -= 1;
}
#if 0
LOG (llevDebug, "hideability of %s is %d\n", ob->name, level);
#endif
return level;
}
/* For Hidden creatures - a chance of becoming 'unhidden'
* every time they move - as we subtract off 'invisibility'
* AND, for players, if they move into a ridiculously unhideable
* spot (surrounded by clear terrain in broad daylight). -b.t.
*/
void
do_hidden_move (object *op)
{
int hide = 0, num = random_roll (0, 19, op, PREFER_LOW);
object *skop;
if (!op || !op->map)
return;
skop = find_obj_by_type_subtype (op, SKILL, SK_HIDING);
/* its *extremely* hard to run and sneak/hide at the same time! */
if (op->type == PLAYER && op->contr->run_on)
if (!skop || num >= skop->level)
{
new_draw_info (NDI_UNIQUE, 0, op, "You ran too much! You are no longer hidden!");
make_visible (op);
return;
}
else
num += 20;
num += op->map->difficulty;
hide = hideability (op); /* modify by terrain hidden level */
num -= hide;
if ((op->type == PLAYER && hide < -10) || ((op->invisible -= num) <= 0))
{
make_visible (op);
if (op->type == PLAYER)
new_draw_info (NDI_UNIQUE, 0, op, "You moved out of hiding! You are visible!");
}
else if (op->type == PLAYER && skop)
change_exp (op, calc_skill_exp (op, NULL, skop), skop->skill, 0);
}
/* determine if who is standing near a hostile creature. */
int
stand_near_hostile (object *who)
{
object *tmp = NULL;
int i, friendly = 0, player = 0, mflags;
maptile *m;
sint16 x, y;
if (!who)
return 0;
if (who->type == PLAYER)
player = 1;
else
friendly = QUERY_FLAG (who, FLAG_FRIENDLY);
/* search adjacent squares */
for (i = 1; i < 9; i++)
{
x = who->x + freearr_x[i];
y = who->y + freearr_y[i];
m = who->map;
mflags = get_map_flags (m, &m, x, y, &x, &y);
/* space must be blocked if there is a monster. If not
* blocked, don't need to check this space.
*/
if (mflags & P_OUT_OF_MAP)
continue;
if (OB_TYPE_MOVE_BLOCK (who, GET_MAP_MOVE_BLOCK (m, x, y)))
continue;
for (tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
{
if ((player ||friendly) &&QUERY_FLAG (tmp, FLAG_MONSTER) && !QUERY_FLAG (tmp, FLAG_UNAGGRESSIVE))
return 1;
else if (tmp->type == PLAYER)
{
/*don't let a hidden DM prevent you from hiding */
if (!QUERY_FLAG (tmp, FLAG_WIZ) || tmp->contr->hidden == 0)
return 1;
}
}
}
return 0;
}
/* check the player los field for viewability of the
* object op. This function works fine for monsters,
* but we dont worry if the object isnt the top one in
* a pile (say a coin under a table would return "viewable"
* by this routine). Another question, should we be
* concerned with the direction the player is looking
* in? Realistically, most of us can't see stuff behind
* our backs...on the other hand, does the "facing" direction
* imply the way your head, or body is facing? It's possible
* for them to differ. Sigh, this fctn could get a bit more complex.
* -b.t.
* This function is now map tiling safe.
*/
int
player_can_view (object *pl, object *op)
{
rv_vector rv;
int dx, dy;
if (pl->type != PLAYER)
{
LOG (llevError, "player_can_view() called for non-player object\n");
return -1;
}
if (!pl || !op)
return 0;
op = op->head_ ();
get_rangevector (pl, op, &rv, 0x1);
/* starting with the 'head' part, lets loop
* through the object and find if it has any
* part that is in the los array but isn't on
* a blocked los square.
* we use the archetype to figure out offsets.
*/
while (op)
{
dx = rv.distance_x + op->arch->x;
dy = rv.distance_y + op->arch->y;
/* only the viewable area the player sees is updated by LOS
* code, so we need to restrict ourselves to that range of values
* for any meaningful values.
*/
if (abs (dx) <= (pl->contr->ns->mapx / 2) &&
abs (dy) <= (pl->contr->ns->mapy / 2) &&
!pl->contr->blocked_los[dx + (pl->contr->ns->mapx / 2)][dy + (pl->contr->ns->mapy / 2)])
return 1;
op = op->more;
}
return 0;
}
/* routine for both players and monsters. We call this when
* there is a possibility for our action distrubing our hiding
* place or invisiblity spell. Artefact invisiblity is not
* effected by this. If we arent invisible to begin with, we
* return 0.
*/
int
action_makes_visible (object *op)
{
if (op->invisible && QUERY_FLAG (op, FLAG_ALIVE))
{
if (QUERY_FLAG (op, FLAG_MAKE_INVIS))
return 0;
if (op->contr && op->contr->tmp_invis == 0)
return 0;
/* If monsters, they should become visible */
if (op->hide || !op->contr || (op->contr && op->contr->tmp_invis))
{
new_draw_info_format (NDI_UNIQUE, 0, op, "You become %s!", op->hide ? "unhidden" : "visible");
return 1;
}
}
return 0;
}
/* op_on_battleground - checks if the given object op (usually
* a player) is standing on a valid battleground-tile,
* function returns TRUE/FALSE. If true x, y returns the battleground
* -exit-coord. (and if x, y not NULL)
* 19 March 2005 - josh@woosworld.net modifed to check if the battleground also has slaying, maxhp, and maxsp set
* and if those are all set and the player has a marker that matches the slaying send them to a different x, y
* Default is to do the same as before, so only people wanting to have different points need worry about this
*/
int
op_on_battleground (object *op, int *x, int *y)
{
/* A battleground-tile needs the following attributes to be valid:
* is_floor 1 (has to be the FIRST floor beneath the player's feet),
* name="battleground", no_pick 1, type=58 (type BATTLEGROUND)
* and the exit-coordinates sp/hp must both be > 0.
* => The intention here is to prevent abuse of the battleground-
* feature (like pickable or hidden battleground tiles). */
for (object *tmp = op->below; tmp; tmp = tmp->below)
{
if (QUERY_FLAG (tmp, FLAG_IS_FLOOR))
{
if (QUERY_FLAG (tmp, FLAG_NO_PICK)
&& tmp->type == BATTLEGROUND
&& tmp->name == shstr_battleground
&& EXIT_X (tmp) && EXIT_Y (tmp))
{
/* before we assign the exit, check if this is a teambattle */
if (EXIT_ALT_X (tmp) && EXIT_ALT_Y (tmp) && EXIT_PATH (tmp))
{
for (object *invtmp = op->inv; invtmp; invtmp = invtmp->below)
{
if (invtmp->type == FORCE && invtmp->slaying && tmp->slaying == invtmp->slaying)
{
if (x && y)
*x = EXIT_ALT_X (tmp), *y = EXIT_ALT_Y (tmp);
return 1;
}
}
}
if (x && y)
*x = EXIT_X (tmp), *y = EXIT_Y (tmp);
return 1;
}
}
}
/* If we got here, did not find a battleground */
return 0;
}
/*
* When a dragon-player gains a new stage of evolution,
* he gets some treasure
*
* attributes:
* object *who the dragon player
* int atnr the attack-number of the ability focus
* int level ability level
*/
void
dragon_ability_gain (object *who, int atnr, int level)
{
treasurelist *trlist = NULL; /* treasurelist */
treasure *tr; /* treasure */
object *tmp, *skop; /* tmp. object */
object *item; /* treasure object */
char buf[MAX_BUF]; /* tmp. string buffer */
int i = 0, j = 0;
/* get the appropriate treasurelist */
if (atnr == ATNR_FIRE)
trlist = treasurelist::find (shstr_dragon_ability_fire);
else if (atnr == ATNR_COLD)
trlist = treasurelist::find (shstr_dragon_ability_cold);
else if (atnr == ATNR_ELECTRICITY)
trlist = treasurelist::find (shstr_dragon_ability_elec);
else if (atnr == ATNR_POISON)
trlist = treasurelist::find (shstr_dragon_ability_poison);
if (trlist == NULL || who->type != PLAYER)
return;
for (i = 0, tr = trlist->items; tr != NULL && i < level - 1; tr = tr->next, i++);
if (!tr || !tr->item)
{
/* LOG(llevDebug, "-> no more treasure for %s\n", change_resist_msg[atnr]); */
return;
}
/* everything seems okay - now bring on the gift: */
item = tr->item;
if (item->type == SPELL)
{
if (check_spell_known (who, item->name))
return;
new_draw_info_format (NDI_UNIQUE | NDI_BLUE, 0, who, "You gained the ability of %s", &item->name);
do_learn_spell (who, item, 0);
return;
}
/* grant direct spell */
if (item->type == SPELLBOOK)
{
if (!item->inv)
{
LOG (llevDebug, "dragon_ability_gain: Broken spellbook %s\n", &item->name);
return;
}
if (check_spell_known (who, item->inv->name))
return;
if (item->invisible)
{
new_draw_info_format (NDI_UNIQUE | NDI_BLUE, 0, who, "You gained the ability of %s", &item->inv->name);
do_learn_spell (who, item->inv, 0);
return;
}
}
else if (item->type == SKILL_TOOL && item->invisible)
{
if (item->subtype == SK_CLAWING && (skop = find_skill_by_name (who, item->skill)) != NULL)
{
/* should this perhaps be (skop->attackyp & item->attacktype)!=item->attacktype ...
* in this way, if the player is missing any of the attacktypes, he gets
* them. As it is now, if the player has any that match the granted skill,
* but not all of them, he gets nothing.
*/
if (!(skop->attacktype & item->attacktype))
{
/* Give new attacktype */
skop->attacktype |= item->attacktype;
/* always add physical if there's none */
skop->attacktype |= AT_PHYSICAL;
if (item->msg != NULL)
new_draw_info (NDI_UNIQUE | NDI_BLUE, 0, who, item->msg);
/* Give player new face */
if (item->animation_id)
{
who->face = skop->face;
who->animation_id = item->animation_id;
who->anim_speed = item->anim_speed;
who->last_anim = 0;
who->state = 0;
animate_object (who, who->direction);
}
}
}
}
else if (item->type == FORCE)
{
/* forces in the treasurelist can alter the player's stats */
object *skin;
/* first get the dragon skin force */
for (skin = who->inv; skin && !(skin->arch->archname == shstr_dragon_skin_force); skin = skin->below)
;
if (!skin)
return;
/* adding new spellpath attunements */
if (item->path_attuned > 0 && !(skin->path_attuned & item->path_attuned))
{
skin->path_attuned |= item->path_attuned; /* add attunement to skin */
/* print message */
sprintf (buf, "You feel attuned to ");
for (i = 0, j = 0; i < NRSPELLPATHS; i++)
{
if (item->path_attuned & (1 << i))
{
if (j)
strcat (buf, " and ");
else
j = 1;
strcat (buf, spellpathnames[i]);
}
}
strcat (buf, ".");
new_draw_info (NDI_UNIQUE | NDI_BLUE, 0, who, buf);
}
/* evtl. adding flags: */
if (QUERY_FLAG (item, FLAG_XRAYS))
SET_FLAG (skin, FLAG_XRAYS);
if (QUERY_FLAG (item, FLAG_STEALTH))
SET_FLAG (skin, FLAG_STEALTH);
if (QUERY_FLAG (item, FLAG_SEE_IN_DARK))
SET_FLAG (skin, FLAG_SEE_IN_DARK);
/* print message if there is one */
if (item->msg != NULL)
new_draw_info (NDI_UNIQUE | NDI_BLUE, 0, who, item->msg);
}
else
{
/* generate misc. treasure */
tmp = arch_to_object (tr->item);
new_draw_info_format (NDI_UNIQUE | NDI_BLUE, 0, who, "You gained %s", query_short_name (tmp));
tmp = insert_ob_in_ob (tmp, who);
if (who->type == PLAYER)
esrv_send_item (who, tmp);
}
}
/**
* Unready an object for a player. This function does nothing if the object was
* not readied.
*/
void
player_unready_range_ob (player *pl, object *ob)
{
if (pl->ob->current_weapon == ob)
pl->ob->current_weapon = 0;
if (pl->combat_ob == ob)
pl->combat_ob = 0;
if (pl->ranged_ob == ob)
pl->ranged_ob = 0;
}
sint8
player::visibility_at (maptile *map, int x, int y) const
{
if (!ns)
return 0;
int dx, dy;
if (!adjacent_map (map, ns->current_map, &dx, &dy))
return 0;
x += dx - ns->current_x + ns->mapx / 2;
y += dy - ns->current_y + ns->mapy / 2;
if (!IN_RANGE_EXC (x, 0, ns->mapx) || !IN_RANGE_EXC (y, 0, ns->mapy))
return 0;
return 100 - blocked_los [x][y];
}
void
player::infobox (const char *title, const char *msg, int color)
{
send_msg (color | NDI_DEF | NDI_REPLY | NDI_CLEAR, title, msg);
}
void
player::statusmsg (const char *msg, int color)
{
send_msg (color | NDI_REPLY, INFO_CHANNEL, msg);
}
void
player::failmsg (const char *msg, int color)
{
play_sound (sound_find ("generic_failure"));
statusmsg (msg, color);
}