| 1 |
**************************************** |
| 2 |
The Caveman Guide to Angband Programming |
| 3 |
**************************************** |
| 4 |
|
| 5 |
Leon Marrick, December 2000 |
| 6 |
|
| 7 |
Said of Amelia Earhart, and good code too: |
| 8 |
"...for she was straight and simple." |
| 9 |
|
| 10 |
Introduction |
| 11 |
============ |
| 12 |
|
| 13 |
Hacking with the Angband code is among the best ways |
| 14 |
to learn C I know of. Ben Harrison's code is so lucid and so well commented |
| 15 |
as to make reading it a pleasure. Starting out with very little C knowledge, |
| 16 |
you can tinker with simple functions, advance to using pointers, see how |
| 17 |
data structures operate, learn interface commands in various operating |
| 18 |
systems, and eventually grapple with some fairly sophisticated algorithms |
| 19 |
and code engines. |
| 20 |
|
| 21 |
This document is designed to help you, the novice |
| 22 |
programmer, produce Angband code that really does what you want it to do. |
| 23 |
It discusses how to turn Angband into a debugger, talks about math from |
| 24 |
a caveman's point of view, and finally offers some miscellaneous suggestions. |
| 25 |
|
| 26 |
Angband is the Best Debugger |
| 27 |
============================ |
| 28 |
|
| 29 |
There are few code problems |
| 30 |
that Angband itself can't help solve. If things are getting confusing, |
| 31 |
writing some temporary code to display messages and variables is often |
| 32 |
your best move. |
| 33 |
|
| 34 |
Use messages as debugging notices |
| 35 |
--------------------------------- |
| 36 |
|
| 37 |
Let's start off with a truly |
| 38 |
basic tip: Use messages. If you don't know when, how often, or even if |
| 39 |
certain segments of your code are activating, the msg_print() and msg_format() |
| 40 |
functions are your friends. Is an if-statement always returning FALSE in |
| 41 |
practice? Is a loop running too much, and bogging things down? Where is |
| 42 |
the code freezing or crashing? Find out with messages. |
| 43 |
|
| 44 |
In some cases, you |
| 45 |
must tweak the various options that affect how messages are displayed in |
| 46 |
order to get useful output, or put messages inside loops to make sure you |
| 47 |
pay attention to them. Another way to get messages to appear is to use |
| 48 |
the prt() function with an on-screen start location. |
| 49 |
|
| 50 |
Display your variables on screen |
| 51 |
-------------------------------- |
| 52 |
|
| 53 |
Debuggers keep track of |
| 54 |
lots of variables. Typically, however, you are interested in only a few, |
| 55 |
but want to make absolutely certain you know what values they have at a |
| 56 |
very precise point. Turn to the msg_format() function, or the format() |
| 57 |
function nested inside a prt() statement. |
| 58 |
|
| 59 |
For example, I still have |
| 60 |
a copy of "generate.c" that prints out on screen the number of all kinds |
| 61 |
of rooms as they are built. How many rooms of each type get created, on |
| 62 |
average, on each level? How often does the build greater vault function |
| 63 |
fail? After each room is built, what does a map of free and used dungeon |
| 64 |
blocks look like? The prt() function can tell you, but no debugger can. |
| 65 |
|
| 66 |
Debugging code that affects the screen |
| 67 |
-------------------------------------- |
| 68 |
|
| 69 |
Much of the code you write |
| 70 |
will affect the screen in some way, if only momentarily. First thing to |
| 71 |
do if such code doesn't seem to work right is to remove any screen refreshes |
| 72 |
after it dumps its output. You may also make the output more obvious temporarily, |
| 73 |
or decide to display special characters showing the value of an internal |
| 74 |
variable at that grid. |
| 75 |
|
| 76 |
For example, many variants |
| 77 |
of Angband have arc spells that rely on the distance() function. If you |
| 78 |
freeze the area of effect of such a spell, you will see that this produces |
| 79 |
some rather ugly results if the arc travels in a direction not divisible |
| 80 |
by 45 degrees. |
| 81 |
|
| 82 |
Caveman Mathematics |
| 83 |
=================== |
| 84 |
|
| 85 |
Anyone who wants to code had better know math, but he |
| 86 |
doesn't necessarily have to know much math. Since a complex equation confuses |
| 87 |
me as fast as the next man, I'll share some of the simple, "caveman" math |
| 88 |
that does most of the heavy lifting in my code. |
| 89 |
|
| 90 |
Multiplication versus addition: The equation a * x + b |
| 91 |
------------------------------------------------------ |
| 92 |
|
| 93 |
There are a lot of times |
| 94 |
in Angband where a variable (say, melee skill) has an initial value and |
| 95 |
is also affected by a variable (like player level). Key to balancing the |
| 96 |
game is thinking carefully about how much weight to give the constant part |
| 97 |
(usually a function of addition) and the variable part (often determined |
| 98 |
by multiplication). How many monsters should appear on a level? How much |
| 99 |
of a Ring of Speed's value should depend on its being a Ring of Speed and |
| 100 |
how much on the actual speed bonus? How much of a spell's damage should |
| 101 |
be guaranteed, and how much level-dependent? The Angband programmer usually |
| 102 |
answers these questions by weighing addition versus multiplication until |
| 103 |
the results work well for all values of the variable. |
| 104 |
|
| 105 |
Division |
| 106 |
-------- |
| 107 |
|
| 108 |
Division by zero is the |
| 109 |
programmer's bugaboo. Variables fed into the equation "4 / x" should be |
| 110 |
checked just beforehand, because this is where failure to control boundaries |
| 111 |
or syntax errors can have immediate and fatal results. |
| 112 |
|
| 113 |
In Angband, floating-point |
| 114 |
math is a no-no. Many of the machines the game runs on crash nastily when |
| 115 |
fed floating-point. Trouble is the division operator in fixed-point math |
| 116 |
turns the numerator 3 divided by the denominator 2 into 1. As long as we |
| 117 |
are careful about the limits of bytes and integers, we can inflate the |
| 118 |
numerator before dividing it until we achieve the level of accuracy we |
| 119 |
desire. If we want accuracy out to three places, then 100 * 3 / 2 will |
| 120 |
yield 150. |
| 121 |
|
| 122 |
The mod operator (%) |
| 123 |
-------------------- |
| 124 |
|
| 125 |
I've never found anything |
| 126 |
floating-point math could do in Angband that fixed-point couldn't with |
| 127 |
a little ingenuity. Along with temporary inflation of values, tables, and |
| 128 |
plain old-fashioned working around the problem, the mod operator is the |
| 129 |
fixed-point mathematician's favorite tool. Remember that 5 / 2 = 2 in fixed-point |
| 130 |
math, despite the remainder of 1. 5 % 2 yields that remainder: 1. All data |
| 131 |
lost in division can be recovered. |
| 132 |
|
| 133 |
There are other uses of |
| 134 |
"%". If you code a loop, and want a portion of it to fire only one time |
| 135 |
in four, you can use the line "if (i % 4 == 0)". If you want a value to |
| 136 |
wrap around from 31 to 32 to 0, to avoid nasty results at 33, you can code |
| 137 |
"if (y > 32) y = y % 32". This is very helpful to ensure legal bit operator |
| 138 |
or table access. |
| 139 |
|
| 140 |
Note: The mod operator causes no speed issues in most situations, |
| 141 |
but is not fast enough for very low-level functions. |
| 142 |
|
| 143 |
Calling the Random Number Generator (RNG) by name |
| 144 |
------------------------------------------------- |
| 145 |
|
| 146 |
Key to programming in this |
| 147 |
game is the evocation of Angband's infamous RNG. You will be calling its |
| 148 |
name frequently, and need to know the basic binding spells. |
| 149 |
|
| 150 |
The function rand_int() yields a value between 0 and x-1. The function |
| 151 |
randint() yields a value between 1 and x. Be ready to use either, with a |
| 152 |
slight preference for rand_int() (randint() is actually rand_int() + 1), |
| 153 |
depending on the comparison you have to make. |
| 154 |
|
| 155 |
For example, if we want x |
| 156 |
to have an x percentage chance of success, then "(x > rand_int(100))" is |
| 157 |
correct, but "(x > rand_int(100))" or "(x > randint(100))" are not. With |
| 158 |
any formula of this type, increases in x past the number being randomized |
| 159 |
yield exactly the same result. 1000 is greater than 100, but so is 101. |
| 160 |
|
| 161 |
You will also often see an |
| 162 |
equation similar to "if (10 < randint(x))", which means that an x of |
| 163 |
less than 10 is certain to yield FALSE, and an x of 10 or larger will yield |
| 164 |
FALSE 10 times out of x. This function is useful for discriminating against |
| 165 |
low 'x's, like the check for a hit in melee does against poor combat skill. |
| 166 |
|
| 167 |
Unlike in the previous equation, |
| 168 |
the result is never certain to be TRUE, no matter how high x gets. On the |
| 169 |
other hand, if x can increase to a large multiple of 10, then we are very |
| 170 |
likely to get a result of TRUE. This happens in the Angband trap code with |
| 171 |
a high disarming skill; traps become almost harmless. If you want the function |
| 172 |
to not approach certainty, but (say) approach a 50% chance of getting a |
| 173 |
desired result, then use an additional test: "if (rand_int(2) == 0)" in |
| 174 |
this case. |
| 175 |
|
| 176 |
The damage of many spells |
| 177 |
is determined by lines similar to "20 + randint(30)". This means that damage |
| 178 |
done will be between 21 and 50, with each value having an equal chance |
| 179 |
of happening. If you really meant damage to vary between 20 and 50, type |
| 180 |
"20 + rand_int(31)" instead. There are many cases where this change might |
| 181 |
profitably be made. |
| 182 |
|
| 183 |
There are lots of other ways |
| 184 |
to use rand_int(). Sometimes if complex or multiple calls to the RNG are |
| 185 |
getting confusing, run the boundary conditions through the equations and |
| 186 |
pay special attention to how ">", and "<", differ from ">=" and "<=". |
| 187 |
|
| 188 |
Normal distributions the easy way with damroll() |
| 189 |
------------------------------------------------ |
| 190 |
|
| 191 |
There are many situations |
| 192 |
where you want controlled variation, biased towards values away from the |
| 193 |
extremes. Normal distributions are ubiquitous in the real world; they're |
| 194 |
good things to have in games too. The easiest way to get a close-to-normal |
| 195 |
distribution is to use damroll(). |
| 196 |
|
| 197 |
This function takes two |
| 198 |
values: number of dice (d), and dice sides (s). The result ranges from |
| 199 |
d * 1 to d * s. A dagger (1d4) does between one and four points of damage. |
| 200 |
The average damage is (d + d * s) / 2 - in the case of a dagger, this would |
| 201 |
be 2.5. For mental calculations, some prefer to use the function: average |
| 202 |
= d * (s+1) / 2. |
| 203 |
|
| 204 |
The more dice there are, |
| 205 |
and the fewer sides each die has, the more closely the results, over an |
| 206 |
infinite number of runs, will cluster around the average. The result of |
| 207 |
100d2 will very likely be quite close to 150. At the other extreme, using |
| 208 |
one die is the same as using randint(): all legal values have the same |
| 209 |
chance of occurring. |
| 210 |
|
| 211 |
Warning: Using damroll is |
| 212 |
like using rand_int() once for every die you roll. This is normally not |
| 213 |
a problem, but be hesitant to use this function where speed is crucial. |
| 214 |
|
| 215 |
General Suggestions |
| 216 |
=================== |
| 217 |
|
| 218 |
Using Angband to help you debug |
| 219 |
and getting comfortable with simple math helps a lot. Below are some ideas |
| 220 |
for getting even better results. |
| 221 |
|
| 222 |
Control boundary cases |
| 223 |
---------------------- |
| 224 |
|
| 225 |
Bad code frays around the |
| 226 |
edges, and breaks starting at extreme values. A good way to look for trouble |
| 227 |
in your code is to run the following four numbers through your equations: |
| 228 |
|
| 229 |
#. absolute minimum: the smallest number the code potentially allows to |
| 230 |
reach your equations |
| 231 |
#. probable low end: the lowest number that a user |
| 232 |
may reasonably be expected to feed into your equations in a real game |
| 233 |
#. probable high end: the highest number that a number may reasonably be |
| 234 |
expected to feed into your equations in a real game |
| 235 |
#. absolute maximum: the largest number the code potentially allows to reach |
| 236 |
your equations |
| 237 |
|
| 238 |
By checking numbers 1 and 4, you help stop your code from |
| 239 |
crashing. By checking 2 and 3, you maintain game balance. |
| 240 |
|
| 241 |
Use a spreadsheet |
| 242 |
----------------- |
| 243 |
|
| 244 |
Most valuable bit of advice |
| 245 |
in this document: use a spreadsheet to work with non-trivial equations. |
| 246 |
Doing so allows you to work interactively with math functions, looking |
| 247 |
at all values for all variables simultaneously, in a way that you cannot |
| 248 |
after you transfer them to code. Here is how you figure out the values |
| 249 |
you want, and than play with equations until what you want is what you |
| 250 |
get. All of a sudden, math starts doing what you want it to do... |
| 251 |
|
| 252 |
Put more information in your code |
| 253 |
--------------------------------- |
| 254 |
|
| 255 |
Code development is iterative |
| 256 |
- it doesn't happen all at once. You can make life really hard on yourself |
| 257 |
by adding no comments and scrunching your code into as small a space as |
| 258 |
possible, or really easy by learning to write commentary early and enthusiastically. |
| 259 |
|
| 260 |
If you're like me, even |
| 261 |
your best friends could not call you a coding genius. If you know how to |
| 262 |
write better than you know how to program, consider writing your way to |
| 263 |
good code. |
| 264 |
|
| 265 |
Learn when and where to worry about execution speed |
| 266 |
--------------------------------------------------- |
| 267 |
|
| 268 |
Some parts of Angband require |
| 269 |
more attention to speed than others. The less thinking the player has to |
| 270 |
do to activate the code in question, and the less distracted he is while |
| 271 |
it runs, the more he will pay attention to speed of execution. Also, the |
| 272 |
more often code is called, whether by nesting of functions or in loops, |
| 273 |
the more oportunity it has to eat up time. |
| 274 |
|
| 275 |
The monster AI is a perfect |
| 276 |
example. Monsters must very quickly decide whether to cast a spell, run, |
| 277 |
or attack, because: 1) the code to make them plot and plan runs even when |
| 278 |
the player is not spending time thinking, and 2) it is called very frequently |
| 279 |
near major concentrations of monsters. |
| 280 |
|
| 281 |
So how do you make the code fast where it matters? Standard Angband code |
| 282 |
offers many a practical example of optimizing for speed. Some are so |
| 283 |
sophisticated that they are almost indecipherable by the novice - others, |
| 284 |
anyone can copy: |
| 285 |
|
| 286 |
* Re-design loops and scans so that they have to cycle less to get the same |
| 287 |
result. See if you can make a search more intelligent, a test more likely |
| 288 |
to succeed, or a scan more focused. Be wary of functions or statements |
| 289 |
being called, failing, and having to be called again. |
| 290 |
* Set up if-statements that winnow out those cases that do not need to activate |
| 291 |
the code. In the code that determines whether a monster will run, for example, |
| 292 |
most of the possible cases will obviously flee, or obviously stand and |
| 293 |
fight. |
| 294 |
* Put tests that do the most work for the least effort first: A simple test |
| 295 |
goes before a complex test. A test that removes 40% of the candidates goes |
| 296 |
before an equally simple test that removes 20%. |
| 297 |
* If you can't avoid calling rand_int altogether, at least get the maximum |
| 298 |
possible use out of the calls you must make. Keep rand_int out of loops, |
| 299 |
use a single call in independant tests (be careful), or divide up a random |
| 300 |
number into chunks, digits, or bits, and use the divisions. For example, |
| 301 |
each of the last four digits of rand_int(10000) may be used as a rand_int(10). |
| 302 |
* Use permanent and temporary tables and global variables. Randomly search |
| 303 |
the entire monster list often enough, and you'll spend more time than you |
| 304 |
would if you filled a table with legal monsters, and used it when needed. |
| 305 |
There are many cases where the monster and object generation codes re-build |
| 306 |
the generation tables for precisely this reason. |
| 307 |
* Sometimes, you need superfast math. Functions to use in these cases include |
| 308 |
the shift expressions "a << x" (multiply a by 2 * x) and "b >> y" |
| 309 |
(divide b by 2 * y), and other bit operators, but do not include "/", or |
| 310 |
"%". |
| 311 |
|
| 312 |
And a last word |
| 313 |
--------------- |
| 314 |
|
| 315 |
Angband still plays off floppy. A lot of coders have worked awfully hard to |
| 316 |
make a game that is a shining example of just have much fun can fit on 1.4 meg |
| 317 |
of space. Let this tradition be your tradition. Leave it to others to write |
| 318 |
elephantine code that squeaks; let your code be mousy-small - and roar! |
| 319 |
|
| 320 |
| Respectfully yours, |
| 321 |
| Leon Marrick |
| 322 |
|
| 323 |
Dedicated to: |
| 324 |
|
| 325 |
* The coders who taught me everything I know, |
| 326 |
* The experts who bailed me out of trouble, and |
| 327 |
* The players who judged my errors gently. |
| 328 |
|
| 329 |
Copyright 2000 by Leon Marrick |
| 330 |
|
| 331 |
.. meta:: |
| 332 |
:author: Leon Marrick |
| 333 |
:description:Hints for Angband developers |
| 334 |
:keywords: Angband, roguelike, development, programmer, developer, hint, tips, tricks, debugging |