Source:NetHack 5.0.0/src/restore.c
Jump to navigation
Jump to search
Below is the full text to restore.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/restore.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 restore.c $NHDT-Date: 1736530208 2025/01/10 09:30:08 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.234 $ */ 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. #include "tcap.h" /* for TERMLIB and ASCIIGRAPH */ 8. 9. #if defined(MICRO) 10. extern int dotcnt; /* shared with save */ 11. extern int dotrow; /* shared with save */ 12. #endif 13. 14. staticfn void find_lev_obj(void); 15. staticfn void restlevchn(NHFILE *); 16. staticfn void restdamage(NHFILE *); 17. staticfn void restobj(NHFILE *, struct obj *); 18. staticfn struct obj *restobjchn(NHFILE *, boolean); 19. staticfn void restmon(NHFILE *, struct monst *); 20. staticfn struct monst *restmonchn(NHFILE *); 21. staticfn struct fruit *loadfruitchn(NHFILE *); 22. staticfn void freefruitchn(struct fruit *); 23. staticfn void rest_levl(NHFILE *); 24. staticfn void rest_stairs(NHFILE *); 25. #ifndef SFCTOOL 26. staticfn void ghostfruit(struct obj *); 27. staticfn boolean restgamestate(NHFILE *); 28. staticfn void restlevelstate(void); 29. staticfn int restlevelfile(xint8); 30. staticfn void rest_bubbles(NHFILE *); 31. staticfn void restore_gamelog(NHFILE *); 32. staticfn void reset_oattached_mids(boolean); 33. /* these ones are declared non-static in extern.h if SFCTOOL is defined */ 34. staticfn boolean restgamestate(NHFILE *); 35. staticfn void rest_bubbles(NHFILE *); 36. staticfn void restore_gamelog(NHFILE *); 37. staticfn void restore_msghistory(NHFILE *); 38. #endif 39. 40. /* 41. * Save a mapping of IDs from ghost levels to the current level. This 42. * map is used by the timer routines when restoring ghost levels. 43. */ 44. #define N_PER_BUCKET 64 45. struct bucket { 46. struct bucket *next; 47. struct { 48. unsigned gid; /* ghost ID */ 49. unsigned nid; /* new ID */ 50. } map[N_PER_BUCKET]; 51. }; 52. 53. #ifndef SFCTOOL 54. staticfn void clear_id_mapping(void); 55. staticfn void add_id_mapping(unsigned, unsigned); 56. #endif /* SFCTOOL */ 57. 58. #ifdef AMII_GRAPHICS 59. void amii_setpens(int); /* use colors from save file */ 60. extern int amii_numcolors; 61. #endif 62. 63. #include "display.h" 64. 65. #define Is_IceBox(o) ((o)->otyp == ICE_BOX ? TRUE : FALSE) 66. 67. #ifndef SFCTOOL 68. /* Recalculate svl.level.objects[x][y], since this info was not saved. */ 69. 70. staticfn void 71. find_lev_obj(void) 72. { 73. struct obj *fobjtmp = (struct obj *) 0; 74. struct obj *otmp; 75. int x, y; 76. 77. for (x = 0; x < COLNO; x++) 78. for (y = 0; y < ROWNO; y++) 79. svl.level.objects[x][y] = (struct obj *) 0; 80. 81. /* 82. * Reverse the entire fobj chain, which is necessary so that we can 83. * place the objects in the proper order. Make all obj in chain 84. * OBJ_FREE so place_object will work correctly. 85. */ 86. while ((otmp = fobj) != 0) { 87. fobj = otmp->nobj; 88. otmp->nobj = fobjtmp; 89. otmp->where = OBJ_FREE; 90. fobjtmp = otmp; 91. } 92. /* fobj should now be empty */ 93. 94. /* Set svl.level.objects (as well as reversing the chain back again) */ 95. while ((otmp = fobjtmp) != 0) { 96. fobjtmp = otmp->nobj; 97. place_object(otmp, otmp->ox, otmp->oy); 98. 99. /* fixup(s) performed when restoring the level that the hero 100. is on, rather than just an arbitrary one */ 101. if (u.uz.dlevel) { /* 0 during full restore until current level */ 102. /* handle uchain and uball when they're on the floor */ 103. if (otmp->owornmask & (W_BALL | W_CHAIN)) 104. setworn(otmp, otmp->owornmask); 105. } 106. } 107. } 108. 109. /* Things that were marked "in_use" when the game was saved (ex. via the 110. * infamous "HUP" cheat) get used up here. 111. */ 112. void 113. inven_inuse(boolean quietly) 114. { 115. struct obj *otmp, *otmp2; 116. 117. for (otmp = gi.invent; otmp; otmp = otmp2) { 118. otmp2 = otmp->nobj; 119. if (otmp->in_use) { 120. if (!quietly) 121. pline("Finishing off %s...", xname(otmp)); 122. useup(otmp); 123. } 124. } 125. } 126. 127. #endif /* SFCTOOL */ 128. 129. staticfn void 130. restlevchn(NHFILE *nhfp) 131. { 132. int cnt = 0; 133. s_level *tmplev, *x; 134. 135. svs.sp_levchn = (s_level *) 0; 136. Sfi_int(nhfp, &cnt, "levchn-lev_count"); 137. for (; cnt > 0; cnt--) { 138. tmplev = (s_level *) alloc(sizeof(s_level)); 139. Sfi_s_level(nhfp, tmplev, "levchn-s_level"); 140. 141. if (!svs.sp_levchn) 142. svs.sp_levchn = tmplev; 143. else { 144. for (x = svs.sp_levchn; x->next; x = x->next) 145. ; 146. x->next = tmplev; 147. } 148. tmplev->next = (s_level *) 0; 149. } 150. } 151. 152. staticfn void 153. restdamage(NHFILE *nhfp) 154. { 155. unsigned int dmgcount = 0; 156. int counter; 157. struct damage *tmp_dam; 158. #ifndef SFCTOOL 159. boolean ghostly = (nhfp->ftype == NHF_BONESFILE); 160. #endif 161. 162. Sfi_unsigned(nhfp, &dmgcount, "damage-damage_count"); 163. counter = (int) dmgcount; 164. 165. if (!counter) 166. return; 167. do { 168. tmp_dam = (struct damage *) alloc(sizeof *tmp_dam); 169. 170. Sfi_damage(nhfp, tmp_dam, "damage"); 171. #ifndef SFCTOOL 172. if (ghostly) 173. tmp_dam->when += (svm.moves - svo.omoves); 174. 175. tmp_dam->next = svl.level.damagelist; 176. svl.level.damagelist = tmp_dam; 177. #endif /* !SFCTOOL */ 178. } while (--counter > 0); 179. } 180. 181. /* restore one object */ 182. staticfn void 183. restobj(NHFILE *nhfp, struct obj *otmp) 184. { 185. int buflen = 0; 186. unsigned omid = 0; 187. 188. Sfi_obj(nhfp, otmp, "obj"); 189. otmp->lua_ref_cnt = 0; 190. /* next object pointers are invalid; otmp->cobj needs to be left 191. as is--being non-null is key to restoring container contents */ 192. otmp->nobj = otmp->nexthere = (struct obj *) 0; 193. /* non-null oextra needs to be reconstructed */ 194. if (otmp->oextra) { 195. otmp->oextra = newoextra(); 196. 197. /* oname - object's name */ 198. Sfi_int(nhfp, &buflen, "obj-oname_length"); 199. if (buflen > 0) { /* includes terminating '\0' */ 200. new_oname(otmp, buflen); 201. Sfi_char(nhfp, ONAME(otmp), "obj-oname", buflen); 202. } 203. 204. /* omonst - corpse or statue might retain full monster details */ 205. Sfi_int(nhfp, &buflen, "obj-omonst_length"); 206. if (buflen > 0) { 207. newomonst(otmp); 208. /* this is actually a monst struct, so we 209. can just defer to restmon() here */ 210. restmon(nhfp, OMONST(otmp)); 211. } 212. 213. /* omailcmd - feedback mechanism for scroll of mail */ 214. Sfi_int(nhfp, &buflen, "obj-omailcmd_length"); 215. if (buflen > 0) { 216. char *omailcmd = (char *) alloc(buflen); 217. 218. Sfi_char(nhfp, omailcmd, "obj-omailcmd", buflen); 219. new_omailcmd(otmp, omailcmd); 220. free((genericptr_t) omailcmd); 221. } 222. 223. /* omid - monster id number, connecting corpse to ghost */ 224. newomid(otmp); /* superfluous; we're already allocated otmp->oextra */ 225. Sfi_unsigned(nhfp, &omid, "obj-omid"); 226. OMID(otmp) = omid; 227. } 228. } 229. 230. staticfn struct obj * 231. restobjchn(NHFILE *nhfp, boolean frozen) 232. { 233. struct obj *otmp, *otmp2 = 0; 234. struct obj *first = (struct obj *) 0; 235. int buflen = 0; 236. #ifndef SFCTOOL 237. boolean ghostly = (nhfp->ftype == NHF_BONESFILE); 238. #endif 239. 240. while (1) { 241. Sfi_int(nhfp, &buflen, "obj-obj_length"); 242. if (buflen == -1) 243. break; 244. 245. otmp = newobj(); 246. assert(otmp != 0); 247. restobj(nhfp, otmp); 248. if (!first) 249. first = otmp; 250. else 251. otmp2->nobj = otmp; 252. 253. #ifndef SFCTOOL 254. if (ghostly) { 255. unsigned nid = next_ident(); 256. 257. add_id_mapping(otmp->o_id, nid); 258. otmp->o_id = nid; 259. } 260. if (ghostly && otmp->otyp == SLIME_MOLD) 261. ghostfruit(otmp); 262. /* Ghost levels get object age shifted from old player's clock 263. * to new player's clock. Assumption: new player arrived 264. * immediately after old player died. 265. */ 266. if (ghostly && !frozen && !age_is_relative(otmp)) 267. otmp->age = svm.moves - svo.omoves + otmp->age; 268. #endif /* !SFCTOOL */ 269. 270. /* get contents of a container or statue */ 271. if (Has_contents(otmp)) { 272. struct obj *otmp3; 273. 274. otmp->cobj = restobjchn(nhfp, Is_IceBox(otmp)); 275. /* restore container back pointers */ 276. for (otmp3 = otmp->cobj; otmp3; otmp3 = otmp3->nobj) 277. otmp3->ocontainer = otmp; 278. } 279. 280. #ifndef SFCTOOL 281. if (otmp->bypass) 282. otmp->bypass = 0; 283. if (!ghostly) { 284. /* fix the pointers */ 285. if (otmp->o_id == svc.context.victual.o_id) 286. svc.context.victual.piece = otmp; 287. if (otmp->o_id == svc.context.tin.o_id) 288. svc.context.tin.tin = otmp; 289. if (otmp->o_id == svc.context.spbook.o_id) 290. svc.context.spbook.book = otmp; 291. } 292. #endif /* !SFADUSTER */ 293. otmp2 = otmp; 294. } 295. if (first && otmp2->nobj) { 296. impossible("Restobjchn: error reading objchn."); 297. otmp2->nobj = 0; 298. } 299. #ifdef SFCTOOL 300. nhUse(frozen); 301. #endif 302. return first; 303. } 304. 305. /* restore one monster */ 306. staticfn void 307. restmon(NHFILE *nhfp, struct monst *mtmp) 308. { 309. int buflen = 0, mc = 0; 310. 311. Sfi_monst(nhfp, mtmp, "monst"); 312. 313. /* next monster pointer is invalid */ 314. mtmp->nmon = (struct monst *) 0; 315. /* non-null mextra needs to be reconstructed */ 316. if (mtmp->mextra) { 317. mtmp->mextra = newmextra(); 318. 319. /* mgivenname - monster's name */ 320. Sfi_int(nhfp, &buflen, "monst-mgivenname_length"); 321. if (buflen > 0) { /* includes terminating '\0' */ 322. new_mgivenname(mtmp, buflen); 323. Sfi_char(nhfp, MGIVENNAME(mtmp), "monst-mgivenname", (int) buflen); 324. } 325. /* egd - vault guard */ 326. Sfi_int(nhfp, &buflen, "monst-egd_length"); 327. if (buflen > 0) { 328. newegd(mtmp); 329. Sfi_egd(nhfp, EGD(mtmp), "monst-egd"); 330. } 331. /* epri - temple priest */ 332. Sfi_int(nhfp, &buflen, "monst-epri_length"); 333. if (buflen > 0) { 334. newepri(mtmp); 335. Sfi_epri(nhfp, EPRI(mtmp), "monst-epri"); 336. } 337. /* eshk - shopkeeper */ 338. Sfi_int(nhfp, &buflen, "monst-eshk_length"); 339. if (buflen > 0) { 340. neweshk(mtmp); 341. Sfi_eshk(nhfp, ESHK(mtmp), "monst-eshk"); 342. } 343. /* emin - minion */ 344. Sfi_int(nhfp, &buflen, "monst-emin_length"); 345. if (buflen > 0) { 346. newemin(mtmp); 347. Sfi_emin(nhfp, EMIN(mtmp), "monst-emin"); 348. } 349. /* edog - pet */ 350. Sfi_int(nhfp, &buflen, "monst-edog_length"); 351. if (buflen > 0) { 352. newedog(mtmp); 353. Sfi_edog(nhfp, EDOG(mtmp), "monst-edog"); 354. /* save or bones held a relative time */ 355. relative_time_to_moves(&EDOG(mtmp)->droptime); 356. relative_time_to_moves(&EDOG(mtmp)->hungrytime); 357. /* sanity check to prevent rn2(0) */ 358. if (EDOG(mtmp)->apport <= 0) { 359. EDOG(mtmp)->apport = 1; 360. } 361. } 362. /* ebones */ 363. Sfi_int(nhfp, &buflen, "monst-ebones_length"); 364. if (buflen > 0) { 365. newebones(mtmp); 366. Sfi_ebones(nhfp, EBONES(mtmp), "monst-ebones"); 367. } 368. /* mcorpsenm - obj->corpsenm for mimic posing as corpse or 369. statue (inline int rather than pointer to something) */ 370. Sfi_int(nhfp, &mc, "monst-mcorpsenm"); 371. MCORPSENM(mtmp) = mc; 372. } /* mextra */ 373. } 374. 375. staticfn struct monst * 376. restmonchn(NHFILE *nhfp) 377. { 378. struct monst *mtmp, *mtmp2 = 0; 379. struct monst *first = (struct monst *) 0; 380. int buflen = 0; 381. #ifndef SFCTOOL 382. int offset; 383. boolean ghostly = (nhfp->ftype == NHF_BONESFILE); 384. #endif 385. 386. while (1) { 387. Sfi_int(nhfp, &buflen, "monst-monst_length"); 388. if (buflen == -1) 389. break; 390. 391. mtmp = newmonst(); 392. assert(mtmp != 0); 393. restmon(nhfp, mtmp); 394. if (!first) 395. first = mtmp; 396. else 397. mtmp2->nmon = mtmp; 398. 399. #ifndef SFCTOOL 400. if (ghostly) { 401. unsigned nid = next_ident(); 402. 403. add_id_mapping(mtmp->m_id, nid); 404. mtmp->m_id = nid; 405. } 406. offset = mtmp->mnum; 407. mtmp->data = &mons[offset]; 408. if (ghostly) { 409. int mndx = (mtmp->cham == NON_PM) ? monsndx(mtmp->data) 410. : mtmp->cham; 411. 412. if (propagate(mndx, TRUE, ghostly) == 0) { 413. /* cookie to trigger purge in getbones() */ 414. mtmp->mhpmax = DEFUNCT_MONSTER; 415. } 416. } 417. #endif /* !SFCTOOL */ 418. 419. if (mtmp->minvent) { 420. struct obj *obj; 421. mtmp->minvent = restobjchn(nhfp, FALSE); 422. #ifndef SFCTOOL 423. /* restore monster back pointer */ 424. for (obj = mtmp->minvent; obj; obj = obj->nobj) 425. obj->ocarry = mtmp; 426. #else 427. nhUse(obj); 428. #endif 429. } 430. 431. #ifndef SFCTOOL 432. if (mtmp->mw) { 433. struct obj *obj; 434. 435. for (obj = mtmp->minvent; obj; obj = obj->nobj) 436. if (obj->owornmask & W_WEP) 437. break; 438. if (obj) 439. mtmp->mw = obj; 440. else { 441. MON_NOWEP(mtmp); 442. impossible("bad monster weapon restore"); 443. } 444. } 445. 446. if (mtmp->isshk) 447. restshk(mtmp, ghostly); 448. if (mtmp->ispriest) 449. restpriest(mtmp, ghostly); 450. 451. if (!ghostly) { 452. if (mtmp->m_id == svc.context.polearm.m_id) 453. svc.context.polearm.hitmon = mtmp; 454. } 455. #endif /* !SFCTOOL */ 456. mtmp2 = mtmp; 457. } 458. #ifndef SFCTOOL 459. if (first && mtmp2->nmon) { 460. impossible("Restmonchn: error reading monchn."); 461. mtmp2->nmon = 0; 462. } 463. #endif 464. return first; 465. } 466. 467. staticfn struct fruit * 468. loadfruitchn(NHFILE *nhfp) 469. { 470. struct fruit *flist, *fnext; 471. 472. flist = 0; 473. for (;;) { 474. fnext = newfruit(); 475. Sfi_fruit(nhfp, fnext, "fruit"); 476. if (fnext->fid != 0) { 477. fnext->nextf = flist; 478. flist = fnext; 479. } else 480. break; 481. } 482. dealloc_fruit(fnext); 483. return flist; 484. } 485. 486. staticfn void 487. freefruitchn(struct fruit *flist) 488. { 489. struct fruit *fnext; 490. 491. while (flist) { 492. fnext = flist->nextf; 493. dealloc_fruit(flist); 494. flist = fnext; 495. } 496. } 497. 498. #ifndef SFCTOOL 499. staticfn void 500. ghostfruit(struct obj *otmp) 501. { 502. struct fruit *oldf; 503. 504. for (oldf = go.oldfruit; oldf; oldf = oldf->nextf) 505. if (oldf->fid == otmp->spe) 506. break; 507. 508. if (!oldf) 509. impossible("no old fruit?"); 510. else 511. otmp->spe = fruitadd(oldf->fname, (struct fruit *) 0); 512. } 513. #endif /* !SFCTOOL */ 514. 515. #ifdef SYSCF 516. #define SYSOPT_CHECK_SAVE_UID sysopt.check_save_uid 517. #else 518. #define SYSOPT_CHECK_SAVE_UID TRUE 519. #endif 520. 521. #ifndef SFCTOOL 522. staticfn 523. #endif 524. boolean 525. restgamestate(NHFILE *nhfp) 526. { 527. int i; 528. struct flag newgameflags; 529. struct context_info newgamecontext; /* all 0, but has some pointers */ 530. struct obj *bc_obj; 531. char timebuf[15]; 532. unsigned long uid = 0; 533. #ifndef SFCTOOL 534. boolean defer_perm_invent, restoring_special; 535. struct obj *otmp; 536. #endif 537. 538. Sfi_ulong(nhfp, &uid, "gamestate-uid"); 539. Sfi_char(nhfp, &svn.nhuuid[0], "nhuuid", sizeof svn.nhuuid); 540. Sfi_long(nhfp, &svm.moves, "gamestate-moves"); 541. #ifndef SFCTOOL 542. if (SYSOPT_CHECK_SAVE_UID 543. && uid != (unsigned long) getuid()) { /* strange ... */ 544. if (!gc.converted_savefile_loaded) 545. /* for wizard mode, issue a reminder; for others, treat it 546. * as an attempt to cheat and refuse to restore this file */ 547. pline("Saved game was not yours."); 548. if (wizard || gc.converted_savefile_loaded) { 549. if (gc.converted_savefile_loaded) 550. gc.converted_savefile_loaded = FALSE; 551. } else { 552. return FALSE; 553. } 554. } 555. #endif /* SFCTOOL */ 556. newgamecontext = svc.context; /* copy statically init'd context */ 557. Sfi_context_info(nhfp, &svc.context, "gamestate-context"); 558. relative_time_to_moves(&svc.context.seer_turn); 559. relative_time_to_moves(&svc.context.digging.lastdigtime); 560. svc.context.warntype.species = (ismnum(svc.context.warntype.speciesidx)) 561. ? &mons[svc.context.warntype.speciesidx] 562. : (struct permonst *) 0; 563. /* context.victual.piece, .tin.tin, .spellbook.book, and .polearm.hitmon 564. are pointers which get set to Null during save and will be recovered 565. via corresponding o_id or m_id while objs or mons are being restored */ 566. 567. /* we want to be able to revert to command line/environment/config 568. file option values instead of keeping old save file option values 569. if partial restore fails and we resort to starting a new game */ 570. newgameflags = flags; 571. Sfi_flag(nhfp, &flags, "gamestate-flags"); 572. 573. #ifndef SFCTOOL 574. /* avoid keeping permanent inventory window up to date during restore 575. (setworn() calls update_inventory); attempting to include the cost 576. of unpaid items before shopkeeper's bill is available is a no-no; 577. named fruit names aren't accessible yet either 578. [3.6.2: moved perm_invent from flags to iflags to keep it out of 579. save files; retaining the override here is simpler than trying 580. to figure out where it really belongs now] */ 581. defer_perm_invent = iflags.perm_invent; 582. iflags.perm_invent = FALSE; 583. /* wizard and discover are actually flags.debug and flags.explore; 584. player might be overriding the save file values for them; 585. in the discover case, we don't want to set that for a normal 586. game until after the save file has been removed */ 587. iflags.deferred_X = (newgameflags.explore && !discover); 588. restoring_special = (wizard || discover); 589. if (newgameflags.debug) { 590. /* authorized by startup code; wizard mode exists and is allowed */ 591. wizard = TRUE, discover = iflags.deferred_X = FALSE; 592. } else if (restoring_special) { 593. /* specified by save file; check authorization now. */ 594. set_playmode(); 595. } 596. role_init(); /* Reset the initial role, race, gender, and alignment */ 597. #ifdef AMII_GRAPHICS 598. amii_setpens(amii_numcolors); /* use colors from save file */ 599. #endif 600. #endif /* !SFCTOOL */ 601. Sfi_long(nhfp, &svw.wreserve, "wreserve"); 602. Sfi_int32(nhfp, &svw.wtreserved, "wtreserved"); 603. Sfi_you(nhfp, &u, "gamestate-you"); 604. gy.youmonst.cham = u.mcham; 605. 606. #ifndef SFCTOOL 607. if (restoring_special && iflags.explore_error_flag) { 608. /* savefile has wizard or explore mode, but player is no longer 609. authorized to access either; can't downgrade mode any further, so 610. fail restoration. */ 611. u.uhp = 0; 612. } 613. #endif 614. 615. Sfi_char(nhfp, timebuf, "gamestate-ubirthday", 14); 616. timebuf[14] = '\0'; 617. ubirthday = time_from_yyyymmddhhmmss(timebuf); 618. Sfi_long(nhfp, &urealtime.realtime, "gamestate-realtime"); 619. Sfi_char(nhfp, timebuf, "gamestate-start_timing", 14); 620. timebuf[14] = '\0'; 621. #ifndef SFCTOOL 622. urealtime.start_timing = time_from_yyyymmddhhmmss(timebuf); 623. 624. /* current time is the time to use for next urealtime.realtime update */ 625. urealtime.start_timing = getnow(); 626. 627. set_uasmon(); 628. #ifdef CLIPPING 629. cliparound(u.ux, u.uy); 630. #endif 631. if (u.uhp <= 0 && (!Upolyd || u.mh <= 0)) { 632. u.ux = u.uy = 0; /* affects pline() [hence You()] */ 633. You("were not healthy enough to survive restoration."); 634. /* wiz1_level.dlevel is used by mklev.c to see if lots of stuff is 635. * uninitialized, so we only have to set it and not the other stuff. 636. */ 637. wiz1_level.dlevel = 0; 638. u.uz.dnum = 0; 639. u.uz.dlevel = 1; 640. /* revert to pre-restore option settings */ 641. iflags.deferred_X = FALSE; 642. iflags.perm_invent = defer_perm_invent; 643. flags = newgameflags; 644. svc.context = newgamecontext; 645. gy.youmonst = cg.zeromonst; 646. return FALSE; 647. } 648. /* in case hangup save occurred in midst of level change */ 649. assign_level(&u.uz0, &u.uz); 650. #endif /* !SFCTOOL */ 651. 652. /* this stuff comes after potential aborted restore attempts */ 653. restore_killers(nhfp); 654. restore_timers(nhfp, RANGE_GLOBAL, 0L); 655. restore_light_sources(nhfp); 656. 657. gi.invent = restobjchn(nhfp, FALSE); 658. 659. /* restore dangling (not on floor or in inventory) ball and/or chain */ 660. bc_obj = restobjchn(nhfp, FALSE); 661. #ifndef SFCTOOL 662. while (bc_obj) { 663. struct obj *nobj = bc_obj->nobj; 664. 665. bc_obj->nobj = (struct obj *) 0; 666. if (bc_obj->owornmask) 667. setworn(bc_obj, bc_obj->owornmask); 668. bc_obj = nobj; 669. } 670. #endif 671. gm.migrating_objs = restobjchn(nhfp, FALSE); 672. gm.migrating_mons = restmonchn(nhfp); 673. 674. for (i = 0; i < NUMMONS; ++i) { 675. Sfi_mvitals(nhfp, &svm.mvitals[i], "gamestate-mvitals"); 676. } 677. 678. #ifndef SFCTOOL 679. /* 680. * There are some things after this that can have unintended display 681. * side-effects too early in the game. 682. * Disable see_monsters() here, re-enable it at the top of moveloop() 683. */ 684. gd.defer_see_monsters = TRUE; 685. 686. /* this comes after inventory has been loaded */ 687. for (otmp = gi.invent; otmp; otmp = otmp->nobj) 688. if (otmp->owornmask) 689. setworn(otmp, otmp->owornmask); 690. 691. /* reset weapon so that player will get a reminder about "bashing" 692. during next fight when bare-handed or wielding an unconventional 693. item; for pick-axe, we aren't able to distinguish between having 694. applied or wielded it, so be conservative and assume the former */ 695. otmp = uwep; /* `uwep' usually init'd by setworn() in loop above */ 696. uwep = 0; /* clear it and have setuwep() reinit */ 697. setuwep(otmp); /* (don't need any null check here) */ 698. if (!uwep || uwep->otyp == PICK_AXE || uwep->otyp == GRAPPLING_HOOK) 699. gu.unweapon = TRUE; 700. #endif /* !SFCTOOL */ 701. 702. restore_dungeon(nhfp); 703. restlevchn(nhfp); 704. /* hero_seq isn't saved and restored because it can be recalculated */ 705. gh.hero_seq = svm.moves << 3; /* normally handled in moveloop() */ 706. Sfi_q_score(nhfp, &svq.quest_status, "gamestate-quest_status"); 707. 708. for (i = 0; i < (MAXSPELL + 1); ++i) { 709. Sfi_spell(nhfp, &svs.spl_book[i], "gamestate-spl_book"); 710. } 711. restore_artifacts(nhfp); 712. restore_oracles(nhfp); 713. Sfi_char(nhfp, svp.pl_character, 714. "gamestate-pl_character", sizeof svp.pl_character); 715. Sfi_char(nhfp, svp.pl_fruit, "gamestate-pl_fruit", sizeof svp.pl_fruit); 716. freefruitchn(gf.ffruit); /* clean up fruit(s) made by initoptions() */ 717. gf.ffruit = loadfruitchn(nhfp); 718. 719. restnames(nhfp); 720. restore_msghistory(nhfp); 721. restore_gamelog(nhfp); 722. restore_luadata(nhfp); 723. #ifndef SFCTOOL 724. /* must come after all mons & objs are restored */ 725. relink_timers(FALSE); 726. relink_light_sources(FALSE); 727. adj_erinys(u.ualign.abuse); 728. /* inventory display is now viable */ 729. iflags.perm_invent = defer_perm_invent; 730. #else 731. nhUse(bc_obj); 732. nhUse(newgamecontext); 733. nhUse(newgameflags); 734. #endif /* !SFCTOOL */ 735. return TRUE; 736. } 737. 738. #ifndef SFCTOOL 739. /* update game state pointers to those valid for the current level (so we 740. don't dereference a wild u.ustuck when saving game state, for instance) */ 741. staticfn void 742. restlevelstate(void) 743. { 744. /* 745. * Note: restoring steed and engulfer/holder/holdee is now handled 746. * in getlev() and there's nothing left for restlevelstate() to do. 747. */ 748. return; 749. } 750. 751. /* after getlev(), put current level into a level/lock file; 752. essential when splitting a save file into individual level files */ 753. /*ARGSUSED*/ 754. staticfn int 755. restlevelfile(xint8 ltmp) 756. { 757. char whynot[BUFSZ]; 758. NHFILE *nhfp = (NHFILE *) 0; 759. 760. nhfp = create_levelfile(ltmp, whynot); 761. if (!nhfp) { 762. /* failed to create a new file; don't attempt to make a panic save */ 763. program_state.something_worth_saving = 0; 764. panic("restlevelfile: %s", whynot); 765. } 766. bufon(nhfp->fd); 767. nhfp->mode = WRITING | FREEING; 768. savelev(nhfp, ltmp); 769. close_nhfile(nhfp); 770. return 2; 771. } 772. 773. /* 774. * restore_saved_game() prior to this left us at this position in 775. * the savefile for dorecover(): 776. * 777. * format indicator (1 byte) 778. * n = count of critical size list (1 byte) 779. * n bytes of critical sizes (n bytes) 780. * version info 781. * --> plnametmp = player name size (int, 2 bytes) 782. * player name (PL_NSIZ_PLUS) 783. * current level (including pets) 784. * (non-level-based) game state 785. * other levels 786. */ 787. 788. int 789. dorecover(NHFILE *nhfp) 790. { 791. xint8 ltmp = 0; 792. int rtmp; 793. 794. /* suppress map display if some part of the code tries to update that */ 795. program_state.restoring = REST_GSTATE; 796. 797. get_plname_from_file(nhfp, svp.plname, TRUE); 798. /* 799. * The position in the save file is now here: 800. * 801. * format indicator (1 byte) 802. * n = count of critical size list (1 byte) 803. * n bytes of critical sizes (n bytes) 804. * version info 805. * plnametmp = player name size (int, 2 bytes) 806. * player name (PL_NSIZ_PLUS) 807. * --> current level (including pets) 808. * (non-level-based) game state 809. * other levels 810. */ 811. getlev(nhfp, 0, (xint8) 0); 812. if (!restgamestate(nhfp)) { 813. NHFILE *tnhfp = get_freeing_nhfile(); 814. 815. display_nhwindow(WIN_MESSAGE, TRUE); 816. savelev(tnhfp, 0); /* discard current level */ 817. close_nhfile(tnhfp); 818. close_nhfile(nhfp); 819. (void) delete_savefile(); 820. u.usteed_mid = u.ustuck_mid = 0; 821. program_state.restoring = 0; 822. return 0; 823. } 824. /* after restgamestate() -> restnames() so that 'bases[]' is populated */ 825. init_oclass_probs(); /* recompute go.oclass_prob_totals[] */ 826. 827. restlevelstate(); 828. #ifdef INSURANCE 829. savestateinlock(); 830. #endif 831. rtmp = restlevelfile(ledger_no(&u.uz)); 832. if (rtmp < 2) 833. return rtmp; /* dorecover called recursively */ 834. 835. program_state.restoring = REST_LEVELS; 836. 837. /* these pointers won't be valid while we're processing the 838. * other levels, but they'll be reset again by restlevelstate() 839. * afterwards, and in the meantime at least u.usteed may mislead 840. * place_monster() on other levels 841. */ 842. u.ustuck = (struct monst *) 0; 843. u.usteed = (struct monst *) 0; 844. 845. #ifdef MICRO 846. #ifdef AMII_GRAPHICS 847. { 848. extern struct window_procs amii_procs; 849. if (WINDOWPORT(amii)) { 850. extern winid WIN_BASE; 851. clear_nhwindow(WIN_BASE); /* hack until there's a hook for this */ 852. } 853. } 854. #else 855. clear_nhwindow(WIN_MAP); 856. #endif 857. clear_nhwindow(WIN_MESSAGE); 858. You("return to level %d in %s%s.", depth(&u.uz), 859. svd.dungeons[u.uz.dnum].dname, 860. flags.debug ? " while in debug mode" 861. : flags.explore ? " while in explore mode" : ""); 862. curs(WIN_MAP, 1, 1); 863. dotcnt = 0; 864. dotrow = 2; 865. if (!WINDOWPORT(X11)) 866. putstr(WIN_MAP, 0, "Restoring:"); 867. #endif 868. restoreinfo.mread_flags = 1; /* return despite error */ 869. while (1) { 870. Sfi_xint8(nhfp, <mp, "gamestate-level_number"); 871. if (nhfp->eof) 872. break; 873. getlev(nhfp, 0, ltmp); 874. #ifdef MICRO 875. curs(WIN_MAP, 1 + dotcnt++, dotrow); 876. if (dotcnt >= (COLNO - 1)) { 877. dotrow++; 878. dotcnt = 0; 879. } 880. if (!WINDOWPORT(X11)) { 881. putstr(WIN_MAP, 0, "."); 882. } 883. mark_synch(); 884. #endif 885. rtmp = restlevelfile(ltmp); 886. if (rtmp < 2) 887. return rtmp; /* dorecover called recursively */ 888. } 889. restoreinfo.mread_flags = 0; 890. 891. rewind_nhfile(nhfp); /* return to beginning of file */ 892. (void) validate(nhfp, (char *) 0, FALSE); 893. get_plname_from_file(nhfp, svp.plname, TRUE); 894. 895. /* not 0 nor REST_GSTATE nor REST_LEVELS */ 896. program_state.restoring = REST_CURRENT_LEVEL; 897. 898. getlev(nhfp, 0, (xint8) 0); 899. close_nhfile(nhfp); 900. restlevelstate(); 901. program_state.something_worth_saving = 1; /* useful data now exists */ 902. 903. if (!wizard && !discover) 904. (void) delete_savefile(); 905. if (Is_rogue_level(&u.uz)) 906. assign_graphics(ROGUESET); 907. reset_glyphmap(gm_levelchange); 908. max_rank_sz(); /* to recompute gm.mrank_sz (botl.c) */ 909. 910. if ((uball && !uchain) || (uchain && !uball)) { 911. impossible("restgamestate: lost ball & chain"); 912. /* poor man's unpunish() */ 913. setworn((struct obj *) 0, W_CHAIN); 914. setworn((struct obj *) 0, W_BALL); 915. } 916. 917. /* in_use processing must be after: 918. * + The inventory has been read so that freeinv() works. 919. * + The current level has been restored so billing information 920. * is available. 921. */ 922. inven_inuse(FALSE); 923. 924. /* Set up the vision internals, after levl[] data is loaded 925. but before docrt(). */ 926. reglyph_darkroom(); 927. vision_reset(); 928. gv.vision_full_recalc = 1; /* recompute vision (not saved) */ 929. 930. run_timers(); /* expire all timers that have gone off while away */ 931. program_state.restoring = 0; /* affects bot() so clear before docrt() */ 932. 933. if (ge.early_raw_messages && !program_state.beyond_savefile_load) { 934. /* 935. * We're about to obliterate some potentially important 936. * startup messages, so give the player a chance to see them. 937. */ 938. ge.early_raw_messages = 0; 939. wait_synch(); 940. } 941. u.usteed_mid = u.ustuck_mid = 0; 942. program_state.beyond_savefile_load = 1; 943. 944. docrt(); 945. clear_nhwindow(WIN_MESSAGE); 946. 947. /* Success! */ 948. welcome(FALSE); 949. check_special_room(FALSE); 950. return 1; 951. } 952. #endif /* !SFCTOOL */ 953. 954. staticfn void 955. rest_stairs(NHFILE *nhfp) 956. { 957. int buflen = 0; 958. stairway stway = UNDEFINED_VALUES; 959. #ifndef SFCTOOL 960. stairway *newst; 961. #endif 962. 963. #ifndef SFCTOOL 964. stairway_free_all(); 965. #endif 966. while (1) { 967. Sfi_int(nhfp, &buflen, "stairs-staircount"); 968. if (buflen == -1) 969. break; 970. 971. Sfi_stairway(nhfp, &stway, "stairs-stairway"); 972. if (program_state.restoring != REST_GSTATE 973. && stway.tolev.dnum == u.uz.dnum) { 974. /* stairway dlevel is relative, make it absolute */ 975. stway.tolev.dlevel += u.uz.dlevel; 976. } 977. #ifndef SFCTOOL 978. stairway_add(stway.sx, stway.sy, stway.up, stway.isladder, 979. &(stway.tolev)); 980. newst = stairway_at(stway.sx, stway.sy); 981. if (newst) 982. newst->u_traversed = stway.u_traversed; 983. #endif 984. } 985. } 986. 987. void 988. restcemetery(NHFILE *nhfp, struct cemetery **cemeteryaddr) 989. { 990. struct cemetery *bonesinfo, **bonesaddr; 991. int cflag = 0; 992. 993. Sfi_int(nhfp, &cflag, "cemetery-cemetery_flag"); 994. if (cflag == 0) { 995. bonesaddr = cemeteryaddr; 996. do { 997. bonesinfo = (struct cemetery *) alloc(sizeof *bonesinfo); 998. Sfi_cemetery(nhfp, bonesinfo, "cemetery-bonesinfo"); 999. *bonesaddr = bonesinfo; 1000. bonesaddr = &(*bonesaddr)->next; 1001. } while (*bonesaddr); 1002. } else { 1003. *cemeteryaddr = 0; 1004. } 1005. if (((nhfp->mode & CONVERTING) != 0) 1006. || ((nhfp->mode & UNCONVERTING) != 0)) { 1007. struct cemetery *thisbones, *nextbones; 1008. 1009. /* free the memory */ 1010. nextbones = *cemeteryaddr; 1011. while ((thisbones = nextbones) != 0) { 1012. nextbones = thisbones->next; 1013. free((genericptr_t)thisbones); 1014. } 1015. *cemeteryaddr = 0; 1016. } 1017. } 1018. 1019. /*ARGSUSED*/ 1020. staticfn void 1021. rest_levl(NHFILE *nhfp) 1022. { 1023. int c, r; 1024. 1025. for (c = 0; c < COLNO; ++c) { 1026. for (r = 0; r < ROWNO; ++r) { 1027. Sfi_rm(nhfp, &levl[c][r], "location-rm"); 1028. } 1029. } 1030. } 1031. 1032. #ifndef SFCTOOL 1033. 1034. void 1035. trickery(char *reason) 1036. { 1037. pline("Strange, this map is not as I remember it."); 1038. pline("Somebody is trying some trickery here..."); 1039. pline("This game is void."); 1040. Strcpy(svk.killer.name, reason ? reason : ""); 1041. done(TRICKED); 1042. } 1043. #endif /* !SFCTOOL */ 1044. 1045. void 1046. getlev(NHFILE *nhfp, int pid, xint8 lev) 1047. { 1048. struct trap *trap; 1049. #ifndef SFCTOOL 1050. struct monst *mtmp; 1051. branch *br; 1052. int x, y; 1053. #endif 1054. long elapsed = 0L; 1055. int hpid = 0; 1056. xint8 dlvl = 0; 1057. int i, c, r; 1058. boolean ghostly = (nhfp->ftype == NHF_BONESFILE); 1059. coord *tmpc = 0; 1060. #ifdef TOS 1061. short tlev; 1062. #endif 1063. 1064. program_state.in_getlev = TRUE; 1065. #ifndef SFCTOOL 1066. 1067. if (ghostly) 1068. clear_id_mapping(); 1069. #endif /* !SFCTOOL */ 1070. 1071. #if 0 1072. #if defined(MSDOS) || defined(OS2) 1073. if (nhfp->structlevel) 1074. setmode(nhfp->fd, O_BINARY); 1075. #endif 1076. #endif 1077. 1078. /* Load the old fruit info. We have to do it first, so the 1079. * information is available when restoring the objects. 1080. */ 1081. if (ghostly) 1082. go.oldfruit = loadfruitchn(nhfp); 1083. 1084. /* First some sanity checks */ 1085. Sfi_int(nhfp, &hpid, "gamestate-hackpid"); 1086. /* CHECK: This may prevent restoration */ 1087. Sfi_xint8(nhfp, &dlvl, "gamestate-dlvl"); 1088. #ifndef SFCTOOL 1089. if ((pid && pid != hpid) || (lev && dlvl != lev)) { 1090. char trickbuf[BUFSZ]; 1091. 1092. if (pid && pid != hpid) 1093. Sprintf(trickbuf, "PID (%d) doesn't match saved PID (%d)!", hpid, 1094. pid); 1095. else 1096. Sprintf(trickbuf, "This is level %d, not %d!", dlvl, lev); 1097. if (wizard) 1098. pline1(trickbuf); 1099. trickery(trickbuf); 1100. } 1101. #endif 1102. restcemetery(nhfp, &svl.level.bonesinfo); 1103. rest_levl(nhfp); 1104. 1105. for (c = 0; c < COLNO; ++c) { 1106. for (r = 0; r < ROWNO; ++r) { 1107. Sfi_schar(nhfp, &svl.lastseentyp[c][r], "lastseentyp"); 1108. } 1109. } 1110. Sfi_long(nhfp, &svo.omoves, "lev-timestmp"); 1111. elapsed = (svm.moves - svo.omoves); 1112. 1113. rest_stairs(nhfp); 1114. Sfi_dest_area(nhfp, &svu.updest, "lev-updest"); 1115. Sfi_dest_area(nhfp, &svd.dndest, "lev-dndest"); 1116. Sfi_levelflags(nhfp, &svl.level.flags, "lev-level_flags"); 1117. rest_adjust_levelflags(); 1118. if (svd.doors) { 1119. free(svd.doors); 1120. svd.doors = 0; 1121. } 1122. 1123. Sfi_int(nhfp, &svd.doors_alloc, "lev-doors_alloc"); 1124. if (svd.doors_alloc) { /* avoid pointless alloc(0) */ 1125. svd.doors = (coord *) alloc(svd.doors_alloc * sizeof (coord)); 1126. tmpc = svd.doors; 1127. for (i = 0; i < svd.doors_alloc; ++i) { 1128. Sfi_nhcoord(nhfp, tmpc, "lev-doors"); 1129. tmpc++; 1130. } 1131. } 1132. rest_rooms(nhfp); /* No joke :-) */ 1133. if (svn.nroom) { 1134. #ifndef SFCTOOL 1135. gd.doorindex = svr.rooms[svn.nroom - 1].fdoor 1136. + svr.rooms[svn.nroom - 1].doorct; 1137. } else { 1138. gd.doorindex = 0; 1139. #else 1140. gd.doorindex = 0; 1141. #endif /* !SFCTOOL */ 1142. } 1143. 1144. restore_timers(nhfp, RANGE_LEVEL, elapsed); 1145. restore_light_sources(nhfp); 1146. fmon = restmonchn(nhfp); 1147. rest_worm(nhfp); /* restore worm information */ 1148. 1149. gf.ftrap = 0; 1150. for (;;) { 1151. trap = newtrap(); 1152. Sfi_trap(nhfp, trap, "trap"); 1153. if (trap->tx != 0) { 1154. if (program_state.restoring != REST_GSTATE 1155. && trap->dst.dnum == u.uz.dnum) { 1156. /* convert relative destination to absolute */ 1157. trap->dst.dlevel += u.uz.dlevel; 1158. } 1159. trap->ntrap = gf.ftrap; 1160. gf.ftrap = trap; 1161. } else 1162. break; 1163. } 1164. dealloc_trap(trap); 1165. 1166. fobj = restobjchn(nhfp, FALSE); 1167. #ifndef SFCTOOL 1168. find_lev_obj(); 1169. #endif /* !SFCTOOL */ 1170. /* restobjchn()'s `frozen' argument probably ought to be a callback 1171. routine so that we can check for objects being buried under ice */ 1172. svl.level.buriedobjlist = restobjchn(nhfp, FALSE); 1173. gb.billobjs = restobjchn(nhfp, FALSE); 1174. rest_engravings(nhfp); 1175. 1176. #ifndef SFCTOOL 1177. /* reset level.monsters for new level */ 1178. for (x = 0; x < COLNO; x++) 1179. for (y = 0; y < ROWNO; y++) 1180. svl.level.monsters[x][y] = (struct monst *) 0; 1181. for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) { 1182. if (mtmp->isshk) 1183. set_residency(mtmp, FALSE); 1184. if (mtmp->m_id == u.usteed_mid) { 1185. /* steed is kept on fmon list but off the map */ 1186. u.usteed = mtmp; 1187. u.usteed_mid = 0; 1188. } else { 1189. if (mtmp->m_id == u.ustuck_mid) { 1190. set_ustuck(mtmp); 1191. u.ustuck_mid = 0; 1192. } 1193. place_monster(mtmp, mtmp->mx, mtmp->my); 1194. if (mtmp->wormno) 1195. place_wsegs(mtmp, NULL); 1196. if (hides_under(mtmp->data) && mtmp->mundetected) 1197. (void) hideunder(mtmp); 1198. } 1199. 1200. /* regenerate monsters while on another level */ 1201. if (!u.uz.dlevel || program_state.restoring == REST_LEVELS) 1202. continue; 1203. if (ghostly) { 1204. /* reset peaceful/malign relative to new character; 1205. shopkeepers will reset based on name */ 1206. if (!mtmp->isshk) 1207. mtmp->mpeaceful = (is_unicorn(mtmp->data) 1208. && (sgn(u.ualign.type) 1209. == sgn(mtmp->data->maligntyp))) ? 1 1210. : peace_minded(mtmp->data); 1211. set_malign(mtmp); 1212. } else if (elapsed > 0L) { 1213. mon_catchup_elapsed_time(mtmp, elapsed); 1214. } 1215. /* update shape-changers in case protection against 1216. them is different now than when the level was saved */ 1217. restore_cham(mtmp); 1218. /* give hiders a chance to hide before their next move */ 1219. if (ghostly || (elapsed > 0L && elapsed > (long) rnd(10))) 1220. hide_monst(mtmp); 1221. } 1222. #endif /* !SFCTOOL */ 1223. 1224. restdamage(nhfp); 1225. rest_regions(nhfp); 1226. rest_bubbles(nhfp); /* for water and air; empty marker on other levels */ 1227. load_exclusions(nhfp); 1228. rest_track(nhfp); 1229. 1230. #ifndef SFCTOOL 1231. if (ghostly) { 1232. stairway *stway = gs.stairs; 1233. while (stway) { 1234. if (!stway->isladder && !stway->up 1235. && stway->tolev.dnum == u.uz.dnum) 1236. break; 1237. stway = stway->next; 1238. } 1239. 1240. /* Now get rid of all the temp fruits... */ 1241. freefruitchn(go.oldfruit), go.oldfruit = 0; 1242. 1243. if (lev > ledger_no(&medusa_level) 1244. && lev < ledger_no(&stronghold_level) && !stway) { 1245. coord cc; 1246. d_level dest; 1247. 1248. dest.dnum = u.uz.dnum; 1249. dest.dlevel = u.uz.dlevel + 1; 1250. 1251. mazexy(&cc); 1252. stairway_add(cc.x, cc.y, FALSE, FALSE, &dest); 1253. levl[cc.x][cc.y].typ = STAIRS; 1254. } 1255. 1256. br = Is_branchlev(&u.uz); 1257. if (br && u.uz.dlevel == 1) { 1258. d_level ltmp; 1259. 1260. if (on_level(&u.uz, &br->end1)) 1261. assign_level(<mp, &br->end2); 1262. else 1263. assign_level(<mp, &br->end1); 1264. 1265. switch (br->type) { 1266. case BR_STAIR: 1267. case BR_NO_END1: 1268. case BR_NO_END2: 1269. stway = gs.stairs; 1270. while (stway) { 1271. if (stway->tolev.dnum != u.uz.dnum) 1272. break; 1273. stway = stway->next; 1274. } 1275. if (stway) 1276. assign_level(&(stway->tolev), <mp); 1277. break; 1278. case BR_PORTAL: /* max of 1 portal per level */ 1279. for (trap = gf.ftrap; trap; trap = trap->ntrap) 1280. if (trap->ttyp == MAGIC_PORTAL) 1281. break; 1282. if (!trap) 1283. panic("getlev: need portal but none found"); 1284. assign_level(&trap->dst, <mp); 1285. break; 1286. } 1287. } else if (!br) { 1288. struct trap *ttmp = 0; 1289. 1290. /* Remove any dangling portals. */ 1291. for (trap = gf.ftrap; trap; trap = ttmp) { 1292. ttmp = trap->ntrap; 1293. if (trap->ttyp == MAGIC_PORTAL) 1294. deltrap(trap); 1295. } 1296. } 1297. } 1298. /* must come after all mons & objs are restored */ 1299. relink_timers(ghostly); 1300. relink_light_sources(ghostly); 1301. reset_oattached_mids(ghostly); 1302. 1303. if (ghostly) 1304. clear_id_mapping(); 1305. program_state.in_getlev = FALSE; 1306. #else 1307. nhUse(pid); 1308. nhUse(lev); 1309. #endif /* !SFCTOOL */ 1310. program_state.in_getlev = FALSE; 1311. } 1312. 1313. void 1314. rest_adjust_levelflags(void) 1315. { 1316. /* adjust timestamps */ 1317. relative_time_to_moves(&svl.level.flags.stasis_until); 1318. } 1319. void 1320. moves_to_relative_time(long *timestamp) 1321. { 1322. long prevts = *timestamp; 1323. 1324. *timestamp = prevts - svm.moves; 1325. } 1326. 1327. void 1328. relative_time_to_moves(long *timestamp) 1329. { 1330. long prevts = *timestamp; 1331. 1332. *timestamp = svm.moves + prevts; 1333. } 1334. 1335. /* "name-role-race-gend-algn" occurs very early in a save file; sometimes we 1336. want the whole thing, other times just "name" (for svp.plname[]) */ 1337. void 1338. get_plname_from_file( 1339. NHFILE *nhfp, 1340. char *outbuf, /* size must be at least [PL_NSIZ_PLUS] even if name_only */ 1341. boolean name_only) /* True: just name; False: name-role-race-gend-algn */ 1342. { 1343. char plbuf[PL_NSIZ_PLUS]; 1344. int pltmpsiz = 0; 1345. 1346. plbuf[0] = '\0'; 1347. 1348. Sfi_int(nhfp, &pltmpsiz, "plname-size"); 1349. /* pltmpsiz should now be PL_NSIZ_PLUS */ 1350. Sfi_char(nhfp, plbuf, "plname", pltmpsiz); 1351. /* plbuf[PL_NSIZ_PLUS-2] should be '\0'; 1352. plbuf[PL_NSIZ_PLUS-1] should be '-' or 'X' or 'D' */ 1353. /* "-race-role-gend-algn" is already present except that it has been 1354. hidden by replacing the initial dash with NUL; if we want that 1355. information, replace the NUL with a dash */ 1356. if (!name_only) 1357. *eos(plbuf) = '-'; 1358. /* not simple strcpy(); playmode is in the last slot and could (probably 1359. will) be preceded by NULs */ 1360. (void) memcpy((genericptr_t) outbuf, (genericptr_t) plbuf, PL_NSIZ_PLUS); 1361. return; 1362. } 1363. 1364. /* restore Plane of Water's air bubbles and Plane of Air's clouds */ 1365. #ifndef SFCTOOL 1366. staticfn 1367. #endif 1368. void 1369. rest_bubbles(NHFILE *nhfp) 1370. { 1371. xint8 bbubbly; 1372. 1373. /* whether or not the Plane of Water's air bubbles or Plane of Air's 1374. clouds are present is recorded during save so that we don't have to 1375. know what level is being restored */ 1376. bbubbly = 0; 1377. Sfi_xint8(nhfp, &bbubbly, "bubbles-bbubbly"); 1378. #if 0 1379. if (nhfp->structlevel) 1380. xxread(nhfp->fd, &bbubbly, sizeof bbubbly); 1381. #endif 1382. if (bbubbly) 1383. restore_waterlevel(nhfp); 1384. } 1385. 1386. #ifndef SFCTOOL 1387. staticfn 1388. #endif 1389. void 1390. restore_gamelog(NHFILE *nhfp) 1391. { 1392. int slen = 0; 1393. char msg[BUFSZ*2]; 1394. struct gamelog_line tmp = { 0 }; 1395. 1396. while (1) { 1397. Sfi_int(nhfp, &slen, "gamelog-length"); 1398. if (slen == -1) 1399. break; 1400. if (slen > ((BUFSZ*2) - 1)) 1401. panic("restore_gamelog: msg too big (%d)", slen); 1402. Sfi_char(nhfp, msg, "gamelog-gamelog_text", slen); 1403. msg[slen] = '\0'; 1404. Sfi_gamelog_line(nhfp, &tmp, "gamelog-gamelog_line"); 1405. #ifndef SFCTOOL 1406. gamelog_add(tmp.flags, tmp.turn, msg); 1407. #endif /* !SFCTOOL */ 1408. } 1409. } 1410. 1411. #ifndef SFCTOOL 1412. staticfn 1413. #endif 1414. void 1415. restore_msghistory(NHFILE *nhfp) 1416. { 1417. int msgsize = 0; 1418. #ifndef SFCTOOL 1419. int msgcount = 0; 1420. #endif 1421. char msg[BUFSZ]; 1422. 1423. while (1) { 1424. Sfi_int(nhfp, &msgsize, "msghistory-length"); 1425. if (msgsize == -1) 1426. break; 1427. if (msgsize > BUFSZ - 1) 1428. panic("restore_msghistory: msg too big (%d)", msgsize); 1429. Sfi_char(nhfp, msg, "msghistory-msg", msgsize); 1430. msg[msgsize] = '\0'; 1431. #ifndef SFCTOOL 1432. putmsghistory(msg, TRUE); 1433. ++msgcount; 1434. #endif /* !SFCTOOL */ 1435. } 1436. #ifndef SFCTOOL 1437. if (msgcount) 1438. putmsghistory((char *) 0, TRUE); 1439. debugpline1("Read %d messages from savefile.", msgcount); 1440. #endif /* !SFCTOOL */ 1441. } 1442. 1443. #ifndef SFCTOOL 1444. 1445. /* Clear all structures for object and monster ID mapping. */ 1446. staticfn void 1447. clear_id_mapping(void) 1448. { 1449. struct bucket *curr; 1450. 1451. while ((curr = gi.id_map) != 0) { 1452. gi.id_map = curr->next; 1453. free((genericptr_t) curr); 1454. } 1455. gn.n_ids_mapped = 0; 1456. } 1457. 1458. /* Add a mapping to the ID map. */ 1459. staticfn void 1460. add_id_mapping(unsigned int gid, unsigned int nid) 1461. { 1462. int idx; 1463. 1464. idx = gn.n_ids_mapped % N_PER_BUCKET; 1465. /* idx is zero on first time through, as well as when a new bucket is */ 1466. /* needed */ 1467. if (idx == 0) { 1468. struct bucket *gnu = (struct bucket *) alloc(sizeof(struct bucket)); 1469. gnu->next = gi.id_map; 1470. gi.id_map = gnu; 1471. } 1472. 1473. gi.id_map->map[idx].gid = gid; 1474. gi.id_map->map[idx].nid = nid; 1475. gn.n_ids_mapped++; 1476. } 1477. 1478. /* 1479. * Global routine to look up a mapping. If found, return TRUE and fill 1480. * in the new ID value. Otherwise, return false and return -1 in the new 1481. * ID. 1482. */ 1483. boolean 1484. lookup_id_mapping(unsigned int gid, unsigned int *nidp) 1485. { 1486. int i; 1487. struct bucket *curr; 1488. 1489. if (gn.n_ids_mapped) 1490. for (curr = gi.id_map; curr; curr = curr->next) { 1491. /* first bucket might not be totally full */ 1492. if (curr == gi.id_map) { 1493. i = gn.n_ids_mapped % N_PER_BUCKET; 1494. if (i == 0) 1495. i = N_PER_BUCKET; 1496. } else 1497. i = N_PER_BUCKET; 1498. 1499. while (--i >= 0) 1500. if (gid == curr->map[i].gid) { 1501. *nidp = curr->map[i].nid; 1502. return TRUE; 1503. } 1504. } 1505. 1506. return FALSE; 1507. } 1508. 1509. staticfn void 1510. reset_oattached_mids(boolean ghostly) 1511. { 1512. struct obj *otmp; 1513. unsigned oldid, nid; 1514. 1515. for (otmp = fobj; otmp; otmp = otmp->nobj) { 1516. if (ghostly && has_omonst(otmp)) { 1517. struct monst *mtmp = OMONST(otmp); 1518. 1519. mtmp->m_id = 0; 1520. mtmp->mpeaceful = mtmp->mtame = 0; /* pet's owner died! */ 1521. } 1522. if (ghostly && has_omid(otmp)) { 1523. oldid = OMID(otmp); 1524. if (lookup_id_mapping(oldid, &nid)) 1525. OMID(otmp) = nid; 1526. else 1527. free_omid(otmp); 1528. } 1529. } 1530. } 1531. 1532. #ifdef SELECTSAVED 1533. /* put up a menu listing each character from this player's saved games; 1534. returns 1: use svp.plname[], 0: new game, -1: quit */ 1535. int 1536. restore_menu( 1537. winid bannerwin) /* if not WIN_ERR, clear window 1538. * and show copyright in menu */ 1539. { 1540. winid tmpwin; 1541. anything any; 1542. char **saved, *next, mode, menutext[BUFSZ]; 1543. boolean all_normal; 1544. menu_item *chosen_game = (menu_item *) 0; 1545. int k, clet, ch = 0; /* ch: 0 => new game */ 1546. int clr = NO_COLOR; 1547. 1548. *svp.plname = '\0'; 1549. saved = get_saved_games(); /* array of character names */ 1550. if (saved && *saved) { 1551. tmpwin = create_nhwindow(NHW_MENU); 1552. start_menu(tmpwin, MENU_BEHAVE_STANDARD); 1553. any = cg.zeroany; /* no selection */ 1554. if (bannerwin != WIN_ERR) { 1555. /* for tty; erase copyright notice and redo it in the menu */ 1556. clear_nhwindow(bannerwin); 1557. /* COPYRIGHT_BANNER_[ABCD] */ 1558. for (k = 1; k <= 4; ++k) 1559. add_menu_str(tmpwin, copyright_banner_line(k)); 1560. add_menu_str(tmpwin, ""); 1561. } 1562. add_menu_str(tmpwin, "Select one of your saved games"); 1563. /* if all the save files have a playmode of '-' then we'll just list 1564. their character name-role-race-gend-algn values, but if any are 1565. 'X' or 'D', we'll list playmode along with name-role-&c values 1566. for every entry; first, figure out if they're all normal play */ 1567. for (all_normal = TRUE, k = 0; all_normal && saved[k]; ++k) { 1568. next = saved[k]; 1569. mode = next[PL_NSIZ_PLUS - 1]; /* fixed last char, beyond '\0' */ 1570. if (mode != '-') 1571. all_normal = FALSE; 1572. } 1573. for (k = 0; saved[k]; ++k) { 1574. any.a_int = k + 1; 1575. next = saved[k]; 1576. mode = next[PL_NSIZ_PLUS - 1]; 1577. if (all_normal) 1578. Sprintf(menutext, "%.*s", PL_NSIZ_PLUS - 1, next); 1579. else 1580. Sprintf(menutext, "%c %.*s", mode, PL_NSIZ_PLUS - 1, next); 1581. add_menu(tmpwin, &nul_glyphinfo, &any, 0, 0, ATR_NONE, clr, 1582. menutext, MENU_ITEMFLAGS_SKIPMENUCOLORS); 1583. } 1584. clet = (k <= 'n' - 'a') ? 'n' /* new game */ 1585. : (k <= 26 + 'N' - 'A') ? 'N' : 0; 1586. any.a_int = -1; /* not >= 0 */ 1587. add_menu(tmpwin, &nul_glyphinfo, &any, clet, 'N', ATR_NONE, clr, 1588. "Start a new character", MENU_ITEMFLAGS_NONE); 1589. clet = (k + 1 <= 'q' - 'a' && clet == 'n') ? 'q' /* quit */ 1590. : (k + 1 <= 26 + 'Q' - 'A' && clet == 'N') ? 'Q' : 0; 1591. any.a_int = -2; 1592. add_menu(tmpwin, &nul_glyphinfo, &any, clet, 'Q', ATR_NONE, clr, 1593. "Never mind (quit)", MENU_ITEMFLAGS_SELECTED); 1594. /* no prompt on end_menu, as we've done our own at the top */ 1595. end_menu(tmpwin, (char *) 0); 1596. if (select_menu(tmpwin, PICK_ONE, &chosen_game) > 0) { 1597. ch = chosen_game->item.a_int; 1598. if (ch > 0) 1599. Strcpy(svp.plname, saved[ch - 1]); 1600. else if (ch < 0) 1601. ++ch; /* -1 -> 0 (new game), -2 -> -1 (quit) */ 1602. free((genericptr_t) chosen_game); 1603. } else { 1604. ch = -1; /* quit menu without making a selection => quit */ 1605. } 1606. destroy_nhwindow(tmpwin); 1607. if (bannerwin != WIN_ERR) { 1608. /* for tty; clear the menu away and put subset of copyright back 1609. */ 1610. clear_nhwindow(bannerwin); 1611. /* COPYRIGHT_BANNER_A, preceding "Who are you?" prompt */ 1612. if (ch == 0) 1613. putstr(bannerwin, 0, copyright_banner_line(1)); 1614. } 1615. } 1616. free_saved_games(saved); 1617. return (ch > 0) ? 1 : ch; 1618. } 1619. #endif /* SELECTSAVED */ 1620. #endif /* !SFCTOOL */ 1621. 1622. /*restore.c*/