Reissue 1.18 - same code, corrected metadata.
[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
34         srand(time(NULL));
35         int seedval = (int)rand();
36         set_seed(seedval);
37
38         for (int i = 1; i <= NDWARVES; i++) {
39                 game.dwarves[i].loc = dwarflocs[i - 1];
40         }
41
42         for (int i = 1; i <= NOBJECTS; i++) {
43                 game.objects[i].place = LOC_NOWHERE;
44         }
45
46         for (int i = 1; i <= NLOCATIONS; i++) {
47                 if (!(locations[i].description.big == 0 || tkey[i] == 0)) {
48                         int k = tkey[i];
49                         if (travel[k].motion == HERE) {
50                                 conditions[i] |= (1 << COND_FORCED);
51                         }
52                 }
53         }
54
55         /*  Set up the game.locs atloc and game.link arrays.
56          *  We'll use the DROP subroutine, which prefaces new objects on the
57          *  lists.  Since we want things in the other order, we'll run the
58          *  loop backwards.  If the object is in two locs, we drop it twice.
59          *  Also, since two-placed objects are typically best described
60          *  last, we'll drop them first. */
61         for (int i = NOBJECTS; i >= 1; i--) {
62                 if (objects[i].fixd > 0) {
63                         drop(i + NOBJECTS, objects[i].fixd);
64                         drop(i, objects[i].plac);
65                 }
66         }
67
68         for (int i = 1; i <= NOBJECTS; i++) {
69                 int k = NOBJECTS + 1 - i;
70                 game.objects[k].fixed = objects[k].fixd;
71                 if (objects[k].plac != 0 && objects[k].fixd <= 0) {
72                         drop(k, objects[k].plac);
73                 }
74         }
75
76         /*  Treasure props are initially STATE_NOTFOUND, and are set to
77          *  STATE_FOUND the first time they are described.  game.tally
78          *  keeps track of how many are not yet found, so we know when to
79          *  close the cave. */
80         for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
81                 if (objects[treasure].is_treasure) {
82                         ++game.tally;
83                         if (objects[treasure].inventory != 0) {
84                                 PROP_SET_NOT_FOUND(treasure);
85                         }
86                 }
87         }
88         game.conds = setbit(COND_HBASE);
89
90         return seedval;
91 }