Source:NetHack 3.6.0/src/shk.c

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

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

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

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

Contents

Top of file

  1.  /* NetHack 3.6	shk.c	$NHDT-Date: 1446854234 2015/11/06 23:57:14 $  $NHDT-Branch: master $:$NHDT-Revision: 1.116 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5.  #include "hack.h"
  6.  
  7.  #define PAY_SOME 2
  8.  #define PAY_BUY 1
  9.  #define PAY_CANT 0 /* too poor */
  10.  #define PAY_SKIP (-1)
  11.  #define PAY_BROKE (-2)
  12.  
  13.  STATIC_DCL void FDECL(makekops, (coord *));
  14.  STATIC_DCL void FDECL(call_kops, (struct monst *, BOOLEAN_P));
  15.  STATIC_DCL void FDECL(kops_gone, (BOOLEAN_P));
  16.  
  17.  #define NOTANGRY(mon) ((mon)->mpeaceful)
  18.  #define ANGRY(mon) (!NOTANGRY(mon))
  19.  #define IS_SHOP(x) (rooms[x].rtype >= SHOPBASE)
  20.  
  21.  #define muteshk(shkp)                       \
  22.      ((shkp)->msleeping || !(shkp)->mcanmove \
  23.       || (shkp)->data->msound <= MS_ANIMAL)
  24.  
  25.  extern const struct shclass shtypes[]; /* defined in shknam.c */
  26.  
  27.  STATIC_VAR NEARDATA long int followmsg; /* last time of follow message */
  28.  STATIC_VAR const char and_its_contents[] = " and its contents";
  29.  STATIC_VAR const char the_contents_of[] = "the contents of ";
  30.  
  31.  STATIC_DCL void FDECL(append_honorific, (char *));
  32.  STATIC_DCL void FDECL(setpaid, (struct monst *));
  33.  STATIC_DCL long FDECL(addupbill, (struct monst *));
  34.  STATIC_DCL void FDECL(pacify_shk, (struct monst *));
  35.  STATIC_DCL struct bill_x *FDECL(onbill, (struct obj *, struct monst *,
  36.                                           BOOLEAN_P));
  37.  STATIC_DCL struct monst *FDECL(next_shkp, (struct monst *, BOOLEAN_P));
  38.  STATIC_DCL long FDECL(shop_debt, (struct eshk *));
  39.  STATIC_DCL char *FDECL(shk_owns, (char *, struct obj *));
  40.  STATIC_DCL char *FDECL(mon_owns, (char *, struct obj *));
  41.  STATIC_DCL void FDECL(clear_unpaid, (struct obj *));
  42.  STATIC_DCL long FDECL(check_credit, (long, struct monst *));
  43.  STATIC_DCL void FDECL(pay, (long, struct monst *));
  44.  STATIC_DCL long FDECL(get_cost, (struct obj *, struct monst *));
  45.  STATIC_DCL long FDECL(set_cost, (struct obj *, struct monst *));
  46.  STATIC_DCL const char *FDECL(shk_embellish, (struct obj *, long));
  47.  STATIC_DCL long FDECL(cost_per_charge, (struct monst *, struct obj *,
  48.                                          BOOLEAN_P));
  49.  STATIC_DCL long FDECL(cheapest_item, (struct monst *));
  50.  STATIC_DCL int FDECL(dopayobj, (struct monst *, struct bill_x *,
  51.                                  struct obj **, int, BOOLEAN_P));
  52.  STATIC_DCL long FDECL(stolen_container, (struct obj *, struct monst *,
  53.                                           long, BOOLEAN_P));
  54.  STATIC_DCL long FDECL(getprice, (struct obj *, BOOLEAN_P));
  55.  STATIC_DCL void FDECL(shk_names_obj, (struct monst *, struct obj *,
  56.                                        const char *, long, const char *));
  57.  STATIC_DCL struct obj *FDECL(bp_to_obj, (struct bill_x *));
  58.  STATIC_DCL boolean FDECL(inherits, (struct monst *, int, int));
  59.  STATIC_DCL void FDECL(set_repo_loc, (struct monst *));
  60.  STATIC_DCL boolean NDECL(angry_shk_exists);
  61.  STATIC_DCL void FDECL(rile_shk, (struct monst *));
  62.  STATIC_DCL void FDECL(rouse_shk, (struct monst *, BOOLEAN_P));
  63.  STATIC_DCL void FDECL(remove_damage, (struct monst *, BOOLEAN_P));
  64.  STATIC_DCL void FDECL(sub_one_frombill, (struct obj *, struct monst *));
  65.  STATIC_DCL void FDECL(add_one_tobill, (struct obj *, BOOLEAN_P,
  66.                                         struct monst *));
  67.  STATIC_DCL void FDECL(dropped_container, (struct obj *, struct monst *,
  68.                                            BOOLEAN_P));
  69.  STATIC_DCL void FDECL(add_to_billobjs, (struct obj *));
  70.  STATIC_DCL void FDECL(bill_box_content, (struct obj *, BOOLEAN_P, BOOLEAN_P,
  71.                                           struct monst *));
  72.  STATIC_DCL boolean FDECL(rob_shop, (struct monst *));
  73.  STATIC_DCL void FDECL(deserted_shop, (char *));
  74.  STATIC_DCL boolean FDECL(special_stock, (struct obj *, struct monst *,
  75.                                           BOOLEAN_P));
  76.  STATIC_DCL const char *FDECL(cad, (BOOLEAN_P));
  77.  
  78.  /*
  79.          invariants: obj->unpaid iff onbill(obj) [unless bp->useup]
  80.                      obj->quan <= bp->bquan
  81.   */
  82.  

money2mon

  1.  /*
  2.   *  Transfer money from inventory to monster when paying
  3.   *  shopkeepers, priests, oracle, succubus, and other demons.
  4.   *  Simple with only gold coins.
  5.   *  This routine will handle money changing when multiple
  6.   *  coin types is implemented, only appropriate
  7.   *  monsters will pay change.  (Peaceful shopkeepers, priests
  8.   *  and the oracle try to maintain goodwill while selling
  9.   *  their wares or services.  Angry monsters and all demons
  10.   *  will keep anything they get their hands on.
  11.   *  Returns the amount actually paid, so we can know
  12.   *  if the monster kept the change.
  13.   */
  14.  long
  15.  money2mon(mon, amount)
  16.  struct monst *mon;
  17.  long amount;
  18.  {
  19.      struct obj *ygold = findgold(invent);
  20.  
  21.      if (amount <= 0) {
  22.          impossible("%s payment in money2mon!", amount ? "negative" : "zero");
  23.          return 0L;
  24.      }
  25.      if (!ygold || ygold->quan < amount) {
  26.          impossible("Paying without %s money?", ygold ? "enough" : "");
  27.          return 0L;
  28.      }
  29.  
  30.      if (ygold->quan > amount)
  31.          ygold = splitobj(ygold, amount);
  32.      else if (ygold->owornmask)
  33.          remove_worn_item(ygold, FALSE); /* quiver */
  34.      freeinv(ygold);
  35.      add_to_minv(mon, ygold);
  36.      context.botl = 1;
  37.      return amount;
  38.  }
  39.  

money2u

  1.  /*
  2.   *  Transfer money from monster to inventory.
  3.   *  Used when the shopkeeper pay for items, and when
  4.   *  the priest gives you money for an ale.
  5.   */
  6.  void
  7.  money2u(mon, amount)
  8.  struct monst *mon;
  9.  long amount;
  10.  {
  11.      struct obj *mongold = findgold(mon->minvent);
  12.  
  13.      if (amount <= 0) {
  14.          impossible("%s payment in money2u!", amount ? "negative" : "zero");
  15.          return;
  16.      }
  17.      if (!mongold || mongold->quan < amount) {
  18.          impossible("%s paying without %s money?", a_monnam(mon),
  19.                     mongold ? "enough" : "");
  20.          return;
  21.      }
  22.  
  23.      if (mongold->quan > amount)
  24.          mongold = splitobj(mongold, amount);
  25.      obj_extract_self(mongold);
  26.  
  27.      if (!merge_choice(invent, mongold) && inv_cnt(FALSE) >= 52) {
  28.          You("have no room for the money!");
  29.          dropy(mongold);
  30.      } else {
  31.          addinv(mongold);
  32.          context.botl = 1;
  33.      }
  34.  }
  35.  

next_shkp

  1.  STATIC_OVL struct monst *
  2.  next_shkp(shkp, withbill)
  3.  register struct monst *shkp;
  4.  register boolean withbill;
  5.  {
  6.      for (; shkp; shkp = shkp->nmon) {
  7.          if (DEADMONSTER(shkp))
  8.              continue;
  9.          if (shkp->isshk && (ESHK(shkp)->billct || !withbill))
  10.              break;
  11.      }
  12.  
  13.      if (shkp) {
  14.          if (NOTANGRY(shkp)) {
  15.              if (ESHK(shkp)->surcharge)
  16.                  pacify_shk(shkp);
  17.          } else {
  18.              if (!ESHK(shkp)->surcharge)
  19.                  rile_shk(shkp);
  20.          }
  21.      }
  22.      return shkp;
  23.  }
  24.  

shkgone

  1.  /* called in mon.c */
  2.  void
  3.  shkgone(mtmp)
  4.  struct monst *mtmp;
  5.  {
  6.      struct eshk *eshk = ESHK(mtmp);
  7.      struct mkroom *sroom = &rooms[eshk->shoproom - ROOMOFFSET];
  8.      struct obj *otmp;
  9.      char *p;
  10.      int sx, sy;
  11.  
  12.      /* [BUG: some of this should be done on the shop level */
  13.      /*       even when the shk dies on a different level.] */
  14.      if (on_level(&eshk->shoplevel, &u.uz)) {
  15.          remove_damage(mtmp, TRUE);
  16.          sroom->resident = (struct monst *) 0;
  17.          if (!search_special(ANY_SHOP))
  18.              level.flags.has_shop = 0;
  19.  
  20.          /* items on shop floor revert to ordinary objects */
  21.          for (sx = sroom->lx; sx <= sroom->hx; sx++)
  22.              for (sy = sroom->ly; sy <= sroom->hy; sy++)
  23.                  for (otmp = level.objects[sx][sy]; otmp;
  24.                       otmp = otmp->nexthere)
  25.                      otmp->no_charge = 0;
  26.  
  27.          /* Make sure bill is set only when the
  28.             dead shk is the resident shk. */
  29.          if ((p = index(u.ushops, eshk->shoproom)) != 0) {
  30.              setpaid(mtmp);
  31.              eshk->bill_p = (struct bill_x *) 0;
  32.              /* remove eshk->shoproom from u.ushops */
  33.              do {
  34.                  *p = *(p + 1);
  35.              } while (*++p);
  36.          }
  37.      }
  38.  }
  39.  

set_residency

  1.  void
  2.  set_residency(shkp, zero_out)
  3.  register struct monst *shkp;
  4.  register boolean zero_out;
  5.  {
  6.      if (on_level(&(ESHK(shkp)->shoplevel), &u.uz))
  7.          rooms[ESHK(shkp)->shoproom - ROOMOFFSET].resident =
  8.              (zero_out) ? (struct monst *) 0 : shkp;
  9.  }
  10.  

replshk

  1.  void
  2.  replshk(mtmp, mtmp2)
  3.  register struct monst *mtmp, *mtmp2;
  4.  {
  5.      rooms[ESHK(mtmp2)->shoproom - ROOMOFFSET].resident = mtmp2;
  6.      if (inhishop(mtmp) && *u.ushops == ESHK(mtmp)->shoproom) {
  7.          ESHK(mtmp2)->bill_p = &(ESHK(mtmp2)->bill[0]);
  8.      }
  9.  }
  10.  

restshk

  1.  /* do shopkeeper specific structure munging -dlc */
  2.  void
  3.  restshk(shkp, ghostly)
  4.  struct monst *shkp;
  5.  boolean ghostly;
  6.  {
  7.      if (u.uz.dlevel) {
  8.          struct eshk *eshkp = ESHK(shkp);
  9.  
  10.          if (eshkp->bill_p != (struct bill_x *) -1000)
  11.              eshkp->bill_p = &eshkp->bill[0];
  12.          /* shoplevel can change as dungeons move around */
  13.          /* savebones guarantees that non-homed shk's will be gone */
  14.          if (ghostly) {
  15.              assign_level(&eshkp->shoplevel, &u.uz);
  16.              if (ANGRY(shkp) && strncmpi(eshkp->customer, plname, PL_NSIZ))
  17.                  pacify_shk(shkp);
  18.          }
  19.      }
  20.  }
  21.  

clear_unpaid

  1.  /* Clear the unpaid bit on all of the objects in the list. */
  2.  STATIC_OVL void
  3.  clear_unpaid(list)
  4.  register struct obj *list;
  5.  {
  6.      while (list) {
  7.          if (Has_contents(list))
  8.              clear_unpaid(list->cobj);
  9.          list->unpaid = 0;
  10.          list = list->nobj;
  11.      }
  12.  }
  13.  

setpaid

  1.  /* either you paid or left the shop or the shopkeeper died */
  2.  STATIC_OVL void
  3.  setpaid(shkp)
  4.  register struct monst *shkp;
  5.  {
  6.      register struct obj *obj;
  7.      register struct monst *mtmp;
  8.  
  9.      /* FIXME: object handling should be limited to
  10.         items which are on this particular shk's bill */
  11.  
  12.      clear_unpaid(invent);
  13.      clear_unpaid(fobj);
  14.      clear_unpaid(level.buriedobjlist);
  15.      if (thrownobj)
  16.          thrownobj->unpaid = 0;
  17.      if (kickedobj)
  18.          kickedobj->unpaid = 0;
  19.      for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  20.          clear_unpaid(mtmp->minvent);
  21.      for (mtmp = migrating_mons; mtmp; mtmp = mtmp->nmon)
  22.          clear_unpaid(mtmp->minvent);
  23.  
  24.      while ((obj = billobjs) != 0) {
  25.          obj_extract_self(obj);
  26.          dealloc_obj(obj);
  27.      }
  28.      if (shkp) {
  29.          ESHK(shkp)->billct = 0;
  30.          ESHK(shkp)->credit = 0L;
  31.          ESHK(shkp)->debit = 0L;
  32.          ESHK(shkp)->loan = 0L;
  33.      }
  34.  }
  35.  

addupbill

  1.  STATIC_OVL long
  2.  addupbill(shkp)
  3.  register struct monst *shkp;
  4.  {
  5.      register int ct = ESHK(shkp)->billct;
  6.      register struct bill_x *bp = ESHK(shkp)->bill_p;
  7.      register long total = 0L;
  8.  
  9.      while (ct--) {
  10.          total += bp->price * bp->bquan;
  11.          bp++;
  12.      }
  13.      return total;
  14.  }
  15.  

call_kops

  1.  STATIC_OVL void
  2.  call_kops(shkp, nearshop)
  3.  register struct monst *shkp;
  4.  register boolean nearshop;
  5.  {
  6.      /* Keystone Kops srt@ucla */
  7.      register boolean nokops;
  8.  
  9.      if (!shkp)
  10.          return;
  11.  
  12.      if (!Deaf)
  13.          pline("An alarm sounds!");
  14.  
  15.      nokops = ((mvitals[PM_KEYSTONE_KOP].mvflags & G_GONE)
  16.                && (mvitals[PM_KOP_SERGEANT].mvflags & G_GONE)
  17.                && (mvitals[PM_KOP_LIEUTENANT].mvflags & G_GONE)
  18.                && (mvitals[PM_KOP_KAPTAIN].mvflags & G_GONE));
  19.  
  20.      if (!angry_guards(!!Deaf) && nokops) {
  21.          if (flags.verbose && !Deaf)
  22.              pline("But no one seems to respond to it.");
  23.          return;
  24.      }
  25.  
  26.      if (nokops)
  27.          return;
  28.  
  29.      {
  30.          coord mm;
  31.  
  32.          if (nearshop) {
  33.              /* Create swarm around you, if you merely "stepped out" */
  34.              if (flags.verbose)
  35.                  pline_The("Keystone Kops appear!");
  36.              mm.x = u.ux;
  37.              mm.y = u.uy;
  38.              makekops(&mm);
  39.              return;
  40.          }
  41.          if (flags.verbose)
  42.              pline_The("Keystone Kops are after you!");
  43.          /* Create swarm near down staircase (hinders return to level) */
  44.          mm.x = xdnstair;
  45.          mm.y = ydnstair;
  46.          makekops(&mm);
  47.          /* Create swarm near shopkeeper (hinders return to shop) */
  48.          mm.x = shkp->mx;
  49.          mm.y = shkp->my;
  50.          makekops(&mm);
  51.      }
  52.  }
  53.  

inside_shop

  1.  /* x,y is strictly inside shop */
  2.  char
  3.  inside_shop(x, y)
  4.  register xchar x, y;
  5.  {
  6.      register char rno;
  7.  
  8.      rno = levl[x][y].roomno;
  9.      if ((rno < ROOMOFFSET) || levl[x][y].edge || !IS_SHOP(rno - ROOMOFFSET))
  10.          return NO_ROOM;
  11.      else
  12.          return rno;
  13.  }
  14.  

u_left_shop

  1.  void
  2.  u_left_shop(leavestring, newlev)
  3.  char *leavestring;
  4.  boolean newlev;
  5.  {
  6.      struct monst *shkp;
  7.      struct eshk *eshkp;
  8.  
  9.      /*
  10.       * IF player
  11.       * ((didn't leave outright) AND
  12.       *  ((he is now strictly-inside the shop) OR
  13.       *   (he wasn't strictly-inside last turn anyway)))
  14.       * THEN (there's nothing to do, so just return)
  15.       */
  16.      if (!*leavestring && (!levl[u.ux][u.uy].edge || levl[u.ux0][u.uy0].edge))
  17.          return;
  18.  
  19.      shkp = shop_keeper(*u.ushops0);
  20.      if (!shkp || !inhishop(shkp))
  21.          return; /* shk died, teleported, changed levels... */
  22.  
  23.      eshkp = ESHK(shkp);
  24.      if (!eshkp->billct && !eshkp->debit) /* bill is settled */
  25.          return;
  26.  
  27.      if (!*leavestring && !muteshk(shkp)) {
  28.          /*
  29.           * Player just stepped onto shop-boundary (known from above logic).
  30.           * Try to intimidate him into paying his bill
  31.           */
  32.          verbalize(NOTANGRY(shkp) ? "%s!  Please pay before leaving."
  33.                                   : "%s!  Don't you leave without paying!",
  34.                    plname);
  35.          return;
  36.      }
  37.  
  38.      if (rob_shop(shkp)) {
  39.          call_kops(shkp, (!newlev && levl[u.ux0][u.uy0].edge));
  40.      }
  41.  }
  42.  

remote_burglary

  1.  /* robbery from outside the shop via telekinesis or grappling hook */
  2.  void
  3.  remote_burglary(x, y)
  4.  xchar x, y;
  5.  {
  6.      struct monst *shkp;
  7.      struct eshk *eshkp;
  8.  
  9.      shkp = shop_keeper(*in_rooms(x, y, SHOPBASE));
  10.      if (!shkp || !inhishop(shkp))
  11.          return; /* shk died, teleported, changed levels... */
  12.  
  13.      eshkp = ESHK(shkp);
  14.      if (!eshkp->billct && !eshkp->debit) /* bill is settled */
  15.          return;
  16.  
  17.      if (rob_shop(shkp)) {
  18.          /*[might want to set 2nd arg based on distance from shop doorway]*/
  19.          call_kops(shkp, FALSE);
  20.      }
  21.  }
  22.  

rob_shop

  1.  /* shop merchandise has been taken; pay for it with any credit available;
  2.     return false if the debt is fully covered by credit, true otherwise */
  3.  STATIC_OVL boolean
  4.  rob_shop(shkp)
  5.  struct monst *shkp;
  6.  {
  7.      struct eshk *eshkp;
  8.      long total;
  9.  
  10.      eshkp = ESHK(shkp);
  11.      rouse_shk(shkp, TRUE);
  12.      total = (addupbill(shkp) + eshkp->debit);
  13.      if (eshkp->credit >= total) {
  14.          Your("credit of %ld %s is used to cover your shopping bill.",
  15.               eshkp->credit, currency(eshkp->credit));
  16.          total = 0L; /* credit gets cleared by setpaid() */
  17.      } else {
  18.          You("escaped the shop without paying!");
  19.          total -= eshkp->credit;
  20.      }
  21.      setpaid(shkp);
  22.      if (!total)
  23.          return FALSE;
  24.  
  25.      /* by this point, we know an actual robbery has taken place */
  26.      eshkp->robbed += total;
  27.      You("stole %ld %s worth of merchandise.", total, currency(total));
  28.      if (!Role_if(PM_ROGUE)) /* stealing is unlawful */
  29.          adjalign(-sgn(u.ualign.type));
  30.  
  31.      hot_pursuit(shkp);
  32.      return TRUE;
  33.  }
  34.  

deserted_shop

  1.  /* give a message when entering an untended shop (caller has verified that) */
  2.  STATIC_OVL void
  3.  deserted_shop(enterstring)
  4.  /*const*/ char *enterstring;
  5.  {
  6.      struct monst *mtmp;
  7.      struct mkroom *r = &rooms[(int) *enterstring - ROOMOFFSET];
  8.      int x, y, m = 0, n = 0;
  9.  
  10.      for (x = r->lx; x <= r->hx; ++x)
  11.          for (y = r->ly; y <= r->hy; ++y) {
  12.              if (x == u.ux && y == u.uy)
  13.                  continue;
  14.              if ((mtmp = m_at(x, y)) != 0) {
  15.                  ++n;
  16.                  if (sensemon(mtmp) || ((mtmp->m_ap_type == M_AP_NOTHING
  17.                                          || mtmp->m_ap_type == M_AP_MONSTER)
  18.                                         && canseemon(mtmp)))
  19.                      ++m;
  20.              }
  21.          }
  22.  
  23.      if (Blind && !(Blind_telepat || Detect_monsters))
  24.          ++n; /* force feedback to be less specific */
  25.  
  26.      pline("This shop %s %s.", (m < n) ? "seems to be" : "is",
  27.            !n ? "deserted" : "untended");
  28.  }
  29.  

