Angband
Functions | Variables
gen-util.c File Reference

Dungeon generation utilities. More...

#include "angband.h"
#include "cave.h"
#include "datafile.h"
#include "math.h"
#include "game-event.h"
#include "generate.h"
#include "init.h"
#include "mon-make.h"
#include "mon-spell.h"
#include "obj-make.h"
#include "obj-pile.h"
#include "obj-tval.h"
#include "obj-util.h"
#include "player-util.h"
#include "trap.h"
#include "z-queue.h"
#include "z-type.h"

Functions

int yx_to_i (int y, int x, int w)
 Used to convert (x, y) into an array index (i) in a chunk of width w. More...
 
void i_to_yx (int i, int w, int *y, int *x)
 Used to convert an array index (i) into (x, y) in a chunk of width w. More...
 
void shuffle (int *arr, int n)
 Shuffle an array using Knuth's shuffle. More...
 
static bool _find_in_range (struct chunk *c, int *y, int y1, int y2, int *x, int x1, int x2, square_predicate pred)
 Locate a square in y1 <= y < y2, x1 <= x < x2 which satisfies the given predicate. More...
 
bool cave_find (struct chunk *c, int *y, int *x, square_predicate pred)
 Locate a square in the dungeon which satisfies the given predicate. More...
 
bool cave_find_in_range (struct chunk *c, int *y, int y1, int y2, int *x, int x1, int x2, square_predicate pred)
 Locate a square in y1 <= y < y2, x1 <= x < x2 which satisfies the given predicate. More...
 
bool find_empty (struct chunk *c, int *y, int *x)
 Locate an empty square for 0 <= y < ymax, 0 <= x < xmax. More...
 
bool find_empty_range (struct chunk *c, int *y, int y1, int y2, int *x, int x1, int x2)
 Locate an empty square for y1 <= y < y2, x1 <= x < x2. More...
 
bool find_nearby_grid (struct chunk *c, int *y, int y0, int yd, int *x, int x0, int xd)
 Locate a grid nearby (y0, x0) within +/- yd, xd. More...
 
void correct_dir (int *rdir, int *cdir, int y1, int x1, int y2, int x2)
 Given two points, pick a valid cardinal direction from one to the other. More...
 
void rand_dir (int *rdir, int *cdir)
 Pick a random cardinal direction. More...
 
static bool square_isstart (struct chunk *c, int y, int x)
 Determine whether the given coordinate is a valid starting location. More...
 
void new_player_spot (struct chunk *c, struct player *p)
 Place the player at a random starting location. More...
 
static int next_to_walls (struct chunk *c, int y, int x)
 Return how many cardinal directions around (x, y) contain walls. More...
 
static void place_rubble (struct chunk *c, int y, int x)
 Place rubble at (x, y). More...
 
static void place_stairs (struct chunk *c, int y, int x, int feat)
 Place stairs (of the requested type 'feat' if allowed) at (x, y). More...
 
void place_random_stairs (struct chunk *c, int y, int x)
 Place random stairs at (x, y). More...
 
void place_object (struct chunk *c, int y, int x, int level, bool good, bool great, byte origin, int tval)
 Place a random object at (x, y). More...
 
void place_gold (struct chunk *c, int y, int x, int level, byte origin)
 Place a random amount of gold at (x, y). More...
 
void place_secret_door (struct chunk *c, int y, int x)
 Place a secret door at (x, y). More...
 
void place_closed_door (struct chunk *c, int y, int x)
 Place a closed door at (x, y). More...
 
void place_random_door (struct chunk *c, int y, int x)
 Place a random door at (x, y). More...
 
void alloc_stairs (struct chunk *c, int feat, int num, int walls)
 Place some staircases near walls. More...
 
void alloc_objects (struct chunk *c, int set, int typ, int num, int depth, byte origin)
 Allocates 'num' random entities in the dungeon. More...
 
bool alloc_object (struct chunk *c, int set, int typ, int depth, byte origin)
 Allocates a single random object in the dungeon. More...
 
void vault_objects (struct chunk *c, int y, int x, int depth, int num)
 Create up to 'num' objects near the given coordinates in a vault. More...
 
static void vault_trap_aux (struct chunk *c, int y, int x, int yd, int xd)
 Place a trap near (x, y), with a given displacement. More...
 
void vault_traps (struct chunk *c, int y, int x, int yd, int xd, int num)
 Place 'num' traps near (x, y), with a given displacement. More...
 
void vault_monsters (struct chunk *c, int y1, int x1, int depth, int num)
 Place 'num' sleeping monsters near (x, y). More...
 

