Angband
gen-util.c File Reference

Dungeon generation utilities. More...

`#include "angband.h"`
`#include "cave.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 "parser.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.
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.
void shuffle (int *arr, int n)
Shuffle an array using Knuth's shuffle.
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.
bool cave_find (struct chunk *c, int *y, int *x, square_predicate pred)
Locate a square in the dungeon which satisfies the given predicate.
static 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.
bool find_empty (struct chunk *c, int *y, int *x)
Locate an empty square for 0 <= y < ymax, 0 <= x < xmax.
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.
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.
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.
void rand_dir (int *rdir, int *cdir)
Pick a random cardinal direction.
static bool square_isstart (struct chunk *c, int y, int x)
Determine whether the given coordinate is a valid starting location.
void new_player_spot (struct chunk *c, struct player *p)
Place the player at a random starting location.
static int next_to_walls (struct chunk *c, int y, int x)
Return how many cardinal directions around (x, y) contain walls.
static void place_rubble (struct chunk *c, int y, int x)
Place rubble at (x, y).
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).
void place_random_stairs (struct chunk *c, int y, int x)
Place random stairs at (x, y).
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).
void place_gold (struct chunk *c, int y, int x, int level, byte origin)
Place a random amount of gold at (x, y).
void place_secret_door (struct chunk *c, int y, int x)
Place a secret door at (x, y).
void place_closed_door (struct chunk *c, int y, int x)
Place a closed door at (x, y).
void place_random_door (struct chunk *c, int y, int x)
Place a random door at (x, y).
void alloc_stairs (struct chunk *c, int feat, int num, int walls)
Place some staircases near walls.
void alloc_objects (struct chunk *c, int set, int typ, int num, int depth, byte origin)
Allocates 'num' random entities in the dungeon.
bool alloc_object (struct chunk *c, int set, int typ, int depth, byte origin)
Allocates a single random object in the dungeon.
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.
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.
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.
void vault_monsters (struct chunk *c, int y1, int x1, int depth, int num)
Place 'num' sleeping monsters near (x, y).

## 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).

## 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:

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
 c current chunk y found y co-ordinate y1 y2 y-range x found x co-ordinate x1 x2 x-range pred square_predicate specifying what we're looking for
Returns
success

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

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
 c the current chunk set where the entity is placed (corridor, room or either) typ what is placed (rubble, trap, gold, item) depth generation depth origin item origin (if appropriate)

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

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
 c the current chunk set where the entity is placed (corridor, room or either) typ what is placed (rubble, trap, gold, item) num number to place depth generation depth origin item origin (if appropriate)

References alloc_object(), num, and ok.

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

Place some staircases near walls.

Parameters
 c the current chunk feat the stair terrain type num number of staircases to place walls number of walls to surround the stairs (negotiable)

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

 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
 c current chunk y found y co-ordinate x found x co-ordinate pred square_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().

 static bool cave_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
 c current chunk y found y co-ordinate y1 y2 y-range x found x co-ordinate x1 x2 x-range pred square_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
 rdir found row change (up or down) cdir found column change (left or right) y1 x1 starting co-ordinates y2 x2 target 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
 c current chunk y found y co-ordinate x found 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
 c current chunk y found y co-ordinate y1 y2 y-range x found x co-ordinate x1 x2 x-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
 c current chunk y found y co-ordinate y0 starting y co-ordinate yd y-range x found x co-ordinate x0 starting x co-ordinate xd x-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
 i grid index w area width y x co-ordinates

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

 void new_player_spot ( struct chunk * c, struct player * p )

Place the player at a random starting location.

Parameters
 c current chunk p the player
 static int next_to_walls ( struct chunk * c, int y, int x )
static

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

Parameters
 c current chunk y x co-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
 c current chunk y x co-ordinates

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

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

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

Parameters
 c current chunk y x co-ordinates level generation depth origin item origin
 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
 c current chunk y x co-ordinates level generation depth good is it a good object? great is it a great object? origin item origin tval specified tval, if any
 void place_random_door ( struct chunk * c, int y, int x )

Place a random door at (x, y).

Parameters
 c current chunk y x co-ordinates

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

References FEAT_BROKEN, FEAT_OPEN, FEAT_SECRET, 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
 c current chunk y x co-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
 c current chunk y x co-ordinates

References FEAT_RUBBLE, 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
 c current chunk y x co-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
 c current chunk y x co-ordinates feat stair terrain type

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

Referenced by alloc_stairs(), and place_random_stairs().

 void rand_dir ( int * rdir, int * cdir )

Pick a random cardinal direction.

Parameters
 rdir cdir direction 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
 arr array n number 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
 c current chunk y x co-ordinates
Returns
success

References FALSE, square_isempty(), square_isvault(), and TRUE.

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
 c the current chunk y1 x1 co-ordinates to place the monsters near depth generation depth num number of monsters to place

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

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
 c the current chunk y x co-ordinates depth geneeration depth num number of objects

References FALSE, 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
 c the current chunk y x co-ordinates to place the trap near yd xd how far afield to look for a place

References chunk::depth, find_nearby_grid(), place_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
 c the current chunk y x co-ordinates to place the trap near yd xd how far afield to look for a place num number of traps to place

References i, num, and vault_trap_aux().

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

 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
 y x co-ordinates w area width
Returns
grid index

## 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().