Source:NetHack 3.6.0/src/ball.c

From NetHackWiki
(Redirected from Ball.c)
Jump to: navigation, search

Below is the full text to ball.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/ball.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	ball.c	$NHDT-Date: 1446808438 2015/11/06 11:13:58 $  $NHDT-Branch: master $:$NHDT-Revision: 1.28 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5.  /* Ball & Chain
  6.   * =============================================================*/
  7.  
  8.  #include "hack.h"
  9.  
  10.  STATIC_DCL int NDECL(bc_order);
  11.  STATIC_DCL void NDECL(litter);
  12.  

ballrelease

  1.  void
  2.  ballrelease(showmsg)
  3.  boolean showmsg;
  4.  {
  5.      if (carried(uball)) {
  6.          if (showmsg)
  7.              pline("Startled, you drop the iron ball.");
  8.          if (uwep == uball)
  9.              setuwep((struct obj *) 0);
  10.          if (uswapwep == uball)
  11.              setuswapwep((struct obj *) 0);
  12.          if (uquiver == uball)
  13.              setuqwep((struct obj *) 0);
  14.          ;
  15.          if (uwep != uball)
  16.              freeinv(uball);
  17.      }
  18.  }
  19.  

ballfall

  1.  void
  2.  ballfall()
  3.  {
  4.      boolean gets_hit;
  5.  
  6.      gets_hit = (((uball->ox != u.ux) || (uball->oy != u.uy))
  7.                  && ((uwep == uball) ? FALSE : (boolean) rn2(5)));
  8.      ballrelease(TRUE);
  9.      if (gets_hit) {
  10.          int dmg = rn1(7, 25);
  11.          pline_The("iron ball falls on your %s.", body_part(HEAD));
  12.          if (uarmh) {
  13.              if (is_metallic(uarmh)) {
  14.                  pline("Fortunately, you are wearing a hard helmet.");
  15.                  dmg = 3;
  16.              } else if (flags.verbose)
  17.                  pline("%s does not protect you.", Yname2(uarmh));
  18.          }
  19.          losehp(Maybe_Half_Phys(dmg), "crunched in the head by an iron ball",
  20.                 NO_KILLER_PREFIX);
  21.      }
  22.  }
  23.  

placebc

  1.  /*
  2.   *  To make this work, we have to mess with the hero's mind.  The rules for
  3.   *  ball&chain are:
  4.   *
  5.   *      1. If the hero can see them, fine.
  6.   *      2. If the hero can't see either, it isn't seen.
  7.   *      3. If either is felt it is seen.
  8.   *      4. If either is felt and moved, it disappears.
  9.   *
  10.   *  If the hero can see, then when a move is done, the ball and chain are
  11.   *  first picked up, the positions under them are corrected, then they
  12.   *  are moved after the hero moves.  Not too bad.
  13.   *
  14.   *  If the hero is blind, then she can "feel" the ball and/or chain at any
  15.   *  time.  However, when the hero moves, the felt ball and/or chain become
  16.   *  unfelt and whatever was felt "under" the ball&chain appears.  Pretty
  17.   *  nifty, but it requires that the ball&chain "remember" what was under
  18.   *  them --- i.e. they pick-up glyphs when they are felt and drop them when
  19.   *  moved (and felt).  When swallowed, the ball&chain are pulled completely
  20.   *  off of the dungeon, but are still on the object chain.  They are placed
  21.   *  under the hero when she is expelled.
  22.   */
  23.  
  24.  /*
  25.   * from you.h
  26.   *      int u.bglyph            glyph under the ball
  27.   *      int u.cglyph            glyph under the chain
  28.   *      int u.bc_felt           mask for ball/chain being felt
  29.   *      #define BC_BALL  0x01   bit mask in u.bc_felt for ball
  30.   *      #define BC_CHAIN 0x02   bit mask in u.bc_felt for chain
  31.   *      int u.bc_order          ball & chain order
  32.   *
  33.   * u.bc_felt is also manipulated in display.c and read.c, the others only
  34.   * in this file.  None of these variables are valid unless the player is
  35.   * Blind.
  36.   */
  37.  
  38.  /* values for u.bc_order */
  39.  #define BCPOS_DIFFER 0 /* ball & chain at different positions */
  40.  #define BCPOS_CHAIN 1  /* chain on top of ball */
  41.  #define BCPOS_BALL 2   /* ball on top of chain */
  42.  
  43.  /*
  44.   *  Place the ball & chain under the hero.  Make sure that the ball & chain
  45.   *  variables are set (actually only needed when blind, but what the heck).
  46.   *  It is assumed that when this is called, the ball and chain are NOT
  47.   *  attached to the object list.
  48.   *
  49.   *  Should not be called while swallowed except on waterlevel.
  50.   */
  51.  void
  52.  placebc()
  53.  {
  54.      if (!uchain || !uball) {
  55.          impossible("Where are your ball and chain?");
  56.          return;
  57.      }
  58.  
  59.      (void) flooreffects(uchain, u.ux, u.uy, ""); /* chain might rust */
  60.  
  61.      if (carried(uball)) /* the ball is carried */
  62.          u.bc_order = BCPOS_DIFFER;
  63.      else {
  64.          /* ball might rust -- already checked when carried */
  65.          (void) flooreffects(uball, u.ux, u.uy, "");
  66.          place_object(uball, u.ux, u.uy);
  67.          u.bc_order = BCPOS_CHAIN;
  68.      }
  69.  
  70.      place_object(uchain, u.ux, u.uy);
  71.  
  72.      u.bglyph = u.cglyph = levl[u.ux][u.uy].glyph; /* pick up glyph */
  73.  
  74.      newsym(u.ux, u.uy);
  75.  }
  76.  

