Source:NetHack 5.0.0/src/save.c
Jump to navigation
Jump to search
Below is the full text to save.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/save.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 5.0 save.c $NHDT-Date: 1737610109 2025/01/22 21:28:29 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.232 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Michael Allison, 2009. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #include "hack.h" 7. 8. #ifndef NO_SIGNAL 9. #include <signal.h> 10. #endif 11. #if !defined(LSC) && !defined(O_WRONLY) && !defined(AZTEC_C) 12. #include <fcntl.h> 13. #endif 14. 15. #ifdef MICRO 16. int dotcnt, dotrow; /* also used in restore */ 17. #endif 18. 19. staticfn void savelevchn(NHFILE *); 20. staticfn void savelevl(NHFILE *); 21. staticfn void savedamage(NHFILE *); 22. staticfn void save_bubbles(NHFILE *, xint8); 23. staticfn void save_stairs(NHFILE *); 24. staticfn void save_bc(NHFILE *); 25. staticfn void saveobj(NHFILE *, struct obj *); 26. staticfn void saveobjchn(NHFILE *, struct obj **) NO_NNARGS; 27. staticfn void savemon(NHFILE *, struct monst *); 28. staticfn void savemonchn(NHFILE *, struct monst *) NO_NNARGS; 29. staticfn void savetrapchn(NHFILE *, struct trap *) NO_NNARGS; 30. staticfn void save_gamelog(NHFILE *); 31. staticfn void savegamestate(NHFILE *); 32. staticfn void savelev_core(NHFILE *, xint8); 33. staticfn void save_msghistory(NHFILE *); 34. staticfn void save_adjust_levelflags(void); 35. #if defined(HANGUPHANDLING) 36. #define HUP if (!program_state.done_hup) 37. #else 38. #define HUP 39. #endif 40. 41. /* the #save command */ 42. int 43. dosave(void) 44. { 45. clear_nhwindow(WIN_MESSAGE); 46. if (y_n("Really save?") == 'n') { 47. clear_nhwindow(WIN_MESSAGE); 48. if (gm.multi > 0) 49. nomul(0); 50. } else { 51. clear_nhwindow(WIN_MESSAGE); 52. pline("Saving..."); 53. #if defined(HANGUPHANDLING) 54. program_state.done_hup = 0; 55. #endif 56. if (dosave0()) { 57. program_state.savefile_completed++; 58. u.uhp = -1; /* universal game's over indicator */ 59. if (soundprocs.sound_exit_nhsound) 60. (*soundprocs.sound_exit_nhsound)("dosave"); 61. 62. /* make sure they see the Saving message */ 63. display_nhwindow(WIN_MESSAGE, TRUE); 64. exit_nhwindows("Be seeing you..."); 65. nh_terminate(EXIT_SUCCESS); 66. } else 67. docrt(); 68. } 69. return ECMD_OK; 70. } 71. 72. /* returns 1 if save successful */ 73. int 74. dosave0(void) 75. { 76. const char *fq_save; 77. xint8 ltmp; 78. char whynot[BUFSZ]; 79. NHFILE *nhfp, *onhfp; 80. int res = 0; 81. 82. program_state.saving++; /* inhibit status and perm_invent updates */ 83. notice_mon_off(); 84. /* we may get here via hangup signal, in which case we want to fix up 85. a few of things before saving so that they won't be restored in 86. an improper state; these will be no-ops for normal save sequence */ 87. u.uinvulnerable = 0; 88. if (iflags.save_uswallow) 89. u.uswallow = 1, iflags.save_uswallow = 0; 90. if (iflags.save_uinwater) 91. u.uinwater = 1, iflags.save_uinwater = 0; /* bypass set_uinwater() */ 92. if (iflags.save_uburied) 93. u.uburied = 1, iflags.save_uburied = 0; 94. /* extra handling for hangup save or panic save; without this, 95. a thrown light source might trigger an "obj_is_local" panic; 96. if a thrown or kicked object is in transit, put it on the map; 97. when punished, make sure ball and chain are placed too */ 98. done_object_cleanup(); /* maybe force some items onto map */ 99. 100. if (!program_state.something_worth_saving || !gs.SAVEF[0]) 101. goto done; 102. 103. fq_save = fqname(gs.SAVEF, SAVEPREFIX, 1); /* level files take 0 */ 104. #ifndef NO_SIGNAL 105. #if defined(UNIX) || defined(VMS) 106. sethanguphandler((void (*)(int) ) SIG_IGN); 107. #endif 108. (void) signal(SIGINT, SIG_IGN); 109. #endif 110. 111. HUP if (iflags.window_inited) { 112. nh_uncompress(fq_save); 113. nhfp = open_savefile(); 114. if (nhfp) { 115. close_nhfile(nhfp); 116. clear_nhwindow(WIN_MESSAGE); 117. There("seems to be an old save file."); 118. if (y_n("Overwrite the old file?") == 'n') { 119. nh_sfconvert(fq_save); 120. nh_compress(fq_save); 121. goto done; 122. } 123. } 124. } 125. 126. HUP mark_synch(); /* flush any buffered screen output */ 127. 128. nhfp = create_savefile(); 129. if (!nhfp) { 130. HUP pline("Cannot open save file."); 131. (void) delete_savefile(); /* ab@unido */ 132. goto done; 133. } 134. if (nhfp && nhfp->fplog) { 135. nhfp->rcount = nhfp->wcount = 0L; 136. } 137. 138. vision_recalc(2); /* shut down vision to prevent problems 139. in the event of an impossible() call */ 140. 141. /* undo date-dependent luck adjustments made at startup time */ 142. if (flags.moonphase == FULL_MOON) /* ut-sally!fletcher */ 143. change_luck(-1); /* and unido!ab */ 144. if (flags.friday13) 145. change_luck(1); 146. if (iflags.window_inited) 147. HUP clear_nhwindow(WIN_MESSAGE); 148. 149. #ifdef MICRO 150. dotcnt = 0; 151. dotrow = 2; 152. curs(WIN_MAP, 1, 1); 153. if (!WINDOWPORT(X11)) 154. putstr(WIN_MAP, 0, "Saving:"); 155. #endif 156. nhfp->mode = WRITING | FREEING; 157. store_version(nhfp); 158. store_plname_in_file(nhfp); 159. /* savelev() might save uball and uchain, releasing their memory if 160. FREEING, so we need to check their status now; if hero is swallowed, 161. uball and uchain will persist beyond saving map floor and inventory 162. so these copies of their pointers will be valid and savegamestate() 163. will know to save them separately (from floor and invent); when not 164. swallowed, uchain will be stale by then, and uball will be too if 165. ball is on the floor rather than carried */ 166. gl.looseball = BALL_IN_MON ? uball : 0; 167. gl.loosechain = CHAIN_IN_MON ? uchain : 0; 168. savelev(nhfp, ledger_no(&u.uz)); 169. savegamestate(nhfp); 170. 171. /* While copying level files around, zero out u.uz to keep 172. * parts of the restore code from completely initializing all 173. * in-core data structures, since all we're doing is copying. 174. * This also avoids at least one nasty core dump. 175. * [gu.uz_save is used by save_bubbles() as well as to restore u.uz] 176. */ 177. gu.uz_save = u.uz; 178. u.uz.dnum = u.uz.dlevel = 0; 179. /* these pointers are no longer valid, and at least u.usteed 180. * may mislead place_monster() on other levels 181. */ 182. set_ustuck((struct monst *) 0); /* also clears u.uswallow */ 183. u.usteed = (struct monst *) 0; 184. 185. for (ltmp = (xint8) 1; ltmp <= maxledgerno(); ltmp++) { 186. if (ltmp == ledger_no(&gu.uz_save)) 187. continue; 188. if (!(svl.level_info[ltmp].flags & LFILE_EXISTS)) 189. continue; 190. #ifdef MICRO 191. curs(WIN_MAP, 1 + dotcnt++, dotrow); 192. if (dotcnt >= (COLNO - 1)) { 193. dotrow++; 194. dotcnt = 0; 195. } 196. if (!WINDOWPORT(X11)) { 197. putstr(WIN_MAP, 0, "."); 198. } 199. mark_synch(); 200. #endif 201. onhfp = open_levelfile(ltmp, whynot); 202. if (!onhfp) { 203. HUP pline1(whynot); 204. close_nhfile(nhfp); 205. (void) delete_savefile(); 206. HUP Strcpy(svk.killer.name, whynot); 207. HUP done(TRICKED); 208. goto done; 209. } 210. getlev(onhfp, svh.hackpid, ltmp); 211. close_nhfile(onhfp); 212. Sfo_xint8(nhfp, <mp, "gamestate-level_number"); 213. savelev(nhfp, ltmp); /* actual level*/ 214. delete_levelfile(ltmp); 215. } 216. close_nhfile(nhfp); 217. 218. u.uz = gu.uz_save; 219. gu.uz_save.dnum = gu.uz_save.dlevel = 0; 220. 221. /* get rid of current level --jgm */ 222. delete_levelfile(ledger_no(&u.uz)); 223. delete_levelfile(0); 224. nh_sfconvert(fq_save); 225. nh_compress(fq_save); 226. /* this should probably come sooner... */ 227. program_state.something_worth_saving = 0; 228. res = 1; 229. 230. done: 231. notice_mon_on(); 232. program_state.saving--; 233. return res; 234. } 235. 236. staticfn void 237. save_gamelog(NHFILE *nhfp) 238. { 239. struct gamelog_line *tmp = gg.gamelog, *tmp2; 240. int slen; 241. 242. while (tmp) { 243. tmp2 = tmp->next; 244. if (nhfp->mode & (COUNTING | WRITING)) { 245. slen = Strlen(tmp->text); 246. Sfo_int(nhfp, &slen, "gamelog-length"); 247. Sfo_char(nhfp, tmp->text, "gamelog-gamelog_text", slen); 248. Sfo_gamelog_line(nhfp, tmp, "gamelog-gamelog_line"); 249. } 250. if (nhfp->mode & FREEING) { 251. free((genericptr_t) tmp->text); 252. free((genericptr_t) tmp); 253. } 254. tmp = tmp2; 255. } 256. if (nhfp->mode & (COUNTING | WRITING)) { 257. slen = -1; 258. Sfo_int(nhfp, &slen, "gamelog-length"); 259. } 260. if (nhfp->mode & FREEING) 261. gg.gamelog = NULL; 262. } 263. 264. staticfn void 265. savegamestate(NHFILE *nhfp) 266. { 267. int i; 268. unsigned long uid; 269. 270. program_state.saving++; /* caller should/did already set this... */ 271. uid = (unsigned long) getuid(); 272. Sfo_ulong(nhfp, &uid, "gamestate-uid"); 273. Sfo_char(nhfp, &svn.nhuuid[0], "nhuuid", sizeof svn.nhuuid); 274. Sfo_long(nhfp, &svm.moves, "gamestate-moves"); 275. moves_to_relative_time(&svc.context.seer_turn); 276. moves_to_relative_time(&svc.context.digging.lastdigtime); 277. Sfo_context_info(nhfp, &svc.context, "gamestate-context"); 278. relative_time_to_moves(&svc.context.seer_turn); 279. relative_time_to_moves(&svc.context.digging.lastdigtime); 280. 281. Sfo_flag(nhfp, &flags, "gamestate-flags"); 282. urealtime.finish_time = getnow(); 283. urealtime.realtime += timet_delta(urealtime.finish_time, 284. urealtime.start_timing); 285. Sfo_long(nhfp, &svw.wreserve, "wreserve"); 286. Sfo_int32(nhfp, &svw.wtreserved, "wtreserved"); 287. Sfo_you(nhfp, &u, "gamestate-you"); 288. Sfo_char(nhfp, yyyymmddhhmmss(ubirthday), "gamestate-ubirthday", 14); 289. Sfo_long(nhfp, &urealtime.realtime, "gamestate-realtime"); 290. Sfo_char(nhfp, yyyymmddhhmmss(urealtime.start_timing), "gamestate-start_timing", 14); 291. /* this is the value to use for the next update of urealtime.realtime */ 292. urealtime.start_timing = urealtime.finish_time; 293. save_killers(nhfp); 294. 295. /* must come before gm.migrating_objs and gm.migrating_mons are freed */ 296. save_timers(nhfp, RANGE_GLOBAL); 297. save_light_sources(nhfp, RANGE_GLOBAL); 298. 299. /* when FREEING, deletes objects in invent and sets invent to Null; 300. pointers into invent (uwep, uarmg, uamul, &c) are set to Null too */ 301. saveobjchn(nhfp, &gi.invent); 302. 303. /* save ball and chain if they happen to be in an unusual state */ 304. save_bc(nhfp); 305. 306. saveobjchn(nhfp, &gm.migrating_objs); /* frees objs and sets to Null */ 307. savemonchn(nhfp, gm.migrating_mons); 308. if (release_data(nhfp)) 309. gm.migrating_mons = (struct monst *) 0; 310. for (i = 0; i < NUMMONS; ++i) { 311. Sfo_mvitals(nhfp, &svm.mvitals[i], "gamestate-mvitals"); 312. } 313. save_dungeon(nhfp, (boolean) !!update_file(nhfp), 314. (boolean) !!release_data(nhfp)); 315. savelevchn(nhfp); 316. Sfo_q_score(nhfp, &svq.quest_status, "gamestate-quest_status"); 317. for (i = 0; i < (MAXSPELL + 1); ++i) { 318. Sfo_spell(nhfp, &svs.spl_book[i], "gamestate-spl_book"); 319. } 320. save_artifacts(nhfp); 321. save_oracles(nhfp); 322. Sfo_char(nhfp, svp.pl_character, "gamestate-pl_character", sizeof svp.pl_character); 323. Sfo_char(nhfp, svp.pl_fruit, "gamestate-pl_fruit", sizeof svp.pl_fruit); 324. savefruitchn(nhfp); 325. savenames(nhfp); 326. save_msghistory(nhfp); 327. save_gamelog(nhfp); 328. save_luadata(nhfp); 329. if (nhfp->structlevel) 330. bflush(nhfp->fd); 331. program_state.saving--; 332. return; 333. } 334. 335. /* potentially called from goto_level(do.c) as well as savestateinlock() */ 336. boolean 337. tricked_fileremoved(NHFILE *nhfp, char *whynot) 338. { 339. if (!nhfp) { 340. pline1(whynot); 341. pline("Probably someone removed it."); 342. Strcpy(svk.killer.name, whynot); 343. done(TRICKED); 344. return TRUE; 345. } 346. return FALSE; 347. } 348. 349. #ifdef INSURANCE 350. void 351. savestateinlock(void) 352. { 353. int hpid = 0; 354. char whynot[BUFSZ]; 355. NHFILE *nhfp; 356. 357. program_state.saving++; /* inhibit status and perm_invent updates */ 358. /* When checkpointing is on, the full state needs to be written 359. * on each checkpoint. When checkpointing is off, only the pid 360. * needs to be in the level.0 file, so it does not need to be 361. * constantly rewritten. When checkpointing is turned off during 362. * a game, however, the file has to be rewritten once to truncate 363. * it and avoid restoring from outdated information. 364. * 365. * Restricting gh.havestate to this routine means that an additional 366. * noop pid rewriting will take place on the first "checkpoint" after 367. * the game is started or restored, if checkpointing is off. 368. */ 369. if (flags.ins_chkpt || gh.havestate) { 370. /* save the rest of the current game state in the lock file, 371. * following the original int pid, the current level number, 372. * and the current savefile name, which should not be subject 373. * to any internal compression schemes since they must be 374. * readable by an external utility 375. */ 376. nhfp = open_levelfile(0, whynot); 377. if (tricked_fileremoved(nhfp, whynot)) { 378. program_state.saving--; 379. return; 380. } 381. 382. Sfi_int(nhfp, &hpid, "gamestate-hackpid"); 383. if (svh.hackpid != hpid) { 384. Sprintf(whynot, "Level #0 pid (%d) doesn't match ours (%d)!", 385. hpid, svh.hackpid); 386. goto giveup; 387. } 388. close_nhfile(nhfp); 389. 390. nhfp = create_levelfile(0, whynot); 391. if (!nhfp) { 392. pline1(whynot); 393. giveup: 394. Strcpy(svk.killer.name, whynot); 395. /* done(TRICKED) will return when running in wizard mode; 396. clear the display-update-suppression flag before rather 397. than after so that screen updating behaves normally; 398. game data shouldn't be inconsistent yet, unlike it would 399. become midway through saving */ 400. program_state.saving--; 401. done(TRICKED); 402. return; 403. } 404. nhfp->mode = WRITING; 405. Sfo_int(nhfp, &svh.hackpid, "gamestate-hackpid"); 406. if (flags.ins_chkpt) { 407. int currlev = ledger_no(&u.uz); 408. 409. Sfo_int(nhfp, &currlev, "gamestate-savestateinlock"); 410. save_savefile_name(nhfp); 411. store_version(nhfp); 412. store_plname_in_file(nhfp); 413. 414. /* if ball and/or chain aren't on floor or in invent, keep a copy 415. of their pointers; not valid when on floor or in invent */ 416. gl.looseball = BALL_IN_MON ? uball : 0; 417. gl.loosechain = CHAIN_IN_MON ? uchain : 0; 418. savegamestate(nhfp); 419. } 420. close_nhfile(nhfp); 421. } 422. program_state.saving--; 423. gh.havestate = flags.ins_chkpt; 424. return; 425. } 426. #endif 427. 428. void 429. savelev(NHFILE *nhfp, xint8 lev) 430. { 431. boolean set_uz_save = (gu.uz_save.dnum == 0 && gu.uz_save.dlevel == 0); 432. 433. /* caller might have already set up gu.uz_save and zeroed u.uz; 434. if not, we need to set it for save_bubbles(); caveat: if the 435. player quits during character selection, u.uz won't be set yet 436. but we'll be called during run-down */ 437. if (set_uz_save && (nhfp->mode & (COUNTING | WRITING))) { 438. if (u.uz.dnum == 0 && u.uz.dlevel == 0) { 439. program_state.something_worth_saving = 0; 440. panic("savelev: where are we?"); 441. } 442. gu.uz_save = u.uz; 443. } 444. 445. savelev_core(nhfp, lev); 446. 447. if (set_uz_save) 448. gu.uz_save.dnum = gu.uz_save.dlevel = 0; /* unset */ 449. } 450. 451. staticfn void 452. savelev_core(NHFILE *nhfp, xint8 lev) 453. { 454. #ifdef TOS 455. short tlev; 456. #endif 457. int i, c, r; 458. coord *tmpc; 459. 460. program_state.saving++; /* even if current mode is FREEING */ 461. 462. if (!nhfp) 463. panic("Save on bad file!"); /* impossible */ 464. /* 465. * Level file contents: 466. * version info (handled by caller); 467. * save file info (compression type; also by caller); 468. * process ID; 469. * internal level number (ledger number); 470. * bones info; 471. * actual level data. 472. * 473. * If we're tearing down the current level without saving anything 474. * (which happens at end of game or upon entrance to endgame or 475. * after an aborted restore attempt) then we don't want to do any 476. * actual I/O. So when only freeing, we skip to the bones info 477. * portion (which has some freeing to do), then jump quite a bit 478. * further ahead to the middle of the 'actual level data' portion. 479. */ 480. if (nhfp->mode != FREEING) { 481. /* WRITING (probably ORed with FREEING), or COUNTING */ 482. 483. /* purge any dead monsters (necessary if we're starting 484. a panic save rather than a normal one, or sometimes 485. when changing levels without taking time -- e.g. 486. create statue trap then immediately level teleport) */ 487. if (iflags.purge_monsters) 488. dmonsfree(); 489. /* clear objs_deleted list too */ 490. if (go.objs_deleted) 491. dobjsfree(); /* really free deleted objects */ 492. 493. if (lev >= 0 && lev <= maxledgerno()) 494. svl.level_info[lev].flags |= VISITED; 495. Sfo_int(nhfp, &svh.hackpid, "gamestate-hackpid"); 496. Sfo_xint8(nhfp, &lev, "gamestate-dlvl"); 497. } 498. 499. /* bones info comes before level data; the intent is for an external 500. program ('hearse') to be able to match a bones file with the 501. corresponding log file entry--or perhaps just skip that?--without 502. the guessing that was needed in 3.4.3 and without having to 503. interpret level data to find where to start; unfortunately it 504. still needs to handle all the data compression schemes */ 505. savecemetery(nhfp, &svl.level.bonesinfo); 506. if (nhfp->mode == FREEING) /* see above */ 507. goto skip_lots; 508. 509. savelevl(nhfp); 510. for (c = 0; c < COLNO; ++c) { 511. for (r = 0; r < ROWNO; ++r) { 512. Sfo_schar(nhfp, &svl.lastseentyp[c][r], "lastseentyp"); 513. } 514. } 515. /* svm.moves will actually be read back into svo.omoves on restore */ 516. Sfo_long(nhfp, &svm.moves, "lev-timestmp"); 517. save_stairs(nhfp); 518. Sfo_dest_area(nhfp, &svu.updest, "lev-updest"); 519. Sfo_dest_area(nhfp, &svd.dndest, "lev-dndest"); 520. save_adjust_levelflags(); 521. Sfo_levelflags(nhfp, &svl.level.flags, "lev-level_flags"); 522. rest_adjust_levelflags(); 523. 524. Sfo_int(nhfp, &svd.doors_alloc, "lev-doors_alloc"); 525. /* don't rely on underlying write() behavior to write 526. * nothing if count arg is 0, just skip it */ 527. if (svd.doors_alloc) { 528. tmpc = svd.doors; 529. for (i = 0; i < svd.doors_alloc; ++i) { 530. Sfo_nhcoord(nhfp, tmpc, "lev-doors"); 531. tmpc++; 532. } 533. } 534. save_rooms(nhfp); /* no dynamic memory to reclaim */ 535. 536. /* from here on out, saving also involves allocated memory cleanup */ 537. skip_lots: 538. /* timers and lights must be saved before monsters and objects */ 539. save_timers(nhfp, RANGE_LEVEL); 540. save_light_sources(nhfp, RANGE_LEVEL); 541. 542. savemonchn(nhfp, fmon); 543. save_worm(nhfp); /* save worm information */ 544. savetrapchn(nhfp, gf.ftrap); 545. saveobjchn(nhfp, &fobj); 546. saveobjchn(nhfp, &svl.level.buriedobjlist); 547. saveobjchn(nhfp, &gb.billobjs); 548. save_engravings(nhfp); 549. savedamage(nhfp); /* pending shop wall and/or floor repair */ 550. save_regions(nhfp); 551. save_bubbles(nhfp, lev); /* for water and air */ 552. save_exclusions(nhfp); 553. save_track(nhfp); 554. 555. if (nhfp->mode != FREEING) { 556. if (nhfp->structlevel) 557. bflush(nhfp->fd); 558. } 559. program_state.saving--; 560. if (release_data(nhfp)) { 561. clear_level_structures(); 562. gf.ftrap = 0; 563. gb.billobjs = 0; 564. (void) memset(svr.rooms, 0, sizeof(svr.rooms)); 565. } 566. return; 567. } 568. 569. void 570. save_adjust_levelflags(void) 571. { 572. /* adjust any timestamps */ 573. moves_to_relative_time(&svl.level.flags.stasis_until); 574. } 575. 576. staticfn void 577. savelevl(NHFILE *nhfp) 578. { 579. int x, y; 580. 581. for (x = 0; x < COLNO; x++) { 582. for (y = 0; y < ROWNO; y++) { 583. Sfo_rm(nhfp, &levl[x][y], "location-rm"); 584. } 585. } 586. return; 587. } 588. 589. /* save Plane of Water's air bubbles and Plane of Air's clouds */ 590. staticfn void 591. save_bubbles(NHFILE *nhfp, xint8 lev) 592. { 593. xint8 bbubbly; 594. 595. /* air bubbles and clouds used to be saved as part of game state 596. because restoring them needs dungeon data that isn't available 597. during the first pass of their levels; now that they are part of 598. the current level instead, we write a zero or non-zero marker 599. so that restore can determine whether they are present even when 600. u.uz and ledger_no() aren't available to it yet */ 601. bbubbly = 0; 602. if (lev == ledger_no(&water_level) || lev == ledger_no(&air_level)) 603. bbubbly = lev; /* non-zero */ 604. if (update_file(nhfp)) 605. Sfo_xint8(nhfp, &bbubbly, "bubbles-bbubbly"); 606. #if 0 607. if (nhfp->structlevel) 608. xxwrite(nhfp->fd, (genericptr_t) &bbubbly, sizeof bbubbly); 609. #endif 610. 611. if (bbubbly) 612. save_waterlevel(nhfp); /* save air bubbles or clouds */ 613. } 614. 615. /* used when saving a level and also when saving dungeon overview data */ 616. void 617. savecemetery(NHFILE *nhfp, struct cemetery **cemeteryaddr) 618. { 619. struct cemetery *thisbones, *nextbones; 620. int flag; 621. 622. flag = *cemeteryaddr ? 0 : -1; 623. if (update_file(nhfp)) { 624. Sfo_int(nhfp, &flag, "cemetery-cemetery_flag"); 625. } 626. nextbones = *cemeteryaddr; 627. while ((thisbones = nextbones) != 0) { 628. nextbones = thisbones->next; 629. if (update_file(nhfp)) { 630. Sfo_cemetery(nhfp, thisbones, "cemetery-bonesinfo"); 631. } 632. if (release_data(nhfp)) 633. free((genericptr_t) thisbones); 634. } 635. if (release_data(nhfp)) 636. *cemeteryaddr = 0; 637. } 638. 639. staticfn void 640. savedamage(NHFILE *nhfp) 641. { 642. struct damage *damageptr, *tmp_dam; 643. unsigned int xl = 0; 644. 645. damageptr = svl.level.damagelist; 646. for (tmp_dam = damageptr; tmp_dam; tmp_dam = tmp_dam->next) 647. xl++; 648. if (update_file(nhfp)) { 649. Sfo_unsigned(nhfp, &xl, "damage-damage_count"); 650. } 651. while (damageptr) { 652. if (update_file(nhfp)) { 653. Sfo_damage(nhfp, damageptr, "damage"); 654. } 655. tmp_dam = damageptr; 656. damageptr = damageptr->next; 657. if (release_data(nhfp)) 658. free((genericptr_t) tmp_dam); 659. } 660. if (release_data(nhfp)) 661. svl.level.damagelist = 0; 662. } 663. 664. staticfn void 665. save_stairs(NHFILE *nhfp) 666. { 667. stairway *stway = gs.stairs; 668. int buflen = (int) sizeof *stway; 669. 670. while (stway) { 671. if (update_file(nhfp)) { 672. boolean use_relative = (program_state.restoring != REST_GSTATE 673. && stway->tolev.dnum == u.uz.dnum); 674. if (use_relative) { 675. /* make dlevel relative to current level */ 676. stway->tolev.dlevel -= u.uz.dlevel; 677. } 678. Sfo_int(nhfp, &buflen, "stairs-staircount"); 679. Sfo_stairway(nhfp, stway, "stairs-stairway"); 680. if (use_relative) { 681. /* reset stairway dlevel back to absolute */ 682. stway->tolev.dlevel += u.uz.dlevel; 683. } 684. } 685. stway = stway->next; 686. } 687. if (update_file(nhfp)) { 688. buflen = -1; 689. Sfo_int(nhfp, &buflen, "stairs-staircount"); 690. } 691. } 692. 693. /* if ball and/or chain are loose, make an object chain for it/them and 694. save that separately from other objects */ 695. staticfn void 696. save_bc(NHFILE *nhfp) 697. { 698. struct obj *bc_objs = 0; 699. 700. /* save ball and chain if they are currently dangling free (i.e. not 701. on floor or in inventory); 'looseball' and 'loosechain' have been 702. set up in caller because ball and chain will be gone by now if on 703. floor, or ball gone if carried */ 704. if (gl.loosechain) { 705. gl.loosechain->nobj = bc_objs; /* uchain */ 706. bc_objs = gl.loosechain; 707. if (nhfp->mode & FREEING) { 708. setworn((struct obj *) 0, W_CHAIN); /* sets 'uchain' to Null */ 709. gl.loosechain = (struct obj *) 0; 710. } 711. } 712. if (gl.looseball) { 713. gl.looseball->nobj = bc_objs; 714. bc_objs = gl.looseball; 715. if (nhfp->mode & FREEING) { 716. setworn((struct obj *) 0, W_BALL); /* sets 'uball' to Null */ 717. gl.looseball = (struct obj *) 0; 718. } 719. } 720. saveobjchn(nhfp, &bc_objs); /* frees objs in list, sets bc_objs to Null */ 721. } 722. 723. /* save one object; 724. caveat: this is only for update_file(); caller handles release_data() */ 725. staticfn void 726. saveobj(NHFILE *nhfp, struct obj *otmp) 727. { 728. int buflen, zerobuf = 0; 729. 730. buflen = (int) sizeof (struct obj); 731. Sfo_int(nhfp, &buflen, "obj-obj_length"); 732. Sfo_obj(nhfp, otmp, "obj"); 733. if (otmp->oextra) { 734. buflen = ONAME(otmp) ? (int) strlen(ONAME(otmp)) + 1 : 0; 735. Sfo_int(nhfp, &buflen, "obj-oname_length"); 736. 737. if (buflen > 0) { 738. Sfo_char(nhfp, ONAME(otmp), "obj-oname", buflen); 739. } 740. /* defer to savemon() for this one */ 741. if (OMONST(otmp)) { 742. savemon(nhfp, OMONST(otmp)); 743. } else { 744. Sfo_int(nhfp, &zerobuf, "obj-omonst_length"); 745. } 746. /* extra info about scroll of mail */ 747. buflen = OMAILCMD(otmp) ? (int) strlen(OMAILCMD(otmp)) + 1 : 0; 748. Sfo_int(nhfp, &buflen, "obj-omailcmd_length"); 749. if (buflen > 0) { 750. Sfo_char(nhfp, OMAILCMD(otmp), "obj-omailcmd", buflen); 751. } 752. /* omid used to be indirect via a pointer in oextra but has 753. become part of oextra itself; 0 means not applicable and 754. gets saved/restored whenever any other oextra components do */ 755. Sfo_unsigned(nhfp, &OMID(otmp), "obj-omid"); 756. } 757. } 758. 759. /* save an object chain; sets head of list to Null when done; 760. handles release_data() for each object in the list */ 761. staticfn void 762. saveobjchn(NHFILE *nhfp, struct obj **obj_p) 763. { 764. struct obj *otmp = *obj_p; 765. struct obj *otmp2; 766. boolean is_invent = (otmp && otmp == gi.invent); 767. int minusone = -1; 768. 769. while (otmp) { 770. otmp2 = otmp->nobj; 771. if (update_file(nhfp)) { 772. saveobj(nhfp, otmp); 773. } 774. if (Has_contents(otmp)) 775. saveobjchn(nhfp, &otmp->cobj); 776. if (release_data(nhfp)) { 777. /* 778. * If these are on the floor, the discarding could be 779. * due to game save, or we could just be changing levels. 780. * Always invalidate the pointer, but ensure that we have 781. * the o_id in order to restore the pointer on reload. 782. */ 783. if (otmp == svc.context.victual.piece) { 784. svc.context.victual.o_id = otmp->o_id; 785. svc.context.victual.piece = (struct obj *) 0; 786. } 787. if (otmp == svc.context.tin.tin) { 788. svc.context.tin.o_id = otmp->o_id; 789. svc.context.tin.tin = (struct obj *) 0; 790. } 791. if (otmp == svc.context.spbook.book) { 792. svc.context.spbook.o_id = otmp->o_id; 793. svc.context.spbook.book = (struct obj *) 0; 794. } 795. otmp->where = OBJ_FREE; /* set to free so dealloc will work */ 796. otmp->nobj = NULL; /* nobj saved into otmp2 */ 797. otmp->cobj = NULL; /* contents handled above */ 798. otmp->timed = 0; /* not timed any more */ 799. otmp->lamplit = 0; /* caller handled lights */ 800. otmp->leashmon = 0; /* mon->mleashed could still be set; 801. * unfortunately, we can't clear that 802. * until after the monster is saved */ 803. /* clear 'uball' and 'uchain' pointers if resetting their mask; 804. could also do same for other worn items but don't need to */ 805. if ((otmp->owornmask & (W_BALL | W_CHAIN)) != 0) 806. setworn((struct obj *) 0, 807. otmp->owornmask & (W_BALL | W_CHAIN)); 808. otmp->owornmask = 0L; /* no longer care */ 809. program_state.freeingdata++; 810. dealloc_obj(otmp); 811. program_state.freeingdata--; 812. } 813. otmp = otmp2; 814. } 815. if (update_file(nhfp)) { 816. Sfo_int(nhfp, &minusone, "obj-obj_length"); 817. } 818. if (release_data(nhfp)) { 819. if (is_invent) 820. allunworn(); /* clear uwep, uarm, uball, &c pointers */ 821. *obj_p = (struct obj *) 0; 822. } 823. } 824. 825. staticfn void 826. savemon(NHFILE *nhfp, struct monst *mtmp) 827. { 828. int buflen; 829. 830. mtmp->mtemplit = 0; /* normally clear; if set here then a panic save 831. * is being written while bhit() was executing */ 832. buflen = (int) sizeof (struct monst); 833. Sfo_int(nhfp, &buflen, "monst-monst_length"); 834. Sfo_monst(nhfp, mtmp, "monst"); 835. if (mtmp->mextra) { 836. buflen = MGIVENNAME(mtmp) ? (int) strlen(MGIVENNAME(mtmp)) + 1 : 0; 837. Sfo_int(nhfp, &buflen, "monst-mgivenname_length"); 838. if (buflen > 0) { 839. Sfo_char(nhfp, MGIVENNAME(mtmp), "monst-mgivenname", buflen); 840. } 841. buflen = EGD(mtmp) ? (int) sizeof (struct egd) : 0; 842. Sfo_int(nhfp, &buflen, "monst-egd_length"); 843. if (buflen > 0) { 844. Sfo_egd(nhfp, EGD(mtmp), "monst-egd"); 845. } 846. buflen = EPRI(mtmp) ? (int) sizeof (struct epri) : 0; 847. Sfo_int(nhfp, &buflen, "monst-epri_length"); 848. if (buflen > 0) { 849. Sfo_epri(nhfp, EPRI(mtmp), "monst-epri"); 850. } 851. buflen = ESHK(mtmp) ? (int) sizeof (struct eshk) : 0; 852. Sfo_int(nhfp, &buflen, "monst-eshk_length"); 853. if (buflen > 0) { 854. Sfo_eshk(nhfp, ESHK(mtmp), "monst-eshk"); 855. } 856. buflen = EMIN(mtmp) ? (int) sizeof (struct emin) : 0; 857. Sfo_int(nhfp, &buflen, "monst-emin_length"); 858. if (buflen > 0) { 859. Sfo_emin(nhfp, EMIN(mtmp), "monst-emin"); 860. } 861. buflen = EDOG(mtmp) ? (int) sizeof (struct edog) : 0; 862. Sfo_int(nhfp, &buflen, "monst-edog_length"); 863. if (buflen > 0) { 864. /* we only store relative times in save and bones */ 865. moves_to_relative_time(&EDOG(mtmp)->droptime); 866. moves_to_relative_time(&EDOG(mtmp)->hungrytime); 867. Sfo_edog(nhfp, EDOG(mtmp), "monst-edog"); 868. relative_time_to_moves(&EDOG(mtmp)->droptime); 869. relative_time_to_moves(&EDOG(mtmp)->hungrytime); 870. } 871. buflen = EBONES(mtmp) ? (int) sizeof (struct ebones) : 0; 872. Sfo_int(nhfp, &buflen, "monst-ebones_length"); 873. if (buflen > 0) { 874. Sfo_ebones(nhfp, EBONES(mtmp), "monst-ebones"); 875. } 876. /* mcorpsenm is inline int rather than pointer to something, 877. so doesn't need to be preceded by a length field */ 878. buflen = (int) MCORPSENM(mtmp); 879. Sfo_int(nhfp, &buflen, "monst-mcorpsenm"); 880. } 881. } 882. 883. staticfn void 884. savemonchn(NHFILE *nhfp, struct monst *mtmp) 885. { 886. struct monst *mtmp2; 887. int minusone = -1; 888. 889. while (mtmp) { 890. mtmp2 = mtmp->nmon; 891. if (update_file(nhfp)) { 892. mtmp->mnum = monsndx(mtmp->data); 893. if (mtmp->ispriest) 894. forget_temple_entry(mtmp); /* EPRI() */ 895. savemon(nhfp, mtmp); 896. } 897. if (mtmp->minvent) 898. saveobjchn(nhfp, &mtmp->minvent); 899. if (release_data(nhfp)) { 900. if (mtmp == svc.context.polearm.hitmon) { 901. svc.context.polearm.m_id = mtmp->m_id; 902. svc.context.polearm.hitmon = NULL; 903. } 904. if (mtmp == u.ustuck) 905. u.ustuck_mid = u.ustuck->m_id; 906. if (mtmp == u.usteed) 907. u.usteed_mid = u.usteed->m_id; 908. mtmp->nmon = NULL; /* nmon saved into mtmp2 */ 909. dealloc_monst(mtmp); 910. } 911. mtmp = mtmp2; 912. } 913. if (update_file(nhfp)) { 914. Sfo_int(nhfp, &minusone, "monst-monst_length"); 915. } 916. } 917. 918. /* save traps; gf.ftrap is the only trap chain so 2nd arg is superfluous */ 919. staticfn void 920. savetrapchn(NHFILE *nhfp, struct trap *trap) 921. { 922. static struct trap zerotrap; 923. struct trap *trap2; 924. 925. while (trap) { 926. boolean use_relative = (program_state.restoring != REST_GSTATE 927. && trap->dst.dnum == u.uz.dnum); 928. trap2 = trap->ntrap; 929. if (use_relative) 930. trap->dst.dlevel -= u.uz.dlevel; /* make it relative */ 931. if (update_file(nhfp)) { 932. Sfo_trap(nhfp, trap, "trap"); 933. } 934. if (use_relative) 935. trap->dst.dlevel += u.uz.dlevel; /* reset back to absolute */ 936. if (release_data(nhfp)) 937. dealloc_trap(trap); 938. trap = trap2; 939. } 940. if (update_file(nhfp)) { 941. Sfo_trap(nhfp, &zerotrap, "trap"); 942. } 943. } 944. 945. /* save all the fruit names and ID's; this is used only in saving whole games 946. * (not levels) and in saving bones levels. When saving a bones level, 947. * we only want to save the fruits which exist on the bones level; the bones 948. * level routine marks nonexistent fruits by making the fid negative. 949. */ 950. void 951. savefruitchn(NHFILE *nhfp) 952. { 953. static struct fruit zerofruit; 954. struct fruit *f2, *f1; 955. 956. f1 = gf.ffruit; 957. while (f1) { 958. f2 = f1->nextf; 959. if (f1->fid >= 0 && update_file(nhfp)) { 960. Sfo_fruit(nhfp, f1, "fruit"); 961. } 962. if (release_data(nhfp)) 963. dealloc_fruit(f1); 964. f1 = f2; 965. } 966. if (update_file(nhfp)) { 967. Sfo_fruit(nhfp, &zerofruit, "fruit"); 968. } 969. if (release_data(nhfp)) 970. gf.ffruit = 0; 971. } 972. 973. staticfn void 974. savelevchn(NHFILE *nhfp) 975. { 976. s_level *tmplev, *tmplev2; 977. int cnt = 0; 978. 979. for (tmplev = svs.sp_levchn; tmplev; tmplev = tmplev->next) 980. cnt++; 981. if (update_file(nhfp)) { 982. Sfo_int(nhfp, &cnt, "levchn-lev_count"); 983. } 984. for (tmplev = svs.sp_levchn; tmplev; tmplev = tmplev2) { 985. tmplev2 = tmplev->next; 986. if (update_file(nhfp)) { 987. Sfo_s_level(nhfp, tmplev, "levchn-s_level"); 988. } 989. if (release_data(nhfp)) 990. free((genericptr_t) tmplev); 991. } 992. if (release_data(nhfp)) 993. svs.sp_levchn = 0; 994. } 995. 996. /* write "name-role-race-gend-algn" into save file for menu-based restore; 997. the first dash is actually stored as '\0' instead of '-' */ 998. void 999. store_plname_in_file(NHFILE *nhfp) 1000. { 1001. char hero[PL_NSIZ_PLUS]; /* [PL_NSIZ + 4*(1+3) + 1] */ 1002. int plsiztmp = (int) sizeof hero; 1003. 1004. (void) memset((genericptr_t) hero, '\0', sizeof hero); 1005. /* augment svp.plname[]; the gender and alignment values reflect those 1006. in effect at time of saving rather than at start of game */ 1007. Snprintf(hero, sizeof hero, "%s-%.3s-%.3s-%.3s-%.3s", 1008. svp.plname, gu.urole.filecode, 1009. gu.urace.filecode, genders[flags.female].filecode, 1010. aligns[1 - u.ualign.type].filecode); 1011. /* replace "-role-race..." with "\0role-race..." so that we can include 1012. or exclude the role-&c suffix easily, without worrying about whether 1013. plname contains any dashes; but don't rely on snprintf() for this */ 1014. hero[strlen(svp.plname)] = '\0'; 1015. /* insert playmode into final slot of hero[]; 1016. 'D','X','-' are the same characters as are used for paniclog entries */ 1017. assert(hero[PL_NSIZ_PLUS - 1 - 1] == '\0'); 1018. hero[PL_NSIZ_PLUS - 1] = wizard ? 'D' : discover ? 'X' : '-'; 1019. 1020. if (nhfp->structlevel) 1021. bufoff(nhfp->fd); /* bwrite() before bufon() uses plain write() */ 1022. Sfo_int(nhfp, &plsiztmp, "plname-size"); 1023. Sfo_char(nhfp, hero, "plname", plsiztmp); 1024. if (nhfp->structlevel) 1025. bufon(nhfp->fd); 1026. return; 1027. } 1028. 1029. staticfn void 1030. save_msghistory(NHFILE *nhfp) 1031. { 1032. char *msg; 1033. int msgcount = 0; 1034. int minusone = -1, msglen; 1035. boolean init = TRUE; 1036. 1037. if (update_file(nhfp)) { 1038. /* ask window port for each message in sequence */ 1039. while ((msg = getmsghistory(init)) != 0) { 1040. init = FALSE; 1041. msglen = (int) Strlen(msg); 1042. if (msglen < 1) 1043. continue; 1044. /* sanity: truncate if necessary (shouldn't happen); 1045. no need to modify msg[] since terminator isn't written */ 1046. if (msglen > BUFSZ - 1) 1047. msglen = BUFSZ - 1; 1048. Sfo_int(nhfp, &msglen, "msghistory-length"); 1049. Sfo_char(nhfp, msg, "msghistory-msg", msglen); 1050. ++msgcount; 1051. } 1052. Sfo_int(nhfp, &minusone, "msghistory-length"); 1053. } 1054. debugpline1("Stored %d messages into savefile.", msgcount); 1055. /* note: we don't attempt to handle release_data() here */ 1056. } 1057. 1058. /* also called by prscore(); this probably belongs in dungeon.c... */ 1059. void 1060. free_dungeons(void) 1061. { 1062. #ifdef FREE_ALL_MEMORY 1063. NHFILE *tnhfp = get_freeing_nhfile(); 1064. 1065. savelevchn(tnhfp); 1066. save_dungeon(tnhfp, FALSE, TRUE); 1067. free_luathemes(all_themes); 1068. close_nhfile(tnhfp); 1069. #endif 1070. return; 1071. } 1072. 1073. extern int options_set_window_colors_flag; /* options.c */ 1074. 1075. /* free a lot of allocated memory which is ordinarily freed during save */ 1076. void 1077. freedynamicdata(void) 1078. { 1079. NHFILE *tnhfp = get_freeing_nhfile(); 1080. 1081. #if defined(UNIX) && defined(MAIL) 1082. free_maildata(); 1083. #endif 1084. free_menu_coloring(); 1085. free_invbuf(); /* let_to_name (invent.c) */ 1086. free_youbuf(); /* You_buf,&c (pline.c) */ 1087. msgtype_free(); 1088. savedsym_free(); 1089. tmp_at(DISP_FREEMEM, 0); /* temporary display effects */ 1090. purge_all_custom_entries(); 1091. #ifdef FREE_ALL_MEMORY 1092. #define free_current_level() savelev(tnhfp, -1) 1093. #define freeobjchn(X) (saveobjchn(tnhfp, &X), X = 0) 1094. #define freemonchn(X) (savemonchn(tnhfp, X), X = 0) 1095. #define freefruitchn() savefruitchn(tnhfp) 1096. #define freenames() savenames(tnhfp) 1097. #define free_killers() save_killers(tnhfp) 1098. #define free_oracles() save_oracles(tnhfp) 1099. #define free_waterlevel() save_waterlevel(tnhfp) 1100. #define free_timers(R) save_timers(tnhfp, R) 1101. #define free_light_sources(R) save_light_sources(tnhfp, R) 1102. #define free_animals() mon_animal_list(FALSE) 1103. #define discard_gamelog() save_gamelog(tnhfp); 1104. 1105. /* move-specific data */ 1106. dmonsfree(); /* release dead monsters */ 1107. /* dobjsfree(); // handled below */ 1108. alloc_itermonarr(0U); /* a request of 0 releases existing allocation */ 1109. 1110. /* level-specific data */ 1111. done_object_cleanup(); /* maybe force some OBJ_FREE items onto map */ 1112. free_current_level(); 1113. 1114. /* game-state data [ought to reorganize savegamestate() to handle this] */ 1115. free_killers(); 1116. free_timers(RANGE_GLOBAL); 1117. free_light_sources(RANGE_GLOBAL); 1118. freeobjchn(gi.invent); 1119. freeobjchn(gm.migrating_objs); 1120. freemonchn(gm.migrating_mons); 1121. freemonchn(gm.mydogs); /* ascension or dungeon escape */ 1122. /* freelevchn(); -- [folded into free_dungeons()] */ 1123. free_animals(); 1124. free_oracles(); 1125. freefruitchn(); 1126. freenames(); 1127. free_waterlevel(); 1128. free_dungeons(); 1129. free_CapMons(); 1130. free_rect(); 1131. freeroleoptvals(); /* saveoptvals(&tnhfp) */ 1132. cmdq_clear(CQ_CANNED); 1133. cmdq_clear(CQ_REPEAT); 1134. cmdbind_freeall(); 1135. free_tutorial(); /* (only needed if quitting while in tutorial) */ 1136. wish_history_flush(); 1137. 1138. /* per-turn data, but might get added to when freeing other stuff */ 1139. dobjsfree(); /* really free deleted objects */ 1140. 1141. /* some pointers in iflags */ 1142. if (iflags.wc_font_map) 1143. free((genericptr_t) iflags.wc_font_map), iflags.wc_font_map = 0; 1144. if (iflags.wc_font_message) 1145. free((genericptr_t) iflags.wc_font_message), 1146. iflags.wc_font_message = 0; 1147. if (iflags.wc_font_text) 1148. free((genericptr_t) iflags.wc_font_text), iflags.wc_font_text = 0; 1149. if (iflags.wc_font_menu) 1150. free((genericptr_t) iflags.wc_font_menu), iflags.wc_font_menu = 0; 1151. if (iflags.wc_font_status) 1152. free((genericptr_t) iflags.wc_font_status), iflags.wc_font_status = 0; 1153. if (iflags.wc_tile_file) 1154. free((genericptr_t) iflags.wc_tile_file), iflags.wc_tile_file = 0; 1155. free_autopickup_exceptions(); 1156. 1157. /* miscellaneous */ 1158. /* free_pickinv_cache(); -- now done from really_done()... */ 1159. free_symsets(); 1160. #ifdef USER_SOUNDS 1161. release_sound_mappings(); 1162. #endif 1163. #ifdef DUMPLOG_CORE 1164. dumplogfreemessages(); 1165. #endif 1166. discard_gamelog(); 1167. release_runtime_info(); /* build-time options and version stuff */ 1168. free_convert_filenames(); 1169. #endif /* FREE_ALL_MEMORY */ 1170. free_nhuuid(); 1171. 1172. if (VIA_WINDOWPORT()) 1173. status_finish(); 1174. 1175. if (options_set_window_colors_flag) 1176. options_free_window_colors(); 1177. 1178. if (glyphid_cache_status()) 1179. free_glyphid_cache(); 1180. 1181. if (tnhfp) { 1182. close_nhfile(tnhfp); 1183. tnhfp = 0; 1184. } 1185. 1186. /* last, because it frees data that might be used by panic() to provide 1187. feedback to the user; conceivably other freeing might trigger panic */ 1188. sysopt_release(); /* SYSCF strings */ 1189. } 1190. 1191. /*save.c*/