Home | History | Annotate | Line # | Download | only in rogue
save.c revision 1.3
      1 /*	$NetBSD: save.c,v 1.3 1995/04/22 10:28:21 cgd 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 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)save.c	8.1 (Berkeley) 5/31/93";
     42 #else
     43 static char rcsid[] = "$NetBSD: save.c,v 1.3 1995/04/22 10:28:21 cgd Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 /*
     48  * save.c
     49  *
     50  * This source herein may be modified and/or distributed by anybody who
     51  * so desires, with the following restrictions:
     52  *    1.)  No portion of this notice shall be removed.
     53  *    2.)  Credit shall not be taken for the creation of this source.
     54  *    3.)  This code is not to be traded, sold, or used for personal
     55  *         gain or profit.
     56  *
     57  */
     58 
     59 #include <stdio.h>
     60 #include "rogue.h"
     61 
     62 short write_failed = 0;
     63 char *save_file = (char *) 0;
     64 
     65 extern boolean detect_monster;
     66 extern short cur_level, max_level;
     67 extern char hunger_str[];
     68 extern char login_name[];
     69 extern short party_room;
     70 extern short foods;
     71 extern boolean is_wood[];
     72 extern short cur_room;
     73 extern boolean being_held;
     74 extern short bear_trap;
     75 extern short halluc;
     76 extern short blind;
     77 extern short confused;
     78 extern short levitate;
     79 extern short haste_self;
     80 extern boolean see_invisible;
     81 extern boolean detect_monster;
     82 extern boolean wizard;
     83 extern boolean score_only;
     84 extern short m_moves;
     85 
     86 extern boolean msg_cleared;
     87 
     88 save_game()
     89 {
     90 	char fname[64];
     91 
     92 	if (!get_input_line("file name?", save_file, fname, "game not saved",
     93 			0, 1)) {
     94 		return;
     95 	}
     96 	check_message();
     97 	message(fname, 0);
     98 	save_into_file(fname);
     99 }
    100 
    101 save_into_file(sfile)
    102 char *sfile;
    103 {
    104 	FILE *fp;
    105 	int file_id;
    106 	char name_buffer[80];
    107 	char *hptr;
    108 	struct rogue_time rt_buf;
    109 
    110 	if (sfile[0] == '~') {
    111 		if (hptr = md_getenv("HOME")) {
    112 			(void) strcpy(name_buffer, hptr);
    113 			(void) strcat(name_buffer, sfile+1);
    114 			sfile = name_buffer;
    115 		}
    116 	}
    117 	if (	((fp = fopen(sfile, "w")) == NULL) ||
    118 			((file_id = md_get_file_id(sfile)) == -1)) {
    119 		message("problem accessing the save file", 0);
    120 		return;
    121 	}
    122 	md_ignore_signals();
    123 	write_failed = 0;
    124 	(void) xxx(1);
    125 	r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
    126 	r_write(fp, (char *) &cur_level, sizeof(cur_level));
    127 	r_write(fp, (char *) &max_level, sizeof(max_level));
    128 	write_string(hunger_str, fp);
    129 	write_string(login_name, fp);
    130 	r_write(fp, (char *) &party_room, sizeof(party_room));
    131 	write_pack(&level_monsters, fp);
    132 	write_pack(&level_objects, fp);
    133 	r_write(fp, (char *) &file_id, sizeof(file_id));
    134 	rw_dungeon(fp, 1);
    135 	r_write(fp, (char *) &foods, sizeof(foods));
    136 	r_write(fp, (char *) &rogue, sizeof(fighter));
    137 	write_pack(&rogue.pack, fp);
    138 	rw_id(id_potions, fp, POTIONS, 1);
    139 	rw_id(id_scrolls, fp, SCROLS, 1);
    140 	rw_id(id_wands, fp, WANDS, 1);
    141 	rw_id(id_rings, fp, RINGS, 1);
    142 	r_write(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
    143 	r_write(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
    144 	r_write(fp, (char *) &cur_room, sizeof(cur_room));
    145 	rw_rooms(fp, 1);
    146 	r_write(fp, (char *) &being_held, sizeof(being_held));
    147 	r_write(fp, (char *) &bear_trap, sizeof(bear_trap));
    148 	r_write(fp, (char *) &halluc, sizeof(halluc));
    149 	r_write(fp, (char *) &blind, sizeof(blind));
    150 	r_write(fp, (char *) &confused, sizeof(confused));
    151 	r_write(fp, (char *) &levitate, sizeof(levitate));
    152 	r_write(fp, (char *) &haste_self, sizeof(haste_self));
    153 	r_write(fp, (char *) &see_invisible, sizeof(see_invisible));
    154 	r_write(fp, (char *) &detect_monster, sizeof(detect_monster));
    155 	r_write(fp, (char *) &wizard, sizeof(wizard));
    156 	r_write(fp, (char *) &score_only, sizeof(score_only));
    157 	r_write(fp, (char *) &m_moves, sizeof(m_moves));
    158 	md_gct(&rt_buf);
    159 	rt_buf.second += 10;		/* allow for some processing time */
    160 	r_write(fp, (char *) &rt_buf, sizeof(rt_buf));
    161 	fclose(fp);
    162 
    163 	if (write_failed) {
    164 		(void) md_df(sfile);	/* delete file */
    165 	} else {
    166 		clean_up("");
    167 	}
    168 }
    169 
    170 restore(fname)
    171 char *fname;
    172 {
    173 	FILE *fp;
    174 	struct rogue_time saved_time, mod_time;
    175 	char buf[4];
    176 	char tbuf[40];
    177 	int new_file_id, saved_file_id;
    178 
    179 	if (	((new_file_id = md_get_file_id(fname)) == -1) ||
    180 			((fp = fopen(fname, "r")) == NULL)) {
    181 		clean_up("cannot open file");
    182 	}
    183 	if (md_link_count(fname) > 1) {
    184 		clean_up("file has link");
    185 	}
    186 	(void) xxx(1);
    187 	r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
    188 	r_read(fp, (char *) &cur_level, sizeof(cur_level));
    189 	r_read(fp, (char *) &max_level, sizeof(max_level));
    190 	read_string(hunger_str, fp);
    191 
    192 	(void) strcpy(tbuf, login_name);
    193 	read_string(login_name, fp);
    194 	if (strcmp(tbuf, login_name)) {
    195 		clean_up("you're not the original player");
    196 	}
    197 
    198 	r_read(fp, (char *) &party_room, sizeof(party_room));
    199 	read_pack(&level_monsters, fp, 0);
    200 	read_pack(&level_objects, fp, 0);
    201 	r_read(fp, (char *) &saved_file_id, sizeof(saved_file_id));
    202 	if (new_file_id != saved_file_id) {
    203 		clean_up("sorry, saved game is not in the same file");
    204 	}
    205 	rw_dungeon(fp, 0);
    206 	r_read(fp, (char *) &foods, sizeof(foods));
    207 	r_read(fp, (char *) &rogue, sizeof(fighter));
    208 	read_pack(&rogue.pack, fp, 1);
    209 	rw_id(id_potions, fp, POTIONS, 0);
    210 	rw_id(id_scrolls, fp, SCROLS, 0);
    211 	rw_id(id_wands, fp, WANDS, 0);
    212 	rw_id(id_rings, fp, RINGS, 0);
    213 	r_read(fp, (char *) traps, (MAX_TRAPS * sizeof(trap)));
    214 	r_read(fp, (char *) is_wood, (WANDS * sizeof(boolean)));
    215 	r_read(fp, (char *) &cur_room, sizeof(cur_room));
    216 	rw_rooms(fp, 0);
    217 	r_read(fp, (char *) &being_held, sizeof(being_held));
    218 	r_read(fp, (char *) &bear_trap, sizeof(bear_trap));
    219 	r_read(fp, (char *) &halluc, sizeof(halluc));
    220 	r_read(fp, (char *) &blind, sizeof(blind));
    221 	r_read(fp, (char *) &confused, sizeof(confused));
    222 	r_read(fp, (char *) &levitate, sizeof(levitate));
    223 	r_read(fp, (char *) &haste_self, sizeof(haste_self));
    224 	r_read(fp, (char *) &see_invisible, sizeof(see_invisible));
    225 	r_read(fp, (char *) &detect_monster, sizeof(detect_monster));
    226 	r_read(fp, (char *) &wizard, sizeof(wizard));
    227 	r_read(fp, (char *) &score_only, sizeof(score_only));
    228 	r_read(fp, (char *) &m_moves, sizeof(m_moves));
    229 	r_read(fp, (char *) &saved_time, sizeof(saved_time));
    230 
    231 	if (fread(buf, sizeof(char), 1, fp) > 0) {
    232 		clear();
    233 		clean_up("extra characters in file");
    234 	}
    235 
    236 	md_gfmt(fname, &mod_time);	/* get file modification time */
    237 
    238 	if (has_been_touched(&saved_time, &mod_time)) {
    239 		clear();
    240 		clean_up("sorry, file has been touched");
    241 	}
    242 	if ((!wizard) && !md_df(fname)) {
    243 		clean_up("cannot delete file");
    244 	}
    245 	msg_cleared = 0;
    246 	ring_stats(0);
    247 	fclose(fp);
    248 }
    249 
    250 write_pack(pack, fp)
    251 object *pack;
    252 FILE *fp;
    253 {
    254 	object t;
    255 
    256 	while (pack = pack->next_object) {
    257 		r_write(fp, (char *) pack, sizeof(object));
    258 	}
    259 	t.ichar = t.what_is = 0;
    260 	r_write(fp, (char *) &t, sizeof(object));
    261 }
    262 
    263 read_pack(pack, fp, is_rogue)
    264 object *pack;
    265 FILE *fp;
    266 boolean is_rogue;
    267 {
    268 	object read_obj, *new_obj;
    269 
    270 	for (;;) {
    271 		r_read(fp, (char *) &read_obj, sizeof(object));
    272 		if (read_obj.ichar == 0) {
    273 			pack->next_object = (object *) 0;
    274 			break;
    275 		}
    276 		new_obj = alloc_object();
    277 		*new_obj = read_obj;
    278 		if (is_rogue) {
    279 			if (new_obj->in_use_flags & BEING_WORN) {
    280 					do_wear(new_obj);
    281 			} else if (new_obj->in_use_flags & BEING_WIELDED) {
    282 					do_wield(new_obj);
    283 			} else if (new_obj->in_use_flags & (ON_EITHER_HAND)) {
    284 				do_put_on(new_obj,
    285 					((new_obj->in_use_flags & ON_LEFT_HAND) ? 1 : 0));
    286 			}
    287 		}
    288 		pack->next_object = new_obj;
    289 		pack = new_obj;
    290 	}
    291 }
    292 
    293 rw_dungeon(fp, rw)
    294 FILE *fp;
    295 boolean rw;
    296 {
    297 	short i, j;
    298 	char buf[DCOLS];
    299 
    300 	for (i = 0; i < DROWS; i++) {
    301 		if (rw) {
    302 			r_write(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
    303 			for (j = 0; j < DCOLS; j++) {
    304 				buf[j] = mvinch(i, j);
    305 			}
    306 			r_write(fp, buf, DCOLS);
    307 		} else {
    308 			r_read(fp, (char *) dungeon[i], (DCOLS * sizeof(dungeon[0][0])));
    309 			r_read(fp, buf, DCOLS);
    310 			for (j = 0; j < DCOLS; j++) {
    311 				mvaddch(i, j, buf[j]);
    312 			}
    313 		}
    314 	}
    315 }
    316 
    317 rw_id(id_table, fp, n, wr)
    318 struct id id_table[];
    319 FILE *fp;
    320 int n;
    321 boolean wr;
    322 {
    323 	short i;
    324 
    325 	for (i = 0; i < n; i++) {
    326 		if (wr) {
    327 			r_write(fp, (char *) &(id_table[i].value), sizeof(short));
    328 			r_write(fp, (char *) &(id_table[i].id_status),
    329 				sizeof(unsigned short));
    330 			write_string(id_table[i].title, fp);
    331 		} else {
    332 			r_read(fp, (char *) &(id_table[i].value), sizeof(short));
    333 			r_read(fp, (char *) &(id_table[i].id_status),
    334 				sizeof(unsigned short));
    335 			read_string(id_table[i].title, fp);
    336 		}
    337 	}
    338 }
    339 
    340 write_string(s, fp)
    341 char *s;
    342 FILE *fp;
    343 {
    344 	short n;
    345 
    346 	n = strlen(s) + 1;
    347 	xxxx(s, n);
    348 	r_write(fp, (char *) &n, sizeof(short));
    349 	r_write(fp, s, n);
    350 }
    351 
    352 read_string(s, fp)
    353 char *s;
    354 FILE *fp;
    355 {
    356 	short n;
    357 
    358 	r_read(fp, (char *) &n, sizeof(short));
    359 	r_read(fp, s, n);
    360 	xxxx(s, n);
    361 }
    362 
    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 r_read(fp, buf, n)
    376 FILE *fp;
    377 char *buf;
    378 int n;
    379 {
    380 	if (fread(buf, sizeof(char), n, fp) != n) {
    381 		clean_up("read() failed, don't know why");
    382 	}
    383 }
    384 
    385 r_write(fp, buf, n)
    386 FILE *fp;
    387 char *buf;
    388 int n;
    389 {
    390 	if (!write_failed) {
    391 		if (fwrite(buf, sizeof(char), n, fp) != 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 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