u_entered_shop

  1.  void
  2.  u_entered_shop(enterstring)
  3.  char *enterstring;
  4.  {
  5.      register int rt;
  6.      register struct monst *shkp;
  7.      register struct eshk *eshkp;
  8.      static char empty_shops[5];
  9.  
  10.      if (!*enterstring)
  11.          return;
  12.  
  13.      if (!(shkp = shop_keeper(*enterstring))) {
  14.          if (!index(empty_shops, *enterstring)
  15.              && in_rooms(u.ux, u.uy, SHOPBASE)
  16.                     != in_rooms(u.ux0, u.uy0, SHOPBASE))
  17.              deserted_shop(enterstring);
  18.          Strcpy(empty_shops, u.ushops);
  19.          u.ushops[0] = '\0';
  20.          return;
  21.      }
  22.  
  23.      eshkp = ESHK(shkp);
  24.  
  25.      if (!inhishop(shkp)) {
  26.          /* dump core when referenced */
  27.          eshkp->bill_p = (struct bill_x *) -1000;
  28.          if (!index(empty_shops, *enterstring))
  29.              deserted_shop(enterstring);
  30.          Strcpy(empty_shops, u.ushops);
  31.          u.ushops[0] = '\0';
  32.          return;
  33.      }
  34.  
  35.      eshkp->bill_p = &(eshkp->bill[0]);
  36.  
  37.      if ((!eshkp->visitct || *eshkp->customer)
  38.          && strncmpi(eshkp->customer, plname, PL_NSIZ)) {
  39.          /* You seem to be new here */
  40.          eshkp->visitct = 0;
  41.          eshkp->following = 0;
  42.          (void) strncpy(eshkp->customer, plname, PL_NSIZ);
  43.          pacify_shk(shkp);
  44.      }
  45.  
  46.      if (muteshk(shkp) || eshkp->following)
  47.          return; /* no dialog */
  48.  
  49.      if (Invis) {
  50.          pline("%s senses your presence.", shkname(shkp));
  51.          verbalize("Invisible customers are not welcome!");
  52.          return;
  53.      }
  54.  
  55.      rt = rooms[*enterstring - ROOMOFFSET].rtype;
  56.  
  57.      if (ANGRY(shkp)) {
  58.          verbalize("So, %s, you dare return to %s %s?!", plname,
  59.                    s_suffix(shkname(shkp)), shtypes[rt - SHOPBASE].name);
  60.      } else if (eshkp->robbed) {
  61.          pline("%s mutters imprecations against shoplifters.", shkname(shkp));
  62.      } else {
  63.          verbalize("%s, %s!  Welcome%s to %s %s!", Hello(shkp), plname,
  64.                    eshkp->visitct++ ? " again" : "", s_suffix(shkname(shkp)),
  65.                    shtypes[rt - SHOPBASE].name);
  66.      }
  67.      /* can't do anything about blocking if teleported in */
  68.      if (!inside_shop(u.ux, u.uy)) {
  69.          boolean should_block;
  70.          int cnt;
  71.          const char *tool;
  72.          struct obj *pick = carrying(PICK_AXE),
  73.                     *mattock = carrying(DWARVISH_MATTOCK);
  74.  
  75.          if (pick || mattock) {
  76.              cnt = 1;               /* so far */
  77.              if (pick && mattock) { /* carrying both types */
  78.                  tool = "digging tool";
  79.                  cnt = 2; /* `more than 1' is all that matters */
  80.              } else if (pick) {
  81.                  tool = "pick-axe";
  82.                  /* hack: `pick' already points somewhere into inventory */
  83.                  while ((pick = pick->nobj) != 0)
  84.                      if (pick->otyp == PICK_AXE)
  85.                          ++cnt;
  86.              } else { /* assert(mattock != 0) */
  87.                  tool = "mattock";
  88.                  while ((mattock = mattock->nobj) != 0)
  89.                      if (mattock->otyp == DWARVISH_MATTOCK)
  90.                          ++cnt;
  91.                  /* [ALI] Shopkeeper identifies mattock(s) */
  92.                  if (!Blind)
  93.                      makeknown(DWARVISH_MATTOCK);
  94.              }
  95.              verbalize(NOTANGRY(shkp)
  96.                            ? "Will you please leave your %s%s outside?"
  97.                            : "Leave the %s%s outside.",
  98.                        tool, plur(cnt));
  99.              should_block = TRUE;
  100.          } else if (u.usteed) {
  101.              verbalize(NOTANGRY(shkp) ? "Will you please leave %s outside?"
  102.                                       : "Leave %s outside.",
  103.                        y_monnam(u.usteed));
  104.              should_block = TRUE;
  105.          } else {
  106.              should_block =
  107.                  (Fast && (sobj_at(PICK_AXE, u.ux, u.uy)
  108.                            || sobj_at(DWARVISH_MATTOCK, u.ux, u.uy)));
  109.          }
  110.          if (should_block)
  111.              (void) dochug(shkp); /* shk gets extra move */
  112.      }
  113.      return;
  114.  }
  115.  

pick_pick

  1.  /* called when removing a pick-axe or mattock from a container */
  2.  void
  3.  pick_pick(obj)
  4.  struct obj *obj;
  5.  {
  6.      struct monst *shkp;
  7.  
  8.      if (obj->unpaid || !is_pick(obj))
  9.          return;
  10.      shkp = shop_keeper(*u.ushops);
  11.      if (shkp && inhishop(shkp) && !muteshk(shkp)) {
  12.          static NEARDATA long pickmovetime = 0L;
  13.  
  14.          /* if you bring a sack of N picks into a shop to sell,
  15.             don't repeat this N times when they're taken out */
  16.          if (moves != pickmovetime)
  17.              verbalize("You sneaky %s!  Get out of here with that pick!",
  18.                        cad(FALSE));
  19.          pickmovetime = moves;
  20.      }
  21.  }
  22.  

same_price

  1.  /*
  2.     Decide whether two unpaid items are mergable; caller is responsible for
  3.     making sure they're unpaid and the same type of object; we check the price
  4.     quoted by the shopkeeper and also that they both belong to the same shk.
  5.   */
  6.  boolean
  7.  same_price(obj1, obj2)
  8.  struct obj *obj1, *obj2;
  9.  {
  10.      register struct monst *shkp1, *shkp2;
  11.      struct bill_x *bp1 = 0, *bp2 = 0;
  12.      boolean are_mergable = FALSE;
  13.  
  14.      /* look up the first object by finding shk whose bill it's on */
  15.      for (shkp1 = next_shkp(fmon, TRUE); shkp1;
  16.           shkp1 = next_shkp(shkp1->nmon, TRUE))
  17.          if ((bp1 = onbill(obj1, shkp1, TRUE)) != 0)
  18.              break;
  19.      /* second object is probably owned by same shk; if not, look harder */
  20.      if (shkp1 && (bp2 = onbill(obj2, shkp1, TRUE)) != 0) {
  21.          shkp2 = shkp1;
  22.      } else {
  23.          for (shkp2 = next_shkp(fmon, TRUE); shkp2;
  24.               shkp2 = next_shkp(shkp2->nmon, TRUE))
  25.              if ((bp2 = onbill(obj2, shkp2, TRUE)) != 0)
  26.                  break;
  27.      }
  28.  
  29.      if (!bp1 || !bp2)
  30.          impossible("same_price: object wasn't on any bill!");
  31.      else
  32.          are_mergable = (shkp1 == shkp2 && bp1->price == bp2->price);
  33.      return are_mergable;
  34.  }
  35.  

shop_debt

  1.  /*
  2.   * Figure out how much is owed to a given shopkeeper.
  3.   * At present, we ignore any amount robbed from the shop, to avoid
  4.   * turning the `$' command into a way to discover that the current
  5.   * level is bones data which has a shk on the warpath.
  6.   */
  7.  STATIC_OVL long
  8.  shop_debt(eshkp)
  9.  struct eshk *eshkp;
  10.  {
  11.      struct bill_x *bp;
  12.      int ct;
  13.      long debt = eshkp->debit;
  14.  
  15.      for (bp = eshkp->bill_p, ct = eshkp->billct; ct > 0; bp++, ct--)
  16.          debt += bp->price * bp->bquan;
  17.      return debt;
  18.  }
  19.  

shopper_financial_report

  1.  /* called in response to the `$' command */
  2.  void
  3.  shopper_financial_report()
  4.  {
  5.      struct monst *shkp, *this_shkp = shop_keeper(inside_shop(u.ux, u.uy));
  6.      struct eshk *eshkp;
  7.      long amt;
  8.      int pass;
  9.  
  10.      eshkp = this_shkp ? ESHK(this_shkp) : 0;
  11.      if (eshkp && !(eshkp->credit || shop_debt(eshkp))) {
  12.          You("have no credit or debt in here.");
  13.          this_shkp = 0; /* skip first pass */
  14.      }
  15.  
  16.      /* pass 0: report for the shop we're currently in, if any;
  17.         pass 1: report for all other shops on this level. */
  18.      for (pass = this_shkp ? 0 : 1; pass <= 1; pass++)
  19.          for (shkp = next_shkp(fmon, FALSE); shkp;
  20.               shkp = next_shkp(shkp->nmon, FALSE)) {
  21.              if ((shkp != this_shkp) ^ pass)
  22.                  continue;
  23.              eshkp = ESHK(shkp);
  24.              if ((amt = eshkp->credit) != 0)
  25.                  You("have %ld %s credit at %s %s.", amt, currency(amt),
  26.                      s_suffix(shkname(shkp)),
  27.                      shtypes[eshkp->shoptype - SHOPBASE].name);
  28.              else if (shkp == this_shkp)
  29.                  You("have no credit in here.");
  30.              if ((amt = shop_debt(eshkp)) != 0)
  31.                  You("owe %s %ld %s.", shkname(shkp), amt, currency(amt));
  32.              else if (shkp == this_shkp)
  33.                  You("don't owe any money here.");
  34.          }
  35.  }
  36.  

inhishop

  1.  int
  2.  inhishop(mtmp)
  3.  register struct monst *mtmp;
  4.  {
  5.      struct eshk *eshkp = ESHK(mtmp);
  6.  
  7.      return (index(in_rooms(mtmp->mx, mtmp->my, SHOPBASE), eshkp->shoproom)
  8.              && on_level(&eshkp->shoplevel, &u.uz));
  9.  }
  10.  

shop_keeper

  1.  struct monst *
  2.  shop_keeper(rmno)
  3.  register char rmno;
  4.  {
  5.      struct monst *shkp =
  6.          rmno >= ROOMOFFSET ? rooms[rmno - ROOMOFFSET].resident : 0;
  7.  
  8.      if (shkp) {
  9.          if (NOTANGRY(shkp)) {
  10.              if (ESHK(shkp)->surcharge)
  11.                  pacify_shk(shkp);
  12.          } else {
  13.              if (!ESHK(shkp)->surcharge)
  14.                  rile_shk(shkp);
  15.          }
  16.      }
  17.      return shkp;
  18.  }
  19.  

tended_shop

  1.  boolean
  2.  tended_shop(sroom)
  3.  register struct mkroom *sroom;
  4.  {
  5.      register struct monst *mtmp = sroom->resident;
  6.  
  7.      if (!mtmp)
  8.          return FALSE;
  9.      else
  10.          return (boolean) inhishop(mtmp);
  11.  }
  12.  

onbill

  1.  STATIC_OVL struct bill_x *
  2.  onbill(obj, shkp, silent)
  3.  register struct obj *obj;
  4.  register struct monst *shkp;
  5.  register boolean silent;
  6.  {
  7.      if (shkp) {
  8.          register struct bill_x *bp = ESHK(shkp)->bill_p;
  9.          register int ct = ESHK(shkp)->billct;
  10.  
  11.          while (--ct >= 0)
  12.              if (bp->bo_id == obj->o_id) {
  13.                  if (!obj->unpaid)
  14.                      pline("onbill: paid obj on bill?");
  15.                  return bp;
  16.              } else
  17.                  bp++;
  18.      }
  19.      if (obj->unpaid & !silent)
  20.          pline("onbill: unpaid obj not on bill?");
  21.      return (struct bill_x *) 0;
  22.  }
  23.  

is_unpaid

  1.  /* check whether an object or any of its contents belongs to a shop */
  2.  boolean
  3.  is_unpaid(obj)
  4.  struct obj *obj;
  5.  {
  6.      return (boolean) (obj->unpaid
  7.                        || (Has_contents(obj) && count_unpaid(obj->cobj)));
  8.  }
  9.  

delete_contents

  1.  /* Delete the contents of the given object. */
  2.  void
  3.  delete_contents(obj)
  4.  register struct obj *obj;
  5.  {
  6.      register struct obj *curr;
  7.  
  8.      while ((curr = obj->cobj) != 0) {
  9.          obj_extract_self(curr);
  10.          obfree(curr, (struct obj *) 0);
  11.      }
  12.  }
  13.  

obfree

  1.  /* called with two args on merge */
  2.  void
  3.  obfree(obj, merge)
  4.  register struct obj *obj, *merge;
  5.  {
  6.      register struct bill_x *bp;
  7.      register struct bill_x *bpm;
  8.      register struct monst *shkp;
  9.  
  10.      if (obj->otyp == LEASH && obj->leashmon)
  11.          o_unleash(obj);
  12.      if (obj->oclass == FOOD_CLASS)
  13.          food_disappears(obj);
  14.      if (obj->oclass == SPBOOK_CLASS)
  15.          book_disappears(obj);
  16.      if (Has_contents(obj))
  17.          delete_contents(obj);
  18.  
  19.      shkp = 0;
  20.      if (obj->unpaid) {
  21.          /* look for a shopkeeper who owns this object */
  22.          for (shkp = next_shkp(fmon, TRUE); shkp;
  23.               shkp = next_shkp(shkp->nmon, TRUE))
  24.              if (onbill(obj, shkp, TRUE))
  25.                  break;
  26.      }
  27.      /* sanity check, more or less */
  28.      if (!shkp)
  29.          shkp = shop_keeper(*u.ushops);
  30.      /*
  31.       * Note:  `shkp = shop_keeper(*u.ushops)' used to be
  32.       *    unconditional.  But obfree() is used all over
  33.       *    the place, so making its behavior be dependent
  34.       *    upon player location doesn't make much sense.
  35.       */
  36.  
  37.      if ((bp = onbill(obj, shkp, FALSE)) != 0) {
  38.          if (!merge) {
  39.              bp->useup = 1;
  40.              obj->unpaid = 0; /* only for doinvbill */
  41.              add_to_billobjs(obj);
  42.              return;
  43.          }
  44.          bpm = onbill(merge, shkp, FALSE);
  45.          if (!bpm) {
  46.              /* this used to be a rename */
  47.              impossible("obfree: not on bill??");
  48.              return;
  49.          } else {
  50.              /* this was a merger */
  51.              bpm->bquan += bp->bquan;
  52.              ESHK(shkp)->billct--;
  53.  #ifdef DUMB
  54.              {
  55.                  /* DRS/NS 2.2.6 messes up -- Peter Kendell */
  56.                  int indx = ESHK(shkp)->billct;
  57.  
  58.                  *bp = ESHK(shkp)->bill_p[indx];
  59.              }
  60.  #else
  61.              *bp = ESHK(shkp)->bill_p[ESHK(shkp)->billct];
  62.  #endif
  63.          }
  64.      }
  65.      if (obj->owornmask) {
  66.          impossible("obfree: deleting worn obj (%d: %ld)", obj->otyp,
  67.                     obj->owornmask);
  68.          /* unfortunately at this point we don't know whether worn mask
  69.             applied to hero or a monster or perhaps something bogus, so
  70.             can't call remove_worn_item() to get <X>_off() side-effects */
  71.          setnotworn(obj);
  72.      }
  73.      dealloc_obj(obj);
  74.  }
  75.  

check_credit

  1.  STATIC_OVL long
  2.  check_credit(tmp, shkp)
  3.  long tmp;
  4.  register struct monst *shkp;
  5.  {
  6.      long credit = ESHK(shkp)->credit;
  7.  
  8.      if (credit == 0L) {
  9.          ; /* nothing to do; just 'return tmp;' */
  10.      } else if (credit >= tmp) {
  11.          pline_The("price is deducted from your credit.");
  12.          ESHK(shkp)->credit -= tmp;
  13.          tmp = 0L;
  14.      } else {
  15.          pline_The("price is partially covered by your credit.");
  16.          ESHK(shkp)->credit = 0L;
  17.          tmp -= credit;
  18.      }
  19.      return tmp;
  20.  }
  21.  

pay

  1.  STATIC_OVL void
  2.  pay(tmp, shkp)
  3.  long tmp;
  4.  register struct monst *shkp;
  5.  {
  6.      long robbed = ESHK(shkp)->robbed;
  7.      long balance = ((tmp <= 0L) ? tmp : check_credit(tmp, shkp));
  8.  
  9.      if (balance > 0)
  10.          money2mon(shkp, balance);
  11.      else if (balance < 0)
  12.          money2u(shkp, -balance);
  13.      context.botl = 1;
  14.      if (robbed) {
  15.          robbed -= tmp;
  16.          if (robbed < 0)
  17.              robbed = 0L;
  18.          ESHK(shkp)->robbed = robbed;
  19.      }
  20.  }
  21.  

home_shk

  1.  /* return shkp to home position */
  2.  void
  3.  home_shk(shkp, killkops)
  4.  register struct monst *shkp;
  5.  register boolean killkops;
  6.  {
  7.      register xchar x = ESHK(shkp)->shk.x, y = ESHK(shkp)->shk.y;
  8.  
  9.      (void) mnearto(shkp, x, y, TRUE);
  10.      level.flags.has_shop = 1;
  11.      if (killkops) {
  12.          kops_gone(TRUE);
  13.          pacify_guards();
  14.      }
  15.      after_shk_move(shkp);
  16.  }
  17.  

angry_shk_exists

  1.  STATIC_OVL boolean
  2.  angry_shk_exists()
  3.  {
  4.      register struct monst *shkp;
  5.  
  6.      for (shkp = next_shkp(fmon, FALSE); shkp;
  7.           shkp = next_shkp(shkp->nmon, FALSE))
  8.          if (ANGRY(shkp))
  9.              return TRUE;
  10.      return FALSE;
  11.  }
  12.  

pacify_shk

  1.  /* remove previously applied surcharge from all billed items */
  2.  STATIC_OVL void
  3.  pacify_shk(shkp)
  4.  register struct monst *shkp;
  5.  {
  6.      NOTANGRY(shkp) = TRUE; /* make peaceful */
  7.      if (ESHK(shkp)->surcharge) {
  8.          register struct bill_x *bp = ESHK(shkp)->bill_p;
  9.          register int ct = ESHK(shkp)->billct;
  10.  
  11.          ESHK(shkp)->surcharge = FALSE;
  12.          while (ct-- > 0) {
  13.              register long reduction = (bp->price + 3L) / 4L;
  14.              bp->price -= reduction; /* undo 33% increase */
  15.              bp++;
  16.          }
  17.      }
  18.  }
  19.  

rile_shk

  1.  /* add aggravation surcharge to all billed items */
  2.  STATIC_OVL void
  3.  rile_shk(shkp)
  4.  register struct monst *shkp;
  5.  {
  6.      NOTANGRY(shkp) = FALSE; /* make angry */
  7.      if (!ESHK(shkp)->surcharge) {
  8.          register struct bill_x *bp = ESHK(shkp)->bill_p;
  9.          register int ct = ESHK(shkp)->billct;
  10.  
  11.          ESHK(shkp)->surcharge = TRUE;
  12.          while (ct-- > 0) {
  13.              register long surcharge = (bp->price + 2L) / 3L;
  14.              bp->price += surcharge;
  15.              bp++;
  16.          }
  17.      }
  18.  }
  19.  

rouse_shk

  1.  /* wakeup and/or unparalyze shopkeeper */
  2.  STATIC_OVL void
  3.  rouse_shk(shkp, verbosely)
  4.  struct monst *shkp;
  5.  boolean verbosely;
  6.  {
  7.      if (!shkp->mcanmove || shkp->msleeping) {
  8.          /* greed induced recovery... */
  9.          if (verbosely && canspotmon(shkp))
  10.              pline("%s %s.", Monnam(shkp),
  11.                    shkp->msleeping ? "wakes up" : "can move again");
  12.          shkp->msleeping = 0;
  13.          shkp->mfrozen = 0;
  14.          shkp->mcanmove = 1;
  15.      }
  16.  }
  17.  

make_happy_shk

  1.  void
  2.  make_happy_shk(shkp, silentkops)
  3.  register struct monst *shkp;
  4.  register boolean silentkops;
  5.  {
  6.      boolean wasmad = ANGRY(shkp);
  7.      struct eshk *eshkp = ESHK(shkp);
  8.  
  9.      pacify_shk(shkp);
  10.      eshkp->following = 0;
  11.      eshkp->robbed = 0L;
  12.      if (!Role_if(PM_ROGUE))
  13.          adjalign(sgn(u.ualign.type));
  14.      if (!inhishop(shkp)) {
  15.          char shk_nam[BUFSZ];
  16.          boolean vanished = canseemon(shkp);
  17.  
  18.          Strcpy(shk_nam, mon_nam(shkp));
  19.          if (on_level(&eshkp->shoplevel, &u.uz)) {
  20.              home_shk(shkp, FALSE);
  21.              /* didn't disappear if shk can still be seen */
  22.              if (canseemon(shkp))
  23.                  vanished = FALSE;
  24.          } else {
  25.              /* if sensed, does disappear regardless whether seen */
  26.              if (sensemon(shkp))
  27.                  vanished = TRUE;
  28.              /* can't act as porter for the Amulet, even if shk
  29.                 happens to be going farther down rather than up */
  30.              mdrop_special_objs(shkp);
  31.              /* arrive near shop's door */
  32.              migrate_to_level(shkp, ledger_no(&eshkp->shoplevel),
  33.                               MIGR_APPROX_XY, &eshkp->shd);
  34.              /* dismiss kops on that level when shk arrives */
  35.              eshkp->dismiss_kops = TRUE;
  36.          }
  37.          if (vanished)
  38.              pline("Satisfied, %s suddenly disappears!", shk_nam);
  39.      } else if (wasmad)
  40.          pline("%s calms down.", Monnam(shkp));
  41.  
  42.      make_happy_shoppers(silentkops);
  43.  }
  44.  

make_happy_shoppers

  1.  /* called by make_happy_shk() and also by losedogs() for migrating shk */
  2.  void
  3.  make_happy_shoppers(silentkops)
  4.  boolean silentkops;
  5.  {
  6.      if (!angry_shk_exists()) {
  7.          kops_gone(silentkops);
  8.          pacify_guards();
  9.      }
  10.  }
  11.  

hot_pursuit

  1.  void
  2.  hot_pursuit(shkp)
  3.  register struct monst *shkp;
  4.  {
  5.      if (!shkp->isshk)
  6.          return;
  7.  
  8.      rile_shk(shkp);
  9.      (void) strncpy(ESHK(shkp)->customer, plname, PL_NSIZ);
  10.      ESHK(shkp)->following = 1;
  11.  }
  12.  

make_angry_shk

  1.  /* used when the shkp is teleported or falls (ox == 0) out of his shop,
  2.   * or when the player is not on a costly_spot and he
  3.   * damages something inside the shop.  these conditions
  4.   * must be checked by the calling function.
  5.   */
  6.  void
  7.  make_angry_shk(shkp, ox, oy)
  8.  register struct monst *shkp;
  9.  register xchar ox, oy;
  10.  {
  11.      xchar sx, sy;
  12.      struct eshk *eshkp = ESHK(shkp);
  13.  
  14.      /* all pending shop transactions are now "past due" */
  15.      if (eshkp->billct || eshkp->debit || eshkp->loan || eshkp->credit) {
  16.          eshkp->robbed += (addupbill(shkp) + eshkp->debit + eshkp->loan);
  17.          eshkp->robbed -= eshkp->credit;
  18.          if (eshkp->robbed < 0L)
  19.              eshkp->robbed = 0L;
  20.          /* billct, debit, loan, and credit will be cleared by setpaid */
  21.          setpaid(shkp);
  22.      }
  23.  
  24.      /* If you just used a wand of teleportation to send the shk away, you
  25.         might not be able to see her any more.  Monnam would yield "it",
  26.         which makes this message look pretty silly, so temporarily restore
  27.         her original location during the call to Monnam. */
  28.      sx = shkp->mx, sy = shkp->my;
  29.      if (isok(ox, oy) && cansee(ox, oy) && !cansee(sx, sy))
  30.          shkp->mx = ox, shkp->my = oy;
  31.      pline("%s %s!", Monnam(shkp), !ANGRY(shkp) ? "gets angry" : "is furious");
  32.      shkp->mx = sx, shkp->my = sy;
  33.      hot_pursuit(shkp);
  34.  }
  35.  