Variables

byte get_angle_to_grid [41][41]
 Accept values for y and x (considered as the endpoints of lines) between 0 and 40, and return an angle in degrees (divided by two). More...
 

Detailed Description

Dungeon generation utilities.

Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke Copyright (c) 2013 Erik Osheim, Nick McConnell

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.

This file contains various utility functions for dungeon generation - mostly for finding appropriate grids for some purposes, or placing things.

Function Documentation

static bool _find_in_range ( struct chunk c,
int y,
int  y1,
int  y2,
int x,
int  x1,
int  x2,
square_predicate  pred 
)
static

Locate a square in y1 <= y < y2, x1 <= x < x2 which satisfies the given predicate.

Parameters
ccurrent chunk
yfound y co-ordinate
y1y-range
y2y-range
xfound x co-ordinate
x1x-range
x2x-range
predsquare_predicate specifying what we're looking for
Returns
success

References i, mem_alloc(), mem_free(), and randint0.

Referenced by cave_find(), and cave_find_in_range().

bool alloc_object ( struct chunk c,
int  set,
int  typ,
int  depth,
byte  origin 
)

Allocates a single random object in the dungeon.

Parameters
cthe current chunk
setwhere the entity is placed (corridor, room or either)
typwhat is placed (rubble, trap, gold, item)
depthgeneration depth
originitem origin (if appropriate)

'set' controls where the object is placed (corridor, room, either). 'typ' conrols the kind of object (rubble, trap, gold, item).

References find_empty(), place_gold(), place_object(), place_rubble(), place_trap(), SET_CORR, SET_ROOM, square_isroom(), TYP_GOLD, TYP_GOOD, TYP_GREAT, TYP_OBJECT, TYP_RUBBLE, and TYP_TRAP.

Referenced by alloc_objects().

void alloc_objects ( struct chunk c,
int  set,
int  typ,
int  num,
int  depth,
byte  origin 
)

Allocates 'num' random entities in the dungeon.

Parameters
cthe current chunk
setwhere the entity is placed (corridor, room or either)
typwhat is placed (rubble, trap, gold, item)
numnumber to place
depthgeneration depth
originitem origin (if appropriate)

See alloc_object() for more information.

References alloc_object(), num, and ok.

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

void alloc_stairs ( struct chunk c,
int  feat,
int  num,
int  walls 
)

Place some staircases near walls.

Parameters
cthe current chunk
featthe stair terrain type
numnumber of staircases to place
wallsnumber of walls to surround the stairs (negotiable)

References find_empty(), i, next_to_walls(), num, and place_stairs().

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

bool cave_find ( struct chunk c,
int y,
int x,
square_predicate  pred 
)

Locate a square in the dungeon which satisfies the given predicate.

Parameters
ccurrent chunk
yfound y co-ordinate
xfound x co-ordinate
predsquare_predicate specifying what we're looking for
Returns
success

References _find_in_range(), chunk::height, and chunk::width.

Referenced by find_empty(), and labyrinth_gen().

bool cave_find_in_range ( struct chunk c,
int y,
int  y1,
int  y2,
int x,
int  x1,
int  x2,
square_predicate  pred 
)

Locate a square in y1 <= y < y2, x1 <= x < x2 which satisfies the given predicate.

Parameters
ccurrent chunk
yfound y co-ordinate
y1y-range
y2y-range
xfound x co-ordinate
x1x-range
x2x-range
predsquare_predicate specifying what we're looking for
Returns
success

References _find_in_range().

Referenced by find_empty_range(), find_nearby_grid(), and new_player_spot().

void correct_dir ( int rdir,
int cdir,
int  y1,
int  x1,
int  y2,
int  x2 
)

Given two points, pick a valid cardinal direction from one to the other.

Parameters
rdirfound row change (up or down)
cdirfound column change (left or right)
y1starting co-ordinates
x1starting co-ordinates
y2target co-ordinates
x2target co-ordinates

References CMP, and randint0.

Referenced by build_tunnel().

bool find_empty ( struct chunk c,
int y,
int x 
)

Locate an empty square for 0 <= y < ymax, 0 <= x < xmax.

Parameters
ccurrent chunk
yfound y co-ordinate
xfound x co-ordinate
Returns
success

References cave_find(), and square_isempty().

Referenced by alloc_object(), alloc_stairs(), cave_generate(), and labyrinth_chunk().

bool find_empty_range ( struct chunk c,
int y,
int  y1,
int  y2,
int x,
int  x1,
int  x2 
)

Locate an empty square for y1 <= y < y2, x1 <= x < x2.

