Improve comments, eliminate magic numbers.
[open-adventure.git] / init.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdbool.h>
5
6 #include "advent.h"
7 #include "database.h"
8
9 /*
10  * Initialisation
11  */
12
13 void initialise(void)
14 {
15     if (oldstyle)
16         printf("Initialising...\n");
17
18     for (int i = 1; i <= NOBJECTS; i++) {
19         game.place[i] = LOC_NOWHERE;
20         game.prop[i] = 0;
21         game.link[i + NOBJECTS] = game.link[i] = 0;
22     }
23
24     for (int i = 1; i <= NLOCATIONS; i++) {
25         game.abbrev[i] = 0;
26         if (!(locations[i].description.big == 0 || KEY[i] == 0)) {
27             int k = KEY[i];
28             if (MOD(labs(TRAVEL[k]), 1000) == 1)
29                 conditions[i] |= (1 << COND_FORCED);
30         }
31         game.atloc[i] = 0;
32     }
33
34     /*  Set up the game.atloc and game.link arrays.
35      *  We'll use the DROP subroutine, which prefaces new objects on the
36      *  lists.  Since we want things in the other order, we'll run the
37      *  loop backwards.  If the object is in two locs, we drop it twice.
38      *  This also sets up "game.place" and "fixed" as copies of "PLAC" and
39      *  "FIXD".  Also, since two-placed objects are typically best
40      *  described last, we'll drop them first. */
41     for (int i = 1; i <= NOBJECTS; i++) {
42         int k = NOBJECTS + 1 - i;
43         if (FIXD[k] > 0) {
44             DROP(k + NOBJECTS, FIXD[k]);
45             DROP(k, PLAC[k]);
46         }
47     }
48
49     for (int i = 1; i <= NOBJECTS; i++) {
50         int k = NOBJECTS + 1 - i;
51         game.fixed[k] = FIXD[k];
52         if (PLAC[k] != 0 && FIXD[k] <= 0)
53             DROP(k, PLAC[k]);
54     }
55
56     /*  Treasure props are initially -1, and are set to 0 the first time
57      *  they are described.  game.tally keeps track of how many are
58      *  not yet found, so we know when to close the cave. */
59     game.tally = 0;
60     for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
61         if (object_descriptions[treasure].is_treasure) {
62             if (object_descriptions[treasure].inventory != 0)
63                 game.prop[treasure] = -1;
64             game.tally = game.tally - game.prop[treasure];
65         }
66     }
67
68     /*  Clear the hint stuff.  game.hintlc[i] is how long he's been at LOC
69      *  with cond bit i.  game.hinted[i] is true iff hint i has been
70      *  used. */
71     for (int i = 0; i < NHINTS; i++) {
72         game.hinted[i] = false;
73         game.hintlc[i] = 0;
74     }
75
76     /* Define some handy mnemonics.  These correspond to object numbers. */
77     AXE = VOCWRD(WORD_AXE, 1);
78     BATTERY = VOCWRD(WORD_BATTERY, 1);
79     BEAR = VOCWRD(WORD_BEAR, 1);
80     BIRD = VOCWRD(WORD_BIRD, 1);
81     BLOOD = VOCWRD(WORD_BLOOD, 1);
82     BOTTLE = VOCWRD(WORD_BOTTLE, 1);
83     CAGE = VOCWRD(WORD_CAGE, 1);
84     CAVITY = VOCWRD(WORD_CAVITY, 1);
85     CHASM = VOCWRD(WORD_CHASM, 1);
86     CLAM = VOCWRD(WORD_CLAM, 1);
87     DOOR = VOCWRD(WORD_DOOR, 1);
88     DRAGON = VOCWRD(WORD_DRAGON, 1);
89     DWARF = VOCWRD(WORD_DWARF, 1);
90     FISSURE = VOCWRD(WORD_FISSURE, 1);
91     FOOD = VOCWRD(WORD_FOOD, 1);
92     GRATE = VOCWRD(WORD_GRATE, 1);
93     KEYS = VOCWRD(WORD_KEYS, 1);
94     KNIFE = VOCWRD(WORD_KNIFE, 1);
95     LAMP = VOCWRD(WORD_LAMP, 1);
96     MAGAZINE = VOCWRD(WORD_MAGAZINE, 1);
97     MESSAG = VOCWRD(WORD_MESSAG, 1);
98     MIRROR = VOCWRD(WORD_MIRROR, 1);
99     OGRE = VOCWRD(WORD_OGRE, 1);
100     OIL = VOCWRD(WORD_OIL, 1);
101     OYSTER = VOCWRD(WORD_OYSTER, 1);
102     PILLOW = VOCWRD(WORD_PILLOW, 1);
103     PLANT = VOCWRD(WORD_PLANT, 1);
104     PLANT2 = PLANT + 1;
105     RESER = VOCWRD(WORD_RESER, 1);
106     ROD = VOCWRD(WORD_ROD, 1);
107     ROD2 = ROD + 1;
108     SIGN = VOCWRD(WORD_SIGN, 1);
109     SNAKE = VOCWRD(WORD_SNAKE, 1);
110     STEPS = VOCWRD(WORD_STEPS, 1);
111     TROLL = VOCWRD(WORD_TROLL, 1);
112     TROLL2 = TROLL + 1;
113     URN = VOCWRD(WORD_URN, 1);
114     VEND = VOCWRD(WORD_VEND, 1);
115     VOLCANO = VOCWRD(WORD_VOLCANO, 1);
116     WATER = VOCWRD(WORD_WATER, 1);
117
118     /* Vocabulary for treasures */
119     AMBER = VOCWRD(WORD_AMBER, 1);
120     CHAIN = VOCWRD(WORD_CHAIN, 1);
121     CHEST = VOCWRD(WORD_CHEST, 1);
122     COINS = VOCWRD(WORD_COINS, 1);
123     EGGS = VOCWRD(WORD_EGGS, 1);
124     EMERALD = VOCWRD(WORD_EMERALD, 1);
125     JADE = VOCWRD(WORD_JADE, 1);
126     NUGGET = VOCWRD(WORD_NUGGET, 1);
127     PEARL = VOCWRD(WORD_PEARL, 1);
128     PYRAMID = VOCWRD(WORD_PYRAMID, 1);
129     RUBY = VOCWRD(WORD_RUBY, 1);
130     RUG = VOCWRD(WORD_RUG, 1);
131     SAPPH = VOCWRD(WORD_SAPPH, 1);
132     TRIDENT = VOCWRD(WORD_TRIDENT, 1);
133     VASE = VOCWRD(WORD_VASE, 1);
134
135     /* These are motion-verb numbers. */
136     BACK = VOCWRD(WORD_BACK, 0);
137     CAVE = VOCWRD(WORD_CAVE, 0);
138     DPRSSN = VOCWRD(WORD_DPRSSN, 0);
139     ENTER = VOCWRD(WORD_ENTER, 0);
140     ENTRNC = VOCWRD(WORD_ENTRNC, 0);
141     LOOK = VOCWRD(WORD_LOOK, 0);
142     NUL = VOCWRD(WORD_NUL, 0);
143     STREAM = VOCWRD(WORD_STREAM, 0);
144
145     /* And some action verbs. */
146     FIND = VOCWRD(WORD_FIND, 2);
147     INVENT = VOCWRD(WORD_INVENT, 2);
148     LOCK = VOCWRD(WORD_LOCK, 2);
149     SAY = VOCWRD(WORD_SAY, 2);
150     THROW = VOCWRD(WORD_THROW, 2);
151
152     /*  Initialise the dwarves.  game.dloc is loc of dwarves,
153      *  hard-wired in.  game.odloc is prior loc of each dwarf,
154      *  initially garbage.  DALTLC is alternate initial loc for dwarf,
155      *  in case one of them starts out on top of the adventurer.  (No
156      *  2 of the 5 initial locs are adjacent.)  game.dseen is true if
157      *  dwarf has seen him.  game.dflag controls the level of
158      *  activation of all this:
159      *  0       No dwarf stuff yet (wait until reaches Hall Of Mists)
160      *  1       Reached Hall Of Mists, but hasn't met first dwarf
161      *  2       Met first dwarf, others start moving, no knives thrown yet
162      *  3       A knife has been thrown (first set always misses)
163      *  3+      Dwarves are mad (increases their accuracy)
164      *  Sixth dwarf is special (the pirate).  He always starts at his
165      *  chest's eventual location inside the maze.  This loc is saved
166      *  in game.chloc for ref.  the dead end in the other maze has its
167      *  loc stored in game.chloc2. */
168     game.chloc = LOC_DEADEND12;
169     game.chloc2 = LOC_DEADEND13;
170     for (int i = 1; i <= NDWARVES; i++) {
171         game.dseen[i] = false;
172     }
173     game.dflag = 0;
174     game.dloc[1] = LOC_KINGHALL;
175     game.dloc[2] = LOC_WESTBANK;
176     game.dloc[3] = LOC_Y2;
177     game.dloc[4] = LOC_ALIKE3;
178     game.dloc[5] = LOC_COMPLEX;
179     game.dloc[6] = game.chloc;
180
181     game.turns = 0;
182     game.trnluz = 0;
183     game.lmwarn = false;
184     game.iwest = 0;
185     game.knfloc = 0;
186     game.detail = 0;
187     game.abbnum = 5;
188     game.numdie = 0;
189     game.holdng = 0;
190     game.dkill = 0;
191     game.foobar = 0;
192     game.bonus = 0;
193     game.clock1 = WARNTIME;
194     game.clock2 = FLASHTIME;
195     game.conds = SETBIT(11);
196     game.saved = 0;
197     game.closng = false;
198     game.panic = false;
199     game.closed = false;
200     game.clshnt = false;
201     game.novice = false;
202     game.blklin = true;
203 }