"cheat" now has command-line arguments for generating cheat files
[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 "database.h"
9 #include "linenoise/linenoise.h"
10 #include "newdb.h"
11
12 struct game_t game;
13
14 long LNLENG, LNPOSN;
15 char rawbuf[LINESIZE], INLINE[LINESIZE + 1];
16
17 FILE  *logfp = NULL, *rfp = NULL;
18 bool oldstyle = false;
19 bool editline = true;
20 bool prompt = true;
21
22 int main(int argc, char *argv[])
23 {
24     int ch;
25     char *savefilename = NULL;
26     long numdie = 0;
27     long saved = 1;
28
29     /*  Options. */
30     const char* opts = "d:s:o:";
31     const char* usage = "Usage: %s [-d numdie] [-s numsaves] -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 'o':
41             savefilename = optarg;
42             break;
43         default:
44             fprintf(stderr,
45                     usage, argv[0]);
46             fprintf(stderr,
47                     "        -d number of deaths. Signed integer value.'\n");
48             fprintf(stderr,
49                     "        -s number of saves. Signed integer value.\n");
50             fprintf(stderr,
51                     "        -o file name of save game to write.\n");
52             exit(-1);
53             break;
54         }
55     }
56     
57     if (savefilename == NULL)
58     {
59         fprintf(stderr,
60                 usage, argv[0]);
61         fprintf(stderr,
62                 "ERROR: filename required\n");
63         exit(-1);
64     }
65
66     FILE *fp = NULL;
67
68     game.lcg_a = 1093;
69     game.lcg_c = 221587;
70     game.lcg_m = 1048576;
71     srand(time(NULL));
72     long seedval = (long)rand();
73     set_seed(seedval);
74
75     /*  Initialize game variables */
76     initialise();
77
78     game.zzword = rndvoc(3, 0);
79     game.newloc = LOC_START;
80     game.loc = LOC_START;
81     game.limit = GAMELIMIT;
82     
83     // apply cheats
84     game.numdie = numdie;
85     game.saved = saved;
86     
87     fp = fopen(savefilename, WRITE_MODE);
88     if (fp == NULL)
89     {
90         printf("Can't open file %s. Exiting.\n", savefilename);
91         exit(-1);
92     }        
93
94     savefile(fp);
95     printf("cheat: %s created.\n", savefilename);
96     return 0;
97 }