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