Source:NetHack 3.6.1/src/pickup.c

From NetHackWiki
(Redirected from Source:Ref/simple look)
Jump to: navigation, search

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

Top of file

  1.  /* NetHack 3.6	pickup.c	$NHDT-Date: 1516581051 2018/01/22 00:30:51 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.194 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  /*-Copyright (c) Robert Patrick Rankin, 2012. */
  4.  /* NetHack may be freely redistributed.  See license for details. */

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

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

  1.  
  2.  /*
  3.   *      Contains code for picking objects up, and container use.
  4.   */
  5.  
  6.  #include "hack.h"
  7.  
  8.  #define CONTAINED_SYM '>' /* from invent.c */
  9.  
  10.  STATIC_DCL void FDECL(simple_look, (struct obj *, BOOLEAN_P));
  11.  STATIC_DCL boolean FDECL(query_classes, (char *, boolean *, boolean *,
  12.                                           const char *, struct obj *,
  13.                                           BOOLEAN_P, int *));
  14.  STATIC_DCL boolean FDECL(fatal_corpse_mistake, (struct obj *, BOOLEAN_P));
  15.  STATIC_DCL void FDECL(check_here, (BOOLEAN_P));
  16.  STATIC_DCL boolean FDECL(n_or_more, (struct obj *));
  17.  STATIC_DCL boolean FDECL(all_but_uchain, (struct obj *));
  18.  #if 0 /* not used */
  19.  STATIC_DCL boolean FDECL(allow_cat_no_uchain, (struct obj *));
  20.  #endif
  21.  STATIC_DCL int FDECL(autopick, (struct obj *, int, menu_item **));
  22.  STATIC_DCL int FDECL(count_categories, (struct obj *, int));
  23.  STATIC_DCL long FDECL(carry_count, (struct obj *, struct obj *, long,
  24.                                      BOOLEAN_P, int *, int *));
  25.  STATIC_DCL int FDECL(lift_object, (struct obj *, struct obj *, long *,
  26.                                     BOOLEAN_P));
  27.  STATIC_DCL boolean FDECL(mbag_explodes, (struct obj *, int));
  28.  STATIC_DCL long FDECL(boh_loss, (struct obj *container, int));
  29.  STATIC_PTR int FDECL(in_container, (struct obj *));
  30.  STATIC_PTR int FDECL(out_container, (struct obj *));
  31.  STATIC_DCL void FDECL(removed_from_icebox, (struct obj *));
  32.  STATIC_DCL long FDECL(mbag_item_gone, (int, struct obj *));
  33.  STATIC_DCL void FDECL(observe_quantum_cat, (struct obj *));
  34.  STATIC_DCL void FDECL(explain_container_prompt, (BOOLEAN_P));
  35.  STATIC_DCL int FDECL(traditional_loot, (BOOLEAN_P));
  36.  STATIC_DCL int FDECL(menu_loot, (int, BOOLEAN_P));
  37.  STATIC_DCL char FDECL(in_or_out_menu, (const char *, struct obj *, BOOLEAN_P,
  38.                                         BOOLEAN_P, BOOLEAN_P, BOOLEAN_P));
  39.  STATIC_DCL boolean FDECL(able_to_loot, (int, int, BOOLEAN_P));
  40.  STATIC_DCL boolean NDECL(reverse_loot);
  41.  STATIC_DCL boolean FDECL(mon_beside, (int, int));
  42.  STATIC_DCL int FDECL(do_loot_cont, (struct obj **, int, int));
  43.  STATIC_DCL void FDECL(tipcontainer, (struct obj *));
  44.  
  45.  /* define for query_objlist() and autopickup() */
  46.  #define FOLLOW(curr, flags) \
  47.      (((flags) & BY_NEXTHERE) ? (curr)->nexthere : (curr)->nobj)
  48.  
  49.  /*
  50.   *  How much the weight of the given container will change when the given
  51.   *  object is removed from it.  This calculation must match the one used
  52.   *  by weight() in mkobj.c.
  53.   */
  54.  #define DELTA_CWT(cont, obj)                                      \
  55.      ((cont)->cursed ? (obj)->owt * 2 : (cont)->blessed            \
  56.                                             ? ((obj)->owt + 3) / 4 \
  57.                                             : ((obj)->owt + 1) / 2)
  58.  #define GOLD_WT(n) (((n) + 50L) / 100L)
  59.  /* if you can figure this out, give yourself a hearty pat on the back... */
  60.  #define GOLD_CAPACITY(w, n) (((w) * -100L) - ((n) + 50L) - 1L)
  61.  
  62.  /* A variable set in use_container(), to be used by the callback routines
  63.     in_container() and out_container() from askchain() and use_container().
  64.     Also used by menu_loot() and container_gone(). */
  65.  static NEARDATA struct obj *current_container;
  66.  static NEARDATA boolean abort_looting;
  67.  #define Icebox (current_container->otyp == ICE_BOX)
  68.  
  69.  static const char
  70.          moderateloadmsg[] = "You have a little trouble lifting",
  71.          nearloadmsg[] = "You have much trouble lifting",
  72.          overloadmsg[] = "You have extreme difficulty lifting";
  73.  
  74.  /* BUG: this lets you look at cockatrice corpses while blind without
  75.     touching them */

simple_look

  1.  /* much simpler version of the look-here code; used by query_classes() */
  2.  STATIC_OVL void
  3.  simple_look(otmp, here)
  4.  struct obj *otmp; /* list of objects */
  5.  boolean here;     /* flag for type of obj list linkage */
  6.  {
  7.      /* Neither of the first two cases is expected to happen, since
  8.       * we're only called after multiple classes of objects have been
  9.       * detected, hence multiple objects must be present.
  10.       */
  11.      if (!otmp) {
  12.          impossible("simple_look(null)");
  13.      } else if (!(here ? otmp->nexthere : otmp->nobj)) {
  14.          pline1(doname(otmp));
  15.      } else {
  16.          winid tmpwin = create_nhwindow(NHW_MENU);
  17.  
  18.          putstr(tmpwin, 0, "");
  19.          do {
  20.              putstr(tmpwin, 0, doname(otmp));
  21.              otmp = here ? otmp->nexthere : otmp->nobj;
  22.          } while (otmp);
  23.          display_nhwindow(tmpwin, TRUE);
  24.          destroy_nhwindow(tmpwin);
  25.      }
  26.  }
  27.  

collect_obj_classes

  1.  int
  2.  collect_obj_classes(ilets, otmp, here, filter, itemcount)
  3.  char ilets[];
  4.  register struct obj *otmp;
  5.  boolean here;
  6.  boolean FDECL((*filter), (OBJ_P));
  7.  int *itemcount;
  8.  {
  9.      register int iletct = 0;
  10.      register char c;
  11.  
  12.      *itemcount = 0;
  13.      ilets[iletct] = '\0'; /* terminate ilets so that index() will work */
  14.      while (otmp) {
  15.          c = def_oc_syms[(int) otmp->oclass].sym;
  16.          if (!index(ilets, c) && (!filter || (*filter)(otmp)))
  17.              ilets[iletct++] = c, ilets[iletct] = '\0';
  18.          *itemcount += 1;
  19.          otmp = here ? otmp->nexthere : otmp->nobj;
  20.      }
  21.  
  22.      return iletct;
  23.  }
  24.  

query_classes

  1.  /*
  2.   * Suppose some '?' and '!' objects are present, but '/' objects aren't:
  3.   *      "a" picks all items without further prompting;
  4.   *      "A" steps through all items, asking one by one;
  5.   *      "?" steps through '?' items, asking, and ignores '!' ones;
  6.   *      "/" becomes 'A', since no '/' present;
  7.   *      "?a" or "a?" picks all '?' without further prompting;
  8.   *      "/a" or "a/" becomes 'A' since there aren't any '/'
  9.   *          (bug fix:  3.1.0 thru 3.1.3 treated it as "a");
  10.   *      "?/a" or "a?/" or "/a?",&c picks all '?' even though no '/'
  11.   *          (ie, treated as if it had just been "?a").
  12.   */
  13.  STATIC_OVL boolean
  14.  query_classes(oclasses, one_at_a_time, everything, action, objs, here,
  15.                menu_on_demand)
  16.  char oclasses[];
  17.  boolean *one_at_a_time, *everything;
  18.  const char *action;
  19.  struct obj *objs;
  20.  boolean here;
  21.  int *menu_on_demand;
  22.  {
  23.      char ilets[36], inbuf[BUFSZ] = DUMMY; /* FIXME: hardcoded ilets[] length */
  24.      int iletct, oclassct;
  25.      boolean not_everything, filtered;
  26.      char qbuf[QBUFSZ];
  27.      boolean m_seen;
  28.      int itemcount, bcnt, ucnt, ccnt, xcnt, ocnt;
  29.  
  30.      oclasses[oclassct = 0] = '\0';
  31.      *one_at_a_time = *everything = m_seen = FALSE;
  32.      if (menu_on_demand)
  33.          *menu_on_demand = 0;
  34.      iletct = collect_obj_classes(ilets, objs, here,
  35.                                   (boolean FDECL((*), (OBJ_P))) 0, &itemcount);
  36.      if (iletct == 0)
  37.          return FALSE;
  38.  
  39.      if (iletct == 1) {
  40.          oclasses[0] = def_char_to_objclass(ilets[0]);
  41.          oclasses[1] = '\0';
  42.      } else { /* more than one choice available */
  43.          /* additional choices */
  44.          ilets[iletct++] = ' ';
  45.          ilets[iletct++] = 'a';
  46.          ilets[iletct++] = 'A';
  47.          ilets[iletct++] = (objs == invent ? 'i' : ':');
  48.      }
  49.      if (itemcount && menu_on_demand)
  50.          ilets[iletct++] = 'm';
  51.      if (count_unpaid(objs))
  52.          ilets[iletct++] = 'u';
  53.  
  54.      tally_BUCX(objs, here, &bcnt, &ucnt, &ccnt, &xcnt, &ocnt);
  55.      if (bcnt)
  56.          ilets[iletct++] = 'B';
  57.      if (ucnt)
  58.          ilets[iletct++] = 'U';
  59.      if (ccnt)
  60.          ilets[iletct++] = 'C';
  61.      if (xcnt)
  62.          ilets[iletct++] = 'X';
  63.      ilets[iletct] = '\0';
  64.  
  65.      if (iletct > 1) {
  66.          const char *where = 0;
  67.          char sym, oc_of_sym, *p;
  68.  
  69.      ask_again:
  70.          oclasses[oclassct = 0] = '\0';
  71.          *one_at_a_time = *everything = FALSE;
  72.          not_everything = filtered = FALSE;
  73.          Sprintf(qbuf, "What kinds of thing do you want to %s? [%s]", action,
  74.                  ilets);
  75.          getlin(qbuf, inbuf);
  76.          if (*inbuf == '\033')
  77.              return FALSE;
  78.  
  79.          for (p = inbuf; (sym = *p++) != 0; ) {
  80.              if (sym == ' ')
  81.                  continue;
  82.              else if (sym == 'A')
  83.                  *one_at_a_time = TRUE;
  84.              else if (sym == 'a')
  85.                  *everything = TRUE;
  86.              else if (sym == ':') {
  87.                  simple_look(objs, here); /* dumb if objs==invent */
  88.                  /* if we just scanned the contents of a container
  89.                     then mark it as having known contents */
  90.                  if (objs->where == OBJ_CONTAINED)
  91.                      objs->ocontainer->cknown = 1;
  92.                  goto ask_again;
  93.              } else if (sym == 'i') {
  94.                  (void) display_inventory((char *) 0, TRUE);
  95.                  goto ask_again;
  96.              } else if (sym == 'm') {
  97.                  m_seen = TRUE;
  98.              } else if (index("uBUCX", sym)) {
  99.                  add_valid_menu_class(sym); /* 'u' or 'B','U','C',or 'X' */
  100.                  filtered = TRUE;
  101.              } else {
  102.                  oc_of_sym = def_char_to_objclass(sym);
  103.                  if (index(ilets, sym)) {
  104.                      add_valid_menu_class(oc_of_sym);
  105.                      oclasses[oclassct++] = oc_of_sym;
  106.                      oclasses[oclassct] = '\0';
  107.                  } else {
  108.                      if (!where)
  109.                          where = !strcmp(action, "pick up") ? "here"
  110.                                  : !strcmp(action, "take out") ? "inside" : "";
  111.                      if (*where)
  112.                          There("are no %c's %s.", sym, where);
  113.                      else
  114.                          You("have no %c's.", sym);
  115.                      not_everything = TRUE;
  116.                  }
  117.              }
  118.          } /* for p:sym in inbuf */
  119.  
  120.          if (m_seen && menu_on_demand) {
  121.              *menu_on_demand = (((*everything || !oclassct) && !filtered)
  122.                                 ? -2 : -3);
  123.              return FALSE;
  124.          }
  125.          if (!oclassct && (!*everything || not_everything)) {
  126.              /* didn't pick anything,
  127.                 or tried to pick something that's not present */
  128.              *one_at_a_time = TRUE; /* force 'A' */
  129.              *everything = FALSE;   /* inhibit 'a' */
  130.          }
  131.      }
  132.      return TRUE;
  133.  }
  134.  

fatal_corpse_mistake

  1.  /* check whether hero is bare-handedly touching a cockatrice corpse */
  2.  STATIC_OVL boolean
  3.  fatal_corpse_mistake(obj, remotely)
  4.  struct obj *obj;
  5.  boolean remotely;
  6.  {
  7.      if (uarmg || remotely || obj->otyp != CORPSE
  8.          || !touch_petrifies(&mons[obj->corpsenm]) || Stone_resistance)
  9.          return FALSE;
  10.  
  11.      if (poly_when_stoned(youmonst.data) && polymon(PM_STONE_GOLEM)) {
  12.          display_nhwindow(WIN_MESSAGE, FALSE); /* --More-- */
  13.          return FALSE;
  14.      }
  15.  
  16.      pline("Touching %s is a fatal mistake.",
  17.            corpse_xname(obj, (const char *) 0, CXN_SINGULAR | CXN_ARTICLE));
  18.      instapetrify(killer_xname(obj));
  19.      return TRUE;
  20.  }
  21.  

rider_corpse_revival

  1.  /* attempting to manipulate a Rider's corpse triggers its revival */
  2.  boolean
  3.  rider_corpse_revival(obj, remotely)
  4.  struct obj *obj;
  5.  boolean remotely;
  6.  {
  7.      if (!obj || obj->otyp != CORPSE || !is_rider(&mons[obj->corpsenm]))
  8.          return FALSE;
  9.  
  10.      pline("At your %s, the corpse suddenly moves...",
  11.            remotely ? "attempted acquisition" : "touch");
  12.      (void) revive_corpse(obj);
  13.      exercise(A_WIS, FALSE);
  14.      return TRUE;
  15.  }
  16.  

check_here

  1.  /* look at the objects at our location, unless there are too many of them */
  2.  STATIC_OVL void
  3.  check_here(picked_some)
  4.  boolean picked_some;
  5.  {
  6.      register struct obj *obj;
  7.      register int ct = 0;
  8.  
  9.      /* count the objects here */
  10.      for (obj = level.objects[u.ux][u.uy]; obj; obj = obj->nexthere) {
  11.          if (obj != uchain)
  12.              ct++;
  13.      }
  14.  
  15.      /* If there are objects here, take a look. */
  16.      if (ct) {
  17.          if (context.run)
  18.              nomul(0);
  19.          flush_screen(1);
  20.          (void) look_here(ct, picked_some);
  21.      } else {
  22.          read_engr_at(u.ux, u.uy);
  23.      }
  24.  }
  25.  
  26.  /* Value set by query_objlist() for n_or_more(). */
  27.  static long val_for_n_or_more;
  28.  

n_or_more

  1.  /* query_objlist callback: return TRUE if obj's count is >= reference value */
  2.  STATIC_OVL boolean
  3.  n_or_more(obj)
  4.  struct obj *obj;
  5.  {
  6.      if (obj == uchain)
  7.          return FALSE;
  8.      return (boolean) (obj->quan >= val_for_n_or_more);
  9.  }
  10.  
  11.  /* list of valid menu classes for query_objlist() and allow_category callback
  12.     (with room for all object classes, 'u'npaid, BUCX, and terminator) */
  13.  static char valid_menu_classes[MAXOCLASSES + 1 + 4 + 1];
  14.  static boolean class_filter, bucx_filter, shop_filter;
  15.  

menu_class_present

  1.  /* check valid_menu_classes[] for an entry; also used by askchain() */
  2.  boolean
  3.  menu_class_present(c)
  4.  int c;
  5.  {
  6.      return (c && index(valid_menu_classes, c)) ? TRUE : FALSE;
  7.  }
  8.  

