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);
349 if (yn("Bones file already exists. Replace it?") == 'y') {
350 if (delete_bonesfile(&u.uz))
353 pline("Cannot unlink old bones.");
356 /* compression can change the file's name, so must
357 wait until after any attempt to delete this file */
358 compress_bonesfile();
364 /* in case these characters are not in their home bases */
365 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
366 if (DEADMONSTER(mtmp))
369 if (mtmp->iswiz || mptr == &mons[PM_MEDUSA]
370 || mptr->msound == MS_NEMESIS || mptr->msound == MS_LEADER
371 || mptr == &mons[PM_VLAD_THE_IMPALER]
372 || (mptr == &mons[PM_ORACLE] && !fixuporacle(mtmp)))
376 dismount_steed(DISMOUNT_BONES);
377 dmonsfree(); /* discard dead or gone monsters */
379 /* mark all fruits as nonexistent; when we come to them we'll mark
380 * them as existing (using goodfruit())
382 for (f = ffruit; f; f = f->nextf)
385 /* check iron balls separately--maybe they're not carrying it */
387 uball->owornmask = uchain->owornmask = 0;
389 /* dispose of your possessions, usually cursed */
390 if (u.ugrave_arise == (NON_PM - 1)) {
393 /* embed your possessions in your statue */
394 otmp = mk_named_object(STATUE, &mons[u.umonnum], u.ux, u.uy, plname);
396 drop_upon_death((struct monst *) 0, otmp, u.ux, u.uy);
398 return; /* couldn't make statue */
399 mtmp = (struct monst *) 0;
400 } else if (u.ugrave_arise < LOW_PM) {
401 /* drop everything */
402 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
403 /* trick makemon() into allowing monster creation
407 mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME);
411 mtmp = christen_monst(mtmp, plname);
413 (void) obj_attach_mid(corpse, mtmp->m_id);
415 /* give your possessions to the monster you become */
416 in_mklev = TRUE; /* use <u.ux,u.uy> as-is */
417 mtmp = makemon(&mons[u.ugrave_arise], u.ux, u.uy, NO_MINVENT);
420 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
421 u.ugrave_arise = NON_PM; /* in case caller cares */
424 /* give mummy-from-hero a wrapping unless hero already
425 carries one; don't bother forcing it to become worn */
426 if (mtmp->data->mlet == S_MUMMY && !carrying(MUMMY_WRAPPING))
427 (void) mongets(mtmp, MUMMY_WRAPPING);
428 mtmp = christen_monst(mtmp, plname);
430 /* ["Your body rises from the dead as an <mname>..." used
431 to be given here, but it has been moved to done() so that
432 it gets delivered even when savebones() isn't called] */
433 drop_upon_death(mtmp, (struct obj *) 0, u.ux, u.uy);
434 m_dowear(mtmp, TRUE);
437 mtmp->m_lev = (u.ulevel ? u.ulevel : 1);
438 mtmp->mhp = mtmp->mhpmax = u.uhpmax;
439 mtmp->female = flags.female;
442 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
443 resetobjs(mtmp->minvent, FALSE);
444 /* do not zero out m_ids for bones levels any more */
447 mtmp->mtame = mtmp->mpeaceful = 0;
449 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
451 ttmp->tseen = (ttmp->ttyp == HOLE);
453 resetobjs(fobj, FALSE);
454 resetobjs(level.buriedobjlist, FALSE);
456 /* Hero is no longer on the map. */
459 /* Clear all memory from the level. */
460 for (x = 1; x < COLNO; x++)
461 for (y = 0; y < ROWNO; y++) {
462 levl[x][y].seenv = 0;
463 levl[x][y].waslit = 0;
464 levl[x][y].glyph = cmap_to_glyph(S_stone);
465 lastseentyp[x][y] = 0;
468 /* Attach bones info to the current level before saving. */
469 newbones = (struct cemetery *) alloc(sizeof *newbones);
470 /* entries are '\0' terminated but have fixed length allocations,
471 so pre-fill with spaces to initialize any excess room */
472 (void) memset((genericptr_t) newbones, ' ', sizeof *newbones);
473 /* format name+role,&c, death reason, and date+time;
474 gender and alignment reflect final values rather than what the
475 character started out as, same as topten and logfile entries */
476 Sprintf(newbones->who, "%s-%.3s-%.3s-%.3s-%.3s", plname, urole.filecode,
477 urace.filecode, genders[flags.female].filecode,
478 aligns[1 - u.ualign.type].filecode);
479 formatkiller(newbones->how, sizeof newbones->how, how);
480 Strcpy(newbones->when, yyyymmddhhmmss(when));
481 /* final resting place, used to decide when bones are discovered */
482 newbones->frpx = u.ux, newbones->frpy = u.uy;
483 newbones->bonesknown = FALSE;
484 /* if current character died on a bones level, the cemetery list
485 will have multiple entries, most recent (this dead hero) first */
486 newbones->next = level.bonesinfo;
487 level.bonesinfo = newbones;
488 /* flag these bones if they are being created in wizard mode;
489 they might already be flagged as such, even when we're playing
490 in normal mode, if this level came from a previous bones file */
492 level.flags.wizard_bones = 1;
494 fd = create_bonesfile(&u.uz, &bonesid, whynot);
498 /* bones file creation problems are silent to the player.
499 * Keep it that way, but place a clue into the paniclog.
501 paniclog("savebones", whynot);
504 c = (char) (strlen(bonesid) + 1);
506 #ifdef MFLOPPY /* check whether there is room */
507 if (iflags.checkspace) {
508 savelev(fd, ledger_no(&u.uz), COUNT_SAVE);
509 /* savelev() initializes bytes_counted to 0, so it must come
510 * first here even though it does not in the real save. the
511 * resulting extra bflush() at the end of savelev() may increase
512 * bytes_counted by a couple over what the real usage will be.
514 * note it is safe to call store_version() here only because
515 * bufon() is null for ZEROCOMP, which MFLOPPY uses -- otherwise
516 * this code would have to know the size of the version
517 * information itself.
520 store_savefileinfo(fd);
521 bwrite(fd, (genericptr_t) &c, sizeof c);
522 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
523 savefruitchn(fd, COUNT_SAVE);
525 if (bytes_counted > freediskspace(bones)) { /* not enough room */
527 pline("Insufficient space to create bones file.");
532 co_false(); /* make sure stuff before savelev() gets written */
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, WRITE_SAVE | FREE_SAVE);
541 update_mlstmv(); /* update monsters for eventual restoration */
542 savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE);
544 commit_bonesfile(&u.uz);
545 compress_bonesfile();
553 char c, *bonesid, oldbonesid[10];
555 if (discover) /* save bones files for real games */
560 /* wizard check added by GAN 02/05/87 */
561 if (rn2(3) /* only once in three times do we find bones */
564 if (no_bones_level(&u.uz))
566 fd = open_bonesfile(&u.uz, &bonesid);
570 if (validate(fd, bones) != 0) {
572 pline("Discarding unuseable bones; no need to panic...");
577 if (yn("Get bones?") == 'n') {
579 compress_bonesfile();
583 mread(fd, (genericptr_t) &c, sizeof c); /* length incl. '\0' */
584 mread(fd, (genericptr_t) oldbonesid, (unsigned) c); /* DD.nnn */
585 if (strcmp(bonesid, oldbonesid) != 0) {
588 Sprintf(errbuf, "This is bones level '%s', not '%s'!", oldbonesid,
592 ok = FALSE; /* won't die of trickery */
596 register struct monst *mtmp;
598 getlev(fd, 0, 0, TRUE);
600 /* Note that getlev() now keeps tabs on unique
601 * monsters such as demon lords, and tracks the
602 * birth counts of all species just as makemon()
603 * does. If a bones monster is extinct or has been
604 * subject to genocide, their mhpmax will be
605 * set to the magic DEFUNCT_MONSTER cookie value.
607 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
609 sanitize_name(MNAME(mtmp));
610 if (mtmp->mhpmax == DEFUNCT_MONSTER) {
612 debugpline1("Removing defunct monster %s from bones.",
617 /* to correctly reset named artifacts on the level */
618 resetobjs(mtmp->minvent, TRUE);
620 resetobjs(fobj, TRUE);
621 resetobjs(level.buriedobjlist, TRUE);
625 sanitize_engravings();
626 u.uroleplay.numbones++;
629 if (yn("Unlink bones?") == 'n') {
630 compress_bonesfile();
634 if (!delete_bonesfile(&u.uz)) {
635 /* When N games try to simultaneously restore the same
636 * bones file, N-1 of them will fail to delete it
637 * (the first N-1 under AmigaDOS, the last N-1 under UNIX).
638 * So no point in a mysterious message for a normal event
639 * -- just generate a new level for those N-1 games.
641 /* pline("Cannot unlink bones."); */