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