add_valid_menu_class

  1.  void
  2.  add_valid_menu_class(c)
  3.  int c;
  4.  {
  5.      static int vmc_count = 0;
  6.  
  7.      if (c == 0) { /* reset */
  8.          vmc_count = 0;
  9.          class_filter = bucx_filter = shop_filter = FALSE;
  10.      } else if (!menu_class_present(c)) {
  11.          valid_menu_classes[vmc_count++] = (char) c;
  12.          /* categorize the new class */
  13.          switch (c) {
  14.          case 'B':
  15.          case 'U':
  16.          case 'C': /*FALLTHRU*/
  17.          case 'X':
  18.              bucx_filter = TRUE;
  19.              break;
  20.          case 'u':
  21.              shop_filter = TRUE;
  22.              break;
  23.          default:
  24.              class_filter = TRUE;
  25.              break;
  26.          }
  27.      }
  28.      valid_menu_classes[vmc_count] = '\0';
  29.  }
  30.  

all_but_uchain

  1.  /* query_objlist callback: return TRUE if not uchain */
  2.  STATIC_OVL boolean
  3.  all_but_uchain(obj)
  4.  struct obj *obj;
  5.  {
  6.      return (boolean) (obj != uchain);
  7.  }
  8.  

allow_all

  1.  /* query_objlist callback: return TRUE */
  2.  /*ARGUSED*/
  3.  boolean
  4.  allow_all(obj)
  5.  struct obj *obj UNUSED;
  6.  {
  7.      return TRUE;
  8.  }
  9.  

allow_category

  1.  boolean
  2.  allow_category(obj)
  3.  struct obj *obj;
  4.  {
  5.      /* For coins, if any class filter is specified, accept if coins
  6.       * are included regardless of whether either unpaid or BUC-status
  7.       * is also specified since player has explicitly requested coins.
  8.       * If no class filtering is specified but bless/curse state is,
  9.       * coins are either unknown or uncursed based on an option setting.
  10.       */
  11.      if (obj->oclass == COIN_CLASS)
  12.          return class_filter
  13.                   ? (index(valid_menu_classes, COIN_CLASS) ? TRUE : FALSE)
  14.                   : shop_filter /* coins are never unpaid, but check anyway */
  15.                      ? (obj->unpaid ? TRUE : FALSE)
  16.                      : bucx_filter
  17.                         ? (index(valid_menu_classes, iflags.goldX ? 'X' : 'U')
  18.                            ? TRUE : FALSE)
  19.                         : TRUE; /* catchall: no filters specified, so accept */
  20.  
  21.      if (Role_if(PM_PRIEST))
  22.          obj->bknown = TRUE;
  23.  
  24.      /*
  25.       * There are three types of filters possible and the first and
  26.       * third can have more than one entry:
  27.       *  1) object class (armor, potion, &c);
  28.       *  2) unpaid shop item;
  29.       *  3) bless/curse state (blessed, uncursed, cursed, BUC-unknown).
  30.       * When only one type is present, the situation is simple:
  31.       * to be accepted, obj's status must match one of the entries.
  32.       * When more than one type is present, the obj will now only
  33.       * be accepted when it matches one entry of each type.
  34.       * So ?!B will accept blessed scrolls or potions, and [u will
  35.       * accept unpaid armor.  (In 3.4.3, an object was accepted by
  36.       * this filter if it met any entry of any type, so ?!B resulted
  37.       * in accepting all scrolls and potions regardless of bless/curse
  38.       * state plus all blessed non-scroll, non-potion objects.)
  39.       */
  40.  
  41.      /* if class is expected but obj's class is not in the list, reject */
  42.      if (class_filter && !index(valid_menu_classes, obj->oclass))
  43.          return FALSE;
  44.      /* if unpaid is expected and obj isn't unpaid, reject (treat a container
  45.         holding any unpaid object as unpaid even if isn't unpaid itself) */
  46.      if (shop_filter && !obj->unpaid
  47.          && !(Has_contents(obj) && count_unpaid(obj->cobj) > 0))
  48.          return FALSE;
  49.      /* check for particular bless/curse state */
  50.      if (bucx_filter) {
  51.          /* first categorize this object's bless/curse state */
  52.          char bucx = !obj->bknown ? 'X'
  53.                        : obj->blessed ? 'B' : obj->cursed ? 'C' : 'U';
  54.  
  55.          /* if its category is not in the list, reject */
  56.          if (!index(valid_menu_classes, bucx))
  57.              return FALSE;
  58.      }
  59.      /* obj didn't fail any of the filter checks, so accept */
  60.      return TRUE;
  61.  }
  62.  
  63.  #if 0 /* not used */
  64.  /* query_objlist callback: return TRUE if valid category (class), no uchain */
  65.  STATIC_OVL boolean
  66.  allow_cat_no_uchain(obj)
  67.  struct obj *obj;
  68.  {
  69.      if (obj != uchain
  70.          && ((index(valid_menu_classes, 'u') && obj->unpaid)
  71.              || index(valid_menu_classes, obj->oclass)))
  72.          return TRUE;
  73.      return FALSE;
  74.  }
  75.  #endif
  76.  

is_worn_by_type

  1.  /* query_objlist callback: return TRUE if valid class and worn */
  2.  boolean
  3.  is_worn_by_type(otmp)
  4.  register struct obj *otmp;
  5.  {
  6.      return (is_worn(otmp) && allow_category(otmp)) ? TRUE : FALSE;
  7.  }
  8.  

pickup

  1.  /*
  2.   * Have the hero pick things from the ground
  3.   * or a monster's inventory if swallowed.
  4.   *
  5.   * Arg what:
  6.   *      >0  autopickup
  7.   *      =0  interactive
  8.   *      <0  pickup count of something
  9.   *
  10.   * Returns 1 if tried to pick something up, whether
  11.   * or not it succeeded.
  12.   */
  13.  int
  14.  pickup(what)
  15.  int what; /* should be a long */
  16.  {
  17.      int i, n, res, count, n_tried = 0, n_picked = 0;
  18.      menu_item *pick_list = (menu_item *) 0;
  19.      boolean autopickup = what > 0;
  20.      struct obj **objchain_p;
  21.      int traverse_how;
  22.  
  23.      /* we might have arrived here while fainted or sleeping, via
  24.         random teleport or levitation timeout; if so, skip check_here
  25.         and read_engr_at in addition to bypassing autopickup itself
  26.         [probably ought to check whether hero is using a cockatrice
  27.         corpse for a pillow here... (also at initial faint/sleep)] */
  28.      if (autopickup && multi < 0 && unconscious())
  29.          return 0;
  30.  
  31.      if (what < 0) /* pick N of something */
  32.          count = -what;
  33.      else /* pick anything */
  34.          count = 0;
  35.  
  36.      if (!u.uswallow) {
  37.          struct trap *ttmp;
  38.  
  39.          /* no auto-pick if no-pick move, nothing there, or in a pool */
  40.          if (autopickup && (context.nopick || !OBJ_AT(u.ux, u.uy)
  41.                             || (is_pool(u.ux, u.uy) && !Underwater)
  42.                             || is_lava(u.ux, u.uy))) {
  43.              read_engr_at(u.ux, u.uy);
  44.              return 0;
  45.          }
  46.          /* no pickup if levitating & not on air or water level */
  47.          if (!can_reach_floor(TRUE)) {
  48.              if ((multi && !context.run) || (autopickup && !flags.pickup)
  49.                  || ((ttmp = t_at(u.ux, u.uy)) != 0
  50.                      && uteetering_at_seen_pit(ttmp)))
  51.                  read_engr_at(u.ux, u.uy);
  52.              return 0;
  53.          }
  54.          /* multi && !context.run means they are in the middle of some other
  55.           * action, or possibly paralyzed, sleeping, etc.... and they just
  56.           * teleported onto the object.  They shouldn't pick it up.
  57.           */
  58.          if ((multi && !context.run) || (autopickup && !flags.pickup)) {
  59.              check_here(FALSE);
  60.              return 0;
  61.          }
  62.          if (notake(youmonst.data)) {
  63.              if (!autopickup)
  64.                  You("are physically incapable of picking anything up.");
  65.              else
  66.                  check_here(FALSE);
  67.              return 0;
  68.          }
  69.  
  70.          /* if there's anything here, stop running */
  71.          if (OBJ_AT(u.ux, u.uy) && context.run && context.run != 8
  72.              && !context.nopick)
  73.              nomul(0);
  74.      }
  75.  
  76.      add_valid_menu_class(0); /* reset */
  77.      if (!u.uswallow) {
  78.          objchain_p = &level.objects[u.ux][u.uy];
  79.          traverse_how = BY_NEXTHERE;
  80.      } else {
  81.          objchain_p = &u.ustuck->minvent;
  82.          traverse_how = 0; /* nobj */
  83.      }
  84.      /*
  85.       * Start the actual pickup process.  This is split into two main
  86.       * sections, the newer menu and the older "traditional" methods.
  87.       * Automatic pickup has been split into its own menu-style routine
  88.       * to make things less confusing.
  89.       */
  90.      if (autopickup) {
  91.          n = autopick(*objchain_p, traverse_how, &pick_list);
  92.          goto menu_pickup;
  93.      }
  94.  
  95.      if (flags.menu_style != MENU_TRADITIONAL || iflags.menu_requested) {
  96.          /* use menus exclusively */
  97.          traverse_how |= AUTOSELECT_SINGLE | INVORDER_SORT;
  98.          if (count) { /* looking for N of something */
  99.              char qbuf[QBUFSZ];
  100.  
  101.              Sprintf(qbuf, "Pick %d of what?", count);
  102.              val_for_n_or_more = count; /* set up callback selector */
  103.              n = query_objlist(qbuf, objchain_p, traverse_how,
  104.                                &pick_list, PICK_ONE, n_or_more);
  105.              /* correct counts, if any given */
  106.              for (i = 0; i < n; i++)
  107.                  pick_list[i].count = count;
  108.          } else {
  109.              n = query_objlist("Pick up what?", objchain_p,
  110.                                (traverse_how | FEEL_COCKATRICE),
  111.                                &pick_list, PICK_ANY, all_but_uchain);
  112.          }
  113.  
  114.      menu_pickup:
  115.          n_tried = n;
  116.          for (n_picked = i = 0; i < n; i++) {
  117.              res = pickup_object(pick_list[i].item.a_obj, pick_list[i].count,
  118.                                  FALSE);
  119.              if (res < 0)
  120.                  break; /* can't continue */
  121.              n_picked += res;
  122.          }
  123.          if (pick_list)
  124.              free((genericptr_t) pick_list);
  125.  
  126.      } else {
  127.          /* old style interface */
  128.          int ct = 0;
  129.          long lcount;
  130.          boolean all_of_a_type, selective, bycat;
  131.          char oclasses[MAXOCLASSES + 10]; /* +10: room for B,U,C,X plus slop */
  132.          struct obj *obj, *obj2;
  133.  
  134.          oclasses[0] = '\0';   /* types to consider (empty for all) */
  135.          all_of_a_type = TRUE; /* take all of considered types */
  136.          selective = FALSE;    /* ask for each item */
  137.  
  138.          /* check for more than one object */
  139.          for (obj = *objchain_p; obj; obj = FOLLOW(obj, traverse_how))
  140.              ct++;
  141.  
  142.          if (ct == 1 && count) {
  143.              /* if only one thing, then pick it */
  144.              obj = *objchain_p;
  145.              lcount = min(obj->quan, (long) count);
  146.              n_tried++;
  147.              if (pickup_object(obj, lcount, FALSE) > 0)
  148.                  n_picked++; /* picked something */
  149.              goto end_query;
  150.  
  151.          } else if (ct >= 2) {
  152.              int via_menu = 0;
  153.  
  154.              There("are %s objects here.", (ct <= 10) ? "several" : "many");
  155.              if (!query_classes(oclasses, &selective, &all_of_a_type,
  156.                                 "pick up", *objchain_p,
  157.                                 (traverse_how & BY_NEXTHERE) ? TRUE : FALSE,
  158.                                 &via_menu)) {
  159.                  if (!via_menu)
  160.                      goto pickupdone;
  161.                  if (selective)
  162.                      traverse_how |= INVORDER_SORT;
  163.                  n = query_objlist("Pick up what?", objchain_p, traverse_how,
  164.                                    &pick_list, PICK_ANY,
  165.                                    (via_menu == -2) ? allow_all
  166.                                                     : allow_category);
  167.                  goto menu_pickup;
  168.              }
  169.          }
  170.          bycat = (menu_class_present('B') || menu_class_present('U')
  171.                   || menu_class_present('C') || menu_class_present('X'));
  172.  
  173.          for (obj = *objchain_p; obj; obj = obj2) {
  174.              obj2 = FOLLOW(obj, traverse_how);
  175.              if (bycat ? !allow_category(obj)
  176.                        : (!selective && oclasses[0]
  177.                           && !index(oclasses, obj->oclass)))
  178.                  continue;
  179.  
  180.              lcount = -1L;
  181.              if (!all_of_a_type) {
  182.                  char qbuf[BUFSZ];
  183.  
  184.                  (void) safe_qbuf(qbuf, "Pick up ", "?", obj, doname,
  185.                                   ansimpleoname, something);
  186.                  switch ((obj->quan < 2L) ? ynaq(qbuf) : ynNaq(qbuf)) {
  187.                  case 'q':
  188.                      goto end_query; /* out 2 levels */
  189.                  case 'n':
  190.                      continue;
  191.                  case 'a':
  192.                      all_of_a_type = TRUE;
  193.                      if (selective) {
  194.                          selective = FALSE;
  195.                          oclasses[0] = obj->oclass;
  196.                          oclasses[1] = '\0';
  197.                      }
  198.                      break;
  199.                  case '#': /* count was entered */
  200.                      if (!yn_number)
  201.                          continue; /* 0 count => No */
  202.                      lcount = (long) yn_number;
  203.                      if (lcount > obj->quan)
  204.                          lcount = obj->quan;
  205.                      /*FALLTHRU*/
  206.                  default: /* 'y' */
  207.                      break;
  208.                  }
  209.              }
  210.              if (lcount == -1L)
  211.                  lcount = obj->quan;
  212.  
  213.              n_tried++;
  214.              if ((res = pickup_object(obj, lcount, FALSE)) < 0)
  215.                  break;
  216.              n_picked += res;
  217.          }
  218.      end_query:
  219.          ; /* statement required after label */
  220.      }
  221.  
  222.      if (!u.uswallow) {
  223.          if (hides_under(youmonst.data))
  224.              (void) hideunder(&youmonst);
  225.  
  226.          /* position may need updating (invisible hero) */
  227.          if (n_picked)
  228.              newsym_force(u.ux, u.uy);
  229.  
  230.          /* check if there's anything else here after auto-pickup is done */
  231.          if (autopickup)
  232.              check_here(n_picked > 0);
  233.      }
  234.   pickupdone:
  235.      add_valid_menu_class(0); /* reset */
  236.      return (n_tried > 0);
  237.  }
  238.  

is_autopickup_exception

  1.  boolean
  2.  is_autopickup_exception(obj, grab)
  3.  struct obj *obj;
  4.  boolean grab; /* forced pickup, rather than forced leave behind? */
  5.  {
  6.      /*
  7.       *  Does the text description of this match an exception?
  8.       */
  9.      struct autopickup_exception
  10.          *ape = (grab) ? iflags.autopickup_exceptions[AP_GRAB]
  11.                        : iflags.autopickup_exceptions[AP_LEAVE];
  12.  
  13.      if (ape) {
  14.          char *objdesc = makesingular(doname(obj));
  15.  
  16.          while (ape) {
  17.              if (regex_match(objdesc, ape->regex))
  18.                  return TRUE;
  19.              ape = ape->next;
  20.          }
  21.      }
  22.      return FALSE;
  23.  }
  24.  

autopick_testobj

  1.  boolean
  2.  autopick_testobj(otmp, calc_costly)
  3.  struct obj *otmp;
  4.  boolean calc_costly;
  5.  {
  6.      static boolean costly = FALSE;
  7.      const char *otypes = flags.pickup_types;
  8.      boolean pickit;
  9.  
  10.      /* calculate 'costly' just once for a given autopickup operation */
  11.      if (calc_costly)
  12.          costly = (otmp->where == OBJ_FLOOR
  13.                    && costly_spot(otmp->ox, otmp->oy));
  14.  
  15.      /* first check: reject if an unpaid item in a shop */
  16.      if (costly && !otmp->no_charge)
  17.          return FALSE;
  18.  
  19.      /* check for pickup_types */
  20.      pickit = (!*otypes || index(otypes, otmp->oclass));
  21.      /* check for "always pick up */
  22.      if (!pickit)
  23.          pickit = is_autopickup_exception(otmp, TRUE);
  24.      /* then for "never pick up */
  25.      if (pickit)
  26.          pickit = !is_autopickup_exception(otmp, FALSE);
  27.      /* pickup_thrown overrides pickup_types and exceptions */
  28.      if (!pickit)
  29.          pickit = (flags.pickup_thrown && otmp->was_thrown);
  30.      return pickit;
  31.  }
  32.  

