1 /* NetHack 3.6 bones.c $NHDT-Date: 1450432756 2015/12/18 09:59:16 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.68 $ */
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
) {
125 /* 0: delivered in-game via external event;
126 1: from bones or wishing; 2: written with marker */
130 } else if (otmp
->otyp
== EGG
) {
132 } else if (otmp
->otyp
== TIN
) {
133 /* make tins of unique monster's meat be empty */
134 if (otmp
->corpsenm
>= LOW_PM
135 && unique_corpstat(&mons
[otmp
->corpsenm
]))
136 otmp
->corpsenm
= NON_PM
;
137 } else if (otmp
->otyp
== CORPSE
|| otmp
->otyp
== STATUE
) {
138 int mnum
= otmp
->corpsenm
;
140 /* Discard incarnation details of unique
141 monsters (by passing null instead of otmp
142 for object), shopkeepers (by passing false
143 for revival flag), temple priests, and
144 vault guards in order to prevent corpse
145 revival or statue reanimation. */
147 && cant_revive(&mnum
, FALSE
, (struct obj
*) 0)) {
149 /* mnum is now either human_zombie or
150 doppelganger; for corpses of uniques,
151 we need to force the transformation
152 now rather than wait until a revival
153 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
== AMULET_OF_YENDOR
) {
159 /* no longer the real Amulet */
160 otmp
->otyp
= FAKE_AMULET_OF_YENDOR
;
162 } else if (otmp
->otyp
== CANDELABRUM_OF_INVOCATION
) {
164 end_burn(otmp
, TRUE
);
165 otmp
->otyp
= WAX_CANDLE
;
166 otmp
->age
= 50L; /* assume used */
168 otmp
->quan
= (long) otmp
->spe
;
170 otmp
->owt
= weight(otmp
);
172 } else if (otmp
->otyp
== BELL_OF_OPENING
) {
175 } else if (otmp
->otyp
== SPE_BOOK_OF_THE_DEAD
) {
176 otmp
->otyp
= SPE_BLANK_PAPER
;
183 /* while loading bones, strip out text possibly supplied by old player
184 that might accidentally or maliciously disrupt new player's display */
186 sanitize_name(namebuf
)
190 boolean strip_8th_bit
=
191 !strcmp(windowprocs
.name
, "tty") && !iflags
.wc_eight_bit_input
;
193 /* it's tempting to skip this for single-user platforms, since
194 only the current player could have left these bones--except
195 things like "hearse" and other bones exchange schemes make
196 that assumption false */
199 if (c
< ' ' || c
== '\177') {
200 /* non-printable or undesirable */
202 } else if (c
!= *namebuf
) {
203 /* expected to be printable if user wants such things */
211 /* called by savebones(); also by finish_paybill(shk.c) */
213 drop_upon_death(mtmp
, cont
, x
, y
)
220 u
.twoweap
= 0; /* ensure curse() won't cause swapwep to drop twice */
221 while ((otmp
= invent
) != 0) {
222 obj_extract_self(otmp
);
223 obj_no_longer_held(otmp
);
226 /* lamps don't go out when dropped */
227 if ((cont
|| artifact_light(otmp
)) && obj_is_burning(otmp
))
228 end_burn(otmp
, TRUE
); /* smother in statue */
230 if (otmp
->otyp
== SLIME_MOLD
)
231 goodfruit(otmp
->spe
);
236 (void) add_to_minv(mtmp
, otmp
);
238 (void) add_to_container(cont
, otmp
);
240 place_object(otmp
, x
, y
);
243 cont
->owt
= weight(cont
);
246 /* possibly restore oracle's room and/or put her back inside it; returns
247 False if she's on the wrong level and should be removed, True otherwise */
250 struct monst
*oracle
;
255 /* oracle doesn't move, but knight's joust or monk's staggering blow
256 could push her onto a hole in the floor; at present, traps don't
257 activate in such situation hence she won't fall to another level;
258 however, that could change so be prepared to cope with such things */
259 if (!Is_oracle_level(&u
.uz
))
262 oracle
->mpeaceful
= 1;
263 o_ridx
= levl
[oracle
->mx
][oracle
->my
].roomno
- ROOMOFFSET
;
264 if (o_ridx
>= 0 && rooms
[o_ridx
].rtype
== DELPHI
)
265 return TRUE
; /* no fixup needed */
268 * The Oracle isn't in DELPHI room. Either hero entered her chamber
269 * and got the one-time welcome message, converting it into an
270 * ordinary room, or she got teleported out, or both. Try to put
271 * her back inside her room, if necessary, and restore its type.
274 /* find original delphi chamber; should always succeed */
275 for (ridx
= 0; ridx
< SIZE(rooms
); ++ridx
)
276 if (rooms
[ridx
].orig_rtype
== DELPHI
)
279 if (o_ridx
!= ridx
&& ridx
< SIZE(rooms
)) {
280 /* room found and she's not not in it, so try to move her there */
281 cc
.x
= (rooms
[ridx
].lx
+ rooms
[ridx
].hx
) / 2;
282 cc
.y
= (rooms
[ridx
].ly
+ rooms
[ridx
].hy
) / 2;
283 if (enexto(&cc
, cc
.x
, cc
.y
, oracle
->data
)) {
284 rloc_to(oracle
, cc
.x
, cc
.y
);
285 o_ridx
= levl
[oracle
->mx
][oracle
->my
].roomno
- ROOMOFFSET
;
287 /* [if her room is already full, she might end up outside;
288 that's ok, next hero just won't get any welcome message,
289 same as used to happen before this fixup was introduced] */
291 if (ridx
== o_ridx
) /* if she's in her room, mark it as such */
292 rooms
[ridx
].rtype
= DELPHI
;
293 return TRUE
; /* keep oracle in new bones file */
296 /* check whether bones are feasible */
300 register struct trap
*ttmp
;
304 if (ledger_no(&u
.uz
) <= 0 || ledger_no(&u
.uz
) > maxledgerno())
306 if (no_bones_level(&u
.uz
))
307 return FALSE
; /* no bones for specific levels */
309 return FALSE
; /* no bones when swallowed */
311 if (!Is_branchlev(&u
.uz
)) {
312 /* no bones on non-branches with portals */
313 for (ttmp
= ftrap
; ttmp
; ttmp
= ttmp
->ntrap
)
314 if (ttmp
->ttyp
== MAGIC_PORTAL
)
318 if (depth(&u
.uz
) <= 0 /* bulletproofing for endgame */
319 || (!rn2(1 + (depth(&u
.uz
) >> 2)) /* fewer ghosts on low levels */
322 /* don't let multiple restarts generate multiple copies of objects
329 /* save bones and possessions of a deceased adventurer */
331 savebones(how
, when
, corpse
)
339 struct permonst
*mptr
;
341 struct cemetery
*newbones
;
345 /* caller has already checked `can_make_bones()' */
348 fd
= open_bonesfile(&u
.uz
, &bonesid
);
352 if (yn("Bones file already exists. Replace it?") == 'y') {
353 if (delete_bonesfile(&u
.uz
))
356 pline("Cannot unlink old bones.");
359 /* compression can change the file's name, so must
360 wait until after any attempt to delete this file */
361 compress_bonesfile();
367 /* in case these characters are not in their home bases */
368 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
) {
369 if (DEADMONSTER(mtmp
))
372 if (mtmp
->iswiz
|| mptr
== &mons
[PM_MEDUSA
]
373 || mptr
->msound
== MS_NEMESIS
|| mptr
->msound
== MS_LEADER
374 || mptr
== &mons
[PM_VLAD_THE_IMPALER
]
375 || (mptr
== &mons
[PM_ORACLE
] && !fixuporacle(mtmp
)))
379 dismount_steed(DISMOUNT_BONES
);
380 dmonsfree(); /* discard dead or gone monsters */
382 /* mark all fruits as nonexistent; when we come to them we'll mark
383 * them as existing (using goodfruit())
385 for (f
= ffruit
; f
; f
= f
->nextf
)
388 /* check iron balls separately--maybe they're not carrying it */
390 uball
->owornmask
= uchain
->owornmask
= 0;
392 /* dispose of your possessions, usually cursed */
393 if (u
.ugrave_arise
== (NON_PM
- 1)) {
396 /* embed your possessions in your statue */
397 otmp
= mk_named_object(STATUE
, &mons
[u
.umonnum
], u
.ux
, u
.uy
, plname
);
399 drop_upon_death((struct monst
*) 0, otmp
, u
.ux
, u
.uy
);
401 return; /* couldn't make statue */
402 mtmp
= (struct monst
*) 0;
403 } else if (u
.ugrave_arise
< LOW_PM
) {
404 /* drop everything */
405 drop_upon_death((struct monst
*) 0, (struct obj
*) 0, u
.ux
, u
.uy
);
406 /* trick makemon() into allowing monster creation
410 mtmp
= makemon(&mons
[PM_GHOST
], u
.ux
, u
.uy
, MM_NONAME
);
414 mtmp
= christen_monst(mtmp
, plname
);
416 (void) obj_attach_mid(corpse
, mtmp
->m_id
);
418 /* give your possessions to the monster you become */
419 in_mklev
= TRUE
; /* use <u.ux,u.uy> as-is */
420 mtmp
= makemon(&mons
[u
.ugrave_arise
], u
.ux
, u
.uy
, NO_MINVENT
);
423 drop_upon_death((struct monst
*) 0, (struct obj
*) 0, u
.ux
, u
.uy
);
424 u
.ugrave_arise
= NON_PM
; /* in case caller cares */
427 /* give mummy-from-hero a wrapping unless hero already
428 carries one; don't bother forcing it to become worn */
429 if (mtmp
->data
->mlet
== S_MUMMY
&& !carrying(MUMMY_WRAPPING
))
430 (void) mongets(mtmp
, MUMMY_WRAPPING
);
431 mtmp
= christen_monst(mtmp
, plname
);
433 /* ["Your body rises from the dead as an <mname>..." used
434 to be given here, but it has been moved to done() so that
435 it gets delivered even when savebones() isn't called] */
436 drop_upon_death(mtmp
, (struct obj
*) 0, u
.ux
, u
.uy
);
437 m_dowear(mtmp
, TRUE
);
440 mtmp
->m_lev
= (u
.ulevel
? u
.ulevel
: 1);
441 mtmp
->mhp
= mtmp
->mhpmax
= u
.uhpmax
;
442 mtmp
->female
= flags
.female
;
445 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
) {
446 resetobjs(mtmp
->minvent
, FALSE
);
447 /* do not zero out m_ids for bones levels any more */
450 mtmp
->mtame
= mtmp
->mpeaceful
= 0;
452 for (ttmp
= ftrap
; ttmp
; ttmp
= ttmp
->ntrap
) {
454 ttmp
->tseen
= (ttmp
->ttyp
== HOLE
);
456 resetobjs(fobj
, FALSE
);
457 resetobjs(level
.buriedobjlist
, FALSE
);
459 /* Hero is no longer on the map. */
462 /* Clear all memory from the level. */
463 for (x
= 1; x
< COLNO
; x
++)
464 for (y
= 0; y
< ROWNO
; y
++) {
465 levl
[x
][y
].seenv
= 0;
466 levl
[x
][y
].waslit
= 0;
467 levl
[x
][y
].glyph
= cmap_to_glyph(S_stone
);
468 lastseentyp
[x
][y
] = 0;
471 /* Attach bones info to the current level before saving. */
472 newbones
= (struct cemetery
*) alloc(sizeof *newbones
);
473 /* entries are '\0' terminated but have fixed length allocations,
474 so pre-fill with spaces to initialize any excess room */
475 (void) memset((genericptr_t
) newbones
, ' ', sizeof *newbones
);
476 /* format name+role,&c, death reason, and date+time;
477 gender and alignment reflect final values rather than what the
478 character started out as, same as topten and logfile entries */
479 Sprintf(newbones
->who
, "%s-%.3s-%.3s-%.3s-%.3s", plname
, urole
.filecode
,
480 urace
.filecode
, genders
[flags
.female
].filecode
,
481 aligns
[1 - u
.ualign
.type
].filecode
);
482 formatkiller(newbones
->how
, sizeof newbones
->how
, how
, TRUE
);
483 Strcpy(newbones
->when
, yyyymmddhhmmss(when
));
484 /* final resting place, used to decide when bones are discovered */
485 newbones
->frpx
= u
.ux
, newbones
->frpy
= u
.uy
;
486 newbones
->bonesknown
= FALSE
;
487 /* if current character died on a bones level, the cemetery list
488 will have multiple entries, most recent (this dead hero) first */
489 newbones
->next
= level
.bonesinfo
;
490 level
.bonesinfo
= newbones
;
491 /* flag these bones if they are being created in wizard mode;
492 they might already be flagged as such, even when we're playing
493 in normal mode, if this level came from a previous bones file */
495 level
.flags
.wizard_bones
= 1;
497 fd
= create_bonesfile(&u
.uz
, &bonesid
, whynot
);
501 /* bones file creation problems are silent to the player.
502 * Keep it that way, but place a clue into the paniclog.
504 paniclog("savebones", whynot
);
507 c
= (char) (strlen(bonesid
) + 1);
509 #ifdef MFLOPPY /* check whether there is room */
510 if (iflags
.checkspace
) {
511 savelev(fd
, ledger_no(&u
.uz
), COUNT_SAVE
);
512 /* savelev() initializes bytes_counted to 0, so it must come
513 * first here even though it does not in the real save. the
514 * resulting extra bflush() at the end of savelev() may increase
515 * bytes_counted by a couple over what the real usage will be.
517 * note it is safe to call store_version() here only because
518 * bufon() is null for ZEROCOMP, which MFLOPPY uses -- otherwise
519 * this code would have to know the size of the version
520 * information itself.
523 store_savefileinfo(fd
);
524 bwrite(fd
, (genericptr_t
) &c
, sizeof c
);
525 bwrite(fd
, (genericptr_t
) bonesid
, (unsigned) c
); /* DD.nnn */
526 savefruitchn(fd
, COUNT_SAVE
);
528 if (bytes_counted
> freediskspace(bones
)) { /* not enough room */
530 pline("Insufficient space to create bones file.");
535 co_false(); /* make sure stuff before savelev() gets written */
540 store_savefileinfo(fd
);
541 bwrite(fd
, (genericptr_t
) &c
, sizeof c
);
542 bwrite(fd
, (genericptr_t
) bonesid
, (unsigned) c
); /* DD.nnn */
543 savefruitchn(fd
, WRITE_SAVE
| FREE_SAVE
);
544 update_mlstmv(); /* update monsters for eventual restoration */
545 savelev(fd
, ledger_no(&u
.uz
), WRITE_SAVE
| FREE_SAVE
);
547 commit_bonesfile(&u
.uz
);
548 compress_bonesfile();
556 char c
, *bonesid
, oldbonesid
[10];
558 if (discover
) /* save bones files for real games */
563 /* wizard check added by GAN 02/05/87 */
564 if (rn2(3) /* only once in three times do we find bones */
567 if (no_bones_level(&u
.uz
))
569 fd
= open_bonesfile(&u
.uz
, &bonesid
);
573 if (validate(fd
, bones
) != 0) {
575 pline("Discarding unuseable bones; no need to panic...");
580 if (yn("Get bones?") == 'n') {
582 compress_bonesfile();
586 mread(fd
, (genericptr_t
) &c
, sizeof c
); /* length incl. '\0' */
587 mread(fd
, (genericptr_t
) oldbonesid
, (unsigned) c
); /* DD.nnn */
588 if (strcmp(bonesid
, oldbonesid
) != 0) {
591 Sprintf(errbuf
, "This is bones level '%s', not '%s'!", oldbonesid
,
595 ok
= FALSE
; /* won't die of trickery */
599 register struct monst
*mtmp
;
601 getlev(fd
, 0, 0, TRUE
);
603 /* Note that getlev() now keeps tabs on unique
604 * monsters such as demon lords, and tracks the
605 * birth counts of all species just as makemon()
606 * does. If a bones monster is extinct or has been
607 * subject to genocide, their mhpmax will be
608 * set to the magic DEFUNCT_MONSTER cookie value.
610 for (mtmp
= fmon
; mtmp
; mtmp
= mtmp
->nmon
) {
612 sanitize_name(MNAME(mtmp
));
613 if (mtmp
->mhpmax
== DEFUNCT_MONSTER
) {
615 debugpline1("Removing defunct monster %s from bones.",
620 /* to correctly reset named artifacts on the level */
621 resetobjs(mtmp
->minvent
, TRUE
);
623 resetobjs(fobj
, TRUE
);
624 resetobjs(level
.buriedobjlist
, TRUE
);
628 sanitize_engravings();
629 u
.uroleplay
.numbones
++;
632 if (yn("Unlink bones?") == 'n') {
633 compress_bonesfile();
637 if (!delete_bonesfile(&u
.uz
)) {
638 /* When N games try to simultaneously restore the same
639 * bones file, N-1 of them will fail to delete it
640 * (the first N-1 under AmigaDOS, the last N-1 under UNIX).
641 * So no point in a mysterious message for a normal event
642 * -- just generate a new level for those N-1 games.
644 /* pline("Cannot unlink bones."); */