Angband
Functions | Variables
mon-make.c File Reference

Monster creation / placement code. More...

#include "angband.h"
#include "alloc.h"
#include "game-world.h"
#include "init.h"
#include "mon-group.h"
#include "mon-lore.h"
#include "mon-make.h"
#include "mon-predicate.h"
#include "mon-timed.h"
#include "mon-util.h"
#include "obj-knowledge.h"
#include "obj-make.h"
#include "obj-pile.h"
#include "obj-tval.h"
#include "obj-util.h"
#include "player-calcs.h"
#include "target.h"

Functions

static void init_race_allocs (void)
 Initialize monster allocation info. More...
 
static void cleanup_race_allocs (void)
 
void get_mon_num_prep (bool(*get_mon_num_hook)(struct monster_race *race))
 Apply a monster restriction function to the monster allocation table. More...
 
static struct monster_raceget_mon_race_aux (long total, const alloc_entry *table)
 Helper function for get_mon_num(). More...
 
struct monster_raceget_mon_num (int level)
 Chooses a monster race that seems appropriate to the given level. More...
 
void delete_monster_idx (int m_idx)
 

Deleting of monsters and monster list handling

More...
 
void delete_monster (struct loc grid)
 Deletes the monster, if any, at the given location. More...
 
static void compact_monsters_aux (int i1, int i2)
 Move a monster from index i1 to index i2 in the monster list. More...
 
void compact_monsters (int num_to_compact)
 Compacts and reorders the monster list. More...
 
void wipe_mon_list (struct chunk *c, struct player *p)
 Deletes all the monsters when the player leaves the level. More...
 
s16b mon_pop (struct chunk *c)
 

Monster creation utilities: Getting a new monster index Creating objects for monsters to carry or mimic

Calculating hitpoints

More...
 
int mon_create_drop_count (const struct monster_race *race, bool maximize)
 Return the number of things dropped by a monster. More...
 
static bool mon_create_drop (struct chunk *c, struct monster *mon, byte origin)
 Creates a specific monster's drop, including any drops specified in the monster.txt file. More...
 
void mon_create_mimicked_object (struct chunk *c, struct monster *mon, int index)
 Creates the onbject a mimic is imitating. More...
 
int mon_hp (const struct monster_race *race, aspect hp_aspect)
 Calculates hp for a monster. More...
 
s16b place_monster (struct chunk *c, struct loc grid, struct monster *mon, byte origin)
 

Placement of a single monster

These are the functions that actually put the monster into the world

More...
 
static bool place_new_monster_one (struct chunk *c, struct loc grid, struct monster_race *race, bool sleep, struct monster_group_info group_info, byte origin)
 Attempts to place a monster of the given race at the given location. More...
 
static bool place_new_monster_group (struct chunk *c, struct loc grid, struct monster_race *race, bool sleep, struct monster_group_info group_info, int total, byte origin)
 

More complex monster placement routines

More...
 
static bool place_monster_base_okay (struct monster_race *race)
 Predicate function for get_mon_num_prep() Check to see if the monster race has the same base as place_monter_base. More...
 
bool place_friends (struct chunk *c, struct loc grid, struct monster_race *race, struct monster_race *friends_race, int total, bool sleep, struct monster_group_info group_info, byte origin)
 Helper function to place monsters that appear as friends or escorts. More...
 
bool place_new_monster (struct chunk *c, struct loc grid, struct monster_race *race, bool sleep, bool group_ok, struct monster_group_info group_info, byte origin)
 Attempts to place a monster of the given race at the given location. More...
 
bool pick_and_place_monster (struct chunk *c, struct loc grid, int depth, bool sleep, bool group_okay, byte origin)
 Picks a monster race, makes a new monster of that race, then attempts to place it in the dungeon. More...
 
bool pick_and_place_distant_monster (struct chunk *c, struct player *p, int dis, bool sleep, int depth)
 Picks a monster race, makes a new monster of that race, then attempts to place it in the dungeon at least dis away from the player. More...
 

