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