autopick

  1.  /*
  2.   * Pick from the given list using flags.pickup_types.  Return the number
  3.   * of items picked (not counts).  Create an array that returns pointers
  4.   * and counts of the items to be picked up.  If the number of items
  5.   * picked is zero, the pickup list is left alone.  The caller of this
  6.   * function must free the pickup list.
  7.   */
  8.  STATIC_OVL int
  9.  autopick(olist, follow, pick_list)
  10.  struct obj *olist;     /* the object list */
  11.  int follow;            /* how to follow the object list */
  12.  menu_item **pick_list; /* list of objects and counts to pick up */
  13.  {
  14.      menu_item *pi; /* pick item */
  15.      struct obj *curr;
  16.      int n;
  17.      boolean check_costly = TRUE;
  18.  
  19.      /* first count the number of eligible items */
  20.      for (n = 0, curr = olist; curr; curr = FOLLOW(curr, follow)) {
  21.          if (autopick_testobj(curr, check_costly))
  22.              ++n;
  23.          check_costly = FALSE; /* only need to check once per autopickup */
  24.      }
  25.  
  26.      if (n) {
  27.          *pick_list = pi = (menu_item *) alloc(sizeof (menu_item) * n);
  28.          for (n = 0, curr = olist; curr; curr = FOLLOW(curr, follow)) {
  29.              if (autopick_testobj(curr, FALSE)) {
  30.                  pi[n].item.a_obj = curr;
  31.                  pi[n].count = curr->quan;
  32.                  n++;
  33.              }
  34.          }
  35.      }
  36.      return n;
  37.  }
  38.  

query_objlist

  1.  /*
  2.   * Put up a menu using the given object list.  Only those objects on the
  3.   * list that meet the approval of the allow function are displayed.  Return
  4.   * a count of the number of items selected, as well as an allocated array of
  5.   * menu_items, containing pointers to the objects selected and counts.  The
  6.   * returned counts are guaranteed to be in bounds and non-zero.
  7.   *
  8.   * Query flags:
  9.   *      BY_NEXTHERE       - Follow object list via nexthere instead of nobj.
  10.   *      AUTOSELECT_SINGLE - Don't ask if only 1 object qualifies - just
  11.   *                          use it.
  12.   *      USE_INVLET        - Use object's invlet.
  13.   *      INVORDER_SORT     - Use hero's pack order.
  14.   *      INCLUDE_HERO      - Showing engulfer's invent; show hero too.
  15.   *      SIGNAL_NOMENU     - Return -1 rather than 0 if nothing passes "allow".
  16.   *      SIGNAL_ESCAPE     - Return -1 rather than 0 if player uses ESC to
  17.   *                          pick nothing.
  18.   *      FEEL_COCKATRICE   - touch corpse.
  19.   */
  20.  int
  21.  query_objlist(qstr, olist_p, qflags, pick_list, how, allow)
  22.  const char *qstr;                 /* query string */
  23.  struct obj **olist_p;             /* the list to pick from */
  24.  int qflags;                       /* options to control the query */
  25.  menu_item **pick_list;            /* return list of items picked */
  26.  int how;                          /* type of query */
  27.  boolean FDECL((*allow), (OBJ_P)); /* allow function */
  28.  {
  29.      int i, n;
  30.      winid win;
  31.      struct obj *curr, *last, fake_hero_object, *olist = *olist_p;
  32.      char *pack;
  33.      anything any;
  34.      boolean printed_type_name, first,
  35.              sorted = (qflags & INVORDER_SORT) != 0,
  36.              engulfer = (qflags & INCLUDE_HERO) != 0;
  37.  
  38.      *pick_list = (menu_item *) 0;
  39.      if (!olist && !engulfer)
  40.          return 0;
  41.  
  42.      /* count the number of items allowed */
  43.      for (n = 0, last = 0, curr = olist; curr; curr = FOLLOW(curr, qflags))
  44.          if ((*allow)(curr)) {
  45.              last = curr;
  46.              n++;
  47.          }
  48.      if (engulfer) {
  49.          ++n;
  50.          /* don't autoselect swallowed hero if it's the only choice */
  51.          qflags &= ~AUTOSELECT_SINGLE;
  52.      }
  53.  
  54.      if (n == 0) /* nothing to pick here */
  55.          return (qflags & SIGNAL_NOMENU) ? -1 : 0;
  56.  
  57.      if (n == 1 && (qflags & AUTOSELECT_SINGLE)) {
  58.          *pick_list = (menu_item *) alloc(sizeof (menu_item));
  59.          (*pick_list)->item.a_obj = last;
  60.          (*pick_list)->count = last->quan;
  61.          return 1;
  62.      }
  63.  
  64.      if (sorted || flags.sortloot != 'n') {
  65.          sortloot(&olist,
  66.                   (((flags.sortloot == 'f'
  67.                      || (flags.sortloot == 'l' && !(qflags & USE_INVLET)))
  68.                     ? SORTLOOT_LOOT
  69.                     : (qflags & USE_INVLET) ? SORTLOOT_INVLET : 0)
  70.                    | (flags.sortpack ? SORTLOOT_PACK : 0)),
  71.                   (qflags & BY_NEXTHERE) ? TRUE : FALSE);
  72.          *olist_p = olist;
  73.      }
  74.  
  75.      win = create_nhwindow(NHW_MENU);
  76.      start_menu(win);
  77.      any = zeroany;
  78.      /*
  79.       * Run through the list and add the objects to the menu.  If
  80.       * INVORDER_SORT is set, we'll run through the list once for
  81.       * each type so we can group them.  The allow function will only
  82.       * be called once per object in the list.
  83.       */
  84.      pack = flags.inv_order;
  85.      first = TRUE;
  86.      do {
  87.          printed_type_name = FALSE;
  88.          for (curr = olist; curr; curr = FOLLOW(curr, qflags)) {
  89.              if (sorted && curr->oclass != *pack)
  90.                  continue;
  91.              if ((qflags & FEEL_COCKATRICE) && curr->otyp == CORPSE
  92.                  && will_feel_cockatrice(curr, FALSE)) {
  93.                  destroy_nhwindow(win); /* stop the menu and revert */
  94.                  (void) look_here(0, FALSE);
  95.                  return 0;
  96.              }
  97.              if ((*allow)(curr)) {
  98.                  /* if sorting, print type name (once only) */
  99.                  if (sorted && !printed_type_name) {
  100.                      any = zeroany;
  101.                      add_menu(win, NO_GLYPH, &any, 0, 0, iflags.menu_headings,
  102.                               let_to_name(*pack, FALSE,
  103.                                           ((how != PICK_NONE)
  104.                                            && iflags.menu_head_objsym)),
  105.                               MENU_UNSELECTED);
  106.                      printed_type_name = TRUE;
  107.                  }
  108.  
  109.                  any.a_obj = curr;
  110.                  add_menu(win, obj_to_glyph(curr), &any,
  111.                           (qflags & USE_INVLET) ? curr->invlet
  112.                             : (first && curr->oclass == COIN_CLASS) ? '$' : 0,
  113.                           def_oc_syms[(int) objects[curr->otyp].oc_class].sym,
  114.                           ATR_NONE, doname_with_price(curr), MENU_UNSELECTED);
  115.                  first = FALSE;
  116.              }
  117.          }
  118.          pack++;
  119.      } while (sorted && *pack);
  120.  
  121.      if (engulfer) {
  122.          char buf[BUFSZ];
  123.  
  124.          any = zeroany;
  125.          if (sorted && n > 1) {
  126.              Sprintf(buf, "%s Creatures",
  127.                      is_animal(u.ustuck->data) ? "Swallowed" : "Engulfed");
  128.              add_menu(win, NO_GLYPH, &any, 0, 0, iflags.menu_headings, buf,
  129.                       MENU_UNSELECTED);
  130.          }
  131.          fake_hero_object = zeroobj;
  132.          fake_hero_object.quan = 1L; /* not strictly necessary... */
  133.          any.a_obj = &fake_hero_object;
  134.          add_menu(win, mon_to_glyph(&youmonst), &any,
  135.                   /* fake inventory letter, no group accelerator */
  136.                   CONTAINED_SYM, 0, ATR_NONE, an(self_lookat(buf)),
  137.                   MENU_UNSELECTED);
  138.      }
  139.  
  140.      end_menu(win, qstr);
  141.      n = select_menu(win, how, pick_list);
  142.      destroy_nhwindow(win);
  143.  
  144.      if (n > 0) {
  145.          menu_item *mi;
  146.          int k;
  147.  
  148.          /* fix up counts:  -1 means no count used => pick all;
  149.             if fake_hero_object was picked, discard that choice */
  150.          for (i = k = 0, mi = *pick_list; i < n; i++, mi++) {
  151.              if (mi->item.a_obj == &fake_hero_object)
  152.                  continue;
  153.              if (mi->count == -1L || mi->count > mi->item.a_obj->quan)
  154.                  mi->count = mi->item.a_obj->quan;
  155.              if (k < i)
  156.                  (*pick_list)[k] = *mi;
  157.              ++k;
  158.          }
  159.          if (!k) {
  160.              /* fake_hero was only choice so discard whole list */
  161.              free((genericptr_t) *pick_list);
  162.              *pick_list = 0;
  163.              n = 0;
  164.          } else if (k < n) {
  165.              /* other stuff plus fake_hero; last slot is now unused */
  166.              (*pick_list)[k].item = zeroany;
  167.              (*pick_list)[k].count = 0L;
  168.              n = k;
  169.          }
  170.      } else if (n < 0) {
  171.          /* -1 is used for SIGNAL_NOMENU, so callers don't expect it
  172.             to indicate that the player declined to make a choice */
  173.          n = (qflags & SIGNAL_ESCAPE) ? -2 : 0;
  174.      }
  175.      return n;
  176.  }
  177.  

query_category

  1.  /*
  2.   * allow menu-based category (class) selection (for Drop,take off etc.)
  3.   *
  4.   */
  5.  int
  6.  query_category(qstr, olist, qflags, pick_list, how)
  7.  const char *qstr;      /* query string */
  8.  struct obj *olist;     /* the list to pick from */
  9.  int qflags;            /* behaviour modification flags */
  10.  menu_item **pick_list; /* return list of items picked */
  11.  int how;               /* type of query */
  12.  {
  13.      int n;
  14.      winid win;
  15.      struct obj *curr;
  16.      char *pack;
  17.      anything any;
  18.      boolean collected_type_name;
  19.      char invlet;
  20.      int ccount;
  21.      boolean FDECL((*ofilter), (OBJ_P)) = (boolean FDECL((*), (OBJ_P))) 0;
  22.      boolean do_unpaid = FALSE;
  23.      boolean do_blessed = FALSE, do_cursed = FALSE, do_uncursed = FALSE,
  24.              do_buc_unknown = FALSE;
  25.      int num_buc_types = 0;
  26.  
  27.      *pick_list = (menu_item *) 0;
  28.      if (!olist)
  29.          return 0;
  30.      if ((qflags & UNPAID_TYPES) && count_unpaid(olist))
  31.          do_unpaid = TRUE;
  32.      if (qflags & WORN_TYPES)
  33.          ofilter = is_worn;
  34.      if ((qflags & BUC_BLESSED) && count_buc(olist, BUC_BLESSED, ofilter)) {
  35.          do_blessed = TRUE;
  36.          num_buc_types++;
  37.      }
  38.      if ((qflags & BUC_CURSED) && count_buc(olist, BUC_CURSED, ofilter)) {
  39.          do_cursed = TRUE;
  40.          num_buc_types++;
  41.      }
  42.      if ((qflags & BUC_UNCURSED) && count_buc(olist, BUC_UNCURSED, ofilter)) {
  43.          do_uncursed = TRUE;
  44.          num_buc_types++;
  45.      }
  46.      if ((qflags & BUC_UNKNOWN) && count_buc(olist, BUC_UNKNOWN, ofilter)) {
  47.          do_buc_unknown = TRUE;
  48.          num_buc_types++;
  49.      }
  50.  
  51.      ccount = count_categories(olist, qflags);
  52.      /* no point in actually showing a menu for a single category */
  53.      if (ccount == 1 && !do_unpaid && num_buc_types <= 1
  54.          && !(qflags & BILLED_TYPES)) {
  55.          for (curr = olist; curr; curr = FOLLOW(curr, qflags)) {
  56.              if (ofilter && !(*ofilter)(curr))
  57.                  continue;
  58.              break;
  59.          }
  60.          if (curr) {
  61.              *pick_list = (menu_item *) alloc(sizeof(menu_item));
  62.              (*pick_list)->item.a_int = curr->oclass;
  63.              return 1;
  64.          } else {
  65.              debugpline0("query_category: no single object match");
  66.          }
  67.          return 0;
  68.      }
  69.  
  70.      win = create_nhwindow(NHW_MENU);
  71.      start_menu(win);
  72.      pack = flags.inv_order;
  73.  
  74.      if (qflags & CHOOSE_ALL) {
  75.          invlet = 'A';
  76.          any = zeroany;
  77.          any.a_int = 'A';
  78.          add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
  79.                   (qflags & WORN_TYPES) ? "Auto-select every item being worn"
  80.                                         : "Auto-select every item",
  81.                   MENU_UNSELECTED);
  82.  
  83.          any = zeroany;
  84.          add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE, "", MENU_UNSELECTED);
  85.      }
  86.  
  87.      if ((qflags & ALL_TYPES) && (ccount > 1)) {
  88.          invlet = 'a';
  89.          any = zeroany;
  90.          any.a_int = ALL_TYPES_SELECTED;
  91.          add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
  92.                   (qflags & WORN_TYPES) ? "All worn types" : "All types",
  93.                   MENU_UNSELECTED);
  94.          invlet = 'b';
  95.      } else
  96.          invlet = 'a';
  97.      do {
  98.          collected_type_name = FALSE;
  99.          for (curr = olist; curr; curr = FOLLOW(curr, qflags)) {
  100.              if (curr->oclass == *pack) {
  101.                  if (ofilter && !(*ofilter)(curr))
  102.                      continue;
  103.                  if (!collected_type_name) {
  104.                      any = zeroany;
  105.                      any.a_int = curr->oclass;
  106.                      add_menu(
  107.                          win, NO_GLYPH, &any, invlet++,
  108.                          def_oc_syms[(int) objects[curr->otyp].oc_class].sym,
  109.                          ATR_NONE, let_to_name(*pack, FALSE,
  110.                                                (how != PICK_NONE)
  111.                                                    && iflags.menu_head_objsym),
  112.                          MENU_UNSELECTED);
  113.                      collected_type_name = TRUE;
  114.                  }
  115.              }
  116.          }
  117.          pack++;
  118.          if (invlet >= 'u') {
  119.              impossible("query_category: too many categories");
  120.              return 0;
  121.          }
  122.      } while (*pack);
  123.  
  124.      if (do_unpaid || (qflags & BILLED_TYPES) || do_blessed || do_cursed
  125.          || do_uncursed || do_buc_unknown) {
  126.          any = zeroany;
  127.          add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE, "", MENU_UNSELECTED);
  128.      }
  129.  
  130.      /* unpaid items if there are any */
  131.      if (do_unpaid) {
  132.          invlet = 'u';
  133.          any = zeroany;
  134.          any.a_int = 'u';
  135.          add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE, "Unpaid items",
  136.                   MENU_UNSELECTED);
  137.      }
  138.      /* billed items: checked by caller, so always include if BILLED_TYPES */
  139.      if (qflags & BILLED_TYPES) {
  140.          invlet = 'x';
  141.          any = zeroany;
  142.          any.a_int = 'x';
  143.          add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
  144.                   "Unpaid items already used up", MENU_UNSELECTED);
  145.      }
  146.  
  147.      /* items with b/u/c/unknown if there are any;
  148.         this cluster of menu entries is in alphabetical order,
  149.         reversing the usual sequence of 'U' and 'C' in BUCX */
  150.      if (do_blessed) {
  151.          invlet = 'B';
  152.          any = zeroany;
  153.          any.a_int = 'B';
  154.          add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
  155.                   "Items known to be Blessed", MENU_UNSELECTED);
  156.      }
  157.      if (do_cursed) {
  158.          invlet = 'C';
  159.          any = zeroany;
  160.          any.a_int = 'C';
  161.          add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
  162.                   "Items known to be Cursed", MENU_UNSELECTED);
  163.      }
  164.      if (do_uncursed) {
  165.          invlet = 'U';
  166.          any = zeroany;
  167.          any.a_int = 'U';
  168.          add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
  169.                   "Items known to be Uncursed", MENU_UNSELECTED);
  170.      }
  171.      if (do_buc_unknown) {
  172.          invlet = 'X';
  173.          any = zeroany;
  174.          any.a_int = 'X';
  175.          add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
  176.                   "Items of unknown Bless/Curse status", MENU_UNSELECTED);
  177.      }
  178.      end_menu(win, qstr);
  179.      n = select_menu(win, how, pick_list);
  180.      destroy_nhwindow(win);
  181.      if (n < 0)
  182.          n = 0; /* caller's don't expect -1 */
  183.      return n;
  184.  }
  185.  