unplacebc

  1.  void
  2.  unplacebc()
  3.  {
  4.      if (u.uswallow) {
  5.          if (Is_waterlevel(&u.uz)) {
  6.              /* we need to proceed with the removal from the floor
  7.               * so that movebubbles() processing will disregard it as
  8.               * intended. Ignore all the vision stuff.
  9.               */
  10.              if (!carried(uball))
  11.                  obj_extract_self(uball);
  12.              obj_extract_self(uchain);
  13.          }
  14.          /* ball&chain not unplaced while swallowed */
  15.          return;
  16.      }
  17.  
  18.      if (!carried(uball)) {
  19.          obj_extract_self(uball);
  20.          if (Blind && (u.bc_felt & BC_BALL)) /* drop glyph */
  21.              levl[uball->ox][uball->oy].glyph = u.bglyph;
  22.  
  23.          newsym(uball->ox, uball->oy);
  24.      }
  25.      obj_extract_self(uchain);
  26.      if (Blind && (u.bc_felt & BC_CHAIN)) /* drop glyph */
  27.          levl[uchain->ox][uchain->oy].glyph = u.cglyph;
  28.  
  29.      newsym(uchain->ox, uchain->oy);
  30.      u.bc_felt = 0; /* feel nothing */
  31.  }
  32.  

bc_order

  1.  /*
  2.   *  Return the stacking of the hero's ball & chain.  This assumes that the
  3.   *  hero is being punished.
  4.   */
  5.  STATIC_OVL int
  6.  bc_order()
  7.  {
  8.      struct obj *obj;
  9.  
  10.      if (uchain->ox != uball->ox || uchain->oy != uball->oy || carried(uball)
  11.          || u.uswallow)
  12.          return BCPOS_DIFFER;
  13.  
  14.      for (obj = level.objects[uball->ox][uball->oy]; obj;
  15.           obj = obj->nexthere) {
  16.          if (obj == uchain)
  17.              return BCPOS_CHAIN;
  18.          if (obj == uball)
  19.              return BCPOS_BALL;
  20.      }
  21.      impossible("bc_order:  ball&chain not in same location!");
  22.      return BCPOS_DIFFER;
  23.  }
  24.  

