Move PRNG initialization to simplify cheat.c
[open-adventure.git] / init.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdbool.h>
5 #include <time.h>
6
7 #include "advent.h"
8
9 /*
10  * Initialisation
11  */
12
13 long initialise(void)
14 {
15     if (oldstyle)
16         printf("Initialising...\n");
17
18     /* Initialize our LCG PRNG with parameters tested against
19      * Knuth vol. 2. by the original authors */
20     game.lcg_a = 1093;
21     game.lcg_c = 221587;
22     game.lcg_m = 1048576;
23     srand(time(NULL));
24     long seedval = (long)rand();
25     set_seed(seedval);
26
27     for (int i = 1; i <= NOBJECTS; i++) {
28         game.place[i] = LOC_NOWHERE;
29     }
30
31     for (int i = 1; i <= NLOCATIONS; i++) {
32         if (!(locations[i].description.big == 0 || tkey[i] == 0)) {
33             int k = tkey[i];
34             if (T_TERMINATE(travel[k]))
35                 conditions[i] |= (1 << COND_FORCED);
36         }
37     }
38
39     /*  Set up the game.atloc and game.link arrays.
40      *  We'll use the DROP subroutine, which prefaces new objects on the
41      *  lists.  Since we want things in the other order, we'll run the
42      *  loop backwards.  If the object is in two locs, we drop it twice.
43      *  Also, since two-placed objects are typically best described
44      *  last, we'll drop them first. */
45     for (int i = NOBJECTS; i >= 1; i--) {
46         if (objects[i].fixd > 0) {
47             drop(i + NOBJECTS, objects[i].fixd);
48             drop(i, objects[i].plac);
49         }
50     }
51
52     for (int i = 1; i <= NOBJECTS; i++) {
53         int k = NOBJECTS + 1 - i;
54         game.fixed[k] = objects[k].fixd;
55         if (objects[k].plac != 0 && objects[k].fixd <= 0)
56             drop(k, objects[k].plac);
57     }
58
59     /*  Treasure props are initially -1, and are set to 0 the first time
60      *  they are described.  game.tally keeps track of how many are
61      *  not yet found, so we know when to close the cave. */
62     for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
63         if (objects[treasure].is_treasure) {
64             if (objects[treasure].inventory != 0)
65                 game.prop[treasure] = -1;
66             game.tally = game.tally - game.prop[treasure];
67         }
68     }
69     game.conds = setbit(11);
70
71     return seedval;
72 }