1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.shk.c - version 1.0.3 */
7 struct monst *shopkeeper = 0;
8 struct obj *billobjs = 0;
9 obfree(obj,merge) register struct obj *obj, *merge; {
12 inshop(){ return(0); }
19 doinvbill(){ return(0); }
21 shkcatch(){ return(0); }
22 shk_move(){ return(0); }
23 replshk(mtmp,mtmp2) struct monst *mtmp, *mtmp2; {}
24 char *shkname(){ return(""); }
27 #include "hack.mfndpos.h"
28 #include "def.mkroom.h"
31 #define ESHK(mon) ((struct eshk *)(&(mon->mextra[0])))
32 #define NOTANGRY(mon) mon->mpeaceful
33 #define ANGRY(mon) !NOTANGRY(mon)
35 extern char plname[], *xname();
36 extern struct obj *o_on(), *bp_to_obj();
38 /* Descriptor of current shopkeeper. Note that the bill need not be
39 per-shopkeeper, since it is valid only when in a shop. */
40 static struct monst *shopkeeper = 0;
41 static struct bill_x *bill;
42 static int shlevel = 0; /* level of this shopkeeper */
43 struct obj *billobjs; /* objects on bill with bp->useup */
44 /* only accessed here and by save & restore */
45 static long int total; /* filled by addupbill() */
46 static long int followmsg; /* last time of follow message */
49 invariants: obj->unpaid iff onbill(obj) [unless bp->useup]
50 obj->quan <= bp->bquan
54 char shtypes[] = { /* 8 shoptypes: 7 specialized, 1 mixed */
55 RING_SYM, WAND_SYM, WEAPON_SYM, FOOD_SYM, SCROLL_SYM,
56 POTION_SYM, ARMOR_SYM, 0
59 static char *shopnam[] = {
60 "engagement ring", "walking cane", "antique weapon",
61 "delicatessen", "second hand book", "liquor",
62 "used armor", "assorted antiques"
66 shkname(mtmp) /* called in do_name.c */
67 register struct monst *mtmp;
69 return(ESHK(mtmp)->shknam);
72 static void setpaid();
74 shkdead(mtmp) /* called in mon.c */
75 register struct monst *mtmp;
77 register struct eshk *eshk = ESHK(mtmp);
79 if(eshk->shoplevel == dlevel)
80 rooms[eshk->shoproom].rtype = 0;
81 if(mtmp == shopkeeper) {
84 bill = (struct bill_x *) -1000; /* dump core when referenced */
89 register struct monst *mtmp, *mtmp2;
91 if(mtmp == shopkeeper) {
93 bill = &(ESHK(shopkeeper)->bill[0]);
98 setpaid(){ /* caller has checked that shopkeeper exists */
99 /* either we paid or left the shop or he just died */
100 register struct obj *obj;
101 register struct monst *mtmp;
102 for(obj = invent; obj; obj = obj->nobj)
104 for(obj = fobj; obj; obj = obj->nobj)
106 for(obj = fcobj; obj; obj = obj->nobj)
108 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
109 for(obj = mtmp->minvent; obj; obj = obj->nobj)
111 for(mtmp = fallen_down; mtmp; mtmp = mtmp->nmon)
112 for(obj = mtmp->minvent; obj; obj = obj->nobj)
114 while(obj = billobjs){
115 billobjs = obj->nobj;
118 ESHK(shopkeeper)->billct = 0;
122 addupbill(){ /* delivers result in total */
123 /* caller has checked that shopkeeper exists */
124 register ct = ESHK(shopkeeper)->billct;
125 register struct bill_x *bp = bill;
128 total += bp->price * bp->bquan;
134 register roomno = inroom(u.ux,u.uy);
136 static void findshk();
138 /* Did we just leave a shop? */
140 (u.uinshop != roomno + 1 || shlevel != dlevel || !shopkeeper)) {
142 if(ESHK(shopkeeper)->billct) {
143 if(inroom(shopkeeper->mx, shopkeeper->my)
144 == u.uinshop - 1) /* ab@unido */
145 pline("Somehow you escaped the shop without paying!");
147 pline("You stole for a total worth of %ld zorkmids.",
149 ESHK(shopkeeper)->robbed += total;
151 if((rooms[ESHK(shopkeeper)->shoproom].rtype == GENERAL)
153 ESHK(shopkeeper)->following = 1;
161 /* Did we just enter a zoo of some kind? */
163 register int rt = rooms[roomno].rtype;
164 register struct monst *mtmp;
166 pline("Welcome to David's treasure zoo!");
169 pline("It looks rather muddy down here.");
173 pline("Go away! Go away!");
175 pline("You get an uncanny feeling ...");
179 rooms[roomno].rtype = 0;
180 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
181 if(rt != ZOO || !rn2(3))
186 /* Did we just enter a shop? */
187 if(roomno >= 0 && rooms[roomno].rtype >= 8) {
188 if(shlevel != dlevel || !shopkeeper
189 || ESHK(shopkeeper)->shoproom != roomno)
192 rooms[roomno].rtype = 0;
194 } else if(!u.uinshop){
195 if(!ESHK(shopkeeper)->visitct ||
196 strncmp(ESHK(shopkeeper)->customer, plname, PL_NSIZ)){
198 /* He seems to be new here */
199 ESHK(shopkeeper)->visitct = 0;
200 ESHK(shopkeeper)->following = 0;
201 (void) strncpy(ESHK(shopkeeper)->customer,plname,PL_NSIZ);
202 NOTANGRY(shopkeeper) = 1;
204 if(!ESHK(shopkeeper)->following) {
207 pline("Hello %s! Welcome%s to %s's %s shop!",
209 ESHK(shopkeeper)->visitct++ ? " again" : "",
211 shopnam[rooms[ESHK(shopkeeper)->shoproom].rtype - 8] );
212 box = carrying(ICE_BOX);
213 pick = carrying(PICK_AXE);
215 if(dochug(shopkeeper)) {
216 u.uinshop = 0; /* he died moving */
219 pline("Will you please leave your %s outside?",
220 (box && pick) ? "box and pick-axe" :
221 box ? "box" : "pick-axe");
224 u.uinshop = roomno + 1;
234 register struct monst *mtmp;
235 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
236 if(mtmp->isshk && ESHK(mtmp)->shoproom == roomno
237 && ESHK(mtmp)->shoplevel == dlevel) {
239 bill = &(ESHK(shopkeeper)->bill[0]);
241 if(ANGRY(shopkeeper) &&
242 strncmp(ESHK(shopkeeper)->customer,plname,PL_NSIZ))
243 NOTANGRY(shopkeeper) = 1;
244 /* billobjs = 0; -- this is wrong if we save in a shop */
245 /* (and it is harmless to have too many things in billobjs) */
250 bill = (struct bill_x *) -1000; /* dump core when referenced */
253 static struct bill_x *
254 onbill(obj) register struct obj *obj; {
255 register struct bill_x *bp;
256 if(!shopkeeper) return(0);
257 for(bp = bill; bp < &bill[ESHK(shopkeeper)->billct]; bp++)
258 if(bp->bo_id == obj->o_id) {
259 if(!obj->unpaid) pline("onbill: paid obj on bill?");
262 if(obj->unpaid) pline("onbill: unpaid obj not on bill?");
266 /* called with two args on merge */
267 obfree(obj,merge) register struct obj *obj, *merge; {
268 register struct bill_x *bp = onbill(obj);
269 register struct bill_x *bpm;
273 obj->unpaid = 0; /* only for doinvbill */
274 obj->nobj = billobjs;
280 /* this used to be a rename */
281 impossible("obfree: not on bill??");
284 /* this was a merger */
285 bpm->bquan += bp->bquan;
286 ESHK(shopkeeper)->billct--;
287 *bp = bill[ESHK(shopkeeper)->billct];
296 register struct monst *shkp;
298 long robbed = ESHK(shkp)->robbed;
305 if(robbed < 0) robbed = 0;
306 ESHK(shkp)->robbed = robbed;
312 register struct bill_x *bp;
313 register struct monst *shkp;
316 static int dopayobj();
320 for(shkp = fmon; shkp; shkp = shkp->nmon)
321 if(shkp->isshk && dist(shkp->mx,shkp->my) < 3)
323 if(!shkp && u.uinshop &&
324 inroom(shopkeeper->mx,shopkeeper->my) == ESHK(shopkeeper)->shoproom)
328 pline("There is nobody here to receive your payment.");
331 ltmp = ESHK(shkp)->robbed;
332 if(shkp != shopkeeper && NOTANGRY(shkp)) {
334 pline("You do not owe %s anything.", monnam(shkp));
337 pline("You have no money.");
339 long ugold = u.ugold;
342 pline("You give %s the %ld gold pieces he asked for.",
346 pline("You give %s all your gold.", monnam(shkp));
350 pline("Unfortunately, he doesn't look satisfied.");
352 ESHK(shkp)->robbed = 0;
353 ESHK(shkp)->following = 0;
354 if(ESHK(shkp)->shoplevel != dlevel) {
355 /* For convenience's sake, let him disappear */
356 shkp->minvent = 0; /* %% */
365 if(!ESHK(shkp)->billct){
366 pline("You do not owe %s anything.", monnam(shkp));
368 pline("Moreover, you have no money.");
371 if(ESHK(shkp)->robbed){
372 #define min(a,b) ((a<b)?a:b)
373 pline("But since his shop has been robbed recently,");
374 pline("you %srepay %s's expenses.",
375 (u.ugold < ESHK(shkp)->robbed) ? "partially " : "",
377 pay(min(u.ugold, ESHK(shkp)->robbed), shkp);
378 ESHK(shkp)->robbed = 0;
382 pline("But in order to appease %s,",
383 amonnam(shkp, "angry"));
386 pline(" you give him 1000 gold pieces.");
389 pline(" you give him all your money.");
392 if(strncmp(ESHK(shkp)->customer, plname, PL_NSIZ)
394 pline("%s calms down.", Monnam(shkp));
396 } else pline("%s is as angry as ever.",
401 if(shkp != shopkeeper) {
402 impossible("dopay: not to shopkeeper?");
403 if(shopkeeper) setpaid();
406 for(pass = 0; pass <= 1; pass++) {
408 while(tmp < ESHK(shopkeeper)->billct) {
410 if(!pass && !bp->useup) {
414 if(!dopayobj(bp)) return(1);
415 bill[tmp] = bill[--ESHK(shopkeeper)->billct];
418 pline("Thank you for shopping in %s's %s store!",
420 shopnam[rooms[ESHK(shopkeeper)->shoproom].rtype - 8]);
421 NOTANGRY(shopkeeper) = 1;
425 /* return 1 if paid successfully */
426 /* 0 if not enough money */
427 /* -1 if object could not be found (but was paid) */
429 dopayobj(bp) register struct bill_x *bp; {
430 register struct obj *obj;
433 /* find the object on one of the lists */
437 impossible("Shopkeeper administration out of order.");
438 setpaid(); /* be nice to the player */
442 if(!obj->unpaid && !bp->useup){
443 impossible("Paid object on bill??");
447 ltmp = bp->price * bp->bquan;
448 if(ANGRY(shopkeeper)) ltmp += ltmp/3;
450 pline("You don't have gold enough to pay %s.",
455 pay(ltmp, shopkeeper);
456 pline("You bought %s for %ld gold piece%s.",
457 doname(obj), ltmp, plur(ltmp));
459 register struct obj *otmp = billobjs;
461 billobjs = obj->nobj;
463 while(otmp && otmp->nobj != obj) otmp = otmp->nobj;
464 if(otmp) otmp->nobj = obj->nobj;
465 else pline("Error in shopkeeper administration.");
472 /* routine called after dying (or quitting) with nonempty bill */
474 if(shlevel == dlevel && shopkeeper && ESHK(shopkeeper)->billct){
477 shopkeeper->mgold += u.ugold;
479 pline("%s comes and takes all your possessions.",
483 shopkeeper->mgold += total;
484 pline("%s comes and takes the %ld zorkmids you owed him.",
485 Monnam(shopkeeper), total);
487 setpaid(); /* in case we create bones */
491 /* find obj on one of the lists */
494 register struct bill_x *bp;
496 register struct obj *obj;
497 register struct monst *mtmp;
498 register unsigned id = bp->bo_id;
501 obj = o_on(id, billobjs);
502 else if(!(obj = o_on(id, invent)) &&
503 !(obj = o_on(id, fobj)) &&
504 !(obj = o_on(id, fcobj))) {
505 for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
506 if(obj = o_on(id, mtmp->minvent))
508 for(mtmp = fallen_down; mtmp; mtmp = mtmp->nmon)
509 if(obj = o_on(id, mtmp->minvent))
515 static int getprice();
517 /* called in hack.c when we pickup an object */
518 addtobill(obj) register struct obj *obj; {
519 register struct bill_x *bp;
521 (u.ux == ESHK(shopkeeper)->shk.x && u.uy == ESHK(shopkeeper)->shk.y) ||
522 (u.ux == ESHK(shopkeeper)->shd.x && u.uy == ESHK(shopkeeper)->shd.y) ||
523 onbill(obj) /* perhaps we threw it away earlier */
525 if(ESHK(shopkeeper)->billct == BILLSZ){
526 pline("You got that for free!");
529 bp = &bill[ESHK(shopkeeper)->billct];
530 bp->bo_id = obj->o_id;
531 bp->bquan = obj->quan;
533 bp->price = getprice(obj);
534 ESHK(shopkeeper)->billct++;
538 splitbill(obj,otmp) register struct obj *obj, *otmp; {
539 /* otmp has been split off from obj */
540 register struct bill_x *bp;
544 impossible("splitbill: not on bill?");
547 if(bp->bquan < otmp->quan) {
548 impossible("Negative quantity on bill??");
550 if(bp->bquan == otmp->quan) {
551 impossible("Zero quantity on bill??");
553 bp->bquan -= otmp->quan;
555 /* addtobill(otmp); */
556 if(ESHK(shopkeeper)->billct == BILLSZ) otmp->unpaid = 0;
559 bp = &bill[ESHK(shopkeeper)->billct];
560 bp->bo_id = otmp->o_id;
561 bp->bquan = otmp->quan;
564 ESHK(shopkeeper)->billct++;
568 subfrombill(obj) register struct obj *obj; {
571 register struct obj *otmp;
572 register struct bill_x *bp;
573 if(!inshop() || (u.ux == ESHK(shopkeeper)->shk.x && u.uy == ESHK(shopkeeper)->shk.y) ||
574 (u.ux == ESHK(shopkeeper)->shd.x && u.uy == ESHK(shopkeeper)->shd.y))
576 if((bp = onbill(obj)) != 0){
578 if(bp->bquan > obj->quan){
581 bp->bo_id = otmp->o_id = flags.ident++;
582 otmp->quan = (bp->bquan -= obj->quan);
583 otmp->owt = 0; /* superfluous */
586 otmp->nobj = billobjs;
590 ESHK(shopkeeper)->billct--;
591 *bp = bill[ESHK(shopkeeper)->billct];
595 pline("%s didn't notice.", Monnam(shopkeeper));
599 /* he dropped something of his own - probably wants to sell it */
600 if(shopkeeper->msleep || shopkeeper->mfroz ||
601 inroom(shopkeeper->mx,shopkeeper->my) != ESHK(shopkeeper)->shoproom)
603 if(ESHK(shopkeeper)->billct == BILLSZ ||
604 ((tmp = shtypes[rooms[ESHK(shopkeeper)->shoproom].rtype-8]) && tmp != obj->olet)
605 || index("_0", obj->olet)) {
606 pline("%s seems not interested.", Monnam(shopkeeper));
609 ltmp = getprice(obj) * obj->quan;
610 if(ANGRY(shopkeeper)) {
612 NOTANGRY(shopkeeper) = 1;
614 if(ESHK(shopkeeper)->robbed){
615 if((ESHK(shopkeeper)->robbed -= ltmp) < 0)
616 ESHK(shopkeeper)->robbed = 0;
617 pline("Thank you for your contribution to restock this recently plundered shop.");
620 if(ltmp > shopkeeper->mgold)
621 ltmp = shopkeeper->mgold;
622 pay(-ltmp, shopkeeper);
624 pline("%s gladly accepts %s but cannot pay you at present.",
625 Monnam(shopkeeper), doname(obj));
627 pline("You sold %s and got %ld gold piece%s.", doname(obj), ltmp,
632 int mode; /* 0: deliver count 1: paged */
634 register struct bill_x *bp;
635 register struct obj *obj;
636 long totused, thisused;
640 register int cnt = 0;
643 for(bp = bill; bp - bill < ESHK(shopkeeper)->billct; bp++)
645 ((obj = bp_to_obj(bp)) && obj->quan < bp->bquan))
651 impossible("doinvbill: no shopkeeper?");
656 if(page_line("Unpaid articles already used up:") || page_line(""))
660 for(bp = bill; bp - bill < ESHK(shopkeeper)->billct; bp++) {
663 impossible("Bad shopkeeper administration.");
666 if(bp->useup || bp->bquan > obj->quan) {
667 register int cnt, oquan, uquan;
670 uquan = (bp->useup ? bp->bquan : bp->bquan - oquan);
671 thisused = bp->price * uquan;
673 obj->quan = uquan; /* cheat doname */
674 (void) sprintf(buf, "x - %s", doname(obj));
675 obj->quan = oquan; /* restore value */
676 for(cnt = 0; buf[cnt]; cnt++);
679 (void) sprintf(&buf[cnt], " %5ld zorkmids", thisused);
684 (void) sprintf(buf, "Total:%50ld zorkmids", totused);
685 if(page_line("") || page_line(buf))
695 getprice(obj) register struct obj *obj; {
696 register int tmp, ac;
697 static int realhunger();
704 tmp = 10*rnd((obj->otyp == EXPENSIVE_CAMERA) ? 150 : 30);
715 if(obj->otyp == SCR_MAIL)
723 tmp = 10*rnd(5 + (2000/realhunger()));
730 if(ac <= -10) /* probably impossible */
732 tmp = 100 + ac*ac*rnd(10+ac);
735 if(obj->otyp < BOOMERANG)
737 else if(obj->otyp == LONG_SWORD ||
738 obj->otyp == TWO_HANDED_SWORD)
740 else tmp = 10*rnd(75);
743 pline("Strange ..., carrying a chain?");
754 realhunger(){ /* not completely foolproof */
755 register tmp = u.uhunger;
756 register struct obj *otmp = invent;
758 if(otmp->olet == FOOD_SYM && !otmp->unpaid)
759 tmp += objects[otmp->otyp].nutrition;
762 return((tmp <= 0) ? 1 : tmp);
766 register struct obj *obj;
768 register struct monst *shkp = shopkeeper;
770 if(u.uinshop && shkp && !shkp->mfroz && !shkp->msleep &&
772 inroom(u.ux+u.dx, u.uy+u.dy) + 1 == u.uinshop &&
773 shkp->mx == ESHK(shkp)->shk.x && shkp->my == ESHK(shkp)->shk.y &&
774 u.ux == ESHK(shkp)->shd.x && u.uy == ESHK(shkp)->shd.y) {
775 pline("%s nimbly catches the %s.", Monnam(shkp), xname(obj));
776 obj->nobj = shkp->minvent;
784 * shk_move: return 1: he moved 0: he didnt -1: let m_move do it
787 register struct monst *shkp;
789 register struct monst *mtmp;
790 register struct permonst *mdat = shkp->data;
791 register xchar gx,gy,omx,omy,nx,ny,nix,niy;
792 register schar appr,i;
795 schar shkroom,chi,chcnt,cnt;
796 boolean uondoor, satdoor, avoid, badinv;
804 if((udist = dist(omx,omy)) < 3) {
806 (void) hitu(shkp, d(mdat->damn, mdat->damd)+1);
809 if(ESHK(shkp)->following) {
810 if(strncmp(ESHK(shkp)->customer, plname, PL_NSIZ)){
811 pline("Hello %s! I was looking for %s.",
812 plname, ESHK(shkp)->customer);
813 ESHK(shkp)->following = 0;
816 if(!ESHK(shkp)->robbed) { /* impossible? */
817 ESHK(shkp)->following = 0;
820 if(moves > followmsg+4) {
821 pline("Hello %s! Didn't you forget to pay?",
830 shkroom = inroom(omx,omy);
832 gx = ESHK(shkp)->shk.x;
833 gy = ESHK(shkp)->shk.y;
834 satdoor = (gx == omx && gy == omy);
835 if(ESHK(shkp)->following || ((z = holetime()) >= 0 && z*z <= udist)){
838 if(shkroom < 0 || shkroom != inroom(u.ux,u.uy))
840 return(-1); /* leave it to m_move */
841 } else if(ANGRY(shkp)) {
842 long saveBlind = Blind;
844 if(shkp->mcansee && !Invis && cansee(omx,omy)) {
851 #define GDIST(x,y) ((x-gx)*(x-gx)+(y-gy)*(y-gy))
855 uondoor = (u.ux == ESHK(shkp)->shd.x &&
856 u.uy == ESHK(shkp)->shd.y);
858 if(ESHK(shkp)->billct)
859 pline("Hello %s! Will you please pay before leaving?",
861 badinv = (carrying(PICK_AXE) || carrying(ICE_BOX));
862 if(satdoor && badinv)
866 avoid = (u.uinshop && dist(gx,gy) > 8);
870 if(((!ESHK(shkp)->robbed && !ESHK(shkp)->billct) || avoid)
871 && GDIST(omx,omy) < 3){
872 if(!badinv && !online(omx,omy))
879 if(omx == gx && omy == gy)
887 cnt = mfndpos(shkp,poss,info,ALLOW_SSM);
888 if(avoid && uondoor) { /* perhaps we cannot avoid him */
890 if(!(info[i] & NOTONL)) goto notonl_ok;
897 for(i=0; i<cnt; i++){
900 if(levl[nx][ny].typ == ROOM
901 || shkroom != ESHK(shkp)->shoproom
902 || ESHK(shkp)->following) {
904 /* cater for stupid compilers */
907 if(uondoor && (ib = sobj_at(ICE_BOX, nx, ny))) {
908 nix = nx; niy = ny; chi = i; break;
910 if(avoid && (info[i] & NOTONL))
912 if((!appr && !rn2(++chcnt)) ||
914 (appr && (zz = GDIST(nix,niy)) && zz > GDIST(nx,ny))
916 (appr && GDIST(nx,ny) < GDIST(nix,niy))
925 if(nix != omx || niy != omy){
926 if(info[chi] & ALLOW_M){
927 mtmp = m_at(nix,niy);
928 if(hitmm(shkp,mtmp) == 1 && rn2(3) &&
929 hitmm(mtmp,shkp) == 2) return(2);
931 } else if(info[chi] & ALLOW_U){
932 (void) hitu(shkp, d(mdat->damn, mdat->damd)+1);
947 /* He is digging in the shop. */
952 if(u.utraptype == TT_PIT)
953 pline("\"Be careful, sir, or you might fall through the floor.\"");
955 pline("\"Please, do not damage the floor here.\"");
956 } else if(dist(shopkeeper->mx, shopkeeper->my) < 3) {
957 register struct obj *obj, *obj2;
959 pline("%s grabs your backpack!", shkname(shopkeeper));
960 for(obj = invent; obj; obj = obj2) {
962 if(obj->owornmask) continue;
964 obj->nobj = shopkeeper->minvent;
965 shopkeeper->minvent = obj;
974 return(x==u.ux || y==u.uy ||
975 (x-u.ux)*(x-u.ux) == (y-u.uy)*(y-u.uy));
978 /* Does this monster follow me downstairs? */
980 register struct monst *mtmp;
982 return( mtmp->mtame || index("1TVWZi&, ", mtmp->data->mlet)
984 || (mtmp->isshk && ESHK(mtmp)->following)