Source:NetHack 3.6.0/src/bones.c

From NetHackWiki
Jump to: navigation, search

Below is the full text to bones.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/bones.c#line123]], 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.

Top of file

  1.  /* NetHack 3.6	bones.c	$NHDT-Date: 1449269914 2015/12/04 22:58:34 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.66 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985,1993. */
  3.  /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5.  #include "hack.h"
  6.  #include "lev.h"
  7.  
  8.  extern char bones[]; /* from files.c */
  9.  #ifdef MFLOPPY
  10.  extern long bytes_counted;
  11.  #endif
  12.  
  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 *));
  17.  

no_bones_level

  1.  STATIC_OVL boolean
  2.  no_bones_level(lev)
  3.  d_level *lev;
  4.  {
  5.      extern d_level save_dlevel; /* in do.c */
  6.      s_level *sptr;
  7.  
  8.      if (ledger_no(&save_dlevel))
  9.          assign_level(lev, &save_dlevel);
  10.  
  11.      return (boolean) (((sptr = Is_special(lev)) != 0 && !sptr->boneid)
  12.                        || !dungeons[lev->dnum].boneid
  13.                        /* no bones on the last or multiway branch levels
  14.                           in any dungeon (level 1 isn't multiway) */
  15.                        || Is_botlevel(lev)
  16.                        || (Is_branchlev(lev) && lev->dlevel > 1)
  17.                        /* no bones in the invocation level */
  18.                        || (In_hell(lev)
  19.                            && lev->dlevel == dunlevs_in_dungeon(lev) - 1));
  20.  }
  21.  

goodfruit

  1.  /* Call this function for each fruit object saved in the bones level: it marks
  2.   * that particular type of fruit as existing (the marker is that that type's
  3.   * ID is positive instead of negative).  This way, when we later save the
  4.   * chain of fruit types, we know to only save the types that exist.
  5.   */
  6.  STATIC_OVL void
  7.  goodfruit(id)
  8.  int id;
  9.  {
  10.      register struct fruit *f;
  11.  
  12.      for (f = ffruit; f; f = f->nextf) {
  13.          if (f->fid == -id) {
  14.              f->fid = id;
  15.              return;
  16.          }
  17.      }
  18.  }
  19.  

