Coverage - bird attack snake in endgame
[open-adventure.git] / init.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdbool.h>
5 #include <time.h>
6
7 #include "advent.h"
8
9 /*
10  * Initialisation
11  */
12
13 struct settings_t settings = {
14     .logfp = NULL,
15     .oldstyle = false,
16     .prompt = true
17 };
18
19 struct game_t game = {
20     .dloc[1] = LOC_KINGHALL,
21     .dloc[2] = LOC_WESTBANK,
22     .dloc[3] = LOC_Y2,
23     .dloc[4] = LOC_ALIKE3,
24     .dloc[5] = LOC_COMPLEX,
25
26     /*  Sixth dwarf is special (the pirate).  He always starts at his
27      *  chest's eventual location inside the maze. This loc is saved
28      *  in chloc for ref. The dead end in the other maze has its
29      *  loc stored in chloc2. */
30     .dloc[6] = LOC_DEADEND12,
31     .chloc   = LOC_DEADEND12,
32     .chloc2  = LOC_DEADEND13,
33     .abbnum  = 5,
34     .clock1  = WARNTIME,
35     .clock2  = FLASHTIME,
36     .newloc  = LOC_START,
37     .loc     = LOC_START,
38     .limit   = GAMELIMIT,
39     .foobar  = WORD_EMPTY,
40
41     /* Initialize our LCG PRNG with parameters tested against
42      * Knuth vol. 2. by the original authors */
43     .lcg_a = 1093,
44     .lcg_c = 221587,
45     .lcg_m = 1048576,
46 };
47
48 long initialise(void)
49 {
50     if (settings.oldstyle)
51         printf("Initialising...\n");
52
53     srand(time(NULL));
54     long seedval = (long)rand();
55     set_seed(seedval);
56
57     for (int i = 1; i <= NOBJECTS; i++) {
58         game.place[i] = LOC_NOWHERE;
59     }
60
61     for (int i = 1; i <= NLOCATIONS; i++) {
62         if (!(locations[i].description.big == 0 ||
63               tkey[i] == 0)) {
64             int k = tkey[i];
65             if (T_TERMINATE(travel[k]))
66                 conditions[i] |= (1 << COND_FORCED);
67         }
68     }
69
70     /*  Set up the game.atloc and game.link arrays.
71      *  We'll use the DROP subroutine, which prefaces new objects on the
72      *  lists.  Since we want things in the other order, we'll run the
73      *  loop backwards.  If the object is in two locs, we drop it twice.
74      *  Also, since two-placed objects are typically best described
75      *  last, we'll drop them first. */
76     for (int i = NOBJECTS; i >= 1; i--) {
77         if (objects[i].fixd > 0) {
78             drop(i + NOBJECTS, objects[i].fixd);
79             drop(i, objects[i].plac);
80         }
81     }
82
83     for (int i = 1; i <= NOBJECTS; i++) {
84         int k = NOBJECTS + 1 - i;
85         game.fixed[k] = objects[k].fixd;
86         if (objects[k].plac != 0 && objects[k].fixd <= 0)
87             drop(k, objects[k].plac);
88     }
89
90     /*  Treasure props are initially -1, and are set to 0 the first time
91      *  they are described.  game.tally keeps track of how many are
92      *  not yet found, so we know when to close the cave. */
93     for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
94         if (objects[treasure].is_treasure) {
95             if (objects[treasure].inventory != 0)
96                 game.prop[treasure] = STATE_NOTFOUND;
97             game.tally = game.tally - game.prop[treasure];
98         }
99     }
100     game.conds = setbit(11);
101
102     return seedval;
103 }