Source:NetHack 3.6.0/src/timeout.c

From NetHackWiki
Jump to: navigation, search

Below is the full text to timeout.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/timeout.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	timeout.c	$NHDT-Date: 1446861771 2015/11/07 02:02:51 $  $NHDT-Branch: master $:$NHDT-Revision: 1.63 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5.  #include "hack.h"
  6.  #include "lev.h" /* for checking save modes */
  7.  

Special timeout functions

  1.  STATIC_DCL void NDECL(stoned_dialogue);
  2.  STATIC_DCL void NDECL(vomiting_dialogue);
  3.  STATIC_DCL void NDECL(choke_dialogue);
  4.  STATIC_DCL void NDECL(slime_dialogue);
  5.  STATIC_DCL void NDECL(slip_or_trip);
  6.  STATIC_DCL void FDECL(see_lamp_flicker, (struct obj *, const char *));
  7.  STATIC_DCL void FDECL(lantern_message, (struct obj *));
  8.  STATIC_DCL void FDECL(cleanup_burn, (ANY_P *, long));
  9.  

stoned_dialogue

  1.  /* He is being petrified - dialogue by inmet!tower */
  2.  static NEARDATA const char *const stoned_texts[] = {
  3.      "You are slowing down.",            /* 5 */
  4.      "Your limbs are stiffening.",       /* 4 */
  5.      "Your limbs have turned to stone.", /* 3 */
  6.      "You have turned to stone.",        /* 2 */
  7.      "You are a statue."                 /* 1 */
  8.  };
  9.  
  10.  STATIC_OVL void
  11.  stoned_dialogue()
  12.  {
  13.      register long i = (Stoned & TIMEOUT);
  14.  
  15.      if (i > 0L && i <= SIZE(stoned_texts)) {
  16.          char buf[BUFSZ];
  17.  
  18.          Strcpy(buf, stoned_texts[SIZE(stoned_texts) - i]);
  19.          if (nolimbs(youmonst.data) && strstri(buf, "limbs"))
  20.              (void) strsubst(buf, "limbs", "extremities");
  21.          pline1(buf);
  22.      }
  23.      switch ((int) i) {
  24.      case 5: /* slowing down */
  25.          HFast = 0L;
  26.          if (multi > 0)
  27.              nomul(0);
  28.          break;
  29.      case 4: /* limbs stiffening */
  30.          /* just one move left to save oneself so quit fiddling around;
  31.             don't stop attempt to eat tin--might be lizard or acidic */
  32.          if (!Popeye(STONED))
  33.              stop_occupation();
  34.          if (multi > 0)
  35.              nomul(0);
  36.          break;
  37.      case 3: /* limbs turned to stone */
  38.          stop_occupation();
  39.          nomul(-3); /* can't move anymore */
  40.          multi_reason = "getting stoned";
  41.          nomovemsg = You_can_move_again; /* not unconscious */
  42.          break;
  43.      default:
  44.          break;
  45.      }
  46.      exercise(A_DEX, FALSE);
  47.  }
  48.  

vomiting_dialogue

  1.  /* He is getting sicker and sicker prior to vomiting */
  2.  static NEARDATA const char *const vomiting_texts[] = {
  3.      "are feeling mildly nauseated.", /* 14 */
  4.      "feel slightly confused.",       /* 11 */
  5.      "can't seem to think straight.", /* 8 */
  6.      "feel incredibly sick.",         /* 5 */
  7.      "suddenly vomit!"                /* 2 */
  8.  };
  9.  
  10.  STATIC_OVL void
  11.  vomiting_dialogue()
  12.  {
  13.      const char *txt = 0;
  14.      long v = (Vomiting & TIMEOUT);
  15.  
  16.      /* note: nhtimeout() hasn't decremented timed properties for the
  17.         current turn yet, so we use Vomiting-1 here */
  18.      switch ((int) (v - 1L)) {
  19.      case 14:
  20.          txt = vomiting_texts[0];
  21.          break;
  22.      case 11:
  23.          txt = vomiting_texts[1];
  24.          break;
  25.      case 6:
  26.          make_stunned((HStun & TIMEOUT) + (long) d(2, 4), FALSE);
  27.          if (!Popeye(VOMITING))
  28.              stop_occupation();
  29.      /*FALLTHRU*/
  30.      case 9:
  31.          make_confused((HConfusion & TIMEOUT) + (long) d(2, 4), FALSE);
  32.          if (multi > 0)
  33.              nomul(0);
  34.          break;
  35.      case 8:
  36.          txt = vomiting_texts[2];
  37.          break;
  38.      case 5:
  39.          txt = vomiting_texts[3];
  40.          break;
  41.      case 2:
  42.          txt = vomiting_texts[4];
  43.          if (cantvomit(youmonst.data))
  44.              txt = "gag uncontrolably.";
  45.          break;
  46.      case 0:
  47.          stop_occupation();
  48.          if (!cantvomit(youmonst.data))
  49.              morehungry(20);
  50.          vomit();
  51.          break;
  52.      default:
  53.          break;
  54.      }
  55.      if (txt)
  56.          You1(txt);
  57.      exercise(A_CON, FALSE);
  58.  }
  59.  

choke_dialogue

  1.  static NEARDATA const char *const choke_texts[] = {
  2.      "You find it hard to breathe.", "You're gasping for air.",
  3.      "You can no longer breathe.", "You're turning %s.", "You suffocate."
  4.  };
  5.  
  6.  static NEARDATA const char *const choke_texts2[] = {
  7.      "Your %s is becoming constricted.",
  8.      "Your blood is having trouble reaching your brain.",
  9.      "The pressure on your %s increases.", "Your consciousness is fading.",
  10.      "You suffocate."
  11.  };
  12.  
  13.  STATIC_OVL void
  14.  choke_dialogue()
  15.  {
  16.      register long i = (Strangled & TIMEOUT);
  17.  
  18.      if (i > 0 && i <= SIZE(choke_texts)) {
  19.          if (Breathless || !rn2(50))
  20.              pline(choke_texts2[SIZE(choke_texts2) - i], body_part(NECK));
  21.          else {
  22.              const char *str = choke_texts[SIZE(choke_texts) - i];
  23.  
  24.              if (index(str, '%'))
  25.                  pline(str, hcolor(NH_BLUE));
  26.              else
  27.                  pline1(str);
  28.          }
  29.      }
  30.      exercise(A_STR, FALSE);
  31.  }
  32.  

slime_dialogue

  1.  static NEARDATA const char *const slime_texts[] = {
  2.      "You are turning a little %s.",   /* 5 */
  3.      "Your limbs are getting oozy.",   /* 4 */
  4.      "Your skin begins to peel away.", /* 3 */
  5.      "You are turning into %s.",       /* 2 */
  6.      "You have become %s."             /* 1 */
  7.  };
  8.  
  9.  STATIC_OVL void
  10.  slime_dialogue()
  11.  {
  12.      register long i = (Slimed & TIMEOUT) / 2L;
  13.  
  14.      if (((Slimed & TIMEOUT) % 2L) && i >= 0L && i < SIZE(slime_texts)) {
  15.          char buf[BUFSZ];
  16.  
  17.          Strcpy(buf, slime_texts[SIZE(slime_texts) - i - 1L]);
  18.          if (nolimbs(youmonst.data) && strstri(buf, "limbs"))
  19.              (void) strsubst(buf, "limbs", "extremities");
  20.  
  21.          if (index(buf, '%')) {
  22.              if (i == 4L) {  /* "you are turning green" */
  23.                  if (!Blind) /* [what if you're already green?] */
  24.                      pline(buf, hcolor(NH_GREEN));
  25.              } else
  26.                  pline(buf,
  27.                        an(Hallucination ? rndmonnam(NULL) : "green slime"));
  28.          } else
  29.              pline1(buf);
  30.      }
  31.      if (i == 3L) {  /* limbs becoming oozy */
  32.          HFast = 0L; /* lose intrinsic speed */
  33.          if (!Popeye(SLIMED))
  34.              stop_occupation();
  35.          if (multi > 0)
  36.              nomul(0);
  37.      }
  38.      exercise(A_DEX, FALSE);
  39.  }
  40.  

burn_away_slime

  1.  void
  2.  burn_away_slime()
  3.  {
  4.      if (Slimed) {
  5.          make_slimed(0L, "The slime that covers you is burned away!");
  6.      }
  7.  }
  8.  