resetobjs

  1.  STATIC_OVL void
  2.  resetobjs(ochain, restore)
  3.  struct obj *ochain;
  4.  boolean restore;
  5.  {
  6.      struct obj *otmp, *nobj;
  7.  
  8.      for (otmp = ochain; otmp; otmp = nobj) {
  9.          nobj = otmp->nobj;
  10.          if (otmp->cobj)
  11.              resetobjs(otmp->cobj, restore);
  12.          if (otmp->in_use) {
  13.              obj_extract_self(otmp);
  14.              dealloc_obj(otmp);
  15.              continue;
  16.          }
  17.  
  18.          if (restore) {
  19.              /* artifact bookkeeping needs to be done during
  20.                 restore; other fixups are done while saving */
  21.              if (otmp->oartifact) {
  22.                  if (exist_artifact(otmp->otyp, safe_oname(otmp))
  23.                      || is_quest_artifact(otmp)) {
  24.                      /* prevent duplicate--revert to ordinary obj */
  25.                      otmp->oartifact = 0;
  26.                      if (has_oname(otmp))
  27.                          free_oname(otmp);
  28.                  } else {
  29.                      artifact_exists(otmp, safe_oname(otmp), TRUE);
  30.                  }
  31.              } else if (has_oname(otmp)) {
  32.                  sanitize_name(ONAME(otmp));
  33.              }
  34.          } else { /* saving */
  35.              /* do not zero out o_ids for ghost levels anymore */
  36.  
  37.              if (objects[otmp->otyp].oc_uses_known)
  38.                  otmp->known = 0;
  39.              otmp->dknown = otmp->bknown = 0;
  40.              otmp->rknown = 0;
  41.              otmp->lknown = 0;
  42.              otmp->cknown = 0;
  43.              otmp->invlet = 0;
  44.              otmp->no_charge = 0;
  45.              otmp->was_thrown = 0;
  46.  
  47.              /* strip user-supplied names */
  48.              /* Statue and some corpse names are left intact,
  49.                 presumably in case they came from score file.
  50.                 [TODO: this ought to be done differently--names
  51.                 which came from such a source or came from any
  52.                 stoned or killed monster should be flagged in
  53.                 some manner; then we could just check the flag
  54.                 here and keep "real" names (dead pets, &c) while
  55.                 discarding player notes attached to statues.] */
  56.              if (has_oname(otmp)
  57.                  && !(otmp->oartifact || otmp->otyp == STATUE
  58.                       || otmp->otyp == SPE_NOVEL
  59.                       || (otmp->otyp == CORPSE
  60.                           && otmp->corpsenm >= SPECIAL_PM))) {
  61.                  free_oname(otmp);
  62.              }
  63.  
  64.              if (otmp->otyp == SLIME_MOLD)
  65.                  goodfruit(otmp->spe);
  66.  #ifdef MAIL
  67.              else if (otmp->otyp == SCR_MAIL)
  68.                  otmp->spe = 1;
  69.  #endif
  70.              else if (otmp->otyp == EGG)
  71.                  otmp->spe = 0;
  72.              else if (otmp->otyp == TIN) {
  73.                  /* make tins of unique monster's meat be empty */
  74.                  if (otmp->corpsenm >= LOW_PM
  75.                      && unique_corpstat(&mons[otmp->corpsenm]))
  76.                      otmp->corpsenm = NON_PM;
  77.              } else if (otmp->otyp == CORPSE || otmp->otyp == STATUE) {
  78.                  int mnum = otmp->corpsenm;
  79.  
  80.                  /* Discard incarnation details of unique
  81.                     monsters (by passing null instead of otmp
  82.                     for object), shopkeepers (by passing false
  83.                     for revival flag), temple priests, and
  84.                     vault guards in order to prevent corpse
  85.                     revival or statue reanimation. */
  86.                  if (has_omonst(otmp)
  87.                      && cant_revive(&mnum, FALSE, (struct obj *) 0)) {
  88.                      free_omonst(otmp);
  89.                      /* mnum is now either human_zombie or
  90.                         doppelganger; for corpses of uniques,
  91.                         we need to force the transformation
  92.                         now rather than wait until a revival
  93.                         attempt, otherwise eating this corpse
  94.                         would behave as if it remains unique */
  95.                      if (mnum == PM_DOPPELGANGER && otmp->otyp == CORPSE)
  96.                          set_corpsenm(otmp, mnum);
  97.                  }
  98.              } else if (otmp->otyp == AMULET_OF_YENDOR) {
  99.                  /* no longer the real Amulet */
  100.                  otmp->otyp = FAKE_AMULET_OF_YENDOR;
  101.                  curse(otmp);
  102.              } else if (otmp->otyp == CANDELABRUM_OF_INVOCATION) {
  103.                  if (otmp->lamplit)
  104.                      end_burn(otmp, TRUE);
  105.                  otmp->otyp = WAX_CANDLE;
  106.                  otmp->age = 50L; /* assume used */
  107.                  if (otmp->spe > 0)
  108.                      otmp->quan = (long) otmp->spe;
  109.                  otmp->spe = 0;
  110.                  otmp->owt = weight(otmp);
  111.                  curse(otmp);
  112.              } else if (otmp->otyp == BELL_OF_OPENING) {
  113.                  otmp->otyp = BELL;
  114.                  curse(otmp);
  115.              } else if (otmp->otyp == SPE_BOOK_OF_THE_DEAD) {
  116.                  otmp->otyp = SPE_BLANK_PAPER;
  117.                  curse(otmp);
  118.              }
  119.          }
  120.      }
  121.  }
  122.  

