Line data Source code
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 11 : int main(int argc, char *argv[]) { 18 : int ch; 19 11 : char *savefilename = NULL; 20 11 : FILE *fp = NULL; 21 : 22 : // Initialize game variables 23 11 : initialise(); 24 : 25 : /* we're generating a saved game, so saved once by default, 26 : * unless overridden with command-line options below. 27 : */ 28 11 : game.saved = 1; 29 : 30 : /* Options. */ 31 11 : const char *opts = "d:l:s:t:v:o:"; 32 11 : 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 27 : while ((ch = getopt(argc, argv, opts)) != EOF) { 43 17 : switch (ch) { 44 4 : case 'd': 45 4 : game.numdie = (turn_t)atoi(optarg); 46 4 : printf("cheat: game.numdie = %d\n", game.numdie); 47 4 : break; 48 1 : case 'l': 49 1 : game.limit = (turn_t)atoi(optarg); 50 1 : printf("cheat: game.limit = %d\n", game.limit); 51 1 : break; 52 1 : case 's': 53 1 : game.saved = (int)atoi(optarg); 54 1 : printf("cheat: game.saved = %d\n", game.saved); 55 1 : break; 56 1 : case 't': 57 1 : game.turns = (turn_t)atoi(optarg); 58 1 : printf("cheat: game.turns = %d\n", game.turns); 59 1 : break; 60 1 : case 'v': 61 1 : save.version = atoi(optarg); 62 1 : printf("cheat: version = %d\n", save.version); 63 1 : break; 64 8 : case 'o': 65 8 : savefilename = optarg; 66 8 : break; 67 1 : default: 68 1 : fprintf(stderr, usage, argv[0]); 69 1 : exit(EXIT_FAILURE); 70 : break; 71 : } 72 : } 73 : 74 : // Save filename required; the point of cheat is to generate save file 75 10 : if (savefilename == NULL) { 76 2 : fprintf(stderr, usage, argv[0]); 77 2 : fprintf(stderr, "ERROR: filename required\n"); 78 2 : exit(EXIT_FAILURE); 79 : } 80 : 81 8 : fp = fopen(savefilename, WRITE_MODE); 82 8 : if (fp == NULL) { 83 1 : fprintf(stderr, "Can't open file %s. Exiting.\n", savefilename); 84 1 : exit(EXIT_FAILURE); 85 : } 86 : 87 7 : savefile(fp); 88 : 89 7 : fclose(fp); 90 : 91 7 : printf("cheat: %s created.\n", savefilename); 92 : 93 7 : 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 */