1 /* SCCS Id: @(#)bones.c 3.4 2003/09/06 */
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 void FDECL(drop_upon_death, (struct monst *, struct obj *));
22 extern d_level save_dlevel; /* in do.c */
25 if (ledger_no(&save_dlevel)) assign_level(lev, &save_dlevel);
27 return (boolean)(((sptr = Is_special(lev)) != 0 && !sptr->boneid)
28 || !dungeons[lev->dnum].boneid
29 /* no bones on the last or multiway branch levels */
30 /* in any dungeon (level 1 isn't multiway). */
31 || Is_botlevel(lev) || (Is_branchlev(lev) && lev->dlevel > 1)
32 /* no bones in the invocation level */
33 || (In_hell(lev) && lev->dlevel == dunlevs_in_dungeon(lev) - 1)
37 /* Call this function for each fruit object saved in the bones level: it marks
38 * that particular type of fruit as existing (the marker is that that type's
39 * ID is positive instead of negative). This way, when we later save the
40 * chain of fruit types, we know to only save the types that exist.
46 register struct fruit *f;
48 for(f=ffruit; f; f=f->nextf) {
57 resetobjs(ochain,restore)
63 for (otmp = ochain; otmp; otmp = otmp->nobj) {
65 resetobjs(otmp->cobj,restore);
67 if (((otmp->otyp != CORPSE || otmp->corpsenm < SPECIAL_PM)
68 && otmp->otyp != STATUE)
69 && (!otmp->oartifact ||
70 (restore && (exist_artifact(otmp->otyp, ONAME(otmp))
71 || is_quest_artifact(otmp))))) {
75 } else if (otmp->oartifact && restore)
76 artifact_exists(otmp,ONAME(otmp),TRUE);
78 /* do not zero out o_ids for ghost levels anymore */
80 if(objects[otmp->otyp].oc_uses_known) otmp->known = 0;
81 otmp->dknown = otmp->bknown = 0;
86 if (otmp->otyp == SLIME_MOLD) goodfruit(otmp->spe);
88 else if (otmp->otyp == SCR_MAIL) otmp->spe = 1;
90 else if (otmp->otyp == EGG) otmp->spe = 0;
91 else if (otmp->otyp == TIN) {
92 /* make tins of unique monster's meat be empty */
93 if (otmp->corpsenm >= LOW_PM &&
94 (mons[otmp->corpsenm].geno & G_UNIQ))
95 otmp->corpsenm = NON_PM;
96 } else if (otmp->otyp == AMULET_OF_YENDOR) {
97 /* no longer the real Amulet */
98 otmp->otyp = FAKE_AMULET_OF_YENDOR;
100 } else if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
102 end_burn(otmp, TRUE);
103 otmp->otyp = WAX_CANDLE;
104 otmp->age = 50L; /* assume used */
106 otmp->quan = (long)otmp->spe;
108 otmp->owt = weight(otmp);
110 } else if (otmp->otyp == BELL_OF_OPENING) {
113 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
114 otmp->otyp = SPE_BLANK_PAPER;
122 drop_upon_death(mtmp, cont)
128 uswapwep = 0; /* ensure curse() won't cause swapwep to drop twice */
129 while ((otmp = invent) != 0) {
130 obj_extract_self(otmp);
131 obj_no_longer_held(otmp);
134 /* lamps don't go out when dropped */
135 if ((cont || artifact_light(otmp)) && obj_is_burning(otmp))
136 end_burn(otmp, TRUE); /* smother in statue */
138 if(otmp->otyp == SLIME_MOLD) goodfruit(otmp->spe);
140 if(rn2(5)) curse(otmp);
142 (void) add_to_minv(mtmp, otmp);
144 (void) add_to_container(cont, otmp);
146 place_object(otmp, u.ux, u.uy);
150 long ugold = u.ugold;
151 if (mtmp) mtmp->mgold = ugold;
152 else if (cont) (void) add_to_container(cont, mkgoldobj(ugold));
153 else (void)mkgold(ugold, u.ux, u.uy);
154 u.ugold = ugold; /* undo mkgoldobj()'s removal */
157 if (cont) cont->owt = weight(cont);
160 /* check whether bones are feasible */
164 register struct trap *ttmp;
166 if (ledger_no(&u.uz) <= 0 || ledger_no(&u.uz) > maxledgerno())
168 if (no_bones_level(&u.uz))
169 return FALSE; /* no bones for specific levels */
171 return FALSE; /* no bones when swallowed */
173 if (!Is_branchlev(&u.uz)) {
174 /* no bones on non-branches with portals */
175 for(ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
176 if (ttmp->ttyp == MAGIC_PORTAL) return FALSE;
179 if(depth(&u.uz) <= 0 || /* bulletproofing for endgame */
180 (!rn2(1 + (depth(&u.uz)>>2)) /* fewer ghosts on low levels */
185 /* don't let multiple restarts generate multiple copies of objects
187 if (discover) return FALSE;
191 /* save bones and possessions of a deceased adventurer */
199 struct permonst *mptr;
204 /* caller has already checked `can_make_bones()' */
207 fd = open_bonesfile(&u.uz, &bonesid);
210 compress_bonesfile();
213 if (yn("Bones file already exists. Replace it?") == 'y') {
214 if (delete_bonesfile(&u.uz)) goto make_bones;
215 else pline("Cannot unlink old bones.");
226 /* in case these characters are not in their home bases */
227 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
228 if (DEADMONSTER(mtmp)) continue;
230 if (mtmp->iswiz || mptr == &mons[PM_MEDUSA] ||
231 mptr->msound == MS_NEMESIS || mptr->msound == MS_LEADER ||
232 mptr == &mons[PM_VLAD_THE_IMPALER])
236 if (u.usteed) dismount_steed(DISMOUNT_BONES);
238 dmonsfree(); /* discard dead or gone monsters */
240 /* mark all fruits as nonexistent; when we come to them we'll mark
241 * them as existing (using goodfruit())
243 for(f=ffruit; f; f=f->nextf) f->fid = -f->fid;
245 /* check iron balls separately--maybe they're not carrying it */
246 if (uball) uball->owornmask = uchain->owornmask = 0;
248 /* dispose of your possessions, usually cursed */
249 if (u.ugrave_arise == (NON_PM - 1)) {
252 /* embed your possessions in your statue */
253 otmp = mk_named_object(STATUE, &mons[u.umonnum],
256 drop_upon_death((struct monst *)0, otmp);
257 if (!otmp) return; /* couldn't make statue */
258 mtmp = (struct monst *)0;
259 } else if (u.ugrave_arise < LOW_PM) {
260 /* drop everything */
261 drop_upon_death((struct monst *)0, (struct obj *)0);
262 /* trick makemon() into allowing monster creation
266 mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME);
269 mtmp = christen_monst(mtmp, plname);
271 (void) obj_attach_mid(corpse, mtmp->m_id);
273 /* give your possessions to the monster you become */
275 mtmp = makemon(&mons[u.ugrave_arise], u.ux, u.uy, NO_MM_FLAGS);
278 drop_upon_death((struct monst *)0, (struct obj *)0);
281 mtmp = christen_monst(mtmp, plname);
283 Your("body rises from the dead as %s...",
284 an(mons[u.ugrave_arise].mname));
285 display_nhwindow(WIN_MESSAGE, FALSE);
286 drop_upon_death(mtmp, (struct obj *)0);
287 m_dowear(mtmp, TRUE);
290 mtmp->m_lev = (u.ulevel ? u.ulevel : 1);
291 mtmp->mhp = mtmp->mhpmax = u.uhpmax;
292 mtmp->female = flags.female;
295 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
296 resetobjs(mtmp->minvent,FALSE);
297 /* do not zero out m_ids for bones levels any more */
299 if(mtmp->mtame) mtmp->mtame = mtmp->mpeaceful = 0;
301 for(ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
303 ttmp->tseen = (ttmp->ttyp == HOLE);
305 resetobjs(fobj,FALSE);
306 resetobjs(level.buriedobjlist, FALSE);
308 /* Hero is no longer on the map. */
311 /* Clear all memory from the level. */
312 for(x=0; x<COLNO; x++) for(y=0; y<ROWNO; y++) {
313 levl[x][y].seenv = 0;
314 levl[x][y].waslit = 0;
315 levl[x][y].glyph = cmap_to_glyph(S_stone);
318 fd = create_bonesfile(&u.uz, &bonesid, whynot);
324 /* bones file creation problems are silent to the player.
325 * Keep it that way, but place a clue into the paniclog.
327 paniclog("savebones", whynot);
330 c = (char) (strlen(bonesid) + 1);
332 #ifdef MFLOPPY /* check whether there is room */
333 if (iflags.checkspace) {
334 savelev(fd, ledger_no(&u.uz), COUNT_SAVE);
335 /* savelev() initializes bytes_counted to 0, so it must come
336 * first here even though it does not in the real save. the
337 * resulting extra bflush() at the end of savelev() may increase
338 * bytes_counted by a couple over what the real usage will be.
340 * note it is safe to call store_version() here only because
341 * bufon() is null for ZEROCOMP, which MFLOPPY uses -- otherwise
342 * this code would have to know the size of the version
343 * information itself.
346 bwrite(fd, (genericptr_t) &c, sizeof c);
347 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
348 savefruitchn(fd, COUNT_SAVE);
350 if (bytes_counted > freediskspace(bones)) { /* not enough room */
353 pline("Insufficient space to create bones file.");
359 co_false(); /* make sure stuff before savelev() gets written */
364 bwrite(fd, (genericptr_t) &c, sizeof c);
365 bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
366 savefruitchn(fd, WRITE_SAVE | FREE_SAVE);
367 update_mlstmv(); /* update monsters for eventual restoration */
368 savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE);
370 commit_bonesfile(&u.uz);
371 compress_bonesfile();
379 char c, *bonesid, oldbonesid[10];
381 if(discover) /* save bones files for real games */
384 /* wizard check added by GAN 02/05/87 */
385 if(rn2(3) /* only once in three times do we find bones */
390 if(no_bones_level(&u.uz)) return(0);
391 fd = open_bonesfile(&u.uz, &bonesid);
392 if (fd < 0) return(0);
394 if ((ok = uptodate(fd, bones)) == 0) {
398 pline("Discarding unuseable bones; no need to panic...");
402 if(yn("Get bones?") == 'n') {
404 compress_bonesfile();
409 mread(fd, (genericptr_t) &c, sizeof c); /* length incl. '\0' */
410 mread(fd, (genericptr_t) oldbonesid, (unsigned) c); /* DD.nnn */
411 if (strcmp(bonesid, oldbonesid) != 0) {
414 Sprintf(errbuf, "This is bones level '%s', not '%s'!",
415 oldbonesid, bonesid);
419 ok = FALSE; /* won't die of trickery */
424 register struct monst *mtmp;
426 getlev(fd, 0, 0, TRUE);
428 /* Note that getlev() now keeps tabs on unique
429 * monsters such as demon lords, and tracks the
430 * birth counts of all species just as makemon()
431 * does. If a bones monster is extinct or has been
432 * subject to genocide, their mhpmax will be
433 * set to the magic DEFUNCT_MONSTER cookie value.
435 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
436 if (mtmp->mhpmax == DEFUNCT_MONSTER) {
437 #if defined(DEBUG) && defined(WIZARD)
439 pline("Removing defunct monster %s from bones.",
444 /* to correctly reset named artifacts on the level */
445 resetobjs(mtmp->minvent,TRUE);
447 resetobjs(fobj,TRUE);
448 resetobjs(level.buriedobjlist,TRUE);
455 if(yn("Unlink bones?") == 'n') {
456 compress_bonesfile();
461 if (!delete_bonesfile(&u.uz)) {
462 /* When N games try to simultaneously restore the same
463 * bones file, N-1 of them will fail to delete it
464 * (the first N-1 under AmigaDOS, the last N-1 under UNIX).
465 * So no point in a mysterious message for a normal event
466 * -- just generate a new level for those N-1 games.
468 /* pline("Cannot unlink bones."); */