sanitize_name

  1.  /* while loading bones, strip out text possibly supplied by old player
  2.     that might accidentally or maliciously disrupt new player's display */
  3.  void
  4.  sanitize_name(namebuf)
  5.  char *namebuf;
  6.  {
  7.      int c;
  8.      boolean strip_8th_bit =
  9.          !strcmp(windowprocs.name, "tty") && !iflags.wc_eight_bit_input;
  10.  
  11.      /* it's tempting to skip this for single-user platforms, since
  12.         only the current player could have left these bones--except
  13.         things like "hearse" and other bones exchange schemes make
  14.         that assumption false */
  15.      while (*namebuf) {
  16.          c = *namebuf & 0177;
  17.          if (c < ' ' || c == '\177') {
  18.              /* non-printable or undesirable */
  19.              *namebuf = '.';
  20.          } else if (c != *namebuf) {
  21.              /* expected to be printable if user wants such things */
  22.              if (strip_8th_bit)
  23.                  *namebuf = '_';
  24.          }
  25.          ++namebuf;
  26.      }
  27.  }
  28.  

drop_upon_death

  1.  /* called by savebones(); also by finish_paybill(shk.c) */
  2.  void
  3.  drop_upon_death(mtmp, cont, x, y)
  4.  struct monst *mtmp;
  5.  struct obj *cont;
  6.  int x, y;
  7.  {
  8.      struct obj *otmp;
  9.  
  10.      u.twoweap = 0; /* ensure curse() won't cause swapwep to drop twice */
  11.      while ((otmp = invent) != 0) {
  12.          obj_extract_self(otmp);
  13.          obj_no_longer_held(otmp);
  14.  
  15.          otmp->owornmask = 0;
  16.          /* lamps don't go out when dropped */
  17.          if ((cont || artifact_light(otmp)) && obj_is_burning(otmp))
  18.              end_burn(otmp, TRUE); /* smother in statue */
  19.  
  20.          if (otmp->otyp == SLIME_MOLD)
  21.              goodfruit(otmp->spe);
  22.  
  23.          if (rn2(5))
  24.              curse(otmp);
  25.          if (mtmp)
  26.              (void) add_to_minv(mtmp, otmp);
  27.          else if (cont)
  28.              (void) add_to_container(cont, otmp);
  29.          else
  30.              place_object(otmp, x, y);
  31.      }
  32.      if (cont)
  33.          cont->owt = weight(cont);
  34.  }
  35.  

fixuporacle

  1.  /* possibly restore oracle's room and/or put her back inside it; returns
  2.     False if she's on the wrong level and should be removed, True otherwise */
  3.  STATIC_OVL boolean
  4.  fixuporacle(oracle)
  5.  struct monst *oracle;
  6.  {
  7.      coord cc;
  8.      int ridx, o_ridx;
  9.  
  10.      /* oracle doesn't move, but knight's joust or monk's staggering blow
  11.         could push her onto a hole in the floor; at present, traps don't
  12.         activate in such situation hence she won't fall to another level;
  13.         however, that could change so be prepared to cope with such things */
  14.      if (!Is_oracle_level(&u.uz))
  15.          return FALSE;
  16.  
  17.      oracle->mpeaceful = 1;
  18.      o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
  19.      if (o_ridx >= 0 && rooms[o_ridx].rtype == DELPHI)
  20.          return TRUE; /* no fixup needed */
  21.  
  22.      /*
  23.       * The Oracle isn't in DELPHI room.  Either hero entered her chamber
  24.       * and got the one-time welcome message, converting it into an
  25.       * ordinary room, or she got teleported out, or both.  Try to put
  26.       * her back inside her room, if necessary, and restore its type.
  27.       */
  28.  
  29.      /* find original delphi chamber; should always succeed */
  30.      for (ridx = 0; ridx < SIZE(rooms); ++ridx)
  31.          if (rooms[ridx].orig_rtype == DELPHI)
  32.              break;
  33.  
  34.      if (o_ridx != ridx && ridx < SIZE(rooms)) {
  35.          /* room found and she's not not in it, so try to move her there */
  36.          cc.x = (rooms[ridx].lx + rooms[ridx].hx) / 2;
  37.          cc.y = (rooms[ridx].ly + rooms[ridx].hy) / 2;
  38.          if (enexto(&cc, cc.x, cc.y, oracle->data)) {
  39.              rloc_to(oracle, cc.x, cc.y);
  40.              o_ridx = levl[oracle->mx][oracle->my].roomno - ROOMOFFSET;
  41.          }
  42.          /* [if her room is already full, she might end up outside;
  43.             that's ok, next hero just won't get any welcome message,
  44.             same as used to happen before this fixup was introduced] */
  45.      }
  46.      if (ridx == o_ridx) /* if she's in her room, mark it as such */
  47.          rooms[ridx].rtype = DELPHI;
  48.      return TRUE; /* keep oracle in new bones file */
  49.  }
  50.  

