-
Notifications
You must be signed in to change notification settings - Fork 540
Expand file tree
/
Copy pathshk.c
More file actions
6060 lines (5503 loc) · 201 KB
/
shk.c
File metadata and controls
6060 lines (5503 loc) · 201 KB
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
/* NetHack 3.7 shk.c $NHDT-Date: 1736516428 2025/01/10 05:40:28 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.306 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2012. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
/*
* FIXME:
* The normal shop messages are verbal. There are a lot of cases
* where an alternate message is given if the hero is deaf or shk
* is mute (when poly'd), but that is usually visual-based. It is
* possible for hero to pay for items while blind (only if adjacent
* to shk) and the alternate messages fail to account for that.
*/
#define PAY_BUY 1
#define PAY_CANT 0 /* too poor */
#define PAY_SKIP (-1)
#define PAY_BROKE (-2)
enum billitem_status {
FullyUsedUp = 1, /* completely used up; obj->where==OBJ_ONBILL */
PartlyUsedUp = 2, /* partly used up; obj->where==OBJ_INVENT or similar */
PartlyIntact = 3, /* intact portion of partly used up item */
FullyIntact = 4, /* normal unpaid item */
KnownContainer = 5, /* container->cknown==1, holding unpaid item(s) */
UndisclosedContainer = 6, /* container->cknown==0 */
};
/* this is similar to sortloot; the shop bill gets converted into an array of
struct sortbill_item so that sorting and traversal don't need to access
the original bill or even the shk; the array gets sorted by usedup vs
unpaid and by cost within each of those two categories */
struct sortbill_item {
struct obj *obj;
long cost; /* full amount for current quantity, not per-unit amount */
long quan; /* count for this entry; subset if this is partly used or
* partly intact */
int bidx; /* index into ESHK(shkp)->bill_p[]; hero-owned container,
* which isn't in bill_p[], uses bidx == -1 */
int8 usedup; /* billitem_status, small but needs to be signed for qsort()
* [for an earlier edition; 'signed' no longer necessary] */
boolean queuedpay; /* buy without asking when containers are involved
* or purchase targets have been picked via menu */
};
typedef struct sortbill_item Bill;
staticfn void makekops(coord *);
staticfn void getcad(struct monst *, const char *, coordxy, coordxy, boolean,
boolean, boolean);
staticfn void call_kops(struct monst *, boolean);
staticfn void kops_gone(boolean);
#define NOTANGRY(mon) ((mon)->mpeaceful)
#define ANGRY(mon) (!NOTANGRY(mon))
#define IS_SHOP(x) (svr.rooms[x].rtype >= SHOPBASE)
#define muteshk(shkp) (helpless(shkp) || (shkp)->data->msound <= MS_ANIMAL)
extern const struct shclass shtypes[]; /* defined in shknam.c */
static const char and_its_contents[] = " and its contents";
static const char the_contents_of[] = "the contents of ";
staticfn void append_honorific(char *);
staticfn long addupbill(struct monst *);
staticfn void pacify_shk(struct monst *, boolean);
staticfn struct bill_x *onbill(struct obj *, struct monst *, boolean);
staticfn struct monst *next_shkp(struct monst *, boolean);
staticfn long shop_debt(struct eshk *);
staticfn char *shk_owns(char *, struct obj *);
staticfn char *mon_owns(char *, struct obj *);
staticfn void clear_unpaid_obj(struct monst *, struct obj *);
staticfn void clear_unpaid(struct monst *, struct obj *);
staticfn void clear_no_charge_obj(struct monst *, struct obj *);
staticfn void clear_no_charge(struct monst *, struct obj *);
staticfn void clear_no_charge_pets(struct monst *);
staticfn long check_credit(long, struct monst *);
staticfn void pay(long, struct monst *);
staticfn long get_cost(struct obj *, struct monst *);
staticfn long set_cost(struct obj *, struct monst *);
staticfn const char *shk_embellish(struct obj *, long);
staticfn long cost_per_charge(struct monst *, struct obj *, boolean);
staticfn int QSORTCALLBACK sortbill_cmp(const genericptr, const genericptr)
NONNULLPTRS;
staticfn long cheapest_item(int, Bill *) NONNULLPTRS;
staticfn int make_itemized_bill(struct monst *shkp, Bill **ibill) NONNULLPTRS;
staticfn int menu_pick_pay_items(int, Bill *) NONNULLPTRS;
staticfn boolean pay_billed_items(struct monst *, int, Bill *, boolean,
boolean *) NONNULLPTRS;
staticfn void update_bill(int, int, Bill *, struct eshk *, struct bill_x *,
struct obj *) NONNULLPTRS;
staticfn int dopayobj(struct monst *, struct bill_x *, struct obj *, int,
boolean, boolean) NONNULLPTRS;
staticfn int buy_container(struct monst *, int, int, Bill *) NONNULLPTRS;
staticfn void reject_purchase(struct monst *, struct obj *, long) NONNULLPTRS;
staticfn boolean insufficient_funds(struct monst *, struct obj *, long)
NONNULLPTRS;
staticfn long stolen_container(struct obj *, struct monst *, long, boolean);
staticfn long corpsenm_price_adj(struct obj *);
staticfn long getprice(struct obj *, boolean);
staticfn void shk_names_obj(struct monst *, struct obj *, const char *, long,
const char *);
staticfn boolean inherits(struct monst *, int, int, boolean);
staticfn void set_repo_loc(struct monst *);
staticfn struct obj *bp_to_obj(struct bill_x *);
staticfn long get_pricing_units(struct obj *);
staticfn boolean angry_shk_exists(void);
staticfn void home_shk(struct monst *, boolean);
staticfn void rile_shk(struct monst *);
staticfn void rouse_shk(struct monst *, boolean);
staticfn boolean shk_impaired(struct monst *);
staticfn boolean repairable_damage(struct damage *, struct monst *);
staticfn struct damage *find_damage(struct monst *);
staticfn void discard_damage_struct(struct damage *);
staticfn void discard_damage_owned_by(struct monst *);
staticfn void shk_fixes_damage(struct monst *);
staticfn uint8 litter_getpos(uint8 *, coordxy, coordxy, struct monst *);
staticfn void litter_scatter(uint8 *, coordxy, coordxy, struct monst *);
staticfn void litter_newsyms(uint8 *, coordxy, coordxy);
staticfn int repair_damage(struct monst *, struct damage *, boolean);
staticfn void sub_one_frombill(struct obj *, struct monst *) NONNULLPTRS;
staticfn void add_one_tobill(struct obj *, boolean, struct monst *);
staticfn void dropped_container(struct obj *, struct monst *, boolean);
staticfn void add_to_billobjs(struct obj *);
staticfn void bill_box_content(struct obj *, boolean, boolean,
struct monst *);
staticfn boolean rob_shop(struct monst *);
staticfn void deserted_shop(char *);
staticfn boolean special_stock(struct obj *, struct monst *, boolean);
staticfn const char *cad(boolean);
/*
invariants: obj->unpaid iff onbill(obj) [unless bp->useup]
obj->quan <= bp->bquan
*/
static const char *const angrytexts[] = {
"quite upset", "ticked off", "furious"
};
/*
* Transfer money from inventory to monster when paying
* shopkeepers, priests, oracle, succubus, and 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
* and 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(struct monst *mon, long amount)
{
struct obj *ygold = findgold(gi.invent);
if (amount <= 0) {
impossible("%s payment in money2mon!", amount ? "negative" : "zero");
return 0L;
}
if (!ygold || ygold->quan < amount) {
impossible("Paying without %s gold?", 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);
disp.botl = TRUE;
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(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 gold?", a_monnam(mon),
mongold ? "enough" : "");
return;
}
if (mongold->quan > amount)
mongold = splitobj(mongold, amount);
obj_extract_self(mongold);
if (!merge_choice(gi.invent, mongold)
&& inv_cnt(FALSE) >= invlet_basic) {
You("have no room for the gold!");
dropy(mongold);
} else {
addinv(mongold);
disp.botl = TRUE;
}
}
staticfn struct monst *
next_shkp(struct monst *shkp, boolean withbill)
{
for (; shkp; shkp = shkp->nmon) {
if (DEADMONSTER(shkp))
continue;
if (shkp->isshk && (ESHK(shkp)->billct || !withbill))
break;
}
if (shkp) {
if (ANGRY(shkp)) {
if (!ESHK(shkp)->surcharge)
rile_shk(shkp);
}
}
return shkp;
}
/* called in mon.c */
void
shkgone(struct monst *mtmp)
{
struct eshk *eshk = ESHK(mtmp);
struct mkroom *sroom = &svr.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)) {
discard_damage_owned_by(mtmp);
sroom->resident = (struct monst *) 0;
if (!search_special(ANY_SHOP))
svl.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 = svl.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 = strchr(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(struct monst *shkp, boolean zero_out)
{
if (on_level(&(ESHK(shkp)->shoplevel), &u.uz))
svr.rooms[ESHK(shkp)->shoproom - ROOMOFFSET].resident =
(zero_out) ? (struct monst *) 0 : shkp;
}
void
replshk(struct monst *mtmp, struct monst *mtmp2)
{
svr.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(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, svp.plname, PL_NSIZ))
pacify_shk(shkp, TRUE);
}
}
}
/* clear the unpaid bit on a single object and its contents */
staticfn void
clear_unpaid_obj(struct monst *shkp, struct obj *otmp)
{
if (Has_contents(otmp))
clear_unpaid(shkp, otmp->cobj);
if (onbill(otmp, shkp, TRUE))
otmp->unpaid = 0;
}
/* clear the unpaid bit on all of the objects in the list */
staticfn void
clear_unpaid(struct monst *shkp, struct obj *list)
{
while (list) {
clear_unpaid_obj(shkp, list);
list = list->nobj;
}
}
/* clear the no_charge bit on a single object and its contents */
staticfn void
clear_no_charge_obj(
struct monst *shkp, /* if null, clear regardless of shop */
struct obj *otmp)
{
if (Has_contents(otmp))
clear_no_charge(shkp, otmp->cobj);
if (otmp->no_charge) {
struct monst *rm_shkp;
int rno;
coordxy x, y;
/*
* Clear no_charge if
* shkp is Null (clear all items on specified list)
* or not located somewhere that we expect no_charge (which is
* floor [of shop] or inside container [on shop floor])
* or can't find object's map coordinates (should never happen
* for floor or contained; conceivable if on shop bill somehow
* but would have failed the floor-or-contained test since
* containers get emptied before going onto bill)
* or fails location sanity check (should always be good when
* location successfully found)
* or not inside any room
* or the room isn't a shop
* or the shop has no shopkeeper (deserted)
* or shopkeeper is the current one (to avoid clearing no_charge
* for items located in some rival's shop).
*
* no_charge items in a shop which is only temporarily deserted
* become owned by the shop now and will be for-sale once the shk
* returns.
*/
if (!shkp
|| (otmp->where != OBJ_FLOOR
&& otmp->where != OBJ_CONTAINED
&& otmp->where != OBJ_BURIED)
|| !get_obj_location(otmp, &x, &y, OBJ_CONTAINED | OBJ_BURIED)
|| !isok(x, y)
|| (rno = levl[x][y].roomno) < ROOMOFFSET
|| !IS_SHOP(rno - ROOMOFFSET)
|| (rm_shkp = svr.rooms[rno - ROOMOFFSET].resident) == 0
|| rm_shkp == shkp)
otmp->no_charge = 0;
}
}
/* clear the no_charge bit on all of the objects in the list */
staticfn void
clear_no_charge(struct monst *shkp, struct obj *list)
{
while (list) {
/* handle first element of list and any contents it may have */
clear_no_charge_obj(shkp, list);
/* move on to next element of list */
list = list->nobj;
}
}
/* clear no_charge from objects in pets' inventories belonging to shkp */
staticfn void
clear_no_charge_pets(struct monst *shkp)
{
struct monst *mtmp;
for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
if (mtmp->mtame && mtmp->minvent)
clear_no_charge(shkp, mtmp->minvent);
}
/* either you paid or left the shop or the shopkeeper died */
void
setpaid(struct monst *shkp)
{
struct obj *obj;
struct monst *mtmp;
clear_unpaid(shkp, gi.invent);
clear_unpaid(shkp, fobj);
if (svl.level.buriedobjlist)
clear_unpaid(shkp, svl.level.buriedobjlist);
if (gt.thrownobj)
clear_unpaid_obj(shkp, gt.thrownobj);
if (gk.kickedobj)
clear_unpaid_obj(shkp, gk.kickedobj);
for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
if (mtmp->minvent)
clear_unpaid(shkp, mtmp->minvent);
for (mtmp = gm.migrating_mons; mtmp; mtmp = mtmp->nmon)
if (mtmp->minvent)
clear_unpaid(shkp, mtmp->minvent);
/* clear obj->no_charge for all obj in shkp's shop */
clear_no_charge(shkp, fobj);
clear_no_charge(shkp, svl.level.buriedobjlist);
while ((obj = gb.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;
}
}
staticfn long
addupbill(struct monst *shkp)
{
int ct = ESHK(shkp)->billct;
struct bill_x *bp = ESHK(shkp)->bill_p;
long total = 0L;
while (ct--) {
total += bp->price * bp->bquan;
bp++;
}
return total;
}
staticfn void
call_kops(struct monst *shkp, boolean nearshop)
{
/* Keystone Kops srt@ucla */
boolean nokops;
if (!shkp)
return;
Soundeffect(se_alarm, 80);
if (!Deaf)
pline("An alarm sounds!");
nokops = ((svm.mvitals[PM_KEYSTONE_KOP].mvflags & G_GONE)
&& (svm.mvitals[PM_KOP_SERGEANT].mvflags & G_GONE)
&& (svm.mvitals[PM_KOP_LIEUTENANT].mvflags & G_GONE)
&& (svm.mvitals[PM_KOP_KAPTAIN].mvflags & G_GONE));
if (!angry_guards(!!Deaf) && nokops) {
if (flags.verbose && !Deaf)
pline("But no one seems to respond to it.");
return;
}
if (nokops)
return;
{
coord mm;
coordxy sx = 0, sy = 0;
choose_stairs(&sx, &sy, TRUE);
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) */
if (isok(sx, sy)) {
mm.x = sx;
mm.y = sy;
makekops(&mm);
}
/* Create swarm near shopkeeper (hinders return to shop) */
mm.x = shkp->mx;
mm.y = shkp->my;
makekops(&mm);
}
}
/* x,y is strictly inside shop */
char
inside_shop(coordxy x, coordxy y)
{
char rno;
rno = levl[x][y].roomno;
if ((rno < ROOMOFFSET) || levl[x][y].edge || !IS_SHOP(rno - ROOMOFFSET))
rno = NO_ROOM;
return rno;
}
void
u_left_shop(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(*leavestring ? *leavestring : *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 && !muteshk(shkp)) {
/*
* Player just stepped onto shop-boundary (known from above logic).
* Try to intimidate him into paying his bill
*/
boolean not_upset = !eshkp->surcharge;
if (!Deaf && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
verbalize(not_upset ? "%s! Please pay before leaving."
: "%s! Don't you leave without paying!",
svp.plname);
} else {
pline("%s %s that you need to pay before leaving%s",
Shknam(shkp),
not_upset ? "points out" : "makes it clear",
not_upset ? "." : "!");
}
return;
}
if (rob_shop(shkp)) {
call_kops(shkp, (!newlev && levl[u.ux0][u.uy0].edge));
}
}
void
credit_report(struct monst *shkp, int idx, boolean silent)
{
struct eshk *eshkp = ESHK(shkp);
static long credit_snap[2][3] = {{0L, 0L, 0L}, {0L, 0L, 0L}};
if (!idx) {
credit_snap[BEFORE][0] = credit_snap[NOW][0] = 0L;
credit_snap[BEFORE][1] = credit_snap[NOW][1] = 0L;
credit_snap[BEFORE][2] = credit_snap[NOW][2] = 0L;
} else {
idx = 1;
}
credit_snap[idx][0] = eshkp->credit;
credit_snap[idx][1] = eshkp->debit;
credit_snap[idx][2] = eshkp->loan;
if (idx && !silent) {
long amt = 0L;
const char *msg = "debt has increased";
if (credit_snap[NOW][0] < credit_snap[BEFORE][0]) {
amt = credit_snap[BEFORE][0] - credit_snap[NOW][0];
msg = "credit has been reduced";
} else if (credit_snap[NOW][1] > credit_snap[BEFORE][1]) {
amt = credit_snap[NOW][1] - credit_snap[BEFORE][1];
} else if (credit_snap[NOW][2] > credit_snap[BEFORE][2]) {
amt = credit_snap[NOW][2] - credit_snap[BEFORE][2];
}
if (amt)
Your("%s by %ld %s.", msg, amt, currency(amt));
}
}
/* robbery from outside the shop via telekinesis or grappling hook */
void
remote_burglary(coordxy x, coordxy 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)) {
/*[might want to set 2nd arg based on distance from shop doorway]*/
call_kops(shkp, FALSE);
}
}
/* shop merchandise has been taken; pay for it with any credit available;
return false if the debt is fully covered by credit, true otherwise */
staticfn boolean
rob_shop(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));
livelog_printf(LL_ACHIEVE, "stole %ld %s worth of merchandise from %s %s",
total, currency(total), s_suffix(shkname(shkp)),
shtypes[eshkp->shoptype - SHOPBASE].name);
if (!Role_if(PM_ROGUE)) /* stealing is unlawful */
adjalign(-sgn(u.ualign.type));
hot_pursuit(shkp);
return TRUE;
}
/* give a message when entering an untended shop (caller has verified that) */
staticfn void
deserted_shop(/*const*/ char *enterstring)
{
struct monst *mtmp;
struct mkroom *r = &svr.rooms[(int) *enterstring - ROOMOFFSET];
int x, y, m = 0, n = 0;
for (x = r->lx; x <= r->hx; ++x)
for (y = r->ly; y <= r->hy; ++y) {
if (u_at(x, y))
continue;
if ((mtmp = m_at(x, y)) != 0) {
++n;
if (sensemon(mtmp) || ((M_AP_TYPE(mtmp) == M_AP_NOTHING
|| M_AP_TYPE(mtmp) == M_AP_MONSTER)
&& canseemon(mtmp)))
++m;
}
}
if (Blind && !(Blind_telepat || Detect_monsters))
++n; /* force feedback to be less specific */
pline("This shop %s %s.", (m < n) ? "seems to be" : "is",
!n ? "deserted" : "untended");
}
/* called from check_special_room(hack.c) */
void
u_entered_shop(char *enterstring)
{
static char empty_shops[5];
struct monst *shkp;
struct eshk *eshkp;
int rt;
if (!*enterstring)
return;
shkp = shop_keeper(*enterstring);
if (!shkp) {
if (!strchr(empty_shops, *enterstring)
&& (in_rooms(u.ux, u.uy, SHOPBASE)
!= in_rooms(u.ux0, u.uy0, SHOPBASE)))
deserted_shop(enterstring);
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 (!strchr(empty_shops, *enterstring))
deserted_shop(enterstring);
Strcpy(empty_shops, u.ushops);
u.ushops[0] = '\0';
return;
}
record_achievement(ACH_SHOP);
eshkp->bill_p = &(eshkp->bill[0]);
if ((!eshkp->visitct || *eshkp->customer)
&& strncmpi(eshkp->customer, svp.plname, PL_NSIZ)) {
/* You seem to be new here */
eshkp->visitct = 0;
eshkp->following = 0;
(void) strncpy(eshkp->customer, svp.plname, PL_NSIZ);
pacify_shk(shkp, TRUE);
}
if (muteshk(shkp) || eshkp->following)
return; /* no dialog */
if (Invis) {
pline("%s senses your presence.", Shknam(shkp));
if (!Deaf && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
verbalize("Invisible customers are not welcome!");
} else {
pline("%s stands firm as if %s knows you are there.",
Shknam(shkp), noit_mhe(shkp));
}
return;
}
rt = svr.rooms[*enterstring - ROOMOFFSET].rtype;
if (ANGRY(shkp)) {
if (!Deaf && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
verbalize("So, %s, you dare return to %s %s?!", svp.plname,
s_suffix(shkname(shkp)), shtypes[rt - SHOPBASE].name);
} else {
pline("%s seems %s over your return to %s %s!",
Shknam(shkp), ROLL_FROM(angrytexts),
noit_mhis(shkp), shtypes[rt - SHOPBASE].name);
}
} else if (eshkp->surcharge) {
if (!Deaf && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
verbalize("Back again, %s? I've got my %s on you.",
svp.plname, mbodypart(shkp, EYE));
} else {
pline_The("atmosphere at %s %s seems unwelcoming.",
s_suffix(shkname(shkp)), shtypes[rt - SHOPBASE].name);
}
} else if (eshkp->robbed) {
if (!Deaf) {
Soundeffect(se_mutter_imprecations, 50);
pline("%s mutters imprecations against shoplifters.",
Shknam(shkp));
} else {
pline("%s is combing through %s inventory list.",
Shknam(shkp), noit_mhis(shkp));
}
} else {
if (!Deaf && !muteshk(shkp)) {
set_voice(shkp, 0, 80, 0);
verbalize("%s, %s! Welcome%s to %s %s!", Hello(shkp), svp.plname,
eshkp->visitct++ ? " again" : "",
s_suffix(shkname(shkp)), shtypes[rt - SHOPBASE].name);
} else {
You("enter %s %s%s!",
s_suffix(shkname(shkp)),
shtypes[rt - SHOPBASE].name,
eshkp->visitct++ ? " again" : "");
}
}
/* can't do anything about blocking if teleported in */
if (!inside_shop(u.ux, u.uy)) {
boolean should_block, not_upset = !eshkp->surcharge;
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);
}
if (!Deaf && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
verbalize(not_upset
? "Will you please leave your %s%s outside?"
: "Leave the %s%s outside.",
tool, plur(cnt));
} else {
pline("%s %s to let you in with your %s%s.",
Shknam(shkp),
not_upset ? "is hesitant" : "refuses",
tool, plur(cnt));
}
should_block = TRUE;
} else if (u.usteed) {
if (!Deaf && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
verbalize(not_upset ? "Will you please leave %s outside?"
: "Leave %s outside.",
y_monnam(u.usteed));
} else {
pline("%s %s to let you in while you're riding %s.",
Shknam(shkp),
not_upset ? "doesn't want" : "refuses",
y_monnam(u.usteed));
}
should_block = TRUE;
} 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;
}
/* called when removing a pick-axe or mattock from a container */
void
pick_pick(struct obj *obj)
{
struct monst *shkp;
if (obj->unpaid || !is_pick(obj))
return;
shkp = shop_keeper(*u.ushops);
if (shkp && inhishop(shkp)) {
static NEARDATA long pickmovetime = 0L;
/* if you bring a sack of N picks into a shop to sell,
don't repeat this N times when they're taken out */
if (svm.moves != pickmovetime) {
if (!Deaf && !muteshk(shkp)) {
SetVoice(shkp, 0, 80, 0);
verbalize("You sneaky %s! Get out of here with that pick!",
cad(FALSE));
} else {
pline("%s %s your pick!",
Shknam(shkp),
haseyes(shkp->data) ? "glares at"
: "is dismayed because of");
}
}
pickmovetime = svm.moves;
}
}
/*
Decide whether two unpaid items are mergeable; 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(struct obj *obj1, struct obj *obj2)
{
struct monst *shkp1, *shkp2;
struct bill_x *bp1 = 0, *bp2 = 0;
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.
*/
staticfn long
shop_debt(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(void)
{
struct monst *shkp, *this_shkp = shop_keeper(inside_shop(u.ux, u.uy));
struct eshk *eshkp;
long amt;
int pass;
eshkp = this_shkp ? ESHK(this_shkp) : 0;
if (eshkp && !(eshkp->credit || shop_debt(eshkp))) {
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 gold here.");
}
}
/* 1: shopkeeper is currently in her shop or its boundary; 0: not */
int
inhishop(struct monst *shkp)
{
char *shkrooms;
struct eshk *eshkp = ESHK(shkp);
if (!on_level(&eshkp->shoplevel, &u.uz))
return FALSE;
shkrooms = in_rooms(shkp->mx, shkp->my, SHOPBASE);
return (strchr(shkrooms, eshkp->shoproom) != 0);
}
/* return the shopkeeper for rooms[rmno-2]; returns Null if there isn't one */
struct monst *
shop_keeper(char rmno)
{
struct monst *shkp;
shkp = (rmno >= ROOMOFFSET) ? svr.rooms[rmno - ROOMOFFSET].resident : 0;
if (shkp) {
if (has_eshk(shkp)) {
if (ANGRY(shkp)) {