Move most build instructions into INSTALL.adoc.
[open-adventure.git] / cheat.c
1 #include <getopt.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdbool.h>
5 #include <time.h>
6 #include "advent.h"
7 #include "dungeon.h"
8
9 FILE  *logfp = NULL, *rfp = NULL;
10 bool oldstyle = false;
11 bool editline = true;
12 bool prompt = true;
13
14 int main(int argc, char *argv[])
15 {
16     int ch;
17     char *savefilename = NULL;
18     long numdie = 0;
19     long saved = 1;
20     long version = 0;
21
22     /*  Options. */
23     const char* opts = "d:s:v:o:";
24     const char* usage = "Usage: %s [-d numdie] [-s numsaves] [-v version] -o savefilename \n";
25     while ((ch = getopt(argc, argv, opts)) != EOF) {
26         switch (ch) {
27         case 'd':
28             numdie = (long)atoi(optarg);
29             break;
30         case 's':
31             saved = (long)atoi(optarg);
32             break;
33         case 'v':
34             version = (long)atoi(optarg);;
35             break;
36         case 'o':
37             savefilename = optarg;
38             break;
39         default:
40             fprintf(stderr,
41                     usage, argv[0]);
42             fprintf(stderr,
43                     "        -d number of deaths. Signed integer value.'\n");
44             fprintf(stderr,
45                     "        -s number of saves. Signed integer value.\n");
46             fprintf(stderr,
47                     "        -v version number of save format.\n");
48             fprintf(stderr,
49                     "        -o file name of save game to write.\n");
50             exit(EXIT_FAILURE);
51             break;
52         }
53     }
54
55     if (savefilename == NULL) {
56         fprintf(stderr,
57                 usage, argv[0]);
58         fprintf(stderr,
59                 "ERROR: filename required\n");
60         exit(EXIT_FAILURE);
61     }
62
63     FILE *fp = NULL;
64
65     /*  Initialize game variables */
66     initialise();
67
68     make_zzword(game.zzword);
69
70     // apply cheats
71     game.numdie = numdie;
72     game.saved = saved;
73
74     fp = fopen(savefilename, WRITE_MODE);
75     if (fp == NULL) {
76         fprintf(stderr,
77                 "Can't open file %s. Exiting.\n", savefilename);
78         exit(EXIT_FAILURE);
79     }
80
81     savefile(fp, version);
82
83     printf("cheat: %s created.\n", savefilename);
84     return 0;
85 }