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. */
5 /* JNetHack Copyright */
6 /* (c) Issei Numata, Naoki Hamada, Shigehiro Miyashita, 1994-2000 */
7 /* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2016 */
8 /* JNetHack may be freely redistributed. See license for details. */
13 extern char bones[]; /* from files.c */
15 extern long bytes_counted;
18 STATIC_DCL boolean FDECL(no_bones_level, (d_level *));
19 STATIC_DCL void FDECL(goodfruit, (int));
20 STATIC_DCL void FDECL(resetobjs, (struct obj *, BOOLEAN_P));
21 STATIC_DCL boolean FDECL(fixuporacle, (struct monst *));
27 extern d_level save_dlevel; /* in do.c */
30 if (ledger_no(&save_dlevel))
31 assign_level(lev, &save_dlevel);
33 return (boolean) (((sptr = Is_special(lev)) != 0 && !sptr->boneid)
34 || !dungeons[lev->dnum].boneid
35 /* no bones on the last or multiway branch levels
36 in any dungeon (level 1 isn't multiway) */
38 || (Is_branchlev(lev) && lev->dlevel > 1)
39 /* no bones in the invocation level */
41 && lev->dlevel == dunlevs_in_dungeon(lev) - 1));
44 /* Call this function for each fruit object saved in the bones level: it marks
45 * that particular type of fruit as existing (the marker is that that type's
46 * ID is positive instead of negative). This way, when we later save the
47 * chain of fruit types, we know to only save the types that exist.
53 register struct fruit *f;
55 for (f = ffruit; f; f = f->nextf) {
64 resetobjs(ochain, restore)
68 struct obj *otmp, *nobj;
70 for (otmp = ochain; otmp; otmp = nobj) {
73 resetobjs(otmp->cobj, restore);
75 obj_extract_self(otmp);
81 /* artifact bookkeeping needs to be done during
82 restore; other fixups are done while saving */
83 if (otmp->oartifact) {
84 if (exist_artifact(otmp->otyp, safe_oname(otmp))
85 || is_quest_artifact(otmp)) {
86 /* prevent duplicate--revert to ordinary obj */
91 artifact_exists(otmp, safe_oname(otmp), TRUE);
93 } else if (has_oname(otmp)) {
94 sanitize_name(ONAME(otmp));
97 /* do not zero out o_ids for ghost levels anymore */
99 if (objects[otmp->otyp].oc_uses_known)
101 otmp->dknown = otmp->bknown = 0;
107 otmp->was_thrown = 0;
109 /* strip user-supplied names */
110 /* Statue and some corpse names are left intact,
111 presumably in case they came from score file.
112 [TODO: this ought to be done differently--names
113 which came from such a source or came from any
114 stoned or killed monster should be flagged in
115 some manner; then we could just check the flag
116 here and keep "real" names (dead pets, &c) while
117 discarding player notes attached to statues.] */
119 && !(otmp->oartifact || otmp->otyp == STATUE
120 || otmp->otyp == SPE_NOVEL
121 || (otmp->otyp == CORPSE
122 && otmp->corpsenm >= SPECIAL_PM))) {
126 if (otmp->otyp == SLIME_MOLD)
127 goodfruit(otmp->spe);
129 else if (otmp->otyp == SCR_MAIL)
132 else if (otmp->otyp == EGG)
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
143 monsters (by passing null instead of otmp
144 for object), shopkeepers (by passing false
145 for revival flag), temple priests, and
146 vault guards in order to prevent corpse
147 revival or statue reanimation. */
149 && cant_revive(&mnum, FALSE, (struct obj *) 0)) {
151 /* mnum is now either human_zombie or
152 doppelganger; for corpses of uniques,
153 we need to force the transformation
154 now rather than wait until a revival
155 attempt, otherwise eating this corpse
156 would behave as if it remains unique */
157 if (mnum == PM_DOPPELGANGER && otmp->otyp == CORPSE)
158 set_corpsenm(otmp, mnum);
160 } else if (otmp->otyp == AMULET_OF_YENDOR) {
161 /* no longer the real Amulet */
162 otmp->otyp = FAKE_AMULET_OF_YENDOR;
164 } else if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
166 end_burn(otmp, TRUE);
167 otmp->otyp = WAX_CANDLE;
168 otmp->age = 50L; /* assume used */
170 otmp->quan = (long) otmp->spe;
172 otmp->owt = weight(otmp);
174 } else if (otmp->otyp == BELL_OF_OPENING) {
177 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
178 otmp->otyp = SPE_BLANK_PAPER;
185 /* while loading bones, strip out text possibly supplied by old player
186 that might accidentally or maliciously disrupt new player's display */
188 sanitize_name(namebuf)
192 boolean strip_8th_bit =
193 !strcmp(windowprocs.name, "tty") && !iflags.wc_eight_bit_input;
195 /* it's tempting to skip this for single-user platforms, since
196 only the current player could have left these bones--except
197 things like "hearse" and other bones exchange schemes make
198 that assumption false */
201 if (c < ' ' || c == '\177') {
202 /* non-printable or undesirable */
204 } else if (c != *namebuf) {
205 /* expected to be printable if user wants such things */
213 /* called by savebones(); also by finish_paybill(shk.c) */
215 drop_upon_death(mtmp, cont, x, y)
222 u.twoweap = 0; /* ensure curse() won't cause swapwep to drop twice */
223 while ((otmp = invent) != 0) {
224 obj_extract_self(otmp);
225 obj_no_longer_held(otmp);
228 /* lamps don't go out when dropped */
229 if ((cont || artifact_light(otmp)) && obj_is_burning(otmp))
230 end_burn(otmp, TRUE); /* smother in statue */
232 if (otmp->otyp == SLIME_MOLD)
233 goodfruit(otmp->spe);
238 (void) add_to_minv(mtmp, otmp);
240 (void) add_to_container(cont, otmp);
242 place_object(otmp, x, y);
245 cont->owt = weight(cont);
248 /* possibly restore oracle's room and/or put her back inside it; returns
249 False if she's on the wrong level and should be removed, True otherwise */
252 struct monst *oracle;
257 /* oracle doesn't move, but knight's joust or monk's staggering blow
258 could push her onto a hole in the floor; at present, traps don't
259 activate in such situation hence she won't fall to another level;
260 however, that could change so be prepared to cope with such things */
261 if (!Is_oracle_level(&u.uz))
264 oracle->mpeaceful = 1;
265 o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
266 if (o_ridx >= 0 && rooms[o_ridx].rtype == DELPHI)
267 return TRUE; /* no fixup needed */
270 * The Oracle isn't in DELPHI room. Either hero entered her chamber
271 * and got the one-time welcome message, converting it into an
272 * ordinary room, or she got teleported out, or both. Try to put
273 * her back inside her room, if necessary, and restore its type.
276 /* find original delphi chamber; should always succeed */
277 for (ridx = 0; ridx < SIZE(rooms); ++ridx)
278 if (rooms[ridx].orig_rtype == DELPHI)
281 if (o_ridx != ridx && ridx < SIZE(rooms)) {
282 /* room found and she's not not in it, so try to move her there */
283 cc.x = (rooms[ridx].lx + rooms[ridx].hx) / 2;
284 cc.y = (rooms[ridx].ly + rooms[ridx].hy) / 2;
285 if (enexto(&cc, cc.x, cc.y, oracle->data)) {
286 rloc_to(oracle, cc.x, cc.y);
287 o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
289 /* [if her room is already full, she might end up outside;
290 that's ok, next hero just won't get any welcome message,
291 same as used to happen before this fixup was introduced] */
293 if (ridx == o_ridx) /* if she's in her room, mark it as such */
294 rooms[ridx].rtype = DELPHI;
295 return TRUE; /* keep oracle in new bones file */
298 /* check whether bones are feasible */
302 register struct trap *ttmp;
306 if (ledger_no(&u.uz) <= 0 || ledger_no(&u.uz) > maxledgerno())
308 if (no_bones_level(&u.uz))
309 return FALSE; /* no bones for specific levels */
311 return FALSE; /* no bones when swallowed */
313 if (!Is_branchlev(&u.uz)) {
314 /* no bones on non-branches with portals */
315 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
316 if (ttmp->ttyp == MAGIC_PORTAL)
320 if (depth(&u.uz) <= 0 /* bulletproofing for endgame */
321 || (!rn2(1 + (depth(&u.uz) >> 2)) /* fewer ghosts on low levels */
324 /* don't let multiple restarts generate multiple copies of objects
331 /* save bones and possessions of a deceased adventurer */
333 savebones(how, when, corpse)
341 struct permonst *mptr;
343 struct cemetery *newbones;
347 /* caller has already checked `can_make_bones()' */
350 fd = open_bonesfile(&u.uz, &bonesid);
355 if (yn("Bones file already exists. Replace it?") == 'y') {
357 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') {
358 if (delete_bonesfile(&u.uz))
362 pline("Cannot unlink old bones.");
364 pline("
\8cÃ
\82¢
\8d\9c\82ð
\8dí
\8f\9c\82Å
\82«
\82È
\82©
\82Á
\82½
\81D");
367 /* compression can change the file's name, so must
368 wait until after any attempt to delete this file */
369 compress_bonesfile();
375 /* in case these characters are not in their home bases */
376 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
377 if (DEADMONSTER(mtmp))
380 if (mtmp->iswiz || mptr == &mons[PM_MEDUSA]
381 || mptr->msound == MS_NEMESIS || mptr->msound == MS_LEADER
382 || mptr == &mons[PM_VLAD_THE_IMPALER]
383 || (mptr == &mons[PM_ORACLE] && !fixuporacle(mtmp)))
387 dismount_steed(DISMOUNT_BONES);
388 dmonsfree(); /* discard dead or gone monsters */
390 /* mark all fruits as nonexistent; when we come to them we'll mark
391 * them as existing (using goodfruit())
393 for (f = ffruit; f; f = f->nextf)
396 /* check iron balls separately--maybe they're not carrying it */
398 uball->owornmask = uchain->owornmask = 0;
400 /* dispose of your possessions, usually cursed */
401 if (u.ugrave_arise == (NON_PM - 1)) {
404 /* embed your possessions in your statue */
405 otmp = mk_named_object(STATUE, &mons[u.umonnum], u.ux, u.uy, plname);
407 drop_upon_death((struct monst *) 0, otmp, u.ux, u.uy);
409 return; /* couldn't make statue */
410 mtmp = (struct monst *) 0;
411 } else if (u.ugrave_arise < LOW_PM) {
412 /* drop everything */
413 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
414 /* trick makemon() into allowing monster creation
418 mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME);
422 mtmp = christen_monst(mtmp, plname);
424 (void) obj_attach_mid(corpse, mtmp->m_id);
426 /* give your possessions to the monster you become */
427 in_mklev = TRUE; /* use <u.ux,u.uy> as-is */
428 mtmp = makemon(&mons[u.ugrave_arise], u.ux, u.uy, NO_MINVENT);
431 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
432 u.ugrave_arise = NON_PM; /* in case caller cares */
435 /* give mummy-from-hero a wrapping unless hero already
436 carries one; don't bother forcing it to become worn */
437 if (mtmp->data->mlet == S_MUMMY && !carrying(MUMMY_WRAPPING))
438 (void) mongets(mtmp, MUMMY_WRAPPING);
439 mtmp = christen_monst(mtmp, plname);
441 /* ["Your body rises from the dead as an <mname>..." used
442 to be given here, but it has been moved to done() so that
443 it gets delivered even when savebones() isn't called] */
444 drop_upon_death(mtmp, (struct obj *) 0, u.ux, u.uy);
445 m_dowear(mtmp, TRUE);
448 mtmp->m_lev = (u.ulevel ? u.ulevel : 1);
449 mtmp->mhp = mtmp->mhpmax = u.uhpmax;
450 mtmp->female = flags.female;
453 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
454 resetobjs(mtmp->minvent, FALSE);
455 /* do not zero out m_ids for bones levels any more */
458 mtmp->mtame = mtmp->mpeaceful = 0;
460 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
462 ttmp->tseen = (ttmp->ttyp == HOLE);
464 resetobjs(fobj, FALSE);
465 resetobjs(level.buriedobjlist, FALSE);
467 /* Hero is no longer on the map. */
470 /* Clear all memory from the level. */
471 for (x = 1; x < COLNO; x++)
472 for (y = 0; y < ROWNO; y++) {
473 levl[x][y].seenv = 0;
474 levl[x][y].waslit = 0;
475 levl[x][y].glyph = cmap_to_glyph(S_stone);
476 lastseentyp[x][y] = 0;
479 /* Attach bones info to the current level before saving. */
480 newbones = (struct cemetery *) alloc(sizeof *newbones);
481 /* entries are '\0' terminated but have fixed length allocations,
482 so pre-fill with spaces to initialize any excess room */
483 (void) memset((genericptr_t) newbones, ' ', sizeof *newbones);
484 /* format name+role,&c, death reason, and date+time;
485 gender and alignment reflect final values rather than what the
486 character started out as, same as topten and logfile entries */
487 Sprintf(newbones->who, "%s-%.3s-%.3s-%.3s-%.3s", plname, urole.filecode,
488 urace.filecode, genders[flags.female].filecode,
489 aligns[1 - u.ualign.type].filecode);
490 formatkiller(newbones->how, sizeof newbones->how, how);
491 Strcpy(newbones->when, yyyymmddhhmmss(when));
492 /* final resting place, used to decide when bones are discovered */
493 newbones->frpx = u.ux, newbones->frpy = u.uy;
494 newbones->bonesknown = FALSE;
495 /* if current character died on a bones level, the cemetery list
496 will have multiple entries, most recent (this dead hero) first */
497 newbones->next = level.bonesinfo;
498 level.bonesinfo = newbones;
499 /* flag these bones if they are being created in wizard mode;
500 they might already be flagged as such, even when we're playing
501 in normal mode, if this level came from a previous bones file */
503 level.flags.wizard_bones = 1;
505 fd = create_bonesfile(&u.uz, &bonesid, whynot);
509 /* bones file creation problems are silent to the player.
510 * Keep it that way, but place a clue into the paniclog.
512 paniclog("savebones", whynot);
515 c = (char) (strlen(bonesid) + 1);
517 #ifdef MFLOPPY /* check whether there is room */
518 if (iflags.checkspace) {
519 savelev(fd, ledger_no(&u.uz), COUNT_SAVE);
520 /* savelev() initializes bytes_counted to 0, so it must come
521 * first here even though it does not in the real save. the
522 * resulting extra bflush() at the end of savelev() may increase
523 * bytes_counted by a couple over what the real usage will be.
525 * note it is safe to call store_version() here only because
526 * bufon() is null for ZEROCOMP, which MFLOPPY uses -- otherwise
527 * this code would have to know the size of the version
528 * information itself.
531 store_savefileinfo(fd);
532 bwrite(fd, (genericptr_t) &c, sizeof c);
533 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
534 savefruitchn(fd, COUNT_SAVE);
536 if (bytes_counted > freediskspace(bones)) { /* not enough room */
539 pline("Insufficient space to create bones file.");
541 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");
546 co_false(); /* make sure stuff before savelev() gets written */
551 store_savefileinfo(fd);
552 bwrite(fd, (genericptr_t) &c, sizeof c);
553 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
554 savefruitchn(fd, WRITE_SAVE | FREE_SAVE);
555 update_mlstmv(); /* update monsters for eventual restoration */
556 savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE);
558 commit_bonesfile(&u.uz);
559 compress_bonesfile();
567 char c, *bonesid, oldbonesid[10];
569 if (discover) /* save bones files for real games */
574 /* wizard check added by GAN 02/05/87 */
575 if (rn2(3) /* only once in three times do we find bones */
578 if (no_bones_level(&u.uz))
580 fd = open_bonesfile(&u.uz, &bonesid);
584 if (validate(fd, bones) != 0) {
587 pline("Discarding unuseable bones; no need to panic...");
589 pline("
\8eg
\82¦
\82È
\82¢
\8d\9c\82ð
\8eÌ
\82Ä
\82½
\81D
\8dQ
\82Ä
\82é
\95K
\97v
\82Í
\82È
\82¢
\81D
\81D
\81D");
595 if (yn("Get bones?") == 'n') {
597 if (yn("
\8d\9c\82ð
\8fE
\82¤
\81H") == 'n') {
599 compress_bonesfile();
603 mread(fd, (genericptr_t) &c, sizeof c); /* length incl. '\0' */
604 mread(fd, (genericptr_t) oldbonesid, (unsigned) c); /* DD.nnn */
605 if (strcmp(bonesid, oldbonesid) != 0) {
609 Sprintf(errbuf, "This is bones level '%s', not '%s'!", oldbonesid,
611 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,
615 ok = FALSE; /* won't die of trickery */
619 register struct monst *mtmp;
621 getlev(fd, 0, 0, TRUE);
623 /* Note that getlev() now keeps tabs on unique
624 * monsters such as demon lords, and tracks the
625 * birth counts of all species just as makemon()
626 * does. If a bones monster is extinct or has been
627 * subject to genocide, their mhpmax will be
628 * set to the magic DEFUNCT_MONSTER cookie value.
630 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
632 sanitize_name(MNAME(mtmp));
633 if (mtmp->mhpmax == DEFUNCT_MONSTER) {
635 debugpline1("Removing defunct monster %s from bones.",
640 /* to correctly reset named artifacts on the level */
641 resetobjs(mtmp->minvent, TRUE);
643 resetobjs(fobj, TRUE);
644 resetobjs(level.buriedobjlist, TRUE);
648 sanitize_engravings();
649 u.uroleplay.numbones++;
653 if (yn("Unlink bones?") == 'n') {
655 if (yn("
\8d\9c\82ð
\8fÁ
\82·
\81H") == 'n') {
656 compress_bonesfile();
660 if (!delete_bonesfile(&u.uz)) {
661 /* When N games try to simultaneously restore the same
662 * bones file, N-1 of them will fail to delete it
663 * (the first N-1 under AmigaDOS, the last N-1 under UNIX).
664 * So no point in a mysterious message for a normal event
665 * -- just generate a new level for those N-1 games.
667 /* pline("Cannot unlink bones."); */