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