Source:NetHack 5.0.0/src/bones.c
Jump to navigation
Jump to search
Below is the full text to bones.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/bones.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 bones.c $NHDT-Date: 1701500709 2023/12/02 07:05:09 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.129 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985,1993. */ 3. /*-Copyright (c) Robert Patrick Rankin, 2012. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #include "hack.h" 7. 8. #ifndef SFCTOOL 9. staticfn boolean no_bones_level(d_level *); 10. staticfn void goodfruit(int); 11. staticfn void resetobjs(struct obj *, boolean); 12. staticfn void give_to_nearby_mon(struct obj *, coordxy, coordxy) NONNULLARG1; 13. staticfn boolean fixuporacle(struct monst *) NONNULLARG1; 14. staticfn void remove_mon_from_bones(struct monst *) NONNULLARG1; 15. staticfn void set_ghostly_objlist(struct obj *objchain); 16. 17. staticfn boolean 18. no_bones_level(d_level *lev) 19. { 20. s_level *sptr; 21. 22. if (ledger_no(&gs.save_dlevel)) 23. assign_level(lev, &gs.save_dlevel); 24. 25. return (boolean) (((sptr = Is_special(lev)) != 0 && !sptr->boneid) 26. || !svd.dungeons[lev->dnum].boneid 27. /* no bones on the last or multiway branch levels 28. in any dungeon (level 1 isn't multiway) */ 29. || Is_botlevel(lev) 30. || (Is_branchlev(lev) && lev->dlevel > 1) 31. /* no bones in the invocation level */ 32. || (In_hell(lev) 33. && lev->dlevel == dunlevs_in_dungeon(lev) - 1)); 34. } 35. 36. /* Call this function for each fruit object saved in the bones level: it marks 37. * that particular type of fruit as existing (the marker is that that type's 38. * ID is positive instead of negative). This way, when we later save the 39. * chain of fruit types, we know to only save the types that exist. 40. */ 41. staticfn void 42. goodfruit(int id) 43. { 44. struct fruit *f = fruit_from_indx(-id); 45. 46. if (f) 47. f->fid = id; 48. } 49. 50. staticfn void 51. resetobjs(struct obj *ochain, boolean restore) 52. { 53. struct obj *otmp, *nobj; 54. 55. for (otmp = ochain; otmp; otmp = nobj) { 56. nobj = otmp->nobj; 57. if (otmp->cobj) 58. resetobjs(otmp->cobj, restore); 59. if (otmp->in_use) { 60. obj_extract_self(otmp); 61. dealloc_obj(otmp); 62. continue; 63. } 64. 65. if (restore) { 66. /* artifact bookkeeping needs to be done during 67. restore; other fixups are done while saving */ 68. if (otmp->oartifact) { 69. if (exist_artifact(otmp->otyp, safe_oname(otmp)) 70. || is_quest_artifact(otmp)) { 71. /* prevent duplicate--revert to ordinary obj */ 72. otmp->oartifact = 0; 73. if (has_oname(otmp)) 74. free_oname(otmp); 75. } else { 76. artifact_exists(otmp, safe_oname(otmp), TRUE, 77. ONAME_BONES); 78. } 79. } else if (has_oname(otmp)) { 80. sanitize_name(ONAME(otmp)); 81. } 82. /* 3.6.3: set no_charge for partly eaten food in shop; 83. all other items become goods for sale if in a shop */ 84. if (otmp->oclass == FOOD_CLASS && otmp->oeaten) { 85. struct obj *top; 86. char *p; 87. coordxy ox, oy; 88. 89. for (top = otmp; top->where == OBJ_CONTAINED; 90. top = top->ocontainer) 91. continue; 92. otmp->no_charge = (top->where == OBJ_FLOOR 93. && get_obj_location(top, &ox, &oy, 0) 94. /* can't use costly_spot() since its 95. result depends upon hero's location */ 96. && inside_shop(ox, oy) 97. && *(p = in_rooms(ox, oy, SHOPBASE)) 98. && tended_shop(&svr.rooms[*p - ROOMOFFSET])); 99. } 100. } else { /* saving */ 101. /* do not zero out o_ids for ghost levels anymore */ 102. 103. if (objects[otmp->otyp].oc_uses_known) 104. otmp->known = 0; 105. otmp->dknown = otmp->bknown = 0; 106. otmp->rknown = 0; 107. otmp->lknown = 0; 108. otmp->cknown = 0; 109. otmp->tknown = 0; 110. otmp->invlet = 0; 111. otmp->no_charge = 0; 112. otmp->how_lost = LOST_NONE; 113. 114. /* strip user-supplied names */ 115. /* Statue and some corpse names are left intact, 116. presumably in case they came from score file. 117. [TODO: this ought to be done differently--names 118. which came from such a source or came from any 119. stoned or killed monster should be flagged in 120. some manner; then we could just check the flag 121. here and keep "real" names (dead pets, &c) while 122. discarding player notes attached to statues.] */ 123. if (has_oname(otmp) 124. && !(otmp->oartifact || otmp->otyp == STATUE 125. || otmp->otyp == SPE_NOVEL 126. || (otmp->otyp == CORPSE 127. && otmp->corpsenm >= SPECIAL_PM))) { 128. free_oname(otmp); 129. } 130. 131. if (otmp->otyp == SLIME_MOLD) { 132. goodfruit(otmp->spe); 133. #ifdef MAIL_STRUCTURES 134. } else if (otmp->otyp == SCR_MAIL) { 135. /* 0: delivered in-game via external event; 136. 1: from bones or wishing; 2: written with marker */ 137. if (otmp->spe == 0) 138. otmp->spe = 1; 139. #endif 140. } else if (otmp->otyp == EGG) { 141. otmp->spe = 0; /* not "laid by you" in next game */ 142. } else if (otmp->otyp == TIN) { 143. /* make tins of unique monster's meat be empty */ 144. if (ismnum(otmp->corpsenm) 145. && unique_corpstat(&mons[otmp->corpsenm])) 146. otmp->corpsenm = NON_PM; 147. } else if (otmp->otyp == CORPSE || otmp->otyp == STATUE) { 148. int mnum = otmp->corpsenm; 149. 150. /* Discard incarnation details of unique monsters 151. (by passing null instead of otmp for object), 152. shopkeepers (by passing false for revival flag), 153. temple priests, and vault guards in order to 154. prevent corpse revival or statue reanimation. */ 155. if (has_omonst(otmp) 156. && cant_revive(&mnum, FALSE, (struct obj *) 0)) { 157. free_omonst(otmp); 158. /* mnum is now either human_zombie or doppelganger; 159. for corpses of uniques, we need to force the 160. transformation now rather than wait until a 161. revival attempt, otherwise eating this corpse 162. would behave as if it remains unique */ 163. if (mnum == PM_DOPPELGANGER && otmp->otyp == CORPSE) 164. set_corpsenm(otmp, mnum); 165. } 166. } else if (is_mines_prize(otmp) || is_soko_prize(otmp)) { 167. /* achievement tracking; in case prize was moved off its 168. original level (which is always a no-bones level) */ 169. otmp->nomerge = 0; 170. } else if (otmp->otyp == AMULET_OF_YENDOR) { 171. /* no longer the real Amulet */ 172. otmp->otyp = FAKE_AMULET_OF_YENDOR; 173. curse(otmp); 174. } else if (otmp->otyp == CANDELABRUM_OF_INVOCATION) { 175. if (otmp->lamplit) 176. end_burn(otmp, TRUE); 177. otmp->otyp = WAX_CANDLE; 178. otmp->age = 50L; /* assume used */ 179. if (otmp->spe > 0) 180. otmp->quan = (long) otmp->spe; 181. otmp->spe = 0; 182. otmp->owt = weight(otmp); 183. curse(otmp); 184. } else if (otmp->otyp == BELL_OF_OPENING) { 185. otmp->otyp = BELL; 186. curse(otmp); 187. } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) { 188. otmp->otyp = SPE_BLANK_PAPER; 189. curse(otmp); 190. } 191. } 192. } 193. } 194. 195. /* while loading bones, strip out text possibly supplied by old player 196. that might accidentally or maliciously disrupt new player's display */ 197. void 198. sanitize_name(char *namebuf) 199. { 200. int c; 201. boolean strip_8th_bit = (WINDOWPORT(tty) 202. && !iflags.wc_eight_bit_input); 203. 204. /* it's tempting to skip this for single-user platforms, since 205. only the current player could have left these bones--except 206. things like "hearse" and other bones exchange schemes make 207. that assumption false */ 208. while (*namebuf) { 209. c = *namebuf & 0177; 210. if (c < ' ' || c == '\177') { 211. /* non-printable or undesirable */ 212. *namebuf = '.'; 213. } else if (c != *namebuf) { 214. /* expected to be printable if user wants such things */ 215. if (strip_8th_bit) 216. *namebuf = '_'; 217. } 218. ++namebuf; 219. } 220. } 221. 222. /* Give object to a random object-liking monster on or adjacent to x,y 223. but skipping hero's location. 224. If no such monster, place object on floor at x,y. */ 225. staticfn void 226. give_to_nearby_mon(struct obj *otmp, coordxy x, coordxy y) 227. { 228. struct monst *mtmp; 229. struct monst *selected = (struct monst *) 0; 230. int nmon = 0, xx, yy; 231. 232. for (xx = x - 1; xx <= x + 1; ++xx) { 233. for (yy = y - 1; yy <= y + 1; ++yy) { 234. if (!isok(xx, yy)) 235. continue; 236. if (u_at(xx, yy)) 237. continue; 238. if (!(mtmp = m_at(xx, yy))) 239. continue; 240. /* This doesn't do any checks on otmp to see that it matches the 241. * likes_* property, intentionally. Assume that the monster is 242. * rifling through and taking things that look interesting. */ 243. if (!(likes_gold(mtmp->data) || likes_gems(mtmp->data) 244. || likes_objs(mtmp->data) || likes_magic(mtmp->data))) 245. continue; 246. nmon++; 247. if (!rn2(nmon)) 248. selected = mtmp; 249. } 250. } 251. if (selected && can_carry(selected, otmp)) 252. add_to_minv(selected, otmp); 253. else 254. place_object(otmp, x, y); 255. } 256. 257. /* called by savebones(); also by finish_paybill(shk.c) */ 258. void 259. drop_upon_death( 260. struct monst *mtmp, /* monster if hero rises as one (non ghost) */ 261. struct obj *cont, /* container if hero is turned into a statue */ 262. coordxy x, coordxy y) 263. { 264. struct obj *otmp; 265. 266. /* when dual-wielding, the second weapon gets dropped rather than 267. welded if it becomes cursed; ensure that that won't happen here 268. by ending dual-wield */ 269. u.twoweap = FALSE; /* bypass set_twoweap() */ 270. 271. /* all inventory is dropped (for the normal case), even non-droppable 272. things like worn armor and accessories, welded weapon, or cursed 273. loadstones */ 274. while ((otmp = gi.invent) != 0) { 275. obj_extract_self(otmp); 276. /* when turning into green slime, all gear remains held; 277. other types "arise from the dead" do aren't holding 278. equipment during their brief interval as a corpse */ 279. if (!mtmp || is_undead(mtmp->data)) 280. obj_no_longer_held(otmp); 281. 282. /* lamps don't go out when dropped */ 283. if ((cont || artifact_light(otmp)) && obj_is_burning(otmp)) 284. end_burn(otmp, TRUE); /* smother in statue */ 285. otmp->owornmask = 0L; 286. 287. if (otmp->otyp == SLIME_MOLD) 288. goodfruit(otmp->spe); 289. 290. if (rn2(5)) 291. curse(otmp); 292. if (mtmp) 293. (void) add_to_minv(mtmp, otmp); 294. else if (cont) 295. (void) add_to_container(cont, otmp); 296. else if (!rn2(8)) 297. give_to_nearby_mon(otmp, x, y); 298. else 299. place_object(otmp, x, y); 300. } 301. if (cont) 302. cont->owt = weight(cont); 303. } 304. 305. /* possibly restore oracle's room and/or put her back inside it; returns 306. False if she's on the wrong level and should be removed, True otherwise */ 307. staticfn boolean 308. fixuporacle(struct monst *oracle) 309. { 310. coord cc; 311. int ridx, o_ridx; 312. 313. /* oracle doesn't move, but knight's joust or monk's staggering blow 314. could push her onto a hole in the floor; at present, traps don't 315. activate in such situation hence she won't fall to another level; 316. however, that could change so be prepared to cope with such things */ 317. if (!Is_oracle_level(&u.uz)) 318. return FALSE; 319. 320. oracle->mpeaceful = 1; /* for behavior toward next character */ 321. o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET; 322. if (o_ridx >= 0 && svr.rooms[o_ridx].rtype == DELPHI) 323. return TRUE; /* no fixup needed */ 324. 325. /* 326. * The Oracle isn't in DELPHI room. Either hero entered her chamber 327. * and got the one-time welcome message, converting it into an 328. * ordinary room, or she got teleported out, or both. Try to put 329. * her back inside her room, if necessary, and restore its type. 330. */ 331. 332. /* find original delphi chamber; should always succeed */ 333. for (ridx = 0; ridx < SIZE(svr.rooms); ++ridx) 334. if (svr.rooms[ridx].orig_rtype == DELPHI) 335. break; 336. 337. if (o_ridx != ridx && ridx < SIZE(svr.rooms)) { 338. /* room found and she's not in it, so try to move her there */ 339. cc.x = (svr.rooms[ridx].lx + svr.rooms[ridx].hx) / 2; 340. cc.y = (svr.rooms[ridx].ly + svr.rooms[ridx].hy) / 2; 341. if (enexto(&cc, cc.x, cc.y, oracle->data)) { 342. rloc_to(oracle, cc.x, cc.y); 343. o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET; 344. } 345. /* [if her room is already full, she might end up outside; 346. that's ok, next hero just won't get any welcome message, 347. same as used to happen before this fixup was introduced] */ 348. } 349. if (ridx == o_ridx) /* if she's in her room, mark it as such */ 350. svr.rooms[ridx].rtype = DELPHI; 351. return TRUE; /* keep oracle in new bones file */ 352. } 353. 354. /* check whether bones are feasible */ 355. boolean 356. can_make_bones(void) 357. { 358. struct trap *ttmp; 359. 360. if (!flags.bones) 361. return FALSE; 362. if (ledger_no(&u.uz) <= 0 || ledger_no(&u.uz) > maxledgerno()) 363. return FALSE; 364. if (no_bones_level(&u.uz)) 365. return FALSE; /* no bones for specific levels */ 366. if (u.uswallow) { 367. return FALSE; /* no bones when swallowed */ 368. } 369. if (!Is_branchlev(&u.uz)) { 370. /* no bones on non-branches with portals */ 371. for (ttmp = gf.ftrap; ttmp; ttmp = ttmp->ntrap) 372. if (ttmp->ttyp == MAGIC_PORTAL) 373. return FALSE; 374. } 375. 376. if (depth(&u.uz) <= 0 /* bulletproofing for endgame */ 377. || (!rn2(1 + (depth(&u.uz) >> 2)) /* fewer ghosts on low levels */ 378. && !wizard)) 379. return FALSE; 380. /* don't let multiple restarts generate multiple copies of objects 381. in bones files */ 382. if (discover) 383. return FALSE; 384. return TRUE; 385. } 386. 387. /* monster might need to be removed before saving a bones file, 388. in case these characters are not in their home bases */ 389. staticfn void 390. remove_mon_from_bones(struct monst *mtmp) 391. { 392. struct permonst *mptr = mtmp->data; 393. 394. if (mtmp->iswiz || mptr == &mons[PM_MEDUSA] 395. || mptr->msound == MS_NEMESIS || mptr->msound == MS_LEADER 396. || is_Vlad(mtmp) /* mptr == &mons[VLAD_THE_IMPALER] || cham == VLAD */ 397. || (mptr == &mons[PM_ORACLE] && !fixuporacle(mtmp))) 398. mongone(mtmp); 399. } 400. 401. /* save bones and possessions of a deceased adventurer */ 402. void 403. savebones(int how, time_t when, struct obj *corpse) 404. { 405. coordxy x, y; 406. struct trap *ttmp; 407. struct monst *mtmp; 408. struct fruit *f; 409. struct cemetery *newbones; 410. char c, *bonesid; 411. char whynot[BUFSZ]; 412. NHFILE *nhfp; 413. 414. /* caller has already checked `can_make_bones()' */ 415. 416. clear_bypasses(); 417. nhfp = open_bonesfile(&u.uz, &bonesid); 418. if (nhfp) { 419. close_nhfile(nhfp); 420. if (wizard) { 421. if (y_n("Bones file already exists. Replace it?") == 'y') { 422. if (delete_bonesfile(&u.uz)) 423. goto make_bones; 424. else 425. pline("Cannot unlink old bones."); 426. } 427. } 428. /* compression can change the file's name, so must 429. wait until after any attempt to delete this file */ 430. compress_bonesfile(); 431. return; 432. } 433. 434. make_bones: 435. unleash_all(); 436. /* new ghost or other undead isn't punished even if hero was; 437. end-of-game disclosure has already had a chance to report the 438. Punished status so we don't need to preserve it any further */ 439. if (Punished) 440. unpunish(); /* unwear uball, destroy uchain */ 441. /* in case dismounting kills steed [is that even possible?], do so 442. before cleaning up dead monsters */ 443. if (u.usteed) 444. dismount_steed(DISMOUNT_BONES); 445. 446. iter_mons(remove_mon_from_bones); /* send various unique monsters away, */ 447. dmonsfree(); /* then discard dead or gone monsters */ 448. 449. forget_engravings(); /* next hero won't have read any engravings yet */ 450. /* mark all named fruits as nonexistent; if/when we come to instances 451. of any of them we'll mark those as existing (using goodfruit()) */ 452. for (f = gf.ffruit; f; f = f->nextf) 453. f->fid = -f->fid; 454. 455. set_ghostly_objlist(gi.invent); 456. /* dispose of your possessions, usually cursed */ 457. if (ismnum(u.ugrave_arise)) { 458. /* give your possessions to the monster you become */ 459. gi.in_mklev = TRUE; /* use <u.ux,u.uy> as-is */ 460. mtmp = makemon(&mons[u.ugrave_arise], u.ux, u.uy, NO_MINVENT); 461. gi.in_mklev = FALSE; 462. if (!mtmp) { /* arise-type might have been genocided */ 463. drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy); 464. u.ugrave_arise = NON_PM; /* in case caller cares */ 465. return; 466. } 467. give_u_to_m_resistances(mtmp); 468. mtmp = christen_monst(mtmp, svp.plname); 469. newsym(u.ux, u.uy); 470. /* ["Your body rises from the dead as an <mname>..." used 471. to be given here, but it has been moved to done() so that 472. it gets delivered even when savebones() isn't called] */ 473. drop_upon_death(mtmp, (struct obj *) 0, u.ux, u.uy); 474. /* 'mtmp' now has hero's inventory; if 'mtmp' is a mummy, give it 475. a wrapping unless already carrying one */ 476. if (mtmp->data->mlet == S_MUMMY && !m_carrying(mtmp, MUMMY_WRAPPING)) 477. (void) mongets(mtmp, MUMMY_WRAPPING); 478. m_dowear(mtmp, TRUE); 479. } else if (u.ugrave_arise == LEAVESTATUE) { 480. struct obj *otmp; 481. 482. /* embed your possessions in your statue */ 483. otmp = mk_named_object(STATUE, &mons[u.umonnum], u.ux, u.uy, 484. svp.plname); 485. 486. drop_upon_death((struct monst *) 0, otmp, u.ux, u.uy); 487. if (!otmp) 488. return; /* couldn't make statue */ 489. mtmp = (struct monst *) 0; 490. } else { /* u.ugrave_arise < LEAVESTATUE */ 491. /* drop everything */ 492. drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy); 493. /* trick makemon() into allowing monster creation 494. * on your location 495. */ 496. gi.in_mklev = TRUE; 497. mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME); 498. gi.in_mklev = FALSE; 499. if (!mtmp) 500. return; 501. mtmp = christen_monst(mtmp, svp.plname); 502. if (corpse) 503. (void) obj_attach_mid(corpse, mtmp->m_id); 504. } 505. if (mtmp) { 506. int i; 507. 508. mtmp->m_lev = (u.ulevel ? u.ulevel : 1); 509. mtmp->mhp = mtmp->mhpmax = u.uhpmax; 510. mtmp->female = flags.female; 511. mtmp->msleeping = 1; 512. 513. if (!has_ebones(mtmp)) 514. newebones(mtmp); 515. if (has_ebones(mtmp)) { 516. for (i = 0; i <= NUM_ROLES; ++i) { 517. if (!strcmp(gu.urole.name.m, roles[i].name.m)) { 518. EBONES(mtmp)->role = i; 519. break; 520. } 521. /* impossible("savebones: bad gu.urole.name.m \"%s\"", 522. gu.urole.name.m); */ 523. } 524. for (i = 0; i <= NUM_RACES; ++i) { 525. if (!strcmp(gu.urace.noun, races[i].noun)) { 526. EBONES(mtmp)->race = i; 527. break; 528. } 529. /* impossible("savebones: bad gu.urace.noun \"%s\"", 530. gu.urace.noun); */ 531. } 532. EBONES(mtmp)->oldalign = u.ualign; 533. EBONES(mtmp)->deathlevel = u.ulevel; 534. EBONES(mtmp)->luck = u.uluck; /* moreluck not included */ 535. EBONES(mtmp)->mnum = Role_switch; 536. EBONES(mtmp)->female = flags.female; 537. EBONES(mtmp)->demigod = u.uevent.udemigod; 538. EBONES(mtmp)->crowned = u.uevent.uhand_of_elbereth; 539. } 540. } 541. for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) { 542. set_ghostly_objlist(mtmp->minvent); 543. resetobjs(mtmp->minvent, FALSE); 544. /* do not zero out m_ids for bones levels any more */ 545. mtmp->mlstmv = 0L; 546. if (mtmp->mtame) 547. mtmp->mtame = mtmp->mpeaceful = 0; 548. /* observations about the current hero won't apply to future game */ 549. mtmp->seen_resistance = M_SEEN_NOTHING; 550. } 551. for (ttmp = gf.ftrap; ttmp; ttmp = ttmp->ntrap) { 552. ttmp->madeby_u = 0; 553. ttmp->tseen = unhideable_trap(ttmp->ttyp); 554. } 555. set_ghostly_objlist(fobj); 556. resetobjs(fobj, FALSE); 557. set_ghostly_objlist(svl.level.buriedobjlist); 558. resetobjs(svl.level.buriedobjlist, FALSE); 559. 560. /* Hero is no longer on the map. */ 561. u.ux0 = u.ux, u.uy0 = u.uy; 562. u.ux = u.uy = 0; 563. 564. /* Clear all memory from the level. */ 565. for (x = 1; x < COLNO; x++) 566. for (y = 0; y < ROWNO; y++) { 567. levl[x][y].seenv = 0; 568. levl[x][y].waslit = 0; 569. levl[x][y].glyph = GLYPH_UNEXPLORED; 570. svl.lastseentyp[x][y] = 0; 571. } 572. 573. /* Attach bones info to the current level before saving. */ 574. newbones = (struct cemetery *) alloc(sizeof *newbones); 575. /* entries are '\0' terminated but have fixed length allocations, 576. so pre-fill with spaces to initialize any excess room */ 577. (void) memset((genericptr_t) newbones, ' ', sizeof *newbones); 578. /* format name+role,&c, death reason, and date+time; 579. gender and alignment reflect final values rather than what the 580. character started out as, same as topten and logfile entries */ 581. Sprintf(newbones->who, "%s-%.3s-%.3s-%.3s-%.3s", 582. svp.plname, gu.urole.filecode, 583. gu.urace.filecode, genders[flags.female].filecode, 584. aligns[1 - u.ualign.type].filecode); 585. formatkiller(newbones->how, sizeof newbones->how, how, TRUE); 586. Strcpy(newbones->when, yyyymmddhhmmss(when)); 587. /* final resting place, used to decide when bones are discovered */ 588. newbones->frpx = u.ux0, newbones->frpy = u.uy0; 589. newbones->bonesknown = FALSE; 590. /* if current character died on a bones level, the cemetery list 591. will have multiple entries, most recent (this dead hero) first */ 592. newbones->next = svl.level.bonesinfo; 593. svl.level.bonesinfo = newbones; 594. /* flag these bones if they are being created in wizard mode; 595. they might already be flagged as such, even when we're playing 596. in normal mode, if this level came from a previous bones file */ 597. if (wizard) 598. svl.level.flags.wizard_bones = 1; 599. 600. nhfp = create_bonesfile(&u.uz, &bonesid, whynot); 601. if (!nhfp) { 602. if (wizard) 603. pline1(whynot); 604. /* bones file creation problems are silent to the player. 605. * Keep it that way, but place a clue into the paniclog. 606. */ 607. paniclog("savebones", whynot); 608. return; 609. } 610. c = (char) (strlen(bonesid) + 1); 611. 612. nhfp->mode = WRITING; 613. store_version(nhfp); 614. Sfo_char(nhfp, &svn.nhuuid[0], "ancestor-nhuuid", sizeof svn.nhuuid); 615. /* if a bones pool digit is in use, it precedes the bonesid 616. string and isn't recorded in the file */ 617. Sfo_char(nhfp, &c, "bones_count", 1); 618. Sfo_char(nhfp, bonesid, "bonesid", (int) c); /* DD.nnn */ 619. savefruitchn(nhfp); 620. update_mlstmv(); /* update monsters for eventual restoration */ 621. savelev(nhfp, ledger_no(&u.uz)); 622. close_nhfile(nhfp); 623. commit_bonesfile(&u.uz); 624. compress_bonesfile(); 625. } 626. 627. #endif /* !SFCTOOL */ 628. 629. int 630. getbones(void) 631. { 632. int ok; 633. NHFILE *nhfp = (NHFILE *) 0; 634. char c = 0, *bonesid, 635. oldbonesid[40] = { 0 }; /* was [10]; more should be safer */ 636. char ancestor_nhuuid[SIZE(svn.nhuuid)]; 637. 638. #ifndef SFCTOOL 639. if (discover) /* save bones files for real games */ 640. return 0; 641. 642. if (!flags.bones) 643. return 0; 644. /* wizard check added by GAN 02/05/87 */ 645. if (rn2(3) /* only once in three times do we find bones */ 646. && !wizard) 647. return 0; 648. if (no_bones_level(&u.uz)) 649. return 0; 650. #endif /* !SFCTOOL */ 651. 652. nhfp = open_bonesfile(&u.uz, &bonesid); 653. if (!nhfp) 654. return 0; 655. if (nhfp && nhfp->structlevel && nhfp->fd < 0) 656. return 0; 657. if (nhfp && nhfp->fieldlevel) { 658. if (nhfp->style.deflt && !nhfp->fpdef) 659. return 0; 660. } 661. 662. program_state.reading_bonesfile = 1; 663. if (validate(nhfp, gb.bones, FALSE) != SF_UPTODATE) { 664. if (!wizard) 665. pline("Discarding unusable bones; no need to panic..."); 666. ok = FALSE; 667. program_state.reading_bonesfile = 0; 668. } else { 669. ok = TRUE; 670. if (wizard) { 671. if (y_n("Get bones?") == 'n') { 672. close_nhfile(nhfp); 673. compress_bonesfile(); 674. program_state.reading_bonesfile = 0; 675. return 0; 676. } 677. } 678. Sfi_char(nhfp, &ancestor_nhuuid[0], "ancestor-nhuuid", 679. sizeof ancestor_nhuuid); 680. Sfi_char(nhfp, &c, "bones_count", 1); /* length incl. '\0' */ 681. if ((unsigned) c <= sizeof oldbonesid) { 682. Sfi_char(nhfp, oldbonesid, "bonesid", (int) c); 683. } else { 684. if (wizard) 685. debugpline2("Abandoning bones , %u > %u.", 686. (unsigned) c, (unsigned) sizeof oldbonesid); 687. close_nhfile(nhfp); 688. compress_bonesfile(); 689. /* ToDo: maybe unlink these problematic bones? */ 690. program_state.reading_bonesfile = 0; 691. return 0; 692. } 693. if (strcmp(bonesid, oldbonesid) != 0) { 694. char errbuf[BUFSZ]; 695. 696. Sprintf(errbuf, "This is bones level '%s', not '%s'!", 697. oldbonesid, bonesid); 698. if (wizard) { 699. pline1(errbuf); 700. ok = FALSE; /* won't die of trickery */ 701. } 702. program_state.reading_bonesfile = 0; 703. trickery(errbuf); 704. } else { 705. struct monst *mtmp; 706. 707. getlev(nhfp, 0, 0); 708. 709. /* Note that getlev() now keeps tabs on unique 710. * monsters such as demon lords, and tracks the 711. * birth counts of all species just as makemon() 712. * does. If a bones monster is extinct or has been 713. * subject to genocide, their mhpmax will be 714. * set to the magic DEFUNCT_MONSTER cookie value. 715. */ 716. for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) { 717. if (has_mgivenname(mtmp)) 718. sanitize_name(MGIVENNAME(mtmp)); 719. if (mtmp->mhpmax == DEFUNCT_MONSTER) { 720. if (wizard) { 721. debugpline1("Removing defunct monster %s from bones.", 722. mtmp->data->pmnames[NEUTRAL]); 723. } 724. mongone(mtmp); 725. } else 726. /* to correctly reset named artifacts on the level */ 727. resetobjs(mtmp->minvent, TRUE); 728. } 729. resetobjs(fobj, TRUE); 730. resetobjs(svl.level.buriedobjlist, TRUE); 731. fix_shop_damage(); 732. } 733. } 734. close_nhfile(nhfp); 735. program_state.reading_bonesfile = 0; 736. sanitize_engravings(); 737. u.uroleplay.numbones++; 738. 739. if (wizard) { 740. if (y_n("Unlink bones?") == 'n') { 741. compress_bonesfile(); 742. return ok; 743. } 744. } 745. if (!delete_bonesfile(&u.uz)) { 746. /* When N games try to simultaneously restore the same 747. * bones file, N-1 of them will fail to delete it 748. * (the first N-1 under AmigaDOS, the last N-1 under UNIX). 749. * So no point in a mysterious message for a normal event 750. * -- just generate a new level for those N-1 games. 751. */ 752. /* pline("Cannot unlink bones."); */ 753. return 0; 754. } 755. return ok; 756. } 757. 758. #ifndef SFCTOOL 759. 760. /* check whether current level contains bones from a particular player */ 761. boolean 762. bones_include_name(const char *name) 763. { 764. struct cemetery *bp; 765. size_t len; 766. char buf[BUFSZ]; 767. 768. /* prepare buffer by appending terminal hyphen to name, to avoid partial 769. * matches producing false positives */ 770. Strcpy(buf, name); 771. Strcat(buf, "-"); 772. len = strlen(buf); 773. 774. for (bp = svl.level.bonesinfo; bp; bp = bp->next) { 775. if (!strncmp(bp->who, buf, len)) 776. return TRUE; 777. } 778. 779. return FALSE; 780. } 781. 782. /* set the ghostly bit in a list of objects */ 783. staticfn void 784. set_ghostly_objlist(struct obj *objchain) 785. { 786. while (objchain) { 787. objchain->ghostly = 1; 788. objchain = objchain->nobj; 789. } 790. } 791. 792. /* This is called when a marked object from a bones file is picked-up. 793. Some could result in a message, and the obj->ghostly flag is always 794. cleared. obj->ghostly has no other usage at this time. */ 795. void 796. fix_ghostly_obj(struct obj *obj) 797. { 798. if (!obj->ghostly) 799. return; 800. switch(obj->otyp) { 801. /* asymmetrical weapons */ 802. case BOW: 803. case ELVEN_BOW: 804. case ORCISH_BOW: 805. case YUMI: 806. case BOOMERANG: 807. You("make adjustments to %s to suit your %s hand.", 808. the(xname(obj)), 809. URIGHTY ? "right" : "left"); 810. break; 811. default: 812. break; 813. } 814. obj->ghostly = 0; 815. } 816. 817. void 818. newebones(struct monst *mtmp) 819. { 820. if (!mtmp->mextra) 821. mtmp->mextra = newmextra(); 822. if (!EBONES(mtmp)) { 823. EBONES(mtmp) = (struct ebones *) alloc( 824. sizeof (struct ebones)); 825. (void) memset((genericptr_t) EBONES(mtmp), 0, 826. sizeof (struct ebones)); 827. EBONES(mtmp)->parentmid = mtmp->m_id; 828. } 829. } 830. 831. /* this is not currently used */ 832. void 833. free_ebones(struct monst *mtmp) 834. { 835. if (mtmp->mextra && EBONES(mtmp)) { 836. free((genericptr_t) EBONES(mtmp)); 837. EBONES(mtmp) = (struct ebones *) 0; 838. } 839. } 840. 841. #endif /* SFCTOOL */ 842. 843. /*bones.c*/