Change YES() to take const char* arguments.
[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(void)
34 {
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. */
39
40 #ifdef ADVENT_NOSAVE
41     return GO_UNKNOWN;
42 #endif
43     long i, k;
44     FILE *fp = NULL;
45
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;
49
50     while (fp == NULL) {
51         char* name = linenoise("\nFile name: ");
52         if (name == NULL)
53             return GO_TOP;
54         fp = fopen(name, WRITE_MODE);
55         if (fp == NULL)
56             printf("Can't open file %s, try again.\n", name);
57         linenoiseFree(name);
58     }
59
60     DATIME(&i, &k);
61     k = i + 650 * k;
62     save.savetime = k;
63     save.mode = -1;
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));
69     fclose(fp);
70     RSPEAK(RESUME_HELP);
71     exit(0);
72 }
73
74 int resume(void)
75 {
76     /*  Resume.  Read a suspended game back from a file.
77      *  If ADVENT_NOSAVE is defined, do nothing instead. */
78
79 #ifdef ADVENT_NOSAVE
80     return GO_UNKNOWN;
81 #endif
82     FILE *fp = NULL;
83
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;
87     }
88
89     while (fp == NULL) {
90         char* name = linenoise("\nFile name: ");
91         if (name == NULL)
92             return GO_TOP;
93         fp = fopen(name, READ_MODE);
94         if (fp == NULL)
95             printf("Can't open file %s, try again.\n", name);
96         linenoiseFree(name);
97     }
98
99     return restore(fp);
100 }
101
102 int restore(FILE* fp)
103 {
104     /*  Read and restore game state from file, assuming
105      *  sane initial state.
106      *  If ADVENT_NOSAVE is defined, do nothing instead. */
107 #ifdef ADVENT_NOSAVE
108     return GO_UNKNOWN;
109 #endif
110
111     IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
112     fclose(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);
117     } else {
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);
122     }
123     return GO_TOP;
124 }
125
126 /* end */