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