Parameters
ccurrent chunk
yfound y co-ordinate
y1y-range
y2y-range
xfound x co-ordinate
x1x-range
x2x-range
Returns
success

References cave_find_in_range(), and square_isempty().

Referenced by hard_centre_gen(), and town_gen_layout().

bool find_nearby_grid ( struct chunk c,
int y,
int  y0,
int  yd,
int x,
int  x0,
int  xd 
)

Locate a grid nearby (y0, x0) within +/- yd, xd.

Parameters
ccurrent chunk
yfound y co-ordinate
y0starting y co-ordinate
ydy-range
xfound x co-ordinate
x0starting x co-ordinate
xdx-range
Returns
success

References cave_find_in_range(), and square_in_bounds().

Referenced by build_streamer(), vault_objects(), and vault_trap_aux().

void i_to_yx ( int  i,
int  w,
int y,
int x 
)

Used to convert an array index (i) into (x, y) in a chunk of width w.

Parameters
igrid index
warea width
yco-ordinates
xco-ordinates

References w.

Referenced by build_color_point(), join_region(), lab_get_adjoin(), and labyrinth_chunk().

void new_player_spot ( struct chunk c,
struct player p 
)
static int next_to_walls ( struct chunk c,
int  y,
int  x 
)
static

Return how many cardinal directions around (x, y) contain walls.

Parameters
ccurrent chunk
yco-ordinates
xco-ordinates
Returns
the number of walls

References square_in_bounds(), and square_iswall().

Referenced by alloc_stairs().

void place_closed_door ( struct chunk c,
int  y,
int  x 
)

Place a closed door at (x, y).

Parameters
ccurrent chunk
yco-ordinates
xco-ordinates

References FEAT_CLOSED, one_in_, randint1, square_set_door_lock(), and square_set_feat().

Referenced by build_circular(), build_large(), build_room_template(), labyrinth_chunk(), place_random_door(), project_feature_handler_KILL_TRAP(), and search().

void place_gold ( struct chunk c,
int  y,
int  x,
int  level,
byte  origin 
)

Place a random amount of gold at (x, y).

Parameters
ccurrent chunk
yco-ordinates
xco-ordinates
levelgeneration depth
originitem origin

References floor_carry(), level, list_object(), make_gold(), NULL, object_delete(), object::origin, object::origin_depth, square_canputitem(), and square_in_bounds().

Referenced by alloc_object(), build_vault(), do_cmd_tunnel_aux(), project_feature_handler_KILL_WALL(), and vault_objects().

void place_object ( struct chunk c,
int  y,
int  x,
int  level,
bool  good,
bool  great,
byte  origin,
int  tval 
)

Place a random object at (x, y).

Parameters
ccurrent chunk
yco-ordinates
xco-ordinates
levelgeneration depth
goodis it a good object?
greatis it a great object?
originitem origin
tvalspecified tval, if any

References object::artifact, artifact::created, chunk::depth, floor_carry(), chunk::good_item, list_object(), make_object(), chunk::obj_rating, object_delete(), object::origin, object::origin_depth, square_canputitem(), and square_in_bounds().

Referenced by alloc_object(), build_crossed(), build_large(), build_nest(), build_pit(), build_room_template(), build_vault(), do_cmd_tunnel_aux(), project_feature_handler_KILL_WALL(), and vault_objects().

void place_random_door ( struct chunk c,
int  y,
int  x 
)

Place a random door at (x, y).

Parameters
ccurrent chunk
yco-ordinates
xco-ordinates

The door generated could be closed, open, broken, or secret.

References FEAT_BROKEN, FEAT_OPEN, place_closed_door(), randint0, and square_set_feat().

Referenced by build_room_of_chambers(), build_tunnel(), and try_door().

void place_random_stairs ( struct chunk c,
int  y,
int  x 
)

Place random stairs at (x, y).

Parameters
ccurrent chunk
yco-ordinates
xco-ordinates

References FEAT_LESS, FEAT_MORE, place_stairs(), randint0, and square_canputitem().

Referenced by build_large(), and build_room_template().

static void place_rubble ( struct chunk c,
int  y,
int  x 
)
static

Place rubble at (x, y).

Parameters
ccurrent chunk
yco-ordinates
xco-ordinates

References FEAT_PASS_RUBBLE, FEAT_RUBBLE, one_in_, and square_set_feat().

Referenced by alloc_object().

void place_secret_door ( struct chunk c,
int  y,
int  x 
)

Place a secret door at (x, y).

Parameters
ccurrent chunk
yco-ordinates
xco-ordinates