set_bc

  1.  /*
  2.   *  set_bc()
  3.   *
  4.   *  The hero is either about to go blind or already blind and just punished.
  5.   *  Set up the ball and chain variables so that the ball and chain are "felt".
  6.   */
  7.  void
  8.  set_bc(already_blind)
  9.  int already_blind;
  10.  {
  11.      int ball_on_floor = !carried(uball);
  12.  
  13.      u.bc_order = bc_order(); /* get the order */
  14.      u.bc_felt = ball_on_floor ? BC_BALL | BC_CHAIN : BC_CHAIN; /* felt */
  15.  
  16.      if (already_blind || u.uswallow) {
  17.          u.cglyph = u.bglyph = levl[u.ux][u.uy].glyph;
  18.          return;
  19.      }
  20.  
  21.      /*
  22.       *  Since we can still see, remove the ball&chain and get the glyph that
  23.       *  would be beneath them.  Then put the ball&chain back.  This is pretty
  24.       *  disgusting, but it will work.
  25.       */
  26.      remove_object(uchain);
  27.      if (ball_on_floor)
  28.          remove_object(uball);
  29.  
  30.      newsym(uchain->ox, uchain->oy);
  31.      u.cglyph = levl[uchain->ox][uchain->oy].glyph;
  32.  
  33.      if (u.bc_order == BCPOS_DIFFER) { /* different locations */
  34.          place_object(uchain, uchain->ox, uchain->oy);
  35.          newsym(uchain->ox, uchain->oy);
  36.          if (ball_on_floor) {
  37.              newsym(uball->ox, uball->oy); /* see under ball */
  38.              u.bglyph = levl[uball->ox][uball->oy].glyph;
  39.              place_object(uball, uball->ox, uball->oy);
  40.              newsym(uball->ox, uball->oy); /* restore ball */
  41.          }
  42.      } else {
  43.          u.bglyph = u.cglyph;
  44.          if (u.bc_order == BCPOS_CHAIN) {
  45.              place_object(uball, uball->ox, uball->oy);
  46.              place_object(uchain, uchain->ox, uchain->oy);
  47.          } else {
  48.              place_object(uchain, uchain->ox, uchain->oy);
  49.              place_object(uball, uball->ox, uball->oy);
  50.          }
  51.          newsym(uball->ox, uball->oy);
  52.      }
  53.  }
  54.  

move_bc

  1.  /*
  2.   *  move_bc()
  3.   *
  4.   *  Move the ball and chain.  This is called twice for every move.  The first
  5.   *  time to pick up the ball and chain before the move, the second time to
  6.   *  place the ball and chain after the move.  If the ball is carried, this
  7.   *  function should never have BC_BALL as part of its control.
  8.   *
  9.   *  Should not be called while swallowed.
  10.   */
  11.  void
  12.  move_bc(before, control, ballx, bally, chainx, chainy)
  13.  int before, control;
  14.  xchar ballx, bally, chainx, chainy; /* only matter !before */
  15.  {
  16.      if (Blind) {
  17.          /*
  18.           *  The hero is blind.  Time to work hard.  The ball and chain that
  19.           *  are attached to the hero are very special.  The hero knows that
  20.           *  they are attached, so when they move, the hero knows that they
  21.           *  aren't at the last position remembered.  This is complicated
  22.           *  by the fact that the hero can "feel" the surrounding locations
  23.           *  at any time, hence, making one or both of them show up again.
  24.           *  So, we have to keep track of which is felt at any one time and
  25.           *  act accordingly.
  26.           */
  27.          if (!before) {
  28.              if ((control & BC_CHAIN) && (control & BC_BALL)) {
  29.                  /*
  30.                   *  Both ball and chain moved.  If felt, drop glyph.
  31.                   */
  32.                  if (u.bc_felt & BC_BALL)
  33.                      levl[uball->ox][uball->oy].glyph = u.bglyph;
  34.                  if (u.bc_felt & BC_CHAIN)
  35.                      levl[uchain->ox][uchain->oy].glyph = u.cglyph;
  36.                  u.bc_felt = 0;
  37.  
  38.                  /* Pick up glyph at new location. */
  39.                  u.bglyph = levl[ballx][bally].glyph;
  40.                  u.cglyph = levl[chainx][chainy].glyph;
  41.  
  42.                  movobj(uball, ballx, bally);
  43.                  movobj(uchain, chainx, chainy);
  44.              } else if (control & BC_BALL) {
  45.                  if (u.bc_felt & BC_BALL) {
  46.                      if (u.bc_order == BCPOS_DIFFER) { /* ball by itself */
  47.                          levl[uball->ox][uball->oy].glyph = u.bglyph;
  48.                      } else if (u.bc_order == BCPOS_BALL) {
  49.                          if (u.bc_felt & BC_CHAIN) { /* know chain is there */
  50.                              map_object(uchain, 0);
  51.                          } else {
  52.                              levl[uball->ox][uball->oy].glyph = u.bglyph;
  53.                          }
  54.                      }
  55.                      u.bc_felt &= ~BC_BALL; /* no longer feel the ball */
  56.                  }
  57.  
  58.                  /* Pick up glyph at new position. */
  59.                  u.bglyph = (ballx != chainx || bally != chainy)
  60.                                 ? levl[ballx][bally].glyph
  61.                                 : u.cglyph;
  62.  
  63.                  movobj(uball, ballx, bally);
  64.              } else if (control & BC_CHAIN) {
  65.                  if (u.bc_felt & BC_CHAIN) {
  66.                      if (u.bc_order == BCPOS_DIFFER) {
  67.                          levl[uchain->ox][uchain->oy].glyph = u.cglyph;
  68.                      } else if (u.bc_order == BCPOS_CHAIN) {
  69.                          if (u.bc_felt & BC_BALL) {
  70.                              map_object(uball, 0);
  71.                          } else {
  72.                              levl[uchain->ox][uchain->oy].glyph = u.cglyph;
  73.                          }
  74.                      }
  75.                      u.bc_felt &= ~BC_CHAIN;
  76.                  }
  77.                  /* Pick up glyph at new position. */
  78.                  u.cglyph = (ballx != chainx || bally != chainy)
  79.                                 ? levl[chainx][chainy].glyph
  80.                                 : u.bglyph;
  81.  
  82.                  movobj(uchain, chainx, chainy);
  83.              }
  84.  
  85.              u.bc_order = bc_order(); /* reset the order */
  86.          }
  87.  
  88.      } else {
  89.          /*
  90.           *  The hero is not blind.  To make this work correctly, we need to
  91.           *  pick up the ball and chain before the hero moves, then put them
  92.           *  in their new positions after the hero moves.
  93.           */
  94.          if (before) {
  95.              if (!control) {
  96.                  /*
  97.                   * Neither ball nor chain is moving, so remember which was
  98.                   * on top until !before.  Use the variable u.bc_order
  99.                   * since it is only valid when blind.
  100.                   */
  101.                  u.bc_order = bc_order();
  102.              }
  103.  
  104.              remove_object(uchain);
  105.              newsym(uchain->ox, uchain->oy);
  106.              if (!carried(uball)) {
  107.                  remove_object(uball);
  108.                  newsym(uball->ox, uball->oy);
  109.              }
  110.          } else {
  111.              int on_floor = !carried(uball);
  112.  
  113.              if ((control & BC_CHAIN)
  114.                  || (!control && u.bc_order == BCPOS_CHAIN)) {
  115.                  /* If the chain moved or nothing moved & chain on top. */
  116.                  if (on_floor)
  117.                      place_object(uball, ballx, bally);
  118.                  place_object(uchain, chainx, chainy); /* chain on top */
  119.              } else {
  120.                  place_object(uchain, chainx, chainy);
  121.                  if (on_floor)
  122.                      place_object(uball, ballx, bally);
  123.                  /* ball on top */
  124.              }
  125.              newsym(chainx, chainy);
  126.              if (on_floor)
  127.                  newsym(ballx, bally);
  128.          }
  129.      }
  130.  }
  131.  