nh_timeout

  1.  void
  2.  nh_timeout()
  3.  {
  4.      register struct prop *upp;
  5.      struct kinfo *kptr;
  6.      int sleeptime;
  7.      int m_idx;
  8.      int baseluck = (flags.moonphase == FULL_MOON) ? 1 : 0;
  9.  
  10.      if (flags.friday13)
  11.          baseluck -= 1;
  12.  
  13.      if (u.uluck != baseluck
  14.          && moves % (u.uhave.amulet || u.ugangr ? 300 : 600) == 0) {
  15.          /* Cursed luckstones stop bad luck from timing out; blessed luckstones
  16.           * stop good luck from timing out; normal luckstones stop both;
  17.           * neither is stopped if you don't have a luckstone.
  18.           * Luck is based at 0 usually, +1 if a full moon and -1 on Friday 13th
  19.           */
  20.          register int time_luck = stone_luck(FALSE);
  21.          boolean nostone = !carrying(LUCKSTONE) && !stone_luck(TRUE);
  22.  
  23.          if (u.uluck > baseluck && (nostone || time_luck < 0))
  24.              u.uluck--;
  25.          else if (u.uluck < baseluck && (nostone || time_luck > 0))
  26.              u.uluck++;
  27.      }
  28.      if (u.uinvulnerable)
  29.          return; /* things past this point could kill you */
  30.      if (Stoned)
  31.          stoned_dialogue();
  32.      if (Slimed)
  33.          slime_dialogue();
  34.      if (Vomiting)
  35.          vomiting_dialogue();
  36.      if (Strangled)
  37.          choke_dialogue();
  38.      if (u.mtimedone && !--u.mtimedone) {
  39.          if (Unchanging)
  40.              u.mtimedone = rnd(100 * youmonst.data->mlevel + 1);
  41.          else
  42.              rehumanize();
  43.      }
  44.      if (u.ucreamed)
  45.          u.ucreamed--;
  46.  
  47.      /* Dissipate spell-based protection. */
  48.      if (u.usptime) {
  49.          if (--u.usptime == 0 && u.uspellprot) {
  50.              u.usptime = u.uspmtime;
  51.              u.uspellprot--;
  52.              find_ac();
  53.              if (!Blind)
  54.                  Norep("The %s haze around you %s.", hcolor(NH_GOLDEN),
  55.                        u.uspellprot ? "becomes less dense" : "disappears");
  56.          }
  57.      }
  58.  
  59.      if (u.ugallop) {
  60.          if (--u.ugallop == 0L && u.usteed)
  61.              pline("%s stops galloping.", Monnam(u.usteed));
  62.      }
  63.  
  64.      for (upp = u.uprops; upp < u.uprops + SIZE(u.uprops); upp++)
  65.          if ((upp->intrinsic & TIMEOUT) && !(--upp->intrinsic & TIMEOUT)) {
  66.              kptr = find_delayed_killer((int) (upp - u.uprops));
  67.              switch (upp - u.uprops) {
  68.              case STONED:
  69.                  if (kptr && kptr->name[0]) {
  70.                      killer.format = kptr->format;
  71.                      Strcpy(killer.name, kptr->name);
  72.                  } else {
  73.                      killer.format = NO_KILLER_PREFIX;
  74.                      Strcpy(killer.name, "killed by petrification");
  75.                  }
  76.                  dealloc_killer(kptr);
  77.                  /* (unlike sliming, you aren't changing form here) */
  78.                  done(STONING);
  79.                  break;
  80.              case SLIMED:
  81.                  if (kptr && kptr->name[0]) {
  82.                      killer.format = kptr->format;
  83.                      Strcpy(killer.name, kptr->name);
  84.                  } else {
  85.                      killer.format = NO_KILLER_PREFIX;
  86.                      Strcpy(killer.name, "turned into green slime");
  87.                  }
  88.                  dealloc_killer(kptr);
  89.                  /* involuntarily break "never changed form" conduct */
  90.                  u.uconduct.polyselfs++;
  91.                  done(TURNED_SLIME);
  92.                  break;
  93.              case VOMITING:
  94.                  make_vomiting(0L, TRUE);
  95.                  break;
  96.              case SICK:
  97.                  You("die from your illness.");
  98.                  if (kptr && kptr->name[0]) {
  99.                      killer.format = kptr->format;
  100.                      Strcpy(killer.name, kptr->name);
  101.                  } else {
  102.                      killer.format = KILLED_BY_AN;
  103.                      killer.name[0] = 0; /* take the default */
  104.                  }
  105.                  dealloc_killer(kptr);
  106.  
  107.                  if ((m_idx = name_to_mon(killer.name)) >= LOW_PM) {
  108.                      if (type_is_pname(&mons[m_idx])) {
  109.                          killer.format = KILLED_BY;
  110.                      } else if (mons[m_idx].geno & G_UNIQ) {
  111.                          Strcpy(killer.name, the(killer.name));
  112.                          killer.format = KILLED_BY;
  113.                      }
  114.                  }
  115.                  u.usick_type = 0;
  116.                  done(POISONING);
  117.                  break;
  118.              case FAST:
  119.                  if (!Very_fast)
  120.                      You_feel("yourself slowing down%s.",
  121.                               Fast ? " a bit" : "");
  122.                  break;
  123.              case CONFUSION:
  124.                  /* So make_confused works properly */
  125.                  set_itimeout(&HConfusion, 1L);
  126.                  make_confused(0L, TRUE);
  127.                  if (!Confusion)
  128.                      stop_occupation();
  129.                  break;
  130.              case STUNNED:
  131.                  set_itimeout(&HStun, 1L);
  132.                  make_stunned(0L, TRUE);
  133.                  if (!Stunned)
  134.                      stop_occupation();
  135.                  break;
  136.              case BLINDED:
  137.                  set_itimeout(&Blinded, 1L);
  138.                  make_blinded(0L, TRUE);
  139.                  if (!Blind)
  140.                      stop_occupation();
  141.                  break;
  142.              case DEAF:
  143.                  set_itimeout(&HDeaf, 1L);
  144.                  make_deaf(0L, TRUE);
  145.                  if (!Deaf)
  146.                      stop_occupation();
  147.                  break;
  148.              case INVIS:
  149.                  newsym(u.ux, u.uy);
  150.                  if (!Invis && !BInvis && !Blind) {
  151.                      You(!See_invisible
  152.                              ? "are no longer invisible."
  153.                              : "can no longer see through yourself.");
  154.                      stop_occupation();
  155.                  }
  156.                  break;
  157.              case SEE_INVIS:
  158.                  set_mimic_blocking(); /* do special mimic handling */
  159.                  see_monsters();       /* make invis mons appear */
  160.                  newsym(u.ux, u.uy);   /* make self appear */
  161.                  stop_occupation();
  162.                  break;
  163.              case WOUNDED_LEGS:
  164.                  heal_legs();
  165.                  stop_occupation();
  166.                  break;
  167.              case HALLUC:
  168.                  set_itimeout(&HHallucination, 1L);
  169.                  (void) make_hallucinated(0L, TRUE, 0L);
  170.                  if (!Hallucination)
  171.                      stop_occupation();
  172.                  break;
  173.              case SLEEPY:
  174.                  if (unconscious() || Sleep_resistance) {
  175.                      incr_itimeout(&HSleepy, rnd(100));
  176.                  } else if (Sleepy) {
  177.                      You("fall asleep.");
  178.                      sleeptime = rnd(20);
  179.                      fall_asleep(-sleeptime, TRUE);
  180.                      incr_itimeout(&HSleepy, sleeptime + rnd(100));
  181.                  }
  182.                  break;
  183.              case LEVITATION:
  184.                  (void) float_down(I_SPECIAL | TIMEOUT, 0L);
  185.                  break;
  186.              case STRANGLED:
  187.                  killer.format = KILLED_BY;
  188.                  Strcpy(killer.name,
  189.                         (u.uburied) ? "suffocation" : "strangulation");
  190.                  done(DIED);
  191.                  /* must be declining to die in explore|wizard mode;
  192.                     treat like being cured of strangulation by prayer */
  193.                  if (uamul && uamul->otyp == AMULET_OF_STRANGULATION) {
  194.                      Your("amulet vanishes!");
  195.                      useup(uamul);
  196.                  }
  197.                  break;
  198.              case FUMBLING:
  199.                  /* call this only when a move took place.  */
  200.                  /* otherwise handle fumbling msgs locally. */
  201.                  if (u.umoved && !Levitation) {
  202.                      slip_or_trip();
  203.                      nomul(-2);
  204.                      multi_reason = "fumbling";
  205.                      nomovemsg = "";
  206.                      /* The more you are carrying the more likely you
  207.                       * are to make noise when you fumble.  Adjustments
  208.                       * to this number must be thoroughly play tested.
  209.                       */
  210.                      if ((inv_weight() > -500)) {
  211.                          You("make a lot of noise!");
  212.                          wake_nearby();
  213.                      }
  214.                  }
  215.                  /* from outside means slippery ice; don't reset
  216.                     counter if that's the only fumble reason */
  217.                  HFumbling &= ~FROMOUTSIDE;
  218.                  if (Fumbling)
  219.                      incr_itimeout(&HFumbling, rnd(20));
  220.                  break;
  221.              case DETECT_MONSTERS:
  222.                  see_monsters();
  223.                  break;
  224.              }
  225.          }
  226.  
  227.      run_timers();
  228.  }
  229.  

fall_asleep

  1.  void
  2.  fall_asleep(how_long, wakeup_msg)
  3.  int how_long;
  4.  boolean wakeup_msg;
  5.  {
  6.      stop_occupation();
  7.      nomul(how_long);
  8.      multi_reason = "sleeping";
  9.      /* generally don't notice sounds while sleeping */
  10.      if (wakeup_msg && multi == how_long) {
  11.          /* caller can follow with a direct call to Hear_again() if
  12.             there's a need to override this when wakeup_msg is true */
  13.          incr_itimeout(&HDeaf, how_long);
  14.          afternmv = Hear_again; /* this won't give any messages */
  15.      }
  16.      /* early wakeup from combat won't be possible until next monster turn */
  17.      u.usleep = monstermoves;
  18.      nomovemsg = wakeup_msg ? "You wake up." : You_can_move_again;
  19.  }
  20.  

attach_egg_hatch_timeout

  1.  /* Attach an egg hatch timeout to the given egg.
  2.   *      when = Time to hatch, usually only passed if re-creating an
  3.   *             existing hatch timer. Pass 0L for random hatch time.
  4.   */
  5.  void
  6.  attach_egg_hatch_timeout(egg, when)
  7.  struct obj *egg;
  8.  long when;
  9.  {
  10.      int i;
  11.  
  12.      /* stop previous timer, if any */
  13.      (void) stop_timer(HATCH_EGG, obj_to_any(egg));
  14.  
  15.      /*
  16.       * Decide if and when to hatch the egg.  The old hatch_it() code tried
  17.       * once a turn from age 151 to 200 (inclusive), hatching if it rolled
  18.       * a number x, 1<=x<=age, where x>150.  This yields a chance of
  19.       * hatching > 99.9993%.  Mimic that here.
  20.       */
  21.      if (!when) {
  22.          for (i = (MAX_EGG_HATCH_TIME - 50) + 1; i <= MAX_EGG_HATCH_TIME; i++)
  23.              if (rnd(i) > 150) {
  24.                  /* egg will hatch */
  25.                  when = (long) i;
  26.                  break;
  27.              }
  28.      }
  29.      if (when) {
  30.          (void) start_timer(when, TIMER_OBJECT, HATCH_EGG, obj_to_any(egg));
  31.      }
  32.  }
  33.  

