NetHack->aNetHack
[aNetHack.git] / src / bones.c
blob6784259c65e74019f67cc52a15028440891cef27
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. */
5 #include "hack.h"
6 #include "lev.h"
8 extern char bones[]; /* from files.c */
9 #ifdef MFLOPPY
10 extern long bytes_counted;
11 #endif
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 *));
18 STATIC_OVL boolean
19 no_bones_level(lev)
20 d_level *lev;
22 extern d_level save_dlevel; /* in do.c */
23 s_level *sptr;
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) */
32 || Is_botlevel(lev)
33 || (Is_branchlev(lev) && lev->dlevel > 1)
34 /* no bones in the invocation level */
35 || (In_hell(lev)
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.
44 STATIC_OVL void
45 goodfruit(id)
46 int id;
48 register struct fruit *f;
50 for (f = ffruit; f; f = f->nextf) {
51 if (f->fid == -id) {
52 f->fid = id;
53 return;
58 STATIC_OVL void
59 resetobjs(ochain, restore)
60 struct obj *ochain;
61 boolean restore;
63 struct obj *otmp, *nobj;
65 for (otmp = ochain; otmp; otmp = nobj) {
66 nobj = otmp->nobj;
67 if (otmp->cobj)
68 resetobjs(otmp->cobj, restore);
69 if (otmp->in_use) {
70 obj_extract_self(otmp);
71 dealloc_obj(otmp);
72 continue;
75 if (restore) {
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 */
82 otmp->oartifact = 0;
83 if (has_oname(otmp))
84 free_oname(otmp);
85 } else {
86 artifact_exists(otmp, safe_oname(otmp), TRUE);
88 } else if (has_oname(otmp)) {
89 sanitize_name(ONAME(otmp));
91 } else { /* saving */
92 /* do not zero out o_ids for ghost levels anymore */
94 if (objects[otmp->otyp].oc_uses_known)
95 otmp->known = 0;
96 otmp->dknown = otmp->bknown = 0;
97 otmp->rknown = 0;
98 otmp->lknown = 0;
99 otmp->cknown = 0;
100 otmp->invlet = 0;
101 otmp->no_charge = 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.] */
113 if (has_oname(otmp)
114 && !(otmp->oartifact || otmp->otyp == STATUE
115 || otmp->otyp == SPE_NOVEL
116 || (otmp->otyp == CORPSE
117 && otmp->corpsenm >= SPECIAL_PM))) {
118 free_oname(otmp);
121 if (otmp->otyp == SLIME_MOLD) {
122 goodfruit(otmp->spe);
123 #ifdef MAIL
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 */
127 if (otmp->spe == 0)
128 otmp->spe = 1;
129 #endif
130 } else if (otmp->otyp == EGG) {
131 otmp->spe = 0;
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. */
146 if (has_omonst(otmp)
147 && cant_revive(&mnum, FALSE, (struct obj *) 0)) {
148 free_omonst(otmp);
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;
161 curse(otmp);
162 } else if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
163 if (otmp->lamplit)
164 end_burn(otmp, TRUE);
165 otmp->otyp = WAX_CANDLE;
166 otmp->age = 50L; /* assume used */
167 if (otmp->spe > 0)
168 otmp->quan = (long) otmp->spe;
169 otmp->spe = 0;
170 otmp->owt = weight(otmp);
171 curse(otmp);
172 } else if (otmp->otyp == BELL_OF_OPENING) {
173 otmp->otyp = BELL;
174 curse(otmp);
175 } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
176 otmp->otyp = SPE_BLANK_PAPER;
177 curse(otmp);
183 /* while loading bones, strip out text possibly supplied by old player
184 that might accidentally or maliciously disrupt new player's display */
185 void
186 sanitize_name(namebuf)
187 char *namebuf;
189 int c;
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 */
197 while (*namebuf) {
198 c = *namebuf & 0177;
199 if (c < ' ' || c == '\177') {
200 /* non-printable or undesirable */
201 *namebuf = '.';
202 } else if (c != *namebuf) {
203 /* expected to be printable if user wants such things */
204 if (strip_8th_bit)
205 *namebuf = '_';
207 ++namebuf;
211 /* called by savebones(); also by finish_paybill(shk.c) */
212 void
213 drop_upon_death(mtmp, cont, x, y)
214 struct monst *mtmp;
215 struct obj *cont;
216 int x, y;
218 struct obj *otmp;
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);
225 otmp->owornmask = 0;
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);
233 if (rn2(5))
234 curse(otmp);
235 if (mtmp)
236 (void) add_to_minv(mtmp, otmp);
237 else if (cont)
238 (void) add_to_container(cont, otmp);
239 else
240 place_object(otmp, x, y);
242 if (cont)
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 */
248 STATIC_OVL boolean
249 fixuporacle(oracle)
250 struct monst *oracle;
252 coord cc;
253 int ridx, o_ridx;
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))
260 return FALSE;
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)
277 break;
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 */
297 boolean
298 can_make_bones()
300 register struct trap *ttmp;
302 if (!flags.bones)
303 return FALSE;
304 if (ledger_no(&u.uz) <= 0 || ledger_no(&u.uz) > maxledgerno())
305 return FALSE;
306 if (no_bones_level(&u.uz))
307 return FALSE; /* no bones for specific levels */
308 if (u.uswallow) {
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)
315 return FALSE;
318 if (depth(&u.uz) <= 0 /* bulletproofing for endgame */
319 || (!rn2(1 + (depth(&u.uz) >> 2)) /* fewer ghosts on low levels */
320 && !wizard))
321 return FALSE;
322 /* don't let multiple restarts generate multiple copies of objects
323 in bones files */
324 if (discover)
325 return FALSE;
326 return TRUE;
329 /* save bones and possessions of a deceased adventurer */
330 void
331 savebones(how, when, corpse)
332 int how;
333 time_t when;
334 struct obj *corpse;
336 int fd, x, y;
337 struct trap *ttmp;
338 struct monst *mtmp;
339 struct permonst *mptr;
340 struct fruit *f;
341 struct cemetery *newbones;
342 char c, *bonesid;
343 char whynot[BUFSZ];
345 /* caller has already checked `can_make_bones()' */
347 clear_bypasses();
348 fd = open_bonesfile(&u.uz, &bonesid);
349 if (fd >= 0) {
350 (void) nhclose(fd);
351 if (wizard) {
352 if (yn("Bones file already exists. Replace it?") == 'y') {
353 if (delete_bonesfile(&u.uz))
354 goto make_bones;
355 else
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();
362 return;
365 make_bones:
366 unleash_all();
367 /* in case these characters are not in their home bases */
368 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
369 if (DEADMONSTER(mtmp))
370 continue;
371 mptr = mtmp->data;
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)))
376 mongone(mtmp);
378 if (u.usteed)
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)
386 f->fid = -f->fid;
388 /* check iron balls separately--maybe they're not carrying it */
389 if (uball)
390 uball->owornmask = uchain->owornmask = 0;
392 /* dispose of your possessions, usually cursed */
393 if (u.ugrave_arise == (NON_PM - 1)) {
394 struct obj *otmp;
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);
400 if (!otmp)
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
407 * on your location
409 in_mklev = TRUE;
410 mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME);
411 in_mklev = FALSE;
412 if (!mtmp)
413 return;
414 mtmp = christen_monst(mtmp, plname);
415 if (corpse)
416 (void) obj_attach_mid(corpse, mtmp->m_id);
417 } else {
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);
421 in_mklev = FALSE;
422 if (!mtmp) {
423 drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
424 u.ugrave_arise = NON_PM; /* in case caller cares */
425 return;
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);
432 newsym(u.ux, u.uy);
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);
439 if (mtmp) {
440 mtmp->m_lev = (u.ulevel ? u.ulevel : 1);
441 mtmp->mhp = mtmp->mhpmax = u.uhpmax;
442 mtmp->female = flags.female;
443 mtmp->msleeping = 1;
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 */
448 mtmp->mlstmv = 0L;
449 if (mtmp->mtame)
450 mtmp->mtame = mtmp->mpeaceful = 0;
452 for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
453 ttmp->madeby_u = 0;
454 ttmp->tseen = (ttmp->ttyp == HOLE);
456 resetobjs(fobj, FALSE);
457 resetobjs(level.buriedobjlist, FALSE);
459 /* Hero is no longer on the map. */
460 u.ux = u.uy = 0;
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 */
494 if (wizard)
495 level.flags.wizard_bones = 1;
497 fd = create_bonesfile(&u.uz, &bonesid, whynot);
498 if (fd < 0) {
499 if (wizard)
500 pline1(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);
505 return;
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.
522 store_version(fd);
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);
527 bflush(fd);
528 if (bytes_counted > freediskspace(bones)) { /* not enough room */
529 if (wizard)
530 pline("Insufficient space to create bones file.");
531 (void) nhclose(fd);
532 cancel_bonesfile();
533 return;
535 co_false(); /* make sure stuff before savelev() gets written */
537 #endif /* MFLOPPY */
539 store_version(fd);
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);
546 bclose(fd);
547 commit_bonesfile(&u.uz);
548 compress_bonesfile();
552 getbones()
554 register int fd;
555 register int ok;
556 char c, *bonesid, oldbonesid[10];
558 if (discover) /* save bones files for real games */
559 return 0;
561 if (!flags.bones)
562 return 0;
563 /* wizard check added by GAN 02/05/87 */
564 if (rn2(3) /* only once in three times do we find bones */
565 && !wizard)
566 return 0;
567 if (no_bones_level(&u.uz))
568 return 0;
569 fd = open_bonesfile(&u.uz, &bonesid);
570 if (fd < 0)
571 return 0;
573 if (validate(fd, bones) != 0) {
574 if (!wizard)
575 pline("Discarding unuseable bones; no need to panic...");
576 ok = FALSE;
577 } else {
578 ok = TRUE;
579 if (wizard) {
580 if (yn("Get bones?") == 'n') {
581 (void) nhclose(fd);
582 compress_bonesfile();
583 return 0;
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) {
589 char errbuf[BUFSZ];
591 Sprintf(errbuf, "This is bones level '%s', not '%s'!", oldbonesid,
592 bonesid);
593 if (wizard) {
594 pline1(errbuf);
595 ok = FALSE; /* won't die of trickery */
597 trickery(errbuf);
598 } else {
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) {
611 if (has_mname(mtmp))
612 sanitize_name(MNAME(mtmp));
613 if (mtmp->mhpmax == DEFUNCT_MONSTER) {
614 if (wizard) {
615 debugpline1("Removing defunct monster %s from bones.",
616 mtmp->data->mname);
618 mongone(mtmp);
619 } else
620 /* to correctly reset named artifacts on the level */
621 resetobjs(mtmp->minvent, TRUE);
623 resetobjs(fobj, TRUE);
624 resetobjs(level.buriedobjlist, TRUE);
627 (void) nhclose(fd);
628 sanitize_engravings();
629 u.uroleplay.numbones++;
631 if (wizard) {
632 if (yn("Unlink bones?") == 'n') {
633 compress_bonesfile();
634 return ok;
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."); */
645 return 0;
647 return ok;
650 /*bones.c*/