1 /* NetHack 3.6 bones.c $NHDT-Date: 1508827591 2017/10/24 06:46:31 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.71 $ */
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-2016 */
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 = (!strcmp(windowprocs.name, "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)
228 u.twoweap = 0; /* ensure curse() won't cause swapwep to drop twice */
229 while ((otmp = invent) != 0) {
230 obj_extract_self(otmp);
231 obj_no_longer_held(otmp);
234 /* lamps don't go out when dropped */
235 if ((cont || artifact_light(otmp)) && obj_is_burning(otmp))
236 end_burn(otmp, TRUE); /* smother in statue */
238 if (otmp->otyp == SLIME_MOLD)
239 goodfruit(otmp->spe);
244 (void) add_to_minv(mtmp, otmp);
246 (void) add_to_container(cont, otmp);
248 place_object(otmp, x, y);
251 cont->owt = weight(cont);
254 /* possibly restore oracle's room and/or put her back inside it; returns
255 False if she's on the wrong level and should be removed, True otherwise */
258 struct monst *oracle;
263 /* oracle doesn't move, but knight's joust or monk's staggering blow
264 could push her onto a hole in the floor; at present, traps don't
265 activate in such situation hence she won't fall to another level;
266 however, that could change so be prepared to cope with such things */
267 if (!Is_oracle_level(&u.uz))
270 oracle->mpeaceful = 1;
271 o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
272 if (o_ridx >= 0 && rooms[o_ridx].rtype == DELPHI)
273 return TRUE; /* no fixup needed */
276 * The Oracle isn't in DELPHI room. Either hero entered her chamber
277 * and got the one-time welcome message, converting it into an
278 * ordinary room, or she got teleported out, or both. Try to put
279 * her back inside her room, if necessary, and restore its type.
282 /* find original delphi chamber; should always succeed */
283 for (ridx = 0; ridx < SIZE(rooms); ++ridx)
284 if (rooms[ridx].orig_rtype == DELPHI)
287 if (o_ridx != ridx && ridx < SIZE(rooms)) {
288 /* room found and she's not not in it, so try to move her there */
289 cc.x = (rooms[ridx].lx + rooms[ridx].hx) / 2;
290 cc.y = (rooms[ridx].ly + rooms[ridx].hy) / 2;
291 if (enexto(&cc, cc.x, cc.y, oracle->data)) {
292 rloc_to(oracle, cc.x, cc.y);
293 o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
295 /* [if her room is already full, she might end up outside;
296 that's ok, next hero just won't get any welcome message,
297 same as used to happen before this fixup was introduced] */
299 if (ridx == o_ridx) /* if she's in her room, mark it as such */
300 rooms[ridx].rtype = DELPHI;
301 return TRUE; /* keep oracle in new bones file */
304 /* check whether bones are feasible */
308 register struct trap *ttmp;
312 if (ledger_no(&u.uz) <= 0 || ledger_no(&u.uz) > maxledgerno())
314 if (no_bones_level(&u.uz))
315 return FALSE; /* no bones for specific levels */
317 return FALSE; /* no bones when swallowed */
319 if (!Is_branchlev(&u.uz)) {
320 /* no bones on non-branches with portals */
321 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
322 if (ttmp->ttyp == MAGIC_PORTAL)
326 if (depth(&u.uz) <= 0 /* bulletproofing for endgame */
327 || (!rn2(1 + (depth(&u.uz) >> 2)) /* fewer ghosts on low levels */
330 /* don't let multiple restarts generate multiple copies of objects
337 /* save bones and possessions of a deceased adventurer */
339 savebones(how, when, corpse)
347 struct permonst *mptr;
349 struct cemetery *newbones;
353 /* caller has already checked `can_make_bones()' */
356 fd = open_bonesfile(&u.uz, &bonesid);
361 if (yn("Bones file already exists. Replace it?") == 'y') {
363 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') {
364 if (delete_bonesfile(&u.uz))
368 pline("Cannot unlink old bones.");
370 pline("
\8cÃ
\82¢
\8d\9c\82ð
\8dí
\8f\9c\82Å
\82«
\82È
\82©
\82Á
\82½
\81D");
373 /* compression can change the file's name, so must
374 wait until after any attempt to delete this file */
375 compress_bonesfile();
381 /* in case these characters are not in their home bases */
382 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
383 if (DEADMONSTER(mtmp))
386 if (mtmp->iswiz || mptr == &mons[PM_MEDUSA]
387 || mptr->msound == MS_NEMESIS || mptr->msound == MS_LEADER
388 || mptr == &mons[PM_VLAD_THE_IMPALER]
389 || (mptr == &mons[PM_ORACLE] && !fixuporacle(mtmp)))
393 dismount_steed(DISMOUNT_BONES);
394 dmonsfree(); /* discard dead or gone monsters */
396 /* mark all fruits as nonexistent; when we come to them we'll mark
397 * them as existing (using goodfruit())
399 for (f = ffruit; f; f = f->nextf)
402 /* check iron balls separately--maybe they're not carrying it */
404 uball->owornmask = uchain->owornmask = 0;
406 /* dispose of your possessions, usually cursed */
407 if (u.ugrave_arise == (NON_PM - 1)) {
410 /* embed your possessions in your statue */
411 otmp = mk_named_object(STATUE, &mons[u.umonnum], u.ux, u.uy, plname);
413 drop_upon_death((struct monst *) 0, otmp, u.ux, u.uy);
415 return; /* couldn't make statue */
416 mtmp = (struct monst *) 0;
417 } else if (u.ugrave_arise < LOW_PM) {
418 /* drop everything */
419 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
420 /* trick makemon() into allowing monster creation
424 mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME);
428 mtmp = christen_monst(mtmp, plname);
430 (void) obj_attach_mid(corpse, mtmp->m_id);
432 /* give your possessions to the monster you become */
433 in_mklev = TRUE; /* use <u.ux,u.uy> as-is */
434 mtmp = makemon(&mons[u.ugrave_arise], u.ux, u.uy, NO_MINVENT);
437 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
438 u.ugrave_arise = NON_PM; /* in case caller cares */
441 /* give mummy-from-hero a wrapping unless hero already
442 carries one; don't bother forcing it to become worn */
443 if (mtmp->data->mlet == S_MUMMY && !carrying(MUMMY_WRAPPING))
444 (void) mongets(mtmp, MUMMY_WRAPPING);
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 m_dowear(mtmp, TRUE);
454 mtmp->m_lev = (u.ulevel ? u.ulevel : 1);
455 mtmp->mhp = mtmp->mhpmax = u.uhpmax;
456 mtmp->female = flags.female;
459 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
460 resetobjs(mtmp->minvent, FALSE);
461 /* do not zero out m_ids for bones levels any more */
464 mtmp->mtame = mtmp->mpeaceful = 0;
466 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
468 ttmp->tseen = (ttmp->ttyp == HOLE);
470 resetobjs(fobj, FALSE);
471 resetobjs(level.buriedobjlist, FALSE);
473 /* Hero is no longer on the map. */
476 /* Clear all memory from the level. */
477 for (x = 1; x < COLNO; x++)
478 for (y = 0; y < ROWNO; y++) {
479 levl[x][y].seenv = 0;
480 levl[x][y].waslit = 0;
481 levl[x][y].glyph = cmap_to_glyph(S_stone);
482 lastseentyp[x][y] = 0;
485 /* Attach bones info to the current level before saving. */
486 newbones = (struct cemetery *) alloc(sizeof *newbones);
487 /* entries are '\0' terminated but have fixed length allocations,
488 so pre-fill with spaces to initialize any excess room */
489 (void) memset((genericptr_t) newbones, ' ', sizeof *newbones);
490 /* format name+role,&c, death reason, and date+time;
491 gender and alignment reflect final values rather than what the
492 character started out as, same as topten and logfile entries */
493 Sprintf(newbones->who, "%s-%.3s-%.3s-%.3s-%.3s", plname, urole.filecode,
494 urace.filecode, genders[flags.female].filecode,
495 aligns[1 - u.ualign.type].filecode);
496 formatkiller(newbones->how, sizeof newbones->how, how, TRUE);
497 Strcpy(newbones->when, yyyymmddhhmmss(when));
498 /* final resting place, used to decide when bones are discovered */
499 newbones->frpx = u.ux, newbones->frpy = u.uy;
500 newbones->bonesknown = FALSE;
501 /* if current character died on a bones level, the cemetery list
502 will have multiple entries, most recent (this dead hero) first */
503 newbones->next = level.bonesinfo;
504 level.bonesinfo = newbones;
505 /* flag these bones if they are being created in wizard mode;
506 they might already be flagged as such, even when we're playing
507 in normal mode, if this level came from a previous bones file */
509 level.flags.wizard_bones = 1;
511 fd = create_bonesfile(&u.uz, &bonesid, whynot);
515 /* bones file creation problems are silent to the player.
516 * Keep it that way, but place a clue into the paniclog.
518 paniclog("savebones", whynot);
521 c = (char) (strlen(bonesid) + 1);
523 #ifdef MFLOPPY /* check whether there is room */
524 if (iflags.checkspace) {
525 savelev(fd, ledger_no(&u.uz), COUNT_SAVE);
526 /* savelev() initializes bytes_counted to 0, so it must come
527 * first here even though it does not in the real save. the
528 * resulting extra bflush() at the end of savelev() may increase
529 * bytes_counted by a couple over what the real usage will be.
531 * note it is safe to call store_version() here only because
532 * bufon() is null for ZEROCOMP, which MFLOPPY uses -- otherwise
533 * this code would have to know the size of the version
534 * information itself.
537 store_savefileinfo(fd);
538 bwrite(fd, (genericptr_t) &c, sizeof c);
539 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
540 savefruitchn(fd, COUNT_SAVE);
542 if (bytes_counted > freediskspace(bones)) { /* not enough room */
545 pline("Insufficient space to create bones file.");
547 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");
552 co_false(); /* make sure stuff before savelev() gets written */
557 store_savefileinfo(fd);
558 bwrite(fd, (genericptr_t) &c, sizeof c);
559 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
560 savefruitchn(fd, WRITE_SAVE | FREE_SAVE);
561 update_mlstmv(); /* update monsters for eventual restoration */
562 savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE);
564 commit_bonesfile(&u.uz);
565 compress_bonesfile();
573 char c, *bonesid, oldbonesid[40]; /* was [10]; more should be safer */
575 if (discover) /* save bones files for real games */
580 /* wizard check added by GAN 02/05/87 */
581 if (rn2(3) /* only once in three times do we find bones */
584 if (no_bones_level(&u.uz))
586 fd = open_bonesfile(&u.uz, &bonesid);
590 if (validate(fd, bones) != 0) {
593 pline("Discarding unuseable bones; no need to panic...");
595 pline("
\8eg
\82¦
\82È
\82¢
\8d\9c\82ð
\8eÌ
\82Ä
\82½
\81D
\8dQ
\82Ä
\82é
\95K
\97v
\82Í
\82È
\82¢
\81D
\81D
\81D");
601 if (yn("Get bones?") == 'n') {
603 if (yn("
\8d\9c\82ð
\8fE
\82¤
\81H") == 'n') {
605 compress_bonesfile();
609 mread(fd, (genericptr_t) &c, sizeof c); /* length incl. '\0' */
610 mread(fd, (genericptr_t) oldbonesid, (unsigned) c); /* DD.nnn */
611 if (strcmp(bonesid, oldbonesid) != 0
612 /* from 3.3.0 through 3.6.0, bones in the quest branch stored
613 a bogus bonesid in the file; 3.6.1 fixed that, but for
614 3.6.0 bones to remain compatible, we need an extra test;
615 once compatibility with 3.6.x goes away, this can too
616 (we don't try to make this conditional upon the value of
617 VERSION_COMPATIBILITY because then we'd need patchlevel.h) */
618 && (strlen(bonesid) <= 2
619 || strcmp(bonesid + 2, oldbonesid) != 0)) {
623 Sprintf(errbuf, "This is bones level '%s', not '%s'!", oldbonesid,
625 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,
629 ok = FALSE; /* won't die of trickery */
633 register struct monst *mtmp;
635 getlev(fd, 0, 0, TRUE);
637 /* Note that getlev() now keeps tabs on unique
638 * monsters such as demon lords, and tracks the
639 * birth counts of all species just as makemon()
640 * does. If a bones monster is extinct or has been
641 * subject to genocide, their mhpmax will be
642 * set to the magic DEFUNCT_MONSTER cookie value.
644 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
646 sanitize_name(MNAME(mtmp));
647 if (mtmp->mhpmax == DEFUNCT_MONSTER) {
649 debugpline1("Removing defunct monster %s from bones.",
654 /* to correctly reset named artifacts on the level */
655 resetobjs(mtmp->minvent, TRUE);
657 resetobjs(fobj, TRUE);
658 resetobjs(level.buriedobjlist, TRUE);
662 sanitize_engravings();
663 u.uroleplay.numbones++;
667 if (yn("Unlink bones?") == 'n') {
669 if (yn("
\8d\9c\82ð
\8fÁ
\82·
\81H") == 'n') {
670 compress_bonesfile();
674 if (!delete_bonesfile(&u.uz)) {
675 /* When N games try to simultaneously restore the same
676 * bones file, N-1 of them will fail to delete it
677 * (the first N-1 under AmigaDOS, the last N-1 under UNIX).
678 * So no point in a mysterious message for a normal event
679 * -- just generate a new level for those N-1 games.
681 /* pline("Cannot unlink bones."); */