drag_ball

  1.  /* return TRUE if the caller needs to place the ball and chain down again
  2.   *
  3.   *  Should not be called while swallowed.  Should be called before movement,
  4.   *  because we might want to move the ball or chain to the hero's old
  5.   * position.
  6.   *
  7.   * It is called if we are moving.  It is also called if we are teleporting
  8.   * *if* the ball doesn't move and we thus must drag the chain.  It is not
  9.   * called for ordinary teleportation.
  10.   *
  11.   * allow_drag is only used in the ugly special case where teleporting must
  12.   * drag the chain, while an identical-looking movement must drag both the ball
  13.   * and chain.
  14.   */
  15.  boolean
  16.  drag_ball(x, y, bc_control, ballx, bally, chainx, chainy, cause_delay,
  17.            allow_drag)
  18.  xchar x, y;
  19.  int *bc_control;
  20.  xchar *ballx, *bally, *chainx, *chainy;
  21.  boolean *cause_delay;
  22.  boolean allow_drag;
  23.  {
  24.      struct trap *t = (struct trap *) 0;
  25.      boolean already_in_rock;
  26.  
  27.      *ballx = uball->ox;
  28.      *bally = uball->oy;
  29.      *chainx = uchain->ox;
  30.      *chainy = uchain->oy;
  31.      *bc_control = 0;
  32.      *cause_delay = FALSE;
  33.  
  34.      if (dist2(x, y, uchain->ox, uchain->oy) <= 2) { /* nothing moved */
  35.          move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy);
  36.          return TRUE;
  37.      }
  38.  
  39.      /* only need to move the chain? */
  40.      if (carried(uball) || distmin(x, y, uball->ox, uball->oy) <= 2) {
  41.          xchar oldchainx = uchain->ox, oldchainy = uchain->oy;
  42.          *bc_control = BC_CHAIN;
  43.          move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy);
  44.          if (carried(uball)) {
  45.              /* move chain only if necessary */
  46.              if (distmin(x, y, uchain->ox, uchain->oy) > 1) {
  47.                  *chainx = u.ux;
  48.                  *chainy = u.uy;
  49.              }
  50.              return TRUE;
  51.          }
  52.  #define CHAIN_IN_MIDDLE(chx, chy) \
  53.      (distmin(x, y, chx, chy) <= 1 \
  54.       && distmin(chx, chy, uball->ox, uball->oy) <= 1)
  55.  #define IS_CHAIN_ROCK(x, y)      \
  56.      (IS_ROCK(levl[x][y].typ)     \
  57.       || (IS_DOOR(levl[x][y].typ) \
  58.           && (levl[x][y].doormask & (D_CLOSED | D_LOCKED))))
  59.  /* Don't ever move the chain into solid rock.  If we have to, then instead
  60.   * undo the move_bc() and jump to the drag ball code.  Note that this also
  61.   * means the "cannot carry and drag" message will not appear, since unless we
  62.   * moved at least two squares there is no possibility of the chain position
  63.   * being in solid rock.
  64.   */
  65.  #define SKIP_TO_DRAG                                               \
  66.      {                                                              \
  67.          *chainx = oldchainx;                                       \
  68.          *chainy = oldchainy;                                       \
  69.          move_bc(0, *bc_control, *ballx, *bally, *chainx, *chainy); \
  70.          goto drag;                                                 \
  71.      }
  72.          if (IS_CHAIN_ROCK(u.ux, u.uy) || IS_CHAIN_ROCK(*chainx, *chainy)
  73.              || IS_CHAIN_ROCK(uball->ox, uball->oy))
  74.              already_in_rock = TRUE;
  75.          else
  76.              already_in_rock = FALSE;
  77.  
  78.          switch (dist2(x, y, uball->ox, uball->oy)) {
  79.          /* two spaces diagonal from ball, move chain inbetween */
  80.          case 8:
  81.              *chainx = (uball->ox + x) / 2;
  82.              *chainy = (uball->oy + y) / 2;
  83.              if (IS_CHAIN_ROCK(*chainx, *chainy) && !already_in_rock)
  84.                  SKIP_TO_DRAG;
  85.              break;
  86.  
  87.          /* player is distance 2/1 from ball; move chain to one of the
  88.           * two spaces between
  89.           *   @
  90.           *   __
  91.           *    0
  92.           */
  93.          case 5: {
  94.              xchar tempx, tempy, tempx2, tempy2;
  95.  
  96.              /* find position closest to current position of chain */
  97.              /* no effect if current position is already OK */
  98.              if (abs(x - uball->ox) == 1) {
  99.                  tempx = x;
  100.                  tempx2 = uball->ox;
  101.                  tempy = tempy2 = (uball->oy + y) / 2;
  102.              } else {
  103.                  tempx = tempx2 = (uball->ox + x) / 2;
  104.                  tempy = y;
  105.                  tempy2 = uball->oy;
  106.              }
  107.              if (IS_CHAIN_ROCK(tempx, tempy) && !IS_CHAIN_ROCK(tempx2, tempy2)
  108.                  && !already_in_rock) {
  109.                  if (allow_drag) {
  110.                      /* Avoid pathological case *if* not teleporting:
  111.                       *   0                          0_
  112.                       *   _X  move northeast  ----->  X@
  113.                       *    @
  114.                       */
  115.                      if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 5
  116.                          && dist2(x, y, tempx, tempy) == 1)
  117.                          SKIP_TO_DRAG;
  118.                      /* Avoid pathological case *if* not teleporting:
  119.                       *    0                          0
  120.                       *   _X  move east       ----->  X_
  121.                       *    @                           @
  122.                       */
  123.                      if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 4
  124.                          && dist2(x, y, tempx, tempy) == 2)
  125.                          SKIP_TO_DRAG;
  126.                  }
  127.                  *chainx = tempx2;
  128.                  *chainy = tempy2;
  129.              } else if (!IS_CHAIN_ROCK(tempx, tempy)
  130.                         && IS_CHAIN_ROCK(tempx2, tempy2) && !already_in_rock) {
  131.                  if (allow_drag) {
  132.                      if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 5
  133.                          && dist2(x, y, tempx2, tempy2) == 1)
  134.                          SKIP_TO_DRAG;
  135.                      if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 4
  136.                          && dist2(x, y, tempx2, tempy2) == 2)
  137.                          SKIP_TO_DRAG;
  138.                  }
  139.                  *chainx = tempx;
  140.                  *chainy = tempy;
  141.              } else if (IS_CHAIN_ROCK(tempx, tempy)
  142.                         && IS_CHAIN_ROCK(tempx2, tempy2) && !already_in_rock) {
  143.                  SKIP_TO_DRAG;
  144.              } else if (dist2(tempx, tempy, uchain->ox, uchain->oy)
  145.                             < dist2(tempx2, tempy2, uchain->ox, uchain->oy)
  146.                         || ((dist2(tempx, tempy, uchain->ox, uchain->oy)
  147.                              == dist2(tempx2, tempy2, uchain->ox, uchain->oy))
  148.                             && rn2(2))) {
  149.                  *chainx = tempx;
  150.                  *chainy = tempy;
  151.              } else {
  152.                  *chainx = tempx2;
  153.                  *chainy = tempy2;
  154.              }
  155.              break;
  156.          }
  157.  
  158.          /* ball is two spaces horizontal or vertical from player; move*/
  159.          /* chain inbetween *unless* current chain position is OK */
  160.          case 4:
  161.              if (CHAIN_IN_MIDDLE(uchain->ox, uchain->oy))
  162.                  break;
  163.              *chainx = (x + uball->ox) / 2;
  164.              *chainy = (y + uball->oy) / 2;
  165.              if (IS_CHAIN_ROCK(*chainx, *chainy) && !already_in_rock)
  166.                  SKIP_TO_DRAG;
  167.              break;
  168.  
  169.          /* ball is one space diagonal from player.  Check for the
  170.           * following special case:
  171.           *   @
  172.           *    _    moving southwest becomes  @_
  173.           *   0                                0
  174.           * (This will also catch teleporting that happens to resemble
  175.           * this case, but oh well.)  Otherwise fall through.
  176.           */
  177.          case 2:
  178.              if (dist2(x, y, uball->ox, uball->oy) == 2
  179.                  && dist2(x, y, uchain->ox, uchain->oy) == 4) {
  180.                  if (uchain->oy == y)
  181.                      *chainx = uball->ox;
  182.                  else
  183.                      *chainy = uball->oy;
  184.                  if (IS_CHAIN_ROCK(*chainx, *chainy) && !already_in_rock)
  185.                      SKIP_TO_DRAG;
  186.                  break;
  187.              }
  188.          /* fall through */
  189.          case 1:
  190.          case 0:
  191.              /* do nothing if possible */
  192.              if (CHAIN_IN_MIDDLE(uchain->ox, uchain->oy))
  193.                  break;
  194.              /* otherwise try to drag chain to player's old position */
  195.              if (CHAIN_IN_MIDDLE(u.ux, u.uy)) {
  196.                  *chainx = u.ux;
  197.                  *chainy = u.uy;
  198.                  break;
  199.              }
  200.              /* otherwise use player's new position (they must have
  201.                 teleported, for this to happen) */
  202.              *chainx = x;
  203.              *chainy = y;
  204.              break;
  205.  
  206.          default:
  207.              impossible("bad chain movement");
  208.              break;
  209.          }
  210.  #undef SKIP_TO_DRAG
  211.  #undef CHAIN_IN_MIDDLE
  212.          return TRUE;
  213.      }
  214.  
  215.  drag:
  216.  
  217.      if (near_capacity() > SLT_ENCUMBER && dist2(x, y, u.ux, u.uy) <= 2) {
  218.          You("cannot %sdrag the heavy iron ball.",
  219.              invent ? "carry all that and also " : "");
  220.          nomul(0);
  221.          return FALSE;
  222.      }
  223.  
  224.      if ((is_pool(uchain->ox, uchain->oy)
  225.           /* water not mere continuation of previous water */
  226.           && (levl[uchain->ox][uchain->oy].typ == POOL
  227.               || !is_pool(uball->ox, uball->oy)
  228.               || levl[uball->ox][uball->oy].typ == POOL))
  229.          || ((t = t_at(uchain->ox, uchain->oy))
  230.              && (t->ttyp == PIT || t->ttyp == SPIKED_PIT || t->ttyp == HOLE
  231.                  || t->ttyp == TRAPDOOR))) {
  232.          if (Levitation) {
  233.              You_feel("a tug from the iron ball.");
  234.              if (t)
  235.                  t->tseen = 1;
  236.          } else {
  237.              struct monst *victim;
  238.  
  239.              You("are jerked back by the iron ball!");
  240.              if ((victim = m_at(uchain->ox, uchain->oy)) != 0) {
  241.                  int tmp;
  242.  
  243.                  tmp = -2 + Luck + find_mac(victim);
  244.                  tmp += omon_adj(victim, uball, TRUE);
  245.                  if (tmp >= rnd(20))
  246.                      (void) hmon(victim, uball, HMON_DRAGGED);
  247.                  else
  248.                      miss(xname(uball), victim);
  249.  
  250.              } /* now check again in case mon died */
  251.              if (!m_at(uchain->ox, uchain->oy)) {
  252.                  u.ux = uchain->ox;
  253.                  u.uy = uchain->oy;
  254.                  newsym(u.ux0, u.uy0);
  255.              }
  256.              nomul(0);
  257.  
  258.              *bc_control = BC_BALL;
  259.              move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy);
  260.              *ballx = uchain->ox;
  261.              *bally = uchain->oy;
  262.              move_bc(0, *bc_control, *ballx, *bally, *chainx, *chainy);
  263.              spoteffects(TRUE);
  264.              return FALSE;
  265.          }
  266.      }
  267.  
  268.      *bc_control = BC_BALL | BC_CHAIN;
  269.  
  270.      move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy);
  271.      if (dist2(x, y, u.ux, u.uy) > 2) {
  272.          /* Awful case: we're still in range of the ball, so we thought we
  273.           * could only move the chain, but it turned out that the target
  274.           * square for the chain was rock, so we had to drag it instead.
  275.           * But we can't drag it either, because we teleported and are more
  276.           * than one square from our old position.  Revert to the teleport
  277.           * behavior.
  278.           */
  279.          *ballx = *chainx = x;
  280.          *bally = *chainy = y;
  281.      } else {
  282.          xchar newchainx = u.ux, newchainy = u.uy;
  283.  
  284.          /*
  285.           * Generally, chain moves to hero's previous location and ball
  286.           * moves to chain's previous location, except that we try to
  287.           * keep the chain directly between the hero and the ball.  But,
  288.           * take the simple approach if the hero's previous location or
  289.           * the potential between location is inaccessible.
  290.           */
  291.          if (dist2(x, y, uchain->ox, uchain->oy) == 4
  292.              && !IS_CHAIN_ROCK(newchainx, newchainy)) {
  293.              newchainx = (x + uchain->ox) / 2;
  294.              newchainy = (y + uchain->oy) / 2;
  295.              if (IS_CHAIN_ROCK(newchainx, newchainy)) {
  296.                  /* don't let chain move to inaccessible location */
  297.                  newchainx = u.ux;
  298.                  newchainy = u.uy;
  299.              }
  300.          }
  301.  
  302.          *ballx = uchain->ox;
  303.          *bally = uchain->oy;
  304.          *chainx = newchainx;
  305.          *chainy = newchainy;
  306.      }
  307.  #undef IS_CHAIN_ROCK
  308.      *cause_delay = TRUE;
  309.      return TRUE;
  310.  }
  311.  

