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