6 #include "linenoise/linenoise.h"
9 * (ESR) This replaces a bunch of particularly nasty FORTRAN-derived code;
10 * see the history.adoc file in the source distribution for discussion.
13 #define VRSION 26 /* bump on save format change */
16 * If you change the first three members, the resume function may not properly
17 * reject saves from older versions. Yes, this glues us to a hardware-
18 * dependent length of long. Later members can change, but bump the version
23 long mode; /* not used, must be present for version detection */
29 #define IGNORE(r) do{if (r){}}while(0)
31 int savefile(FILE *fp, long version)
32 /* Save game to file. No input or output from user. */
40 save.version = (version == 0) ? VRSION : version;
42 memcpy(&save.game, &game, sizeof(struct game_t));
43 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
47 /* Suspend and resume */
50 /* Suspend. Offer to save things in a file, but charging
51 * some points (so can't win by using saved games to retry
52 * battles or to start over after learning zzword).
53 * If ADVENT_NOSAVE is defined, do nothing instead. */
60 rspeak(SUSPEND_WARNING);
61 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
63 game.saved = game.saved + 5;
66 char* name = linenoise("\nFile name: ");
69 fp = fopen(name, WRITE_MODE);
71 printf("Can't open file %s, try again.\n", name);
83 /* Resume. Read a suspended game back from a file.
84 * If ADVENT_NOSAVE is defined, do nothing instead. */
91 if (game.loc != 1 || game.abbrev[1] != 1) {
92 rspeak(RESUME_ABANDON);
93 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
98 char* name = linenoise("\nFile name: ");
101 fp = fopen(name, READ_MODE);
103 printf("Can't open file %s, try again.\n", name);
110 int restore(FILE* fp)
112 /* Read and restore game state from file, assuming
113 * sane initial state.
114 * If ADVENT_NOSAVE is defined, do nothing instead. */
119 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
121 if (save.version != VRSION) {
122 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
124 memcpy(&game, &save.game, sizeof(struct game_t));
125 //game.zzword = rndvoc(3, game.zzword);
126 make_zzword(game.zzword);