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 */
32 /* Suspend and resume */
35 /* Suspend. Offer to save things in a file, but charging
36 * some points (so can't win by using saved games to retry
37 * battles or to start over after learning zzword).
38 * If ADVENT_NOSAVE is defined, do nothing instead. */
46 RSPEAK(SUSPEND_WARNING);
47 if (!YES(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN])) return GO_CLEAROBJ;
48 game.saved = game.saved + 5;
51 char* name = linenoise("\nFile name: ");
54 fp = fopen(name, WRITE_MODE);
56 printf("Can't open file %s, try again.\n", name);
64 save.version = VRSION;
65 memcpy(&save.game, &game, sizeof(struct game_t));
66 save.bird = OBJSND[BIRD];
67 save.bivalve = OBJTXT[OYSTER];
68 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
76 /* Resume. Read a suspended game back from a file.
77 * If ADVENT_NOSAVE is defined, do nothing instead. */
84 if (game.loc != 1 || game.abbrev[1] != 1) {
85 RSPEAK(RESUME_ABANDON);
86 if (!YES(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN])) return GO_CLEAROBJ;
90 char* name = linenoise("\nFile name: ");
93 fp = fopen(name, READ_MODE);
95 printf("Can't open file %s, try again.\n", name);
102 int restore(FILE* fp)
104 /* Read and restore game state from file, assuming
105 * sane initial state.
106 * If ADVENT_NOSAVE is defined, do nothing instead. */
111 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
113 if (save.version != VRSION) {
114 SETPRM(1, save.version / 10, MOD(save.version, 10));
115 SETPRM(3, VRSION / 10, MOD(VRSION, 10));
116 RSPEAK(VERSION_SKEW);
118 memcpy(&game, &save.game, sizeof(struct game_t));
119 OBJSND[BIRD] = save.bird;
120 OBJTXT[OYSTER] = save.bivalve;
121 game.zzword = RNDVOC(3, game.zzword);