3 #include <editline/readline.h>
10 * (ESR) This replaces a bunch of particularly nasty FORTRAN-derived code;
11 * see the history.adoc file in the source distribution for discussion.
14 #define VRSION 27 /* bump on save format change */
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
24 long mode; /* not used, must be present for version detection */
30 #define IGNORE(r) do{if (r){}}while(0)
32 int savefile(FILE *fp, long version)
33 /* Save game to file. No input or output from user. */
35 save.savetime = time(NULL);
37 save.version = (version == 0) ? VRSION : version;
39 memcpy(&save.game, &game, sizeof(struct game_t));
40 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
44 /* Suspend and resume */
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. */
57 rspeak(SUSPEND_WARNING);
58 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
60 game.saved = game.saved + 5;
63 char* name = readline("\nFile name: ");
66 fp = fopen(name, WRITE_MODE);
68 printf("Can't open file %s, try again.\n", name);
80 /* Resume. Read a suspended game back from a file.
81 * If ADVENT_NOSAVE is defined, do nothing instead. */
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]))
95 char* name = readline("\nFile name: ");
98 fp = fopen(name, READ_MODE);
100 printf("Can't open file %s, try again.\n", name);
107 int restore(FILE* fp)
109 /* Read and restore game state from file, assuming
110 * sane initial state.
111 * If ADVENT_NOSAVE is defined, do nothing instead. */
116 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
118 if (save.version != VRSION) {
119 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
121 memcpy(&game, &save.game, sizeof(struct game_t));