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