| 1 |
============ |
| 2 |
How It Works |
| 3 |
============ |
| 4 |
|
| 5 |
This document describes how Angband actually *works* at a high level. Individual |
| 6 |
sections referenced from the TOC are marked with anchors in square brackets to |
| 7 |
make grepping for them easier. |
| 8 |
|
| 9 |
.. contents:: Contents |
| 10 |
:local: |
| 11 |
|
| 12 |
|
| 13 |
The Game |
| 14 |
======== |
| 15 |
|
| 16 |
As you probably know if you're reading this, Angband is a roguelike game set in |
| 17 |
a high-fantasy universe. The game world is made up of levels, numbered from zero |
| 18 |
("the town") to some maximum depth. Levels are increasingly dangerous the deeper |
| 19 |
they are into the dungeon. Levels are filled with monsters, traps, and objects. |
| 20 |
Monsters move and act on their own, traps react to creatures entering their |
| 21 |
square, and objects are inert unless used by a creature. The objective of the |
| 22 |
game is to find Morgoth at depth 100 and kill him. |
| 23 |
|
| 24 |
Data Structures |
| 25 |
=============== |
| 26 |
|
| 27 |
There are three important top-level data structures in Angband: the 'chunk', the |
| 28 |
player, and the static data tables. |
| 29 |
|
| 30 |
The Chunk |
| 31 |
--------- |
| 32 |
A chunk represents an area of dungeon, and contains everything inside it; this |
| 33 |
includes any monsters, objects, or traps inside the bounds of that chunk. A |
| 34 |
chunk also keeps a map of the terrain in its area. For unpleasant historical |
| 35 |
reasons, all monsters/objects/traps in a chunk are stored in arrays and usually |
| 36 |
referred to by index; each square of a chunk knows the indexes (if any) of |
| 37 |
monsters/objects/traps contained in it. A chunk also stores AI pathfinding data |
| 38 |
for its contained area. All data in the 'current' chunk is lost when leaving the |
| 39 |
level. |
| 40 |
|
| 41 |
The Player |
| 42 |
---------- |
| 43 |
|
| 44 |
The player is a global object containing information about, well, the player. |
| 45 |
All the information in the player is level-independent. This structure contains |
| 46 |
stats, any current effects, hunger status, sex/race/class, the player's |
| 47 |
inventory, and a grab-bag of other information. Although there is a global |
| 48 |
player object, many functions instead take a player object explicitly to make |
| 49 |
them easier to test. |
| 50 |
|
| 51 |
The Static Data |
| 52 |
--------------- |
| 53 |
|
| 54 |
Angband's static data - player and monster races, object types, artifacts, et |
| 55 |
cetera - is loaded from the `gamedata Files`_. Once loaded, this |
| 56 |
data is stored in global tables, sometimes referred to as the 'info arrays'. |
| 57 |
These arrays are generally declared in the header files of the code that uses |
| 58 |
them most, but they are mostly initialized by the edit-file code. The sizes of |
| 59 |
these arrays are stored in a 'maxima' structure, called z_info. |
| 60 |
|
| 61 |
The Z Layer |
| 62 |
=========== |
| 63 |
|
| 64 |
The lowest-level code in Angband is the "Z" layer, which provides |
| 65 |
platform-independent abstractions and generic data structures. Currently, the Z |
| 66 |
layer provides: |
| 67 |
|
| 68 |
================= ======================================== |
| 69 |
``z-bitflag`` Densely-packed bit flag arrays |
| 70 |
``z-color`` Colors |
| 71 |
``z-debug`` Debugging annotations |
| 72 |
``z-dice`` Dice expressions |
| 73 |
``z-expression`` Mathematical expressions |
| 74 |
``z-file`` File I/O |
| 75 |
``z-form`` String formatting |
| 76 |
``z-msg`` Rich messages |
| 77 |
``z-msg`` Message buffering -lis |
| 78 |
``z-quark`` String interning |
| 79 |
``z-queue`` Queues |
| 80 |
``z-rand`` Randomness |
| 81 |
``z-textblock`` Wrapped text |
| 82 |
``z-type`` Basic types |
| 83 |
``z-util`` Random utility macros |
| 84 |
``z-virt`` malloc() wrappers |
| 85 |
================= ======================================== |
| 86 |
|
| 87 |
Code in the Z layer may not depend on files outside the Z layer. |
| 88 |
|
| 89 |
Key Abstractions |
| 90 |
================ |
| 91 |
|
| 92 |
Certain game-specific abstractions are important and widely used in Angband to |
| 93 |
glue the UI code to the game engine. These are the command queue, which sends |
| 94 |
player commands to the game engine, and events, which indicate to the UI that |
| 95 |
the state of the game changed. |
| 96 |
|
| 97 |
The command queue |
| 98 |
----------------- |
| 99 |
|
| 100 |
TBD |
| 101 |
|
| 102 |
Events |
| 103 |
------ |
| 104 |
|
| 105 |
TBD |
| 106 |
|
| 107 |
Files |
| 108 |
===== |
| 109 |
|
| 110 |
Angband uses three types of files for storing data: gamedata files, which contain |
| 111 |
the game's static data, pref files, which contain UI settings, |
| 112 |
and save files, which contain the state of a game in progress. |
| 113 |
|
| 114 |
Gamedata Files |
| 115 |
-------------- |
| 116 |
|
| 117 |
Gamedata files use a line-oriented format where fields are separated by colons. The |
| 118 |
parser for this format is in ``parser.h``. These files are mostly loaded at |
| 119 |
initialization time (see `init.c - init_angband`_) and used to fill in the static data |
| 120 |
arrays (see `The Static Data`_). |
| 121 |
|
| 122 |
Pref Files |
| 123 |
---------- |
| 124 |
TBD |
| 125 |
|
| 126 |
Savefiles |
| 127 |
---------- |
| 128 |
|
| 129 |
Currently, a savefile is a series of concatenated blocks. Each block has a name |
| 130 |
describing what type it is and a version tag. The version tag allows for old |
| 131 |
savefiles to be loaded, although the load/save code will only write new |
| 132 |
savefiles. Numbers in savefiles are stored in little-endian byte order and |
| 133 |
strings are stored null-terminated. |
| 134 |
|
| 135 |
Control Flow |
| 136 |
============ |
| 137 |
|
| 138 |
The flow of control through Angband is complicated and can be very non-obvious |
| 139 |
due to overuse of global variables as special-behavior hooks. That said, this |
| 140 |
section gives a high-level overview of the control flow of a game session. |
| 141 |
|
| 142 |
Startup |
| 143 |
------- |
| 144 |
|
| 145 |
Execution begins in main.c, which runs frontend-independent initialization code, |
| 146 |
then continues in the appropriate ``main-*.c`` file for the current frontend. After |
| 147 |
the game engine is initialized, the player is loaded (or generated) and gameplay |
| 148 |
begins. |
| 149 |
|
| 150 |
``main.c`` and ``main-*.c`` |
| 151 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 152 |
main.c's ``main()`` is the entry point for Angband execution except on Windows, |
| 153 |
where main-win.c's ``WinMain()`` is used, on Nintendo DS, where a special |
| 154 |
``main()`` in main-nds.c is used, and on OS X where main-cocoa.m's ``main()`` |
| 155 |
is used. The ``main()`` function is responsible for dropping permissions if |
| 156 |
Angband is running setuid, parsing command line arguments, then finding a |
| 157 |
frontend to use and initializing it. Once ``main()`` finds a frontend, it sets |
| 158 |
up signal handlers, sets up the display, and calls `init.c - init_angband`_, |
| 159 |
which loads all the `gamedata files`_ and initializes other static data used |
| 160 |
by the game. |
| 161 |
|
| 162 |
init.c - ``init_angband`` |
| 163 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 164 |
The init_angband() function in init.c is responsible for loading and setting up |
| 165 |
static data needed by the game engine. Inside init.c, there is a list of 'init |
| 166 |
modules' that have startup-time static data they need to initialize, these are |
| 167 |
registered in an array of module pointers in init.c, and init_angband() calls |
| 168 |
their initialization hooks before doing any other work. Finally it sets up the |
| 169 |
RNG. |
| 170 |
|
| 171 |
ui-init.c - ``textui_init`` |
| 172 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 173 |
The textui_init() function then loads the top-level pref file (see |
| 174 |
`pref files`_), initializes the command queue (see `the command queue`_), |
| 175 |
and configures subwindows. |
| 176 |
|
| 177 |
ui-prefs.c - ``process_pref_file`` |
| 178 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 179 |
The process_pref_file() function in ui-prefs.c is responsible for loading user |
| 180 |
pref files, which can live at multiple paths. User preference files override |
| 181 |
default preference files. See `pref files`_ for more details. |
| 182 |
|
| 183 |
ui-game.c - ``play_game`` |
| 184 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 185 |
This function calls start_game() to load a saved game if there is a valid save |
| 186 |
(see `savefiles`_) or birth a new character if not. It then asks for a command |
| 187 |
from the player, and then runs the game main loop (see |
| 188 |
`game-world.c - the game main loop`_), over and over until the character dies |
| 189 |
or the player quits |
| 190 |
|
| 191 |
Gameplay |
| 192 |
-------- |
| 193 |
Once the simulation is set up, the game main loop in `ui-game.c - play_game`_ |
| 194 |
is responsible for stepping the simulation. |
| 195 |
|
| 196 |
game-world.c - the game main loop |
| 197 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 198 |
The main loop of the game, run_game_loop() is repeatedly called inside |
| 199 |
play_game(). Each iteration of the main loop is one "turn" in Angband parlance, |
| 200 |
or one step of the simulator. During each turn: |
| 201 |
|
| 202 |
* All monsters with more energy than the player act |
| 203 |
* The player acts |
| 204 |
* All other monsters act |
| 205 |
* The UI updates |
| 206 |
* The world acts |
| 207 |
* End-of-turn housekeeping is done |
| 208 |
|
| 209 |
mon-move.c - process_monsters() |
| 210 |
********************************* |
| 211 |
|
| 212 |
In Angband, creatures act in order of "energy", which roughly determines how |
| 213 |
many actions they can take per step through the simulation. The |
| 214 |
process_monsters() function in mon-move.c is responsible for walking through |
| 215 |
the list of all monsters in the current chunk (see `the chunk`_) and having each |
| 216 |
monster act by calling process_monster(), which implements the highest level AI |
| 217 |
for monsters. |
| 218 |
|
| 219 |
game-world.c - process_player() |
| 220 |
******************************* |
| 221 |
|
| 222 |
The process_player() function allows the player to act repeatedly until they do |
| 223 |
something that uses energy. Commands like looking around or inscribing items do |
| 224 |
not use energy; movement, attacking, casting spells, using items, and so on do. |
| 225 |
The rule of thumb is that a command that does not alter game engine state does |
| 226 |
not use energy, because it does not represent an action the character in the |
| 227 |
simulation is doing. The guts of the process_player() function are actually |
| 228 |
handled by process_command() in cmd-core.c, which looks up commands in the |
| 229 |
game_cmds table in that file. |
| 230 |
|
| 231 |
Keeping the UI up to date |
| 232 |
************************* |
| 233 |
|
| 234 |
Four related horribly-named functions in player-calcs.h are responsible for |
| 235 |
keeping the UI in sync with the simulated character's state: |
| 236 |
|
| 237 |
================== =============================================================== |
| 238 |
``notice_stuff()`` which deals with pack combining and dropping ignored items; |
| 239 |
``update_stuff()`` which recalculates derived bonuses, AI data, vision, seen |
| 240 |
monsters, and other things based on the flags in |
| 241 |
``player->upkeep->update``; |
| 242 |
``redraw_stuff()`` which signals the UI to redraw changed sections of the |
| 243 |
game state; |
| 244 |
``handle_stuff()`` which calls update_stuff() and redraw_stuff() if needed. |
| 245 |
================== =============================================================== |
| 246 |
|
| 247 |
These functions are called during every game loop, after the player and all |
| 248 |
monsters have acted. |
| 249 |
|
| 250 |
game-world.c - process_world() |
| 251 |
****************************** |
| 252 |
|
| 253 |
The process_world() function only runs every 10 turns. It is responsible for the |
| 254 |
day/night transition in town, restocking the stores, generating new creatures |
| 255 |
over time, dealing poison/cut damage, applying hunger, regeneration, ticking |
| 256 |
down timed effects, consuming light fuel, and applying a litany of spell effects |
| 257 |
that happen 'at random' from the player's point of view. |
| 258 |
|
| 259 |
Dungeon Generation |
| 260 |
------------------ |
| 261 |
|
| 262 |
prepare_next_level() in generate.c controls the process of generating or loading |
| 263 |
a level. To signal that run_game_loop() in game-world.c should call |
| 264 |
prepare_next_level(), game logic calls dungeon_change_level() in player-util.c |
| 265 |
to set the necessary data in the player structure. When a level change happens |
| 266 |
by traversing a staircase, some other data in the player structure is set to |
| 267 |
indicate what should be done to connect stairs. That doesn't happen in |
| 268 |
dungeon_change_level() and is instead set directly, currently in do_cmd_go_up() |
| 269 |
and do_cmd_go_down() in cmd-cave.c. |
| 270 |
|
| 271 |
With the default for non-persistent levels, loading only happens when |
| 272 |
returning to the town or when returning from a single combat arena. The code |
| 273 |
and global data for handling stored levels is in gen-chunk.c. |
| 274 |
|
| 275 |
When a new level is needed, prepare_next_level() calls cave_generate(), also in |
| 276 |
generate.c. That initializes a global bit of state, a dun_data structure called |
| 277 |
dun declared in generate.h, for passing a lot of the details needed when |
| 278 |
generating a level. It then selects a level profile via choose_profile() in |
| 279 |
generate.c. The level profile controls the layout of the level. The available |
| 280 |
level profiles are those listed in list-dun-profiles.h and several aspects of |
| 281 |
each profile are configured at runtime from the contents of |
| 282 |
lib/gamedata/dungeon_profile.txt. With a profile selected, cave_generate() |
| 283 |
uses the profile's builder function pointer to attempt to layout the new level. |
| 284 |
Those function pointers are initialized when list-dun-profiles.h is included |
| 285 |
in generate.c. The level layout functions all have names with the name of |
| 286 |
the profile followed by *_gen*, classic_gen() for classic levels as an |
| 287 |
example. Those functions are defined in gen-cave.c. |
| 288 |
|
| 289 |
Three of the level layout functions, classic_gen(), modified_gen(), and |
| 290 |
moria_gen() follow the same basic procedure. They divide the level into a |
| 291 |
grid of rectangular blocks where, in general, each block can only contain |
| 292 |
one room though a room could occupy many blocks. They then try to randomly |
| 293 |
place rooms in those blocks until some criteria is met. Room selection is |
| 294 |
configurable from lib/gamedata/dungeon_profile.txt and uses the predefined |
| 295 |
room types listed in list-rooms.h. When building a room, those level layout |
| 296 |
functions use the convenience function, room_build() from gen-room.c. That, in |
| 297 |
turn, calls the appropriate function to build the type of room chosen. The |
| 298 |
names of the room building functions have *build_* followed by the name of the |
| 299 |
room type, build_simple() for instance. Those functions are defined in |
| 300 |
gen-room.c. Once the rooms are built, there's an initial pass to connect them |
| 301 |
with corridors. That happens in gen-cave.c's do_traditional_tunneling(). |
| 302 |
A second pass, to try and ensure connectedness though vault areas can disrupt |
| 303 |
that, is then done with ensure_connectedness(). At that point, most other |
| 304 |
features (mineral veins, staircases, objects, and monsters) are added. Some |
| 305 |
features will have already been added through some of the types of rooms. |
| 306 |
|
| 307 |
The other layout functions are more of a grab bag. They are all in gen-cave.c. |
| 308 |
Many of them have portions that are caverns or labyrinths. Those are generated |
| 309 |
using cavern_chunk() or labyrinth_chunk(), respectively, in gen-cave.c. |
| 310 |
|
| 311 |
Monster AI |
| 312 |
---------- |
| 313 |
|
| 314 |
TBD |
| 315 |
|
| 316 |
|
| 317 |
Stats |
| 318 |
----- |
| 319 |
|
| 320 |
The stats generation code aims to make it easy to analyze object generation, |
| 321 |
monster generation, and other Angband processes suitable for Monte Carlo |
| 322 |
simulation. The stats pseudo-visual module will repeatedly create a character, |
| 323 |
walk her down the dungeon, and, for each dungeon level, kill the monsters |
| 324 |
there and dump information about the monsters and objects. The end result |
| 325 |
is a SQLite3 database, written to the stats subdirectory of Angband's user |
| 326 |
directory. A similar procedure is used by the ``S`` debugging command. It |
| 327 |
will generate a text file summarizing the monsters and objects generated. |
| 328 |
That output may be more accessible, since one doesn't have to deal with the |
| 329 |
structure of the database, but the database stores finer-grained classifications |
| 330 |
of the objects and monsters. |