cheapest_item

  1.  STATIC_VAR const char no_money[] = "Moreover, you%s have no money.";
  2.  STATIC_VAR const char not_enough_money[] =
  3.      "Besides, you don't have enough to interest %s.";
  4.  
  5.  STATIC_OVL long
  6.  cheapest_item(shkp) /* delivers the cheapest item on the list */
  7.  register struct monst *shkp;
  8.  {
  9.      register int ct = ESHK(shkp)->billct;
  10.      register struct bill_x *bp = ESHK(shkp)->bill_p;
  11.      register long gmin = (bp->price * bp->bquan);
  12.  
  13.      while (ct--) {
  14.          if (bp->price * bp->bquan < gmin)
  15.              gmin = bp->price * bp->bquan;
  16.          bp++;
  17.      }
  18.      return gmin;
  19.  }
  20.  

dopay

  1.  int
  2.  dopay()
  3.  {
  4.      register struct eshk *eshkp;
  5.      register struct monst *shkp;
  6.      struct monst *nxtm, *resident;
  7.      long ltmp;
  8.      long umoney;
  9.      int pass, tmp, sk = 0, seensk = 0;
  10.      boolean paid = FALSE, stashed_gold = (hidden_gold() > 0L);
  11.  
  12.      multi = 0;
  13.  
  14.      /* Find how many shk's there are, how many are in
  15.       * sight, and are you in a shop room with one.
  16.       */
  17.      nxtm = resident = 0;
  18.      for (shkp = next_shkp(fmon, FALSE); shkp;
  19.           shkp = next_shkp(shkp->nmon, FALSE)) {
  20.          sk++;
  21.          if (ANGRY(shkp) && distu(shkp->mx, shkp->my) <= 2)
  22.              nxtm = shkp;
  23.          if (canspotmon(shkp))
  24.              seensk++;
  25.          if (inhishop(shkp) && (*u.ushops == ESHK(shkp)->shoproom))
  26.              resident = shkp;
  27.      }
  28.  
  29.      if (nxtm) {      /* Player should always appease an */
  30.          shkp = nxtm; /* irate shk standing next to them. */
  31.          goto proceed;
  32.      }
  33.  
  34.      if ((!sk && (!Blind || Blind_telepat)) || (!Blind && !seensk)) {
  35.          There("appears to be no shopkeeper here to receive your payment.");
  36.          return 0;
  37.      }
  38.  
  39.      if (!seensk) {
  40.          You_cant("see...");
  41.          return 0;
  42.      }
  43.  
  44.      /* The usual case.  Allow paying at a distance when
  45.       * inside a tended shop.  Should we change that?
  46.       */
  47.      if (sk == 1 && resident) {
  48.          shkp = resident;
  49.          goto proceed;
  50.      }
  51.  
  52.      if (seensk == 1) {
  53.          for (shkp = next_shkp(fmon, FALSE); shkp;
  54.               shkp = next_shkp(shkp->nmon, FALSE))
  55.              if (canspotmon(shkp))
  56.                  break;
  57.          if (shkp != resident && distu(shkp->mx, shkp->my) > 2) {
  58.              pline("%s is not near enough to receive your payment.",
  59.                    Monnam(shkp));
  60.              return 0;
  61.          }
  62.      } else {
  63.          struct monst *mtmp;
  64.          coord cc;
  65.          int cx, cy;
  66.  
  67.          pline("Pay whom?");
  68.          cc.x = u.ux;
  69.          cc.y = u.uy;
  70.          if (getpos(&cc, TRUE, "the creature you want to pay") < 0)
  71.              return 0; /* player pressed ESC */
  72.          cx = cc.x;
  73.          cy = cc.y;
  74.          if (cx < 0) {
  75.              pline("Try again...");
  76.              return 0;
  77.          }
  78.          if (u.ux == cx && u.uy == cy) {
  79.              You("are generous to yourself.");
  80.              return 0;
  81.          }
  82.          mtmp = m_at(cx, cy);
  83.          if (!mtmp) {
  84.              There("is no one there to receive your payment.");
  85.              return 0;
  86.          }
  87.          if (!mtmp->isshk) {
  88.              pline("%s is not interested in your payment.", Monnam(mtmp));
  89.              return 0;
  90.          }
  91.          if (mtmp != resident && distu(mtmp->mx, mtmp->my) > 2) {
  92.              pline("%s is too far to receive your payment.", Monnam(mtmp));
  93.              return 0;
  94.          }
  95.          shkp = mtmp;
  96.      }
  97.  
  98.      if (!shkp) {
  99.          debugpline0("dopay: null shkp.");
  100.          return 0;
  101.      }
  102.  proceed:
  103.      eshkp = ESHK(shkp);
  104.      ltmp = eshkp->robbed;
  105.  
  106.      /* wake sleeping shk when someone who owes money offers payment */
  107.      if (ltmp || eshkp->billct || eshkp->debit)
  108.          rouse_shk(shkp, TRUE);
  109.  
  110.      if (!shkp->mcanmove || shkp->msleeping) { /* still asleep/paralyzed */
  111.          pline("%s %s.", Monnam(shkp),
  112.                rn2(2) ? "seems to be napping" : "doesn't respond");
  113.          return 0;
  114.      }
  115.  
  116.      if (shkp != resident && NOTANGRY(shkp)) {
  117.          umoney = money_cnt(invent);
  118.          if (!ltmp)
  119.              You("do not owe %s anything.", mon_nam(shkp));
  120.          else if (!umoney) {
  121.              You("%shave no money.", stashed_gold ? "seem to " : "");
  122.              if (stashed_gold)
  123.                  pline("But you have some gold stashed away.");
  124.          } else {
  125.              if (umoney > ltmp) {
  126.                  You("give %s the %ld gold piece%s %s asked for.",
  127.                      shkname(shkp), ltmp, plur(ltmp), mhe(shkp));
  128.                  pay(ltmp, shkp);
  129.              } else {
  130.                  You("give %s all your%s gold.", shkname(shkp),
  131.                      stashed_gold ? " openly kept" : "");
  132.                  pay(umoney, shkp);
  133.                  if (stashed_gold)
  134.                      pline("But you have hidden gold!");
  135.              }
  136.              if ((umoney < ltmp / 2L) || (umoney < ltmp && stashed_gold))
  137.                  pline("Unfortunately, %s doesn't look satisfied.", mhe(shkp));
  138.              else
  139.                  make_happy_shk(shkp, FALSE);
  140.          }
  141.          return 1;
  142.      }
  143.  
  144.      /* ltmp is still eshkp->robbed here */
  145.      if (!eshkp->billct && !eshkp->debit) {
  146.          umoney = money_cnt(invent);
  147.          if (!ltmp && NOTANGRY(shkp)) {
  148.              You("do not owe %s anything.", shkname(shkp));
  149.              if (!umoney)
  150.                  pline(no_money, stashed_gold ? " seem to" : "");
  151.          } else if (ltmp) {
  152.              pline("%s is after blood, not money!", shkname(shkp));
  153.              if (umoney < ltmp / 2L || (umoney < ltmp && stashed_gold)) {
  154.                  if (!umoney)
  155.                      pline(no_money, stashed_gold ? " seem to" : "");
  156.                  else
  157.                      pline(not_enough_money, mhim(shkp));
  158.                  return 1;
  159.              }
  160.              pline("But since %s shop has been robbed recently,", mhis(shkp));
  161.              pline("you %scompensate %s for %s losses.",
  162.                    (umoney < ltmp) ? "partially " : "", shkname(shkp),
  163.                    mhis(shkp));
  164.              pay(umoney < ltmp ? umoney : ltmp, shkp);
  165.              make_happy_shk(shkp, FALSE);
  166.          } else {
  167.              /* shopkeeper is angry, but has not been robbed --
  168.               * door broken, attacked, etc. */
  169.              pline("%s is after your hide, not your money!", Monnam(shkp));
  170.              if (umoney < 1000L) {
  171.                  if (!umoney)
  172.                      pline(no_money, stashed_gold ? " seem to" : "");
  173.                  else
  174.                      pline(not_enough_money, mhim(shkp));
  175.                  return 1;
  176.              }
  177.              You("try to appease %s by giving %s 1000 gold pieces.",
  178.                  x_monnam(shkp, ARTICLE_THE, "angry", 0, FALSE), mhim(shkp));
  179.              pay(1000L, shkp);
  180.              if (strncmp(eshkp->customer, plname, PL_NSIZ) || rn2(3))
  181.                  make_happy_shk(shkp, FALSE);
  182.              else
  183.                  pline("But %s is as angry as ever.", shkname(shkp));
  184.          }
  185.          return 1;
  186.      }
  187.      if (shkp != resident) {
  188.          impossible("dopay: not to shopkeeper?");
  189.          if (resident)
  190.              setpaid(resident);
  191.          return 0;
  192.      }
  193.      /* pay debt, if any, first */
  194.      if (eshkp->debit) {
  195.          long dtmp = eshkp->debit;
  196.          long loan = eshkp->loan;
  197.          char sbuf[BUFSZ];
  198.          umoney = money_cnt(invent);
  199.          Sprintf(sbuf, "You owe %s %ld %s ", shkname(shkp), dtmp,
  200.                  currency(dtmp));
  201.          if (loan) {
  202.              if (loan == dtmp)
  203.                  Strcat(sbuf, "you picked up in the store.");
  204.              else
  205.                  Strcat(sbuf,
  206.                         "for gold picked up and the use of merchandise.");
  207.          } else
  208.              Strcat(sbuf, "for the use of merchandise.");
  209.          pline1(sbuf);
  210.          if (umoney + eshkp->credit < dtmp) {
  211.              pline("But you don't%s have enough gold%s.",
  212.                    stashed_gold ? " seem to" : "",
  213.                    eshkp->credit ? " or credit" : "");
  214.              return 1;
  215.          } else {
  216.              if (eshkp->credit >= dtmp) {
  217.                  eshkp->credit -= dtmp;
  218.                  eshkp->debit = 0L;
  219.                  eshkp->loan = 0L;
  220.                  Your("debt is covered by your credit.");
  221.              } else if (!eshkp->credit) {
  222.                  money2mon(shkp, dtmp);
  223.                  eshkp->debit = 0L;
  224.                  eshkp->loan = 0L;
  225.                  You("pay that debt.");
  226.                  context.botl = 1;
  227.              } else {
  228.                  dtmp -= eshkp->credit;
  229.                  eshkp->credit = 0L;
  230.                  money2mon(shkp, dtmp);
  231.                  eshkp->debit = 0L;
  232.                  eshkp->loan = 0L;
  233.                  pline("That debt is partially offset by your credit.");
  234.                  You("pay the remainder.");
  235.                  context.botl = 1;
  236.              }
  237.              paid = TRUE;
  238.          }
  239.      }
  240.      /* now check items on bill */
  241.      if (eshkp->billct) {
  242.          register boolean itemize;
  243.          int iprompt;
  244.          umoney = money_cnt(invent);
  245.          if (!umoney && !eshkp->credit) {
  246.              You("%shave no money or credit%s.",
  247.                  stashed_gold ? "seem to " : "", paid ? " left" : "");
  248.              return 0;
  249.          }
  250.          if ((umoney + eshkp->credit) < cheapest_item(shkp)) {
  251.              You("don't have enough money to buy%s the item%s you picked.",
  252.                  eshkp->billct > 1 ? " any of" : "", plur(eshkp->billct));
  253.              if (stashed_gold)
  254.                  pline("Maybe you have some gold stashed away?");
  255.              return 0;
  256.          }
  257.  
  258.          /* this isn't quite right; it itemizes without asking if the
  259.           * single item on the bill is partly used up and partly unpaid */
  260.          iprompt = (eshkp->billct > 1 ? ynq("Itemized billing?") : 'y');
  261.          itemize = (iprompt == 'y');
  262.          if (iprompt == 'q')
  263.              goto thanks;
  264.  
  265.          for (pass = 0; pass <= 1; pass++) {
  266.              tmp = 0;
  267.              while (tmp < eshkp->billct) {
  268.                  struct obj *otmp;
  269.                  register struct bill_x *bp = &(eshkp->bill_p[tmp]);
  270.  
  271.                  /* find the object on one of the lists */
  272.                  if ((otmp = bp_to_obj(bp)) != 0) {
  273.                      /* if completely used up, object quantity is stale;
  274.                         restoring it to its original value here avoids
  275.                         making the partly-used-up code more complicated */
  276.                      if (bp->useup)
  277.                          otmp->quan = bp->bquan;
  278.                  } else {
  279.                      impossible("Shopkeeper administration out of order.");
  280.                      setpaid(shkp); /* be nice to the player */
  281.                      return 1;
  282.                  }
  283.                  if (pass == bp->useup && otmp->quan == bp->bquan) {
  284.                      /* pay for used-up items on first pass and others
  285.                       * on second, so player will be stuck in the store
  286.                       * less often; things which are partly used up
  287.                       * are processed on both passes */
  288.                      tmp++;
  289.                  } else {
  290.                      switch (dopayobj(shkp, bp, &otmp, pass, itemize)) {
  291.                      case PAY_CANT:
  292.                          return 1; /*break*/
  293.                      case PAY_BROKE:
  294.                          paid = TRUE;
  295.                          goto thanks; /*break*/
  296.                      case PAY_SKIP:
  297.                          tmp++;
  298.                          continue; /*break*/
  299.                      case PAY_SOME:
  300.                          paid = TRUE;
  301.                          if (itemize)
  302.                              bot();
  303.                          continue; /*break*/
  304.                      case PAY_BUY:
  305.                          paid = TRUE;
  306.                          break;
  307.                      }
  308.                      if (itemize)
  309.                          bot();
  310.                      *bp = eshkp->bill_p[--eshkp->billct];
  311.                  }
  312.              }
  313.          }
  314.      thanks:
  315.          if (!itemize)
  316.              update_inventory(); /* Done in dopayobj() if itemize. */
  317.      }
  318.      if (!ANGRY(shkp) && paid && !muteshk(shkp))
  319.          verbalize("Thank you for shopping in %s %s!", s_suffix(shkname(shkp)),
  320.                    shtypes[eshkp->shoptype - SHOPBASE].name);
  321.      return 1;
  322.  }
  323.  

dopayobj

  1.  /* return 2 if used-up portion paid
  2.   *        1 if paid successfully
  3.   *        0 if not enough money
  4.   *       -1 if skip this object
  5.   *       -2 if no money/credit left
  6.   */
  7.  STATIC_OVL int
  8.  dopayobj(shkp, bp, obj_p, which, itemize)
  9.  register struct monst *shkp;
  10.  register struct bill_x *bp;
  11.  struct obj **obj_p;
  12.  int which; /* 0 => used-up item, 1 => other (unpaid or lost) */
  13.  boolean itemize;
  14.  {
  15.      register struct obj *obj = *obj_p;
  16.      long ltmp, quan, save_quan;
  17.      long umoney = money_cnt(invent);
  18.      int buy;
  19.      boolean stashed_gold = (hidden_gold() > 0L), consumed = (which == 0);
  20.  
  21.      if (!obj->unpaid && !bp->useup) {
  22.          impossible("Paid object on bill??");
  23.          return PAY_BUY;
  24.      }
  25.      if (itemize && umoney + ESHK(shkp)->credit == 0L) {
  26.          You("%shave no money or credit left.",
  27.              stashed_gold ? "seem to " : "");
  28.          return PAY_BROKE;
  29.      }
  30.      /* we may need to temporarily adjust the object, if part of the
  31.         original quantity has been used up but part remains unpaid  */
  32.      save_quan = obj->quan;
  33.      if (consumed) {
  34.          /* either completely used up (simple), or split needed */
  35.          quan = bp->bquan;
  36.          if (quan > obj->quan) /* difference is amount used up */
  37.              quan -= obj->quan;
  38.      } else {
  39.          /* dealing with ordinary unpaid item */
  40.          quan = obj->quan;
  41.      }
  42.      obj->quan = quan;        /* to be used by doname() */
  43.      obj->unpaid = 0;         /* ditto */
  44.      iflags.suppress_price++; /* affects containers */
  45.      ltmp = bp->price * quan;
  46.      buy = PAY_BUY; /* flag; if changed then return early */
  47.  
  48.      if (itemize) {
  49.          char qbuf[BUFSZ], qsfx[BUFSZ];
  50.  
  51.          Sprintf(qsfx, " for %ld %s.  Pay?", ltmp, currency(ltmp));
  52.          (void) safe_qbuf(qbuf, (char *) 0, qsfx, obj,
  53.                           (quan == 1L) ? Doname2 : doname, ansimpleoname,
  54.                           (quan == 1L) ? "that" : "those");
  55.          if (yn(qbuf) == 'n') {
  56.              buy = PAY_SKIP;                         /* don't want to buy */
  57.          } else if (quan < bp->bquan && !consumed) { /* partly used goods */
  58.              obj->quan = bp->bquan - save_quan;      /* used up amount */
  59.              verbalize("%s for the other %s before buying %s.",
  60.                        ANGRY(shkp) ? "Pay" : "Please pay",
  61.                        simpleonames(obj), /* short name suffices */
  62.                        save_quan > 1L ? "these" : "this one");
  63.              buy = PAY_SKIP; /* shk won't sell */
  64.          }
  65.      }
  66.      if (buy == PAY_BUY && umoney + ESHK(shkp)->credit < ltmp) {
  67.          You("don't%s have gold%s enough to pay for %s.",
  68.              stashed_gold ? " seem to" : "",
  69.              (ESHK(shkp)->credit > 0L) ? " or credit" : "",
  70.              thesimpleoname(obj));
  71.          buy = itemize ? PAY_SKIP : PAY_CANT;
  72.      }
  73.  
  74.      if (buy != PAY_BUY) {
  75.          /* restore unpaid object to original state */
  76.          obj->quan = save_quan;
  77.          obj->unpaid = 1;
  78.          iflags.suppress_price--;
  79.          return buy;
  80.      }
  81.  
  82.      pay(ltmp, shkp);
  83.      shk_names_obj(shkp, obj,
  84.                    consumed ? "paid for %s at a cost of %ld gold piece%s.%s"
  85.                             : "bought %s for %ld gold piece%s.%s",
  86.                    ltmp, "");
  87.      obj->quan = save_quan; /* restore original count */
  88.      /* quan => amount just bought, save_quan => remaining unpaid count */
  89.      if (consumed) {
  90.          if (quan != bp->bquan) {
  91.              /* eliminate used-up portion; remainder is still unpaid */
  92.              bp->bquan = obj->quan;
  93.              obj->unpaid = 1;
  94.              bp->useup = 0;
  95.              buy = PAY_SOME;
  96.          } else { /* completely used-up, so get rid of it */
  97.              obj_extract_self(obj);
  98.              /* assert( obj == *obj_p ); */
  99.              dealloc_obj(obj);
  100.              *obj_p = 0; /* destroy pointer to freed object */
  101.          }
  102.      } else if (itemize)
  103.          update_inventory(); /* Done just once in dopay() if !itemize. */
  104.      iflags.suppress_price--;
  105.      return buy;
  106.  }
  107.  

paybill

  1.  static struct repo { /* repossession context */
  2.      struct monst *shopkeeper;
  3.      coord location;
  4.  } repo;
  5.  
  6.  /* routine called after dying (or quitting) */
  7.  boolean
  8.  paybill(croaked)
  9.  int croaked; /* -1: escaped dungeon; 0: quit; 1: died */
  10.  {
  11.      struct monst *mtmp, *mtmp2, *firstshk, *resident, *creditor, *hostile,
  12.          *localshk;
  13.      struct eshk *eshkp;
  14.      boolean taken = FALSE, local;
  15.      int numsk = 0;
  16.  
  17.      /* if we escaped from the dungeon, shopkeepers can't reach us;
  18.         shops don't occur on level 1, but this could happen if hero
  19.         level teleports out of the dungeon and manages not to die */
  20.      if (croaked < 0)
  21.          return FALSE;
  22.      /* [should probably also return false when dead hero has been
  23.          petrified since shk shouldn't be able to grab inventory
  24.          which has been shut inside a statue] */
  25.  
  26.      /* this is where inventory will end up if any shk takes it */
  27.      repo.location.x = repo.location.y = 0;
  28.      repo.shopkeeper = 0;
  29.  
  30.      /*
  31.       * Scan all shopkeepers on the level, to prioritize them:
  32.       * 1) keeper of shop hero is inside and who is owed money,
  33.       * 2) keeper of shop hero is inside who isn't owed any money,
  34.       * 3) other shk who is owed money, 4) other shk who is angry,
  35.       * 5) any shk local to this level, and if none is found,
  36.       * 6) first shk on monster list (last resort; unlikely, since
  37.       * any nonlocal shk will probably be in the owed category
  38.       * and almost certainly be in the angry category).
  39.       */
  40.      resident = creditor = hostile = localshk = (struct monst *) 0;
  41.      for (mtmp = next_shkp(fmon, FALSE); mtmp;
  42.           mtmp = next_shkp(mtmp2, FALSE)) {
  43.          mtmp2 = mtmp->nmon;
  44.          eshkp = ESHK(mtmp);
  45.          local = on_level(&eshkp->shoplevel, &u.uz);
  46.          if (local && index(u.ushops, eshkp->shoproom)) {
  47.              /* inside this shk's shop [there might be more than one
  48.                 resident shk if hero is standing in a breech of a shared
  49.                 wall, so give priority to one who's also owed money] */
  50.              if (!resident || eshkp->billct || eshkp->debit || eshkp->robbed)
  51.                  resident = mtmp;
  52.          } else if (eshkp->billct || eshkp->debit || eshkp->robbed) {
  53.              /* owe this shopkeeper money (might also owe others) */
  54.              if (!creditor)
  55.                  creditor = mtmp;
  56.          } else if (eshkp->following || ANGRY(mtmp)) {
  57.              /* this shopkeeper is antagonistic (others might be too) */
  58.              if (!hostile)
  59.                  hostile = mtmp;
  60.          } else if (local) {
  61.              /* this shopkeeper's shop is on current level */
  62.              if (!localshk)
  63.                  localshk = mtmp;
  64.          }
  65.      }
  66.  
  67.      /* give highest priority shopkeeper first crack */
  68.      firstshk = resident ? resident
  69.                          : creditor ? creditor
  70.                                     : hostile ? hostile
  71.                                               : localshk;
  72.      if (firstshk) {
  73.          numsk++;
  74.          taken = inherits(firstshk, numsk, croaked);
  75.      }
  76.  
  77.      /* now handle the rest */
  78.      for (mtmp = next_shkp(fmon, FALSE); mtmp;
  79.           mtmp = next_shkp(mtmp2, FALSE)) {
  80.          mtmp2 = mtmp->nmon;
  81.          eshkp = ESHK(mtmp);
  82.          local = on_level(&eshkp->shoplevel, &u.uz);
  83.          if (mtmp != firstshk) {
  84.              numsk++;
  85.              taken |= inherits(mtmp, numsk, croaked);
  86.          }
  87.          /* for bones: we don't want a shopless shk around */
  88.          if (!local)
  89.              mongone(mtmp);
  90.      }
  91.      return taken;
  92.  }
  93.  

