-
Notifications
You must be signed in to change notification settings - Fork 4
/
save.c
213 lines (195 loc) · 5.43 KB
/
save.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* File save.c */
#include "save.h"
#include "wand_head.h"
#include "display.h"
#include "edit.h"
#include "encrypt.h"
#include "monsters.h"
#include <curses.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
extern char screen[NOOFROWS][ROWLEN + 1];
extern int saved_game;
extern char screen_name[ROWLEN + 1];
struct saved_game
{
short num;
long score;
short bell;
short maxmoves;
short num_monsters;
};
struct save_vars zz;
extern struct mon_rec start_of_list, *tail_of_list;
/*********************************************************
* function save_game *
**********************************************************/
void save_game(int num, long *score, int *bell, int maxmoves)
{
char fname[128], buf[70], *fp;
FILE *fo;
struct saved_game s;
struct mon_rec *mp;
if ((char *) NULL == (fp = getenv("SAVENAME")))
{
move(20, 0);
addstr("Saving: Filename? ");
refresh();
readstring(fname, 127);
fp = fname;
}
move(20, 0);
printw("%77s", "");
move(20, 0);
refresh();
if ((FILE *) NULL == (fo = fopen(fp, W_BIN)))
{
perror(fp);
return;
}
s.num = num;
s.score = *score;
s.bell = *bell;
s.maxmoves = maxmoves;
s.num_monsters = 0;
mp = &start_of_list; /* first entry is dummy */
while (mp != tail_of_list)
{
mp = mp->next;
s.num_monsters++; /* count them monsters */
}
if ((1 != fwrite((char *) &s, sizeof(s), 1, fo))
|| (1 != fwrite((char *) screen, sizeof(screen), 1, fo))
|| (1 != fwrite((char *) &zz, sizeof(zz), 1, fo)))
{
sprintf(buf, "Write error on '%s'\n", fname);
inform_me(buf, 0);
fclose(fo);
unlink(fname);
return;
}
mp = &start_of_list;
while (mp != tail_of_list)
{
/* save them monsters */
mp = mp->next;
if (1 != fwrite((char *) mp, sizeof(struct mon_rec), 1, fo))
{
sprintf(buf, "Write error on '%s'\n", fname);
inform_me(buf, 0);
fclose(fo);
unlink(fname);
return;
}
}
fwrite(screen_name, sizeof(char), strlen(screen_name), fo);
fclose(fo);
#ifndef NO_ENCRYPTION
crypt_file(fp); /* encrpyt the saved game */
#endif
clear();
CBON;
echo();
refresh();
endwin();
printf("Game saved.\n\nWanderer Copyright (C) 1988 S Shipway\n\n");
exit(0);
}
/*************************************************
* function restore_game *
**************************************************/
void restore_game(int *num, long *score, int *bell, int *maxmoves)
{
FILE *fi;
struct saved_game s;
struct mon_rec *mp, *tmp, tmp_monst;
char fname[128], *fp;
if ((char *) NULL == (fp = getenv("SAVENAME")))
{
move((LINES - 1), 0);
addstr("Restore Filename ? ");
refresh();
readstring(fname, 127);
fp = fname;
}
clear();
refresh();
#ifndef NO_ENCRYPTION
crypt_file(fp); /* decrypt it */
#endif
if ((FILE *) NULL == (fi = fopen(fp, R_BIN)))
{
endwin();
printf("Open error on '%s'\n", fp);
printf("Cannot restore game --- sorry.\n");
exit(1);
}
if ((1 != fread((char *) &s, sizeof(s), 1, fi))
|| (1 != fread((char *) screen, sizeof(screen), 1, fi))
|| (1 != fread((char *) &zz, sizeof(zz), 1, fi)))
{
endwin();
printf("Read error on '%s'n", fp);
printf("Cannot restore game --- sorry.\n");
fclose(fi);
exit(1);
}
*num = s.num;
*score = s.score;
*bell = s.bell;
*maxmoves = s.maxmoves;
/* free any monsters already on chain, to start clean */
mp = start_of_list.next;
while ((mp != NULL) && (mp != &start_of_list))
{
/* free them monsters */
tmp = mp;
mp = mp->next;
free(tmp);
}
/* re-initialize the monster list */
/* start_of_list = {0,0,0,0,0,NULL,NULL}; */
start_of_list.x = 0;
start_of_list.y = 0;
start_of_list.mx = 0;
start_of_list.my = 0;
start_of_list.under = 0;
start_of_list.next = (struct mon_rec *) NULL;
start_of_list.prev = (struct mon_rec *) NULL;
tail_of_list = &start_of_list;
while (s.num_monsters--)
{
/* use make_monster to allocate the monster structures */
/* to get all the linking right without even trying */
if ((struct mon_rec *) NULL == (mp = make_monster(0, 0)))
{
endwin();
printf("Monster alloc error on '%s'n", fp);
printf("Try again - it might work.\nBut then,pigs might fly...\n");
fclose(fi);
exit(1);
}
if (1 != fread((char *) &tmp_monst, sizeof(struct mon_rec), 1, fi))
{
endwin();
printf("Monster read error on '%s'\n", fp);
printf("Cannot restore game --- sorry.\n");
fclose(fi);
exit(1);
}
/* copy info without trashing links */
mp->x = tmp_monst.x;
mp->y = tmp_monst.y;
mp->mx = tmp_monst.mx;
mp->my = tmp_monst.my;
mp->under = tmp_monst.under;
}
if (fgets(screen_name, ROWLEN, fi) == NULL)
*screen_name = '#';
fclose(fi);
unlink(fp);
saved_game = 1;
}