/
shk.c
4092 lines (3714 loc) · 107 KB
/
shk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SCCS Id: @(#)shk.c 3.4 2003/12/04 */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "eshk.h"
/*#define DEBUG*/
#define PAY_SOME 2
#define PAY_BUY 1
#define PAY_CANT 0 /* too poor */
#define PAY_SKIP (-1)
#define PAY_BROKE (-2)
#ifdef KOPS
STATIC_DCL void FDECL(makekops, (coord *));
STATIC_DCL void FDECL(call_kops, (struct monst *,BOOLEAN_P));
# ifdef OVLB
STATIC_DCL void FDECL(kops_gone, (BOOLEAN_P));
# endif /* OVLB */
#endif /* KOPS */
#define IS_SHOP(x) (rooms[x].rtype >= SHOPBASE)
extern const struct shclass shtypes[]; /* defined in shknam.c */
extern struct obj *thrownobj; /* defined in dothrow.c */
STATIC_VAR NEARDATA long int followmsg; /* last time of follow message */
STATIC_DCL void FDECL(setpaid, (struct monst *));
STATIC_DCL long FDECL(addupbill, (struct monst *));
STATIC_DCL void FDECL(pacify_shk, (struct monst *));
STATIC_DCL struct bill_x *FDECL(onbill, (struct obj *, struct monst *, BOOLEAN_P));
STATIC_DCL struct monst *FDECL(next_shkp, (struct monst *, BOOLEAN_P));
STATIC_DCL long FDECL(shop_debt, (struct eshk *));
STATIC_DCL char *FDECL(shk_owns, (char *,struct obj *));
STATIC_DCL char *FDECL(mon_owns, (char *,struct obj *));
STATIC_DCL void FDECL(clear_unpaid,(struct obj *));
STATIC_DCL long FDECL(check_credit, (long, struct monst *));
STATIC_DCL void FDECL(pay, (long, struct monst *));
STATIC_DCL long FDECL(get_cost, (struct obj *, struct monst *));
STATIC_DCL long FDECL(set_cost, (struct obj *, struct monst *));
STATIC_DCL const char *FDECL(shk_embellish, (struct obj *, long));
STATIC_DCL long FDECL(cost_per_charge, (struct monst *,struct obj *,BOOLEAN_P));
STATIC_DCL long FDECL(cheapest_item, (struct monst *));
STATIC_DCL int FDECL(dopayobj, (struct monst *, struct bill_x *,
struct obj **, int, BOOLEAN_P));
STATIC_DCL long FDECL(stolen_container, (struct obj *, struct monst *, long,
BOOLEAN_P));
STATIC_DCL long FDECL(getprice, (struct obj *,BOOLEAN_P));
STATIC_DCL void FDECL(shk_names_obj,
(struct monst *,struct obj *,const char *,long,const char *));
STATIC_DCL struct obj *FDECL(bp_to_obj, (struct bill_x *));
STATIC_DCL boolean FDECL(inherits, (struct monst *,int,int));
STATIC_DCL void FDECL(set_repo_loc, (struct eshk *));
STATIC_DCL boolean NDECL(angry_shk_exists);
STATIC_DCL void FDECL(rile_shk, (struct monst *));
STATIC_DCL void FDECL(rouse_shk, (struct monst *,BOOLEAN_P));
STATIC_DCL void FDECL(remove_damage, (struct monst *, BOOLEAN_P));
STATIC_DCL void FDECL(sub_one_frombill, (struct obj *, struct monst *));
STATIC_DCL void FDECL(add_one_tobill, (struct obj *, BOOLEAN_P));
STATIC_DCL void FDECL(dropped_container, (struct obj *, struct monst *,
BOOLEAN_P));
STATIC_DCL void FDECL(add_to_billobjs, (struct obj *));
STATIC_DCL void FDECL(bill_box_content, (struct obj *, BOOLEAN_P, BOOLEAN_P,
struct monst *));
#ifdef OVL1
static boolean FDECL(rob_shop, (struct monst *));
#endif
#ifdef OVLB
/*
invariants: obj->unpaid iff onbill(obj) [unless bp->useup]
obj->quan <= bp->bquan
*/
#ifdef GOLDOBJ
/*
Transfer money from inventory to monster when paying
shopkeepers, priests, oracle, succubus, & other demons.
Simple with only gold coins.
This routine will handle money changing when multiple
coin types is implemented, only appropriate
monsters will pay change. (Peaceful shopkeepers, priests
& the oracle try to maintain goodwill while selling
their wares or services. Angry monsters and all demons
will keep anything they get their hands on.
Returns the amount actually paid, so we can know
if the monster kept the change.
*/
long
money2mon(mon, amount)
struct monst *mon;
long amount;
{
struct obj *ygold = findgold(invent);
if (amount <= 0) {
impossible("%s payment in money2mon!", amount ? "negative" : "zero");
return 0L;
}
if (!ygold || ygold->quan < amount) {
impossible("Paying without %s money?", ygold ? "enough" : "");
return 0L;
}
if (ygold->quan > amount)
ygold = splitobj(ygold, amount);
else if (ygold->owornmask)
remove_worn_item(ygold, FALSE); /* quiver */
freeinv(ygold);
add_to_minv(mon, ygold);
flags.botl = 1;
return amount;
}
/*
Transfer money from monster to inventory.
Used when the shopkeeper pay for items, and when
the priest gives you money for an ale.
*/
void
money2u(mon, amount)
struct monst *mon;
long amount;
{
struct obj *mongold = findgold(mon->minvent);
if (amount <= 0) {
impossible("%s payment in money2u!", amount ? "negative" : "zero");
return;
}
if (!mongold || mongold->quan < amount) {
impossible("%s paying without %s money?", a_monnam(mon),
mongold ? "enough" : "");
return;
}
if (mongold->quan > amount) mongold = splitobj(mongold, amount);
obj_extract_self(mongold);
if (!merge_choice(invent, mongold) && inv_cnt() >= 52) {
You("have no room for the money!");
dropy(mongold);
} else {
addinv(mongold);
flags.botl = 1;
}
}
#endif /* GOLDOBJ */
STATIC_OVL struct monst *
next_shkp(shkp, withbill)
register struct monst *shkp;
register boolean withbill;
{
for (; shkp; shkp = shkp->nmon) {
if (DEADMONSTER(shkp)) continue;
if (shkp->isshk && (ESHK(shkp)->billct || !withbill)) break;
}
if (shkp) {
if (NOTANGRY(shkp)) {
if (ESHK(shkp)->surcharge) pacify_shk(shkp);
} else {
if (!ESHK(shkp)->surcharge) rile_shk(shkp);
}
}
return(shkp);
}
char *
shkname(mtmp) /* called in do_name.c */
register struct monst *mtmp;
{
return(ESHK(mtmp)->shknam);
}
void
shkgone(mtmp) /* called in mon.c */
struct monst *mtmp;
{
struct eshk *eshk = ESHK(mtmp);
struct mkroom *sroom = &rooms[eshk->shoproom - ROOMOFFSET];
struct obj *otmp;
char *p;
int sx, sy;
/* [BUG: some of this should be done on the shop level */
/* even when the shk dies on a different level.] */
if (on_level(&eshk->shoplevel, &u.uz)) {
remove_damage(mtmp, TRUE);
sroom->resident = (struct monst *)0;
if (!search_special(ANY_SHOP))
level.flags.has_shop = 0;
/* items on shop floor revert to ordinary objects */
for (sx = sroom->lx; sx <= sroom->hx; sx++)
for (sy = sroom->ly; sy <= sroom->hy; sy++)
for (otmp = level.objects[sx][sy]; otmp; otmp = otmp->nexthere)
otmp->no_charge = 0;
/* Make sure bill is set only when the
dead shk is the resident shk. */
if ((p = index(u.ushops, eshk->shoproom)) != 0) {
setpaid(mtmp);
eshk->bill_p = (struct bill_x *)0;
/* remove eshk->shoproom from u.ushops */
do { *p = *(p + 1); } while (*++p);
}
}
}
void
set_residency(shkp, zero_out)
register struct monst *shkp;
register boolean zero_out;
{
if (on_level(&(ESHK(shkp)->shoplevel), &u.uz))
rooms[ESHK(shkp)->shoproom - ROOMOFFSET].resident =
(zero_out)? (struct monst *)0 : shkp;
}
void
replshk(mtmp,mtmp2)
register struct monst *mtmp, *mtmp2;
{
rooms[ESHK(mtmp2)->shoproom - ROOMOFFSET].resident = mtmp2;
if (inhishop(mtmp) && *u.ushops == ESHK(mtmp)->shoproom) {
ESHK(mtmp2)->bill_p = &(ESHK(mtmp2)->bill[0]);
}
}
/* do shopkeeper specific structure munging -dlc */
void
restshk(shkp, ghostly)
struct monst *shkp;
boolean ghostly;
{
if (u.uz.dlevel) {
struct eshk *eshkp = ESHK(shkp);
if (eshkp->bill_p != (struct bill_x *) -1000)
eshkp->bill_p = &eshkp->bill[0];
/* shoplevel can change as dungeons move around */
/* savebones guarantees that non-homed shk's will be gone */
if (ghostly) {
assign_level(&eshkp->shoplevel, &u.uz);
if (ANGRY(shkp) && strncmpi(eshkp->customer, plname, PL_NSIZ))
pacify_shk(shkp);
}
}
}
#endif /* OVLB */
#ifdef OVL3
/* Clear the unpaid bit on all of the objects in the list. */
STATIC_OVL void
clear_unpaid(list)
register struct obj *list;
{
while (list) {
if (Has_contents(list)) clear_unpaid(list->cobj);
list->unpaid = 0;
list = list->nobj;
}
}
#endif /*OVL3*/
#ifdef OVLB
/* either you paid or left the shop or the shopkeeper died */
STATIC_OVL void
setpaid(shkp)
register struct monst *shkp;
{
register struct obj *obj;
register struct monst *mtmp;
/* FIXME: object handling should be limited to
items which are on this particular shk's bill */
clear_unpaid(invent);
clear_unpaid(fobj);
clear_unpaid(level.buriedobjlist);
if (thrownobj) thrownobj->unpaid = 0;
for(mtmp = fmon; mtmp; mtmp = mtmp->nmon)
clear_unpaid(mtmp->minvent);
for(mtmp = migrating_mons; mtmp; mtmp = mtmp->nmon)
clear_unpaid(mtmp->minvent);
while ((obj = billobjs) != 0) {
obj_extract_self(obj);
dealloc_obj(obj);
}
if(shkp) {
ESHK(shkp)->billct = 0;
ESHK(shkp)->credit = 0L;
ESHK(shkp)->debit = 0L;
ESHK(shkp)->loan = 0L;
}
}
STATIC_OVL long
addupbill(shkp)
register struct monst *shkp;
{
register int ct = ESHK(shkp)->billct;
register struct bill_x *bp = ESHK(shkp)->bill_p;
register long total = 0L;
while(ct--){
total += bp->price * bp->bquan;
bp++;
}
return(total);
}
#endif /* OVLB */
#ifdef OVL1
#ifdef KOPS
STATIC_OVL void
call_kops(shkp, nearshop)
register struct monst *shkp;
register boolean nearshop;
{
/* Keystone Kops srt@ucla */
register boolean nokops;
if(!shkp) return;
if (flags.soundok)
pline("An alarm sounds!");
nokops = ((mvitals[PM_KEYSTONE_KOP].mvflags & G_GONE) &&
(mvitals[PM_KOP_SERGEANT].mvflags & G_GONE) &&
(mvitals[PM_KOP_LIEUTENANT].mvflags & G_GONE) &&
(mvitals[PM_KOP_KAPTAIN].mvflags & G_GONE));
if(!angry_guards(!flags.soundok) && nokops) {
if(flags.verbose && flags.soundok)
pline("But no one seems to respond to it.");
return;
}
if(nokops) return;
{
coord mm;
if (nearshop) {
/* Create swarm around you, if you merely "stepped out" */
if (flags.verbose)
pline_The("Keystone Kops appear!");
mm.x = u.ux;
mm.y = u.uy;
makekops(&mm);
return;
}
if (flags.verbose)
pline_The("Keystone Kops are after you!");
/* Create swarm near down staircase (hinders return to level) */
mm.x = xdnstair;
mm.y = ydnstair;
makekops(&mm);
/* Create swarm near shopkeeper (hinders return to shop) */
mm.x = shkp->mx;
mm.y = shkp->my;
makekops(&mm);
}
}
#endif /* KOPS */
/* x,y is strictly inside shop */
char
inside_shop(x, y)
register xchar x, y;
{
register char rno;
rno = levl[x][y].roomno;
if ((rno < ROOMOFFSET) || levl[x][y].edge || !IS_SHOP(rno-ROOMOFFSET))
return(NO_ROOM);
else
return(rno);
}
void
u_left_shop(leavestring, newlev)
char *leavestring;
boolean newlev;
{
struct monst *shkp;
struct eshk *eshkp;
/*
* IF player
* ((didn't leave outright) AND
* ((he is now strictly-inside the shop) OR
* (he wasn't strictly-inside last turn anyway)))
* THEN (there's nothing to do, so just return)
*/
if(!*leavestring &&
(!levl[u.ux][u.uy].edge || levl[u.ux0][u.uy0].edge))
return;
shkp = shop_keeper(*u.ushops0);
if (!shkp || !inhishop(shkp))
return; /* shk died, teleported, changed levels... */
eshkp = ESHK(shkp);
if (!eshkp->billct && !eshkp->debit) /* bill is settled */
return;
if (!*leavestring && shkp->mcanmove && !shkp->msleeping) {
/*
* Player just stepped onto shop-boundary (known from above logic).
* Try to intimidate him into paying his bill
*/
verbalize(NOTANGRY(shkp) ?
"%s! Please pay before leaving." :
"%s! Don't you leave without paying!",
plname);
return;
}
if (rob_shop(shkp)) {
#ifdef KOPS
call_kops(shkp, (!newlev && levl[u.ux0][u.uy0].edge));
#else
(void) angry_guards(FALSE);
#endif
}
}
/* robbery from outside the shop via telekinesis or grappling hook */
void
remote_burglary(x, y)
xchar x, y;
{
struct monst *shkp;
struct eshk *eshkp;
shkp = shop_keeper(*in_rooms(x, y, SHOPBASE));
if (!shkp || !inhishop(shkp))
return; /* shk died, teleported, changed levels... */
eshkp = ESHK(shkp);
if (!eshkp->billct && !eshkp->debit) /* bill is settled */
return;
if (rob_shop(shkp)) {
#ifdef KOPS
/*[might want to set 2nd arg based on distance from shop doorway]*/
call_kops(shkp, FALSE);
#else
(void) angry_guards(FALSE);
#endif
}
}
/* shop merchandise has been taken; pay for it with any credit available;
return false if the debt is fully covered by credit, true otherwise */
static boolean
rob_shop(shkp)
struct monst *shkp;
{
struct eshk *eshkp;
long total;
eshkp = ESHK(shkp);
rouse_shk(shkp, TRUE);
total = (addupbill(shkp) + eshkp->debit);
if (eshkp->credit >= total) {
Your("credit of %ld %s is used to cover your shopping bill.",
eshkp->credit, currency(eshkp->credit));
total = 0L; /* credit gets cleared by setpaid() */
} else {
You("escaped the shop without paying!");
total -= eshkp->credit;
}
setpaid(shkp);
if (!total) return FALSE;
/* by this point, we know an actual robbery has taken place */
eshkp->robbed += total;
You("stole %ld %s worth of merchandise.",
total, currency(total));
if (!Role_if(PM_ROGUE)) /* stealing is unlawful */
adjalign(-sgn(u.ualign.type));
hot_pursuit(shkp);
return TRUE;
}
void
u_entered_shop(enterstring)
register char *enterstring;
{
register int rt;
register struct monst *shkp;
register struct eshk *eshkp;
static const char no_shk[] = "This shop appears to be deserted.";
static char empty_shops[5];
if(!*enterstring)
return;
if(!(shkp = shop_keeper(*enterstring))) {
if (!index(empty_shops, *enterstring) &&
in_rooms(u.ux, u.uy, SHOPBASE) !=
in_rooms(u.ux0, u.uy0, SHOPBASE))
pline(no_shk);
Strcpy(empty_shops, u.ushops);
u.ushops[0] = '\0';
return;
}
eshkp = ESHK(shkp);
if (!inhishop(shkp)) {
/* dump core when referenced */
eshkp->bill_p = (struct bill_x *) -1000;
if (!index(empty_shops, *enterstring))
pline(no_shk);
Strcpy(empty_shops, u.ushops);
u.ushops[0] = '\0';
return;
}
eshkp->bill_p = &(eshkp->bill[0]);
if ((!eshkp->visitct || *eshkp->customer) &&
strncmpi(eshkp->customer, plname, PL_NSIZ)) {
/* You seem to be new here */
eshkp->visitct = 0;
eshkp->following = 0;
(void) strncpy(eshkp->customer,plname,PL_NSIZ);
pacify_shk(shkp);
}
if (shkp->msleeping || !shkp->mcanmove || eshkp->following)
return; /* no dialog */
if (Invis) {
pline("%s senses your presence.", shkname(shkp));
verbalize("Invisible customers are not welcome!");
return;
}
rt = rooms[*enterstring - ROOMOFFSET].rtype;
if (ANGRY(shkp)) {
verbalize("So, %s, you dare return to %s %s?!",
plname,
s_suffix(shkname(shkp)),
shtypes[rt - SHOPBASE].name);
} else if (eshkp->robbed) {
pline("%s mutters imprecations against shoplifters.", shkname(shkp));
} else {
verbalize("%s, %s! Welcome%s to %s %s!",
Hello(shkp), plname,
eshkp->visitct++ ? " again" : "",
s_suffix(shkname(shkp)),
shtypes[rt - SHOPBASE].name);
}
/* can't do anything about blocking if teleported in */
if (!inside_shop(u.ux, u.uy)) {
boolean should_block;
int cnt;
const char *tool;
struct obj *pick = carrying(PICK_AXE),
*mattock = carrying(DWARVISH_MATTOCK);
if (pick || mattock) {
cnt = 1; /* so far */
if (pick && mattock) { /* carrying both types */
tool = "digging tool";
cnt = 2; /* `more than 1' is all that matters */
} else if (pick) {
tool = "pick-axe";
/* hack: `pick' already points somewhere into inventory */
while ((pick = pick->nobj) != 0)
if (pick->otyp == PICK_AXE) ++cnt;
} else { /* assert(mattock != 0) */
tool = "mattock";
while ((mattock = mattock->nobj) != 0)
if (mattock->otyp == DWARVISH_MATTOCK) ++cnt;
/* [ALI] Shopkeeper identifies mattock(s) */
if (!Blind) makeknown(DWARVISH_MATTOCK);
}
verbalize(NOTANGRY(shkp) ?
"Will you please leave your %s%s outside?" :
"Leave the %s%s outside.",
tool, plur(cnt));
should_block = TRUE;
#ifdef STEED
} else if (u.usteed) {
verbalize(NOTANGRY(shkp) ?
"Will you please leave %s outside?" :
"Leave %s outside.", y_monnam(u.usteed));
should_block = TRUE;
#endif
} else {
should_block = (Fast && (sobj_at(PICK_AXE, u.ux, u.uy) ||
sobj_at(DWARVISH_MATTOCK, u.ux, u.uy)));
}
if (should_block) (void) dochug(shkp); /* shk gets extra move */
}
return;
}
/*
Decide whether two unpaid items are mergable; caller is responsible for
making sure they're unpaid and the same type of object; we check the price
quoted by the shopkeeper and also that they both belong to the same shk.
*/
boolean
same_price(obj1, obj2)
struct obj *obj1, *obj2;
{
register struct monst *shkp1, *shkp2;
register struct bill_x *bp1 = 0, *bp2 = 0;
register boolean are_mergable = FALSE;
/* look up the first object by finding shk whose bill it's on */
for (shkp1 = next_shkp(fmon, TRUE); shkp1;
shkp1 = next_shkp(shkp1->nmon, TRUE))
if ((bp1 = onbill(obj1, shkp1, TRUE)) != 0) break;
/* second object is probably owned by same shk; if not, look harder */
if (shkp1 && (bp2 = onbill(obj2, shkp1, TRUE)) != 0) {
shkp2 = shkp1;
} else {
for (shkp2 = next_shkp(fmon, TRUE); shkp2;
shkp2 = next_shkp(shkp2->nmon, TRUE))
if ((bp2 = onbill(obj2, shkp2, TRUE)) != 0) break;
}
if (!bp1 || !bp2) impossible("same_price: object wasn't on any bill!");
else are_mergable = (shkp1 == shkp2 && bp1->price == bp2->price);
return are_mergable;
}
/*
* Figure out how much is owed to a given shopkeeper.
* At present, we ignore any amount robbed from the shop, to avoid
* turning the `$' command into a way to discover that the current
* level is bones data which has a shk on the warpath.
*/
STATIC_OVL long
shop_debt(eshkp)
struct eshk *eshkp;
{
struct bill_x *bp;
int ct;
long debt = eshkp->debit;
for (bp = eshkp->bill_p, ct = eshkp->billct; ct > 0; bp++, ct--)
debt += bp->price * bp->bquan;
return debt;
}
/* called in response to the `$' command */
void
shopper_financial_report()
{
struct monst *shkp, *this_shkp = shop_keeper(inside_shop(u.ux, u.uy));
struct eshk *eshkp;
long amt;
int pass;
if (this_shkp &&
!(ESHK(this_shkp)->credit || shop_debt(ESHK(this_shkp)))) {
You("have no credit or debt in here.");
this_shkp = 0; /* skip first pass */
}
/* pass 0: report for the shop we're currently in, if any;
pass 1: report for all other shops on this level. */
for (pass = this_shkp ? 0 : 1; pass <= 1; pass++)
for (shkp = next_shkp(fmon, FALSE);
shkp; shkp = next_shkp(shkp->nmon, FALSE)) {
if ((shkp != this_shkp) ^ pass) continue;
eshkp = ESHK(shkp);
if ((amt = eshkp->credit) != 0)
You("have %ld %s credit at %s %s.",
amt, currency(amt), s_suffix(shkname(shkp)),
shtypes[eshkp->shoptype - SHOPBASE].name);
else if (shkp == this_shkp)
You("have no credit in here.");
if ((amt = shop_debt(eshkp)) != 0)
You("owe %s %ld %s.",
shkname(shkp), amt, currency(amt));
else if (shkp == this_shkp)
You("don't owe any money here.");
}
}
#endif /* OVL1 */
#ifdef OVLB
int
inhishop(mtmp)
register struct monst *mtmp;
{
return(index(in_rooms(mtmp->mx, mtmp->my, SHOPBASE),
ESHK(mtmp)->shoproom) &&
on_level(&(ESHK(mtmp)->shoplevel), &u.uz));
}
struct monst *
shop_keeper(rmno)
register char rmno;
{
struct monst *shkp = rmno >= ROOMOFFSET ?
rooms[rmno - ROOMOFFSET].resident : 0;
if (shkp) {
if (NOTANGRY(shkp)) {
if (ESHK(shkp)->surcharge) pacify_shk(shkp);
} else {
if (!ESHK(shkp)->surcharge) rile_shk(shkp);
}
}
return shkp;
}
boolean
tended_shop(sroom)
register struct mkroom *sroom;
{
register struct monst *mtmp = sroom->resident;
if (!mtmp)
return(FALSE);
else
return((boolean)(inhishop(mtmp)));
}
STATIC_OVL struct bill_x *
onbill(obj, shkp, silent)
register struct obj *obj;
register struct monst *shkp;
register boolean silent;
{
if (shkp) {
register struct bill_x *bp = ESHK(shkp)->bill_p;
register int ct = ESHK(shkp)->billct;
while (--ct >= 0)
if (bp->bo_id == obj->o_id) {
if (!obj->unpaid) pline("onbill: paid obj on bill?");
return bp;
} else bp++;
}
if(obj->unpaid & !silent) pline("onbill: unpaid obj not on bill?");
return (struct bill_x *)0;
}
/* Delete the contents of the given object. */
void
delete_contents(obj)
register struct obj *obj;
{
register struct obj *curr;
while ((curr = obj->cobj) != 0) {
obj_extract_self(curr);
obfree(curr, (struct obj *)0);
}
}
/* called with two args on merge */
void
obfree(obj, merge)
register struct obj *obj, *merge;
{
register struct bill_x *bp;
register struct bill_x *bpm;
register struct monst *shkp;
if (obj->otyp == LEASH && obj->leashmon) o_unleash(obj);
if (obj->oclass == FOOD_CLASS) food_disappears(obj);
if (obj->oclass == SPBOOK_CLASS) book_disappears(obj);
if (Has_contents(obj)) delete_contents(obj);
shkp = 0;
if (obj->unpaid) {
/* look for a shopkeeper who owns this object */
for (shkp = next_shkp(fmon, TRUE); shkp;
shkp = next_shkp(shkp->nmon, TRUE))
if (onbill(obj, shkp, TRUE)) break;
}
/* sanity check, more or less */
if (!shkp) shkp = shop_keeper(*u.ushops);
/*
* Note: `shkp = shop_keeper(*u.ushops)' used to be
* unconditional. But obfree() is used all over
* the place, so making its behavior be dependent
* upon player location doesn't make much sense.
*/
if ((bp = onbill(obj, shkp, FALSE)) != 0) {
if(!merge){
bp->useup = 1;
obj->unpaid = 0; /* only for doinvbill */
add_to_billobjs(obj);
return;
}
bpm = onbill(merge, shkp, FALSE);
if(!bpm){
/* this used to be a rename */
impossible("obfree: not on bill??");
return;
} else {
/* this was a merger */
bpm->bquan += bp->bquan;
ESHK(shkp)->billct--;
#ifdef DUMB
{
/* DRS/NS 2.2.6 messes up -- Peter Kendell */
int indx = ESHK(shkp)->billct;
*bp = ESHK(shkp)->bill_p[indx];
}
#else
*bp = ESHK(shkp)->bill_p[ESHK(shkp)->billct];
#endif
}
}
dealloc_obj(obj);
}
#endif /* OVLB */
#ifdef OVL3
STATIC_OVL long
check_credit(tmp, shkp)
long tmp;
register struct monst *shkp;
{
long credit = ESHK(shkp)->credit;
if(credit == 0L) return(tmp);
if(credit >= tmp) {
pline_The("price is deducted from your credit.");
ESHK(shkp)->credit -=tmp;
tmp = 0L;
} else {
pline_The("price is partially covered by your credit.");
ESHK(shkp)->credit = 0L;
tmp -= credit;
}
return(tmp);
}
STATIC_OVL void
pay(tmp,shkp)
long tmp;
register struct monst *shkp;
{
long robbed = ESHK(shkp)->robbed;
long balance = ((tmp <= 0L) ? tmp : check_credit(tmp, shkp));
#ifndef GOLDOBJ
u.ugold -= balance;
shkp->mgold += balance;
#else
if (balance > 0) money2mon(shkp, balance);
else if (balance < 0) money2u(shkp, -balance);
#endif
flags.botl = 1;
if(robbed) {
robbed -= tmp;
if(robbed < 0) robbed = 0L;
ESHK(shkp)->robbed = robbed;
}
}
#endif /*OVL3*/
#ifdef OVLB
/* return shkp to home position */
void
home_shk(shkp, killkops)
register struct monst *shkp;
register boolean killkops;
{
register xchar x = ESHK(shkp)->shk.x, y = ESHK(shkp)->shk.y;
(void) mnearto(shkp, x, y, TRUE);
level.flags.has_shop = 1;
if (killkops) {
#ifdef KOPS
kops_gone(TRUE);
#else
You_feel("vaguely apprehensive.");
#endif
pacify_guards();
}
after_shk_move(shkp);
}
STATIC_OVL boolean
angry_shk_exists()
{
register struct monst *shkp;
for (shkp = next_shkp(fmon, FALSE);
shkp; shkp = next_shkp(shkp->nmon, FALSE))
if (ANGRY(shkp)) return(TRUE);
return(FALSE);
}
/* remove previously applied surcharge from all billed items */
STATIC_OVL void
pacify_shk(shkp)
register struct monst *shkp;
{
NOTANGRY(shkp) = TRUE; /* make peaceful */
if (ESHK(shkp)->surcharge) {
register struct bill_x *bp = ESHK(shkp)->bill_p;
register int ct = ESHK(shkp)->billct;
ESHK(shkp)->surcharge = FALSE;
while (ct-- > 0) {
register long reduction = (bp->price + 3L) / 4L;
bp->price -= reduction; /* undo 33% increase */
bp++;
}
}
}
/* add aggravation surcharge to all billed items */
STATIC_OVL void
rile_shk(shkp)
register struct monst *shkp;
{
NOTANGRY(shkp) = FALSE; /* make angry */
if (!ESHK(shkp)->surcharge) {
register struct bill_x *bp = ESHK(shkp)->bill_p;
register int ct = ESHK(shkp)->billct;
ESHK(shkp)->surcharge = TRUE;
while (ct-- > 0) {
register long surcharge = (bp->price + 2L) / 3L;
bp->price += surcharge;
bp++;
}
}
}
/* wakeup and/or unparalyze shopkeeper */
STATIC_OVL void
rouse_shk(shkp, verbosely)
struct monst *shkp;
boolean verbosely;
{
if (!shkp->mcanmove || shkp->msleeping) {
/* greed induced recovery... */
if (verbosely && canspotmon(shkp))
pline("%s %s.", Monnam(shkp),
shkp->msleeping ? "wakes up" : "can move again");
shkp->msleeping = 0;
shkp->mfrozen = 0;
shkp->mcanmove = 1;
}
}
void
make_happy_shk(shkp, silentkops)
register struct monst *shkp;
register boolean silentkops;
{
boolean wasmad = ANGRY(shkp);
struct eshk *eshkp = ESHK(shkp);
pacify_shk(shkp);
eshkp->following = 0;
eshkp->robbed = 0L;
if (!Role_if(PM_ROGUE))
adjalign(sgn(u.ualign.type));
if(!inhishop(shkp)) {
char shk_nam[BUFSZ];
boolean vanished = canseemon(shkp);
Strcpy(shk_nam, mon_nam(shkp));
if (on_level(&eshkp->shoplevel, &u.uz)) {
home_shk(shkp, FALSE);
/* didn't disappear if shk can still be seen */
if (canseemon(shkp)) vanished = FALSE;
} else {
/* if sensed, does disappear regardless whether seen */
if (sensemon(shkp)) vanished = TRUE;
/* can't act as porter for the Amulet, even if shk
happens to be going farther down rather than up */
mdrop_special_objs(shkp);