centralize calls to make_zzword()
[open-adventure.git] / saveresume.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <editline/readline.h>
4
5 #include "advent.h"
6 #include "dungeon.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 #define IGNORE(r) do{if (r){}}while(0)
30
31 int savefile(FILE *fp, long version)
32 /* Save game to file. No input or output from user. */
33 {
34     long i, k;
35     datime(&i, &k);
36     k = i + 650 * k;
37     save.savetime = k;
38     save.mode = -1;
39
40     save.version = (version == 0) ? VRSION : version;
41
42     memcpy(&save.game, &game, sizeof(struct game_t));
43     IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
44     return (0);
45 }
46
47 /* Suspend and resume */
48 int suspend(void)
49 {
50     /*  Suspend.  Offer to save things in a file, but charging
51      *  some points (so can't win by using saved games to retry
52      *  battles or to start over after learning zzword).
53      *  If ADVENT_NOSAVE is defined, do nothing instead. */
54
55 #ifdef ADVENT_NOSAVE
56     return GO_UNKNOWN;
57 #endif
58     FILE *fp = NULL;
59
60     rspeak(SUSPEND_WARNING);
61     if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
62         return GO_CLEAROBJ;
63     game.saved = game.saved + 5;
64
65     while (fp == NULL) {
66         char* name = readline("\nFile name: ");
67         if (name == NULL)
68             return GO_TOP;
69         fp = fopen(name, WRITE_MODE);
70         if (fp == NULL)
71             printf("Can't open file %s, try again.\n", name);
72         free(name);
73     }
74
75     savefile(fp, VRSION);
76     fclose(fp);
77     rspeak(RESUME_HELP);
78     exit(EXIT_SUCCESS);
79 }
80
81 int resume(void)
82 {
83     /*  Resume.  Read a suspended game back from a file.
84      *  If ADVENT_NOSAVE is defined, do nothing instead. */
85
86 #ifdef ADVENT_NOSAVE
87     return GO_UNKNOWN;
88 #endif
89     FILE *fp = NULL;
90
91     if (game.loc != 1 || game.abbrev[1] != 1) {
92         rspeak(RESUME_ABANDON);
93         if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
94             return GO_CLEAROBJ;
95     }
96
97     while (fp == NULL) {
98         char* name = readline("\nFile name: ");
99         if (name == NULL)
100             return GO_TOP;
101         fp = fopen(name, READ_MODE);
102         if (fp == NULL)
103             printf("Can't open file %s, try again.\n", name);
104         free(name);
105     }
106
107     return restore(fp);
108 }
109
110 int restore(FILE* fp)
111 {
112     /*  Read and restore game state from file, assuming
113      *  sane initial state.
114      *  If ADVENT_NOSAVE is defined, do nothing instead. */
115 #ifdef ADVENT_NOSAVE
116     return GO_UNKNOWN;
117 #endif
118
119     IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
120     fclose(fp);
121     if (save.version != VRSION) {
122         rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
123     } else {
124         memcpy(&game, &save.game, sizeof(struct game_t));
125     }
126     return GO_TOP;
127 }
128
129 /* end */