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