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