Home | History | Annotate | Line # | Download | only in rogue
save.c revision 1.11
      1 /*	$NetBSD: save.c,v 1.11 2007/12/27 23:53:01 dholland 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.11 2007/12/27 23:53:01 dholland 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, sizeof(fname),
     68 			"game not saved", 0, 1)) {
     69 		return;
     70 	}
     71 	check_message();
     72 	messagef(0, "%s", fname);
     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 				messagef(0,
     93 					"out of memory for save file name");
     94 				sfile = error_file;
     95 			} else {
     96 				(void) strcpy(name_buffer, hptr);
     97 				(void) strcat(name_buffer, sfile+1);
     98 				sfile = name_buffer;
     99 			}
    100 			/*
    101 			 * Note: name_buffer gets leaked. But it's small,
    102 			 * and in the common case we're about to exit.
    103 			 */
    104 		}
    105 	}
    106 	if (((fp = fopen(sfile, "w")) == NULL) ||
    107 	    ((file_id = md_get_file_id(sfile)) == -1)) {
    108 		if (fp)
    109 			fclose(fp);
    110 		messagef(0, "problem accessing the save file");
    111 		return;
    112 	}
    113 	md_ignore_signals();
    114 	write_failed = 0;
    115 	(void) xxx(1);
    116 	r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
    117 	r_write(fp, (char *) &cur_level, sizeof(cur_level));
    118 	r_write(fp, (char *) &max_level, sizeof(max_level));
    119 	write_string(hunger_str, fp);
    120 	write_string(login_name, fp);
    121 	r_write(fp, (char *) &party_room, sizeof(party_room));
    122 	write_pack(&level_monsters, fp);
    123 	write_pack(&level_objects, fp);
    124 	r_write(fp, (char *) &file_id, sizeof(file_id));
    125 	rw_dungeon(fp, 1);
    126 	r_write(fp, (char *) &foods, sizeof(foods));
    127 	r_write(fp, (char *) &rogue, sizeof(fighter));
    128 	write_pack(&rogue.pack, fp);
    129 	rw_id(id_potions, fp, POTIONS, 1);
    130 	rw_id(id_scrolls, fp, SCROLS, 1);
    131 	rw_id(id_wands, fp, WANDS, 1);
    132 	rw_id(id_rings, fp, RINGS, 1);
    133 	r_write(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
    134 	r_write(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
    135 	r_write(fp, (char *) &cur_room, sizeof(cur_room));
    136 	rw_rooms(fp, 1);
    137 	r_write(fp, (char *) &being_held, sizeof(being_held));
    138 	r_write(fp, (char *) &bear_trap, sizeof(bear_trap));
    139 	r_write(fp, (char *) &halluc, sizeof(halluc));
    140 	r_write(fp, (char *) &blind, sizeof(blind));
    141 	r_write(fp, (char *) &confused, sizeof(confused));
    142 	r_write(fp, (char *) &levitate, sizeof(levitate));
    143 	r_write(fp, (char *) &haste_self, sizeof(haste_self));
    144 	r_write(fp, (char *) &see_invisible, sizeof(see_invisible));
    145 	r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
    146 	r_write(fp, (char *) &wizard, sizeof(wizard));
    147 	r_write(fp, (char *) &score_only, sizeof(score_only));
    148 	r_write(fp, (char *) &m_moves, sizeof(m_moves));
    149 	md_gct(&rt_buf);
    150 	rt_buf.second += 10;		/* allow for some processing time */
    151 	r_write(fp, (char *) &rt_buf, sizeof(rt_buf));
    152 	fclose(fp);
    153 
    154 	if (write_failed) {
    155 		(void) md_df(sfile);	/* delete file */
    156 	} else {
    157 		clean_up("");
    158 	}
    159 }
    160 
    161 void
    162 restore(fname)
    163 	const char *fname;
    164 {
    165 	FILE *fp;
    166 	struct rogue_time saved_time, mod_time;
    167 	char buf[4];
    168 	char tbuf[MAX_OPT_LEN];
    169 	int new_file_id, saved_file_id;
    170 
    171 	fp = NULL;
    172 	if (((new_file_id = md_get_file_id(fname)) == -1) ||
    173 	    ((fp = fopen(fname, "r")) == NULL)) {
    174 		clean_up("cannot open file");
    175 	}
    176 	if (md_link_count(fname) > 1) {
    177 		clean_up("file has link");
    178 	}
    179 	(void) xxx(1);
    180 	r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
    181 	r_read(fp, (char *) &cur_level, sizeof(cur_level));
    182 	r_read(fp, (char *) &max_level, sizeof(max_level));
    183 	read_string(hunger_str, fp, sizeof hunger_str);
    184 
    185 	(void) strlcpy(tbuf, login_name, sizeof tbuf);
    186 	read_string(login_name, fp, sizeof login_name);
    187 	if (strcmp(tbuf, login_name)) {
    188 		clean_up("you're not the original player");
    189 	}
    190 
    191 	r_read(fp, (char *) &party_room, sizeof(party_room));
    192 	read_pack(&level_monsters, fp, 0);
    193 	read_pack(&level_objects, fp, 0);
    194 	r_read(fp, (char *) &saved_file_id, sizeof(saved_file_id));
    195 	if (new_file_id != saved_file_id) {
    196 		clean_up("sorry, saved game is not in the same file");
    197 	}
    198 	rw_dungeon(fp, 0);
    199 	r_read(fp, (char *) &foods, sizeof(foods));
    200 	r_read(fp, (char *) &rogue, sizeof(fighter));
    201 	read_pack(&rogue.pack, fp, 1);
    202 	rw_id(id_potions, fp, POTIONS, 0);
    203 	rw_id(id_scrolls, fp, SCROLS, 0);
    204 	rw_id(id_wands, fp, WANDS, 0);
    205 	rw_id(id_rings, fp, RINGS, 0);
    206 	r_read(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
    207 	r_read(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
    208 	r_read(fp, (char *) &cur_room, sizeof(cur_room));
    209 	rw_rooms(fp, 0);
    210 	r_read(fp, (char *) &being_held, sizeof(being_held));
    211 	r_read(fp, (char *) &bear_trap, sizeof(bear_trap));
    212 	r_read(fp, (char *) &halluc, sizeof(halluc));
    213 	r_read(fp, (char *) &blind, sizeof(blind));
    214 	r_read(fp, (char *) &confused, sizeof(confused));
    215 	r_read(fp, (char *) &levitate, sizeof(levitate));
    216 	r_read(fp, (char *) &haste_self, sizeof(haste_self));
    217 	r_read(fp, (char *) &see_invisible, sizeof(see_invisible));
    218 	r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
    219 	r_read(fp, (char *) &wizard, sizeof(wizard));
    220 	r_read(fp, (char *) &score_only, sizeof(score_only));
    221 	r_read(fp, (char *) &m_moves, sizeof(m_moves));
    222 	r_read(fp, (char *) &saved_time, sizeof(saved_time));
    223 
    224 	if (fread(buf, sizeof(char), 1, fp) > 0) {
    225 		clear();
    226 		clean_up("extra characters in file");
    227 	}
    228 
    229 	md_gfmt(fname, &mod_time);	/* get file modification time */
    230 
    231 	if (has_been_touched(&saved_time, &mod_time)) {
    232 		clear();
    233 		clean_up("sorry, file has been touched");
    234 	}
    235 	if ((!wizard) && !md_df(fname)) {
    236 		clean_up("cannot delete file");
    237 	}
    238 	msg_cleared = 0;
    239 	ring_stats(0);
    240 	fclose(fp);
    241 }
    242 
    243 void
    244 write_pack(pack, fp)
    245 	const object *pack;
    246 	FILE *fp;
    247 {
    248 	object t;
    249 
    250 	while ((pack = pack->next_object) != NULL) {
    251 		r_write(fp, (const char *) pack, sizeof(object));
    252 	}
    253 	t.ichar = t.what_is = 0;
    254 	r_write(fp, (const char *) &t, sizeof(object));
    255 }
    256 
    257 void
    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 void
    289 rw_dungeon(fp, rw)
    290 	FILE *fp;
    291 	boolean rw;
    292 {
    293 	short i, j;
    294 	char buf[DCOLS];
    295 
    296 	for (i = 0; i < DROWS; i++) {
    297 		if (rw) {
    298 			r_write(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
    299 			for (j = 0; j < DCOLS; j++) {
    300 				buf[j] = mvinch(i, j);
    301 			}
    302 			r_write(fp, buf, DCOLS);
    303 		} else {
    304 			r_read(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
    305 			r_read(fp, buf, DCOLS);
    306 			for (j = 0; j < DCOLS; j++) {
    307 				mvaddch(i, j, buf[j]);
    308 			}
    309 		}
    310 	}
    311 }
    312 
    313 void
    314 rw_id(id_table, fp, n, wr)
    315 	struct id id_table[];
    316 	FILE *fp;
    317 	int n;
    318 	boolean wr;
    319 {
    320 	short i;
    321 
    322 	for (i = 0; i < n; i++) {
    323 		if (wr) {
    324 			r_write(fp, (const char *) &(id_table[i].value), sizeof(short));
    325 			r_write(fp, (const char *) &(id_table[i].id_status),
    326 				sizeof(unsigned short));
    327 			write_string(id_table[i].title, fp);
    328 		} else {
    329 			r_read(fp, (char *) &(id_table[i].value), sizeof(short));
    330 			r_read(fp, (char *) &(id_table[i].id_status),
    331 				sizeof(unsigned short));
    332 			read_string(id_table[i].title, fp, MAX_ID_TITLE_LEN);
    333 		}
    334 	}
    335 }
    336 
    337 void
    338 write_string(s, fp)
    339 	char *s;
    340 	FILE *fp;
    341 {
    342 	short n;
    343 
    344 	n = strlen(s) + 1;
    345 	xxxx(s, n);
    346 	r_write(fp, (char *) &n, sizeof(short));
    347 	r_write(fp, s, n);
    348 }
    349 
    350 void
    351 read_string(s, fp, len)
    352 	char *s;
    353 	FILE *fp;
    354 	size_t len;
    355 {
    356 	short n;
    357 
    358 	r_read(fp, (char *) &n, sizeof(short));
    359 	if (n<=0 || (size_t)(unsigned short)n > len) {
    360 		clean_up("read_string: corrupt game file");
    361 	}
    362 	r_read(fp, s, n);
    363 	xxxx(s, n);
    364 	/* ensure null termination */
    365 	s[n-1] = 0;
    366 }
    367 
    368 void
    369 rw_rooms(fp, rw)
    370 	FILE *fp;
    371 	boolean rw;
    372 {
    373 	short i;
    374 
    375 	for (i = 0; i < MAXROOMS; i++) {
    376 		rw ? r_write(fp, (char *) (rooms + i), sizeof(room)) :
    377 			r_read(fp, (char *) (rooms + i), sizeof(room));
    378 	}
    379 }
    380 
    381 void
    382 r_read(fp, buf, n)
    383 	FILE *fp;
    384 	char *buf;
    385 	int n;
    386 {
    387 	if (fread(buf, sizeof(char), n, fp) != (size_t)n) {
    388 		clean_up("read() failed, don't know why");
    389 	}
    390 }
    391 
    392 void
    393 r_write(fp, buf, n)
    394 	FILE *fp;
    395 	const char *buf;
    396 	int n;
    397 {
    398 	if (!write_failed) {
    399 		if (fwrite(buf, sizeof(char), n, fp) != (size_t)n) {
    400 			messagef(0, "write() failed, don't know why");
    401 			sound_bell();
    402 			write_failed = 1;
    403 		}
    404 	}
    405 }
    406 
    407 boolean
    408 has_been_touched(saved_time, mod_time)
    409 	const struct rogue_time *saved_time, *mod_time;
    410 {
    411 	if (saved_time->year < mod_time->year) {
    412 		return(1);
    413 	} else if (saved_time->year > mod_time->year) {
    414 		return(0);
    415 	}
    416 	if (saved_time->month < mod_time->month) {
    417 		return(1);
    418 	} else if (saved_time->month > mod_time->month) {
    419 		return(0);
    420 	}
    421 	if (saved_time->day < mod_time->day) {
    422 		return(1);
    423 	} else if (saved_time->day > mod_time->day) {
    424 		return(0);
    425 	}
    426 	if (saved_time->hour < mod_time->hour) {
    427 		return(1);
    428 	} else if (saved_time->hour > mod_time->hour) {
    429 		return(0);
    430 	}
    431 	if (saved_time->minute < mod_time->minute) {
    432 		return(1);
    433 	} else if (saved_time->minute > mod_time->minute) {
    434 		return(0);
    435 	}
    436 	if (saved_time->second < mod_time->second) {
    437 		return(1);
    438 	}
    439 	return(0);
    440 }
    441