Source:NetHack 3.3.0/shk.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to shk.c from the source code of NetHack 3.3.0. To link to a particular line, write [[NetHack 3.3.0/shk.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1.    /*	SCCS Id: @(#)shk.c	3.3	99/03/22	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    #include "hack.h"
6.    #include "eshk.h"
7.    
8.    /*#define DEBUG*/
9.    
10.   #define PAY_SOME    2
11.   #define PAY_BUY     1
12.   #define PAY_CANT    0	/* too poor */
13.   #define PAY_SKIP  (-1)
14.   #define PAY_BROKE (-2)
15.   
16.   #ifdef KOPS
17.   STATIC_DCL void FDECL(makekops, (coord *));
18.   STATIC_DCL void FDECL(call_kops, (struct monst *,BOOLEAN_P));
19.   # ifdef OVLB
20.   STATIC_DCL void FDECL(kops_gone, (BOOLEAN_P));
21.   # endif /* OVLB */
22.   #endif /* KOPS */
23.   
24.   #define IS_SHOP(x)	(rooms[x].rtype >= SHOPBASE)
25.   
26.   extern const struct shclass shtypes[];	/* defined in shknam.c */
27.   
28.   STATIC_VAR NEARDATA long int followmsg;	/* last time of follow message */
29.   
30.   STATIC_DCL void FDECL(setpaid, (struct monst *));
31.   STATIC_DCL long FDECL(addupbill, (struct monst *));
32.   STATIC_DCL void FDECL(pacify_shk, (struct monst *));
33.   STATIC_DCL struct bill_x *FDECL(onbill, (struct obj *, struct monst *, BOOLEAN_P));
34.   STATIC_DCL struct monst *FDECL(next_shkp, (struct monst *, BOOLEAN_P));
35.   STATIC_DCL long FDECL(shop_debt, (struct eshk *));
36.   STATIC_DCL char *FDECL(shk_owns, (char *,struct obj *));
37.   STATIC_DCL char *FDECL(mon_owns, (char *,struct obj *));
38.   STATIC_DCL void FDECL(clear_unpaid,(struct obj *));
39.   STATIC_DCL long FDECL(check_credit, (long, struct monst *));
40.   STATIC_DCL void FDECL(pay, (long, struct monst *));
41.   STATIC_DCL long FDECL(get_cost, (struct obj *, struct monst *));
42.   STATIC_DCL long FDECL(set_cost, (struct obj *, struct monst *));
43.   STATIC_DCL const char *FDECL(shk_embellish, (struct obj *, long));
44.   STATIC_DCL long FDECL(cost_per_charge, (struct monst *,struct obj *,BOOLEAN_P));
45.   STATIC_DCL long FDECL(cheapest_item, (struct monst *));
46.   STATIC_DCL int FDECL(dopayobj, (struct monst *, struct bill_x *,
47.   			    struct obj **, int, BOOLEAN_P));
48.   STATIC_DCL long FDECL(stolen_container, (struct obj *, struct monst *, long,
49.   				     BOOLEAN_P));
50.   STATIC_DCL long FDECL(getprice, (struct obj *,BOOLEAN_P));
51.   STATIC_DCL void FDECL(shk_names_obj,
52.   		 (struct monst *,struct obj *,const char *,long,const char *));
53.   STATIC_DCL struct obj *FDECL(bp_to_obj, (struct bill_x *));
54.   STATIC_DCL boolean FDECL(inherits, (struct monst *, int, BOOLEAN_P));
55.   STATIC_DCL void FDECL(set_repo_loc, (struct eshk *));
56.   STATIC_DCL boolean NDECL(angry_shk_exists);
57.   STATIC_DCL void FDECL(rile_shk, (struct monst *));
58.   STATIC_DCL void FDECL(remove_damage, (struct monst *, BOOLEAN_P));
59.   STATIC_DCL void FDECL(sub_one_frombill, (struct obj *, struct monst *));
60.   STATIC_DCL void FDECL(add_one_tobill, (struct obj *, BOOLEAN_P));
61.   STATIC_DCL void FDECL(dropped_container, (struct obj *, struct monst *,
62.   				      BOOLEAN_P));
63.   STATIC_DCL void FDECL(add_to_billobjs, (struct obj *));
64.   STATIC_DCL void FDECL(bill_box_content, (struct obj *, BOOLEAN_P, BOOLEAN_P,
65.   				     struct monst *));
66.   
67.   #ifdef OVLB
68.   /*
69.   	invariants: obj->unpaid iff onbill(obj) [unless bp->useup]
70.   		obj->quan <= bp->bquan
71.    */
72.   
73.   STATIC_OVL struct monst *
74.   next_shkp(shkp, withbill)
75.   register struct monst *shkp;
76.   register boolean withbill;
77.   {
78.   	for (; shkp; shkp = shkp->nmon)
79.   	    if (shkp->isshk)
80.   		if (ESHK(shkp)->billct || !withbill) break;
81.   
82.   	if (shkp) {
83.   	    if (NOTANGRY(shkp)) {
84.   		if (ESHK(shkp)->surcharge) pacify_shk(shkp);
85.   	    } else {
86.   		if (!ESHK(shkp)->surcharge) rile_shk(shkp);
87.   	    }
88.   	}
89.   	return(shkp);
90.   }
91.   
92.   char *
93.   shkname(mtmp)				/* called in do_name.c */
94.   register struct monst *mtmp;
95.   {
96.   	return(ESHK(mtmp)->shknam);
97.   }
98.   
99.   void
100.  shkgone(mtmp)				/* called in mon.c */
101.  register struct monst *mtmp;
102.  {
103.  	register struct eshk *eshk = ESHK(mtmp);
104.  
105.  	if(on_level(&(eshk->shoplevel), &u.uz)) {
106.  		remove_damage(mtmp, TRUE);
107.  		rooms[eshk->shoproom - ROOMOFFSET].resident
108.  						  = (struct monst *)0;
109.  		if(!search_special(ANY_SHOP))
110.  		    level.flags.has_shop = 0;
111.  	}
112.  	/* make sure bill is set only when the
113.  	 * dead shk is the resident shk.	*/
114.  	if(*u.ushops == eshk->shoproom) {
115.  	    setpaid(mtmp);
116.  	    /* dump core when referenced */
117.  	    ESHK(mtmp)->bill_p = (struct bill_x *) -1000;
118.  	    u.ushops[0] = '\0';
119.  	}
120.  }
121.  
122.  void
123.  set_residency(shkp, zero_out)
124.  register struct monst *shkp;
125.  register boolean zero_out;
126.  {
127.  	if (on_level(&(ESHK(shkp)->shoplevel), &u.uz))
128.  	    rooms[ESHK(shkp)->shoproom - ROOMOFFSET].resident =
129.  		(zero_out)? (struct monst *)0 : shkp;
130.  }
131.  
132.  void
133.  replshk(mtmp,mtmp2)
134.  register struct monst *mtmp, *mtmp2;
135.  {
136.  	rooms[ESHK(mtmp2)->shoproom - ROOMOFFSET].resident = mtmp2;
137.  	if (inhishop(mtmp) && *u.ushops == ESHK(mtmp)->shoproom) {
138.  		ESHK(mtmp2)->bill_p = &(ESHK(mtmp2)->bill[0]);
139.  	}
140.  }
141.  
142.  /* do shopkeeper specific structure munging -dlc */
143.  void
144.  restshk(shkp, ghostly)
145.  struct monst *shkp;
146.  boolean ghostly;
147.  {
148.      if (u.uz.dlevel) {
149.  	struct eshk *eshkp = ESHK(shkp);
150.  
151.  	if (eshkp->bill_p != (struct bill_x *) -1000)
152.  	    eshkp->bill_p = &eshkp->bill[0];
153.  	/* shoplevel can change as dungeons move around */
154.  	/* savebones guarantees that non-homed shk's will be gone */
155.  	if (ghostly) {
156.  	    assign_level(&eshkp->shoplevel, &u.uz);
157.  	    if (ANGRY(shkp) && strncmpi(eshkp->customer, plname, PL_NSIZ))
158.  		pacify_shk(shkp);
159.  	}
160.      }
161.  }
162.  
163.  #endif /* OVLB */
164.  #ifdef OVL3
165.  
166.  /* Clear the unpaid bit on all of the objects in the list. */
167.  STATIC_OVL void
168.  clear_unpaid(list)
169.  register struct obj *list;
170.  {
171.      while (list) {
172.  	if (Has_contents(list)) clear_unpaid(list->cobj);
173.  	list->unpaid = 0;
174.  	list = list->nobj;
175.      }
176.  }
177.  #endif /*OVL3*/
178.  #ifdef OVLB
179.  
180.  STATIC_OVL void
181.  setpaid(shkp)	/* either you paid or left the shop or the shopkeeper died */
182.  register struct monst *shkp;
183.  {
184.  	register struct obj *obj;
185.  	register struct monst *mtmp;
186.  
187.  	clear_unpaid(invent);
188.  	clear_unpaid(fobj);
189.  	clear_unpaid(level.buriedobjlist);
190.  	for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
191.  		clear_unpaid(mtmp->minvent);
192.  	for(mtmp = migrating_mons; mtmp; mtmp = mtmp->nmon)
193.  		clear_unpaid(mtmp->minvent);
194.  
195.  	while ((obj = billobjs) != 0) {
196.  		obj_extract_self(obj);
197.  		dealloc_obj(obj);
198.  	}
199.  	if(shkp) {
200.  		ESHK(shkp)->billct = 0;
201.  		ESHK(shkp)->credit = 0L;
202.  		ESHK(shkp)->debit = 0L;
203.  		ESHK(shkp)->loan = 0L;
204.  	}
205.  }
206.  
207.  STATIC_OVL long
208.  addupbill(shkp)
209.  register struct monst *shkp;
210.  {
211.  	register int ct = ESHK(shkp)->billct;
212.  	register struct bill_x *bp = ESHK(shkp)->bill_p;
213.  	register long total = 0L;
214.  
215.  	while(ct--){
216.  		total += bp->price * bp->bquan;
217.  		bp++;
218.  	}
219.  	return(total);
220.  }
221.  
222.  #endif /* OVLB */
223.  #ifdef OVL1
224.  
225.  #ifdef KOPS
226.  STATIC_OVL void
227.  call_kops(shkp, nearshop)
228.  register struct monst *shkp;
229.  register boolean nearshop;
230.  {
231.  	/* Keystone Kops srt@ucla */
232.  	register boolean nokops;
233.  
234.  	if(!shkp) return;
235.  
236.  	if (flags.soundok)
237.  	    pline("An alarm sounds!");
238.  
239.  	nokops = ((mvitals[PM_KEYSTONE_KOP].mvflags & G_GONE) &&
240.  		  (mvitals[PM_KOP_SERGEANT].mvflags & G_GONE) &&
241.  		  (mvitals[PM_KOP_LIEUTENANT].mvflags & G_GONE) &&
242.  		  (mvitals[PM_KOP_KAPTAIN].mvflags & G_GONE));
243.  
244.  	if(!angry_guards(!flags.soundok) && nokops) {
245.  	    if(flags.verbose && flags.soundok)
246.  		pline("But no one seems to respond to it.");
247.  	    return;
248.  	}
249.  
250.  	if(nokops) return;
251.  
252.  	{
253.  	    coord mm;
254.  
255.  	    if (nearshop) {
256.  		/* Create swarm around you, if you merely "stepped out" */
257.  		if (flags.verbose)
258.  		    pline_The("Keystone Kops appear!");
259.  		mm.x = u.ux;
260.  		mm.y = u.uy;
261.  		makekops(&mm);
262.  		return;
263.  	    }
264.  	    if (flags.verbose)
265.  		 pline_The("Keystone Kops are after you!");
266.  	    /* Create swarm near down staircase (hinders return to level) */
267.  	    mm.x = xdnstair;
268.  	    mm.y = ydnstair;
269.  	    makekops(&mm);
270.  	    /* Create swarm near shopkeeper (hinders return to shop) */
271.  	    mm.x = shkp->mx;
272.  	    mm.y = shkp->my;
273.  	    makekops(&mm);
274.  	}
275.  }
276.  #endif	/* KOPS */
277.  
278.  /* x,y is strictly inside shop */
279.  char
280.  inside_shop(x, y)
281.  register xchar x, y;
282.  {
283.  	register char rno;
284.  
285.  	rno = levl[x][y].roomno;
286.  	if ((rno < ROOMOFFSET) || levl[x][y].edge || !IS_SHOP(rno-ROOMOFFSET))
287.  	    return(NO_ROOM);
288.  	else
289.  	    return(rno);
290.  }
291.  
292.  void
293.  u_left_shop(leavestring, newlev)
294.  register char *leavestring;
295.  register boolean newlev;
296.  {
297.  	register struct monst *shkp;
298.  	register struct eshk *eshkp;
299.  	register long total;
300.  
301.  	/*
302.  	 * IF player
303.  	 * ((didn't leave outright) AND
304.  	 *  ((he is now strictly-inside the shop) OR
305.  	 *   (he wasn't strictly-inside last turn anyway)))
306.  	 * THEN (there's nothing to do, so just return)
307.  	 */
308.  	if(!*leavestring &&
309.  	   (!levl[u.ux][u.uy].edge || levl[u.ux0][u.uy0].edge))
310.  	    return;
311.  
312.  	shkp = shop_keeper(*u.ushops0);
313.  
314.  	if(!shkp || !inhishop(shkp))
315.  				/* shk died, teleported, changed levels... */
316.  	    return;
317.  
318.  	eshkp = ESHK(shkp);
319.  
320.  	if(!eshkp->billct && !eshkp->debit)	/* bill is settled */
321.  	    return;
322.  
323.  	if (!*leavestring && shkp->mcanmove && !shkp->msleeping) {
324.  	    /*
325.  	     * Player just stepped onto shop-boundary (known from above logic).
326.  	     * Try to intimidate him into paying his bill
327.  	     */
328.  	    verbalize(NOTANGRY(shkp) ?
329.  		      "%s!  Please pay before leaving." :
330.  		      "%s!  Don't you leave without paying!",
331.  		      plname);
332.  	    return;
333.  	}
334.  	total = (addupbill(shkp) + eshkp->debit);
335.  	if (eshkp->credit >= total) {
336.  	    Your("credit of %ld zorkmid%s is used to cover your shopping bill.",
337.  		 eshkp->credit, plur(eshkp->credit));
338.  	    total = 0L;		/* credit gets cleared by setpaid() */
339.  	} else {
340.  	    You("escaped the shop without paying!");
341.  	    total -= eshkp->credit;
342.  	}
343.  	setpaid(shkp);
344.  	if (!total) return;
345.  
346.  	/* by this point, we know an actual robbery has taken place */
347.  	eshkp->robbed += total;
348.  	You("stole %ld zorkmid%s worth of merchandise.",
349.  	    total, plur(total));
350.  	if (!Role_if(PM_ROGUE))	/* stealing is unlawful */
351.  	    adjalign(-sgn(u.ualign.type));
352.  
353.  	hot_pursuit(shkp);
354.  #ifdef KOPS
355.  	call_kops(shkp, (!newlev && levl[u.ux0][u.uy0].edge));
356.  #else
357.  	(void) angry_guards(FALSE);
358.  #endif
359.  }
360.  
361.  void
362.  u_entered_shop(enterstring)
363.  register char *enterstring;
364.  {
365.  
366.  	register int rt;
367.  	register struct monst *shkp;
368.  	register struct eshk *eshkp;
369.  	static const char no_shk[] = "This shop appears to be deserted.";
370.  	static char empty_shops[5];
371.  
372.  	if(!*enterstring)
373.  		return;
374.  
375.  	if(!(shkp = shop_keeper(*enterstring))) {
376.  	    if (!index(empty_shops, *enterstring) &&
377.  		in_rooms(u.ux, u.uy, SHOPBASE) !=
378.  				  in_rooms(u.ux0, u.uy0, SHOPBASE))
379.  		pline(no_shk);
380.  	    Strcpy(empty_shops, u.ushops);
381.  	    u.ushops[0] = '\0';
382.  	    return;
383.  	}
384.  
385.  	eshkp = ESHK(shkp);
386.  
387.  	if (!inhishop(shkp)) {
388.  	    /* dump core when referenced */
389.  	    eshkp->bill_p = (struct bill_x *) -1000;
390.  	    if (!index(empty_shops, *enterstring))
391.  		pline(no_shk);
392.  	    Strcpy(empty_shops, u.ushops);
393.  	    u.ushops[0] = '\0';
394.  	    return;
395.  	}
396.  
397.  	eshkp->bill_p = &(eshkp->bill[0]);
398.  
399.  	if (!eshkp->visitct || strncmpi(eshkp->customer, plname, PL_NSIZ)) {
400.  	    /* You seem to be new here */
401.  	    eshkp->visitct = 0;
402.  	    eshkp->following = 0;
403.  	    (void) strncpy(eshkp->customer,plname,PL_NSIZ);
404.  	    pacify_shk(shkp);
405.  	}
406.  
407.  	if (shkp->msleeping || !shkp->mcanmove || eshkp->following)
408.  	    return;	/* no dialog */
409.  
410.  	if (Invis) {
411.  	    pline("%s senses your presence.", shkname(shkp));
412.  	    verbalize("Invisible customers are not welcome!");
413.  	    return;
414.  	}
415.  
416.  	rt = rooms[*enterstring - ROOMOFFSET].rtype;
417.  
418.  	if (ANGRY(shkp)) {
419.  	    verbalize("So, %s, you dare return to %s %s?!",
420.  		      plname,
421.  		      s_suffix(shkname(shkp)),
422.  		      shtypes[rt - SHOPBASE].name);
423.  	} else if (eshkp->robbed) {
424.  	    pline("%s mutters imprecations against shoplifters.", shkname(shkp));
425.  	} else {
426.  	    verbalize("%s, %s!  Welcome%s to %s %s!",
427.  		      Hello(), plname,
428.  		      eshkp->visitct++ ? " again" : "",
429.  		      s_suffix(shkname(shkp)),
430.  		      shtypes[rt - SHOPBASE].name);
431.  	}
432.  	/* can't do anything about blocking if teleported in */
433.  	if (!inside_shop(u.ux, u.uy)) {
434.  	    boolean should_block;
435.  	    int cnt;
436.  	    const char *tool;
437.  	    struct obj *pick = carrying(PICK_AXE),
438.  		       *mattock = carrying(DWARVISH_MATTOCK);
439.  
440.  	    if (pick || mattock) {
441.  		cnt = 1;	/* so far */
442.  		if (pick && mattock) {	/* carrying both types */
443.  		    tool = "digging tool";
444.  		    cnt = 2;	/* `more than 1' is all that matters */
445.  		} else if (pick) {
446.  		    tool = "pick-axe";
447.  		    /* hack: `pick' already points somewhere into inventory */
448.  		    while ((pick = pick->nobj) != 0)
449.  			if (pick->otyp == PICK_AXE) ++cnt;
450.  		} else {	/* assert(mattock != 0) */
451.  		    tool = "mattock";
452.  		    while ((mattock = mattock->nobj) != 0)
453.  			if (mattock->otyp == DWARVISH_MATTOCK) ++cnt;
454.  		}
455.  		verbalize(NOTANGRY(shkp) ?
456.  			  "Will you please leave your %s%s outside?" :
457.  			  "Leave the %s%s outside.",
458.  			  tool, plur(cnt));
459.  		should_block = TRUE;
460.  	    } else {
461.  		should_block = (Fast && (sobj_at(PICK_AXE, u.ux, u.uy) ||
462.  				      sobj_at(DWARVISH_MATTOCK, u.ux, u.uy)));
463.  	    }
464.  	    if (should_block) (void) dochug(shkp);  /* shk gets extra move */
465.  	}
466.  	return;
467.  }
468.  
469.  /*
470.     Decide whether two unpaid items are mergable; caller is responsible for
471.     making sure they're unpaid and the same type of object; we check the price
472.     quoted by the shopkeeper and also that they both belong to the same shk.
473.   */
474.  boolean same_price(obj1, obj2)
475.  struct obj *obj1, *obj2;
476.  {
477.  	register struct monst *shkp1, *shkp2;
478.  	register struct bill_x *bp1 = 0, *bp2 = 0;
479.  	register boolean are_mergable = FALSE;
480.  
481.  	/* look up the first object by finding shk whose bill it's on */
482.  	for (shkp1 = next_shkp(fmon, TRUE); shkp1;
483.  		shkp1 = next_shkp(shkp1, TRUE))
484.  	    if ((bp1 = onbill(obj1, shkp1, TRUE)) != 0) break;
485.  	/* second object is probably owned by same shk; if not, look harder */
486.  	if (shkp1 && (bp2 = onbill(obj2, shkp1, TRUE)) != 0) {
487.  	    shkp2 = shkp1;
488.  	} else {
489.  	    for (shkp2 = next_shkp(fmon, TRUE); shkp2;
490.  		    shkp2 = next_shkp(shkp2, TRUE))
491.  		if ((bp2 = onbill(obj2, shkp2, TRUE)) != 0) break;
492.  	}
493.  
494.  	if (!bp1 || !bp2) impossible("same_price: object wasn't on any bill!");
495.  	else are_mergable = (shkp1 == shkp2 && bp1->price == bp2->price);
496.  	return are_mergable;
497.  }
498.  
499.  /*
500.   * Figure out how much is owed to a given shopkeeper.
501.   * At present, we ignore any amount robbed from the shop, to avoid
502.   * turning the `$' command into a way to discover that the current
503.   * level is bones data which has a shk on the warpath.
504.   */
505.  STATIC_OVL long
506.  shop_debt(eshkp)
507.  struct eshk *eshkp;
508.  {
509.  	struct bill_x *bp;
510.  	int ct;
511.  	long debt = eshkp->debit;
512.  
513.  	for (bp = eshkp->bill_p, ct = eshkp->billct; ct > 0; bp++, ct--)
514.  	    debt += bp->price * bp->bquan;
515.  	return debt;
516.  }
517.  
518.  /* called in response to the `$' command */
519.  void
520.  shopper_financial_report()
521.  {
522.  	struct monst *shkp, *this_shkp = shop_keeper(inside_shop(u.ux, u.uy));
523.  	struct eshk *eshkp;
524.  	long amt;
525.  	int pass;
526.  
527.  	if (this_shkp &&
528.  	    !(ESHK(this_shkp)->credit || shop_debt(ESHK(this_shkp)))) {
529.  	    You("have no credit or debt in here.");
530.  	    this_shkp = 0;	/* skip first pass */
531.  	}
532.  
533.  	/* pass 0: report for the shop we're currently in, if any;
534.  	   pass 1: report for all other shops on this level. */
535.  	for (pass = this_shkp ? 0 : 1; pass <= 1; pass++)
536.  	    for (shkp = next_shkp(fmon, FALSE);
537.  		    shkp; shkp = next_shkp(shkp->nmon, FALSE)) {
538.  		if ((shkp != this_shkp) ^ pass) continue;
539.  		eshkp = ESHK(shkp);
540.  		if ((amt = eshkp->credit) != 0)
541.  		    You("have %ld zorkmid%s credit at %s %s.",
542.  			amt, plur(amt), s_suffix(shkname(shkp)),
543.  			shtypes[eshkp->shoptype - SHOPBASE].name);
544.  		else if (shkp == this_shkp)
545.  		    You("have no credit in here.");
546.  		if ((amt = shop_debt(eshkp)) != 0)
547.  		    You("owe %s %ld zorkmid%s.",
548.  			shkname(shkp), amt, plur(amt));
549.  		else if (shkp == this_shkp)
550.  		    You("don't owe any money here.");
551.  	    }
552.  }
553.  
554.  #endif /* OVL1 */
555.  #ifdef OVLB
556.  
557.  int
558.  inhishop(mtmp)
559.  register struct monst *mtmp;
560.  {
561.  	return(index(in_rooms(mtmp->mx, mtmp->my, SHOPBASE),
562.  		     ESHK(mtmp)->shoproom) &&
563.  		on_level(&(ESHK(mtmp)->shoplevel), &u.uz));
564.  }
565.  
566.  struct monst *
567.  shop_keeper(rmno)
568.  register char rmno;
569.  {
570.  	struct monst *shkp = rmno >= ROOMOFFSET ?
571.  				rooms[rmno - ROOMOFFSET].resident : 0;
572.  
573.  	if (shkp) {
574.  	    if (NOTANGRY(shkp)) {
575.  		if (ESHK(shkp)->surcharge) pacify_shk(shkp);
576.  	    } else {
577.  		if (!ESHK(shkp)->surcharge) rile_shk(shkp);
578.  	    }
579.  	}
580.  	return shkp;
581.  }
582.  
583.  boolean
584.  tended_shop(sroom)
585.  register struct mkroom *sroom;
586.  {
587.  	register struct monst *mtmp = sroom->resident;
588.  
589.  	if (!mtmp)
590.  		return(FALSE);
591.  	else
592.  		return((boolean)(inhishop(mtmp)));
593.  }
594.  
595.  STATIC_OVL struct bill_x *
596.  onbill(obj, shkp, silent)
597.  register struct obj *obj;
598.  register struct monst *shkp;
599.  register boolean silent;
600.  {
601.  	if (shkp) {
602.  		register struct bill_x *bp = ESHK(shkp)->bill_p;
603.  		register int ct = ESHK(shkp)->billct;
604.  
605.  		while (--ct >= 0)
606.  		    if (bp->bo_id == obj->o_id) {
607.  			if (!obj->unpaid) pline("onbill: paid obj on bill?");
608.  			return bp;
609.  		    } else bp++;
610.  	}
611.  	if(obj->unpaid & !silent) pline("onbill: unpaid obj not on bill?");
612.  	return (struct bill_x *)0;
613.  }
614.  
615.  /* Delete the contents of the given object. */
616.  void
617.  delete_contents(obj)
618.  register struct obj *obj;
619.  {
620.  	register struct obj *curr;
621.  
622.  	while ((curr = obj->cobj) != 0) {
623.  	    obj_extract_self(curr);
624.  	    obfree(curr, (struct obj *)0);
625.  	}
626.  }
627.  
628.  /* called with two args on merge */
629.  void
630.  obfree(obj, merge)
631.  register struct obj *obj, *merge;
632.  {
633.  	register struct bill_x *bp;
634.  	register struct bill_x *bpm;
635.  	register struct monst *shkp;
636.  
637.  
638.  	if (obj->otyp == LEASH && obj->leashmon) o_unleash(obj);
639.  	if (obj->oclass == FOOD_CLASS) food_disappears(obj);
640.  	if (Has_contents(obj)) delete_contents(obj);
641.  
642.  	shkp = 0;
643.  	if (obj->unpaid) {
644.  	    /* look for a shopkeeper who owns this object */
645.  	    for (shkp = next_shkp(fmon, TRUE); shkp;
646.  		    shkp = next_shkp(shkp, TRUE))
647.  		if (onbill(obj, shkp, TRUE)) break;
648.  	}
649.  	/* sanity check, more or less */
650.  	if (!shkp) shkp = shop_keeper(*u.ushops);
651.  		/*
652.  		 * Note:  `shkp = shop_keeper(*u.ushops)' used to be
653.  		 *	  unconditional.  But obfree() is used all over
654.  		 *	  the place, so making its behavior be dependent
655.  		 *	  upon player location doesn't make much sense.
656.  		 */
657.  
658.  	if ((bp = onbill(obj, shkp, FALSE)) != 0) {
659.  		if(!merge){
660.  			bp->useup = 1;
661.  			obj->unpaid = 0;	/* only for doinvbill */
662.  			add_to_billobjs(obj);
663.  			return;
664.  		}
665.  		bpm = onbill(merge, shkp, FALSE);
666.  		if(!bpm){
667.  			/* this used to be a rename */
668.  			impossible("obfree: not on bill??");
669.  			return;
670.  		} else {
671.  			/* this was a merger */
672.  			bpm->bquan += bp->bquan;
673.  			ESHK(shkp)->billct--;
674.  #ifdef DUMB
675.  			{
676.  			/* DRS/NS 2.2.6 messes up -- Peter Kendell */
677.  				int indx = ESHK(shkp)->billct;
678.  				*bp = ESHK(shkp)->bill_p[indx];
679.  			}
680.  #else
681.  			*bp = ESHK(shkp)->bill_p[ESHK(shkp)->billct];
682.  #endif
683.  		}
684.  	}
685.  	dealloc_obj(obj);
686.  }
687.  #endif /* OVLB */
688.  #ifdef OVL3
689.  
690.  STATIC_OVL long
691.  check_credit(tmp, shkp)
692.  long tmp;
693.  register struct monst *shkp;
694.  {
695.  	long credit = ESHK(shkp)->credit;
696.  
697.  	if(credit == 0L) return(tmp);
698.  	if(credit >= tmp) {
699.  		pline_The("price is deducted from your credit.");
700.  		ESHK(shkp)->credit -=tmp;
701.  		tmp = 0L;
702.  	} else {
703.  		pline_The("price is partially covered by your credit.");
704.  		ESHK(shkp)->credit = 0L;
705.  		tmp -= credit;
706.  	}
707.  	return(tmp);
708.  }
709.  
710.  STATIC_OVL void
711.  pay(tmp,shkp)
712.  long tmp;
713.  register struct monst *shkp;
714.  {
715.  	long robbed = ESHK(shkp)->robbed;
716.  	long balance = ((tmp <= 0L) ? tmp : check_credit(tmp, shkp));
717.  
718.  	u.ugold -= balance;
719.  	shkp->mgold += balance;
720.  	flags.botl = 1;
721.  	if(robbed) {
722.  		robbed -= tmp;
723.  		if(robbed < 0) robbed = 0L;
724.  		ESHK(shkp)->robbed = robbed;
725.  	}
726.  }
727.  #endif /*OVL3*/
728.  #ifdef OVLB
729.  
730.  /* return shkp to home position */
731.  void
732.  home_shk(shkp, killkops)
733.  register struct monst *shkp;
734.  register boolean killkops;
735.  {
736.  	register xchar x = ESHK(shkp)->shk.x, y = ESHK(shkp)->shk.y;
737.  
738.  	(void) mnearto(shkp, x, y, TRUE);
739.  	level.flags.has_shop = 1;
740.  	if (killkops) {
741.  #ifdef KOPS
742.  		kops_gone(TRUE);
743.  #else
744.  		You_feel("vaguely apprehensive.");
745.  #endif
746.  		pacify_guards();
747.  	}
748.  }
749.  
750.  STATIC_OVL boolean
751.  angry_shk_exists()
752.  {
753.  	register struct monst *shkp;
754.  
755.  	for (shkp = next_shkp(fmon, FALSE);
756.  		shkp; shkp = next_shkp(shkp->nmon, FALSE))
757.  	    if (ANGRY(shkp)) return(TRUE);
758.  	return(FALSE);
759.  }
760.  
761.  /* remove previously applied surcharge from all billed items */
762.  STATIC_OVL void
763.  pacify_shk(shkp)
764.  register struct monst *shkp;
765.  {
766.  	NOTANGRY(shkp) = TRUE;	/* make peaceful */
767.  	if (ESHK(shkp)->surcharge) {
768.  		register struct bill_x *bp = ESHK(shkp)->bill_p;
769.  		register int ct = ESHK(shkp)->billct;
770.  
771.  		ESHK(shkp)->surcharge = FALSE;
772.  		while (ct-- > 0) {
773.  			register long reduction = (bp->price + 3L) / 4L;
774.  			bp->price -= reduction;		/* undo 33% increase */
775.  			bp++;
776.  		}
777.  	}
778.  }
779.  
780.  /* add aggravation surcharge to all billed items */
781.  STATIC_OVL void
782.  rile_shk(shkp)
783.  register struct monst *shkp;
784.  {
785.  	NOTANGRY(shkp) = FALSE;	/* make angry */
786.  	if (!ESHK(shkp)->surcharge) {
787.  		register struct bill_x *bp = ESHK(shkp)->bill_p;
788.  		register int ct = ESHK(shkp)->billct;
789.  
790.  		ESHK(shkp)->surcharge = TRUE;
791.  		while (ct-- > 0) {
792.  			register long surcharge = (bp->price + 2L) / 3L;
793.  			bp->price += surcharge;
794.  			bp++;
795.  		}
796.  	}
797.  }
798.  
799.  void
800.  make_happy_shk(shkp, silentkops)
801.  register struct monst *shkp;
802.  register boolean silentkops;
803.  {
804.  	boolean wasmad = ANGRY(shkp);
805.  	struct eshk *eshkp = ESHK(shkp);
806.  
807.  	pacify_shk(shkp);
808.  	eshkp->following = 0;
809.  	eshkp->robbed = 0L;
810.  	if (!Role_if(PM_ROGUE))
811.  		adjalign(sgn(u.ualign.type));
812.  	if(!inhishop(shkp)) {
813.  		char shk_nam[BUFSZ];
814.  		boolean vanished = canseemon(shkp);
815.  
816.  		Strcpy(shk_nam, mon_nam(shkp));
817.  		if (on_level(&eshkp->shoplevel, &u.uz)) {
818.  			home_shk(shkp, FALSE);
819.  			/* didn't disappear if shk can still be seen */
820.  			if (canseemon(shkp)) vanished = FALSE;
821.  		} else {
822.  			/* if sensed, does disappear regardless whether seen */
823.  			if (sensemon(shkp)) vanished = TRUE;
824.  			/* arrive near shop's door */
825.  			migrate_to_level(shkp, ledger_no(&eshkp->shoplevel),
826.  					 MIGR_APPROX_XY, &eshkp->shd);
827.  		}
828.  		if (vanished)
829.  		    pline("Satisfied, %s suddenly disappears!", shk_nam);
830.  	} else if(wasmad)
831.  		pline("%s calms down.", Monnam(shkp));
832.  
833.  	if(!angry_shk_exists()) {
834.  #ifdef KOPS
835.  		kops_gone(silentkops);
836.  #endif
837.  		pacify_guards();
838.  	}
839.  }
840.  
841.  void
842.  hot_pursuit(shkp)
843.  register struct monst *shkp;
844.  {
845.  	if(!shkp->isshk) return;
846.  
847.  	rile_shk(shkp);
848.  	ESHK(shkp)->following = 1;
849.  }
850.  
851.  /* used when the shkp is teleported out of his shop,
852.   * or when the player is not on a costly_spot and he
853.   * damages something inside the shop.  these conditions
854.   * must be checked by the calling function.
855.   */
856.  void
857.  make_angry_shk(shkp, ox, oy)
858.  register struct monst *shkp;
859.  register xchar ox,oy;
860.  {
861.  	xchar sx, sy;
862.  	struct eshk *eshkp = ESHK(shkp);
863.  
864.  	/* all pending shop transactions are now "past due" */
865.  	if (eshkp->billct || eshkp->debit || eshkp->loan || eshkp->credit) {
866.  	    eshkp->robbed += (addupbill(shkp) + eshkp->debit + eshkp->loan);
867.  	    eshkp->robbed -= eshkp->credit;
868.  	    if (eshkp->robbed < 0L) eshkp->robbed = 0L;
869.  	    /* billct, debit, loan, and credit will be cleared by setpaid */
870.  	    setpaid(shkp);
871.  	}
872.  
873.  	/* If you just used a wand of teleportation to send the shk away, you
874.  	   might not be able to see her any more.  Monnam would yield "it",
875.  	   which makes this message look pretty silly, so temporarily restore
876.  	   her original location during the call to Monnam. */
877.  	sx = shkp->mx,  sy = shkp->my;
878.  	if (cansee(ox, oy) && !cansee(sx, sy))
879.  		shkp->mx = ox,  shkp->my = oy;
880.  	pline("%s %s!", Monnam(shkp),
881.  	      !ANGRY(shkp) ? "gets angry" : "is furious");
882.  	shkp->mx = sx,  shkp->my = sy;
883.  	hot_pursuit(shkp);
884.  }
885.  
886.  STATIC_VAR const char no_money[] = "Moreover, you%s have no money.";
887.  STATIC_VAR const char not_enough_money[] =
888.  			    "Besides, you don't have enough to interest %s.";
889.  
890.  #else
891.  STATIC_VAR const char no_money[];
892.  STATIC_VAR const char not_enough_money[];
893.  #endif /*OVLB*/
894.  
895.  #ifdef OVL3
896.  
897.  STATIC_OVL long
898.  cheapest_item(shkp)   /* delivers the cheapest item on the list */
899.  register struct monst *shkp;
900.  {
901.  	register int ct = ESHK(shkp)->billct;
902.  	register struct bill_x *bp = ESHK(shkp)->bill_p;
903.  	register long gmin = (bp->price * bp->bquan);
904.  
905.  	while(ct--){
906.  		if(bp->price * bp->bquan < gmin)
907.  			gmin = bp->price * bp->bquan;
908.  		bp++;
909.  	}
910.  	return(gmin);
911.  }
912.  #endif /*OVL3*/
913.  #ifdef OVL0
914.  
915.  int
916.  dopay()
917.  {
918.  	register struct eshk *eshkp;
919.  	register struct monst *shkp;
920.  	struct monst *nxtm, *resident;
921.  	long ltmp;
922.  	int pass, tmp, shk_pronoun, sk = 0, seensk = 0;
923.  	boolean paid = FALSE, stashed_gold = (hidden_gold() > 0L);
924.  
925.  	multi = 0;
926.  
927.  	/* find how many shk's there are, how many are in */
928.  	/* sight, and are you in a shop room with one.    */
929.  	nxtm = resident = 0;
930.  	for (shkp = next_shkp(fmon, FALSE);
931.  		shkp; shkp = next_shkp(shkp->nmon, FALSE)) {
932.  	    sk++;
933.  	    if (ANGRY(shkp) && distu(shkp->mx, shkp->my) <= 2) nxtm = shkp;
934.  	    if (canspotmon(shkp)) seensk++;
935.  	    if (inhishop(shkp) && (*u.ushops == ESHK(shkp)->shoproom))
936.  		resident = shkp;
937.  	}
938.  
939.  	if (nxtm) {			/* Player should always appease an */
940.  	     shkp = nxtm;		/* irate shk standing next to them. */
941.  	     goto proceed;
942.  	}
943.  
944.  	if ((!sk && (!Blind || Blind_telepat)) || (!Blind && !seensk)) {
945.        pline("There appears to be no shopkeeper here to receive your payment.");
946.  		return(0);
947.  	}
948.  
949.  	if(!seensk) {
950.  		You_cant("see...");
951.  		return(0);
952.  	}
953.  
954.  	/* the usual case.  allow paying at a distance when */
955.  	/* inside a tended shop.  should we change that?    */
956.  	if(sk == 1 && resident) {
957.  		shkp = resident;
958.  		goto proceed;
959.  	}
960.  
961.  	if (seensk == 1) {
962.  		for (shkp = next_shkp(fmon, FALSE);
963.  			shkp; shkp = next_shkp(shkp->nmon, FALSE))
964.  		    if (canspotmon(shkp)) break;
965.  		if (shkp != resident && distu(shkp->mx, shkp->my) > 2) {
966.  		    pline("%s is not near enough to receive your payment.",
967.  					     Monnam(shkp));
968.  		    return(0);
969.  		}
970.  	} else {
971.  		struct monst *mtmp;
972.  		coord cc;
973.  		int cx, cy;
974.  
975.  		pline("Pay whom?");
976.  		cc.x = u.ux;
977.  		cc.y = u.uy;
978.  		if (getpos(&cc, TRUE, "the creature you want to pay") < 0)
979.  		    return 0;	/* player pressed ESC */
980.  		cx = cc.x;
981.  		cy = cc.y;
982.  		if(cx < 0) {
983.  		     pline("Try again...");
984.  		     return(0);
985.  		}
986.  		if(u.ux == cx && u.uy == cy) {
987.  		     You("are generous to yourself.");
988.  		     return(0);
989.  		}
990.  		mtmp = m_at(cx, cy);
991.  		if(!mtmp) {
992.  		     pline("There is no one there to receive your payment.");
993.  		     return(0);
994.  		}
995.  		if(!mtmp->isshk) {
996.  		     pline("%s is not interested in your payment.",
997.  				    Monnam(mtmp));
998.  		     return(0);
999.  		}
1000. 		if (mtmp != resident && distu(mtmp->mx, mtmp->my) > 2) {
1001. 		     pline("%s is too far to receive your payment.",
1002. 				    Monnam(mtmp));
1003. 		     return(0);
1004. 		}
1005. 		shkp = mtmp;
1006. 	}
1007. 
1008. 	if(!shkp) {
1009. #ifdef DEBUG
1010. 		pline("dopay: null shkp.");
1011. #endif
1012. 		return(0);
1013. 	}
1014. proceed:
1015. 
1016. 	if (shkp->msleeping || !shkp->mcanmove) {
1017. 		pline("%s %s.", Monnam(shkp),
1018. 		      rn2(2) ? "seems to be napping" : "doesn't respond");
1019. 		return 0;
1020. 	}
1021. 	eshkp = ESHK(shkp);
1022. 	shk_pronoun = pronoun_gender(shkp);
1023. 
1024. 	ltmp = eshkp->robbed;
1025. 	if(shkp != resident && NOTANGRY(shkp)) {
1026. 		if(!ltmp)
1027. 		    You("do not owe %s anything.", mon_nam(shkp));
1028. 		else if(!u.ugold) {
1029. 		    You("%shave no money.", stashed_gold ? "seem to " : "");
1030. 		    if(stashed_gold)
1031. 			pline("But you have some gold stashed away.");
1032. 		} else {
1033. 		    long ugold = u.ugold;
1034. 
1035. 		    if(ugold > ltmp) {
1036. 			You("give %s the %ld gold piece%s %s asked for.",
1037. 			    mon_nam(shkp), ltmp, plur(ltmp), he[shk_pronoun]);
1038. 			pay(ltmp, shkp);
1039. 		    } else {
1040. 			You("give %s all your%s gold.", mon_nam(shkp),
1041. 					stashed_gold ? " openly kept" : "");
1042. 			pay(u.ugold, shkp);
1043. 			if (stashed_gold) pline("But you have hidden gold!");
1044. 		    }
1045. 		    if((ugold < ltmp/2L) || (ugold < ltmp && stashed_gold))
1046. 			pline("Unfortunately, %s doesn't look satisfied.",
1047. 			      he[shk_pronoun]);
1048. 		    else
1049. 			make_happy_shk(shkp, FALSE);
1050. 		}
1051. 		return(1);
1052. 	}
1053. 
1054. 	/* ltmp is still eshkp->robbed here */
1055. 	if (!eshkp->billct && !eshkp->debit) {
1056. 		if(!ltmp && NOTANGRY(shkp)) {
1057. 		    You("do not owe %s anything.", mon_nam(shkp));
1058. 		    if (!u.ugold)
1059. 			pline(no_money, stashed_gold ? " seem to" : "");
1060. 		} else if(ltmp) {
1061. 		    pline("%s is after blood, not money!", Monnam(shkp));
1062. 		    if(u.ugold < ltmp/2L ||
1063. 				(u.ugold < ltmp && stashed_gold)) {
1064. 			if (!u.ugold)
1065. 			    pline(no_money, stashed_gold ? " seem to" : "");
1066. 			else pline(not_enough_money, him[shk_pronoun]);
1067. 			return(1);
1068. 		    }
1069. 		    pline("But since %s shop has been robbed recently,",
1070. 			  his[shk_pronoun]);
1071. 		    pline("you %scompensate %s for %s losses.",
1072. 			  (u.ugold < ltmp) ? "partially " : "",
1073. 			  mon_nam(shkp), his[shk_pronoun]);
1074. 		    pay(u.ugold < ltmp ? u.ugold : ltmp, shkp);
1075. 		    make_happy_shk(shkp, FALSE);
1076. 		} else {
1077. 		    /* shopkeeper is angry, but has not been robbed --
1078. 		     * door broken, attacked, etc. */
1079. 		    pline("%s is after your hide, not your money!",
1080. 			  Monnam(shkp));
1081. 		    if(u.ugold < 1000L) {
1082. 			if (!u.ugold)
1083. 			    pline(no_money, stashed_gold ? " seem to" : "");
1084. 			else pline(not_enough_money, him[shk_pronoun]);
1085. 			return(1);
1086. 		    }
1087. 		    You("try to appease %s by giving %s 1000 gold pieces.",
1088. 			x_monnam(shkp, 1, "angry", 0), him[shk_pronoun]);
1089. 		    pay(1000L,shkp);
1090. 		    if (strncmp(eshkp->customer, plname, PL_NSIZ) || rn2(3))
1091. 			make_happy_shk(shkp, FALSE);
1092. 		    else
1093. 			pline("But %s is as angry as ever.", mon_nam(shkp));
1094. 		}
1095. 		return(1);
1096. 	}
1097. 	if(shkp != resident) {
1098. 		impossible("dopay: not to shopkeeper?");
1099. 		if(resident) setpaid(resident);
1100. 		return(0);
1101. 	}
1102. 	/* pay debt, if any, first */
1103. 	if(eshkp->debit) {
1104. 		long dtmp = eshkp->debit;
1105. 		long loan = eshkp->loan;
1106. 		char sbuf[BUFSZ];
1107. 
1108. 		Sprintf(sbuf, "You owe %s %ld zorkmid%s ",
1109. 					   shkname(shkp), dtmp, plur(dtmp));
1110. 		if(loan) {
1111. 		    if(loan == dtmp)
1112. 			Strcat(sbuf, "you picked up in the store.");
1113. 		    else Strcat(sbuf,
1114. 			   "for gold picked up and the use of merchandise.");
1115. 		} else Strcat(sbuf, "for the use of merchandise.");
1116. 		pline(sbuf);
1117. 		if (u.ugold + eshkp->credit < dtmp) {
1118. 		    pline("But you don't%s have enough gold%s.",
1119. 			stashed_gold ? " seem to" : "",
1120. 			eshkp->credit ? " or credit" : "");
1121. 		    return(1);
1122. 		} else {
1123. 		    if (eshkp->credit >= dtmp) {
1124. 			eshkp->credit -= dtmp;
1125. 			eshkp->debit = 0L;
1126. 			eshkp->loan = 0L;
1127. 			Your("debt is covered by your credit.");
1128. 		    } else if (!eshkp->credit) {
1129. 			u.ugold -= dtmp;
1130. 			shkp->mgold += dtmp;
1131. 			eshkp->debit = 0L;
1132. 			eshkp->loan = 0L;
1133. 			You("pay that debt.");
1134. 			flags.botl = 1;
1135. 		    } else {
1136. 			dtmp -= eshkp->credit;
1137. 			eshkp->credit = 0L;
1138. 			u.ugold -= dtmp;
1139. 			shkp->mgold += dtmp;
1140. 			eshkp->debit = 0L;
1141. 			eshkp->loan = 0L;
1142. 			pline("That debt is partially offset by your credit.");
1143. 			You("pay the remainder.");
1144. 			flags.botl = 1;
1145. 		    }
1146. 		    paid = TRUE;
1147. 		}
1148. 	}
1149. 	/* now check items on bill */
1150. 	if (eshkp->billct) {
1151. 	    register boolean itemize;
1152. 
1153. 	    if (!u.ugold && !eshkp->credit) {
1154. 		You("%shave no money or credit%s.",
1155. 				    stashed_gold ? "seem to " : "",
1156. 				    paid ? " left" : "");
1157. 		return(0);
1158. 	    }
1159. 	    if ((u.ugold + eshkp->credit) < cheapest_item(shkp)) {
1160. 		You("don't have enough money to buy%s the item%s you picked.",
1161. 		    eshkp->billct > 1 ? " any of" : "", plur(eshkp->billct));
1162. 		if(stashed_gold)
1163. 		    pline("Maybe you have some gold stashed away?");
1164. 		return(0);
1165. 	    }
1166. 
1167. 	    /* this isn't quite right; it itemizes without asking if the
1168. 	     * single item on the bill is partly used up and partly unpaid */
1169. 	    itemize = (eshkp->billct > 1 ? yn("Itemized billing?") == 'y' : 1);
1170. 
1171. 	    for (pass = 0; pass <= 1; pass++) {
1172. 		tmp = 0;
1173. 		while (tmp < eshkp->billct) {
1174. 		    struct obj *otmp;
1175. 		    register struct bill_x *bp = &(eshkp->bill_p[tmp]);
1176. 
1177. 		    /* find the object on one of the lists */
1178. 		    if ((otmp = bp_to_obj(bp)) != 0) {
1179. 			/* if completely used up, object quantity is stale;
1180. 			   restoring it to its original value here avoids
1181. 			   making the partly-used-up code more complicated */
1182. 			if (bp->useup) otmp->quan = bp->bquan;
1183. 		    } else {
1184. 			impossible("Shopkeeper administration out of order.");
1185. 			setpaid(shkp);	/* be nice to the player */
1186. 			return 1;
1187. 		    }
1188. 		    if (pass == bp->useup && otmp->quan == bp->bquan) {
1189. 			/* pay for used-up items on first pass and others
1190. 			 * on second, so player will be stuck in the store
1191. 			 * less often; things which are partly used up
1192. 			 * are processed on both passes */
1193. 			tmp++;
1194. 		    } else {
1195. 			switch (dopayobj(shkp, bp, &otmp, pass, itemize)) {
1196. 			  case PAY_CANT:
1197. 				return 1;	/*break*/
1198. 			  case PAY_BROKE:
1199. 				paid = TRUE;
1200. 				goto thanks;	/*break*/
1201. 			  case PAY_SKIP:
1202. 				tmp++;
1203. 				continue;	/*break*/
1204. 			  case PAY_SOME:
1205. 				paid = TRUE;
1206. 				if (itemize) bot();
1207. 				continue;	/*break*/
1208. 			  case PAY_BUY:
1209. 				paid = TRUE;
1210. 				break;
1211. 			}
1212. 			if (itemize) bot();
1213. 			*bp = eshkp->bill_p[--eshkp->billct];
1214. 		    }
1215. 		}
1216. 	    }
1217. 	}
1218. thanks:
1219. 	if(!ANGRY(shkp) && paid)
1220. 	    verbalize("Thank you for shopping in %s %s!",
1221. 		s_suffix(shkname(shkp)),
1222. 		shtypes[eshkp->shoptype - SHOPBASE].name);
1223. 	return(1);
1224. }
1225. #endif /*OVL0*/
1226. #ifdef OVL3
1227. 
1228. /* return 2 if used-up portion paid */
1229. /*	  1 if paid successfully    */
1230. /*	  0 if not enough money     */
1231. /*	 -1 if skip this object     */
1232. /*	 -2 if no money/credit left */
1233. STATIC_OVL int
1234. dopayobj(shkp, bp, obj_p, which, itemize)
1235. register struct monst *shkp;
1236. register struct bill_x *bp;
1237. struct obj **obj_p;
1238. int	which;		/* 0 => used-up item, 1 => other (unpaid or lost) */
1239. boolean itemize;
1240. {
1241. 	register struct obj *obj = *obj_p;
1242. 	long ltmp, quan, save_quan;
1243. 	int buy;
1244. 	boolean stashed_gold = (hidden_gold() > 0L),
1245. 		consumed = (which == 0);
1246. 
1247. 	if(!obj->unpaid && !bp->useup){
1248. 		impossible("Paid object on bill??");
1249. 		return PAY_BUY;
1250. 	}
1251. 	if(itemize && u.ugold + ESHK(shkp)->credit == 0L){
1252. 		You("%shave no money or credit left.",
1253. 			     stashed_gold ? "seem to " : "");
1254. 		return PAY_BROKE;
1255. 	}
1256. 	/* we may need to temporarily adjust the object, if part of the
1257. 	   original quantity has been used up but part remains unpaid  */
1258. 	save_quan = obj->quan;
1259. 	if (consumed) {
1260. 	    /* either completely used up (simple), or split needed */
1261. 	    quan = bp->bquan;
1262. 	    if (quan > obj->quan)	/* difference is amount used up */
1263. 		quan -= obj->quan;
1264. 	} else {
1265. 	    /* dealing with ordinary unpaid item */
1266. 	    quan = obj->quan;
1267. 	}
1268. 	obj->quan = quan;	/* to be used by doname() */
1269. 	obj->unpaid = 0;	/* ditto */
1270. 	ltmp = bp->price * quan;
1271. 	buy = PAY_BUY;		/* flag; if changed then return early */
1272. 
1273. 	if (itemize) {
1274. 	    char qbuf[BUFSZ];
1275. 	    Sprintf(qbuf,"%s for %ld zorkmid%s.  Pay?", quan == 1L ?
1276. 		    Doname2(obj) : doname(obj), ltmp, plur(ltmp));
1277. 	    if (yn(qbuf) == 'n') {
1278. 		buy = PAY_SKIP;		/* don't want to buy */
1279. 	    } else if (quan < bp->bquan && !consumed) { /* partly used goods */
1280. 		obj->quan = bp->bquan - save_quan;	/* used up amount */
1281. 		verbalize("%s for the other %s before buying %s.",
1282. 			  ANGRY(shkp) ? "Pay" : "Please pay", xname(obj),
1283. 			  save_quan > 1L ? "these" : "this one");
1284. 		buy = PAY_SKIP;		/* shk won't sell */
1285. 	    }
1286. 	}
1287. 	if (buy == PAY_BUY && u.ugold + ESHK(shkp)->credit < ltmp) {
1288. 	    You("don't%s have gold%s enough to pay for %s.",
1289. 		stashed_gold ? " seem to" : "",
1290. 		(ESHK(shkp)->credit > 0L) ? " or credit" : "",
1291. 		doname(obj));
1292. 	    buy = itemize ? PAY_SKIP : PAY_CANT;
1293. 	}
1294. 
1295. 	if (buy != PAY_BUY) {
1296. 	    /* restore unpaid object to original state */
1297. 	    obj->quan = save_quan;
1298. 	    obj->unpaid = 1;
1299. 	    return buy;
1300. 	}
1301. 
1302. 	pay(ltmp, shkp);
1303. 	shk_names_obj(shkp, obj, "bought %s for %ld gold piece%s.%s", ltmp, "");
1304. 	obj->quan = save_quan;		/* restore original count */
1305. 	/* quan => amount just bought, save_quan => remaining unpaid count */
1306. 	if (consumed) {
1307. 	    if (quan != bp->bquan) {
1308. 		/* eliminate used-up portion; remainder is still unpaid */
1309. 		bp->bquan = obj->quan;
1310. 		obj->unpaid = 1;
1311. 		bp->useup = 0;
1312. 		buy = PAY_SOME;
1313. 	    } else {	/* completely used-up, so get rid of it */
1314. 		obj_extract_self(obj);
1315. 	     /* assert( obj == *obj_p ); */
1316. 		dealloc_obj(obj);
1317. 		*obj_p = 0;	/* destroy pointer to freed object */
1318. 	    }
1319. 	}
1320. 	return buy;
1321. }
1322. #endif /*OVL3*/
1323. #ifdef OVLB
1324. 
1325. static coord repo_location;	/* repossession context */
1326. 
1327. /* routine called after dying (or quitting) */
1328. boolean
1329. paybill(croaked)
1330. boolean croaked;
1331. {
1332. 	register struct monst *mtmp, *mtmp2, *resident= (struct monst *)0;
1333. 	register boolean taken = FALSE;
1334. 	register int numsk = 0;
1335. 
1336. 	/* this is where inventory will end up if any shk takes it */
1337. 	repo_location.x = repo_location.y = 0;
1338. 
1339. 	/* give shopkeeper first crack */
1340. 	if ((mtmp = shop_keeper(*u.ushops)) && inhishop(mtmp)) {
1341. 	    numsk++;
1342. 	    resident = mtmp;
1343. 	    taken = inherits(resident, numsk, croaked);
1344. 	}
1345. 	for (mtmp = next_shkp(fmon, FALSE);
1346. 		mtmp; mtmp = next_shkp(mtmp2, FALSE)) {
1347. 	    mtmp2 = mtmp->nmon;
1348. 	    if (mtmp != resident) {
1349. 		/* for bones: we don't want a shopless shk around */
1350. 		if(!on_level(&(ESHK(mtmp)->shoplevel), &u.uz))
1351. 			mongone(mtmp);
1352. 		else {
1353. 		    numsk++;
1354. 		    taken |= inherits(mtmp, numsk, croaked);
1355. 		}
1356. 	    }
1357. 	}
1358. 	if(numsk == 0) return(FALSE);
1359. 	return(taken);
1360. }
1361. 
1362. STATIC_OVL boolean
1363. inherits(shkp, numsk, croaked)
1364. struct monst *shkp;
1365. int numsk;
1366. boolean croaked;
1367. {
1368. 	long loss = 0L;
1369. 	struct eshk *eshkp = ESHK(shkp);
1370. 	boolean take = FALSE, taken = FALSE;
1371. 	int roomno = *u.ushops;
1372. 
1373. 	/* the simplifying principle is that first-come */
1374. 	/* already took everything you had.		*/
1375. 	if(numsk > 1) {
1376. 	    if(cansee(shkp->mx, shkp->my) && croaked)
1377. 		pline("%s %slooks at your corpse%s%s", Monnam(shkp),
1378. 		     (shkp->msleeping || !shkp->mcanmove) ?
1379. 				   "wakes up, " : "",
1380. 		     !rn2(2) ? (shkp->female ? ", shakes her head," :
1381. 				 ", shakes his head,") : "",
1382. 		     !inhishop(shkp) ? " and disappears. " : " and sighs.");
1383. 	    taken = (roomno == eshkp->shoproom);
1384. 	    goto skip;
1385. 	}
1386. 
1387. 	/* get one case out of the way: you die in the shop, the */
1388. 	/* shopkeeper is peaceful, nothing stolen, nothing owed. */
1389. 	if(roomno == eshkp->shoproom && inhishop(shkp) &&
1390. 	    !eshkp->billct && !eshkp->robbed && !eshkp->debit &&
1391. 	     NOTANGRY(shkp) && !eshkp->following) {
1392. 		if (invent)
1393. 			pline("%s gratefully inherits all your possessions.",
1394. 				shkname(shkp));
1395. 		set_repo_loc(eshkp);
1396. 		goto clear;
1397. 	}
1398. 
1399. 	if (eshkp->billct || eshkp->debit || eshkp->robbed) {
1400. 		if (roomno == eshkp->shoproom && inhishop(shkp))
1401. 		    loss = addupbill(shkp) + eshkp->debit;
1402. 		if (loss < eshkp->robbed) loss = eshkp->robbed;
1403. 		take = TRUE;
1404. 	}
1405. 
1406. 	if (eshkp->following || ANGRY(shkp) || take) {
1407. 		if (!invent && !u.ugold) goto skip;
1408. 
1409. 		if (loss > u.ugold || !loss || roomno == eshkp->shoproom) {
1410. 			eshkp->robbed -= u.ugold;
1411. 			if (eshkp->robbed < 0L) eshkp->robbed = 0L;
1412. 			shkp->mgold += u.ugold;
1413. 			u.ugold = 0L;
1414. 			flags.botl = 1;
1415. 			pline("%s %s%stakes all your possessions.",
1416. 			      shkname(shkp),
1417. 			      (shkp->msleeping || !shkp->mcanmove) ?
1418. 					"wakes up and " : "",
1419. 			      (distu(shkp->mx, shkp->my) > 2) ?
1420. 					"comes and " : "");
1421. 			taken = TRUE;
1422. 			/* where to put player's invent (after disclosure) */
1423. 			set_repo_loc(eshkp);
1424. 		} else {
1425. 			shkp->mgold += loss;
1426. 			u.ugold -= loss;
1427. 			flags.botl = 1;
1428. 			pline("%s %sand takes %ld zorkmid%s %sowed %s.",
1429. 			      Monnam(shkp),
1430. 			      (shkp->msleeping || !shkp->mcanmove) ?
1431. 					"wakes up " : "comes ",
1432. 			      loss, plur(loss),
1433. 			      strncmp(eshkp->customer, plname, PL_NSIZ) ?
1434. 					"" : "you ",
1435. 			      shkp->female ? "her" : "him");
1436. 			/* shopkeeper has now been paid in full */
1437. 			pacify_shk(shkp);
1438. 			eshkp->following = 0;
1439. 			eshkp->robbed = 0L;
1440. 		}
1441. skip:
1442. 		/* in case we create bones */
1443. 		shkp->msleeping = 0;
1444. 		if (!inhishop(shkp))
1445. 			home_shk(shkp, FALSE);
1446. 	}
1447. clear:
1448. 	setpaid(shkp);
1449. 	return(taken);
1450. }
1451. 
1452. STATIC_OVL void
1453. set_repo_loc(eshkp)
1454. struct eshk *eshkp;
1455. {
1456. 	register xchar ox, oy;
1457. 
1458. 	/* if you're not in this shk's shop room, or if you're in its doorway
1459. 	    or entry spot, then your gear gets dumped all the way inside */
1460. 	if (*u.ushops != eshkp->shoproom ||
1461. 		IS_DOOR(levl[u.ux][u.uy].typ) ||
1462. 		(u.ux == eshkp->shk.x && u.uy == eshkp->shk.y)) {
1463. 	    /* shk.x,shk.y is the position immediately in
1464. 	     * front of the door -- move in one more space
1465. 	     */
1466. 	    ox = eshkp->shk.x;
1467. 	    oy = eshkp->shk.y;
1468. 	    ox += sgn(ox - eshkp->shd.x);
1469. 	    oy += sgn(oy - eshkp->shd.y);
1470. 	} else {		/* already inside this shk's shop */
1471. 	    ox = u.ux;
1472. 	    oy = u.uy;
1473. 	}
1474. 	/* finish_paybill will deposit invent here */
1475. 	repo_location.x = ox;
1476. 	repo_location.y = oy;
1477. }
1478. 
1479. /* called at game exit, after inventory disclosure but before making bones */
1480. void finish_paybill()
1481. {
1482. 	register struct obj *otmp;
1483. 	int ox = repo_location.x,
1484. 	    oy = repo_location.y;
1485. 
1486. #if 0		/* don't bother */
1487. 	if (ox == 0 && oy == 0) impossible("finish_paybill: no location");
1488. #endif
1489. 	/* transfer all of the character's inventory to the shop floor */
1490. 	while ((otmp = invent) != 0) {
1491. 	    otmp->owornmask = 0L;	/* perhaps we should call setnotworn? */
1492. 	    otmp->lamplit = 0;		/* avoid "goes out" msg from freeinv */
1493. 	    if (rn2(5)) curse(otmp);	/* normal bones treatment for invent */
1494. 	    obj_extract_self(otmp);
1495. 	    place_object(otmp, ox, oy);
1496. 	}
1497. }
1498. 
1499. /* find obj on one of the lists */
1500. STATIC_OVL struct obj *
1501. bp_to_obj(bp)
1502. register struct bill_x *bp;
1503. {
1504. 	register struct obj *obj;
1505. 	register unsigned int id = bp->bo_id;
1506. 
1507. 	if(bp->useup)
1508. 		obj = o_on(id, billobjs);
1509. 	else
1510. 		obj = find_oid(id);
1511. 	return obj;
1512. }
1513. 
1514. /*
1515.  * Look for o_id on all lists but billobj.  Return obj or NULL if not found.
1516.  * Its OK for restore_timers() to call this function, there should not
1517.  * be any timeouts on the billobjs chain.
1518.  */
1519. struct obj *
1520. find_oid(id)
1521. unsigned id;
1522. {
1523. 	struct obj *obj;
1524. 	struct monst *mon, *mmtmp[3];
1525. 	int i;
1526. 
1527. 	/* first check various obj lists directly */
1528. 	if ((obj = o_on(id, invent)) != 0) return obj;
1529. 	if ((obj = o_on(id, fobj)) != 0) return obj;
1530. 	if ((obj = o_on(id, level.buriedobjlist)) != 0) return obj;
1531. 	if ((obj = o_on(id, migrating_objs)) != 0) return obj;
1532. 
1533. 	/* not found yet; check inventory for members of various monst lists */
1534. 	mmtmp[0] = fmon;
1535. 	mmtmp[1] = migrating_mons;
1536. 	mmtmp[2] = mydogs;		/* for use during level changes */
1537. 	for (i = 0; i < 3; i++)
1538. 	    for (mon = mmtmp[i]; mon; mon = mon->nmon)
1539. 		if ((obj = o_on(id, mon->minvent)) != 0) return obj;
1540. 
1541. 	/* not found at all */
1542. 	return (struct obj *)0;
1543. }
1544. #endif /*OVLB*/
1545. #ifdef OVL3
1546. 
1547. /* calculate the value that the shk will charge for [one of] an object */
1548. STATIC_OVL long
1549. get_cost(obj, shkp)
1550. register struct obj *obj;
1551. register struct monst *shkp;	/* if angry, impose a surcharge */
1552. {
1553. 	register long tmp = getprice(obj, FALSE);
1554. 
1555. 	if (!tmp) tmp = 5L;
1556. 	/* shopkeeper may notice if the player isn't very knowledgeable -
1557. 	   especially when gem prices are concerned */
1558. 	if (!obj->dknown || !objects[obj->otyp].oc_name_known) {
1559. 		if (obj->oclass == GEM_CLASS) {
1560. 			/* all gems are priced high - real or not */
1561. 			if (objects[obj->otyp].oc_material == GLASS) {
1562. 			    int i = obj->otyp - LUCKSTONE + JADE + 1;
1563. 			    /* real gem's cost (worthless gems come
1564. 			       after jade but before luckstone) */
1565. 			    tmp = (long) objects[i].oc_cost;
1566. 			}
1567. 		} else if (!(obj->o_id % 4)) /* arbitrarily impose surcharge */
1568. 			tmp += tmp / 3L;
1569. 	}
1570. #ifdef TOURIST
1571. 	if ((Role_if(PM_TOURIST) && u.ulevel < (MAXULEV/2))
1572. 	    || (uarmu && !uarm && !uarmc))	/* touristy shirt visible */
1573. 		tmp += tmp / 3L;
1574. 	else
1575. #endif
1576. 	if (uarmh && uarmh->otyp == DUNCE_CAP)
1577. 		tmp += tmp / 3L;
1578. 
1579. 	if (ACURR(A_CHA) > 18)		tmp /= 2L;
1580. 	else if (ACURR(A_CHA) > 17)	tmp -= tmp / 3L;
1581. 	else if (ACURR(A_CHA) > 15)	tmp -= tmp / 4L;
1582. 	else if (ACURR(A_CHA) < 6)	tmp *= 2L;
1583. 	else if (ACURR(A_CHA) < 8)	tmp += tmp / 2L;
1584. 	else if (ACURR(A_CHA) < 11)	tmp += tmp / 3L;
1585. 	if (tmp <= 0L) tmp = 1L;
1586. 	else if (obj->oartifact) tmp *= 4L;
1587. 	/* anger surcharge should match rile_shk's */
1588. 	if (shkp && ESHK(shkp)->surcharge) tmp += (tmp + 2L) / 3L;
1589. 	return tmp;
1590. }
1591. #endif /*OVL3*/
1592. #ifdef OVLB
1593. 
1594. /* returns the price of a container's content.  the price
1595.  * of the "top" container is added in the calling functions.
1596.  * a different price quoted for selling as vs. buying.
1597.  */
1598. long
1599. contained_cost(obj, shkp, price, usell)
1600. register struct obj *obj;
1601. register struct monst *shkp;
1602. long price;
1603. register boolean usell;
1604. {
1605. 	register struct obj *otmp;
1606. 
1607. 	/* the price of contained objects */
1608. 	for (otmp = obj->cobj; otmp; otmp = otmp->nobj) {
1609. 	    if (otmp->oclass == GOLD_CLASS) continue;
1610. 	    /* the "top" container is evaluated by caller */
1611. 	    if (usell) {
1612. 		if (saleable(shkp, otmp) &&
1613. 			!otmp->unpaid && otmp->oclass != BALL_CLASS &&
1614. 			!(otmp->oclass == FOOD_CLASS && otmp->oeaten) &&
1615. 			!(Is_candle(otmp) && otmp->age <
1616. 				20L * (long)objects[otmp->otyp].oc_cost))
1617. 		    price += set_cost(otmp, shkp);
1618. 	    } else if (!otmp->no_charge) {
1619. 		    price += get_cost(otmp, shkp) * otmp->quan;
1620. 	    }
1621. 
1622. 	    if (Has_contents(otmp))
1623. 		    price += contained_cost(otmp, shkp, price, usell);
1624. 	}
1625. 
1626. 	return(price);
1627. }
1628. 
1629. long
1630. contained_gold(obj)
1631. register struct obj *obj;
1632. {
1633. 	register struct obj *otmp;
1634. 	register long value = 0L;
1635. 
1636. 	/* accumulate contained gold */
1637. 	for (otmp = obj->cobj; otmp; otmp = otmp->nobj)
1638. 	    if (otmp->oclass == GOLD_CLASS)
1639. 		value += otmp->quan;
1640. 	    else if (Has_contents(otmp))
1641. 		value += contained_gold(otmp);
1642. 
1643. 	return(value);
1644. }
1645. 
1646. STATIC_OVL void
1647. dropped_container(obj, shkp, sale)
1648. register struct obj *obj;
1649. register struct monst *shkp;
1650. register boolean sale;
1651. {
1652. 	register struct obj *otmp;
1653. 
1654. 	/* the "top" container is treated in the calling fn */
1655. 	for (otmp = obj->cobj; otmp; otmp = otmp->nobj) {
1656. 	    if (otmp->oclass == GOLD_CLASS) continue;
1657. 
1658. 	    if (!otmp->unpaid && !(sale && saleable(shkp, otmp)))
1659. 		otmp->no_charge = 1;
1660. 
1661. 	    if (Has_contents(otmp))
1662. 		dropped_container(otmp, shkp, sale);
1663. 	}
1664. }
1665. 
1666. void
1667. picked_container(obj)
1668. register struct obj *obj;
1669. {
1670. 	register struct obj *otmp;
1671. 
1672. 	/* the "top" container is treated in the calling fn */
1673. 	for (otmp = obj->cobj; otmp; otmp = otmp->nobj) {
1674. 	    if (otmp->oclass == GOLD_CLASS) continue;
1675. 
1676. 	    if (otmp->no_charge)
1677. 		otmp->no_charge = 0;
1678. 
1679. 	    if (Has_contents(otmp))
1680. 		picked_container(otmp);
1681. 	}
1682. }
1683. #endif /*OVLB*/
1684. #ifdef OVL3
1685. 
1686. /* calculate how much the shk will pay when buying [all of] an object */
1687. STATIC_OVL long
1688. set_cost(obj, shkp)
1689. register struct obj *obj;
1690. register struct monst *shkp;
1691. {
1692. 	long tmp = getprice(obj, TRUE) * obj->quan;
1693. 
1694. #ifdef TOURIST
1695. 	if ((Role_if(PM_TOURIST) && u.ulevel < (MAXULEV/2))
1696. 	    || (uarmu && !uarm && !uarmc))	/* touristy shirt visible */
1697. 		tmp /= 3L;
1698. 	else
1699. #endif
1700. 	if (uarmh && uarmh->otyp == DUNCE_CAP)
1701. 		tmp /= 3L;
1702. 	else
1703. 		tmp /= 2L;
1704. 
1705. 	/* shopkeeper may notice if the player isn't very knowledgeable -
1706. 	   especially when gem prices are concerned */
1707. 	if (!obj->dknown || !objects[obj->otyp].oc_name_known) {
1708. 		if (obj->oclass == GEM_CLASS) {
1709. 			/* different shop keepers give different prices */
1710. 			if (objects[obj->otyp].oc_material == GEMSTONE ||
1711. 			    objects[obj->otyp].oc_material == GLASS) {
1712. 				tmp = (obj->otyp % (6 - shkp->m_id % 3));
1713. 				tmp = (tmp + 3) * obj->quan;
1714. 			}
1715. 		} else if (tmp > 1L && !rn2(4))
1716. 			tmp -= tmp / 4L;
1717. 	}
1718. 	return tmp;
1719. }
1720. 
1721. #endif /*OVL3*/
1722. #ifdef OVLB
1723. 
1724. /* called from doinv(invent.c) for inventory of unpaid objects */
1725. long
1726. unpaid_cost(unp_obj)
1727. register struct obj *unp_obj;	/* known to be unpaid */
1728. {
1729. 	register struct bill_x *bp = (struct bill_x *)0;
1730. 	register struct monst *shkp;
1731. 
1732. 	for(shkp = next_shkp(fmon, TRUE); shkp;
1733. 					shkp = next_shkp(shkp->nmon, TRUE))
1734. 	    if ((bp = onbill(unp_obj, shkp, TRUE)) != 0) break;
1735. 
1736. 	/* onbill() gave no message if unexpected problem occurred */
1737. 	if(!bp) impossible("unpaid_cost: object wasn't on any bill!");
1738. 
1739. 	return bp ? unp_obj->quan * bp->price : 0L;
1740. }
1741. 
1742. STATIC_OVL void
1743. add_one_tobill(obj, dummy)
1744. register struct obj *obj;
1745. register boolean dummy;
1746. {
1747. 	register struct monst *shkp;
1748. 	register struct bill_x *bp;
1749. 	register int bct;
1750. 	register char roomno = *u.ushops;
1751. 
1752. 	if (!roomno) return;
1753. 	if (!(shkp = shop_keeper(roomno))) return;
1754. 	if (!inhishop(shkp)) return;
1755. 
1756. 	if (onbill(obj, shkp, FALSE) || /* perhaps thrown away earlier */
1757. 		    (obj->oclass == FOOD_CLASS && obj->oeaten))
1758. 		return;
1759. 
1760. 	if (ESHK(shkp)->billct == BILLSZ) {
1761. 		You("got that for free!");
1762. 		return;
1763. 	}
1764. 
1765. 	/* To recognize objects the shopkeeper is not interested in. -dgk
1766. 	 */
1767. 	if (obj->no_charge) {
1768. 		obj->no_charge = 0;
1769. 		return;
1770. 	}
1771. 
1772. 	bct = ESHK(shkp)->billct;
1773. 	bp = &(ESHK(shkp)->bill_p[bct]);
1774. 	bp->bo_id = obj->o_id;
1775. 	bp->bquan = obj->quan;
1776. 	if(dummy) {		  /* a dummy object must be inserted into  */
1777. 	    bp->useup = 1;	  /* the billobjs chain here.  crucial for */
1778. 	    add_to_billobjs(obj); /* eating floorfood in shop.  see eat.c  */
1779. 	} else	bp->useup = 0;
1780. 	bp->price = get_cost(obj, shkp);
1781. 	ESHK(shkp)->billct++;
1782. 	obj->unpaid = 1;
1783. }
1784. 
1785. STATIC_OVL void
1786. add_to_billobjs(obj)
1787.     struct obj *obj;
1788. {
1789.     if (obj->where != OBJ_FREE)
1790. 	panic("add_to_billobjs: obj not free");
1791.     if (obj->timed)
1792. 	panic("add_to_billobjs: obj is timed");
1793. 
1794.     obj->nobj = billobjs;
1795.     billobjs = obj;
1796.     obj->where = OBJ_ONBILL;
1797. }
1798. 
1799. /* recursive billing of objects within containers. */
1800. STATIC_OVL void
1801. bill_box_content(obj, ininv, dummy, shkp)
1802. register struct obj *obj;
1803. register boolean ininv, dummy;
1804. register struct monst *shkp;
1805. {
1806. 	register struct obj *otmp;
1807. 
1808. 	for (otmp = obj->cobj; otmp; otmp = otmp->nobj) {
1809. 		if (otmp->oclass == GOLD_CLASS) continue;
1810. 
1811. 		/* the "top" box is added in addtobill() */
1812. 		if (!otmp->no_charge)
1813. 		    add_one_tobill(otmp, dummy);
1814. 		if (Has_contents(otmp))
1815. 		    bill_box_content(otmp, ininv, dummy, shkp);
1816. 	}
1817. 
1818. }
1819. 
1820. /* shopkeeper tells you what you bought or sold, sometimes partly IDing it */
1821. STATIC_OVL void
1822. shk_names_obj(shkp, obj, fmt, amt, arg)
1823. struct monst *shkp;
1824. struct obj *obj;
1825. const char *fmt;	/* "%s %ld %s %s", doname(obj), amt, plur(amt), arg */
1826. long amt;
1827. const char *arg;
1828. {
1829. 	char *obj_name, fmtbuf[BUFSZ];
1830. 	boolean was_unknown = !obj->dknown;
1831. 
1832. 	obj->dknown = TRUE;
1833. 	/* Use real name for ordinary weapons/armor, and spell-less
1834. 	 * scrolls/books (that is, blank and mail), but only if the
1835. 	 * object is within the shk's area of interest/expertise.
1836. 	 */
1837. 	if (!objects[obj->otyp].oc_magic && saleable(shkp, obj) &&
1838. 	    (obj->oclass == WEAPON_CLASS || obj->oclass == ARMOR_CLASS ||
1839. 	     obj->oclass == SCROLL_CLASS || obj->oclass == SPBOOK_CLASS ||
1840. 	     obj->otyp == MIRROR)) {
1841. 	    was_unknown |= !objects[obj->otyp].oc_name_known;
1842. 	    makeknown(obj->otyp);
1843. 	}
1844. 	obj_name = doname(obj);
1845. 	/* Use an alternate message when extra information is being provided */
1846. 	if (was_unknown) {
1847. 	    Sprintf(fmtbuf, "%%s; you %s", fmt);
1848. 	    obj_name[0] = highc(obj_name[0]);
1849. 	    pline(fmtbuf, obj_name, (obj->quan > 1) ? "them" : "it",
1850. 		  amt, plur(amt), arg);
1851. 	} else {
1852. 	    You(fmt, obj_name, amt, plur(amt), arg);
1853. 	}
1854. }
1855. 
1856. void
1857. addtobill(obj, ininv, dummy, silent)
1858. register struct obj *obj;
1859. register boolean ininv, dummy, silent;
1860. {
1861. 	register struct monst *shkp;
1862. 	register char roomno = *u.ushops;
1863. 	long ltmp = 0L, cltmp = 0L, gltmp = 0L;
1864. 	register boolean container = Has_contents(obj);
1865. 
1866. 	if(!*u.ushops) return;
1867. 
1868. 	if(!(shkp = shop_keeper(roomno))) return;
1869. 
1870. 	if(!inhishop(shkp)) return;
1871. 
1872. 	if(/* perhaps we threw it away earlier */
1873. 		 onbill(obj, shkp, FALSE) ||
1874. 		 (obj->oclass == FOOD_CLASS && obj->oeaten)
1875. 	      ) return;
1876. 
1877. 	if(ESHK(shkp)->billct == BILLSZ) {
1878. 		You("got that for free!");
1879. 		return;
1880. 	}
1881. 
1882. 	if(obj->oclass == GOLD_CLASS) {
1883. 		costly_gold(obj->ox, obj->oy, obj->quan);
1884. 		return;
1885. 	}
1886. 
1887. 	if(!obj->no_charge)
1888. 	    ltmp = get_cost(obj, shkp);
1889. 
1890. 	if (obj->no_charge && !container) {
1891. 		obj->no_charge = 0;
1892. 		return;
1893. 	}
1894. 
1895. 	if(container) {
1896. 	    if(obj->cobj == (struct obj *)0) {
1897. 		if(obj->no_charge) {
1898. 		    obj->no_charge = 0;
1899. 		    return;
1900. 		} else {
1901. 		    add_one_tobill(obj, dummy);
1902. 		    goto speak;
1903. 		}
1904. 	    } else {
1905. 		cltmp += contained_cost(obj, shkp, cltmp, FALSE);
1906. 		gltmp += contained_gold(obj);
1907. 	    }
1908. 
1909. 	    if(ltmp) add_one_tobill(obj, dummy);
1910. 	    if(cltmp) bill_box_content(obj, ininv, dummy, shkp);
1911. 	    picked_container(obj); /* reset contained obj->no_charge */
1912. 
1913. 	    ltmp += cltmp;
1914. 
1915. 	    if(gltmp) {
1916. 		costly_gold(obj->ox, obj->oy, gltmp);
1917. 		if(!ltmp) return;
1918. 	    }
1919. 
1920. 	    if(obj->no_charge)
1921. 		obj->no_charge = 0;
1922. 
1923. 	} else /* i.e., !container */
1924. 	    add_one_tobill(obj, dummy);
1925. speak:
1926. 	if (shkp->mcanmove && !shkp->msleeping && !silent) {
1927. 	    char buf[BUFSZ];
1928. 
1929. 	    if(!ltmp) {
1930. 		pline("%s has no interest in %s.", Monnam(shkp),
1931. 					     the(xname(obj)));
1932. 		return;
1933. 	    }
1934. 	    Strcpy(buf, "\"For you, ");
1935. 	    if (ANGRY(shkp)) Strcat(buf, "scum ");
1936. 	    else {
1937. 		static const char *honored[5] = {
1938. 		  "good", "honored", "most gracious", "esteemed",
1939. 		  "most renowned and sacred"
1940. 		};
1941. 		Strcat(buf, honored[rn2(4) + u.uevent.udemigod]);
1942. 		if (!is_human(youmonst.data)) Strcat(buf, " creature");
1943. 		else
1944. 		    Strcat(buf, (flags.female) ? " lady" : " sir");
1945. 	    }
1946. 	    if(ininv) {
1947. 		long quan = obj->quan;
1948. 		obj->quan = 1L; /* fool xname() into giving singular */
1949. 		pline("%s; only %ld %s %s.\"", buf, ltmp,
1950. 			(quan > 1L) ? "per" : "for this", xname(obj));
1951. 		obj->quan = quan;
1952. 	    } else
1953. 		pline("%s will cost you %ld zorkmid%s%s.",
1954. 			The(xname(obj)), ltmp, plur(ltmp),
1955. 			(obj->quan > 1L) ? " each" : "");
1956. 	} else if(!silent) {
1957. 	    if(ltmp) pline_The("list price of %s is %ld zorkmid%s%s.",
1958. 				   the(xname(obj)), ltmp, plur(ltmp),
1959. 				   (obj->quan > 1L) ? " each" : "");
1960. 	    else pline("%s does not notice.", Monnam(shkp));
1961. 	}
1962. }
1963. 
1964. void
1965. splitbill(obj, otmp)
1966. register struct obj *obj, *otmp;
1967. {
1968. 	/* otmp has been split off from obj */
1969. 	register struct bill_x *bp;
1970. 	register long tmp;
1971. 	register struct monst *shkp = shop_keeper(*u.ushops);
1972. 
1973. 	if(!shkp || !inhishop(shkp)) {
1974. 		impossible("splitbill: no resident shopkeeper??");
1975. 		return;
1976. 	}
1977. 	bp = onbill(obj, shkp, FALSE);
1978. 	if(!bp) {
1979. 		impossible("splitbill: not on bill?");
1980. 		return;
1981. 	}
1982. 	if(bp->bquan < otmp->quan) {
1983. 		impossible("Negative quantity on bill??");
1984. 	}
1985. 	if(bp->bquan == otmp->quan) {
1986. 		impossible("Zero quantity on bill??");
1987. 	}
1988. 	bp->bquan -= otmp->quan;
1989. 
1990. 	if(ESHK(shkp)->billct == BILLSZ) otmp->unpaid = 0;
1991. 	else {
1992. 		tmp = bp->price;
1993. 		bp = &(ESHK(shkp)->bill_p[ESHK(shkp)->billct]);
1994. 		bp->bo_id = otmp->o_id;
1995. 		bp->bquan = otmp->quan;
1996. 		bp->useup = 0;
1997. 		bp->price = tmp;
1998. 		ESHK(shkp)->billct++;
1999. 	}
2000. }
2001. 
2002. STATIC_OVL void
2003. sub_one_frombill(obj, shkp)
2004. register struct obj *obj;
2005. register struct monst *shkp;
2006. {
2007. 	register struct bill_x *bp;
2008. 
2009. 	if((bp = onbill(obj, shkp, FALSE)) != 0) {
2010. 		register struct obj *otmp;
2011. 
2012. 		obj->unpaid = 0;
2013. 		if(bp->bquan > obj->quan){
2014. 			otmp = newobj(0);
2015. 			*otmp = *obj;
2016. 			bp->bo_id = otmp->o_id = flags.ident++;
2017. 			otmp->where = OBJ_FREE;
2018. 			otmp->quan = (bp->bquan -= obj->quan);
2019. 			otmp->owt = 0;	/* superfluous */
2020. 			otmp->onamelth = 0;
2021. 			otmp->oxlth = 0;
2022. 			otmp->oattached = OATTACHED_NOTHING;
2023. 			bp->useup = 1;
2024. 			add_to_billobjs(otmp);
2025. 			return;
2026. 		}
2027. 		ESHK(shkp)->billct--;
2028. #ifdef DUMB
2029. 		{
2030. 		/* DRS/NS 2.2.6 messes up -- Peter Kendell */
2031. 			int indx = ESHK(shkp)->billct;
2032. 			*bp = ESHK(shkp)->bill_p[indx];
2033. 		}
2034. #else
2035. 		*bp = ESHK(shkp)->bill_p[ESHK(shkp)->billct];
2036. #endif
2037. 		return;
2038. 	} else if (obj->unpaid) {
2039. 		impossible("sub_one_frombill: unpaid object not on bill");
2040. 		obj->unpaid = 0;
2041. 	}
2042. }
2043. 
2044. /* recursive check of unpaid objects within nested containers. */
2045. void
2046. subfrombill(obj, shkp)
2047. register struct obj *obj;
2048. register struct monst *shkp;
2049. {
2050. 	register struct obj *otmp;
2051. 
2052. 	sub_one_frombill(obj, shkp);
2053. 
2054. 	if (Has_contents(obj))
2055. 	    for(otmp = obj->cobj; otmp; otmp = otmp->nobj) {
2056. 		if(otmp->oclass == GOLD_CLASS) continue;
2057. 
2058. 		if (Has_contents(otmp))
2059. 		    subfrombill(otmp, shkp);
2060. 		else
2061. 		    sub_one_frombill(otmp, shkp);
2062. 	    }
2063. }
2064. 
2065. #endif /*OVLB*/
2066. #ifdef OVL3
2067. 
2068. STATIC_OVL long
2069. stolen_container(obj, shkp, price, ininv)
2070. register struct obj *obj;
2071. register struct monst *shkp;
2072. long price;
2073. register boolean ininv;
2074. {
2075. 	register struct obj *otmp;
2076. 
2077. 	if(ininv && obj->unpaid)
2078. 	    price += get_cost(obj, shkp);
2079. 	else {
2080. 	    if(!obj->no_charge)
2081. 		price += get_cost(obj, shkp);
2082. 	    obj->no_charge = 0;
2083. 	}
2084. 
2085. 	/* the price of contained objects, if any */
2086. 	for(otmp = obj->cobj; otmp; otmp = otmp->nobj) {
2087. 
2088. 	    if(otmp->oclass == GOLD_CLASS) continue;
2089. 
2090. 	    if (!Has_contents(otmp)) {
2091. 		if(ininv) {
2092. 		    if(otmp->unpaid)
2093. 			price += get_cost(otmp, shkp);
2094. 		} else {
2095. 		    if(!otmp->no_charge) {
2096. 			if(!(otmp->oclass == BALL_CLASS ||
2097. 			    (otmp->oclass == FOOD_CLASS && otmp->oeaten) ||
2098. 			    (Is_candle(otmp) && otmp->age <
2099. 				  20L * (long)objects[otmp->otyp].oc_cost))
2100. 			  ) price += get_cost(otmp, shkp);
2101. 		    }
2102. 		    otmp->no_charge = 0;
2103. 		}
2104. 	    } else
2105. 		price += stolen_container(otmp, shkp, price, ininv);
2106. 	}
2107. 
2108. 	return(price);
2109. }
2110. #endif /*OVL3*/
2111. #ifdef OVLB
2112. 
2113. long
2114. stolen_value(obj, x, y, peaceful, silent)
2115. register struct obj *obj;
2116. register xchar x, y;
2117. register boolean peaceful, silent;
2118. {
2119. 	register long value = 0L, gvalue = 0L;
2120. 	register struct monst *shkp = shop_keeper(*in_rooms(x, y, SHOPBASE));
2121. 
2122. 	if (!shkp || !inhishop(shkp))
2123. 	    return (0L);
2124. 
2125. 	if(obj->oclass == GOLD_CLASS) {
2126. 	    gvalue += obj->quan;
2127. 	} else if (Has_contents(obj)) {
2128. 	    register boolean ininv = !!count_unpaid(obj->cobj);
2129. 
2130. 	    value += stolen_container(obj, shkp, value, ininv);
2131. 	    if(!ininv) gvalue += contained_gold(obj);
2132. 	} else if (!obj->no_charge && saleable(shkp, obj)) {
2133. 	    value += get_cost(obj, shkp);
2134. 	}
2135. 
2136. 	if(gvalue + value == 0L) return(0L);
2137. 
2138. 	value += gvalue;
2139. 
2140. 	if(peaceful) {
2141. 	    value = check_credit(value, shkp);
2142. 	    ESHK(shkp)->debit += value;
2143. 
2144. 	    if(!silent) {
2145. 		if(obj->oclass == GOLD_CLASS)
2146. 		    You("owe %s %ld zorkmids!", mon_nam(shkp), value);
2147. 		else You("owe %s %ld zorkmids for %s!",
2148. 			mon_nam(shkp),
2149. 			value,
2150. 			obj->quan > 1L ? "them" : "it");
2151. 	    }
2152. 	} else {
2153. 	    ESHK(shkp)->robbed += value;
2154. 
2155. 	    if(!silent) {
2156. 		if(cansee(shkp->mx, shkp->my)) {
2157. 		    if(ESHK(shkp)->customer[0] == 0)
2158. 			(void) strncpy(ESHK(shkp)->customer,plname,PL_NSIZ);
2159. 		    Norep("%s booms: \"%s, you are a thief!\"",
2160. 				Monnam(shkp), plname);
2161. 		} else  Norep("You hear a scream, \"Thief!\"");
2162. 	    }
2163. 	    hot_pursuit(shkp);
2164. 	    (void) angry_guards(FALSE);
2165. 	}
2166. 	return(value);
2167. }
2168. 
2169. /* auto-response flag for/from "sell foo?" 'a' => 'y', 'q' => 'n' */
2170. static char sell_response = 'a';
2171. static boolean sell_voluntarily = FALSE;
2172. 
2173. void
2174. sellobj_state(deliberate)	/* called from dodrop(do.c) and doddrop() */
2175. boolean deliberate;
2176. {
2177. 	/* If we're deliberately dropping something, there's no automatic
2178. 	response to the shopkeeper's "want to sell" query; however, if we
2179. 	accidentally drop anything, the shk will buy it/them without asking.
2180. 	This retains the old pre-query risk that slippery fingers while in
2181. 	shops entailed:  you drop it, you've lost it.
2182. 	 */
2183. 	sell_response = deliberate ? '\0' : 'a';
2184. 	sell_voluntarily = deliberate;
2185. }
2186. 
2187. void
2188. sellobj(obj, x, y)
2189. register struct obj *obj;
2190. xchar x, y;
2191. {
2192. 	register struct monst *shkp;
2193. 	register struct eshk *eshkp;
2194. 	long ltmp = 0L, cltmp = 0L, gltmp = 0L, offer;
2195. 	boolean saleitem, cgold = FALSE, container = Has_contents(obj);
2196. 	boolean isgold = (obj->oclass == GOLD_CLASS);
2197. 
2198. 	if(!(shkp = shop_keeper(*in_rooms(x, y, SHOPBASE))) ||
2199. 	   !inhishop(shkp)) return;
2200. 	if(!costly_spot(x, y))	return;
2201. 	if(!*u.ushops) return;
2202. 
2203. 	if(obj->unpaid && !container && !isgold) {
2204. 	    sub_one_frombill(obj, shkp);
2205. 	    return;
2206. 	}
2207. 	if(container) {
2208. 		/* find the price of content before subfrombill */
2209. 		cltmp += contained_cost(obj, shkp, cltmp, TRUE);
2210. 		/* find the value of contained gold */
2211. 		gltmp += contained_gold(obj);
2212. 		cgold = (gltmp > 0L);
2213. 	}
2214. 
2215. 	saleitem = saleable(shkp, obj);
2216. 	if(!isgold && !obj->unpaid && saleitem)
2217. 	    ltmp = set_cost(obj, shkp);
2218. 
2219. 	offer = ltmp + cltmp;
2220. 
2221. 	/* get one case out of the way: nothing to sell, and no gold */
2222. 	if(!isgold && (offer + gltmp) == 0L) {
2223. 		register boolean unpaid = (obj->unpaid ||
2224. 				  (container && count_unpaid(obj->cobj)));
2225. 
2226. 		if(container) {
2227. 			dropped_container(obj, shkp, FALSE);
2228. 			if(!obj->unpaid && !saleitem)
2229. 			    obj->no_charge = 1;
2230. 			if(obj->unpaid || count_unpaid(obj->cobj))
2231. 			    subfrombill(obj, shkp);
2232. 		} else obj->no_charge = 1;
2233. 
2234. 		if(!unpaid)
2235. 		    pline("%s seems uninterested.", Monnam(shkp));
2236. 		return;
2237. 	}
2238. 
2239. 	/* you dropped something of your own - probably want to sell it */
2240. 	if (shkp->msleeping || !shkp->mcanmove) {
2241. 		if (container)
2242. 		    dropped_container(obj, shkp, TRUE);
2243. 		if (!obj->unpaid)
2244. 		    obj->no_charge = 1;
2245. 		if (!shkp->mcanmove) {
2246. 		    if(ANGRY(shkp) && !rn2(4))
2247. 			pline("%s utters a curse.", Monnam(shkp));
2248. 		    else pline("%s is indisposed.", Monnam(shkp));
2249. 		} else if(!rn2(3)) {
2250. 		    pline("%s snores indifferently.", Monnam(shkp));
2251. 		}
2252. 		subfrombill(obj, shkp);
2253. 		return;
2254. 	}
2255. 
2256. 	eshkp = ESHK(shkp);
2257. 
2258. 	if (ANGRY(shkp)) { /* they become shop-objects, no pay */
2259. 		pline("Thank you, scum!");
2260. 		subfrombill(obj, shkp);
2261. 		return;
2262. 	}
2263. 
2264. 	if(eshkp->robbed) {  /* shkp is not angry? */
2265. 		if(isgold) offer = obj->quan;
2266. 		else if(cgold) offer += cgold;
2267. 		if((eshkp->robbed -= offer < 0L))
2268. 			eshkp->robbed = 0L;
2269. 		if(offer) verbalize(
2270.   "Thank you for your contribution to restock this recently plundered shop.");
2271. 		subfrombill(obj, shkp);
2272. 		return;
2273. 	}
2274. 
2275. 	if(isgold || cgold) {
2276. 		if(!cgold) gltmp = obj->quan;
2277. 
2278. 		if(eshkp->debit >= gltmp) {
2279. 		    if(eshkp->loan) { /* you carry shop's gold */
2280. 			 if(eshkp->loan >= gltmp)
2281. 			     eshkp->loan -= gltmp;
2282. 			 else eshkp->loan = 0L;
2283. 		    }
2284. 		    eshkp->debit -= gltmp;
2285. 		    Your("debt is %spaid off.",
2286. 				eshkp->debit ? "partially " : "");
2287. 		} else {
2288. 		    long delta = gltmp - eshkp->debit;
2289. 
2290. 		    eshkp->credit += delta;
2291. 		    if(eshkp->debit) {
2292. 			eshkp->debit = 0L;
2293. 			eshkp->loan = 0L;
2294. 			Your("debt is paid off.");
2295. 		    }
2296. 		    pline("%ld zorkmid%s added to your credit.",
2297. 				delta, delta > 1L ? "s are" : " is");
2298. 		}
2299. 		if(offer) goto move_on;
2300. 		else {
2301. 		    if(!isgold) {
2302. 			if (container)
2303. 			    dropped_container(obj, shkp, FALSE);
2304. 			if (!obj->unpaid && !saleitem) obj->no_charge = 1;
2305. 			subfrombill(obj, shkp);
2306. 		    }
2307. 		    return;
2308. 		}
2309. 	}
2310. move_on:
2311. 	if((!saleitem && !(container && cltmp > 0L))
2312. 	   || eshkp->billct == BILLSZ
2313. 	   || obj->oclass == BALL_CLASS
2314. 	   || obj->oclass == CHAIN_CLASS || offer == 0L
2315. 	   || (obj->oclass == FOOD_CLASS && obj->oeaten)
2316. 	   || (Is_candle(obj) &&
2317. 		   obj->age < 20L * (long)objects[obj->otyp].oc_cost)) {
2318. 		pline("%s seems not interested%s.", Monnam(shkp),
2319. 			cgold ? " in the rest" : "");
2320. 		if (container)
2321. 		    dropped_container(obj, shkp, FALSE);
2322. 		obj->no_charge = 1;
2323. 		return;
2324. 	}
2325. 
2326. 	if(!shkp->mgold) {
2327. 		char c, qbuf[BUFSZ];
2328. 		long tmpcr = ((offer * 9L) / 10L) + (offer <= 1L);
2329. 
2330. 		if (!sell_voluntarily) {
2331. 		    c = sell_response = 'y';
2332. 		} else if (sell_response != 'n') {
2333. 		    pline("%s cannot pay you at present.", Monnam(shkp));
2334. 		    Sprintf(qbuf,
2335. 			    "Will you accept %ld zorkmid%s in credit for %s?",
2336. 			    tmpcr, plur(tmpcr), doname(obj));
2337. 		    /* won't accept 'a' response here */
2338. 		    c = ynq(qbuf);
2339. 		} else		/* previously specified "quit" */
2340. 		    c = 'n';
2341. 
2342. 		if (c == 'y') {
2343. 		    shk_names_obj(shkp, obj, sell_voluntarily ?
2344. 			    "traded %s for %ld zorkmid%s in %scredit." :
2345. 			"relinquish %s and acquire %ld zorkmid%s in %scredit.",
2346. 			    tmpcr,
2347. 			    (eshkp->credit > 0L) ? "additional " : "");
2348. 		    eshkp->credit += tmpcr;
2349. 		    subfrombill(obj, shkp);
2350. 		} else {
2351. 		    if (c == 'q') sell_response = 'n';
2352. 		    if (container)
2353. 			dropped_container(obj, shkp, FALSE);
2354. 		    if (!obj->unpaid) obj->no_charge = 1;
2355. 		    subfrombill(obj, shkp);
2356. 		}
2357. 	} else {
2358. 		char qbuf[BUFSZ];
2359. 		boolean short_funds = (offer > shkp->mgold);
2360. 
2361. 		if (short_funds) offer = shkp->mgold;
2362. 
2363. 		if (!sell_response) {
2364. 		    Sprintf(qbuf,
2365. 			 "%s offers%s %ld gold piece%s for%s %s %s.  Sell %s?",
2366. 			    Monnam(shkp), short_funds ? " only" : "",
2367. 			    offer, plur(offer),
2368. 			    (!ltmp && cltmp) ? " the contents of" : "",
2369. 			    obj->unpaid ? "the" : "your", xname(obj),
2370. 			    (obj->quan == 1L) ? "it" : "them");
2371. 		} else  qbuf[0] = '\0';		/* just to pacify lint */
2372. 
2373. 		switch (sell_response ? sell_response : ynaq(qbuf)) {
2374. 		 case 'q':  sell_response = 'n';
2375. 		 case 'n':  if (container)
2376. 				dropped_container(obj, shkp, FALSE);
2377. 			    if (!obj->unpaid) obj->no_charge = 1;
2378. 			    subfrombill(obj, shkp);
2379. 			    break;
2380. 		 case 'a':  sell_response = 'y';
2381. 		 case 'y':  if (container)
2382. 				dropped_container(obj, shkp, TRUE);
2383. 			    if (!obj->unpaid && !saleitem) obj->no_charge = 1;
2384. 			    subfrombill(obj, shkp);
2385. 			    pay(-offer, shkp);
2386. 			    shk_names_obj(shkp, obj, sell_voluntarily ?
2387. 				    "sold %s for %ld gold piece%s.%s" :
2388. 	       "relinquish %s and receive %ld gold piece%s in compensation.%s",
2389. 				    offer, "");
2390. 			    break;
2391. 		 default:   impossible("invalid sell response");
2392. 		}
2393. 	}
2394. }
2395. 
2396. int
2397. doinvbill(mode)
2398. int mode;		/* 0: deliver count 1: paged */
2399. {
2400. #ifdef	__SASC
2401. 	void sasc_bug(struct obj *, unsigned);
2402. #endif
2403. 	struct monst *shkp;
2404. 	struct eshk *eshkp;
2405. 	struct bill_x *bp, *end_bp;
2406. 	struct obj *obj;
2407. 	long totused;
2408. 	char *buf_p;
2409. 	winid datawin;
2410. 
2411. 	shkp = shop_keeper(*u.ushops);
2412. 	if (!shkp || !inhishop(shkp)) {
2413. 	    if (mode != 0) impossible("doinvbill: no shopkeeper?");
2414. 	    return 0;
2415. 	}
2416. 	eshkp = ESHK(shkp);
2417. 
2418. 	if (mode == 0) {
2419. 	    /* count expended items, so that the `I' command can decide
2420. 	       whether to include 'x' in its prompt string */
2421. 	    int cnt = !eshkp->debit ? 0 : 1;
2422. 
2423. 	    for (bp = eshkp->bill_p, end_bp = &eshkp->bill_p[eshkp->billct];
2424. 		    bp < end_bp; bp++)
2425. 		if (bp->useup ||
2426. 			((obj = bp_to_obj(bp)) != 0 && obj->quan < bp->bquan))
2427. 		    cnt++;
2428. 	    return cnt;
2429. 	}
2430. 
2431. 	datawin = create_nhwindow(NHW_MENU);
2432. 	putstr(datawin, 0, "Unpaid articles already used up:");
2433. 	putstr(datawin, 0, "");
2434. 
2435. 	totused = 0L;
2436. 	for (bp = eshkp->bill_p, end_bp = &eshkp->bill_p[eshkp->billct];
2437. 		bp < end_bp; bp++) {
2438. 	    obj = bp_to_obj(bp);
2439. 	    if(!obj) {
2440. 		impossible("Bad shopkeeper administration.");
2441. 		goto quit;
2442. 	    }
2443. 	    if(bp->useup || bp->bquan > obj->quan) {
2444. 		long oquan, uquan, thisused;
2445. 		unsigned save_unpaid;
2446. 
2447. 		save_unpaid = obj->unpaid;
2448. 		oquan = obj->quan;
2449. 		uquan = (bp->useup ? bp->bquan : bp->bquan - oquan);
2450. 		thisused = bp->price * uquan;
2451. 		totused += thisused;
2452. 		obj->quan = uquan;		/* cheat doname */
2453. 		obj->unpaid = 0;		/* ditto */
2454. 		/* Why 'x'?  To match `I x', more or less. */
2455. 		buf_p = xprname(obj, (char *)0, 'x', FALSE, thisused);
2456. 		obj->quan = oquan;		/* restore value */
2457. #ifdef __SASC
2458. 				/* SAS/C 6.2 can't cope for some reason */
2459. 		sasc_bug(obj,save_unpaid);
2460. #else
2461. 		obj->unpaid = save_unpaid;
2462. #endif
2463. 		putstr(datawin, 0, buf_p);
2464. 	    }
2465. 	}
2466. 	if (eshkp->debit) {
2467. 	    /* additional shop debt which has no itemization available */
2468. 	    if (totused) putstr(datawin, 0, "");
2469. 	    totused += eshkp->debit;
2470. 	    buf_p = xprname((struct obj *)0,
2471. 			    "usage charges and/or other fees",
2472. 			    GOLD_SYM, FALSE, eshkp->debit);
2473. 	    putstr(datawin, 0, buf_p);
2474. 	}
2475. 	buf_p = xprname((struct obj *)0, "Total:", '*', FALSE, totused);
2476. 	putstr(datawin, 0, "");
2477. 	putstr(datawin, 0, buf_p);
2478. 	display_nhwindow(datawin, FALSE);
2479.     quit:
2480. 	destroy_nhwindow(datawin);
2481. 	return(0);
2482. }
2483. 
2484. #define HUNGRY	2
2485. 
2486. STATIC_OVL long
2487. getprice(obj, shk_buying)
2488. register struct obj *obj;
2489. boolean shk_buying;
2490. {
2491. 	register long tmp = (long) objects[obj->otyp].oc_cost;
2492. 
2493. 	switch(obj->oclass) {
2494. 	case FOOD_CLASS:
2495. 		/* simpler hunger check, (2-4)*cost */
2496. 		if (u.uhs >= HUNGRY && !shk_buying) tmp *= (long) u.uhs;
2497. 		if (obj->oeaten) tmp = 0L;
2498. 		break;
2499. 	case WAND_CLASS:
2500. 		if (obj->spe == -1) tmp = 0L;
2501. 		break;
2502. 	case POTION_CLASS:
2503. 		if (obj->otyp == POT_WATER && !obj->blessed && !obj->cursed)
2504. 			tmp = 0L;
2505. 		break;
2506. 	case ARMOR_CLASS:
2507. 	case WEAPON_CLASS:
2508. 		if (obj->spe > 0) tmp += 10L * (long) obj->spe;
2509. 		break;
2510. 	case TOOL_CLASS:
2511. 		if (Is_candle(obj) &&
2512. 			obj->age < 20L * (long)objects[obj->otyp].oc_cost)
2513. 		    tmp /= 2L;
2514. 		break;
2515. 	}
2516. 	if (obj->oartifact) tmp *= 25L;
2517. 	return tmp;
2518. }
2519. 
2520. /* shk catches thrown pick-axe */
2521. int
2522. shkcatch(obj, x, y)
2523. register struct obj *obj;
2524. register xchar x, y;
2525. {
2526. 	register struct monst *shkp;
2527. 
2528. 	if (!(shkp = shop_keeper(inside_shop(x, y))) ||
2529. 	    !inhishop(shkp)) return(0);
2530. 
2531. 	if (shkp->mcanmove && !shkp->msleeping &&
2532. 	    (*u.ushops != ESHK(shkp)->shoproom || !inside_shop(u.ux, u.uy)) &&
2533. 	    dist2(shkp->mx, shkp->my, x, y) < 3 &&
2534. 	    /* if it is the shk's pos, you hit and anger him */
2535. 	    (shkp->mx != x || shkp->my != y)) {
2536. 		if (mnearto(shkp, x, y, TRUE))
2537. 		    verbalize("Out of my way, scum!");
2538. 		if (cansee(x, y)) {
2539. 		    pline("%s nimbly catches %s.",
2540. 			  Monnam(shkp), the(xname(obj)));
2541. 		    if (!canspotmon(shkp))
2542. 			map_invisible(x, y);
2543. 		    delay_output();
2544. 		    mark_synch();
2545. 		}
2546. 		mpickobj(shkp, obj);
2547. 		subfrombill(obj, shkp);
2548. 		return(1);
2549. 	}
2550. 	return(0);
2551. }
2552. 
2553. void
2554. add_damage(x, y, cost)
2555. register xchar x, y;
2556. long cost;
2557. {
2558. 	struct damage *tmp_dam;
2559. 	char *shops;
2560. 
2561. 	if (IS_DOOR(levl[x][y].typ)) {
2562. 	    struct monst *mtmp;
2563. 
2564. 	    /* Don't schedule for repair unless it's a real shop entrance */
2565. 	    for (shops = in_rooms(x, y, SHOPBASE); *shops; shops++)
2566. 		if ((mtmp = shop_keeper(*shops)) != 0 &&
2567. 			x == ESHK(mtmp)->shd.x && y == ESHK(mtmp)->shd.y)
2568. 		    break;
2569. 	    if (!*shops) return;
2570. 	}
2571. 	for (tmp_dam = level.damagelist; tmp_dam; tmp_dam = tmp_dam->next)
2572. 	    if (tmp_dam->place.x == x && tmp_dam->place.y == y) {
2573. 		tmp_dam->cost += cost;
2574. 		return;
2575. 	    }
2576. 	tmp_dam = (struct damage *)alloc((unsigned)sizeof(struct damage));
2577. 	tmp_dam->when = monstermoves;
2578. 	tmp_dam->place.x = x;
2579. 	tmp_dam->place.y = y;
2580. 	tmp_dam->cost = cost;
2581. 	tmp_dam->typ = levl[x][y].typ;
2582. 	tmp_dam->next = level.damagelist;
2583. 	level.damagelist = tmp_dam;
2584. 	/* If player saw damage, display as a wall forever */
2585. 	if (cansee(x, y))
2586. 	    levl[x][y].seenv = SVALL;
2587. }
2588. 
2589. #endif /*OVLB*/
2590. #ifdef OVL0
2591. 
2592. /*
2593.  * Do something about damage. Either (!croaked) try to repair it, or
2594.  * (croaked) just discard damage structs for non-shared locations, since
2595.  * they'll never get repaired. Assume that shared locations will get
2596.  * repaired eventually by the other shopkeeper(s). This might be an erroneous
2597.  * assumption (they might all be dead too), but we have no reasonable way of
2598.  * telling that.
2599.  */
2600. STATIC_OVL
2601. void
2602. remove_damage(shkp, croaked)
2603. register struct monst *shkp;
2604. register boolean croaked;
2605. {
2606. 	register struct damage *tmp_dam, *tmp2_dam;
2607. 	register boolean did_repair = FALSE, saw_door = FALSE;
2608. 	register boolean saw_floor = FALSE, stop_picking = FALSE;
2609. 	register boolean saw_untrap = FALSE;
2610. 	uchar saw_walls = 0;
2611. 
2612. 	tmp_dam = level.damagelist;
2613. 	tmp2_dam = 0;
2614. 	while (tmp_dam) {
2615. 	    register xchar x = tmp_dam->place.x, y = tmp_dam->place.y;
2616. 	    char shops[5];
2617. 	    int disposition;
2618. 
2619. 	    disposition = 0;
2620. 	    Strcpy(shops, in_rooms(x, y, SHOPBASE));
2621. 	    if (index(shops, ESHK(shkp)->shoproom)) {
2622. 		if (croaked)
2623. 		    disposition = (shops[1])? 0 : 1;
2624. 		else if (stop_picking)
2625. 		    disposition = repair_damage(shkp, tmp_dam, FALSE);
2626. 		else {
2627. 		    /* Defer the stop_occupation() until after repair msgs */
2628. 		    if (closed_door(x, y))
2629. 			stop_picking = picking_at(x, y);
2630. 		    disposition = repair_damage(shkp, tmp_dam, FALSE);
2631. 		    if (!disposition)
2632. 			stop_picking = FALSE;
2633. 		}
2634. 	    }
2635. 
2636. 	    if (!disposition) {
2637. 		tmp2_dam = tmp_dam;
2638. 		tmp_dam = tmp_dam->next;
2639. 		continue;
2640. 	    }
2641. 
2642. 	    if (disposition > 1) {
2643. 		did_repair = TRUE;
2644. 		if (cansee(x, y)) {
2645. 		    if (IS_WALL(levl[x][y].typ))
2646. 			saw_walls++;
2647. 		    else if (IS_DOOR(levl[x][y].typ))
2648. 			saw_door = TRUE;
2649. 		    else if (disposition == 3)		/* untrapped */
2650. 			saw_untrap = TRUE;
2651. 		    else
2652. 			saw_floor = TRUE;
2653. 		}
2654. 	    }
2655. 
2656. 	    tmp_dam = tmp_dam->next;
2657. 	    if (!tmp2_dam) {
2658. 		free((genericptr_t)level.damagelist);
2659. 		level.damagelist = tmp_dam;
2660. 	    } else {
2661. 		free((genericptr_t)tmp2_dam->next);
2662. 		tmp2_dam->next = tmp_dam;
2663. 	    }
2664. 	}
2665. 	if (!did_repair)
2666. 	    return;
2667. 	if (saw_walls) {
2668. 	    pline("Suddenly, %s section%s of wall close%s up!",
2669. 		  (saw_walls == 1) ? "a" : (saw_walls <= 3) ?
2670. 						  "some" : "several",
2671. 		  (saw_walls == 1) ? "" : "s", (saw_walls == 1) ? "s" : "");
2672. 	    if (saw_door)
2673. 		pline_The("shop door reappears!");
2674. 	    if (saw_floor)
2675. 		pline_The("floor is repaired!");
2676. 	} else {
2677. 	    if (saw_door)
2678. 		pline("Suddenly, the shop door reappears!");
2679. 	    else if (saw_floor)
2680. 		pline("Suddenly, the floor damage is gone!");
2681. 	    else if (saw_untrap)
2682. 	        pline("Suddenly, the trap is removed from the floor!");
2683. 	    else if (inside_shop(u.ux, u.uy) == ESHK(shkp)->shoproom)
2684. 		You_feel("more claustrophobic than before.");
2685. 	    else if (flags.soundok && !rn2(10))
2686. 		Norep("The dungeon acoustics noticeably change.");
2687. 	}
2688. 	if (stop_picking)
2689. 		stop_occupation();
2690. }
2691. 
2692. /*
2693.  * 0: repair postponed, 1: silent repair (no messages), 2: normal repair
2694.  * 3: untrap
2695.  */
2696. int
2697. repair_damage(shkp, tmp_dam, catchup)
2698. register struct monst *shkp;
2699. register struct damage *tmp_dam;
2700. boolean catchup;	/* restoring a level */
2701. {
2702. 	register xchar x, y, i;
2703. 	xchar litter[9];
2704. 	register struct monst *mtmp;
2705. 	register struct obj *otmp;
2706. 	register struct trap *ttmp;
2707. 
2708. 	if ((monstermoves - tmp_dam->when) < REPAIR_DELAY)
2709. 	    return(0);
2710. 	if (shkp->msleeping || !shkp->mcanmove || ESHK(shkp)->following)
2711. 	    return(0);
2712. 	x = tmp_dam->place.x;
2713. 	y = tmp_dam->place.y;
2714. 	if (!IS_ROOM(tmp_dam->typ)) {
2715. 	    if (x == u.ux && y == u.uy)
2716. 		if (!Passes_walls)
2717. 		    return(0);
2718. 	    if (x == shkp->mx && y == shkp->my)
2719. 		return(0);
2720. 	    if ((mtmp = m_at(x, y)) && (!passes_walls(mtmp->data)))
2721. 		return(0);
2722. 	}
2723. 	if ((ttmp = t_at(x, y)) != 0) {
2724. 	    if (x == u.ux && y == u.uy)
2725. 		if (!Passes_walls)
2726. 		    return(0);
2727. 	    if (ttmp->ttyp == LANDMINE || ttmp->ttyp == BEAR_TRAP) {
2728. 		/* convert to an object */
2729. 		otmp = mksobj((ttmp->ttyp == LANDMINE) ? LAND_MINE :
2730. 				BEARTRAP, TRUE, FALSE);
2731. 		otmp->quan= 1;
2732. 		otmp->owt = weight(otmp);
2733. 		mpickobj(shkp, otmp);
2734. 	    }
2735. 	    deltrap(ttmp);
2736. 	    newsym(x, y);
2737. 	    return(3);
2738. 	}
2739. 	if (IS_ROOM(tmp_dam->typ)) {
2740. 	    /* No messages if player already filled trapdoor */
2741. 	    if (catchup || !ttmp)
2742. 		return(1);
2743. 	    newsym(x, y);
2744. 	    return(2);
2745. 	}
2746. 	if (!ttmp && (tmp_dam->typ == levl[x][y].typ) &&
2747. 	    (!IS_DOOR(tmp_dam->typ) || (levl[x][y].doormask > D_BROKEN)))
2748. 	    /* No messages if player already replaced shop door */
2749. 	    return(1);
2750. 	levl[x][y].typ = tmp_dam->typ;
2751. 	(void) memset((genericptr_t)litter, 0, sizeof(litter));
2752. 	if ((otmp = level.objects[x][y]) != 0) {
2753. 	    /* Scatter objects haphazardly into the shop */
2754. #define NEED_UPDATE 1
2755. #define OPEN	    2
2756. #define INSHOP	    4
2757. #define horiz(i) ((i%3)-1)
2758. #define vert(i)  ((i/3)-1)
2759. 	    for (i = 0; i < 9; i++) {
2760. 		if ((i == 4) || (!ZAP_POS(levl[x+horiz(i)][y+vert(i)].typ)))
2761. 		    continue;
2762. 		litter[i] = OPEN;
2763. 		if (inside_shop(x+horiz(i),
2764. 				y+vert(i)) == ESHK(shkp)->shoproom)
2765. 		    litter[i] |= INSHOP;
2766. 	    }
2767. 	    if (Punished && !u.uswallow &&
2768. 				((uchain->ox == x && uchain->oy == y) ||
2769. 				 (uball->ox == x && uball->oy == y))) {
2770. 		/*
2771. 		 * Either the ball or chain is in the repair location.
2772. 		 *
2773. 		 * Take the easy way out and put ball&chain under hero.
2774. 		 */
2775. 		verbalize("Get your junk out of my wall!");
2776. 		unplacebc();	/* pick 'em up */
2777. 		placebc();	/* put 'em down */
2778. 	    }
2779. 	    while ((otmp = level.objects[x][y]) != 0)
2780. 		/* Don't mess w/ boulders -- just merge into wall */
2781. 		if ((otmp->otyp == BOULDER) || (otmp->otyp == ROCK)) {
2782. 		    obj_extract_self(otmp);
2783. 		    obfree(otmp, (struct obj *)0);
2784. 		} else {
2785. 		    while (!(litter[i = rn2(9)] & INSHOP));
2786. 			remove_object(otmp);
2787. 			place_object(otmp, x+horiz(i), y+vert(i));
2788. 			litter[i] |= NEED_UPDATE;
2789. 		}
2790. 	}
2791. 	if (catchup) return 1;	/* repair occurred while off level */
2792. 
2793. 	block_point(x, y);
2794. 	if(IS_DOOR(tmp_dam->typ)) {
2795. 	    levl[x][y].doormask = D_CLOSED; /* arbitrary */
2796. 	    newsym(x, y);
2797. 	} else {
2798. 	    /* don't set doormask  - it is (hopefully) the same as it was */
2799. 	    /* if not, perhaps save it with the damage array...  */
2800. 
2801. 	    if (IS_WALL(tmp_dam->typ) && cansee(x, y)) {
2802. 	    /* Player sees actual repair process, so they KNOW it's a wall */
2803. 		levl[x][y].seenv = SVALL;
2804. 		newsym(x, y);
2805. 	    }
2806. 	    /* Mark this wall as "repaired".  There currently is no code */
2807. 	    /* to do anything about repaired walls, so don't do it.	 */
2808. 	}
2809. 	for (i = 0; i < 9; i++)
2810. 	    if (litter[i] & NEED_UPDATE)
2811. 		newsym(x+horiz(i), y+vert(i));
2812. 	return(2);
2813. #undef NEED_UPDATE
2814. #undef OPEN
2815. #undef INSHOP
2816. #undef vert
2817. #undef horiz
2818. }
2819. #endif /*OVL0*/
2820. #ifdef OVL3
2821. /*
2822.  * shk_move: return 1: moved  0: didn't  -1: let m_move do it  -2: died
2823.  */
2824. int
2825. shk_move(shkp)
2826. register struct monst *shkp;
2827. {
2828. 	register xchar gx,gy,omx,omy;
2829. 	register int udist;
2830. 	register schar appr;
2831. 	register struct eshk *eshkp = ESHK(shkp);
2832. 	int z;
2833. 	boolean uondoor = FALSE, satdoor, avoid = FALSE, badinv;
2834. 
2835. 	omx = shkp->mx;
2836. 	omy = shkp->my;
2837. 
2838. 	if (inhishop(shkp))
2839. 	    remove_damage(shkp, FALSE);
2840. 
2841. 	if((udist = distu(omx,omy)) < 3 &&
2842. 	   (shkp->data != &mons[PM_GRID_BUG] || (omx==u.ux || omy==u.uy))) {
2843. 		if(ANGRY(shkp) ||
2844. 		   (Conflict && !resist(shkp, RING_CLASS, 0, 0))) {
2845. 			if(Displaced)
2846. 			  Your("displaced image doesn't fool %s!",
2847. 				mon_nam(shkp));
2848. 			(void) mattacku(shkp);
2849. 			return(0);
2850. 		}
2851. 		if(eshkp->following) {
2852. 			if(strncmp(eshkp->customer, plname, PL_NSIZ)) {
2853. 			    verbalize("%s, %s!  I was looking for %s.",
2854. 				    Hello(), plname, eshkp->customer);
2855. 				    eshkp->following = 0;
2856. 			    return(0);
2857. 			}
2858. 			if(moves > followmsg+4) {
2859. 			    verbalize("%s, %s!  Didn't you forget to pay?",
2860. 				    Hello(), plname);
2861. 			    followmsg = moves;
2862. 			    if (!rn2(9)) {
2863. 			      pline("%s doesn't like customers who don't pay.",
2864. 				    Monnam(shkp));
2865. 				rile_shk(shkp);
2866. 			    }
2867. 			}
2868. 			if(udist < 2)
2869. 			    return(0);
2870. 		}
2871. 	}
2872. 
2873. 	appr = 1;
2874. 	gx = eshkp->shk.x;
2875. 	gy = eshkp->shk.y;
2876. 	satdoor = (gx == omx && gy == omy);
2877. 	if(eshkp->following || ((z = holetime()) >= 0 && z*z <= udist)){
2878. 		if(udist > 4)
2879. 		    return(-1);	/* leave it to m_move */
2880. 		gx = u.ux;
2881. 		gy = u.uy;
2882. 	} else if(ANGRY(shkp)) {
2883. 		/* Move towards the hero if the shopkeeper can see him. */
2884. 		if(shkp->mcansee && m_canseeu(shkp)) {
2885. 			gx = u.ux;
2886. 			gy = u.uy;
2887. 		}
2888. 		avoid = FALSE;
2889. 	} else {
2890. #define	GDIST(x,y)	(dist2(x,y,gx,gy))
2891. 		if(Invis) {
2892. 		    avoid = FALSE;
2893. 		} else {
2894. 		    uondoor = (u.ux == eshkp->shd.x && u.uy == eshkp->shd.y);
2895. 		    if(uondoor) {
2896. 			badinv = (carrying(PICK_AXE) || carrying(DWARVISH_MATTOCK) ||
2897. 				  (Fast && (sobj_at(PICK_AXE, u.ux, u.uy) ||
2898. 				  sobj_at(DWARVISH_MATTOCK, u.ux, u.uy))));
2899. 			if(satdoor && badinv)
2900. 			    return(0);
2901. 			avoid = !badinv;
2902. 		    } else {
2903. 			avoid = (*u.ushops && distu(gx,gy) > 8);
2904. 			badinv = FALSE;
2905. 		    }
2906. 
2907. 		    if(((!eshkp->robbed && !eshkp->billct && !eshkp->debit)
2908. 			|| avoid) && GDIST(omx,omy) < 3) {
2909. 			if (!badinv && !onlineu(omx,omy))
2910. 			    return(0);
2911. 			if(satdoor)
2912. 			    appr = gx = gy = 0;
2913. 		    }
2914. 		}
2915. 	}
2916. 
2917. 	return(move_special(shkp,inhishop(shkp),
2918. 			    appr,uondoor,avoid,omx,omy,gx,gy));
2919. }
2920. 
2921. #endif /*OVL3*/
2922. #ifdef OVLB
2923. 
2924. /* for use in levl_follower (mondata.c) */
2925. boolean
2926. is_fshk(mtmp)
2927. register struct monst *mtmp;
2928. {
2929. 	return((boolean)(mtmp->isshk && ESHK(mtmp)->following));
2930. }
2931. 
2932. /* You are digging in the shop. */
2933. void
2934. shopdig(fall)
2935. register int fall;
2936. {
2937.     register struct monst *shkp = shop_keeper(*u.ushops);
2938. 
2939.     if(!shkp) return;
2940. 
2941.     if(!inhishop(shkp)) {
2942. 	if (Role_if(PM_KNIGHT)) {
2943. 	    You_feel("like a common thief.");
2944. 	    adjalign(-sgn(u.ualign.type));
2945. 	}
2946. 	return;
2947.     }
2948. 
2949.     if(!fall) {
2950. 	if(u.utraptype == TT_PIT)
2951. 	    verbalize("Be careful, %s, or you might fall through the floor.",
2952. 		flags.female ? "madam" : "sir");
2953. 	else
2954. 	    verbalize("%s, do not damage the floor here!",
2955. 			flags.female ? "Madam" : "Sir");
2956. 	if (Role_if(PM_KNIGHT)) {
2957. 	    You_feel("like a common thief.");
2958. 	    adjalign(-sgn(u.ualign.type));
2959. 	}
2960.     } else if(!um_dist(shkp->mx, shkp->my, 5) &&
2961. 		!shkp->msleeping && shkp->mcanmove &&
2962. 		(ESHK(shkp)->billct || ESHK(shkp)->debit)) {
2963. 	    register struct obj *obj, *obj2;
2964. 
2965. 	    if (distu(shkp->mx, shkp->my) > 2) {
2966. 		mnexto(shkp);
2967. 		/* for some reason the shopkeeper can't come next to you */
2968. 		if (distu(shkp->mx, shkp->my) > 2) {
2969. 		    pline("%s curses you in anger and frustration!",
2970. 					shkname(shkp));
2971. 		    rile_shk(shkp);
2972. 		    return;
2973. 		} else pline("%s leaps, and grabs your backpack!",
2974. 					shkname(shkp));
2975. 	    } else pline("%s grabs your backpack!", shkname(shkp));
2976. 
2977. 	    for(obj = invent; obj; obj = obj2) {
2978. 		obj2 = obj->nobj;
2979. 		if(obj->owornmask) continue;
2980. 		if(obj->otyp == LEASH && obj->leashmon) continue;
2981. 		freeinv(obj);
2982. 		add_to_minv(shkp, obj);
2983. 		subfrombill(obj, shkp);
2984. 	    }
2985.     }
2986. }
2987. 
2988. #ifdef KOPS
2989. STATIC_OVL void
2990. makekops(mm)
2991. coord *mm;
2992. {
2993. 	static const short k_mndx[4] = {
2994. 	    PM_KEYSTONE_KOP, PM_KOP_SERGEANT, PM_KOP_LIEUTENANT, PM_KOP_KAPTAIN
2995. 	};
2996. 	int k_cnt[4], cnt, mndx, k;
2997. 
2998. 	k_cnt[0] = cnt = abs(depth(&u.uz)) + rnd(5);
2999. 	k_cnt[1] = (cnt / 3) + 1;	/* at least one sarge */
3000. 	k_cnt[2] = (cnt / 6);		/* maybe a lieutenant */
3001. 	k_cnt[3] = (cnt / 9);		/* and maybe a kaptain */
3002. 
3003. 	for (k = 0; k < 4; k++) {
3004. 	    if ((cnt = k_cnt[k]) == 0) break;
3005. 	    mndx = k_mndx[k];
3006. 	    if (mvitals[mndx].mvflags & G_GONE) continue;
3007. 
3008. 	    while (cnt--)
3009. 		if (enexto(mm, mm->x, mm->y, &mons[mndx]))
3010. 		    (void) makemon(&mons[mndx], mm->x, mm->y, NO_MM_FLAGS);
3011. 	}
3012. }
3013. #endif	/* KOPS */
3014. 
3015. void
3016. pay_for_damage(dmgstr)
3017. const char *dmgstr;
3018. {
3019. 	register struct monst *shkp = (struct monst *)0;
3020. 	char shops_affected[5];
3021. 	register boolean uinshp = (*u.ushops != '\0');
3022. 	char qbuf[80];
3023. 	register xchar x, y;
3024. 	boolean dugwall = !strcmp(dmgstr, "dig into") ||	/* wand */
3025. 			  !strcmp(dmgstr, "damage");		/* pick-axe */
3026. 	struct damage *tmp_dam, *appear_here = 0;
3027. 	/* any number >= (80*80)+(24*24) would do, actually */
3028. 	long cost_of_damage = 0L;
3029. 	unsigned int nearest_shk = 7000, nearest_damage = 7000;
3030. 	int picks = 0;
3031. 
3032. 	for (tmp_dam = level.damagelist;
3033. 	     (tmp_dam && (tmp_dam->when == monstermoves));
3034. 	     tmp_dam = tmp_dam->next) {
3035. 	    char *shp;
3036. 
3037. 	    if (!tmp_dam->cost)
3038. 		continue;
3039. 	    cost_of_damage += tmp_dam->cost;
3040. 	    Strcpy(shops_affected,
3041. 		   in_rooms(tmp_dam->place.x, tmp_dam->place.y, SHOPBASE));
3042. 	    for (shp = shops_affected; *shp; shp++) {
3043. 		struct monst *tmp_shk;
3044. 		unsigned int shk_distance;
3045. 
3046. 		if (!(tmp_shk = shop_keeper(*shp)))
3047. 		    continue;
3048. 		if (tmp_shk == shkp) {
3049. 		    unsigned int damage_distance =
3050. 				   distu(tmp_dam->place.x, tmp_dam->place.y);
3051. 
3052. 		    if (damage_distance < nearest_damage) {
3053. 			nearest_damage = damage_distance;
3054. 			appear_here = tmp_dam;
3055. 		    }
3056. 		    continue;
3057. 		}
3058. 		if (!inhishop(tmp_shk))
3059. 		    continue;
3060. 		shk_distance = distu(tmp_shk->mx, tmp_shk->my);
3061. 		if (shk_distance > nearest_shk)
3062. 		    continue;
3063. 		if ((shk_distance == nearest_shk) && picks) {
3064. 		    if (rn2(++picks))
3065. 			continue;
3066. 		} else
3067. 		    picks = 1;
3068. 		shkp = tmp_shk;
3069. 		nearest_shk = shk_distance;
3070. 		appear_here = tmp_dam;
3071. 		nearest_damage = distu(tmp_dam->place.x, tmp_dam->place.y);
3072. 	    }
3073. 	}
3074. 
3075. 	if (!cost_of_damage || !shkp)
3076. 	    return;
3077. 
3078. 	x = appear_here->place.x;
3079. 	y = appear_here->place.y;
3080. 
3081. 	/* not the best introduction to the shk... */
3082. 	(void) strncpy(ESHK(shkp)->customer,plname,PL_NSIZ);
3083. 
3084. 	/* if the shk is already on the war path, be sure it's all out */
3085. 	if(ANGRY(shkp) || ESHK(shkp)->following) {
3086. 		hot_pursuit(shkp);
3087. 		return;
3088. 	}
3089. 
3090. 	/* if the shk is not in their shop.. */
3091. 	if(!*in_rooms(shkp->mx,shkp->my,SHOPBASE)) {
3092. 		if(!cansee(shkp->mx, shkp->my))
3093. 			return;
3094. 		goto getcad;
3095. 	}
3096. 
3097. 	if(uinshp) {
3098. 		if(um_dist(shkp->mx, shkp->my, 1) &&
3099. 			!um_dist(shkp->mx, shkp->my, 3)) {
3100. 		    pline("%s leaps towards you!", shkname(shkp));
3101. 		    mnexto(shkp);
3102. 		}
3103. 		if(um_dist(shkp->mx, shkp->my, 1)) goto getcad;
3104. 	} else {
3105. 	    /*
3106. 	     * Make shkp show up at the door.  Effect:  If there is a monster
3107. 	     * in the doorway, have the hero hear the shopkeeper yell a bit,
3108. 	     * pause, then have the shopkeeper appear at the door, having
3109. 	     * yanked the hapless critter out of the way.
3110. 	     */
3111. 	    if (MON_AT(x, y)) {
3112. 		if(flags.soundok) {
3113. 		    You_hear("an angry voice:");
3114. 		    verbalize("Out of my way, scum!");
3115. 		    wait_synch();
3116. #if defined(UNIX) || defined(VMS)
3117. # if defined(SYSV) || defined(ULTRIX) || defined(VMS)
3118. 		    (void)
3119. # endif
3120. 			sleep(1);
3121. #endif
3122. 		}
3123. 	    }
3124. 	    (void) mnearto(shkp, x, y, TRUE);
3125. 	}
3126. 
3127. 	if((um_dist(x, y, 1) && !uinshp) ||
3128. 			(u.ugold + ESHK(shkp)->credit) < cost_of_damage
3129. 				|| !rn2(50)) {
3130. 		if(um_dist(x, y, 1) && !uinshp) {
3131. 		    pline("%s shouts:", shkname(shkp));
3132. 		    verbalize("Who dared %s my %s?", dmgstr,
3133. 					 dugwall ? "shop" : "door");
3134. 		} else {
3135. getcad:
3136. 		    verbalize("How dare you %s my %s?", dmgstr,
3137. 					 dugwall ? "shop" : "door");
3138. 		}
3139. 		hot_pursuit(shkp);
3140. 		return;
3141. 	}
3142. 
3143. 	if(Invis) Your("invisibility does not fool %s!", shkname(shkp));
3144. 	Sprintf(qbuf,"\"Cad!  You did %ld zorkmids worth of damage!\"  Pay? ",
3145. 		 cost_of_damage);
3146. 	if(yn(qbuf) != 'n') {
3147. 		cost_of_damage = check_credit(cost_of_damage, shkp);
3148. 		u.ugold -= cost_of_damage;
3149. 		shkp->mgold += cost_of_damage;
3150. 		flags.botl = 1;
3151. 		pline("Mollified, %s accepts your restitution.",
3152. 			shkname(shkp));
3153. 		/* move shk back to his home loc */
3154. 		home_shk(shkp, FALSE);
3155. 		pacify_shk(shkp);
3156. 	} else {
3157. 		verbalize("Oh, yes!  You'll pay!");
3158. 		hot_pursuit(shkp);
3159. 		adjalign(-sgn(u.ualign.type));
3160. 	}
3161. }
3162. #endif /*OVLB*/
3163. #ifdef OVL0
3164. /* called in dokick.c when we kick an object that might be in a store */
3165. boolean
3166. costly_spot(x, y)
3167. register xchar x, y;
3168. {
3169. 	register struct monst *shkp;
3170. 
3171. 	if (!level.flags.has_shop) return FALSE;
3172. 	shkp = shop_keeper(*in_rooms(x, y, SHOPBASE));
3173. 	if(!shkp || !inhishop(shkp)) return(FALSE);
3174. 
3175. 	return((boolean)(inside_shop(x, y) &&
3176. 		!(x == ESHK(shkp)->shk.x &&
3177. 			y == ESHK(shkp)->shk.y)));
3178. }
3179. #endif /*OVL0*/
3180. #ifdef OVLB
3181. 
3182. /* called by dotalk(sounds.c) when #chatting; returns obj if location
3183.    contains shop goods and shopkeeper is willing & able to speak */
3184. struct obj *
3185. shop_object(x, y)
3186. register xchar x, y;
3187. {
3188.     register struct obj *otmp;
3189.     register struct monst *shkp;
3190. 
3191.     if(!(shkp = shop_keeper(*in_rooms(x, y, SHOPBASE))) || !inhishop(shkp))
3192. 	return(struct obj *)0;
3193. 
3194.     for (otmp = level.objects[x][y]; otmp; otmp = otmp->nexthere)
3195. 	if (otmp->oclass != GOLD_CLASS)
3196. 	    break;
3197.     /* note: otmp might have ->no_charge set, but that's ok */
3198.     return (otmp && costly_spot(x, y) && NOTANGRY(shkp)
3199. 	    && shkp->mcanmove && !shkp->msleeping)
3200. 		? otmp : (struct obj *)0;
3201. }
3202. 
3203. /* give price quotes for all objects linked to this one (ie, on this spot) */
3204. void
3205. price_quote(first_obj)
3206. register struct obj *first_obj;
3207. {
3208.     register struct obj *otmp;
3209.     char buf[BUFSZ], price[40];
3210.     long cost;
3211.     int cnt = 0;
3212.     winid tmpwin;
3213.     struct monst *shkp = shop_keeper(inside_shop(u.ux, u.uy));
3214. 
3215.     tmpwin = create_nhwindow(NHW_MENU);
3216.     putstr(tmpwin, 0, "Fine goods for sale:");
3217.     putstr(tmpwin, 0, "");
3218.     for (otmp = first_obj; otmp; otmp = otmp->nexthere) {
3219. 	if (otmp->oclass == GOLD_CLASS) continue;
3220. 	cost = (otmp->no_charge || otmp == uball || otmp == uchain) ? 0L :
3221. 		get_cost(otmp, (struct monst *)0);
3222. 	if (Has_contents(otmp))
3223. 	    cost += contained_cost(otmp, shkp, 0L, FALSE);
3224. 	if (!cost) {
3225. 	    Strcpy(price, "no charge");
3226. 	} else {
3227. 	    Sprintf(price, "%ld zorkmid%s%s", cost, plur(cost),
3228. 		    otmp->quan > 1L ? " each" : "");
3229. 	}
3230. 	Sprintf(buf, "%s, %s", doname(otmp), price);
3231. 	putstr(tmpwin, 0, buf),  cnt++;
3232.     }
3233.     if (cnt > 1) {
3234. 	display_nhwindow(tmpwin, TRUE);
3235.     } else if (cnt == 1) {
3236. 	if (first_obj->no_charge || first_obj == uball || first_obj == uchain){
3237. 	    pline("%s!", buf);	/* buf still contains the string */
3238. 	} else {
3239. 	    /* print cost in slightly different format, so can't reuse buf */
3240. 	    cost = get_cost(first_obj, (struct monst *)0);
3241. 	    if (Has_contents(first_obj))
3242. 		cost += contained_cost(first_obj, shkp, 0L, FALSE);
3243. 	    pline("%s, price %ld zorkmid%s%s%s", doname(first_obj),
3244. 		cost, plur(cost), first_obj->quan > 1L ? " each" : "",
3245. 		shk_embellish(first_obj, cost));
3246. 	}
3247.     }
3248.     destroy_nhwindow(tmpwin);
3249. }
3250. #endif /*OVLB*/
3251. #ifdef OVL3
3252. 
3253. STATIC_OVL const char *
3254. shk_embellish(itm, cost)
3255. register struct obj *itm;
3256. long cost;
3257. {
3258.     if (!rn2(3)) {
3259. 	register int o, choice = rn2(5);
3260. 	if (choice == 0) choice = (cost < 100L ? 1 : cost < 500L ? 2 : 3);
3261. 	switch (choice) {
3262. 	    case 4:
3263. 		if (cost < 10L) break; else o = itm->oclass;
3264. 		if (o == FOOD_CLASS) return ", gourmets' delight!";
3265. 		if (objects[itm->otyp].oc_name_known
3266. 		    ? objects[itm->otyp].oc_magic
3267. 		    : (o == AMULET_CLASS || o == RING_CLASS   ||
3268. 		       o == WAND_CLASS   || o == POTION_CLASS ||
3269. 		       o == SCROLL_CLASS || o == SPBOOK_CLASS))
3270. 		    return ", painstakingly developed!";
3271. 		return ", superb craftsmanship!";
3272. 	    case 3: return ", finest quality.";
3273. 	    case 2: return ", an excellent choice.";
3274. 	    case 1: return ", a real bargain.";
3275. 	   default: break;
3276. 	}
3277.     } else if (itm->oartifact) {
3278. 	return ", one of a kind!";
3279.     }
3280.     return ".";
3281. }
3282. #endif /*OVL3*/
3283. #ifdef OVLB
3284. 
3285. /* First 4 supplied by Ronen and Tamar, remainder by development team */
3286. const char *Izchak_speaks[]={
3287.     "%s says: 'These shopping malls give me a headache.'",
3288.     "%s says: 'Slow down.  Think clearly.'",
3289.     "%s says: 'You need to take things one at a time.'",
3290.     "%s says: 'I don't like poofy coffee... give me Columbian Supremo.'",
3291.     "%s says that getting the devteam's agreement on anything is difficult.",
3292.     "%s says that he has noticed those who serve their deity will prosper.",
3293.     "%s says: 'Don't try to steal from me - I have friends in high places!'",
3294.     "%s says: 'You may well need something from this shop in the future.'",
3295.     "%s comments about the Valley of the Dead as being a gateway."
3296. };
3297. 
3298. void
3299. shk_chat(shkp)
3300. register struct monst *shkp;
3301. {
3302. 	register struct eshk *eshk = ESHK(shkp);
3303. 
3304. 	if (ANGRY(shkp))
3305. 		pline("%s mentions how much %s dislikes %s customers.",
3306. 			shkname(shkp), he[shkp->female],
3307. 			eshk->robbed ? "non-paying" : "rude");
3308. 	else if (eshk->following) {
3309. 		if (strncmp(eshk->customer, plname, PL_NSIZ)) {
3310. 		    verbalize("%s %s!  I was looking for %s.",
3311. 			    Hello(), plname, eshk->customer);
3312. 		    eshk->following = 0;
3313. 		} else {
3314. 		    verbalize("%s %s!  Didn't you forget to pay?", Hello(), plname);
3315. 		}
3316. 	} else if (eshk->billct) {
3317. 		register long total = addupbill(shkp) + eshk->debit;
3318. 		pline("%s says that your bill comes to %ld zorkmid%s.",
3319. 		      shkname(shkp), total, plur(total));
3320. 	} else if (eshk->debit)
3321. 		pline("%s reminds you that you owe %s %ld zorkmid%s.",
3322. 		      shkname(shkp), him[shkp->female],
3323. 		      eshk->debit, plur(eshk->debit));
3324. 	else if (eshk->credit)
3325. 		pline("%s encourages you to use your %ld zorkmid%s of credit.",
3326. 		      shkname(shkp), eshk->credit, plur(eshk->credit));
3327. 	else if (eshk->robbed)
3328. 		pline("%s complains about a recent robbery.", shkname(shkp));
3329. 	else if (shkp->mgold < 50)
3330. 		pline("%s complains that business is bad.", shkname(shkp));
3331. 	else if (shkp->mgold > 4000)
3332. 		pline("%s says that business is good.", shkname(shkp));
3333. 	else if (strcmp(shkname(shkp), "Izchak") == 0)
3334. 		pline(Izchak_speaks[rn2(SIZE(Izchak_speaks))],shkname(shkp));
3335. 	else
3336. 		pline("%s talks about the problem of shoplifters.",shkname(shkp));
3337. }
3338. 
3339. #ifdef KOPS
3340. STATIC_OVL void
3341. kops_gone(silent)
3342. register boolean silent;
3343. {
3344. 	register int cnt = 0;
3345. 	register struct monst *mtmp, *mtmp2;
3346. 
3347. 	for (mtmp = fmon; mtmp; mtmp = mtmp2) {
3348. 	    mtmp2 = mtmp->nmon;
3349. 	    if (mtmp->data->mlet == S_KOP) {
3350. 		if (canspotmon(mtmp)) cnt++;
3351. 		mongone(mtmp);
3352. 	    }
3353. 	}
3354. 	if (cnt && !silent)
3355. 	    pline_The("Kop%s (disappointed) vanish%s into thin air.",
3356. 		      plur(cnt), cnt == 1 ? "es" : "");
3357. }
3358. #endif	/* KOPS */
3359. 
3360. #endif /*OVLB*/
3361. #ifdef OVL3
3362. 
3363. STATIC_OVL long
3364. cost_per_charge(shkp, otmp, altusage)
3365. struct monst *shkp;
3366. struct obj *otmp;
3367. boolean altusage; /* some items have an "alternate" use with different cost */
3368. {
3369. 	long tmp = 0L;
3370. 
3371. 	if(!shkp || !inhishop(shkp)) return(0L); /* insurance */
3372. 	tmp = get_cost(otmp, shkp);
3373. 
3374. 	/* The idea is to make the exhaustive use of */
3375. 	/* an unpaid item more expensive than buying */
3376. 	/* it outright.				     */
3377. 	if(otmp->otyp == MAGIC_LAMP) {			 /* 1 */
3378. 		/* normal use (ie, as light source) of a magic lamp never
3379. 		   degrades its value, but not charging anything would make
3380. 		   identifcation too easy; charge an amount comparable to
3381. 		   what is charged for an ordinary lamp (don't bother with
3382. 		   angry shk surchage) */
3383. 		if (!altusage) tmp = (long) objects[OIL_LAMP].oc_cost;
3384. 		else tmp += tmp / 3L;	/* djinni is being released */
3385. 	} else if(otmp->otyp == MAGIC_MARKER) {		 /* 70 - 100 */
3386. 		/* no way to determine in advance   */
3387. 		/* how many charges will be wasted. */
3388. 		/* so, arbitrarily, one half of the */
3389. 		/* price per use.		    */
3390. 		tmp /= 2L;
3391. 	} else if(otmp->otyp == BAG_OF_TRICKS ||	 /* 1 - 20 */
3392. 		  otmp->otyp == HORN_OF_PLENTY) {
3393. 		tmp /= 5L;
3394. 	} else if(otmp->otyp == CRYSTAL_BALL ||		 /* 1 - 5 */
3395. 		  otmp->otyp == OIL_LAMP ||		 /* 1 - 10 */
3396. 		  otmp->otyp == BRASS_LANTERN ||
3397. 		 (otmp->otyp >= MAGIC_FLUTE &&
3398. 		  otmp->otyp <= DRUM_OF_EARTHQUAKE) ||	 /* 5 - 9 */
3399. 		  otmp->oclass == WAND_CLASS) {		 /* 3 - 11 */
3400. 		    if (otmp->spe > 1) tmp /= 4L;
3401. 	} else if (otmp->oclass == SPBOOK_CLASS) {
3402. 		    tmp -= tmp / 5L;
3403. 	} else if (otmp->otyp == CAN_OF_GREASE) {
3404. 		    tmp /= 10L;
3405. 	} else if (otmp->otyp == POT_OIL) {
3406. 		    tmp /= 5L;
3407. 	}
3408. 	return(tmp);
3409. }
3410. #endif /*OVL3*/
3411. #ifdef OVLB
3412. 
3413. /* for using charges of unpaid objects */
3414. void
3415. check_unpaid_usage(otmp, altusage)
3416. struct obj *otmp;
3417. boolean altusage;
3418. {
3419. 	struct monst *shkp;
3420. 	const char *fmt, *arg1, *arg2;
3421. 	long tmp;
3422. 
3423. 	if (!otmp->unpaid || !*u.ushops ||
3424. 		(otmp->spe <= 0 && objects[otmp->otyp].oc_charged))
3425. 	    return;
3426. 	if (!(shkp = shop_keeper(*u.ushops)) || !inhishop(shkp))
3427. 	    return;
3428. 	if ((tmp = cost_per_charge(shkp, otmp, altusage)) == 0L)
3429. 	    return;
3430. 
3431. 	arg1 = arg2 = "";
3432. 	if (otmp->oclass == SPBOOK_CLASS) {
3433. 	    fmt = "%sYou owe%s %ld zorkmids.";
3434. 	    arg1 = rn2(2) ? "This is no free library, cad!  " : "";
3435. 	    arg2 = ESHK(shkp)->debit > 0L ? " an additional" : "";
3436. 	} else if (otmp->otyp == POT_OIL) {
3437. 	    fmt = "%s%sThat will cost you %ld zorkmids (Yendorian Fuel Tax).";
3438. 	} else {
3439. 	    fmt = "%s%sUsage fee, %ld zorkmids.";
3440. 	    if (!rn2(3)) arg1 = "Hey!  ";
3441. 	    if (!rn2(3)) arg2 = "Ahem.  ";
3442. 	}
3443. 
3444. 	if (shkp->mcanmove || !shkp->msleeping)
3445. 	    verbalize(fmt, arg1, arg2, tmp);
3446. 	ESHK(shkp)->debit += tmp;
3447. 	exercise(A_WIS, TRUE);		/* you just got info */
3448. }
3449. 
3450. /* for using charges of unpaid objects "used in the normal manner" */
3451. void
3452. check_unpaid(otmp)
3453. struct obj *otmp;
3454. {
3455. 	check_unpaid_usage(otmp, FALSE);		/* normal item use */
3456. }
3457. 
3458. void
3459. costly_gold(x, y, amount)
3460. register xchar x, y;
3461. register long amount;
3462. {
3463. 	register long delta;
3464. 	register struct monst *shkp;
3465. 	register struct eshk *eshkp;
3466. 
3467. 	if(!costly_spot(x, y)) return;
3468. 	/* shkp now guaranteed to exist by costly_spot() */
3469. 	shkp = shop_keeper(*in_rooms(x, y, SHOPBASE));
3470. 
3471. 	eshkp = ESHK(shkp);
3472. 	if(eshkp->credit >= amount) {
3473. 	    if(eshkp->credit > amount)
3474. 		Your("credit is reduced by %ld zorkmid%s.",
3475. 					amount, plur(amount));
3476. 	    else Your("credit is erased.");
3477. 	    eshkp->credit -= amount;
3478. 	} else {
3479. 	    delta = amount - eshkp->credit;
3480. 	    if(eshkp->credit)
3481. 		Your("credit is erased.");
3482. 	    if(eshkp->debit)
3483. 		Your("debt increases by %ld zorkmid%s.",
3484. 					delta, plur(delta));
3485. 	    else You("owe %s %ld zorkmid%s.",
3486. 				shkname(shkp), delta, plur(delta));
3487. 	    eshkp->debit += delta;
3488. 	    eshkp->loan += delta;
3489. 	    eshkp->credit = 0L;
3490. 	}
3491. }
3492. 
3493. /* used in domove to block diagonal shop-exit */
3494. /* x,y should always be a door */
3495. boolean
3496. block_door(x,y)
3497. register xchar x, y;
3498. {
3499. 	register int roomno = *in_rooms(x, y, SHOPBASE);
3500. 	register struct monst *shkp;
3501. 
3502. 	if(roomno < 0 || !IS_SHOP(roomno)) return(FALSE);
3503. 	if(!IS_DOOR(levl[x][y].typ)) return(FALSE);
3504. 	if(roomno != *u.ushops) return(FALSE);
3505. 
3506. 	if(!(shkp = shop_keeper((char)roomno)) || !inhishop(shkp))
3507. 		return(FALSE);
3508. 
3509. 	if(shkp->mx == ESHK(shkp)->shk.x && shkp->my == ESHK(shkp)->shk.y
3510. 	    /* Actually, the shk should be made to block _any_
3511. 	     * door, including a door the player digs, if the
3512. 	     * shk is within a 'jumping' distance.
3513. 	     */
3514. 	    && ESHK(shkp)->shd.x == x && ESHK(shkp)->shd.y == y
3515. 	    && shkp->mcanmove && !shkp->msleeping
3516. 	    && (ESHK(shkp)->debit || ESHK(shkp)->billct ||
3517. 		ESHK(shkp)->robbed)) {
3518. 		pline("%s%s blocks your way!", shkname(shkp),
3519. 				Invis ? " senses your motion and" : "");
3520. 		return(TRUE);
3521. 	}
3522. 	return(FALSE);
3523. }
3524. 
3525. /* used in domove to block diagonal shop-entry */
3526. /* u.ux, u.uy should always be a door */
3527. boolean
3528. block_entry(x,y)
3529. register xchar x, y;
3530. {
3531. 	register xchar sx, sy;
3532. 	register int roomno;
3533. 	register struct monst *shkp;
3534. 
3535. 	if(!(IS_DOOR(levl[u.ux][u.uy].typ) &&
3536. 		levl[u.ux][u.uy].doormask == D_BROKEN)) return(FALSE);
3537. 
3538. 	roomno = *in_rooms(x, y, SHOPBASE);
3539. 	if(roomno < 0 || !IS_SHOP(roomno)) return(FALSE);
3540. 	if(!(shkp = shop_keeper((char)roomno)) || !inhishop(shkp))
3541. 		return(FALSE);
3542. 
3543. 	if(ESHK(shkp)->shd.x != u.ux || ESHK(shkp)->shd.y != u.uy)
3544. 		return(FALSE);
3545. 
3546. 	sx = ESHK(shkp)->shk.x;
3547. 	sy = ESHK(shkp)->shk.y;
3548. 
3549. 	if(shkp->mx == sx && shkp->my == sy
3550. 		&& shkp->mcanmove && !shkp->msleeping
3551. 		&& (x == sx-1 || x == sx+1 || y == sy-1 || y == sy+1)
3552. 		&& (Invis || carrying(PICK_AXE) || carrying(DWARVISH_MATTOCK))
3553. 	  ) {
3554. 		pline("%s%s blocks your way!", shkname(shkp),
3555. 				Invis ? " senses your motion and" : "");
3556. 		return(TRUE);
3557. 	}
3558. 	return(FALSE);
3559. }
3560. 
3561. #endif /* OVLB */
3562. #ifdef OVL2
3563. 
3564. char *
3565. shk_your(buf, obj)
3566. char *buf;
3567. struct obj *obj;
3568. {
3569. 	if (!shk_owns(buf, obj) && !mon_owns(buf, obj))
3570. 	    Strcpy(buf, obj->where == OBJ_INVENT ? "your" : "the");
3571. 	return buf;
3572. }
3573. 
3574. char *
3575. Shk_Your(buf, obj)
3576. char *buf;
3577. struct obj *obj;
3578. {
3579. 	(void) shk_your(buf, obj);
3580. 	*buf = highc(*buf);
3581. 	return buf;
3582. }
3583. 
3584. STATIC_OVL char *
3585. shk_owns(buf, obj)
3586. char *buf;
3587. struct obj *obj;
3588. {
3589. 	struct monst *shkp;
3590. 	xchar x, y;
3591. 
3592. 	if (get_obj_location(obj, &x, &y, 0) &&
3593. 	    (obj->unpaid ||
3594. 	     (obj->where==OBJ_FLOOR && !obj->no_charge && costly_spot(x,y)))) {
3595. 	    shkp = shop_keeper(inside_shop(x, y));
3596. 	    return strcpy(buf, shkp ? s_suffix(shkname(shkp)) : "the");
3597. 	}
3598. 	return (char *)0;
3599. }
3600. 
3601. STATIC_OVL char *
3602. mon_owns(buf, obj)
3603. char *buf;
3604. struct obj *obj;
3605. {
3606. 	if (obj->where == OBJ_MINVENT)
3607. 	    return strcpy(buf, s_suffix(mon_nam(obj->ocarry)));
3608. 	return (char *)0;
3609. }
3610. 
3611. #endif /* OVL2 */
3612. #ifdef OVLB
3613. 
3614. #ifdef __SASC
3615. void
3616. sasc_bug(struct obj *op, unsigned x){
3617. 	op->unpaid=x;
3618. }
3619. #endif
3620. 
3621. #endif /* OVLB */
3622. 
3623. /*shk.c*/