OSDN Git Service

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