Abolish unclean operation on OBJSOUND - it's now static.
[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.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(void)
74 {
75     /*  Resume.  Read a suspended game back from a file.
76      *  If ADVENT_NOSAVE is defined, do nothing instead. */
77
78 #ifdef ADVENT_NOSAVE
79     return GO_UNKNOWN;
80 #endif
81     FILE *fp = NULL;
82
83     if (game.loc != 1 || game.abbrev[1] != 1) {
84         rspeak(RESUME_ABANDON);
85         if (!YES(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN])) return GO_CLEAROBJ;
86     }
87
88     while (fp == NULL) {
89         char* name = linenoise("\nFile name: ");
90         if (name == NULL)
91             return GO_TOP;
92         fp = fopen(name, READ_MODE);
93         if (fp == NULL)
94             printf("Can't open file %s, try again.\n", name);
95         linenoiseFree(name);
96     }
97
98     return restore(fp);
99 }
100
101 int restore(FILE* fp)
102 {
103     /*  Read and restore game state from file, assuming
104      *  sane initial state.
105      *  If ADVENT_NOSAVE is defined, do nothing instead. */
106 #ifdef ADVENT_NOSAVE
107     return GO_UNKNOWN;
108 #endif
109
110     IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
111     fclose(fp);
112     if (save.version != VRSION) {
113         rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
114     } else {
115         memcpy(&game, &save.game, sizeof(struct game_t));
116         OBJTXT[OYSTER] = save.bivalve;
117         game.zzword = RNDVOC(3, game.zzword);
118     }
119     return GO_TOP;
120 }
121
122 /* end */