Improved docs, fixed identation
[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(FILE *input)
34 {
35     long i, k;
36     FILE *fp = NULL;
37
38     /*  Suspend.  Offer to save things in a file, but charging
39      *  some points (so can't win by using saved games to retry
40      *  battles or to start over after learning zzword). */
41     RSPEAK(SUSPEND_WARNING);
42     if (!YES(input,THIS_ACCEPTABLE,OK_MAN,OK_MAN)) return GO_CLEAROBJ;
43     game.saved=game.saved+5;
44
45     while (fp == NULL) {
46         char* name = linenoise("\nFile name: ");
47         if (name == NULL)
48             return GO_TOP;
49         fp = fopen(name, WRITE_MODE);
50         if (fp == NULL)
51             printf("Can't open file %s, try again.\n", name); 
52         linenoiseFree(name);
53     }
54
55     DATIME(&i,&k);
56     k=i+650*k;
57     save.savetime = k;
58     save.mode = -1;
59     save.version = VRSION;
60     memcpy(&save.game, &game, sizeof(struct game_t));
61     save.bird = OBJSND[BIRD];
62     save.bivalve = OBJTXT[OYSTER];
63     IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
64     fclose(fp);
65     RSPEAK(RESUME_HELP);
66     exit(0);
67 }
68
69 int resume(FILE *input)
70 {
71     FILE *fp = NULL;
72      
73     /*  Resume.  Read a suspended game back from a file. */
74     if (game.loc != 1 || game.abbrev[1] != 1) {
75         RSPEAK(RESUME_ABANDON);
76         if (!YES(input,THIS_ACCEPTABLE,OK_MAN,OK_MAN)) return GO_CLEAROBJ;
77     }
78
79     while (fp == NULL) {
80         char* name = linenoise("\nFile name: ");
81         if (name == NULL)
82             return GO_TOP;
83         fp = fopen(name, READ_MODE);
84         if (fp == NULL)
85             printf("Can't open file %s, try again.\n", name); 
86         linenoiseFree(name);
87     }
88
89         return restore(fp);
90 }
91
92 int restore(FILE* fp){
93     IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
94     fclose(fp);
95     if (save.version != VRSION) {
96         SETPRM(1,save.version/10,MOD(save.version,10));
97         SETPRM(3,VRSION/10,MOD(VRSION,10));
98         RSPEAK(VERSION_SKEW);
99     } else {
100         memcpy(&game, &save.game, sizeof(struct game_t));
101         OBJSND[BIRD] = save.bird;
102         OBJTXT[OYSTER] = save.bivalve;
103         game.zzword=RNDVOC(3,game.zzword);
104     }
105     return GO_TOP;
106 }
107
108 /* end */