kill_egg

  1.  /* prevent an egg from ever hatching */
  2.  void
  3.  kill_egg(egg)
  4.  struct obj *egg;
  5.  {
  6.      /* stop previous timer, if any */
  7.      (void) stop_timer(HATCH_EGG, obj_to_any(egg));
  8.  }
  9.  

hatch_egg

  1.  /* timer callback routine: hatch the given egg */
  2.  void
  3.  hatch_egg(arg, timeout)
  4.  anything *arg;
  5.  long timeout;
  6.  {
  7.      struct obj *egg;
  8.      struct monst *mon, *mon2;
  9.      coord cc;
  10.      xchar x, y;
  11.      boolean yours, silent, knows_egg = FALSE;
  12.      boolean cansee_hatchspot = FALSE;
  13.      int i, mnum, hatchcount = 0;
  14.  
  15.      egg = arg->a_obj;
  16.      /* sterilized while waiting */
  17.      if (egg->corpsenm == NON_PM)
  18.          return;
  19.  
  20.      mon = mon2 = (struct monst *) 0;
  21.      mnum = big_to_little(egg->corpsenm);
  22.      /* The identity of one's father is learned, not innate */
  23.      yours = (egg->spe || (!flags.female && carried(egg) && !rn2(2)));
  24.      silent = (timeout != monstermoves); /* hatched while away */
  25.  
  26.      /* only can hatch when in INVENT, FLOOR, MINVENT */
  27.      if (get_obj_location(egg, &x, &y, 0)) {
  28.          hatchcount = rnd((int) egg->quan);
  29.          cansee_hatchspot = cansee(x, y) && !silent;
  30.          if (!(mons[mnum].geno & G_UNIQ)
  31.              && !(mvitals[mnum].mvflags & (G_GENOD | G_EXTINCT))) {
  32.              for (i = hatchcount; i > 0; i--) {
  33.                  if (!enexto(&cc, x, y, &mons[mnum])
  34.                      || !(mon = makemon(&mons[mnum], cc.x, cc.y, NO_MINVENT)))
  35.                      break;
  36.                  /* tame if your own egg hatches while you're on the
  37.                     same dungeon level, or any dragon egg which hatches
  38.                     while it's in your inventory */
  39.                  if ((yours && !silent)
  40.                      || (carried(egg) && mon->data->mlet == S_DRAGON)) {
  41.                      if (tamedog(mon, (struct obj *) 0)) {
  42.                          if (carried(egg) && mon->data->mlet != S_DRAGON)
  43.                              mon->mtame = 20;
  44.                      }
  45.                  }
  46.                  if (mvitals[mnum].mvflags & G_EXTINCT)
  47.                      break;  /* just made last one */
  48.                  mon2 = mon; /* in case makemon() fails on 2nd egg */
  49.              }
  50.              if (!mon)
  51.                  mon = mon2;
  52.              hatchcount -= i;
  53.              egg->quan -= (long) hatchcount;
  54.          }
  55.  #if 0
  56.      /*
  57.       * We could possibly hatch while migrating, but the code isn't
  58.       * set up for it...
  59.       */
  60.      } else if (obj->where == OBJ_MIGRATING) {
  61.          /*
  62.           * We can do several things.  The first ones that come to
  63.           * mind are:
  64.           * + Create the hatched monster then place it on the migrating
  65.           *   mons list.  This is tough because all makemon() is made
  66.           *   to place the monster as well.  Makemon() also doesn't lend
  67.           *   itself well to splitting off a "not yet placed" subroutine.
  68.           * + Mark the egg as hatched, then place the monster when we
  69.           *   place the migrating objects.
  70.           * + Or just kill any egg which gets sent to another level.
  71.           *   Falling is the usual reason such transportation occurs.
  72.           */
  73.          cansee_hatchspot = FALSE;
  74.          mon = ???;
  75.  #endif
  76.      }
  77.  
  78.      if (mon) {
  79.          char monnambuf[BUFSZ], carriedby[BUFSZ];
  80.          boolean siblings = (hatchcount > 1), redraw = FALSE;
  81.  
  82.          if (cansee_hatchspot) {
  83.              Sprintf(monnambuf, "%s%s", siblings ? "some " : "",
  84.                      siblings ? makeplural(m_monnam(mon)) : an(m_monnam(mon)));
  85.              /* we don't learn the egg type here because learning
  86.                 an egg type requires either seeing the egg hatch
  87.                 or being familiar with the egg already,
  88.                 as well as being able to see the resulting
  89.                 monster, checked below
  90.              */
  91.          }
  92.          switch (egg->where) {
  93.          case OBJ_INVENT:
  94.              knows_egg = TRUE; /* true even if you are blind */
  95.              if (!cansee_hatchspot)
  96.                  You_feel("%s %s from your pack!", something,
  97.                           locomotion(mon->data, "drop"));
  98.              else
  99.                  You_see("%s %s out of your pack!", monnambuf,
  100.                          locomotion(mon->data, "drop"));
  101.              if (yours) {
  102.                  pline("%s cries sound like \"%s%s\"",
  103.                        siblings ? "Their" : "Its",
  104.                        flags.female ? "mommy" : "daddy", egg->spe ? "." : "?");
  105.              } else if (mon->data->mlet == S_DRAGON && !Deaf) {
  106.                  verbalize("Gleep!"); /* Mything eggs :-) */
  107.              }
  108.              break;
  109.  
  110.          case OBJ_FLOOR:
  111.              if (cansee_hatchspot) {
  112.                  knows_egg = TRUE;
  113.                  You_see("%s hatch.", monnambuf);
  114.                  redraw = TRUE; /* update egg's map location */
  115.              }
  116.              break;
  117.  
  118.          case OBJ_MINVENT:
  119.              if (cansee_hatchspot) {
  120.                  /* egg carrying monster might be invisible */
  121.                  if (canseemon(egg->ocarry)) {
  122.                      Sprintf(carriedby, "%s pack",
  123.                              s_suffix(a_monnam(egg->ocarry)));
  124.                      knows_egg = TRUE;
  125.                  } else if (is_pool(mon->mx, mon->my))
  126.                      Strcpy(carriedby, "empty water");
  127.                  else
  128.                      Strcpy(carriedby, "thin air");
  129.                  You_see("%s %s out of %s!", monnambuf,
  130.                          locomotion(mon->data, "drop"), carriedby);
  131.              }
  132.              break;
  133.  #if 0
  134.          case OBJ_MIGRATING:
  135.              break;
  136.  #endif
  137.          default:
  138.              impossible("egg hatched where? (%d)", (int) egg->where);
  139.              break;
  140.          }
  141.  
  142.          if (cansee_hatchspot && knows_egg)
  143.              learn_egg_type(mnum);
  144.  
  145.          if (egg->quan > 0) {
  146.              /* still some eggs left */
  147.              /* Instead of ordinary egg timeout use a short one */
  148.              attach_egg_hatch_timeout(egg, (long) rnd(12));
  149.          } else if (carried(egg)) {
  150.              useup(egg);
  151.          } else {
  152.              /* free egg here because we use it above */
  153.              obj_extract_self(egg);
  154.              obfree(egg, (struct obj *) 0);
  155.          }
  156.          if (redraw)
  157.              newsym(x, y);
  158.      }
  159.  }
  160.  

learn_egg_type

  1.  /* Learn to recognize eggs of the given type. */
  2.  void
  3.  learn_egg_type(mnum)
  4.  int mnum;
  5.  {
  6.      /* baby monsters hatch from grown-up eggs */
  7.      mnum = little_to_big(mnum);
  8.      mvitals[mnum].mvflags |= MV_KNOWS_EGG;
  9.      /* we might have just learned about other eggs being carried */
  10.      update_inventory();
  11.  }
  12.  

attach_fig_transform_timeout

  1.  /* Attach a fig_transform timeout to the given figurine. */
  2.  void
  3.  attach_fig_transform_timeout(figurine)
  4.  struct obj *figurine;
  5.  {
  6.      int i;
  7.  
  8.      /* stop previous timer, if any */
  9.      (void) stop_timer(FIG_TRANSFORM, obj_to_any(figurine));
  10.  
  11.      /*
  12.       * Decide when to transform the figurine.
  13.       */
  14.      i = rnd(9000) + 200;
  15.      /* figurine will transform */
  16.      (void) start_timer((long) i, TIMER_OBJECT, FIG_TRANSFORM,
  17.                         obj_to_any(figurine));
  18.  }
  19.  

