Lowering the scope And cleaning up some warnings from static analysis
[open-adventure.git] / saveresume.c
1 #include <stdlib.h>
2 #include <string.h>
3
4 #include "advent.h"
5 #include "database.h"
6 #include "linenoise/linenoise.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     long bird;
27     long bivalve;
28 };
29 struct save_t save;
30
31 int saveresume(FILE *input, bool resume)
32 /* Suspend and resume */
33 {
34     long i, k;
35     FILE *fp = NULL;
36      
37     if (!resume) {
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(260);
42         if (!YES(input,200,54,54)) return GO_CLEAROBJ;
43         game.saved=game.saved+5;
44     }
45     else
46     {
47         /*  Resume.  Read a suspended game back from a file. */
48         if (game.loc != 1 || game.abbrev[1] != 1) {
49             RSPEAK(268);
50             if (!YES(input,200,54,54)) return GO_CLEAROBJ;
51         }
52     }
53
54     while (fp == NULL) {
55         char* name = linenoise("\nFile name: ");
56         if (name == NULL)
57             return GO_TOP;
58         fp = fopen(name,(resume ? READ_MODE : WRITE_MODE));
59         if (fp == NULL)
60             printf("Can't open file %s, try again.\n", name); 
61         linenoiseFree(name);
62     }
63     
64     DATIME(&i,&k);
65     k=i+650*k;
66     if (!resume)
67     {
68         save.savetime = k;
69         save.mode = -1;
70         save.version = VRSION;
71         memcpy(&save.game, &game, sizeof(struct game_t));
72         save.bird = OBJSND[BIRD];
73         save.bivalve = OBJTXT[OYSTER];
74         IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
75         fclose(fp);
76         RSPEAK(266);
77         exit(0);
78     } else {
79         IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
80         fclose(fp);
81         if (save.version != VRSION) {
82             SETPRM(1,k/10,MOD(k,10));
83             SETPRM(3,VRSION/10,MOD(VRSION,10));
84             RSPEAK(269);
85         } else {
86             memcpy(&game, &save.game, sizeof(struct game_t));
87             OBJSND[BIRD] = save.bird;
88             OBJTXT[OYSTER] = save.bivalve;
89             game.zzword=RNDVOC(3,game.zzword);
90         }
91         return GO_TOP;
92     }
93 }
94
95 /* end */