7 #include "linenoise/linenoise.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 26 /* 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 int savefile(FILE *fp)
31 /* Save game to file. No input or output from user. */
38 save.version = VRSION;
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])) return GO_CLEAROBJ;
59 game.saved = game.saved + 5;
62 char* name = linenoise("\nFile name: ");
65 fp = fopen(name, WRITE_MODE);
67 printf("Can't open file %s, try again.\n", name);
72 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
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])) return GO_CLEAROBJ;
94 char* name = linenoise("\nFile name: ");
97 fp = fopen(name, READ_MODE);
99 printf("Can't open file %s, try again.\n", name);
106 int restore(FILE* fp)
108 /* Read and restore game state from file, assuming
109 * sane initial state.
110 * If ADVENT_NOSAVE is defined, do nothing instead. */
115 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
117 if (save.version != VRSION) {
118 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
120 memcpy(&game, &save.game, sizeof(struct game_t));
121 game.zzword = rndvoc(3, game.zzword);