home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / HACKSRC.ZIP / SHK.C < prev    next >
C/C++ Source or Header  |  1985-10-16  |  25KB  |  1,007 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  2. /* shk.c - version 1.0.3 */
  3.  
  4. #include "hack.h"
  5. #ifdef QUEST
  6. int shlevel = 0;
  7. struct monst *shopkeeper = 0;
  8. struct obj *billobjs = 0;
  9. obfree(obj,merge) register struct obj *obj, *merge; {
  10.     free((char *) obj);
  11. }
  12. inshop(){ return(0); }
  13. addtobill(){}
  14. subfrombill(){}
  15. splitbill(){}
  16. dopay(){ return(0); }
  17. paybill(){}
  18. doinvbill(){ return(0); }
  19. shkdead(){}
  20. shkcatch(){ return(0); }
  21. shk_move(){ return(0); }
  22. replshk(mtmp,mtmp2) struct monst *mtmp, *mtmp2; {}
  23. char *shkname(){ return(""); }
  24.  
  25. #else QUEST
  26. #include    "mfndpos.h"
  27. #include    "mkroom.h"
  28. #include    "eshk.h"
  29.  
  30. #define    ESHK(mon)    ((struct eshk *)(&(mon->mextra[0])))
  31. #define    NOTANGRY(mon)    mon->mpeaceful
  32. #define    ANGRY(mon)    !NOTANGRY(mon)
  33.  
  34. extern char plname[], *xname();
  35. extern struct obj *o_on(), *bp_to_obj();
  36.  
  37. /* Descriptor of current shopkeeper. Note that the bill need not be
  38.    per-shopkeeper, since it is valid only when in a shop. */
  39. static struct monst *shopkeeper = 0;
  40. static struct bill_x *bill;
  41. static int shlevel = 0;    /* level of this shopkeeper */
  42.        struct obj *billobjs;    /* objects on bill with bp->useup */
  43.                 /* only accessed here and by save & restore */
  44. static long int total;        /* filled by addupbill() */
  45. static long int followmsg;    /* last time of follow message */
  46.  
  47. /*
  48.     invariants: obj->unpaid iff onbill(obj) [unless bp->useup]
  49.         obj->quan <= bp->bquan
  50.  */
  51.  
  52.  
  53. char shtypes[] = {    /* 8 shoptypes: 7 specialized, 1 mixed */
  54.     RING_SYM, WAND_SYM, WEAPON_SYM, FOOD_SYM, SCROLL_SYM,
  55.     POTION_SYM, ARMOR_SYM, 0
  56. };
  57.  
  58. static char *shopnam[] = {
  59.     "engagement ring", "walking cane", "antique weapon",
  60.     "delicatessen", "second hand book", "liquor",
  61.     "used armor", "assorted antiques"
  62. };
  63.  
  64. char *
  65. shkname(mtmp)                /* called in do_name.c */
  66. register struct monst *mtmp;
  67. {
  68.     return(ESHK(mtmp)->shknam);
  69. }
  70.  
  71. shkdead(mtmp)                /* called in mon.c */
  72. register struct monst *mtmp;
  73. {
  74.     register struct eshk *eshk = ESHK(mtmp);
  75.  
  76.     if(eshk->shoplevel == dlevel)
  77.         rooms[eshk->shoproom].rtype = 0;
  78.     if(mtmp == shopkeeper) {
  79.         setpaid();
  80.         shopkeeper = 0;
  81.         bill = (struct bill_x *) -1000;    /* dump core when referenced */
  82.     }
  83. }
  84.  
  85. replshk(mtmp,mtmp2)
  86. register struct monst *mtmp, *mtmp2;
  87. {
  88.     if(mtmp == shopkeeper) {
  89.         shopkeeper = mtmp2;
  90.         bill = &(ESHK(shopkeeper)->bill[0]);
  91.     }
  92. }
  93.  
  94. static
  95. setpaid(){    /* caller has checked that shopkeeper exists */
  96.         /* either we paid or left the shop or he just died */
  97. register struct obj *obj;
  98. register struct monst *mtmp;
  99.     for(obj = invent; obj; obj = obj->nobj)
  100.         obj->unpaid = 0;
  101.     for(obj = fobj; obj; obj = obj->nobj)
  102.         obj->unpaid = 0;
  103.     for(obj = fcobj; obj; obj = obj->nobj)
  104.         obj->unpaid = 0;
  105.     for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  106.         for(obj = mtmp->minvent; obj; obj = obj->nobj)
  107.             obj->unpaid = 0;
  108.     for(mtmp = fallen_down; mtmp; mtmp = mtmp->nmon)
  109.         for(obj = mtmp->minvent; obj; obj = obj->nobj)
  110.             obj->unpaid = 0;
  111.     while(obj = billobjs){
  112.         billobjs = obj->nobj;
  113.         free((char *) obj);
  114.     }
  115.     ESHK(shopkeeper)->billct = 0;
  116. }
  117.  
  118. static
  119. addupbill(){    /* delivers result in total */
  120.         /* caller has checked that shopkeeper exists */
  121. register ct = ESHK(shopkeeper)->billct;
  122. register struct bill_x *bp = bill;
  123.     total = 0;
  124.     while(ct--){
  125.         total += bp->price * bp->bquan;
  126.         bp++;
  127.     }
  128. }
  129.  
  130. inshop(){
  131. register roomno = inroom(u.ux,u.uy);
  132.  
  133.     /* Did we just leave a shop? */
  134.     if(u.uinshop &&
  135.         (u.uinshop != roomno + 1 || shlevel != dlevel || !shopkeeper)) {
  136. #ifdef DGK
  137.     /* This is part of the bugfix for shopkeepers not having their
  138.      * bill paid.  As reported by ab@unido -dgk
  139.      */
  140.         if(shopkeeper) {
  141.             if(ESHK(shopkeeper)->billct) {
  142.             if (inroom(shopkeeper->mx, shopkeeper->my) ==
  143.                  u.uinshop - 1)
  144.                 pline("Somehow you escaped the shop without paying!");
  145.             addupbill();
  146.             pline("You stole for a total worth of %ld zorkmids.",
  147.                 total);
  148.             ESHK(shopkeeper)->robbed += total;
  149.             setpaid();
  150.             if((rooms[ESHK(shopkeeper)->shoproom].rtype == GENERAL)
  151.                 == (rn2(3) == 0))
  152.                 ESHK(shopkeeper)->following = 1;
  153.             }
  154.             shopkeeper = 0;
  155.             shlevel = 0;
  156.         }
  157.         u.uinshop = 0;
  158. #else
  159.         u.uinshop = 0;
  160.         if(shopkeeper) {
  161.             if(ESHK(shopkeeper)->billct) {
  162.             pline("Somehow you escaped the shop without paying!");
  163.             addupbill();
  164.             pline("You stole for a total worth of %ld zorkmids.",
  165.                 total);
  166.             ESHK(shopkeeper)->robbed += total;
  167.             setpaid();
  168.             if((rooms[ESHK(shopkeeper)->shoproom].rtype == GENERAL)
  169.                 == (rn2(3) == 0))
  170.                 ESHK(shopkeeper)->following = 1;
  171.             }
  172.             shopkeeper = 0;
  173.             shlevel = 0;
  174.         }
  175. #endif DGK
  176.     }
  177.  
  178.     /* Did we just enter a zoo of some kind? */
  179.     if(roomno >= 0) {
  180.         register int rt = rooms[roomno].rtype;
  181.         register struct monst *mtmp;
  182.         if(rt == ZOO) {
  183.             pline("Welcome to David's treasure zoo!");
  184.         } else
  185.         if(rt == SWAMP) {
  186.             pline("It looks rather muddy down here.");
  187.         } else
  188.         if(rt == MORGUE) {
  189.             if(midnight())
  190.                 pline("Go away! Go away!");
  191.             else
  192.                 pline("You get an uncanny feeling ...");
  193.         } else
  194.             rt = 0;
  195.         if(rt != 0) {
  196.             rooms[roomno].rtype = 0;
  197.             for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  198.                 if(rt != ZOO || !rn2(3))
  199.                     mtmp->msleep = 0;
  200.         }
  201.     }
  202.  
  203.     /* Did we just enter a shop? */
  204.     if(roomno >= 0 && rooms[roomno].rtype >= 8) {
  205.         if(shlevel != dlevel || !shopkeeper
  206.                  || ESHK(shopkeeper)->shoproom != roomno)
  207.         findshk(roomno);
  208.         if(!shopkeeper) {
  209.         rooms[roomno].rtype = 0;
  210.         u.uinshop = 0;
  211. #ifndef DGK
  212.     /* This is part of the bugfix for shopkeepers not having their
  213.      * bill paid.  As reported by ab@unido -dgk
  214.      */
  215.         } else if(inroom(shopkeeper->mx, shopkeeper->my) != roomno) {
  216.         u.uinshop = 0;
  217. #endif DGK
  218.         } else if(!u.uinshop){
  219.         if(!ESHK(shopkeeper)->visitct ||
  220.             strncmp(ESHK(shopkeeper)->customer, plname, PL_NSIZ)){
  221.  
  222.             /* He seems to be new here */
  223.             ESHK(shopkeeper)->visitct = 0;
  224.             ESHK(shopkeeper)->following = 0;
  225.             (void) strncpy(ESHK(shopkeeper)->customer,plname,PL_NSIZ);
  226.             NOTANGRY(shopkeeper) = 1;
  227.         }
  228.         if(!ESHK(shopkeeper)->following) {
  229.             boolean box, pick;
  230.  
  231.             pline("Hello %s! Welcome%s to %s's %s shop!",
  232.             plname,
  233.             ESHK(shopkeeper)->visitct++ ? " again" : "",
  234.             shkname(shopkeeper),
  235.             shopnam[rooms[ESHK(shopkeeper)->shoproom].rtype - 8] );
  236.             box = carrying(ICE_BOX);
  237.             pick = carrying(PICK_AXE);
  238.             if(box || pick) {
  239.             if(dochug(shopkeeper)) {
  240.                 u.uinshop = 0;    /* he died moving */
  241.                 return(0);
  242.             }
  243.             pline("Will you please leave your %s outside?",
  244.                 (box && pick) ? "box and pick-axe" :
  245.                 box ? "box" : "pick-axe");
  246.             }
  247.         }
  248.         u.uinshop = roomno + 1;
  249.         }
  250.     }
  251.     return(u.uinshop);
  252. }
  253.  
  254. static
  255. findshk(roomno)
  256. register roomno;
  257. {
  258. register struct monst *mtmp;
  259.     for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  260.         if(mtmp->isshk && ESHK(mtmp)->shoproom == roomno
  261.                && ESHK(mtmp)->shoplevel == dlevel) {
  262.         shopkeeper = mtmp;
  263.         bill = &(ESHK(shopkeeper)->bill[0]);
  264.         shlevel = dlevel;
  265.         if(ANGRY(shopkeeper) &&
  266.            strncmp(ESHK(shopkeeper)->customer,plname,PL_NSIZ))
  267.             NOTANGRY(shopkeeper) = 1;
  268.         /* billobjs = 0; -- this is wrong if we save in a shop */
  269.         /* (and it is harmless to have too many things in billobjs) */
  270.         return;
  271.     }
  272.     shopkeeper = 0;
  273.     shlevel = 0;
  274.     bill = (struct bill_x *) -1000;    /* dump core when referenced */
  275. }
  276.  
  277. static struct bill_x *
  278. onbill(obj) register struct obj *obj; {
  279. register struct bill_x *bp;
  280.     if(!shopkeeper) return(0);
  281.     for(bp = bill; bp < &bill[ESHK(shopkeeper)->billct]; bp++)
  282.         if(bp->bo_id == obj->o_id) {
  283.             if(!obj->unpaid) pline("onbill: paid obj on bill?");
  284.             return(bp);
  285.         }
  286.     if(obj->unpaid) pline("onbill: unpaid obj not on bill?");
  287.     return(0);
  288. }
  289.  
  290. /* called with two args on merge */
  291. obfree(obj,merge) register struct obj *obj, *merge; {
  292. register struct bill_x *bp = onbill(obj);
  293. register struct bill_x *bpm;
  294.     if(bp) {
  295.         if(!merge){
  296.             bp->useup = 1;
  297.             obj->unpaid = 0;    /* only for doinvbill */
  298.             obj->nobj = billobjs;
  299.             billobjs = obj;
  300.             return;
  301.         }
  302.         bpm = onbill(merge);
  303.         if(!bpm){
  304.             /* this used to be a rename */
  305.             impossible("obfree: not on bill??");
  306.             return;
  307.         } else {
  308.             /* this was a merger */
  309.             bpm->bquan += bp->bquan;
  310.             ESHK(shopkeeper)->billct--;
  311.             *bp = bill[ESHK(shopkeeper)->billct];
  312.         }
  313.     }
  314.     free((char *) obj);
  315. }
  316.  
  317. static
  318. pay(tmp,shkp)
  319. long tmp;
  320. register struct monst *shkp;
  321. {
  322.     long robbed = ESHK(shkp)->robbed;
  323.  
  324.     u.ugold -= tmp;
  325.     shkp->mgold += tmp;
  326.     flags.botl = 1;
  327.     if(robbed) {
  328.         robbed -= tmp;
  329.         if(robbed < 0) robbed = 0;
  330.         ESHK(shkp)->robbed = robbed;
  331.     }
  332. }
  333.  
  334. dopay(){
  335. long ltmp;
  336. register struct bill_x *bp;
  337. register struct monst *shkp;
  338. int pass, tmp;
  339.  
  340.     multi = 0;
  341.     (void) inshop();
  342.     for(shkp = fmon; shkp; shkp = shkp->nmon)
  343.         if(shkp->isshk && dist(shkp->mx,shkp->my) < 3)
  344.             break;
  345.     if(!shkp && u.uinshop &&
  346.        inroom(shopkeeper->mx,shopkeeper->my) == ESHK(shopkeeper)->shoproom)
  347.         shkp = shopkeeper;
  348.  
  349.     if(!shkp) {
  350.         pline("There is nobody here to receive your payment.");
  351.         return(0);
  352.     }
  353.     ltmp = ESHK(shkp)->robbed;
  354.     if(shkp != shopkeeper && NOTANGRY(shkp)) {
  355.         if(!ltmp) {
  356.             pline("You do not owe %s anything.", monnam(shkp));
  357.         } else
  358.         if(!u.ugold) {
  359.             pline("You have no money.");
  360.         } else {
  361.             long ugold = u.ugold;
  362.  
  363.             if(u.ugold > ltmp) {
  364.             pline("You give %s the %ld gold pieces he asked for.",
  365.                 monnam(shkp), ltmp);
  366.             pay(ltmp, shkp);
  367.             } else {
  368.             pline("You give %s all your gold.", monnam(shkp));
  369.             pay(u.ugold, shkp);
  370.             }
  371.             if(ugold < ltmp/2) {
  372.             pline("Unfortunately, he doesn't look satisfied.");
  373.             } else {
  374.             ESHK(shkp)->robbed = 0;
  375.             ESHK(shkp)->following = 0;
  376.             if(ESHK(shkp)->shoplevel != dlevel) {
  377.             /* For convenience's sake, let him disappear */
  378.                 shkp->minvent = 0;        /* %% */
  379.                 shkp->mgold = 0;
  380.                 mondead(shkp);
  381.             }
  382.             }
  383.         }
  384.         return(1);
  385.     }
  386.         
  387.     if(!ESHK(shkp)->billct){
  388.         pline("You do not owe %s anything.", monnam(shkp));
  389.         if(!u.ugold){
  390.             pline("Moreover, you have no money.");
  391.             return(1);
  392.         }
  393.         if(ESHK(shkp)->robbed){
  394. #define min(a,b)    ((a<b)?a:b)
  395.             pline("But since his shop has been robbed recently,");
  396.             pline("you %srepay %s's expenses.",
  397.               (u.ugold < ESHK(shkp)->robbed) ? "partially " : "",
  398.               monnam(shkp));
  399.             pay(min(u.ugold, ESHK(shkp)->robbed), shkp);
  400.             ESHK(shkp)->robbed = 0;
  401.             return(1);
  402.         }
  403.         if(ANGRY(shkp)){
  404.             pline("But in order to appease %s,",
  405.                 amonnam(shkp, "angry"));
  406.             if(u.ugold >= 1000){
  407.                 ltmp = 1000;
  408.                 pline(" you give him 1000 gold pieces.");
  409.             } else {
  410.                 ltmp = u.ugold;
  411.                 pline(" you give him all your money.");
  412.             }
  413.             pay(ltmp, shkp);
  414.             if(strncmp(ESHK(shkp)->customer, plname, PL_NSIZ)
  415.                || rn2(3)){
  416.                 pline("%s calms down.", Monnam(shkp));
  417.                 NOTANGRY(shkp) = 1;
  418.             } else    pline("%s is as angry as ever.",
  419.                     Monnam(shkp));
  420.         }
  421.         return(1);
  422.     }
  423.     if(shkp != shopkeeper) {
  424.         impossible("dopay: not to shopkeeper?");
  425.         if(shopkeeper) setpaid();
  426.         return(0);
  427.     }
  428.     for(pass = 0; pass <= 1; pass++) {
  429.         tmp = 0;
  430.         while(tmp < ESHK(shopkeeper)->billct) {
  431.             bp = &bill[tmp];
  432.             if(!pass && !bp->useup) {
  433.                 tmp++;
  434.                 continue;
  435.             }
  436.             if(!dopayobj(bp)) return(1);
  437. #ifdef MSDOS
  438.             *bp = bill[--ESHK(shopkeeper)->billct];
  439. #else
  440.             bill[tmp] = bill[--ESHK(shopkeeper)->billct];
  441. #endif MSDOS
  442.         }
  443.     }
  444.     pline("Thank you for shopping in %s's %s store!",
  445.         shkname(shopkeeper),
  446.         shopnam[rooms[ESHK(shopkeeper)->shoproom].rtype - 8]);
  447.     NOTANGRY(shopkeeper) = 1;
  448.     return(1);
  449. }
  450.  
  451. /* return 1 if paid successfully */
  452. /*        0 if not enough money */
  453. /*       -1 if object could not be found (but was paid) */
  454. static
  455. dopayobj(bp) register struct bill_x *bp; {
  456. register struct obj *obj;
  457. long ltmp;
  458.  
  459.     /* find the object on one of the lists */
  460.     obj = bp_to_obj(bp);
  461.  
  462.     if(!obj) {
  463.         impossible("Shopkeeper administration out of order.");
  464.         setpaid();    /* be nice to the player */
  465.         return(0);
  466.     }
  467.  
  468.     if(!obj->unpaid && !bp->useup){
  469.         impossible("Paid object on bill??");
  470.         return(1);
  471.     }
  472.     obj->unpaid = 0;
  473.     ltmp = bp->price * bp->bquan;
  474.     if(ANGRY(shopkeeper)) ltmp += ltmp/3;
  475.     if(u.ugold < ltmp){
  476.         pline("You don't have gold enough to pay %s.",
  477.             doname(obj));
  478.         obj->unpaid = 1;
  479.         return(0);
  480.     }
  481.     pay(ltmp, shopkeeper);
  482.     pline("You bought %s for %ld gold piece%s.",
  483.         doname(obj), ltmp, plur(ltmp));
  484.     if(bp->useup) {
  485.         register struct obj *otmp = billobjs;
  486.         if(obj == billobjs)
  487.             billobjs = obj->nobj;
  488.         else {
  489.             while(otmp && otmp->nobj != obj) otmp = otmp->nobj;
  490.             if(otmp) otmp->nobj = obj->nobj;
  491.             else pline("Error in shopkeeper administration.");
  492.         }
  493.         free((char *) obj);
  494.     }
  495.     return(1);
  496. }
  497.  
  498. /* routine called after dying (or quitting) with nonempty bill */
  499. paybill(){
  500.     if(shlevel == dlevel && shopkeeper && ESHK(shopkeeper)->billct){
  501.         addupbill();
  502.         if(total > u.ugold){
  503.             shopkeeper->mgold += u.ugold;
  504.             u.ugold = 0;
  505.         pline("%s comes and takes all your possessions.",
  506.             Monnam(shopkeeper));
  507.         } else {
  508.             u.ugold -= total;
  509.             shopkeeper->mgold += total;
  510.     pline("%s comes and takes the %ld zorkmids you owed him.",
  511.         Monnam(shopkeeper), total);
  512.         }
  513.         setpaid();    /* in case we create bones */
  514.     }
  515. }
  516.  
  517. /* find obj on one of the lists */
  518. struct obj *
  519. bp_to_obj(bp)
  520. register struct bill_x *bp;
  521. {
  522.     register struct obj *obj;
  523.     register struct monst *mtmp;
  524.     register unsigned id = bp->bo_id;
  525.  
  526.     if(bp->useup)
  527.         obj = o_on(id, billobjs);
  528.     else if(!(obj = o_on(id, invent)) &&
  529.         !(obj = o_on(id, fobj)) &&
  530.         !(obj = o_on(id, fcobj))) {
  531.             for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  532.             if(obj = o_on(id, mtmp->minvent))
  533.                 break;
  534.             for(mtmp = fallen_down; mtmp; mtmp = mtmp->nmon)
  535.             if(obj = o_on(id, mtmp->minvent))
  536.                 break;
  537.         }
  538.     return(obj);
  539. }
  540.  
  541. /* called in hack.c when we pickup an object */
  542. addtobill(obj) register struct obj *obj; {
  543. register struct bill_x *bp;
  544.     if(!inshop() ||
  545.     (u.ux == ESHK(shopkeeper)->shk.x && u.uy == ESHK(shopkeeper)->shk.y) ||
  546.     (u.ux == ESHK(shopkeeper)->shd.x && u.uy == ESHK(shopkeeper)->shd.y) ||
  547.         onbill(obj) /* perhaps we threw it away earlier */
  548.       ) return;
  549.     if(ESHK(shopkeeper)->billct == BILLSZ){
  550.         pline("You got that for free!");
  551.         return;
  552.     }
  553.     bp = &bill[ESHK(shopkeeper)->billct];
  554.     bp->bo_id = obj->o_id;
  555.     bp->bquan = obj->quan;
  556.     bp->useup = 0;
  557.     bp->price = getprice(obj);
  558.     ESHK(shopkeeper)->billct++;
  559.     obj->unpaid = 1;
  560. }
  561.  
  562. splitbill(obj,otmp) register struct obj *obj, *otmp; {
  563.     /* otmp has been split off from obj */
  564. register struct bill_x *bp;
  565. register int tmp;
  566.     bp = onbill(obj);
  567.     if(!bp) {
  568.         impossible("splitbill: not on bill?");
  569.         return;
  570.     }
  571.     if(bp->bquan < otmp->quan) {
  572.         impossible("Negative quantity on bill??");
  573.     }
  574.     if(bp->bquan == otmp->quan) {
  575.         impossible("Zero quantity on bill??");
  576.     }
  577.     bp->bquan -= otmp->quan;
  578.  
  579.     /* addtobill(otmp); */
  580.     if(ESHK(shopkeeper)->billct == BILLSZ) otmp->unpaid = 0;
  581.     else {
  582.         tmp = bp->price;
  583.         bp = &bill[ESHK(shopkeeper)->billct];
  584.         bp->bo_id = otmp->o_id;
  585.         bp->bquan = otmp->quan;
  586.         bp->useup = 0;
  587.         bp->price = tmp;
  588.         ESHK(shopkeeper)->billct++;
  589.     }
  590. }
  591.  
  592. subfrombill(obj) register struct obj *obj; {
  593. long ltmp;
  594. register int tmp;
  595. register struct obj *otmp;
  596. register struct bill_x *bp;
  597.     if(!inshop() || (u.ux == ESHK(shopkeeper)->shk.x && u.uy == ESHK(shopkeeper)->shk.y) ||
  598.         (u.ux == ESHK(shopkeeper)->shd.x && u.uy == ESHK(shopkeeper)->shd.y))
  599.         return;
  600.     if((bp = onbill(obj)) != 0){
  601.         obj->unpaid = 0;
  602.         if(bp->bquan > obj->quan){
  603.             otmp = newobj(0);
  604.             *otmp = *obj;
  605.             bp->bo_id = otmp->o_id = flags.ident++;
  606.             otmp->quan = (bp->bquan -= obj->quan);
  607.             otmp->owt = 0;    /* superfluous */
  608.             otmp->onamelth = 0;
  609.             bp->useup = 1;
  610.             otmp->nobj = billobjs;
  611.             billobjs = otmp;
  612.             return;
  613.         }
  614.         ESHK(shopkeeper)->billct--;
  615.         *bp = bill[ESHK(shopkeeper)->billct];
  616.         return;
  617.     }
  618.     if(obj->unpaid){
  619.         pline("%s didn't notice.", Monnam(shopkeeper));
  620.         obj->unpaid = 0;
  621.         return;        /* %% */
  622.     }
  623.     /* he dropped something of his own - probably wants to sell it */
  624.     if(shopkeeper->msleep || shopkeeper->mfroz ||
  625.         inroom(shopkeeper->mx,shopkeeper->my) != ESHK(shopkeeper)->shoproom)
  626.         return;
  627.     if(ESHK(shopkeeper)->billct == BILLSZ ||
  628.       ((tmp = shtypes[rooms[ESHK(shopkeeper)->shoproom].rtype-8]) && tmp != obj->olet)
  629.       || index("_0", obj->olet)) {
  630.         pline("%s seems not interested.", Monnam(shopkeeper));
  631.         return;
  632.     }
  633.     ltmp = getprice(obj) * obj->quan;
  634.     if(ANGRY(shopkeeper)) {
  635.         ltmp /= 3;
  636.         NOTANGRY(shopkeeper) = 1;
  637.     } else    ltmp /= 2;
  638.     if(ESHK(shopkeeper)->robbed){
  639.         if((ESHK(shopkeeper)->robbed -= ltmp) < 0)
  640.             ESHK(shopkeeper)->robbed = 0;
  641. pline("Thank you for your contribution to restock this recently plundered shop.");
  642.         return;
  643.     }
  644.     if(ltmp > shopkeeper->mgold)
  645.         ltmp = shopkeeper->mgold;
  646.     pay(-ltmp, shopkeeper);
  647.     if(!ltmp)
  648.     pline("%s gladly accepts %s but cannot pay you at present.",
  649.         Monnam(shopkeeper), doname(obj));
  650.     else
  651.     pline("You sold %s and got %ld gold piece%s.", doname(obj), ltmp,
  652.         plur(ltmp));
  653. }
  654.  
  655. doinvbill(mode)
  656. int mode;        /* 0: deliver count 1: paged */
  657. {
  658.     register struct bill_x *bp;
  659.     register struct obj *obj;
  660.     long totused, thisused;
  661.     char buf[BUFSZ];
  662.  
  663.     if(mode == 0) {
  664.         register int cnt = 0;
  665.  
  666.         if(shopkeeper)
  667.         for(bp = bill; bp - bill < ESHK(shopkeeper)->billct; bp++)
  668.             if(bp->useup ||
  669.               ((obj = bp_to_obj(bp)) && obj->quan < bp->bquan))
  670.             cnt++;
  671.         return(cnt);
  672.     }
  673.  
  674.     if(!shopkeeper) {
  675.         impossible("doinvbill: no shopkeeper?");
  676.         return(0);
  677.     }
  678.  
  679.     set_pager(0);
  680.     if(page_line("Unpaid articles already used up:") || page_line(""))
  681.         goto quit;
  682.  
  683.     totused = 0;
  684.     for(bp = bill; bp - bill < ESHK(shopkeeper)->billct; bp++) {
  685.         obj = bp_to_obj(bp);
  686.         if(!obj) {
  687.         impossible("Bad shopkeeper administration.");
  688.         goto quit;
  689.         }
  690.         if(bp->useup || bp->bquan > obj->quan) {
  691.         register int cnt, oquan, uquan;
  692.  
  693.         oquan = obj->quan;
  694.         uquan = (bp->useup ? bp->bquan : bp->bquan - oquan);
  695.         thisused = bp->price * uquan;
  696.         totused += thisused;
  697.         obj->quan = uquan;        /* cheat doname */
  698.         (void) sprintf(buf, "x -  %s", doname(obj));
  699.         obj->quan = oquan;        /* restore value */
  700.         for(cnt = 0; buf[cnt]; cnt++);
  701.         while(cnt < 50)
  702.             buf[cnt++] = ' ';
  703.         (void) sprintf(&buf[cnt], " %5ld zorkmids", thisused);
  704.         if(page_line(buf))
  705.             goto quit;
  706.         }
  707.     }
  708.     (void) sprintf(buf, "Total:%50ld zorkmids", totused);
  709.     if(page_line("") || page_line(buf))
  710.         goto quit;
  711.     set_pager(1);
  712.     return(0);
  713. quit:
  714.     set_pager(2);
  715.     return(0);
  716. }
  717.  
  718. static
  719. getprice(obj) register struct obj *obj; {
  720. register int tmp, ac;
  721.     switch(obj->olet){
  722.     case AMULET_SYM:
  723.         tmp = 10*rnd(500);
  724.         break;
  725.     case TOOL_SYM:
  726.         tmp = 10*rnd((obj->otyp == EXPENSIVE_CAMERA) ? 150 : 30);
  727.         break;
  728.     case RING_SYM:
  729.         tmp = 10*rnd(100);
  730.         break;
  731.     case WAND_SYM:
  732.         tmp = 10*rnd(100);
  733.         break;
  734.     case SCROLL_SYM:
  735.         tmp = 10*rnd(50);
  736. #ifdef MAIL
  737.         if(obj->otyp == SCR_MAIL)
  738.             tmp = rnd(5);
  739. #endif MAIL
  740.         break;
  741.     case POTION_SYM:
  742.         tmp = 10*rnd(50);
  743.         break;
  744.     case FOOD_SYM:
  745.         tmp = 10*rnd(5 + (2000/realhunger()));
  746.         break;
  747.     case GEM_SYM:
  748.         tmp = 10*rnd(20);
  749.         break;
  750.     case ARMOR_SYM:
  751.         ac = ARM_BONUS(obj);
  752.         if(ac <= -10)        /* probably impossible */
  753.             ac = -9;
  754.         tmp = 100 + ac*ac*rnd(10+ac);
  755.         break;
  756.     case WEAPON_SYM:
  757.         if(obj->otyp < BOOMERANG)
  758.             tmp = 5*rnd(10);
  759.         else if(obj->otyp == LONG_SWORD ||
  760.             obj->otyp == TWO_HANDED_SWORD)
  761.             tmp = 10*rnd(150);
  762.         else    tmp = 10*rnd(75);
  763.         break;
  764.     case CHAIN_SYM:
  765.         pline("Strange ..., carrying a chain?");
  766.     case BALL_SYM:
  767.         tmp = 10;
  768.         break;
  769.     default:
  770.         tmp = 10000;
  771.     }
  772.     return(tmp);
  773. }
  774.  
  775. static
  776. realhunger(){    /* not completely foolproof */
  777. register tmp = u.uhunger;
  778. register struct obj *otmp = invent;
  779.     while(otmp){
  780.         if(otmp->olet == FOOD_SYM && !otmp->unpaid)
  781.             tmp += objects[otmp->otyp].nutrition;
  782.         otmp = otmp->nobj;
  783.     }
  784.     return((tmp <= 0) ? 1 : tmp);
  785. }
  786.  
  787. shkcatch(obj)
  788. register struct obj *obj;
  789. {
  790.     register struct monst *shkp = shopkeeper;
  791.  
  792.     if(u.uinshop && shkp && !shkp->mfroz && !shkp->msleep &&
  793.         u.dx && u.dy &&
  794.         inroom(u.ux+u.dx, u.uy+u.dy) + 1 == u.uinshop &&
  795.         shkp->mx == ESHK(shkp)->shk.x && shkp->my == ESHK(shkp)->shk.y &&
  796.         u.ux == ESHK(shkp)->shd.x && u.uy == ESHK(shkp)->shd.y) {
  797.         pline("%s nimbly catches the %s.", Monnam(shkp), xname(obj));
  798.         obj->nobj = shkp->minvent;
  799.         shkp->minvent = obj;
  800.         return(1);
  801.     }
  802.     return(0);
  803. }
  804.  
  805. /*
  806.  * shk_move: return 1: he moved  0: he didnt  -1: let m_move do it
  807.  */
  808. shk_move(shkp)
  809. register struct monst *shkp;
  810. {
  811.     register struct monst *mtmp;
  812.     register struct permonst *mdat = shkp->data;
  813.     register xchar gx,gy,omx,omy,nx,ny,nix,niy;
  814.     register schar appr,i;
  815.     register int udist;
  816.     int z;
  817.     schar shkroom,chi,chcnt,cnt;
  818.     boolean uondoor, satdoor, avoid, badinv;
  819.     coord poss[9];
  820.     int info[9];
  821.     struct obj *ib = 0;
  822.  
  823.     omx = shkp->mx;
  824.     omy = shkp->my;
  825.  
  826.     if((udist = dist(omx,omy)) < 3) {
  827.         if(ANGRY(shkp)) {
  828.             (void) hitu(shkp, d(mdat->damn, mdat->damd)+1);
  829.             return(0);
  830.         }
  831.         if(ESHK(shkp)->following) {
  832.             if(strncmp(ESHK(shkp)->customer, plname, PL_NSIZ)){
  833.                 pline("Hello %s! I was looking for %s.",
  834.                     plname, ESHK(shkp)->customer);
  835.                 ESHK(shkp)->following = 0;
  836.                 return(0);
  837.             }
  838.             if(!ESHK(shkp)->robbed) {    /* impossible? */
  839.                 ESHK(shkp)->following = 0;
  840.                 return(0);
  841.             }
  842.             if(moves > followmsg+4) {
  843.                 pline("Hello %s! Didn't you forget to pay?",
  844.                     plname);
  845.                 followmsg = moves;
  846.             }
  847.             if(udist < 2)
  848.                 return(0);
  849.         }
  850.     }
  851.  
  852.     shkroom = inroom(omx,omy);
  853.     appr = 1;
  854.     gx = ESHK(shkp)->shk.x;
  855.     gy = ESHK(shkp)->shk.y;
  856.     satdoor = (gx == omx && gy == omy);
  857.     if(ESHK(shkp)->following || ((z = holetime()) >= 0 && z*z <= udist)){
  858.         gx = u.ux;
  859.         gy = u.uy;
  860.         if(shkroom < 0 || shkroom != inroom(u.ux,u.uy))
  861.             if(udist > 4)
  862.             return(-1);    /* leave it to m_move */
  863.     } else if(ANGRY(shkp)) {
  864.         long saveBlind = Blind;
  865.         Blind = 0;
  866.         if(shkp->mcansee && !Invis && cansee(omx,omy)) {
  867.             gx = u.ux;
  868.             gy = u.uy;
  869.         }
  870.         Blind = saveBlind;
  871.         avoid = FALSE;
  872.     } else {
  873. #define    GDIST(x,y)    ((x-gx)*(x-gx)+(y-gy)*(y-gy))
  874.         if(Invis)
  875.           avoid = FALSE;
  876.         else {
  877.           uondoor = (u.ux == ESHK(shkp)->shd.x &&
  878.                 u.uy == ESHK(shkp)->shd.y);
  879.           if(uondoor) {
  880.             if(ESHK(shkp)->billct)
  881.             pline("Hello %s! Will you please pay before leaving?",
  882.                 plname);
  883.             badinv = (carrying(PICK_AXE) || carrying(ICE_BOX));
  884.             if(satdoor && badinv)
  885.             return(0);
  886.             avoid = !badinv;
  887.           } else {
  888.             avoid = (u.uinshop && dist(gx,gy) > 8);
  889.             badinv = FALSE;
  890.           }
  891.  
  892.           if(((!ESHK(shkp)->robbed && !ESHK(shkp)->billct) || avoid)
  893.               && GDIST(omx,omy) < 3){
  894.               if(!badinv && !online(omx,omy))
  895.                 return(0);
  896.               if(satdoor)
  897.                   appr = gx = gy = 0;
  898.           }
  899.         }
  900.     }
  901.     if(omx == gx && omy == gy)
  902.         return(0);
  903.     if(shkp->mconf) {
  904.         avoid = FALSE;
  905.         appr = 0;
  906.     }
  907.     nix = omx;
  908.     niy = omy;
  909.     cnt = mfndpos(shkp,poss,info,ALLOW_SSM);
  910.     if(avoid && uondoor) {        /* perhaps we cannot avoid him */
  911.         for(i=0; i<cnt; i++)
  912.             if(!(info[i] & NOTONL)) goto notonl_ok;
  913.         avoid = FALSE;
  914.     notonl_ok:
  915.         ;
  916.     }
  917.     chi = -1;
  918.     chcnt = 0;
  919.     for(i=0; i<cnt; i++){
  920.         nx = poss[i].x;
  921.         ny = poss[i].y;
  922.            if(levl[nx][ny].typ == ROOM
  923.         || shkroom != ESHK(shkp)->shoproom
  924.         || ESHK(shkp)->following) {
  925. #ifdef STUPID
  926.             /* cater for stupid compilers */
  927.             register int zz;
  928. #endif STUPID
  929.             if(uondoor && (ib = sobj_at(ICE_BOX, nx, ny))) {
  930.             nix = nx; niy = ny; chi = i; break;
  931.             }
  932.             if(avoid && (info[i] & NOTONL))
  933.             continue;
  934.             if((!appr && !rn2(++chcnt)) ||
  935. #ifdef STUPID
  936.             (appr && (zz = GDIST(nix,niy)) && zz > GDIST(nx,ny))
  937. #else
  938.             (appr && GDIST(nx,ny) < GDIST(nix,niy))
  939. #endif STUPID
  940.             ) {
  941.                 nix = nx;
  942.                 niy = ny;
  943.                 chi = i;
  944.             }
  945.         }
  946.     }
  947.     if(nix != omx || niy != omy){
  948.         if(info[chi] & ALLOW_M){
  949.             mtmp = m_at(nix,niy);
  950.             if(hitmm(shkp,mtmp) == 1 && rn2(3) &&
  951.                hitmm(mtmp,shkp) == 2) return(2);
  952.             return(0);
  953.         } else if(info[chi] & ALLOW_U){
  954.             (void) hitu(shkp, d(mdat->damn, mdat->damd)+1);
  955.             return(0);
  956.         }
  957.         shkp->mx = nix;
  958.         shkp->my = niy;
  959.         pmon(shkp);
  960.         if(ib) {
  961.             freeobj(ib);
  962.             mpickobj(shkp, ib);
  963.         }
  964.         return(1);
  965.     }
  966.     return(0);
  967. }
  968. #endif QUEST
  969.  
  970. online(x,y) {
  971.     return(x==u.ux || y==u.uy ||
  972.         (x-u.ux)*(x-u.ux) == (y-u.uy)*(y-u.uy));
  973. }
  974.  
  975. /* Does this monster follow me downstairs? */
  976. follower(mtmp)
  977. register struct monst *mtmp;
  978. {
  979.     return( mtmp->mtame || index("1TVWZi&, ", mtmp->data->mlet) ||
  980.         (mtmp->isshk && ESHK(mtmp)->following) );
  981. }
  982.  
  983. /* He is digging in the shop. */
  984. shopdig(fall)
  985. register int fall;
  986. {
  987.     if(!fall) {
  988.     if(u.utraptype == TT_PIT)
  989.         pline("\"Be careful, sir, or you might fall through the floor.\"");
  990.     else
  991.         pline("\"Please, do not damage the floor here.\"");
  992.     } else if(dist(shopkeeper->mx, shopkeeper->my) < 3) {
  993.     register struct obj *obj, *obj2;
  994.  
  995.     pline("%s grabs your backpack!", shkname(shopkeeper));
  996.     for(obj = invent; obj; obj = obj2) {
  997.         obj2 = obj->nobj;
  998.         if(obj->owornmask) continue;
  999.         freeinv(obj);
  1000.         obj->nobj = shopkeeper->minvent;
  1001.         shopkeeper->minvent = obj;
  1002.         if(obj->unpaid)
  1003.             subfrombill(obj);
  1004.     }
  1005.     }
  1006. }
  1007.