home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / misc / 40 / shk1b.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-07-17  |  4.4 KB  |  171 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. extern struct monst *shopkeeper;
  40. extern struct bill_x *bill;
  41. extern int shlevel;    /* level of this shopkeeper */
  42. extern       struct obj *billobjs;    /* objects on bill with bp->useup */
  43.                 /* only accessed here and by save & restore */
  44. extern long int total;        /* filled by addupbill() */
  45. extern 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. extern char shtypes[];
  54.  
  55. extern char *shopnam[];
  56.  
  57. extern char *shkname();
  58.  
  59. extern struct bill_x *onbill();
  60.  
  61.  
  62. /* return 1 if paid successfully */
  63. /*        0 if not enough money */
  64. /*       -1 if object could not be found (but was paid) */
  65. dopayobj(bp) register struct bill_x *bp; {
  66. register struct obj *obj;
  67. long ltmp;
  68.  
  69.     /* find the object on one of the lists */
  70.     obj = bp_to_obj(bp);
  71.  
  72.     if(!obj) {
  73.         impossible("Shopkeeper administration out of order.");
  74.         setpaid();    /* be nice to the player */
  75.         return(0);
  76.     }
  77.  
  78.     if(!obj->unpaid && !bp->useup){
  79.         impossible("Paid object on bill??");
  80.         return(1);
  81.     }
  82.     obj->unpaid = 0;
  83.     ltmp = bp->price * bp->bquan;
  84.     if(ANGRY(shopkeeper)) ltmp += ltmp/3;
  85.     if(u.ugold < ltmp){
  86.         pline("You don't have gold enough to pay %s.",
  87.             doname(obj));
  88.         obj->unpaid = 1;
  89.         return(0);
  90.     }
  91.     pay(ltmp, shopkeeper);
  92.     pline("You bought %s for %ld gold piece%s.",
  93.         doname(obj), ltmp, plur(ltmp));
  94.     if(bp->useup) {
  95.         register struct obj *otmp = billobjs;
  96.         if(obj == billobjs)
  97.             billobjs = obj->nobj;
  98.         else {
  99.             while(otmp && otmp->nobj != obj) otmp = otmp->nobj;
  100.             if(otmp) otmp->nobj = obj->nobj;
  101.             else pline("Error in shopkeeper administration.");
  102.         }
  103.         free((char *) obj);
  104.     }
  105.     return(1);
  106. }
  107.  
  108. /* routine called after dying (or quitting) with nonempty bill */
  109. paybill(){
  110.     if(shlevel == dlevel && shopkeeper && ESHK(shopkeeper)->billct){
  111.         addupbill();
  112.         if(total > u.ugold){
  113.             shopkeeper->mgold += u.ugold;
  114.             u.ugold = 0;
  115.         pline("%s comes and takes all your possessions.",
  116.             Monnam(shopkeeper));
  117.         } else {
  118.             u.ugold -= total;
  119.             shopkeeper->mgold += total;
  120.     pline("%s comes and takes the %ld zorkmids you owed him.",
  121.         Monnam(shopkeeper), total);
  122.         }
  123.         setpaid();    /* in case we create bones */
  124.     }
  125. }
  126.  
  127. /* find obj on one of the lists */
  128. struct obj *
  129. bp_to_obj(bp)
  130. register struct bill_x *bp;
  131. {
  132.     register struct obj *obj;
  133.     register struct monst *mtmp;
  134.     register unsigned id = bp->bo_id;
  135.  
  136.     if(bp->useup)
  137.         obj = o_on(id, billobjs);
  138.     else if(!(obj = o_on(id, invent)) &&
  139.         !(obj = o_on(id, fobj)) &&
  140.         !(obj = o_on(id, fcobj))) {
  141.             for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
  142.             if(obj = o_on(id, mtmp->minvent))
  143.                 break;
  144.             for(mtmp = fallen_down; mtmp; mtmp = mtmp->nmon)
  145.             if(obj = o_on(id, mtmp->minvent))
  146.                 break;
  147.         }
  148.     return(obj);
  149. }
  150.  
  151. /* called in hack.c when we pickup an object */
  152. addtobill(obj) register struct obj *obj; {
  153. register struct bill_x *bp;
  154.     if(!inshop() ||
  155.     (u.ux == ESHK(shopkeeper)->shk.x && u.uy == ESHK(shopkeeper)->shk.y) ||
  156.     (u.ux == ESHK(shopkeeper)->shd.x && u.uy == ESHK(shopkeeper)->shd.y) ||
  157.         onbill(obj) /* perhaps we threw it away earlier */
  158.       ) return;
  159.     if(ESHK(shopkeeper)->billct == BILLSZ){
  160.         pline("You got that for free!");
  161.         return;
  162.     }
  163.     bp = &bill[ESHK(shopkeeper)->billct];
  164.     bp->bo_id = obj->o_id;
  165.     bp->bquan = obj->quan;
  166.     bp->useup = 0;
  167.     bp->price = getprice(obj);
  168.     ESHK(shopkeeper)->billct++;
  169.     obj->unpaid = 1;
  170. }
  171.