Test coverage - add lots more coverage from actions.c
[open-adventure.git] / cheat.c
1 #define DEFINE_GLOBALS_FROM_INCLUDES
2 #include <getopt.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdbool.h>
6 #include <time.h>
7 #include "advent.h"
8 #include "linenoise/linenoise.h"
9 #include "dungeon.h"
10
11 struct game_t game;
12
13 long LNLENG, LNPOSN;
14 char rawbuf[LINESIZE], INLINE[LINESIZE + 1];
15
16 FILE  *logfp = NULL, *rfp = NULL;
17 bool oldstyle = false;
18 bool editline = true;
19 bool prompt = true;
20
21 int main(int argc, char *argv[])
22 {
23     int ch;
24     char *savefilename = NULL;
25     long numdie = 0;
26     long saved = 1;
27     long version = 0;
28
29     /*  Options. */
30     const char* opts = "d:s:v:o:";
31     const char* usage = "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename \n";
32     while ((ch = getopt(argc, argv, opts)) != EOF) {
33         switch (ch) {
34         case 'd':
35             numdie = (long)atoi(optarg);
36             break;
37         case 's':
38             saved = (long)atoi(optarg);
39             break;
40         case 'v':
41             version = (long)atoi(optarg);;
42             break;
43         case 'o':
44             savefilename = optarg;
45             break;
46         default:
47             fprintf(stderr,
48                     usage, argv[0]);
49             fprintf(stderr,
50                     "        -d number of deaths. Signed integer value.'\n");
51             fprintf(stderr,
52                     "        -s number of saves. Signed integer value.\n");
53             fprintf(stderr,
54                     "        -v version number of save format.\n");
55             fprintf(stderr,
56                     "        -o file name of save game to write.\n");
57             exit(-1);
58             break;
59         }
60     }
61
62     if (savefilename == NULL) {
63         fprintf(stderr,
64                 usage, argv[0]);
65         fprintf(stderr,
66                 "ERROR: filename required\n");
67         exit(-1);
68     }
69
70     FILE *fp = NULL;
71
72     game.lcg_a = 1093;
73     game.lcg_c = 221587;
74     game.lcg_m = 1048576;
75     srand(time(NULL));
76     long seedval = (long)rand();
77     set_seed(seedval);
78
79     /*  Initialize game variables */
80     initialise();
81
82     make_zzword(game.zzword);
83     game.newloc = LOC_START;
84     game.loc = LOC_START;
85     game.limit = GAMELIMIT;
86
87     // apply cheats
88     game.numdie = numdie;
89     game.saved = saved;
90
91     fp = fopen(savefilename, WRITE_MODE);
92     if (fp == NULL) {
93         fprintf(stderr,
94                 "Can't open file %s. Exiting.\n", savefilename);
95         exit(-1);
96     }
97
98     savefile(fp, version);
99
100     printf("cheat: %s created.\n", savefilename);
101     return 0;
102 }