can_make_bones

  1.  /* check whether bones are feasible */
  2.  boolean
  3.  can_make_bones()
  4.  {
  5.      register struct trap *ttmp;
  6.  
  7.      if (!flags.bones)
  8.          return FALSE;
  9.      if (ledger_no(&u.uz) <= 0 || ledger_no(&u.uz) > maxledgerno())
  10.          return FALSE;
  11.      if (no_bones_level(&u.uz))
  12.          return FALSE; /* no bones for specific levels */
  13.      if (u.uswallow) {
  14.          return FALSE; /* no bones when swallowed */
  15.      }
  16.      if (!Is_branchlev(&u.uz)) {
  17.          /* no bones on non-branches with portals */
  18.          for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap)
  19.              if (ttmp->ttyp == MAGIC_PORTAL)
  20.                  return FALSE;
  21.      }
  22.  
  23.      if (depth(&u.uz) <= 0                 /* bulletproofing for endgame */
  24.          || (!rn2(1 + (depth(&u.uz) >> 2)) /* fewer ghosts on low levels */
  25.              && !wizard))
  26.          return FALSE;
  27.      /* don't let multiple restarts generate multiple copies of objects
  28.         in bones files */
  29.      if (discover)
  30.          return FALSE;
  31.      return TRUE;
  32.  }
  33.  