inherits

  1.  STATIC_OVL boolean
  2.  inherits(shkp, numsk, croaked)
  3.  struct monst *shkp;
  4.  int numsk;
  5.  int croaked;
  6.  {
  7.      long loss = 0L;
  8.      long umoney;
  9.      struct eshk *eshkp = ESHK(shkp);
  10.      boolean take = FALSE, taken = FALSE;
  11.      unsigned save_minvis = shkp->minvis;
  12.      int roomno = *u.ushops;
  13.      char takes[BUFSZ];
  14.  
  15.      shkp->minvis = 0;
  16.      /* The simplifying principle is that first-come
  17.         already took everything you had. */
  18.      if (numsk > 1) {
  19.          if (cansee(shkp->mx, shkp->my) && croaked) {
  20.              takes[0] = '\0';
  21.              if (has_head(shkp->data) && !rn2(2))
  22.                  Sprintf(takes, ", shakes %s %s,", mhis(shkp),
  23.                          mbodypart(shkp, HEAD));
  24.              pline("%s %slooks at your corpse%s and %s.", Monnam(shkp),
  25.                    (!shkp->mcanmove || shkp->msleeping) ? "wakes up, " : "",
  26.                    takes, !inhishop(shkp) ? "disappears" : "sighs");
  27.          }
  28.          rouse_shk(shkp, FALSE); /* wake shk for bones */
  29.          taken = (roomno == eshkp->shoproom);
  30.          goto skip;
  31.      }
  32.  
  33.      /* get one case out of the way: you die in the shop, the */
  34.      /* shopkeeper is peaceful, nothing stolen, nothing owed. */
  35.      if (roomno == eshkp->shoproom && inhishop(shkp) && !eshkp->billct
  36.          && !eshkp->robbed && !eshkp->debit && NOTANGRY(shkp)
  37.          && !eshkp->following) {
  38.          taken = (invent != 0);
  39.          if (taken)
  40.              pline("%s gratefully inherits all your possessions.",
  41.                    shkname(shkp));
  42.          set_repo_loc(shkp);
  43.          goto clear;
  44.      }
  45.  
  46.      if (eshkp->billct || eshkp->debit || eshkp->robbed) {
  47.          if (roomno == eshkp->shoproom && inhishop(shkp))
  48.              loss = addupbill(shkp) + eshkp->debit;
  49.          if (loss < eshkp->robbed)
  50.              loss = eshkp->robbed;
  51.          take = TRUE;
  52.      }
  53.  
  54.      if (eshkp->following || ANGRY(shkp) || take) {
  55.          if (!invent)
  56.              goto skip;
  57.          umoney = money_cnt(invent);
  58.          takes[0] = '\0';
  59.          if (!shkp->mcanmove || shkp->msleeping)
  60.              Strcat(takes, "wakes up and ");
  61.          if (distu(shkp->mx, shkp->my) > 2)
  62.              Strcat(takes, "comes and ");
  63.          Strcat(takes, "takes");
  64.  
  65.          if (loss > umoney || !loss || roomno == eshkp->shoproom) {
  66.              eshkp->robbed -= umoney;
  67.              if (eshkp->robbed < 0L)
  68.                  eshkp->robbed = 0L;
  69.              if (umoney > 0)
  70.                  money2mon(shkp, umoney);
  71.              context.botl = 1;
  72.              pline("%s %s all your possessions.", shkname(shkp), takes);
  73.              taken = TRUE;
  74.              /* where to put player's invent (after disclosure) */
  75.              set_repo_loc(shkp);
  76.          } else {
  77.              money2mon(shkp, loss);
  78.              context.botl = 1;
  79.              pline("%s %s the %ld %s %sowed %s.", Monnam(shkp), takes, loss,
  80.                    currency(loss),
  81.                    strncmp(eshkp->customer, plname, PL_NSIZ) ? "" : "you ",
  82.                    shkp->female ? "her" : "him");
  83.              /* shopkeeper has now been paid in full */
  84.              pacify_shk(shkp);
  85.              eshkp->following = 0;
  86.              eshkp->robbed = 0L;
  87.          }
  88.      skip:
  89.          /* in case we create bones */
  90.          rouse_shk(shkp, FALSE); /* wake up */
  91.          if (!inhishop(shkp))
  92.              home_shk(shkp, FALSE);
  93.      }
  94.  clear:
  95.      shkp->minvis = save_minvis;
  96.      setpaid(shkp);
  97.      return taken;
  98.  }
  99.  

set_repo_loc

  1.  STATIC_OVL void
  2.  set_repo_loc(shkp)
  3.  struct monst *shkp;
  4.  {
  5.      register xchar ox, oy;
  6.      struct eshk *eshkp = ESHK(shkp);
  7.  
  8.      /* if you're not in this shk's shop room, or if you're in its doorway
  9.          or entry spot, then your gear gets dumped all the way inside */
  10.      if (*u.ushops != eshkp->shoproom || IS_DOOR(levl[u.ux][u.uy].typ)
  11.          || (u.ux == eshkp->shk.x && u.uy == eshkp->shk.y)) {
  12.          /* shk.x,shk.y is the position immediately in
  13.           * front of the door -- move in one more space
  14.           */
  15.          ox = eshkp->shk.x;
  16.          oy = eshkp->shk.y;
  17.          ox += sgn(ox - eshkp->shd.x);
  18.          oy += sgn(oy - eshkp->shd.y);
  19.      } else { /* already inside this shk's shop */
  20.          ox = u.ux;
  21.          oy = u.uy;
  22.      }
  23.      /* finish_paybill will deposit invent here */
  24.      repo.location.x = ox;
  25.      repo.location.y = oy;
  26.      repo.shopkeeper = shkp;
  27.  }
  28.  

finish_paybill

  1.  /* called at game exit, after inventory disclosure but before making bones */
  2.  void
  3.  finish_paybill()
  4.  {
  5.      struct monst *shkp = repo.shopkeeper;
  6.      int ox = repo.location.x, oy = repo.location.y;
  7.  
  8.  #if 0 /* don't bother */
  9.      if (ox == 0 && oy == 0)
  10.          impossible("finish_paybill: no location");
  11.  #endif
  12.      /* normally done by savebones(), but that's too late in this case */
  13.      unleash_all();
  14.      /* if hero has any gold left, take it into shopkeeper's possession */
  15.      if (shkp) {
  16.          long umoney = money_cnt(invent);
  17.  
  18.          if (umoney)
  19.              money2mon(shkp, umoney);
  20.      }
  21.      /* transfer rest of the character's inventory to the shop floor */
  22.      drop_upon_death((struct monst *) 0, (struct obj *) 0, ox, oy);
  23.  }
  24.  

bp_to_obj

  1.  /* find obj on one of the lists */
  2.  STATIC_OVL struct obj *
  3.  bp_to_obj(bp)
  4.  register struct bill_x *bp;
  5.  {
  6.      register struct obj *obj;
  7.      register unsigned int id = bp->bo_id;
  8.  
  9.      if (bp->useup)
  10.          obj = o_on(id, billobjs);
  11.      else
  12.          obj = find_oid(id);
  13.      return obj;
  14.  }
  15.  

find_oid

  1.  /*
  2.   * Look for o_id on all lists but billobj.  Return obj or NULL if not found.
  3.   * Its OK for restore_timers() to call this function, there should not
  4.   * be any timeouts on the billobjs chain.
  5.   */
  6.  struct obj *
  7.  find_oid(id)
  8.  unsigned id;
  9.  {
  10.      struct obj *obj;
  11.      struct monst *mon, *mmtmp[3];
  12.      int i;
  13.  
  14.      /* first check various obj lists directly */
  15.      if ((obj = o_on(id, invent)) != 0)
  16.          return obj;
  17.      if ((obj = o_on(id, fobj)) != 0)
  18.          return obj;
  19.      if ((obj = o_on(id, level.buriedobjlist)) != 0)
  20.          return obj;
  21.      if ((obj = o_on(id, migrating_objs)) != 0)
  22.          return obj;
  23.  
  24.      /* not found yet; check inventory for members of various monst lists */
  25.      mmtmp[0] = fmon;
  26.      mmtmp[1] = migrating_mons;
  27.      mmtmp[2] = mydogs; /* for use during level changes */
  28.      for (i = 0; i < 3; i++)
  29.          for (mon = mmtmp[i]; mon; mon = mon->nmon)
  30.              if ((obj = o_on(id, mon->minvent)) != 0)
  31.                  return obj;
  32.  
  33.      /* not found at all */
  34.      return (struct obj *) 0;
  35.  }
  36.  

get_cost_of_shop_item

  1.  /* Returns the price of an arbitrary item in the shop.
  2.     Returns 0 if the item doesn't belong to a shopkeeper. */
  3.  long
  4.  get_cost_of_shop_item(obj)
  5.  register struct obj *obj;
  6.  {
  7.      struct monst *shkp;
  8.      xchar x, y;
  9.      long cost = 0L;
  10.  
  11.      if (*u.ushops
  12.          && obj->oclass != COIN_CLASS
  13.          && obj != uball && obj != uchain
  14.          && get_obj_location(obj, &x, &y, 0)
  15.          && (obj->unpaid
  16.              || (obj->where == OBJ_FLOOR
  17.                  && !obj->no_charge && costly_spot(x, y)))
  18.          && (shkp = shop_keeper(*in_rooms(x, y, SHOPBASE))) != 0
  19.          && inhishop(shkp)) {
  20.          cost = obj->quan * get_cost(obj, shkp);
  21.          if (Has_contents(obj))
  22.              cost += contained_cost(obj, shkp, 0L, FALSE, FALSE);
  23.      }
  24.      return cost;
  25.  }
  26.  

get_cost

  1.  /* calculate the value that the shk will charge for [one of] an object */
  2.  STATIC_OVL long
  3.  get_cost(obj, shkp)
  4.  register struct obj *obj;
  5.  register struct monst *shkp; /* if angry, impose a surcharge */
  6.  {
  7.      long tmp = getprice(obj, FALSE),
  8.           /* used to perform a single calculation even when multiple
  9.              adjustments (unID'd, dunce/tourist, charisma) are made */
  10.          multiplier = 1L, divisor = 1L;
  11.  
  12.      if (!tmp)
  13.          tmp = 5L;
  14.      /* shopkeeper may notice if the player isn't very knowledgeable -
  15.         especially when gem prices are concerned */
  16.      if (!obj->dknown || !objects[obj->otyp].oc_name_known) {
  17.          if (obj->oclass == GEM_CLASS
  18.              && objects[obj->otyp].oc_material == GLASS) {
  19.              int i;
  20.              /* get a value that's 'random' from game to game, but the
  21.                 same within the same game */
  22.              boolean pseudorand =
  23.                  (((int) ubirthday % obj->otyp) >= obj->otyp / 2);
  24.  
  25.              /* all gems are priced high - real or not */
  26.              switch (obj->otyp - LAST_GEM) {
  27.              case 1: /* white */
  28.                  i = pseudorand ? DIAMOND : OPAL;
  29.                  break;
  30.              case 2: /* blue */
  31.                  i = pseudorand ? SAPPHIRE : AQUAMARINE;
  32.                  break;
  33.              case 3: /* red */
  34.                  i = pseudorand ? RUBY : JASPER;
  35.                  break;
  36.              case 4: /* yellowish brown */
  37.                  i = pseudorand ? AMBER : TOPAZ;
  38.                  break;
  39.              case 5: /* orange */
  40.                  i = pseudorand ? JACINTH : AGATE;
  41.                  break;
  42.              case 6: /* yellow */
  43.                  i = pseudorand ? CITRINE : CHRYSOBERYL;
  44.                  break;
  45.              case 7: /* black */
  46.                  i = pseudorand ? BLACK_OPAL : JET;
  47.                  break;
  48.              case 8: /* green */
  49.                  i = pseudorand ? EMERALD : JADE;
  50.                  break;
  51.              case 9: /* violet */
  52.                  i = pseudorand ? AMETHYST : FLUORITE;
  53.                  break;
  54.              default:
  55.                  impossible("bad glass gem %d?", obj->otyp);
  56.                  i = STRANGE_OBJECT;
  57.                  break;
  58.              }
  59.              tmp = (long) objects[i].oc_cost;
  60.          } else if (!(obj->o_id % 4)) {
  61.              /* unid'd, arbitrarily impose surcharge: tmp *= 4/3 */
  62.              multiplier *= 4L;
  63.              divisor *= 3L;
  64.          }
  65.      }
  66.      if (uarmh && uarmh->otyp == DUNCE_CAP)
  67.          multiplier *= 4L, divisor *= 3L;
  68.      else if ((Role_if(PM_TOURIST) && u.ulevel < (MAXULEV / 2))
  69.               || (uarmu && !uarm && !uarmc)) /* touristy shirt visible */
  70.          multiplier *= 4L, divisor *= 3L;
  71.  
  72.      if (ACURR(A_CHA) > 18)
  73.          divisor *= 2L;
  74.      else if (ACURR(A_CHA) == 18)
  75.          multiplier *= 2L, divisor *= 3L;
  76.      else if (ACURR(A_CHA) >= 16)
  77.          multiplier *= 3L, divisor *= 4L;
  78.      else if (ACURR(A_CHA) <= 5)
  79.          multiplier *= 2L;
  80.      else if (ACURR(A_CHA) <= 7)
  81.          multiplier *= 3L, divisor *= 2L;
  82.      else if (ACURR(A_CHA) <= 10)
  83.          multiplier *= 4L, divisor *= 3L;
  84.  
  85.      /* tmp = (tmp * multiplier) / divisor [with roundoff tweak] */
  86.      tmp *= multiplier;
  87.      if (divisor > 1L) {
  88.          /* tmp = (((tmp * 10) / divisor) + 5) / 10 */
  89.          tmp *= 10L;
  90.          tmp /= divisor;
  91.          tmp += 5L;
  92.          tmp /= 10L;
  93.      }
  94.  
  95.      if (tmp <= 0L)
  96.          tmp = 1L;
  97.      /* the artifact prices in artilist[] are also used as a score bonus;
  98.         inflate their shop price here without affecting score calculation */
  99.      if (obj->oartifact)
  100.          tmp *= 4L;
  101.  
  102.      /* anger surcharge should match rile_shk's, so we do it separately
  103.         from the multiplier/divisor calculation */
  104.      if (shkp && ESHK(shkp)->surcharge)
  105.          tmp += (tmp + 2L) / 3L;
  106.      return tmp;
  107.  }
  108.  

contained_cost

  1.  /* returns the price of a container's content.  the price
  2.   * of the "top" container is added in the calling functions.
  3.   * a different price quoted for selling as vs. buying.
  4.   */
  5.  long
  6.  contained_cost(obj, shkp, price, usell, unpaid_only)
  7.  struct obj *obj;
  8.  struct monst *shkp;
  9.  long price;
  10.  boolean usell;
  11.  boolean unpaid_only;
  12.  {
  13.      register struct obj *otmp;
  14.  
  15.      /* price of contained objects; "top" container handled by caller */
  16.      for (otmp = obj->cobj; otmp; otmp = otmp->nobj) {
  17.          if (otmp->oclass == COIN_CLASS)
  18.              continue;
  19.  
  20.          if (usell) {
  21.              if (saleable(shkp, otmp) && !otmp->unpaid
  22.                  && otmp->oclass != BALL_CLASS
  23.                  && !(otmp->oclass == FOOD_CLASS && otmp->oeaten)
  24.                  && !(Is_candle(otmp)
  25.                       && otmp->age < 20L * (long) objects[otmp->otyp].oc_cost))
  26.                  price += set_cost(otmp, shkp);
  27.          } else if (!otmp->no_charge && (otmp->unpaid || !unpaid_only)) {
  28.              price += get_cost(otmp, shkp) * otmp->quan;
  29.          }
  30.  
  31.          if (Has_contents(otmp))
  32.              price = contained_cost(otmp, shkp, price, usell, unpaid_only);
  33.      }
  34.  
  35.      return price;
  36.  }
  37.  

contained_gold

  1.  long
  2.  contained_gold(obj)
  3.  register struct obj *obj;
  4.  {
  5.      register struct obj *otmp;
  6.      register long value = 0L;
  7.  
  8.      /* accumulate contained gold */
  9.      for (otmp = obj->cobj; otmp; otmp = otmp->nobj)
  10.          if (otmp->oclass == COIN_CLASS)
  11.              value += otmp->quan;
  12.          else if (Has_contents(otmp))
  13.              value += contained_gold(otmp);
  14.  
  15.      return value;
  16.  }
  17.  

dropped_container

  1.  STATIC_OVL void
  2.  dropped_container(obj, shkp, sale)
  3.  register struct obj *obj;
  4.  register struct monst *shkp;
  5.  register boolean sale;
  6.  {
  7.      register struct obj *otmp;
  8.  
  9.      /* the "top" container is treated in the calling fn */
  10.      for (otmp = obj->cobj; otmp; otmp = otmp->nobj) {
  11.          if (otmp->oclass == COIN_CLASS)
  12.              continue;
  13.  
  14.          if (!otmp->unpaid && !(sale && saleable(shkp, otmp)))
  15.              otmp->no_charge = 1;
  16.  
  17.          if (Has_contents(otmp))
  18.              dropped_container(otmp, shkp, sale);
  19.      }
  20.  }
  21.  

picked_container

  1.  void
  2.  picked_container(obj)
  3.  register struct obj *obj;
  4.  {
  5.      register struct obj *otmp;
  6.  
  7.      /* the "top" container is treated in the calling fn */
  8.      for (otmp = obj->cobj; otmp; otmp = otmp->nobj) {
  9.          if (otmp->oclass == COIN_CLASS)
  10.              continue;
  11.  
  12.          if (otmp->no_charge)
  13.              otmp->no_charge = 0;
  14.  
  15.          if (Has_contents(otmp))
  16.              picked_container(otmp);
  17.      }
  18.  }
  19.  

special_stock

  1.  STATIC_OVL boolean
  2.  special_stock(obj, shkp, quietly)
  3.  struct obj *obj;
  4.  struct monst *shkp;
  5.  boolean quietly;
  6.  {
  7.      /* for unique situations */
  8.      if (ESHK(shkp)->shoptype == CANDLESHOP
  9.          && obj->otyp == CANDELABRUM_OF_INVOCATION) {
  10.          if (!quietly) {
  11.              if (is_izchak(shkp, TRUE) && !u.uevent.invoked) {
  12.                  verbalize("No thanks, I'd hang onto that if I were you.");
  13.                  if (obj->spe < 7)
  14.                      verbalize(
  15.                               "You'll need %d%s candle%s to go along with it.",
  16.                                (7 - obj->spe), (obj->spe > 0) ? " more" : "",
  17.                                plur(7 - obj->spe));
  18.                  /* [what if hero is already carrying enough candles?
  19.                     should Izchak explain how to attach them instead] */
  20.              } else {
  21.                  verbalize("I won't stock that.  Take it out of here!");
  22.              }
  23.          }
  24.          return TRUE;
  25.      }
  26.      return FALSE;
  27.  }
  28.  

set_cost

  1.  /* calculate how much the shk will pay when buying [all of] an object */
  2.  STATIC_OVL long
  3.  set_cost(obj, shkp)
  4.  register struct obj *obj;
  5.  register struct monst *shkp;
  6.  {
  7.      long tmp = getprice(obj, TRUE) * obj->quan, multiplier = 1L, divisor = 1L;
  8.  
  9.      if (uarmh && uarmh->otyp == DUNCE_CAP)
  10.          divisor *= 3L;
  11.      else if ((Role_if(PM_TOURIST) && u.ulevel < (MAXULEV / 2))
  12.               || (uarmu && !uarm && !uarmc)) /* touristy shirt visible */
  13.          divisor *= 3L;
  14.      else
  15.          divisor *= 2L;
  16.  
  17.      /* shopkeeper may notice if the player isn't very knowledgeable -
  18.         especially when gem prices are concerned */
  19.      if (!obj->dknown || !objects[obj->otyp].oc_name_known) {
  20.          if (obj->oclass == GEM_CLASS) {
  21.              /* different shop keepers give different prices */
  22.              if (objects[obj->otyp].oc_material == GEMSTONE
  23.                  || objects[obj->otyp].oc_material == GLASS) {
  24.                  tmp = (obj->otyp % (6 - shkp->m_id % 3));
  25.                  tmp = (tmp + 3) * obj->quan;
  26.              }
  27.          } else if (tmp > 1L && !rn2(4))
  28.              multiplier *= 3L, divisor *= 4L;
  29.      }
  30.  
  31.      if (tmp >= 1L) {
  32.          /* [see get_cost()] */
  33.          tmp *= multiplier;
  34.          if (divisor > 1L) {
  35.              tmp *= 10L;
  36.              tmp /= divisor;
  37.              tmp += 5L;
  38.              tmp /= 10L;
  39.          }
  40.          /* avoid adjusting nonzero to zero */
  41.          if (tmp < 1L)
  42.              tmp = 1L;
  43.      }
  44.  
  45.      /* (no adjustment for angry shk here) */
  46.      return tmp;
  47.  }
  48.  

alter_cost

  1.  /* called when an item's value has been enhanced; if it happens to be
  2.     on any shop bill, update that bill to reflect the new higher price
  3.     [if the new price drops for some reason, keep the old one in place] */
  4.  void
  5.  alter_cost(obj, amt)
  6.  struct obj *obj;
  7.  long amt; /* if 0, use regular shop pricing, otherwise force amount;
  8.               if negative, use abs(amt) even if it's less than old cost */
  9.  {
  10.      struct bill_x *bp = 0;
  11.      struct monst *shkp;
  12.      long new_price;
  13.  
  14.      for (shkp = next_shkp(fmon, TRUE); shkp; shkp = next_shkp(shkp, TRUE))
  15.          if ((bp = onbill(obj, shkp, TRUE)) != 0) {
  16.              new_price = !amt ? get_cost(obj, shkp) : (amt < 0L) ? -amt : amt;
  17.              if (new_price > bp->price || amt < 0L) {
  18.                  bp->price = new_price;
  19.                  update_inventory();
  20.              }
  21.              break; /* done */
  22.          }
  23.      return;
  24.  }
  25.  

unpaid_cost

  1.  /* called from doinv(invent.c) for inventory of unpaid objects */
  2.  long
  3.  unpaid_cost(unp_obj, include_contents)
  4.  struct obj *unp_obj; /* known to be unpaid or contain unpaid */
  5.  boolean include_contents;
  6.  {
  7.      struct bill_x *bp = (struct bill_x *) 0;
  8.      struct monst *shkp;
  9.      long amt = 0L;
  10.      xchar ox, oy;
  11.  
  12.      if (!get_obj_location(unp_obj, &ox, &oy, BURIED_TOO | CONTAINED_TOO))
  13.          ox = u.ux, oy = u.uy; /* (shouldn't happen) */
  14.      if ((shkp = shop_keeper(*in_rooms(ox, oy, SHOPBASE))) != 0) {
  15.          bp = onbill(unp_obj, shkp, TRUE);
  16.      } else {
  17.          /* didn't find shk?  try searching bills */
  18.          for (shkp = next_shkp(fmon, TRUE); shkp;
  19.               shkp = next_shkp(shkp->nmon, TRUE))
  20.              if ((bp = onbill(unp_obj, shkp, TRUE)) != 0)
  21.                  break;
  22.      }
  23.  
  24.      /* onbill() gave no message if unexpected problem occurred */
  25.      if (!shkp || (unp_obj->unpaid && !bp)) {
  26.          impossible("unpaid_cost: object wasn't on any bill.");
  27.      } else {
  28.          if (bp)
  29.              amt = unp_obj->quan * bp->price;
  30.          if (include_contents && Has_contents(unp_obj))
  31.              amt = contained_cost(unp_obj, shkp, amt, FALSE, TRUE);
  32.      }
  33.      return amt;
  34.  }
  35.  

