Reissue 1.18 - same code, corrected metadata.
[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 outputting save files that advent
5  * can import.
6  *
7  * SPDX-FileCopyrightText: (C) 1977, 2005 by Will Crowther and Don Woods
8  * SPDX-License-Identifier: BSD-2-Clause
9  */
10 #include "advent.h"
11 #include <editline/readline.h>
12 #include <getopt.h>
13 #include <stdbool.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 int main(int argc, char *argv[]) {
18         int ch;
19         char *savefilename = NULL;
20         FILE *fp = NULL;
21
22         // Initialize game variables
23         initialise();
24
25         /* we're generating a saved game, so saved once by default,
26          * unless overridden with command-line options below.
27          */
28         game.saved = 1;
29
30         /*  Options. */
31         const char *opts = "d:l:s:t:v:o:";
32         const char *usage =
33             "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename "
34             "\n"
35             "        -d number of deaths. Signed integer.\n"
36             "        -l lifetime of lamp in turns. Signed integer.\n"
37             "        -s number of saves. Signed integer.\n"
38             "        -t number of turns. Signed integer.\n"
39             "        -v version number of save format.\n"
40             "        -o required. File name of save game to write.\n";
41
42         while ((ch = getopt(argc, argv, opts)) != EOF) {
43                 switch (ch) {
44                 case 'd':
45                         game.numdie = (turn_t)atoi(optarg);
46                         printf("cheat: game.numdie = %d\n", game.numdie);
47                         break;
48                 case 'l':
49                         game.limit = (turn_t)atoi(optarg);
50                         printf("cheat: game.limit = %d\n", game.limit);
51                         break;
52                 case 's':
53                         game.saved = (int)atoi(optarg);
54                         printf("cheat: game.saved = %d\n", game.saved);
55                         break;
56                 case 't':
57                         game.turns = (turn_t)atoi(optarg);
58                         printf("cheat: game.turns = %d\n", game.turns);
59                         break;
60                 case 'v':
61                         save.version = atoi(optarg);
62                         printf("cheat: version = %d\n", save.version);
63                         break;
64                 case 'o':
65                         savefilename = optarg;
66                         break;
67                 default:
68                         fprintf(stderr, usage, argv[0]);
69                         exit(EXIT_FAILURE);
70                         break;
71                 }
72         }
73
74         // Save filename required; the point of cheat is to generate save file
75         if (savefilename == NULL) {
76                 fprintf(stderr, usage, argv[0]);
77                 fprintf(stderr, "ERROR: filename required\n");
78                 exit(EXIT_FAILURE);
79         }
80
81         fp = fopen(savefilename, WRITE_MODE);
82         if (fp == NULL) {
83                 fprintf(stderr, "Can't open file %s. Exiting.\n", savefilename);
84                 exit(EXIT_FAILURE);
85         }
86
87         savefile(fp);
88
89         fclose(fp);
90
91         printf("cheat: %s created.\n", savefilename);
92
93         return EXIT_SUCCESS;
94 }
95
96 // LCOV_EXCL_START
97 /*
98  * Ugh...unused, but required for linkage.
99  * See the actually useful version of this in main.c
100  */
101
102 char *myreadline(const char *prompt) { return readline(prompt); }
103 // LCOV_EXCL_STOP
104
105 /* end */