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, long version)
31 /* Save game to file. No input or output from user. */
39 save.version = (version == 0) ? VRSION : version;
41 memcpy(&save.game, &game, sizeof(struct game_t));
42 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
46 /* Suspend and resume */
49 /* Suspend. Offer to save things in a file, but charging
50 * some points (so can't win by using saved games to retry
51 * battles or to start over after learning zzword).
52 * If ADVENT_NOSAVE is defined, do nothing instead. */
59 rspeak(SUSPEND_WARNING);
60 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN])) return GO_CLEAROBJ;
61 game.saved = game.saved + 5;
64 char* name = linenoise("\nFile name: ");
67 fp = fopen(name, WRITE_MODE);
69 printf("Can't open file %s, try again.\n", name);
81 /* Resume. Read a suspended game back from a file.
82 * If ADVENT_NOSAVE is defined, do nothing instead. */
89 if (game.loc != 1 || game.abbrev[1] != 1) {
90 rspeak(RESUME_ABANDON);
91 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN])) return GO_CLEAROBJ;
95 char* name = linenoise("\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));
122 game.zzword = rndvoc(3, game.zzword);