count_categories

  1.  STATIC_OVL int
  2.  count_categories(olist, qflags)
  3.  struct obj *olist;
  4.  int qflags;
  5.  {
  6.      char *pack;
  7.      boolean counted_category;
  8.      int ccount = 0;
  9.      struct obj *curr;
  10.  
  11.      pack = flags.inv_order;
  12.      do {
  13.          counted_category = FALSE;
  14.          for (curr = olist; curr; curr = FOLLOW(curr, qflags)) {
  15.              if (curr->oclass == *pack) {
  16.                  if ((qflags & WORN_TYPES)
  17.                      && !(curr->owornmask & (W_ARMOR | W_ACCESSORY | W_WEAPON)))
  18.                      continue;
  19.                  if (!counted_category) {
  20.                      ccount++;
  21.                      counted_category = TRUE;
  22.                  }
  23.              }
  24.          }
  25.          pack++;
  26.      } while (*pack);
  27.      return ccount;
  28.  }
  29.  

carry_count

  1.  /* could we carry `obj'? if not, could we carry some of it/them? */
  2.  STATIC_OVL long
  3.  carry_count(obj, container, count, telekinesis, wt_before, wt_after)
  4.  struct obj *obj, *container; /* object to pick up, bag it's coming out of */
  5.  long count;
  6.  boolean telekinesis;
  7.  int *wt_before, *wt_after;
  8.  {
  9.      boolean adjust_wt = container && carried(container),
  10.              is_gold = obj->oclass == COIN_CLASS;
  11.      int wt, iw, ow, oow;
  12.      long qq, savequan, umoney;
  13.      unsigned saveowt;
  14.      const char *verb, *prefx1, *prefx2, *suffx;
  15.      char obj_nambuf[BUFSZ], where[BUFSZ];
  16.  
  17.      savequan = obj->quan;
  18.      saveowt = obj->owt;
  19.      umoney = money_cnt(invent);
  20.      iw = max_capacity();
  21.  
  22.      if (count != savequan) {
  23.          obj->quan = count;
  24.          obj->owt = (unsigned) weight(obj);
  25.      }
  26.      wt = iw + (int) obj->owt;
  27.      if (adjust_wt)
  28.          wt -= (container->otyp == BAG_OF_HOLDING)
  29.                    ? (int) DELTA_CWT(container, obj)
  30.                    : (int) obj->owt;
  31.      /* This will go with silver+copper & new gold weight */
  32.      if (is_gold) /* merged gold might affect cumulative weight */
  33.          wt -= (GOLD_WT(umoney) + GOLD_WT(count) - GOLD_WT(umoney + count));
  34.      if (count != savequan) {
  35.          obj->quan = savequan;
  36.          obj->owt = saveowt;
  37.      }
  38.      *wt_before = iw;
  39.      *wt_after = wt;
  40.  
  41.      if (wt < 0)
  42.          return count;
  43.  
  44.      /* see how many we can lift */
  45.      if (is_gold) {
  46.          iw -= (int) GOLD_WT(umoney);
  47.          if (!adjust_wt) {
  48.              qq = GOLD_CAPACITY((long) iw, umoney);
  49.          } else {
  50.              oow = 0;
  51.              qq = 50L - (umoney % 100L) - 1L;
  52.              if (qq < 0L)
  53.                  qq += 100L;
  54.              for (; qq <= count; qq += 100L) {
  55.                  obj->quan = qq;
  56.                  obj->owt = (unsigned) GOLD_WT(qq);
  57.                  ow = (int) GOLD_WT(umoney + qq);
  58.                  ow -= (container->otyp == BAG_OF_HOLDING)
  59.                            ? (int) DELTA_CWT(container, obj)
  60.                            : (int) obj->owt;
  61.                  if (iw + ow >= 0)
  62.                      break;
  63.                  oow = ow;
  64.              }
  65.              iw -= oow;
  66.              qq -= 100L;
  67.          }
  68.          if (qq < 0L)
  69.              qq = 0L;
  70.          else if (qq > count)
  71.              qq = count;
  72.          wt = iw + (int) GOLD_WT(umoney + qq);
  73.      } else if (count > 1 || count < obj->quan) {
  74.          /*
  75.           * Ugh. Calc num to lift by changing the quan of of the
  76.           * object and calling weight.
  77.           *
  78.           * This works for containers only because containers
  79.           * don't merge.  -dean
  80.           */
  81.          for (qq = 1L; qq <= count; qq++) {
  82.              obj->quan = qq;
  83.              obj->owt = (unsigned) (ow = weight(obj));
  84.              if (adjust_wt)
  85.                  ow -= (container->otyp == BAG_OF_HOLDING)
  86.                            ? (int) DELTA_CWT(container, obj)
  87.                            : (int) obj->owt;
  88.              if (iw + ow >= 0)
  89.                  break;
  90.              wt = iw + ow;
  91.          }
  92.          --qq;
  93.      } else {
  94.          /* there's only one, and we can't lift it */
  95.          qq = 0L;
  96.      }
  97.      obj->quan = savequan;
  98.      obj->owt = saveowt;
  99.  
  100.      if (qq < count) {
  101.          /* some message will be given */
  102.          Strcpy(obj_nambuf, doname(obj));
  103.          if (container) {
  104.              Sprintf(where, "in %s", the(xname(container)));
  105.              verb = "carry";
  106.          } else {
  107.              Strcpy(where, "lying here");
  108.              verb = telekinesis ? "acquire" : "lift";
  109.          }
  110.      } else {
  111.          /* lint suppression */
  112.          *obj_nambuf = *where = '\0';
  113.          verb = "";
  114.      }
  115.      /* we can carry qq of them */
  116.      if (qq > 0) {
  117.          if (qq < count)
  118.              You("can only %s %s of the %s %s.", verb,
  119.                  (qq == 1L) ? "one" : "some", obj_nambuf, where);
  120.          *wt_after = wt;
  121.          return qq;
  122.      }
  123.  
  124.      if (!container)
  125.          Strcpy(where, "here"); /* slightly shorter form */
  126.      if (invent || umoney) {
  127.          prefx1 = "you cannot ";
  128.          prefx2 = "";
  129.          suffx = " any more";
  130.      } else {
  131.          prefx1 = (obj->quan == 1L) ? "it " : "even one ";
  132.          prefx2 = "is too heavy for you to ";
  133.          suffx = "";
  134.      }
  135.      There("%s %s %s, but %s%s%s%s.", otense(obj, "are"), obj_nambuf, where,
  136.            prefx1, prefx2, verb, suffx);
  137.  
  138.      /* *wt_after = iw; */
  139.      return 0L;
  140.  }
  141.  

lift_object

  1.  /* determine whether character is able and player is willing to carry `obj' */
  2.  STATIC_OVL
  3.  int
  4.  lift_object(obj, container, cnt_p, telekinesis)
  5.  struct obj *obj, *container; /* object to pick up, bag it's coming out of */
  6.  long *cnt_p;
  7.  boolean telekinesis;
  8.  {
  9.      int result, old_wt, new_wt, prev_encumbr, next_encumbr;
  10.  
  11.      if (obj->otyp == BOULDER && Sokoban) {
  12.          You("cannot get your %s around this %s.", body_part(HAND),
  13.              xname(obj));
  14.          return -1;
  15.      }
  16.      /* override weight consideration for loadstone picked up by anybody
  17.         and for boulder picked up by hero poly'd into a giant; override
  18.         availability of open inventory slot iff not already carrying one */
  19.      if (obj->otyp == LOADSTONE
  20.          || (obj->otyp == BOULDER && throws_rocks(youmonst.data))) {
  21.          if (inv_cnt(FALSE) < 52 || !carrying(obj->otyp)
  22.              || merge_choice(invent, obj))
  23.              return 1; /* lift regardless of current situation */
  24.          /* if we reach here, we're out of slots and already have at least
  25.             one of these, so treat this one more like a normal item */
  26.          You("are carrying too much stuff to pick up %s %s.",
  27.              (obj->quan == 1L) ? "another" : "more", simpleonames(obj));
  28.          return -1;
  29.      }
  30.  
  31.      *cnt_p = carry_count(obj, container, *cnt_p, telekinesis,
  32.                           &old_wt, &new_wt);
  33.      if (*cnt_p < 1L) {
  34.          result = -1; /* nothing lifted */
  35.      } else if (obj->oclass != COIN_CLASS
  36.                 /* [exception for gold coins will have to change
  37.                     if silver/copper ones ever get implemented] */
  38.                 && inv_cnt(FALSE) >= 52 && !merge_choice(invent, obj)) {
  39.          Your("knapsack cannot accommodate any more items.");
  40.          result = -1; /* nothing lifted */
  41.      } else {
  42.          result = 1;
  43.          prev_encumbr = near_capacity();
  44.          if (prev_encumbr < flags.pickup_burden)
  45.              prev_encumbr = flags.pickup_burden;
  46.          next_encumbr = calc_capacity(new_wt - old_wt);
  47.          if (next_encumbr > prev_encumbr) {
  48.              if (telekinesis) {
  49.                  result = 0; /* don't lift */
  50.              } else {
  51.                  char qbuf[BUFSZ];
  52.                  long savequan = obj->quan;
  53.  
  54.                  obj->quan = *cnt_p;
  55.                  Strcpy(qbuf, (next_encumbr > HVY_ENCUMBER)
  56.                                   ? overloadmsg
  57.                                   : (next_encumbr > MOD_ENCUMBER)
  58.                                         ? nearloadmsg
  59.                                         : moderateloadmsg);
  60.                  if (container)
  61.                      (void) strsubst(qbuf, "lifting", "removing");
  62.                  Strcat(qbuf, " ");
  63.                  (void) safe_qbuf(qbuf, qbuf, ".  Continue?", obj, doname,
  64.                                   ansimpleoname, something);
  65.                  obj->quan = savequan;
  66.                  switch (ynq(qbuf)) {
  67.                  case 'q':
  68.                      result = -1;
  69.                      break;
  70.                  case 'n':
  71.                      result = 0;
  72.                      break;
  73.                  default:
  74.                      break; /* 'y' => result == 1 */
  75.                  }
  76.                  clear_nhwindow(WIN_MESSAGE);
  77.              }
  78.          }
  79.      }
  80.  
  81.      if (obj->otyp == SCR_SCARE_MONSTER && result <= 0 && !container)
  82.          obj->spe = 0;
  83.      return result;
  84.  }
  85.  

pickup_object

  1.  /*
  2.   * Pick up <count> of obj from the ground and add it to the hero's inventory.
  3.   * Returns -1 if caller should break out of its loop, 0 if nothing picked
  4.   * up, 1 if otherwise.
  5.   */
  6.  int
  7.  pickup_object(obj, count, telekinesis)
  8.  struct obj *obj;
  9.  long count;
  10.  boolean telekinesis; /* not picking it up directly by hand */
  11.  {
  12.      int res, nearload;
  13.  
  14.      if (obj->quan < count) {
  15.          impossible("pickup_object: count %ld > quan %ld?", count, obj->quan);
  16.          return 0;
  17.      }
  18.  
  19.      /* In case of auto-pickup, where we haven't had a chance
  20.         to look at it yet; affects docall(SCR_SCARE_MONSTER). */
  21.      if (!Blind)
  22.          obj->dknown = 1;
  23.  
  24.      if (obj == uchain) { /* do not pick up attached chain */
  25.          return 0;
  26.      } else if (obj->oartifact && !touch_artifact(obj, &youmonst)) {
  27.          return 0;
  28.      } else if (obj->otyp == CORPSE) {
  29.          if (fatal_corpse_mistake(obj, telekinesis)
  30.              || rider_corpse_revival(obj, telekinesis))
  31.              return -1;
  32.      } else if (obj->otyp == SCR_SCARE_MONSTER) {
  33.          if (obj->blessed)
  34.              obj->blessed = 0;
  35.          else if (!obj->spe && !obj->cursed)
  36.              obj->spe = 1;
  37.          else {
  38.              pline_The("scroll%s %s to dust as you %s %s up.", plur(obj->quan),
  39.                        otense(obj, "turn"), telekinesis ? "raise" : "pick",
  40.                        (obj->quan == 1L) ? "it" : "them");
  41.              if (!(objects[SCR_SCARE_MONSTER].oc_name_known)
  42.                  && !(objects[SCR_SCARE_MONSTER].oc_uname))
  43.                  docall(obj);
  44.              useupf(obj, obj->quan);
  45.              return 1; /* tried to pick something up and failed, but
  46.                           don't want to terminate pickup loop yet   */
  47.          }
  48.      }
  49.  
  50.      if ((res = lift_object(obj, (struct obj *) 0, &count, telekinesis)) <= 0)
  51.          return res;
  52.  
  53.      /* Whats left of the special case for gold :-) */
  54.      if (obj->oclass == COIN_CLASS)
  55.          context.botl = 1;
  56.      if (obj->quan != count && obj->otyp != LOADSTONE)
  57.          obj = splitobj(obj, count);
  58.  
  59.      obj = pick_obj(obj);
  60.  
  61.      if (uwep && uwep == obj)
  62.          mrg_to_wielded = TRUE;
  63.      nearload = near_capacity();
  64.      prinv(nearload == SLT_ENCUMBER ? moderateloadmsg : (char *) 0, obj,
  65.            count);
  66.      mrg_to_wielded = FALSE;
  67.      return 1;
  68.  }
  69.  

pick_obj

  1.  /*
  2.   * Do the actual work of picking otmp from the floor or monster's interior
  3.   * and putting it in the hero's inventory.  Take care of billing.  Return a
  4.   * pointer to the object where otmp ends up.  This may be different
  5.   * from otmp because of merging.
  6.   */
  7.  struct obj *
  8.  pick_obj(otmp)
  9.  struct obj *otmp;
  10.  {
  11.      struct obj *result;
  12.      int ox = otmp->ox, oy = otmp->oy;
  13.      boolean robshop = (!u.uswallow && otmp != uball && costly_spot(ox, oy));
  14.  
  15.      obj_extract_self(otmp);
  16.      newsym(ox, oy);
  17.  
  18.      /* for shop items, addinv() needs to be after addtobill() (so that
  19.         object merger can take otmp->unpaid into account) but before
  20.         remote_robbery() (which calls rob_shop() which calls setpaid()
  21.         after moving costs of unpaid items to shop debt; setpaid()
  22.         calls clear_unpaid() for lots of object chains, but 'otmp' isn't
  23.         on any of those between obj_extract_self() and addinv(); for
  24.         3.6.0, 'otmp' remained flagged as an unpaid item in inventory
  25.         and triggered impossible() every time inventory was examined) */
  26.      if (robshop) {
  27.          char saveushops[5], fakeshop[2];
  28.  
  29.          /* addtobill cares about your location rather than the object's;
  30.             usually they'll be the same, but not when using telekinesis
  31.             (if ever implemented) or a grappling hook */
  32.          Strcpy(saveushops, u.ushops);
  33.          fakeshop[0] = *in_rooms(ox, oy, SHOPBASE);
  34.          fakeshop[1] = '\0';
  35.          Strcpy(u.ushops, fakeshop);
  36.          /* sets obj->unpaid if necessary */
  37.          addtobill(otmp, TRUE, FALSE, FALSE);
  38.          Strcpy(u.ushops, saveushops);
  39.          robshop = otmp->unpaid && !index(u.ushops, *fakeshop);
  40.      }
  41.  
  42.      result = addinv(otmp);
  43.      /* if you're taking a shop item from outside the shop, make shk notice */
  44.      if (robshop)
  45.          remote_burglary(ox, oy);
  46.  
  47.      return result;
  48.  }
  49.  

encumber_msg

  1.  /*
  2.   * prints a message if encumbrance changed since the last check and
  3.   * returns the new encumbrance value (from near_capacity()).
  4.   */
  5.  int
  6.  encumber_msg()
  7.  {
  8.      static int oldcap = UNENCUMBERED;
  9.      int newcap = near_capacity();
  10.  
  11.      if (oldcap < newcap) {
  12.          switch (newcap) {
  13.          case 1:
  14.              Your("movements are slowed slightly because of your load.");
  15.              break;
  16.          case 2:
  17.              You("rebalance your load.  Movement is difficult.");
  18.              break;
  19.          case 3:
  20.              You("%s under your heavy load.  Movement is very hard.",
  21.                  stagger(youmonst.data, "stagger"));
  22.              break;
  23.          default:
  24.              You("%s move a handspan with this load!",
  25.                  newcap == 4 ? "can barely" : "can't even");
  26.              break;
  27.          }
  28.          context.botl = 1;
  29.      } else if (oldcap > newcap) {
  30.          switch (newcap) {
  31.          case 0:
  32.              Your("movements are now unencumbered.");
  33.              break;
  34.          case 1:
  35.              Your("movements are only slowed slightly by your load.");
  36.              break;
  37.          case 2:
  38.              You("rebalance your load.  Movement is still difficult.");
  39.              break;
  40.          case 3:
  41.              You("%s under your load.  Movement is still very hard.",
  42.                  stagger(youmonst.data, "stagger"));
  43.              break;
  44.          }
  45.          context.botl = 1;
  46.      }
  47.  
  48.      oldcap = newcap;
  49.      return newcap;
  50.  }
  51.  

