Source:NetHack 3.6.7/src/bones.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to bones.c from the source code of NetHack 3.6.7. To link to a particular line, write [[Source:NetHack 3.6.7/src/bones.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 3.6	bones.c	$NHDT-Date: 1571363147 2019/10/18 01:45:47 $  $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.76 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985,1993. */
3. /*-Copyright (c) Robert Patrick Rankin, 2012. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. #include "lev.h"
8. 
9. extern char bones[]; /* from files.c */
10. #ifdef MFLOPPY
11. extern long bytes_counted;
12. #endif
13. 
14. STATIC_DCL boolean FDECL(no_bones_level, (d_level *));
15. STATIC_DCL void FDECL(goodfruit, (int));
16. STATIC_DCL void FDECL(resetobjs, (struct obj *, BOOLEAN_P));
17. STATIC_DCL boolean FDECL(fixuporacle, (struct monst *));
18. 
19. STATIC_OVL boolean
20. no_bones_level(lev)
21. d_level *lev;
22. {
23.     extern d_level save_dlevel; /* in do.c */
24.     s_level *sptr;
25. 
26.     if (ledger_no(&save_dlevel))
27.         assign_level(lev, &save_dlevel);
28. 
29.     return (boolean) (((sptr = Is_special(lev)) != 0 && !sptr->boneid)
30.                       || !dungeons[lev->dnum].boneid
31.                       /* no bones on the last or multiway branch levels
32.                          in any dungeon (level 1 isn't multiway) */
33.                       || Is_botlevel(lev)
34.                       || (Is_branchlev(lev) && lev->dlevel > 1)
35.                       /* no bones in the invocation level */
36.                       || (In_hell(lev)
37.                           && lev->dlevel == dunlevs_in_dungeon(lev) - 1));
38. }
39. 
40. /* Call this function for each fruit object saved in the bones level: it marks
41.  * that particular type of fruit as existing (the marker is that that type's
42.  * ID is positive instead of negative).  This way, when we later save the
43.  * chain of fruit types, we know to only save the types that exist.
44.  */
45. STATIC_OVL void
46. goodfruit(id)
47. int id;
48. {
49.     struct fruit *f = fruit_from_indx(-id);
50. 
51.     if (f)
52.         f->fid = id;
53. }
54. 
55. STATIC_OVL void
56. resetobjs(ochain, restore)
57. struct obj *ochain;
58. boolean restore;
59. {
60.     struct obj *otmp, *nobj;
61. 
62.     for (otmp = ochain; otmp; otmp = nobj) {
63.         nobj = otmp->nobj;
64.         if (otmp->cobj)
65.             resetobjs(otmp->cobj, restore);
66.         if (otmp->in_use) {
67.             obj_extract_self(otmp);
68.             dealloc_obj(otmp);
69.             continue;
70.         }
71. 
72.         if (restore) {
73.             /* artifact bookkeeping needs to be done during
74.                restore; other fixups are done while saving */
75.             if (otmp->oartifact) {
76.                 if (exist_artifact(otmp->otyp, safe_oname(otmp))
77.                     || is_quest_artifact(otmp)) {
78.                     /* prevent duplicate--revert to ordinary obj */
79.                     otmp->oartifact = 0;
80.                     if (has_oname(otmp))
81.                         free_oname(otmp);
82.                 } else {
83.                     artifact_exists(otmp, safe_oname(otmp), TRUE);
84.                 }
85.             } else if (has_oname(otmp)) {
86.                 sanitize_name(ONAME(otmp));
87.             }
88.             /* 3.6.3: set no_charge for partly eaten food in shop;
89.                all other items become goods for sale if in a shop */
90.             if (otmp->oclass == FOOD_CLASS && otmp->oeaten) {
91.                 struct obj *top;
92.                 char *p;
93.                 xchar ox, oy;
94. 
95.                 for (top = otmp; top->where == OBJ_CONTAINED;
96.                      top = top->ocontainer)
97.                     continue;
98.                 otmp->no_charge = (top->where == OBJ_FLOOR
99.                                    && get_obj_location(top, &ox, &oy, 0)
100.                                    /* can't use costly_spot() since its
101.                                       result depends upon hero's location */
102.                                    && inside_shop(ox, oy)
103.                                    && *(p = in_rooms(ox, oy, SHOPBASE))
104.                                    && tended_shop(&rooms[*p - ROOMOFFSET]));
105.             }
106.         } else { /* saving */
107.             /* do not zero out o_ids for ghost levels anymore */
108. 
109.             if (objects[otmp->otyp].oc_uses_known)
110.                 otmp->known = 0;
111.             otmp->dknown = otmp->bknown = 0;
112.             otmp->rknown = 0;
113.             otmp->lknown = 0;
114.             otmp->cknown = 0;
115.             otmp->invlet = 0;
116.             otmp->no_charge = 0;
117.             otmp->was_thrown = 0;
118. 
119.             /* strip user-supplied names */
120.             /* Statue and some corpse names are left intact,
121.                presumably in case they came from score file.
122.                [TODO: this ought to be done differently--names
123.                which came from such a source or came from any
124.                stoned or killed monster should be flagged in
125.                some manner; then we could just check the flag
126.                here and keep "real" names (dead pets, &c) while
127.                discarding player notes attached to statues.] */
128.             if (has_oname(otmp)
129.                 && !(otmp->oartifact || otmp->otyp == STATUE
130.                      || otmp->otyp == SPE_NOVEL
131.                      || (otmp->otyp == CORPSE
132.                          && otmp->corpsenm >= SPECIAL_PM))) {
133.                 free_oname(otmp);
134.             }
135. 
136.             if (otmp->otyp == SLIME_MOLD) {
137.                 goodfruit(otmp->spe);
138. #ifdef MAIL
139.             } else if (otmp->otyp == SCR_MAIL) {
140.                 /* 0: delivered in-game via external event;
141.                    1: from bones or wishing; 2: written with marker */
142.                 if (otmp->spe == 0)
143.                     otmp->spe = 1;
144. #endif
145.             } else if (otmp->otyp == EGG) {
146.                 otmp->spe = 0; /* not "laid by you" in next game */
147.             } else if (otmp->otyp == TIN) {
148.                 /* make tins of unique monster's meat be empty */
149.                 if (otmp->corpsenm >= LOW_PM
150.                     && unique_corpstat(&mons[otmp->corpsenm]))
151.                     otmp->corpsenm = NON_PM;
152.             } else if (otmp->otyp == CORPSE || otmp->otyp == STATUE) {
153.                 int mnum = otmp->corpsenm;
154. 
155.                 /* Discard incarnation details of unique monsters
156.                    (by passing null instead of otmp for object),
157.                    shopkeepers (by passing false for revival flag),
158.                    temple priests, and vault guards in order to
159.                    prevent corpse revival or statue reanimation. */
160.                 if (has_omonst(otmp)
161.                     && cant_revive(&mnum, FALSE, (struct obj *) 0)) {
162.                     free_omonst(otmp);
163.                     /* mnum is now either human_zombie or doppelganger;
164.                        for corpses of uniques, we need to force the
165.                        transformation now rather than wait until a
166.                        revival attempt, otherwise eating this corpse
167.                        would behave as if it remains unique */
168.                     if (mnum == PM_DOPPELGANGER && otmp->otyp == CORPSE)
169.                         set_corpsenm(otmp, mnum);
170.                 }
171.             } else if ((otmp->otyp == iflags.mines_prize_type
172.                         && !Is_mineend_level(&u.uz))
173.                        || ((otmp->otyp == iflags.soko_prize_type1
174.                             || otmp->otyp == iflags.soko_prize_type2)
175.                            && !Is_sokoend_level(&u.uz))) {
176.                 /* "special prize" in this game becomes ordinary object
177.                    if loaded into another game */
178.                 otmp->record_achieve_special = NON_PM;
179.             } else if (otmp->otyp == AMULET_OF_YENDOR) {
180.                 /* no longer the real Amulet */
181.                 otmp->otyp = FAKE_AMULET_OF_YENDOR;
182.                 curse(otmp);
183.             } else if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
184.                 if (otmp->lamplit)
185.                     end_burn(otmp, TRUE);
186.                 otmp->otyp = WAX_CANDLE;
187.                 otmp->age = 50L; /* assume used */
188.                 if (otmp->spe > 0)
189.                     otmp->quan = (long) otmp->spe;
190.                 otmp->spe = 0;
191.                 otmp->owt = weight(otmp);
192.                 curse(otmp);
193.             } else if (otmp->otyp == BELL_OF_OPENING) {
194.                 otmp->otyp = BELL;
195.                 curse(otmp);
196.             } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
197.                 otmp->otyp = SPE_BLANK_PAPER;
198.                 curse(otmp);
199.             }
200.         }
201.     }
202. }
203. 
204. /* while loading bones, strip out text possibly supplied by old player
205.    that might accidentally or maliciously disrupt new player's display */
206. void
207. sanitize_name(namebuf)
208. char *namebuf;
209. {
210.     int c;
211.     boolean strip_8th_bit = (WINDOWPORT("tty")
212.                              && !iflags.wc_eight_bit_input);
213. 
214.     /* it's tempting to skip this for single-user platforms, since
215.        only the current player could have left these bones--except
216.        things like "hearse" and other bones exchange schemes make
217.        that assumption false */
218.     while (*namebuf) {
219.         c = *namebuf & 0177;
220.         if (c < ' ' || c == '\177') {
221.             /* non-printable or undesirable */
222.             *namebuf = '.';
223.         } else if (c != *namebuf) {
224.             /* expected to be printable if user wants such things */
225.             if (strip_8th_bit)
226.                 *namebuf = '_';
227.         }
228.         ++namebuf;
229.     }
230. }
231. 
232. /* called by savebones(); also by finish_paybill(shk.c) */
233. void
234. drop_upon_death(mtmp, cont, x, y)
235. struct monst *mtmp; /* monster if hero turned into one (other than ghost) */
236. struct obj *cont; /* container if hero is turned into a statue */
237. int x, y;
238. {
239.     struct obj *otmp;
240. 
241.     u.twoweap = 0; /* ensure curse() won't cause swapwep to drop twice */
242.     while ((otmp = invent) != 0) {
243.         obj_extract_self(otmp);
244.         /* when turning into green slime, all gear remains held;
245.            other types "arise from the dead" do aren't holding
246.            equipment during their brief interval as a corpse */
247.         if (!mtmp || is_undead(mtmp->data))
248.             obj_no_longer_held(otmp);
249. 
250.         otmp->owornmask = 0L;
251.         /* lamps don't go out when dropped */
252.         if ((cont || artifact_light(otmp)) && obj_is_burning(otmp))
253.             end_burn(otmp, TRUE); /* smother in statue */
254. 
255.         if (otmp->otyp == SLIME_MOLD)
256.             goodfruit(otmp->spe);
257. 
258.         if (rn2(5))
259.             curse(otmp);
260.         if (mtmp)
261.             (void) add_to_minv(mtmp, otmp);
262.         else if (cont)
263.             (void) add_to_container(cont, otmp);
264.         else
265.             place_object(otmp, x, y);
266.     }
267.     if (cont)
268.         cont->owt = weight(cont);
269. }
270. 
271. /* possibly restore oracle's room and/or put her back inside it; returns
272.    False if she's on the wrong level and should be removed, True otherwise */
273. STATIC_OVL boolean
274. fixuporacle(oracle)
275. struct monst *oracle;
276. {
277.     coord cc;
278.     int ridx, o_ridx;
279. 
280.     /* oracle doesn't move, but knight's joust or monk's staggering blow
281.        could push her onto a hole in the floor; at present, traps don't
282.        activate in such situation hence she won't fall to another level;
283.        however, that could change so be prepared to cope with such things */
284.     if (!Is_oracle_level(&u.uz))
285.         return FALSE;
286. 
287.     oracle->mpeaceful = 1;
288.     o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
289.     if (o_ridx >= 0 && rooms[o_ridx].rtype == DELPHI)
290.         return TRUE; /* no fixup needed */
291. 
292.     /*
293.      * The Oracle isn't in DELPHI room.  Either hero entered her chamber
294.      * and got the one-time welcome message, converting it into an
295.      * ordinary room, or she got teleported out, or both.  Try to put
296.      * her back inside her room, if necessary, and restore its type.
297.      */
298. 
299.     /* find original delphi chamber; should always succeed */
300.     for (ridx = 0; ridx < SIZE(rooms); ++ridx)
301.         if (rooms[ridx].orig_rtype == DELPHI)
302.             break;
303. 
304.     if (o_ridx != ridx && ridx < SIZE(rooms)) {
305.         /* room found and she's not not in it, so try to move her there */
306.         cc.x = (rooms[ridx].lx + rooms[ridx].hx) / 2;
307.         cc.y = (rooms[ridx].ly + rooms[ridx].hy) / 2;
308.         if (enexto(&cc, cc.x, cc.y, oracle->data)) {
309.             rloc_to(oracle, cc.x, cc.y);
310.             o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
311.         }
312.         /* [if her room is already full, she might end up outside;
313.            that's ok, next hero just won't get any welcome message,
314.            same as used to happen before this fixup was introduced] */
315.     }
316.     if (ridx == o_ridx) /* if she's in her room, mark it as such */
317.         rooms[ridx].rtype = DELPHI;
318.     return TRUE; /* keep oracle in new bones file */
319. }
320. 
321. /* check whether bones are feasible */
322. boolean
323. can_make_bones()
324. {
325.     register struct trap *ttmp;
326. 
327.     if (!flags.bones)
328.         return FALSE;
329.     if (ledger_no(&u.uz) <= 0 || ledger_no(&u.uz) > maxledgerno())
330.         return FALSE;
331.     if (no_bones_level(&u.uz))
332.         return FALSE; /* no bones for specific levels */
333.     if (u.uswallow) {
334.         return FALSE; /* no bones when swallowed */
335.     }
336.     if (!Is_branchlev(&u.uz)) {
337.         /* no bones on non-branches with portals */
338.         for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
339.             if (ttmp->ttyp == MAGIC_PORTAL)
340.                 return FALSE;
341.     }
342. 
343.     if (depth(&u.uz) <= 0                 /* bulletproofing for endgame */
344.         || (!rn2(1 + (depth(&u.uz) >> 2)) /* fewer ghosts on low levels */
345.             && !wizard))
346.         return FALSE;
347.     /* don't let multiple restarts generate multiple copies of objects
348.        in bones files */
349.     if (discover)
350.         return FALSE;
351.     return TRUE;
352. }
353. 
354. /* save bones and possessions of a deceased adventurer */
355. void
356. savebones(how, when, corpse)
357. int how;
358. time_t when;
359. struct obj *corpse;
360. {
361.     int fd, x, y;
362.     struct trap *ttmp;
363.     struct monst *mtmp;
364.     struct permonst *mptr;
365.     struct fruit *f;
366.     struct cemetery *newbones;
367.     char c, *bonesid;
368.     char whynot[BUFSZ];
369. 
370.     /* caller has already checked `can_make_bones()' */
371. 
372.     clear_bypasses();
373.     fd = open_bonesfile(&u.uz, &bonesid);
374.     if (fd >= 0) {
375.         (void) nhclose(fd);
376.         if (wizard) {
377.             if (yn("Bones file already exists.  Replace it?") == 'y') {
378.                 if (delete_bonesfile(&u.uz))
379.                     goto make_bones;
380.                 else
381.                     pline("Cannot unlink old bones.");
382.             }
383.         }
384.         /* compression can change the file's name, so must
385.            wait until after any attempt to delete this file */
386.         compress_bonesfile();
387.         return;
388.     }
389. 
390.  make_bones:
391.     unleash_all();
392.     /* in case these characters are not in their home bases */
393.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
394.         if (DEADMONSTER(mtmp))
395.             continue;
396.         mptr = mtmp->data;
397.         if (mtmp->iswiz || mptr == &mons[PM_MEDUSA]
398.             || mptr->msound == MS_NEMESIS || mptr->msound == MS_LEADER
399.             || mptr == &mons[PM_VLAD_THE_IMPALER]
400.             || (mptr == &mons[PM_ORACLE] && !fixuporacle(mtmp)))
401.             mongone(mtmp);
402.     }
403.     if (u.usteed)
404.         dismount_steed(DISMOUNT_BONES);
405.     dmonsfree(); /* discard dead or gone monsters */
406. 
407.     /* mark all fruits as nonexistent; when we come to them we'll mark
408.      * them as existing (using goodfruit())
409.      */
410.     for (f = ffruit; f; f = f->nextf)
411.         f->fid = -f->fid;
412. 
413.     /* check iron balls separately--maybe they're not carrying it */
414.     if (uball)
415.         uball->owornmask = uchain->owornmask = 0L;
416. 
417.     /* dispose of your possessions, usually cursed */
418.     if (u.ugrave_arise == (NON_PM - 1)) {
419.         struct obj *otmp;
420. 
421.         /* embed your possessions in your statue */
422.         otmp = mk_named_object(STATUE, &mons[u.umonnum], u.ux, u.uy, plname);
423. 
424.         drop_upon_death((struct monst *) 0, otmp, u.ux, u.uy);
425.         if (!otmp)
426.             return; /* couldn't make statue */
427.         mtmp = (struct monst *) 0;
428.     } else if (u.ugrave_arise < LOW_PM) {
429.         /* drop everything */
430.         drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
431.         /* trick makemon() into allowing monster creation
432.          * on your location
433.          */
434.         in_mklev = TRUE;
435.         mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME);
436.         in_mklev = FALSE;
437.         if (!mtmp)
438.             return;
439.         mtmp = christen_monst(mtmp, plname);
440.         if (corpse)
441.             (void) obj_attach_mid(corpse, mtmp->m_id);
442.     } else {
443.         /* give your possessions to the monster you become */
444.         in_mklev = TRUE; /* use <u.ux,u.uy> as-is */
445.         mtmp = makemon(&mons[u.ugrave_arise], u.ux, u.uy, NO_MINVENT);
446.         in_mklev = FALSE;
447.         if (!mtmp) { /* arise-type might have been genocided */
448.             drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
449.             u.ugrave_arise = NON_PM; /* in case caller cares */
450.             return;
451.         }
452.         mtmp = christen_monst(mtmp, plname);
453.         newsym(u.ux, u.uy);
454.         /* ["Your body rises from the dead as an <mname>..." used
455.            to be given here, but it has been moved to done() so that
456.            it gets delivered even when savebones() isn't called] */
457.         drop_upon_death(mtmp, (struct obj *) 0, u.ux, u.uy);
458.         /* 'mtmp' now has hero's inventory; if 'mtmp' is a mummy, give it
459.            a wrapping unless already carrying one */
460.         if (mtmp->data->mlet == S_MUMMY && !m_carrying(mtmp, MUMMY_WRAPPING))
461.             (void) mongets(mtmp, MUMMY_WRAPPING);
462.         m_dowear(mtmp, TRUE);
463.     }
464.     if (mtmp) {
465.         mtmp->m_lev = (u.ulevel ? u.ulevel : 1);
466.         mtmp->mhp = mtmp->mhpmax = u.uhpmax;
467.         mtmp->female = flags.female;
468.         mtmp->msleeping = 1;
469.     }
470.     for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
471.         resetobjs(mtmp->minvent, FALSE);
472.         /* do not zero out m_ids for bones levels any more */
473.         mtmp->mlstmv = 0L;
474.         if (mtmp->mtame)
475.             mtmp->mtame = mtmp->mpeaceful = 0;
476.     }
477.     for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
478.         ttmp->madeby_u = 0;
479.         ttmp->tseen = (ttmp->ttyp == HOLE);
480.     }
481.     resetobjs(fobj, FALSE);
482.     resetobjs(level.buriedobjlist, FALSE);
483. 
484.     /* Hero is no longer on the map. */
485.     u.ux0 = u.ux, u.uy0 = u.uy;
486.     u.ux = u.uy = 0;
487. 
488.     /* Clear all memory from the level. */
489.     for (x = 1; x < COLNO; x++)
490.         for (y = 0; y < ROWNO; y++) {
491.             levl[x][y].seenv = 0;
492.             levl[x][y].waslit = 0;
493.             levl[x][y].glyph = cmap_to_glyph(S_stone);
494.             lastseentyp[x][y] = 0;
495.         }
496. 
497.     /* Attach bones info to the current level before saving. */
498.     newbones = (struct cemetery *) alloc(sizeof *newbones);
499.     /* entries are '\0' terminated but have fixed length allocations,
500.        so pre-fill with spaces to initialize any excess room */
501.     (void) memset((genericptr_t) newbones, ' ', sizeof *newbones);
502.     /* format name+role,&c, death reason, and date+time;
503.        gender and alignment reflect final values rather than what the
504.        character started out as, same as topten and logfile entries */
505.     Sprintf(newbones->who, "%s-%.3s-%.3s-%.3s-%.3s", plname, urole.filecode,
506.             urace.filecode, genders[flags.female].filecode,
507.             aligns[1 - u.ualign.type].filecode);
508.     formatkiller(newbones->how, sizeof newbones->how, how, TRUE);
509.     Strcpy(newbones->when, yyyymmddhhmmss(when));
510.     /* final resting place, used to decide when bones are discovered */
511.     newbones->frpx = u.ux0, newbones->frpy = u.uy0;
512.     newbones->bonesknown = FALSE;
513.     /* if current character died on a bones level, the cemetery list
514.        will have multiple entries, most recent (this dead hero) first */
515.     newbones->next = level.bonesinfo;
516.     level.bonesinfo = newbones;
517.     /* flag these bones if they are being created in wizard mode;
518.        they might already be flagged as such, even when we're playing
519.        in normal mode, if this level came from a previous bones file */
520.     if (wizard)
521.         level.flags.wizard_bones = 1;
522. 
523.     fd = create_bonesfile(&u.uz, &bonesid, whynot);
524.     if (fd < 0) {
525.         if (wizard)
526.             pline1(whynot);
527.         /* bones file creation problems are silent to the player.
528.          * Keep it that way, but place a clue into the paniclog.
529.          */
530.         paniclog("savebones", whynot);
531.         return;
532.     }
533.     c = (char) (strlen(bonesid) + 1);
534. 
535. #ifdef MFLOPPY /* check whether there is room */
536.     if (iflags.checkspace) {
537.         savelev(fd, ledger_no(&u.uz), COUNT_SAVE);
538.         /* savelev() initializes bytes_counted to 0, so it must come
539.          * first here even though it does not in the real save.  the
540.          * resulting extra bflush() at the end of savelev() may increase
541.          * bytes_counted by a couple over what the real usage will be.
542.          *
543.          * note it is safe to call store_version() here only because
544.          * bufon() is null for ZEROCOMP, which MFLOPPY uses -- otherwise
545.          * this code would have to know the size of the version
546.          * information itself.
547.          */
548.         store_version(fd);
549.         store_savefileinfo(fd);
550.         bwrite(fd, (genericptr_t) &c, sizeof c);
551.         bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
552.         savefruitchn(fd, COUNT_SAVE);
553.         bflush(fd);
554.         if (bytes_counted > freediskspace(bones)) { /* not enough room */
555.             if (wizard)
556.                 pline("Insufficient space to create bones file.");
557.             (void) nhclose(fd);
558.             cancel_bonesfile();
559.             return;
560.         }
561.         co_false(); /* make sure stuff before savelev() gets written */
562.     }
563. #endif /* MFLOPPY */
564. 
565.     store_version(fd);
566.     store_savefileinfo(fd);
567.     bwrite(fd, (genericptr_t) &c, sizeof c);
568.     bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
569.     savefruitchn(fd, WRITE_SAVE | FREE_SAVE);
570.     update_mlstmv(); /* update monsters for eventual restoration */
571.     savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE);
572.     bclose(fd);
573.     commit_bonesfile(&u.uz);
574.     compress_bonesfile();
575. }
576. 
577. int
578. getbones()
579. {
580.     register int fd;
581.     register int ok;
582.     char c, *bonesid, oldbonesid[40]; /* was [10]; more should be safer */
583. 
584.     if (discover) /* save bones files for real games */
585.         return 0;
586. 
587.     if (!flags.bones)
588.         return 0;
589.     /* wizard check added by GAN 02/05/87 */
590.     if (rn2(3) /* only once in three times do we find bones */
591.         && !wizard)
592.         return 0;
593.     if (no_bones_level(&u.uz))
594.         return 0;
595.     fd = open_bonesfile(&u.uz, &bonesid);
596.     if (fd < 0)
597.         return 0;
598. 
599.     if (validate(fd, bones) != 0) {
600.         if (!wizard)
601.             pline("Discarding unusable bones; no need to panic...");
602.         ok = FALSE;
603.     } else {
604.         ok = TRUE;
605.         if (wizard) {
606.             if (yn("Get bones?") == 'n') {
607.                 (void) nhclose(fd);
608.                 compress_bonesfile();
609.                 return 0;
610.             }
611.         }
612.         mread(fd, (genericptr_t) &c, sizeof c); /* length incl. '\0' */
613.         mread(fd, (genericptr_t) oldbonesid, (unsigned) c); /* DD.nnn */
614.         if (strcmp(bonesid, oldbonesid) != 0
615.             /* from 3.3.0 through 3.6.0, bones in the quest branch stored
616.                a bogus bonesid in the file; 3.6.1 fixed that, but for
617.                3.6.0 bones to remain compatible, we need an extra test;
618.                once compatibility with 3.6.x goes away, this can too
619.                (we don't try to make this conditional upon the value of
620.                VERSION_COMPATIBILITY because then we'd need patchlevel.h) */
621.             && (strlen(bonesid) <= 2
622.                 || strcmp(bonesid + 2, oldbonesid) != 0)) {
623.             char errbuf[BUFSZ];
624. 
625.             Sprintf(errbuf, "This is bones level '%s', not '%s'!", oldbonesid,
626.                     bonesid);
627.             if (wizard) {
628.                 pline1(errbuf);
629.                 ok = FALSE; /* won't die of trickery */
630.             }
631.             trickery(errbuf);
632.         } else {
633.             register struct monst *mtmp;
634. 
635.             getlev(fd, 0, 0, TRUE);
636. 
637.             /* Note that getlev() now keeps tabs on unique
638.              * monsters such as demon lords, and tracks the
639.              * birth counts of all species just as makemon()
640.              * does.  If a bones monster is extinct or has been
641.              * subject to genocide, their mhpmax will be
642.              * set to the magic DEFUNCT_MONSTER cookie value.
643.              */
644.             for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
645.                 if (has_mname(mtmp))
646.                     sanitize_name(MNAME(mtmp));
647.                 if (mtmp->mhpmax == DEFUNCT_MONSTER) {
648.                     if (wizard) {
649.                         debugpline1("Removing defunct monster %s from bones.",
650.                                     mtmp->data->mname);
651.                     }
652.                     mongone(mtmp);
653.                 } else
654.                     /* to correctly reset named artifacts on the level */
655.                     resetobjs(mtmp->minvent, TRUE);
656.             }
657.             resetobjs(fobj, TRUE);
658.             resetobjs(level.buriedobjlist, TRUE);
659.         }
660.     }
661.     (void) nhclose(fd);
662.     sanitize_engravings();
663.     u.uroleplay.numbones++;
664. 
665.     if (wizard) {
666.         if (yn("Unlink bones?") == 'n') {
667.             compress_bonesfile();
668.             return ok;
669.         }
670.     }
671.     if (!delete_bonesfile(&u.uz)) {
672.         /* When N games try to simultaneously restore the same
673.          * bones file, N-1 of them will fail to delete it
674.          * (the first N-1 under AmigaDOS, the last N-1 under UNIX).
675.          * So no point in a mysterious message for a normal event
676.          * -- just generate a new level for those N-1 games.
677.          */
678.         /* pline("Cannot unlink bones."); */
679.         return 0;
680.     }
681.     return ok;
682. }
683. 
684. /*bones.c*/