add_one_tobill

  1.  STATIC_OVL void
  2.  add_one_tobill(obj, dummy, shkp)
  3.  struct obj *obj;
  4.  boolean dummy;
  5.  struct monst *shkp;
  6.  {
  7.      struct eshk *eshkp;
  8.      struct bill_x *bp;
  9.      int bct;
  10.  
  11.      if (!billable(&shkp, obj, *u.ushops, TRUE))
  12.          return;
  13.      eshkp = ESHK(shkp);
  14.  
  15.      if (eshkp->billct == BILLSZ) {
  16.          You("got that for free!");
  17.          return;
  18.      }
  19.  
  20.      bct = eshkp->billct;
  21.      bp = &(eshkp->bill_p[bct]);
  22.      bp->bo_id = obj->o_id;
  23.      bp->bquan = obj->quan;
  24.      if (dummy) {              /* a dummy object must be inserted into  */
  25.          bp->useup = 1;        /* the billobjs chain here.  crucial for */
  26.          add_to_billobjs(obj); /* eating floorfood in shop.  see eat.c  */
  27.      } else
  28.          bp->useup = 0;
  29.      bp->price = get_cost(obj, shkp);
  30.      eshkp->billct++;
  31.      obj->unpaid = 1;
  32.  }
  33.  

add_to_billobjs

  1.  STATIC_OVL void
  2.  add_to_billobjs(obj)
  3.  struct obj *obj;
  4.  {
  5.      if (obj->where != OBJ_FREE)
  6.          panic("add_to_billobjs: obj not free");
  7.      if (obj->timed)
  8.          obj_stop_timers(obj);
  9.  
  10.      obj->nobj = billobjs;
  11.      billobjs = obj;
  12.      obj->where = OBJ_ONBILL;
  13.  }
  14.  

bill_box_content

  1.  /* recursive billing of objects within containers. */
  2.  STATIC_OVL void
  3.  bill_box_content(obj, ininv, dummy, shkp)
  4.  register struct obj *obj;
  5.  register boolean ininv, dummy;
  6.  register struct monst *shkp;
  7.  {
  8.      register struct obj *otmp;
  9.  
  10.      for (otmp = obj->cobj; otmp; otmp = otmp->nobj) {
  11.          if (otmp->oclass == COIN_CLASS)
  12.              continue;
  13.  
  14.          /* the "top" box is added in addtobill() */
  15.          if (!otmp->no_charge)
  16.              add_one_tobill(otmp, dummy, shkp);
  17.          if (Has_contents(otmp))
  18.              bill_box_content(otmp, ininv, dummy, shkp);
  19.      }
  20.  }
  21.  

shk_names_obj

  1.  /* shopkeeper tells you what you bought or sold, sometimes partly IDing it */
  2.  STATIC_OVL void
  3.  shk_names_obj(shkp, obj, fmt, amt, arg)
  4.  struct monst *shkp;
  5.  struct obj *obj;
  6.  const char *fmt; /* "%s %ld %s %s", doname(obj), amt, plur(amt), arg */
  7.  long amt;
  8.  const char *arg;
  9.  {
  10.      char *obj_name, fmtbuf[BUFSZ];
  11.      boolean was_unknown = !obj->dknown;
  12.  
  13.      obj->dknown = TRUE;
  14.      /* Use real name for ordinary weapons/armor, and spell-less
  15.       * scrolls/books (that is, blank and mail), but only if the
  16.       * object is within the shk's area of interest/expertise.
  17.       */
  18.      if (!objects[obj->otyp].oc_magic && saleable(shkp, obj)
  19.          && (obj->oclass == WEAPON_CLASS || obj->oclass == ARMOR_CLASS
  20.              || obj->oclass == SCROLL_CLASS || obj->oclass == SPBOOK_CLASS
  21.              || obj->otyp == MIRROR)) {
  22.          was_unknown |= !objects[obj->otyp].oc_name_known;
  23.          makeknown(obj->otyp);
  24.      }
  25.      obj_name = doname(obj);
  26.      /* Use an alternate message when extra information is being provided */
  27.      if (was_unknown) {
  28.          Sprintf(fmtbuf, "%%s; you %s", fmt);
  29.          obj_name[0] = highc(obj_name[0]);
  30.          pline(fmtbuf, obj_name, (obj->quan > 1L) ? "them" : "it", amt,
  31.                plur(amt), arg);
  32.      } else {
  33.          You(fmt, obj_name, amt, plur(amt), arg);
  34.      }
  35.  }
  36.  

billable

  1.  /* decide whether a shopkeeper thinks an item belongs to her */
  2.  boolean
  3.  billable(shkpp, obj, roomno, reset_nocharge)
  4.  struct monst **shkpp; /* in: non-null if shk has been validated; out: shk */
  5.  struct obj *obj;
  6.  char roomno;
  7.  boolean reset_nocharge;
  8.  {
  9.      struct monst *shkp = *shkpp;
  10.  
  11.      /* if caller hasn't supplied a shopkeeper, look one up now */
  12.      if (!shkp) {
  13.          if (!roomno)
  14.              return FALSE;
  15.          shkp = shop_keeper(roomno);
  16.          if (!shkp || !inhishop(shkp))
  17.              return FALSE;
  18.          *shkpp = shkp;
  19.      }
  20.      /* perhaps we threw it away earlier */
  21.      if (onbill(obj, shkp, FALSE)
  22.          || (obj->oclass == FOOD_CLASS && obj->oeaten))
  23.          return FALSE;
  24.      /* outer container might be marked no_charge but still have contents
  25.         which should be charged for; clear no_charge when picking things up */
  26.      if (obj->no_charge) {
  27.          if (!Has_contents(obj) || (contained_gold(obj) == 0L
  28.                                     && contained_cost(obj, shkp, 0L, FALSE,
  29.                                                       !reset_nocharge) == 0L))
  30.              shkp = 0; /* not billable */
  31.          if (reset_nocharge && !shkp && obj->oclass != COIN_CLASS) {
  32.              obj->no_charge = 0;
  33.              if (Has_contents(obj))
  34.                  picked_container(obj); /* clear no_charge */
  35.          }
  36.      }
  37.      return shkp ? TRUE : FALSE;
  38.  }
  39.  

addtobill

  1.  void
  2.  addtobill(obj, ininv, dummy, silent)
  3.  struct obj *obj;
  4.  boolean ininv, dummy, silent;
  5.  {
  6.      struct monst *shkp = 0;
  7.      long ltmp, cltmp, gltmp;
  8.      int contentscount;
  9.      boolean container;
  10.  
  11.      if (!billable(&shkp, obj, *u.ushops, TRUE))
  12.          return;
  13.  
  14.      if (obj->oclass == COIN_CLASS) {
  15.          costly_gold(obj->ox, obj->oy, obj->quan);
  16.          return;
  17.      } else if (ESHK(shkp)->billct == BILLSZ) {
  18.          if (!silent)
  19.              You("got that for free!");
  20.          return;
  21.      }
  22.  
  23.      ltmp = cltmp = gltmp = 0L;
  24.      container = Has_contents(obj);
  25.  
  26.      if (!obj->no_charge)
  27.          ltmp = get_cost(obj, shkp);
  28.  
  29.      if (obj->no_charge && !container) {
  30.          obj->no_charge = 0;
  31.          return;
  32.      }
  33.  
  34.      if (container) {
  35.          cltmp = contained_cost(obj, shkp, cltmp, FALSE, FALSE);
  36.          gltmp = contained_gold(obj);
  37.  
  38.          if (ltmp)
  39.              add_one_tobill(obj, dummy, shkp);
  40.          if (cltmp)
  41.              bill_box_content(obj, ininv, dummy, shkp);
  42.          picked_container(obj); /* reset contained obj->no_charge */
  43.  
  44.          ltmp += cltmp;
  45.  
  46.          if (gltmp) {
  47.              costly_gold(obj->ox, obj->oy, gltmp);
  48.              if (!ltmp)
  49.                  return;
  50.          }
  51.  
  52.          if (obj->no_charge)
  53.              obj->no_charge = 0;
  54.          contentscount = count_unpaid(obj->cobj);
  55.      } else { /* !container */
  56.          add_one_tobill(obj, dummy, shkp);
  57.          contentscount = 0;
  58.      }
  59.  
  60.      if (!muteshk(shkp) && !silent) {
  61.          char buf[BUFSZ];
  62.  
  63.          if (!ltmp) {
  64.              pline("%s has no interest in %s.", Monnam(shkp), the(xname(obj)));
  65.              return;
  66.          }
  67.          if (!ininv) {
  68.              pline("%s will cost you %ld %s%s.", The(xname(obj)), ltmp,
  69.                    currency(ltmp), (obj->quan > 1L) ? " each" : "");
  70.          } else {
  71.              long save_quan = obj->quan;
  72.  
  73.              Strcpy(buf, "\"For you, ");
  74.              if (ANGRY(shkp)) {
  75.                  Strcat(buf, "scum;");
  76.              } else {
  77.                  append_honorific(buf);
  78.                  Strcat(buf, "; only");
  79.              }
  80.              obj->quan = 1L; /* fool xname() into giving singular */
  81.              pline("%s %ld %s %s %s%s.\"", buf, ltmp, currency(ltmp),
  82.                    (save_quan > 1L) ? "per" : (contentscount && !obj->unpaid)
  83.                                                   ? "for the contents of this"
  84.                                                   : "for this",
  85.                    xname(obj),
  86.                    (contentscount && obj->unpaid) ? and_its_contents : "");
  87.              obj->quan = save_quan;
  88.          }
  89.      } else if (!silent) {
  90.          if (ltmp)
  91.              pline_The("list price of %s%s%s is %ld %s%s.",
  92.                        (contentscount && !obj->unpaid) ? the_contents_of : "",
  93.                        the(xname(obj)),
  94.                        (contentscount && obj->unpaid) ? and_its_contents : "",
  95.                        ltmp, currency(ltmp), (obj->quan > 1L) ? " each" : "");
  96.          else
  97.              pline("%s does not notice.", Monnam(shkp));
  98.      }
  99.  }
  100.  

append_honorific

  1.  void
  2.  append_honorific(buf)
  3.  char *buf;
  4.  {
  5.      /* (chooses among [0]..[3] normally; [1]..[4] after the
  6.         Wizard has been killed or invocation ritual performed) */
  7.      static const char *const honored[] = { "good", "honored", "most gracious",
  8.                                             "esteemed",
  9.                                             "most renowned and sacred" };
  10.      Strcat(buf, honored[rn2(SIZE(honored) - 1) + u.uevent.udemigod]);
  11.      if (is_vampire(youmonst.data))
  12.          Strcat(buf, (flags.female) ? " dark lady" : " dark lord");
  13.      else if (is_elf(youmonst.data))
  14.          Strcat(buf, (flags.female) ? " hiril" : " hir");
  15.      else
  16.          Strcat(buf, !is_human(youmonst.data) ? " creature" : (flags.female)
  17.                                                                   ? " lady"
  18.                                                                   : " sir");
  19.  }
  20.  

splitbill

  1.  void
  2.  splitbill(obj, otmp)
  3.  register struct obj *obj, *otmp;
  4.  {
  5.      /* otmp has been split off from obj */
  6.      register struct bill_x *bp;
  7.      register long tmp;
  8.      register struct monst *shkp = shop_keeper(*u.ushops);
  9.  
  10.      if (!shkp || !inhishop(shkp)) {
  11.          impossible("splitbill: no resident shopkeeper??");
  12.          return;
  13.      }
  14.      bp = onbill(obj, shkp, FALSE);
  15.      if (!bp) {
  16.          impossible("splitbill: not on bill?");
  17.          return;
  18.      }
  19.      if (bp->bquan < otmp->quan) {
  20.          impossible("Negative quantity on bill??");
  21.      }
  22.      if (bp->bquan == otmp->quan) {
  23.          impossible("Zero quantity on bill??");
  24.      }
  25.      bp->bquan -= otmp->quan;
  26.  
  27.      if (ESHK(shkp)->billct == BILLSZ)
  28.          otmp->unpaid = 0;
  29.      else {
  30.          tmp = bp->price;
  31.          bp = &(ESHK(shkp)->bill_p[ESHK(shkp)->billct]);
  32.          bp->bo_id = otmp->o_id;
  33.          bp->bquan = otmp->quan;
  34.          bp->useup = 0;
  35.          bp->price = tmp;
  36.          ESHK(shkp)->billct++;
  37.      }
  38.  }
  39.  

sub_one_frombill

  1.  STATIC_OVL void
  2.  sub_one_frombill(obj, shkp)
  3.  register struct obj *obj;
  4.  register struct monst *shkp;
  5.  {
  6.      register struct bill_x *bp;
  7.  
  8.      if ((bp = onbill(obj, shkp, FALSE)) != 0) {
  9.          register struct obj *otmp;
  10.  
  11.          obj->unpaid = 0;
  12.          if (bp->bquan > obj->quan) {
  13.              otmp = newobj();
  14.              *otmp = *obj;
  15.              otmp->oextra = (struct oextra *) 0;
  16.              bp->bo_id = otmp->o_id = context.ident++;
  17.              otmp->where = OBJ_FREE;
  18.              otmp->quan = (bp->bquan -= obj->quan);
  19.              otmp->owt = 0; /* superfluous */
  20.              bp->useup = 1;
  21.              add_to_billobjs(otmp);
  22.              return;
  23.          }
  24.          ESHK(shkp)->billct--;
  25.  #ifdef DUMB
  26.          {
  27.              /* DRS/NS 2.2.6 messes up -- Peter Kendell */
  28.              int indx = ESHK(shkp)->billct;
  29.  
  30.              *bp = ESHK(shkp)->bill_p[indx];
  31.          }
  32.  #else
  33.          *bp = ESHK(shkp)->bill_p[ESHK(shkp)->billct];
  34.  #endif
  35.          return;
  36.      } else if (obj->unpaid) {
  37.          impossible("sub_one_frombill: unpaid object not on bill");
  38.          obj->unpaid = 0;
  39.      }
  40.  }
  41.  

subfrombill

  1.  /* recursive check of unpaid objects within nested containers. */
  2.  void
  3.  subfrombill(obj, shkp)
  4.  register struct obj *obj;
  5.  register struct monst *shkp;
  6.  {
  7.      register struct obj *otmp;
  8.  
  9.      sub_one_frombill(obj, shkp);
  10.  
  11.      if (Has_contents(obj))
  12.          for (otmp = obj->cobj; otmp; otmp = otmp->nobj) {
  13.              if (otmp->oclass == COIN_CLASS)
  14.                  continue;
  15.  
  16.              if (Has_contents(otmp))
  17.                  subfrombill(otmp, shkp);
  18.              else
  19.                  sub_one_frombill(otmp, shkp);
  20.          }
  21.  }
  22.  

stolen_container

  1.  STATIC_OVL long
  2.  stolen_container(obj, shkp, price, ininv)
  3.  struct obj *obj;
  4.  struct monst *shkp;
  5.  long price;
  6.  boolean ininv;
  7.  {
  8.      struct obj *otmp;
  9.      struct bill_x *bp;
  10.      long billamt;
  11.  
  12.      /* the price of contained objects; caller handles top container */
  13.      for (otmp = obj->cobj; otmp; otmp = otmp->nobj) {
  14.          if (otmp->oclass == COIN_CLASS)
  15.              continue;
  16.          billamt = 0L;
  17.          if (!billable(&shkp, otmp, ESHK(shkp)->shoproom, TRUE)) {
  18.              /* billable() returns false for objects already on bill */
  19.              if ((bp = onbill(otmp, shkp, FALSE)) == 0)
  20.                  continue;
  21.              /* this assumes that we're being called by stolen_value()
  22.                 (or by a recursive call to self on behalf of it) where
  23.                 the cost of this object is about to be added to shop
  24.                 debt in place of having it remain on the current bill */
  25.              billamt = bp->bquan * bp->price;
  26.              sub_one_frombill(otmp, shkp); /* avoid double billing */
  27.          }
  28.  
  29.          if (billamt)
  30.              price += billamt;
  31.          else if (ininv ? otmp->unpaid : !otmp->no_charge)
  32.              price += otmp->quan * get_cost(otmp, shkp);
  33.  
  34.          if (Has_contents(otmp))
  35.              price = stolen_container(otmp, shkp, price, ininv);
  36.      }
  37.  
  38.      return price;
  39.  }
  40.  

stolen_value

  1.  long
  2.  stolen_value(obj, x, y, peaceful, silent)
  3.  struct obj *obj;
  4.  xchar x, y;
  5.  boolean peaceful, silent;
  6.  {
  7.      long value = 0L, gvalue = 0L, billamt = 0L;
  8.      char roomno = *in_rooms(x, y, SHOPBASE);
  9.      struct bill_x *bp;
  10.      struct monst *shkp = 0;
  11.  
  12.      if (!billable(&shkp, obj, roomno, FALSE)) {
  13.          /* things already on the bill yield a not-billable result, so
  14.             we need to check bill before deciding that shk doesn't care */
  15.          if ((bp = onbill(obj, shkp, FALSE)) == 0)
  16.              return 0L;
  17.          /* shk does care; take obj off bill to avoid double billing */
  18.          billamt = bp->bquan * bp->price;
  19.          sub_one_frombill(obj, shkp);
  20.      }
  21.  
  22.      if (obj->oclass == COIN_CLASS) {
  23.          gvalue += obj->quan;
  24.      } else {
  25.          if (billamt)
  26.              value += billamt;
  27.          else if (!obj->no_charge)
  28.              value += obj->quan * get_cost(obj, shkp);
  29.  
  30.          if (Has_contents(obj)) {
  31.              boolean ininv =
  32.                  (obj->where == OBJ_INVENT || obj->where == OBJ_FREE);
  33.  
  34.              value += stolen_container(obj, shkp, 0L, ininv);
  35.              if (!ininv)
  36.                  gvalue += contained_gold(obj);
  37.          }
  38.      }
  39.  
  40.      if (gvalue + value == 0L)
  41.          return 0L;
  42.  
  43.      value += gvalue;
  44.  
  45.      if (peaceful) {
  46.          boolean credit_use = !!ESHK(shkp)->credit;
  47.          value = check_credit(value, shkp);
  48.          /* 'peaceful' affects general treatment, but doesn't affect
  49.           * the fact that other code expects that all charges after the
  50.           * shopkeeper is angry are included in robbed, not debit */
  51.          if (ANGRY(shkp))
  52.              ESHK(shkp)->robbed += value;
  53.          else
  54.              ESHK(shkp)->debit += value;
  55.  
  56.          if (!silent) {
  57.              const char *still = "";
  58.  
  59.              if (credit_use) {
  60.                  if (ESHK(shkp)->credit) {
  61.                      You("have %ld %s credit remaining.", ESHK(shkp)->credit,
  62.                          currency(ESHK(shkp)->credit));
  63.                      return value;
  64.                  } else if (!value) {
  65.                      You("have no credit remaining.");
  66.                      return 0;
  67.                  }
  68.                  still = "still ";
  69.              }
  70.              if (obj->oclass == COIN_CLASS)
  71.                  You("%sowe %s %ld %s!", still, mon_nam(shkp), value,
  72.                      currency(value));
  73.              else
  74.                  You("%sowe %s %ld %s for %s!", still, mon_nam(shkp), value,
  75.                      currency(value), obj->quan > 1L ? "them" : "it");
  76.          }
  77.      } else {
  78.          ESHK(shkp)->robbed += value;
  79.  
  80.          if (!silent) {
  81.              if (cansee(shkp->mx, shkp->my)) {
  82.                  Norep("%s booms: \"%s, you are a thief!\"", Monnam(shkp),
  83.                        plname);
  84.              } else
  85.                  Norep("You hear a scream, \"Thief!\"");
  86.          }
  87.          hot_pursuit(shkp);
  88.          (void) angry_guards(FALSE);
  89.      }
  90.      return value;
  91.  }
  92.  

sellobj_state

  1.  /* auto-response flag for/from "sell foo?" 'a' => 'y', 'q' => 'n' */
  2.  static char sell_response = 'a';
  3.  static int sell_how = SELL_NORMAL;
  4.  /* can't just use sell_response='y' for auto_credit because the 'a' response
  5.     shouldn't carry over from ordinary selling to credit selling */
  6.  static boolean auto_credit = FALSE;
  7.  
  8.  void
  9.  sellobj_state(deliberate)
  10.  int deliberate;
  11.  {
  12.      /* If we're deliberately dropping something, there's no automatic
  13.         response to the shopkeeper's "want to sell" query; however, if we
  14.         accidentally drop anything, the shk will buy it/them without asking.
  15.         This retains the old pre-query risk that slippery fingers while in
  16.         shops entailed:  you drop it, you've lost it.
  17.       */
  18.      sell_response = (deliberate != SELL_NORMAL) ? '\0' : 'a';
  19.      sell_how = deliberate;
  20.      auto_credit = FALSE;
  21.  }
  22.  

