/
pickup.c
3272 lines (3008 loc) · 112 KB
/
pickup.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
/* NetHack 3.6 pickup.c $NHDT-Date: 1576282488 2019/12/14 00:14:48 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.237 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2012. */
/* NetHack may be freely redistributed. See license for details. */
/*
* Contains code for picking objects up, and container use.
*/
#include "hack.h"
#define CONTAINED_SYM '>' /* from invent.c */
STATIC_DCL void FDECL(simple_look, (struct obj *, BOOLEAN_P));
STATIC_DCL boolean FDECL(query_classes, (char *, boolean *, boolean *,
const char *, struct obj *,
BOOLEAN_P, int *));
STATIC_DCL boolean FDECL(fatal_corpse_mistake, (struct obj *, BOOLEAN_P));
STATIC_DCL void FDECL(check_here, (BOOLEAN_P));
STATIC_DCL boolean FDECL(n_or_more, (struct obj *));
STATIC_DCL boolean FDECL(all_but_uchain, (struct obj *));
#if 0 /* not used */
STATIC_DCL boolean FDECL(allow_cat_no_uchain, (struct obj *));
#endif
STATIC_DCL int FDECL(autopick, (struct obj *, int, menu_item **));
STATIC_DCL int FDECL(count_categories, (struct obj *, int));
STATIC_DCL int FDECL(delta_cwt, (struct obj *, struct obj *));
STATIC_DCL long FDECL(carry_count, (struct obj *, struct obj *, long,
BOOLEAN_P, int *, int *));
STATIC_DCL int FDECL(lift_object, (struct obj *, struct obj *, long *,
BOOLEAN_P));
STATIC_DCL boolean FDECL(mbag_explodes, (struct obj *, int));
STATIC_DCL long FDECL(boh_loss, (struct obj *container, int));
STATIC_PTR int FDECL(in_container, (struct obj *));
STATIC_PTR int FDECL(out_container, (struct obj *));
STATIC_DCL void FDECL(removed_from_icebox, (struct obj *));
STATIC_DCL long FDECL(mbag_item_gone, (int, struct obj *));
STATIC_DCL void FDECL(explain_container_prompt, (BOOLEAN_P));
STATIC_DCL int FDECL(traditional_loot, (BOOLEAN_P));
STATIC_DCL int FDECL(menu_loot, (int, BOOLEAN_P));
STATIC_DCL char FDECL(in_or_out_menu, (const char *, struct obj *, BOOLEAN_P,
BOOLEAN_P, BOOLEAN_P, BOOLEAN_P));
STATIC_DCL boolean FDECL(able_to_loot, (int, int, BOOLEAN_P));
STATIC_DCL boolean NDECL(reverse_loot);
STATIC_DCL boolean FDECL(mon_beside, (int, int));
STATIC_DCL int FDECL(do_loot_cont, (struct obj **, int, int));
STATIC_DCL void FDECL(tipcontainer, (struct obj *));
/* define for query_objlist() and autopickup() */
#define FOLLOW(curr, flags) \
(((flags) & BY_NEXTHERE) ? (curr)->nexthere : (curr)->nobj)
#define GOLD_WT(n) (((n) + 50L) / 100L)
/* if you can figure this out, give yourself a hearty pat on the back... */
#define GOLD_CAPACITY(w, n) (((w) * -100L) - ((n) + 50L) - 1L)
/* A variable set in use_container(), to be used by the callback routines
in_container() and out_container() from askchain() and use_container().
Also used by menu_loot() and container_gone(). */
static NEARDATA struct obj *current_container;
static NEARDATA boolean abort_looting;
#define Icebox (current_container->otyp == ICE_BOX)
static const char
moderateloadmsg[] = "You have a little trouble lifting",
nearloadmsg[] = "You have much trouble lifting",
overloadmsg[] = "You have extreme difficulty lifting";
/* BUG: this lets you look at cockatrice corpses while blind without
touching them */
/* much simpler version of the look-here code; used by query_classes() */
STATIC_OVL void
simple_look(otmp, here)
struct obj *otmp; /* list of objects */
boolean here; /* flag for type of obj list linkage */
{
/* Neither of the first two cases is expected to happen, since
* we're only called after multiple classes of objects have been
* detected, hence multiple objects must be present.
*/
if (!otmp) {
impossible("simple_look(null)");
} else if (!(here ? otmp->nexthere : otmp->nobj)) {
pline1(doname(otmp));
} else {
winid tmpwin = create_nhwindow(NHW_MENU);
putstr(tmpwin, 0, "");
do {
putstr(tmpwin, 0, doname(otmp));
otmp = here ? otmp->nexthere : otmp->nobj;
} while (otmp);
display_nhwindow(tmpwin, TRUE);
destroy_nhwindow(tmpwin);
}
}
int
collect_obj_classes(ilets, otmp, here, filter, itemcount)
char ilets[];
register struct obj *otmp;
boolean here;
boolean FDECL((*filter), (OBJ_P));
int *itemcount;
{
register int iletct = 0;
register char c;
*itemcount = 0;
ilets[iletct] = '\0'; /* terminate ilets so that index() will work */
while (otmp) {
c = def_oc_syms[(int) otmp->oclass].sym;
if (!index(ilets, c) && (!filter || (*filter)(otmp)))
ilets[iletct++] = c, ilets[iletct] = '\0';
*itemcount += 1;
otmp = here ? otmp->nexthere : otmp->nobj;
}
return iletct;
}
/*
* Suppose some '?' and '!' objects are present, but '/' objects aren't:
* "a" picks all items without further prompting;
* "A" steps through all items, asking one by one;
* "?" steps through '?' items, asking, and ignores '!' ones;
* "/" becomes 'A', since no '/' present;
* "?a" or "a?" picks all '?' without further prompting;
* "/a" or "a/" becomes 'A' since there aren't any '/'
* (bug fix: 3.1.0 thru 3.1.3 treated it as "a");
* "?/a" or "a?/" or "/a?",&c picks all '?' even though no '/'
* (ie, treated as if it had just been "?a").
*/
STATIC_OVL boolean
query_classes(oclasses, one_at_a_time, everything, action, objs, here,
menu_on_demand)
char oclasses[];
boolean *one_at_a_time, *everything;
const char *action;
struct obj *objs;
boolean here;
int *menu_on_demand;
{
char ilets[36], inbuf[BUFSZ] = DUMMY; /* FIXME: hardcoded ilets[] length */
int iletct, oclassct;
boolean not_everything, filtered;
char qbuf[QBUFSZ];
boolean m_seen;
int itemcount, bcnt, ucnt, ccnt, xcnt, ocnt;
oclasses[oclassct = 0] = '\0';
*one_at_a_time = *everything = m_seen = FALSE;
if (menu_on_demand)
*menu_on_demand = 0;
iletct = collect_obj_classes(ilets, objs, here,
(boolean FDECL((*), (OBJ_P))) 0, &itemcount);
if (iletct == 0)
return FALSE;
if (iletct == 1) {
oclasses[0] = def_char_to_objclass(ilets[0]);
oclasses[1] = '\0';
} else { /* more than one choice available */
/* additional choices */
ilets[iletct++] = ' ';
ilets[iletct++] = 'a';
ilets[iletct++] = 'A';
ilets[iletct++] = (objs == invent ? 'i' : ':');
}
if (itemcount && menu_on_demand)
ilets[iletct++] = 'm';
if (count_unpaid(objs))
ilets[iletct++] = 'u';
tally_BUCX(objs, here, &bcnt, &ucnt, &ccnt, &xcnt, &ocnt);
if (bcnt)
ilets[iletct++] = 'B';
if (ucnt)
ilets[iletct++] = 'U';
if (ccnt)
ilets[iletct++] = 'C';
if (xcnt)
ilets[iletct++] = 'X';
ilets[iletct] = '\0';
if (iletct > 1) {
const char *where = 0;
char sym, oc_of_sym, *p;
ask_again:
oclasses[oclassct = 0] = '\0';
*one_at_a_time = *everything = FALSE;
not_everything = filtered = FALSE;
Sprintf(qbuf, "What kinds of thing do you want to %s? [%s]", action,
ilets);
getlin(qbuf, inbuf);
if (*inbuf == '\033')
return FALSE;
for (p = inbuf; (sym = *p++) != 0; ) {
if (sym == ' ')
continue;
else if (sym == 'A')
*one_at_a_time = TRUE;
else if (sym == 'a')
*everything = TRUE;
else if (sym == ':') {
simple_look(objs, here); /* dumb if objs==invent */
/* if we just scanned the contents of a container
then mark it as having known contents */
if (objs->where == OBJ_CONTAINED)
objs->ocontainer->cknown = 1;
goto ask_again;
} else if (sym == 'i') {
(void) display_inventory((char *) 0, TRUE);
goto ask_again;
} else if (sym == 'm') {
m_seen = TRUE;
} else if (index("uBUCX", sym)) {
add_valid_menu_class(sym); /* 'u' or 'B','U','C',or 'X' */
filtered = TRUE;
} else {
oc_of_sym = def_char_to_objclass(sym);
if (index(ilets, sym)) {
add_valid_menu_class(oc_of_sym);
oclasses[oclassct++] = oc_of_sym;
oclasses[oclassct] = '\0';
} else {
if (!where)
where = !strcmp(action, "pick up") ? "here"
: !strcmp(action, "take out") ? "inside" : "";
if (*where)
There("are no %c's %s.", sym, where);
else
You("have no %c's.", sym);
not_everything = TRUE;
}
}
} /* for p:sym in inbuf */
if (m_seen && menu_on_demand) {
*menu_on_demand = (((*everything || !oclassct) && !filtered)
? -2 : -3);
return FALSE;
}
if (!oclassct && (!*everything || not_everything)) {
/* didn't pick anything,
or tried to pick something that's not present */
*one_at_a_time = TRUE; /* force 'A' */
*everything = FALSE; /* inhibit 'a' */
}
}
return TRUE;
}
/* check whether hero is bare-handedly touching a cockatrice corpse */
STATIC_OVL boolean
fatal_corpse_mistake(obj, remotely)
struct obj *obj;
boolean remotely;
{
if (uarmg || remotely || obj->otyp != CORPSE
|| !touch_petrifies(&mons[obj->corpsenm]) || Stone_resistance)
return FALSE;
if (poly_when_stoned(youmonst.data) && polymon(PM_STONE_GOLEM)) {
display_nhwindow(WIN_MESSAGE, FALSE); /* --More-- */
return FALSE;
}
pline("Touching %s is a fatal mistake.",
corpse_xname(obj, (const char *) 0, CXN_SINGULAR | CXN_ARTICLE));
instapetrify(killer_xname(obj));
return TRUE;
}
/* attempting to manipulate a Rider's corpse triggers its revival */
boolean
rider_corpse_revival(obj, remotely)
struct obj *obj;
boolean remotely;
{
if (!obj || obj->otyp != CORPSE || !is_rider(&mons[obj->corpsenm]))
return FALSE;
pline("At your %s, the corpse suddenly moves...",
remotely ? "attempted acquisition" : "touch");
(void) revive_corpse(obj);
exercise(A_WIS, FALSE);
return TRUE;
}
/* look at the objects at our location, unless there are too many of them */
STATIC_OVL void
check_here(picked_some)
boolean picked_some;
{
register struct obj *obj;
register int ct = 0;
/* count the objects here */
for (obj = level.objects[u.ux][u.uy]; obj; obj = obj->nexthere) {
if (obj != uchain)
ct++;
}
/* If there are objects here, take a look. */
if (ct) {
if (context.run)
nomul(0);
flush_screen(1);
(void) look_here(ct, picked_some);
} else {
read_engr_at(u.ux, u.uy);
}
}
/* Value set by query_objlist() for n_or_more(). */
static long val_for_n_or_more;
/* query_objlist callback: return TRUE if obj's count is >= reference value */
STATIC_OVL boolean
n_or_more(obj)
struct obj *obj;
{
if (obj == uchain)
return FALSE;
return (boolean) (obj->quan >= val_for_n_or_more);
}
/* list of valid menu classes for query_objlist() and allow_category callback
(with room for all object classes, 'u'npaid, BUCX, and terminator) */
static char valid_menu_classes[MAXOCLASSES + 1 + 4 + 1];
static boolean class_filter, bucx_filter, shop_filter;
/* check valid_menu_classes[] for an entry; also used by askchain() */
boolean
menu_class_present(c)
int c;
{
return (c && index(valid_menu_classes, c)) ? TRUE : FALSE;
}
void
add_valid_menu_class(c)
int c;
{
static int vmc_count = 0;
if (c == 0) { /* reset */
vmc_count = 0;
class_filter = bucx_filter = shop_filter = FALSE;
} else if (!menu_class_present(c)) {
valid_menu_classes[vmc_count++] = (char) c;
/* categorize the new class */
switch (c) {
case 'B':
case 'U':
case 'C': /*FALLTHRU*/
case 'X':
bucx_filter = TRUE;
break;
case 'u':
shop_filter = TRUE;
break;
default:
class_filter = TRUE;
break;
}
}
valid_menu_classes[vmc_count] = '\0';
}
/* query_objlist callback: return TRUE if not uchain */
STATIC_OVL boolean
all_but_uchain(obj)
struct obj *obj;
{
return (boolean) (obj != uchain);
}
/* query_objlist callback: return TRUE */
/*ARGUSED*/
boolean
allow_all(obj)
struct obj *obj UNUSED;
{
return TRUE;
}
boolean
allow_category(obj)
struct obj *obj;
{
/* For coins, if any class filter is specified, accept if coins
* are included regardless of whether either unpaid or BUC-status
* is also specified since player has explicitly requested coins.
* If no class filtering is specified but bless/curse state is,
* coins are either unknown or uncursed based on an option setting.
*/
if (obj->oclass == COIN_CLASS)
return class_filter
? (index(valid_menu_classes, COIN_CLASS) ? TRUE : FALSE)
: shop_filter /* coins are never unpaid, but check anyway */
? (obj->unpaid ? TRUE : FALSE)
: bucx_filter
? (index(valid_menu_classes, iflags.goldX ? 'X' : 'U')
? TRUE : FALSE)
: TRUE; /* catchall: no filters specified, so accept */
if (Role_if(PM_PRIEST) && !obj->bknown)
set_bknown(obj, 1);
/*
* There are three types of filters possible and the first and
* third can have more than one entry:
* 1) object class (armor, potion, &c);
* 2) unpaid shop item;
* 3) bless/curse state (blessed, uncursed, cursed, BUC-unknown).
* When only one type is present, the situation is simple:
* to be accepted, obj's status must match one of the entries.
* When more than one type is present, the obj will now only
* be accepted when it matches one entry of each type.
* So ?!B will accept blessed scrolls or potions, and [u will
* accept unpaid armor. (In 3.4.3, an object was accepted by
* this filter if it met any entry of any type, so ?!B resulted
* in accepting all scrolls and potions regardless of bless/curse
* state plus all blessed non-scroll, non-potion objects.)
*/
/* if class is expected but obj's class is not in the list, reject */
if (class_filter && !index(valid_menu_classes, obj->oclass))
return FALSE;
/* if unpaid is expected and obj isn't unpaid, reject (treat a container
holding any unpaid object as unpaid even if isn't unpaid itself) */
if (shop_filter && !obj->unpaid
&& !(Has_contents(obj) && count_unpaid(obj->cobj) > 0))
return FALSE;
/* check for particular bless/curse state */
if (bucx_filter) {
/* first categorize this object's bless/curse state */
char bucx = !obj->bknown ? 'X'
: obj->blessed ? 'B' : obj->cursed ? 'C' : 'U';
/* if its category is not in the list, reject */
if (!index(valid_menu_classes, bucx))
return FALSE;
}
/* obj didn't fail any of the filter checks, so accept */
return TRUE;
}
#if 0 /* not used */
/* query_objlist callback: return TRUE if valid category (class), no uchain */
STATIC_OVL boolean
allow_cat_no_uchain(obj)
struct obj *obj;
{
if (obj != uchain
&& ((index(valid_menu_classes, 'u') && obj->unpaid)
|| index(valid_menu_classes, obj->oclass)))
return TRUE;
return FALSE;
}
#endif
/* query_objlist callback: return TRUE if valid class and worn */
boolean
is_worn_by_type(otmp)
register struct obj *otmp;
{
return (is_worn(otmp) && allow_category(otmp)) ? TRUE : FALSE;
}
/*
* Have the hero pick things from the ground
* or a monster's inventory if swallowed.
*
* Arg what:
* >0 autopickup
* =0 interactive
* <0 pickup count of something
*
* Returns 1 if tried to pick something up, whether
* or not it succeeded.
*/
int
pickup(what)
int what; /* should be a long */
{
int i, n, res, count, n_tried = 0, n_picked = 0;
menu_item *pick_list = (menu_item *) 0;
boolean autopickup = what > 0;
struct obj **objchain_p;
int traverse_how;
/* we might have arrived here while fainted or sleeping, via
random teleport or levitation timeout; if so, skip check_here
and read_engr_at in addition to bypassing autopickup itself
[probably ought to check whether hero is using a cockatrice
corpse for a pillow here... (also at initial faint/sleep)] */
if (autopickup && multi < 0 && unconscious())
return 0;
if (what < 0) /* pick N of something */
count = -what;
else /* pick anything */
count = 0;
if (!u.uswallow) {
struct trap *ttmp;
/* no auto-pick if no-pick move, nothing there, or in a pool */
if (autopickup && (context.nopick || !OBJ_AT(u.ux, u.uy)
|| (is_pool(u.ux, u.uy) && !Underwater)
|| is_lava(u.ux, u.uy))) {
read_engr_at(u.ux, u.uy);
return 0;
}
/* no pickup if levitating & not on air or water level */
if (!can_reach_floor(TRUE)) {
if ((multi && !context.run) || (autopickup && !flags.pickup)
|| ((ttmp = t_at(u.ux, u.uy)) != 0
&& (uteetering_at_seen_pit(ttmp) || uescaped_shaft(ttmp))))
read_engr_at(u.ux, u.uy);
return 0;
}
/* multi && !context.run means they are in the middle of some other
* action, or possibly paralyzed, sleeping, etc.... and they just
* teleported onto the object. They shouldn't pick it up.
*/
if ((multi && !context.run) || (autopickup && !flags.pickup)) {
check_here(FALSE);
return 0;
}
if (notake(youmonst.data)) {
if (!autopickup)
You("are physically incapable of picking anything up.");
else
check_here(FALSE);
return 0;
}
/* if there's anything here, stop running */
if (OBJ_AT(u.ux, u.uy) && context.run && context.run != 8
&& !context.nopick)
nomul(0);
}
add_valid_menu_class(0); /* reset */
if (!u.uswallow) {
objchain_p = &level.objects[u.ux][u.uy];
traverse_how = BY_NEXTHERE;
} else {
objchain_p = &u.ustuck->minvent;
traverse_how = 0; /* nobj */
}
/*
* Start the actual pickup process. This is split into two main
* sections, the newer menu and the older "traditional" methods.
* Automatic pickup has been split into its own menu-style routine
* to make things less confusing.
*/
if (autopickup) {
n = autopick(*objchain_p, traverse_how, &pick_list);
goto menu_pickup;
}
if (flags.menu_style != MENU_TRADITIONAL || iflags.menu_requested) {
/* use menus exclusively */
traverse_how |= AUTOSELECT_SINGLE
| (flags.sortpack ? INVORDER_SORT : 0);
if (count) { /* looking for N of something */
char qbuf[QBUFSZ];
Sprintf(qbuf, "Pick %d of what?", count);
val_for_n_or_more = count; /* set up callback selector */
n = query_objlist(qbuf, objchain_p, traverse_how,
&pick_list, PICK_ONE, n_or_more);
/* correct counts, if any given */
for (i = 0; i < n; i++)
pick_list[i].count = count;
} else {
n = query_objlist("Pick up what?", objchain_p,
(traverse_how | FEEL_COCKATRICE),
&pick_list, PICK_ANY, all_but_uchain);
}
menu_pickup:
n_tried = n;
for (n_picked = i = 0; i < n; i++) {
res = pickup_object(pick_list[i].item.a_obj, pick_list[i].count,
FALSE);
if (res < 0)
break; /* can't continue */
n_picked += res;
}
if (pick_list)
free((genericptr_t) pick_list);
} else {
/* old style interface */
int ct = 0;
long lcount;
boolean all_of_a_type, selective, bycat;
char oclasses[MAXOCLASSES + 10]; /* +10: room for B,U,C,X plus slop */
struct obj *obj, *obj2;
oclasses[0] = '\0'; /* types to consider (empty for all) */
all_of_a_type = TRUE; /* take all of considered types */
selective = FALSE; /* ask for each item */
/* check for more than one object */
for (obj = *objchain_p; obj; obj = FOLLOW(obj, traverse_how))
ct++;
if (ct == 1 && count) {
/* if only one thing, then pick it */
obj = *objchain_p;
lcount = min(obj->quan, (long) count);
n_tried++;
if (pickup_object(obj, lcount, FALSE) > 0)
n_picked++; /* picked something */
goto end_query;
} else if (ct >= 2) {
int via_menu = 0;
There("are %s objects here.", (ct <= 10) ? "several" : "many");
if (!query_classes(oclasses, &selective, &all_of_a_type,
"pick up", *objchain_p,
(traverse_how & BY_NEXTHERE) ? TRUE : FALSE,
&via_menu)) {
if (!via_menu)
goto pickupdone;
if (selective)
traverse_how |= INVORDER_SORT;
n = query_objlist("Pick up what?", objchain_p, traverse_how,
&pick_list, PICK_ANY,
(via_menu == -2) ? allow_all
: allow_category);
goto menu_pickup;
}
}
bycat = (menu_class_present('B') || menu_class_present('U')
|| menu_class_present('C') || menu_class_present('X'));
for (obj = *objchain_p; obj; obj = obj2) {
obj2 = FOLLOW(obj, traverse_how);
if (bycat ? !allow_category(obj)
: (!selective && oclasses[0]
&& !index(oclasses, obj->oclass)))
continue;
lcount = -1L;
if (!all_of_a_type) {
char qbuf[BUFSZ];
(void) safe_qbuf(qbuf, "Pick up ", "?", obj, doname,
ansimpleoname, something);
switch ((obj->quan < 2L) ? ynaq(qbuf) : ynNaq(qbuf)) {
case 'q':
goto end_query; /* out 2 levels */
case 'n':
continue;
case 'a':
all_of_a_type = TRUE;
if (selective) {
selective = FALSE;
oclasses[0] = obj->oclass;
oclasses[1] = '\0';
}
break;
case '#': /* count was entered */
if (!yn_number)
continue; /* 0 count => No */
lcount = (long) yn_number;
if (lcount > obj->quan)
lcount = obj->quan;
/*FALLTHRU*/
default: /* 'y' */
break;
}
}
if (lcount == -1L)
lcount = obj->quan;
n_tried++;
if ((res = pickup_object(obj, lcount, FALSE)) < 0)
break;
n_picked += res;
}
end_query:
; /* statement required after label */
}
if (!u.uswallow) {
if (hides_under(youmonst.data))
(void) hideunder(&youmonst);
/* position may need updating (invisible hero) */
if (n_picked)
newsym_force(u.ux, u.uy);
/* check if there's anything else here after auto-pickup is done */
if (autopickup)
check_here(n_picked > 0);
}
pickupdone:
add_valid_menu_class(0); /* reset */
return (n_tried > 0);
}
struct autopickup_exception *
check_autopickup_exceptions(obj)
struct obj *obj;
{
/*
* Does the text description of this match an exception?
*/
struct autopickup_exception *ape = apelist;
if (ape) {
char *objdesc = makesingular(doname(obj));
while (ape && !regex_match(objdesc, ape->regex))
ape = ape->next;
}
return ape;
}
boolean
autopick_testobj(otmp, calc_costly)
struct obj *otmp;
boolean calc_costly;
{
struct autopickup_exception *ape;
static boolean costly = FALSE;
const char *otypes = flags.pickup_types;
boolean pickit;
/* calculate 'costly' just once for a given autopickup operation */
if (calc_costly)
costly = (otmp->where == OBJ_FLOOR
&& costly_spot(otmp->ox, otmp->oy));
/* first check: reject if an unpaid item in a shop */
if (costly && !otmp->no_charge)
return FALSE;
/* check for pickup_types */
pickit = (!*otypes || index(otypes, otmp->oclass));
/* check for autopickup exceptions */
ape = check_autopickup_exceptions(otmp);
if (ape)
pickit = ape->grab;
/* pickup_thrown overrides pickup_types and exceptions */
if (!pickit)
pickit = (flags.pickup_thrown && otmp->was_thrown);
return pickit;
}
/*
* Pick from the given list using flags.pickup_types. Return the number
* of items picked (not counts). Create an array that returns pointers
* and counts of the items to be picked up. If the number of items
* picked is zero, the pickup list is left alone. The caller of this
* function must free the pickup list.
*/
STATIC_OVL int
autopick(olist, follow, pick_list)
struct obj *olist; /* the object list */
int follow; /* how to follow the object list */
menu_item **pick_list; /* list of objects and counts to pick up */
{
menu_item *pi; /* pick item */
struct obj *curr;
int n;
boolean check_costly = TRUE;
/* first count the number of eligible items */
for (n = 0, curr = olist; curr; curr = FOLLOW(curr, follow)) {
if (autopick_testobj(curr, check_costly))
++n;
check_costly = FALSE; /* only need to check once per autopickup */
}
if (n) {
*pick_list = pi = (menu_item *) alloc(sizeof (menu_item) * n);
for (n = 0, curr = olist; curr; curr = FOLLOW(curr, follow)) {
if (autopick_testobj(curr, FALSE)) {
pi[n].item.a_obj = curr;
pi[n].count = curr->quan;
n++;
}
}
}
return n;
}
/*
* Put up a menu using the given object list. Only those objects on the
* list that meet the approval of the allow function are displayed. Return
* a count of the number of items selected, as well as an allocated array of
* menu_items, containing pointers to the objects selected and counts. The
* returned counts are guaranteed to be in bounds and non-zero.
*
* Query flags:
* BY_NEXTHERE - Follow object list via nexthere instead of nobj.
* AUTOSELECT_SINGLE - Don't ask if only 1 object qualifies - just
* use it.
* USE_INVLET - Use object's invlet.
* INVORDER_SORT - Use hero's pack order.
* INCLUDE_HERO - Showing engulfer's invent; show hero too.
* SIGNAL_NOMENU - Return -1 rather than 0 if nothing passes "allow".
* SIGNAL_ESCAPE - Return -1 rather than 0 if player uses ESC to
* pick nothing.
* FEEL_COCKATRICE - touch corpse.
*/
int
query_objlist(qstr, olist_p, qflags, pick_list, how, allow)
const char *qstr; /* query string */
struct obj **olist_p; /* the list to pick from */
int qflags; /* options to control the query */
menu_item **pick_list; /* return list of items picked */
int how; /* type of query */
boolean FDECL((*allow), (OBJ_P)); /* allow function */
{
int i, n;
winid win;
struct obj *curr, *last, fake_hero_object, *olist = *olist_p;
char *pack;
anything any;
boolean printed_type_name, first,
sorted = (qflags & INVORDER_SORT) != 0,
engulfer = (qflags & INCLUDE_HERO) != 0,
engulfer_minvent;
unsigned sortflags;
Loot *sortedolist, *srtoli;
*pick_list = (menu_item *) 0;
if (!olist && !engulfer)
return 0;
/* count the number of items allowed */
for (n = 0, last = 0, curr = olist; curr; curr = FOLLOW(curr, qflags))
if ((*allow)(curr)) {
last = curr;
n++;
}
/* can't depend upon 'engulfer' because that's used to indicate whether
hero should be shown as an extra, fake item */
engulfer_minvent = (olist && olist->where == OBJ_MINVENT
&& u.uswallow && olist->ocarry == u.ustuck);
if (engulfer_minvent && n == 1 && olist->owornmask != 0L) {
qflags &= ~AUTOSELECT_SINGLE;
}
if (engulfer) {
++n;
/* don't autoselect swallowed hero if it's the only choice */
qflags &= ~AUTOSELECT_SINGLE;
}
if (n == 0) /* nothing to pick here */
return (qflags & SIGNAL_NOMENU) ? -1 : 0;
if (n == 1 && (qflags & AUTOSELECT_SINGLE)) {
*pick_list = (menu_item *) alloc(sizeof (menu_item));
(*pick_list)->item.a_obj = last;
(*pick_list)->count = last->quan;
return 1;
}
sortflags = (((flags.sortloot == 'f'
|| (flags.sortloot == 'l' && !(qflags & USE_INVLET)))
? SORTLOOT_LOOT
: ((qflags & USE_INVLET) ? SORTLOOT_INVLET : 0))
| (flags.sortpack ? SORTLOOT_PACK : 0)
| ((qflags & FEEL_COCKATRICE) ? SORTLOOT_PETRIFY : 0));
sortedolist = sortloot(&olist, sortflags,
(qflags & BY_NEXTHERE) ? TRUE : FALSE, allow);
win = create_nhwindow(NHW_MENU);
start_menu(win);
any = zeroany;
/*
* Run through the list and add the objects to the menu. If
* INVORDER_SORT is set, we'll run through the list once for
* each type so we can group them. The allow function was
* called by sortloot() and will be called once per item here.
*/
pack = flags.inv_order;
first = TRUE;
do {
printed_type_name = FALSE;
for (srtoli = sortedolist; ((curr = srtoli->obj) != 0); ++srtoli) {
if (sorted && curr->oclass != *pack)
continue;
if ((qflags & FEEL_COCKATRICE) && curr->otyp == CORPSE
&& will_feel_cockatrice(curr, FALSE)) {
destroy_nhwindow(win); /* stop the menu and revert */
(void) look_here(0, FALSE);
unsortloot(&sortedolist);
return 0;
}
if ((*allow)(curr)) {
/* if sorting, print type name (once only) */
if (sorted && !printed_type_name) {
any = zeroany;
add_menu(win, NO_GLYPH, &any, 0, 0, iflags.menu_headings,
let_to_name(*pack, FALSE,
((how != PICK_NONE)
&& iflags.menu_head_objsym)),
MENU_UNSELECTED);
printed_type_name = TRUE;
}
any.a_obj = curr;
add_menu(win, obj_to_glyph(curr, rn2_on_display_rng), &any,
(qflags & USE_INVLET) ? curr->invlet
: (first && curr->oclass == COIN_CLASS) ? '$' : 0,
def_oc_syms[(int) objects[curr->otyp].oc_class].sym,
ATR_NONE, doname_with_price(curr), MENU_UNSELECTED);
first = FALSE;
}
}
pack++;
} while (sorted && *pack);
unsortloot(&sortedolist);
if (engulfer) {
char buf[BUFSZ];
any = zeroany;
if (sorted && n > 1) {
Sprintf(buf, "%s Creatures",
is_animal(u.ustuck->data) ? "Swallowed" : "Engulfed");
add_menu(win, NO_GLYPH, &any, 0, 0, iflags.menu_headings, buf,
MENU_UNSELECTED);
}
fake_hero_object = zeroobj;
fake_hero_object.quan = 1L; /* not strictly necessary... */
any.a_obj = &fake_hero_object;
add_menu(win, mon_to_glyph(&youmonst, rn2_on_display_rng), &any,
/* fake inventory letter, no group accelerator */
CONTAINED_SYM, 0, ATR_NONE, an(self_lookat(buf)),
MENU_UNSELECTED);
}
end_menu(win, qstr);
n = select_menu(win, how, pick_list);
destroy_nhwindow(win);
if (n > 0) {
menu_item *mi;
int k;
/* fix up counts: -1 means no count used => pick all;
if fake_hero_object was picked, discard that choice */
for (i = k = 0, mi = *pick_list; i < n; i++, mi++) {
curr = mi->item.a_obj;
if (curr == &fake_hero_object) {
/* this isn't actually possible; fake item representing
hero is only included for look here (':'), not pickup,
and that's PICK_NONE so we can't get here from there */
You_cant("pick yourself up!");
continue;
}
if (engulfer_minvent && curr->owornmask != 0L) {
You_cant("pick %s up.", ysimple_name(curr));
continue;
}
if (mi->count == -1L || mi->count > curr->quan)
mi->count = curr->quan;
if (k < i)
(*pick_list)[k] = *mi;
++k;
}
if (!k) {
/* fake_hero was only choice so discard whole list */
free((genericptr_t) *pick_list);
*pick_list = 0;
n = 0;
} else if (k < n) {
/* other stuff plus fake_hero; last slot is now unused
(could be more than one if player tried to pick items
worn by engulfer) */
while (n > k) {
--n;
(*pick_list)[n].item = zeroany;
(*pick_list)[n].count = 0L;
}
}
} else if (n < 0) {
/* -1 is used for SIGNAL_NOMENU, so callers don't expect it
to indicate that the player declined to make a choice */
n = (qflags & SIGNAL_ESCAPE) ? -2 : 0;
}