Variables

static s16b alloc_race_size
 

Monster race allocation More...

 
static struct alloc_entryalloc_race_table
 
static struct monster_baseplace_monster_base = NULL
 
struct init_module mon_make_module
 

Detailed Description

Monster creation / placement code.

Copyright (c) 1997-2007 Ben Harrison, James E. Wilson, Robert A. Koeneke

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

◆ cleanup_race_allocs()

static void cleanup_race_allocs ( void  )
static

References mem_free().

◆ compact_monsters()

void compact_monsters ( int  num_to_compact)

Compacts and reorders the monster list.

This function can be very dangerous, use with caution!

When num_to_compact is 0, we just reorder the monsters into a more compact order, eliminating any "holes" left by dead monsters. If num_to_compact is positive, then we delete at least that many monsters and then reorder. We try not to delete monsters that are high level or close to the player. Each time we make a full pass through the monster list, if we haven't deleted enough monsters, we relax our bounds a little to accept monsters of a slightly higher level, and monsters slightly closer to the player.

References cave, cave_monster(), cave_monster_max(), monster::cdis, compact_monsters_aux(), delete_monster(), monster_race::flags, monster::grid, iter, monster_race::level, chunk::mon_max, msg, monster::race, randint0, and rf_has.

Referenced by prepare_next_level(), process_world(), and wr_dungeon().

◆ compact_monsters_aux()

static void compact_monsters_aux ( int  i1,
int  i2 
)
static

◆ delete_monster()

void delete_monster ( struct loc  grid)

Deletes the monster, if any, at the given location.

References cave, delete_monster_idx(), square(), and square_in_bounds().

Referenced by compact_monsters(), effect_handler_DESTRUCTION(), effect_handler_EARTHQUAKE(), and monster_turn_try_push().

◆ delete_monster_idx()

void delete_monster_idx ( int  m_idx)

◆ get_mon_num()

struct monster_race* get_mon_num ( int  level)

Chooses a monster race that seems appropriate to the given level.

This function uses the "prob2" field of the monster allocation table, and various local information, to calculate the "prob3" field of the same table, which is then used to choose an appropriate monster, in a relatively efficient manner.

Note that town monsters will only be created in the town, and "normal" monsters will never be created in the town, unless the level is modified, for example, by polymorph or summoning.

There is a small chance (1/25) of boosting the given depth by a small amount (up to four levels), except in the town.

It is (slightly) more likely to acquire a monster of the given level than one of a lower level. This is done by choosing several monsters appropriate to the given level and keeping the deepest one.

Note that if no monsters are appropriate, then this function will fail, and return zero, but this should almost never happen.

References alloc_race_size, alloc_race_table, monster_race::cur_num, player::depth, monster_race::flags, get_mon_race_aux(), i, alloc_entry::index, monster_race::level, monster_race::max_num, MIN, one_in_, angband_constants::ood_monster_amount, angband_constants::ood_monster_chance, p, alloc_entry::prob3, r_info, randint0, rf_has, and z_info.

Referenced by build_nest(), build_pit(), get_chamber_monsters(), get_vault_monsters(), monster_change_shape(), pick_and_place_monster(), place_new_monster(), poly_race(), select_shape(), spread_monsters(), and summon_specific().

◆ get_mon_num_prep()

void get_mon_num_prep ( bool(*)(struct monster_race *race)  get_mon_num_hook)

Apply a monster restriction function to the monster allocation table.

This way, we can use get_mon_num() to get a level-appropriate monster that satisfies certain conditions (such as belonging to a particular monster family).

References alloc_race_size, i, alloc_entry::index, alloc_entry::prob1, alloc_entry::prob2, and r_info.

Referenced by build_nest(), build_pit(), get_vault_monsters(), mon_restrict(), monster_change_shape(), place_new_monster(), select_shape(), and summon_specific().