sellobj

  1.  void
  2.  sellobj(obj, x, y)
  3.  register struct obj *obj;
  4.  xchar x, y;
  5.  {
  6.      register struct monst *shkp;
  7.      register struct eshk *eshkp;
  8.      long ltmp = 0L, cltmp = 0L, gltmp = 0L, offer, shkmoney;
  9.      boolean saleitem, cgold = FALSE, container = Has_contents(obj);
  10.      boolean isgold = (obj->oclass == COIN_CLASS);
  11.      boolean only_partially_your_contents = FALSE;
  12.  
  13.      if (!(shkp = shop_keeper(*in_rooms(x, y, SHOPBASE))) || !inhishop(shkp))
  14.          return;
  15.      if (!costly_spot(x, y))
  16.          return;
  17.      if (!*u.ushops)
  18.          return;
  19.  
  20.      if (obj->unpaid && !container && !isgold) {
  21.          sub_one_frombill(obj, shkp);
  22.          return;
  23.      }
  24.      if (container) {
  25.          /* find the price of content before subfrombill */
  26.          cltmp = contained_cost(obj, shkp, cltmp, TRUE, FALSE);
  27.          /* find the value of contained gold */
  28.          gltmp += contained_gold(obj);
  29.          cgold = (gltmp > 0L);
  30.      }
  31.  
  32.      saleitem = saleable(shkp, obj);
  33.      if (!isgold && !obj->unpaid && saleitem)
  34.          ltmp = set_cost(obj, shkp);
  35.  
  36.      offer = ltmp + cltmp;
  37.  
  38.      /* get one case out of the way: nothing to sell, and no gold */
  39.      if (!isgold && ((offer + gltmp) == 0L || sell_how == SELL_DONTSELL)) {
  40.          boolean unpaid = is_unpaid(obj);
  41.  
  42.          if (container) {
  43.              dropped_container(obj, shkp, FALSE);
  44.              if (!obj->unpaid && !saleitem)
  45.                  obj->no_charge = 1;
  46.              if (unpaid)
  47.                  subfrombill(obj, shkp);
  48.          } else
  49.              obj->no_charge = 1;
  50.  
  51.          if (!unpaid && (sell_how != SELL_DONTSELL)
  52.              && !special_stock(obj, shkp, FALSE))
  53.              pline("%s seems uninterested.", Monnam(shkp));
  54.          return;
  55.      }
  56.  
  57.      /* you dropped something of your own - probably want to sell it */
  58.      rouse_shk(shkp, TRUE); /* wake up sleeping or paralyzed shk */
  59.      eshkp = ESHK(shkp);
  60.  
  61.      if (ANGRY(shkp)) { /* they become shop-objects, no pay */
  62.          if (!muteshk(shkp))
  63.              verbalize("Thank you, scum!");
  64.          subfrombill(obj, shkp);
  65.          return;
  66.      }
  67.  
  68.      if (eshkp->robbed) { /* shkp is not angry? */
  69.          if (isgold)
  70.              offer = obj->quan;
  71.          else if (cgold)
  72.              offer += cgold;
  73.          if ((eshkp->robbed -= offer < 0L))
  74.              eshkp->robbed = 0L;
  75.          if (offer && !muteshk(shkp))
  76.              verbalize(
  77.    "Thank you for your contribution to restock this recently plundered shop.");
  78.          subfrombill(obj, shkp);
  79.          return;
  80.      }
  81.  
  82.      if (isgold || cgold) {
  83.          if (!cgold)
  84.              gltmp = obj->quan;
  85.  
  86.          if (eshkp->debit >= gltmp) {
  87.              if (eshkp->loan) { /* you carry shop's gold */
  88.                  if (eshkp->loan >= gltmp)
  89.                      eshkp->loan -= gltmp;
  90.                  else
  91.                      eshkp->loan = 0L;
  92.              }
  93.              eshkp->debit -= gltmp;
  94.              Your("debt is %spaid off.", eshkp->debit ? "partially " : "");
  95.          } else {
  96.              long delta = gltmp - eshkp->debit;
  97.  
  98.              eshkp->credit += delta;
  99.              if (eshkp->debit) {
  100.                  eshkp->debit = 0L;
  101.                  eshkp->loan = 0L;
  102.                  Your("debt is paid off.");
  103.              }
  104.              if (eshkp->credit == delta)
  105.                  You("have established %ld %s credit.", delta,
  106.                      currency(delta));
  107.              else
  108.                  pline("%ld %s added to your credit; total is now %ld %s.",
  109.                        delta, currency(delta), eshkp->credit,
  110.                        currency(eshkp->credit));
  111.          }
  112.  
  113.          if (!offer) {
  114.              if (!isgold) {
  115.                  if (container)
  116.                      dropped_container(obj, shkp, FALSE);
  117.                  if (!obj->unpaid && !saleitem)
  118.                      obj->no_charge = 1;
  119.                  subfrombill(obj, shkp);
  120.              }
  121.              return;
  122.          }
  123.      }
  124.  
  125.      if ((!saleitem && !(container && cltmp > 0L)) || eshkp->billct == BILLSZ
  126.          || obj->oclass == BALL_CLASS || obj->oclass == CHAIN_CLASS
  127.          || offer == 0L || (obj->oclass == FOOD_CLASS && obj->oeaten)
  128.          || (Is_candle(obj)
  129.              && obj->age < 20L * (long) objects[obj->otyp].oc_cost)) {
  130.          pline("%s seems uninterested%s.", Monnam(shkp),
  131.                cgold ? " in the rest" : "");
  132.          if (container)
  133.              dropped_container(obj, shkp, FALSE);
  134.          obj->no_charge = 1;
  135.          return;
  136.      }
  137.  
  138.      shkmoney = money_cnt(shkp->minvent);
  139.      if (!shkmoney) {
  140.          char c, qbuf[BUFSZ];
  141.          long tmpcr = ((offer * 9L) / 10L) + (offer <= 1L);
  142.  
  143.          if (sell_how == SELL_NORMAL || auto_credit) {
  144.              c = sell_response = 'y';
  145.          } else if (sell_response != 'n') {
  146.              pline("%s cannot pay you at present.", shkname(shkp));
  147.              Sprintf(qbuf, "Will you accept %ld %s in credit for ", tmpcr,
  148.                      currency(tmpcr));
  149.              c = ynaq(safe_qbuf(qbuf, qbuf, "?", obj, doname, thesimpleoname,
  150.                                 (obj->quan == 1L) ? "that" : "those"));
  151.              if (c == 'a') {
  152.                  c = 'y';
  153.                  auto_credit = TRUE;
  154.              }
  155.          } else /* previously specified "quit" */
  156.              c = 'n';
  157.  
  158.          if (c == 'y') {
  159.              shk_names_obj(
  160.                  shkp, obj,
  161.                  (sell_how != SELL_NORMAL)
  162.                      ? "traded %s for %ld zorkmid%s in %scredit."
  163.                      : "relinquish %s and acquire %ld zorkmid%s in %scredit.",
  164.                  tmpcr, (eshkp->credit > 0L) ? "additional " : "");
  165.              eshkp->credit += tmpcr;
  166.              subfrombill(obj, shkp);
  167.          } else {
  168.              if (c == 'q')
  169.                  sell_response = 'n';
  170.              if (container)
  171.                  dropped_container(obj, shkp, FALSE);
  172.              if (!obj->unpaid)
  173.                  obj->no_charge = 1;
  174.              subfrombill(obj, shkp);
  175.          }
  176.      } else {
  177.          char qbuf[BUFSZ], qsfx[BUFSZ];
  178.          boolean short_funds = (offer > shkmoney), one;
  179.  
  180.          if (short_funds)
  181.              offer = shkmoney;
  182.          if (!sell_response) {
  183.              long yourc = 0L, shksc;
  184.  
  185.              if (container) {
  186.                  /* number of items owned by shk */
  187.                  shksc = count_contents(obj, TRUE, TRUE, FALSE);
  188.                  /* number of items owned by you (total - shksc) */
  189.                  yourc = count_contents(obj, TRUE, TRUE, TRUE) - shksc;
  190.                  only_partially_your_contents = shksc && yourc;
  191.              }
  192.              /*
  193.                 "<shk> offers * for ..." query formatting.
  194.                 Normal item(s):
  195.                  "... your <object>.  Sell it?"
  196.                  "... your <objects>.  Sell them?"
  197.                 A container is either owned by the hero, or already
  198.                 owned by the shk (!ltmp), or the shk isn't interested
  199.                 in buying it (also !ltmp).  It's either empty (!cltmp)
  200.                 or it has contents owned by the hero or it has some
  201.                 contents owned by the hero and others by the shk.
  202.                 (The case where it has contents already entirely owned
  203.                 by the shk is treated the same was if it were empty
  204.                 since the hero isn't selling any of those contents.)
  205.                 Your container:
  206.                  "... your <empty bag>.  Sell it?"
  207.                  "... your <bag> and its contents.  Sell them?"
  208.                  "... your <bag> and item inside.  Sell them?"
  209.                  "... your <bag> and items inside.  Sell them?"
  210.                 Shk's container:
  211.                  "... your item in the <bag>.  Sell it?"
  212.                  "... your items in the <bag>.  Sell them?"
  213.               */
  214.              Sprintf(qbuf, "%s offers%s %ld gold piece%s for %s%s ",
  215.                      shkname(shkp), short_funds ? " only" : "", offer,
  216.                      plur(offer),
  217.                      (cltmp && !ltmp)
  218.                          ? ((yourc == 1L) ? "your item in " : "your items in ")
  219.                          : "",
  220.                      obj->unpaid ? "the" : "your");
  221.              one = obj->unpaid ? (yourc == 1L) : (obj->quan == 1L && !cltmp);
  222.              Sprintf(qsfx, "%s.  Sell %s?",
  223.                      (cltmp && ltmp)
  224.                          ? (only_partially_your_contents
  225.                                 ? ((yourc == 1L) ? " and item inside"
  226.                                                  : " and items inside")
  227.                                 : and_its_contents)
  228.                          : "",
  229.                      one ? "it" : "them");
  230.              (void) safe_qbuf(qbuf, qbuf, qsfx, obj, xname, simpleonames,
  231.                               one ? "that" : "those");
  232.          } else
  233.              qbuf[0] = '\0'; /* just to pacify lint */
  234.  
  235.          switch (sell_response ? sell_response : ynaq(qbuf)) {
  236.          case 'q':
  237.              sell_response = 'n';
  238.          case 'n':
  239.              if (container)
  240.                  dropped_container(obj, shkp, FALSE);
  241.              if (!obj->unpaid)
  242.                  obj->no_charge = 1;
  243.              subfrombill(obj, shkp);
  244.              break;
  245.          case 'a':
  246.              sell_response = 'y';
  247.          case 'y':
  248.              if (container)
  249.                  dropped_container(obj, shkp, TRUE);
  250.              if (!obj->unpaid && !saleitem)
  251.                  obj->no_charge = 1;
  252.              subfrombill(obj, shkp);
  253.              pay(-offer, shkp);
  254.              shk_names_obj(shkp, obj,
  255.                            (sell_how != SELL_NORMAL)
  256.                             ? ((!ltmp && cltmp && only_partially_your_contents)
  257.                           ? "sold some items inside %s for %ld gold piece%s.%s"
  258.                           : "sold %s for %ld gold piece%s.%s")
  259.              : "relinquish %s and receive %ld gold piece%s in compensation.%s",
  260.                            offer, "");
  261.              break;
  262.          default:
  263.              impossible("invalid sell response");
  264.          }
  265.      }
  266.  }
  267.  

doinvbill

  1.  int
  2.  doinvbill(mode)
  3.  int mode; /* 0: deliver count 1: paged */
  4.  {
  5.  #ifdef __SASC
  6.      void sasc_bug(struct obj *, unsigned);
  7.  #endif
  8.      struct monst *shkp;
  9.      struct eshk *eshkp;
  10.      struct bill_x *bp, *end_bp;
  11.      struct obj *obj;
  12.      long totused;
  13.      char *buf_p;
  14.      winid datawin;
  15.  
  16.      shkp = shop_keeper(*u.ushops);
  17.      if (!shkp || !inhishop(shkp)) {
  18.          if (mode != 0)
  19.              impossible("doinvbill: no shopkeeper?");
  20.          return 0;
  21.      }
  22.      eshkp = ESHK(shkp);
  23.  
  24.      if (mode == 0) {
  25.          /* count expended items, so that the `I' command can decide
  26.             whether to include 'x' in its prompt string */
  27.          int cnt = !eshkp->debit ? 0 : 1;
  28.  
  29.          for (bp = eshkp->bill_p, end_bp = &eshkp->bill_p[eshkp->billct];
  30.               bp < end_bp; bp++)
  31.              if (bp->useup
  32.                  || ((obj = bp_to_obj(bp)) != 0 && obj->quan < bp->bquan))
  33.                  cnt++;
  34.          return cnt;
  35.      }
  36.  
  37.      datawin = create_nhwindow(NHW_MENU);
  38.      putstr(datawin, 0, "Unpaid articles already used up:");
  39.      putstr(datawin, 0, "");
  40.  
  41.      totused = 0L;
  42.      for (bp = eshkp->bill_p, end_bp = &eshkp->bill_p[eshkp->billct];
  43.           bp < end_bp; bp++) {
  44.          obj = bp_to_obj(bp);
  45.          if (!obj) {
  46.              impossible("Bad shopkeeper administration.");
  47.              goto quit;
  48.          }
  49.          if (bp->useup || bp->bquan > obj->quan) {
  50.              long oquan, uquan, thisused;
  51.  
  52.              oquan = obj->quan;
  53.              uquan = (bp->useup ? bp->bquan : bp->bquan - oquan);
  54.              thisused = bp->price * uquan;
  55.              totused += thisused;
  56.              iflags.suppress_price++; /* suppress "(unpaid)" suffix */
  57.              /* Why 'x'?  To match `I x', more or less. */
  58.              buf_p = xprname(obj, (char *) 0, 'x', FALSE, thisused, uquan);
  59.              iflags.suppress_price--;
  60.              putstr(datawin, 0, buf_p);
  61.          }
  62.      }
  63.      if (eshkp->debit) {
  64.          /* additional shop debt which has no itemization available */
  65.          if (totused)
  66.              putstr(datawin, 0, "");
  67.          totused += eshkp->debit;
  68.          buf_p = xprname((struct obj *) 0, "usage charges and/or other fees",
  69.                          GOLD_SYM, FALSE, eshkp->debit, 0L);
  70.          putstr(datawin, 0, buf_p);
  71.      }
  72.      buf_p = xprname((struct obj *) 0, "Total:", '*', FALSE, totused, 0L);
  73.      putstr(datawin, 0, "");
  74.      putstr(datawin, 0, buf_p);
  75.      display_nhwindow(datawin, FALSE);
  76.  quit:
  77.      destroy_nhwindow(datawin);
  78.      return 0;
  79.  }
  80.  

getprice

  1.  #define HUNGRY 2
  2.  
  3.  STATIC_OVL long
  4.  getprice(obj, shk_buying)
  5.  register struct obj *obj;
  6.  boolean shk_buying;
  7.  {
  8.      register long tmp = (long) objects[obj->otyp].oc_cost;
  9.  
  10.      if (obj->oartifact) {
  11.          tmp = arti_cost(obj);
  12.          if (shk_buying)
  13.              tmp /= 4;
  14.      }
  15.      switch (obj->oclass) {
  16.      case FOOD_CLASS:
  17.          /* simpler hunger check, (2-4)*cost */
  18.          if (u.uhs >= HUNGRY && !shk_buying)
  19.              tmp *= (long) u.uhs;
  20.          if (obj->oeaten)
  21.              tmp = 0L;
  22.          break;
  23.      case WAND_CLASS:
  24.          if (obj->spe == -1)
  25.              tmp = 0L;
  26.          break;
  27.      case POTION_CLASS:
  28.          if (obj->otyp == POT_WATER && !obj->blessed && !obj->cursed)
  29.              tmp = 0L;
  30.          break;
  31.      case ARMOR_CLASS:
  32.      case WEAPON_CLASS:
  33.          if (obj->spe > 0)
  34.              tmp += 10L * (long) obj->spe;
  35.          break;
  36.      case TOOL_CLASS:
  37.          if (Is_candle(obj)
  38.              && obj->age < 20L * (long) objects[obj->otyp].oc_cost)
  39.              tmp /= 2L;
  40.          break;
  41.      }
  42.      return tmp;
  43.  }
  44.  

shkcatch

  1.  /* shk catches thrown pick-axe */
  2.  struct monst *
  3.  shkcatch(obj, x, y)
  4.  register struct obj *obj;
  5.  register xchar x, y;
  6.  {
  7.      register struct monst *shkp;
  8.  
  9.      if (!(shkp = shop_keeper(inside_shop(x, y))) || !inhishop(shkp))
  10.          return 0;
  11.  
  12.      if (shkp->mcanmove && !shkp->msleeping
  13.          && (*u.ushops != ESHK(shkp)->shoproom || !inside_shop(u.ux, u.uy))
  14.          && dist2(shkp->mx, shkp->my, x, y) < 3
  15.          /* if it is the shk's pos, you hit and anger him */
  16.          && (shkp->mx != x || shkp->my != y)) {
  17.          if (mnearto(shkp, x, y, TRUE) && !muteshk(shkp))
  18.              verbalize("Out of my way, scum!");
  19.          if (cansee(x, y)) {
  20.              pline("%s nimbly%s catches %s.", Monnam(shkp),
  21.                    (x == shkp->mx && y == shkp->my) ? "" : " reaches over and",
  22.                    the(xname(obj)));
  23.              if (!canspotmon(shkp))
  24.                  map_invisible(x, y);
  25.              delay_output();
  26.              mark_synch();
  27.          }
  28.          subfrombill(obj, shkp);
  29.          (void) mpickobj(shkp, obj);
  30.          return shkp;
  31.      }
  32.      return (struct monst *) 0;
  33.  }
  34.  

add_damage

  1.  void
  2.  add_damage(x, y, cost)
  3.  register xchar x, y;
  4.  long cost;
  5.  {
  6.      struct damage *tmp_dam;
  7.      char *shops;
  8.  
  9.      if (IS_DOOR(levl[x][y].typ)) {
  10.          struct monst *mtmp;
  11.  
  12.          /* Don't schedule for repair unless it's a real shop entrance */
  13.          for (shops = in_rooms(x, y, SHOPBASE); *shops; shops++)
  14.              if ((mtmp = shop_keeper(*shops)) != 0 && x == ESHK(mtmp)->shd.x
  15.                  && y == ESHK(mtmp)->shd.y)
  16.                  break;
  17.          if (!*shops)
  18.              return;
  19.      }
  20.      for (tmp_dam = level.damagelist; tmp_dam; tmp_dam = tmp_dam->next)
  21.          if (tmp_dam->place.x == x && tmp_dam->place.y == y) {
  22.              tmp_dam->cost += cost;
  23.              return;
  24.          }
  25.      tmp_dam = (struct damage *) alloc((unsigned) sizeof(struct damage));
  26.      tmp_dam->when = monstermoves;
  27.      tmp_dam->place.x = x;
  28.      tmp_dam->place.y = y;
  29.      tmp_dam->cost = cost;
  30.      tmp_dam->typ = levl[x][y].typ;
  31.      tmp_dam->next = level.damagelist;
  32.      level.damagelist = tmp_dam;
  33.      /* If player saw damage, display as a wall forever */
  34.      if (cansee(x, y))
  35.          levl[x][y].seenv = SVALL;
  36.  }
  37.  

remove_damage

  1.  /*
  2.   * Do something about damage. Either (!croaked) try to repair it, or
  3.   * (croaked) just discard damage structs for non-shared locations, since
  4.   * they'll never get repaired. Assume that shared locations will get
  5.   * repaired eventually by the other shopkeeper(s). This might be an erroneous
  6.   * assumption (they might all be dead too), but we have no reasonable way of
  7.   * telling that.
  8.   */
  9.  STATIC_OVL
  10.  void
  11.  remove_damage(shkp, croaked)
  12.  struct monst *shkp;
  13.  boolean croaked;
  14.  {
  15.      struct damage *tmp_dam, *tmp2_dam;
  16.      boolean did_repair = FALSE, saw_door = FALSE, saw_floor = FALSE,
  17.              stop_picking = FALSE, doorway_trap = FALSE;
  18.      int saw_walls = 0, saw_untrap = 0;
  19.      char trapmsg[BUFSZ];
  20.  
  21.      tmp_dam = level.damagelist;
  22.      tmp2_dam = 0;
  23.      while (tmp_dam) {
  24.          register xchar x = tmp_dam->place.x, y = tmp_dam->place.y;
  25.          char shops[5];
  26.          int disposition;
  27.          unsigned old_doormask = 0;
  28.  
  29.          disposition = 0;
  30.          Strcpy(shops, in_rooms(x, y, SHOPBASE));
  31.          if (index(shops, ESHK(shkp)->shoproom)) {
  32.              if (IS_DOOR(levl[x][y].typ))
  33.                  old_doormask = levl[x][y].doormask;
  34.  
  35.              if (croaked)
  36.                  disposition = (shops[1]) ? 0 : 1;
  37.              else if (stop_picking)
  38.                  disposition = repair_damage(shkp, tmp_dam, FALSE);
  39.              else {
  40.                  /* Defer the stop_occupation() until after repair msgs */
  41.                  if (closed_door(x, y))
  42.                      stop_picking = picking_at(x, y);
  43.                  disposition = repair_damage(shkp, tmp_dam, FALSE);
  44.                  if (!disposition)
  45.                      stop_picking = FALSE;
  46.              }
  47.          }
  48.  
  49.          if (!disposition) {
  50.              tmp2_dam = tmp_dam;
  51.              tmp_dam = tmp_dam->next;
  52.              continue;
  53.          }
  54.  
  55.          if (disposition > 1) {
  56.              did_repair = TRUE;
  57.              if (cansee(x, y)) {
  58.                  if (IS_WALL(levl[x][y].typ)) {
  59.                      saw_walls++;
  60.                  } else if (IS_DOOR(levl[x][y].typ)
  61.                             /* an existing door here implies trap removal */
  62.                             && !(old_doormask & (D_ISOPEN | D_CLOSED))) {
  63.                      saw_door = TRUE;
  64.                  } else if (disposition == 3) { /* untrapped */
  65.                      saw_untrap++;
  66.                      if (IS_DOOR(levl[x][y].typ))
  67.                          doorway_trap = TRUE;
  68.                  } else {
  69.                      saw_floor = TRUE;
  70.                  }
  71.              }
  72.          }
  73.  
  74.          tmp_dam = tmp_dam->next;
  75.          if (!tmp2_dam) {
  76.              free((genericptr_t) level.damagelist);
  77.              level.damagelist = tmp_dam;
  78.          } else {
  79.              free((genericptr_t) tmp2_dam->next);
  80.              tmp2_dam->next = tmp_dam;
  81.          }
  82.      }
  83.      if (!did_repair)
  84.          return;
  85.  
  86.      if (saw_untrap) {
  87.          Sprintf(trapmsg, "%s trap%s",
  88.                  (saw_untrap > 3) ? "several" : (saw_untrap > 1) ? "some"
  89.                                                                  : "a",
  90.                  plur(saw_untrap));
  91.          Sprintf(eos(trapmsg), " %s", vtense(trapmsg, "are"));
  92.          Sprintf(eos(trapmsg), " removed from the %s",
  93.                  (doorway_trap && saw_untrap == 1) ? "doorway" : "floor");
  94.      } else
  95.          trapmsg[0] = '\0'; /* not just lint suppression... */
  96.  
  97.      if (saw_walls) {
  98.          char wallbuf[BUFSZ];
  99.  
  100.          Sprintf(wallbuf, "section%s", plur(saw_walls));
  101.          pline("Suddenly, %s %s of wall %s up!",
  102.                (saw_walls == 1) ? "a" : (saw_walls <= 3) ? "some" : "several",
  103.                wallbuf, vtense(wallbuf, "close"));
  104.  
  105.          if (saw_door)
  106.              pline_The("shop door reappears!");
  107.          if (saw_floor)
  108.              pline_The("floor is repaired!");
  109.          if (saw_untrap)
  110.              pline("%s!", upstart(trapmsg));
  111.      } else {
  112.          if (saw_door || saw_floor || saw_untrap)
  113.              pline("Suddenly, %s%s%s%s%s!",
  114.                    saw_door ? "the shop door reappears" : "",
  115.                    (saw_door && saw_floor) ? " and " : "",
  116.                    saw_floor ? "the floor damage is gone" : "",
  117.                    ((saw_door || saw_floor) && *trapmsg) ? " and " : "",
  118.                    trapmsg);
  119.          else if (inside_shop(u.ux, u.uy) == ESHK(shkp)->shoproom)
  120.              You_feel("more claustrophobic than before.");
  121.          else if (!Deaf && !rn2(10))
  122.              Norep("The dungeon acoustics noticeably change.");
  123.      }
  124.      if (stop_picking)
  125.          stop_occupation();
  126.  }
  127.  