container_at

  1.  /* Is there a container at x,y. Optional: return count of containers at x,y */
  2.  int
  3.  container_at(x, y, countem)
  4.  int x, y;
  5.  boolean countem;
  6.  {
  7.      struct obj *cobj, *nobj;
  8.      int container_count = 0;
  9.  
  10.      for (cobj = level.objects[x][y]; cobj; cobj = nobj) {
  11.          nobj = cobj->nexthere;
  12.          if (Is_container(cobj)) {
  13.              container_count++;
  14.              if (!countem)
  15.                  break;
  16.          }
  17.      }
  18.      return container_count;
  19.  }
  20.  

able_to_loot

  1.  STATIC_OVL boolean
  2.  able_to_loot(x, y, looting)
  3.  int x, y;
  4.  boolean looting; /* loot vs tip */
  5.  {
  6.      const char *verb = looting ? "loot" : "tip";
  7.  
  8.      if (!can_reach_floor(TRUE)) {
  9.          if (u.usteed && P_SKILL(P_RIDING) < P_BASIC)
  10.              rider_cant_reach(); /* not skilled enough to reach */
  11.          else
  12.              cant_reach_floor(x, y, FALSE, TRUE);
  13.          return FALSE;
  14.      } else if ((is_pool(x, y) && (looting || !Underwater)) || is_lava(x, y)) {
  15.          /* at present, can't loot in water even when Underwater;
  16.             can tip underwater, but not when over--or stuck in--lava */
  17.          You("cannot %s things that are deep in the %s.", verb,
  18.              hliquid(is_lava(x, y) ? "lava" : "water"));
  19.          return FALSE;
  20.      } else if (nolimbs(youmonst.data)) {
  21.          pline("Without limbs, you cannot %s anything.", verb);
  22.          return FALSE;
  23.      } else if (looting && !freehand()) {
  24.          pline("Without a free %s, you cannot loot anything.",
  25.                body_part(HAND));
  26.          return FALSE;
  27.      }
  28.      return TRUE;
  29.  }
  30.  

mon_beside

  1.  STATIC_OVL boolean
  2.  mon_beside(x, y)
  3.  int x, y;
  4.  {
  5.      int i, j, nx, ny;
  6.  
  7.      for (i = -1; i <= 1; i++)
  8.          for (j = -1; j <= 1; j++) {
  9.              nx = x + i;
  10.              ny = y + j;
  11.              if (isok(nx, ny) && MON_AT(nx, ny))
  12.                  return TRUE;
  13.          }
  14.      return FALSE;
  15.  }
  16.  

do_loot_cont

  1.  int
  2.  do_loot_cont(cobjp, cindex, ccount)
  3.  struct obj **cobjp;
  4.  int cindex, ccount; /* index of this container (1..N), number of them (N) */
  5.  {
  6.      struct obj *cobj = *cobjp;
  7.  
  8.      if (!cobj)
  9.          return 0;
  10.      if (cobj->olocked) {
  11.          if (ccount < 2)
  12.              pline("%s locked.",
  13.                    cobj->lknown ? "It is" : "Hmmm, it turns out to be");
  14.          else if (cobj->lknown)
  15.              pline("%s is locked.", The(xname(cobj)));
  16.          else
  17.              pline("Hmmm, %s turns out to be locked.", the(xname(cobj)));
  18.          cobj->lknown = 1;
  19.          return 0;
  20.      }
  21.      cobj->lknown = 1;
  22.  
  23.      if (cobj->otyp == BAG_OF_TRICKS) {
  24.          int tmp;
  25.  
  26.          You("carefully open %s...", the(xname(cobj)));
  27.          pline("It develops a huge set of teeth and bites you!");
  28.          tmp = rnd(10);
  29.          losehp(Maybe_Half_Phys(tmp), "carnivorous bag", KILLED_BY_AN);
  30.          makeknown(BAG_OF_TRICKS);
  31.          abort_looting = TRUE;
  32.          return 1;
  33.      }
  34.  
  35.      You("%sopen %s...", (!cobj->cknown || !cobj->lknown) ? "carefully " : "",
  36.          the(xname(cobj)));
  37.      return use_container(cobjp, 0, (boolean) (cindex < ccount));
  38.  }
  39.  

doloot

  1.  /* loot a container on the floor or loot saddle from mon. */
  2.  int
  3.  doloot()
  4.  {
  5.      struct obj *cobj, *nobj;
  6.      register int c = -1;
  7.      int timepassed = 0;
  8.      coord cc;
  9.      boolean underfoot = TRUE;
  10.      const char *dont_find_anything = "don't find anything";
  11.      struct monst *mtmp;
  12.      char qbuf[BUFSZ];
  13.      int prev_inquiry = 0;
  14.      boolean prev_loot = FALSE;
  15.      int num_conts = 0;
  16.  
  17.      abort_looting = FALSE;
  18.  
  19.      if (check_capacity((char *) 0)) {
  20.          /* "Can't do that while carrying so much stuff." */
  21.          return 0;
  22.      }
  23.      if (nohands(youmonst.data)) {
  24.          You("have no hands!"); /* not `body_part(HAND)' */
  25.          return 0;
  26.      }
  27.      if (Confusion) {
  28.          if (rn2(6) && reverse_loot())
  29.              return 1;
  30.          if (rn2(2)) {
  31.              pline("Being confused, you find nothing to loot.");
  32.              return 1; /* costs a turn */
  33.          }             /* else fallthrough to normal looting */
  34.      }
  35.      cc.x = u.ux;
  36.      cc.y = u.uy;
  37.  
  38.      if (iflags.menu_requested)
  39.          goto lootmon;
  40.  
  41.   lootcont:
  42.      if ((num_conts = container_at(cc.x, cc.y, TRUE)) > 0) {
  43.          boolean anyfound = FALSE;
  44.  
  45.          if (!able_to_loot(cc.x, cc.y, TRUE))
  46.              return 0;
  47.  
  48.          if (Blind && !uarmg) {
  49.              /* if blind and without gloves, attempting to #loot at the
  50.                 location of a cockatrice corpse is fatal before asking
  51.                 whether to manipulate any containers */
  52.              for (nobj = sobj_at(CORPSE, cc.x, cc.y); nobj;
  53.                   nobj = nxtobj(nobj, CORPSE, TRUE))
  54.                  if (will_feel_cockatrice(nobj, FALSE)) {
  55.                      feel_cockatrice(nobj, FALSE);
  56.                      /* if life-saved (or poly'd into stone golem),
  57.                         terminate attempt to loot */
  58.                      return 1;
  59.                  }
  60.          }
  61.  
  62.          if (num_conts > 1) {
  63.              /* use a menu to loot many containers */
  64.              int n, i;
  65.              winid win;
  66.              anything any;
  67.              menu_item *pick_list = (menu_item *) 0;
  68.  
  69.              any.a_void = 0;
  70.              win = create_nhwindow(NHW_MENU);
  71.              start_menu(win);
  72.  
  73.              for (cobj = level.objects[cc.x][cc.y]; cobj;
  74.                   cobj = cobj->nexthere)
  75.                  if (Is_container(cobj)) {
  76.                      any.a_obj = cobj;
  77.                      add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE,
  78.                               doname(cobj), MENU_UNSELECTED);
  79.                  }
  80.              end_menu(win, "Loot which containers?");
  81.              n = select_menu(win, PICK_ANY, &pick_list);
  82.              destroy_nhwindow(win);
  83.  
  84.              if (n > 0) {
  85.                  for (i = 1; i <= n; i++) {
  86.                      cobj = pick_list[i - 1].item.a_obj;
  87.                      timepassed |= do_loot_cont(&cobj, i, n);
  88.                      if (abort_looting) {
  89.                          /* chest trap or magic bag explosion or <esc> */
  90.                          free((genericptr_t) pick_list);
  91.                          return timepassed;
  92.                      }
  93.                  }
  94.                  free((genericptr_t) pick_list);
  95.              }
  96.              if (n != 0)
  97.                  c = 'y';
  98.          } else {
  99.              for (cobj = level.objects[cc.x][cc.y]; cobj; cobj = nobj) {
  100.                  nobj = cobj->nexthere;
  101.  
  102.                  if (Is_container(cobj)) {
  103.                      c = ynq(safe_qbuf(qbuf, "There is ", " here, loot it?",
  104.                                        cobj, doname, ansimpleoname,
  105.                                        "a container"));
  106.                      if (c == 'q')
  107.                          return timepassed;
  108.                      if (c == 'n')
  109.                          continue;
  110.                      anyfound = TRUE;
  111.  
  112.                      timepassed |= do_loot_cont(&cobj, 1, 1);
  113.                      if (abort_looting)
  114.                          /* chest trap or magic bag explosion or <esc> */
  115.                          return timepassed;
  116.                  }
  117.              }
  118.              if (anyfound)
  119.                  c = 'y';
  120.          }
  121.      } else if (IS_GRAVE(levl[cc.x][cc.y].typ)) {
  122.          You("need to dig up the grave to effectively loot it...");
  123.      }
  124.  
  125.      /*
  126.       * 3.3.1 introduced directional looting for some things.
  127.       */
  128.   lootmon:
  129.      if (c != 'y' && mon_beside(u.ux, u.uy)) {
  130.          if (!get_adjacent_loc("Loot in what direction?",
  131.                                "Invalid loot location", u.ux, u.uy, &cc))
  132.              return 0;
  133.          if (cc.x == u.ux && cc.y == u.uy) {
  134.              underfoot = TRUE;
  135.              if (container_at(cc.x, cc.y, FALSE))
  136.                  goto lootcont;
  137.          } else
  138.              underfoot = FALSE;
  139.          if (u.dz < 0) {
  140.              You("%s to loot on the %s.", dont_find_anything,
  141.                  ceiling(cc.x, cc.y));
  142.              timepassed = 1;
  143.              return timepassed;
  144.          }
  145.          mtmp = m_at(cc.x, cc.y);
  146.          if (mtmp)
  147.              timepassed = loot_mon(mtmp, &prev_inquiry, &prev_loot);
  148.          /* always use a turn when choosing a direction is impaired,
  149.             even if you've successfully targetted a saddled creature
  150.             and then answered "no" to the "remove its saddle?" prompt */
  151.          if (Confusion || Stunned)
  152.              timepassed = 1;
  153.  
  154.          /* Preserve pre-3.3.1 behaviour for containers.
  155.           * Adjust this if-block to allow container looting
  156.           * from one square away to change that in the future.
  157.           */
  158.          if (!underfoot) {
  159.              if (container_at(cc.x, cc.y, FALSE)) {
  160.                  if (mtmp) {
  161.                      You_cant("loot anything %sthere with %s in the way.",
  162.                               prev_inquiry ? "else " : "", mon_nam(mtmp));
  163.                      return timepassed;
  164.                  } else {
  165.                      You("have to be at a container to loot it.");
  166.                  }
  167.              } else {
  168.                  You("%s %sthere to loot.", dont_find_anything,
  169.                      (prev_inquiry || prev_loot) ? "else " : "");
  170.                  return timepassed;
  171.              }
  172.          }
  173.      } else if (c != 'y' && c != 'n') {
  174.          You("%s %s to loot.", dont_find_anything,
  175.              underfoot ? "here" : "there");
  176.      }
  177.      return timepassed;
  178.  }
  179.  

reverse_loot

  1.  /* called when attempting to #loot while confused */
  2.  STATIC_OVL boolean
  3.  reverse_loot()
  4.  {
  5.      struct obj *goldob = 0, *coffers, *otmp, boxdummy;
  6.      struct monst *mon;
  7.      long contribution;
  8.      int n, x = u.ux, y = u.uy;
  9.  
  10.      if (!rn2(3)) {
  11.          /* n objects: 1/(n+1) chance per object plus 1/(n+1) to fall off end
  12.           */
  13.          for (n = inv_cnt(TRUE), otmp = invent; otmp; --n, otmp = otmp->nobj)
  14.              if (!rn2(n + 1)) {
  15.                  prinv("You find old loot:", otmp, 0L);
  16.                  return TRUE;
  17.              }
  18.          return FALSE;
  19.      }
  20.  
  21.      /* find a money object to mess with */
  22.      for (goldob = invent; goldob; goldob = goldob->nobj)
  23.          if (goldob->oclass == COIN_CLASS) {
  24.              contribution = ((long) rnd(5) * goldob->quan + 4L) / 5L;
  25.              if (contribution < goldob->quan)
  26.                  goldob = splitobj(goldob, contribution);
  27.              break;
  28.          }
  29.      if (!goldob)
  30.          return FALSE;
  31.  
  32.      if (!IS_THRONE(levl[x][y].typ)) {
  33.          dropx(goldob);
  34.          /* the dropped gold might have fallen to lower level */
  35.          if (g_at(x, y))
  36.              pline("Ok, now there is loot here.");
  37.      } else {
  38.          /* find original coffers chest if present, otherwise use nearest one
  39.           */
  40.          otmp = 0;
  41.          for (coffers = fobj; coffers; coffers = coffers->nobj)
  42.              if (coffers->otyp == CHEST) {
  43.                  if (coffers->spe == 2)
  44.                      break; /* a throne room chest */
  45.                  if (!otmp
  46.                      || distu(coffers->ox, coffers->oy)
  47.                             < distu(otmp->ox, otmp->oy))
  48.                      otmp = coffers; /* remember closest ordinary chest */
  49.              }
  50.          if (!coffers)
  51.              coffers = otmp;
  52.  
  53.          if (coffers) {
  54.              verbalize("Thank you for your contribution to reduce the debt.");
  55.              freeinv(goldob);
  56.              (void) add_to_container(coffers, goldob);
  57.              coffers->owt = weight(coffers);
  58.              coffers->cknown = 0;
  59.              if (!coffers->olocked) {
  60.                  boxdummy = zeroobj, boxdummy.otyp = SPE_WIZARD_LOCK;
  61.                  (void) boxlock(coffers, &boxdummy);
  62.              }
  63.          } else if (levl[x][y].looted != T_LOOTED
  64.                     && (mon = makemon(courtmon(), x, y, NO_MM_FLAGS)) != 0) {
  65.              freeinv(goldob);
  66.              add_to_minv(mon, goldob);
  67.              pline("The exchequer accepts your contribution.");
  68.              if (!rn2(10))
  69.                  levl[x][y].looted = T_LOOTED;
  70.          } else {
  71.              You("drop %s.", doname(goldob));
  72.              dropx(goldob);
  73.          }
  74.      }
  75.      return TRUE;
  76.  }
  77.  

loot_mon

  1.  /* loot_mon() returns amount of time passed.
  2.   */
  3.  int
  4.  loot_mon(mtmp, passed_info, prev_loot)
  5.  struct monst *mtmp;
  6.  int *passed_info;
  7.  boolean *prev_loot;
  8.  {
  9.      int c = -1;
  10.      int timepassed = 0;
  11.      struct obj *otmp;
  12.      char qbuf[QBUFSZ];
  13.  
  14.      /* 3.3.1 introduced the ability to remove saddle from a steed.
  15.       *  *passed_info is set to TRUE if a loot query was given.
  16.       *  *prev_loot is set to TRUE if something was actually acquired in here.
  17.       */
  18.      if (mtmp && mtmp != u.usteed && (otmp = which_armor(mtmp, W_SADDLE))) {
  19.          long unwornmask;
  20.  
  21.          if (passed_info)
  22.              *passed_info = 1;
  23.          Sprintf(qbuf, "Do you want to remove the saddle from %s?",
  24.                  x_monnam(mtmp, ARTICLE_THE, (char *) 0,
  25.                           SUPPRESS_SADDLE, FALSE));
  26.          if ((c = yn_function(qbuf, ynqchars, 'n')) == 'y') {
  27.              if (nolimbs(youmonst.data)) {
  28.                  You_cant("do that without limbs."); /* not body_part(HAND) */
  29.                  return 0;
  30.              }
  31.              if (otmp->cursed) {
  32.                  You("can't.  The saddle seems to be stuck to %s.",
  33.                      x_monnam(mtmp, ARTICLE_THE, (char *) 0,
  34.                               SUPPRESS_SADDLE, FALSE));
  35.                  /* the attempt costs you time */
  36.                  return 1;
  37.              }
  38.              obj_extract_self(otmp);
  39.              if ((unwornmask = otmp->owornmask) != 0L) {
  40.                  mtmp->misc_worn_check &= ~unwornmask;
  41.                  otmp->owornmask = 0L;
  42.                  update_mon_intrinsics(mtmp, otmp, FALSE, FALSE);
  43.              }
  44.              otmp = hold_another_object(otmp, "You drop %s!", doname(otmp),
  45.                                         (const char *) 0);
  46.              timepassed = rnd(3);
  47.              if (prev_loot)
  48.                  *prev_loot = TRUE;
  49.          } else if (c == 'q') {
  50.              return 0;
  51.          }
  52.      }
  53.      /* 3.4.0 introduced ability to pick things up from swallower's stomach */
  54.      if (u.uswallow) {
  55.          int count = passed_info ? *passed_info : 0;
  56.  
  57.          timepassed = pickup(count);
  58.      }
  59.      return timepassed;
  60.  }
  61.  