slip_or_trip

  1.  /* give a fumble message */
  2.  STATIC_OVL void
  3.  slip_or_trip()
  4.  {
  5.      struct obj *otmp = vobj_at(u.ux, u.uy), *otmp2;
  6.      const char *what;
  7.      char buf[BUFSZ];
  8.      boolean on_foot = TRUE;
  9.      if (u.usteed)
  10.          on_foot = FALSE;
  11.  
  12.      if (otmp && on_foot && !u.uinwater && is_pool(u.ux, u.uy))
  13.          otmp = 0;
  14.  
  15.      if (otmp && on_foot) { /* trip over something in particular */
  16.          /*
  17.            If there is only one item, it will have just been named
  18.            during the move, so refer to by via pronoun; otherwise,
  19.            if the top item has been or can be seen, refer to it by
  20.            name; if not, look for rocks to trip over; trip over
  21.            anonymous "something" if there aren't any rocks.
  22.          */
  23.          what = (iflags.last_msg == PLNMSG_ONE_ITEM_HERE)
  24.                  ? ((otmp->quan == 1L) ? "it"
  25.                        : Hallucination ? "they" : "them")
  26.                  : (otmp->dknown || !Blind)
  27.                        ? doname(otmp)
  28.                        : ((otmp2 = sobj_at(ROCK, u.ux, u.uy)) == 0
  29.                               ? something
  30.                               : (otmp2->quan == 1L ? "a rock" : "some rocks"));
  31.          if (Hallucination) {
  32.              what = strcpy(buf, what);
  33.              buf[0] = highc(buf[0]);
  34.              pline("Egads!  %s bite%s your %s!", what,
  35.                    (!otmp || otmp->quan == 1L) ? "s" : "", body_part(FOOT));
  36.          } else {
  37.              You("trip over %s.", what);
  38.          }
  39.          if (!uarmf && otmp->otyp == CORPSE
  40.              && touch_petrifies(&mons[otmp->corpsenm]) && !Stone_resistance) {
  41.              Sprintf(killer.name, "tripping over %s corpse",
  42.                      an(mons[otmp->corpsenm].mname));
  43.              instapetrify(killer.name);
  44.          }
  45.      } else if (rn2(3) && is_ice(u.ux, u.uy)) {
  46.          pline("%s %s%s on the ice.",
  47.                u.usteed ? upstart(x_monnam(u.usteed,
  48.                                            (has_mname(u.usteed)) ? ARTICLE_NONE
  49.                                                                  : ARTICLE_THE,
  50.                                            (char *) 0, SUPPRESS_SADDLE, FALSE))
  51.                         : "You",
  52.                rn2(2) ? "slip" : "slide", on_foot ? "" : "s");
  53.      } else {
  54.          if (on_foot) {
  55.              switch (rn2(4)) {
  56.              case 1:
  57.                  You("trip over your own %s.",
  58.                      Hallucination ? "elbow" : makeplural(body_part(FOOT)));
  59.                  break;
  60.              case 2:
  61.                  You("slip %s.",
  62.                      Hallucination ? "on a banana peel" : "and nearly fall");
  63.                  break;
  64.              case 3:
  65.                  You("flounder.");
  66.                  break;
  67.              default:
  68.                  You("stumble.");
  69.                  break;
  70.              }
  71.          } else {
  72.              switch (rn2(4)) {
  73.              case 1:
  74.                  Your("%s slip out of the stirrups.",
  75.                       makeplural(body_part(FOOT)));
  76.                  break;
  77.              case 2:
  78.                  You("let go of the reins.");
  79.                  break;
  80.              case 3:
  81.                  You("bang into the saddle-horn.");
  82.                  break;
  83.              default:
  84.                  You("slide to one side of the saddle.");
  85.                  break;
  86.              }
  87.              dismount_steed(DISMOUNT_FELL);
  88.          }
  89.      }
  90.  }
  91.  

see_lamp_flicker

  1.  /* Print a lamp flicker message with tailer. */
  2.  STATIC_OVL void
  3.  see_lamp_flicker(obj, tailer)
  4.  struct obj *obj;
  5.  const char *tailer;
  6.  {
  7.      switch (obj->where) {
  8.      case OBJ_INVENT:
  9.      case OBJ_MINVENT:
  10.          pline("%s flickers%s.", Yname2(obj), tailer);
  11.          break;
  12.      case OBJ_FLOOR:
  13.          You_see("%s flicker%s.", an(xname(obj)), tailer);
  14.          break;
  15.      }
  16.  }
  17.  

lantern_message

  1.  /* Print a dimming message for brass lanterns. */
  2.  STATIC_OVL void
  3.  lantern_message(obj)
  4.  struct obj *obj;
  5.  {
  6.      /* from adventure */
  7.      switch (obj->where) {
  8.      case OBJ_INVENT:
  9.          Your("lantern is getting dim.");
  10.          if (Hallucination)
  11.              pline("Batteries have not been invented yet.");
  12.          break;
  13.      case OBJ_FLOOR:
  14.          You_see("a lantern getting dim.");
  15.          break;
  16.      case OBJ_MINVENT:
  17.          pline("%s lantern is getting dim.", s_suffix(Monnam(obj->ocarry)));
  18.          break;
  19.      }
  20.  }
  21.  

burn_object

  1.  /*
  2.   * Timeout callback for for objects that are burning. E.g. lamps, candles.
  3.   * See begin_burn() for meanings of obj->age and obj->spe.
  4.   */
  5.  void
  6.  burn_object(arg, timeout)
  7.  anything *arg;
  8.  long timeout;
  9.  {
  10.      struct obj *obj = arg->a_obj;
  11.      boolean canseeit, many, menorah, need_newsym;
  12.      xchar x, y;
  13.      char whose[BUFSZ];
  14.  
  15.      menorah = obj->otyp == CANDELABRUM_OF_INVOCATION;
  16.      many = menorah ? obj->spe > 1 : obj->quan > 1L;
  17.  
  18.      /* timeout while away */
  19.      if (timeout != monstermoves) {
  20.          long how_long = monstermoves - timeout;
  21.  
  22.          if (how_long >= obj->age) {
  23.              obj->age = 0;
  24.              end_burn(obj, FALSE);
  25.  
  26.              if (menorah) {
  27.                  obj->spe = 0; /* no more candles */
  28.              } else if (Is_candle(obj) || obj->otyp == POT_OIL) {
  29.                  /* get rid of candles and burning oil potions;
  30.                     we know this object isn't carried by hero,
  31.                     nor is it migrating */
  32.                  obj_extract_self(obj);
  33.                  obfree(obj, (struct obj *) 0);
  34.                  obj = (struct obj *) 0;
  35.              }
  36.  
  37.          } else {
  38.              obj->age -= how_long;
  39.              begin_burn(obj, TRUE);
  40.          }
  41.          return;
  42.      }
  43.  
  44.      /* only interested in INVENT, FLOOR, and MINVENT */
  45.      if (get_obj_location(obj, &x, &y, 0)) {
  46.          canseeit = !Blind && cansee(x, y);
  47.          /* set `whose[]' to be "Your " or "Fred's " or "The goblin's " */
  48.          (void) Shk_Your(whose, obj);
  49.      } else {
  50.          canseeit = FALSE;
  51.      }
  52.      need_newsym = FALSE;
  53.  
  54.      /* obj->age is the age remaining at this point.  */
  55.      switch (obj->otyp) {
  56.      case POT_OIL:
  57.          /* this should only be called when we run out */
  58.          if (canseeit) {
  59.              switch (obj->where) {
  60.              case OBJ_INVENT:
  61.              case OBJ_MINVENT:
  62.                  pline("%spotion of oil has burnt away.", whose);
  63.                  break;
  64.              case OBJ_FLOOR:
  65.                  You_see("a burning potion of oil go out.");
  66.                  need_newsym = TRUE;
  67.                  break;
  68.              }
  69.          }
  70.          end_burn(obj, FALSE); /* turn off light source */
  71.          if (carried(obj)) {
  72.              useupall(obj);
  73.          } else {
  74.              /* clear migrating obj's destination code before obfree
  75.                 to avoid false complaint of deleting worn item */
  76.              if (obj->where == OBJ_MIGRATING)
  77.                  obj->owornmask = 0L;
  78.              obj_extract_self(obj);
  79.              obfree(obj, (struct obj *) 0);
  80.          }
  81.          obj = (struct obj *) 0;
  82.          break;
  83.  
  84.      case BRASS_LANTERN:
  85.      case OIL_LAMP:
  86.          switch ((int) obj->age) {
  87.          case 150:
  88.          case 100:
  89.          case 50:
  90.              if (canseeit) {
  91.                  if (obj->otyp == BRASS_LANTERN)
  92.                      lantern_message(obj);
  93.                  else
  94.                      see_lamp_flicker(obj,
  95.                                       obj->age == 50L ? " considerably" : "");
  96.              }
  97.              break;
  98.  
  99.          case 25:
  100.              if (canseeit) {
  101.                  if (obj->otyp == BRASS_LANTERN)
  102.                      lantern_message(obj);
  103.                  else {
  104.                      switch (obj->where) {
  105.                      case OBJ_INVENT:
  106.                      case OBJ_MINVENT:
  107.                          pline("%s seems about to go out.", Yname2(obj));
  108.                          break;
  109.                      case OBJ_FLOOR:
  110.                          You_see("%s about to go out.", an(xname(obj)));
  111.                          break;
  112.                      }
  113.                  }
  114.              }
  115.              break;
  116.  
  117.          case 0:
  118.              /* even if blind you'll know if holding it */
  119.              if (canseeit || obj->where == OBJ_INVENT) {
  120.                  switch (obj->where) {
  121.                  case OBJ_INVENT:
  122.                  case OBJ_MINVENT:
  123.                      if (obj->otyp == BRASS_LANTERN)
  124.                          pline("%slantern has run out of power.", whose);
  125.                      else
  126.                          pline("%s has gone out.", Yname2(obj));
  127.                      break;
  128.                  case OBJ_FLOOR:
  129.                      if (obj->otyp == BRASS_LANTERN)
  130.                          You_see("a lantern run out of power.");
  131.                      else
  132.                          You_see("%s go out.", an(xname(obj)));
  133.                      break;
  134.                  }
  135.              }
  136.              end_burn(obj, FALSE);
  137.              break;
  138.  
  139.          default:
  140.              /*
  141.               * Someone added fuel to the lamp while it was
  142.               * lit.  Just fall through and let begin burn
  143.               * handle the new age.
  144.               */
  145.              break;
  146.          }
  147.  
  148.          if (obj->age)
  149.              begin_burn(obj, TRUE);
  150.  
  151.          break;
  152.  
  153.      case CANDELABRUM_OF_INVOCATION:
  154.      case TALLOW_CANDLE:
  155.      case WAX_CANDLE:
  156.          switch (obj->age) {
  157.          case 75:
  158.              if (canseeit)
  159.                  switch (obj->where) {
  160.                  case OBJ_INVENT:
  161.                  case OBJ_MINVENT:
  162.                      pline("%s%scandle%s getting short.", whose,
  163.                            menorah ? "candelabrum's " : "",
  164.                            many ? "s are" : " is");
  165.                      break;
  166.                  case OBJ_FLOOR:
  167.                      You_see("%scandle%s getting short.",
  168.                              menorah ? "a candelabrum's " : many ? "some "
  169.                                                                  : "a ",
  170.                              many ? "s" : "");
  171.                      break;
  172.                  }
  173.              break;
  174.  
  175.          case 15:
  176.              if (canseeit)
  177.                  switch (obj->where) {
  178.                  case OBJ_INVENT:
  179.                  case OBJ_MINVENT:
  180.                      pline("%s%scandle%s flame%s flicker%s low!", whose,
  181.                            menorah ? "candelabrum's " : "", many ? "s'" : "'s",
  182.                            many ? "s" : "", many ? "" : "s");
  183.                      break;
  184.                  case OBJ_FLOOR:
  185.                      You_see("%scandle%s flame%s flicker low!",
  186.                              menorah ? "a candelabrum's " : many ? "some "
  187.                                                                  : "a ",
  188.                              many ? "s'" : "'s", many ? "s" : "");
  189.                      break;
  190.                  }
  191.              break;
  192.  
  193.          case 0:
  194.              /* we know even if blind and in our inventory */
  195.              if (canseeit || obj->where == OBJ_INVENT) {
  196.                  if (menorah) {
  197.                      switch (obj->where) {
  198.                      case OBJ_INVENT:
  199.                      case OBJ_MINVENT:
  200.                          pline("%scandelabrum's flame%s.", whose,
  201.                                many ? "s die" : " dies");
  202.                          break;
  203.                      case OBJ_FLOOR:
  204.                          You_see("a candelabrum's flame%s die.",
  205.                                  many ? "s" : "");
  206.                          break;
  207.                      }
  208.                  } else {
  209.                      switch (obj->where) {
  210.                      case OBJ_INVENT:
  211.                      case OBJ_MINVENT:
  212.                          pline("%s %s consumed!", Yname2(obj),
  213.                                many ? "are" : "is");
  214.                          break;
  215.                      case OBJ_FLOOR:
  216.                          /*
  217.                          You see some wax candles consumed!
  218.                          You see a wax candle consumed!
  219.                          */
  220.                          You_see("%s%s consumed!", many ? "some " : "",
  221.                                  many ? xname(obj) : an(xname(obj)));
  222.                          need_newsym = TRUE;
  223.                          break;
  224.                      }
  225.  
  226.                      /* post message */
  227.                      pline(Hallucination
  228.                                ? (many ? "They shriek!" : "It shrieks!")
  229.                                : Blind ? "" : (many ? "Their flames die."
  230.                                                     : "Its flame dies."));
  231.                  }
  232.              }
  233.              end_burn(obj, FALSE);
  234.  
  235.              if (menorah) {
  236.                  obj->spe = 0;
  237.              } else {
  238.                  if (carried(obj)) {
  239.                      useupall(obj);
  240.                  } else {
  241.                      /* clear migrating obj's destination code
  242.                         so obfree won't think this item is worn */
  243.                      if (obj->where == OBJ_MIGRATING)
  244.                          obj->owornmask = 0L;
  245.                      obj_extract_self(obj);
  246.                      obfree(obj, (struct obj *) 0);
  247.                  }
  248.                  obj = (struct obj *) 0;
  249.              }
  250.              break;
  251.  
  252.          default:
  253.              /*
  254.               * Someone added fuel (candles) to the menorah while
  255.               * it was lit.  Just fall through and let begin burn
  256.               * handle the new age.
  257.               */
  258.              break;
  259.          }
  260.  
  261.          if (obj && obj->age)
  262.              begin_burn(obj, TRUE);
  263.  
  264.          break;
  265.  
  266.      default:
  267.          impossible("burn_object: unexpeced obj %s", xname(obj));
  268.          break;
  269.      }
  270.      if (need_newsym)
  271.          newsym(x, y);
  272.  }
  273.  

