Fixed copy-paste errors
[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 int main(int argc, char *argv[])
14 {
15     int ch;
16     char *savefilename = NULL;
17     int version = 0;
18     FILE *fp = NULL;
19
20     // Initialize game variables
21     initialise();
22
23     /* we're generating a saved game, so saved once by default,
24      * unless overridden with command-line options below.
25      */
26     game.saved = 1;
27
28     /*  Options. */
29     const char* opts = "d:l:s:t:v:o:";
30     const char* usage = "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename \n"
31                         "        -d number of deaths. Signed integer.\n"
32                         "        -l lifetime of lamp in turns. Signed integer.\n"
33                         "        -s number of saves. Signed integer.\n"
34                         "        -t number of turns. Signed integer.\n"
35                         "        -v version number of save format.\n"
36                         "        -o required. File name of save game to write.\n";
37
38     while ((ch = getopt(argc, argv, opts)) != EOF) {
39         switch (ch) {
40         case 'd':
41             game.numdie = (turn_t)atoi(optarg);
42             printf("cheat: game.numdie = %ld\n", game.numdie);
43             break;
44         case 'l':
45             game.limit = (turn_t)atoi(optarg);
46             printf("cheat: game.limit = %ld\n", game.limit);
47             break;
48         case 's':
49             game.saved = (long)atoi(optarg);
50             printf("cheat: game.saved = %ld\n", game.saved);
51             break;
52         case 't':
53             game.turns = (turn_t)atoi(optarg);
54             printf("cheat: game.turns = %ld\n", game.turns);
55             break;
56         case 'v':
57             version = atoi(optarg);
58             printf("cheat: version = %d\n", version);
59             break;
60         case 'o':
61             savefilename = optarg;
62             break;
63         default:
64             fprintf(stderr,
65                     usage, argv[0]);
66             exit(EXIT_FAILURE);
67             break;
68         }
69     }
70
71     // Save filename required; the point of cheat is to generate save file
72     if (savefilename == NULL) {
73         fprintf(stderr,
74                 usage, argv[0]);
75         fprintf(stderr,
76                 "ERROR: filename required\n");
77         exit(EXIT_FAILURE);
78     }
79
80     fp = fopen(savefilename, WRITE_MODE);
81     if (fp == NULL) {
82         fprintf(stderr,
83                 "Can't open file %s. Exiting.\n", savefilename);
84         exit(EXIT_FAILURE);
85     }
86
87     savefile(fp, version);
88
89     fclose(fp);
90
91     printf("cheat: %s created.\n", savefilename);
92
93     return EXIT_SUCCESS;
94 }