Create a cheater to test strange save/resume cases.
[open-adventure.git] / saveresume.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "advent.h"
5 #include "database.h"
6 #include "newdb.h"
7 #include "linenoise/linenoise.h"
8
9 /*
10  * (ESR) This replaces  a bunch of particularly nasty FORTRAN-derived code;
11  * see the history.adoc file in the source distribution for discussion.
12  */
13
14 #define VRSION  26      /* bump on save format change */
15
16 /*
17  * If you change the first three members, the resume function may not properly
18  * reject saves from older versions.  Yes, this glues us to a hardware-
19  * dependent length of long.  Later members can change, but bump the version
20  * when you do that.
21  */
22 struct save_t {
23     long savetime;
24     long mode;          /* not used, must be present for version detection */
25     long version;
26     struct game_t game;
27 };
28 struct save_t save;
29
30 int savefile(FILE *fp)
31     /* Save game to file. No input or output from user. */
32 {
33     long i, k;
34     datime(&i, &k);
35     k = i + 650 * k;
36     save.savetime = k;
37     save.mode = -1;
38     save.version = VRSION;
39     memcpy(&save.game, &game, sizeof(struct game_t));
40     IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
41     return(0);
42 }
43
44 /* Suspend and resume */
45 int suspend(void)
46 {
47     /*  Suspend.  Offer to save things in a file, but charging
48      *  some points (so can't win by using saved games to retry
49      *  battles or to start over after learning zzword).
50      *  If ADVENT_NOSAVE is defined, do nothing instead. */
51
52 #ifdef ADVENT_NOSAVE
53     return GO_UNKNOWN;
54 #endif
55     FILE *fp = NULL;
56
57     rspeak(SUSPEND_WARNING);
58     if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN])) return GO_CLEAROBJ;
59     game.saved = game.saved + 5;
60
61     while (fp == NULL) {
62         char* name = linenoise("\nFile name: ");
63         if (name == NULL)
64             return GO_TOP;
65         fp = fopen(name, WRITE_MODE);
66         if (fp == NULL)
67             printf("Can't open file %s, try again.\n", name);
68         linenoiseFree(name);
69     }
70
71     savefile(fp);
72     IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
73     fclose(fp);
74     rspeak(RESUME_HELP);
75     exit(0);
76 }
77
78 int resume(void)
79 {
80     /*  Resume.  Read a suspended game back from a file.
81      *  If ADVENT_NOSAVE is defined, do nothing instead. */
82
83 #ifdef ADVENT_NOSAVE
84     return GO_UNKNOWN;
85 #endif
86     FILE *fp = NULL;
87
88     if (game.loc != 1 || game.abbrev[1] != 1) {
89         rspeak(RESUME_ABANDON);
90         if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN])) return GO_CLEAROBJ;
91     }
92
93     while (fp == NULL) {
94         char* name = linenoise("\nFile name: ");
95         if (name == NULL)
96             return GO_TOP;
97         fp = fopen(name, READ_MODE);
98         if (fp == NULL)
99             printf("Can't open file %s, try again.\n", name);
100         linenoiseFree(name);
101     }
102
103     return restore(fp);
104 }
105
106 int restore(FILE* fp)
107 {
108     /*  Read and restore game state from file, assuming
109      *  sane initial state.
110      *  If ADVENT_NOSAVE is defined, do nothing instead. */
111 #ifdef ADVENT_NOSAVE
112     return GO_UNKNOWN;
113 #endif
114
115     IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
116     fclose(fp);
117     if (save.version != VRSION) {
118         rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
119     } else {
120         memcpy(&game, &save.game, sizeof(struct game_t));
121         game.zzword = rndvoc(3, game.zzword);
122     }
123     return GO_TOP;
124 }
125
126 /* end */