begin_burn

  1.  /*
  2.   * Start a burn timeout on the given object. If not "already lit" then
  3.   * create a light source for the vision system.  There had better not
  4.   * be a burn already running on the object.
  5.   *
  6.   * Magic lamps stay lit as long as there's a genie inside, so don't start
  7.   * a timer.
  8.   *
  9.   * Burn rules:
  10.   *      potions of oil, lamps & candles:
  11.   *              age = # of turns of fuel left
  12.   *              spe = <unused>
  13.   *      magic lamps:
  14.   *              age = <unused>
  15.   *              spe = 0 not lightable, 1 lightable forever
  16.   *      candelabrum:
  17.   *              age = # of turns of fuel left
  18.   *              spe = # of candles
  19.   *
  20.   * Once the burn begins, the age will be set to the amount of fuel
  21.   * remaining _once_the_burn_finishes_.  If the burn is terminated
  22.   * early then fuel is added back.
  23.   *
  24.   * This use of age differs from the use of age for corpses and eggs.
  25.   * For the latter items, age is when the object was created, so we
  26.   * know when it becomes "bad".
  27.   *
  28.   * This is a "silent" routine - it should not print anything out.
  29.   */
  30.  void
  31.  begin_burn(obj, already_lit)
  32.  struct obj *obj;
  33.  boolean already_lit;
  34.  {
  35.      int radius = 3;
  36.      long turns = 0;
  37.      boolean do_timer = TRUE;
  38.  
  39.      if (obj->age == 0 && obj->otyp != MAGIC_LAMP && !artifact_light(obj))
  40.          return;
  41.  
  42.      switch (obj->otyp) {
  43.      case MAGIC_LAMP:
  44.          obj->lamplit = 1;
  45.          do_timer = FALSE;
  46.          break;
  47.  
  48.      case POT_OIL:
  49.          turns = obj->age;
  50.          radius = 1; /* very dim light */
  51.          break;
  52.  
  53.      case BRASS_LANTERN:
  54.      case OIL_LAMP:
  55.          /* magic times are 150, 100, 50, 25, and 0 */
  56.          if (obj->age > 150L)
  57.              turns = obj->age - 150L;
  58.          else if (obj->age > 100L)
  59.              turns = obj->age - 100L;
  60.          else if (obj->age > 50L)
  61.              turns = obj->age - 50L;
  62.          else if (obj->age > 25L)
  63.              turns = obj->age - 25L;
  64.          else
  65.              turns = obj->age;
  66.          break;
  67.  
  68.      case CANDELABRUM_OF_INVOCATION:
  69.      case TALLOW_CANDLE:
  70.      case WAX_CANDLE:
  71.          /* magic times are 75, 15, and 0 */
  72.          if (obj->age > 75L)
  73.              turns = obj->age - 75L;
  74.          else if (obj->age > 15L)
  75.              turns = obj->age - 15L;
  76.          else
  77.              turns = obj->age;
  78.          radius = candle_light_range(obj);
  79.          break;
  80.  
  81.      default:
  82.          /* [ALI] Support artifact light sources */
  83.          if (artifact_light(obj)) {
  84.              obj->lamplit = 1;
  85.              do_timer = FALSE;
  86.              radius = arti_light_radius(obj);
  87.          } else {
  88.              impossible("begin burn: unexpected %s", xname(obj));
  89.              turns = obj->age;
  90.          }
  91.          break;
  92.      }
  93.  
  94.      if (do_timer) {
  95.          if (start_timer(turns, TIMER_OBJECT, BURN_OBJECT, obj_to_any(obj))) {
  96.              obj->lamplit = 1;
  97.              obj->age -= turns;
  98.              if (carried(obj) && !already_lit)
  99.                  update_inventory();
  100.          } else {
  101.              obj->lamplit = 0;
  102.          }
  103.      } else {
  104.          if (carried(obj) && !already_lit)
  105.              update_inventory();
  106.      }
  107.  
  108.      if (obj->lamplit && !already_lit) {
  109.          xchar x, y;
  110.  
  111.          if (get_obj_location(obj, &x, &y, CONTAINED_TOO | BURIED_TOO))
  112.              new_light_source(x, y, radius, LS_OBJECT, obj_to_any(obj));
  113.          else
  114.              impossible("begin_burn: can't get obj position");
  115.      }
  116.  }
  117.  

end_burn

  1.  /*
  2.   * Stop a burn timeout on the given object if timer attached.  Darken
  3.   * light source.
  4.   */
  5.  void
  6.  end_burn(obj, timer_attached)
  7.  struct obj *obj;
  8.  boolean timer_attached;
  9.  {
  10.      if (!obj->lamplit) {
  11.          impossible("end_burn: obj %s not lit", xname(obj));
  12.          return;
  13.      }
  14.  
  15.      if (obj->otyp == MAGIC_LAMP || artifact_light(obj))
  16.          timer_attached = FALSE;
  17.  
  18.      if (!timer_attached) {
  19.          /* [DS] Cleanup explicitly, since timer cleanup won't happen */
  20.          del_light_source(LS_OBJECT, obj_to_any(obj));
  21.          obj->lamplit = 0;
  22.          if (obj->where == OBJ_INVENT)
  23.              update_inventory();
  24.      } else if (!stop_timer(BURN_OBJECT, obj_to_any(obj)))
  25.          impossible("end_burn: obj %s not timed!", xname(obj));
  26.  }
  27.  

