Move PRNG initialization to simplify cheat.c
[open-adventure.git] / cheat.c
1 #include <getopt.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdbool.h>
5 #include <time.h>
6 #include "advent.h"
7 #include "linenoise/linenoise.h"
8 #include "dungeon.h"
9
10 struct game_t game = {
11     .dloc[1] = LOC_KINGHALL,
12     .dloc[2] = LOC_WESTBANK,
13     .dloc[3] = LOC_Y2,
14     .dloc[4] = LOC_ALIKE3,
15     .dloc[5] = LOC_COMPLEX,
16
17     /*  Sixth dwarf is special (the pirate).  He always starts at his
18      *  chest's eventual location inside the maze. This loc is saved
19      *  in chloc for ref. The dead end in the other maze has its
20      *  loc stored in chloc2. */
21     .dloc[6] = LOC_DEADEND12,
22     .chloc   = LOC_DEADEND12,
23     .chloc2  = LOC_DEADEND13,
24     .abbnum  = 5,
25     .clock1  = WARNTIME,
26     .clock2  = FLASHTIME,
27     .blklin  = true
28 };
29
30 FILE  *logfp = NULL, *rfp = NULL;
31 bool oldstyle = false;
32 bool editline = true;
33 bool prompt = true;
34
35 int main(int argc, char *argv[])
36 {
37     int ch;
38     char *savefilename = NULL;
39     long numdie = 0;
40     long saved = 1;
41     long version = 0;
42
43     /*  Options. */
44     const char* opts = "d:s:v:o:";
45     const char* usage = "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename \n";
46     while ((ch = getopt(argc, argv, opts)) != EOF) {
47         switch (ch) {
48         case 'd':
49             numdie = (long)atoi(optarg);
50             break;
51         case 's':
52             saved = (long)atoi(optarg);
53             break;
54         case 'v':
55             version = (long)atoi(optarg);;
56             break;
57         case 'o':
58             savefilename = optarg;
59             break;
60         default:
61             fprintf(stderr,
62                     usage, argv[0]);
63             fprintf(stderr,
64                     "        -d number of deaths. Signed integer value.'\n");
65             fprintf(stderr,
66                     "        -s number of saves. Signed integer value.\n");
67             fprintf(stderr,
68                     "        -v version number of save format.\n");
69             fprintf(stderr,
70                     "        -o file name of save game to write.\n");
71             exit(EXIT_FAILURE);
72             break;
73         }
74     }
75
76     if (savefilename == NULL) {
77         fprintf(stderr,
78                 usage, argv[0]);
79         fprintf(stderr,
80                 "ERROR: filename required\n");
81         exit(EXIT_FAILURE);
82     }
83
84     FILE *fp = NULL;
85
86     /*  Initialize game variables */
87     initialise();
88
89     make_zzword(game.zzword);
90     game.newloc = LOC_START;
91     game.loc = LOC_START;
92     game.limit = GAMELIMIT;
93
94     // apply cheats
95     game.numdie = numdie;
96     game.saved = saved;
97
98     fp = fopen(savefilename, WRITE_MODE);
99     if (fp == NULL) {
100         fprintf(stderr,
101                 "Can't open file %s. Exiting.\n", savefilename);
102         exit(EXIT_FAILURE);
103     }
104
105     savefile(fp, version);
106
107     printf("cheat: %s created.\n", savefilename);
108     return 0;
109 }