mbag_explodes

  1.  /*
  2.   * Decide whether an object being placed into a magic bag will cause
  3.   * it to explode.  If the object is a bag itself, check recursively.
  4.   */
  5.  STATIC_OVL boolean
  6.  mbag_explodes(obj, depthin)
  7.  struct obj *obj;
  8.  int depthin;
  9.  {
  10.      /* these won't cause an explosion when they're empty */
  11.      if ((obj->otyp == WAN_CANCELLATION || obj->otyp == BAG_OF_TRICKS)
  12.          && obj->spe <= 0)
  13.          return FALSE;
  14.  
  15.      /* odds: 1/1, 2/2, 3/4, 4/8, 5/16, 6/32, 7/64, 8/128, 9/128, 10/128,... */
  16.      if ((Is_mbag(obj) || obj->otyp == WAN_CANCELLATION)
  17.          && (rn2(1 << (depthin > 7 ? 7 : depthin)) <= depthin))
  18.          return TRUE;
  19.      else if (Has_contents(obj)) {
  20.          struct obj *otmp;
  21.  
  22.          for (otmp = obj->cobj; otmp; otmp = otmp->nobj)
  23.              if (mbag_explodes(otmp, depthin + 1))
  24.                  return TRUE;
  25.      }
  26.      return FALSE;
  27.  }
  28.  

boh_loss

  1.  STATIC_OVL long
  2.  boh_loss(container, held)
  3.  struct obj *container;
  4.  int held;
  5.  {
  6.      /* sometimes toss objects if a cursed magic bag */
  7.      if (Is_mbag(container) && container->cursed && Has_contents(container)) {
  8.          long loss = 0L;
  9.          struct obj *curr, *otmp;
  10.  
  11.          for (curr = container->cobj; curr; curr = otmp) {
  12.              otmp = curr->nobj;
  13.              if (!rn2(13)) {
  14.                  obj_extract_self(curr);
  15.                  loss += mbag_item_gone(held, curr);
  16.              }
  17.          }
  18.          return loss;
  19.      }
  20.      return 0;
  21.  }
  22.  

in_container

  1.  /* Returns: -1 to stop, 1 item was inserted, 0 item was not inserted. */
  2.  STATIC_PTR int
  3.  in_container(obj)
  4.  register struct obj *obj;
  5.  {
  6.      boolean floor_container = !carried(current_container);
  7.      boolean was_unpaid = FALSE;
  8.      char buf[BUFSZ];
  9.  
  10.      if (!current_container) {
  11.          impossible("<in> no current_container?");
  12.          return 0;
  13.      } else if (obj == uball || obj == uchain) {
  14.          You("must be kidding.");
  15.          return 0;
  16.      } else if (obj == current_container) {
  17.          pline("That would be an interesting topological exercise.");
  18.          return 0;
  19.      } else if (obj->owornmask & (W_ARMOR | W_ACCESSORY)) {
  20.          Norep("You cannot %s %s you are wearing.",
  21.                Icebox ? "refrigerate" : "stash", something);
  22.          return 0;
  23.      } else if ((obj->otyp == LOADSTONE) && obj->cursed) {
  24.          obj->bknown = 1;
  25.          pline_The("stone%s won't leave your person.", plur(obj->quan));
  26.          return 0;
  27.      } else if (obj->otyp == AMULET_OF_YENDOR
  28.                 || obj->otyp == CANDELABRUM_OF_INVOCATION
  29.                 || obj->otyp == BELL_OF_OPENING
  30.                 || obj->otyp == SPE_BOOK_OF_THE_DEAD) {
  31.          /* Prohibit Amulets in containers; if you allow it, monsters can't
  32.           * steal them.  It also becomes a pain to check to see if someone
  33.           * has the Amulet.  Ditto for the Candelabrum, the Bell and the Book.
  34.           */
  35.          pline("%s cannot be confined in such trappings.", The(xname(obj)));
  36.          return 0;
  37.      } else if (obj->otyp == LEASH && obj->leashmon != 0) {
  38.          pline("%s attached to your pet.", Tobjnam(obj, "are"));
  39.          return 0;
  40.      } else if (obj == uwep) {
  41.          if (welded(obj)) {
  42.              weldmsg(obj);
  43.              return 0;
  44.          }
  45.          setuwep((struct obj *) 0);
  46.          /* This uwep check is obsolete.  It dates to 3.0 and earlier when
  47.           * unwielding Firebrand would be fatal in hell if hero had no other
  48.           * fire resistance.  Life-saving would force it to be re-wielded.
  49.           */
  50.          if (uwep)
  51.              return 0; /* unwielded, died, rewielded */
  52.      } else if (obj == uswapwep) {
  53.          setuswapwep((struct obj *) 0);
  54.      } else if (obj == uquiver) {
  55.          setuqwep((struct obj *) 0);
  56.      }
  57.  
  58.      if (fatal_corpse_mistake(obj, FALSE))
  59.          return -1;
  60.  
  61.      /* boxes, boulders, and big statues can't fit into any container */
  62.      if (obj->otyp == ICE_BOX || Is_box(obj) || obj->otyp == BOULDER
  63.          || (obj->otyp == STATUE && bigmonst(&mons[obj->corpsenm]))) {
  64.          /*
  65.           *  xname() uses a static result array.  Save obj's name
  66.           *  before current_container's name is computed.  Don't
  67.           *  use the result of strcpy() within You() --- the order
  68.           *  of evaluation of the parameters is undefined.
  69.           */
  70.          Strcpy(buf, the(xname(obj)));
  71.          You("cannot fit %s into %s.", buf, the(xname(current_container)));
  72.          return 0;
  73.      }
  74.  
  75.      freeinv(obj);
  76.  
  77.      if (obj_is_burning(obj)) /* this used to be part of freeinv() */
  78.          (void) snuff_lit(obj);
  79.  
  80.      if (floor_container && costly_spot(u.ux, u.uy)) {
  81.          /* defer gold until after put-in message */
  82.          if (obj->oclass != COIN_CLASS) {
  83.              /* sellobj() will take an unpaid item off the shop bill */
  84.              was_unpaid = obj->unpaid ? TRUE : FALSE;
  85.              /* don't sell when putting the item into your own container,
  86.               * but handle billing correctly */
  87.              sellobj_state(current_container->no_charge
  88.                            ? SELL_DONTSELL : SELL_DELIBERATE);
  89.              sellobj(obj, u.ux, u.uy);
  90.              sellobj_state(SELL_NORMAL);
  91.          }
  92.      }
  93.      if (Icebox && !age_is_relative(obj)) {
  94.          obj->age = monstermoves - obj->age; /* actual age */
  95.          /* stop any corpse timeouts when frozen */
  96.          if (obj->otyp == CORPSE && obj->timed) {
  97.              long rot_alarm = stop_timer(ROT_CORPSE, obj_to_any(obj));
  98.  
  99.              (void) stop_timer(REVIVE_MON, obj_to_any(obj));
  100.              /* mark a non-reviving corpse as such */
  101.              if (rot_alarm)
  102.                  obj->norevive = 1;
  103.          }
  104.      } else if (Is_mbag(current_container) && mbag_explodes(obj, 0)) {
  105.          /* explicitly mention what item is triggering the explosion */
  106.          pline("As you put %s inside, you are blasted by a magical explosion!",
  107.                doname(obj));
  108.          /* did not actually insert obj yet */
  109.          if (was_unpaid)
  110.              addtobill(obj, FALSE, FALSE, TRUE);
  111.          obfree(obj, (struct obj *) 0);
  112.          delete_contents(current_container);
  113.          if (!floor_container)
  114.              useup(current_container);
  115.          else if (obj_here(current_container, u.ux, u.uy))
  116.              useupf(current_container, current_container->quan);
  117.          else
  118.              panic("in_container:  bag not found.");
  119.  
  120.          losehp(d(6, 6), "magical explosion", KILLED_BY_AN);
  121.          current_container = 0; /* baggone = TRUE; */
  122.      }
  123.  
  124.      if (current_container) {
  125.          Strcpy(buf, the(xname(current_container)));
  126.          You("put %s into %s.", doname(obj), buf);
  127.  
  128.          /* gold in container always needs to be added to credit */
  129.          if (floor_container && obj->oclass == COIN_CLASS)
  130.              sellobj(obj, current_container->ox, current_container->oy);
  131.          (void) add_to_container(current_container, obj);
  132.          current_container->owt = weight(current_container);
  133.      }
  134.      /* gold needs this, and freeinv() many lines above may cause
  135.       * the encumbrance to disappear from the status, so just always
  136.       * update status immediately.
  137.       */
  138.      bot();
  139.      return (current_container ? 1 : -1);
  140.  }
  141.  

ck_bag

  1.  /* askchain() filter used by in_container();
  2.   * returns True if the container is intact and 'obj' isn't it, False if
  3.   * container is gone (magic bag explosion) or 'obj' is the container itself;
  4.   * also used by getobj() when picking a single item to stash
  5.   */
  6.  int
  7.  ck_bag(obj)
  8.  struct obj *obj;
  9.  {
  10.      return (current_container && obj != current_container);
  11.  }
  12.  

out_container

  1.  /* Returns: -1 to stop, 1 item was removed, 0 item was not removed. */
  2.  STATIC_PTR int
  3.  out_container(obj)
  4.  register struct obj *obj;
  5.  {
  6.      register struct obj *otmp;
  7.      boolean is_gold = (obj->oclass == COIN_CLASS);
  8.      int res, loadlev;
  9.      long count;
  10.  
  11.      if (!current_container) {
  12.          impossible("<out> no current_container?");
  13.          return -1;
  14.      } else if (is_gold) {
  15.          obj->owt = weight(obj);
  16.      }
  17.  
  18.      if (obj->oartifact && !touch_artifact(obj, &youmonst))
  19.          return 0;
  20.  
  21.      if (fatal_corpse_mistake(obj, FALSE))
  22.          return -1;
  23.  
  24.      count = obj->quan;
  25.      if ((res = lift_object(obj, current_container, &count, FALSE)) <= 0)
  26.          return res;
  27.  
  28.      if (obj->quan != count && obj->otyp != LOADSTONE)
  29.          obj = splitobj(obj, count);
  30.  
  31.      /* Remove the object from the list. */
  32.      obj_extract_self(obj);
  33.      current_container->owt = weight(current_container);
  34.  
  35.      if (Icebox)
  36.          removed_from_icebox(obj);
  37.  
  38.      if (!obj->unpaid && !carried(current_container)
  39.          && costly_spot(current_container->ox, current_container->oy)) {
  40.          obj->ox = current_container->ox;
  41.          obj->oy = current_container->oy;
  42.          addtobill(obj, FALSE, FALSE, FALSE);
  43.      }
  44.      if (is_pick(obj))
  45.          pick_pick(obj); /* shopkeeper feedback */
  46.  
  47.      otmp = addinv(obj);
  48.      loadlev = near_capacity();
  49.      prinv(loadlev ? ((loadlev < MOD_ENCUMBER)
  50.                          ? "You have a little trouble removing"
  51.                          : "You have much trouble removing")
  52.                    : (char *) 0,
  53.            otmp, count);
  54.  
  55.      if (is_gold) {
  56.          bot(); /* update character's gold piece count immediately */
  57.      }
  58.      return 1;
  59.  }
  60.  

removed_from_icebox

  1.  /* taking a corpse out of an ice box needs a couple of adjustments */
  2.  STATIC_OVL void
  3.  removed_from_icebox(obj)
  4.  struct obj *obj;
  5.  {
  6.      if (!age_is_relative(obj)) {
  7.          obj->age = monstermoves - obj->age; /* actual age */
  8.          if (obj->otyp == CORPSE)
  9.              start_corpse_timeout(obj);
  10.      }
  11.  }
  12.  

mbag_item_gone

  1.  /* an object inside a cursed bag of holding is being destroyed */
  2.  STATIC_OVL long
  3.  mbag_item_gone(held, item)
  4.  int held;
  5.  struct obj *item;
  6.  {
  7.      struct monst *shkp;
  8.      long loss = 0L;
  9.  
  10.      if (item->dknown)
  11.          pline("%s %s vanished!", Doname2(item), otense(item, "have"));
  12.      else
  13.          You("%s %s disappear!", Blind ? "notice" : "see", doname(item));
  14.  
  15.      if (*u.ushops && (shkp = shop_keeper(*u.ushops)) != 0) {
  16.          if (held ? (boolean) item->unpaid : costly_spot(u.ux, u.uy))
  17.              loss = stolen_value(item, u.ux, u.uy, (boolean) shkp->mpeaceful,
  18.                                  TRUE);
  19.      }
  20.      obfree(item, (struct obj *) 0);
  21.      return loss;
  22.  }
  23.  

observe_quantum_cat

  1.  STATIC_OVL void
  2.  observe_quantum_cat(box)
  3.  struct obj *box;
  4.  {
  5.      static NEARDATA const char sc[] = "Schroedinger's Cat";
  6.      struct obj *deadcat;
  7.      struct monst *livecat;
  8.      xchar ox, oy;
  9.  
  10.      box->spe = 0; /* box->owt will be updated below */
  11.      if (get_obj_location(box, &ox, &oy, 0))
  12.          box->ox = ox, box->oy = oy; /* in case it's being carried */
  13.  
  14.      /* this isn't really right, since any form of observation
  15.         (telepathic or monster/object/food detection) ought to
  16.         force the determination of alive vs dead state; but basing
  17.         it just on opening the box is much simpler to cope with */
  18.      livecat = rn2(2)
  19.                    ? makemon(&mons[PM_HOUSECAT], box->ox, box->oy, NO_MINVENT)
  20.                    : 0;
  21.      if (livecat) {
  22.          livecat->mpeaceful = 1;
  23.          set_malign(livecat);
  24.          if (!canspotmon(livecat))
  25.              You("think %s brushed your %s.", something, body_part(FOOT));
  26.          else
  27.              pline("%s inside the box is still alive!", Monnam(livecat));
  28.          (void) christen_monst(livecat, sc);
  29.      } else {
  30.          deadcat = mk_named_object(CORPSE, &mons[PM_HOUSECAT],
  31.                                    box->ox, box->oy, sc);
  32.          if (deadcat) {
  33.              obj_extract_self(deadcat);
  34.              (void) add_to_container(box, deadcat);
  35.          }
  36.          pline_The("%s inside the box is dead!",
  37.                    Hallucination ? rndmonnam((char *) 0) : "housecat");
  38.      }
  39.      box->owt = weight(box);
  40.      return;
  41.  }
  42.  
  43.  #undef Icebox
  44.  

container_gone

  1.  /* used by askchain() to check for magic bag explosion */
  2.  boolean
  3.  container_gone(fn)
  4.  int FDECL((*fn), (OBJ_P));
  5.  {
  6.      /* result is only meaningful while use_container() is executing */
  7.      return ((fn == in_container || fn == out_container)
  8.              && !current_container);
  9.  }
  10.  

explain_container_prompt

  1.  STATIC_OVL void
  2.  explain_container_prompt(more_containers)
  3.  boolean more_containers;
  4.  {
  5.      static const char *const explaintext[] = {
  6.          "Container actions:",
  7.          "",
  8.          " : -- Look: examine contents",
  9.          " o -- Out: take things out",
  10.          " i -- In: put things in",
  11.          " b -- Both: first take things out, then put things in",
  12.          " r -- Reversed: put things in, then take things out",
  13.          " s -- Stash: put one item in", "",
  14.          " n -- Next: loot next selected container",
  15.          " q -- Quit: finished",
  16.          " ? -- Help: display this text.",
  17.          "", 0
  18.      };
  19.      const char *const *txtpp;
  20.      winid win;
  21.  
  22.      /* "Do what with <container>? [:oibrsq or ?] (q)" */
  23.      if ((win = create_nhwindow(NHW_TEXT)) != WIN_ERR) {
  24.          for (txtpp = explaintext; *txtpp; ++txtpp) {
  25.              if (!more_containers && !strncmp(*txtpp, " n ", 3))
  26.                  continue;
  27.              putstr(win, 0, *txtpp);
  28.          }
  29.          display_nhwindow(win, FALSE);
  30.          destroy_nhwindow(win);
  31.      }
  32.  }
  33.  