cleanup_burn

  1.  /*
  2.   * Cleanup a burning object if timer stopped.
  3.   */
  4.  static void
  5.  cleanup_burn(arg, expire_time)
  6.  anything *arg;
  7.  long expire_time;
  8.  {
  9.      struct obj *obj = arg->a_obj;
  10.      if (!obj->lamplit) {
  11.          impossible("cleanup_burn: obj %s not lit", xname(obj));
  12.          return;
  13.      }
  14.  
  15.      del_light_source(LS_OBJECT, obj_to_any(obj));
  16.  
  17.      /* restore unused time */
  18.      obj->age += expire_time - monstermoves;
  19.  
  20.      obj->lamplit = 0;
  21.  
  22.      if (obj->where == OBJ_INVENT)
  23.          update_inventory();
  24.  }
  25.  

do_storms

  1.  void
  2.  do_storms()
  3.  {
  4.      int nstrike;
  5.      register int x, y;
  6.      int dirx, diry;
  7.      int count;
  8.  
  9.      /* no lightning if not the air level or too often, even then */
  10.      if (!Is_airlevel(&u.uz) || rn2(8))
  11.          return;
  12.  
  13.      /* the number of strikes is 8-log2(nstrike) */
  14.      for (nstrike = rnd(64); nstrike <= 64; nstrike *= 2) {
  15.          count = 0;
  16.          do {
  17.              x = rnd(COLNO - 1);
  18.              y = rn2(ROWNO);
  19.          } while (++count < 100 && levl[x][y].typ != CLOUD);
  20.  
  21.          if (count < 100) {
  22.              dirx = rn2(3) - 1;
  23.              diry = rn2(3) - 1;
  24.              if (dirx != 0 || diry != 0)
  25.                  buzz(-15, /* "monster" LIGHTNING spell */
  26.                       8, x, y, dirx, diry);
  27.          }
  28.      }
  29.  
  30.      if (levl[u.ux][u.uy].typ == CLOUD) {
  31.          /* Inside a cloud during a thunder storm is deafening. */
  32.          /* Even if already deaf, we sense the thunder's vibrations. */
  33.          pline("Kaboom!!!  Boom!!  Boom!!");
  34.          incr_itimeout(&HDeaf, rn1(20, 30));
  35.          if (!u.uinvulnerable) {
  36.              stop_occupation();
  37.              nomul(-3);
  38.              multi_reason = "hiding from thunderstorm";
  39.              nomovemsg = 0;
  40.          }
  41.      } else
  42.          You_hear("a rumbling noise.");
  43.  }
1300.     
  1.  /* -------------------------------------------------------------------------
  2.   */

Generic timeout functions

  1.  /*
  2.   * Generic Timeout Functions.
  3.   *
  4.   * Interface:
  5.   *
  6.   * General:
  7.   *  boolean start_timer(long timeout,short kind,short func_index,
  8.   *                      anything *arg)
  9.   *      Start a timer of kind 'kind' that will expire at time
  10.   *      monstermoves+'timeout'.  Call the function at 'func_index'
  11.   *      in the timeout table using argument 'arg'.  Return TRUE if
  12.   *      a timer was started.  This places the timer on a list ordered
  13.   *      "sooner" to "later".  If an object, increment the object's
  14.   *      timer count.
  15.   *
  16.   *  long stop_timer(short func_index, anything *arg)
  17.   *      Stop a timer specified by the (func_index, arg) pair.  This
  18.   *      assumes that such a pair is unique.  Return the time the
  19.   *      timer would have gone off.  If no timer is found, return 0.
  20.   *      If an object, decrement the object's timer count.
  21.   *
  22.   *  long peek_timer(short func_index, anything *arg)
  23.   *      Return time specified timer will go off (0 if no such timer).
  24.   *
  25.   *  void run_timers(void)
  26.   *      Call timers that have timed out.
  27.   *
  28.   * Save/Restore:
  29.   *  void save_timers(int fd, int mode, int range)
  30.   *      Save all timers of range 'range'.  Range is either global
  31.   *      or local.  Global timers follow game play, local timers
  32.   *      are saved with a level.  Object and monster timers are
  33.   *      saved using their respective id's instead of pointers.
  34.   *
  35.   *  void restore_timers(int fd, int range, boolean ghostly, long adjust)
  36.   *      Restore timers of range 'range'.  If from a ghost pile,
  37.   *      adjust the timeout by 'adjust'.  The object and monster
  38.   *      ids are not restored until later.
  39.   *
  40.   *  void relink_timers(boolean ghostly)
  41.   *      Relink all object and monster timers that had been saved
  42.   *      using their object's or monster's id number.
  43.   *
  44.   * Object Specific:
  45.   *  void obj_move_timers(struct obj *src, struct obj *dest)
  46.   *      Reassign all timers from src to dest.
  47.   *
  48.   *  void obj_split_timers(struct obj *src, struct obj *dest)
  49.   *      Duplicate all timers assigned to src and attach them to dest.
  50.   *
  51.   *  void obj_stop_timers(struct obj *obj)
  52.   *      Stop all timers attached to obj.
  53.   *
  54.   *  boolean obj_has_timer(struct obj *object, short timer_type)
  55.   *      Check whether object has a timer of type timer_type.
  56.   */
  57.  
  58.  STATIC_DCL const char *FDECL(kind_name, (SHORT_P));
  59.  STATIC_DCL void FDECL(print_queue, (winid, timer_element *));
  60.  STATIC_DCL void FDECL(insert_timer, (timer_element *));
  61.  STATIC_DCL timer_element *FDECL(remove_timer,
  62.                                  (timer_element **, SHORT_P, ANY_P *));
  63.  STATIC_DCL void FDECL(write_timer, (int, timer_element *));
  64.  STATIC_DCL boolean FDECL(mon_is_local, (struct monst *));
  65.  STATIC_DCL boolean FDECL(timer_is_local, (timer_element *));
  66.  STATIC_DCL int FDECL(maybe_write_timer, (int, int, BOOLEAN_P));
  67.  
  68.  /* ordered timer list */
  69.  static timer_element *timer_base; /* "active" */
  70.  static unsigned long timer_id = 1;
  71.  
  72.  /* If defined, then include names when printing out the timer queue */
  73.  #define VERBOSE_TIMER
  74.  
  75.  typedef struct {
  76.      timeout_proc f, cleanup;
  77.  #ifdef VERBOSE_TIMER
  78.      const char *name;
  79.  #define TTAB(a, b, c) \
  80.      {                 \
  81.          a, b, c       \
  82.      }
  83.  #else
  84.  #define TTAB(a, b, c) \
  85.      {                 \
  86.          a, b          \
  87.      }
  88.  #endif
  89.  } ttable;
  90.  
  91.  /* table of timeout functions */
  92.  static const ttable timeout_funcs[NUM_TIME_FUNCS] = {
  93.      TTAB(rot_organic, (timeout_proc) 0, "rot_organic"),
  94.      TTAB(rot_corpse, (timeout_proc) 0, "rot_corpse"),
  95.      TTAB(revive_mon, (timeout_proc) 0, "revive_mon"),
  96.      TTAB(burn_object, cleanup_burn, "burn_object"),
  97.      TTAB(hatch_egg, (timeout_proc) 0, "hatch_egg"),
  98.      TTAB(fig_transform, (timeout_proc) 0, "fig_transform"),
  99.      TTAB(melt_ice_away, (timeout_proc) 0, "melt_ice_away")
  100.  };
  101.  #undef TTAB
  102.  

kind_name

  1.  STATIC_OVL const char *
  2.  kind_name(kind)
  3.  short kind;
  4.  {
  5.      switch (kind) {
  6.      case TIMER_LEVEL:
  7.          return "level";
  8.      case TIMER_GLOBAL:
  9.          return "global";
  10.      case TIMER_OBJECT:
  11.          return "object";
  12.      case TIMER_MONSTER:
  13.          return "monster";
  14.      }
  15.      return "unknown";
  16.  }
  17.  

print_queue

  1.  STATIC_OVL void
  2.  print_queue(win, base)
  3.  winid win;
  4.  timer_element *base;
  5.  {
  6.      timer_element *curr;
  7.      char buf[BUFSZ];
  8.  
  9.      if (!base) {
  10.          putstr(win, 0, "<empty>");
  11.      } else {
  12.          putstr(win, 0, "timeout  id   kind   call");
  13.          for (curr = base; curr; curr = curr->next) {
  14.  #ifdef VERBOSE_TIMER
  15.              Sprintf(buf, " %4ld   %4ld  %-6s %s(%s)", curr->timeout,
  16.                      curr->tid, kind_name(curr->kind),
  17.                      timeout_funcs[curr->func_index].name,
  18.                      fmt_ptr((genericptr_t) curr->arg.a_void));
  19.  #else
  20.              Sprintf(buf, " %4ld   %4ld  %-6s #%d(%s)", curr->timeout,
  21.                      curr->tid, kind_name(curr->kind), curr->func_index,
  22.                      fmt_ptr((genericptr_t) curr->arg.a_void));
  23.  #endif
  24.              putstr(win, 0, buf);
  25.          }
  26.      }
  27.  }
  28.  

wiz_timeout_queue

  1.  int
  2.  wiz_timeout_queue()
  3.  {
  4.      winid win;
  5.      char buf[BUFSZ];
  6.  
  7.      win = create_nhwindow(NHW_MENU); /* corner text window */
  8.      if (win == WIN_ERR)
  9.          return 0;
  10.  
  11.      Sprintf(buf, "Current time = %ld.", monstermoves);
  12.      putstr(win, 0, buf);
  13.      putstr(win, 0, "");
  14.      putstr(win, 0, "Active timeout queue:");
  15.      putstr(win, 0, "");
  16.      print_queue(win, timer_base);
  17.  
  18.      display_nhwindow(win, FALSE);
  19.      destroy_nhwindow(win);
  20.  
  21.      return 0;
  22.  }
  23.  