◆ get_mon_race_aux()

static struct monster_race* get_mon_race_aux ( long  total,
const alloc_entry table 
)
static

Helper function for get_mon_num().

Scans the prepared monster allocation table and picks a random monster. Returns the index of a monster in table.

References alloc_race_size, i, alloc_entry::index, alloc_entry::prob3, r_info, randint0, and value.

Referenced by get_mon_num().

◆ init_race_allocs()

static void init_race_allocs ( void  )
static

◆ mon_create_drop()

static bool mon_create_drop ( struct chunk c,
struct monster mon,
byte  origin 
)
static

◆ mon_create_drop_count()

int mon_create_drop_count ( const struct monster_race race,
bool  maximize 
)

Return the number of things dropped by a monster.

Parameters
raceis the monster race.
maximizeshould be set to false for a random number, true to find out the maximum count.

References monster_race::flags, rand_range(), randint0, and rf_has.

Referenced by lore_append_drop(), and mon_create_drop().

◆ mon_create_mimicked_object()

void mon_create_mimicked_object ( struct chunk c,
struct monster mon,
int  index 
)

◆ mon_hp()

int mon_hp ( const struct monster_race race,
aspect  hp_aspect 
)

Calculates hp for a monster.

This function assumes that the Rand_normal function has limits of +/- 4x std_dev. If that changes, this function will become inaccurate.

Parameters
raceis the race of the monster in question.
hp_aspectis the hp calc we want (min, max, avg, random).

References AVERAGE, monster_race::avg_hp, EXTREMIFY, MAXIMISE, MINIMISE, Rand_normal(), and RANDOMISE.

Referenced by place_new_monster_one().

◆ mon_pop()

s16b mon_pop ( struct chunk c)


Monster creation utilities: Getting a new monster index Creating objects for monsters to carry or mimic

Calculating hitpoints

Returns the index of a "free" monster, or 0 if no slot is available.

This routine should almost never fail, but it can happen. The calling code must check for and handle a 0 return.

References cave_monster(), cave_monster_max(), character_dungeon, angband_constants::level_monster_max, chunk::mon_cnt, chunk::mon_max, msg, monster::race, and z_info.

Referenced by chunk_copy(), and place_monster().

◆ pick_and_place_distant_monster()

bool pick_and_place_distant_monster ( struct chunk c,
struct player p,
int  dis,
bool  sleep,
int  depth 
)

Picks a monster race, makes a new monster of that race, then attempts to place it in the dungeon at least dis away from the player.

The monster race chosen will be appropriate for dungeon level equal to depth.

If sleep is true, the monster is placed with its default sleep value, which is given in monster.txt.

Returns true if we successfully place a monster.

References character_dungeon, distance(), player::grid, chunk::height, loc(), msg, OPT, pick_and_place_monster(), randint0, square_isempty(), square_ismon_restrict(), and chunk::width.

Referenced by cavern_gen(), classic_gen(), gauntlet_gen(), hard_centre_gen(), labyrinth_gen(), lair_gen(), modified_gen(), moria_gen(), process_world(), and town_gen().

◆ pick_and_place_monster()

bool pick_and_place_monster ( struct chunk c,
struct loc  grid,
int  depth,
bool  sleep,
bool  group_okay,
byte  origin 
)

Picks a monster race, makes a new monster of that race, then attempts to place it in the dungeon.

The monster race chosen will be appropriate for dungeon level equal to depth.

If sleep is true, the monster is placed with its default sleep value, which is given in monster.txt.

If group_okay is true, we allow the placing of a group, if the chosen monster appears with friends or an escort.

origin is the item origin to use for any monster drops (e.g. ORIGIN_DROP, ORIGIN_DROP_PIT, etc.)

Returns true if we successfully place a monster.

References get_mon_num(), and place_new_monster().

Referenced by build_vault(), get_chamber_monsters(), get_vault_monsters(), pick_and_place_distant_monster(), spread_monsters(), and vault_monsters().