repair_damage

  1.  /*
  2.   * 0: repair postponed, 1: silent repair (no messages), 2: normal repair
  3.   * 3: untrap
  4.   */
  5.  int
  6.  repair_damage(shkp, tmp_dam, catchup)
  7.  register struct monst *shkp;
  8.  register struct damage *tmp_dam;
  9.  boolean catchup; /* restoring a level */
  10.  {
  11.      register xchar x, y, i;
  12.      xchar litter[9];
  13.      register struct monst *mtmp;
  14.      register struct obj *otmp;
  15.      register struct trap *ttmp;
  16.  
  17.      if ((monstermoves - tmp_dam->when) < REPAIR_DELAY)
  18.          return 0;
  19.      if (shkp->msleeping || !shkp->mcanmove || ESHK(shkp)->following)
  20.          return 0;
  21.      x = tmp_dam->place.x;
  22.      y = tmp_dam->place.y;
  23.      if (!IS_ROOM(tmp_dam->typ)) {
  24.          if (x == u.ux && y == u.uy)
  25.              if (!Passes_walls)
  26.                  return 0;
  27.          if (x == shkp->mx && y == shkp->my)
  28.              return 0;
  29.          if ((mtmp = m_at(x, y)) && (!passes_walls(mtmp->data)))
  30.              return 0;
  31.      }
  32.      if ((ttmp = t_at(x, y)) != 0) {
  33.          if (x == u.ux && y == u.uy)
  34.              if (!Passes_walls)
  35.                  return 0;
  36.          if (ttmp->ttyp == LANDMINE || ttmp->ttyp == BEAR_TRAP) {
  37.              /* convert to an object */
  38.              otmp = mksobj((ttmp->ttyp == LANDMINE) ? LAND_MINE : BEARTRAP,
  39.                            TRUE, FALSE);
  40.              otmp->quan = 1L;
  41.              otmp->owt = weight(otmp);
  42.              (void) mpickobj(shkp, otmp);
  43.          }
  44.          deltrap(ttmp);
  45.          if (IS_DOOR(tmp_dam->typ) && !(levl[x][y].doormask & D_ISOPEN)) {
  46.              levl[x][y].doormask = D_CLOSED;
  47.              block_point(x, y);
  48.          } else if (IS_WALL(tmp_dam->typ)) {
  49.              levl[x][y].typ = tmp_dam->typ;
  50.              block_point(x, y);
  51.          }
  52.          newsym(x, y);
  53.          return 3;
  54.      }
  55.      if (IS_ROOM(tmp_dam->typ)) {
  56.          /* No messages, because player already filled trap door */
  57.          return 1;
  58.      }
  59.      if ((tmp_dam->typ == levl[x][y].typ)
  60.          && (!IS_DOOR(tmp_dam->typ) || (levl[x][y].doormask > D_BROKEN)))
  61.          /* No messages if player already replaced shop door */
  62.          return 1;
  63.      levl[x][y].typ = tmp_dam->typ;
  64.      (void) memset((genericptr_t) litter, 0, sizeof(litter));
  65.      if ((otmp = level.objects[x][y]) != 0) {
  66.  /* Scatter objects haphazardly into the shop */
  67.  #define NEED_UPDATE 1
  68.  #define OPEN 2
  69.  #define INSHOP 4
  70.  #define horiz(i) ((i % 3) - 1)
  71.  #define vert(i) ((i / 3) - 1)
  72.          for (i = 0; i < 9; i++) {
  73.              if ((i == 4) || (!ZAP_POS(levl[x + horiz(i)][y + vert(i)].typ)))
  74.                  continue;
  75.              litter[i] = OPEN;
  76.              if (inside_shop(x + horiz(i), y + vert(i))
  77.                  == ESHK(shkp)->shoproom)
  78.                  litter[i] |= INSHOP;
  79.          }
  80.          if (Punished && !u.uswallow
  81.              && ((uchain->ox == x && uchain->oy == y)
  82.                  || (uball->ox == x && uball->oy == y))) {
  83.              /*
  84.               * Either the ball or chain is in the repair location.
  85.               *
  86.               * Take the easy way out and put ball&chain under hero.
  87.               */
  88.              if (!muteshk(shkp))
  89.                  verbalize("Get your junk out of my wall!");
  90.              unplacebc(); /* pick 'em up */
  91.              placebc();   /* put 'em down */
  92.          }
  93.          while ((otmp = level.objects[x][y]) != 0)
  94.              /* Don't mess w/ boulders -- just merge into wall */
  95.              if ((otmp->otyp == BOULDER) || (otmp->otyp == ROCK)) {
  96.                  obj_extract_self(otmp);
  97.                  obfree(otmp, (struct obj *) 0);
  98.              } else {
  99.                  while (!(litter[i = rn2(9)] & INSHOP))
  100.                      ;
  101.                  remove_object(otmp);
  102.                  place_object(otmp, x + horiz(i), y + vert(i));
  103.                  litter[i] |= NEED_UPDATE;
  104.              }
  105.      }
  106.      if (catchup)
  107.          return 1; /* repair occurred while off level */
  108.  
  109.      block_point(x, y);
  110.      if (IS_DOOR(tmp_dam->typ)) {
  111.          levl[x][y].doormask = D_CLOSED; /* arbitrary */
  112.          newsym(x, y);
  113.      } else {
  114.          /* don't set doormask  - it is (hopefully) the same as it was
  115.             if not, perhaps save it with the damage array... */
  116.  
  117.          if (IS_WALL(tmp_dam->typ) && cansee(x, y)) {
  118.              /* Player sees actual repair process, so they KNOW it's a wall */
  119.              levl[x][y].seenv = SVALL;
  120.              newsym(x, y);
  121.          }
  122.          /* Mark this wall as "repaired".  There currently is no code
  123.             to do anything about repaired walls, so don't do it. */
  124.      }
  125.      for (i = 0; i < 9; i++)
  126.          if (litter[i] & NEED_UPDATE)
  127.              newsym(x + horiz(i), y + vert(i));
  128.      return 2;
  129.  #undef NEED_UPDATE
  130.  #undef OPEN
  131.  #undef INSHOP
  132.  #undef vert
  133.  #undef horiz
  134.  }
  135.  

shk_move

  1.  /*
  2.   * shk_move: return 1: moved  0: didn't  -1: let m_move do it  -2: died
  3.   */
  4.  int
  5.  shk_move(shkp)
  6.  register struct monst *shkp;
  7.  {
  8.      register xchar gx, gy, omx, omy;
  9.      register int udist;
  10.      register schar appr;
  11.      register struct eshk *eshkp = ESHK(shkp);
  12.      int z;
  13.      boolean uondoor = FALSE, satdoor, avoid = FALSE, badinv;
  14.  
  15.      omx = shkp->mx;
  16.      omy = shkp->my;
  17.  
  18.      if (inhishop(shkp))
  19.          remove_damage(shkp, FALSE);
  20.  
  21.      if ((udist = distu(omx, omy)) < 3 && (shkp->data != &mons[PM_GRID_BUG]
  22.                                            || (omx == u.ux || omy == u.uy))) {
  23.          if (ANGRY(shkp) || (Conflict && !resist(shkp, RING_CLASS, 0, 0))) {
  24.              if (Displaced)
  25.                  Your("displaced image doesn't fool %s!", mon_nam(shkp));
  26.              (void) mattacku(shkp);
  27.              return 0;
  28.          }
  29.          if (eshkp->following) {
  30.              if (strncmp(eshkp->customer, plname, PL_NSIZ)) {
  31.                  if (!muteshk(shkp))
  32.                      verbalize("%s, %s!  I was looking for %s.", Hello(shkp),
  33.                                plname, eshkp->customer);
  34.                  eshkp->following = 0;
  35.                  return 0;
  36.              }
  37.              if (moves > followmsg + 4) {
  38.                  if (!muteshk(shkp))
  39.                      verbalize("%s, %s!  Didn't you forget to pay?",
  40.                                Hello(shkp), plname);
  41.                  followmsg = moves;
  42.                  if (!rn2(9)) {
  43.                      pline("%s doesn't like customers who don't pay.",
  44.                            Monnam(shkp));
  45.                      rile_shk(shkp);
  46.                  }
  47.              }
  48.              if (udist < 2)
  49.                  return 0;
  50.          }
  51.      }
  52.  
  53.      appr = 1;
  54.      gx = eshkp->shk.x;
  55.      gy = eshkp->shk.y;
  56.      satdoor = (gx == omx && gy == omy);
  57.      if (eshkp->following || ((z = holetime()) >= 0 && z * z <= udist)) {
  58.          /* [This distance check used to apply regardless of
  59.              whether the shk was following, but that resulted in
  60.              m_move() sometimes taking the shk out of the shop if
  61.              the player had fenced him in with boulders or traps.
  62.              Such voluntary abandonment left unpaid objects in
  63.              invent, triggering billing impossibilities on the
  64.              next level once the character fell through the hole.] */
  65.          if (udist > 4 && eshkp->following && !eshkp->billct)
  66.              return -1; /* leave it to m_move */
  67.          gx = u.ux;
  68.          gy = u.uy;
  69.      } else if (ANGRY(shkp)) {
  70.          /* Move towards the hero if the shopkeeper can see him. */
  71.          if (shkp->mcansee && m_canseeu(shkp)) {
  72.              gx = u.ux;
  73.              gy = u.uy;
  74.          }
  75.          avoid = FALSE;
  76.      } else {
  77.  #define GDIST(x, y) (dist2(x, y, gx, gy))
  78.          if (Invis || u.usteed) {
  79.              avoid = FALSE;
  80.          } else {
  81.              uondoor = (u.ux == eshkp->shd.x && u.uy == eshkp->shd.y);
  82.              if (uondoor) {
  83.                  badinv =
  84.                      (carrying(PICK_AXE) || carrying(DWARVISH_MATTOCK)
  85.                       || (Fast && (sobj_at(PICK_AXE, u.ux, u.uy)
  86.                                    || sobj_at(DWARVISH_MATTOCK, u.ux, u.uy))));
  87.                  if (satdoor && badinv)
  88.                      return 0;
  89.                  avoid = !badinv;
  90.              } else {
  91.                  avoid = (*u.ushops && distu(gx, gy) > 8);
  92.                  badinv = FALSE;
  93.              }
  94.  
  95.              if (((!eshkp->robbed && !eshkp->billct && !eshkp->debit) || avoid)
  96.                  && GDIST(omx, omy) < 3) {
  97.                  if (!badinv && !onlineu(omx, omy))
  98.                      return 0;
  99.                  if (satdoor)
  100.                      appr = gx = gy = 0;
  101.              }
  102.          }
  103.      }
  104.  
  105.      z = move_special(shkp, inhishop(shkp), appr, uondoor, avoid, omx, omy, gx,
  106.                       gy);
  107.      if (z > 0)
  108.          after_shk_move(shkp);
  109.  
  110.      return z;
  111.  }
  112.  

after_shk_move

  1.  /* called after shopkeeper moves, in case themove causes re-entry into shop */
  2.  void
  3.  after_shk_move(shkp)
  4.  struct monst *shkp;
  5.  {
  6.      struct eshk *eshkp = ESHK(shkp);
  7.  
  8.      if (eshkp->bill_p == (struct bill_x *) -1000 && inhishop(shkp)) {
  9.          /* reset bill_p, need to re-calc player's occupancy too */
  10.          eshkp->bill_p = &eshkp->bill[0];
  11.          check_special_room(FALSE);
  12.      }
  13.  }
  14.  

is_fshk

  1.  /* for use in levl_follower (mondata.c) */
  2.  boolean
  3.  is_fshk(mtmp)
  4.  register struct monst *mtmp;
  5.  {
  6.      return (boolean) (mtmp->isshk && ESHK(mtmp)->following);
  7.  }
  8.  

shopdig

  1.  /* You are digging in the shop. */
  2.  void
  3.  shopdig(fall)
  4.  register int fall;
  5.  {
  6.      register struct monst *shkp = shop_keeper(*u.ushops);
  7.      int lang;
  8.      const char *grabs = "grabs";
  9.  
  10.      if (!shkp)
  11.          return;
  12.  
  13.      /* 0 == can't speak, 1 == makes animal noises, 2 == speaks */
  14.      lang = 0;
  15.      if (shkp->msleeping || !shkp->mcanmove || is_silent(shkp->data))
  16.          ; /* lang stays 0 */
  17.      else if (shkp->data->msound <= MS_ANIMAL)
  18.          lang = 1;
  19.      else if (shkp->data->msound >= MS_HUMANOID)
  20.          lang = 2;
  21.  
  22.      if (!inhishop(shkp)) {
  23.          if (Role_if(PM_KNIGHT)) {
  24.              You_feel("like a common thief.");
  25.              adjalign(-sgn(u.ualign.type));
  26.          }
  27.          return;
  28.      }
  29.  
  30.      if (!fall) {
  31.          if (lang == 2) {
  32.              if (u.utraptype == TT_PIT)
  33.                  verbalize(
  34.                      "Be careful, %s, or you might fall through the floor.",
  35.                      flags.female ? "madam" : "sir");
  36.              else
  37.                  verbalize("%s, do not damage the floor here!",
  38.                            flags.female ? "Madam" : "Sir");
  39.          }
  40.          if (Role_if(PM_KNIGHT)) {
  41.              You_feel("like a common thief.");
  42.              adjalign(-sgn(u.ualign.type));
  43.          }
  44.      } else if (!um_dist(shkp->mx, shkp->my, 5)
  45.                 && !shkp->msleeping && shkp->mcanmove
  46.                 && (ESHK(shkp)->billct || ESHK(shkp)->debit)) {
  47.          register struct obj *obj, *obj2;
  48.  
  49.          if (nolimbs(shkp->data)) {
  50.              grabs = "knocks off";
  51.  #if 0
  52.              /* This is what should happen, but for balance
  53.               * reasons, it isn't currently.
  54.               */
  55.              if (lang == 2)
  56.                  pline("%s curses %s inability to grab your backpack!",
  57.                        shkname(shkp), mhim(shkp));
  58.              rile_shk(shkp);
  59.              return;
  60.  #endif
  61.          }
  62.          if (distu(shkp->mx, shkp->my) > 2) {
  63.              mnexto(shkp);
  64.              /* for some reason the shopkeeper can't come next to you */
  65.              if (distu(shkp->mx, shkp->my) > 2) {
  66.                  if (lang == 2)
  67.                      pline("%s curses you in anger and frustration!",
  68.                            shkname(shkp));
  69.                  else if (lang == 1)
  70.                      growl(shkp);
  71.                  rile_shk(shkp);
  72.                  return;
  73.              } else
  74.                  pline("%s %s, and %s your backpack!", shkname(shkp),
  75.                        makeplural(locomotion(shkp->data, "leap")), grabs);
  76.          } else
  77.              pline("%s %s your backpack!", shkname(shkp), grabs);
  78.  
  79.          for (obj = invent; obj; obj = obj2) {
  80.              obj2 = obj->nobj;
  81.              if ((obj->owornmask & ~(W_SWAPWEP | W_QUIVER)) != 0
  82.                  || (obj == uswapwep && u.twoweap)
  83.                  || (obj->otyp == LEASH && obj->leashmon))
  84.                  continue;
  85.              if (obj == current_wand)
  86.                  continue;
  87.              setnotworn(obj);
  88.              freeinv(obj);
  89.              subfrombill(obj, shkp);
  90.              (void) add_to_minv(shkp, obj); /* may free obj */
  91.          }
  92.      }
  93.  }
  94.  

makekops

  1.  STATIC_OVL void
  2.  makekops(mm)
  3.  coord *mm;
  4.  {
  5.      static const short k_mndx[4] = { PM_KEYSTONE_KOP, PM_KOP_SERGEANT,
  6.                                       PM_KOP_LIEUTENANT, PM_KOP_KAPTAIN };
  7.      int k_cnt[4], cnt, mndx, k;
  8.  
  9.      k_cnt[0] = cnt = abs(depth(&u.uz)) + rnd(5);
  10.      k_cnt[1] = (cnt / 3) + 1; /* at least one sarge */
  11.      k_cnt[2] = (cnt / 6);     /* maybe a lieutenant */
  12.      k_cnt[3] = (cnt / 9);     /* and maybe a kaptain */
  13.  
  14.      for (k = 0; k < 4; k++) {
  15.          if ((cnt = k_cnt[k]) == 0)
  16.              break;
  17.          mndx = k_mndx[k];
  18.          if (mvitals[mndx].mvflags & G_GONE)
  19.              continue;
  20.  
  21.          while (cnt--)
  22.              if (enexto(mm, mm->x, mm->y, &mons[mndx]))
  23.                  (void) makemon(&mons[mndx], mm->x, mm->y, NO_MM_FLAGS);
  24.      }
  25.  }
  26.  

pay_for_damage

  1.  void
  2.  pay_for_damage(dmgstr, cant_mollify)
  3.  const char *dmgstr;
  4.  boolean cant_mollify;
  5.  {
  6.      register struct monst *shkp = (struct monst *) 0;
  7.      char shops_affected[5];
  8.      register boolean uinshp = (*u.ushops != '\0');
  9.      char qbuf[80];
  10.      register xchar x, y;
  11.      boolean dugwall = (!strcmp(dmgstr, "dig into")    /* wand */
  12.                         || !strcmp(dmgstr, "damage")); /* pick-axe */
  13.      boolean animal, pursue;
  14.      struct damage *tmp_dam, *appear_here = 0;
  15.      /* any number >= (80*80)+(24*24) would do, actually */
  16.      long cost_of_damage = 0L;
  17.      unsigned int nearest_shk = 7000, nearest_damage = 7000;
  18.      int picks = 0;
  19.  
  20.      for (tmp_dam = level.damagelist;
  21.           (tmp_dam && (tmp_dam->when == monstermoves));
  22.           tmp_dam = tmp_dam->next) {
  23.          char *shp;
  24.  
  25.          if (!tmp_dam->cost)
  26.              continue;
  27.          cost_of_damage += tmp_dam->cost;
  28.          Strcpy(shops_affected,
  29.                 in_rooms(tmp_dam->place.x, tmp_dam->place.y, SHOPBASE));
  30.          for (shp = shops_affected; *shp; shp++) {
  31.              struct monst *tmp_shk;
  32.              unsigned int shk_distance;
  33.  
  34.              if (!(tmp_shk = shop_keeper(*shp)))
  35.                  continue;
  36.              if (tmp_shk == shkp) {
  37.                  unsigned int damage_distance =
  38.                      distu(tmp_dam->place.x, tmp_dam->place.y);
  39.  
  40.                  if (damage_distance < nearest_damage) {
  41.                      nearest_damage = damage_distance;
  42.                      appear_here = tmp_dam;
  43.                  }
  44.                  continue;
  45.              }
  46.              if (!inhishop(tmp_shk))
  47.                  continue;
  48.              shk_distance = distu(tmp_shk->mx, tmp_shk->my);
  49.              if (shk_distance > nearest_shk)
  50.                  continue;
  51.              if ((shk_distance == nearest_shk) && picks) {
  52.                  if (rn2(++picks))
  53.                      continue;
  54.              } else
  55.                  picks = 1;
  56.              shkp = tmp_shk;
  57.              nearest_shk = shk_distance;
  58.              appear_here = tmp_dam;
  59.              nearest_damage = distu(tmp_dam->place.x, tmp_dam->place.y);
  60.          }
  61.      }
  62.  
  63.      if (!cost_of_damage || !shkp)
  64.          return;
  65.  
  66.      animal = (shkp->data->msound <= MS_ANIMAL);
  67.      pursue = FALSE;
  68.      x = appear_here->place.x;
  69.      y = appear_here->place.y;
  70.  
  71.      /* not the best introduction to the shk... */
  72.      (void) strncpy(ESHK(shkp)->customer, plname, PL_NSIZ);
  73.  
  74.      /* if the shk is already on the war path, be sure it's all out */
  75.      if (ANGRY(shkp) || ESHK(shkp)->following) {
  76.          hot_pursuit(shkp);
  77.          return;
  78.      }
  79.  
  80.      /* if the shk is not in their shop.. */
  81.      if (!*in_rooms(shkp->mx, shkp->my, SHOPBASE)) {
  82.          if (!cansee(shkp->mx, shkp->my))
  83.              return;
  84.          pursue = TRUE;
  85.          goto getcad;
  86.      }
  87.  
  88.      if (uinshp) {
  89.          if (um_dist(shkp->mx, shkp->my, 1)
  90.              && !um_dist(shkp->mx, shkp->my, 3)) {
  91.              pline("%s leaps towards you!", shkname(shkp));
  92.              mnexto(shkp);
  93.          }
  94.          pursue = um_dist(shkp->mx, shkp->my, 1);
  95.          if (pursue)
  96.              goto getcad;
  97.      } else {
  98.          /*
  99.           * Make shkp show up at the door.  Effect:  If there is a monster
  100.           * in the doorway, have the hero hear the shopkeeper yell a bit,
  101.           * pause, then have the shopkeeper appear at the door, having
  102.           * yanked the hapless critter out of the way.
  103.           */
  104.          if (MON_AT(x, y)) {
  105.              if (!Deaf && !animal) {
  106.                  You_hear("an angry voice:");
  107.                  verbalize("Out of my way, scum!");
  108.                  wait_synch();
  109.  #if defined(UNIX) || defined(VMS)
  110.  #if defined(SYSV) || defined(ULTRIX) || defined(VMS)
  111.                  (void)
  112.  #endif
  113.                      sleep(1);
  114.  #endif
  115.              } else {
  116.                  growl(shkp);
  117.              }
  118.          }
  119.          (void) mnearto(shkp, x, y, TRUE);
  120.      }
  121.  
  122.      if ((um_dist(x, y, 1) && !uinshp) || cant_mollify
  123.          || (money_cnt(invent) + ESHK(shkp)->credit) < cost_of_damage
  124.          || !rn2(50)) {
  125.      getcad:
  126.          if (muteshk(shkp)) {
  127.              if (animal && shkp->mcanmove && !shkp->msleeping)
  128.                  yelp(shkp);
  129.          } else if (pursue || uinshp || !um_dist(x, y, 1)) {
  130.              verbalize("How dare you %s my %s?", dmgstr,
  131.                        dugwall ? "shop" : "door");
  132.          } else {
  133.              pline("%s shouts:", shkname(shkp));
  134.              verbalize("Who dared %s my %s?", dmgstr,
  135.                        dugwall ? "shop" : "door");
  136.          }
  137.          hot_pursuit(shkp);
  138.          return;
  139.      }
  140.  
  141.      if (Invis)
  142.          Your("invisibility does not fool %s!", shkname(shkp));
  143.      Sprintf(qbuf, "%sYou did %ld %s worth of damage!%s  Pay?",
  144.              !animal ? cad(TRUE) : "", cost_of_damage,
  145.              currency(cost_of_damage), !animal ? "\"" : "");
  146.      if (yn(qbuf) != 'n') {
  147.          cost_of_damage = check_credit(cost_of_damage, shkp);
  148.          money2mon(shkp, cost_of_damage);
  149.          context.botl = 1;
  150.          pline("Mollified, %s accepts your restitution.", shkname(shkp));
  151.          /* move shk back to his home loc */
  152.          home_shk(shkp, FALSE);
  153.          pacify_shk(shkp);
  154.      } else {
  155.          if (!animal)
  156.              verbalize("Oh, yes!  You'll pay!");
  157.          else
  158.              growl(shkp);
  159.          hot_pursuit(shkp);
  160.          adjalign(-sgn(u.ualign.type));
  161.      }
  162.  }
  163.  