timer_sanity_check

  1.  void
  2.  timer_sanity_check()
  3.  {
  4.      timer_element *curr;
  5.  
  6.      /* this should be much more complete */
  7.      for (curr = timer_base; curr; curr = curr->next)
  8.          if (curr->kind == TIMER_OBJECT) {
  9.              struct obj *obj = curr->arg.a_obj;
  10.              if (obj->timed == 0) {
  11.                  pline("timer sanity: untimed obj %s, timer %ld",
  12.                        fmt_ptr((genericptr_t) obj), curr->tid);
  13.              }
  14.          }
  15.  }
  16.  

run_timers

  1.  /*
  2.   * Pick off timeout elements from the global queue and call their functions.
  3.   * Do this until their time is less than or equal to the move count.
  4.   */
  5.  void
  6.  run_timers()
  7.  {
  8.      timer_element *curr;
  9.  
  10.      /*
  11.       * Always use the first element.  Elements may be added or deleted at
  12.       * any time.  The list is ordered, we are done when the first element
  13.       * is in the future.
  14.       */
  15.      while (timer_base && timer_base->timeout <= monstermoves) {
  16.          curr = timer_base;
  17.          timer_base = curr->next;
  18.  
  19.          if (curr->kind == TIMER_OBJECT)
  20.              (curr->arg.a_obj)->timed--;
  21.          (*timeout_funcs[curr->func_index].f)(&curr->arg, curr->timeout);
  22.          free((genericptr_t) curr);
  23.      }
  24.  }
  25.  

start_timer

  1.  /*
  2.   * Start a timer.  Return TRUE if successful.
  3.   */
  4.  boolean
  5.  start_timer(when, kind, func_index, arg)
  6.  long when;
  7.  short kind;
  8.  short func_index;
  9.  anything *arg;
  10.  {
  11.      timer_element *gnu;
  12.  
  13.      if (func_index < 0 || func_index >= NUM_TIME_FUNCS)
  14.          panic("start_timer");
  15.  
  16.      gnu = (timer_element *) alloc(sizeof(timer_element));
  17.      gnu->next = 0;
  18.      gnu->tid = timer_id++;
  19.      gnu->timeout = monstermoves + when;
  20.      gnu->kind = kind;
  21.      gnu->needs_fixup = 0;
  22.      gnu->func_index = func_index;
  23.      gnu->arg = *arg;
  24.      insert_timer(gnu);
  25.  
  26.      if (kind == TIMER_OBJECT) /* increment object's timed count */
  27.          (arg->a_obj)->timed++;
  28.  
  29.      /* should check for duplicates and fail if any */
  30.      return TRUE;
  31.  }
  32.  

stop_timer

  1.  /*
  2.   * Remove the timer from the current list and free it up.  Return the time
  3.   * remaining until it would have gone off, 0 if not found.
  4.   */
  5.  long
  6.  stop_timer(func_index, arg)
  7.  short func_index;
  8.  anything *arg;
  9.  {
  10.      timer_element *doomed;
  11.      long timeout;
  12.  
  13.      doomed = remove_timer(&timer_base, func_index, arg);
  14.  
  15.      if (doomed) {
  16.          timeout = doomed->timeout;
  17.          if (doomed->kind == TIMER_OBJECT)
  18.              (arg->a_obj)->timed--;
  19.          if (timeout_funcs[doomed->func_index].cleanup)
  20.              (*timeout_funcs[doomed->func_index].cleanup)(arg, timeout);
  21.          free((genericptr_t) doomed);
  22.          return (timeout - monstermoves);
  23.      }
  24.      return 0L;
  25.  }
  26.  

peek_timer

  1.  /*
  2.   * Find the timeout of specified timer; return 0 if none.
  3.   */
  4.  long
  5.  peek_timer(type, arg)
  6.  short type;
  7.  anything *arg;
  8.  {
  9.      timer_element *curr;
  10.  
  11.      for (curr = timer_base; curr; curr = curr->next) {
  12.          if (curr->func_index == type && curr->arg.a_void == arg->a_void)
  13.              return curr->timeout;
  14.      }
  15.      return 0L;
  16.  }
  17.  

obj_move_timers

  1.  /*
  2.   * Move all object timers from src to dest, leaving src untimed.
  3.   */
  4.  void
  5.  obj_move_timers(src, dest)
  6.  struct obj *src, *dest;
  7.  {
  8.      int count;
  9.      timer_element *curr;
  10.  
  11.      for (count = 0, curr = timer_base; curr; curr = curr->next)
  12.          if (curr->kind == TIMER_OBJECT && curr->arg.a_obj == src) {
  13.              curr->arg.a_obj = dest;
  14.              dest->timed++;
  15.              count++;
  16.          }
  17.      if (count != src->timed)
  18.          panic("obj_move_timers");
  19.      src->timed = 0;
  20.  }
  21.  

obj_split_timers

  1.  /*
  2.   * Find all object timers and duplicate them for the new object "dest".
  3.   */
  4.  void
  5.  obj_split_timers(src, dest)
  6.  struct obj *src, *dest;
  7.  {
  8.      timer_element *curr, *next_timer = 0;
  9.  
  10.      for (curr = timer_base; curr; curr = next_timer) {
  11.          next_timer = curr->next; /* things may be inserted */
  12.          if (curr->kind == TIMER_OBJECT && curr->arg.a_obj == src) {
  13.              (void) start_timer(curr->timeout - monstermoves, TIMER_OBJECT,
  14.                                 curr->func_index, obj_to_any(dest));
  15.          }
  16.      }
  17.  }
  18.  

obj_stop_timers

  1.  /*
  2.   * Stop all timers attached to this object.  We can get away with this because
  3.   * all object pointers are unique.
  4.   */
  5.  void
  6.  obj_stop_timers(obj)
  7.  struct obj *obj;
  8.  {
  9.      timer_element *curr, *prev, *next_timer = 0;
  10.  
  11.      for (prev = 0, curr = timer_base; curr; curr = next_timer) {
  12.          next_timer = curr->next;
  13.          if (curr->kind == TIMER_OBJECT && curr->arg.a_obj == obj) {
  14.              if (prev)
  15.                  prev->next = curr->next;
  16.              else
  17.                  timer_base = curr->next;
  18.              if (timeout_funcs[curr->func_index].cleanup)
  19.                  (*timeout_funcs[curr->func_index].cleanup)(&curr->arg,
  20.                                                             curr->timeout);
  21.              free((genericptr_t) curr);
  22.          } else {
  23.              prev = curr;
  24.          }
  25.      }
  26.      obj->timed = 0;
  27.  }
  28.  

obj_has_timer

  1.  /*
  2.   * Check whether object has a timer of type timer_type.
  3.   */
  4.  boolean
  5.  obj_has_timer(object, timer_type)
  6.  struct obj *object;
  7.  short timer_type;
  8.  {
  9.      long timeout = peek_timer(timer_type, obj_to_any(object));
  10.  
  11.      return (boolean) (timeout != 0L);
  12.  }
  13.  

spot_stop_timers

  1.  /*
  2.   * Stop all timers of index func_index at this spot.
  3.   *
  4.   */
  5.  void
  6.  spot_stop_timers(x, y, func_index)
  7.  xchar x, y;
  8.  short func_index;
  9.  {
  10.      timer_element *curr, *prev, *next_timer = 0;
  11.      long where = (((long) x << 16) | ((long) y));
  12.  
  13.      for (prev = 0, curr = timer_base; curr; curr = next_timer) {
  14.          next_timer = curr->next;
  15.          if (curr->kind == TIMER_LEVEL && curr->func_index == func_index
  16.              && curr->arg.a_long == where) {
  17.              if (prev)
  18.                  prev->next = curr->next;
  19.              else
  20.                  timer_base = curr->next;
  21.              if (timeout_funcs[curr->func_index].cleanup)
  22.                  (*timeout_funcs[curr->func_index].cleanup)(&curr->arg,
  23.                                                             curr->timeout);
  24.              free((genericptr_t) curr);
  25.          } else {
  26.              prev = curr;
  27.          }
  28.      }
  29.  }
  30.  

spot_time_expires

  1.  /*
  2.   * When is the spot timer of type func_index going to expire?
  3.   * Returns 0L if no such timer.
  4.   */
  5.  long
  6.  spot_time_expires(x, y, func_index)
  7.  xchar x, y;
  8.  short func_index;
  9.  {
  10.      timer_element *curr;
  11.      long where = (((long) x << 16) | ((long) y));
  12.  
  13.      for (curr = timer_base; curr; curr = curr->next) {
  14.          if (curr->kind == TIMER_LEVEL && curr->func_index == func_index
  15.              && curr->arg.a_long == where)
  16.              return curr->timeout;
  17.      }
  18.      return 0L;
  19.  }
  20.  

spot_time_left

  1.  long
  2.  spot_time_left(x, y, func_index)
  3.  xchar x, y;
  4.  short func_index;
  5.  {
  6.      long expires = spot_time_expires(x, y, func_index);
  7.      return (expires > 0L) ? expires - monstermoves : 0L;
  8.  }
  9.  

insert_timer

  1.  /* Insert timer into the global queue */
  2.  STATIC_OVL void
  3.  insert_timer(gnu)
  4.  timer_element *gnu;
  5.  {
  6.      timer_element *curr, *prev;
  7.  
  8.      for (prev = 0, curr = timer_base; curr; prev = curr, curr = curr->next)
  9.          if (curr->timeout >= gnu->timeout)
  10.              break;
  11.  
  12.      gnu->next = curr;
  13.      if (prev)
  14.          prev->next = gnu;
  15.      else
  16.          timer_base = gnu;
  17.  }
  18.  