◆ place_friends()

bool place_friends ( struct chunk c,
struct loc  grid,
struct monster_race race,
struct monster_race friends_race,
int  total,
bool  sleep,
struct monster_group_info  group_info,
byte  origin 
)

◆ place_monster()

s16b place_monster ( struct chunk c,
struct loc  grid,
struct monster mon,
byte  origin 
)


Placement of a single monster

These are the functions that actually put the monster into the world

Attempts to place a copy of the given monster at the given position in the dungeon.

All of the monster placement routines eventually call this function. This is what actually puts the monster in the dungeon (i.e., it notifies the cave and sets the monster's position). The dungeon loading code also calls this function directly.

origin is the item origin to use for any monster drops (e.g. ORIGIN_DROP, ORIGIN_DROP_PIT, etc.) The dungeon loading code calls this with origin = 0, which prevents the monster's drops from being generated again.

Returns the m_idx of the newly copied monster, or 0 if the placement fails.

References cave_monster(), monster_race::cur_num, monster_race::flags, monster::grid, monster::group_info, monster::midx, monster_race::mimic_kinds, chunk::mon_cnt, mon_create_drop(), mon_create_mimicked_object(), chunk::mon_max, mon_pop(), monster_group_assign(), chunk::num_repro, monster::race, rf_has, square_in_bounds(), square_monster(), square_set_mon(), update_mon(), and void().

Referenced by place_new_monster_one(), and rd_monsters_aux().

◆ place_monster_base_okay()

static bool place_monster_base_okay ( struct monster_race race)
static

Predicate function for get_mon_num_prep() Check to see if the monster race has the same base as place_monter_base.

References monster_race::base, monster_race::flags, and rf_has.

Referenced by place_new_monster().

◆ place_new_monster()

bool place_new_monster ( struct chunk c,
struct loc  grid,
struct monster_race race,
bool  sleep,
bool  group_ok,
struct monster_group_info  group_info,
byte  origin 
)

Attempts to place a monster of the given race at the given location.

Note that certain monsters are placed with a large group of identical or similar monsters. However, if group_okay is false, then such monsters are placed by themselves.

If sleep is true, the monster is placed with its default sleep value, which is given in monster.txt.

origin is the item origin to use for any monster drops (e.g. ORIGIN_DROP, ORIGIN_DROP_PIT, etc.)

References monster_friends_base::base, damroll(), monster_race::friends, monster_race::friends_base, get_mon_num(), get_mon_num_prep(), monster_group_info::index, monster_race::level, monster_group_index_new(), monster_friends::next, monster_friends_base::next, monster_friends::number_dice, monster_friends_base::number_dice, monster_friends::number_side, monster_friends_base::number_side, monster_friends::percent_chance, monster_friends_base::percent_chance, place_friends(), place_monster_base_okay(), place_new_monster_one(), monster_friends::race, randint0, monster_friends::role, monster_friends_base::role, and monster_group_info::role.

Referenced by build_nest(), build_pit(), cave_generate(), do_cmd_wiz_named(), multiply_monster(), pick_and_place_monster(), project_m_apply_side_effects(), and summon_specific().

◆ place_new_monster_group()

static bool place_new_monster_group ( struct chunk c,
struct loc  grid,
struct monster_race race,
bool  sleep,
struct monster_group_info  group_info,
int  total,
byte  origin 
)
static


More complex monster placement routines

Attempts to place a group of monsters of race r_idx around the given location. The number of monsters to place is total.

If sleep is true, the monster is placed with its default sleep value, which is given in monster.txt.

origin is the item origin to use for any monster drops (e.g. ORIGIN_DROP, ORIGIN_DROP_PIT, etc.)

References ddgrid_ddd, i, loc_sum(), mem_free(), mem_zalloc(), MIN, angband_constants::monster_group_max, place_new_monster_one(), square_isempty(), and z_info.

Referenced by place_friends().

◆ place_new_monster_one()

static bool place_new_monster_one ( struct chunk c,
struct loc  grid,
struct monster_race race,
bool  sleep,
struct monster_group_info  group_info,
byte  origin 
)
static

Attempts to place a monster of the given race at the given location.

If sleep is true, the monster is placed with its default sleep value, which is given in monster.txt.

origin is the item origin to use for any monster drops (e.g. ORIGIN_DROP, ORIGIN_DROP_PIT, etc.)

To give the player a sporting chance, some especially dangerous monsters are marked as "FORCE_SLEEP" in monster.txt, which will cause them to be placed with low energy. This helps ensure that if such a monster suddenly appears in line-of-sight (due to a summon, for instance), the player gets a chance to move before they do.

This routine refuses to place out-of-depth "FORCE_DEPTH" monsters.

This is the only function which may place a monster in the dungeon, except for the savefile loading code, which calls place_monster() directly.

References monster_race::avg_hp, BASIC_COLORS, monster_race::cur_num, chunk::depth, player::depth, monster_race::flags, monster::grid, player::grid, monster::group_info, i, monster_group_info::index, monster_race::level, loc_eq(), monster::m_timed, MAX, monster_race::max_num, monster::maxhp, mflag_off, mflag_on, mon_hp(), chunk::mon_rating, msg, monster_race::name, OPT, place_monster(), PRIMARY_GROUP, PU_UPDATE_VIEW, monster::race, rand_spread, randint0, randint1, RANDOMISE, rf_has, monster_group_info::role, monster_race::sleep, monster_race::speed, square_in_bounds(), square_is_monster_walkable(), square_isdecoyed(), square_iswarded(), square_monster(), turn_energy(), player_upkeep::update, and player::upkeep.

Referenced by place_friends(), place_new_monster(), and place_new_monster_group().

◆ wipe_mon_list()

void wipe_mon_list ( struct chunk c,
struct player p 
)

Deletes all the monsters when the player leaves the level.

This is an efficient method of simulating multiple calls to the "delete_monster()" function, with no visual effects.

Note that we must delete the objects the monsters are carrying, but we do nothing with mimicked objects.

References object::artifact, cave_monster(), cave_monster_max(), artifact::created, monster_race::cur_num, monster::grid, health_track(), monster::held_obj, i, object::known, angband_constants::level_monster_max, chunk::mon_cnt, chunk::mon_max, monster_group_free(), chunk::monster_groups, object::next, chunk::num_repro, object_pile_free(), monster::race, square_set_mon(), target_set_monster(), player::upkeep, and z_info.

Referenced by cave_clear(), close_game(), and teardown_tests().

Variable Documentation

◆ alloc_race_size

s16b alloc_race_size
static


Monster race allocation

Monster race allocation is done using an allocation table (see alloc.h). This table is sorted by depth. Each line of the table contains the monster race index, the monster race level, and three probabilities:

  • prob1 is the base probability of the race, calculated from monster.txt.
  • prob2 is calculated by get_mon_num_prep(), which decides whether a monster is appropriate based on a secondary function; prob2 is always either prob1 or 0.
  • prob3 is calculated by get_mon_num(), which checks whether universal restrictions apply (for example, unique monsters can only appear

    once on a given level); prob3 is always either prob2 or 0.

Referenced by get_mon_num(), get_mon_num_prep(), get_mon_race_aux(), and init_race_allocs().

◆ alloc_race_table

struct alloc_entry* alloc_race_table
static

Referenced by get_mon_num(), and init_race_allocs().

◆ mon_make_module

struct init_module mon_make_module
Initial value:
= {
.name = "monster/mon-make",
}
static void cleanup_race_allocs(void)
Definition: mon-make.c:137
static void init_race_allocs(void)
Initialize monster allocation info.
Definition: mon-make.c:58

◆ place_monster_base

struct monster_base* place_monster_base = NULL
static