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