Compile switch to disable save and restore
[open-adventure.git] / saveresume.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "advent.h"
5 #include "database.h"
6 #include "newdb.h"
7 #include "linenoise/linenoise.h"
8
9 /*
10  * (ESR) This replaces  a bunch of particularly nasty FORTRAN-derived code;
11  * see the history.adoc file in the source distribution for discussion.
12  */
13
14 #define VRSION  26      /* bump on save format change */
15
16 /*
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
20  * when you do that.
21  */
22 struct save_t {
23     long savetime;
24     long mode;          /* not used, must be present for version detection */
25     long version;
26     struct game_t game;
27     long bird;
28     long bivalve;
29 };
30 struct save_t save;
31
32 /* Suspend and resume */
33 int suspend(FILE *input)
34 {   /*  Suspend.  Offer to save things in a file, but charging
35      *  some points (so can't win by using saved games to retry
36      *  battles or to start over after learning zzword).
37      *  If ADVENT_NOSAVE is defined, do nothing instead. */
38
39 #ifdef ADVENT_NOSAVE
40     return GO_UNKNOWN;
41 #endif
42     long i, k;
43     FILE *fp = NULL;
44
45     RSPEAK(SUSPEND_WARNING);
46     if (!YES(input,THIS_ACCEPTABLE,OK_MAN,OK_MAN)) return GO_CLEAROBJ;
47     game.saved=game.saved+5;
48
49     while (fp == NULL) {
50         char* name = linenoise("\nFile name: ");
51         if (name == NULL)
52             return GO_TOP;
53         fp = fopen(name, WRITE_MODE);
54         if (fp == NULL)
55             printf("Can't open file %s, try again.\n", name); 
56         linenoiseFree(name);
57     }
58
59     DATIME(&i,&k);
60     k=i+650*k;
61     save.savetime = k;
62     save.mode = -1;
63     save.version = VRSION;
64     memcpy(&save.game, &game, sizeof(struct game_t));
65     save.bird = OBJSND[BIRD];
66     save.bivalve = OBJTXT[OYSTER];
67     IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
68     fclose(fp);
69     RSPEAK(RESUME_HELP);
70     exit(0);
71 }
72
73 int resume(FILE *input)
74 {   /*  Resume.  Read a suspended game back from a file.
75      *  If ADVENT_NOSAVE is defined, do nothing instead. */
76
77 #ifdef ADVENT_NOSAVE
78     return GO_UNKNOWN;
79 #endif
80     FILE *fp = NULL;
81
82     if (game.loc != 1 || game.abbrev[1] != 1) {
83         RSPEAK(RESUME_ABANDON);
84         if (!YES(input,THIS_ACCEPTABLE,OK_MAN,OK_MAN)) return GO_CLEAROBJ;
85     }
86
87     while (fp == NULL) {
88         char* name = linenoise("\nFile name: ");
89         if (name == NULL)
90             return GO_TOP;
91         fp = fopen(name, READ_MODE);
92         if (fp == NULL)
93             printf("Can't open file %s, try again.\n", name); 
94         linenoiseFree(name);
95     }
96
97     return restore(fp);
98 }
99
100 int restore(FILE* fp)
101 {   /*  Read and restore game state from file, assuming
102      *  sane initial state.
103      *  If ADVENT_NOSAVE is defined, do nothing instead. */
104 #ifdef ADVENT_NOSAVE
105     return GO_UNKNOWN;
106 #endif
107
108     IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
109     fclose(fp);
110     if (save.version != VRSION) {
111         SETPRM(1,save.version/10,MOD(save.version,10));
112         SETPRM(3,VRSION/10,MOD(VRSION,10));
113         RSPEAK(VERSION_SKEW);
114     } else {
115         memcpy(&game, &save.game, sizeof(struct game_t));
116         OBJSND[BIRD] = save.bird;
117         OBJTXT[OYSTER] = save.bivalve;
118         game.zzword=RNDVOC(3,game.zzword);
119     }
120     return GO_TOP;
121 }
122
123 /* end */