1 /* NetHack 3.6 bones.c $NHDT-Date: 1557092711 2019/05/05 21:45:11 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.75 $ */
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. */
6 /* JNetHack Copyright */
7 /* (c) Issei Numata, Naoki Hamada, Shigehiro Miyashita, 1994-2000 */
8 /* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2019 */
9 /* JNetHack may be freely redistributed. See license for details. */
14 extern char bones[]; /* from files.c */
16 extern long bytes_counted;
19 STATIC_DCL boolean FDECL(no_bones_level, (d_level *));
20 STATIC_DCL void FDECL(goodfruit, (int));
21 STATIC_DCL void FDECL(resetobjs, (struct obj *, BOOLEAN_P));
22 STATIC_DCL boolean FDECL(fixuporacle, (struct monst *));
28 extern d_level save_dlevel; /* in do.c */
31 if (ledger_no(&save_dlevel))
32 assign_level(lev, &save_dlevel);
34 return (boolean) (((sptr = Is_special(lev)) != 0 && !sptr->boneid)
35 || !dungeons[lev->dnum].boneid
36 /* no bones on the last or multiway branch levels
37 in any dungeon (level 1 isn't multiway) */
39 || (Is_branchlev(lev) && lev->dlevel > 1)
40 /* no bones in the invocation level */
42 && lev->dlevel == dunlevs_in_dungeon(lev) - 1));
45 /* Call this function for each fruit object saved in the bones level: it marks
46 * that particular type of fruit as existing (the marker is that that type's
47 * ID is positive instead of negative). This way, when we later save the
48 * chain of fruit types, we know to only save the types that exist.
54 struct fruit *f = fruit_from_indx(-id);
61 resetobjs(ochain, restore)
65 struct obj *otmp, *nobj;
67 for (otmp = ochain; otmp; otmp = nobj) {
70 resetobjs(otmp->cobj, restore);
72 obj_extract_self(otmp);
78 /* artifact bookkeeping needs to be done during
79 restore; other fixups are done while saving */
80 if (otmp->oartifact) {
81 if (exist_artifact(otmp->otyp, safe_oname(otmp))
82 || is_quest_artifact(otmp)) {
83 /* prevent duplicate--revert to ordinary obj */
88 artifact_exists(otmp, safe_oname(otmp), TRUE);
90 } else if (has_oname(otmp)) {
91 sanitize_name(ONAME(otmp));
94 /* do not zero out o_ids for ghost levels anymore */
96 if (objects[otmp->otyp].oc_uses_known)
98 otmp->dknown = otmp->bknown = 0;
104 otmp->was_thrown = 0;
106 /* strip user-supplied names */
107 /* Statue and some corpse names are left intact,
108 presumably in case they came from score file.
109 [TODO: this ought to be done differently--names
110 which came from such a source or came from any
111 stoned or killed monster should be flagged in
112 some manner; then we could just check the flag
113 here and keep "real" names (dead pets, &c) while
114 discarding player notes attached to statues.] */
116 && !(otmp->oartifact || otmp->otyp == STATUE
117 || otmp->otyp == SPE_NOVEL
118 || (otmp->otyp == CORPSE
119 && otmp->corpsenm >= SPECIAL_PM))) {
123 if (otmp->otyp == SLIME_MOLD) {
124 goodfruit(otmp->spe);
126 } else if (otmp->otyp == SCR_MAIL) {
127 /* 0: delivered in-game via external event;
128 1: from bones or wishing; 2: written with marker */
132 } else if (otmp->otyp == EGG) {
133 otmp->spe = 0; /* not "laid by you" in next game */
134 } else if (otmp->otyp == TIN) {
135 /* make tins of unique monster's meat be empty */
136 if (otmp->corpsenm >= LOW_PM
137 && unique_corpstat(&mons[otmp->corpsenm]))
138 otmp->corpsenm = NON_PM;
139 } else if (otmp->otyp == CORPSE || otmp->otyp == STATUE) {
140 int mnum = otmp->corpsenm;
142 /* Discard incarnation details of unique monsters
143 (by passing null instead of otmp for object),
144 shopkeepers (by passing false for revival flag),
145 temple priests, and vault guards in order to
146 prevent corpse revival or statue reanimation. */
148 && cant_revive(&mnum, FALSE, (struct obj *) 0)) {
150 /* mnum is now either human_zombie or doppelganger;
151 for corpses of uniques, we need to force the
152 transformation now rather than wait until a
153 revival attempt, otherwise eating this corpse
154 would behave as if it remains unique */
155 if (mnum == PM_DOPPELGANGER && otmp->otyp == CORPSE)
156 set_corpsenm(otmp, mnum);
158 } else if ((otmp->otyp == iflags.mines_prize_type
159 && !Is_mineend_level(&u.uz))
160 || ((otmp->otyp == iflags.soko_prize_type1
161 || otmp->otyp == iflags.soko_prize_type2)
162 && !Is_sokoend_level(&u.uz))) {
163 /* "special prize" in this game becomes ordinary object
164 if loaded into another game */
165 otmp->record_achieve_special = NON_PM;
166 } else if (otmp->otyp == AMULET_OF_YENDOR) {
167 /* no longer the real Amulet */
168 otmp->otyp = FAKE_AMULET_OF_YENDOR;
170 } else if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
172 end_burn(otmp, TRUE);
173 otmp->otyp = WAX_CANDLE;
174 otmp->age = 50L; /* assume used */
176 otmp->quan = (long) otmp->spe;
178 otmp->owt = weight(otmp);
180 } else if (otmp->otyp == BELL_OF_OPENING) {
183 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
184 otmp->otyp = SPE_BLANK_PAPER;
191 /* while loading bones, strip out text possibly supplied by old player
192 that might accidentally or maliciously disrupt new player's display */
194 sanitize_name(namebuf)
198 boolean strip_8th_bit = (WINDOWPORT("tty")
199 && !iflags.wc_eight_bit_input);
201 /* it's tempting to skip this for single-user platforms, since
202 only the current player could have left these bones--except
203 things like "hearse" and other bones exchange schemes make
204 that assumption false */
207 if (c < ' ' || c == '\177') {
208 /* non-printable or undesirable */
210 } else if (c != *namebuf) {
211 /* expected to be printable if user wants such things */
219 /* called by savebones(); also by finish_paybill(shk.c) */
221 drop_upon_death(mtmp, cont, x, y)
222 struct monst *mtmp; /* monster if hero turned into one (other than ghost) */
223 struct obj *cont; /* container if hero is turned into a statue */
228 u.twoweap = 0; /* ensure curse() won't cause swapwep to drop twice */
229 while ((otmp = invent) != 0) {
230 obj_extract_self(otmp);
231 /* when turning into green slime, all gear remains held;
232 other types "arise from the dead" do aren't holding
233 equipment during their brief interval as a corpse */
234 if (!mtmp || is_undead(mtmp->data))
235 obj_no_longer_held(otmp);
237 otmp->owornmask = 0L;
238 /* lamps don't go out when dropped */
239 if ((cont || artifact_light(otmp)) && obj_is_burning(otmp))
240 end_burn(otmp, TRUE); /* smother in statue */
242 if (otmp->otyp == SLIME_MOLD)
243 goodfruit(otmp->spe);
248 (void) add_to_minv(mtmp, otmp);
250 (void) add_to_container(cont, otmp);
252 place_object(otmp, x, y);
255 cont->owt = weight(cont);
258 /* possibly restore oracle's room and/or put her back inside it; returns
259 False if she's on the wrong level and should be removed, True otherwise */
262 struct monst *oracle;
267 /* oracle doesn't move, but knight's joust or monk's staggering blow
268 could push her onto a hole in the floor; at present, traps don't
269 activate in such situation hence she won't fall to another level;
270 however, that could change so be prepared to cope with such things */
271 if (!Is_oracle_level(&u.uz))
274 oracle->mpeaceful = 1;
275 o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
276 if (o_ridx >= 0 && rooms[o_ridx].rtype == DELPHI)
277 return TRUE; /* no fixup needed */
280 * The Oracle isn't in DELPHI room. Either hero entered her chamber
281 * and got the one-time welcome message, converting it into an
282 * ordinary room, or she got teleported out, or both. Try to put
283 * her back inside her room, if necessary, and restore its type.
286 /* find original delphi chamber; should always succeed */
287 for (ridx = 0; ridx < SIZE(rooms); ++ridx)
288 if (rooms[ridx].orig_rtype == DELPHI)
291 if (o_ridx != ridx && ridx < SIZE(rooms)) {
292 /* room found and she's not not in it, so try to move her there */
293 cc.x = (rooms[ridx].lx + rooms[ridx].hx) / 2;
294 cc.y = (rooms[ridx].ly + rooms[ridx].hy) / 2;
295 if (enexto(&cc, cc.x, cc.y, oracle->data)) {
296 rloc_to(oracle, cc.x, cc.y);
297 o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
299 /* [if her room is already full, she might end up outside;
300 that's ok, next hero just won't get any welcome message,
301 same as used to happen before this fixup was introduced] */
303 if (ridx == o_ridx) /* if she's in her room, mark it as such */
304 rooms[ridx].rtype = DELPHI;
305 return TRUE; /* keep oracle in new bones file */
308 /* check whether bones are feasible */
312 register struct trap *ttmp;
316 if (ledger_no(&u.uz) <= 0 || ledger_no(&u.uz) > maxledgerno())
318 if (no_bones_level(&u.uz))
319 return FALSE; /* no bones for specific levels */
321 return FALSE; /* no bones when swallowed */
323 if (!Is_branchlev(&u.uz)) {
324 /* no bones on non-branches with portals */
325 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
326 if (ttmp->ttyp == MAGIC_PORTAL)
330 if (depth(&u.uz) <= 0 /* bulletproofing for endgame */
331 || (!rn2(1 + (depth(&u.uz) >> 2)) /* fewer ghosts on low levels */
334 /* don't let multiple restarts generate multiple copies of objects
341 /* save bones and possessions of a deceased adventurer */
343 savebones(how, when, corpse)
351 struct permonst *mptr;
353 struct cemetery *newbones;
357 /* caller has already checked `can_make_bones()' */
360 fd = open_bonesfile(&u.uz, &bonesid);
365 if (yn("Bones file already exists. Replace it?") == 'y') {
367 if (yn("
\8d\9c\83t
\83@
\83C
\83\8b\82ª
\8aù
\82É
\91¶
\8dÝ
\82µ
\82Ä
\82é
\82æ
\81D
\92u
\82«
\8a·
\82¦
\82é
\81H") == 'y') {
368 if (delete_bonesfile(&u.uz))
372 pline("Cannot unlink old bones.");
374 pline("
\8cÃ
\82¢
\8d\9c\82ð
\8dí
\8f\9c\82Å
\82«
\82È
\82©
\82Á
\82½
\81D");
377 /* compression can change the file's name, so must
378 wait until after any attempt to delete this file */
379 compress_bonesfile();
385 /* in case these characters are not in their home bases */
386 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
387 if (DEADMONSTER(mtmp))
390 if (mtmp->iswiz || mptr == &mons[PM_MEDUSA]
391 || mptr->msound == MS_NEMESIS || mptr->msound == MS_LEADER
392 || mptr == &mons[PM_VLAD_THE_IMPALER]
393 || (mptr == &mons[PM_ORACLE] && !fixuporacle(mtmp)))
397 dismount_steed(DISMOUNT_BONES);
398 dmonsfree(); /* discard dead or gone monsters */
400 /* mark all fruits as nonexistent; when we come to them we'll mark
401 * them as existing (using goodfruit())
403 for (f = ffruit; f; f = f->nextf)
406 /* check iron balls separately--maybe they're not carrying it */
408 uball->owornmask = uchain->owornmask = 0L;
410 /* dispose of your possessions, usually cursed */
411 if (u.ugrave_arise == (NON_PM - 1)) {
414 /* embed your possessions in your statue */
415 otmp = mk_named_object(STATUE, &mons[u.umonnum], u.ux, u.uy, plname);
417 drop_upon_death((struct monst *) 0, otmp, u.ux, u.uy);
419 return; /* couldn't make statue */
420 mtmp = (struct monst *) 0;
421 } else if (u.ugrave_arise < LOW_PM) {
422 /* drop everything */
423 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
424 /* trick makemon() into allowing monster creation
428 mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME);
432 mtmp = christen_monst(mtmp, plname);
434 (void) obj_attach_mid(corpse, mtmp->m_id);
436 /* give your possessions to the monster you become */
437 in_mklev = TRUE; /* use <u.ux,u.uy> as-is */
438 mtmp = makemon(&mons[u.ugrave_arise], u.ux, u.uy, NO_MINVENT);
440 if (!mtmp) { /* arise-type might have been genocided */
441 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
442 u.ugrave_arise = NON_PM; /* in case caller cares */
445 mtmp = christen_monst(mtmp, plname);
447 /* ["Your body rises from the dead as an <mname>..." used
448 to be given here, but it has been moved to done() so that
449 it gets delivered even when savebones() isn't called] */
450 drop_upon_death(mtmp, (struct obj *) 0, u.ux, u.uy);
451 /* 'mtmp' now has hero's inventory; if 'mtmp' is a mummy, give it
452 a wrapping unless already carrying one */
453 if (mtmp->data->mlet == S_MUMMY && !m_carrying(mtmp, MUMMY_WRAPPING))
454 (void) mongets(mtmp, MUMMY_WRAPPING);
455 m_dowear(mtmp, TRUE);
458 mtmp->m_lev = (u.ulevel ? u.ulevel : 1);
459 mtmp->mhp = mtmp->mhpmax = u.uhpmax;
460 mtmp->female = flags.female;
463 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
464 resetobjs(mtmp->minvent, FALSE);
465 /* do not zero out m_ids for bones levels any more */
468 mtmp->mtame = mtmp->mpeaceful = 0;
470 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
472 ttmp->tseen = (ttmp->ttyp == HOLE);
474 resetobjs(fobj, FALSE);
475 resetobjs(level.buriedobjlist, FALSE);
477 /* Hero is no longer on the map. */
478 u.ux0 = u.ux, u.uy0 = u.uy;
481 /* Clear all memory from the level. */
482 for (x = 1; x < COLNO; x++)
483 for (y = 0; y < ROWNO; y++) {
484 levl[x][y].seenv = 0;
485 levl[x][y].waslit = 0;
486 levl[x][y].glyph = cmap_to_glyph(S_stone);
487 lastseentyp[x][y] = 0;
490 /* Attach bones info to the current level before saving. */
491 newbones = (struct cemetery *) alloc(sizeof *newbones);
492 /* entries are '\0' terminated but have fixed length allocations,
493 so pre-fill with spaces to initialize any excess room */
494 (void) memset((genericptr_t) newbones, ' ', sizeof *newbones);
495 /* format name+role,&c, death reason, and date+time;
496 gender and alignment reflect final values rather than what the
497 character started out as, same as topten and logfile entries */
498 Sprintf(newbones->who, "%s-%.3s-%.3s-%.3s-%.3s", plname, urole.filecode,
499 urace.filecode, genders[flags.female].filecode,
500 aligns[1 - u.ualign.type].filecode);
501 formatkiller(newbones->how, sizeof newbones->how, how, TRUE);
502 Strcpy(newbones->when, yyyymmddhhmmss(when));
503 /* final resting place, used to decide when bones are discovered */
504 newbones->frpx = u.ux0, newbones->frpy = u.uy0;
505 newbones->bonesknown = FALSE;
506 /* if current character died on a bones level, the cemetery list
507 will have multiple entries, most recent (this dead hero) first */
508 newbones->next = level.bonesinfo;
509 level.bonesinfo = newbones;
510 /* flag these bones if they are being created in wizard mode;
511 they might already be flagged as such, even when we're playing
512 in normal mode, if this level came from a previous bones file */
514 level.flags.wizard_bones = 1;
516 fd = create_bonesfile(&u.uz, &bonesid, whynot);
520 /* bones file creation problems are silent to the player.
521 * Keep it that way, but place a clue into the paniclog.
523 paniclog("savebones", whynot);
526 c = (char) (strlen(bonesid) + 1);
528 #ifdef MFLOPPY /* check whether there is room */
529 if (iflags.checkspace) {
530 savelev(fd, ledger_no(&u.uz), COUNT_SAVE);
531 /* savelev() initializes bytes_counted to 0, so it must come
532 * first here even though it does not in the real save. the
533 * resulting extra bflush() at the end of savelev() may increase
534 * bytes_counted by a couple over what the real usage will be.
536 * note it is safe to call store_version() here only because
537 * bufon() is null for ZEROCOMP, which MFLOPPY uses -- otherwise
538 * this code would have to know the size of the version
539 * information itself.
542 store_savefileinfo(fd);
543 bwrite(fd, (genericptr_t) &c, sizeof c);
544 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
545 savefruitchn(fd, COUNT_SAVE);
547 if (bytes_counted > freediskspace(bones)) { /* not enough room */
550 pline("Insufficient space to create bones file.");
552 pline("
\8d\9c\83t
\83@
\83C
\83\8b\82ð
\90¶
\90¬
\82·
\82é
\82½
\82ß
\82Ì
\8f[
\95ª
\82È
\97Ì
\88æ
\82ª
\82È
\82¢
\81D");
557 co_false(); /* make sure stuff before savelev() gets written */
562 store_savefileinfo(fd);
563 bwrite(fd, (genericptr_t) &c, sizeof c);
564 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
565 savefruitchn(fd, WRITE_SAVE | FREE_SAVE);
566 update_mlstmv(); /* update monsters for eventual restoration */
567 savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE);
569 commit_bonesfile(&u.uz);
570 compress_bonesfile();
578 char c, *bonesid, oldbonesid[40]; /* was [10]; more should be safer */
580 if (discover) /* save bones files for real games */
585 /* wizard check added by GAN 02/05/87 */
586 if (rn2(3) /* only once in three times do we find bones */
589 if (no_bones_level(&u.uz))
591 fd = open_bonesfile(&u.uz, &bonesid);
595 if (validate(fd, bones) != 0) {
598 pline("Discarding unuseable bones; no need to panic...");
600 pline("
\8eg
\82¦
\82È
\82¢
\8d\9c\82ð
\8eÌ
\82Ä
\82½
\81D
\8dQ
\82Ä
\82é
\95K
\97v
\82Í
\82È
\82¢
\81D
\81D
\81D");
606 if (yn("Get bones?") == 'n') {
608 if (yn("
\8d\9c\82ð
\8fE
\82¤
\81H") == 'n') {
610 compress_bonesfile();
614 mread(fd, (genericptr_t) &c, sizeof c); /* length incl. '\0' */
615 mread(fd, (genericptr_t) oldbonesid, (unsigned) c); /* DD.nnn */
616 if (strcmp(bonesid, oldbonesid) != 0
617 /* from 3.3.0 through 3.6.0, bones in the quest branch stored
618 a bogus bonesid in the file; 3.6.1 fixed that, but for
619 3.6.0 bones to remain compatible, we need an extra test;
620 once compatibility with 3.6.x goes away, this can too
621 (we don't try to make this conditional upon the value of
622 VERSION_COMPATIBILITY because then we'd need patchlevel.h) */
623 && (strlen(bonesid) <= 2
624 || strcmp(bonesid + 2, oldbonesid) != 0)) {
628 Sprintf(errbuf, "This is bones level '%s', not '%s'!", oldbonesid,
630 Sprintf(errbuf, "
\82±
\82Ì
\8d\9c\82Ì
\83\8c\83x
\83\8b\82Í'%s'
\82Å
\82 \82Á
\82Ä
\81A'%s'
\82Å
\82Í
\82È
\82¢
\81I", oldbonesid,
634 ok = FALSE; /* won't die of trickery */
638 register struct monst *mtmp;
640 getlev(fd, 0, 0, TRUE);
642 /* Note that getlev() now keeps tabs on unique
643 * monsters such as demon lords, and tracks the
644 * birth counts of all species just as makemon()
645 * does. If a bones monster is extinct or has been
646 * subject to genocide, their mhpmax will be
647 * set to the magic DEFUNCT_MONSTER cookie value.
649 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
651 sanitize_name(MNAME(mtmp));
652 if (mtmp->mhpmax == DEFUNCT_MONSTER) {
654 debugpline1("Removing defunct monster %s from bones.",
659 /* to correctly reset named artifacts on the level */
660 resetobjs(mtmp->minvent, TRUE);
662 resetobjs(fobj, TRUE);
663 resetobjs(level.buriedobjlist, TRUE);
667 sanitize_engravings();
668 u.uroleplay.numbones++;
672 if (yn("Unlink bones?") == 'n') {
674 if (yn("
\8d\9c\82ð
\8fÁ
\82·
\81H") == 'n') {
675 compress_bonesfile();
679 if (!delete_bonesfile(&u.uz)) {
680 /* When N games try to simultaneously restore the same
681 * bones file, N-1 of them will fail to delete it
682 * (the first N-1 under AmigaDOS, the last N-1 under UNIX).
683 * So no point in a mysterious message for a normal event
684 * -- just generate a new level for those N-1 games.
686 /* pline("Cannot unlink bones."); */