bdfaa8a59b06c547612628e37ac119615156212c
[open-adventure.git] / cheat.c
1 /*
2  * 'cheat' is a tool for generating save game files to test states that ought
3  * not happen. It leverages chunks of advent, mostly initialize() and
4  * savefile(), so we know we're always outputing save files that advent
5  * can import.
6  */
7 #include <getopt.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <stdbool.h>
11 #include "advent.h"
12
13 FILE *logfp = NULL;
14 bool oldstyle = false;
15 bool prompt = true;
16
17 int main(int argc, char *argv[])
18 {
19     int ch;
20     char *savefilename = NULL;
21     long numdie = 0;
22     long saved = 1;
23     long version = 0;
24     FILE *fp = NULL;
25
26     /*  Options. */
27     const char* opts = "d:s:v:o:";
28     const char* usage = "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename \n";
29     while ((ch = getopt(argc, argv, opts)) != EOF) {
30         switch (ch) {
31         case 'd':
32             numdie = (long)atoi(optarg);
33             break;
34         case 's':
35             saved = (long)atoi(optarg);
36             break;
37         case 'v':
38             version = (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                     "        -v version number of save format.\n");
52             fprintf(stderr,
53                     "        -o file name of save game to write.\n");
54             exit(EXIT_FAILURE);
55             break;
56         }
57     }
58
59     // Save filename required; the point of cheat is to generate save file
60     if (savefilename == NULL) {
61         fprintf(stderr,
62                 usage, argv[0]);
63         fprintf(stderr,
64                 "ERROR: filename required\n");
65         exit(EXIT_FAILURE);
66     }
67
68     // Initialize game variables
69     initialise();
70
71     // apply cheats
72     game.numdie = numdie;
73     game.saved = saved;
74
75     fp = fopen(savefilename, WRITE_MODE);
76     if (fp == NULL) {
77         fprintf(stderr,
78                 "Can't open file %s. Exiting.\n", savefilename);
79         exit(EXIT_FAILURE);
80     }
81
82     savefile(fp, version);
83
84     printf("cheat: %s created.\n", savefilename);
85
86     return EXIT_SUCCESS;
87 }