1 /* NetHack 3.6 bones.c $NHDT-Date: 1449269914 2015/12/04 22:58:34 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.66 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985,1993. */
3 /* NetHack may be freely redistributed. See license for details. */
8 extern char bones[]; /* from files.c */
10 extern long bytes_counted;
13 STATIC_DCL boolean FDECL(no_bones_level, (d_level *));
14 STATIC_DCL void FDECL(goodfruit, (int));
15 STATIC_DCL void FDECL(resetobjs, (struct obj *, BOOLEAN_P));
16 STATIC_DCL boolean FDECL(fixuporacle, (struct monst *));
22 extern d_level save_dlevel; /* in do.c */
25 if (ledger_no(&save_dlevel))
26 assign_level(lev, &save_dlevel);
28 return (boolean) (((sptr = Is_special(lev)) != 0 && !sptr->boneid)
29 || !dungeons[lev->dnum].boneid
30 /* no bones on the last or multiway branch levels
31 in any dungeon (level 1 isn't multiway) */
33 || (Is_branchlev(lev) && lev->dlevel > 1)
34 /* no bones in the invocation level */
36 && lev->dlevel == dunlevs_in_dungeon(lev) - 1));
39 /* Call this function for each fruit object saved in the bones level: it marks
40 * that particular type of fruit as existing (the marker is that that type's
41 * ID is positive instead of negative). This way, when we later save the
42 * chain of fruit types, we know to only save the types that exist.
48 register struct fruit *f;
50 for (f = ffruit; f; f = f->nextf) {
59 resetobjs(ochain, restore)
63 struct obj *otmp, *nobj;
65 for (otmp = ochain; otmp; otmp = nobj) {
68 resetobjs(otmp->cobj, restore);
70 obj_extract_self(otmp);
76 /* artifact bookkeeping needs to be done during
77 restore; other fixups are done while saving */
78 if (otmp->oartifact) {
79 if (exist_artifact(otmp->otyp, safe_oname(otmp))
80 || is_quest_artifact(otmp)) {
81 /* prevent duplicate--revert to ordinary obj */
86 artifact_exists(otmp, safe_oname(otmp), TRUE);
88 } else if (has_oname(otmp)) {
89 sanitize_name(ONAME(otmp));
92 /* do not zero out o_ids for ghost levels anymore */
94 if (objects[otmp->otyp].oc_uses_known)
96 otmp->dknown = otmp->bknown = 0;
102 otmp->was_thrown = 0;
104 /* strip user-supplied names */
105 /* Statue and some corpse names are left intact,
106 presumably in case they came from score file.
107 [TODO: this ought to be done differently--names
108 which came from such a source or came from any
109 stoned or killed monster should be flagged in
110 some manner; then we could just check the flag
111 here and keep "real" names (dead pets, &c) while
112 discarding player notes attached to statues.] */
114 && !(otmp->oartifact || otmp->otyp == STATUE
115 || otmp->otyp == SPE_NOVEL
116 || (otmp->otyp == CORPSE
117 && otmp->corpsenm >= SPECIAL_PM))) {
121 if (otmp->otyp == SLIME_MOLD)
122 goodfruit(otmp->spe);
124 else if (otmp->otyp == SCR_MAIL)
127 else if (otmp->otyp == EGG)
129 else if (otmp->otyp == TIN) {
130 /* make tins of unique monster's meat be empty */
131 if (otmp->corpsenm >= LOW_PM
132 && unique_corpstat(&mons[otmp->corpsenm]))
133 otmp->corpsenm = NON_PM;
134 } else if (otmp->otyp == CORPSE || otmp->otyp == STATUE) {
135 int mnum = otmp->corpsenm;
137 /* Discard incarnation details of unique
138 monsters (by passing null instead of otmp
139 for object), shopkeepers (by passing false
140 for revival flag), temple priests, and
141 vault guards in order to prevent corpse
142 revival or statue reanimation. */
144 && cant_revive(&mnum, FALSE, (struct obj *) 0)) {
146 /* mnum is now either human_zombie or
147 doppelganger; for corpses of uniques,
148 we need to force the transformation
149 now rather than wait until a revival
150 attempt, otherwise eating this corpse
151 would behave as if it remains unique */
152 if (mnum == PM_DOPPELGANGER && otmp->otyp == CORPSE)
153 set_corpsenm(otmp, mnum);
155 } else if (otmp->otyp == AMULET_OF_YENDOR) {
156 /* no longer the real Amulet */
157 otmp->otyp = FAKE_AMULET_OF_YENDOR;
159 } else if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
161 end_burn(otmp, TRUE);
162 otmp->otyp = WAX_CANDLE;
163 otmp->age = 50L; /* assume used */
165 otmp->quan = (long) otmp->spe;
167 otmp->owt = weight(otmp);
169 } else if (otmp->otyp == BELL_OF_OPENING) {
172 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
173 otmp->otyp = SPE_BLANK_PAPER;
180 /* while loading bones, strip out text possibly supplied by old player
181 that might accidentally or maliciously disrupt new player's display */
183 sanitize_name(namebuf)
187 boolean strip_8th_bit =
188 !strcmp(windowprocs.name, "tty") && !iflags.wc_eight_bit_input;
190 /* it's tempting to skip this for single-user platforms, since
191 only the current player could have left these bones--except
192 things like "hearse" and other bones exchange schemes make
193 that assumption false */
196 if (c < ' ' || c == '\177') {
197 /* non-printable or undesirable */
199 } else if (c != *namebuf) {
200 /* expected to be printable if user wants such things */
208 /* called by savebones(); also by finish_paybill(shk.c) */
210 drop_upon_death(mtmp, cont, x, y)
217 u.twoweap = 0; /* ensure curse() won't cause swapwep to drop twice */
218 while ((otmp = invent) != 0) {
219 obj_extract_self(otmp);
220 obj_no_longer_held(otmp);
223 /* lamps don't go out when dropped */
224 if ((cont || artifact_light(otmp)) && obj_is_burning(otmp))
225 end_burn(otmp, TRUE); /* smother in statue */
227 if (otmp->otyp == SLIME_MOLD)
228 goodfruit(otmp->spe);
233 (void) add_to_minv(mtmp, otmp);
235 (void) add_to_container(cont, otmp);
237 place_object(otmp, x, y);
240 cont->owt = weight(cont);
243 /* possibly restore oracle's room and/or put her back inside it; returns
244 False if she's on the wrong level and should be removed, True otherwise */
247 struct monst *oracle;
252 /* oracle doesn't move, but knight's joust or monk's staggering blow
253 could push her onto a hole in the floor; at present, traps don't
254 activate in such situation hence she won't fall to another level;
255 however, that could change so be prepared to cope with such things */
256 if (!Is_oracle_level(&u.uz))
259 oracle->mpeaceful = 1;
260 o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
261 if (o_ridx >= 0 && rooms[o_ridx].rtype == DELPHI)
262 return TRUE; /* no fixup needed */
265 * The Oracle isn't in DELPHI room. Either hero entered her chamber
266 * and got the one-time welcome message, converting it into an
267 * ordinary room, or she got teleported out, or both. Try to put
268 * her back inside her room, if necessary, and restore its type.
271 /* find original delphi chamber; should always succeed */
272 for (ridx = 0; ridx < SIZE(rooms); ++ridx)
273 if (rooms[ridx].orig_rtype == DELPHI)
276 if (o_ridx != ridx && ridx < SIZE(rooms)) {
277 /* room found and she's not not in it, so try to move her there */
278 cc.x = (rooms[ridx].lx + rooms[ridx].hx) / 2;
279 cc.y = (rooms[ridx].ly + rooms[ridx].hy) / 2;
280 if (enexto(&cc, cc.x, cc.y, oracle->data)) {
281 rloc_to(oracle, cc.x, cc.y);
282 o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
284 /* [if her room is already full, she might end up outside;
285 that's ok, next hero just won't get any welcome message,
286 same as used to happen before this fixup was introduced] */
288 if (ridx == o_ridx) /* if she's in her room, mark it as such */
289 rooms[ridx].rtype = DELPHI;
290 return TRUE; /* keep oracle in new bones file */
293 /* check whether bones are feasible */
297 register struct trap *ttmp;
301 if (ledger_no(&u.uz) <= 0 || ledger_no(&u.uz) > maxledgerno())
303 if (no_bones_level(&u.uz))
304 return FALSE; /* no bones for specific levels */
306 return FALSE; /* no bones when swallowed */
308 if (!Is_branchlev(&u.uz)) {
309 /* no bones on non-branches with portals */
310 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
311 if (ttmp->ttyp == MAGIC_PORTAL)
315 if (depth(&u.uz) <= 0 /* bulletproofing for endgame */
316 || (!rn2(1 + (depth(&u.uz) >> 2)) /* fewer ghosts on low levels */
319 /* don't let multiple restarts generate multiple copies of objects
326 /* save bones and possessions of a deceased adventurer */
328 savebones(how, when, corpse)
336 struct permonst *mptr;
338 struct cemetery *newbones;
342 /* caller has already checked `can_make_bones()' */
345 fd = open_bonesfile(&u.uz, &bonesid);
350 if (yn("Bones file already exists. Replace it?") == 'y') {
352 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') {
353 if (delete_bonesfile(&u.uz))
357 pline("Cannot unlink old bones.");
359 pline("
\8cÃ
\82¢
\8d\9c\82ð
\8dí
\8f\9c\82Å
\82«
\82È
\82©
\82Á
\82½
\81D");
362 /* compression can change the file's name, so must
363 wait until after any attempt to delete this file */
364 compress_bonesfile();
370 /* in case these characters are not in their home bases */
371 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
372 if (DEADMONSTER(mtmp))
375 if (mtmp->iswiz || mptr == &mons[PM_MEDUSA]
376 || mptr->msound == MS_NEMESIS || mptr->msound == MS_LEADER
377 || mptr == &mons[PM_VLAD_THE_IMPALER]
378 || (mptr == &mons[PM_ORACLE] && !fixuporacle(mtmp)))
382 dismount_steed(DISMOUNT_BONES);
383 dmonsfree(); /* discard dead or gone monsters */
385 /* mark all fruits as nonexistent; when we come to them we'll mark
386 * them as existing (using goodfruit())
388 for (f = ffruit; f; f = f->nextf)
391 /* check iron balls separately--maybe they're not carrying it */
393 uball->owornmask = uchain->owornmask = 0;
395 /* dispose of your possessions, usually cursed */
396 if (u.ugrave_arise == (NON_PM - 1)) {
399 /* embed your possessions in your statue */
400 otmp = mk_named_object(STATUE, &mons[u.umonnum], u.ux, u.uy, plname);
402 drop_upon_death((struct monst *) 0, otmp, u.ux, u.uy);
404 return; /* couldn't make statue */
405 mtmp = (struct monst *) 0;
406 } else if (u.ugrave_arise < LOW_PM) {
407 /* drop everything */
408 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
409 /* trick makemon() into allowing monster creation
413 mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME);
417 mtmp = christen_monst(mtmp, plname);
419 (void) obj_attach_mid(corpse, mtmp->m_id);
421 /* give your possessions to the monster you become */
422 in_mklev = TRUE; /* use <u.ux,u.uy> as-is */
423 mtmp = makemon(&mons[u.ugrave_arise], u.ux, u.uy, NO_MINVENT);
426 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
427 u.ugrave_arise = NON_PM; /* in case caller cares */
430 /* give mummy-from-hero a wrapping unless hero already
431 carries one; don't bother forcing it to become worn */
432 if (mtmp->data->mlet == S_MUMMY && !carrying(MUMMY_WRAPPING))
433 (void) mongets(mtmp, MUMMY_WRAPPING);
434 mtmp = christen_monst(mtmp, plname);
436 /* ["Your body rises from the dead as an <mname>..." used
437 to be given here, but it has been moved to done() so that
438 it gets delivered even when savebones() isn't called] */
439 drop_upon_death(mtmp, (struct obj *) 0, u.ux, u.uy);
440 m_dowear(mtmp, TRUE);
443 mtmp->m_lev = (u.ulevel ? u.ulevel : 1);
444 mtmp->mhp = mtmp->mhpmax = u.uhpmax;
445 mtmp->female = flags.female;
448 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
449 resetobjs(mtmp->minvent, FALSE);
450 /* do not zero out m_ids for bones levels any more */
453 mtmp->mtame = mtmp->mpeaceful = 0;
455 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
457 ttmp->tseen = (ttmp->ttyp == HOLE);
459 resetobjs(fobj, FALSE);
460 resetobjs(level.buriedobjlist, FALSE);
462 /* Hero is no longer on the map. */
465 /* Clear all memory from the level. */
466 for (x = 1; x < COLNO; x++)
467 for (y = 0; y < ROWNO; y++) {
468 levl[x][y].seenv = 0;
469 levl[x][y].waslit = 0;
470 levl[x][y].glyph = cmap_to_glyph(S_stone);
471 lastseentyp[x][y] = 0;
474 /* Attach bones info to the current level before saving. */
475 newbones = (struct cemetery *) alloc(sizeof *newbones);
476 /* entries are '\0' terminated but have fixed length allocations,
477 so pre-fill with spaces to initialize any excess room */
478 (void) memset((genericptr_t) newbones, ' ', sizeof *newbones);
479 /* format name+role,&c, death reason, and date+time;
480 gender and alignment reflect final values rather than what the
481 character started out as, same as topten and logfile entries */
482 Sprintf(newbones->who, "%s-%.3s-%.3s-%.3s-%.3s", plname, urole.filecode,
483 urace.filecode, genders[flags.female].filecode,
484 aligns[1 - u.ualign.type].filecode);
485 formatkiller(newbones->how, sizeof newbones->how, how);
486 Strcpy(newbones->when, yyyymmddhhmmss(when));
487 /* final resting place, used to decide when bones are discovered */
488 newbones->frpx = u.ux, newbones->frpy = u.uy;
489 newbones->bonesknown = FALSE;
490 /* if current character died on a bones level, the cemetery list
491 will have multiple entries, most recent (this dead hero) first */
492 newbones->next = level.bonesinfo;
493 level.bonesinfo = newbones;
494 /* flag these bones if they are being created in wizard mode;
495 they might already be flagged as such, even when we're playing
496 in normal mode, if this level came from a previous bones file */
498 level.flags.wizard_bones = 1;
500 fd = create_bonesfile(&u.uz, &bonesid, whynot);
504 /* bones file creation problems are silent to the player.
505 * Keep it that way, but place a clue into the paniclog.
507 paniclog("savebones", whynot);
510 c = (char) (strlen(bonesid) + 1);
512 #ifdef MFLOPPY /* check whether there is room */
513 if (iflags.checkspace) {
514 savelev(fd, ledger_no(&u.uz), COUNT_SAVE);
515 /* savelev() initializes bytes_counted to 0, so it must come
516 * first here even though it does not in the real save. the
517 * resulting extra bflush() at the end of savelev() may increase
518 * bytes_counted by a couple over what the real usage will be.
520 * note it is safe to call store_version() here only because
521 * bufon() is null for ZEROCOMP, which MFLOPPY uses -- otherwise
522 * this code would have to know the size of the version
523 * information itself.
526 store_savefileinfo(fd);
527 bwrite(fd, (genericptr_t) &c, sizeof c);
528 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
529 savefruitchn(fd, COUNT_SAVE);
531 if (bytes_counted > freediskspace(bones)) { /* not enough room */
534 pline("Insufficient space to create bones file.");
536 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");
541 co_false(); /* make sure stuff before savelev() gets written */
546 store_savefileinfo(fd);
547 bwrite(fd, (genericptr_t) &c, sizeof c);
548 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
549 savefruitchn(fd, WRITE_SAVE | FREE_SAVE);
550 update_mlstmv(); /* update monsters for eventual restoration */
551 savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE);
553 commit_bonesfile(&u.uz);
554 compress_bonesfile();
562 char c, *bonesid, oldbonesid[10];
564 if (discover) /* save bones files for real games */
569 /* wizard check added by GAN 02/05/87 */
570 if (rn2(3) /* only once in three times do we find bones */
573 if (no_bones_level(&u.uz))
575 fd = open_bonesfile(&u.uz, &bonesid);
579 if (validate(fd, bones) != 0) {
582 pline("Discarding unuseable bones; no need to panic...");
584 pline("
\8eg
\82¦
\82È
\82¢
\8d\9c\82ð
\8eÌ
\82Ä
\82½
\81D
\8dQ
\82Ä
\82é
\95K
\97v
\82Í
\82È
\82¢
\81D
\81D
\81D");
590 if (yn("Get bones?") == 'n') {
592 if (yn("
\8d\9c\82ð
\8fE
\82¤
\81H") == 'n') {
594 compress_bonesfile();
598 mread(fd, (genericptr_t) &c, sizeof c); /* length incl. '\0' */
599 mread(fd, (genericptr_t) oldbonesid, (unsigned) c); /* DD.nnn */
600 if (strcmp(bonesid, oldbonesid) != 0) {
604 Sprintf(errbuf, "This is bones level '%s', not '%s'!", oldbonesid,
606 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,
610 ok = FALSE; /* won't die of trickery */
614 register struct monst *mtmp;
616 getlev(fd, 0, 0, TRUE);
618 /* Note that getlev() now keeps tabs on unique
619 * monsters such as demon lords, and tracks the
620 * birth counts of all species just as makemon()
621 * does. If a bones monster is extinct or has been
622 * subject to genocide, their mhpmax will be
623 * set to the magic DEFUNCT_MONSTER cookie value.
625 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
627 sanitize_name(MNAME(mtmp));
628 if (mtmp->mhpmax == DEFUNCT_MONSTER) {
630 debugpline1("Removing defunct monster %s from bones.",
635 /* to correctly reset named artifacts on the level */
636 resetobjs(mtmp->minvent, TRUE);
638 resetobjs(fobj, TRUE);
639 resetobjs(level.buriedobjlist, TRUE);
643 sanitize_engravings();
644 u.uroleplay.numbones++;
648 if (yn("Unlink bones?") == 'n') {
650 if (yn("
\8d\9c\82ð
\8fÁ
\82·
\81H") == 'n') {
651 compress_bonesfile();
655 if (!delete_bonesfile(&u.uz)) {
656 /* When N games try to simultaneously restore the same
657 * bones file, N-1 of them will fail to delete it
658 * (the first N-1 under AmigaDOS, the last N-1 under UNIX).
659 * So no point in a mysterious message for a normal event
660 * -- just generate a new level for those N-1 games.
662 /* pline("Cannot unlink bones."); */