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