savebones

  1.  /* save bones and possessions of a deceased adventurer */
  2.  void
  3.  savebones(how, when, corpse)
  4.  int how;
  5.  time_t when;
  6.  struct obj *corpse;
  7.  {
  8.      int fd, x, y;
  9.      struct trap *ttmp;
  10.      struct monst *mtmp;
  11.      struct permonst *mptr;
  12.      struct fruit *f;
  13.      struct cemetery *newbones;
  14.      char c, *bonesid;
  15.      char whynot[BUFSZ];
  16.  
  17.      /* caller has already checked `can_make_bones()' */
  18.  
  19.      clear_bypasses();
  20.      fd = open_bonesfile(&u.uz, &bonesid);
  21.      if (fd >= 0) {
  22.          (void) nhclose(fd);
  23.          if (wizard) {
  24.              if (yn("Bones file already exists.  Replace it?") == 'y') {
  25.                  if (delete_bonesfile(&u.uz))
  26.                      goto make_bones;
  27.                  else
  28.                      pline("Cannot unlink old bones.");
  29.              }
  30.          }
  31.          /* compression can change the file's name, so must
  32.             wait until after any attempt to delete this file */
  33.          compress_bonesfile();
  34.          return;
  35.      }
  36.  
  37.  make_bones:
  38.      unleash_all();
  39.      /* in case these characters are not in their home bases */
  40.      for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
  41.          if (DEADMONSTER(mtmp))
  42.              continue;
  43.          mptr = mtmp->data;
  44.          if (mtmp->iswiz || mptr == &mons[PM_MEDUSA]
  45.              || mptr->msound == MS_NEMESIS || mptr->msound == MS_LEADER
  46.              || mptr == &mons[PM_VLAD_THE_IMPALER]
  47.              || (mptr == &mons[PM_ORACLE] && !fixuporacle(mtmp)))
  48.              mongone(mtmp);
  49.      }
  50.      if (u.usteed)
  51.          dismount_steed(DISMOUNT_BONES);
  52.      dmonsfree(); /* discard dead or gone monsters */
  53.  
  54.      /* mark all fruits as nonexistent; when we come to them we'll mark
  55.       * them as existing (using goodfruit())
  56.       */
  57.      for (f = ffruit; f; f = f->nextf)
  58.          f->fid = -f->fid;
  59.  
  60.      /* check iron balls separately--maybe they're not carrying it */
  61.      if (uball)
  62.          uball->owornmask = uchain->owornmask = 0;
  63.  
  64.      /* dispose of your possessions, usually cursed */
  65.      if (u.ugrave_arise == (NON_PM - 1)) {
  66.          struct obj *otmp;
  67.  
  68.          /* embed your possessions in your statue */
  69.          otmp = mk_named_object(STATUE, &mons[u.umonnum], u.ux, u.uy, plname);
  70.  
  71.          drop_upon_death((struct monst *) 0, otmp, u.ux, u.uy);
  72.          if (!otmp)
  73.              return; /* couldn't make statue */
  74.          mtmp = (struct monst *) 0;
  75.      } else if (u.ugrave_arise < LOW_PM) {
  76.          /* drop everything */
  77.          drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
  78.          /* trick makemon() into allowing monster creation
  79.           * on your location
  80.           */
  81.          in_mklev = TRUE;
  82.          mtmp = makemon(&mons[PM_GHOST], u.ux, u.uy, MM_NONAME);
  83.          in_mklev = FALSE;
  84.          if (!mtmp)
  85.              return;
  86.          mtmp = christen_monst(mtmp, plname);
  87.          if (corpse)
  88.              (void) obj_attach_mid(corpse, mtmp->m_id);
  89.      } else {
  90.          /* give your possessions to the monster you become */
  91.          in_mklev = TRUE; /* use <u.ux,u.uy> as-is */
  92.          mtmp = makemon(&mons[u.ugrave_arise], u.ux, u.uy, NO_MINVENT);
  93.          in_mklev = FALSE;
  94.          if (!mtmp) {
  95.              drop_upon_death((struct monst *) 0, (struct obj *) 0, u.ux, u.uy);
  96.              u.ugrave_arise = NON_PM; /* in case caller cares */
  97.              return;
  98.          }
  99.          /* give mummy-from-hero a wrapping unless hero already
  100.             carries one; don't bother forcing it to become worn */
  101.          if (mtmp->data->mlet == S_MUMMY && !carrying(MUMMY_WRAPPING))
  102.              (void) mongets(mtmp, MUMMY_WRAPPING);
  103.          mtmp = christen_monst(mtmp, plname);
  104.          newsym(u.ux, u.uy);
  105.          /* ["Your body rises from the dead as an <mname>..." used
  106.             to be given here, but it has been moved to done() so that
  107.             it gets delivered even when savebones() isn't called] */
  108.          drop_upon_death(mtmp, (struct obj *) 0, u.ux, u.uy);
  109.          m_dowear(mtmp, TRUE);
  110.      }
  111.      if (mtmp) {
  112.          mtmp->m_lev = (u.ulevel ? u.ulevel : 1);
  113.          mtmp->mhp = mtmp->mhpmax = u.uhpmax;
  114.          mtmp->female = flags.female;
  115.          mtmp->msleeping = 1;
  116.      }
  117.      for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
  118.          resetobjs(mtmp->minvent, FALSE);
  119.          /* do not zero out m_ids for bones levels any more */
  120.          mtmp->mlstmv = 0L;
  121.          if (mtmp->mtame)
  122.              mtmp->mtame = mtmp->mpeaceful = 0;
  123.      }
  124.      for (ttmp = ftrap; ttmp; ttmp = ttmp->ntrap) {
  125.          ttmp->madeby_u = 0;
  126.          ttmp->tseen = (ttmp->ttyp == HOLE);
  127.      }
  128.      resetobjs(fobj, FALSE);
  129.      resetobjs(level.buriedobjlist, FALSE);
  130.  
  131.      /* Hero is no longer on the map. */
  132.      u.ux = u.uy = 0;
  133.  
  134.      /* Clear all memory from the level. */
  135.      for (x = 1; x < COLNO; x++)
  136.          for (y = 0; y < ROWNO; y++) {
  137.              levl[x][y].seenv = 0;
  138.              levl[x][y].waslit = 0;
  139.              levl[x][y].glyph = cmap_to_glyph(S_stone);
  140.              lastseentyp[x][y] = 0;
  141.          }
  142.  
  143.      /* Attach bones info to the current level before saving. */
  144.      newbones = (struct cemetery *) alloc(sizeof *newbones);
  145.      /* entries are '\0' terminated but have fixed length allocations,
  146.         so pre-fill with spaces to initialize any excess room */
  147.      (void) memset((genericptr_t) newbones, ' ', sizeof *newbones);
  148.      /* format name+role,&c, death reason, and date+time;
  149.         gender and alignment reflect final values rather than what the
  150.         character started out as, same as topten and logfile entries */
  151.      Sprintf(newbones->who, "%s-%.3s-%.3s-%.3s-%.3s", plname, urole.filecode,
  152.              urace.filecode, genders[flags.female].filecode,
  153.              aligns[1 - u.ualign.type].filecode);
  154.      formatkiller(newbones->how, sizeof newbones->how, how);
  155.      Strcpy(newbones->when, yyyymmddhhmmss(when));
  156.      /* final resting place, used to decide when bones are discovered */
  157.      newbones->frpx = u.ux, newbones->frpy = u.uy;
  158.      newbones->bonesknown = FALSE;
  159.      /* if current character died on a bones level, the cemetery list
  160.         will have multiple entries, most recent (this dead hero) first */
  161.      newbones->next = level.bonesinfo;
  162.      level.bonesinfo = newbones;
  163.      /* flag these bones if they are being created in wizard mode;
  164.         they might already be flagged as such, even when we're playing
  165.         in normal mode, if this level came from a previous bones file */
  166.      if (wizard)
  167.          level.flags.wizard_bones = 1;
  168.  
  169.      fd = create_bonesfile(&u.uz, &bonesid, whynot);
  170.      if (fd < 0) {
  171.          if (wizard)
  172.              pline1(whynot);
  173.          /* bones file creation problems are silent to the player.
  174.           * Keep it that way, but place a clue into the paniclog.
  175.           */
  176.          paniclog("savebones", whynot);
  177.          return;
  178.      }
  179.      c = (char) (strlen(bonesid) + 1);
  180.  
  181.  #ifdef MFLOPPY /* check whether there is room */
  182.      if (iflags.checkspace) {
  183.          savelev(fd, ledger_no(&u.uz), COUNT_SAVE);
  184.          /* savelev() initializes bytes_counted to 0, so it must come
  185.           * first here even though it does not in the real save.  the
  186.           * resulting extra bflush() at the end of savelev() may increase
  187.           * bytes_counted by a couple over what the real usage will be.
  188.           *
  189.           * note it is safe to call store_version() here only because
  190.           * bufon() is null for ZEROCOMP, which MFLOPPY uses -- otherwise
  191.           * this code would have to know the size of the version
  192.           * information itself.
  193.           */
  194.          store_version(fd);
  195.          store_savefileinfo(fd);
  196.          bwrite(fd, (genericptr_t) &c, sizeof c);
  197.          bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
  198.          savefruitchn(fd, COUNT_SAVE);
  199.          bflush(fd);
  200.          if (bytes_counted > freediskspace(bones)) { /* not enough room */
  201.              if (wizard)
  202.                  pline("Insufficient space to create bones file.");
  203.              (void) nhclose(fd);
  204.              cancel_bonesfile();
  205.              return;
  206.          }
  207.          co_false(); /* make sure stuff before savelev() gets written */
  208.      }
  209.  #endif /* MFLOPPY */
  210.  
  211.      store_version(fd);
  212.      store_savefileinfo(fd);
  213.      bwrite(fd, (genericptr_t) &c, sizeof c);
  214.      bwrite(fd, (genericptr_t) bonesid, (unsigned) c); /* DD.nnn */
  215.      savefruitchn(fd, WRITE_SAVE | FREE_SAVE);
  216.      update_mlstmv(); /* update monsters for eventual restoration */
  217.      savelev(fd, ledger_no(&u.uz), WRITE_SAVE | FREE_SAVE);
  218.      bclose(fd);
  219.      commit_bonesfile(&u.uz);
  220.      compress_bonesfile();
  221.  }
  222.  

