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