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