u_handsy

  1.  boolean
  2.  u_handsy()
  3.  {
  4.      if (nohands(youmonst.data)) {
  5.          You("have no hands!"); /* not `body_part(HAND)' */
  6.          return FALSE;
  7.      } else if (!freehand()) {
  8.          You("have no free %s.", body_part(HAND));
  9.          return FALSE;
  10.      }
  11.      return TRUE;
  12.  }
  13.  
  14.  static const char stashable[] = { ALLOW_COUNT, COIN_CLASS, ALL_CLASSES, 0 };
  15.  

use_container

  1.  int
  2.  use_container(objp, held, more_containers)
  3.  struct obj **objp;
  4.  int held;
  5.  boolean more_containers; /* True iff #loot multiple and this isn't last one */
  6.  {
  7.      struct obj *otmp, *obj = *objp;
  8.      boolean quantum_cat, cursed_mbag, loot_out, loot_in, loot_in_first,
  9.          stash_one, inokay, outokay, outmaybe;
  10.      char c, emptymsg[BUFSZ], qbuf[QBUFSZ], pbuf[QBUFSZ], xbuf[QBUFSZ];
  11.      int used = 0;
  12.      long loss;
  13.  
  14.      abort_looting = FALSE;
  15.      emptymsg[0] = '\0';
  16.  
  17.      if (!u_handsy())
  18.          return 0;
  19.  
  20.      if (obj->olocked) {
  21.          pline("%s locked.", Tobjnam(obj, "are"));
  22.          if (held)
  23.              You("must put it down to unlock.");
  24.          obj->lknown = 1;
  25.          return 0;
  26.      } else if (obj->otrapped) {
  27.          if (held)
  28.              You("open %s...", the(xname(obj)));
  29.          obj->lknown = 1;
  30.          (void) chest_trap(obj, HAND, FALSE);
  31.          /* even if the trap fails, you've used up this turn */
  32.          if (multi >= 0) { /* in case we didn't become paralyzed */
  33.              nomul(-1);
  34.              multi_reason = "opening a container";
  35.              nomovemsg = "";
  36.          }
  37.          abort_looting = TRUE;
  38.          return 1;
  39.      }
  40.      obj->lknown = 1;
  41.  
  42.      current_container = obj; /* for use by in/out_container */
  43.      /*
  44.       * From here on out, all early returns go through 'containerdone:'.
  45.       */
  46.  
  47.      /* check for Schroedinger's Cat */
  48.      quantum_cat = SchroedingersBox(current_container);
  49.      if (quantum_cat) {
  50.          observe_quantum_cat(current_container);
  51.          used = 1;
  52.      }
  53.  
  54.      cursed_mbag = Is_mbag(current_container)
  55.          && current_container->cursed
  56.          && Has_contents(current_container);
  57.      if (cursed_mbag
  58.          && (loss = boh_loss(current_container, held)) != 0) {
  59.          used = 1;
  60.          You("owe %ld %s for lost merchandise.", loss, currency(loss));
  61.          current_container->owt = weight(current_container);
  62.      }
  63.      inokay = (invent != 0
  64.                && !(invent == current_container && !current_container->nobj));
  65.      outokay = Has_contents(current_container);
  66.      if (!outokay) /* preformat the empty-container message */
  67.          Sprintf(emptymsg, "%s is %sempty.", Ysimple_name2(current_container),
  68.                  (quantum_cat || cursed_mbag) ? "now " : "");
  69.  
  70.      /*
  71.       * What-to-do prompt's list of possible actions:
  72.       * always include the look-inside choice (':');
  73.       * include the take-out choice ('o') if container
  74.       * has anything in it or if player doesn't yet know
  75.       * that it's empty (latter can change on subsequent
  76.       * iterations if player picks ':' response);
  77.       * include the put-in choices ('i','s') if hero
  78.       * carries any inventory (including gold);
  79.       * include do-both when 'o' is available, even if
  80.       * inventory is empty--taking out could alter that;
  81.       * include do-both-reversed when 'i' is available,
  82.       * even if container is empty--for similar reason;
  83.       * include the next container choice ('n') when
  84.       * relevant, and make it the default;
  85.       * always include the quit choice ('q'), and make
  86.       * it the default if there's no next containter;
  87.       * include the help choice (" or ?") if `cmdassist'
  88.       * run-time option is set;
  89.       * (Player can pick any of (o,i,b,r,n,s,?) even when
  90.       * they're not listed among the available actions.)
  91.       *
  92.       * Do what with <the/your/Shk's container>? [:oibrs nq or ?] (q)
  93.       * or
  94.       * <The/Your/Shk's container> is empty.  Do what with it? [:irs nq or ?]
  95.       */
  96.      for (;;) { /* repeats iff '?' or ":' gets chosen */
  97.          outmaybe = (outokay || !current_container->cknown);
  98.          if (!outmaybe)
  99.              (void) safe_qbuf(qbuf, (char *) 0, " is empty.  Do what with it?",
  100.                               current_container, Yname2, Ysimple_name2,
  101.                               "This");
  102.          else
  103.              (void) safe_qbuf(qbuf, "Do what with ", "?", current_container,
  104.                               yname, ysimple_name, "it");
  105.          /* ask player about what to do with this container */
  106.          if (flags.menu_style == MENU_PARTIAL
  107.              || flags.menu_style == MENU_FULL) {
  108.              if (!inokay && !outmaybe) {
  109.                  /* nothing to take out, nothing to put in;
  110.                     trying to do both will yield proper feedback */
  111.                  c = 'b';
  112.              } else {
  113.                  c = in_or_out_menu(qbuf, current_container, outmaybe, inokay,
  114.                                     (boolean) (used != 0), more_containers);
  115.              }
  116.          } else { /* TRADITIONAL or COMBINATION */
  117.              xbuf[0] = '\0'; /* list of extra acceptable responses */
  118.              Strcpy(pbuf, ":");                   /* look inside */
  119.              Strcat(outmaybe ? pbuf : xbuf, "o"); /* take out */
  120.              Strcat(inokay ? pbuf : xbuf, "i");   /* put in */
  121.              Strcat(outmaybe ? pbuf : xbuf, "b"); /* both */
  122.              Strcat(inokay ? pbuf : xbuf, "rs");  /* reversed, stash */
  123.              Strcat(pbuf, " ");                   /* separator */
  124.              Strcat(more_containers ? pbuf : xbuf, "n"); /* next container */
  125.              Strcat(pbuf, "q");                   /* quit */
  126.              if (iflags.cmdassist)
  127.                  /* this unintentionally allows user to answer with 'o' or
  128.                     'r'; fortunately, those are already valid choices here */
  129.                  Strcat(pbuf, " or ?"); /* help */
  130.              else
  131.                  Strcat(xbuf, "?");
  132.              if (*xbuf)
  133.                  Strcat(strcat(pbuf, "\033"), xbuf);
  134.              c = yn_function(qbuf, pbuf, more_containers ? 'n' : 'q');
  135.          } /* PARTIAL|FULL vs other modes */
  136.  
  137.          if (c == '?') {
  138.              explain_container_prompt(more_containers);
  139.          } else if (c == ':') { /* note: will set obj->cknown */
  140.              if (!current_container->cknown)
  141.                  used = 1; /* gaining info */
  142.              container_contents(current_container, FALSE, FALSE, TRUE);
  143.          } else
  144.              break;
  145.      } /* loop until something other than '?' or ':' is picked */
  146.  
  147.      if (c == 'q')
  148.          abort_looting = TRUE;
  149.      if (c == 'n' || c == 'q') /* [not strictly needed; falling thru works] */
  150.          goto containerdone;
  151.      loot_out = (c == 'o' || c == 'b' || c == 'r');
  152.      loot_in = (c == 'i' || c == 'b' || c == 'r');
  153.      loot_in_first = (c == 'r'); /* both, reversed */
  154.      stash_one = (c == 's');
  155.  
  156.      /* out-only or out before in */
  157.      if (loot_out && !loot_in_first) {
  158.          if (!Has_contents(current_container)) {
  159.              pline1(emptymsg); /* <whatever> is empty. */
  160.              if (!current_container->cknown)
  161.                  used = 1;
  162.              current_container->cknown = 1;
  163.          } else {
  164.              add_valid_menu_class(0); /* reset */
  165.              if (flags.menu_style == MENU_TRADITIONAL)
  166.                  used |= traditional_loot(FALSE);
  167.              else
  168.                  used |= (menu_loot(0, FALSE) > 0);
  169.              add_valid_menu_class(0);
  170.          }
  171.      }
  172.  
  173.      if ((loot_in || stash_one)
  174.          && (!invent || (invent == current_container && !invent->nobj))) {
  175.          You("don't have anything%s to %s.", invent ? " else" : "",
  176.              stash_one ? "stash" : "put in");
  177.          loot_in = stash_one = FALSE;
  178.      }
  179.  
  180.      /*
  181.       * Gone: being nice about only selecting food if we know we are
  182.       * putting things in an ice chest.
  183.       */
  184.      if (loot_in) {
  185.          add_valid_menu_class(0); /* reset */
  186.          if (flags.menu_style == MENU_TRADITIONAL)
  187.              used |= traditional_loot(TRUE);
  188.          else
  189.              used |= (menu_loot(0, TRUE) > 0);
  190.          add_valid_menu_class(0);
  191.      } else if (stash_one) {
  192.          /* put one item into container */
  193.          if ((otmp = getobj(stashable, "stash")) != 0) {
  194.              if (in_container(otmp)) {
  195.                  used = 1;
  196.              } else {
  197.                  /* couldn't put selected item into container for some
  198.                     reason; might need to undo splitobj() */
  199.                  (void) unsplitobj(otmp);
  200.              }
  201.          }
  202.      }
  203.      /* putting something in might have triggered magic bag explosion */
  204.      if (!current_container)
  205.          loot_out = FALSE;
  206.  
  207.      /* out after in */
  208.      if (loot_out && loot_in_first) {
  209.          if (!Has_contents(current_container)) {
  210.              pline1(emptymsg); /* <whatever> is empty. */
  211.              if (!current_container->cknown)
  212.                  used = 1;
  213.              current_container->cknown = 1;
  214.          } else {
  215.              add_valid_menu_class(0); /* reset */
  216.              if (flags.menu_style == MENU_TRADITIONAL)
  217.                  used |= traditional_loot(FALSE);
  218.              else
  219.                  used |= (menu_loot(0, FALSE) > 0);
  220.              add_valid_menu_class(0);
  221.          }
  222.      }
  223.  
  224.  containerdone:
  225.      if (used) {
  226.          /* Not completely correct; if we put something in without knowing
  227.             whatever was already inside, now we suddenly do.  That can't
  228.             be helped unless we want to track things item by item and then
  229.             deal with containers whose contents are "partly known". */
  230.          if (current_container)
  231.              current_container->cknown = 1;
  232.          update_inventory();
  233.      }
  234.  
  235.      *objp = current_container; /* might have become null */
  236.      if (current_container)
  237.          current_container = 0; /* avoid hanging on to stale pointer */
  238.      else
  239.          abort_looting = TRUE;
  240.      return used;
  241.  }
  242.  

traditional_loot

  1.  /* loot current_container (take things out or put things in), by prompting */
  2.  STATIC_OVL int
  3.  traditional_loot(put_in)
  4.  boolean put_in;
  5.  {
  6.      int FDECL((*actionfunc), (OBJ_P)), FDECL((*checkfunc), (OBJ_P));
  7.      struct obj **objlist;
  8.      char selection[MAXOCLASSES + 10]; /* +10: room for B,U,C,X plus slop */
  9.      const char *action;
  10.      boolean one_by_one, allflag;
  11.      int used = 0, menu_on_request = 0;
  12.  
  13.      if (put_in) {
  14.          action = "put in";
  15.          objlist = &invent;
  16.          actionfunc = in_container;
  17.          checkfunc = ck_bag;
  18.      } else {
  19.          action = "take out";
  20.          objlist = &(current_container->cobj);
  21.          actionfunc = out_container;
  22.          checkfunc = (int FDECL((*), (OBJ_P))) 0;
  23.      }
  24.  
  25.      if (query_classes(selection, &one_by_one, &allflag, action, *objlist,
  26.                        FALSE, &menu_on_request)) {
  27.          if (askchain(objlist, (one_by_one ? (char *) 0 : selection), allflag,
  28.                       actionfunc, checkfunc, 0, action))
  29.              used = 1;
  30.      } else if (menu_on_request < 0) {
  31.          used = (menu_loot(menu_on_request, put_in) > 0);
  32.      }
  33.      return used;
  34.  }
  35.  

menu_loot

  1.  /* loot current_container (take things out or put things in), using a menu */
  2.  STATIC_OVL int
  3.  menu_loot(retry, put_in)
  4.  int retry;
  5.  boolean put_in;
  6.  {
  7.      int n, i, n_looted = 0;
  8.      boolean all_categories = TRUE, loot_everything = FALSE;
  9.      char buf[BUFSZ];
  10.      const char *action = put_in ? "Put in" : "Take out";
  11.      struct obj *otmp, *otmp2;
  12.      menu_item *pick_list;
  13.      int mflags, res;
  14.      long count;
  15.  
  16.      if (retry) {
  17.          all_categories = (retry == -2);
  18.      } else if (flags.menu_style == MENU_FULL) {
  19.          all_categories = FALSE;
  20.          Sprintf(buf, "%s what type of objects?", action);
  21.          mflags = (ALL_TYPES | UNPAID_TYPES | BUCX_TYPES);
  22.          if (put_in)
  23.              mflags |= CHOOSE_ALL;
  24.          n = query_category(buf, put_in ? invent : current_container->cobj,
  25.                             mflags, &pick_list, PICK_ANY);
  26.          if (!n)
  27.              return 0;
  28.          for (i = 0; i < n; i++) {
  29.              if (pick_list[i].item.a_int == 'A')
  30.                  loot_everything = TRUE;
  31.              else if (pick_list[i].item.a_int == ALL_TYPES_SELECTED)
  32.                  all_categories = TRUE;
  33.              else
  34.                  add_valid_menu_class(pick_list[i].item.a_int);
  35.          }
  36.          free((genericptr_t) pick_list);
  37.      }
  38.  
  39.      if (loot_everything) {
  40.          current_container->cknown = 1;
  41.          for (otmp = current_container->cobj; otmp; otmp = otmp2) {
  42.              otmp2 = otmp->nobj;
  43.              res = out_container(otmp);
  44.              if (res < 0)
  45.                  break;
  46.          }
  47.      } else {
  48.          mflags = INVORDER_SORT;
  49.          if (put_in && flags.invlet_constant)
  50.              mflags |= USE_INVLET;
  51.          if (!put_in)
  52.              current_container->cknown = 1;
  53.          Sprintf(buf, "%s what?", action);
  54.          n = query_objlist(buf, put_in ? &invent : &(current_container->cobj),
  55.                            mflags, &pick_list, PICK_ANY,
  56.                            all_categories ? allow_all : allow_category);
  57.          if (n) {
  58.              n_looted = n;
  59.              for (i = 0; i < n; i++) {
  60.                  otmp = pick_list[i].item.a_obj;
  61.                  count = pick_list[i].count;
  62.                  if (count > 0 && count < otmp->quan) {
  63.                      otmp = splitobj(otmp, count);
  64.                      /* special split case also handled by askchain() */
  65.                  }
  66.                  res = put_in ? in_container(otmp) : out_container(otmp);
  67.                  if (res < 0) {
  68.                      if (!current_container) {
  69.                          /* otmp caused current_container to explode;
  70.                             both are now gone */
  71.                          otmp = 0; /* and break loop */
  72.                      } else if (otmp && otmp != pick_list[i].item.a_obj) {
  73.                          /* split occurred, merge again */
  74.                          (void) merged(&pick_list[i].item.a_obj, &otmp);
  75.                      }
  76.                      break;
  77.                  }
  78.              }
  79.              free((genericptr_t) pick_list);
  80.          }
  81.      }
  82.      return n_looted;
  83.  }
  84.  

