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.6 2006/08/21 19:45:32 pavalos Exp $ */
9 struct monst *shopkeeper = 0;
10 struct obj *billobjs = 0;
13 obfree(struct obj *obj, struct obj *merge)
79 replshk(struct monst *mtmp, struct monst *mtmp2)
90 #include "hack.mfndpos.h"
93 #define ESHK(mon) ((struct eshk *)(&(mon->mextra[0])))
94 #define NOTANGRY(mon) mon->mpeaceful
95 #define ANGRY(mon) !NOTANGRY(mon)
97 /* Descriptor of current shopkeeper. Note that the bill need not be
98 * per-shopkeeper, since it is valid only when in a shop. */
99 static struct monst *shopkeeper = 0;
100 static struct bill_x *bill;
101 static int shlevel = 0; /* level of this shopkeeper */
102 struct obj *billobjs; /* objects on bill with bp->useup */
103 /* only accessed here and by save & restore */
104 static long int total; /* filled by addupbill() */
105 static long int followmsg; /* last time of follow message */
108 * invariants: obj->unpaid iff onbill(obj) [unless bp->useup]
109 * obj->quan <= bp->bquan
112 char shtypes[] = { /* 8 shoptypes: 7 specialized, 1 mixed */
113 RING_SYM, WAND_SYM, WEAPON_SYM, FOOD_SYM, SCROLL_SYM,
114 POTION_SYM, ARMOR_SYM, 0
117 static const char *shopnam[] = {
118 "engagement ring", "walking cane", "antique weapon",
119 "delicatessen", "second hand book", "liquor",
120 "used armor", "assorted antiques"
123 static void setpaid(void);
124 static void addupbill(void);
125 static void findshk(int);
126 static struct bill_x *onbill(struct obj *);
127 static void pay(long, struct monst *);
128 static int dopayobj(struct bill_x *);
129 static struct obj *bp_to_obj(struct bill_x *);
130 static int getprice(struct obj *);
131 static int realhunger(void);
134 shkname(struct monst *mtmp) /* called in do_name.c */
136 return (ESHK(mtmp)->shknam);
140 shkdead(struct monst *mtmp) /* called in mon.c */
142 struct eshk *eshk = ESHK(mtmp);
144 if (eshk->shoplevel == dlevel)
145 rooms[eshk->shoproom].rtype = 0;
146 if (mtmp == shopkeeper) {
149 bill = (struct bill_x *) - 1000; /* dump core when referenced */
154 replshk(struct monst *mtmp, struct monst *mtmp2)
156 if (mtmp == shopkeeper) {
158 bill = &(ESHK(shopkeeper)->bill[0]);
163 setpaid(void) /* caller has checked that shopkeeper exists */
164 /* either we paid or left the shop or he just died */
169 for (obj = invent; obj; obj = obj->nobj)
171 for (obj = fobj; obj; obj = obj->nobj)
173 for (obj = fcobj; obj; obj = obj->nobj)
175 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
176 for (obj = mtmp->minvent; obj; obj = obj->nobj)
178 for (mtmp = fallen_down; mtmp; mtmp = mtmp->nmon)
179 for (obj = mtmp->minvent; obj; obj = obj->nobj)
181 while ((obj = billobjs) != NULL) {
182 billobjs = obj->nobj;
185 ESHK(shopkeeper)->billct = 0;
189 addupbill(void) /* delivers result in total */
190 /* caller has checked that shopkeeper exists */
192 int ct = ESHK(shopkeeper)->billct;
193 struct bill_x *bp = bill;
197 total += bp->price * bp->bquan;
205 int roomno = inroom(u.ux, u.uy);
207 /* Did we just leave a shop? */
209 (u.uinshop != roomno + 1 || shlevel != dlevel || !shopkeeper)) {
211 if (ESHK(shopkeeper)->billct) {
212 if (inroom(shopkeeper->mx, shopkeeper->my)
213 == u.uinshop - 1) /* ab@unido */
214 pline("Somehow you escaped the shop without paying!");
216 pline("You stole for a total worth of %ld zorkmids.",
218 ESHK(shopkeeper)->robbed += total;
220 if ((rooms[ESHK(shopkeeper)->shoproom].rtype == GENERAL)
222 ESHK(shopkeeper)->following = 1;
230 /* Did we just enter a zoo of some kind? */
232 int rt = rooms[roomno].rtype;
235 pline("Welcome to David's treasure zoo!");
236 else if (rt == SWAMP)
237 pline("It looks rather muddy down here.");
238 else if (rt == MORGUE) {
240 pline("Go away! Go away!");
242 pline("You get an uncanny feeling ...");
246 rooms[roomno].rtype = 0;
247 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
248 if (rt != ZOO || !rn2(3))
253 /* Did we just enter a shop? */
254 if (roomno >= 0 && rooms[roomno].rtype >= 8) {
255 if (shlevel != dlevel || !shopkeeper
256 || ESHK(shopkeeper)->shoproom != roomno)
259 rooms[roomno].rtype = 0;
261 } else if (!u.uinshop) {
262 if (!ESHK(shopkeeper)->visitct ||
263 strncmp(ESHK(shopkeeper)->customer, plname, PL_NSIZ)) {
264 /* He seems to be new here */
265 ESHK(shopkeeper)->visitct = 0;
266 ESHK(shopkeeper)->following = 0;
267 strncpy(ESHK(shopkeeper)->customer, plname, PL_NSIZ);
268 NOTANGRY(shopkeeper) = 1;
270 if (!ESHK(shopkeeper)->following) {
273 pline("Hello %s! Welcome%s to %s's %s shop!",
275 ESHK(shopkeeper)->visitct++ ? " again" : "",
277 shopnam[rooms[ESHK(shopkeeper)->shoproom].rtype - 8]);
278 box = carrying(ICE_BOX);
279 pick = carrying(PICK_AXE);
281 if (dochug(shopkeeper)) {
282 u.uinshop = 0; /* he died moving */
285 pline("Will you please leave your %s outside?",
286 (box && pick) ? "box and pick-axe" :
287 box ? "box" : "pick-axe");
290 u.uinshop = roomno + 1;
301 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
302 if (mtmp->isshk && ESHK(mtmp)->shoproom == roomno
303 && ESHK(mtmp)->shoplevel == dlevel) {
305 bill = &(ESHK(shopkeeper)->bill[0]);
307 if (ANGRY(shopkeeper) &&
308 strncmp(ESHK(shopkeeper)->customer, plname, PL_NSIZ))
309 NOTANGRY(shopkeeper) = 1;
310 /* billobjs = 0; -- this is wrong if we save in a shop */
311 /* (and it is harmless to have too many things in billobjs) */
316 bill = (struct bill_x *) - 1000; /* dump core when referenced */
319 static struct bill_x *
320 onbill(struct obj *obj)
326 for (bp = bill; bp < &bill[ESHK(shopkeeper)->billct]; bp++)
327 if (bp->bo_id == obj->o_id) {
329 pline("onbill: paid obj on bill?");
333 pline("onbill: unpaid obj not on bill?");
337 /* called with two args on merge */
339 obfree(struct obj *obj, struct obj *merge)
341 struct bill_x *bp = onbill(obj);
347 obj->unpaid = 0; /* only for doinvbill */
348 obj->nobj = billobjs;
354 /* this used to be a rename */
355 impossible("obfree: not on bill??");
358 /* this was a merger */
359 bpm->bquan += bp->bquan;
360 ESHK(shopkeeper)->billct--;
361 *bp = bill[ESHK(shopkeeper)->billct];
368 pay(long tmp, struct monst *shkp)
370 long robbed = ESHK(shkp)->robbed;
379 ESHK(shkp)->robbed = robbed;
393 for (shkp = fmon; shkp; shkp = shkp->nmon)
394 if (shkp->isshk && dist(shkp->mx, shkp->my) < 3)
396 if (!shkp && u.uinshop &&
397 inroom(shopkeeper->mx, shopkeeper->my) == ESHK(shopkeeper)->shoproom)
401 pline("There is nobody here to receive your payment.");
404 ltmp = ESHK(shkp)->robbed;
405 if (shkp != shopkeeper && NOTANGRY(shkp)) {
407 pline("You do not owe %s anything.", monnam(shkp));
409 pline("You have no money.");
411 long ugold = u.ugold;
413 if (u.ugold > ltmp) {
414 pline("You give %s the %ld gold pieces he asked for.",
418 pline("You give %s all your gold.", monnam(shkp));
421 if (ugold < ltmp / 2)
422 pline("Unfortunately, he doesn't look satisfied.");
424 ESHK(shkp)->robbed = 0;
425 ESHK(shkp)->following = 0;
426 if (ESHK(shkp)->shoplevel != dlevel) {
427 /* For convenience's sake, let him disappear */
428 shkp->minvent = 0; /* %% */
437 if (!ESHK(shkp)->billct) {
438 pline("You do not owe %s anything.", monnam(shkp));
440 pline("Moreover, you have no money.");
443 if (ESHK(shkp)->robbed) {
444 #define min(a, b) ((a < b) ? a : b)
445 pline("But since his shop has been robbed recently,");
446 pline("you %srepay %s's expenses.",
447 (u.ugold < ESHK(shkp)->robbed) ? "partially " : "",
449 pay(min(u.ugold, ESHK(shkp)->robbed), shkp);
450 ESHK(shkp)->robbed = 0;
454 pline("But in order to appease %s,",
455 amonnam(shkp, "angry"));
456 if (u.ugold >= 1000) {
458 pline(" you give him 1000 gold pieces.");
461 pline(" you give him all your money.");
464 if (strncmp(ESHK(shkp)->customer, plname, PL_NSIZ)
466 pline("%s calms down.", Monnam(shkp));
469 pline("%s is as angry as ever.", Monnam(shkp));
473 if (shkp != shopkeeper) {
474 impossible("dopay: not to shopkeeper?");
479 for (pass = 0; pass <= 1; pass++) {
481 while (tmp < ESHK(shopkeeper)->billct) {
483 if (!pass && !bp->useup) {
489 bill[tmp] = bill[--ESHK(shopkeeper)->billct];
492 pline("Thank you for shopping in %s's %s store!",
494 shopnam[rooms[ESHK(shopkeeper)->shoproom].rtype - 8]);
495 NOTANGRY(shopkeeper) = 1;
499 /* return 1 if paid successfully */
500 /* 0 if not enough money */
501 /* -1 if object could not be found (but was paid) */
503 dopayobj(struct bill_x *bp)
508 /* find the object on one of the lists */
512 impossible("Shopkeeper administration out of order.");
513 setpaid(); /* be nice to the player */
517 if (!obj->unpaid && !bp->useup) {
518 impossible("Paid object on bill??");
522 ltmp = bp->price * bp->bquan;
523 if (ANGRY(shopkeeper))
525 if (u.ugold < ltmp) {
526 pline("You don't have gold enough to pay %s.",
531 pay(ltmp, shopkeeper);
532 pline("You bought %s for %ld gold piece%s.",
533 doname(obj), ltmp, plur(ltmp));
535 struct obj *otmp = billobjs;
537 billobjs = obj->nobj;
539 while (otmp && otmp->nobj != obj)
542 otmp->nobj = obj->nobj;
544 pline("Error in shopkeeper administration.");
551 /* routine called after dying (or quitting) with nonempty bill */
555 if (shlevel == dlevel && shopkeeper && ESHK(shopkeeper)->billct) {
557 if (total > u.ugold) {
558 shopkeeper->mgold += u.ugold;
560 pline("%s comes and takes all your possessions.",
564 shopkeeper->mgold += total;
565 pline("%s comes and takes the %ld zorkmids you owed him.",
566 Monnam(shopkeeper), total);
568 setpaid(); /* in case we create bones */
572 /* find obj on one of the lists */
574 bp_to_obj(struct bill_x *bp)
578 unsigned id = bp->bo_id;
581 obj = o_on(id, billobjs);
582 else if (!(obj = o_on(id, invent)) &&
583 !(obj = o_on(id, fobj)) &&
584 !(obj = o_on(id, fcobj))) {
585 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
586 if ((obj = o_on(id, mtmp->minvent)) != NULL)
588 for (mtmp = fallen_down; mtmp; mtmp = mtmp->nmon)
589 if ((obj = o_on(id, mtmp->minvent)) != NULL)
595 /* called in hack.c when we pickup an object */
597 addtobill(struct obj *obj)
602 (u.ux == ESHK(shopkeeper)->shk.x && u.uy == ESHK(shopkeeper)->shk.y) ||
603 (u.ux == ESHK(shopkeeper)->shd.x && u.uy == ESHK(shopkeeper)->shd.y) ||
604 onbill(obj) /* perhaps we threw it away earlier */
607 if (ESHK(shopkeeper)->billct == BILLSZ) {
608 pline("You got that for free!");
611 bp = &bill[ESHK(shopkeeper)->billct];
612 bp->bo_id = obj->o_id;
613 bp->bquan = obj->quan;
615 bp->price = getprice(obj);
616 ESHK(shopkeeper)->billct++;
621 splitbill(struct obj *obj, struct obj *otmp)
623 /* otmp has been split off from obj */
629 impossible("splitbill: not on bill?");
632 if (bp->bquan < otmp->quan)
633 impossible("Negative quantity on bill??");
634 if (bp->bquan == otmp->quan)
635 impossible("Zero quantity on bill??");
636 bp->bquan -= otmp->quan;
638 if (ESHK(shopkeeper)->billct == BILLSZ)
642 bp = &bill[ESHK(shopkeeper)->billct];
643 bp->bo_id = otmp->o_id;
644 bp->bquan = otmp->quan;
647 ESHK(shopkeeper)->billct++;
652 subfrombill(struct obj *obj)
660 (u.ux == ESHK(shopkeeper)->shk.x && u.uy == ESHK(shopkeeper)->shk.y) ||
661 (u.ux == ESHK(shopkeeper)->shd.x && u.uy == ESHK(shopkeeper)->shd.y))
663 if ((bp = onbill(obj)) != 0) {
665 if (bp->bquan > obj->quan) {
668 bp->bo_id = otmp->o_id = flags.ident++;
669 otmp->quan = (bp->bquan -= obj->quan);
670 otmp->owt = 0; /* superfluous */
673 otmp->nobj = billobjs;
677 ESHK(shopkeeper)->billct--;
678 *bp = bill[ESHK(shopkeeper)->billct];
682 pline("%s didn't notice.", Monnam(shopkeeper));
686 /* he dropped something of his own - probably wants to sell it */
687 if (shopkeeper->msleep || shopkeeper->mfroz ||
688 inroom(shopkeeper->mx, shopkeeper->my) != ESHK(shopkeeper)->shoproom)
690 if (ESHK(shopkeeper)->billct == BILLSZ ||
691 ((tmp = shtypes[rooms[ESHK(shopkeeper)->shoproom].rtype - 8]) &&
692 tmp != obj->olet) || strchr("_0", obj->olet)) {
693 pline("%s seems not interested.", Monnam(shopkeeper));
696 ltmp = getprice(obj) * obj->quan;
697 if (ANGRY(shopkeeper)) {
699 NOTANGRY(shopkeeper) = 1;
702 if (ESHK(shopkeeper)->robbed) {
703 if ((ESHK(shopkeeper)->robbed -= ltmp) < 0)
704 ESHK(shopkeeper)->robbed = 0;
705 pline("Thank you for your contribution to restock this recently plundered shop.");
708 if (ltmp > shopkeeper->mgold)
709 ltmp = shopkeeper->mgold;
710 pay(-ltmp, shopkeeper);
712 pline("%s gladly accepts %s but cannot pay you at present.",
713 Monnam(shopkeeper), doname(obj));
715 pline("You sold %s and got %ld gold piece%s.", doname(obj), ltmp,
720 doinvbill(int mode) /* 0: deliver count 1: paged */
724 long totused, thisused;
731 for (bp = bill; bp - bill < ESHK(shopkeeper)->billct; bp++)
733 ((obj = bp_to_obj(bp)) && obj->quan < bp->bquan))
739 impossible("doinvbill: no shopkeeper?");
744 if (page_line("Unpaid articles already used up:") || page_line(""))
748 for (bp = bill; bp - bill < ESHK(shopkeeper)->billct; bp++) {
751 impossible("Bad shopkeeper administration.");
754 if (bp->useup || bp->bquan > obj->quan) {
755 int cnt, oquan, uquan;
758 uquan = (bp->useup ? bp->bquan : bp->bquan - oquan);
759 thisused = bp->price * uquan;
761 obj->quan = uquan; /* cheat doname */
762 sprintf(buf, "x - %s", doname(obj));
763 obj->quan = oquan; /* restore value */
764 for (cnt = 0; buf[cnt]; cnt++)
768 sprintf(&buf[cnt], " %5ld zorkmids", thisused);
773 sprintf(buf, "Total:%50ld zorkmids", totused);
774 if (page_line("") || page_line(buf))
784 getprice(struct obj *obj)
793 tmp = 10 * rnd((obj->otyp == EXPENSIVE_CAMERA) ? 150 : 30);
804 if (obj->otyp == SCR_MAIL)
812 tmp = 10 * rnd(5 + (2000 / realhunger()));
819 if (ac <= -10) /* probably impossible */
821 tmp = 100 + ac * ac * rnd(10 + ac);
824 if (obj->otyp < BOOMERANG)
826 else if (obj->otyp == LONG_SWORD ||
827 obj->otyp == TWO_HANDED_SWORD)
833 pline("Strange ..., carrying a chain?");
844 realhunger(void) /* not completely foolproof */
847 struct obj *otmp = invent;
850 if (otmp->olet == FOOD_SYM && !otmp->unpaid)
851 tmp += objects[otmp->otyp].nutrition;
854 return ((tmp <= 0) ? 1 : tmp);
858 shkcatch(struct obj *obj)
860 struct monst *shkp = shopkeeper;
862 if (u.uinshop && shkp && !shkp->mfroz && !shkp->msleep &&
864 inroom(u.ux + u.dx, u.uy + u.dy) + 1 == u.uinshop &&
865 shkp->mx == ESHK(shkp)->shk.x && shkp->my == ESHK(shkp)->shk.y &&
866 u.ux == ESHK(shkp)->shd.x && u.uy == ESHK(shkp)->shd.y) {
867 pline("%s nimbly catches the %s.", Monnam(shkp), xname(obj));
868 obj->nobj = shkp->minvent;
876 * shk_move: return 1: he moved 0: he didnt -1: let m_move do it
879 shk_move(struct monst *shkp)
882 struct permonst *mdat = shkp->data;
883 xchar gx, gy, omx, omy, nx, ny, nix, niy;
887 schar shkroom, chi, chcnt, cnt;
888 boolean uondoor = 0, satdoor, avoid = 0, badinv;
896 if ((udist = dist(omx, omy)) < 3) {
898 hitu(shkp, d(mdat->damn, mdat->damd) + 1);
901 if (ESHK(shkp)->following) {
902 if (strncmp(ESHK(shkp)->customer, plname, PL_NSIZ)) {
903 pline("Hello %s! I was looking for %s.",
904 plname, ESHK(shkp)->customer);
905 ESHK(shkp)->following = 0;
908 if (!ESHK(shkp)->robbed) { /* impossible? */
909 ESHK(shkp)->following = 0;
912 if (moves > followmsg + 4) {
913 pline("Hello %s! Didn't you forget to pay?",
922 shkroom = inroom(omx, omy);
924 gx = ESHK(shkp)->shk.x;
925 gy = ESHK(shkp)->shk.y;
926 satdoor = (gx == omx && gy == omy);
927 if (ESHK(shkp)->following || ((z = holetime()) >= 0 && z * z <= udist)) {
930 if (shkroom < 0 || shkroom != inroom(u.ux, u.uy))
932 return (-1); /* leave it to m_move */
933 } else if (ANGRY(shkp)) {
934 long saveBlind = Blind;
936 if (shkp->mcansee && !Invis && cansee(omx, omy)) {
943 #define GDIST(x, y) ((x - gx) * (x - gx) + (y - gy) * (y - gy))
947 uondoor = (u.ux == ESHK(shkp)->shd.x &&
948 u.uy == ESHK(shkp)->shd.y);
950 if (ESHK(shkp)->billct)
951 pline("Hello %s! Will you please pay before leaving?",
953 badinv = (carrying(PICK_AXE) || carrying(ICE_BOX));
954 if (satdoor && badinv)
958 avoid = (u.uinshop && dist(gx, gy) > 8);
962 if (((!ESHK(shkp)->robbed && !ESHK(shkp)->billct) || avoid)
963 && GDIST(omx, omy) < 3) {
964 if (!badinv && !online(omx, omy))
971 if (omx == gx && omy == gy)
979 cnt = mfndpos(shkp, poss, info, ALLOW_SSM);
980 if (avoid && uondoor) { /* perhaps we cannot avoid him */
981 for (i = 0; i < cnt; i++)
982 if (!(info[i] & NOTONL))
990 for (i = 0; i < cnt; i++) {
993 if (levl[nx][ny].typ == ROOM
994 || shkroom != ESHK(shkp)->shoproom
995 || ESHK(shkp)->following) {
997 /* cater for stupid compilers */
1000 if (uondoor && (ib = sobj_at(ICE_BOX, nx, ny))) {
1005 if (avoid && (info[i] & NOTONL))
1007 if ((!appr && !rn2(++chcnt)) ||
1009 (appr && (zz = GDIST(nix, niy)) && zz > GDIST(nx, ny))
1011 (appr && GDIST(nx, ny) < GDIST(nix, niy))
1020 if (nix != omx || niy != omy) {
1021 if (info[chi] & ALLOW_M) {
1022 mtmp = m_at(nix, niy);
1023 if (hitmm(shkp, mtmp) == 1 && rn2(3) &&
1024 hitmm(mtmp, shkp) == 2)
1027 } else if (info[chi] & ALLOW_U) {
1028 hitu(shkp, d(mdat->damn, mdat->damd) + 1);
1043 /* He is digging in the shop. */
1048 if (u.utraptype == TT_PIT)
1049 pline("\"Be careful, sir, or you might fall through the floor.\"");
1051 pline("\"Please, do not damage the floor here.\"");
1052 } else if (dist(shopkeeper->mx, shopkeeper->my) < 3) {
1053 struct obj *obj, *obj2;
1055 pline("%s grabs your backpack!", shkname(shopkeeper));
1056 for (obj = invent; obj; obj = obj2) {
1061 obj->nobj = shopkeeper->minvent;
1062 shopkeeper->minvent = obj;
1071 online(int x, int y)
1073 return (x == u.ux || y == u.uy ||
1074 (x - u.ux) * (x - u.ux) == (y - u.uy) * (y - u.uy));
1077 /* Does this monster follow me downstairs? */
1079 follower(struct monst *mtmp)
1081 return (mtmp->mtame || strchr("1TVWZi&, ", mtmp->data->mlet)
1083 || (mtmp->isshk && ESHK(mtmp)->following)