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