References FEAT_SECRET, and square_set_feat().

Referenced by build_large(), build_room_template(), and build_vault().

static void place_stairs ( struct chunk c,
int  y,
int  x,
int  feat 
)
static

Place stairs (of the requested type 'feat' if allowed) at (x, y).

Parameters
ccurrent chunk
yco-ordinates
xco-ordinates
featstair terrain type

All stairs from town go down. All stairs on an unfinished quest level go up.

References chunk::depth, FEAT_LESS, FEAT_MORE, is_quest(), angband_constants::max_depth, square_set_feat(), and z_info.

Referenced by alloc_stairs(), and place_random_stairs().

void rand_dir ( int rdir,
int cdir 
)

Pick a random cardinal direction.

Parameters
rdirdirection co-ordinates
cdirdirection co-ordinates

References ddx_ddd, ddy_ddd, i, and randint0.

Referenced by build_circular(), and build_tunnel().

void shuffle ( int arr,
int  n 
)

Shuffle an array using Knuth's shuffle.

Parameters
arrarray
nnumber of shuffle moves

References i, and randint0.

Referenced by labyrinth_chunk().

static bool square_isstart ( struct chunk c,
int  y,
int  x 
)
static

Determine whether the given coordinate is a valid starting location.

Parameters
ccurrent chunk
yco-ordinates
xco-ordinates
Returns
success

References square_isempty(), and square_isvault().

Referenced by new_player_spot().

void vault_monsters ( struct chunk c,
int  y1,
int  x1,
int  depth,
int  num 
)

Place 'num' sleeping monsters near (x, y).

Parameters
cthe current chunk
y1co-ordinates to place the monsters near
x1co-ordinates to place the monsters near
depthgeneration depth
numnumber of monsters to place

References i, num, pick_and_place_monster(), scatter(), and square_isempty().

Referenced by build_circular(), build_crossed(), build_large(), and build_room_template().

void vault_objects ( struct chunk c,
int  y,
int  x,
int  depth,
int  num 
)

Create up to 'num' objects near the given coordinates in a vault.

Parameters
cthe current chunk
yco-ordinates
xco-ordinates
depthgeneeration depth
numnumber of objects

References find_nearby_grid(), i, num, place_gold(), place_object(), randint0, and square_canputitem().

Referenced by build_circular(), build_large(), and build_room_template().

static void vault_trap_aux ( struct chunk c,
int  y,
int  x,
int  yd,
int  xd 
)
static

Place a trap near (x, y), with a given displacement.

Parameters
cthe current chunk
yco-ordinates to place the trap near
xco-ordinates to place the trap near
ydhow far afield to look for a place
xdhow far afield to look for a place

References find_nearby_grid(), square_add_trap(), and square_isempty().

Referenced by vault_traps().

void vault_traps ( struct chunk c,
int  y,
int  x,
int  yd,
int  xd,
int  num 
)

Place 'num' traps near (x, y), with a given displacement.

Parameters
cthe current chunk
yco-ordinates to place the trap near
xco-ordinates to place the trap near
ydhow far afield to look for a place
xdhow far afield to look for a place
numnumber of traps to place

References i, num, and vault_trap_aux().

Referenced by build_crossed(), and build_large().

int yx_to_i ( int  y,
int  x,
int  w 
)

Used to convert (x, y) into an array index (i) in a chunk of width w.

Parameters
yco-ordinates
xco-ordinates
warea width
Returns
grid index

Referenced by build_color_point(), clear_small_regions(), connect_caverns(), ignore_point(), join_region(), lab_get_adjoin(), and labyrinth_chunk().

Variable Documentation

byte get_angle_to_grid[41][41]

Accept values for y and x (considered as the endpoints of lines) between 0 and 40, and return an angle in degrees (divided by two).

-LM-

This table's input and output need some processing:

Because this table gives degrees for a whole circle, up to radius 20, its origin is at (x,y) = (20, 20). Therefore, the input code needs to find the origin grid (where the lines being compared come from), and then map it to table grid 20,20. Do not, however, actually try to compare the angle of a line that begins and ends at the origin with any other line - it is impossible mathematically, and the table will return the value "255".

The output of this table also needs to be massaged, in order to avoid the discontinuity at 0/180 degrees. This can be done by: rotate = 90 - first value this rotates the first input to the 90 degree line) tmp = ABS(second value + rotate) % 180 diff = ABS(90 - tmp) = the angular difference (divided by two) between the first and second values.

Note that grids diagonal to the origin have unique angles.

Referenced by effect_handler_THRUST_AWAY(), generate_starburst_room(), and project().