1 /* NetHack 3.6 bones.c $NHDT-Date: 1571363147 2019/10/18 01:45:47 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.76 $ */
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-2020 */
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));
93 /* 3.6.3: set no_charge for partly eaten food in shop;
94 all other items become goods for sale if in a shop */
95 if (otmp->oclass == FOOD_CLASS && otmp->oeaten) {
100 for (top = otmp; top->where == OBJ_CONTAINED;
101 top = top->ocontainer)
103 otmp->no_charge = (top->where == OBJ_FLOOR
104 && get_obj_location(top, &ox, &oy, 0)
105 /* can't use costly_spot() since its
106 result depends upon hero's location */
107 && inside_shop(ox, oy)
108 && *(p = in_rooms(ox, oy, SHOPBASE))
109 && tended_shop(&rooms[*p - ROOMOFFSET]));
111 } else { /* saving */
112 /* do not zero out o_ids for ghost levels anymore */
114 if (objects[otmp->otyp].oc_uses_known)
116 otmp->dknown = otmp->bknown = 0;
122 otmp->was_thrown = 0;
124 /* strip user-supplied names */
125 /* Statue and some corpse names are left intact,
126 presumably in case they came from score file.
127 [TODO: this ought to be done differently--names
128 which came from such a source or came from any
129 stoned or killed monster should be flagged in
130 some manner; then we could just check the flag
131 here and keep "real" names (dead pets, &c) while
132 discarding player notes attached to statues.] */
134 && !(otmp->oartifact || otmp->otyp == STATUE
135 || otmp->otyp == SPE_NOVEL
136 || (otmp->otyp == CORPSE
137 && otmp->corpsenm >= SPECIAL_PM))) {
141 if (otmp->otyp == SLIME_MOLD) {
142 goodfruit(otmp->spe);
144 } else if (otmp->otyp == SCR_MAIL) {
145 /* 0: delivered in-game via external event;
146 1: from bones or wishing; 2: written with marker */
150 } else if (otmp->otyp == EGG) {
151 otmp->spe = 0; /* not "laid by you" in next game */
152 } else if (otmp->otyp == TIN) {
153 /* make tins of unique monster's meat be empty */
154 if (otmp->corpsenm >= LOW_PM
155 && unique_corpstat(&mons[otmp->corpsenm]))
156 otmp->corpsenm = NON_PM;
157 } else if (otmp->otyp == CORPSE || otmp->otyp == STATUE) {
158 int mnum = otmp->corpsenm;
160 /* Discard incarnation details of unique monsters
161 (by passing null instead of otmp for object),
162 shopkeepers (by passing false for revival flag),
163 temple priests, and vault guards in order to
164 prevent corpse revival or statue reanimation. */
166 && cant_revive(&mnum, FALSE, (struct obj *) 0)) {
168 /* mnum is now either human_zombie or doppelganger;
169 for corpses of uniques, we need to force the
170 transformation now rather than wait until a
171 revival attempt, otherwise eating this corpse
172 would behave as if it remains unique */
173 if (mnum == PM_DOPPELGANGER && otmp->otyp == CORPSE)
174 set_corpsenm(otmp, mnum);
176 } else if ((otmp->otyp == iflags.mines_prize_type
177 && !Is_mineend_level(&u.uz))
178 || ((otmp->otyp == iflags.soko_prize_type1
179 || otmp->otyp == iflags.soko_prize_type2)
180 && !Is_sokoend_level(&u.uz))) {
181 /* "special prize" in this game becomes ordinary object
182 if loaded into another game */
183 otmp->record_achieve_special = NON_PM;
184 } else if (otmp->otyp == AMULET_OF_YENDOR) {
185 /* no longer the real Amulet */
186 otmp->otyp = FAKE_AMULET_OF_YENDOR;
188 } else if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
190 end_burn(otmp, TRUE);
191 otmp->otyp = WAX_CANDLE;
192 otmp->age = 50L; /* assume used */
194 otmp->quan = (long) otmp->spe;
196 otmp->owt = weight(otmp);
198 } else if (otmp->otyp == BELL_OF_OPENING) {
201 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
202 otmp->otyp = SPE_BLANK_PAPER;
209 /* while loading bones, strip out text possibly supplied by old player
210 that might accidentally or maliciously disrupt new player's display */
212 sanitize_name(namebuf)
216 boolean strip_8th_bit = (WINDOWPORT("tty")
217 && !iflags.wc_eight_bit_input);
219 /* it's tempting to skip this for single-user platforms, since
220 only the current player could have left these bones--except
221 things like "hearse" and other bones exchange schemes make
222 that assumption false */
225 if (c < ' ' || c == '\177') {
226 /* non-printable or undesirable */
228 } else if (c != *namebuf) {
229 /* expected to be printable if user wants such things */
237 /* called by savebones(); also by finish_paybill(shk.c) */
239 drop_upon_death(mtmp, cont, x, y)
240 struct monst *mtmp; /* monster if hero turned into one (other than ghost) */
241 struct obj *cont; /* container if hero is turned into a statue */
246 u.twoweap = 0; /* ensure curse() won't cause swapwep to drop twice */
247 while ((otmp = invent) != 0) {
248 obj_extract_self(otmp);
249 /* when turning into green slime, all gear remains held;
250 other types "arise from the dead" do aren't holding
251 equipment during their brief interval as a corpse */
252 if (!mtmp || is_undead(mtmp->data))
253 obj_no_longer_held(otmp);
255 otmp->owornmask = 0L;
256 /* lamps don't go out when dropped */
257 if ((cont || artifact_light(otmp)) && obj_is_burning(otmp))
258 end_burn(otmp, TRUE); /* smother in statue */
260 if (otmp->otyp == SLIME_MOLD)
261 goodfruit(otmp->spe);
266 (void) add_to_minv(mtmp, otmp);
268 (void) add_to_container(cont, otmp);
270 place_object(otmp, x, y);
273 cont->owt = weight(cont);
276 /* possibly restore oracle's room and/or put her back inside it; returns
277 False if she's on the wrong level and should be removed, True otherwise */
280 struct monst *oracle;
285 /* oracle doesn't move, but knight's joust or monk's staggering blow
286 could push her onto a hole in the floor; at present, traps don't
287 activate in such situation hence she won't fall to another level;
288 however, that could change so be prepared to cope with such things */
289 if (!Is_oracle_level(&u.uz))
292 oracle->mpeaceful = 1;
293 o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
294 if (o_ridx >= 0 && rooms[o_ridx].rtype == DELPHI)
295 return TRUE; /* no fixup needed */
298 * The Oracle isn't in DELPHI room. Either hero entered her chamber
299 * and got the one-time welcome message, converting it into an
300 * ordinary room, or she got teleported out, or both. Try to put
301 * her back inside her room, if necessary, and restore its type.
304 /* find original delphi chamber; should always succeed */
305 for (ridx = 0; ridx < SIZE(rooms); ++ridx)
306 if (rooms[ridx].orig_rtype == DELPHI)
309 if (o_ridx != ridx && ridx < SIZE(rooms)) {
310 /* room found and she's not not in it, so try to move her there */
311 cc.x = (rooms[ridx].lx + rooms[ridx].hx) / 2;
312 cc.y = (rooms[ridx].ly + rooms[ridx].hy) / 2;
313 if (enexto(&cc, cc.x, cc.y, oracle->data)) {
314 rloc_to(oracle, cc.x, cc.y);
315 o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
317 /* [if her room is already full, she might end up outside;
318 that's ok, next hero just won't get any welcome message,
319 same as used to happen before this fixup was introduced] */
321 if (ridx == o_ridx) /* if she's in her room, mark it as such */
322 rooms[ridx].rtype = DELPHI;
323 return TRUE; /* keep oracle in new bones file */
326 /* check whether bones are feasible */
330 register struct trap *ttmp;
334 if (ledger_no(&u.uz) <= 0 || ledger_no(&u.uz) > maxledgerno())
336 if (no_bones_level(&u.uz))
337 return FALSE; /* no bones for specific levels */
339 return FALSE; /* no bones when swallowed */
341 if (!Is_branchlev(&u.uz)) {
342 /* no bones on non-branches with portals */
343 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
344 if (ttmp->ttyp == MAGIC_PORTAL)
348 if (depth(&u.uz) <= 0 /* bulletproofing for endgame */
349 || (!rn2(1 + (depth(&u.uz) >> 2)) /* fewer ghosts on low levels */
352 /* don't let multiple restarts generate multiple copies of objects
359 /* save bones and possessions of a deceased adventurer */
361 savebones(how, when, corpse)
369 struct permonst *mptr;
371 struct cemetery *newbones;
375 /* caller has already checked `can_make_bones()' */
378 fd = open_bonesfile(&u.uz, &bonesid);
383 if (yn("Bones file already exists. Replace it?") == 'y') {
385 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') {
386 if (delete_bonesfile(&u.uz))
390 pline("Cannot unlink old bones.");
392 pline("
\8cÃ
\82¢
\8d\9c\82ð
\8dí
\8f\9c\82Å
\82«
\82È
\82©
\82Á
\82½
\81D");
395 /* compression can change the file's name, so must
396 wait until after any attempt to delete this file */
397 compress_bonesfile();
403 /* in case these characters are not in their home bases */
404 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
405 if (DEADMONSTER(mtmp))
408 if (mtmp->iswiz || mptr == &mons[PM_MEDUSA]
409 || mptr->msound == MS_NEMESIS || mptr->msound == MS_LEADER
410 || mptr == &mons[PM_VLAD_THE_IMPALER]
411 || (mptr == &mons[PM_ORACLE] && !fixuporacle(mtmp)))
415 dismount_steed(DISMOUNT_BONES);
416 dmonsfree(); /* discard dead or gone monsters */
418 /* mark all fruits as nonexistent; when we come to them we'll mark
419 * them as existing (using goodfruit())
421 for (f = ffruit; f; f = f->nextf)
424 /* check iron balls separately--maybe they're not carrying it */
426 uball->owornmask = uchain->owornmask = 0L;
428 /* dispose of your possessions, usually cursed */
429 if (u.ugrave_arise == (NON_PM - 1)) {
432 /* embed your possessions in your statue */
433 otmp = mk_named_object(STATUE, &mons[u.umonnum], u.ux, u.uy, plname);
435 drop_upon_death((struct monst *) 0, otmp, u.ux, u.uy);
437 return; /* couldn't make statue */
438 mtmp = (struct monst *) 0;
439 } else if (u.ugrave_arise < LOW_PM) {
440 /* drop everything */
441 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
442 /* trick makemon() into allowing monster creation
446 mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME);
450 mtmp = christen_monst(mtmp, plname);
452 (void) obj_attach_mid(corpse, mtmp->m_id);
454 /* give your possessions to the monster you become */
455 in_mklev = TRUE; /* use <u.ux,u.uy> as-is */
456 mtmp = makemon(&mons[u.ugrave_arise], u.ux, u.uy, NO_MINVENT);
458 if (!mtmp) { /* arise-type might have been genocided */
459 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
460 u.ugrave_arise = NON_PM; /* in case caller cares */
463 mtmp = christen_monst(mtmp, plname);
465 /* ["Your body rises from the dead as an <mname>..." used
466 to be given here, but it has been moved to done() so that
467 it gets delivered even when savebones() isn't called] */
468 drop_upon_death(mtmp, (struct obj *) 0, u.ux, u.uy);
469 /* 'mtmp' now has hero's inventory; if 'mtmp' is a mummy, give it
470 a wrapping unless already carrying one */
471 if (mtmp->data->mlet == S_MUMMY && !m_carrying(mtmp, MUMMY_WRAPPING))
472 (void) mongets(mtmp, MUMMY_WRAPPING);
473 m_dowear(mtmp, TRUE);
476 mtmp->m_lev = (u.ulevel ? u.ulevel : 1);
477 mtmp->mhp = mtmp->mhpmax = u.uhpmax;
478 mtmp->female = flags.female;
481 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
482 resetobjs(mtmp->minvent, FALSE);
483 /* do not zero out m_ids for bones levels any more */
486 mtmp->mtame = mtmp->mpeaceful = 0;
488 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
490 ttmp->tseen = (ttmp->ttyp == HOLE);
492 resetobjs(fobj, FALSE);
493 resetobjs(level.buriedobjlist, FALSE);
495 /* Hero is no longer on the map. */
496 u.ux0 = u.ux, u.uy0 = u.uy;
499 /* Clear all memory from the level. */
500 for (x = 1; x < COLNO; x++)
501 for (y = 0; y < ROWNO; y++) {
502 levl[x][y].seenv = 0;
503 levl[x][y].waslit = 0;
504 levl[x][y].glyph = cmap_to_glyph(S_stone);
505 lastseentyp[x][y] = 0;
508 /* Attach bones info to the current level before saving. */
509 newbones = (struct cemetery *) alloc(sizeof *newbones);
510 /* entries are '\0' terminated but have fixed length allocations,
511 so pre-fill with spaces to initialize any excess room */
512 (void) memset((genericptr_t) newbones, ' ', sizeof *newbones);
513 /* format name+role,&c, death reason, and date+time;
514 gender and alignment reflect final values rather than what the
515 character started out as, same as topten and logfile entries */
516 Sprintf(newbones->who, "%s-%.3s-%.3s-%.3s-%.3s", plname, urole.filecode,
517 urace.filecode, genders[flags.female].filecode,
518 aligns[1 - u.ualign.type].filecode);
519 formatkiller(newbones->how, sizeof newbones->how, how, TRUE);
520 Strcpy(newbones->when, yyyymmddhhmmss(when));
521 /* final resting place, used to decide when bones are discovered */
522 newbones->frpx = u.ux0, newbones->frpy = u.uy0;
523 newbones->bonesknown = FALSE;
524 /* if current character died on a bones level, the cemetery list
525 will have multiple entries, most recent (this dead hero) first */
526 newbones->next = level.bonesinfo;
527 level.bonesinfo = newbones;
528 /* flag these bones if they are being created in wizard mode;
529 they might already be flagged as such, even when we're playing
530 in normal mode, if this level came from a previous bones file */
532 level.flags.wizard_bones = 1;
534 fd = create_bonesfile(&u.uz, &bonesid, whynot);
538 /* bones file creation problems are silent to the player.
539 * Keep it that way, but place a clue into the paniclog.
541 paniclog("savebones", whynot);
544 c = (char) (strlen(bonesid) + 1);
546 #ifdef MFLOPPY /* check whether there is room */
547 if (iflags.checkspace) {
548 savelev(fd, ledger_no(&u.uz), COUNT_SAVE);
549 /* savelev() initializes bytes_counted to 0, so it must come
550 * first here even though it does not in the real save. the
551 * resulting extra bflush() at the end of savelev() may increase
552 * bytes_counted by a couple over what the real usage will be.
554 * note it is safe to call store_version() here only because
555 * bufon() is null for ZEROCOMP, which MFLOPPY uses -- otherwise
556 * this code would have to know the size of the version
557 * information itself.
560 store_savefileinfo(fd);
561 bwrite(fd, (genericptr_t) &c, sizeof c);
562 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
563 savefruitchn(fd, COUNT_SAVE);
565 if (bytes_counted > freediskspace(bones)) { /* not enough room */
568 pline("Insufficient space to create bones file.");
570 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");
575 co_false(); /* make sure stuff before savelev() gets written */
580 store_savefileinfo(fd);
581 bwrite(fd, (genericptr_t) &c, sizeof c);
582 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
583 savefruitchn(fd, WRITE_SAVE | FREE_SAVE);
584 update_mlstmv(); /* update monsters for eventual restoration */
585 savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE);
587 commit_bonesfile(&u.uz);
588 compress_bonesfile();
596 char c, *bonesid, oldbonesid[40]; /* was [10]; more should be safer */
598 if (discover) /* save bones files for real games */
603 /* wizard check added by GAN 02/05/87 */
604 if (rn2(3) /* only once in three times do we find bones */
607 if (no_bones_level(&u.uz))
609 fd = open_bonesfile(&u.uz, &bonesid);
613 if (validate(fd, bones) != 0) {
616 pline("Discarding unusable bones; no need to panic...");
618 pline("
\8eg
\82¦
\82È
\82¢
\8d\9c\82ð
\8eÌ
\82Ä
\82½
\81D
\8dQ
\82Ä
\82é
\95K
\97v
\82Í
\82È
\82¢
\81D
\81D
\81D");
624 if (yn("Get bones?") == 'n') {
626 if (yn("
\8d\9c\82ð
\8fE
\82¤
\81H") == 'n') {
628 compress_bonesfile();
632 mread(fd, (genericptr_t) &c, sizeof c); /* length incl. '\0' */
633 mread(fd, (genericptr_t) oldbonesid, (unsigned) c); /* DD.nnn */
634 if (strcmp(bonesid, oldbonesid) != 0
635 /* from 3.3.0 through 3.6.0, bones in the quest branch stored
636 a bogus bonesid in the file; 3.6.1 fixed that, but for
637 3.6.0 bones to remain compatible, we need an extra test;
638 once compatibility with 3.6.x goes away, this can too
639 (we don't try to make this conditional upon the value of
640 VERSION_COMPATIBILITY because then we'd need patchlevel.h) */
641 && (strlen(bonesid) <= 2
642 || strcmp(bonesid + 2, oldbonesid) != 0)) {
646 Sprintf(errbuf, "This is bones level '%s', not '%s'!", oldbonesid,
648 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,
652 ok = FALSE; /* won't die of trickery */
656 register struct monst *mtmp;
658 getlev(fd, 0, 0, TRUE);
660 /* Note that getlev() now keeps tabs on unique
661 * monsters such as demon lords, and tracks the
662 * birth counts of all species just as makemon()
663 * does. If a bones monster is extinct or has been
664 * subject to genocide, their mhpmax will be
665 * set to the magic DEFUNCT_MONSTER cookie value.
667 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
669 sanitize_name(MNAME(mtmp));
670 if (mtmp->mhpmax == DEFUNCT_MONSTER) {
672 debugpline1("Removing defunct monster %s from bones.",
677 /* to correctly reset named artifacts on the level */
678 resetobjs(mtmp->minvent, TRUE);
680 resetobjs(fobj, TRUE);
681 resetobjs(level.buriedobjlist, TRUE);
685 sanitize_engravings();
686 u.uroleplay.numbones++;
690 if (yn("Unlink bones?") == 'n') {
692 if (yn("
\8d\9c\82ð
\8fÁ
\82·
\81H") == 'n') {
693 compress_bonesfile();
697 if (!delete_bonesfile(&u.uz)) {
698 /* When N games try to simultaneously restore the same
699 * bones file, N-1 of them will fail to delete it
700 * (the first N-1 under AmigaDOS, the last N-1 under UNIX).
701 * So no point in a mysterious message for a normal event
702 * -- just generate a new level for those N-1 games.
704 /* pline("Cannot unlink bones."); */