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