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