Angband
Functions
mon-move.c File Reference

Monster movement. More...

#include "angband.h"
#include "cave.h"
#include "game-world.h"
#include "init.h"
#include "monster.h"
#include "mon-attack.h"
#include "mon-desc.h"
#include "mon-lore.h"
#include "mon-make.h"
#include "mon-predicate.h"
#include "mon-spell.h"
#include "mon-util.h"
#include "mon-timed.h"
#include "obj-desc.h"
#include "obj-ignore.h"
#include "obj-pile.h"
#include "obj-slays.h"
#include "obj-tval.h"
#include "obj-util.h"
#include "player-calcs.h"
#include "player-util.h"
#include "project.h"
#include "trap.h"

Functions

static void find_range (struct monster *mon)
 Calculate minimum and desired combat ranges. More...
 
static bool near_permwall (const struct monster *mon, struct chunk *c)
 From Will Asher in DJA: Find whether a monster is near a permanent wall this decides whether PASS_WALL & KILL_WALL monsters use the monster flow code. More...
 
static bool get_moves_advance (struct chunk *c, struct monster *mon)
 Choose the best direction to advance toward the player, using sound or scent. More...
 
static bool monster_can_hear (struct chunk *c, struct monster *mon)
 Check if the monster can hear anything. More...
 
static bool monster_can_smell (struct chunk *c, struct monster *mon)
 Check if the monster can smell anything. More...
 
static bool get_moves_fear (struct chunk *c, struct monster *mon)
 Provide a location to flee to, but give the player a wide berth. More...
 
static bool find_safety (struct chunk *c, struct monster *mon)
 Choose a "safe" location near a monster for it to run toward. More...
 
static bool find_hiding (struct chunk *c, struct monster *mon)
 Choose a good hiding place near a monster for it to run toward. More...
 
static int choose_direction (int dy, int dx)
 Choose the basic direction of movement, and whether to bias left or right if the main direction is blocked. More...
 
static bool get_moves (struct chunk *c, struct monster *mon, int *dir)
 Choose "logical" directions for monster movement. More...
 
bool multiply_monster (const struct monster *mon)
 Lets the given monster attempt to reproduce. More...
 
static bool process_monster_multiply (struct chunk *c, struct monster *mon)
 Attempt to reproduce, if possible. More...
 
static bool process_monster_should_stagger (struct monster *mon)
 Check if a monster should stagger or not. More...
 
static bool process_monster_can_move (struct chunk *c, struct monster *mon, const char *m_name, int nx, int ny, bool *did_something)
 Work out if a monster can move through the grid, if necessary bashing down doors in the way. More...
 
static bool process_monster_glyph (struct chunk *c, struct monster *mon, int nx, int ny)
 Try to break a glyph. More...
 
static int compare_monsters (const struct monster *mon1, const struct monster *mon2)
 Compare the "strength" of two monsters XXX XXX XXX. More...
 
static bool process_monster_try_push (struct chunk *c, struct monster *mon, const char *m_name, int nx, int ny)
 Try to push past / kill another monster. More...
 
void process_monster_grab_objects (struct chunk *c, struct monster *mon, const char *m_name, int nx, int ny)
 Grab all objects from the grid. More...
 
static void process_monster (struct chunk *c, struct monster *mon)
 Process a monster. More...
 
static bool monster_check_active (struct chunk *c, struct monster *mon)
 Determine whether a monster is active or passive. More...
 
static void monster_reduce_sleep (struct monster *mon)
 Wake a monster or reduce its depth of sleep. More...
 
static bool process_monster_timed (struct chunk *c, struct monster *mon)
 Process a monster's timed effects, e.g. More...
 
static void regen_monster (struct monster *mon)
 Monster regeneration of HPs. More...
 
void process_monsters (struct chunk *c, int minimum_energy)
 Process all the "live" monsters, once per game turn. More...
 
void reset_monsters (void)
 Clear 'moved' status from all monsters. More...
 

Detailed Description

Monster movement.

Monster AI affecting movement and spells, process a monster (with spells and actions of all kinds, reproduction, effects of any terrain on monster movement, picking up and destroying objects), process all monsters.

Copyright (c) 1997 Ben Harrison, David Reeve Sward, Keldon Jones.

This work is free software; you can redistribute it and/or modify it under the terms of either:

a) the GNU General Public License as published by the Free Software Foundation, version 2, or

b) the "Angband licence": This software may be copied and distributed for educational, research, and not for profit purposes provided that this copyright and statement are included in all such copies. Other copyrights may also apply.

Function Documentation

static int choose_direction ( int  dy,
int  dx 
)
static

Choose the basic direction of movement, and whether to bias left or right if the main direction is blocked.

Note that this direction is intended as an index into the side_dirs array.

References ABS, and turn.

Referenced by get_moves().

