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