costly_spot

  1.  /* called in dokick.c when we kick an object that might be in a store */
  2.  boolean
  3.  costly_spot(x, y)
  4.  register xchar x, y;
  5.  {
  6.      struct monst *shkp;
  7.      struct eshk *eshkp;
  8.  
  9.      if (!level.flags.has_shop)
  10.          return FALSE;
  11.      shkp = shop_keeper(*in_rooms(x, y, SHOPBASE));
  12.      if (!shkp || !inhishop(shkp))
  13.          return FALSE;
  14.      eshkp = ESHK(shkp);
  15.      return  (boolean) (inside_shop(x, y)
  16.                         && !(x == eshkp->shk.x && y == eshkp->shk.y));
  17.  }
  18.  

shop_object

  1.  /* called by dotalk(sounds.c) when #chatting; returns obj if location
  2.     contains shop goods and shopkeeper is willing & able to speak */
  3.  struct obj *
  4.  shop_object(x, y)
  5.  register xchar x, y;
  6.  {
  7.      register struct obj *otmp;
  8.      register struct monst *shkp;
  9.  
  10.      if (!(shkp = shop_keeper(*in_rooms(x, y, SHOPBASE))) || !inhishop(shkp))
  11.          return (struct obj *) 0;
  12.  
  13.      for (otmp = level.objects[x][y]; otmp; otmp = otmp->nexthere)
  14.          if (otmp->oclass != COIN_CLASS)
  15.              break;
  16.      /* note: otmp might have ->no_charge set, but that's ok */
  17.      return (otmp && costly_spot(x, y)
  18.              && NOTANGRY(shkp) && shkp->mcanmove && !shkp->msleeping)
  19.                 ? otmp
  20.                 : (struct obj *) 0;
  21.  }
  22.  

price_quote

  1.  /* give price quotes for all objects linked to this one (ie, on this spot) */
  2.  void
  3.  price_quote(first_obj)
  4.  register struct obj *first_obj;
  5.  {
  6.      register struct obj *otmp;
  7.      char buf[BUFSZ], price[40];
  8.      long cost = 0L;
  9.      int cnt = 0;
  10.      boolean contentsonly = FALSE;
  11.      winid tmpwin;
  12.      struct monst *shkp = shop_keeper(inside_shop(u.ux, u.uy));
  13.  
  14.      tmpwin = create_nhwindow(NHW_MENU);
  15.      putstr(tmpwin, 0, "Fine goods for sale:");
  16.      putstr(tmpwin, 0, "");
  17.      for (otmp = first_obj; otmp; otmp = otmp->nexthere) {
  18.          if (otmp->oclass == COIN_CLASS)
  19.              continue;
  20.          cost = (otmp->no_charge || otmp == uball || otmp == uchain)
  21.                     ? 0L
  22.                     : get_cost(otmp, (struct monst *) 0);
  23.          contentsonly = !cost;
  24.          if (Has_contents(otmp))
  25.              cost += contained_cost(otmp, shkp, 0L, FALSE, FALSE);
  26.          if (!cost) {
  27.              Strcpy(price, "no charge");
  28.              contentsonly = FALSE;
  29.          } else {
  30.              Sprintf(price, "%ld %s%s", cost, currency(cost),
  31.                      (otmp->quan) > 1L ? " each" : "");
  32.          }
  33.          Sprintf(buf, "%s%s, %s", contentsonly ? the_contents_of : "",
  34.                  doname(otmp), price);
  35.          putstr(tmpwin, 0, buf), cnt++;
  36.      }
  37.      if (cnt > 1) {
  38.          display_nhwindow(tmpwin, TRUE);
  39.      } else if (cnt == 1) {
  40.          if (!cost) {
  41.              /* "<doname(obj)>, no charge" */
  42.              pline("%s!", upstart(buf)); /* buf still contains the string */
  43.          } else {
  44.              /* print cost in slightly different format, so can't reuse buf;
  45.                 cost and contentsonly are already set up */
  46.              Sprintf(buf, "%s%s", contentsonly ? the_contents_of : "",
  47.                      doname(first_obj));
  48.              pline("%s, price %ld %s%s%s", upstart(buf), cost, currency(cost),
  49.                    (first_obj->quan > 1L) ? " each" : "",
  50.                    contentsonly ? "." : shk_embellish(first_obj, cost));
  51.          }
  52.      }
  53.      destroy_nhwindow(tmpwin);
  54.  }
  55.  

shk_embellish

  1.  STATIC_OVL const char *
  2.  shk_embellish(itm, cost)
  3.  register struct obj *itm;
  4.  long cost;
  5.  {
  6.      if (!rn2(3)) {
  7.          register int o, choice = rn2(5);
  8.          if (choice == 0)
  9.              choice = (cost < 100L ? 1 : cost < 500L ? 2 : 3);
  10.          switch (choice) {
  11.          case 4:
  12.              if (cost < 10L)
  13.                  break;
  14.              else
  15.                  o = itm->oclass;
  16.              if (o == FOOD_CLASS)
  17.                  return ", gourmets' delight!";
  18.              if (objects[itm->otyp].oc_name_known
  19.                      ? objects[itm->otyp].oc_magic
  20.                      : (o == AMULET_CLASS || o == RING_CLASS || o == WAND_CLASS
  21.                         || o == POTION_CLASS || o == SCROLL_CLASS
  22.                         || o == SPBOOK_CLASS))
  23.                  return ", painstakingly developed!";
  24.              return ", superb craftsmanship!";
  25.          case 3:
  26.              return ", finest quality.";
  27.          case 2:
  28.              return ", an excellent choice.";
  29.          case 1:
  30.              return ", a real bargain.";
  31.          default:
  32.              break;
  33.          }
  34.      } else if (itm->oartifact) {
  35.          return ", one of a kind!";
  36.      }
  37.      return ".";
  38.  }
  39.  

shk_chat

  1.  /* First 4 supplied by Ronen and Tamar, remainder by development team */
  2.  const char *Izchak_speaks[] = {
  3.      "%s says: 'These shopping malls give me a headache.'",
  4.      "%s says: 'Slow down.  Think clearly.'",
  5.      "%s says: 'You need to take things one at a time.'",
  6.      "%s says: 'I don't like poofy coffee... give me Columbian Supremo.'",
  7.      "%s says that getting the devteam's agreement on anything is difficult.",
  8.      "%s says that he has noticed those who serve their deity will prosper.",
  9.      "%s says: 'Don't try to steal from me - I have friends in high places!'",
  10.      "%s says: 'You may well need something from this shop in the future.'",
  11.      "%s comments about the Valley of the Dead as being a gateway."
  12.  };
  13.  
  14.  void
  15.  shk_chat(shkp)
  16.  struct monst *shkp;
  17.  {
  18.      struct eshk *eshk;
  19.      long shkmoney;
  20.      if (!shkp->isshk) {
  21.          /* The monster type is shopkeeper, but this monster is
  22.             not actually a shk, which could happen if someone
  23.             wishes for a shopkeeper statue and then animates it.
  24.             (Note: shkname() would be "" in a case like this.) */
  25.          pline("%s asks whether you've seen any untended shops recently.",
  26.                Monnam(shkp));
  27.          /* [Perhaps we ought to check whether this conversation
  28.             is taking place inside an untended shop, but a shopless
  29.             shk can probably be expected to be rather disoriented.] */
  30.          return;
  31.      }
  32.  
  33.      eshk = ESHK(shkp);
  34.      if (ANGRY(shkp)) {
  35.          pline("%s mentions how much %s dislikes %s customers.",
  36.                shkname(shkp), mhe(shkp), eshk->robbed ? "non-paying" : "rude");
  37.      } else if (eshk->following) {
  38.          if (strncmp(eshk->customer, plname, PL_NSIZ)) {
  39.              verbalize("%s %s!  I was looking for %s.",
  40.                        Hello(shkp), plname, eshk->customer);
  41.              eshk->following = 0;
  42.          } else {
  43.              verbalize("%s %s!  Didn't you forget to pay?",
  44.                        Hello(shkp), plname);
  45.          }
  46.      } else if (eshk->billct) {
  47.          register long total = addupbill(shkp) + eshk->debit;
  48.  
  49.          pline("%s says that your bill comes to %ld %s.",
  50.                shkname(shkp), total, currency(total));
  51.      } else if (eshk->debit) {
  52.          pline("%s reminds you that you owe %s %ld %s.",
  53.                shkname(shkp), mhim(shkp), eshk->debit, currency(eshk->debit));
  54.      } else if (eshk->credit) {
  55.          pline("%s encourages you to use your %ld %s of credit.",
  56.                shkname(shkp), eshk->credit, currency(eshk->credit));
  57.      } else if (eshk->robbed) {
  58.          pline("%s complains about a recent robbery.", shkname(shkp));
  59.      } else if ((shkmoney = money_cnt(shkp->minvent)) < 50) {
  60.          pline("%s complains that business is bad.", shkname(shkp));
  61.      } else if (shkmoney > 4000) {
  62.          pline("%s says that business is good.", shkname(shkp));
  63.      } else if (is_izchak(shkp, FALSE)) {
  64.          pline(Izchak_speaks[rn2(SIZE(Izchak_speaks))], shkname(shkp));
  65.      } else {
  66.          pline("%s talks about the problem of shoplifters.", shkname(shkp));
  67.      }
  68.  }
  69.  

kops_gone

  1.  STATIC_OVL void
  2.  kops_gone(silent)
  3.  boolean silent;
  4.  {
  5.      register int cnt = 0;
  6.      register struct monst *mtmp, *mtmp2;
  7.  
  8.      for (mtmp = fmon; mtmp; mtmp = mtmp2) {
  9.          mtmp2 = mtmp->nmon;
  10.          if (mtmp->data->mlet == S_KOP) {
  11.              if (canspotmon(mtmp))
  12.                  cnt++;
  13.              mongone(mtmp);
  14.          }
  15.      }
  16.      if (cnt && !silent)
  17.          pline_The("Kop%s (disappointed) vanish%s into thin air.",
  18.                    plur(cnt), (cnt == 1) ? "es" : "");
  19.  }
  20.  

cost_per_charge

  1.  STATIC_OVL long
  2.  cost_per_charge(shkp, otmp, altusage)
  3.  struct monst *shkp;
  4.  struct obj *otmp;
  5.  boolean altusage; /* some items have an "alternate" use with different cost */
  6.  {
  7.      long tmp = 0L;
  8.  
  9.      if (!shkp || !inhishop(shkp))
  10.          return 0L; /* insurance */
  11.      tmp = get_cost(otmp, shkp);
  12.  
  13.      /* The idea is to make the exhaustive use of an unpaid item
  14.       * more expensive than buying it outright.
  15.       */
  16.      if (otmp->otyp == MAGIC_LAMP) { /* 1 */
  17.          /* normal use (ie, as light source) of a magic lamp never
  18.             degrades its value, but not charging anything would make
  19.             identification too easy; charge an amount comparable to
  20.             what is charged for an ordinary lamp (don't bother with
  21.             angry shk surcharge) */
  22.          if (!altusage)
  23.              tmp = (long) objects[OIL_LAMP].oc_cost;
  24.          else
  25.              tmp += tmp / 3L;                 /* djinni is being released */
  26.      } else if (otmp->otyp == MAGIC_MARKER) { /* 70 - 100 */
  27.          /* No way to determine in advance how many charges will be
  28.           * wasted.  So, arbitrarily, one half of the price per use.
  29.           */
  30.          tmp /= 2L;
  31.      } else if (otmp->otyp == BAG_OF_TRICKS /* 1 - 20 */
  32.                 || otmp->otyp == HORN_OF_PLENTY) {
  33.          /* altusage: emptying of all the contents at once */
  34.          if (!altusage)
  35.              tmp /= 5L;
  36.      } else if (otmp->otyp == CRYSTAL_BALL               /* 1 - 5 */
  37.                 || otmp->otyp == OIL_LAMP                /* 1 - 10 */
  38.                 || otmp->otyp == BRASS_LANTERN
  39.                 || (otmp->otyp >= MAGIC_FLUTE
  40.                     && otmp->otyp <= DRUM_OF_EARTHQUAKE) /* 5 - 9 */
  41.                 || otmp->oclass == WAND_CLASS) {         /* 3 - 11 */
  42.          if (otmp->spe > 1)
  43.              tmp /= 4L;
  44.      } else if (otmp->oclass == SPBOOK_CLASS) {
  45.          tmp -= tmp / 5L;
  46.      } else if (otmp->otyp == CAN_OF_GREASE || otmp->otyp == TINNING_KIT
  47.                 || otmp->otyp == EXPENSIVE_CAMERA) {
  48.          tmp /= 10L;
  49.      } else if (otmp->otyp == POT_OIL) {
  50.          tmp /= 5L;
  51.      }
  52.      return tmp;
  53.  }
  54.  

check_unpaid_usage

  1.  /* Charge the player for partial use of an unpaid object.
  2.   *
  3.   * Note that bill_dummy_object() should be used instead
  4.   * when an object is completely used.
  5.   */
  6.  void
  7.  check_unpaid_usage(otmp, altusage)
  8.  struct obj *otmp;
  9.  boolean altusage;
  10.  {
  11.      struct monst *shkp;
  12.      const char *fmt, *arg1, *arg2;
  13.      char buf[BUFSZ];
  14.      long tmp;
  15.  
  16.      if (!otmp->unpaid || !*u.ushops
  17.          || (otmp->spe <= 0 && objects[otmp->otyp].oc_charged))
  18.          return;
  19.      if (!(shkp = shop_keeper(*u.ushops)) || !inhishop(shkp))
  20.          return;
  21.      if ((tmp = cost_per_charge(shkp, otmp, altusage)) == 0L)
  22.          return;
  23.  
  24.      arg1 = arg2 = "";
  25.      if (otmp->oclass == SPBOOK_CLASS) {
  26.          fmt = "%sYou owe%s %ld %s.";
  27.          Sprintf(buf, "This is no free library, %s!  ", cad(FALSE));
  28.          arg1 = rn2(2) ? buf : "";
  29.          arg2 = ESHK(shkp)->debit > 0L ? " an additional" : "";
  30.      } else if (otmp->otyp == POT_OIL) {
  31.          fmt = "%s%sThat will cost you %ld %s (Yendorian Fuel Tax).";
  32.      } else if (altusage && (otmp->otyp == BAG_OF_TRICKS
  33.                              || otmp->otyp == HORN_OF_PLENTY)) {
  34.          fmt = "%s%sEmptying that will cost you %ld %s.";
  35.          if (!rn2(3))
  36.              arg1 = "Whoa!  ";
  37.          if (!rn2(3))
  38.              arg1 = "Watch it!  ";
  39.      } else {
  40.          fmt = "%s%sUsage fee, %ld %s.";
  41.          if (!rn2(3))
  42.              arg1 = "Hey!  ";
  43.          if (!rn2(3))
  44.              arg2 = "Ahem.  ";
  45.      }
  46.  
  47.      if (!muteshk(shkp)) {
  48.          verbalize(fmt, arg1, arg2, tmp, currency(tmp));
  49.          exercise(A_WIS, TRUE); /* you just got info */
  50.      }
  51.      ESHK(shkp)->debit += tmp;
  52.  }
  53.  

check_unpaid

  1.  /* for using charges of unpaid objects "used in the normal manner" */
  2.  void
  3.  check_unpaid(otmp)
  4.  struct obj *otmp;
  5.  {
  6.      check_unpaid_usage(otmp, FALSE); /* normal item use */
  7.  }
  8.  

costly_gold

  1.  void
  2.  costly_gold(x, y, amount)
  3.  register xchar x, y;
  4.  register long amount;
  5.  {
  6.      register long delta;
  7.      register struct monst *shkp;
  8.      register struct eshk *eshkp;
  9.  
  10.      if (!costly_spot(x, y))
  11.          return;
  12.      /* shkp now guaranteed to exist by costly_spot() */
  13.      shkp = shop_keeper(*in_rooms(x, y, SHOPBASE));
  14.  
  15.      eshkp = ESHK(shkp);
  16.      if (eshkp->credit >= amount) {
  17.          if (eshkp->credit > amount)
  18.              Your("credit is reduced by %ld %s.", amount, currency(amount));
  19.          else
  20.              Your("credit is erased.");
  21.          eshkp->credit -= amount;
  22.      } else {
  23.          delta = amount - eshkp->credit;
  24.          if (eshkp->credit)
  25.              Your("credit is erased.");
  26.          if (eshkp->debit)
  27.              Your("debt increases by %ld %s.", delta, currency(delta));
  28.          else
  29.              You("owe %s %ld %s.", shkname(shkp), delta, currency(delta));
  30.          eshkp->debit += delta;
  31.          eshkp->loan += delta;
  32.          eshkp->credit = 0L;
  33.      }
  34.  }
  35.  

block_door

  1.  /* used in domove to block diagonal shop-exit */
  2.  /* x,y should always be a door */
  3.  boolean
  4.  block_door(x, y)
  5.  register xchar x, y;
  6.  {
  7.      register int roomno = *in_rooms(x, y, SHOPBASE);
  8.      register struct monst *shkp;
  9.  
  10.      if (roomno < 0 || !IS_SHOP(roomno))
  11.          return FALSE;
  12.      if (!IS_DOOR(levl[x][y].typ))
  13.          return FALSE;
  14.      if (roomno != *u.ushops)
  15.          return FALSE;
  16.  
  17.      if (!(shkp = shop_keeper((char) roomno)) || !inhishop(shkp))
  18.          return FALSE;
  19.  
  20.      if (shkp->mx == ESHK(shkp)->shk.x && shkp->my == ESHK(shkp)->shk.y
  21.          /* Actually, the shk should be made to block _any_
  22.           * door, including a door the player digs, if the
  23.           * shk is within a 'jumping' distance.
  24.           */
  25.          && ESHK(shkp)->shd.x == x
  26.          && ESHK(shkp)->shd.y == y
  27.          && shkp->mcanmove && !shkp->msleeping
  28.          && (ESHK(shkp)->debit || ESHK(shkp)->billct || ESHK(shkp)->robbed)) {
  29.          pline("%s%s blocks your way!", shkname(shkp),
  30.                Invis ? " senses your motion and" : "");
  31.          return TRUE;
  32.      }
  33.      return FALSE;
  34.  }
  35.  

block_entry

  1.  /* used in domove to block diagonal shop-entry;
  2.     u.ux, u.uy should always be a door */
  3.  boolean
  4.  block_entry(x, y)
  5.  register xchar x, y;
  6.  {
  7.      register xchar sx, sy;
  8.      register int roomno;
  9.      register struct monst *shkp;
  10.  
  11.      if (!(IS_DOOR(levl[u.ux][u.uy].typ)
  12.            && levl[u.ux][u.uy].doormask == D_BROKEN))
  13.          return FALSE;
  14.  
  15.      roomno = *in_rooms(x, y, SHOPBASE);
  16.      if (roomno < 0 || !IS_SHOP(roomno))
  17.          return FALSE;
  18.      if (!(shkp = shop_keeper((char) roomno)) || !inhishop(shkp))
  19.          return FALSE;
  20.  
  21.      if (ESHK(shkp)->shd.x != u.ux || ESHK(shkp)->shd.y != u.uy)
  22.          return FALSE;
  23.  
  24.      sx = ESHK(shkp)->shk.x;
  25.      sy = ESHK(shkp)->shk.y;
  26.  
  27.      if (shkp->mx == sx && shkp->my == sy && shkp->mcanmove && !shkp->msleeping
  28.          && (x == sx - 1 || x == sx + 1 || y == sy - 1 || y == sy + 1)
  29.          && (Invis || carrying(PICK_AXE) || carrying(DWARVISH_MATTOCK)
  30.              || u.usteed)) {
  31.          pline("%s%s blocks your way!", shkname(shkp),
  32.                Invis ? " senses your motion and" : "");
  33.          return TRUE;
  34.      }
  35.      return FALSE;
  36.  }
  37.  

shk_your

  1.  /* "your " or "Foobar's " (note the trailing space) */
  2.  char *
  3.  shk_your(buf, obj)
  4.  char *buf;
  5.  struct obj *obj;
  6.  {
  7.      if (!shk_owns(buf, obj) && !mon_owns(buf, obj))
  8.          Strcpy(buf, the_your[carried(obj) ? 1 : 0]);
  9.      return strcat(buf, " ");
  10.  }
  11.  

Shk_Your

  1.  char *
  2.  Shk_Your(buf, obj)
  3.  char *buf;
  4.  struct obj *obj;
  5.  {
  6.      (void) shk_your(buf, obj);
  7.      *buf = highc(*buf);
  8.      return buf;
  9.  }
  10.  

shk_owns

  1.  STATIC_OVL char *
  2.  shk_owns(buf, obj)
  3.  char *buf;
  4.  struct obj *obj;
  5.  {
  6.      struct monst *shkp;
  7.      xchar x, y;
  8.  
  9.      if (get_obj_location(obj, &x, &y, 0)
  10.          && (obj->unpaid || (obj->where == OBJ_FLOOR && !obj->no_charge
  11.                              && costly_spot(x, y)))) {
  12.          shkp = shop_keeper(inside_shop(x, y));
  13.          return strcpy(buf, shkp ? s_suffix(shkname(shkp)) : the_your[0]);
  14.      }
  15.      return (char *) 0;
  16.  }
  17.  

mon_owns

  1.  STATIC_OVL char *
  2.  mon_owns(buf, obj)
  3.  char *buf;
  4.  struct obj *obj;
  5.  {
  6.      if (obj->where == OBJ_MINVENT)
  7.          return strcpy(buf, s_suffix(y_monnam(obj->ocarry)));
  8.      return (char *) 0;
  9.  }
  10.  

cad

  1.  STATIC_OVL const char *
  2.  cad(altusage)
  3.  boolean altusage; /* used as a verbalized exclamation:  \"Cad! ...\" */
  4.  {
  5.      const char *res = 0;
  6.  
  7.      switch (is_demon(youmonst.data) ? 3 : poly_gender()) {
  8.      case 0:
  9.          res = "cad";
  10.          break;
  11.      case 1:
  12.          res = "minx";
  13.          break;
  14.      case 2:
  15.          res = "beast";
  16.          break;
  17.      case 3:
  18.          res = "fiend";
  19.          break;
  20.      default:
  21.          impossible("cad: unknown gender");
  22.          res = "thing";
  23.          break;
  24.      }
  25.      if (altusage) {
  26.          char *cadbuf = mon_nam(&youmonst); /* snag an output buffer */
  27.  
  28.          /* alternate usage adds a leading double quote and trailing
  29.             exclamation point plus sentence separating spaces */
  30.          Sprintf(cadbuf, "\"%s!  ", res);
  31.          cadbuf[1] = highc(cadbuf[1]);
  32.          res = cadbuf;
  33.      }
  34.      return res;
  35.  }
  36.  

sasc_bug

  1.  #ifdef __SASC
  2.  void
  3.  sasc_bug(struct obj *op, unsigned x)
  4.  {
  5.      op->unpaid = x;
  6.  }
  7.  #endif
  8.  
  9.  /*shk.c*/