drop_ball

  1.  /*
  2.   *  drop_ball()
  3.   *
  4.   *  The punished hero drops or throws her iron ball.  If the hero is
  5.   *  blind, we must reset the order and glyph.  Check for side effects.
  6.   *  This routine expects the ball to be already placed.
  7.   *
  8.   *  Should not be called while swallowed.
  9.   */
  10.  void
  11.  drop_ball(x, y)
  12.  xchar x, y;
  13.  {
  14.      if (Blind) {
  15.          /* get the order */
  16.          u.bc_order = bc_order();
  17.          /* pick up glyph */
  18.          u.bglyph = (u.bc_order) ? u.cglyph : levl[x][y].glyph;
  19.      }
  20.  
  21.      if (x != u.ux || y != u.uy) {
  22.          struct trap *t;
  23.          const char *pullmsg = "The ball pulls you out of the %s!";
  24.  
  25.          if (u.utrap && u.utraptype != TT_INFLOOR
  26.              && u.utraptype != TT_BURIEDBALL) {
  27.              switch (u.utraptype) {
  28.              case TT_PIT:
  29.                  pline(pullmsg, "pit");
  30.                  break;
  31.              case TT_WEB:
  32.                  pline(pullmsg, "web");
  33.                  pline_The("web is destroyed!");
  34.                  deltrap(t_at(u.ux, u.uy));
  35.                  break;
  36.              case TT_LAVA:
  37.                  pline(pullmsg, "lava");
  38.                  break;
  39.              case TT_BEARTRAP: {
  40.                  register long side = rn2(3) ? LEFT_SIDE : RIGHT_SIDE;
  41.                  pline(pullmsg, "bear trap");
  42.                  set_wounded_legs(side, rn1(1000, 500));
  43.                  if (!u.usteed) {
  44.                      Your("%s %s is severely damaged.",
  45.                           (side == LEFT_SIDE) ? "left" : "right",
  46.                           body_part(LEG));
  47.                      losehp(Maybe_Half_Phys(2),
  48.                             "leg damage from being pulled out of a bear trap",
  49.                             KILLED_BY);
  50.                  }
  51.                  break;
  52.              }
  53.              }
  54.              u.utrap = 0;
  55.              fill_pit(u.ux, u.uy);
  56.          }
  57.  
  58.          u.ux0 = u.ux;
  59.          u.uy0 = u.uy;
  60.          if (!Levitation && !MON_AT(x, y) && !u.utrap
  61.              && (is_pool(x, y)
  62.                  || ((t = t_at(x, y))
  63.                      && (t->ttyp == PIT || t->ttyp == SPIKED_PIT
  64.                          || t->ttyp == TRAPDOOR || t->ttyp == HOLE)))) {
  65.              u.ux = x;
  66.              u.uy = y;
  67.          } else {
  68.              u.ux = x - u.dx;
  69.              u.uy = y - u.dy;
  70.          }
  71.          vision_full_recalc = 1; /* hero has moved, recalculate vision later */
  72.  
  73.          if (Blind) {
  74.              /* drop glyph under the chain */
  75.              if (u.bc_felt & BC_CHAIN)
  76.                  levl[uchain->ox][uchain->oy].glyph = u.cglyph;
  77.              u.bc_felt = 0; /* feel nothing */
  78.              /* pick up new glyph */
  79.              u.cglyph = (u.bc_order) ? u.bglyph : levl[u.ux][u.uy].glyph;
  80.          }
  81.          movobj(uchain, u.ux, u.uy); /* has a newsym */
  82.          if (Blind) {
  83.              u.bc_order = bc_order();
  84.          }
  85.          newsym(u.ux0, u.uy0); /* clean up old position */
  86.          if (u.ux0 != u.ux || u.uy0 != u.uy) {
  87.              spoteffects(TRUE);
  88.              sokoban_guilt();
  89.          }
  90.      }
  91.  }
  92.  

