1 /* File save.c */
2 /***************************************************************************
3 * Copyright 2003 - Steven Shipway <steve@cheshire.demon.co.uk> *
4 * Put "nospam" in subject to avoid spam filter *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA *
19 * 02111-1307, USA. *
20 ***************************************************************************/
21
22
23 #include "wand_head.h"
24 #include <errno.h>
25
26 extern char screen[NOOFROWS][ROWLEN+1];
27 extern int saved_game;
28 extern char screen_name[ROWLEN+1];
29 extern void crypt_file();
30 extern int inform_me();
31 extern void readstring();
32
33 struct saved_game
34 {
35 short num;
36 long score;
37 short bell;
38 short maxmoves;
39 short num_monsters;
40 };
41
42 struct save_vars zz;
43
44 extern struct mon_rec start_of_list, *tail_of_list;
45
46 /*********************************************************
47 * function save_game *
48 **********************************************************/
save_game(num,score,bell,maxmoves)49 void save_game(num, score, bell, maxmoves)
50 int num, *bell, maxmoves;
51 long *score;
52 {
53 char fname[128], buf[70], *fp;
54 FILE *fo;
55 struct saved_game s;
56 extern char *getenv();
57 struct mon_rec *mp;
58
59 if ((char *)NULL == (fp = getenv("SAVENAME")))
60 {
61 move(20,0);
62 addstr("Saving: Filename? ");
63 refresh();
64 readstring(fname,127);
65 fp = fname;
66 }
67 move(20,0);
68 addstr(" ");
69 move(20,0);
70 refresh();
71 if ((FILE *)NULL == (fo = fopen(fp, W_BIN)))
72 {
73 perror(fp);
74 return;
75 }
76
77 s.num = num;
78 s.score = *score;
79 s.bell = *bell;
80 s.maxmoves = maxmoves;
81 s.num_monsters = 0;
82
83 mp = &start_of_list; /* first entry is dummy */
84 while (mp != tail_of_list)
85 {
86 mp = mp->next;
87 s.num_monsters++; /* count them monsters */
88 }
89
90 if ( (1 != fwrite((char *)&s, sizeof(s), 1, fo)) ||
91 (1 != fwrite((char *)screen, sizeof(screen), 1, fo)) ||
92 (1 != fwrite((char *)&zz, sizeof(zz), 1, fo)) )
93 {
94 sprintf(buf,"Write error on '%s'\n", fname);
95 inform_me(buf,0);
96 fclose(fo);
97 unlink(fname);
98 return;
99 }
100
101 mp = &start_of_list;
102 while (mp != tail_of_list)
103 {
104 /* save them monsters */
105 mp = mp->next;
106 if(1 != fwrite((char *)mp, sizeof(struct mon_rec), 1, fo))
107 {
108 sprintf(buf,"Write error on '%s'\n", fname);
109 inform_me(buf);
110 fclose(fo);
111 unlink(fname);
112 return;
113 }
114 }
115 fwrite(screen_name,sizeof(char),strlen(screen_name),fo);
116 fclose(fo);
117 #ifndef NO_ENCRYPTION
118 crypt_file(fp,0); /* encrpyt the saved game */
119 #endif
120 clear();
121 CBON;
122 echo();
123 refresh();
124 endwin();
125 printf("Game saved.\n\nWanderer Copyright (C) 1988 S Shipway\n\n");
126 exit(0);
127 }
128
129 /*************************************************
130 * function restore_game *
131 **************************************************/
restore_game(num,score,bell,maxmoves)132 void restore_game(num, score, bell, maxmoves)
133 int *num, *score, *bell, *maxmoves;
134 {
135 FILE *fi;
136 struct saved_game s;
137 struct mon_rec *mp, *tmp, tmp_monst;
138 char fname[128], *fp;
139 char *m_terminate = NULL;
140 FILE *fo;
141 extern char *getenv();
142
143 if ((char *)NULL == (fp = getenv("SAVENAME")))
144 {
145 move((LINES-1),0);
146 addstr("Restore Filename ? ");
147 refresh();
148 echo(); CBOFF;
149 fp = fname;
150 fgets(fp,sizeof(fname),stdin); /* Marina Brown */
151 m_terminate=strchr(fp,'\n');
152 m_terminate="\0"; /* End Marina delta */
153 CBON; noecho();
154 }
155 clear();
156 refresh();
157 #ifndef NO_ENCRYPTION
158 crypt_file(fp,1); /* decrypt it */
159 #endif
160 if ((FILE *)NULL == (fi = fopen(fp, R_BIN)))
161 {
162 endwin();
163 printf("Open error on '%s'\n", fp);
164 printf("Cannot restore game --- sorry.\n");
165 exit(1);
166 }
167 if ( (1 != fread((char *)&s, sizeof(s), 1, fi)) ||
168 (1 != fread((char *)screen, sizeof(screen), 1, fi)) ||
169 (1 != fread((char *)&zz, sizeof(zz), 1, fi)) )
170 {
171 endwin();
172 printf("Read error on '%s'n", fp);
173 printf("Cannot restore game --- sorry.\n");
174 fclose(fi);
175 exit(1);
176 }
177
178 *num = s.num;
179 *score = s.score;
180 *bell = s.bell;
181 *maxmoves = s.maxmoves;
182
183 /* free any monsters already on chain, to start clean */
184 mp = start_of_list.next;
185 while ((mp != NULL) && (mp != &start_of_list))
186 {
187 /* free them monsters */
188 tmp = mp;
189 mp = mp->next;
190 free(tmp);
191 }
192
193 /* re-initialize the monster list */
194 /* start_of_list = {0,0,0,0,0,NULL,NULL}; */
195 start_of_list.x = 0;
196 start_of_list.y = 0;
197 start_of_list.mx = 0;
198 start_of_list.my = 0;
199 start_of_list.under = 0;
200 start_of_list.next = (struct mon_rec *)NULL;
201 start_of_list.prev = (struct mon_rec *)NULL;
202
203 tail_of_list = &start_of_list;
204
205 while(s.num_monsters--)
206 {
207 /* use make_monster to allocate the monster structures */
208 /* to get all the linking right without even trying */
209 if ((struct mon_rec *)NULL == (mp = make_monster(0, 0)))
210 {
211 endwin();
212 printf("Monster alloc error on '%s'n", fp);
213 printf("Try again - it might work.\nBut then,pigs might fly...\n");
214 fclose(fi);
215 exit(1);
216 }
217 if(1 != fread((char *)&tmp_monst, sizeof(struct mon_rec),1,fi))
218 {
219 endwin();
220 printf("Monster read error on '%s'\n", fp);
221 printf("Cannot restore game --- sorry.\n");
222 fclose(fi);
223 exit(1);
224 }
225 /* copy info without trashing links */
226 mp->x = tmp_monst.x;
227 mp->y = tmp_monst.y;
228 mp->mx = tmp_monst.mx;
229 mp->my = tmp_monst.my;
230 mp->under = tmp_monst.under;
231 }
232 if(fgets(screen_name,ROWLEN,fi) == NULL)
233 *screen_name = '#';
234 fclose(fi);
235 unlink(fp);
236 saved_game = 1;
237 }
238