Source:NetHack 3.6.4/src/pickup.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to pickup.c from the source code of NetHack 3.6.4. To link to a particular line, write [[Source:NetHack 3.6.4/src/pickup.c#line12]], for example.

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. /* NetHack 3.6	pickup.c	$NHDT-Date: 1576282488 2019/12/14 00:14:48 $  $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.237 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2012. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. /*
7.  *      Contains code for picking objects up, and container use.
8.  */
9. 
10. #include "hack.h"
11. 
12. #define CONTAINED_SYM '>' /* from invent.c */
13. 
14. STATIC_DCL void FDECL(simple_look, (struct obj *, BOOLEAN_P));
15. STATIC_DCL boolean FDECL(query_classes, (char *, boolean *, boolean *,
16.                                          const char *, struct obj *,
17.                                          BOOLEAN_P, int *));
18. STATIC_DCL boolean FDECL(fatal_corpse_mistake, (struct obj *, BOOLEAN_P));
19. STATIC_DCL void FDECL(check_here, (BOOLEAN_P));
20. STATIC_DCL boolean FDECL(n_or_more, (struct obj *));
21. STATIC_DCL boolean FDECL(all_but_uchain, (struct obj *));
22. #if 0 /* not used */
23. STATIC_DCL boolean FDECL(allow_cat_no_uchain, (struct obj *));
24. #endif
25. STATIC_DCL int FDECL(autopick, (struct obj *, int, menu_item **));
26. STATIC_DCL int FDECL(count_categories, (struct obj *, int));
27. STATIC_DCL int FDECL(delta_cwt, (struct obj *, struct obj *));
28. STATIC_DCL long FDECL(carry_count, (struct obj *, struct obj *, long,
29.                                     BOOLEAN_P, int *, int *));
30. STATIC_DCL int FDECL(lift_object, (struct obj *, struct obj *, long *,
31.                                    BOOLEAN_P));
32. STATIC_DCL boolean FDECL(mbag_explodes, (struct obj *, int));
33. STATIC_DCL long FDECL(boh_loss, (struct obj *container, int));
34. STATIC_PTR int FDECL(in_container, (struct obj *));
35. STATIC_PTR int FDECL(out_container, (struct obj *));
36. STATIC_DCL void FDECL(removed_from_icebox, (struct obj *));
37. STATIC_DCL long FDECL(mbag_item_gone, (int, struct obj *));
38. STATIC_DCL void FDECL(explain_container_prompt, (BOOLEAN_P));
39. STATIC_DCL int FDECL(traditional_loot, (BOOLEAN_P));
40. STATIC_DCL int FDECL(menu_loot, (int, BOOLEAN_P));
41. STATIC_DCL char FDECL(in_or_out_menu, (const char *, struct obj *, BOOLEAN_P,
42.                                        BOOLEAN_P, BOOLEAN_P, BOOLEAN_P));
43. STATIC_DCL boolean FDECL(able_to_loot, (int, int, BOOLEAN_P));
44. STATIC_DCL boolean NDECL(reverse_loot);
45. STATIC_DCL boolean FDECL(mon_beside, (int, int));
46. STATIC_DCL int FDECL(do_loot_cont, (struct obj **, int, int));
47. STATIC_DCL void FDECL(tipcontainer, (struct obj *));
48. 
49. /* define for query_objlist() and autopickup() */
50. #define FOLLOW(curr, flags) \
51.     (((flags) & BY_NEXTHERE) ? (curr)->nexthere : (curr)->nobj)
52. 
53. #define GOLD_WT(n) (((n) + 50L) / 100L)
54. /* if you can figure this out, give yourself a hearty pat on the back... */
55. #define GOLD_CAPACITY(w, n) (((w) * -100L) - ((n) + 50L) - 1L)
56. 
57. /* A variable set in use_container(), to be used by the callback routines
58.    in_container() and out_container() from askchain() and use_container().
59.    Also used by menu_loot() and container_gone(). */
60. static NEARDATA struct obj *current_container;
61. static NEARDATA boolean abort_looting;
62. #define Icebox (current_container->otyp == ICE_BOX)
63. 
64. static const char
65.         moderateloadmsg[] = "You have a little trouble lifting",
66.         nearloadmsg[] = "You have much trouble lifting",
67.         overloadmsg[] = "You have extreme difficulty lifting";
68. 
69. /* BUG: this lets you look at cockatrice corpses while blind without
70.    touching them */
71. /* much simpler version of the look-here code; used by query_classes() */
72. STATIC_OVL void
73. simple_look(otmp, here)
74. struct obj *otmp; /* list of objects */
75. boolean here;     /* flag for type of obj list linkage */
76. {
77.     /* Neither of the first two cases is expected to happen, since
78.      * we're only called after multiple classes of objects have been
79.      * detected, hence multiple objects must be present.
80.      */
81.     if (!otmp) {
82.         impossible("simple_look(null)");
83.     } else if (!(here ? otmp->nexthere : otmp->nobj)) {
84.         pline1(doname(otmp));
85.     } else {
86.         winid tmpwin = create_nhwindow(NHW_MENU);
87. 
88.         putstr(tmpwin, 0, "");
89.         do {
90.             putstr(tmpwin, 0, doname(otmp));
91.             otmp = here ? otmp->nexthere : otmp->nobj;
92.         } while (otmp);
93.         display_nhwindow(tmpwin, TRUE);
94.         destroy_nhwindow(tmpwin);
95.     }
96. }
97. 
98. int
99. collect_obj_classes(ilets, otmp, here, filter, itemcount)
100. char ilets[];
101. register struct obj *otmp;
102. boolean here;
103. boolean FDECL((*filter), (OBJ_P));
104. int *itemcount;
105. {
106.     register int iletct = 0;
107.     register char c;
108. 
109.     *itemcount = 0;
110.     ilets[iletct] = '\0'; /* terminate ilets so that index() will work */
111.     while (otmp) {
112.         c = def_oc_syms[(int) otmp->oclass].sym;
113.         if (!index(ilets, c) && (!filter || (*filter)(otmp)))
114.             ilets[iletct++] = c, ilets[iletct] = '\0';
115.         *itemcount += 1;
116.         otmp = here ? otmp->nexthere : otmp->nobj;
117.     }
118. 
119.     return iletct;
120. }
121. 
122. /*
123.  * Suppose some '?' and '!' objects are present, but '/' objects aren't:
124.  *      "a" picks all items without further prompting;
125.  *      "A" steps through all items, asking one by one;
126.  *      "?" steps through '?' items, asking, and ignores '!' ones;
127.  *      "/" becomes 'A', since no '/' present;
128.  *      "?a" or "a?" picks all '?' without further prompting;
129.  *      "/a" or "a/" becomes 'A' since there aren't any '/'
130.  *          (bug fix:  3.1.0 thru 3.1.3 treated it as "a");
131.  *      "?/a" or "a?/" or "/a?",&c picks all '?' even though no '/'
132.  *          (ie, treated as if it had just been "?a").
133.  */
134. STATIC_OVL boolean
135. query_classes(oclasses, one_at_a_time, everything, action, objs, here,
136.               menu_on_demand)
137. char oclasses[];
138. boolean *one_at_a_time, *everything;
139. const char *action;
140. struct obj *objs;
141. boolean here;
142. int *menu_on_demand;
143. {
144.     char ilets[36], inbuf[BUFSZ] = DUMMY; /* FIXME: hardcoded ilets[] length */
145.     int iletct, oclassct;
146.     boolean not_everything, filtered;
147.     char qbuf[QBUFSZ];
148.     boolean m_seen;
149.     int itemcount, bcnt, ucnt, ccnt, xcnt, ocnt;
150. 
151.     oclasses[oclassct = 0] = '\0';
152.     *one_at_a_time = *everything = m_seen = FALSE;
153.     if (menu_on_demand)
154.         *menu_on_demand = 0;
155.     iletct = collect_obj_classes(ilets, objs, here,
156.                                  (boolean FDECL((*), (OBJ_P))) 0, &itemcount);
157.     if (iletct == 0)
158.         return FALSE;
159. 
160.     if (iletct == 1) {
161.         oclasses[0] = def_char_to_objclass(ilets[0]);
162.         oclasses[1] = '\0';
163.     } else { /* more than one choice available */
164.         /* additional choices */
165.         ilets[iletct++] = ' ';
166.         ilets[iletct++] = 'a';
167.         ilets[iletct++] = 'A';
168.         ilets[iletct++] = (objs == invent ? 'i' : ':');
169.     }
170.     if (itemcount && menu_on_demand)
171.         ilets[iletct++] = 'm';
172.     if (count_unpaid(objs))
173.         ilets[iletct++] = 'u';
174. 
175.     tally_BUCX(objs, here, &bcnt, &ucnt, &ccnt, &xcnt, &ocnt);
176.     if (bcnt)
177.         ilets[iletct++] = 'B';
178.     if (ucnt)
179.         ilets[iletct++] = 'U';
180.     if (ccnt)
181.         ilets[iletct++] = 'C';
182.     if (xcnt)
183.         ilets[iletct++] = 'X';
184.     ilets[iletct] = '\0';
185. 
186.     if (iletct > 1) {
187.         const char *where = 0;
188.         char sym, oc_of_sym, *p;
189. 
190.  ask_again:
191.         oclasses[oclassct = 0] = '\0';
192.         *one_at_a_time = *everything = FALSE;
193.         not_everything = filtered = FALSE;
194.         Sprintf(qbuf, "What kinds of thing do you want to %s? [%s]", action,
195.                 ilets);
196.         getlin(qbuf, inbuf);
197.         if (*inbuf == '\033')
198.             return FALSE;
199. 
200.         for (p = inbuf; (sym = *p++) != 0; ) {
201.             if (sym == ' ')
202.                 continue;
203.             else if (sym == 'A')
204.                 *one_at_a_time = TRUE;
205.             else if (sym == 'a')
206.                 *everything = TRUE;
207.             else if (sym == ':') {
208.                 simple_look(objs, here); /* dumb if objs==invent */
209.                 /* if we just scanned the contents of a container
210.                    then mark it as having known contents */
211.                 if (objs->where == OBJ_CONTAINED)
212.                     objs->ocontainer->cknown = 1;
213.                 goto ask_again;
214.             } else if (sym == 'i') {
215.                 (void) display_inventory((char *) 0, TRUE);
216.                 goto ask_again;
217.             } else if (sym == 'm') {
218.                 m_seen = TRUE;
219.             } else if (index("uBUCX", sym)) {
220.                 add_valid_menu_class(sym); /* 'u' or 'B','U','C',or 'X' */
221.                 filtered = TRUE;
222.             } else {
223.                 oc_of_sym = def_char_to_objclass(sym);
224.                 if (index(ilets, sym)) {
225.                     add_valid_menu_class(oc_of_sym);
226.                     oclasses[oclassct++] = oc_of_sym;
227.                     oclasses[oclassct] = '\0';
228.                 } else {
229.                     if (!where)
230.                         where = !strcmp(action, "pick up") ? "here"
231.                                 : !strcmp(action, "take out") ? "inside" : "";
232.                     if (*where)
233.                         There("are no %c's %s.", sym, where);
234.                     else
235.                         You("have no %c's.", sym);
236.                     not_everything = TRUE;
237.                 }
238.             }
239.         } /* for p:sym in inbuf */
240. 
241.         if (m_seen && menu_on_demand) {
242.             *menu_on_demand = (((*everything || !oclassct) && !filtered)
243.                                ? -2 : -3);
244.             return FALSE;
245.         }
246.         if (!oclassct && (!*everything || not_everything)) {
247.             /* didn't pick anything,
248.                or tried to pick something that's not present */
249.             *one_at_a_time = TRUE; /* force 'A' */
250.             *everything = FALSE;   /* inhibit 'a' */
251.         }
252.     }
253.     return TRUE;
254. }
255. 
256. /* check whether hero is bare-handedly touching a cockatrice corpse */
257. STATIC_OVL boolean
258. fatal_corpse_mistake(obj, remotely)
259. struct obj *obj;
260. boolean remotely;
261. {
262.     if (uarmg || remotely || obj->otyp != CORPSE
263.         || !touch_petrifies(&mons[obj->corpsenm]) || Stone_resistance)
264.         return FALSE;
265. 
266.     if (poly_when_stoned(youmonst.data) && polymon(PM_STONE_GOLEM)) {
267.         display_nhwindow(WIN_MESSAGE, FALSE); /* --More-- */
268.         return FALSE;
269.     }
270. 
271.     pline("Touching %s is a fatal mistake.",
272.           corpse_xname(obj, (const char *) 0, CXN_SINGULAR | CXN_ARTICLE));
273.     instapetrify(killer_xname(obj));
274.     return TRUE;
275. }
276. 
277. /* attempting to manipulate a Rider's corpse triggers its revival */
278. boolean
279. rider_corpse_revival(obj, remotely)
280. struct obj *obj;
281. boolean remotely;
282. {
283.     if (!obj || obj->otyp != CORPSE || !is_rider(&mons[obj->corpsenm]))
284.         return FALSE;
285. 
286.     pline("At your %s, the corpse suddenly moves...",
287.           remotely ? "attempted acquisition" : "touch");
288.     (void) revive_corpse(obj);
289.     exercise(A_WIS, FALSE);
290.     return TRUE;
291. }
292. 
293. /* look at the objects at our location, unless there are too many of them */
294. STATIC_OVL void
295. check_here(picked_some)
296. boolean picked_some;
297. {
298.     register struct obj *obj;
299.     register int ct = 0;
300. 
301.     /* count the objects here */
302.     for (obj = level.objects[u.ux][u.uy]; obj; obj = obj->nexthere) {
303.         if (obj != uchain)
304.             ct++;
305.     }
306. 
307.     /* If there are objects here, take a look. */
308.     if (ct) {
309.         if (context.run)
310.             nomul(0);
311.         flush_screen(1);
312.         (void) look_here(ct, picked_some);
313.     } else {
314.         read_engr_at(u.ux, u.uy);
315.     }
316. }
317. 
318. /* Value set by query_objlist() for n_or_more(). */
319. static long val_for_n_or_more;
320. 
321. /* query_objlist callback: return TRUE if obj's count is >= reference value */
322. STATIC_OVL boolean
323. n_or_more(obj)
324. struct obj *obj;
325. {
326.     if (obj == uchain)
327.         return FALSE;
328.     return (boolean) (obj->quan >= val_for_n_or_more);
329. }
330. 
331. /* list of valid menu classes for query_objlist() and allow_category callback
332.    (with room for all object classes, 'u'npaid, BUCX, and terminator) */
333. static char valid_menu_classes[MAXOCLASSES + 1 + 4 + 1];
334. static boolean class_filter, bucx_filter, shop_filter;
335. 
336. /* check valid_menu_classes[] for an entry; also used by askchain() */
337. boolean
338. menu_class_present(c)
339. int c;
340. {
341.     return (c && index(valid_menu_classes, c)) ? TRUE : FALSE;
342. }
343. 
344. void
345. add_valid_menu_class(c)
346. int c;
347. {
348.     static int vmc_count = 0;
349. 
350.     if (c == 0) { /* reset */
351.         vmc_count = 0;
352.         class_filter = bucx_filter = shop_filter = FALSE;
353.     } else if (!menu_class_present(c)) {
354.         valid_menu_classes[vmc_count++] = (char) c;
355.         /* categorize the new class */
356.         switch (c) {
357.         case 'B':
358.         case 'U':
359.         case 'C': /*FALLTHRU*/
360.         case 'X':
361.             bucx_filter = TRUE;
362.             break;
363.         case 'u':
364.             shop_filter = TRUE;
365.             break;
366.         default:
367.             class_filter = TRUE;
368.             break;
369.         }
370.     }
371.     valid_menu_classes[vmc_count] = '\0';
372. }
373. 
374. /* query_objlist callback: return TRUE if not uchain */
375. STATIC_OVL boolean
376. all_but_uchain(obj)
377. struct obj *obj;
378. {
379.     return (boolean) (obj != uchain);
380. }
381. 
382. /* query_objlist callback: return TRUE */
383. /*ARGUSED*/
384. boolean
385. allow_all(obj)
386. struct obj *obj UNUSED;
387. {
388.     return TRUE;
389. }
390. 
391. boolean
392. allow_category(obj)
393. struct obj *obj;
394. {
395.     /* For coins, if any class filter is specified, accept if coins
396.      * are included regardless of whether either unpaid or BUC-status
397.      * is also specified since player has explicitly requested coins.
398.      * If no class filtering is specified but bless/curse state is,
399.      * coins are either unknown or uncursed based on an option setting.
400.      */
401.     if (obj->oclass == COIN_CLASS)
402.         return class_filter
403.                  ? (index(valid_menu_classes, COIN_CLASS) ? TRUE : FALSE)
404.                  : shop_filter /* coins are never unpaid, but check anyway */
405.                     ? (obj->unpaid ? TRUE : FALSE)
406.                     : bucx_filter
407.                        ? (index(valid_menu_classes, iflags.goldX ? 'X' : 'U')
408.                           ? TRUE : FALSE)
409.                        : TRUE; /* catchall: no filters specified, so accept */
410. 
411.     if (Role_if(PM_PRIEST) && !obj->bknown)
412.         set_bknown(obj, 1);
413. 
414.     /*
415.      * There are three types of filters possible and the first and
416.      * third can have more than one entry:
417.      *  1) object class (armor, potion, &c);
418.      *  2) unpaid shop item;
419.      *  3) bless/curse state (blessed, uncursed, cursed, BUC-unknown).
420.      * When only one type is present, the situation is simple:
421.      * to be accepted, obj's status must match one of the entries.
422.      * When more than one type is present, the obj will now only
423.      * be accepted when it matches one entry of each type.
424.      * So ?!B will accept blessed scrolls or potions, and [u will
425.      * accept unpaid armor.  (In 3.4.3, an object was accepted by
426.      * this filter if it met any entry of any type, so ?!B resulted
427.      * in accepting all scrolls and potions regardless of bless/curse
428.      * state plus all blessed non-scroll, non-potion objects.)
429.      */
430. 
431.     /* if class is expected but obj's class is not in the list, reject */
432.     if (class_filter && !index(valid_menu_classes, obj->oclass))
433.         return FALSE;
434.     /* if unpaid is expected and obj isn't unpaid, reject (treat a container
435.        holding any unpaid object as unpaid even if isn't unpaid itself) */
436.     if (shop_filter && !obj->unpaid
437.         && !(Has_contents(obj) && count_unpaid(obj->cobj) > 0))
438.         return FALSE;
439.     /* check for particular bless/curse state */
440.     if (bucx_filter) {
441.         /* first categorize this object's bless/curse state */
442.         char bucx = !obj->bknown ? 'X'
443.                       : obj->blessed ? 'B' : obj->cursed ? 'C' : 'U';
444. 
445.         /* if its category is not in the list, reject */
446.         if (!index(valid_menu_classes, bucx))
447.             return FALSE;
448.     }
449.     /* obj didn't fail any of the filter checks, so accept */
450.     return TRUE;
451. }
452. 
453. #if 0 /* not used */
454. /* query_objlist callback: return TRUE if valid category (class), no uchain */
455. STATIC_OVL boolean
456. allow_cat_no_uchain(obj)
457. struct obj *obj;
458. {
459.     if (obj != uchain
460.         && ((index(valid_menu_classes, 'u') && obj->unpaid)
461.             || index(valid_menu_classes, obj->oclass)))
462.         return TRUE;
463.     return FALSE;
464. }
465. #endif
466. 
467. /* query_objlist callback: return TRUE if valid class and worn */
468. boolean
469. is_worn_by_type(otmp)
470. register struct obj *otmp;
471. {
472.     return (is_worn(otmp) && allow_category(otmp)) ? TRUE : FALSE;
473. }
474. 
475. /*
476.  * Have the hero pick things from the ground
477.  * or a monster's inventory if swallowed.
478.  *
479.  * Arg what:
480.  *      >0  autopickup
481.  *      =0  interactive
482.  *      <0  pickup count of something
483.  *
484.  * Returns 1 if tried to pick something up, whether
485.  * or not it succeeded.
486.  */
487. int
488. pickup(what)
489. int what; /* should be a long */
490. {
491.     int i, n, res, count, n_tried = 0, n_picked = 0;
492.     menu_item *pick_list = (menu_item *) 0;
493.     boolean autopickup = what > 0;
494.     struct obj **objchain_p;
495.     int traverse_how;
496. 
497.     /* we might have arrived here while fainted or sleeping, via
498.        random teleport or levitation timeout; if so, skip check_here
499.        and read_engr_at in addition to bypassing autopickup itself
500.        [probably ought to check whether hero is using a cockatrice
501.        corpse for a pillow here... (also at initial faint/sleep)] */
502.     if (autopickup && multi < 0 && unconscious())
503.         return 0;
504. 
505.     if (what < 0) /* pick N of something */
506.         count = -what;
507.     else /* pick anything */
508.         count = 0;
509. 
510.     if (!u.uswallow) {
511.         struct trap *ttmp;
512. 
513.         /* no auto-pick if no-pick move, nothing there, or in a pool */
514.         if (autopickup && (context.nopick || !OBJ_AT(u.ux, u.uy)
515.                            || (is_pool(u.ux, u.uy) && !Underwater)
516.                            || is_lava(u.ux, u.uy))) {
517.             read_engr_at(u.ux, u.uy);
518.             return 0;
519.         }
520.         /* no pickup if levitating & not on air or water level */
521.         if (!can_reach_floor(TRUE)) {
522.             if ((multi && !context.run) || (autopickup && !flags.pickup)
523.                 || ((ttmp = t_at(u.ux, u.uy)) != 0
524.                     && (uteetering_at_seen_pit(ttmp) || uescaped_shaft(ttmp))))
525.                 read_engr_at(u.ux, u.uy);
526.             return 0;
527.         }
528.         /* multi && !context.run means they are in the middle of some other
529.          * action, or possibly paralyzed, sleeping, etc.... and they just
530.          * teleported onto the object.  They shouldn't pick it up.
531.          */
532.         if ((multi && !context.run) || (autopickup && !flags.pickup)) {
533.             check_here(FALSE);
534.             return 0;
535.         }
536.         if (notake(youmonst.data)) {
537.             if (!autopickup)
538.                 You("are physically incapable of picking anything up.");
539.             else
540.                 check_here(FALSE);
541.             return 0;
542.         }
543. 
544.         /* if there's anything here, stop running */
545.         if (OBJ_AT(u.ux, u.uy) && context.run && context.run != 8
546.             && !context.nopick)
547.             nomul(0);
548.     }
549. 
550.     add_valid_menu_class(0); /* reset */
551.     if (!u.uswallow) {
552.         objchain_p = &level.objects[u.ux][u.uy];
553.         traverse_how = BY_NEXTHERE;
554.     } else {
555.         objchain_p = &u.ustuck->minvent;
556.         traverse_how = 0; /* nobj */
557.     }
558.     /*
559.      * Start the actual pickup process.  This is split into two main
560.      * sections, the newer menu and the older "traditional" methods.
561.      * Automatic pickup has been split into its own menu-style routine
562.      * to make things less confusing.
563.      */
564.     if (autopickup) {
565.         n = autopick(*objchain_p, traverse_how, &pick_list);
566.         goto menu_pickup;
567.     }
568. 
569.     if (flags.menu_style != MENU_TRADITIONAL || iflags.menu_requested) {
570.         /* use menus exclusively */
571.         traverse_how |= AUTOSELECT_SINGLE
572.                         | (flags.sortpack ? INVORDER_SORT : 0);
573.         if (count) { /* looking for N of something */
574.             char qbuf[QBUFSZ];
575. 
576.             Sprintf(qbuf, "Pick %d of what?", count);
577.             val_for_n_or_more = count; /* set up callback selector */
578.             n = query_objlist(qbuf, objchain_p, traverse_how,
579.                               &pick_list, PICK_ONE, n_or_more);
580.             /* correct counts, if any given */
581.             for (i = 0; i < n; i++)
582.                 pick_list[i].count = count;
583.         } else {
584.             n = query_objlist("Pick up what?", objchain_p,
585.                               (traverse_how | FEEL_COCKATRICE),
586.                               &pick_list, PICK_ANY, all_but_uchain);
587.         }
588. 
589.  menu_pickup:
590.         n_tried = n;
591.         for (n_picked = i = 0; i < n; i++) {
592.             res = pickup_object(pick_list[i].item.a_obj, pick_list[i].count,
593.                                 FALSE);
594.             if (res < 0)
595.                 break; /* can't continue */
596.             n_picked += res;
597.         }
598.         if (pick_list)
599.             free((genericptr_t) pick_list);
600. 
601.     } else {
602.         /* old style interface */
603.         int ct = 0;
604.         long lcount;
605.         boolean all_of_a_type, selective, bycat;
606.         char oclasses[MAXOCLASSES + 10]; /* +10: room for B,U,C,X plus slop */
607.         struct obj *obj, *obj2;
608. 
609.         oclasses[0] = '\0';   /* types to consider (empty for all) */
610.         all_of_a_type = TRUE; /* take all of considered types */
611.         selective = FALSE;    /* ask for each item */
612. 
613.         /* check for more than one object */
614.         for (obj = *objchain_p; obj; obj = FOLLOW(obj, traverse_how))
615.             ct++;
616. 
617.         if (ct == 1 && count) {
618.             /* if only one thing, then pick it */
619.             obj = *objchain_p;
620.             lcount = min(obj->quan, (long) count);
621.             n_tried++;
622.             if (pickup_object(obj, lcount, FALSE) > 0)
623.                 n_picked++; /* picked something */
624.             goto end_query;
625. 
626.         } else if (ct >= 2) {
627.             int via_menu = 0;
628. 
629.             There("are %s objects here.", (ct <= 10) ? "several" : "many");
630.             if (!query_classes(oclasses, &selective, &all_of_a_type,
631.                                "pick up", *objchain_p,
632.                                (traverse_how & BY_NEXTHERE) ? TRUE : FALSE,
633.                                &via_menu)) {
634.                 if (!via_menu)
635.                     goto pickupdone;
636.                 if (selective)
637.                     traverse_how |= INVORDER_SORT;
638.                 n = query_objlist("Pick up what?", objchain_p, traverse_how,
639.                                   &pick_list, PICK_ANY,
640.                                   (via_menu == -2) ? allow_all
641.                                                    : allow_category);
642.                 goto menu_pickup;
643.             }
644.         }
645.         bycat = (menu_class_present('B') || menu_class_present('U')
646.                  || menu_class_present('C') || menu_class_present('X'));
647. 
648.         for (obj = *objchain_p; obj; obj = obj2) {
649.             obj2 = FOLLOW(obj, traverse_how);
650.             if (bycat ? !allow_category(obj)
651.                       : (!selective && oclasses[0]
652.                          && !index(oclasses, obj->oclass)))
653.                 continue;
654. 
655.             lcount = -1L;
656.             if (!all_of_a_type) {
657.                 char qbuf[BUFSZ];
658. 
659.                 (void) safe_qbuf(qbuf, "Pick up ", "?", obj, doname,
660.                                  ansimpleoname, something);
661.                 switch ((obj->quan < 2L) ? ynaq(qbuf) : ynNaq(qbuf)) {
662.                 case 'q':
663.                     goto end_query; /* out 2 levels */
664.                 case 'n':
665.                     continue;
666.                 case 'a':
667.                     all_of_a_type = TRUE;
668.                     if (selective) {
669.                         selective = FALSE;
670.                         oclasses[0] = obj->oclass;
671.                         oclasses[1] = '\0';
672.                     }
673.                     break;
674.                 case '#': /* count was entered */
675.                     if (!yn_number)
676.                         continue; /* 0 count => No */
677.                     lcount = (long) yn_number;
678.                     if (lcount > obj->quan)
679.                         lcount = obj->quan;
680.                     /*FALLTHRU*/
681.                 default: /* 'y' */
682.                     break;
683.                 }
684.             }
685.             if (lcount == -1L)
686.                 lcount = obj->quan;
687. 
688.             n_tried++;
689.             if ((res = pickup_object(obj, lcount, FALSE)) < 0)
690.                 break;
691.             n_picked += res;
692.         }
693.  end_query:
694.         ; /* statement required after label */
695.     }
696. 
697.     if (!u.uswallow) {
698.         if (hides_under(youmonst.data))
699.             (void) hideunder(&youmonst);
700. 
701.         /* position may need updating (invisible hero) */
702.         if (n_picked)
703.             newsym_force(u.ux, u.uy);
704. 
705.         /* check if there's anything else here after auto-pickup is done */
706.         if (autopickup)
707.             check_here(n_picked > 0);
708.     }
709.  pickupdone:
710.     add_valid_menu_class(0); /* reset */
711.     return (n_tried > 0);
712. }
713. 
714. struct autopickup_exception *
715. check_autopickup_exceptions(obj)
716. struct obj *obj;
717. {
718.     /*
719.      *  Does the text description of this match an exception?
720.      */
721.     struct autopickup_exception *ape = apelist;
722. 
723.     if (ape) {
724.         char *objdesc = makesingular(doname(obj));
725. 
726.         while (ape && !regex_match(objdesc, ape->regex))
727.             ape = ape->next;
728.     }
729.     return ape;
730. }
731. 
732. boolean
733. autopick_testobj(otmp, calc_costly)
734. struct obj *otmp;
735. boolean calc_costly;
736. {
737.     struct autopickup_exception *ape;
738.     static boolean costly = FALSE;
739.     const char *otypes = flags.pickup_types;
740.     boolean pickit;
741. 
742.     /* calculate 'costly' just once for a given autopickup operation */
743.     if (calc_costly)
744.         costly = (otmp->where == OBJ_FLOOR
745.                   && costly_spot(otmp->ox, otmp->oy));
746. 
747.     /* first check: reject if an unpaid item in a shop */
748.     if (costly && !otmp->no_charge)
749.         return FALSE;
750. 
751.     /* check for pickup_types */
752.     pickit = (!*otypes || index(otypes, otmp->oclass));
753. 
754.     /* check for autopickup exceptions */
755.     ape = check_autopickup_exceptions(otmp);
756.     if (ape)
757.         pickit = ape->grab;
758. 
759.     /* pickup_thrown overrides pickup_types and exceptions */
760.     if (!pickit)
761.         pickit = (flags.pickup_thrown && otmp->was_thrown);
762.     return pickit;
763. }
764. 
765. /*
766.  * Pick from the given list using flags.pickup_types.  Return the number
767.  * of items picked (not counts).  Create an array that returns pointers
768.  * and counts of the items to be picked up.  If the number of items
769.  * picked is zero, the pickup list is left alone.  The caller of this
770.  * function must free the pickup list.
771.  */
772. STATIC_OVL int
773. autopick(olist, follow, pick_list)
774. struct obj *olist;     /* the object list */
775. int follow;            /* how to follow the object list */
776. menu_item **pick_list; /* list of objects and counts to pick up */
777. {
778.     menu_item *pi; /* pick item */
779.     struct obj *curr;
780.     int n;
781.     boolean check_costly = TRUE;
782. 
783.     /* first count the number of eligible items */
784.     for (n = 0, curr = olist; curr; curr = FOLLOW(curr, follow)) {
785.         if (autopick_testobj(curr, check_costly))
786.             ++n;
787.         check_costly = FALSE; /* only need to check once per autopickup */
788.     }
789. 
790.     if (n) {
791.         *pick_list = pi = (menu_item *) alloc(sizeof (menu_item) * n);
792.         for (n = 0, curr = olist; curr; curr = FOLLOW(curr, follow)) {
793.             if (autopick_testobj(curr, FALSE)) {
794.                 pi[n].item.a_obj = curr;
795.                 pi[n].count = curr->quan;
796.                 n++;
797.             }
798.         }
799.     }
800.     return n;
801. }
802. 
803. /*
804.  * Put up a menu using the given object list.  Only those objects on the
805.  * list that meet the approval of the allow function are displayed.  Return
806.  * a count of the number of items selected, as well as an allocated array of
807.  * menu_items, containing pointers to the objects selected and counts.  The
808.  * returned counts are guaranteed to be in bounds and non-zero.
809.  *
810.  * Query flags:
811.  *      BY_NEXTHERE       - Follow object list via nexthere instead of nobj.
812.  *      AUTOSELECT_SINGLE - Don't ask if only 1 object qualifies - just
813.  *                          use it.
814.  *      USE_INVLET        - Use object's invlet.
815.  *      INVORDER_SORT     - Use hero's pack order.
816.  *      INCLUDE_HERO      - Showing engulfer's invent; show hero too.
817.  *      SIGNAL_NOMENU     - Return -1 rather than 0 if nothing passes "allow".
818.  *      SIGNAL_ESCAPE     - Return -1 rather than 0 if player uses ESC to
819.  *                          pick nothing.
820.  *      FEEL_COCKATRICE   - touch corpse.
821.  */
822. int
823. query_objlist(qstr, olist_p, qflags, pick_list, how, allow)
824. const char *qstr;                 /* query string */
825. struct obj **olist_p;             /* the list to pick from */
826. int qflags;                       /* options to control the query */
827. menu_item **pick_list;            /* return list of items picked */
828. int how;                          /* type of query */
829. boolean FDECL((*allow), (OBJ_P)); /* allow function */
830. {
831.     int i, n;
832.     winid win;
833.     struct obj *curr, *last, fake_hero_object, *olist = *olist_p;
834.     char *pack;
835.     anything any;
836.     boolean printed_type_name, first,
837.             sorted = (qflags & INVORDER_SORT) != 0,
838.             engulfer = (qflags & INCLUDE_HERO) != 0,
839.             engulfer_minvent;
840.     unsigned sortflags;
841.     Loot *sortedolist, *srtoli;
842. 
843.     *pick_list = (menu_item *) 0;
844.     if (!olist && !engulfer)
845.         return 0;
846. 
847.     /* count the number of items allowed */
848.     for (n = 0, last = 0, curr = olist; curr; curr = FOLLOW(curr, qflags))
849.         if ((*allow)(curr)) {
850.             last = curr;
851.             n++;
852.         }
853.     /* can't depend upon 'engulfer' because that's used to indicate whether
854.        hero should be shown as an extra, fake item */
855.     engulfer_minvent = (olist && olist->where == OBJ_MINVENT
856.                         && u.uswallow && olist->ocarry == u.ustuck);
857.     if (engulfer_minvent && n == 1 && olist->owornmask != 0L) {
858.         qflags &= ~AUTOSELECT_SINGLE;
859.     }
860.     if (engulfer) {
861.         ++n;
862.         /* don't autoselect swallowed hero if it's the only choice */
863.         qflags &= ~AUTOSELECT_SINGLE;
864.     }
865. 
866.     if (n == 0) /* nothing to pick here */
867.         return (qflags & SIGNAL_NOMENU) ? -1 : 0;
868. 
869.     if (n == 1 && (qflags & AUTOSELECT_SINGLE)) {
870.         *pick_list = (menu_item *) alloc(sizeof (menu_item));
871.         (*pick_list)->item.a_obj = last;
872.         (*pick_list)->count = last->quan;
873.         return 1;
874.     }
875. 
876.     sortflags = (((flags.sortloot == 'f'
877.                    || (flags.sortloot == 'l' && !(qflags & USE_INVLET)))
878.                   ? SORTLOOT_LOOT
879.                   : ((qflags & USE_INVLET) ? SORTLOOT_INVLET : 0))
880.                  | (flags.sortpack ? SORTLOOT_PACK : 0)
881.                  | ((qflags & FEEL_COCKATRICE) ? SORTLOOT_PETRIFY : 0));
882.     sortedolist = sortloot(&olist, sortflags,
883.                            (qflags & BY_NEXTHERE) ? TRUE : FALSE, allow);
884. 
885.     win = create_nhwindow(NHW_MENU);
886.     start_menu(win);
887.     any = zeroany;
888.     /*
889.      * Run through the list and add the objects to the menu.  If
890.      * INVORDER_SORT is set, we'll run through the list once for
891.      * each type so we can group them.  The allow function was
892.      * called by sortloot() and will be called once per item here.
893.      */
894.     pack = flags.inv_order;
895.     first = TRUE;
896.     do {
897.         printed_type_name = FALSE;
898.         for (srtoli = sortedolist; ((curr = srtoli->obj) != 0); ++srtoli) {
899.             if (sorted && curr->oclass != *pack)
900.                 continue;
901.             if ((qflags & FEEL_COCKATRICE) && curr->otyp == CORPSE
902.                 && will_feel_cockatrice(curr, FALSE)) {
903.                 destroy_nhwindow(win); /* stop the menu and revert */
904.                 (void) look_here(0, FALSE);
905.                 unsortloot(&sortedolist);
906.                 return 0;
907.             }
908.             if ((*allow)(curr)) {
909.                 /* if sorting, print type name (once only) */
910.                 if (sorted && !printed_type_name) {
911.                     any = zeroany;
912.                     add_menu(win, NO_GLYPH, &any, 0, 0, iflags.menu_headings,
913.                              let_to_name(*pack, FALSE,
914.                                          ((how != PICK_NONE)
915.                                           && iflags.menu_head_objsym)),
916.                              MENU_UNSELECTED);
917.                     printed_type_name = TRUE;
918.                 }
919. 
920.                 any.a_obj = curr;
921.                 add_menu(win, obj_to_glyph(curr, rn2_on_display_rng), &any,
922.                          (qflags & USE_INVLET) ? curr->invlet
923.                            : (first && curr->oclass == COIN_CLASS) ? '$' : 0,
924.                          def_oc_syms[(int) objects[curr->otyp].oc_class].sym,
925.                          ATR_NONE, doname_with_price(curr), MENU_UNSELECTED);
926.                 first = FALSE;
927.             }
928.         }
929.         pack++;
930.     } while (sorted && *pack);
931.     unsortloot(&sortedolist);
932. 
933.     if (engulfer) {
934.         char buf[BUFSZ];
935. 
936.         any = zeroany;
937.         if (sorted && n > 1) {
938.             Sprintf(buf, "%s Creatures",
939.                     is_animal(u.ustuck->data) ? "Swallowed" : "Engulfed");
940.             add_menu(win, NO_GLYPH, &any, 0, 0, iflags.menu_headings, buf,
941.                      MENU_UNSELECTED);
942.         }
943.         fake_hero_object = zeroobj;
944.         fake_hero_object.quan = 1L; /* not strictly necessary... */
945.         any.a_obj = &fake_hero_object;
946.         add_menu(win, mon_to_glyph(&youmonst, rn2_on_display_rng), &any,
947.                  /* fake inventory letter, no group accelerator */
948.                  CONTAINED_SYM, 0, ATR_NONE, an(self_lookat(buf)),
949.                  MENU_UNSELECTED);
950.     }
951. 
952.     end_menu(win, qstr);
953.     n = select_menu(win, how, pick_list);
954.     destroy_nhwindow(win);
955. 
956.     if (n > 0) {
957.         menu_item *mi;
958.         int k;
959. 
960.         /* fix up counts:  -1 means no count used => pick all;
961.            if fake_hero_object was picked, discard that choice */
962.         for (i = k = 0, mi = *pick_list; i < n; i++, mi++) {
963.             curr = mi->item.a_obj;
964.             if (curr == &fake_hero_object) {
965.                 /* this isn't actually possible; fake item representing
966.                    hero is only included for look here (':'), not pickup,
967.                    and that's PICK_NONE so we can't get here from there */
968.                 You_cant("pick yourself up!");
969.                 continue;
970.             }
971.             if (engulfer_minvent && curr->owornmask != 0L) {
972.                 You_cant("pick %s up.", ysimple_name(curr));
973.                 continue;
974.             }
975.             if (mi->count == -1L || mi->count > curr->quan)
976.                 mi->count = curr->quan;
977.             if (k < i)
978.                 (*pick_list)[k] = *mi;
979.             ++k;
980.         }
981.         if (!k) {
982.             /* fake_hero was only choice so discard whole list */
983.             free((genericptr_t) *pick_list);
984.             *pick_list = 0;
985.             n = 0;
986.         } else if (k < n) {
987.             /* other stuff plus fake_hero; last slot is now unused
988.                (could be more than one if player tried to pick items
989.                worn by engulfer) */
990.             while (n > k) {
991.                 --n;
992.                 (*pick_list)[n].item = zeroany;
993.                 (*pick_list)[n].count = 0L;
994.             }
995.         }
996.     } else if (n < 0) {
997.         /* -1 is used for SIGNAL_NOMENU, so callers don't expect it
998.            to indicate that the player declined to make a choice */
999.         n = (qflags & SIGNAL_ESCAPE) ? -2 : 0;
1000.     }
1001.     return n;
1002. }
1003. 
1004. /*
1005.  * allow menu-based category (class) selection (for Drop,take off etc.)
1006.  *
1007.  */
1008. int
1009. query_category(qstr, olist, qflags, pick_list, how)
1010. const char *qstr;      /* query string */
1011. struct obj *olist;     /* the list to pick from */
1012. int qflags;            /* behaviour modification flags */
1013. menu_item **pick_list; /* return list of items picked */
1014. int how;               /* type of query */
1015. {
1016.     int n;
1017.     winid win;
1018.     struct obj *curr;
1019.     char *pack;
1020.     anything any;
1021.     boolean collected_type_name;
1022.     char invlet;
1023.     int ccount;
1024.     boolean FDECL((*ofilter), (OBJ_P)) = (boolean FDECL((*), (OBJ_P))) 0;
1025.     boolean do_unpaid = FALSE;
1026.     boolean do_blessed = FALSE, do_cursed = FALSE, do_uncursed = FALSE,
1027.             do_buc_unknown = FALSE;
1028.     int num_buc_types = 0;
1029. 
1030.     *pick_list = (menu_item *) 0;
1031.     if (!olist)
1032.         return 0;
1033.     if ((qflags & UNPAID_TYPES) && count_unpaid(olist))
1034.         do_unpaid = TRUE;
1035.     if (qflags & WORN_TYPES)
1036.         ofilter = is_worn;
1037.     if ((qflags & BUC_BLESSED) && count_buc(olist, BUC_BLESSED, ofilter)) {
1038.         do_blessed = TRUE;
1039.         num_buc_types++;
1040.     }
1041.     if ((qflags & BUC_CURSED) && count_buc(olist, BUC_CURSED, ofilter)) {
1042.         do_cursed = TRUE;
1043.         num_buc_types++;
1044.     }
1045.     if ((qflags & BUC_UNCURSED) && count_buc(olist, BUC_UNCURSED, ofilter)) {
1046.         do_uncursed = TRUE;
1047.         num_buc_types++;
1048.     }
1049.     if ((qflags & BUC_UNKNOWN) && count_buc(olist, BUC_UNKNOWN, ofilter)) {
1050.         do_buc_unknown = TRUE;
1051.         num_buc_types++;
1052.     }
1053. 
1054.     ccount = count_categories(olist, qflags);
1055.     /* no point in actually showing a menu for a single category */
1056.     if (ccount == 1 && !do_unpaid && num_buc_types <= 1
1057.         && !(qflags & BILLED_TYPES)) {
1058.         for (curr = olist; curr; curr = FOLLOW(curr, qflags)) {
1059.             if (ofilter && !(*ofilter)(curr))
1060.                 continue;
1061.             break;
1062.         }
1063.         if (curr) {
1064.             *pick_list = (menu_item *) alloc(sizeof(menu_item));
1065.             (*pick_list)->item.a_int = curr->oclass;
1066.             n = 1;
1067.         } else {
1068.             debugpline0("query_category: no single object match");
1069.             n = 0;
1070.         }
1071.         /* early return is ok; there's no temp window yet */
1072.         return n;
1073.     }
1074. 
1075.     win = create_nhwindow(NHW_MENU);
1076.     start_menu(win);
1077.     pack = flags.inv_order;
1078. 
1079.     if (qflags & CHOOSE_ALL) {
1080.         invlet = 'A';
1081.         any = zeroany;
1082.         any.a_int = 'A';
1083.         add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
1084.                  (qflags & WORN_TYPES) ? "Auto-select every item being worn"
1085.                                        : "Auto-select every item",
1086.                  MENU_UNSELECTED);
1087. 
1088.         any = zeroany;
1089.         add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE, "", MENU_UNSELECTED);
1090.     }
1091. 
1092.     if ((qflags & ALL_TYPES) && (ccount > 1)) {
1093.         invlet = 'a';
1094.         any = zeroany;
1095.         any.a_int = ALL_TYPES_SELECTED;
1096.         add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
1097.                  (qflags & WORN_TYPES) ? "All worn types" : "All types",
1098.                  MENU_UNSELECTED);
1099.         invlet = 'b';
1100.     } else
1101.         invlet = 'a';
1102.     do {
1103.         collected_type_name = FALSE;
1104.         for (curr = olist; curr; curr = FOLLOW(curr, qflags)) {
1105.             if (curr->oclass == *pack) {
1106.                 if (ofilter && !(*ofilter)(curr))
1107.                     continue;
1108.                 if (!collected_type_name) {
1109.                     any = zeroany;
1110.                     any.a_int = curr->oclass;
1111.                     add_menu(
1112.                         win, NO_GLYPH, &any, invlet++,
1113.                         def_oc_syms[(int) objects[curr->otyp].oc_class].sym,
1114.                         ATR_NONE, let_to_name(*pack, FALSE,
1115.                                               (how != PICK_NONE)
1116.                                                   && iflags.menu_head_objsym),
1117.                         MENU_UNSELECTED);
1118.                     collected_type_name = TRUE;
1119.                 }
1120.             }
1121.         }
1122.         pack++;
1123.         if (invlet >= 'u') {
1124.             impossible("query_category: too many categories");
1125.             n = 0;
1126.             goto query_done;
1127.         }
1128.     } while (*pack);
1129. 
1130.     if (do_unpaid || (qflags & BILLED_TYPES) || do_blessed || do_cursed
1131.         || do_uncursed || do_buc_unknown) {
1132.         any = zeroany;
1133.         add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE, "", MENU_UNSELECTED);
1134.     }
1135. 
1136.     /* unpaid items if there are any */
1137.     if (do_unpaid) {
1138.         invlet = 'u';
1139.         any = zeroany;
1140.         any.a_int = 'u';
1141.         add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE, "Unpaid items",
1142.                  MENU_UNSELECTED);
1143.     }
1144.     /* billed items: checked by caller, so always include if BILLED_TYPES */
1145.     if (qflags & BILLED_TYPES) {
1146.         invlet = 'x';
1147.         any = zeroany;
1148.         any.a_int = 'x';
1149.         add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
1150.                  "Unpaid items already used up", MENU_UNSELECTED);
1151.     }
1152. 
1153.     /* items with b/u/c/unknown if there are any;
1154.        this cluster of menu entries is in alphabetical order,
1155.        reversing the usual sequence of 'U' and 'C' in BUCX */
1156.     if (do_blessed) {
1157.         invlet = 'B';
1158.         any = zeroany;
1159.         any.a_int = 'B';
1160.         add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
1161.                  "Items known to be Blessed", MENU_UNSELECTED);
1162.     }
1163.     if (do_cursed) {
1164.         invlet = 'C';
1165.         any = zeroany;
1166.         any.a_int = 'C';
1167.         add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
1168.                  "Items known to be Cursed", MENU_UNSELECTED);
1169.     }
1170.     if (do_uncursed) {
1171.         invlet = 'U';
1172.         any = zeroany;
1173.         any.a_int = 'U';
1174.         add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
1175.                  "Items known to be Uncursed", MENU_UNSELECTED);
1176.     }
1177.     if (do_buc_unknown) {
1178.         invlet = 'X';
1179.         any = zeroany;
1180.         any.a_int = 'X';
1181.         add_menu(win, NO_GLYPH, &any, invlet, 0, ATR_NONE,
1182.                  "Items of unknown Bless/Curse status", MENU_UNSELECTED);
1183.     }
1184.     end_menu(win, qstr);
1185.     n = select_menu(win, how, pick_list);
1186.  query_done:
1187.     destroy_nhwindow(win);
1188.     if (n < 0)
1189.         n = 0; /* caller's don't expect -1 */
1190.     return n;
1191. }
1192. 
1193. STATIC_OVL int
1194. count_categories(olist, qflags)
1195. struct obj *olist;
1196. int qflags;
1197. {
1198.     char *pack;
1199.     boolean counted_category;
1200.     int ccount = 0;
1201.     struct obj *curr;
1202. 
1203.     pack = flags.inv_order;
1204.     do {
1205.         counted_category = FALSE;
1206.         for (curr = olist; curr; curr = FOLLOW(curr, qflags)) {
1207.             if (curr->oclass == *pack) {
1208.                 if ((qflags & WORN_TYPES)
1209.                     && !(curr->owornmask & (W_ARMOR | W_ACCESSORY
1210.                                             | W_WEAPONS)))
1211.                     continue;
1212.                 if (!counted_category) {
1213.                     ccount++;
1214.                     counted_category = TRUE;
1215.                 }
1216.             }
1217.         }
1218.         pack++;
1219.     } while (*pack);
1220.     return ccount;
1221. }
1222. 
1223. /*
1224.  *  How much the weight of the given container will change when the given
1225.  *  object is removed from it.  Use before and after weight amounts rather
1226.  *  than trying to match the calculation used by weight() in mkobj.c.
1227.  */
1228. STATIC_OVL int
1229. delta_cwt(container, obj)
1230. struct obj *container, *obj;
1231. {
1232.     struct obj **prev;
1233.     int owt, nwt;
1234. 
1235.     if (container->otyp != BAG_OF_HOLDING)
1236.         return obj->owt;
1237. 
1238.     owt = nwt = container->owt;
1239.     /* find the object so that we can remove it */
1240.     for (prev = &container->cobj; *prev; prev = &(*prev)->nobj)
1241.         if (*prev == obj)
1242.             break;
1243.     if (!*prev) {
1244.         panic("delta_cwt: obj not inside container?");
1245.     } else {
1246.         /* temporarily remove the object and calculate resulting weight */
1247.         *prev = obj->nobj;
1248.         nwt = weight(container);
1249.         *prev = obj; /* put the object back; obj->nobj is still valid */
1250.     }
1251.     return owt - nwt;
1252. }
1253. 
1254. /* could we carry `obj'? if not, could we carry some of it/them? */
1255. STATIC_OVL long
1256. carry_count(obj, container, count, telekinesis, wt_before, wt_after)
1257. struct obj *obj, *container; /* object to pick up, bag it's coming out of */
1258. long count;
1259. boolean telekinesis;
1260. int *wt_before, *wt_after;
1261. {
1262.     boolean adjust_wt = container && carried(container),
1263.             is_gold = obj->oclass == COIN_CLASS;
1264.     int wt, iw, ow, oow;
1265.     long qq, savequan, umoney;
1266.     unsigned saveowt;
1267.     const char *verb, *prefx1, *prefx2, *suffx;
1268.     char obj_nambuf[BUFSZ], where[BUFSZ];
1269. 
1270.     savequan = obj->quan;
1271.     saveowt = obj->owt;
1272.     umoney = money_cnt(invent);
1273.     iw = max_capacity();
1274. 
1275.     if (count != savequan) {
1276.         obj->quan = count;
1277.         obj->owt = (unsigned) weight(obj);
1278.     }
1279.     wt = iw + (int) obj->owt;
1280.     if (adjust_wt)
1281.         wt -= delta_cwt(container, obj);
1282.     /* This will go with silver+copper & new gold weight */
1283.     if (is_gold) /* merged gold might affect cumulative weight */
1284.         wt -= (GOLD_WT(umoney) + GOLD_WT(count) - GOLD_WT(umoney + count));
1285.     if (count != savequan) {
1286.         obj->quan = savequan;
1287.         obj->owt = saveowt;
1288.     }
1289.     *wt_before = iw;
1290.     *wt_after = wt;
1291. 
1292.     if (wt < 0)
1293.         return count;
1294. 
1295.     /* see how many we can lift */
1296.     if (is_gold) {
1297.         iw -= (int) GOLD_WT(umoney);
1298.         if (!adjust_wt) {
1299.             qq = GOLD_CAPACITY((long) iw, umoney);
1300.         } else {
1301.             oow = 0;
1302.             qq = 50L - (umoney % 100L) - 1L;
1303.             if (qq < 0L)
1304.                 qq += 100L;
1305.             for (; qq <= count; qq += 100L) {
1306.                 obj->quan = qq;
1307.                 obj->owt = (unsigned) GOLD_WT(qq);
1308.                 ow = (int) GOLD_WT(umoney + qq);
1309.                 ow -= delta_cwt(container, obj);
1310.                 if (iw + ow >= 0)
1311.                     break;
1312.                 oow = ow;
1313.             }
1314.             iw -= oow;
1315.             qq -= 100L;
1316.         }
1317.         if (qq < 0L)
1318.             qq = 0L;
1319.         else if (qq > count)
1320.             qq = count;
1321.         wt = iw + (int) GOLD_WT(umoney + qq);
1322.     } else if (count > 1 || count < obj->quan) {
1323.         /*
1324.          * Ugh. Calc num to lift by changing the quan of of the
1325.          * object and calling weight.
1326.          *
1327.          * This works for containers only because containers
1328.          * don't merge.  -dean
1329.          */
1330.         for (qq = 1L; qq <= count; qq++) {
1331.             obj->quan = qq;
1332.             obj->owt = (unsigned) (ow = weight(obj));
1333.             if (adjust_wt)
1334.                 ow -= delta_cwt(container, obj);
1335.             if (iw + ow >= 0)
1336.                 break;
1337.             wt = iw + ow;
1338.         }
1339.         --qq;
1340.     } else {
1341.         /* there's only one, and we can't lift it */
1342.         qq = 0L;
1343.     }
1344.     obj->quan = savequan;
1345.     obj->owt = saveowt;
1346. 
1347.     if (qq < count) {
1348.         /* some message will be given */
1349.         Strcpy(obj_nambuf, doname(obj));
1350.         if (container) {
1351.             Sprintf(where, "in %s", the(xname(container)));
1352.             verb = "carry";
1353.         } else {
1354.             Strcpy(where, "lying here");
1355.             verb = telekinesis ? "acquire" : "lift";
1356.         }
1357.     } else {
1358.         /* lint suppression */
1359.         *obj_nambuf = *where = '\0';
1360.         verb = "";
1361.     }
1362.     /* we can carry qq of them */
1363.     if (qq > 0) {
1364.         if (qq < count)
1365.             You("can only %s %s of the %s %s.", verb,
1366.                 (qq == 1L) ? "one" : "some", obj_nambuf, where);
1367.         *wt_after = wt;
1368.         return qq;
1369.     }
1370. 
1371.     if (!container)
1372.         Strcpy(where, "here"); /* slightly shorter form */
1373.     if (invent || umoney) {
1374.         prefx1 = "you cannot ";
1375.         prefx2 = "";
1376.         suffx = " any more";
1377.     } else {
1378.         prefx1 = (obj->quan == 1L) ? "it " : "even one ";
1379.         prefx2 = "is too heavy for you to ";
1380.         suffx = "";
1381.     }
1382.     There("%s %s %s, but %s%s%s%s.", otense(obj, "are"), obj_nambuf, where,
1383.           prefx1, prefx2, verb, suffx);
1384. 
1385.     /* *wt_after = iw; */
1386.     return 0L;
1387. }
1388. 
1389. /* determine whether character is able and player is willing to carry `obj' */
1390. STATIC_OVL
1391. int
1392. lift_object(obj, container, cnt_p, telekinesis)
1393. struct obj *obj, *container; /* object to pick up, bag it's coming out of */
1394. long *cnt_p;
1395. boolean telekinesis;
1396. {
1397.     int result, old_wt, new_wt, prev_encumbr, next_encumbr;
1398. 
1399.     if (obj->otyp == BOULDER && Sokoban) {
1400.         You("cannot get your %s around this %s.", body_part(HAND),
1401.             xname(obj));
1402.         return -1;
1403.     }
1404.     /* override weight consideration for loadstone picked up by anybody
1405.        and for boulder picked up by hero poly'd into a giant; override
1406.        availability of open inventory slot iff not already carrying one */
1407.     if (obj->otyp == LOADSTONE
1408.         || (obj->otyp == BOULDER && throws_rocks(youmonst.data))) {
1409.         if (inv_cnt(FALSE) < 52 || !carrying(obj->otyp)
1410.             || merge_choice(invent, obj))
1411.             return 1; /* lift regardless of current situation */
1412.         /* if we reach here, we're out of slots and already have at least
1413.            one of these, so treat this one more like a normal item */
1414.         You("are carrying too much stuff to pick up %s %s.",
1415.             (obj->quan == 1L) ? "another" : "more", simpleonames(obj));
1416.         return -1;
1417.     }
1418. 
1419.     *cnt_p = carry_count(obj, container, *cnt_p, telekinesis,
1420.                          &old_wt, &new_wt);
1421.     if (*cnt_p < 1L) {
1422.         result = -1; /* nothing lifted */
1423.     } else if (obj->oclass != COIN_CLASS
1424.                /* [exception for gold coins will have to change
1425.                    if silver/copper ones ever get implemented] */
1426.                && inv_cnt(FALSE) >= 52 && !merge_choice(invent, obj)) {
1427.         /* if there is some gold here (and we haven't already skipped it),
1428.            we aren't limited by the 52 item limit for it, but caller and
1429.            "grandcaller" aren't prepared to skip stuff and then pickup
1430.            just gold, so the best we can do here is vary the message */
1431.         Your("knapsack cannot accommodate any more items%s.",
1432.              /* floor follows by nexthere, otherwise container so by nobj */
1433.              nxtobj(obj, GOLD_PIECE, (boolean) (obj->where == OBJ_FLOOR))
1434.                  ? " (except gold)" : "");
1435.         result = -1; /* nothing lifted */
1436.     } else {
1437.         result = 1;
1438.         prev_encumbr = near_capacity();
1439.         if (prev_encumbr < flags.pickup_burden)
1440.             prev_encumbr = flags.pickup_burden;
1441.         next_encumbr = calc_capacity(new_wt - old_wt);
1442.         if (next_encumbr > prev_encumbr) {
1443.             if (telekinesis) {
1444.                 result = 0; /* don't lift */
1445.             } else {
1446.                 char qbuf[BUFSZ];
1447.                 long savequan = obj->quan;
1448. 
1449.                 obj->quan = *cnt_p;
1450.                 Strcpy(qbuf, (next_encumbr > HVY_ENCUMBER)
1451.                                  ? overloadmsg
1452.                                  : (next_encumbr > MOD_ENCUMBER)
1453.                                        ? nearloadmsg
1454.                                        : moderateloadmsg);
1455.                 if (container)
1456.                     (void) strsubst(qbuf, "lifting", "removing");
1457.                 Strcat(qbuf, " ");
1458.                 (void) safe_qbuf(qbuf, qbuf, ".  Continue?", obj, doname,
1459.                                  ansimpleoname, something);
1460.                 obj->quan = savequan;
1461.                 switch (ynq(qbuf)) {
1462.                 case 'q':
1463.                     result = -1;
1464.                     break;
1465.                 case 'n':
1466.                     result = 0;
1467.                     break;
1468.                 default:
1469.                     break; /* 'y' => result == 1 */
1470.                 }
1471.                 clear_nhwindow(WIN_MESSAGE);
1472.             }
1473.         }
1474.     }
1475. 
1476.     if (obj->otyp == SCR_SCARE_MONSTER && result <= 0 && !container)
1477.         obj->spe = 0;
1478.     return result;
1479. }
1480. 
1481. /*
1482.  * Pick up <count> of obj from the ground and add it to the hero's inventory.
1483.  * Returns -1 if caller should break out of its loop, 0 if nothing picked
1484.  * up, 1 if otherwise.
1485.  */
1486. int
1487. pickup_object(obj, count, telekinesis)
1488. struct obj *obj;
1489. long count;
1490. boolean telekinesis; /* not picking it up directly by hand */
1491. {
1492.     int res, nearload;
1493. 
1494.     if (obj->quan < count) {
1495.         impossible("pickup_object: count %ld > quan %ld?", count, obj->quan);
1496.         return 0;
1497.     }
1498. 
1499.     /* In case of auto-pickup, where we haven't had a chance
1500.        to look at it yet; affects docall(SCR_SCARE_MONSTER). */
1501.     if (!Blind)
1502.         obj->dknown = 1;
1503. 
1504.     if (obj == uchain) { /* do not pick up attached chain */
1505.         return 0;
1506.     } else if (obj->where == OBJ_MINVENT && obj->owornmask != 0L
1507.                && u.uswallow && obj->ocarry == u.ustuck) {
1508.         You_cant("pick %s up.", ysimple_name(obj));
1509.         return 0;
1510.     } else if (obj->oartifact && !touch_artifact(obj, &youmonst)) {
1511.         return 0;
1512.     } else if (obj->otyp == CORPSE) {
1513.         if (fatal_corpse_mistake(obj, telekinesis)
1514.             || rider_corpse_revival(obj, telekinesis))
1515.             return -1;
1516.     } else if (obj->otyp == SCR_SCARE_MONSTER) {
1517.         if (obj->blessed)
1518.             obj->blessed = 0;
1519.         else if (!obj->spe && !obj->cursed)
1520.             obj->spe = 1;
1521.         else {
1522.             pline_The("scroll%s %s to dust as you %s %s up.", plur(obj->quan),
1523.                       otense(obj, "turn"), telekinesis ? "raise" : "pick",
1524.                       (obj->quan == 1L) ? "it" : "them");
1525.             if (!(objects[SCR_SCARE_MONSTER].oc_name_known)
1526.                 && !(objects[SCR_SCARE_MONSTER].oc_uname))
1527.                 docall(obj);
1528.             useupf(obj, obj->quan);
1529.             return 1; /* tried to pick something up and failed, but
1530.                          don't want to terminate pickup loop yet   */
1531.         }
1532.     }
1533. 
1534.     if ((res = lift_object(obj, (struct obj *) 0, &count, telekinesis)) <= 0)
1535.         return res;
1536. 
1537.     /* Whats left of the special case for gold :-) */
1538.     if (obj->oclass == COIN_CLASS)
1539.         context.botl = 1;
1540.     if (obj->quan != count && obj->otyp != LOADSTONE)
1541.         obj = splitobj(obj, count);
1542. 
1543.     obj = pick_obj(obj);
1544. 
1545.     if (uwep && uwep == obj)
1546.         mrg_to_wielded = TRUE;
1547.     nearload = near_capacity();
1548.     prinv(nearload == SLT_ENCUMBER ? moderateloadmsg : (char *) 0, obj,
1549.           count);
1550.     mrg_to_wielded = FALSE;
1551.     return 1;
1552. }
1553. 
1554. /*
1555.  * Do the actual work of picking otmp from the floor or monster's interior
1556.  * and putting it in the hero's inventory.  Take care of billing.  Return a
1557.  * pointer to the object where otmp ends up.  This may be different
1558.  * from otmp because of merging.
1559.  */
1560. struct obj *
1561. pick_obj(otmp)
1562. struct obj *otmp;
1563. {
1564.     struct obj *result;
1565.     int ox = otmp->ox, oy = otmp->oy;
1566.     boolean robshop = (!u.uswallow && otmp != uball && costly_spot(ox, oy));
1567. 
1568.     obj_extract_self(otmp);
1569.     newsym(ox, oy);
1570. 
1571.     /* for shop items, addinv() needs to be after addtobill() (so that
1572.        object merger can take otmp->unpaid into account) but before
1573.        remote_robbery() (which calls rob_shop() which calls setpaid()
1574.        after moving costs of unpaid items to shop debt; setpaid()
1575.        calls clear_unpaid() for lots of object chains, but 'otmp' isn't
1576.        on any of those between obj_extract_self() and addinv(); for
1577.        3.6.0, 'otmp' remained flagged as an unpaid item in inventory
1578.        and triggered impossible() every time inventory was examined) */
1579.     if (robshop) {
1580.         char saveushops[5], fakeshop[2];
1581. 
1582.         /* addtobill cares about your location rather than the object's;
1583.            usually they'll be the same, but not when using telekinesis
1584.            (if ever implemented) or a grappling hook */
1585.         Strcpy(saveushops, u.ushops);
1586.         fakeshop[0] = *in_rooms(ox, oy, SHOPBASE);
1587.         fakeshop[1] = '\0';
1588.         Strcpy(u.ushops, fakeshop);
1589.         /* sets obj->unpaid if necessary */
1590.         addtobill(otmp, TRUE, FALSE, FALSE);
1591.         Strcpy(u.ushops, saveushops);
1592.         robshop = otmp->unpaid && !index(u.ushops, *fakeshop);
1593.     }
1594. 
1595.     result = addinv(otmp);
1596.     /* if you're taking a shop item from outside the shop, make shk notice */
1597.     if (robshop)
1598.         remote_burglary(ox, oy);
1599. 
1600.     return result;
1601. }
1602. 
1603. /*
1604.  * prints a message if encumbrance changed since the last check and
1605.  * returns the new encumbrance value (from near_capacity()).
1606.  */
1607. int
1608. encumber_msg()
1609. {
1610.     static int oldcap = UNENCUMBERED;
1611.     int newcap = near_capacity();
1612. 
1613.     if (oldcap < newcap) {
1614.         switch (newcap) {
1615.         case 1:
1616.             Your("movements are slowed slightly because of your load.");
1617.             break;
1618.         case 2:
1619.             You("rebalance your load.  Movement is difficult.");
1620.             break;
1621.         case 3:
1622.             You("%s under your heavy load.  Movement is very hard.",
1623.                 stagger(youmonst.data, "stagger"));
1624.             break;
1625.         default:
1626.             You("%s move a handspan with this load!",
1627.                 newcap == 4 ? "can barely" : "can't even");
1628.             break;
1629.         }
1630.         context.botl = 1;
1631.     } else if (oldcap > newcap) {
1632.         switch (newcap) {
1633.         case 0:
1634.             Your("movements are now unencumbered.");
1635.             break;
1636.         case 1:
1637.             Your("movements are only slowed slightly by your load.");
1638.             break;
1639.         case 2:
1640.             You("rebalance your load.  Movement is still difficult.");
1641.             break;
1642.         case 3:
1643.             You("%s under your load.  Movement is still very hard.",
1644.                 stagger(youmonst.data, "stagger"));
1645.             break;
1646.         }
1647.         context.botl = 1;
1648.     }
1649. 
1650.     oldcap = newcap;
1651.     return newcap;
1652. }
1653. 
1654. /* Is there a container at x,y. Optional: return count of containers at x,y */
1655. int
1656. container_at(x, y, countem)
1657. int x, y;
1658. boolean countem;
1659. {
1660.     struct obj *cobj, *nobj;
1661.     int container_count = 0;
1662. 
1663.     for (cobj = level.objects[x][y]; cobj; cobj = nobj) {
1664.         nobj = cobj->nexthere;
1665.         if (Is_container(cobj)) {
1666.             container_count++;
1667.             if (!countem)
1668.                 break;
1669.         }
1670.     }
1671.     return container_count;
1672. }
1673. 
1674. STATIC_OVL boolean
1675. able_to_loot(x, y, looting)
1676. int x, y;
1677. boolean looting; /* loot vs tip */
1678. {
1679.     const char *verb = looting ? "loot" : "tip";
1680. 
1681.     if (!can_reach_floor(TRUE)) {
1682.         if (u.usteed && P_SKILL(P_RIDING) < P_BASIC)
1683.             rider_cant_reach(); /* not skilled enough to reach */
1684.         else
1685.             cant_reach_floor(x, y, FALSE, TRUE);
1686.         return FALSE;
1687.     } else if ((is_pool(x, y) && (looting || !Underwater)) || is_lava(x, y)) {
1688.         /* at present, can't loot in water even when Underwater;
1689.            can tip underwater, but not when over--or stuck in--lava */
1690.         You("cannot %s things that are deep in the %s.", verb,
1691.             hliquid(is_lava(x, y) ? "lava" : "water"));
1692.         return FALSE;
1693.     } else if (nolimbs(youmonst.data)) {
1694.         pline("Without limbs, you cannot %s anything.", verb);
1695.         return FALSE;
1696.     } else if (looting && !freehand()) {
1697.         pline("Without a free %s, you cannot loot anything.",
1698.               body_part(HAND));
1699.         return FALSE;
1700.     }
1701.     return TRUE;
1702. }
1703. 
1704. STATIC_OVL boolean
1705. mon_beside(x, y)
1706. int x, y;
1707. {
1708.     int i, j, nx, ny;
1709. 
1710.     for (i = -1; i <= 1; i++)
1711.         for (j = -1; j <= 1; j++) {
1712.             nx = x + i;
1713.             ny = y + j;
1714.             if (isok(nx, ny) && MON_AT(nx, ny))
1715.                 return TRUE;
1716.         }
1717.     return FALSE;
1718. }
1719. 
1720. STATIC_OVL int
1721. do_loot_cont(cobjp, cindex, ccount)
1722. struct obj **cobjp;
1723. int cindex, ccount; /* index of this container (1..N), number of them (N) */
1724. {
1725.     struct obj *cobj = *cobjp;
1726. 
1727.     if (!cobj)
1728.         return 0;
1729.     if (cobj->olocked) {
1730.         if (ccount < 2)
1731.             pline("%s locked.",
1732.                   cobj->lknown ? "It is" : "Hmmm, it turns out to be");
1733.         else if (cobj->lknown)
1734.             pline("%s is locked.", The(xname(cobj)));
1735.         else
1736.             pline("Hmmm, %s turns out to be locked.", the(xname(cobj)));
1737.         cobj->lknown = 1;
1738.         return 0;
1739.     }
1740.     cobj->lknown = 1; /* floor container, so no need for update_inventory() */
1741. 
1742.     if (cobj->otyp == BAG_OF_TRICKS) {
1743.         int tmp;
1744. 
1745.         You("carefully open %s...", the(xname(cobj)));
1746.         pline("It develops a huge set of teeth and bites you!");
1747.         tmp = rnd(10);
1748.         losehp(Maybe_Half_Phys(tmp), "carnivorous bag", KILLED_BY_AN);
1749.         makeknown(BAG_OF_TRICKS);
1750.         abort_looting = TRUE;
1751.         return 1;
1752.     }
1753. 
1754.     You("%sopen %s...", (!cobj->cknown || !cobj->lknown) ? "carefully " : "",
1755.         the(xname(cobj)));
1756.     return use_container(cobjp, 0, (boolean) (cindex < ccount));
1757. }
1758. 
1759. /* loot a container on the floor or loot saddle from mon. */
1760. int
1761. doloot()
1762. {
1763.     struct obj *cobj, *nobj;
1764.     register int c = -1;
1765.     int timepassed = 0;
1766.     coord cc;
1767.     boolean underfoot = TRUE;
1768.     const char *dont_find_anything = "don't find anything";
1769.     struct monst *mtmp;
1770.     char qbuf[BUFSZ];
1771.     int prev_inquiry = 0;
1772.     boolean prev_loot = FALSE;
1773.     int num_conts = 0;
1774. 
1775.     abort_looting = FALSE;
1776. 
1777.     if (check_capacity((char *) 0)) {
1778.         /* "Can't do that while carrying so much stuff." */
1779.         return 0;
1780.     }
1781.     if (nohands(youmonst.data)) {
1782.         You("have no hands!"); /* not `body_part(HAND)' */
1783.         return 0;
1784.     }
1785.     if (Confusion) {
1786.         if (rn2(6) && reverse_loot())
1787.             return 1;
1788.         if (rn2(2)) {
1789.             pline("Being confused, you find nothing to loot.");
1790.             return 1; /* costs a turn */
1791.         }             /* else fallthrough to normal looting */
1792.     }
1793.     cc.x = u.ux;
1794.     cc.y = u.uy;
1795. 
1796.     if (iflags.menu_requested)
1797.         goto lootmon;
1798. 
1799.  lootcont:
1800.     if ((num_conts = container_at(cc.x, cc.y, TRUE)) > 0) {
1801.         boolean anyfound = FALSE;
1802. 
1803.         if (!able_to_loot(cc.x, cc.y, TRUE))
1804.             return 0;
1805. 
1806.         if (Blind && !uarmg) {
1807.             /* if blind and without gloves, attempting to #loot at the
1808.                location of a cockatrice corpse is fatal before asking
1809.                whether to manipulate any containers */
1810.             for (nobj = sobj_at(CORPSE, cc.x, cc.y); nobj;
1811.                  nobj = nxtobj(nobj, CORPSE, TRUE))
1812.                 if (will_feel_cockatrice(nobj, FALSE)) {
1813.                     feel_cockatrice(nobj, FALSE);
1814.                     /* if life-saved (or poly'd into stone golem),
1815.                        terminate attempt to loot */
1816.                     return 1;
1817.                 }
1818.         }
1819. 
1820.         if (num_conts > 1) {
1821.             /* use a menu to loot many containers */
1822.             int n, i;
1823.             winid win;
1824.             anything any;
1825.             menu_item *pick_list = (menu_item *) 0;
1826. 
1827.             any.a_void = 0;
1828.             win = create_nhwindow(NHW_MENU);
1829.             start_menu(win);
1830. 
1831.             for (cobj = level.objects[cc.x][cc.y]; cobj;
1832.                  cobj = cobj->nexthere)
1833.                 if (Is_container(cobj)) {
1834.                     any.a_obj = cobj;
1835.                     add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE,
1836.                              doname(cobj), MENU_UNSELECTED);
1837.                 }
1838.             end_menu(win, "Loot which containers?");
1839.             n = select_menu(win, PICK_ANY, &pick_list);
1840.             destroy_nhwindow(win);
1841. 
1842.             if (n > 0) {
1843.                 for (i = 1; i <= n; i++) {
1844.                     cobj = pick_list[i - 1].item.a_obj;
1845.                     timepassed |= do_loot_cont(&cobj, i, n);
1846.                     if (abort_looting) {
1847.                         /* chest trap or magic bag explosion or <esc> */
1848.                         free((genericptr_t) pick_list);
1849.                         return timepassed;
1850.                     }
1851.                 }
1852.                 free((genericptr_t) pick_list);
1853.             }
1854.             if (n != 0)
1855.                 c = 'y';
1856.         } else {
1857.             for (cobj = level.objects[cc.x][cc.y]; cobj; cobj = nobj) {
1858.                 nobj = cobj->nexthere;
1859. 
1860.                 if (Is_container(cobj)) {
1861.                     c = ynq(safe_qbuf(qbuf, "There is ", " here, loot it?",
1862.                                       cobj, doname, ansimpleoname,
1863.                                       "a container"));
1864.                     if (c == 'q')
1865.                         return timepassed;
1866.                     if (c == 'n')
1867.                         continue;
1868.                     anyfound = TRUE;
1869. 
1870.                     timepassed |= do_loot_cont(&cobj, 1, 1);
1871.                     if (abort_looting)
1872.                         /* chest trap or magic bag explosion or <esc> */
1873.                         return timepassed;
1874.                 }
1875.             }
1876.             if (anyfound)
1877.                 c = 'y';
1878.         }
1879.     } else if (IS_GRAVE(levl[cc.x][cc.y].typ)) {
1880.         You("need to dig up the grave to effectively loot it...");
1881.     }
1882. 
1883.     /*
1884.      * 3.3.1 introduced directional looting for some things.
1885.      */
1886.  lootmon:
1887.     if (c != 'y' && mon_beside(u.ux, u.uy)) {
1888.         if (!get_adjacent_loc("Loot in what direction?",
1889.                               "Invalid loot location", u.ux, u.uy, &cc))
1890.             return 0;
1891.         if (cc.x == u.ux && cc.y == u.uy) {
1892.             underfoot = TRUE;
1893.             if (container_at(cc.x, cc.y, FALSE))
1894.                 goto lootcont;
1895.         } else
1896.             underfoot = FALSE;
1897.         if (u.dz < 0) {
1898.             You("%s to loot on the %s.", dont_find_anything,
1899.                 ceiling(cc.x, cc.y));
1900.             timepassed = 1;
1901.             return timepassed;
1902.         }
1903.         mtmp = m_at(cc.x, cc.y);
1904.         if (mtmp)
1905.             timepassed = loot_mon(mtmp, &prev_inquiry, &prev_loot);
1906.         /* always use a turn when choosing a direction is impaired,
1907.            even if you've successfully targetted a saddled creature
1908.            and then answered "no" to the "remove its saddle?" prompt */
1909.         if (Confusion || Stunned)
1910.             timepassed = 1;
1911. 
1912.         /* Preserve pre-3.3.1 behaviour for containers.
1913.          * Adjust this if-block to allow container looting
1914.          * from one square away to change that in the future.
1915.          */
1916.         if (!underfoot) {
1917.             if (container_at(cc.x, cc.y, FALSE)) {
1918.                 if (mtmp) {
1919.                     You_cant("loot anything %sthere with %s in the way.",
1920.                              prev_inquiry ? "else " : "", mon_nam(mtmp));
1921.                     return timepassed;
1922.                 } else {
1923.                     You("have to be at a container to loot it.");
1924.                 }
1925.             } else {
1926.                 You("%s %sthere to loot.", dont_find_anything,
1927.                     (prev_inquiry || prev_loot) ? "else " : "");
1928.                 return timepassed;
1929.             }
1930.         }
1931.     } else if (c != 'y' && c != 'n') {
1932.         You("%s %s to loot.", dont_find_anything,
1933.             underfoot ? "here" : "there");
1934.     }
1935.     return timepassed;
1936. }
1937. 
1938. /* called when attempting to #loot while confused */
1939. STATIC_OVL boolean
1940. reverse_loot()
1941. {
1942.     struct obj *goldob = 0, *coffers, *otmp, boxdummy;
1943.     struct monst *mon;
1944.     long contribution;
1945.     int n, x = u.ux, y = u.uy;
1946. 
1947.     if (!rn2(3)) {
1948.         /* n objects: 1/(n+1) chance per object plus 1/(n+1) to fall off end
1949.          */
1950.         for (n = inv_cnt(TRUE), otmp = invent; otmp; --n, otmp = otmp->nobj)
1951.             if (!rn2(n + 1)) {
1952.                 prinv("You find old loot:", otmp, 0L);
1953.                 return TRUE;
1954.             }
1955.         return FALSE;
1956.     }
1957. 
1958.     /* find a money object to mess with */
1959.     for (goldob = invent; goldob; goldob = goldob->nobj)
1960.         if (goldob->oclass == COIN_CLASS) {
1961.             contribution = ((long) rnd(5) * goldob->quan + 4L) / 5L;
1962.             if (contribution < goldob->quan)
1963.                 goldob = splitobj(goldob, contribution);
1964.             break;
1965.         }
1966.     if (!goldob)
1967.         return FALSE;
1968. 
1969.     if (!IS_THRONE(levl[x][y].typ)) {
1970.         dropx(goldob);
1971.         /* the dropped gold might have fallen to lower level */
1972.         if (g_at(x, y))
1973.             pline("Ok, now there is loot here.");
1974.     } else {
1975.         /* find original coffers chest if present, otherwise use nearest one */
1976.         otmp = 0;
1977.         for (coffers = fobj; coffers; coffers = coffers->nobj)
1978.             if (coffers->otyp == CHEST) {
1979.                 if (coffers->spe == 2)
1980.                     break; /* a throne room chest */
1981.                 if (!otmp
1982.                     || distu(coffers->ox, coffers->oy)
1983.                            < distu(otmp->ox, otmp->oy))
1984.                     otmp = coffers; /* remember closest ordinary chest */
1985.             }
1986.         if (!coffers)
1987.             coffers = otmp;
1988. 
1989.         if (coffers) {
1990.             verbalize("Thank you for your contribution to reduce the debt.");
1991.             freeinv(goldob);
1992.             (void) add_to_container(coffers, goldob);
1993.             coffers->owt = weight(coffers);
1994.             coffers->cknown = 0;
1995.             if (!coffers->olocked) {
1996.                 boxdummy = zeroobj, boxdummy.otyp = SPE_WIZARD_LOCK;
1997.                 (void) boxlock(coffers, &boxdummy);
1998.             }
1999.         } else if (levl[x][y].looted != T_LOOTED
2000.                    && (mon = makemon(courtmon(), x, y, NO_MM_FLAGS)) != 0) {
2001.             freeinv(goldob);
2002.             add_to_minv(mon, goldob);
2003.             pline("The exchequer accepts your contribution.");
2004.             if (!rn2(10))
2005.                 levl[x][y].looted = T_LOOTED;
2006.         } else {
2007.             You("drop %s.", doname(goldob));
2008.             dropx(goldob);
2009.         }
2010.     }
2011.     return TRUE;
2012. }
2013. 
2014. /* loot_mon() returns amount of time passed.
2015.  */
2016. int
2017. loot_mon(mtmp, passed_info, prev_loot)
2018. struct monst *mtmp;
2019. int *passed_info;
2020. boolean *prev_loot;
2021. {
2022.     int c = -1;
2023.     int timepassed = 0;
2024.     struct obj *otmp;
2025.     char qbuf[QBUFSZ];
2026. 
2027.     /* 3.3.1 introduced the ability to remove saddle from a steed.
2028.      *  *passed_info is set to TRUE if a loot query was given.
2029.      *  *prev_loot is set to TRUE if something was actually acquired in here.
2030.      */
2031.     if (mtmp && mtmp != u.usteed && (otmp = which_armor(mtmp, W_SADDLE))) {
2032.         long unwornmask;
2033. 
2034.         if (passed_info)
2035.             *passed_info = 1;
2036.         Sprintf(qbuf, "Do you want to remove the saddle from %s?",
2037.                 x_monnam(mtmp, ARTICLE_THE, (char *) 0,
2038.                          SUPPRESS_SADDLE, FALSE));
2039.         if ((c = yn_function(qbuf, ynqchars, 'n')) == 'y') {
2040.             if (nolimbs(youmonst.data)) {
2041.                 You_cant("do that without limbs."); /* not body_part(HAND) */
2042.                 return 0;
2043.             }
2044.             if (otmp->cursed) {
2045.                 You("can't.  The saddle seems to be stuck to %s.",
2046.                     x_monnam(mtmp, ARTICLE_THE, (char *) 0,
2047.                              SUPPRESS_SADDLE, FALSE));
2048.                 /* the attempt costs you time */
2049.                 return 1;
2050.             }
2051.             obj_extract_self(otmp);
2052.             if ((unwornmask = otmp->owornmask) != 0L) {
2053.                 mtmp->misc_worn_check &= ~unwornmask;
2054.                 otmp->owornmask = 0L;
2055.                 update_mon_intrinsics(mtmp, otmp, FALSE, FALSE);
2056.             }
2057.             otmp = hold_another_object(otmp, "You drop %s!", doname(otmp),
2058.                                        (const char *) 0);
2059.             nhUse(otmp);
2060.             timepassed = rnd(3);
2061.             if (prev_loot)
2062.                 *prev_loot = TRUE;
2063.         } else if (c == 'q') {
2064.             return 0;
2065.         }
2066.     }
2067.     /* 3.4.0 introduced ability to pick things up from swallower's stomach */
2068.     if (u.uswallow) {
2069.         int count = passed_info ? *passed_info : 0;
2070. 
2071.         timepassed = pickup(count);
2072.     }
2073.     return timepassed;
2074. }
2075. 
2076. /*
2077.  * Decide whether an object being placed into a magic bag will cause
2078.  * it to explode.  If the object is a bag itself, check recursively.
2079.  */
2080. STATIC_OVL boolean
2081. mbag_explodes(obj, depthin)
2082. struct obj *obj;
2083. int depthin;
2084. {
2085.     /* these won't cause an explosion when they're empty */
2086.     if ((obj->otyp == WAN_CANCELLATION || obj->otyp == BAG_OF_TRICKS)
2087.         && obj->spe <= 0)
2088.         return FALSE;
2089. 
2090.     /* odds: 1/1, 2/2, 3/4, 4/8, 5/16, 6/32, 7/64, 8/128, 9/128, 10/128,... */
2091.     if ((Is_mbag(obj) || obj->otyp == WAN_CANCELLATION)
2092.         && (rn2(1 << (depthin > 7 ? 7 : depthin)) <= depthin))
2093.         return TRUE;
2094.     else if (Has_contents(obj)) {
2095.         struct obj *otmp;
2096. 
2097.         for (otmp = obj->cobj; otmp; otmp = otmp->nobj)
2098.             if (mbag_explodes(otmp, depthin + 1))
2099.                 return TRUE;
2100.     }
2101.     return FALSE;
2102. }
2103. 
2104. STATIC_OVL long
2105. boh_loss(container, held)
2106. struct obj *container;
2107. int held;
2108. {
2109.     /* sometimes toss objects if a cursed magic bag */
2110.     if (Is_mbag(container) && container->cursed && Has_contents(container)) {
2111.         long loss = 0L;
2112.         struct obj *curr, *otmp;
2113. 
2114.         for (curr = container->cobj; curr; curr = otmp) {
2115.             otmp = curr->nobj;
2116.             if (!rn2(13)) {
2117.                 obj_extract_self(curr);
2118.                 loss += mbag_item_gone(held, curr);
2119.             }
2120.         }
2121.         return loss;
2122.     }
2123.     return 0;
2124. }
2125. 
2126. /* Returns: -1 to stop, 1 item was inserted, 0 item was not inserted. */
2127. STATIC_PTR int
2128. in_container(obj)
2129. register struct obj *obj;
2130. {
2131.     boolean floor_container = !carried(current_container);
2132.     boolean was_unpaid = FALSE;
2133.     char buf[BUFSZ];
2134. 
2135.     if (!current_container) {
2136.         impossible("<in> no current_container?");
2137.         return 0;
2138.     } else if (obj == uball || obj == uchain) {
2139.         You("must be kidding.");
2140.         return 0;
2141.     } else if (obj == current_container) {
2142.         pline("That would be an interesting topological exercise.");
2143.         return 0;
2144.     } else if (obj->owornmask & (W_ARMOR | W_ACCESSORY)) {
2145.         Norep("You cannot %s %s you are wearing.",
2146.               Icebox ? "refrigerate" : "stash", something);
2147.         return 0;
2148.     } else if ((obj->otyp == LOADSTONE) && obj->cursed) {
2149.         set_bknown(obj, 1);
2150.         pline_The("stone%s won't leave your person.", plur(obj->quan));
2151.         return 0;
2152.     } else if (obj->otyp == AMULET_OF_YENDOR
2153.                || obj->otyp == CANDELABRUM_OF_INVOCATION
2154.                || obj->otyp == BELL_OF_OPENING
2155.                || obj->otyp == SPE_BOOK_OF_THE_DEAD) {
2156.         /* Prohibit Amulets in containers; if you allow it, monsters can't
2157.          * steal them.  It also becomes a pain to check to see if someone
2158.          * has the Amulet.  Ditto for the Candelabrum, the Bell and the Book.
2159.          */
2160.         pline("%s cannot be confined in such trappings.", The(xname(obj)));
2161.         return 0;
2162.     } else if (obj->otyp == LEASH && obj->leashmon != 0) {
2163.         pline("%s attached to your pet.", Tobjnam(obj, "are"));
2164.         return 0;
2165.     } else if (obj == uwep) {
2166.         if (welded(obj)) {
2167.             weldmsg(obj);
2168.             return 0;
2169.         }
2170.         setuwep((struct obj *) 0);
2171.         /* This uwep check is obsolete.  It dates to 3.0 and earlier when
2172.          * unwielding Firebrand would be fatal in hell if hero had no other
2173.          * fire resistance.  Life-saving would force it to be re-wielded.
2174.          */
2175.         if (uwep)
2176.             return 0; /* unwielded, died, rewielded */
2177.     } else if (obj == uswapwep) {
2178.         setuswapwep((struct obj *) 0);
2179.     } else if (obj == uquiver) {
2180.         setuqwep((struct obj *) 0);
2181.     }
2182. 
2183.     if (fatal_corpse_mistake(obj, FALSE))
2184.         return -1;
2185. 
2186.     /* boxes, boulders, and big statues can't fit into any container */
2187.     if (obj->otyp == ICE_BOX || Is_box(obj) || obj->otyp == BOULDER
2188.         || (obj->otyp == STATUE && bigmonst(&mons[obj->corpsenm]))) {
2189.         /*
2190.          *  xname() uses a static result array.  Save obj's name
2191.          *  before current_container's name is computed.  Don't
2192.          *  use the result of strcpy() within You() --- the order
2193.          *  of evaluation of the parameters is undefined.
2194.          */
2195.         Strcpy(buf, the(xname(obj)));
2196.         You("cannot fit %s into %s.", buf, the(xname(current_container)));
2197.         return 0;
2198.     }
2199. 
2200.     freeinv(obj);
2201. 
2202.     if (obj_is_burning(obj)) /* this used to be part of freeinv() */
2203.         (void) snuff_lit(obj);
2204. 
2205.     if (floor_container && costly_spot(u.ux, u.uy)) {
2206.         /* defer gold until after put-in message */
2207.         if (obj->oclass != COIN_CLASS) {
2208.             /* sellobj() will take an unpaid item off the shop bill */
2209.             was_unpaid = obj->unpaid ? TRUE : FALSE;
2210.             /* don't sell when putting the item into your own container,
2211.              * but handle billing correctly */
2212.             sellobj_state(current_container->no_charge
2213.                           ? SELL_DONTSELL : SELL_DELIBERATE);
2214.             sellobj(obj, u.ux, u.uy);
2215.             sellobj_state(SELL_NORMAL);
2216.         }
2217.     }
2218.     if (Icebox && !age_is_relative(obj)) {
2219.         obj->age = monstermoves - obj->age; /* actual age */
2220.         /* stop any corpse timeouts when frozen */
2221.         if (obj->otyp == CORPSE && obj->timed) {
2222.             long rot_alarm = stop_timer(ROT_CORPSE, obj_to_any(obj));
2223. 
2224.             (void) stop_timer(REVIVE_MON, obj_to_any(obj));
2225.             /* mark a non-reviving corpse as such */
2226.             if (rot_alarm)
2227.                 obj->norevive = 1;
2228.         }
2229.     } else if (Is_mbag(current_container) && mbag_explodes(obj, 0)) {
2230.         /* explicitly mention what item is triggering the explosion */
2231.         pline("As you put %s inside, you are blasted by a magical explosion!",
2232.               doname(obj));
2233.         /* did not actually insert obj yet */
2234.         if (was_unpaid)
2235.             addtobill(obj, FALSE, FALSE, TRUE);
2236.         obfree(obj, (struct obj *) 0);
2237.         /* if carried, shop goods will be flagged 'unpaid' and obfree() will
2238.            handle bill issues, but if on floor, we need to put them on bill
2239.            before deleting them (non-shop items will be flagged 'no_charge') */
2240.         if (floor_container
2241.             && costly_spot(current_container->ox, current_container->oy)) {
2242.             struct obj save_no_charge;
2243. 
2244.             save_no_charge.no_charge = current_container->no_charge;
2245.             addtobill(current_container, FALSE, FALSE, FALSE);
2246.             /* addtobill() clears no charge; we need to set it back
2247.                so that useupf() doesn't double bill */
2248.             current_container->no_charge = save_no_charge.no_charge;
2249.         }
2250.         delete_contents(current_container);
2251.         if (!floor_container)
2252.             useup(current_container);
2253.         else if (obj_here(current_container, u.ux, u.uy))
2254.             useupf(current_container, current_container->quan);
2255.         else
2256.             panic("in_container:  bag not found.");
2257. 
2258.         losehp(d(6, 6), "magical explosion", KILLED_BY_AN);
2259.         current_container = 0; /* baggone = TRUE; */
2260.     }
2261. 
2262.     if (current_container) {
2263.         Strcpy(buf, the(xname(current_container)));
2264.         You("put %s into %s.", doname(obj), buf);
2265. 
2266.         /* gold in container always needs to be added to credit */
2267.         if (floor_container && obj->oclass == COIN_CLASS)
2268.             sellobj(obj, current_container->ox, current_container->oy);
2269.         (void) add_to_container(current_container, obj);
2270.         current_container->owt = weight(current_container);
2271.     }
2272.     /* gold needs this, and freeinv() many lines above may cause
2273.      * the encumbrance to disappear from the status, so just always
2274.      * update status immediately.
2275.      */
2276.     bot();
2277.     return (current_container ? 1 : -1);
2278. }
2279. 
2280. /* askchain() filter used by in_container();
2281.  * returns True if the container is intact and 'obj' isn't it, False if
2282.  * container is gone (magic bag explosion) or 'obj' is the container itself;
2283.  * also used by getobj() when picking a single item to stash
2284.  */
2285. int
2286. ck_bag(obj)
2287. struct obj *obj;
2288. {
2289.     return (current_container && obj != current_container);
2290. }
2291. 
2292. /* Returns: -1 to stop, 1 item was removed, 0 item was not removed. */
2293. STATIC_PTR int
2294. out_container(obj)
2295. register struct obj *obj;
2296. {
2297.     register struct obj *otmp;
2298.     boolean is_gold = (obj->oclass == COIN_CLASS);
2299.     int res, loadlev;
2300.     long count;
2301. 
2302.     if (!current_container) {
2303.         impossible("<out> no current_container?");
2304.         return -1;
2305.     } else if (is_gold) {
2306.         obj->owt = weight(obj);
2307.     }
2308. 
2309.     if (obj->oartifact && !touch_artifact(obj, &youmonst))
2310.         return 0;
2311. 
2312.     if (fatal_corpse_mistake(obj, FALSE))
2313.         return -1;
2314. 
2315.     count = obj->quan;
2316.     if ((res = lift_object(obj, current_container, &count, FALSE)) <= 0)
2317.         return res;
2318. 
2319.     if (obj->quan != count && obj->otyp != LOADSTONE)
2320.         obj = splitobj(obj, count);
2321. 
2322.     /* Remove the object from the list. */
2323.     obj_extract_self(obj);
2324.     current_container->owt = weight(current_container);
2325. 
2326.     if (Icebox)
2327.         removed_from_icebox(obj);
2328. 
2329.     if (!obj->unpaid && !carried(current_container)
2330.         && costly_spot(current_container->ox, current_container->oy)) {
2331.         obj->ox = current_container->ox;
2332.         obj->oy = current_container->oy;
2333.         addtobill(obj, FALSE, FALSE, FALSE);
2334.     }
2335.     if (is_pick(obj))
2336.         pick_pick(obj); /* shopkeeper feedback */
2337. 
2338.     otmp = addinv(obj);
2339.     loadlev = near_capacity();
2340.     prinv(loadlev ? ((loadlev < MOD_ENCUMBER)
2341.                         ? "You have a little trouble removing"
2342.                         : "You have much trouble removing")
2343.                   : (char *) 0,
2344.           otmp, count);
2345. 
2346.     if (is_gold) {
2347.         bot(); /* update character's gold piece count immediately */
2348.     }
2349.     return 1;
2350. }
2351. 
2352. /* taking a corpse out of an ice box needs a couple of adjustments */
2353. STATIC_OVL void
2354. removed_from_icebox(obj)
2355. struct obj *obj;
2356. {
2357.     if (!age_is_relative(obj)) {
2358.         obj->age = monstermoves - obj->age; /* actual age */
2359.         if (obj->otyp == CORPSE)
2360.             start_corpse_timeout(obj);
2361.     }
2362. }
2363. 
2364. /* an object inside a cursed bag of holding is being destroyed */
2365. STATIC_OVL long
2366. mbag_item_gone(held, item)
2367. int held;
2368. struct obj *item;
2369. {
2370.     struct monst *shkp;
2371.     long loss = 0L;
2372. 
2373.     if (item->dknown)
2374.         pline("%s %s vanished!", Doname2(item), otense(item, "have"));
2375.     else
2376.         You("%s %s disappear!", Blind ? "notice" : "see", doname(item));
2377. 
2378.     if (*u.ushops && (shkp = shop_keeper(*u.ushops)) != 0) {
2379.         if (held ? (boolean) item->unpaid : costly_spot(u.ux, u.uy))
2380.             loss = stolen_value(item, u.ux, u.uy, (boolean) shkp->mpeaceful,
2381.                                 TRUE);
2382.     }
2383.     obfree(item, (struct obj *) 0);
2384.     return loss;
2385. }
2386. 
2387. /* used for #loot/apply, #tip, and final disclosure */
2388. void
2389. observe_quantum_cat(box, makecat, givemsg)
2390. struct obj *box;
2391. boolean makecat, givemsg;
2392. {
2393.     static NEARDATA const char sc[] = "Schroedinger's Cat";
2394.     struct obj *deadcat;
2395.     struct monst *livecat = 0;
2396.     xchar ox, oy;
2397.     boolean itsalive = !rn2(2);
2398. 
2399.     if (get_obj_location(box, &ox, &oy, 0))
2400.         box->ox = ox, box->oy = oy; /* in case it's being carried */
2401. 
2402.     /* this isn't really right, since any form of observation
2403.        (telepathic or monster/object/food detection) ought to
2404.        force the determination of alive vs dead state; but basing it
2405.        just on opening or disclosing the box is much simpler to cope with */
2406. 
2407.     /* SchroedingersBox already has a cat corpse in it */
2408.     deadcat = box->cobj;
2409.     if (itsalive) {
2410.         if (makecat)
2411.             livecat = makemon(&mons[PM_HOUSECAT], box->ox, box->oy,
2412.                               NO_MINVENT | MM_ADJACENTOK);
2413.         if (livecat) {
2414.             livecat->mpeaceful = 1;
2415.             set_malign(livecat);
2416.             if (givemsg) {
2417.                 if (!canspotmon(livecat))
2418.                     You("think %s brushed your %s.", something,
2419.                         body_part(FOOT));
2420.                 else
2421.                     pline("%s inside the box is still alive!",
2422.                           Monnam(livecat));
2423.             }
2424.             (void) christen_monst(livecat, sc);
2425.             if (deadcat) {
2426.                 obj_extract_self(deadcat);
2427.                 obfree(deadcat, (struct obj *) 0), deadcat = 0;
2428.             }
2429.             box->owt = weight(box);
2430.             box->spe = 0;
2431.         }
2432.     } else {
2433.         box->spe = 0; /* now an ordinary box (with a cat corpse inside) */
2434.         if (deadcat) {
2435.             /* set_corpsenm() will start the rot timer that was removed
2436.                when makemon() created SchroedingersBox; start it from
2437.                now rather than from when this special corpse got created */
2438.             deadcat->age = monstermoves;
2439.             set_corpsenm(deadcat, PM_HOUSECAT);
2440.             deadcat = oname(deadcat, sc);
2441.         }
2442.         if (givemsg)
2443.             pline_The("%s inside the box is dead!",
2444.                       Hallucination ? rndmonnam((char *) 0) : "housecat");
2445.     }
2446.     nhUse(deadcat);
2447.     return;
2448. }
2449. 
2450. #undef Icebox
2451. 
2452. /* used by askchain() to check for magic bag explosion */
2453. boolean
2454. container_gone(fn)
2455. int FDECL((*fn), (OBJ_P));
2456. {
2457.     /* result is only meaningful while use_container() is executing */
2458.     return ((fn == in_container || fn == out_container)
2459.             && !current_container);
2460. }
2461. 
2462. STATIC_OVL void
2463. explain_container_prompt(more_containers)
2464. boolean more_containers;
2465. {
2466.     static const char *const explaintext[] = {
2467.         "Container actions:",
2468.         "",
2469.         " : -- Look: examine contents",
2470.         " o -- Out: take things out",
2471.         " i -- In: put things in",
2472.         " b -- Both: first take things out, then put things in",
2473.         " r -- Reversed: put things in, then take things out",
2474.         " s -- Stash: put one item in", "",
2475.         " n -- Next: loot next selected container",
2476.         " q -- Quit: finished",
2477.         " ? -- Help: display this text.",
2478.         "", 0
2479.     };
2480.     const char *const *txtpp;
2481.     winid win;
2482. 
2483.     /* "Do what with <container>? [:oibrsq or ?] (q)" */
2484.     if ((win = create_nhwindow(NHW_TEXT)) != WIN_ERR) {
2485.         for (txtpp = explaintext; *txtpp; ++txtpp) {
2486.             if (!more_containers && !strncmp(*txtpp, " n ", 3))
2487.                 continue;
2488.             putstr(win, 0, *txtpp);
2489.         }
2490.         display_nhwindow(win, FALSE);
2491.         destroy_nhwindow(win);
2492.     }
2493. }
2494. 
2495. boolean
2496. u_handsy()
2497. {
2498.     if (nohands(youmonst.data)) {
2499.         You("have no hands!"); /* not `body_part(HAND)' */
2500.         return FALSE;
2501.     } else if (!freehand()) {
2502.         You("have no free %s.", body_part(HAND));
2503.         return FALSE;
2504.     }
2505.     return TRUE;
2506. }
2507. 
2508. static const char stashable[] = { ALLOW_COUNT, COIN_CLASS, ALL_CLASSES, 0 };
2509. 
2510. int
2511. use_container(objp, held, more_containers)
2512. struct obj **objp;
2513. int held;
2514. boolean more_containers; /* True iff #loot multiple and this isn't last one */
2515. {
2516.     struct obj *otmp, *obj = *objp;
2517.     boolean quantum_cat, cursed_mbag, loot_out, loot_in, loot_in_first,
2518.         stash_one, inokay, outokay, outmaybe;
2519.     char c, emptymsg[BUFSZ], qbuf[QBUFSZ], pbuf[QBUFSZ], xbuf[QBUFSZ];
2520.     int used = 0;
2521.     long loss;
2522. 
2523.     abort_looting = FALSE;
2524.     emptymsg[0] = '\0';
2525. 
2526.     if (!u_handsy())
2527.         return 0;
2528. 
2529.     if (!obj->lknown) { /* do this in advance */
2530.         obj->lknown = 1;
2531.         if (held)
2532.             update_inventory();
2533.     }
2534.     if (obj->olocked) {
2535.         pline("%s locked.", Tobjnam(obj, "are"));
2536.         if (held)
2537.             You("must put it down to unlock.");
2538.         return 0;
2539.     } else if (obj->otrapped) {
2540.         if (held)
2541.             You("open %s...", the(xname(obj)));
2542.         (void) chest_trap(obj, HAND, FALSE);
2543.         /* even if the trap fails, you've used up this turn */
2544.         if (multi >= 0) { /* in case we didn't become paralyzed */
2545.             nomul(-1);
2546.             multi_reason = "opening a container";
2547.             nomovemsg = "";
2548.         }
2549.         abort_looting = TRUE;
2550.         return 1;
2551.     }
2552. 
2553.     current_container = obj; /* for use by in/out_container */
2554.     /*
2555.      * From here on out, all early returns go through 'containerdone:'.
2556.      */
2557. 
2558.     /* check for Schroedinger's Cat */
2559.     quantum_cat = SchroedingersBox(current_container);
2560.     if (quantum_cat) {
2561.         observe_quantum_cat(current_container, TRUE, TRUE);
2562.         used = 1;
2563.     }
2564. 
2565.     cursed_mbag = Is_mbag(current_container)
2566.         && current_container->cursed
2567.         && Has_contents(current_container);
2568.     if (cursed_mbag
2569.         && (loss = boh_loss(current_container, held)) != 0) {
2570.         used = 1;
2571.         You("owe %ld %s for lost merchandise.", loss, currency(loss));
2572.         current_container->owt = weight(current_container);
2573.     }
2574.     inokay = (invent != 0
2575.               && !(invent == current_container && !current_container->nobj));
2576.     outokay = Has_contents(current_container);
2577.     if (!outokay) /* preformat the empty-container message */
2578.         Sprintf(emptymsg, "%s is %sempty.", Ysimple_name2(current_container),
2579.                 (quantum_cat || cursed_mbag) ? "now " : "");
2580. 
2581.     /*
2582.      * What-to-do prompt's list of possible actions:
2583.      * always include the look-inside choice (':');
2584.      * include the take-out choice ('o') if container
2585.      * has anything in it or if player doesn't yet know
2586.      * that it's empty (latter can change on subsequent
2587.      * iterations if player picks ':' response);
2588.      * include the put-in choices ('i','s') if hero
2589.      * carries any inventory (including gold);
2590.      * include do-both when 'o' is available, even if
2591.      * inventory is empty--taking out could alter that;
2592.      * include do-both-reversed when 'i' is available,
2593.      * even if container is empty--for similar reason;
2594.      * include the next container choice ('n') when
2595.      * relevant, and make it the default;
2596.      * always include the quit choice ('q'), and make
2597.      * it the default if there's no next containter;
2598.      * include the help choice (" or ?") if `cmdassist'
2599.      * run-time option is set;
2600.      * (Player can pick any of (o,i,b,r,n,s,?) even when
2601.      * they're not listed among the available actions.)
2602.      *
2603.      * Do what with <the/your/Shk's container>? [:oibrs nq or ?] (q)
2604.      * or
2605.      * <The/Your/Shk's container> is empty.  Do what with it? [:irs nq or ?]
2606.      */
2607.     for (;;) { /* repeats iff '?' or ':' gets chosen */
2608.         outmaybe = (outokay || !current_container->cknown);
2609.         if (!outmaybe)
2610.             (void) safe_qbuf(qbuf, (char *) 0, " is empty.  Do what with it?",
2611.                              current_container, Yname2, Ysimple_name2,
2612.                              "This");
2613.         else
2614.             (void) safe_qbuf(qbuf, "Do what with ", "?", current_container,
2615.                              yname, ysimple_name, "it");
2616.         /* ask player about what to do with this container */
2617.         if (flags.menu_style == MENU_PARTIAL
2618.             || flags.menu_style == MENU_FULL) {
2619.             if (!inokay && !outmaybe) {
2620.                 /* nothing to take out, nothing to put in;
2621.                    trying to do both will yield proper feedback */
2622.                 c = 'b';
2623.             } else {
2624.                 c = in_or_out_menu(qbuf, current_container, outmaybe, inokay,
2625.                                    (boolean) (used != 0), more_containers);
2626.             }
2627.         } else { /* TRADITIONAL or COMBINATION */
2628.             xbuf[0] = '\0'; /* list of extra acceptable responses */
2629.             Strcpy(pbuf, ":");                   /* look inside */
2630.             Strcat(outmaybe ? pbuf : xbuf, "o"); /* take out */
2631.             Strcat(inokay ? pbuf : xbuf, "i");   /* put in */
2632.             Strcat(outmaybe ? pbuf : xbuf, "b"); /* both */
2633.             Strcat(inokay ? pbuf : xbuf, "rs");  /* reversed, stash */
2634.             Strcat(pbuf, " ");                   /* separator */
2635.             Strcat(more_containers ? pbuf : xbuf, "n"); /* next container */
2636.             Strcat(pbuf, "q");                   /* quit */
2637.             if (iflags.cmdassist)
2638.                 /* this unintentionally allows user to answer with 'o' or
2639.                    'r'; fortunately, those are already valid choices here */
2640.                 Strcat(pbuf, " or ?"); /* help */
2641.             else
2642.                 Strcat(xbuf, "?");
2643.             if (*xbuf)
2644.                 Strcat(strcat(pbuf, "\033"), xbuf);
2645.             c = yn_function(qbuf, pbuf, more_containers ? 'n' : 'q');
2646.         } /* PARTIAL|FULL vs other modes */
2647. 
2648.         if (c == '?') {
2649.             explain_container_prompt(more_containers);
2650.         } else if (c == ':') { /* note: will set obj->cknown */
2651.             if (!current_container->cknown)
2652.                 used = 1; /* gaining info */
2653.             container_contents(current_container, FALSE, FALSE, TRUE);
2654.         } else
2655.             break;
2656.     } /* loop until something other than '?' or ':' is picked */
2657. 
2658.     if (c == 'q')
2659.         abort_looting = TRUE;
2660.     if (c == 'n' || c == 'q') /* [not strictly needed; falling thru works] */
2661.         goto containerdone;
2662.     loot_out = (c == 'o' || c == 'b' || c == 'r');
2663.     loot_in = (c == 'i' || c == 'b' || c == 'r');
2664.     loot_in_first = (c == 'r'); /* both, reversed */
2665.     stash_one = (c == 's');
2666. 
2667.     /* out-only or out before in */
2668.     if (loot_out && !loot_in_first) {
2669.         if (!Has_contents(current_container)) {
2670.             pline1(emptymsg); /* <whatever> is empty. */
2671.             if (!current_container->cknown)
2672.                 used = 1;
2673.             current_container->cknown = 1;
2674.         } else {
2675.             add_valid_menu_class(0); /* reset */
2676.             if (flags.menu_style == MENU_TRADITIONAL)
2677.                 used |= traditional_loot(FALSE);
2678.             else
2679.                 used |= (menu_loot(0, FALSE) > 0);
2680.             add_valid_menu_class(0);
2681.         }
2682.     }
2683. 
2684.     if ((loot_in || stash_one)
2685.         && (!invent || (invent == current_container && !invent->nobj))) {
2686.         You("don't have anything%s to %s.", invent ? " else" : "",
2687.             stash_one ? "stash" : "put in");
2688.         loot_in = stash_one = FALSE;
2689.     }
2690. 
2691.     /*
2692.      * Gone: being nice about only selecting food if we know we are
2693.      * putting things in an ice chest.
2694.      */
2695.     if (loot_in) {
2696.         add_valid_menu_class(0); /* reset */
2697.         if (flags.menu_style == MENU_TRADITIONAL)
2698.             used |= traditional_loot(TRUE);
2699.         else
2700.             used |= (menu_loot(0, TRUE) > 0);
2701.         add_valid_menu_class(0);
2702.     } else if (stash_one) {
2703.         /* put one item into container */
2704.         if ((otmp = getobj(stashable, "stash")) != 0) {
2705.             if (in_container(otmp)) {
2706.                 used = 1;
2707.             } else {
2708.                 /* couldn't put selected item into container for some
2709.                    reason; might need to undo splitobj() */
2710.                 (void) unsplitobj(otmp);
2711.             }
2712.         }
2713.     }
2714.     /* putting something in might have triggered magic bag explosion */
2715.     if (!current_container)
2716.         loot_out = FALSE;
2717. 
2718.     /* out after in */
2719.     if (loot_out && loot_in_first) {
2720.         if (!Has_contents(current_container)) {
2721.             pline1(emptymsg); /* <whatever> is empty. */
2722.             if (!current_container->cknown)
2723.                 used = 1;
2724.             current_container->cknown = 1;
2725.         } else {
2726.             add_valid_menu_class(0); /* reset */
2727.             if (flags.menu_style == MENU_TRADITIONAL)
2728.                 used |= traditional_loot(FALSE);
2729.             else
2730.                 used |= (menu_loot(0, FALSE) > 0);
2731.             add_valid_menu_class(0);
2732.         }
2733.     }
2734. 
2735.  containerdone:
2736.     if (used) {
2737.         /* Not completely correct; if we put something in without knowing
2738.            whatever was already inside, now we suddenly do.  That can't
2739.            be helped unless we want to track things item by item and then
2740.            deal with containers whose contents are "partly known". */
2741.         if (current_container)
2742.             current_container->cknown = 1;
2743.         update_inventory();
2744.     }
2745. 
2746.     *objp = current_container; /* might have become null */
2747.     if (current_container)
2748.         current_container = 0; /* avoid hanging on to stale pointer */
2749.     else
2750.         abort_looting = TRUE;
2751.     return used;
2752. }
2753. 
2754. /* loot current_container (take things out or put things in), by prompting */
2755. STATIC_OVL int
2756. traditional_loot(put_in)
2757. boolean put_in;
2758. {
2759.     int FDECL((*actionfunc), (OBJ_P)), FDECL((*checkfunc), (OBJ_P));
2760.     struct obj **objlist;
2761.     char selection[MAXOCLASSES + 10]; /* +10: room for B,U,C,X plus slop */
2762.     const char *action;
2763.     boolean one_by_one, allflag;
2764.     int used = 0, menu_on_request = 0;
2765. 
2766.     if (put_in) {
2767.         action = "put in";
2768.         objlist = &invent;
2769.         actionfunc = in_container;
2770.         checkfunc = ck_bag;
2771.     } else {
2772.         action = "take out";
2773.         objlist = &(current_container->cobj);
2774.         actionfunc = out_container;
2775.         checkfunc = (int FDECL((*), (OBJ_P))) 0;
2776.     }
2777. 
2778.     if (query_classes(selection, &one_by_one, &allflag, action, *objlist,
2779.                       FALSE, &menu_on_request)) {
2780.         if (askchain(objlist, (one_by_one ? (char *) 0 : selection), allflag,
2781.                      actionfunc, checkfunc, 0, action))
2782.             used = 1;
2783.     } else if (menu_on_request < 0) {
2784.         used = (menu_loot(menu_on_request, put_in) > 0);
2785.     }
2786.     return used;
2787. }
2788. 
2789. /* loot current_container (take things out or put things in), using a menu */
2790. STATIC_OVL int
2791. menu_loot(retry, put_in)
2792. int retry;
2793. boolean put_in;
2794. {
2795.     int n, i, n_looted = 0;
2796.     boolean all_categories = TRUE, loot_everything = FALSE;
2797.     char buf[BUFSZ];
2798.     const char *action = put_in ? "Put in" : "Take out";
2799.     struct obj *otmp, *otmp2;
2800.     menu_item *pick_list;
2801.     int mflags, res;
2802.     long count;
2803. 
2804.     if (retry) {
2805.         all_categories = (retry == -2);
2806.     } else if (flags.menu_style == MENU_FULL) {
2807.         all_categories = FALSE;
2808.         Sprintf(buf, "%s what type of objects?", action);
2809.         mflags = (ALL_TYPES | UNPAID_TYPES | BUCX_TYPES | CHOOSE_ALL);
2810.         n = query_category(buf, put_in ? invent : current_container->cobj,
2811.                            mflags, &pick_list, PICK_ANY);
2812.         if (!n)
2813.             return 0;
2814.         for (i = 0; i < n; i++) {
2815.             if (pick_list[i].item.a_int == 'A')
2816.                 loot_everything = TRUE;
2817.             else if (pick_list[i].item.a_int == ALL_TYPES_SELECTED)
2818.                 all_categories = TRUE;
2819.             else
2820.                 add_valid_menu_class(pick_list[i].item.a_int);
2821.         }
2822.         free((genericptr_t) pick_list);
2823.     }
2824. 
2825.     if (loot_everything) {
2826.         if (!put_in) {
2827.             current_container->cknown = 1;
2828.             for (otmp = current_container->cobj; otmp; otmp = otmp2) {
2829.                 otmp2 = otmp->nobj;
2830.                 res = out_container(otmp);
2831.                 if (res < 0)
2832.                     break;
2833.                 n_looted += res;
2834.             }
2835.         } else {
2836.             for (otmp = invent; otmp && current_container; otmp = otmp2) {
2837.                 otmp2 = otmp->nobj;
2838.                 res = in_container(otmp);
2839.                 if (res < 0)
2840.                     break;
2841.                 n_looted += res;
2842.             }
2843.         }
2844.     } else {
2845.         mflags = INVORDER_SORT;
2846.         if (put_in && flags.invlet_constant)
2847.             mflags |= USE_INVLET;
2848.         if (!put_in)
2849.             current_container->cknown = 1;
2850.         Sprintf(buf, "%s what?", action);
2851.         n = query_objlist(buf, put_in ? &invent : &(current_container->cobj),
2852.                           mflags, &pick_list, PICK_ANY,
2853.                           all_categories ? allow_all : allow_category);
2854.         if (n) {
2855.             n_looted = n;
2856.             for (i = 0; i < n; i++) {
2857.                 otmp = pick_list[i].item.a_obj;
2858.                 count = pick_list[i].count;
2859.                 if (count > 0 && count < otmp->quan) {
2860.                     otmp = splitobj(otmp, count);
2861.                     /* special split case also handled by askchain() */
2862.                 }
2863.                 res = put_in ? in_container(otmp) : out_container(otmp);
2864.                 if (res < 0) {
2865.                     if (!current_container) {
2866.                         /* otmp caused current_container to explode;
2867.                            both are now gone */
2868.                         otmp = 0; /* and break loop */
2869.                     } else if (otmp && otmp != pick_list[i].item.a_obj) {
2870.                         /* split occurred, merge again */
2871.                         (void) merged(&pick_list[i].item.a_obj, &otmp);
2872.                     }
2873.                     break;
2874.                 }
2875.             }
2876.             free((genericptr_t) pick_list);
2877.         }
2878.     }
2879.     return n_looted;
2880. }
2881. 
2882. STATIC_OVL char
2883. in_or_out_menu(prompt, obj, outokay, inokay, alreadyused, more_containers)
2884. const char *prompt;
2885. struct obj *obj;
2886. boolean outokay, inokay, alreadyused, more_containers;
2887. {
2888.     /* underscore is not a choice; it's used to skip element [0] */
2889.     static const char lootchars[] = "_:oibrsnq", abc_chars[] = "_:abcdenq";
2890.     winid win;
2891.     anything any;
2892.     menu_item *pick_list;
2893.     char buf[BUFSZ];
2894.     int n;
2895.     const char *menuselector = flags.lootabc ? abc_chars : lootchars;
2896. 
2897.     any = zeroany;
2898.     win = create_nhwindow(NHW_MENU);
2899.     start_menu(win);
2900. 
2901.     any.a_int = 1; /* ':' */
2902.     Sprintf(buf, "Look inside %s", thesimpleoname(obj));
2903.     add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE, buf,
2904.              MENU_UNSELECTED);
2905.     if (outokay) {
2906.         any.a_int = 2; /* 'o' */
2907.         Sprintf(buf, "take %s out", something);
2908.         add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
2909.                  buf, MENU_UNSELECTED);
2910.     }
2911.     if (inokay) {
2912.         any.a_int = 3; /* 'i' */
2913.         Sprintf(buf, "put %s in", something);
2914.         add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
2915.                  buf, MENU_UNSELECTED);
2916.     }
2917.     if (outokay) {
2918.         any.a_int = 4; /* 'b' */
2919.         Sprintf(buf, "%stake out, then put in", inokay ? "both; " : "");
2920.         add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
2921.                  buf, MENU_UNSELECTED);
2922.     }
2923.     if (inokay) {
2924.         any.a_int = 5; /* 'r' */
2925.         Sprintf(buf, "%sput in, then take out",
2926.                 outokay ? "both reversed; " : "");
2927.         add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
2928.                  buf, MENU_UNSELECTED);
2929.         any.a_int = 6; /* 's' */
2930.         Sprintf(buf, "stash one item into %s", thesimpleoname(obj));
2931.         add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
2932.                  buf, MENU_UNSELECTED);
2933.     }
2934.     any.a_int = 0;
2935.     add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE, "", MENU_UNSELECTED);
2936.     if (more_containers) {
2937.         any.a_int = 7; /* 'n' */
2938.         add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE,
2939.                  "loot next container", MENU_SELECTED);
2940.     }
2941.     any.a_int = 8; /* 'q' */
2942.     Strcpy(buf, alreadyused ? "done" : "do nothing");
2943.     add_menu(win, NO_GLYPH, &any, menuselector[any.a_int], 0, ATR_NONE, buf,
2944.              more_containers ? MENU_UNSELECTED : MENU_SELECTED);
2945. 
2946.     end_menu(win, prompt);
2947.     n = select_menu(win, PICK_ONE, &pick_list);
2948.     destroy_nhwindow(win);
2949.     if (n > 0) {
2950.         int k = pick_list[0].item.a_int;
2951. 
2952.         if (n > 1 && k == (more_containers ? 7 : 8))
2953.             k = pick_list[1].item.a_int;
2954.         free((genericptr_t) pick_list);
2955.         return lootchars[k]; /* :,o,i,b,r,s,n,q */
2956.     }
2957.     return (n == 0 && more_containers) ? 'n' : 'q'; /* next or quit */
2958. }
2959. 
2960. static const char tippables[] = { ALL_CLASSES, TOOL_CLASS, 0 };
2961. 
2962. /* #tip command -- empty container contents onto floor */
2963. int
2964. dotip()
2965. {
2966.     struct obj *cobj, *nobj;
2967.     coord cc;
2968.     int boxes;
2969.     char c, buf[BUFSZ], qbuf[BUFSZ];
2970.     const char *spillage = 0;
2971. 
2972.     /*
2973.      * doesn't require free hands;
2974.      * limbs are needed to tip floor containers
2975.      */
2976. 
2977.     /* at present, can only tip things at current spot, not adjacent ones */
2978.     cc.x = u.ux, cc.y = u.uy;
2979. 
2980.     /* check floor container(s) first; at most one will be accessed */
2981.     if ((boxes = container_at(cc.x, cc.y, TRUE)) > 0) {
2982.         Sprintf(buf, "You can't tip %s while carrying so much.",
2983.                 !flags.verbose ? "a container" : (boxes > 1) ? "one" : "it");
2984.         if (!check_capacity(buf) && able_to_loot(cc.x, cc.y, FALSE)) {
2985.             if (boxes > 1 && (flags.menu_style != MENU_TRADITIONAL
2986.                               || iflags.menu_requested)) {
2987.                 /* use menu to pick a container to tip */
2988.                 int n, i;
2989.                 winid win;
2990.                 anything any;
2991.                 menu_item *pick_list = (menu_item *) 0;
2992.                 struct obj dummyobj, *otmp;
2993. 
2994.                 any = zeroany;
2995.                 win = create_nhwindow(NHW_MENU);
2996.                 start_menu(win);
2997. 
2998.                 for (cobj = level.objects[cc.x][cc.y], i = 0; cobj;
2999.                      cobj = cobj->nexthere)
3000.                     if (Is_container(cobj)) {
3001.                         ++i;
3002.                         any.a_obj = cobj;
3003.                         add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE,
3004.                                  doname(cobj), MENU_UNSELECTED);
3005.                     }
3006.                 if (invent) {
3007.                     any = zeroany;
3008.                     add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE,
3009.                              "", MENU_UNSELECTED);
3010.                     any.a_obj = &dummyobj;
3011.                     /* use 'i' for inventory unless there are so many
3012.                        containers that it's already being used */
3013.                     i = (i <= 'i' - 'a' && !flags.lootabc) ? 'i' : 0;
3014.                     add_menu(win, NO_GLYPH, &any, i, 0, ATR_NONE,
3015.                              "tip something being carried", MENU_SELECTED);
3016.                 }
3017.                 end_menu(win, "Tip which container?");
3018.                 n = select_menu(win, PICK_ONE, &pick_list);
3019.                 destroy_nhwindow(win);
3020.                 /*
3021.                  * Deal with quirk of preselected item in pick-one menu:
3022.                  * n ==  0 => picked preselected entry, toggling it off;
3023.                  * n ==  1 => accepted preselected choice via SPACE or RETURN;
3024.                  * n ==  2 => picked something other than preselected entry;
3025.                  * n == -1 => cancelled via ESC;
3026.                  */
3027.                 otmp = (n <= 0) ? (struct obj *) 0 : pick_list[0].item.a_obj;
3028.                 if (n > 1 && otmp == &dummyobj)
3029.                     otmp = pick_list[1].item.a_obj;
3030.                 if (pick_list)
3031.                     free((genericptr_t) pick_list);
3032.                 if (otmp && otmp != &dummyobj) {
3033.                     tipcontainer(otmp);
3034.                     return 1;
3035.                 }
3036.                 if (n == -1)
3037.                     return 0;
3038.                 /* else pick-from-invent below */
3039.             } else {
3040.                 for (cobj = level.objects[cc.x][cc.y]; cobj; cobj = nobj) {
3041.                     nobj = cobj->nexthere;
3042.                     if (!Is_container(cobj))
3043.                         continue;
3044.                     c = ynq(safe_qbuf(qbuf, "There is ", " here, tip it?",
3045.                                       cobj,
3046.                                       doname, ansimpleoname, "container"));
3047.                     if (c == 'q')
3048.                         return 0;
3049.                     if (c == 'n')
3050.                         continue;
3051.                     tipcontainer(cobj);
3052.                     /* can only tip one container at a time */
3053.                     return 1;
3054.                 }
3055.             }
3056.         }
3057.     }
3058. 
3059.     /* either no floor container(s) or couldn't tip one or didn't tip any */
3060.     cobj = getobj(tippables, "tip");
3061.     if (!cobj)
3062.         return 0;
3063. 
3064.     /* normal case */
3065.     if (Is_container(cobj) || cobj->otyp == HORN_OF_PLENTY) {
3066.         tipcontainer(cobj);
3067.         return 1;
3068.     }
3069.     /* assorted other cases */
3070.     if (Is_candle(cobj) && cobj->lamplit) {
3071.         /* note "wax" even for tallow candles to avoid giving away info */
3072.         spillage = "wax";
3073.     } else if ((cobj->otyp == POT_OIL && cobj->lamplit)
3074.                || (cobj->otyp == OIL_LAMP && cobj->age != 0L)
3075.                || (cobj->otyp == MAGIC_LAMP && cobj->spe != 0)) {
3076.         spillage = "oil";
3077.         /* todo: reduce potion's remaining burn timer or oil lamp's fuel */
3078.     } else if (cobj->otyp == CAN_OF_GREASE && cobj->spe > 0) {
3079.         /* charged consumed below */
3080.         spillage = "grease";
3081.     } else if (cobj->otyp == FOOD_RATION || cobj->otyp == CRAM_RATION
3082.                || cobj->otyp == LEMBAS_WAFER) {
3083.         spillage = "crumbs";
3084.     } else if (cobj->oclass == VENOM_CLASS) {
3085.         spillage = "venom";
3086.     }
3087.     if (spillage) {
3088.         buf[0] = '\0';
3089.         if (is_pool(u.ux, u.uy))
3090.             Sprintf(buf, " and gradually %s", vtense(spillage, "dissipate"));
3091.         else if (is_lava(u.ux, u.uy))
3092.             Sprintf(buf, " and immediately %s away",
3093.                     vtense(spillage, "burn"));
3094.         pline("Some %s %s onto the %s%s.", spillage,
3095.               vtense(spillage, "spill"), surface(u.ux, u.uy), buf);
3096.         /* shop usage message comes after the spill message */
3097.         if (cobj->otyp == CAN_OF_GREASE && cobj->spe > 0) {
3098.             consume_obj_charge(cobj, TRUE);
3099.         }
3100.         /* something [useless] happened */
3101.         return 1;
3102.     }
3103.     /* anything not covered yet */
3104.     if (cobj->oclass == POTION_CLASS) /* can't pour potions... */
3105.         pline_The("%s %s securely sealed.", xname(cobj), otense(cobj, "are"));
3106.     else if (cobj->otyp == STATUE)
3107.         pline("Nothing interesting happens.");
3108.     else
3109.         pline1(nothing_happens);
3110.     return 0;
3111. }
3112. 
3113. STATIC_OVL void
3114. tipcontainer(box)
3115. struct obj *box; /* or bag */
3116. {
3117.     xchar ox = u.ux, oy = u.uy; /* #tip only works at hero's location */
3118.     boolean empty_it = FALSE, maybeshopgoods;
3119. 
3120.     /* box is either held or on floor at hero's spot; no need to check for
3121.        nesting; when held, we need to update its location to match hero's;
3122.        for floor, the coordinate updating is redundant */
3123.     if (get_obj_location(box, &ox, &oy, 0))
3124.         box->ox = ox, box->oy = oy;
3125. 
3126.     /* Shop handling:  can't rely on the container's own unpaid
3127.        or no_charge status because contents might differ with it.
3128.        A carried container's contents will be flagged as unpaid
3129.        or not, as appropriate, and need no special handling here.
3130.        Items owned by the hero get sold to the shop without
3131.        confirmation as with other uncontrolled drops.  A floor
3132.        container's contents will be marked no_charge if owned by
3133.        hero, otherwise they're owned by the shop.  By passing
3134.        the contents through shop billing, they end up getting
3135.        treated the same as in the carried case.   We do so one
3136.        item at a time instead of doing whole container at once
3137.        to reduce the chance of exhausting shk's billing capacity. */
3138.     maybeshopgoods = !carried(box) && costly_spot(box->ox, box->oy);
3139. 
3140.     /* caveat: this assumes that cknown, lknown, olocked, and otrapped
3141.        fields haven't been overloaded to mean something special for the
3142.        non-standard "container" horn of plenty */
3143.     if (!box->lknown) {
3144.         box->lknown = 1;
3145.         if (carried(box))
3146.             update_inventory(); /* jumping the gun slightly; hope that's ok */
3147.     }
3148.     if (box->olocked) {
3149.         pline("It's locked.");
3150.     } else if (box->otrapped) {
3151.         /* we're not reaching inside but we're still handling it... */
3152.         (void) chest_trap(box, HAND, FALSE);
3153.         /* even if the trap fails, you've used up this turn */
3154.         if (multi >= 0) { /* in case we didn't become paralyzed */
3155.             nomul(-1);
3156.             multi_reason = "tipping a container";
3157.             nomovemsg = "";
3158.         }
3159.     } else if (box->otyp == BAG_OF_TRICKS || box->otyp == HORN_OF_PLENTY) {
3160.         boolean bag = box->otyp == BAG_OF_TRICKS;
3161.         int old_spe = box->spe, seen = 0;
3162. 
3163.         if (maybeshopgoods && !box->no_charge)
3164.             addtobill(box, FALSE, FALSE, TRUE);
3165.         /* apply this bag/horn until empty or monster/object creation fails
3166.            (if the latter occurs, force the former...) */
3167.         do {
3168.             if (!(bag ? bagotricks(box, TRUE, &seen)
3169.                       : hornoplenty(box, TRUE)))
3170.                 break;
3171.         } while (box->spe > 0);
3172. 
3173.         if (box->spe < old_spe) {
3174.             if (bag)
3175.                 pline((seen == 0) ? "Nothing seems to happen."
3176.                                   : (seen == 1) ? "A monster appears."
3177.                                                 : "Monsters appear!");
3178.             /* check_unpaid wants to see a non-zero charge count */
3179.             box->spe = old_spe;
3180.             check_unpaid_usage(box, TRUE);
3181.             box->spe = 0; /* empty */
3182.             box->cknown = 1;
3183.         }
3184.         if (maybeshopgoods && !box->no_charge)
3185.             subfrombill(box, shop_keeper(*in_rooms(ox, oy, SHOPBASE)));
3186.     } else if (SchroedingersBox(box)) {
3187.         char yourbuf[BUFSZ];
3188. 
3189.         observe_quantum_cat(box, TRUE, TRUE);
3190.         if (!Has_contents(box)) /* evidently a live cat came out */
3191.             /* container type of "large box" is inferred */
3192.             pline("%sbox is now empty.", Shk_Your(yourbuf, box));
3193.         else /* holds cat corpse */
3194.             empty_it = TRUE;
3195.         box->cknown = 1;
3196.     } else if (!Has_contents(box)) {
3197.         box->cknown = 1;
3198.         pline("It's empty.");
3199.     } else {
3200.         empty_it = TRUE;
3201.     }
3202. 
3203.     if (empty_it) {
3204.         struct obj *otmp, *nobj;
3205.         boolean terse, highdrop = !can_reach_floor(TRUE),
3206.                 altarizing = IS_ALTAR(levl[ox][oy].typ),
3207.                 cursed_mbag = (Is_mbag(box) && box->cursed);
3208.         int held = carried(box);
3209.         long loss = 0L;
3210. 
3211.         if (u.uswallow)
3212.             highdrop = altarizing = FALSE;
3213.         terse = !(highdrop || altarizing || costly_spot(box->ox, box->oy));
3214.         box->cknown = 1;
3215.         /* Terse formatting is
3216.          * "Objects spill out: obj1, obj2, obj3, ..., objN."
3217.          * If any other messages intervene between objects, we revert to
3218.          * "ObjK drops to the floor.", "ObjL drops to the floor.", &c.
3219.          */
3220.         pline("%s out%c",
3221.               box->cobj->nobj ? "Objects spill" : "An object spills",
3222.               terse ? ':' : '.');
3223.         for (otmp = box->cobj; otmp; otmp = nobj) {
3224.             nobj = otmp->nobj;
3225.             obj_extract_self(otmp);
3226.             otmp->ox = box->ox, otmp->oy = box->oy;
3227. 
3228.             if (box->otyp == ICE_BOX) {
3229.                 removed_from_icebox(otmp); /* resume rotting for corpse */
3230.             } else if (cursed_mbag && !rn2(13)) {
3231.                 loss += mbag_item_gone(held, otmp);
3232.                 /* abbreviated drop format is no longer appropriate */
3233.                 terse = FALSE;
3234.                 continue;
3235.             }
3236. 
3237.             if (maybeshopgoods) {
3238.                 addtobill(otmp, FALSE, FALSE, TRUE);
3239.                 iflags.suppress_price++; /* doname formatting */
3240.             }
3241. 
3242.             if (highdrop) {
3243.                 /* might break or fall down stairs; handles altars itself */
3244.                 hitfloor(otmp, TRUE);
3245.             } else {
3246.                 if (altarizing) {
3247.                     doaltarobj(otmp);
3248.                 } else if (!terse) {
3249.                     pline("%s %s to the %s.", Doname2(otmp),
3250.                           otense(otmp, "drop"), surface(ox, oy));
3251.                 } else {
3252.                     pline("%s%c", doname(otmp), nobj ? ',' : '.');
3253.                     iflags.last_msg = PLNMSG_OBJNAM_ONLY;
3254.                 }
3255.                 dropy(otmp);
3256.                 if (iflags.last_msg != PLNMSG_OBJNAM_ONLY)
3257.                     terse = FALSE; /* terse formatting has been interrupted */
3258.             }
3259.             if (maybeshopgoods)
3260.                 iflags.suppress_price--; /* reset */
3261.         }
3262.         if (loss) /* magic bag lost some shop goods */
3263.             You("owe %ld %s for lost merchandise.", loss, currency(loss));
3264.         box->owt = weight(box); /* mbag_item_gone() doesn't update this */
3265.         if (held)
3266.             (void) encumber_msg();
3267.     }
3268.     if (carried(box)) /* box is now empty with cknown set */
3269.         update_inventory();
3270. }
3271. 
3272. /*pickup.c*/