Cut initialize in favor of implicit/default initialization where possible
[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     .chloc = LOC_DEADEND12,
12     .chloc2 = LOC_DEADEND13,
13     .dloc[1] = LOC_KINGHALL,
14     .dloc[2] = LOC_WESTBANK,
15     .dloc[3] = LOC_Y2,
16     .dloc[4] = LOC_ALIKE3,
17     .dloc[5] = LOC_COMPLEX,
18     .dloc[6] = LOC_DEADEND12,
19     .abbnum = 5,
20     .clock1 = WARNTIME,
21     .clock2 = FLASHTIME,
22     .blklin = true
23 };
24
25 FILE  *logfp = NULL, *rfp = NULL;
26 bool oldstyle = false;
27 bool editline = true;
28 bool prompt = true;
29
30 int main(int argc, char *argv[])
31 {
32     int ch;
33     char *savefilename = NULL;
34     long numdie = 0;
35     long saved = 1;
36     long version = 0;
37
38     /*  Options. */
39     const char* opts = "d:s:v:o:";
40     const char* usage = "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename \n";
41     while ((ch = getopt(argc, argv, opts)) != EOF) {
42         switch (ch) {
43         case 'd':
44             numdie = (long)atoi(optarg);
45             break;
46         case 's':
47             saved = (long)atoi(optarg);
48             break;
49         case 'v':
50             version = (long)atoi(optarg);;
51             break;
52         case 'o':
53             savefilename = optarg;
54             break;
55         default:
56             fprintf(stderr,
57                     usage, argv[0]);
58             fprintf(stderr,
59                     "        -d number of deaths. Signed integer value.'\n");
60             fprintf(stderr,
61                     "        -s number of saves. Signed integer value.\n");
62             fprintf(stderr,
63                     "        -v version number of save format.\n");
64             fprintf(stderr,
65                     "        -o file name of save game to write.\n");
66             exit(EXIT_FAILURE);
67             break;
68         }
69     }
70
71     if (savefilename == NULL) {
72         fprintf(stderr,
73                 usage, argv[0]);
74         fprintf(stderr,
75                 "ERROR: filename required\n");
76         exit(EXIT_FAILURE);
77     }
78
79     FILE *fp = NULL;
80
81     game.lcg_a = 1093;
82     game.lcg_c = 221587;
83     game.lcg_m = 1048576;
84     srand(time(NULL));
85     long seedval = (long)rand();
86     set_seed(seedval);
87
88     /*  Initialize game variables */
89     initialise();
90
91     make_zzword(game.zzword);
92     game.newloc = LOC_START;
93     game.loc = LOC_START;
94     game.limit = GAMELIMIT;
95
96     // apply cheats
97     game.numdie = numdie;
98     game.saved = saved;
99
100     fp = fopen(savefilename, WRITE_MODE);
101     if (fp == NULL) {
102         fprintf(stderr,
103                 "Can't open file %s. Exiting.\n", savefilename);
104         exit(EXIT_FAILURE);
105     }
106
107     savefile(fp, version);
108
109     printf("cheat: %s created.\n", savefilename);
110     return 0;
111 }