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 $ */
8 struct monst *shopkeeper = NULL;
9 struct obj *billobjs = NULL;
12 obfree(struct obj *obj, struct obj *merge)
78 replshk(struct monst *mtmp, struct monst *mtmp2)
89 #include "hack.mfndpos.h"
92 #define ESHK(mon) ((struct eshk *)(&(mon->mextra[0])))
93 #define NOTANGRY(mon) mon->mpeaceful
94 #define ANGRY(mon) !NOTANGRY(mon)
96 /* Descriptor of current shopkeeper. Note that the bill need not be
97 * per-shopkeeper, since it is valid only when in a shop. */
98 static struct monst *shopkeeper = NULL;
99 static struct bill_x *bill;
100 static int shlevel = 0; /* level of this shopkeeper */
101 struct obj *billobjs; /* objects on bill with bp->useup */
102 /* only accessed here and by save & restore */
103 static long int total; /* filled by addupbill() */
104 static long int followmsg; /* last time of follow message */
107 * invariants: obj->unpaid iff onbill(obj) [unless bp->useup]
108 * obj->quan <= bp->bquan
111 char shtypes[] = { /* 8 shoptypes: 7 specialized, 1 mixed */
112 RING_SYM, WAND_SYM, WEAPON_SYM, FOOD_SYM, SCROLL_SYM,
113 POTION_SYM, ARMOR_SYM, 0
116 static const char *shopnam[] = {
117 "engagement ring", "walking cane", "antique weapon",
118 "delicatessen", "second hand book", "liquor",
119 "used armor", "assorted antiques"
122 static void setpaid(void);
123 static void addupbill(void);
124 static void findshk(int);
125 static struct bill_x *onbill(struct obj *);
126 static void pay(long, struct monst *);
127 static int dopayobj(struct bill_x *);
128 static struct obj *bp_to_obj(struct bill_x *);
129 static int getprice(struct obj *);
130 static int realhunger(void);
133 shkname(struct monst *mtmp) /* called in do_name.c */
135 return (ESHK(mtmp)->shknam);
139 shkdead(struct monst *mtmp) /* called in mon.c */
141 struct eshk *eshk = ESHK(mtmp);
143 if (eshk->shoplevel == dlevel)
144 rooms[eshk->shoproom].rtype = 0;
145 if (mtmp == shopkeeper) {
148 bill = (struct bill_x *) - 1000; /* dump core when referenced */
153 replshk(struct monst *mtmp, struct monst *mtmp2)
155 if (mtmp == shopkeeper) {
157 bill = &(ESHK(shopkeeper)->bill[0]);
162 setpaid(void) /* caller has checked that shopkeeper exists */
163 /* either we paid or left the shop or he just died */
168 for (obj = invent; obj; obj = obj->nobj)
170 for (obj = fobj; obj; obj = obj->nobj)
172 for (obj = fcobj; obj; obj = obj->nobj)
174 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
175 for (obj = mtmp->minvent; obj; obj = obj->nobj)
177 for (mtmp = fallen_down; mtmp; mtmp = mtmp->nmon)
178 for (obj = mtmp->minvent; obj; obj = obj->nobj)
180 while ((obj = billobjs) != NULL) {
181 billobjs = obj->nobj;
184 ESHK(shopkeeper)->billct = 0;
188 addupbill(void) /* delivers result in total */
189 /* caller has checked that shopkeeper exists */
191 int ct = ESHK(shopkeeper)->billct;
192 struct bill_x *bp = bill;
196 total += bp->price * bp->bquan;
204 int roomno = inroom(u.ux, u.uy);
206 /* Did we just leave a shop? */
208 (u.uinshop != roomno + 1 || shlevel != dlevel || !shopkeeper)) {
210 if (ESHK(shopkeeper)->billct) {
211 if (inroom(shopkeeper->mx, shopkeeper->my)
212 == u.uinshop - 1) /* ab@unido */
213 pline("Somehow you escaped the shop without paying!");
215 pline("You stole for a total worth of %ld zorkmids.",
217 ESHK(shopkeeper)->robbed += total;
219 if ((rooms[ESHK(shopkeeper)->shoproom].rtype == GENERAL)
221 ESHK(shopkeeper)->following = 1;
229 /* Did we just enter a zoo of some kind? */
231 int rt = rooms[roomno].rtype;
234 pline("Welcome to David's treasure zoo!");
235 else if (rt == SWAMP)
236 pline("It looks rather muddy down here.");
237 else if (rt == MORGUE) {
239 pline("Go away! Go away!");
241 pline("You get an uncanny feeling ...");
245 rooms[roomno].rtype = 0;
246 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
247 if (rt != ZOO || !rn2(3))
252 /* Did we just enter a shop? */
253 if (roomno >= 0 && rooms[roomno].rtype >= 8) {
254 if (shlevel != dlevel || !shopkeeper
255 || ESHK(shopkeeper)->shoproom != roomno)
258 rooms[roomno].rtype = 0;
260 } else if (!u.uinshop) {
261 if (!ESHK(shopkeeper)->visitct ||
262 strncmp(ESHK(shopkeeper)->customer, plname, PL_NSIZ)) {
263 /* He seems to be new here */
264 ESHK(shopkeeper)->visitct = 0;
265 ESHK(shopkeeper)->following = 0;
266 strncpy(ESHK(shopkeeper)->customer, plname, PL_NSIZ);
267 NOTANGRY(shopkeeper) = 1;
269 if (!ESHK(shopkeeper)->following) {
272 pline("Hello %s! Welcome%s to %s's %s shop!",
274 ESHK(shopkeeper)->visitct++ ? " again" : "",
276 shopnam[rooms[ESHK(shopkeeper)->shoproom].rtype - 8]);
277 box = carrying(ICE_BOX);
278 pick = carrying(PICK_AXE);
280 if (dochug(shopkeeper)) {
281 u.uinshop = 0; /* he died moving */
284 pline("Will you please leave your %s outside?",
285 (box && pick) ? "box and pick-axe" :
286 box ? "box" : "pick-axe");
289 u.uinshop = roomno + 1;
300 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
301 if (mtmp->isshk && ESHK(mtmp)->shoproom == roomno
302 && ESHK(mtmp)->shoplevel == dlevel) {
304 bill = &(ESHK(shopkeeper)->bill[0]);
306 if (ANGRY(shopkeeper) &&
307 strncmp(ESHK(shopkeeper)->customer, plname, PL_NSIZ))
308 NOTANGRY(shopkeeper) = 1;
309 /* billobjs = 0; -- this is wrong if we save in a shop */
310 /* (and it is harmless to have too many things in billobjs) */
315 bill = (struct bill_x *) - 1000; /* dump core when referenced */
318 static struct bill_x *
319 onbill(struct obj *obj)
325 for (bp = bill; bp < &bill[ESHK(shopkeeper)->billct]; bp++)
326 if (bp->bo_id == obj->o_id) {
328 pline("onbill: paid obj on bill?");
332 pline("onbill: unpaid obj not on bill?");
336 /* called with two args on merge */
338 obfree(struct obj *obj, struct obj *merge)
340 struct bill_x *bp = onbill(obj);
346 obj->unpaid = 0; /* only for doinvbill */
347 obj->nobj = billobjs;
353 /* this used to be a rename */
354 impossible("obfree: not on bill??");
357 /* this was a merger */
358 bpm->bquan += bp->bquan;
359 ESHK(shopkeeper)->billct--;
360 *bp = bill[ESHK(shopkeeper)->billct];
367 pay(long tmp, struct monst *shkp)
369 long robbed = ESHK(shkp)->robbed;
378 ESHK(shkp)->robbed = robbed;
392 for (shkp = fmon; shkp; shkp = shkp->nmon)
393 if (shkp->isshk && dist(shkp->mx, shkp->my) < 3)
395 if (!shkp && u.uinshop &&
396 inroom(shopkeeper->mx, shopkeeper->my) == ESHK(shopkeeper)->shoproom)
400 pline("There is nobody here to receive your payment.");
403 ltmp = ESHK(shkp)->robbed;
404 if (shkp != shopkeeper && NOTANGRY(shkp)) {
406 pline("You do not owe %s anything.", monnam(shkp));
408 pline("You have no money.");
410 long ugold = u.ugold;
412 if (u.ugold > ltmp) {
413 pline("You give %s the %ld gold pieces he asked for.",
417 pline("You give %s all your gold.", monnam(shkp));
420 if (ugold < ltmp / 2)
421 pline("Unfortunately, he doesn't look satisfied.");
423 ESHK(shkp)->robbed = 0;
424 ESHK(shkp)->following = 0;
425 if (ESHK(shkp)->shoplevel != dlevel) {
426 /* For convenience's sake, let him disappear */
427 shkp->minvent = 0; /* %% */
436 if (!ESHK(shkp)->billct) {
437 pline("You do not owe %s anything.", monnam(shkp));
439 pline("Moreover, you have no money.");
442 if (ESHK(shkp)->robbed) {
443 #define min(a, b) ((a < b) ? a : b)
444 pline("But since his shop has been robbed recently,");
445 pline("you %srepay %s's expenses.",
446 (u.ugold < ESHK(shkp)->robbed) ? "partially " : "",
448 pay(min(u.ugold, ESHK(shkp)->robbed), shkp);
449 ESHK(shkp)->robbed = 0;
453 pline("But in order to appease %s,",
454 amonnam(shkp, "angry"));
455 if (u.ugold >= 1000) {
457 pline(" you give him 1000 gold pieces.");
460 pline(" you give him all your money.");
463 if (strncmp(ESHK(shkp)->customer, plname, PL_NSIZ)
465 pline("%s calms down.", Monnam(shkp));
468 pline("%s is as angry as ever.", Monnam(shkp));
472 if (shkp != shopkeeper) {
473 impossible("dopay: not to shopkeeper?");
478 for (pass = 0; pass <= 1; pass++) {
480 while (tmp < ESHK(shopkeeper)->billct) {
482 if (!pass && !bp->useup) {
488 bill[tmp] = bill[--ESHK(shopkeeper)->billct];
491 pline("Thank you for shopping in %s's %s store!",
493 shopnam[rooms[ESHK(shopkeeper)->shoproom].rtype - 8]);
494 NOTANGRY(shopkeeper) = 1;
498 /* return 1 if paid successfully */
499 /* 0 if not enough money */
500 /* -1 if object could not be found (but was paid) */
502 dopayobj(struct bill_x *bp)
507 /* find the object on one of the lists */
511 impossible("Shopkeeper administration out of order.");
512 setpaid(); /* be nice to the player */
516 if (!obj->unpaid && !bp->useup) {
517 impossible("Paid object on bill??");
521 ltmp = bp->price * bp->bquan;
522 if (ANGRY(shopkeeper))
524 if (u.ugold < ltmp) {
525 pline("You don't have gold enough to pay %s.",
530 pay(ltmp, shopkeeper);
531 pline("You bought %s for %ld gold piece%s.",
532 doname(obj), ltmp, plur(ltmp));
534 struct obj *otmp = billobjs;
536 billobjs = obj->nobj;
538 while (otmp && otmp->nobj != obj)
541 otmp->nobj = obj->nobj;
543 pline("Error in shopkeeper administration.");
550 /* routine called after dying (or quitting) with nonempty bill */
554 if (shlevel == dlevel && shopkeeper && ESHK(shopkeeper)->billct) {
556 if (total > u.ugold) {
557 shopkeeper->mgold += u.ugold;
559 pline("%s comes and takes all your possessions.",
563 shopkeeper->mgold += total;
564 pline("%s comes and takes the %ld zorkmids you owed him.",
565 Monnam(shopkeeper), total);
567 setpaid(); /* in case we create bones */
571 /* find obj on one of the lists */
573 bp_to_obj(struct bill_x *bp)
577 unsigned id = bp->bo_id;
580 obj = o_on(id, billobjs);
581 else if (!(obj = o_on(id, invent)) &&
582 !(obj = o_on(id, fobj)) &&
583 !(obj = o_on(id, fcobj))) {
584 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
585 if ((obj = o_on(id, mtmp->minvent)) != NULL)
587 for (mtmp = fallen_down; mtmp; mtmp = mtmp->nmon)
588 if ((obj = o_on(id, mtmp->minvent)) != NULL)
594 /* called in hack.c when we pickup an object */
596 addtobill(struct obj *obj)
601 (u.ux == ESHK(shopkeeper)->shk.x && u.uy == ESHK(shopkeeper)->shk.y) ||
602 (u.ux == ESHK(shopkeeper)->shd.x && u.uy == ESHK(shopkeeper)->shd.y) ||
603 onbill(obj) /* perhaps we threw it away earlier */
606 if (ESHK(shopkeeper)->billct == BILLSZ) {
607 pline("You got that for free!");
610 bp = &bill[ESHK(shopkeeper)->billct];
611 bp->bo_id = obj->o_id;
612 bp->bquan = obj->quan;
614 bp->price = getprice(obj);
615 ESHK(shopkeeper)->billct++;
620 splitbill(struct obj *obj, struct obj *otmp)
622 /* otmp has been split off from obj */
628 impossible("splitbill: not on bill?");
631 if (bp->bquan < otmp->quan)
632 impossible("Negative quantity on bill??");
633 if (bp->bquan == otmp->quan)
634 impossible("Zero quantity on bill??");
635 bp->bquan -= otmp->quan;
637 if (ESHK(shopkeeper)->billct == BILLSZ)
641 bp = &bill[ESHK(shopkeeper)->billct];
642 bp->bo_id = otmp->o_id;
643 bp->bquan = otmp->quan;
646 ESHK(shopkeeper)->billct++;
651 subfrombill(struct obj *obj)
659 (u.ux == ESHK(shopkeeper)->shk.x && u.uy == ESHK(shopkeeper)->shk.y) ||
660 (u.ux == ESHK(shopkeeper)->shd.x && u.uy == ESHK(shopkeeper)->shd.y))
662 if ((bp = onbill(obj)) != NULL) {
664 if (bp->bquan > obj->quan) {
667 bp->bo_id = otmp->o_id = flags.ident++;
668 otmp->quan = (bp->bquan -= obj->quan);
669 otmp->owt = 0; /* superfluous */
672 otmp->nobj = billobjs;
676 ESHK(shopkeeper)->billct--;
677 *bp = bill[ESHK(shopkeeper)->billct];
681 pline("%s didn't notice.", Monnam(shopkeeper));
685 /* he dropped something of his own - probably wants to sell it */
686 if (shopkeeper->msleep || shopkeeper->mfroz ||
687 inroom(shopkeeper->mx, shopkeeper->my) != ESHK(shopkeeper)->shoproom)
689 if (ESHK(shopkeeper)->billct == BILLSZ ||
690 ((tmp = shtypes[rooms[ESHK(shopkeeper)->shoproom].rtype - 8]) &&
691 tmp != obj->olet) || strchr("_0", obj->olet)) {
692 pline("%s seems not interested.", Monnam(shopkeeper));
695 ltmp = getprice(obj) * obj->quan;
696 if (ANGRY(shopkeeper)) {
698 NOTANGRY(shopkeeper) = 1;
701 if (ESHK(shopkeeper)->robbed) {
702 if ((ESHK(shopkeeper)->robbed -= ltmp) < 0)
703 ESHK(shopkeeper)->robbed = 0;
704 pline("Thank you for your contribution to restock this recently plundered shop.");
707 if (ltmp > shopkeeper->mgold)
708 ltmp = shopkeeper->mgold;
709 pay(-ltmp, shopkeeper);
711 pline("%s gladly accepts %s but cannot pay you at present.",
712 Monnam(shopkeeper), doname(obj));
714 pline("You sold %s and got %ld gold piece%s.", doname(obj), ltmp,
719 doinvbill(int mode) /* 0: deliver count 1: paged */
723 long totused, thisused;
730 for (bp = bill; bp - bill < ESHK(shopkeeper)->billct; bp++)
732 ((obj = bp_to_obj(bp)) && obj->quan < bp->bquan))
738 impossible("doinvbill: no shopkeeper?");
743 if (page_line("Unpaid articles already used up:") || page_line(""))
747 for (bp = bill; bp - bill < ESHK(shopkeeper)->billct; bp++) {
750 impossible("Bad shopkeeper administration.");
753 if (bp->useup || bp->bquan > obj->quan) {
754 int cnt, oquan, uquan;
757 uquan = (bp->useup ? bp->bquan : bp->bquan - oquan);
758 thisused = bp->price * uquan;
760 obj->quan = uquan; /* cheat doname */
761 sprintf(buf, "x - %s", doname(obj));
762 obj->quan = oquan; /* restore value */
763 for (cnt = 0; buf[cnt]; cnt++)
767 sprintf(&buf[cnt], " %5ld zorkmids", thisused);
772 sprintf(buf, "Total:%50ld zorkmids", totused);
773 if (page_line("") || page_line(buf))
783 getprice(struct obj *obj)
792 tmp = 10 * rnd((obj->otyp == EXPENSIVE_CAMERA) ? 150 : 30);
803 if (obj->otyp == SCR_MAIL)
811 tmp = 10 * rnd(5 + (2000 / realhunger()));
818 if (ac <= -10) /* probably impossible */
820 tmp = 100 + ac * ac * rnd(10 + ac);
823 if (obj->otyp < BOOMERANG)
825 else if (obj->otyp == LONG_SWORD ||
826 obj->otyp == TWO_HANDED_SWORD)
832 pline("Strange ..., carrying a chain?");
843 realhunger(void) /* not completely foolproof */
846 struct obj *otmp = invent;
849 if (otmp->olet == FOOD_SYM && !otmp->unpaid)
850 tmp += objects[otmp->otyp].nutrition;
853 return ((tmp <= 0) ? 1 : tmp);
857 shkcatch(struct obj *obj)
859 struct monst *shkp = shopkeeper;
861 if (u.uinshop && shkp && !shkp->mfroz && !shkp->msleep &&
863 inroom(u.ux + u.dx, u.uy + u.dy) + 1 == u.uinshop &&
864 shkp->mx == ESHK(shkp)->shk.x && shkp->my == ESHK(shkp)->shk.y &&
865 u.ux == ESHK(shkp)->shd.x && u.uy == ESHK(shkp)->shd.y) {
866 pline("%s nimbly catches the %s.", Monnam(shkp), xname(obj));
867 obj->nobj = shkp->minvent;
875 * shk_move: return 1: he moved 0: he didnt -1: let m_move do it
878 shk_move(struct monst *shkp)
881 struct permonst *mdat = shkp->data;
882 xchar gx, gy, omx, omy, nx, ny, nix, niy;
886 schar shkroom, chi, chcnt, cnt;
887 boolean uondoor = 0, satdoor, avoid = 0, badinv;
890 struct obj *ib = NULL;
895 if ((udist = dist(omx, omy)) < 3) {
897 hitu(shkp, d(mdat->damn, mdat->damd) + 1);
900 if (ESHK(shkp)->following) {
901 if (strncmp(ESHK(shkp)->customer, plname, PL_NSIZ)) {
902 pline("Hello %s! I was looking for %s.",
903 plname, ESHK(shkp)->customer);
904 ESHK(shkp)->following = 0;
907 if (!ESHK(shkp)->robbed) { /* impossible? */
908 ESHK(shkp)->following = 0;
911 if (moves > followmsg + 4) {
912 pline("Hello %s! Didn't you forget to pay?",
921 shkroom = inroom(omx, omy);
923 gx = ESHK(shkp)->shk.x;
924 gy = ESHK(shkp)->shk.y;
925 satdoor = (gx == omx && gy == omy);
926 if (ESHK(shkp)->following || ((z = holetime()) >= 0 && z * z <= udist)) {
929 if (shkroom < 0 || shkroom != inroom(u.ux, u.uy))
931 return (-1); /* leave it to m_move */
932 } else if (ANGRY(shkp)) {
933 long saveBlind = Blind;
935 if (shkp->mcansee && !Invis && cansee(omx, omy)) {
942 #define GDIST(x, y) ((x - gx) * (x - gx) + (y - gy) * (y - gy))
946 uondoor = (u.ux == ESHK(shkp)->shd.x &&
947 u.uy == ESHK(shkp)->shd.y);
949 if (ESHK(shkp)->billct)
950 pline("Hello %s! Will you please pay before leaving?",
952 badinv = (carrying(PICK_AXE) || carrying(ICE_BOX));
953 if (satdoor && badinv)
957 avoid = (u.uinshop && dist(gx, gy) > 8);
961 if (((!ESHK(shkp)->robbed && !ESHK(shkp)->billct) || avoid)
962 && GDIST(omx, omy) < 3) {
963 if (!badinv && !online(omx, omy))
970 if (omx == gx && omy == gy)
978 cnt = mfndpos(shkp, poss, info, ALLOW_SSM);
979 if (avoid && uondoor) { /* perhaps we cannot avoid him */
980 for (i = 0; i < cnt; i++)
981 if (!(info[i] & NOTONL))
989 for (i = 0; i < cnt; i++) {
992 if (levl[nx][ny].typ == ROOM
993 || shkroom != ESHK(shkp)->shoproom
994 || ESHK(shkp)->following) {
996 /* cater for stupid compilers */
999 if (uondoor && (ib = sobj_at(ICE_BOX, nx, ny))) {
1004 if (avoid && (info[i] & NOTONL))
1006 if ((!appr && !rn2(++chcnt)) ||
1008 (appr && (zz = GDIST(nix, niy)) && zz > GDIST(nx, ny))
1010 (appr && GDIST(nx, ny) < GDIST(nix, niy))
1019 if (nix != omx || niy != omy) {
1020 if (info[chi] & ALLOW_M) {
1021 mtmp = m_at(nix, niy);
1022 if (hitmm(shkp, mtmp) == 1 && rn2(3) &&
1023 hitmm(mtmp, shkp) == 2)
1026 } else if (info[chi] & ALLOW_U) {
1027 hitu(shkp, d(mdat->damn, mdat->damd) + 1);
1042 /* He is digging in the shop. */
1047 if (u.utraptype == TT_PIT)
1048 pline("\"Be careful, sir, or you might fall through the floor.\"");
1050 pline("\"Please, do not damage the floor here.\"");
1051 } else if (dist(shopkeeper->mx, shopkeeper->my) < 3) {
1052 struct obj *obj, *obj2;
1054 pline("%s grabs your backpack!", shkname(shopkeeper));
1055 for (obj = invent; obj; obj = obj2) {
1060 obj->nobj = shopkeeper->minvent;
1061 shopkeeper->minvent = obj;
1070 online(int x, int y)
1072 return (x == u.ux || y == u.uy ||
1073 (x - u.ux) * (x - u.ux) == (y - u.uy) * (y - u.uy));
1076 /* Does this monster follow me downstairs? */
1078 follower(struct monst *mtmp)
1080 return (mtmp->mtame || strchr("1TVWZi&, ", mtmp->data->mlet)
1082 || (mtmp->isshk && ESHK(mtmp)->following)