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