litter

  1.  STATIC_OVL void
  2.  litter()
  3.  {
  4.      struct obj *otmp = invent, *nextobj;
  5.      int capacity = weight_cap();
  6.  
  7.      while (otmp) {
  8.          nextobj = otmp->nobj;
  9.          if ((otmp != uball) && (rnd(capacity) <= (int) otmp->owt)) {
  10.              if (canletgo(otmp, "")) {
  11.                  pline("%s you down the stairs.", Yobjnam2(otmp, "follow"));
  12.                  dropx(otmp);
  13.              }
  14.          }
  15.          otmp = nextobj;
  16.      }
  17.  }
  18.  

drag_down

  1.  void
  2.  drag_down()
  3.  {
  4.      boolean forward;
  5.      uchar dragchance = 3;
  6.  
  7.      /*
  8.       *  Assume that the ball falls forward if:
  9.       *
  10.       *  a) the character is wielding it, or
  11.       *  b) the character has both hands available to hold it (i.e. is
  12.       *     not wielding any weapon), or
  13.       *  c) (perhaps) it falls forward out of his non-weapon hand
  14.       */
  15.      forward = carried(uball) && (uwep == uball || !uwep || !rn2(3));
  16.  
  17.      if (carried(uball))
  18.          You("lose your grip on the iron ball.");
  19.  
  20.      if (forward) {
  21.          if (rn2(6)) {
  22.              pline_The("iron ball drags you downstairs!");
  23.              losehp(Maybe_Half_Phys(rnd(6)),
  24.                     "dragged downstairs by an iron ball", NO_KILLER_PREFIX);
  25.              litter();
  26.          }
  27.      } else {
  28.          if (rn2(2)) {
  29.              pline_The("iron ball smacks into you!");
  30.              losehp(Maybe_Half_Phys(rnd(20)), "iron ball collision",
  31.                     KILLED_BY_AN);
  32.              exercise(A_STR, FALSE);
  33.              dragchance -= 2;
  34.          }
  35.          if ((int) dragchance >= rnd(6)) {
  36.              pline_The("iron ball drags you downstairs!");
  37.              losehp(Maybe_Half_Phys(rnd(3)),
  38.                     "dragged downstairs by an iron ball", NO_KILLER_PREFIX);
  39.              exercise(A_STR, FALSE);
  40.              litter();
  41.          }
  42.      }
  43.  }
  44.  
  45.  /*ball.c*/