in_or_out_menu

  1.  STATIC_OVL char
  2.  in_or_out_menu(prompt, obj, outokay, inokay, alreadyused, more_containers)
  3.  const char *prompt;
  4.  struct obj *obj;
  5.  boolean outokay, inokay, alreadyused, more_containers;
  6.  {
  7.      /* underscore is not a choice; it's used to skip element [0] */
  8.      static const char lootchars[] = "_:oibrsnq", abc_chars[] = "_:abcdenq";
  9.      winid win;
  10.      anything any;
  11.      menu_item *pick_list;
  12.      char buf[BUFSZ];
  13.      int n;
  14.      const char *menuselector = flags.lootabc ? abc_chars : lootchars;
  15.  
  16.      any = zeroany;
  17.      win = create_nhwindow(NHW_MENU);
  18.      start_menu(win);
  19.  
  20.      any.a_int = 1; /* ':' */
  21.      Sprintf(buf, "Look inside %s", thesimpleoname(obj));
  22.      add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE, buf,
  23.               MENU_UNSELECTED);
  24.      if (outokay) {
  25.          any.a_int = 2; /* 'o' */
  26.          Sprintf(buf, "take %s out", something);
  27.          add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
  28.                   buf, MENU_UNSELECTED);
  29.      }
  30.      if (inokay) {
  31.          any.a_int = 3; /* 'i' */
  32.          Sprintf(buf, "put %s in", something);
  33.          add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
  34.                   buf, MENU_UNSELECTED);
  35.      }
  36.      if (outokay) {
  37.          any.a_int = 4; /* 'b' */
  38.          Sprintf(buf, "%stake out, then put in", inokay ? "both; " : "");
  39.          add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
  40.                   buf, MENU_UNSELECTED);
  41.      }
  42.      if (inokay) {
  43.          any.a_int = 5; /* 'r' */
  44.          Sprintf(buf, "%sput in, then take out",
  45.                  outokay ? "both reversed; " : "");
  46.          add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
  47.                   buf, MENU_UNSELECTED);
  48.          any.a_int = 6; /* 's' */
  49.          Sprintf(buf, "stash one item into %s", thesimpleoname(obj));
  50.          add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
  51.                   buf, MENU_UNSELECTED);
  52.      }
  53.      any.a_int = 0;
  54.      add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE, "", MENU_UNSELECTED);
  55.      if (more_containers) {
  56.          any.a_int = 7; /* 'n' */
  57.          add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
  58.                   "loot next container", MENU_SELECTED);
  59.      }
  60.      any.a_int = 8; /* 'q' */
  61.      Strcpy(buf, alreadyused ? "done" : "do nothing");
  62.      add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE, buf,
  63.               more_containers ? MENU_UNSELECTED : MENU_SELECTED);
  64.  
  65.      end_menu(win, prompt);
  66.      n = select_menu(win, PICK_ONE, &pick_list);
  67.      destroy_nhwindow(win);
  68.      if (n > 0) {
  69.          int k = pick_list[0].item.a_int;
  70.  
  71.          if (n > 1 && k == (more_containers ? 7 : 8))
  72.              k = pick_list[1].item.a_int;
  73.          free((genericptr_t) pick_list);
  74.          return lootchars[k]; /* :,o,i,b,r,s,n,q */
  75.      }
  76.      return (n == 0 && more_containers) ? 'n' : 'q'; /* next or quit */
  77.  }
  78.  
  79.  static const char tippables[] = { ALL_CLASSES, TOOL_CLASS, 0 };
  80.  

dotip

  1.  /* #tip command -- empty container contents onto floor */
  2.  int
  3.  dotip()
  4.  {
  5.      struct obj *cobj, *nobj;
  6.      coord cc;
  7.      int boxes;
  8.      char c, buf[BUFSZ], qbuf[BUFSZ];
  9.      const char *spillage = 0;
  10.  
  11.      /*
  12.       * doesn't require free hands;
  13.       * limbs are needed to tip floor containers
  14.       */
  15.  
  16.      /* at present, can only tip things at current spot, not adjacent ones */
  17.      cc.x = u.ux, cc.y = u.uy;
  18.  
  19.      /* check floor container(s) first; at most one will be accessed */
  20.      if ((boxes = container_at(cc.x, cc.y, TRUE)) > 0) {
  21.          Sprintf(buf, "You can't tip %s while carrying so much.",
  22.                  !flags.verbose ? "a container" : (boxes > 1) ? "one" : "it");
  23.          if (!check_capacity(buf) && able_to_loot(cc.x, cc.y, FALSE)) {
  24.              if (boxes > 1 && (flags.menu_style != MENU_TRADITIONAL
  25.                                || iflags.menu_requested)) {
  26.                  /* use menu to pick a container to tip */
  27.                  int n, i;
  28.                  winid win;
  29.                  anything any;
  30.                  menu_item *pick_list = (menu_item *) 0;
  31.                  struct obj dummyobj, *otmp;
  32.  
  33.                  any = zeroany;
  34.                  win = create_nhwindow(NHW_MENU);
  35.                  start_menu(win);
  36.  
  37.                  for (cobj = level.objects[cc.x][cc.y], i = 0; cobj;
  38.                       cobj = cobj->nexthere)
  39.                      if (Is_container(cobj)) {
  40.                          ++i;
  41.                          any.a_obj = cobj;
  42.                          add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE,
  43.                                   doname(cobj), MENU_UNSELECTED);
  44.                      }
  45.                  if (invent) {
  46.                      any = zeroany;
  47.                      add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE,
  48.                               "", MENU_UNSELECTED);
  49.                      any.a_obj = &dummyobj;
  50.                      /* use 'i' for inventory unless there are so many
  51.                         containers that it's already being used */
  52.                      i = (i <= 'i' - 'a' && !flags.lootabc) ? 'i' : 0;
  53.                      add_menu(win, NO_GLYPH, &any, i, 0, ATR_NONE,
  54.                               "tip something being carried", MENU_SELECTED);
  55.                  }
  56.                  end_menu(win, "Tip which container?");
  57.                  n = select_menu(win, PICK_ONE, &pick_list);
  58.                  destroy_nhwindow(win);
  59.                  /*
  60.                   * Deal with quirk of preselected item in pick-one menu:
  61.                   * n ==  0 => picked preselected entry, toggling it off;
  62.                   * n ==  1 => accepted preselected choice via SPACE or RETURN;
  63.                   * n ==  2 => picked something other than preselected entry;
  64.                   * n == -1 => cancelled via ESC;
  65.                   */
  66.                  otmp = (n <= 0) ? (struct obj *) 0 : pick_list[0].item.a_obj;
  67.                  if (n > 1 && otmp == &dummyobj)
  68.                      otmp = pick_list[1].item.a_obj;
  69.                  if (pick_list)
  70.                      free((genericptr_t) pick_list);
  71.                  if (otmp && otmp != &dummyobj) {
  72.                      tipcontainer(otmp);
  73.                      return 1;
  74.                  }
  75.                  if (n == -1)
  76.                      return 0;
  77.                  /* else pick-from-invent below */
  78.              } else {
  79.                  for (cobj = level.objects[cc.x][cc.y]; cobj; cobj = nobj) {
  80.                      nobj = cobj->nexthere;
  81.                      if (!Is_container(cobj))
  82.                          continue;
  83.                      c = ynq(safe_qbuf(qbuf, "There is ", " here, tip it?",
  84.                                        cobj,
  85.                                        doname, ansimpleoname, "container"));
  86.                      if (c == 'q')
  87.                          return 0;
  88.                      if (c == 'n')
  89.                          continue;
  90.                      tipcontainer(cobj);
  91.                      /* can only tip one container at a time */
  92.                      return 1;
  93.                  }
  94.              }
  95.          }
  96.      }
  97.  
  98.      /* either no floor container(s) or couldn't tip one or didn't tip any */
  99.      cobj = getobj(tippables, "tip");
  100.      if (!cobj)
  101.          return 0;
  102.  
  103.      /* normal case */
  104.      if (Is_container(cobj) || cobj->otyp == HORN_OF_PLENTY) {
  105.          tipcontainer(cobj);
  106.          return 1;
  107.      }
  108.      /* assorted other cases */
  109.      if (Is_candle(cobj) && cobj->lamplit) {
  110.          /* note "wax" even for tallow candles to avoid giving away info */
  111.          spillage = "wax";
  112.      } else if ((cobj->otyp == POT_OIL && cobj->lamplit)
  113.                 || (cobj->otyp == OIL_LAMP && cobj->age != 0L)
  114.                 || (cobj->otyp == MAGIC_LAMP && cobj->spe != 0)) {
  115.          spillage = "oil";
  116.          /* todo: reduce potion's remaining burn timer or oil lamp's fuel */
  117.      } else if (cobj->otyp == CAN_OF_GREASE && cobj->spe > 0) {
  118.          /* charged consumed below */
  119.          spillage = "grease";
  120.      } else if (cobj->otyp == FOOD_RATION || cobj->otyp == CRAM_RATION
  121.                 || cobj->otyp == LEMBAS_WAFER) {
  122.          spillage = "crumbs";
  123.      } else if (cobj->oclass == VENOM_CLASS) {
  124.          spillage = "venom";
  125.      }
  126.      if (spillage) {
  127.          buf[0] = '\0';
  128.          if (is_pool(u.ux, u.uy))
  129.              Sprintf(buf, " and gradually %s", vtense(spillage, "dissipate"));
  130.          else if (is_lava(u.ux, u.uy))
  131.              Sprintf(buf, " and immediately %s away",
  132.                      vtense(spillage, "burn"));
  133.          pline("Some %s %s onto the %s%s.", spillage,
  134.                vtense(spillage, "spill"), surface(u.ux, u.uy), buf);
  135.          /* shop usage message comes after the spill message */
  136.          if (cobj->otyp == CAN_OF_GREASE && cobj->spe > 0) {
  137.              consume_obj_charge(cobj, TRUE);
  138.          }
  139.          /* something [useless] happened */
  140.          return 1;
  141.      }
  142.      /* anything not covered yet */
  143.      if (cobj->oclass == POTION_CLASS) /* can't pour potions... */
  144.          pline_The("%s %s securely sealed.", xname(cobj), otense(cobj, "are"));
  145.      else if (cobj->otyp == STATUE)
  146.          pline("Nothing interesting happens.");
  147.      else
  148.          pline1(nothing_happens);
  149.      return 0;
  150.  }
  151.  

tipcontainer

  1.  STATIC_OVL void
  2.  tipcontainer(box)
  3.  struct obj *box; /* or bag */
  4.  {
  5.      xchar ox = u.ux, oy = u.uy; /* #tip only works at hero's location */
  6.      boolean empty_it = FALSE, maybeshopgoods;
  7.  
  8.      /* box is either held or on floor at hero's spot; no need to check for
  9.         nesting; when held, we need to update its location to match hero's;
  10.         for floor, the coordinate updating is redundant */
  11.      if (get_obj_location(box, &ox, &oy, 0))
  12.          box->ox = ox, box->oy = oy;
  13.  
  14.      /* Shop handling:  can't rely on the container's own unpaid
  15.         or no_charge status because contents might differ with it.
  16.         A carried container's contents will be flagged as unpaid
  17.         or not, as appropriate, and need no special handling here.
  18.         Items owned by the hero get sold to the shop without
  19.         confirmation as with other uncontrolled drops.  A floor
  20.         container's contents will be marked no_charge if owned by
  21.         hero, otherwise they're owned by the shop.  By passing
  22.         the contents through shop billing, they end up getting
  23.         treated the same as in the carried case.   We do so one
  24.         item at a time instead of doing whole container at once
  25.         to reduce the chance of exhausting shk's billing capacity. */
  26.      maybeshopgoods = !carried(box) && costly_spot(box->ox, box->oy);
  27.  
  28.      /* caveat: this assumes that cknown, lknown, olocked, and otrapped
  29.         fields haven't been overloaded to mean something special for the
  30.         non-standard "container" horn of plenty */
  31.      box->lknown = 1;
  32.      if (box->olocked) {
  33.          pline("It's locked.");
  34.      } else if (box->otrapped) {
  35.          /* we're not reaching inside but we're still handling it... */
  36.          (void) chest_trap(box, HAND, FALSE);
  37.          /* even if the trap fails, you've used up this turn */
  38.          if (multi >= 0) { /* in case we didn't become paralyzed */
  39.              nomul(-1);
  40.              multi_reason = "tipping a container";
  41.              nomovemsg = "";
  42.          }
  43.      } else if (box->otyp == BAG_OF_TRICKS || box->otyp == HORN_OF_PLENTY) {
  44.          boolean bag = box->otyp == BAG_OF_TRICKS;
  45.          int old_spe = box->spe, seen = 0;
  46.  
  47.          if (maybeshopgoods && !box->no_charge)
  48.              addtobill(box, FALSE, FALSE, TRUE);
  49.          /* apply this bag/horn until empty or monster/object creation fails
  50.             (if the latter occurs, force the former...) */
  51.          do {
  52.              if (!(bag ? bagotricks(box, TRUE, &seen)
  53.                        : hornoplenty(box, TRUE)))
  54.                  break;
  55.          } while (box->spe > 0);
  56.  
  57.          if (box->spe < old_spe) {
  58.              if (bag)
  59.                  pline((seen == 0) ? "Nothing seems to happen."
  60.                                    : (seen == 1) ? "A monster appears."
  61.                                                  : "Monsters appear!");
  62.              /* check_unpaid wants to see a non-zero charge count */
  63.              box->spe = old_spe;
  64.              check_unpaid_usage(box, TRUE);
  65.              box->spe = 0; /* empty */
  66.              box->cknown = 1;
  67.          }
  68.          if (maybeshopgoods && !box->no_charge)
  69.              subfrombill(box, shop_keeper(*in_rooms(ox, oy, SHOPBASE)));
  70.      } else if (SchroedingersBox(box)) {
  71.          char yourbuf[BUFSZ];
  72.  
  73.          observe_quantum_cat(box);
  74.          if (!Has_contents(box)) /* evidently a live cat came out */
  75.              /* container type of "large box" is inferred */
  76.              pline("%sbox is now empty.", Shk_Your(yourbuf, box));
  77.          else /* holds cat corpse */
  78.              empty_it = TRUE;
  79.          box->cknown = 1;
  80.      } else if (!Has_contents(box)) {
  81.          box->cknown = 1;
  82.          pline("It's empty.");
  83.      } else {
  84.          empty_it = TRUE;
  85.      }
  86.  
  87.      if (empty_it) {
  88.          struct obj *otmp, *nobj;
  89.          boolean terse, highdrop = !can_reach_floor(TRUE),
  90.                  altarizing = IS_ALTAR(levl[ox][oy].typ),
  91.                  cursed_mbag = (Is_mbag(box) && box->cursed);
  92.          int held = carried(box);
  93.          long loss = 0L;
  94.  
  95.          if (u.uswallow)
  96.              highdrop = altarizing = FALSE;
  97.          terse = !(highdrop || altarizing || costly_spot(box->ox, box->oy));
  98.          box->cknown = 1;
  99.          /* Terse formatting is
  100.           * "Objects spill out: obj1, obj2, obj3, ..., objN."
  101.           * If any other messages intervene between objects, we revert to
  102.           * "ObjK drops to the floor.", "ObjL drops to the floor.", &c.
  103.           */
  104.          pline("%s out%c",
  105.                box->cobj->nobj ? "Objects spill" : "An object spills",
  106.                terse ? ':' : '.');
  107.          for (otmp = box->cobj; otmp; otmp = nobj) {
  108.              nobj = otmp->nobj;
  109.              obj_extract_self(otmp);
  110.              otmp->ox = box->ox, otmp->oy = box->oy;
  111.  
  112.              if (box->otyp == ICE_BOX) {
  113.                  removed_from_icebox(otmp); /* resume rotting for corpse */
  114.              } else if (cursed_mbag && !rn2(13)) {
  115.                  loss += mbag_item_gone(held, otmp);
  116.                  /* abbreviated drop format is no longer appropriate */
  117.                  terse = FALSE;
  118.                  continue;
  119.              }
  120.  
  121.              if (maybeshopgoods) {
  122.                  addtobill(otmp, FALSE, FALSE, TRUE);
  123.                  iflags.suppress_price++; /* doname formatting */
  124.              }
  125.  
  126.              if (highdrop) {
  127.                  /* might break or fall down stairs; handles altars itself */
  128.                  hitfloor(otmp);
  129.              } else {
  130.                  if (altarizing) {
  131.                      doaltarobj(otmp);
  132.                  } else if (!terse) {
  133.                      pline("%s %s to the %s.", Doname2(otmp),
  134.                            otense(otmp, "drop"), surface(ox, oy));
  135.                  } else {
  136.                      pline("%s%c", doname(otmp), nobj ? ',' : '.');
  137.                      iflags.last_msg = PLNMSG_OBJNAM_ONLY;
  138.                  }
  139.                  dropy(otmp);
  140.                  if (iflags.last_msg != PLNMSG_OBJNAM_ONLY)
  141.                      terse = FALSE; /* terse formatting has been interrupted */
  142.              }
  143.              if (maybeshopgoods)
  144.                  iflags.suppress_price--; /* reset */
  145.          }
  146.          if (loss) /* magic bag lost some shop goods */
  147.              You("owe %ld %s for lost merchandise.", loss, currency(loss));
  148.          box->owt = weight(box); /* mbag_item_gone() doesn't update this */
  149.          if (held)
  150.              (void) encumber_msg();
  151.      }
  152.  }
  153.  
  154.  /*pickup.c*/