xref: /netbsd-src/games/rogue/save.c (revision 0b9f50897e9a9c6709320fafb4c3787fddcc0a45)
1 /*
2  * Copyright (c) 1988 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Timothy C. Stoehr.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)save.c	5.3 (Berkeley) 6/1/90";*/
39 static char rcsid[] = "$Id: save.c,v 1.2 1993/08/01 18:52:14 mycroft Exp $";
40 #endif /* not lint */
41 
42 /*
43  * save.c
44  *
45  * This source herein may be modified and/or distributed by anybody who
46  * so desires, with the following restrictions:
47  *    1.)  No portion of this notice shall be removed.
48  *    2.)  Credit shall not be taken for the creation of this source.
49  *    3.)  This code is not to be traded, sold, or used for personal
50  *         gain or profit.
51  *
52  */
53 
54 #include <stdio.h>
55 #include "rogue.h"
56 
57 short write_failed = 0;
58 char *save_file = (char *) 0;
59 
60 extern boolean detect_monster;
61 extern short cur_level, max_level;
62 extern char hunger_str[];
63 extern char login_name[];
64 extern short party_room;
65 extern short foods;
66 extern boolean is_wood[];
67 extern short cur_room;
68 extern boolean being_held;
69 extern short bear_trap;
70 extern short halluc;
71 extern short blind;
72 extern short confused;
73 extern short levitate;
74 extern short haste_self;
75 extern boolean see_invisible;
76 extern boolean detect_monster;
77 extern boolean wizard;
78 extern boolean score_only;
79 extern short m_moves;
80 
81 extern boolean msg_cleared;
82 
83 save_game()
84 {
85 	char fname[64];
86 
87 	if (!get_input_line("file name?", save_file, fname, "game not saved",
88 			0, 1)) {
89 		return;
90 	}
91 	check_message();
92 	message(fname, 0);
93 	save_into_file(fname);
94 }
95 
96 save_into_file(sfile)
97 char *sfile;
98 {
99 	FILE *fp;
100 	int file_id;
101 	char name_buffer[80];
102 	char *hptr;
103 	struct rogue_time rt_buf;
104 
105 	if (sfile[0] == '~') {
106 		if (hptr = md_getenv("HOME")) {
107 			(void) strcpy(name_buffer, hptr);
108 			(void) strcat(name_buffer, sfile+1);
109 			sfile = name_buffer;
110 		}
111 	}
112 	if (	((fp = fopen(sfile, "w")) == NULL) ||
113 			((file_id = md_get_file_id(sfile)) == -1)) {
114 		message("problem accessing the save file", 0);
115 		return;
116 	}
117 	md_ignore_signals();
118 	write_failed = 0;
119 	(void) xxx(1);
120 	r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
121 	r_write(fp, (char *) &cur_level, sizeof(cur_level));
122 	r_write(fp, (char *) &max_level, sizeof(max_level));
123 	write_string(hunger_str, fp);
124 	write_string(login_name, fp);
125 	r_write(fp, (char *) &party_room, sizeof(party_room));
126 	write_pack(&level_monsters, fp);
127 	write_pack(&level_objects, fp);
128 	r_write(fp, (char *) &file_id, sizeof(file_id));
129 	rw_dungeon(fp, 1);
130 	r_write(fp, (char *) &foods, sizeof(foods));
131 	r_write(fp, (char *) &rogue, sizeof(fighter));
132 	write_pack(&rogue.pack, fp);
133 	rw_id(id_potions, fp, POTIONS, 1);
134 	rw_id(id_scrolls, fp, SCROLS, 1);
135 	rw_id(id_wands, fp, WANDS, 1);
136 	rw_id(id_rings, fp, RINGS, 1);
137 	r_write(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
138 	r_write(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
139 	r_write(fp, (char *) &cur_room, sizeof(cur_room));
140 	rw_rooms(fp, 1);
141 	r_write(fp, (char *) &being_held, sizeof(being_held));
142 	r_write(fp, (char *) &bear_trap, sizeof(bear_trap));
143 	r_write(fp, (char *) &halluc, sizeof(halluc));
144 	r_write(fp, (char *) &blind, sizeof(blind));
145 	r_write(fp, (char *) &confused, sizeof(confused));
146 	r_write(fp, (char *) &levitate, sizeof(levitate));
147 	r_write(fp, (char *) &haste_self, sizeof(haste_self));
148 	r_write(fp, (char *) &see_invisible, sizeof(see_invisible));
149 	r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
150 	r_write(fp, (char *) &wizard, sizeof(wizard));
151 	r_write(fp, (char *) &score_only, sizeof(score_only));
152 	r_write(fp, (char *) &m_moves, sizeof(m_moves));
153 	md_gct(&rt_buf);
154 	rt_buf.second += 10;		/* allow for some processing time */
155 	r_write(fp, (char *) &rt_buf, sizeof(rt_buf));
156 	fclose(fp);
157 
158 	if (write_failed) {
159 		(void) md_df(sfile);	/* delete file */
160 	} else {
161 		clean_up("");
162 	}
163 }
164 
165 restore(fname)
166 char *fname;
167 {
168 	FILE *fp;
169 	struct rogue_time saved_time, mod_time;
170 	char buf[4];
171 	char tbuf[40];
172 	int new_file_id, saved_file_id;
173 
174 	if (	((new_file_id = md_get_file_id(fname)) == -1) ||
175 			((fp = fopen(fname, "r")) == NULL)) {
176 		clean_up("cannot open file");
177 	}
178 	if (md_link_count(fname) > 1) {
179 		clean_up("file has link");
180 	}
181 	(void) xxx(1);
182 	r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
183 	r_read(fp, (char *) &cur_level, sizeof(cur_level));
184 	r_read(fp, (char *) &max_level, sizeof(max_level));
185 	read_string(hunger_str, fp);
186 
187 	(void) strcpy(tbuf, login_name);
188 	read_string(login_name, fp);
189 	if (strcmp(tbuf, login_name)) {
190 		clean_up("you're not the original player");
191 	}
192 
193 	r_read(fp, (char *) &party_room, sizeof(party_room));
194 	read_pack(&level_monsters, fp, 0);
195 	read_pack(&level_objects, fp, 0);
196 	r_read(fp, (char *) &saved_file_id, sizeof(saved_file_id));
197 	if (new_file_id != saved_file_id) {
198 		clean_up("sorry, saved game is not in the same file");
199 	}
200 	rw_dungeon(fp, 0);
201 	r_read(fp, (char *) &foods, sizeof(foods));
202 	r_read(fp, (char *) &rogue, sizeof(fighter));
203 	read_pack(&rogue.pack, fp, 1);
204 	rw_id(id_potions, fp, POTIONS, 0);
205 	rw_id(id_scrolls, fp, SCROLS, 0);
206 	rw_id(id_wands, fp, WANDS, 0);
207 	rw_id(id_rings, fp, RINGS, 0);
208 	r_read(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
209 	r_read(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
210 	r_read(fp, (char *) &cur_room, sizeof(cur_room));
211 	rw_rooms(fp, 0);
212 	r_read(fp, (char *) &being_held, sizeof(being_held));
213 	r_read(fp, (char *) &bear_trap, sizeof(bear_trap));
214 	r_read(fp, (char *) &halluc, sizeof(halluc));
215 	r_read(fp, (char *) &blind, sizeof(blind));
216 	r_read(fp, (char *) &confused, sizeof(confused));
217 	r_read(fp, (char *) &levitate, sizeof(levitate));
218 	r_read(fp, (char *) &haste_self, sizeof(haste_self));
219 	r_read(fp, (char *) &see_invisible, sizeof(see_invisible));
220 	r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
221 	r_read(fp, (char *) &wizard, sizeof(wizard));
222 	r_read(fp, (char *) &score_only, sizeof(score_only));
223 	r_read(fp, (char *) &m_moves, sizeof(m_moves));
224 	r_read(fp, (char *) &saved_time, sizeof(saved_time));
225 
226 	if (fread(buf, sizeof(char), 1, fp) > 0) {
227 		clear();
228 		clean_up("extra characters in file");
229 	}
230 
231 	md_gfmt(fname, &mod_time);	/* get file modification time */
232 
233 	if (has_been_touched(&saved_time, &mod_time)) {
234 		clear();
235 		clean_up("sorry, file has been touched");
236 	}
237 	if ((!wizard) && !md_df(fname)) {
238 		clean_up("cannot delete file");
239 	}
240 	msg_cleared = 0;
241 	ring_stats(0);
242 	fclose(fp);
243 }
244 
245 write_pack(pack, fp)
246 object *pack;
247 FILE *fp;
248 {
249 	object t;
250 
251 	while (pack = pack->next_object) {
252 		r_write(fp, (char *) pack, sizeof(object));
253 	}
254 	t.ichar = t.what_is = 0;
255 	r_write(fp, (char *) &t, sizeof(object));
256 }
257 
258 read_pack(pack, fp, is_rogue)
259 object *pack;
260 FILE *fp;
261 boolean is_rogue;
262 {
263 	object read_obj, *new_obj;
264 
265 	for (;;) {
266 		r_read(fp, (char *) &read_obj, sizeof(object));
267 		if (read_obj.ichar == 0) {
268 			pack->next_object = (object *) 0;
269 			break;
270 		}
271 		new_obj = alloc_object();
272 		*new_obj = read_obj;
273 		if (is_rogue) {
274 			if (new_obj->in_use_flags & BEING_WORN) {
275 					do_wear(new_obj);
276 			} else if (new_obj->in_use_flags & BEING_WIELDED) {
277 					do_wield(new_obj);
278 			} else if (new_obj->in_use_flags & (ON_EITHER_HAND)) {
279 				do_put_on(new_obj,
280 					((new_obj->in_use_flags & ON_LEFT_HAND) ? 1 : 0));
281 			}
282 		}
283 		pack->next_object = new_obj;
284 		pack = new_obj;
285 	}
286 }
287 
288 rw_dungeon(fp, rw)
289 FILE *fp;
290 boolean rw;
291 {
292 	short i, j;
293 	char buf[DCOLS];
294 
295 	for (i = 0; i < DROWS; i++) {
296 		if (rw) {
297 			r_write(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
298 			for (j = 0; j < DCOLS; j++) {
299 				buf[j] = mvinch(i, j);
300 			}
301 			r_write(fp, buf, DCOLS);
302 		} else {
303 			r_read(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
304 			r_read(fp, buf, DCOLS);
305 			for (j = 0; j < DCOLS; j++) {
306 				mvaddch(i, j, buf[j]);
307 			}
308 		}
309 	}
310 }
311 
312 rw_id(id_table, fp, n, wr)
313 struct id id_table[];
314 FILE *fp;
315 int n;
316 boolean wr;
317 {
318 	short i;
319 
320 	for (i = 0; i < n; i++) {
321 		if (wr) {
322 			r_write(fp, (char *) &(id_table[i].value), sizeof(short));
323 			r_write(fp, (char *) &(id_table[i].id_status),
324 				sizeof(unsigned short));
325 			write_string(id_table[i].title, fp);
326 		} else {
327 			r_read(fp, (char *) &(id_table[i].value), sizeof(short));
328 			r_read(fp, (char *) &(id_table[i].id_status),
329 				sizeof(unsigned short));
330 			read_string(id_table[i].title, fp);
331 		}
332 	}
333 }
334 
335 write_string(s, fp)
336 char *s;
337 FILE *fp;
338 {
339 	short n;
340 
341 	n = strlen(s) + 1;
342 	xxxx(s, n);
343 	r_write(fp, (char *) &n, sizeof(short));
344 	r_write(fp, s, n);
345 }
346 
347 read_string(s, fp)
348 char *s;
349 FILE *fp;
350 {
351 	short n;
352 
353 	r_read(fp, (char *) &n, sizeof(short));
354 	r_read(fp, s, n);
355 	xxxx(s, n);
356 }
357 
358 rw_rooms(fp, rw)
359 FILE *fp;
360 boolean rw;
361 {
362 	short i;
363 
364 	for (i = 0; i < MAXROOMS; i++) {
365 		rw ? r_write(fp, (char *) (rooms + i), sizeof(room)) :
366 			r_read(fp, (char *) (rooms + i), sizeof(room));
367 	}
368 }
369 
370 r_read(fp, buf, n)
371 FILE *fp;
372 char *buf;
373 int n;
374 {
375 	if (fread(buf, sizeof(char), n, fp) != n) {
376 		clean_up("read() failed, don't know why");
377 	}
378 }
379 
380 r_write(fp, buf, n)
381 FILE *fp;
382 char *buf;
383 int n;
384 {
385 	if (!write_failed) {
386 		if (fwrite(buf, sizeof(char), n, fp) != n) {
387 			message("write() failed, don't know why", 0);
388 			sound_bell();
389 			write_failed = 1;
390 		}
391 	}
392 }
393 
394 boolean
395 has_been_touched(saved_time, mod_time)
396 struct rogue_time *saved_time, *mod_time;
397 {
398 	if (saved_time->year < mod_time->year) {
399 		return(1);
400 	} else if (saved_time->year > mod_time->year) {
401 		return(0);
402 	}
403 	if (saved_time->month < mod_time->month) {
404 		return(1);
405 	} else if (saved_time->month > mod_time->month) {
406 		return(0);
407 	}
408 	if (saved_time->day < mod_time->day) {
409 		return(1);
410 	} else if (saved_time->day > mod_time->day) {
411 		return(0);
412 	}
413 	if (saved_time->hour < mod_time->hour) {
414 		return(1);
415 	} else if (saved_time->hour > mod_time->hour) {
416 		return(0);
417 	}
418 	if (saved_time->minute < mod_time->minute) {
419 		return(1);
420 	} else if (saved_time->minute > mod_time->minute) {
421 		return(0);
422 	}
423 	if (saved_time->second < mod_time->second) {
424 		return(1);
425 	}
426 	return(0);
427 }
428