home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / misc / 40 / shk4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-07-17  |  3.5 KB  |  139 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  2. /* shk.c - version 1.0.3 */
  3.  
  4. #include "hack.h"
  5. #include    "mfndpos.h"
  6. #include    "mkroom.h"
  7. #include    "eshk.h"
  8.  
  9. #define    ESHK(mon)    ((struct eshk *)(&(mon->mextra[0])))
  10. #define    NOTANGRY(mon)    mon->mpeaceful
  11. #define    ANGRY(mon)    !NOTANGRY(mon)
  12.  
  13. extern char plname[], *xname();
  14. extern struct obj *o_on(), *bp_to_obj();
  15.  
  16. /* Descriptor of current shopkeeper. Note that the bill need not be
  17.    per-shopkeeper, since it is valid only when in a shop. */
  18. extern struct monst *shopkeeper;
  19. extern struct bill_x *bill;
  20. extern int shlevel;    /* level of this shopkeeper */
  21. extern       struct obj *billobjs;    /* objects on bill with bp->useup */
  22.                 /* only accessed here and by save & restore */
  23. extern long int total;        /* filled by addupbill() */
  24. extern long int followmsg;    /* last time of follow message */
  25.  
  26. /*
  27.     invariants: obj->unpaid iff onbill(obj) [unless bp->useup]
  28.         obj->quan <= bp->bquan
  29.  */
  30.  
  31.  
  32. extern char shtypes[];
  33.  
  34. extern char *shopnam[];
  35.  
  36. extern char *shkname();
  37.  
  38. extern struct bill_x *onbill();
  39.  
  40. dopay(){
  41. long ltmp;
  42. register struct bill_x *bp;
  43. register struct monst *shkp;
  44. int pass, tmp;
  45.  
  46.     multi = 0;
  47.     (void) inshop();
  48.     for(shkp = fmon; shkp; shkp = shkp->nmon)
  49.         if(shkp->isshk && dist(shkp->mx,shkp->my) < 3)
  50.             break;
  51.     if(!shkp && u.uinshop &&
  52.        inroom(shopkeeper->mx,shopkeeper->my) == ESHK(shopkeeper)->shoproom)
  53.         shkp = shopkeeper;
  54.  
  55.     if(!shkp) {
  56.         pline("There is nobody here to receive your payment.");
  57.         return(0);
  58.     }
  59.     ltmp = ESHK(shkp)->robbed;
  60.     if(shkp != shopkeeper && NOTANGRY(shkp)) {
  61.         if(!ltmp) {
  62.             pline("You do not owe %s anything.", monnam(shkp));
  63.         } else
  64.         if(!u.ugold) {
  65.             pline("You have no money.");
  66.         } else {
  67.             long ugold = u.ugold;
  68.  
  69.             if(u.ugold > ltmp) {
  70.             pline("You give %s the %ld gold pieces he asked for.",
  71.                 monnam(shkp), ltmp);
  72.             pay(ltmp, shkp);
  73.             } else {
  74.             pline("You give %s all your gold.", monnam(shkp));
  75.             pay(u.ugold, shkp);
  76.             }
  77.             if(ugold < (ltmp>>1)) {
  78.             pline("Unfortunately, he doesn't look satisfied.");
  79.             } else {
  80.             ESHK(shkp)->robbed = 0;
  81.             ESHK(shkp)->following = 0;
  82.             if(ESHK(shkp)->shoplevel != dlevel) {
  83.             /* For convenience's sake, let him disappear */
  84.                 shkp->minvent = 0;        /* %% */
  85.                 shkp->mgold = 0;
  86.                 mondead(shkp);
  87.             }
  88.             }
  89.         }
  90.         return(1);
  91.     }
  92.         
  93.     if(!ESHK(shkp)->billct){
  94.         pline("You do not owe %s anything.", monnam(shkp));
  95.         if(!u.ugold){
  96.             pline("Moreover, you have no money.");
  97.             return(1);
  98.         }
  99.         if(ESHK(shkp)->robbed){
  100. #define min(a,b)    ((a<b)?a:b)
  101.             pline("But since his shop has been robbed recently,");
  102.             pline("you %srepay %s's expenses.",
  103.               (u.ugold < ESHK(shkp)->robbed) ? "partially " : "",
  104.               monnam(shkp));
  105.             pay(min(u.ugold, ESHK(shkp)->robbed), shkp);
  106.             ESHK(shkp)->robbed = 0;
  107.             return(1);
  108.         }
  109.         if(ANGRY(shkp)) dopay2(shkp);
  110.         return(1);
  111.     }
  112.     if(shkp != shopkeeper) {
  113.         impossible("dopay: not to shopkeeper?");
  114.         if(shopkeeper) setpaid();
  115.         return(0);
  116.     }
  117.     for(pass = 0; pass <= 1; pass++) {
  118.         tmp = 0;
  119.         while(tmp < ESHK(shopkeeper)->billct) {
  120.             bp = &bill[tmp];
  121.             if(!pass && !bp->useup) {
  122.                 tmp++;
  123.                 continue;
  124.             }
  125.             if(!dopayobj(bp)) return(1);
  126. #ifdef MSDOS
  127.             *bp = bill[--ESHK(shopkeeper)->billct];
  128. #else
  129.             bill[tmp] = bill[--ESHK(shopkeeper)->billct];
  130. #endif MSDOS
  131.         }
  132.     }
  133.     pline("Thank you for shopping in %s's %s store!",
  134.         shkname(shopkeeper),
  135.         shopnam[rooms[ESHK(shopkeeper)->shoproom].rtype - 8]);
  136.     NOTANGRY(shopkeeper) = 1;
  137.     return(1);
  138. }
  139.