Removed advent.info and added to .gitignore
[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     long version = 0;
29
30     /*  Options. */
31     const char* opts = "d:s:v:o:";
32     const char* usage = "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename \n";
33     while ((ch = getopt(argc, argv, opts)) != EOF) {
34         switch (ch) {
35         case 'd':
36             numdie = (long)atoi(optarg);
37             break;
38         case 's':
39             saved = (long)atoi(optarg);
40             break;
41         case 'v':
42             version = (long)atoi(optarg);;
43             break;
44         case 'o':
45             savefilename = optarg;
46             break;
47         default:
48             fprintf(stderr,
49                     usage, argv[0]);
50             fprintf(stderr,
51                     "        -d number of deaths. Signed integer value.'\n");
52             fprintf(stderr,
53                     "        -s number of saves. Signed integer value.\n");
54             fprintf(stderr,
55                     "        -v version number of save format.\n");
56             fprintf(stderr,
57                     "        -o file name of save game to write.\n");
58             exit(-1);
59             break;
60         }
61     }
62     
63     if (savefilename == NULL)
64     {
65         fprintf(stderr,
66                 usage, argv[0]);
67         fprintf(stderr,
68                 "ERROR: filename required\n");
69         exit(-1);
70     }
71
72     FILE *fp = NULL;
73
74     game.lcg_a = 1093;
75     game.lcg_c = 221587;
76     game.lcg_m = 1048576;
77     srand(time(NULL));
78     long seedval = (long)rand();
79     set_seed(seedval);
80
81     /*  Initialize game variables */
82     initialise();
83
84     game.zzword = rndvoc(3, 0);
85     game.newloc = LOC_START;
86     game.loc = LOC_START;
87     game.limit = GAMELIMIT;
88     
89     // apply cheats
90     game.numdie = numdie;
91     game.saved = saved;
92     
93     fp = fopen(savefilename, WRITE_MODE);
94     if (fp == NULL)
95     {
96         fprintf(stderr,
97                 "Can't open file %s. Exiting.\n", savefilename);
98         exit(-1);
99     }        
100
101     savefile(fp, version);
102     
103     printf("cheat: %s created.\n", savefilename);
104     return 0;
105 }