5bca67f3e0f267f3ee07a0cf6362514ef4916793
[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     game.lcg_a = 1093;
87     game.lcg_c = 221587;
88     game.lcg_m = 1048576;
89     srand(time(NULL));
90     long seedval = (long)rand();
91     set_seed(seedval);
92
93     /*  Initialize game variables */
94     initialise();
95
96     make_zzword(game.zzword);
97     game.newloc = LOC_START;
98     game.loc = LOC_START;
99     game.limit = GAMELIMIT;
100
101     // apply cheats
102     game.numdie = numdie;
103     game.saved = saved;
104
105     fp = fopen(savefilename, WRITE_MODE);
106     if (fp == NULL) {
107         fprintf(stderr,
108                 "Can't open file %s. Exiting.\n", savefilename);
109         exit(EXIT_FAILURE);
110     }
111
112     savefile(fp, version);
113
114     printf("cheat: %s created.\n", savefilename);
115     return 0;
116 }