static int compare_monsters ( const struct monster mon1,
const struct monster mon2 
)
static

Compare the "strength" of two monsters XXX XXX XXX.

References monster_race::mexp, and monster::race.

Referenced by process_monster_try_push().

static bool find_hiding ( struct chunk c,
struct monster mon 
)
static

Choose a good hiding place near a monster for it to run toward.

Pack monsters will use this to "ambush" the player and lure him out of corridors into open space so they can swarm him.

Return true if a good location is available.

References dist_offsets_x, dist_offsets_y, distance(), monster::fx, monster::fy, i, PROJECT_STOP, projectable(), player::px, player::py, square_in_bounds_fully(), square_isempty(), square_isview(), monster::tx, and monster::ty.

Referenced by get_moves().

static void find_range ( struct monster mon)
static
static bool find_safety ( struct chunk c,
struct monster mon 
)
static

Choose a "safe" location near a monster for it to run toward.

A location is "safe" if it can be reached quickly and the player is not able to fire into it (it isn't a "clean shot"). So, this will cause monsters to "duck" behind walls. Hopefully, monsters will also try to run towards corridor openings if they are in a room.

This function may take lots of CPU time if lots of monsters are fleeing.

Return true if a safe location is available.

References dist_offsets_x, dist_offsets_y, distance(), monster_race::flags, monster::fx, monster::fy, heatmap::grids, i, chunk::noise, player::px, player::py, monster::race, rf_has, square_in_bounds_fully(), square_isfiery(), square_ispassable(), square_isview(), monster::tx, and monster::ty.

Referenced by get_moves().

static bool get_moves ( struct chunk c,
struct monster mon,
int dir 
)
static
static bool get_moves_advance ( struct chunk c,
struct monster mon 
)
static

Choose the best direction to advance toward the player, using sound or scent.

Note that ghosts and rock-eaters generally just head straight for the player.

Monsters first try to use current sound information as saved in cave->noise.grids[y][x]. Failing that, they'll try using scent, saved in cave->scent.grids[y][x].

Tracking by 'scent' means that monsters end up near enough the player to switch to 'sound' (noise), or they end up somewhere the player left via teleport. Teleporting away from a location will cause the monsters who were chasing the player to converge on that location as long as the player is still near enough to "annoy" them without being close enough to chase directly.

References cave, ddx_ddd, ddy_ddd, monster::fx, monster::fy, heatmap::grids, monster_race::hearing, i, monster_passes_walls(), near_permwall(), chunk::noise, player::px, player::py, monster::race, chunk::scent, SKILL_STEALTH, player_state::skills, monster_race::smell, square_in_bounds(), square_isview(), player::state, monster::tx, and monster::ty.

Referenced by get_moves().

static bool get_moves_fear ( struct chunk c,
struct monster mon 
)
static

Provide a location to flee to, but give the player a wide berth.

A monster may wish to flee to a location that is behind the player, but instead of heading directly for it, the monster should "swerve" around the player so that it has a smaller chance of getting hit.

References monster::best_range, monster::cdis, ddx_ddd, ddy_ddd, distance(), monster::fx, monster::fy, heatmap::grids, i, monster_can_hear(), monster_can_smell(), chunk::noise, square_in_bounds(), monster::tx, and monster::ty.

Referenced by get_moves().

static bool monster_can_hear ( struct chunk c,
struct monster mon 
)
static
static bool monster_can_smell ( struct chunk c,
struct monster mon 
)
static

Check if the monster can smell anything.

References cave, monster::fx, monster::fy, heatmap::grids, monster::race, chunk::scent, and monster_race::smell.

Referenced by get_moves_fear(), and monster_check_active().

static bool monster_check_active ( struct chunk c,
struct monster mon 
)
static
static void monster_reduce_sleep ( struct monster mon)
static

Wake a monster or reduce its depth of sleep.

Chance of waking up is dependent only on the player's stealth, but the amount of sleep reduction takes into account the monster's distance from the player. Currently straight line distance is used; possibly this should take into account dungeon structure.

References cave, monster::fx, monster::fy, get_lore(), heatmap::grids, monster_lore::ignore, lore_update(), monster::m_timed, MDESC_CAPITAL, MDESC_IND_HID, mon_clear_timed(), mon_dec_timed(), MON_TMD_FLG_NOMESSAGE, MON_TMD_FLG_NOTIFY, monster_desc(), monster_is_obvious(), msg, chunk::noise, player_of_has(), monster::race, randint0, SKILL_STEALTH, player_state::skills, player::state, and monster_lore::wake.

Referenced by process_monster_timed().

bool multiply_monster ( const struct monster mon)

Lets the given monster attempt to reproduce.

Note that "reproduction" REQUIRES empty space.

Returns true if the monster successfully reproduced.

References cave, monster::fx, monster::fy, i, place_new_monster(), monster::race, scatter(), and square_isempty().

Referenced by process_monster_multiply(), and project_monster_handler_MON_CLONE().

static bool near_permwall ( const struct monster mon,
struct chunk c 
)
static

From Will Asher in DJA: Find whether a monster is near a permanent wall this decides whether PASS_WALL & KILL_WALL monsters use the monster flow code.

References cave, monster::fx, monster::fy, PROJECT_NONE, projectable(), player::px, player::py, randint0, square_in_bounds_fully(), and square_isperm().

Referenced by get_moves_advance().

static void process_monster ( struct chunk c,
struct monster mon 
)
static

Process a monster.

In several cases, we directly update the monster lore

Note that a monster is only allowed to "reproduce" if there are a limited number of "reproducing" monsters on the current level. This should prevent the level from being "swamped" by reproducing monsters. It also allows a large mass of mice to prevent a louse from multiplying, but this is a small price to pay for a simple multiplication method.

XXX Monster fear is slightly odd, in particular, monsters will fixate on opening a door even if they cannot open it. Actually, the same thing happens to normal monsters when they hit a door

In addition, monsters which cannot open or bash down a door will still stand there trying to open it... XXX XXX XXX

Technically, need to check for monster in the way combined with that monster being in a wall (or door?) XXX

References become_aware(), ddd, ddx, ddy, disturb(), monster_lore::flags, monster_race::flags, monster::fx, monster::fy, get_lore(), get_moves(), i, monster::m_timed, make_attack_normal(), make_attack_spell(), MDESC_CAPITAL, MDESC_IND_HID, mon_clear_timed(), MON_TMD_FLG_NOTIFY, monster_desc(), monster_is_camouflaged(), monster_is_in_view(), monster_is_visible(), monster_swap(), OPT, ox, oy, process_monster_can_move(), process_monster_glyph(), process_monster_grab_objects(), process_monster_multiply(), process_monster_should_stagger(), process_monster_try_push(), monster::race, randint0, rf_has, rf_on, side_dirs, square_isplayer(), square_isview(), square_iswarded(), and square_monster().

Referenced by process_monsters().

static bool process_monster_can_move ( struct chunk c,
struct monster mon,
const char *  m_name,
int  nx,
int  ny,
bool *  did_something 
)
static
static bool process_monster_glyph ( struct chunk c,
struct monster mon,
int  nx,
int  ny 
)
static
void process_monster_grab_objects ( struct chunk c,
struct monster mon,
const char *  m_name,
int  nx,
int  ny 
)
static bool process_monster_multiply ( struct chunk c,
struct monster mon 
)
static
static bool process_monster_should_stagger ( struct monster mon)
static

Check if a monster should stagger or not.

Always stagger when confused, but also deal with random movement for RAND_25 and _50 monsters.

References CONF_ERRATIC_CHANCE, monster_lore::flags, monster_race::flags, get_lore(), monster::m_timed, monster_is_visible(), monster::race, randint0, rf_has, and rf_on.

Referenced by process_monster().

static bool process_monster_timed ( struct chunk c,
struct monster mon 
)
static

Process a monster's timed effects, e.g.

decrease them.

Returns true if the monster is skipping its turn.

References monster_race::level, monster::m_timed, mon_dec_timed(), MON_TMD_FLG_NOTIFY, monster_reduce_sleep(), monster::race, randint0, randint1, and STUN_MISS_CHANCE.

Referenced by process_monsters().

static bool process_monster_try_push ( struct chunk c,
struct monster mon,
const char *  m_name,
int  nx,
int  ny 
)
static
void process_monsters ( struct chunk c,
int  minimum_energy 
)

Process all the "live" monsters, once per game turn.

During each game turn, we scan through the list of all the "live" monsters, (backwards, so we can excise any "freshly dead" monsters), energizing each monster, and allowing fully energized monsters to move, attack, pass, etc.

This function and its children are responsible for a considerable fraction of the processor time in normal situations, greater if the character is resting.

References cave_monster(), cave_monster_max(), monster::energy, player_upkeep::generate_level, i, player::is_dead, monster::m_timed, monster::mflag, mflag_has, mflag_on, chunk::mon_current, monster_check_active(), monster_is_mimicking(), angband_constants::move_energy, monster::mspeed, process_monster(), process_monster_timed(), PU_MONSTERS, monster::race, regen_monster(), turn, turn_energy(), player_upkeep::update, player::upkeep, and z_info.

Referenced by run_game_loop().

static void regen_monster ( struct monster mon)
static
void reset_monsters ( void  )

Clear 'moved' status from all monsters.

Clear noise if appropriate.

References cave, cave_monster(), cave_monster_max(), i, monster::mflag, and mflag_off.

Referenced by run_game_loop().