remove_timer

  1.  STATIC_OVL timer_element *
  2.  remove_timer(base, func_index, arg)
  3.  timer_element **base;
  4.  short func_index;
  5.  anything *arg;
  6.  {
  7.      timer_element *prev, *curr;
  8.  
  9.      for (prev = 0, curr = *base; curr; prev = curr, curr = curr->next)
  10.          if (curr->func_index == func_index && curr->arg.a_void == arg->a_void)
  11.              break;
  12.  
  13.      if (curr) {
  14.          if (prev)
  15.              prev->next = curr->next;
  16.          else
  17.              *base = curr->next;
  18.      }
  19.  
  20.      return curr;
  21.  }
  22.  

write_timer

  1.  STATIC_OVL void
  2.  write_timer(fd, timer)
  3.  int fd;
  4.  timer_element *timer;
  5.  {
  6.      anything arg_save;
  7.  
  8.      arg_save = zeroany;
  9.      switch (timer->kind) {
  10.      case TIMER_GLOBAL:
  11.      case TIMER_LEVEL:
  12.          /* assume no pointers in arg */
  13.          bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
  14.          break;
  15.  
  16.      case TIMER_OBJECT:
  17.          if (timer->needs_fixup)
  18.              bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
  19.          else {
  20.              /* replace object pointer with id */
  21.              arg_save.a_obj = timer->arg.a_obj;
  22.              timer->arg = zeroany;
  23.              timer->arg.a_uint = (arg_save.a_obj)->o_id;
  24.              timer->needs_fixup = 1;
  25.              bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
  26.              timer->arg.a_obj = arg_save.a_obj;
  27.              timer->needs_fixup = 0;
  28.          }
  29.          break;
  30.  
  31.      case TIMER_MONSTER:
  32.          if (timer->needs_fixup)
  33.              bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
  34.          else {
  35.              /* replace monster pointer with id */
  36.              arg_save.a_monst = timer->arg.a_monst;
  37.              timer->arg = zeroany;
  38.              timer->arg.a_uint = (arg_save.a_monst)->m_id;
  39.              timer->needs_fixup = 1;
  40.              bwrite(fd, (genericptr_t) timer, sizeof(timer_element));
  41.              timer->arg.a_monst = arg_save.a_monst;
  42.              timer->needs_fixup = 0;
  43.          }
  44.          break;
  45.  
  46.      default:
  47.          panic("write_timer");
  48.          break;
  49.      }
  50.  }
  51.  

obj_is_local

  1.  /*
  2.   * Return TRUE if the object will stay on the level when the level is
  3.   * saved.
  4.   */
  5.  boolean
  6.  obj_is_local(obj)
  7.  struct obj *obj;
  8.  {
  9.      switch (obj->where) {
  10.      case OBJ_INVENT:
  11.      case OBJ_MIGRATING:
  12.          return FALSE;
  13.      case OBJ_FLOOR:
  14.      case OBJ_BURIED:
  15.          return TRUE;
  16.      case OBJ_CONTAINED:
  17.          return obj_is_local(obj->ocontainer);
  18.      case OBJ_MINVENT:
  19.          return mon_is_local(obj->ocarry);
  20.      }
  21.      panic("obj_is_local");
  22.      return FALSE;
  23.  }
  24.  

mon_is_local

  1.  /*
  2.   * Return TRUE if the given monster will stay on the level when the
  3.   * level is saved.
  4.   */
  5.  STATIC_OVL boolean
  6.  mon_is_local(mon)
  7.  struct monst *mon;
  8.  {
  9.      struct monst *curr;
  10.  
  11.      for (curr = migrating_mons; curr; curr = curr->nmon)
  12.          if (curr == mon)
  13.              return FALSE;
  14.      /* `mydogs' is used during level changes, never saved and restored */
  15.      for (curr = mydogs; curr; curr = curr->nmon)
  16.          if (curr == mon)
  17.              return FALSE;
  18.      return TRUE;
  19.  }
  20.  

timer_is_local

  1.  /*
  2.   * Return TRUE if the timer is attached to something that will stay on the
  3.   * level when the level is saved.
  4.   */
  5.  STATIC_OVL boolean
  6.  timer_is_local(timer)
  7.  timer_element *timer;
  8.  {
  9.      switch (timer->kind) {
  10.      case TIMER_LEVEL:
  11.          return TRUE;
  12.      case TIMER_GLOBAL:
  13.          return FALSE;
  14.      case TIMER_OBJECT:
  15.          return obj_is_local(timer->arg.a_obj);
  16.      case TIMER_MONSTER:
  17.          return mon_is_local(timer->arg.a_monst);
  18.      }
  19.      panic("timer_is_local");
  20.      return FALSE;
  21.  }
  22.  

maybe_write_timer

  1.  /*
  2.   * Part of the save routine.  Count up the number of timers that would
  3.   * be written.  If write_it is true, actually write the timer.
  4.   */
  5.  STATIC_OVL int
  6.  maybe_write_timer(fd, range, write_it)
  7.  int fd, range;
  8.  boolean write_it;
  9.  {
  10.      int count = 0;
  11.      timer_element *curr;
  12.  
  13.      for (curr = timer_base; curr; curr = curr->next) {
  14.          if (range == RANGE_GLOBAL) {
  15.              /* global timers */
  16.  
  17.              if (!timer_is_local(curr)) {
  18.                  count++;
  19.                  if (write_it)
  20.                      write_timer(fd, curr);
  21.              }
  22.  
  23.          } else {
  24.              /* local timers */
  25.  
  26.              if (timer_is_local(curr)) {
  27.                  count++;
  28.                  if (write_it)
  29.                      write_timer(fd, curr);
  30.              }
  31.          }
  32.      }
  33.  
  34.      return count;
  35.  }
  36.  

save_timers

  1.  /*
  2.   * Save part of the timer list.  The parameter 'range' specifies either
  3.   * global or level timers to save.  The timer ID is saved with the global
  4.   * timers.
  5.   *
  6.   * Global range:
  7.   *      + timeouts that follow the hero (global)
  8.   *      + timeouts that follow obj & monst that are migrating
  9.   *
  10.   * Level range:
  11.   *      + timeouts that are level specific (e.g. storms)
  12.   *      + timeouts that stay with the level (obj & monst)
  13.   */
  14.  void
  15.  save_timers(fd, mode, range)
  16.  int fd, mode, range;
  17.  {
  18.      timer_element *curr, *prev, *next_timer = 0;
  19.      int count;
  20.  
  21.      if (perform_bwrite(mode)) {
  22.          if (range == RANGE_GLOBAL)
  23.              bwrite(fd, (genericptr_t) &timer_id, sizeof(timer_id));
  24.  
  25.          count = maybe_write_timer(fd, range, FALSE);
  26.          bwrite(fd, (genericptr_t) &count, sizeof count);
  27.          (void) maybe_write_timer(fd, range, TRUE);
  28.      }
  29.  
  30.      if (release_data(mode)) {
  31.          for (prev = 0, curr = timer_base; curr; curr = next_timer) {
  32.              next_timer = curr->next; /* in case curr is removed */
  33.  
  34.              if (!(!!(range == RANGE_LEVEL) ^ !!timer_is_local(curr))) {
  35.                  if (prev)
  36.                      prev->next = curr->next;
  37.                  else
  38.                      timer_base = curr->next;
  39.                  free((genericptr_t) curr);
  40.                  /* prev stays the same */
  41.              } else {
  42.                  prev = curr;
  43.              }
  44.          }
  45.      }
  46.  }
  47.  

restore_timers

  1.  /*
  2.   * Pull in the structures from disk, but don't recalculate the object and
  3.   * monster pointers.
  4.   */
  5.  void
  6.  restore_timers(fd, range, ghostly, adjust)
  7.  int fd, range;
  8.  boolean ghostly; /* restoring from a ghost level */
  9.  long adjust;     /* how much to adjust timeout */
  10.  {
  11.      int count;
  12.      timer_element *curr;
  13.  
  14.      if (range == RANGE_GLOBAL)
  15.          mread(fd, (genericptr_t) &timer_id, sizeof timer_id);
  16.  
  17.      /* restore elements */
  18.      mread(fd, (genericptr_t) &count, sizeof count);
  19.      while (count-- > 0) {
  20.          curr = (timer_element *) alloc(sizeof(timer_element));
  21.          mread(fd, (genericptr_t) curr, sizeof(timer_element));
  22.          if (ghostly)
  23.              curr->timeout += adjust;
  24.          insert_timer(curr);
  25.      }
  26.  }
  27.  

relink_timers

  1.  /* reset all timers that are marked for reseting */
  2.  void
  3.  relink_timers(ghostly)
  4.  boolean ghostly;
  5.  {
  6.      timer_element *curr;
  7.      unsigned nid;
  8.  
  9.      for (curr = timer_base; curr; curr = curr->next) {
  10.          if (curr->needs_fixup) {
  11.              if (curr->kind == TIMER_OBJECT) {
  12.                  if (ghostly) {
  13.                      if (!lookup_id_mapping(curr->arg.a_uint, &nid))
  14.                          panic("relink_timers 1");
  15.                  } else
  16.                      nid = curr->arg.a_uint;
  17.                  curr->arg.a_obj = find_oid(nid);
  18.                  if (!curr->arg.a_obj)
  19.                      panic("cant find o_id %d", nid);
  20.                  curr->needs_fixup = 0;
  21.              } else if (curr->kind == TIMER_MONSTER) {
  22.                  panic("relink_timers: no monster timer implemented");
  23.              } else
  24.                  panic("relink_timers 2");
  25.          }
  26.      }
  27.  }
  28.  
  29.  /*timeout.c*/