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