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