/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
* Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
* Copyright (©) 2002 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
*/
//+GPL
#include
#include
#include
#include
#define MIN_MON_RADIUS 3 /* minimum monster detection radius */
/* checks npc->enemy and returns that enemy if still valid,
* NULL otherwise.
* this is map tile aware.
* If this returns an enemy, the range vector rv should also be
* set to sane values.
*/
object *
check_enemy (object *npc, rv_vector * rv)
{
/* if this is pet, let him attack the same enemy as his owner
* TODO: when there is no ower enemy, try to find a target,
* which CAN attack the owner. */
if ((npc->attack_movement & HI4) == PETMOVE)
{
if (npc->owner == NULL)
npc->enemy = NULL;
else if (npc->enemy == NULL)
npc->enemy = npc->owner->enemy;
}
/* periodically, a monster may change its target. Also, if the object
* has been destroyed, etc, clear the enemy.
* TODO: this should be changed, because it invokes to attack forced or
* attacked monsters to leave the attacker alone, before it is destroyed
*/
/* I had removed the random target leave, this invokes problems with friendly
* objects, getting attacked and defending herself - they don't try to attack
* again then but perhaps get attack on and on
* If we include a aggravated flag in , we can handle evil vs evil and good vs good
* too. */
if (npc->enemy)
{
/* I broke these if's apart to better be able to see what
* the grouping checks are. Code is the same.
*/
if (npc->enemy->flag [FLAG_REMOVED] ||
npc->enemy->flag [FLAG_FREED] ||
!on_same_map (npc, npc->enemy) || npc == npc->enemy || npc->flag [FLAG_NEUTRAL] || npc->enemy->flag [FLAG_NEUTRAL])
npc->enemy = 0;
else if (npc->flag [FLAG_FRIENDLY] && ((npc->enemy->flag [FLAG_FRIENDLY]
&& !(should_arena_attack (npc, npc->owner, npc->enemy)))
|| ((npc->enemy->type == PLAYER) && !(should_arena_attack (npc, npc->owner, npc->enemy)))
|| npc->enemy == npc->owner))
npc->enemy = 0;
else if (!npc->flag [FLAG_FRIENDLY] && (!npc->enemy->flag [FLAG_FRIENDLY] && npc->enemy->type != PLAYER))
npc->enemy = 0;
/* I've noticed that pets could sometimes get an arrow as the
* target enemy - this code below makes sure the enemy is something
* that should be attacked. My guess is that the arrow hits
* the creature/owner, and so the creature then takes that
* as the enemy to attack.
*/
else if (!npc->enemy->flag [FLAG_MONSTER]
&& !npc->enemy->flag [FLAG_GENERATOR]
&& npc->enemy->type != PLAYER
&& npc->enemy->type != GOLEM)
npc->enemy = 0;
}
return can_detect_enemy (npc, npc->enemy, rv) ? npc->enemy : 0;
}
/* Returns the nearest living creature (monster or generator).
* Modified to deal with tiled maps properly.
* Also fixed logic so that monsters in the lower directions were more
* likely to be skipped - instead of just skipping the 'start' number
* of direction, revisit them after looking at all the other spaces.
*
* Note that being this may skip some number of spaces, it will
* not necessarily find the nearest living creature - it basically
* chooses one from within a 3 space radius, and since it skips
* the first few directions, it could very well choose something
* 3 spaces away even though something directly north is closer.
*
* this function is map tile aware.
*/
object *
find_nearest_living_creature (object *npc)
{
int i, mflags;
sint16 nx, ny;
maptile *m;
int search_arr[SIZEOFFREE];
get_search_arr (search_arr);
for (i = 0; i < SIZEOFFREE; i++)
{
/* modified to implement smart searching using search_arr
* guidance array to determine direction of search order
*/
nx = npc->x + DIRX (search_arr[i]);
ny = npc->y + DIRY (search_arr[i]);
m = npc->map;
mflags = get_map_flags (m, &m, nx, ny, &nx, &ny);
if (mflags & P_OUT_OF_MAP)
continue;
if (mflags & P_IS_ALIVE)
{
for (object *tmp = m->at (nx, ny).top; tmp; tmp = tmp->below)
if (tmp->flag [FLAG_MONSTER] || tmp->flag [FLAG_GENERATOR] || tmp->type == PLAYER)
if (can_see_monsterP (m, nx, ny, i))
return tmp;
}
}
return 0;
}
/* Tries to find an enemy for npc. We pass the range vector since
* our caller will find the information useful.
* Currently, only move_monster calls this function.
* Fix function so that we always make calls to get_rangevector
* if we have a valid target - function as not doing so in
* many cases.
*/
static object *
find_enemy (object *npc, rv_vector * rv)
{
object *attacker, *tmp = NULL;
attacker = npc->attacked_by; /* save this for later use. This can be a attacker. */
npc->attacked_by = 0; /* always clear the attacker entry */
/* if we berserk, we don't care about others - we attack all we can find */
if (npc->flag [FLAG_BERSERK])
{
tmp = find_nearest_living_creature (npc);
if (tmp)
get_rangevector (npc, tmp, rv, 0);
return tmp;
}
/* Here is the main enemy selection.
* We want this: if there is an enemy, attack him until its not possible or
* one of both is dead.
* If we have no enemy and we are...
* a monster: try to find a player, a pet or a friendly monster
* a friendly: only target a monster which is targeting you first or targeting a player
* a neutral: fight a attacker (but there should be none), then do nothing
* a pet: attack player enemy or a monster
*/
/* pet move */
if ((npc->attack_movement & HI4) == PETMOVE)
{
tmp = get_pet_enemy (npc, rv);
if (tmp)
get_rangevector (npc, tmp, rv, 0);
return tmp;
}
/* we check our old enemy. */
if (!(tmp = check_enemy (npc, rv)))
{
if (attacker) /* if we have an attacker, check him */
{
/* TODO: that's not finished */
/* we don't want a fight evil vs evil or good against non evil */
if (npc->flag [FLAG_NEUTRAL] || attacker->flag [FLAG_NEUTRAL] || /* neutral */
(npc->flag [FLAG_FRIENDLY] && attacker->flag [FLAG_FRIENDLY]) ||
(!npc->flag [FLAG_FRIENDLY] && (!attacker->flag [FLAG_FRIENDLY] && attacker->type != PLAYER)))
npc->clr_flag (FLAG_SLEEP); /* skip it, but lets wakeup */
else if (on_same_map (npc, attacker)) /* that's the only thing we must know... */
{
npc->clr_flag (FLAG_SLEEP); /* well, NOW we really should wake up! */
npc->enemy = attacker;
return attacker; /* yes, we face our attacker! */
}
}
/* we have no legal enemy or attacker, so we try to target a new one */
if (!npc->flag [FLAG_UNAGGRESSIVE] && !npc->flag [FLAG_FRIENDLY] && !npc->flag [FLAG_NEUTRAL])
{
npc->enemy = get_nearest_player (npc);
if (npc->enemy)
tmp = check_enemy (npc, rv);
}
}
return tmp;
}
/* Sees if this monster should wake up.
* Currently, this is only called from move_monster, and
* if enemy is set, then so should be rv.
* returns 1 if the monster should wake up, 0 otherwise.
*/
static int
check_wakeup (object *op, object *enemy, rv_vector *rv)
{
/* Trim work - if no enemy, no need to do anything below */
if (!enemy)
return 0;
if (!op->flag [FLAG_SLEEP])
return 1;
int radius = max (op->stats.Wis, MIN_MON_RADIUS);
if (op->flag [FLAG_BLIND])
/* blinded monsters can only find nearby objects to attack */
radius = MIN_MON_RADIUS;
else if (op->map
&& !enemy->invisible
&& !stand_in_light (enemy)
&& (!op->flag[FLAG_SEE_IN_DARK] || !op->flag [FLAG_SEE_INVISIBLE]))
/* This covers the situation where the monster is in the dark
* and has an enemy. If the enemy has no carried light (or isnt
* glowing!) then the monster has trouble finding the enemy.
* Remember we already checked to see if the monster can see in
* the dark. */
min_it (radius, MIN_MON_RADIUS + MAX_DARKNESS - op->map->darklevel ());
if (enemy->flag [FLAG_STEALTH])
radius = radius / 2 + 1;
/* enemy should already be on this map, so don't really need to check
* for that.
*/
if (rv->distance <= radius)
{
op->clr_flag (FLAG_SLEEP);
return 1;
}
return 0;
}
static int
move_randomly (object *op)
{
/* Give up to 15 chances for a monster to move randomly */
for (int i = 0; i < 15; i++)
if (op->move (rndm (8) + 1))
return 1;
return 0;
}
/*
* monster_can_pick(): If the monster is interested in picking up
* the item, then return 0. Otherwise 0.
* Instead of pick_up, flags for "greed", etc, should be used.
* I've already utilized flags for bows, wands, rings, etc, etc. -Frank.
*/
static int
monster_can_pick (object *monster, object *item)
{
int flag = 0;
int i;
if (!can_pick (monster, item))
return 0;
if (item->flag [FLAG_UNPAID])
return 0;
if (monster->pick_up & 64) /* All */
flag = 1;
else
switch (item->type)
{
case MONEY:
case GEM:
flag = monster->pick_up & 2;
break;
case FOOD:
flag = monster->pick_up & 4;
break;
case WEAPON:
flag = (monster->pick_up & 8) || monster->flag [FLAG_USE_WEAPON];
break;
case ARMOUR:
case SHIELD:
case HELMET:
case BOOTS:
case GLOVES:
case GIRDLE:
flag = (monster->pick_up & 16) || monster->flag [FLAG_USE_ARMOUR];
break;
case SKILL:
flag = monster->flag [FLAG_CAN_USE_SKILL];
break;
case RING:
flag = monster->flag [FLAG_USE_RING];
break;
case RANGED:
case WAND:
case HORN:
case ROD:
flag = monster->flag [FLAG_USE_RANGE];
break;
case SPELLBOOK:
flag = monster->arch && monster->arch->flag [FLAG_CAST_SPELL];
break;
case SCROLL:
flag = monster->flag [FLAG_USE_SCROLL];
break;
case BOW:
case ARROW:
flag = monster->flag [FLAG_USE_BOW];
break;
}
/* Simplistic check - if the monster has a location to equip it, he will
* pick it up. Note that this doesn't handle cases where an item may
* use several locations.
*/
for (i = 0; i < NUM_BODY_LOCATIONS; i++)
{
if (monster->slot[i].info && item->slot[i].info)
{
flag = 1;
break;
}
}
if (((!(monster->pick_up & 32)) && flag) || ((monster->pick_up & 32) && !flag))
return 1;
return 0;
}
/*
* monster_check_pickup(): checks for items that monster can pick up.
*
* Vick's (vick@bern.docs.uu.se) fix 921030 for the sweeper blob.
* Each time the blob passes over some treasure, it will
* grab it a.s.a.p.
*
* Eneq(@csd.uu.se): This can now be defined in the archetypes, added code
* to handle this.
*
* This function was seen be continueing looping at one point (tmp->below
* became a recursive loop. It may be better to call monster_check_apply
* after we pick everything up, since that function may call others which
* affect stacking on this space.
*/
static void
monster_check_pickup (object *monster)
{
for (object *next, *tmp = monster->below; tmp; tmp = next)
{
next = tmp->below;
if (monster_can_pick (monster, tmp))
{
tmp->remove ();
tmp = insert_ob_in_ob (tmp, monster);
monster_check_apply (monster, tmp);
}
/* We could try to re-establish the cycling, of the space, but probably
* not a big deal to just bail out.
*/
if (next && next->destroyed ())
return;
}
}
/*
* monster_apply_below():
* Vick's (vick@bern.docs.uu.se) @921107 -> If a monster who's
* eager to apply things, encounters something apply-able,
* then make him apply it
*/
static void
monster_apply_below (object *monster)
{
object *tmp, *next;
for (tmp = monster->below; tmp != NULL; tmp = next)
{
next = tmp->below;
switch (tmp->type)
{
case T_HANDLE:
case TRIGGER:
if (monster->will_apply & 1)
monster->apply (tmp);
break;
case TREASURE:
if (monster->will_apply & 2)
monster->apply (tmp);
break;
}
if (tmp->flag [FLAG_IS_FLOOR])
break;
}
}
/* Returns 1 is monster should cast spell sp at an enemy
* Returns 0 if the monster should not cast this spell.
*
* Note that this function does not check to see if the monster can
* in fact cast the spell (sp dependencies and what not.) That is because
* this function is also sued to see if the monster should use spell items
* (rod/horn/wand/scroll).
* Note that there are certainly other offensive spells that could be
* included, but I decided to leave out the spells that may kill more
* monsters than players (eg, disease).
*
* This could be a lot smarter - if there are few monsters around,
* then disease might not be as bad. Likewise, if the monster is damaged,
* the right type of healing spell could be useful.
*/
static int
monster_should_cast_spell (object *monster, object *spell_ob)
{
if (spell_ob->subtype == SP_BOLT || spell_ob->subtype == SP_BULLET ||
spell_ob->subtype == SP_EXPLOSION || spell_ob->subtype == SP_CONE ||
spell_ob->subtype == SP_BOMB || spell_ob->subtype == SP_SMITE ||
spell_ob->subtype == SP_MAGIC_MISSILE || spell_ob->subtype == SP_SUMMON_GOLEM ||
spell_ob->subtype == SP_MAGIC_WALL || spell_ob->subtype == SP_SUMMON_MONSTER ||
spell_ob->subtype == SP_MOVING_BALL || spell_ob->subtype == SP_SWARM || spell_ob->subtype == SP_INVISIBLE)
return 1;
return 0;
}
/* Checks if putting on 'item' will make 'who' do more
* damage. This is a very simplistic check - also checking things
* like speed and ac are also relevant.
*
* return true if item is a better object.
*/
static int
check_good_weapon (object *who, object *item)
{
object *other_weap;
int val = 0, i;
for (other_weap = who->inv; other_weap; other_weap = other_weap->below)
if (other_weap->type == item->type && other_weap->flag [FLAG_APPLIED])
break;
if (!other_weap) /* No other weapons */
return 1;
/* Rather than go through and apply the new one, and see if it is
* better, just do some simple checks
* Put some multipliers for things that hvae several effects,
* eg, magic affects both damage and wc, so it has more weight
*/
val = item->stats.dam - other_weap->stats.dam;
val += (item->magic - other_weap->magic) * 3;
/* Monsters don't really get benefits from things like regen rates
* from items. But the bonus for their stats are very important.
*/
for (i = 0; i < NUM_STATS; i++)
val += item->stats.stat (i) - other_weap->stats.stat (i) * 2;
if (val > 0)
return 1;
else
return 0;
}
static int
check_good_armour (object *who, object *item)
{
object *other_armour;
int val = 0, i;
for (other_armour = who->inv; other_armour; other_armour = other_armour->below)
if (other_armour->type == item->type && other_armour->flag [FLAG_APPLIED])
break;
if (other_armour == NULL) /* No other armour, use the new */
return 1;
/* Like above function , see which is better */
val = item->stats.ac - other_armour->stats.ac;
val = (item->resist[ATNR_PHYSICAL] - other_armour->resist[ATNR_PHYSICAL]) / 5;
val += (item->magic - other_armour->magic) * 3;
/* for the other protections, do weigh them very much in the equation -
* it is the armor protection which is most important, because there is
* no good way to know what the player may attack the monster with.
* So if the new item has better protection than the old, give that higher
* value. If the reverse, then decrease the value of this item some.
*/
for (i = 1; i < NROFATTACKS; i++)
{
if (item->resist[i] > other_armour->resist[i])
val++;
else if (item->resist[i] < other_armour->resist[i])
val--;
}
/* Very few armours have stats, so not much need to worry about those. */
if (val > 0)
return 1;
else
return 0;
}
/*
* monster_check_apply() is meant to be called after an item is
* inserted in a monster.
* If an item becomes outdated (monster found a better item),
* a pointer to that object is returned, so it can be dropped.
* (so that other monsters can pick it up and use it)
* Note that as things are now, monsters never drop something -
* they can pick up all that they can use.
*/
/* Sept 96, fixed this so skills will be readied -b.t.*/
void
monster_check_apply (object *mon, object *item)
{
int flag = 0;
if (item->type == SPELLBOOK && mon->arch && (mon->arch->flag [FLAG_CAST_SPELL]))
{
mon->set_flag (FLAG_CAST_SPELL);
return;
}
/* If for some reason, this item is already applied, no more work to do */
if (item->flag [FLAG_APPLIED])
return;
/* Might be better not to do this - if the monster can fire a bow,
* it is possible in his wanderings, he will find one to use. In
* which case, it would be nice to have ammo for it.
*/
if (mon->flag [FLAG_USE_BOW] && item->type == ARROW)
{
/* Check for the right kind of bow */
object *bow;
for (bow = mon->inv; bow; bow = bow->below)
if (bow->type == BOW && bow->race == item->race)
{
mon->set_flag (FLAG_READY_BOW);
return; /* nothing more to do for arrows */
}
}
if (item->type == TREASURE && mon->will_apply & WILL_APPLY_TREASURE)
flag = 1;
/* Eating food gets hp back */
else if (item->type == FOOD && mon->will_apply & WILL_APPLY_FOOD)
flag = 1;
else if (item->type == SCROLL && mon->flag [FLAG_USE_SCROLL])
{
if (!item->inv)
LOG (llevDebug, "Monster %d having scroll %d with empty inventory!", mon->count, item->count);
else if (monster_should_cast_spell (mon, item->inv))
mon->set_flag (FLAG_READY_SCROLL);
/* Don't use it right now */
return;
}
else if (item->type == WEAPON)
flag = check_good_weapon (mon, item);
else if (item->is_armor ())
flag = check_good_armour (mon, item);
/* Should do something more, like make sure this is a better item */
else if (item->type == RING)
flag = 1;
else if (item->type == WAND || item->type == ROD || item->type == HORN)
{
/* We never really 'ready' the wand/rod/horn, because that would mean the
* weapon would get undone.
*/
if (!(can_apply_object (mon, item) & CAN_APPLY_NOT_MASK))
{
mon->set_flag (FLAG_READY_RANGE);
item->set_flag (FLAG_APPLIED);
}
return;
}
else if (item->type == BOW)
{
/* We never really 'ready' the bow, because that would mean the
* weapon would get undone.
*/
if (!(can_apply_object (mon, item) & CAN_APPLY_NOT_MASK))
mon->set_flag (FLAG_READY_BOW);
return;
}
else if (item->type == SKILL)
{
/*
* skills are specials: monsters must have the 'FLAG_READY_SKILL' flag set,
* else they can't use the skill...
* Skills also don't need to get applied, so return now.
*/
mon->set_flag (FLAG_READY_SKILL);
return;
}
/* if we don't match one of the above types, return now.
* can_apply_object will say that we can apply things like flesh,
* bolts, and whatever else, because it only checks against the
* body_info locations.
*/
if (!flag)
return;
/* Check to see if the monster can use this item. If not, no need
* to do further processing. Note that can_apply_object already checks
* for the CAN_USE flags.
*/
if (can_apply_object (mon, item) & CAN_APPLY_NOT_MASK)
return;
/* should only be applying this item, not unapplying it.
* also, ignore status of curse so they can take off old armour.
* monsters have some advantages after all.
*/
mon->apply (item, AP_APPLY | AP_IGNORE_CURSE);
}
static int
can_hit (object *ob1, object *ob2, rv_vector * rv)
{
object *more;
rv_vector rv1;
if (ob1->flag [FLAG_CONFUSED] && !(rndm (3)))
return 0;
if (abs (rv->distance_x) < 2 && abs (rv->distance_y) < 2)
return 1;
/* check all the parts of ob2 - just because we can't get to
* its head doesn't mean we don't want to pound its feet
*/
for (more = ob2->more; more; more = more->more)
{
get_rangevector (ob1, more, &rv1, 0);
if (abs (rv1.distance_x) < 2 && abs (rv1.distance_y) < 2)
return 1;
}
return 0;
}
static int
dist_att (int dir, object *ob, object *enemy, object *part, rv_vector *rv)
{
if (can_hit (part, enemy, rv))
return dir;
if (rv->distance < 10)
return absdir (dir + 4);
else if (rv->distance > 18)
return dir;
return 0;
}
static int
run_att (int dir, object *ob, object *enemy, object *part, rv_vector *rv)
{
if (can_hit (part, enemy, rv))
return dir;
else
return absdir (dir + 4);
}
static int
hitrun_att (int dir, object *ob, object *enemy)
{
if (ob->move_status++ < 25)
return dir;
else if (ob->move_status < 50)
return absdir (dir + 4);
else
ob->move_status = 0;
return absdir (dir + 4);
}
static int
wait_att (int dir, object *ob, object *enemy, object *part, rv_vector * rv)
{
int inrange = can_hit (part, enemy, rv);
if (ob->move_status || inrange)
ob->move_status++;
if (ob->move_status == 0)
return 0;
else if (ob->move_status < 10)
return dir;
else if (ob->move_status < 15)
return absdir (dir + 4);
ob->move_status = 0;
return 0;
}
static int
disthit_att (int dir, object *ob, object *enemy, object *part, rv_vector * rv)
{
/* The logic below here looked plain wrong before. Basically, what should
* happen is that if the creatures hp percentage falls below run_away,
* the creature should run away (dir+4)
* I think its wrong for a creature to have a zero maxhp value, but
* at least one map has this set, and whatever the map contains, the
* server should try to be resilant enough to avoid the problem
*/
if (ob->stats.hp * 100 < ob->stats.maxhp * ob->run_away)
return absdir (dir + 4);
return dist_att (dir, ob, enemy, part, rv);
}
static int
wait_att2 (int dir, object *ob, object *enemy, object *part, rv_vector * rv)
{
if (rv->distance < 9)
return absdir (dir + 4);
return 0;
}
static void
circ1_move (object *ob)
{
static int circle[12] = { 3, 3, 4, 5, 5, 6, 7, 7, 8, 1, 1, 2 };
if (++ob->move_status > 11)
ob->move_status = 0;
if (!(ob->move (circle[ob->move_status])))
ob->move (rndm (8) + 1);
}
static void
circ2_move (object *ob)
{
static int circle[20] = { 3, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 1, 1, 1, 2, 2 };
if (++ob->move_status > 19)
ob->move_status = 0;
if (!(ob->move (circle[ob->move_status])))
ob->move (rndm (8) + 1);
}
static void
pace_movev (object *ob)
{
if (ob->move_status++ > 6)
ob->move_status = 0;
if (ob->move_status < 4)
ob->move (5);
else
ob->move (1);
}
static void
pace_moveh (object *ob)
{
if (ob->move_status++ > 6)
ob->move_status = 0;
if (ob->move_status < 4)
ob->move (3);
else
ob->move (7);
}
static void
pace2_movev (object *ob)
{
if (ob->move_status++ > 16)
ob->move_status = 0;
if (ob->move_status < 6)
ob->move (5);
else if (ob->move_status < 8)
return;
else if (ob->move_status < 13)
ob->move (1);
else
return;
}
static void
pace2_moveh (object *ob)
{
if (ob->move_status++ > 16)
ob->move_status = 0;
if (ob->move_status < 6)
ob->move (3);
else if (ob->move_status < 8)
return;
else if (ob->move_status < 13)
ob->move (7);
else
return;
}
static void
rand_move (object *ob)
{
if (ob->move_status < 1 || ob->move_status > 8 || !(ob->move (ob->move_status || !(rndm (9)))))
for (int i = 0; i < 5; i++)
if (ob->move (ob->move_status = rndm (8) + 1))
return;
}
#define MAX_KNOWN_SPELLS 20
/* Returns a randomly selected spell. This logic is still
* less than ideal. This code also only seems to deal with
* wizard spells, as the check is against sp, and not grace.
* can mosnters know cleric spells?
*/
static object *
monster_choose_random_spell (object *monster)
{
object *altern[MAX_KNOWN_SPELLS];
int i = 0;
for (object *tmp = monster->inv; tmp; tmp = tmp->below)
if (tmp->type == SPELLBOOK || tmp->type == SPELL)
{
/* Check and see if it's actually a useful spell.
* If its a spellbook, the spell is actually the inventory item.
* if it is a spell, then it is just the object itself.
*/
if (monster_should_cast_spell (monster, (tmp->type == SPELLBOOK) ? tmp->inv : tmp))
{
altern [i++] = tmp;
if (i == MAX_KNOWN_SPELLS)
break;
}
}
return i ? altern [rndm (i)] : 0;
}
/* This checks to see if the monster should cast a spell/ability.
* it returns true if the monster casts a spell, 0 if he doesn't.
* head is the head of the monster.
* part is the part of the monster we are checking against.
* pl is the target.
* dir is the direction to case.
* rv is the vector which describes where the enemy is.
*/
static int
monster_cast_spell (object *head, object *part, object *pl, int dir, rv_vector * rv)
{
object *spell_item;
object *owner;
rv_vector rv1;
/* If you want monsters to cast spells over friends, this spell should
* be removed. It probably should be in most cases, since monsters still
* don't care about residual effects (ie, casting a cone which may have a
* clear path to the player, the side aspects of the code will still hit
* other monsters)
*/
if (!(dir = path_to_player (part, pl, 0)))
return 0;
if (head->flag [FLAG_FRIENDLY] && (owner = head->owner) != NULL)
{
get_rangevector (head, owner, &rv1, 0x1);
if (dirdiff (dir, rv1.direction) < 2)
return 0; /* Might hit owner with spell */
}
if (head->flag [FLAG_CONFUSED])
dir = absdir (dir + rndm (3) + rndm (3) - 2);
/* If the monster hasn't already chosen a spell, choose one
* I'm not sure if it really make sense to pre-select spells (events
* could be different by the time the monster goes again).
*/
if (head->spellitem == NULL)
{
if ((spell_item = monster_choose_random_spell (head)) == NULL)
{
head->clr_flag (FLAG_CAST_SPELL); /* Will be turned on when picking up book */
return 0;
}
if (spell_item->type == SPELLBOOK)
{
if (!spell_item->inv)
{
LOG (llevError, "spellbook %s does not contain a spell?\n", &spell_item->name);
return 0;
}
spell_item = spell_item->inv;
}
}
else
spell_item = head->spellitem;
if (!spell_item)
return 0;
/* Best guess this is a defensive/healing spell */
if (spell_item->range <= 1 || spell_item->stats.dam < 0)
dir = 0;
/* Monster doesn't have enough spell-points */
if (head->stats.sp < SP_level_spellpoint_cost (head, spell_item, SPELL_MANA))
return 0;
if (head->stats.grace < SP_level_spellpoint_cost (head, spell_item, SPELL_GRACE))
return 0;
head->stats.sp -= SP_level_spellpoint_cost (head, spell_item, SPELL_MANA);
head->stats.grace -= SP_level_spellpoint_cost (head, spell_item, SPELL_GRACE);
/* set this to null, so next time monster will choose something different */
head->spellitem = NULL;
return cast_spell (part, part, dir, spell_item, NULL);
}
static int
monster_use_scroll (object *head, object *part, object *pl, int dir, rv_vector * rv)
{
object *scroll;
object *owner;
rv_vector rv1;
/* If you want monsters to cast spells over friends, this spell should
* be removed. It probably should be in most cases, since monsters still
* don't care about residual effects (ie, casting a cone which may have a
* clear path to the player, the side aspects of the code will still hit
* other monsters)
*/
if (!(dir = path_to_player (part, pl, 0)))
return 0;
if (head->flag [FLAG_FRIENDLY] && (owner = head->owner) != NULL)
{
get_rangevector (head, owner, &rv1, 0x1);
if (dirdiff (dir, rv1.direction) < 2)
{
return 0; /* Might hit owner with spell */
}
}
if (head->flag [FLAG_CONFUSED])
dir = absdir (dir + rndm (3) + rndm (3) - 2);
for (scroll = head->inv; scroll; scroll = scroll->below)
if (scroll->type == SCROLL && monster_should_cast_spell (head, scroll->inv))
break;
/* Used up all his scrolls, so nothing do to */
if (!scroll)
{
head->clr_flag (FLAG_READY_SCROLL);
return 0;
}
/* Spell should be cast on caster (ie, heal, strength) */
if (scroll->inv->range == 0)
dir = 0;
apply_scroll (part, scroll, dir);
return 1;
}
/* monster_use_skill()-implemented 95-04-28 to allow monster skill use.
* Note that monsters do not need the skills SK_MELEE_WEAPON and
* SK_MISSILE_WEAPON to make those respective attacks, if we
* required that we would drastically increase the memory
* requirements of CF!!
*
* The skills we are treating here are all but those. -b.t.
*
* At the moment this is only useful for throwing, perhaps for
* stealing. TODO: This should be more integrated in the game. -MT, 25.11.01
*/
static int
monster_use_skill (object *head, object *part, object *pl, int dir)
{
object *skill, *owner;
if (!(dir = path_to_player (part, pl, 0)))
return 0;
if (head->flag [FLAG_FRIENDLY] && (owner = head->owner) != NULL)
{
int dir2 = find_dir_2 (owner->x - head->x, owner->y - head->y);
if (dirdiff (dir, dir2) < 1)
return 0; /* Might hit owner with skill -thrown rocks for example ? */
}
if (head->flag [FLAG_CONFUSED])
dir = absdir (dir + rndm (3) + rndm (3) - 2);
object *new_skill = 0;
// skill selection - monster will use the last unused skill
// and rotate, eventually cycling through all skills.
for (skill = head->inv; skill; skill = skill->below)
if (skill->type == SKILL && skill != head->chosen_skill)
new_skill = skill;
if (new_skill)
splay (head->chosen_skill = new_skill);
else if (!head->chosen_skill)
{
LOG (llevDebug, "Error: Monster %s (%d) has FLAG_READY_SKILL without skill.\n", &head->name, head->count);
head->clr_flag (FLAG_READY_SKILL);
return 0;
}
/* use skill */
return do_skill (head, part, head->chosen_skill, dir, NULL);
}
/* Monster will use a ranged spell attack. */
static int
monster_use_range (object *head, object *part, object *pl, int dir)
{
object *wand, *owner;
int at_least_one = 0;
if (!(dir = path_to_player (part, pl, 0)))
return 0;
if (head->flag [FLAG_FRIENDLY] && (owner = head->owner) != NULL)
{
int dir2 = find_dir_2 (owner->x - head->x, owner->y - head->y);
if (dirdiff (dir, dir2) < 2)
return 0; /* Might hit owner with spell */
}
if (head->flag [FLAG_CONFUSED])
dir = absdir (dir + rndm (3) + rndm (3) - 2);
for (wand = head->inv; wand; wand = wand->below)
{
if (wand->type == WAND)
{
/* Found a wand, let's see if it has charges left */
at_least_one = 1;
if (wand->stats.food <= 0)
continue;
cast_spell (head, wand, dir, wand->inv, NULL);
if (!(--wand->stats.food))
{
if (wand->arch)
{
wand->clr_flag (FLAG_ANIMATE);
wand->face = wand->arch->face;
wand->set_speed (0);
}
}
/* Success */
return 1;
}
else if (wand->type == ROD || wand->type == HORN)
{
/* Found rod/horn, let's use it if possible */
at_least_one = 1;
if (wand->stats.hp < max (wand->inv->stats.sp, wand->inv->stats.grace))
continue;
/* drain charge before casting spell - can be a case where the
* spell destroys the monster, and rod, so if done after, results
* in crash.
*/
drain_rod_charge (wand);
cast_spell (head, wand, dir, wand->inv, NULL);
/* Success */
return 1;
}
}
if (at_least_one)
return 0;
LOG (llevError, "Error: Monster %s (%d) HAS_READY_RANGE() without wand/horn/rod.\n", &head->name, head->count);
head->clr_flag (FLAG_READY_RANGE);
return 0;
}
static int
monster_use_bow (object *head, object *part, object *pl, int dir)
{
object *owner;
if (!(dir = path_to_player (part, pl, 0)))
return 0;
if (head->flag [FLAG_CONFUSED])
dir = absdir (dir + rndm (3) + rndm (3) - 2);
if (head->flag [FLAG_FRIENDLY] && (owner = head->owner) != NULL)
{
int dir2 = find_dir_2 (owner->x - head->x, owner->y - head->y);
if (dirdiff (dir, dir2) < 1)
return 0; /* Might hit owner with arrow */
}
/* in server/player.c */
return fire_bow (head, part, NULL, dir, 0, part->x, part->y);
}
void
npc_call_help (object *op)
{
unordered_mapwalk (mapwalk_buf, op, -7, -7, 7, 7)
{
mapspace &ms = m->at (nx, ny);
/* If nothing alive on this space, no need to search the space. */
if (!(ms.flags () & P_IS_ALIVE))
continue;
for (object *npc = ms.bot; npc; npc = npc->above)
if (npc->flag [FLAG_ALIVE] && npc->flag [FLAG_UNAGGRESSIVE])
npc->enemy = op->enemy;
}
}
void
check_earthwalls (object *op, maptile *m, int x, int y)
{
for (object *tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
if (tmp->type == EARTHWALL)
{
hit_player (tmp, op->stats.dam, op, AT_PHYSICAL, 1);
return;
}
}
void
check_doors (object *op, maptile *m, int x, int y)
{
for (object *tmp = GET_MAP_OB (m, x, y); tmp; tmp = tmp->above)
if (tmp->type == DOOR)
{
hit_player (tmp, 1000, op, AT_PHYSICAL, 1);
return;
}
}
/* find_mon_throw_ob() - modeled on find_throw_ob
* This is probably overly simplistic as it is now - We want
* monsters to throw things like chairs and other pieces of
* furniture, even if they are not good throwable objects.
* Probably better to have the monster throw a throwable object
* first, then throw any non equipped weapon.
*/
object *
find_mon_throw_ob (object *op)
{
object *tmp = NULL;
if (op->head)
tmp = op->head;
else
tmp = op;
/* New throw code: look through the inventory. Grap the first legal is_thrown
* marked item and throw it to the enemy.
*/
for (tmp = op->inv; tmp; tmp = tmp->below)
{
/* Can't throw invisible objects or items that are applied */
if (tmp->invisible || tmp->flag [FLAG_APPLIED])
continue;
if (tmp->flag [FLAG_IS_THROWN])
break;
}
#ifdef DEBUG_THROW
LOG (llevDebug, "%s chooses to throw: %s (%d)\n", op->name, !(tmp) ? "(nothing)" : query_name (tmp), tmp ? tmp->count : -1);
#endif
return tmp;
}
/*
* Move-monster returns 1 if the object has been freed, otherwise 0.
*/
int
move_monster (object *op)
{
int dir, diff, pre_att_dir; /* elmex: pre_att_dir remembers the direction before attack movement */
object *owner, *enemy, *part, *oph = op;
rv_vector rv;
/* Monsters not on maps don't do anything. These monsters are things
* Like royal guards in city dwellers inventories.
*/
if (!op->map)
return 0;
/* for target facing, we copy this value here for fast access */
if (oph->head) /* force update the head - one arch one pic */
oph = oph->head;
if (op->flag [FLAG_NO_ATTACK]) /* we never ever attack */
enemy = op->enemy = 0;
else if ((enemy = find_enemy (op, &rv)))
/* we have an enemy, just tell him we want him dead */
enemy->attacked_by = op; /* our ptr */
/* generate hp, if applicable */
if (op->stats.Con > 0 && op->stats.hp < op->stats.maxhp)
{
/* last heal is in funny units. Dividing by speed puts
* the regeneration rate on a basis of time instead of
* #moves the monster makes. The scaling by 8 is
* to capture 8th's of a hp fraction regens
*/
uint32_t last_heal = op->last_heal + op->stats.Con * 8 / op->speed;
op->stats.hp = min (op->stats.hp + last_heal / 32, op->stats.maxhp); /* causes Con/4 hp/tick */
op->last_heal = last_heal % 32;
/* So if the monster has gained enough HP that they are no longer afraid */
if (op->flag [FLAG_RUN_AWAY] && op->stats.hp * 100 >= op->stats.maxhp * op->run_away)
op->flag [FLAG_RUN_AWAY] = false;
}
/* generate sp, if applicable */
if (op->stats.Pow > 0 && op->stats.sp < op->stats.maxsp)
{
/* last_sp is in funny units. Dividing by speed puts
* the regeneration rate on a basis of time instead of
* #moves the monster makes. The scaling by 8 is
* to capture 8th's of a sp fraction regens
*/
uint32_t last_sp = op->last_sp + op->stats.Pow * 8 / op->speed;
op->stats.sp = min (op->stats.sp + last_sp / 128, op->stats.maxsp); /* causes Pow/16 sp/tick */
op->last_sp = last_sp % 128;
}
/* this should probably get modified by many more values.
* (eg, creatures resistance to fear, level, etc. )
*/
if (op->flag [FLAG_SCARED] && !(rndm (20)))
op->clr_flag (FLAG_SCARED); /* Time to regain some "guts"... */
if (INVOKE_OBJECT (MONSTER_MOVE, op, ARG_OBJECT (op->enemy)))
return op->flag [FLAG_FREED];
if (op->flag [FLAG_SLEEP] || op->flag [FLAG_BLIND] ||
((op->map->darklevel () > 0) && !op->flag [FLAG_SEE_IN_DARK] && !op->flag [FLAG_SEE_INVISIBLE]))
if (!check_wakeup (op, enemy, &rv))
return 0;
/* check if monster pops out of hidden spot */
if (op->flag [FLAG_HIDDEN])
do_hidden_move (op);
if (op->pick_up)
monster_check_pickup (op);
if (op->will_apply)
monster_apply_below (op); /* Check for items to apply below */
/* If we don't have an enemy, do special movement or the like */
if (!enemy)
{
if (op->flag [FLAG_ONLY_ATTACK])
{
op->drop_and_destroy ();
return 1;
}
/* Probably really a bug for a creature to have both
* stand still and a movement type set.
*/
if (!op->flag [FLAG_STAND_STILL])
{
if (op->attack_movement & HI4)
{
switch (op->attack_movement & HI4)
{
case PETMOVE:
pet_move (op);
break;
case CIRCLE1:
circ1_move (op);
break;
case CIRCLE2:
circ2_move (op);
break;
case PACEV:
pace_movev (op);
break;
case PACEH:
pace_moveh (op);
break;
case PACEV2:
pace2_movev (op);
break;
case PACEH2:
pace2_moveh (op);
break;
case RANDO:
rand_move (op);
break;
case RANDO2:
move_randomly (op);
break;
}
return 0;
}
else if (op->flag [FLAG_RANDOM_MOVE])
move_randomly (op);
} /* stand still */
return 0;
} /* no enemy */
/* We have an enemy. Block immediately below is for pets */
if ((op->attack_movement & HI4) == PETMOVE
&& (owner = op->owner) != NULL
&& !on_same_map (op, owner)
&& !owner->flag [FLAG_REMOVED])
return follow_owner (op, owner);
/* doppleganger code to change monster facing to that of the nearest
* player. Hmm. The code is here, but no monster in the current
* arch set uses it.
*/
if (op->race == shstr_doppleganger)
{
op->face = enemy->face;
op->name = enemy->name;
}
/* Calculate range information for closest body part - this
* is used for the 'skill' code, which isn't that smart when
* it comes to figuring it out - otherwise, giants throw boulders
* into themselves.
*/
get_rangevector (op, enemy, &rv, 0);
/* Move the check for scared up here - if the monster was scared,
* we were not doing any of the logic below, so might as well save
* a few cpu cycles.
*/
if (!op->flag [FLAG_SCARED])
{
rv_vector rv1;
/* now we test every part of an object .... this is a real ugly piece of code */
for (part = op; part; part = part->more)
{
get_rangevector (part, enemy, &rv1, 0x1);
dir = rv1.direction;
/* hm, not sure about this part - in original was a scared flag here too
* but that we test above... so can be old code here
*/
if (op->flag [FLAG_RUN_AWAY])
dir = absdir (dir + 4);
if (op->flag [FLAG_CONFUSED])
dir = absdir (dir + rndm (3) + rndm (3) - 2);
if (op->flag [FLAG_CAST_SPELL] && !(rndm (3)))
if (monster_cast_spell (op, part, enemy, dir, &rv1))
return 0;
if (op->flag [FLAG_READY_SCROLL] && !(rndm (3)))
if (monster_use_scroll (op, part, enemy, dir, &rv1))
return 0;
if (op->flag [FLAG_READY_RANGE] && !(rndm (3)))
if (monster_use_range (op, part, enemy, dir))
return 0;
if (op->flag [FLAG_READY_SKILL] && !(rndm (3)))
if (monster_use_skill (op, rv.part, enemy, rv.direction))
return 0;
if (op->flag [FLAG_READY_BOW] && !(rndm (2)))
if (monster_use_bow (op, part, enemy, dir))
return 0;
} /* for processing of all parts */
} /* If not scared */
part = rv.part;
dir = rv.direction;
//-GPL
// if the enemy is a player, we have los. if los says we
// can directly reach the player, we do not deviate.
// for non-players, we never deviate
if (op->stats.Wis >= 8
&& enemy->contr && enemy->contr->darkness_at (op->map, op->x, op->y) == LOS_BLOCKED)
{
int sdir = 0;
uint32_t &smell = op->ms ().smell;
for (int ndir = 1; ndir <= 8; ++ndir)
{
mapxy pos (op); pos.move (ndir);
if (pos.normalise ())
{
mapspace &ms = pos.ms ();
if (ms.smell > smell)
{
//printf ("%s: found smell, following it, apparently (%d, %d)\n",& op->name,op->ms().smell,ms.smell);//D
if (op->stats.Wis >= 10)
smell = ms.smell - 1; // smarter: tell others
sdir = ndir;
// perturbing the path might let the monster lose track,
// but it will also widen the actual path, spreading information
if (op->stats.Wis >= 15 && !rndm (20)) // even smarter, deviate and spread?
sdir = absdir (sdir + 1 - rndm (2) * 2);
}
}
}
if (sdir)
dir = sdir;
else if (smell)
{
// no better smell found, so assume the player jumped, and erase this smell
//printf ("erasing smell %d\n", op->ms ().smell);//D
unordered_mapwalk (mapwalk_buf, op, -1, -1, 1, 1)
m->at (nx, ny).smell = 0;
}
}
//+GPL
if (op->flag [FLAG_SCARED] || op->flag [FLAG_RUN_AWAY])
dir = absdir (dir + 4);
if (op->flag [FLAG_CONFUSED])
dir = absdir (dir + rndm (3) + rndm (3) - 2);
pre_att_dir = dir; /* remember the original direction */
if ((op->attack_movement & LO4) && !op->flag [FLAG_SCARED])
{
switch (op->attack_movement & LO4)
{
case DISTATT:
dir = dist_att (dir, op, enemy, part, &rv);
break;
case RUNATT:
dir = run_att (dir, op, enemy, part, &rv);
break;
case HITRUN:
dir = hitrun_att (dir, op, enemy);
break;
case WAITATT:
dir = wait_att (dir, op, enemy, part, &rv);
break;
case RUSH: /* default - monster normally moves towards player */
case ALLRUN:
break;
case DISTHIT:
dir = disthit_att (dir, op, enemy, part, &rv);
break;
case WAIT2:
dir = wait_att2 (dir, op, enemy, part, &rv);
break;
default:
LOG (llevDebug, "Illegal low mon-move: %d\n", op->attack_movement & LO4);
}
}
if (!dir)
return 0;
if (!op->flag [FLAG_STAND_STILL])
{
if (op->move (dir)) /* Can the monster move directly toward player? */
{
/* elmex: Turn our monster after it moved if it has DISTATT attack */
if ((op->attack_movement & LO4) == DISTATT)
op->direction = pre_att_dir;
return 0;
}
if (op->flag [FLAG_SCARED] || !can_hit (part, enemy, &rv) || op->flag [FLAG_RUN_AWAY])
{
/* Try move around corners if !close */
int maxdiff = (op->flag [FLAG_ONLY_ATTACK] || rndm (2)) ? 1 : 2;
for (diff = 1; diff <= maxdiff; diff++)
{
/* try different detours */
int m = 1 - rndm (2) * 2; /* Try left or right first? */
if (op->move (absdir (dir + diff * m)) || op->move (absdir (dir - diff * m)))
return 0;
}
}
} /* if monster is not standing still */
/* elmex: Turn our monster after it moved if it has DISTATT attack */
if ((op->attack_movement & LO4) == DISTATT)
op->direction = pre_att_dir;
/*
* Eneq(@csd.uu.se): Patch to make RUN_AWAY or SCARED monsters move a random
* direction if they can't move away.
*/
if (!op->flag [FLAG_ONLY_ATTACK] && (op->flag [FLAG_RUN_AWAY] || op->flag [FLAG_SCARED]))
if (move_randomly (op))
return 0;
/*
* Try giving the monster a new enemy - the player that is closest
* to it. In this way, it won't just keep trying to get to a target
* that is inaccessible.
* This could be more clever - it should go through a list of several
* enemies, as it is now, you could perhaps get situations where there
* are two players flanking the monster at close distance, but which
* the monster can't get to, and a third one at a far distance that
* the monster could get to - as it is, the monster won't look at that
* third one.
*/
if (!op->flag [FLAG_FRIENDLY] && enemy == op->enemy)
{
object *nearest_player = get_nearest_player (op);
if (nearest_player && nearest_player != enemy && !can_hit (part, enemy, &rv))
{
op->enemy = 0;
enemy = nearest_player;
}
}
if (!op->flag [FLAG_SCARED] && can_hit (part, enemy, &rv))
{
/* The adjustement to wc that was here before looked totally bogus -
* since wc can in fact get negative, that would mean by adding
* the current wc, the creature gets better? Instead, just
* add a fixed amount - nasty creatures that are runny away should
* still be pretty nasty.
*/
if (op->flag [FLAG_RUN_AWAY])
{
part->stats.wc += 10;
skill_attack (enemy, part, 0, NULL, NULL);
part->stats.wc -= 10;
}
else
skill_attack (enemy, part, 0, NULL, NULL);
} /* if monster is in attack range */
if (part->flag [FLAG_FREED]) /* Might be freed by ghost-attack or hit-back */
return 1;
if (op->flag [FLAG_ONLY_ATTACK])
{
op->drop_and_destroy ();
return 1;
}
return 0;
}
/* determine if we can 'detect' the enemy. Check for walls blocking the
* los. Also, just because its hidden/invisible, we may be sensitive/smart
* enough (based on Wis & Int) to figure out where the enemy is. -b.t.
* modified by MSW to use the get_rangevector so that map tiling works
* properly. I also so odd code in place that checked for x distance
* OR y distance being within some range - that seemed wrong - both should
* be within the valid range. MSW 2001-08-05
* Returns 0 if enemy can not be detected, 1 if it is detected
*/
int
can_detect_enemy (object *op, object *enemy, rv_vector * rv)
{
/* null detection for any of these condtions always */
if (!op || !enemy || !op->map || !enemy->map)
return 0;
/* If the monster (op) has no way to get to the enemy, do nothing */
if (!on_same_map (op, enemy))
return 0;
get_rangevector (op, enemy, rv, 0);
/* Monsters always ignore the DM */
if (!op->is_player () && enemy->flag [FLAG_WIZ])
return 0;
/* simple check. Should probably put some range checks in here. */
if (can_see_enemy (op, enemy))
return 1;
/* The rest of this is for monsters. Players are on their own for
* finding enemies!
*/
if (op->is_player ())
return 0;
/* Quality invisible? Bah, we wont see them w/o SEE_INVISIBLE
* flag (which was already checked) in can_see_enemy (). Lets get out of here
*/
if (enemy->invisible && (!enemy->contr || (!enemy->contr->tmp_invis && !enemy->contr->hidden)))
return 0;
int radius = MIN_MON_RADIUS;
/* use this for invis also */
int hide_discovery = op->stats.Int / 5;
/* Determine Detection radii */
if (!enemy->flag [FLAG_HIDDEN]) /* to detect non-hidden (eg dark/invis enemy) */
radius = max (MIN_MON_RADIUS, op->stats.Wis / 3 + 1);
else
{ /* a level/INT/Dex adjustment for hiding */
int bonus = op->level / 2 + op->stats.Int / 5;
if (enemy->is_player ())
{
if (object *sk_hide = find_skill_by_number (enemy, SK_HIDING))
bonus -= sk_hide->level;
else
{
LOG (llevError, "can_detect_enemy() got hidden player w/o hiding skill!\n");
make_visible (enemy);
radius = radius < MIN_MON_RADIUS ? MIN_MON_RADIUS : radius;
}
}
else /* enemy is not a player */
bonus -= enemy->level;
radius += bonus / 5;
hide_discovery += bonus * 5;
} /* else creature has modifiers for hiding */
/* Radii stealth adjustment. Only if you are stealthy
* will you be able to sneak up closer to creatures */
if (enemy->flag [FLAG_STEALTH])
{
radius /= 2;
hide_discovery /= 3;
}
/* Radii adjustment for enemy standing in the dark */
if (!stand_in_light (enemy))
{
/* on dark maps body heat can help indicate location with infravision
* undead don't have body heat, so no benefit detecting them.
*/
if (op->flag [FLAG_SEE_IN_DARK] && !is_true_undead (enemy))
radius += op->map->darklevel () / 2;
else
radius -= op->map->darklevel () / 2;
/* op next to a monster (and not in complete darkness)
* the monster should have a chance to see you.
*/
if (radius < MIN_MON_RADIUS && op->map->darklevel () < MAX_DARKNESS && rv->distance <= 1)
radius = MIN_MON_RADIUS;
} /* if on dark map */
/* Lets not worry about monsters that have incredible detection
* radii, we only need to worry here about things the player can
* (potentially) see. This is 13, as that is the maximum size the player
* may have for their map - in that way, creatures at the edge will
* do something. Note that the distance field in the
* vector is real distance, so in theory this should be 18 to
* find that.
*/
// note that the above reasoning was utter bullshit even at the time it was written
// we use 25, lets see if we have the cpu time for it
min_it (radius, 25);
/* Enemy in range! Now test for detection */
if (rv->distance <= radius)
{
/* ah, we are within range, detected? take cases */
if (!enemy->invisible) /* enemy in dark squares... are seen! */
return 1;
/* hidden or low-quality invisible */
if (enemy->flag [FLAG_HIDDEN] && rv->distance <= 1 && rndm (100) <= hide_discovery)
{
make_visible (enemy);
/* inform players of new status */
if (enemy->type == PLAYER && player_can_view (enemy, op))
new_draw_info_format (NDI_UNIQUE, 0, enemy, "You are discovered by %s!", &op->name);
return 1; /* detected enemy */
}
else if (enemy->invisible)
{
/* Change this around - instead of negating the invisible, just
* return true so that the monster that managed to detect you can
* do something to you. Decreasing the duration of invisible
* doesn't make a lot of sense IMO, as a bunch of stupid creatures
* can then basically negate the spell. The spell isn't negated -
* they just know where you are!
*/
if (rndm (50) <= hide_discovery)
{
if (enemy->type == PLAYER)
new_draw_info_format (NDI_UNIQUE, 0, enemy, "You see %s noticing your position.", query_name (op));
return 1;
}
}
} /* within range */
/* Wasn't detected above, so still hidden */
return 0;
}
/* determine if op stands in a lighted square. This is not a very
* intelligent algorithm. For one thing, we ignore los here, SO it
* is possible for a bright light to illuminate a player on the
* other side of a wall (!).
*/
int
stand_in_light (object *op)
{
if (op)
{
if (!op->is_on_map ())
return 0;
if (op->map->darklevel () <= 0)
return 1;
if (op->glow_radius > 0)
return 1;
if (op->map)
unordered_mapwalk (mapwalk_buf, op, -MAX_LIGHT_RADIUS, -MAX_LIGHT_RADIUS, MAX_LIGHT_RADIUS, MAX_LIGHT_RADIUS)
{
mapspace &ms = m->at (nx, ny);
ms.update ();
/* Check the spaces with the max light radius to see if any of them
* have lights, and if any of them light the player enough, then return 1.
*/
int light = ms.light;
if (expect_false (light > 0) && idistance (dx, dy) <= light)
return 1;
}
}
return 0;
}
/* assuming no walls/barriers, lets check to see if its *possible*
* to see an enemy. Note, "detection" is different from "seeing".
* See can_detect_enemy() for more details. -b.t.
* return 0 if can't be seen, 1 if can be
*/
int
can_see_enemy (object *op, object *enemy)
{
object *looker = op->head ? op->head : op;
/* safety */
if (!looker || !enemy || !looker->flag [FLAG_ALIVE])
return 0;
/* we dont give a full treatment of xrays here (shorter range than normal,
* see through walls). Should we change the code elsewhere to make you
* blind even if you can xray?
*/
if (looker->flag [FLAG_BLIND] && !looker->flag [FLAG_XRAYS])
return 0;
/* checking for invisible things */
if (enemy->invisible)
{
/* HIDDEN ENEMY. by definition, you can't see hidden stuff!
* However,if you carry any source of light, then the hidden
* creature is seeable (and stupid) */
if (enemy->has_carried_lights ())
{
if (enemy->flag [FLAG_HIDDEN])
{
make_visible (enemy);
new_draw_info (NDI_UNIQUE, 0, enemy, "Your light reveals your hiding spot!");
}
return 1;
}
else if (enemy->flag [FLAG_HIDDEN])
return 0;
/* Invisible enemy. Break apart the check for invis undead/invis looker
* into more simple checks - the QUERY_FLAG doesn't return 1/0 values,
* and making it a conditional makes the code pretty ugly.
*/
if (!looker->flag [FLAG_SEE_INVISIBLE])
if (makes_invisible_to (enemy, looker))
return 0;
}
else if (looker->is_player ()) /* for players, a (possible) shortcut */
if (player_can_view (looker, enemy))
return 1;
/* ENEMY IN DARK MAP. Without infravision, the enemy is not seen
* unless they carry a light or stand in light. Darkness doesnt
* inhibit the undead per se (but we should give their archs
* CAN_SEE_IN_DARK, this is just a safety
* we care about the enemy maps status, not the looker.
* only relevant for tiled maps, but it is possible that the
* enemy is on a bright map and the looker on a dark - in that
* case, the looker can still see the enemy
*/
if (!stand_in_light (enemy)
&& (!looker->flag [FLAG_SEE_IN_DARK] || !is_true_undead (looker) || !looker->flag [FLAG_XRAYS]))
return 0;
return 1;
}
//-GPL