getbones

  1.  int
  2.  getbones()
  3.  {
  4.      register int fd;
  5.      register int ok;
  6.      char c, *bonesid, oldbonesid[10];
  7.  
  8.      if (discover) /* save bones files for real games */
  9.          return 0;
  10.  
  11.      if (!flags.bones)
  12.          return 0;
  13.      /* wizard check added by GAN 02/05/87 */
  14.      if (rn2(3) /* only once in three times do we find bones */
  15.          && !wizard)
  16.          return 0;
  17.      if (no_bones_level(&u.uz))
  18.          return 0;
  19.      fd = open_bonesfile(&u.uz, &bonesid);
  20.      if (fd < 0)
  21.          return 0;
  22.  
  23.      if (validate(fd, bones) != 0) {
  24.          if (!wizard)
  25.              pline("Discarding unuseable bones; no need to panic...");
  26.          ok = FALSE;
  27.      } else {
  28.          ok = TRUE;
  29.          if (wizard) {
  30.              if (yn("Get bones?") == 'n') {
  31.                  (void) nhclose(fd);
  32.                  compress_bonesfile();
  33.                  return 0;
  34.              }
  35.          }
  36.          mread(fd, (genericptr_t) &c, sizeof c); /* length incl. '\0' */
  37.          mread(fd, (genericptr_t) oldbonesid, (unsigned) c); /* DD.nnn */
  38.          if (strcmp(bonesid, oldbonesid) != 0) {
  39.              char errbuf[BUFSZ];
  40.  
  41.              Sprintf(errbuf, "This is bones level '%s', not '%s'!", oldbonesid,
  42.                      bonesid);
  43.              if (wizard) {
  44.                  pline1(errbuf);
  45.                  ok = FALSE; /* won't die of trickery */
  46.              }
  47.              trickery(errbuf);
  48.          } else {
  49.              register struct monst *mtmp;
  50.  
  51.              getlev(fd, 0, 0, TRUE);
  52.  
  53.              /* Note that getlev() now keeps tabs on unique
  54.               * monsters such as demon lords, and tracks the
  55.               * birth counts of all species just as makemon()
  56.               * does.  If a bones monster is extinct or has been
  57.               * subject to genocide, their mhpmax will be
  58.               * set to the magic DEFUNCT_MONSTER cookie value.
  59.               */
  60.              for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
  61.                  if (has_mname(mtmp))
  62.                      sanitize_name(MNAME(mtmp));
  63.                  if (mtmp->mhpmax == DEFUNCT_MONSTER) {
  64.                      if (wizard) {
  65.                          debugpline1("Removing defunct monster %s from bones.",
  66.                                      mtmp->data->mname);
  67.                      }
  68.                      mongone(mtmp);
  69.                  } else
  70.                      /* to correctly reset named artifacts on the level */
  71.                      resetobjs(mtmp->minvent, TRUE);
  72.              }
  73.              resetobjs(fobj, TRUE);
  74.              resetobjs(level.buriedobjlist, TRUE);
  75.          }
  76.      }
  77.      (void) nhclose(fd);
  78.      sanitize_engravings();
  79.      u.uroleplay.numbones++;
  80.  
  81.      if (wizard) {
  82.          if (yn("Unlink bones?") == 'n') {
  83.              compress_bonesfile();
  84.              return ok;
  85.          }
  86.      }
  87.      if (!delete_bonesfile(&u.uz)) {
  88.          /* When N games try to simultaneously restore the same
  89.           * bones file, N-1 of them will fail to delete it
  90.           * (the first N-1 under AmigaDOS, the last N-1 under UNIX).
  91.           * So no point in a mysterious message for a normal event
  92.           * -- just generate a new level for those N-1 games.
  93.           */
  94.          /* pline("Cannot unlink bones."); */
  95.          return 0;
  96.      }
  97.      return ok;
  98.  }
  99.  
  100.  /*bones.c*/