Source:NetHack 5.0.0/src/pickup.c
Jump to navigation
Jump to search
Below is the full text to pickup.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/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 5.0 pickup.c $NHDT-Date: 1773373633 2026/03/12 19:47:13 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.386 $ */ 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. staticfn void simple_look(struct obj *, boolean); 15. staticfn boolean query_classes(char *, boolean *, boolean *, const char *, 16. struct obj *, boolean, int *); 17. staticfn boolean fatal_corpse_mistake(struct obj *, boolean); 18. staticfn boolean describe_decor(void); 19. staticfn void check_here(boolean); 20. staticfn boolean n_or_more(struct obj *); 21. staticfn boolean all_but_uchain(struct obj *); 22. #if 0 /* not used */ 23. staticfn boolean allow_cat_no_uchain(struct obj *); 24. #endif 25. staticfn int autopick(struct obj *, int, menu_item **); 26. staticfn int count_categories(struct obj *, int); 27. staticfn int delta_cwt(struct obj *, struct obj *); 28. staticfn long carry_count(struct obj *, struct obj *, long, boolean, int *, 29. int *); 30. staticfn int lift_object(struct obj *, struct obj *, long *, boolean); 31. staticfn void pickup_prinv(struct obj *, long, const char *); 32. staticfn boolean mbag_explodes(struct obj *, int); 33. staticfn boolean is_boh_item_gone(void); 34. staticfn void do_boh_explosion(struct obj *, boolean); 35. staticfn long boh_loss(struct obj *, boolean); 36. staticfn int in_container(struct obj *); 37. staticfn int out_container(struct obj *); 38. staticfn long mbag_item_gone(boolean, struct obj *, boolean); 39. staticfn int stash_ok(struct obj *); 40. staticfn void explain_container_prompt(boolean); 41. staticfn int traditional_loot(boolean); 42. staticfn int menu_loot(int, boolean); 43. staticfn int tip_ok(struct obj *); 44. staticfn int choose_tip_container_menu(void); 45. staticfn struct obj *tipcontainer_gettarget(struct obj *, boolean *); 46. staticfn int tipcontainer_checks(struct obj *, struct obj *, boolean); 47. staticfn char in_or_out_menu(const char *, struct obj *, boolean, boolean, 48. boolean, boolean); 49. staticfn boolean able_to_loot(coordxy, coordxy, boolean); 50. staticfn boolean reverse_loot(void); 51. staticfn boolean mon_beside(coordxy, coordxy); 52. staticfn int do_loot_cont(struct obj **, int, int); 53. staticfn int doloot_core(void); 54. staticfn void tipcontainer(struct obj *); 55. 56. /* define for query_objlist() and autopickup() */ 57. #define FOLLOW(curr, flags) \ 58. (((flags) & BY_NEXTHERE) ? (curr)->nexthere : (curr)->nobj) 59. 60. #define GOLD_WT(n) (((n) + 50L) / 100L) 61. /* if you can figure this out, give yourself a hearty pat on the back... */ 62. #define GOLD_CAPACITY(w, n) (((w) * -100L) - ((n) + 50L) - 1L) 63. 64. #define Icebox (gc.current_container->otyp == ICE_BOX) 65. 66. static const char 67. slightloadpfx[] = "You have a little trouble", 68. moderateloadpfx[] = "You have trouble", 69. nearloadpfx[] = "You have much trouble", 70. overloadpfx[] = "You have extreme difficulty"; 71. 72. /* BUG: this lets you look at cockatrice corpses while blind without 73. touching them */ 74. /* much simpler version of the look-here code; used by query_classes() */ 75. staticfn void 76. simple_look(struct obj *otmp, /* list of objects */ 77. boolean here) /* flag for type of obj list linkage */ 78. { 79. /* Neither of the first two cases is expected to happen, since 80. * we're only called after multiple classes of objects have been 81. * detected, hence multiple objects must be present. 82. */ 83. if (!otmp) { 84. impossible("simple_look(null)"); 85. } else if (!(here ? otmp->nexthere : otmp->nobj)) { 86. pline1(doname(otmp)); 87. } else { 88. winid tmpwin = create_nhwindow(NHW_MENU); 89. 90. putstr(tmpwin, 0, ""); 91. do { 92. putstr(tmpwin, 0, doname(otmp)); 93. otmp = here ? otmp->nexthere : otmp->nobj; 94. } while (otmp); 95. display_nhwindow(tmpwin, TRUE); 96. destroy_nhwindow(tmpwin); 97. } 98. } 99. 100. int 101. collect_obj_classes(char ilets[], struct obj *otmp, boolean here, 102. boolean (*filter)(OBJ_P), int *itemcount) 103. { 104. int iletct = 0; 105. char c; 106. 107. *itemcount = 0; 108. ilets[iletct] = '\0'; /* terminate ilets so that strchr() will work */ 109. while (otmp) { 110. c = def_oc_syms[(int) otmp->oclass].sym; 111. if (!strchr(ilets, c) && (!filter || (*filter)(otmp))) 112. ilets[iletct++] = c, ilets[iletct] = '\0'; 113. *itemcount += 1; 114. otmp = here ? otmp->nexthere : otmp->nobj; 115. } 116. 117. return iletct; 118. } 119. 120. /* 121. * For menustyle:Traditional and menustyle:Combination. 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. * Note: the behavior and meaning of 'a' vs 'A' is effectively reversed 135. * when using menustyle:Full. For Traditional, the choice is based on 136. * ease of typing (using 'a' is much more common than 'A'); for Full, 137. * it was changed to enhance menu entry ordering ('A' stands out, but 138. * some players complain that it is too easy to choose accidentally). 139. */ 140. staticfn boolean 141. query_classes( 142. char oclasses[], /* selected classes */ 143. boolean *one_at_a_time, /* to tell caller that user picked 'A' */ 144. boolean *everything, /* to tell caller that user picked 'a' */ 145. const char *action, /* verb for what activity needs objects */ 146. struct obj *objs, /* invent or container->cobj or level.objects[x][y] */ 147. boolean here, /* True: traverse by obj->nexthere; False: by obj->nobj */ 148. int *menu_on_demand) /* to tell caller that user picked 'm' */ 149. { 150. char ilets[36], inbuf[BUFSZ] = DUMMY; /* FIXME: hardcoded ilets[] length */ 151. int iletct, oclassct; 152. boolean not_everything, filtered; 153. char qbuf[QBUFSZ]; 154. boolean m_seen; 155. int itemcount, bcnt, ucnt, ccnt, xcnt, ocnt, jcnt; 156. 157. oclasses[oclassct = 0] = '\0'; 158. *one_at_a_time = *everything = m_seen = FALSE; 159. if (menu_on_demand) 160. *menu_on_demand = 0; 161. iletct = collect_obj_classes(ilets, objs, here, 162. (boolean (*)(OBJ_P)) 0, &itemcount); 163. if (iletct == 0) 164. return FALSE; 165. 166. if (iletct == 1) { 167. oclasses[0] = def_char_to_objclass(ilets[0]); 168. oclasses[1] = '\0'; 169. } else { /* more than one choice available */ 170. /* additional choices */ 171. ilets[iletct++] = ' '; 172. ilets[iletct++] = 'a'; 173. ilets[iletct++] = 'A'; 174. ilets[iletct++] = (objs == gi.invent ? 'i' : ':'); 175. } 176. if (itemcount && menu_on_demand) 177. ilets[iletct++] = 'm'; 178. if (count_unpaid(objs)) 179. ilets[iletct++] = 'u'; 180. 181. tally_BUCX(objs, here, &bcnt, &ucnt, &ccnt, &xcnt, &ocnt, &jcnt); 182. if (bcnt) 183. ilets[iletct++] = 'B'; 184. if (ucnt) 185. ilets[iletct++] = 'U'; 186. if (ccnt) 187. ilets[iletct++] = 'C'; 188. if (xcnt) 189. ilets[iletct++] = 'X'; 190. if (jcnt) 191. ilets[iletct++] = 'P'; 192. ilets[iletct] = '\0'; 193. 194. if (iletct > 1) { 195. const char *where = 0; 196. char sym, oc_of_sym, *p; 197. 198. ask_again: 199. oclasses[oclassct = 0] = '\0'; 200. *one_at_a_time = *everything = FALSE; 201. not_everything = filtered = FALSE; 202. Sprintf(qbuf, "What kinds of thing do you want to %s? [%s]", action, 203. ilets); 204. getlin(qbuf, inbuf); 205. if (*inbuf == '\033') 206. return FALSE; 207. 208. for (p = inbuf; (sym = *p++) != 0; ) { 209. if (sym == ' ') 210. continue; 211. else if (sym == 'A') 212. *one_at_a_time = TRUE; 213. else if (sym == 'a') 214. *everything = TRUE; 215. else if (sym == ':') { 216. simple_look(objs, here); /* dumb if objs==invent */ 217. /* if we just scanned the contents of a container 218. then mark it as having known contents */ 219. if (objs->where == OBJ_CONTAINED) 220. objs->ocontainer->cknown = 1; 221. goto ask_again; 222. } else if (sym == 'i') { 223. (void) display_inventory((char *) 0, TRUE); 224. goto ask_again; 225. } else if (sym == 'm') { 226. m_seen = TRUE; 227. } else if (strchr("uBUCXP", sym)) { 228. add_valid_menu_class(sym); /* 'u' or 'B','U','C','X','P' */ 229. filtered = TRUE; 230. } else { 231. oc_of_sym = def_char_to_objclass(sym); 232. if (strchr(ilets, sym)) { 233. add_valid_menu_class(oc_of_sym); 234. oclasses[oclassct++] = oc_of_sym; 235. oclasses[oclassct] = '\0'; 236. } else { 237. if (!where) 238. where = !strcmp(action, "pick up") ? "here" 239. : !strcmp(action, "take out") ? "inside" : ""; 240. if (*where) 241. There("are no %c's %s.", sym, where); 242. else 243. You("have no %c's.", sym); 244. not_everything = TRUE; 245. } 246. } 247. } /* for p:sym in inbuf */ 248. 249. if (m_seen && menu_on_demand) { 250. *menu_on_demand = (((*everything || !oclassct) && !filtered) 251. ? -2 : -3); 252. return FALSE; 253. } 254. if (!oclassct && (!*everything || not_everything)) { 255. /* didn't pick anything, 256. or tried to pick something that's not present */ 257. *one_at_a_time = TRUE; /* force 'A' */ 258. *everything = FALSE; /* inhibit 'a' */ 259. } 260. } 261. return TRUE; 262. } 263. 264. /* 265. * tests: 266. * st_gloves wearing gloves? 267. * st_corpse is it a corpse obj? 268. * st_petrifies does the corpse petrify on touch? 269. * st_resists does hero have stoning resistance? 270. * st_all st_gloves | st_corpse | st_petrifies | st_resists 271. */ 272. boolean 273. u_safe_from_fatal_corpse(struct obj *obj, int tests) 274. { 275. if (((tests & st_gloves) && uarmg) 276. || ((tests & st_corpse) && obj->otyp != CORPSE) 277. || ((tests & st_petrifies) && !touch_petrifies(&mons[obj->corpsenm])) 278. || ((tests & st_resists) && Stone_resistance)) 279. return TRUE; 280. return FALSE; 281. } 282. 283. /* check whether hero is bare-handedly touching a cockatrice corpse */ 284. staticfn boolean 285. fatal_corpse_mistake(struct obj *obj, boolean remotely) 286. { 287. if (u_safe_from_fatal_corpse(obj, st_all) || remotely) 288. return FALSE; 289. 290. if (poly_when_stoned(gy.youmonst.data) && polymon(PM_STONE_GOLEM)) { 291. display_nhwindow(WIN_MESSAGE, FALSE); /* --More-- */ 292. return FALSE; 293. } 294. 295. pline("Touching %s is a fatal mistake.", 296. corpse_xname(obj, (const char *) 0, CXN_SINGULAR | CXN_ARTICLE)); 297. instapetrify(killer_xname(obj)); 298. return TRUE; 299. } 300. 301. /* attempting to manipulate a Rider's corpse triggers its revival */ 302. boolean 303. rider_corpse_revival(struct obj *obj, boolean remotely) 304. { 305. if (!obj || obj->otyp != CORPSE || !is_rider(&mons[obj->corpsenm])) 306. return FALSE; 307. 308. pline("At your %s, the corpse suddenly moves...", 309. remotely ? "attempted acquisition" : "touch"); 310. (void) revive_corpse(obj); 311. exercise(A_WIS, FALSE); 312. return TRUE; 313. } 314. 315. /* wand of probing zapped down; perhaps hero is levitating while blind */ 316. void 317. force_decor(boolean via_probing) 318. { 319. /* we don't want describe_decor() to defer feedback if hero is fumbling 320. with 1 turn left until next slip_or_trip(), or for ice_descr() to 321. omit thawing details if hero is probing when levitating while blind 322. (those will be skipped for look_here() and farlook() or autodescribe); 323. we can't control that by temporarily tweaking properties because that 324. could become noticeable if status gets updated while decor feedback 325. is being delivered */ 326. gd.decor_fumble_override = TRUE; 327. gd.decor_levitate_override = via_probing; 328. /* force current terrain to be different from previous location, or 329. uninteresting if previous location was actually inside solid stone */ 330. iflags.prev_decor = STONE; 331. (void) describe_decor(); 332. gd.decor_fumble_override = gd.decor_levitate_override = FALSE; 333. svl.lastseentyp[u.ux][u.uy] = levl[u.ux][u.uy].typ; 334. } 335. 336. void 337. deferred_decor( 338. boolean setup) /* True: deferring, False: catching up */ 339. { 340. if (!flags.mention_decor) { 341. iflags.defer_decor = FALSE; 342. } else if (setup) { 343. iflags.defer_decor = TRUE; 344. } else { 345. (void) describe_decor(); 346. iflags.defer_decor = FALSE; 347. } 348. } 349. 350. /* handle 'mention_decor' (when walking onto a dungeon feature such as 351. stairs or altar, describe it even if it isn't covered up by an object) */ 352. staticfn boolean 353. describe_decor(void) 354. { 355. char outbuf[BUFSZ], fbuf[QBUFSZ]; 356. boolean doorhere, waterhere, res = TRUE; 357. const char *dfeature; 358. int ltyp; 359. 360. if ((HFumbling & TIMEOUT) == 1L /* about to slip_or_trip */ 361. && !iflags.defer_decor 362. && !gd.decor_fumble_override) { /* probe_decor() */ 363. /* 364. * Work around a message sequencing issue if Fumbling's periodic 365. * timeout is about to kick in: avoid the combination 366. * |You are back on floor. 367. * |You trip over <object>. or You flounder. 368. * when the trip is being caused by moving on ice as hero 369. * steps off ice onto non-ice. Defer the back-on-floor part if 370. * that is about to happen. 371. */ 372. deferred_decor(TRUE); 373. return FALSE; 374. } 375. 376. ltyp = SURFACE_AT(u.ux, u.uy); 377. dfeature = dfeature_at(u.ux, u.uy, fbuf); 378. 379. /* we don't mention "ordinary" doors but do mention broken ones (and 380. closed ones, which will only happen for Passes_walls) */ 381. doorhere = dfeature && (!strcmp(dfeature, "open door") 382. || !strcmp(dfeature, "doorway")); 383. waterhere = dfeature && !strcmp(dfeature, "pool of water"); 384. if (doorhere || Underwater 385. || (ltyp == ICE && IS_POOL(iflags.prev_decor))) /* pooleffects() */ 386. dfeature = 0; 387. /* 388. * TODO: if on ice, report moving between thicker and thinner ice (based 389. * on ice_descr()'s classification) as if moving onto different terrain. 390. */ 391. 392. if (ltyp == iflags.prev_decor && !IS_FURNITURE(ltyp)) { 393. res = FALSE; 394. } else if (dfeature) { 395. if (waterhere) 396. dfeature = strcpy(fbuf, waterbody_name(u.ux, u.uy)); 397. if (strcmp(dfeature, "swamp") && ltyp != ICE) 398. dfeature = an(dfeature); 399. 400. if (flags.verbose) { 401. Sprintf(outbuf, "There is %s here.", dfeature); 402. } else { 403. if (dfeature != fbuf) 404. Strcpy(fbuf, dfeature); 405. Sprintf(outbuf, "%s.", upstart(fbuf)); 406. } 407. if (ltyp == ICE && flags.mention_decor) 408. Norep("%s", outbuf); 409. else 410. pline("%s", outbuf); 411. } else if (!Underwater) { 412. if (IS_POOL(iflags.prev_decor) 413. || IS_LAVA(iflags.prev_decor) 414. || iflags.prev_decor == ICE) { 415. if (iflags.last_msg != PLNMSG_BACK_ON_GROUND) { 416. back_on_ground(FALSE); 417. } 418. } 419. } 420. /* describe_decor() is normally called when moving onto a different 421. type of terrain, but it is also called by pickup() even when 422. mention_decor is Off if hero can't reach floor; only adapt the next 423. describe_decor() by what has just occurred in this one when it's On */ 424. iflags.prev_decor = flags.mention_decor ? ltyp : STONE; 425. return res; 426. } 427. 428. /* look at the objects at our location, unless there are too many of them */ 429. staticfn void 430. check_here(boolean picked_some) 431. { 432. struct obj *obj; 433. int ct = 0; 434. unsigned lhflags = picked_some ? LOOKHERE_PICKED_SOME : LOOKHERE_NOFLAGS; 435. 436. if (flags.mention_decor) { 437. if (describe_decor()) 438. lhflags |= LOOKHERE_SKIP_DFEATURE; 439. } 440. 441. /* count the objects here */ 442. for (obj = svl.level.objects[u.ux][u.uy]; obj; obj = obj->nexthere) { 443. if (obj != uchain) 444. ct++; 445. } 446. 447. /* If there are objects here, take a look. */ 448. if (ct) { 449. if (svc.context.run) 450. nomul(0); 451. flush_screen(1); 452. (void) look_here(ct, lhflags); 453. } else { 454. read_engr_at(u.ux, u.uy); 455. } 456. } 457. 458. /* query_objlist callback: return TRUE if obj's count is >= reference value */ 459. staticfn boolean 460. n_or_more(struct obj *obj) 461. { 462. if (obj == uchain) 463. return FALSE; 464. return (boolean) (obj->quan >= gv.val_for_n_or_more); 465. } 466. 467. /* check valid_menu_classes[] for an entry; also used by askchain() */ 468. boolean 469. menu_class_present(int c) 470. { 471. return (c && strchr(gv.valid_menu_classes, c)) ? TRUE : FALSE; 472. } 473. 474. void 475. add_valid_menu_class(int c) 476. { 477. static int vmc_count = 0; 478. 479. if (c == 0) { /* reset */ 480. vmc_count = 0; 481. gc.class_filter = gb.bucx_filter = gs.shop_filter = FALSE; 482. gp.picked_filter = FALSE; 483. } else if (!menu_class_present(c)) { 484. gv.valid_menu_classes[vmc_count++] = (char) c; 485. /* categorize the new class */ 486. switch (c) { 487. case 'B': 488. case 'U': 489. case 'C': /*FALLTHRU*/ 490. case 'X': 491. gb.bucx_filter = TRUE; 492. break; 493. case 'P': 494. gp.picked_filter = TRUE; 495. break; 496. case 'u': 497. gs.shop_filter = TRUE; 498. break; 499. default: 500. gc.class_filter = TRUE; 501. break; 502. } 503. } 504. gv.valid_menu_classes[vmc_count] = '\0'; 505. } 506. 507. /* query_objlist callback: return TRUE if not uchain */ 508. staticfn boolean 509. all_but_uchain(struct obj *obj) 510. { 511. return (boolean) (obj != uchain); 512. } 513. 514. /* query_objlist callback: return TRUE */ 515. /*ARGUSED*/ 516. boolean 517. allow_all(struct obj *obj UNUSED) 518. { 519. return TRUE; 520. } 521. 522. boolean 523. allow_category(struct obj *obj) 524. { 525. /* If no filters are active, nothing will match unless 526. paranoid_confirm:A is set. */ 527. if (!gc.class_filter && !gs.shop_filter && !gb.bucx_filter 528. && !gp.picked_filter && !ParanoidAutoAll) 529. return FALSE; 530. 531. /* For coins, if any class filter is specified, accept if coins 532. * are included regardless of whether either unpaid or BUC-status 533. * is also specified since player has explicitly requested coins. 534. */ 535. if (obj->oclass == COIN_CLASS && gc.class_filter) 536. return strchr(gv.valid_menu_classes, COIN_CLASS) ? TRUE : FALSE; 537. 538. if (Role_if(PM_CLERIC) && !obj->bknown) 539. set_bknown(obj, 1); 540. 541. /* 542. * Version 3.6 had three types of filters possible and the first 543. * and third can have more than one entry: 544. * 1) object class (armor, potion, &c); 545. * 2) unpaid shop item; 546. * 3) bless/curse state (blessed, uncursed, cursed, BUC-unknown). 547. * Version 5.0 added a fourth: 548. * 4) 'novelty' ('P' for just picked up items). 549. * When only one type is present, the situation is simple: 550. * to be accepted, obj's status must match one of the entries. 551. * When more than one type is present, the obj will now only 552. * be accepted when it matches one entry of each type. 553. * So ?!B will accept blessed scrolls or potions, and [u will 554. * accept unpaid armor. (In 3.4.3, an object was accepted by 555. * this filter if it met any entry of any type, so ?!B resulted 556. * in accepting all scrolls and potions regardless of bless/curse 557. * state plus all blessed non-scroll, non-potion objects.) 558. */ 559. 560. /* if class is expected but obj's class is not in the list, reject */ 561. if (gc.class_filter && !strchr(gv.valid_menu_classes, obj->oclass)) 562. return FALSE; 563. /* if unpaid is expected and obj isn't unpaid, reject (treat a container 564. holding any unpaid object as unpaid even if isn't unpaid itself) */ 565. if (gs.shop_filter && !obj->unpaid 566. && !(Has_contents(obj) && count_unpaid(obj->cobj) > 0)) 567. return FALSE; 568. /* check for particular bless/curse state */ 569. if (gb.bucx_filter) { 570. /* first categorize this object's bless/curse state */ 571. char bucx; 572. if (obj->oclass == COIN_CLASS) { 573. /* If no class filtering is specified but bless/curse state is, 574. coins are treated as either unknown or uncursed based on an 575. option setting. */ 576. bucx = flags.goldX ? 'X' : 'U'; 577. } else { 578. bucx = !obj->bknown ? 'X' 579. : obj->blessed ? 'B' 580. : obj->cursed ? 'C' 581. : 'U'; 582. } 583. 584. /* if its category is not in the list, reject */ 585. if (!strchr(gv.valid_menu_classes, bucx)) 586. return FALSE; 587. } 588. if (gp.picked_filter && !obj->pickup_prev) 589. return FALSE; 590. /* obj didn't fail any of the filter checks, so accept */ 591. return TRUE; 592. } 593. 594. #if 0 /* not used */ 595. /* query_objlist callback: return TRUE if valid category (class), no uchain */ 596. staticfn boolean 597. allow_cat_no_uchain(struct obj *obj) 598. { 599. if (obj != uchain 600. && ((strchr(gv.valid_menu_classes, 'u') && obj->unpaid) 601. || strchr(gv.valid_menu_classes, obj->oclass))) 602. return TRUE; 603. return FALSE; 604. } 605. #endif 606. 607. /* query_objlist callback: return TRUE if valid class and worn */ 608. boolean 609. is_worn_by_type(struct obj *otmp) 610. { 611. return (is_worn(otmp) && allow_category(otmp)) ? TRUE : FALSE; 612. } 613. 614. /* reset last-picked-up flags */ 615. void 616. reset_justpicked(struct obj *olist) 617. { 618. struct obj *otmp; 619. /* 620. * TODO? Possible enhancement: don't reset if hero is still at same 621. * spot where most recent pickup took place. Not resetting will be 622. * the correct behavior for autopickup immediately followed by manual 623. * pickup. It would probably be correct for either or both pickups 624. * followed by manual pickup of a newly arrived missile after some 625. * time has elapsed. Things becomes murkier for other activity. 626. * Taking anything out of a container ought to be treated as if 627. * having moved to another spot. 628. */ 629. 630. for (otmp = olist; otmp; otmp = otmp->nobj) 631. otmp->pickup_prev = 0; 632. } 633. 634. int 635. count_justpicked(struct obj *olist) 636. { 637. struct obj *otmp; 638. int cnt = 0; 639. 640. for (otmp = olist; otmp; otmp = otmp->nobj) 641. if (otmp->pickup_prev) 642. cnt++; 643. 644. return cnt; 645. } 646. 647. struct obj * 648. find_justpicked(struct obj *olist) 649. { 650. struct obj *otmp; 651. 652. for (otmp = olist; otmp; otmp = otmp->nobj) 653. if (otmp->pickup_prev) 654. return otmp; 655. 656. return (struct obj *) 0; 657. } 658. 659. /* 660. * Have the hero pick things from the ground 661. * or a monster's inventory if swallowed. 662. * 663. * Arg what: 664. * >0 autopickup 665. * =0 interactive 666. * <0 pickup count of something 667. * 668. * Returns 1 if tried to pick something up, whether 669. * or not it succeeded. 670. */ 671. int 672. pickup(int what) /* should be a long */ 673. { 674. int i, n, res, count, n_tried = 0, n_picked = 0; 675. menu_item *pick_list = (menu_item *) 0; 676. boolean autopickup = what > 0; 677. struct obj **objchain_p; 678. int traverse_how; 679. 680. /* we might have arrived here while fainted or sleeping, via 681. random teleport or levitation timeout; if so, skip check_here 682. and read_engr_at in addition to bypassing autopickup itself 683. [probably ought to check whether hero is using a cockatrice 684. corpse for a pillow here... (also at initial faint/sleep)] */ 685. if (autopickup && gm.multi < 0 && unconscious()) { 686. iflags.prev_decor = STONE; 687. return 0; 688. } 689. 690. /* used by pickup_object() for encumbrance feedback */ 691. gp.pickup_encumbrance = 0; 692. 693. if (what < 0) /* pick N of something */ 694. count = -what; 695. else /* pick anything */ 696. count = 0; 697. 698. if (!u.uswallow) { 699. struct trap *t; 700. 701. /* no auto-pick if no-pick move, nothing there, or in a pool */ 702. if (autopickup && (svc.context.nopick || !OBJ_AT(u.ux, u.uy) 703. || (is_pool(u.ux, u.uy) && !Underwater) 704. || is_lava(u.ux, u.uy))) { 705. if (flags.mention_decor) 706. (void) describe_decor(); 707. read_engr_at(u.ux, u.uy); 708. return 0; 709. } 710. /* no pickup if levitating & not on air or water level */ 711. t = t_at(u.ux, u.uy); 712. if (!can_reach_floor(t && is_pit(t->ttyp))) { 713. (void) describe_decor(); /* even when !flags.mention_decor */ 714. if ((gm.multi && !svc.context.run) 715. || (autopickup && !flags.pickup) 716. || (t && (uteetering_at_seen_pit(t) || uescaped_shaft(t)))) 717. read_engr_at(u.ux, u.uy); 718. return 0; 719. } 720. /* multi && !svc.context.run means they are in the middle of some 721. * other action, or possibly paralyzed, sleeping, etc.... and they 722. * just teleported onto the object. They shouldn't pick it up. 723. */ 724. if ((gm.multi && !svc.context.run) 725. || (autopickup && !flags.pickup) 726. || notake(gy.youmonst.data)) { 727. check_here(FALSE); 728. if (notake(gy.youmonst.data) && OBJ_AT(u.ux, u.uy) 729. && (autopickup || flags.pickup)) 730. You("are physically incapable of picking anything up."); 731. return 0; 732. } 733. 734. /* if there's anything here, stop running */ 735. if (OBJ_AT(u.ux, u.uy) && svc.context.run && svc.context.run != 8 736. && !svc.context.nopick) 737. nomul(0); 738. } 739. 740. add_valid_menu_class(0); /* reset */ 741. if (!u.uswallow) { 742. objchain_p = &svl.level.objects[u.ux][u.uy]; 743. traverse_how = BY_NEXTHERE; 744. } else { 745. objchain_p = &u.ustuck->minvent; 746. traverse_how = 0; /* nobj */ 747. } 748. /* 749. * Start the actual pickup process. This is split into two main 750. * sections, the newer menu and the older "traditional" methods. 751. * Automatic pickup has been split into its own menu-style routine 752. * to make things less confusing. 753. */ 754. if (autopickup) { 755. n = autopick(*objchain_p, traverse_how, &pick_list); 756. goto menu_pickup; 757. } 758. 759. if (flags.menu_style != MENU_TRADITIONAL || iflags.menu_requested) { 760. /* use menus exclusively */ 761. traverse_how |= AUTOSELECT_SINGLE 762. | (flags.sortpack ? INVORDER_SORT : 0); 763. if (count) { /* looking for N of something */ 764. char qbuf[QBUFSZ]; 765. 766. Sprintf(qbuf, "Pick %d of what?", count); 767. gv.val_for_n_or_more = count; /* set up callback selector */ 768. n = query_objlist(qbuf, objchain_p, traverse_how, 769. &pick_list, PICK_ONE, n_or_more); 770. /* correct counts, if any given */ 771. for (i = 0; i < n; i++) 772. pick_list[i].count = count; 773. } else { 774. n = query_objlist("Pick up what?", objchain_p, 775. (traverse_how | FEEL_COCKATRICE), 776. &pick_list, PICK_ANY, all_but_uchain); 777. } 778. 779. menu_pickup: 780. if (n > 0) 781. reset_justpicked(gi.invent); 782. n_tried = n; 783. for (n_picked = i = 0; i < n; i++) { 784. res = pickup_object(pick_list[i].item.a_obj, pick_list[i].count, 785. FALSE); 786. if (res < 0) 787. break; /* can't continue */ 788. n_picked += res; 789. } 790. if (pick_list) 791. free((genericptr_t) pick_list); 792. 793. } else { 794. /* old style interface */ 795. int ct = 0; 796. long lcount; 797. boolean all_of_a_type, selective, bycat; 798. char oclasses[MAXOCLASSES + 10]; /* +10: room for B,U,C,X plus slop */ 799. struct obj *obj, *obj2; 800. 801. oclasses[0] = '\0'; /* types to consider (empty for all) */ 802. all_of_a_type = TRUE; /* take all of considered types */ 803. selective = FALSE; /* ask for each item */ 804. 805. /* check for more than one object */ 806. for (obj = *objchain_p; obj; obj = FOLLOW(obj, traverse_how)) 807. ct++; 808. 809. if (ct == 1 && count) { 810. /* if only one thing, then pick it */ 811. obj = *objchain_p; 812. lcount = min(obj->quan, (long) count); 813. n_tried++; 814. reset_justpicked(gi.invent); 815. if (pickup_object(obj, lcount, FALSE) > 0) 816. n_picked++; /* picked something */ 817. goto end_query; 818. 819. } else if (ct >= 2) { 820. int via_menu = 0; 821. 822. There("are %s objects here.", (ct <= 10) ? "several" : "many"); 823. if (!query_classes(oclasses, &selective, &all_of_a_type, 824. "pick up", *objchain_p, 825. (traverse_how & BY_NEXTHERE) ? TRUE : FALSE, 826. &via_menu)) { 827. if (!via_menu) 828. goto pickupdone; 829. if (selective) 830. traverse_how |= INVORDER_SORT; 831. n = query_objlist("Pick up what?", objchain_p, traverse_how, 832. &pick_list, PICK_ANY, 833. (via_menu == -2) ? allow_all 834. : allow_category); 835. goto menu_pickup; 836. } 837. } 838. bycat = (menu_class_present('B') || menu_class_present('U') 839. || menu_class_present('C') || menu_class_present('X')); 840. 841. for (obj = *objchain_p; obj; obj = obj2) { 842. obj2 = FOLLOW(obj, traverse_how); 843. if (bycat ? !allow_category(obj) 844. : (!selective && oclasses[0] 845. && !strchr(oclasses, obj->oclass))) 846. continue; 847. 848. lcount = -1L; 849. if (!all_of_a_type) { 850. char qbuf[BUFSZ]; 851. 852. (void) safe_qbuf(qbuf, "Pick up ", "?", obj, doname, 853. ansimpleoname, something); 854. switch ((obj->quan < 2L) ? ynaq(qbuf) : ynNaq(qbuf)) { 855. case 'q': 856. goto end_query; /* out 2 levels */ 857. case 'n': 858. continue; 859. case 'a': 860. all_of_a_type = TRUE; 861. if (selective) { 862. selective = FALSE; 863. oclasses[0] = obj->oclass; 864. oclasses[1] = '\0'; 865. } 866. break; 867. case '#': /* count was entered */ 868. if (!yn_number) 869. continue; /* 0 count => No */ 870. lcount = (long) yn_number; 871. if (lcount > obj->quan) 872. lcount = obj->quan; 873. FALLTHROUGH; 874. /*FALLTHRU*/ 875. default: /* 'y' */ 876. break; 877. } 878. } 879. if (lcount == -1L) 880. lcount = obj->quan; 881. 882. if (!n_tried) /* reset just before the first item picked */ 883. reset_justpicked(gi.invent); 884. 885. n_tried++; 886. if ((res = pickup_object(obj, lcount, FALSE)) < 0) 887. break; 888. n_picked += res; 889. } 890. end_query: 891. ; /* statement required after label */ 892. } 893. 894. if (!u.uswallow) { 895. if (hides_under(gy.youmonst.data)) 896. (void) hideunder(&gy.youmonst); 897. 898. /* position may need updating (invisible hero) */ 899. if (n_picked) 900. newsym_force(u.ux, u.uy); 901. 902. /* check if there's anything else here after auto-pickup is done */ 903. if (autopickup) 904. check_here(n_picked > 0); 905. } 906. pickupdone: 907. gp.pickup_encumbrance = 0; 908. add_valid_menu_class(0); /* reset */ 909. return (n_tried > 0); 910. } 911. 912. struct autopickup_exception * 913. check_autopickup_exceptions(struct obj *obj) 914. { 915. /* 916. * Does the text description of this match an exception? 917. */ 918. struct autopickup_exception *ape = ga.apelist; 919. 920. if (ape) { 921. char *objdesc = makesingular(doname(obj)); 922. 923. while (ape && !regex_match(objdesc, ape->regex)) 924. ape = ape->next; 925. } 926. return ape; 927. } 928. 929. boolean 930. autopick_testobj(struct obj *otmp, boolean calc_costly) 931. { 932. struct autopickup_exception *ape; 933. static boolean costly = FALSE; 934. const char *otypes = flags.pickup_types; 935. boolean pickit; 936. 937. /* calculate 'costly' just once for a given autopickup operation */ 938. if (calc_costly) 939. costly = (otmp->where == OBJ_FLOOR 940. && costly_spot(otmp->ox, otmp->oy)); 941. 942. /* first check: reject if an unpaid item in a shop */ 943. if (costly && !otmp->no_charge) 944. return FALSE; 945. 946. /* pickup_thrown/pickup_stolen/nopick_dropped override pickup_types and 947. exceptions */ 948. if ((flags.pickup_thrown && otmp->how_lost == LOST_THROWN) 949. || (flags.pickup_stolen && otmp->how_lost == LOST_STOLEN)) 950. return TRUE; 951. if (flags.nopick_dropped && otmp->how_lost == LOST_DROPPED) 952. return FALSE; 953. if (otmp->how_lost == LOST_EXPLODING) 954. return FALSE; 955. 956. /* check for pickup_types */ 957. pickit = (!*otypes || strchr(otypes, otmp->oclass)); 958. 959. /* check for autopickup exceptions */ 960. ape = check_autopickup_exceptions(otmp); 961. if (ape) 962. pickit = ape->grab; 963. 964. return pickit; 965. } 966. 967. /* 968. * Pick from the given list using flags.pickup_types. Return the number 969. * of items picked (not counts). Create an array that returns pointers 970. * and counts of the items to be picked up. If the number of items 971. * picked is zero, the pickup list is left alone. The caller of this 972. * function must free the pickup list. 973. */ 974. staticfn int 975. autopick( 976. struct obj *olist, /* the object list */ 977. int follow, /* how to follow the object list */ 978. menu_item **pick_list) /* list of objects and counts to pick up */ 979. { 980. menu_item *pi; /* pick item */ 981. struct obj *curr; 982. int n; 983. boolean check_costly = TRUE; 984. 985. /* first count the number of eligible items */ 986. for (n = 0, curr = olist; curr; curr = FOLLOW(curr, follow)) { 987. if (autopick_testobj(curr, check_costly)) 988. ++n; 989. check_costly = FALSE; /* only need to check once per autopickup */ 990. } 991. 992. if (n) { 993. *pick_list = pi = (menu_item *) alloc(sizeof (menu_item) * n); 994. for (n = 0, curr = olist; curr; curr = FOLLOW(curr, follow)) { 995. if (autopick_testobj(curr, FALSE)) { 996. pi[n].item.a_obj = curr; 997. pi[n].count = curr->quan; 998. n++; 999. } 1000. } 1001. } 1002. return n; 1003. } 1004. 1005. /* 1006. * Put up a menu using the given object list. Only those objects on the 1007. * list that meet the approval of the allow function are displayed. Return 1008. * a count of the number of items selected, as well as an allocated array of 1009. * menu_items, containing pointers to the objects selected and counts. The 1010. * returned counts are guaranteed to be in bounds and non-zero. 1011. * 1012. * Query flags: 1013. * BY_NEXTHERE - Follow object list via nexthere instead of nobj. 1014. * AUTOSELECT_SINGLE - Don't ask if only 1 object qualifies - just 1015. * use it. 1016. * USE_INVLET - Use object's invlet. 1017. * INVORDER_SORT - Use hero's pack order. 1018. * INCLUDE_HERO - Showing engulfer's invent; show hero too. 1019. * SIGNAL_NOMENU - Return -1 rather than 0 if nothing passes "allow". 1020. * SIGNAL_ESCAPE - Return -1 rather than 0 if player uses ESC to 1021. * pick nothing. 1022. * FEEL_COCKATRICE - touch corpse. 1023. */ 1024. int 1025. query_objlist(const char *qstr, /* query string */ 1026. struct obj **olist_p, /* the list to pick from */ 1027. int qflags, /* options to control the query */ 1028. menu_item **pick_list, /* return list of items picked */ 1029. int how, /* type of query */ 1030. boolean (*allow)(OBJ_P)) /* allow function */ 1031. { 1032. int i, n, tmpglyph; 1033. winid win; 1034. struct obj *curr, *last, fake_hero_object, *olist = *olist_p; 1035. char *pack, packbuf[MAXOCLASSES + 1]; 1036. anything any; 1037. boolean printed_type_name, first, 1038. sorted = (qflags & INVORDER_SORT) != 0, 1039. engulfer = (qflags & INCLUDE_HERO) != 0, 1040. engulfer_minvent; 1041. unsigned sortflags; 1042. glyph_info tmpglyphinfo = nul_glyphinfo; 1043. Loot *sortedolist, *srtoli; 1044. int clr = NO_COLOR; 1045. 1046. *pick_list = (menu_item *) 0; 1047. if (!olist && !engulfer) 1048. return 0; 1049. 1050. /* count the number of items allowed */ 1051. for (n = 0, last = 0, curr = olist; curr; curr = FOLLOW(curr, qflags)) 1052. if ((*allow)(curr)) { 1053. last = curr; 1054. n++; 1055. } 1056. /* can't depend upon 'engulfer' because that's used to indicate whether 1057. hero should be shown as an extra, fake item */ 1058. engulfer_minvent = (olist && olist->where == OBJ_MINVENT 1059. && engulfing_u(olist->ocarry)); 1060. if (engulfer_minvent && n == 1 && olist->owornmask != 0L) { 1061. qflags &= ~AUTOSELECT_SINGLE; 1062. } 1063. if (engulfer) { 1064. ++n; 1065. /* don't autoselect swallowed hero if it's the only choice */ 1066. qflags &= ~AUTOSELECT_SINGLE; 1067. } 1068. 1069. if (n == 0) /* nothing to pick here */ 1070. return (qflags & SIGNAL_NOMENU) ? -1 : 0; 1071. 1072. if (n == 1 && (qflags & AUTOSELECT_SINGLE)) { 1073. *pick_list = (menu_item *) alloc(sizeof (menu_item)); 1074. (*pick_list)->item.a_obj = last; 1075. (*pick_list)->count = last->quan; 1076. return 1; 1077. } 1078. 1079. sortflags = (((flags.sortloot == 'f' 1080. || (flags.sortloot == 'l' && !(qflags & USE_INVLET))) 1081. ? SORTLOOT_LOOT 1082. : ((qflags & USE_INVLET) ? SORTLOOT_INVLET : 0)) 1083. | (flags.sortpack ? SORTLOOT_PACK : 0) 1084. | ((qflags & FEEL_COCKATRICE) ? SORTLOOT_PETRIFY : 0)); 1085. sortedolist = sortloot(&olist, sortflags, 1086. (qflags & BY_NEXTHERE) ? TRUE : FALSE, allow); 1087. 1088. win = create_nhwindow(NHW_MENU); 1089. start_menu(win, MENU_BEHAVE_STANDARD); 1090. any = cg.zeroany; 1091. if (gt.this_title) { 1092. /* dotypeinv() supplies gt.this_title to display as initial header; 1093. intentionally avoid the menu_headings highlight attribute here */ 1094. add_menu_str(win, gt.this_title); 1095. } 1096. /* 1097. * Run through the list and add the objects to the menu. If 1098. * INVORDER_SORT is set, we'll run through the list once for 1099. * each type so we can group them. The allow function was 1100. * called by sortloot() and will be called once per item here. 1101. */ 1102. pack = strcpy(packbuf, flags.inv_order); 1103. if (qflags & INCLUDE_VENOM) 1104. (void) strkitten(pack, VENOM_CLASS); /* venom is not in inv_order */ 1105. first = TRUE; 1106. do { 1107. printed_type_name = FALSE; 1108. for (srtoli = sortedolist; ((curr = srtoli->obj) != 0); ++srtoli) { 1109. if (sorted && curr->oclass != *pack) 1110. continue; 1111. if ((qflags & FEEL_COCKATRICE) && curr->otyp == CORPSE 1112. && will_feel_cockatrice(curr, FALSE)) { 1113. destroy_nhwindow(win); /* stop the menu and revert */ 1114. (void) look_here(0, LOOKHERE_NOFLAGS); 1115. unsortloot(&sortedolist); 1116. return 0; 1117. } 1118. if ((*allow)(curr)) { 1119. /* if sorting, print type name (once only) */ 1120. if (sorted && !printed_type_name) { 1121. boolean with_oc_sym = (how != PICK_NONE 1122. && iflags.menu_head_objsym); 1123. 1124. any = cg.zeroany; 1125. add_menu_heading(win, 1126. let_to_name(*pack, FALSE, with_oc_sym)); 1127. printed_type_name = TRUE; 1128. } 1129. 1130. any.a_obj = curr; 1131. tmpglyph = obj_to_glyph(curr, rn2_on_display_rng); 1132. map_glyphinfo(0, 0, tmpglyph, 0U, &tmpglyphinfo); 1133. add_menu(win, &tmpglyphinfo, &any, 1134. (qflags & USE_INVLET) ? curr->invlet 1135. : (first && curr->oclass == COIN_CLASS) ? '$' : 0, 1136. def_oc_syms[(int) objects[curr->otyp].oc_class].sym, 1137. ATR_NONE, clr, doname_with_price(curr), 1138. MENU_ITEMFLAGS_NONE); 1139. first = FALSE; 1140. } 1141. } 1142. pack++; 1143. } while (sorted && *pack); 1144. unsortloot(&sortedolist); 1145. 1146. if (engulfer) { 1147. char buf[BUFSZ]; 1148. 1149. any = cg.zeroany; 1150. if (sorted && n > 1) { 1151. Sprintf(buf, "%s Creatures", 1152. digests(u.ustuck->data) ? "Swallowed" : "Engulfed"); 1153. add_menu_heading(win, buf); 1154. } 1155. fake_hero_object = cg.zeroobj; 1156. fake_hero_object.quan = 1L; /* not strictly necessary... */ 1157. any.a_obj = &fake_hero_object; 1158. tmpglyph = mon_to_glyph(&gy.youmonst, rn2_on_display_rng); 1159. map_glyphinfo(0, 0, tmpglyph, 0U, &tmpglyphinfo); 1160. add_menu(win, &tmpglyphinfo, &any, 1161. /* fake inventory letter, no group accelerator */ 1162. CONTAINED_SYM, 0, ATR_NONE, clr, an(self_lookat(buf)), 1163. MENU_ITEMFLAGS_NONE); 1164. } 1165. 1166. end_menu(win, qstr); 1167. n = select_menu(win, how, pick_list); 1168. destroy_nhwindow(win); 1169. 1170. if (n > 0) { 1171. menu_item *mi; 1172. int k; 1173. 1174. /* fix up counts: -1 means no count used => pick all; 1175. if fake_hero_object was picked, discard that choice */ 1176. for (i = k = 0, mi = *pick_list; i < n; i++, mi++) { 1177. curr = mi->item.a_obj; 1178. if (curr == &fake_hero_object) { 1179. /* this isn't actually possible; fake item representing 1180. hero is only included for look here (':'), not pickup, 1181. and that's PICK_NONE so we can't get here from there */ 1182. You_cant("pick yourself up!"); 1183. continue; 1184. } 1185. if (engulfer_minvent && curr->owornmask != 0L) { 1186. You_cant("pick %s up.", ysimple_name(curr)); 1187. continue; 1188. } 1189. if (mi->count == -1L || mi->count > curr->quan) 1190. mi->count = curr->quan; 1191. if (k < i) 1192. (*pick_list)[k] = *mi; 1193. ++k; 1194. } 1195. if (!k) { 1196. /* fake_hero was only choice so discard whole list */ 1197. free((genericptr_t) *pick_list); 1198. *pick_list = 0; 1199. n = 0; 1200. } else if (k < n) { 1201. /* other stuff plus fake_hero; last slot is now unused 1202. (could be more than one if player tried to pick items 1203. worn by engulfer) */ 1204. while (n > k) { 1205. --n; 1206. (*pick_list)[n].item = cg.zeroany; 1207. (*pick_list)[n].count = 0L; 1208. } 1209. } 1210. } else if (n < 0) { 1211. /* -1 is used for SIGNAL_NOMENU, so callers don't expect it 1212. to indicate that the player declined to make a choice */ 1213. n = (qflags & SIGNAL_ESCAPE) ? -2 : 0; 1214. } 1215. return n; 1216. } 1217. 1218. /* 1219. * For menustyle:Full. 1220. * 1221. * allow menu-based category (class) selection (for Drop,take off etc.) 1222. * 1223. * If ParanoidAutoAll, requires confirmation when 'A' has been picked. 1224. */ 1225. int 1226. query_category( 1227. const char *qstr, /* query string */ 1228. struct obj *olist, /* the list to pick from */ 1229. int qflags, /* behavior modification flags */ 1230. menu_item **pick_list, /* return list of items picked */ 1231. int how) /* type of query */ 1232. { 1233. int n; 1234. winid win; 1235. struct obj *curr; 1236. char *pack, packbuf[MAXOCLASSES + 1]; 1237. anything any; 1238. boolean collected_type_name; 1239. char invlet; 1240. int ccount; 1241. boolean (*ofilter)(OBJ_P) = (boolean (*)(OBJ_P)) 0; 1242. boolean show_a, 1243. do_unpaid = FALSE, do_usedup = FALSE, 1244. do_blessed = FALSE, do_cursed = FALSE, 1245. do_uncursed = FALSE, do_buc_unknown = FALSE, 1246. do_worn = FALSE, verify_All = FALSE; 1247. int num_buc_types = 0, num_justpicked = 0, clr = NO_COLOR; 1248. 1249. *pick_list = (menu_item *) 0; 1250. if (!olist) 1251. return 0; 1252. if ((qflags & UNPAID_TYPES) != 0 && count_unpaid(olist)) 1253. do_unpaid = TRUE; 1254. /* caller only passes BILLED_TYPES when there are some used up items 1255. on shop's bill */ 1256. if ((qflags & BILLED_TYPES) != 0) 1257. do_usedup = TRUE; 1258. /* for the 'A' command to remove worn/wielded */ 1259. if ((qflags & WORN_TYPES) != 0) { 1260. do_worn = TRUE; 1261. ofilter = is_worn; 1262. } 1263. if ((qflags & BUC_BLESSED) != 0 1264. && count_buc(olist, BUC_BLESSED, ofilter)) { 1265. do_blessed = TRUE; 1266. num_buc_types++; 1267. } 1268. if ((qflags & BUC_CURSED) != 0 1269. && count_buc(olist, BUC_CURSED, ofilter)) { 1270. do_cursed = TRUE; 1271. num_buc_types++; 1272. } 1273. if ((qflags & BUC_UNCURSED) != 0 1274. && count_buc(olist, BUC_UNCURSED, ofilter)) { 1275. do_uncursed = TRUE; 1276. num_buc_types++; 1277. } 1278. if ((qflags & BUC_UNKNOWN) != 0 1279. && count_buc(olist, BUC_UNKNOWN, ofilter)) { 1280. do_buc_unknown = TRUE; 1281. num_buc_types++; 1282. } 1283. if ((qflags & JUSTPICKED) != 0) { 1284. num_justpicked = count_justpicked(olist); 1285. } 1286. 1287. ccount = count_categories(olist, qflags); 1288. /* no point in actually showing a menu for a single category */ 1289. if (ccount == 1 && !do_unpaid && !do_usedup && num_buc_types <= 1) { 1290. for (curr = olist; curr; curr = FOLLOW(curr, qflags)) { 1291. if (ofilter && !(*ofilter)(curr)) 1292. continue; 1293. break; 1294. } 1295. if (curr) { 1296. *pick_list = (menu_item *) alloc(sizeof(menu_item)); 1297. (*pick_list)->item.a_int = curr->oclass; 1298. n = 1; 1299. } else { 1300. debugpline0("query_category: no single object match"); 1301. n = 0; 1302. } 1303. /* early return is ok; there's no temp window yet */ 1304. return n; 1305. } 1306. 1307. win = create_nhwindow(NHW_MENU); 1308. start_menu(win, MENU_BEHAVE_STANDARD); 1309. 1310. pack = strcpy(packbuf, flags.inv_order); 1311. if ((qflags & INCLUDE_VENOM) != 0) 1312. (void) strkitten(pack, VENOM_CLASS); /* venom is not in inv_order */ 1313. 1314. show_a = ((qflags & ALL_TYPES) != 0 && ccount > 1); 1315. 1316. if ((qflags & CHOOSE_ALL) != 0) { 1317. invlet = 'A'; 1318. any = cg.zeroany; 1319. any.a_int = 'A'; 1320. add_menu(win, &nul_glyphinfo, &any, invlet, 0, ATR_NONE, clr, 1321. /* note: menu_remarm() doesn't pass the CHOOSE_ALL flag, 1322. so do_worn handling here is moot */ 1323. do_worn ? "Auto-select every item being worn or wielded" 1324. : "Auto-select every relevant item", 1325. MENU_ITEMFLAGS_SKIPINVERT); 1326. verify_All = (how == PICK_ANY) && ParanoidAutoAll; 1327. if (!verify_All) { 1328. if (!ga.A_first_hint++ || iflags.cmdassist) 1329. add_menu_str(win, 1330. " (ignored unless some other choices are also picked)"); 1331. } else if (show_a) { 1332. if (!ga.A_second_hint++ || iflags.cmdassist) 1333. add_menu_str(win, 1334. " (if no other choices are picked, 'a' is implied)"); 1335. } 1336. /* blank separator */ 1337. add_menu_str(win, ""); 1338. } 1339. 1340. invlet = 'a'; 1341. if (show_a) { 1342. any = cg.zeroany; 1343. any.a_int = ALL_TYPES_SELECTED; 1344. add_menu(win, &nul_glyphinfo, &any, invlet, 0, ATR_NONE, clr, 1345. do_worn ? "All worn and wielded types" : "All types", 1346. MENU_ITEMFLAGS_SKIPINVERT); 1347. ++invlet; /* invlet = 'b'; */ 1348. } 1349. 1350. do { 1351. collected_type_name = FALSE; 1352. for (curr = olist; curr; curr = FOLLOW(curr, qflags)) { 1353. if (curr->oclass == *pack) { 1354. if (ofilter && !(*ofilter)(curr)) 1355. continue; 1356. if (!collected_type_name) { 1357. int oclass = (int) curr->oclass; 1358. 1359. any = cg.zeroany; 1360. any.a_int = oclass; 1361. add_menu(win, &nul_glyphinfo, &any, invlet++, 1362. (int) def_oc_syms[oclass].sym, ATR_NONE, clr, 1363. let_to_name(*pack, FALSE, 1364. (how != PICK_NONE 1365. && iflags.menu_head_objsym)), 1366. MENU_ITEMFLAGS_NONE); 1367. collected_type_name = TRUE; 1368. } 1369. } 1370. } 1371. pack++; 1372. if (invlet >= 'u') { 1373. impossible("query_category: too many categories"); 1374. n = 0; 1375. goto query_done; 1376. } 1377. } while (*pack); 1378. 1379. if (do_unpaid || do_usedup 1380. || do_blessed || do_cursed || do_uncursed || do_buc_unknown 1381. || num_justpicked) { 1382. add_menu_str(win, ""); 1383. } 1384. 1385. /* unpaid items if there are any */ 1386. if (do_unpaid) { 1387. invlet = 'u'; 1388. any = cg.zeroany; 1389. any.a_int = 'u'; 1390. add_menu(win, &nul_glyphinfo, &any, invlet, 0, 1391. ATR_NONE, clr, "Unpaid items", MENU_ITEMFLAGS_SKIPINVERT); 1392. } 1393. /* billed items: checked by caller, so always include if BILLED_TYPES */ 1394. if (do_usedup) { 1395. invlet = 'x'; 1396. any = cg.zeroany; 1397. any.a_int = 'x'; 1398. add_menu(win, &nul_glyphinfo, &any, invlet, 0, ATR_NONE, clr, 1399. "Unpaid items already used up", MENU_ITEMFLAGS_SKIPINVERT); 1400. } 1401. 1402. /* items with b/u/c/unknown if there are any; 1403. this cluster of menu entries is in alphabetical order, 1404. reversing the usual sequence of 'U' and 'C' in BUCX */ 1405. if (do_blessed) { 1406. invlet = 'B'; 1407. any = cg.zeroany; 1408. any.a_int = 'B'; 1409. add_menu(win, &nul_glyphinfo, &any, invlet, 0, ATR_NONE, clr, 1410. "Items known to be Blessed", MENU_ITEMFLAGS_SKIPINVERT); 1411. } 1412. if (do_cursed) { 1413. invlet = 'C'; 1414. any = cg.zeroany; 1415. any.a_int = 'C'; 1416. add_menu(win, &nul_glyphinfo, &any, invlet, 0, ATR_NONE, clr, 1417. "Items known to be Cursed", MENU_ITEMFLAGS_SKIPINVERT); 1418. } 1419. if (do_uncursed) { 1420. invlet = 'U'; 1421. any = cg.zeroany; 1422. any.a_int = 'U'; 1423. add_menu(win, &nul_glyphinfo, &any, invlet, 0, ATR_NONE, clr, 1424. "Items known to be Uncursed", MENU_ITEMFLAGS_SKIPINVERT); 1425. } 1426. if (do_buc_unknown) { 1427. invlet = 'X'; 1428. any = cg.zeroany; 1429. any.a_int = 'X'; 1430. add_menu(win, &nul_glyphinfo, &any, invlet, 0, ATR_NONE, clr, 1431. "Items of unknown Bless/Curse status", 1432. MENU_ITEMFLAGS_SKIPINVERT); 1433. } 1434. if (num_justpicked) { 1435. char tmpbuf[BUFSZ]; 1436. 1437. if (num_justpicked == 1) 1438. Sprintf(tmpbuf, "Just picked up: %s", 1439. doname(find_justpicked(olist))); 1440. else 1441. Strcpy(tmpbuf, "Items you just picked up"); 1442. invlet = 'P'; 1443. any = cg.zeroany; 1444. any.a_int = 'P'; 1445. add_menu(win, &nul_glyphinfo, &any, invlet, 0, ATR_NONE, clr, 1446. tmpbuf, MENU_ITEMFLAGS_SKIPINVERT); 1447. } 1448. end_menu(win, qstr); 1449. n = select_menu(win, how, pick_list); 1450. if (n > 0) { 1451. assert(*pick_list != NULL); 1452. } 1453. 1454. /* handle ParanoidAutoAll by confirming 'A' choice if present */ 1455. if (n > 0 && verify_All) { 1456. int i, j; 1457. 1458. for (i = 0; i < n; ++i) 1459. if ((*pick_list)[i].item.a_int == 'A') { 1460. /* ParanoidAutoAll is set (otherwise verify_All is false); 1461. if ParanoidConfirm is also set, require "yes" rather than 1462. just "y" to accept (and "no" rather than "n" to decline; 1463. accepts "quit" and ESC without converting them to 'n') */ 1464. switch (paranoid_ynq(ParanoidConfirm, 1465. "Really autoselect All?", TRUE)) { 1466. case 'y': 1467. /* yes => honor Auto-select All */ 1468. break; 1469. case 'n': 1470. /* no => remove 'A' from the list; if that would make 1471. it empty then replace with 'a' */ 1472. if (n > 1) { 1473. for (j = i + 1; j < n; ++j) 1474. (*pick_list)[j - 1] = (*pick_list)[j]; 1475. --n; 1476. break; /* from switch */ 1477. } else if ((qflags & ALL_TYPES) != 0) { 1478. /* 'A' was the only choice; convert it to 'a' and 1479. then let the next menu offer a choice of all */ 1480. (*pick_list)[0].item.a_int = ALL_TYPES_SELECTED; 1481. /* assert( n == 1 ); */ 1482. break; /* from switch */ 1483. } 1484. FALLTHROUGH; 1485. /*FALLTHRU*/ 1486. case 'q': 1487. default: 1488. /* quit | ESC => cancel, no Auto-select and no 2nd menu */ 1489. n = 0; 1490. free((genericptr_t) *pick_list), *pick_list = 0; 1491. break; 1492. } 1493. break; /* from for => goto query_done; */ 1494. } 1495. } else if (n == 1 && !verify_All && (*pick_list)[0].item.a_int == 'A') { 1496. /* without paranoid_confirm:A, choosing 'A' by itself is rejected */ 1497. n = 0; 1498. free((genericptr_t) *pick_list), *pick_list = 0; 1499. /* the menu entry description is "Auto-select every relevant item" 1500. [not sure whether issuing a message here is a good idea...] */ 1501. pline("No relevant items selected."); 1502. } 1503. query_done: 1504. destroy_nhwindow(win); 1505. if (n < 0) /* closed menu with ESC */ 1506. n = 0; /* callers don't expect -1 */ 1507. return n; 1508. } 1509. 1510. staticfn int 1511. count_categories(struct obj *olist, int qflags) 1512. { 1513. char *pack; 1514. boolean counted_category; 1515. int ccount = 0; 1516. struct obj *curr; 1517. boolean do_worn = (qflags & WORN_TYPES) != 0; 1518. 1519. pack = flags.inv_order; 1520. do { 1521. counted_category = FALSE; 1522. for (curr = olist; curr; curr = FOLLOW(curr, qflags)) { 1523. if (curr->oclass == *pack) { 1524. if (do_worn && !(curr->owornmask 1525. & (W_ARMOR | W_ACCESSORY | W_WEAPONS))) 1526. continue; 1527. if (!counted_category) { 1528. ccount++; 1529. counted_category = TRUE; 1530. } 1531. } 1532. } 1533. pack++; 1534. } while (*pack); 1535. return ccount; 1536. } 1537. 1538. /* 1539. * How much the weight of the given container will change when the given 1540. * object is removed from it. Use before and after weight amounts rather 1541. * than trying to match the calculation used by weight() in mkobj.c. 1542. */ 1543. staticfn int 1544. delta_cwt(struct obj *container, struct obj *obj) 1545. { 1546. struct obj **prev; 1547. int owt, nwt; 1548. 1549. if (container->otyp != BAG_OF_HOLDING) 1550. return obj->owt; 1551. 1552. owt = nwt = container->owt; 1553. /* find the object so that we can remove it */ 1554. for (prev = &container->cobj; *prev; prev = &(*prev)->nobj) 1555. if (*prev == obj) 1556. break; 1557. if (!*prev) { 1558. panic("delta_cwt: obj not inside container?"); 1559. } else { 1560. /* temporarily remove the object and calculate resulting weight */ 1561. *prev = obj->nobj; 1562. nwt = weight(container); 1563. *prev = obj; /* put the object back; obj->nobj is still valid */ 1564. } 1565. return owt - nwt; 1566. } 1567. 1568. /* could we carry `obj'? if not, could we carry some of it/them? */ 1569. staticfn long 1570. carry_count(struct obj *obj, /* object to pick up... */ 1571. struct obj *container, /* ...bag it is coming out of */ 1572. long count, 1573. boolean telekinesis, 1574. int *wt_before, int *wt_after) 1575. { 1576. boolean adjust_wt = container && carried(container), 1577. is_gold = obj->oclass == COIN_CLASS; 1578. int wt, iw, ow, oow; 1579. long qq, savequan, umoney; 1580. unsigned saveowt; 1581. const char *verb, *prefx1, *prefx2, *suffx; 1582. char obj_nambuf[BUFSZ], where[BUFSZ]; 1583. 1584. savequan = obj->quan; 1585. saveowt = obj->owt; 1586. umoney = money_cnt(gi.invent); 1587. iw = max_capacity(); 1588. 1589. if (count != savequan) { 1590. obj->quan = count; 1591. obj->owt = (unsigned) weight(obj); 1592. } 1593. wt = iw + (int) obj->owt; 1594. if (adjust_wt) 1595. wt -= delta_cwt(container, obj); 1596. /* This will go with silver+copper & new gold weight */ 1597. if (is_gold) /* merged gold might affect cumulative weight */ 1598. wt -= (GOLD_WT(umoney) + GOLD_WT(count) - GOLD_WT(umoney + count)); 1599. if (count != savequan) { 1600. obj->quan = savequan; 1601. obj->owt = saveowt; 1602. } 1603. *wt_before = iw; 1604. *wt_after = wt; 1605. 1606. if (wt < 0) 1607. return count; 1608. 1609. /* see how many we can lift */ 1610. if (is_gold) { 1611. iw -= (int) GOLD_WT(umoney); 1612. if (!adjust_wt) { 1613. qq = GOLD_CAPACITY((long) iw, umoney); 1614. } else { 1615. oow = 0; 1616. qq = 50L - (umoney % 100L) - 1L; 1617. if (qq < 0L) 1618. qq += 100L; 1619. for (; qq <= count; qq += 100L) { 1620. obj->quan = qq; 1621. obj->owt = (unsigned) GOLD_WT(qq); 1622. ow = (int) GOLD_WT(umoney + qq); 1623. ow -= delta_cwt(container, obj); 1624. if (iw + ow >= 0) 1625. break; 1626. oow = ow; 1627. } 1628. iw -= oow; 1629. qq -= 100L; 1630. } 1631. if (qq < 0L) 1632. qq = 0L; 1633. else if (qq > count) 1634. qq = count; 1635. wt = iw + (int) GOLD_WT(umoney + qq); 1636. } else if (count > 1 || count < obj->quan) { 1637. /* 1638. * Ugh. Calc num to lift by changing the quan of the 1639. * object and calling weight. 1640. * 1641. * This works for containers only because containers 1642. * don't merge. -dean 1643. */ 1644. for (qq = 1L; qq <= count; qq++) { 1645. obj->quan = qq; 1646. obj->owt = (unsigned) (ow = weight(obj)); 1647. if (adjust_wt) 1648. ow -= delta_cwt(container, obj); 1649. if (iw + ow >= 0) 1650. break; 1651. wt = iw + ow; 1652. } 1653. --qq; 1654. } else { 1655. /* there's only one, and we can't lift it */ 1656. qq = 0L; 1657. } 1658. obj->quan = savequan; 1659. obj->owt = saveowt; 1660. 1661. if (qq < count) { 1662. /* some message will be given */ 1663. Strcpy(obj_nambuf, doname(obj)); 1664. if (container) { 1665. Sprintf(where, "in %s", the(xname(container))); 1666. verb = "carry"; 1667. } else { 1668. Strcpy(where, "lying here"); 1669. verb = telekinesis ? "acquire" : "lift"; 1670. } 1671. } else { 1672. /* lint suppression */ 1673. *obj_nambuf = *where = '\0'; 1674. verb = ""; 1675. } 1676. /* we can carry qq of them */ 1677. if (qq > 0) { 1678. if (qq < count) 1679. You("can only %s %s of the %s %s.", verb, 1680. (qq == 1L) ? "one" : "some", obj_nambuf, where); 1681. *wt_after = wt; 1682. return qq; 1683. } 1684. 1685. if (!container) 1686. Strcpy(where, "here"); /* slightly shorter form */ 1687. if (gi.invent || umoney) { 1688. prefx1 = "you cannot "; 1689. prefx2 = ""; 1690. suffx = " any more"; 1691. } else { 1692. prefx1 = (obj->quan == 1L) ? "it " : "even one "; 1693. prefx2 = "is too heavy for you to "; 1694. suffx = ""; 1695. } 1696. There("%s %s %s, but %s%s%s%s.", otense(obj, "are"), obj_nambuf, where, 1697. prefx1, prefx2, verb, suffx); 1698. 1699. /* *wt_after = iw; */ 1700. return 0L; 1701. } 1702. 1703. /* determine whether character is able and player is willing to carry `obj' */ 1704. staticfn int 1705. lift_object( 1706. struct obj *obj, /* object to pick up... */ 1707. struct obj *container, /* ...bag it's coming out of */ 1708. long *cnt_p, 1709. boolean telekinesis) 1710. { 1711. int result, old_wt, new_wt, prev_encumbr, next_encumbr; 1712. 1713. if (obj->otyp == BOULDER && Sokoban) { 1714. You("cannot get your %s around this %s.", body_part(HAND), 1715. xname(obj)); 1716. return -1; 1717. } 1718. /* override weight consideration for loadstone picked up by anybody 1719. and for boulder picked up by hero poly'd into a giant; override 1720. availability of open inventory slot iff not already carrying one */ 1721. if (obj->otyp == LOADSTONE 1722. || (obj->otyp == BOULDER && throws_rocks(gy.youmonst.data))) { 1723. if (inv_cnt(FALSE) < invlet_basic || !carrying(obj->otyp) 1724. || merge_choice(gi.invent, obj)) 1725. return 1; /* lift regardless of current situation */ 1726. /* if we reach here, we're out of slots and already have at least 1727. one of these, so treat this one more like a normal item 1728. [this was using simpleonames(obj) for shortest description, but 1729. that's suboptimal for loadstones because it omits user-assigned 1730. type name which is something of interest for gray stones] */ 1731. You("are carrying too much stuff to pick up %s %s.", 1732. (obj->quan == 1L) ? "another" : "more", xname(obj)); 1733. return -1; 1734. } 1735. 1736. *cnt_p = carry_count(obj, container, *cnt_p, telekinesis, 1737. &old_wt, &new_wt); 1738. if (*cnt_p < 1L) { 1739. result = -1; /* nothing lifted */ 1740. } else if (obj->oclass != COIN_CLASS 1741. /* [exception for gold coins will have to change 1742. if silver/copper ones ever get implemented] */ 1743. && inv_cnt(FALSE) >= invlet_basic 1744. && !merge_choice(gi.invent, obj)) { 1745. /* if there is some gold here (and we haven't already skipped it), 1746. we aren't limited by the 52 item limit for it, but caller and 1747. "grandcaller" aren't prepared to skip stuff and then pickup 1748. just gold, so the best we can do here is vary the message */ 1749. Your("knapsack cannot accommodate any more items%s.", 1750. /* floor follows by nexthere, otherwise container so by nobj */ 1751. nxtobj(obj, GOLD_PIECE, (boolean) (obj->where == OBJ_FLOOR)) 1752. ? " (except gold)" : ""); 1753. result = -1; /* nothing lifted */ 1754. } else { 1755. result = 1; 1756. prev_encumbr = near_capacity(); 1757. if (prev_encumbr < flags.pickup_burden) 1758. prev_encumbr = flags.pickup_burden; 1759. next_encumbr = calc_capacity(new_wt - old_wt); 1760. if (next_encumbr > prev_encumbr) { 1761. if (telekinesis) { 1762. result = 0; /* don't lift */ 1763. } else { 1764. char qbuf[BUFSZ]; 1765. long savequan = obj->quan; 1766. 1767. obj->quan = *cnt_p; 1768. Sprintf(qbuf, "%s %s ", 1769. (next_encumbr >= EXT_ENCUMBER) ? overloadpfx 1770. : (next_encumbr >= HVY_ENCUMBER) ? nearloadpfx 1771. : (next_encumbr >= MOD_ENCUMBER) ? moderateloadpfx 1772. : slightloadpfx, 1773. !container ? "lifting" : "removing"); 1774. (void) safe_qbuf(qbuf, qbuf, ". Continue?", obj, doname, 1775. ansimpleoname, something); 1776. obj->quan = savequan; 1777. switch (ynq(qbuf)) { 1778. case 'q': 1779. result = -1; 1780. break; 1781. case 'n': 1782. result = 0; 1783. break; 1784. default: 1785. break; /* 'y' => result == 1 */ 1786. } 1787. clear_nhwindow(WIN_MESSAGE); 1788. } 1789. } 1790. } 1791. 1792. if (obj->otyp == SCR_SCARE_MONSTER && result <= 0 && !container) 1793. obj->spe = 0; 1794. return result; 1795. } 1796. 1797. /* 1798. * Pick up <count> of obj from the ground and add it to the hero's inventory. 1799. * Returns -1 if caller should break out of its loop, 0 if nothing picked 1800. * up, 1 if otherwise. 1801. */ 1802. int 1803. pickup_object( 1804. struct obj *obj, 1805. long count, /* if non-zero, pick up a subset of this amount */ 1806. boolean telekinesis) /* not picking it up directly by hand */ 1807. { 1808. int res; 1809. 1810. if (obj->quan < count) { 1811. impossible("pickup_object: count %ld > quan %ld?", count, obj->quan); 1812. return 0; 1813. } 1814. 1815. /* In case of auto-pickup, where we haven't had a chance 1816. to look at it yet; affects docall(SCR_SCARE_MONSTER). */ 1817. if (!Blind) 1818. observe_object(obj); 1819. 1820. if (obj == uchain) { /* do not pick up attached chain */ 1821. return 0; 1822. } else if (obj->where == OBJ_MINVENT && obj->owornmask != 0L 1823. && engulfing_u(obj->ocarry)) { 1824. You_cant("pick %s up.", ysimple_name(obj)); 1825. return 0; 1826. } else if (obj->oartifact && !touch_artifact(obj, &gy.youmonst)) { 1827. return 0; 1828. } else if (obj->otyp == CORPSE) { 1829. if (fatal_corpse_mistake(obj, telekinesis) 1830. || rider_corpse_revival(obj, telekinesis)) 1831. return -1; 1832. } else if (obj->otyp == SCR_SCARE_MONSTER) { 1833. int old_wt, new_wt; 1834. 1835. /* process a count before altering/deleting scrolls; 1836. tricky because being unable to lift full stack imposes an 1837. implicit count; unliftable ones should be treated as if 1838. the count excluded them so that they don't change state */ 1839. if ((count = carry_count(obj, (struct obj *) 0, 1840. count ? count : obj->quan, 1841. FALSE, &old_wt, &new_wt)) < 1L) 1842. return -1; /* couldn't even pick up 1, so effectively untouched */ 1843. /* all current callers handle a new object sanely when traversing 1844. a list; other half of a split will be left as-is and whatever 1845. already follows 'obj' will still be processed next */ 1846. if (count > 0L && count < obj->quan) 1847. obj = splitobj(obj, count); 1848. 1849. if (obj->blessed) { 1850. unbless(obj); 1851. } else if (!obj->spe && !obj->cursed) { 1852. obj->spe = 1; 1853. } else { 1854. pline_The("scroll%s %s to dust as you %s %s up.", plur(obj->quan), 1855. otense(obj, "turn"), telekinesis ? "raise" : "pick", 1856. (obj->quan == 1L) ? "it" : "them"); 1857. trycall(obj); 1858. useupf(obj, obj->quan); 1859. return 1; /* tried to pick something up and failed, but 1860. don't want to terminate pickup loop yet */ 1861. } 1862. } 1863. 1864. /* obj has either already passed autopick_testobj or we are explicitly 1865. picking it off the floor, so addinv() will override obj->how_lost; 1866. otherwise we couldn't pick up a thrown, stolen, or dropped item that 1867. was split off from a carried stack even while still carrying the 1868. rest of the stack unless we have at least one free slot available */ 1869. res = lift_object(obj, (struct obj *) 0, &count, telekinesis); 1870. if (res <= 0) 1871. return res; 1872. 1873. /* What's left of the special case for gold :-) */ 1874. if (obj->oclass == COIN_CLASS) 1875. disp.botl = TRUE; 1876. if (obj->quan != count && obj->otyp != LOADSTONE) 1877. obj = splitobj(obj, count); 1878. 1879. obj = pick_obj(obj); 1880. 1881. if (uwep && uwep == obj) 1882. gm.mrg_to_wielded = TRUE; 1883. pickup_prinv(obj, count, "lifting"); 1884. if (obj->ghostly) 1885. fix_ghostly_obj(obj); 1886. gm.mrg_to_wielded = FALSE; 1887. return 1; 1888. } 1889. 1890. /* 1891. * Do the actual work of picking otmp from the floor or monster's interior 1892. * and putting it in the hero's inventory. Take care of billing. Return a 1893. * pointer to the object where otmp ends up. This may be different 1894. * from otmp because of merging. 1895. */ 1896. struct obj * 1897. pick_obj(struct obj *otmp) 1898. { 1899. struct obj *result; 1900. coordxy ox, oy; 1901. boolean robshop, fromfloor = otmp->where == OBJ_FLOOR; 1902. 1903. /* otmp is either on the floor or in an engulfer's inventory; for the 1904. latter, its <ox,oy> probably won't be set */ 1905. (void) get_obj_location(otmp, &ox, &oy, 0); 1906. 1907. robshop = (!u.uswallow && otmp != uball && costly_spot(ox, oy)); 1908. obj_extract_self(otmp); 1909. if (fromfloor) 1910. newsym(ox, oy); 1911. 1912. /* for shop items, addinv() needs to be after addtobill() (so that 1913. object merger can take otmp->unpaid into account) but before 1914. remote_robbery() (which calls rob_shop() which calls setpaid() 1915. after moving costs of unpaid items to shop debt; setpaid() 1916. calls clear_unpaid() for lots of object chains, but 'otmp' isn't 1917. on any of those between obj_extract_self() and addinv(); for 1918. 3.6.0, 'otmp' remained flagged as an unpaid item in inventory 1919. and triggered impossible() every time inventory was examined) */ 1920. if (robshop) { 1921. char saveushops[5], fakeshop[2]; 1922. 1923. /* addtobill cares about your location rather than the object's; 1924. usually they'll be the same, but not when using telekinesis 1925. (if ever implemented) or a grappling hook */ 1926. Strcpy(saveushops, u.ushops); 1927. fakeshop[0] = *in_rooms(ox, oy, SHOPBASE); 1928. fakeshop[1] = '\0'; 1929. Strcpy(u.ushops, fakeshop); 1930. /* sets obj->unpaid if necessary */ 1931. addtobill(otmp, TRUE, FALSE, FALSE); 1932. Strcpy(u.ushops, saveushops); 1933. robshop = otmp->unpaid && !strchr(u.ushops, *fakeshop); 1934. } 1935. 1936. result = addinv(otmp); 1937. /* if you're taking a shop item from outside the shop, make shk notice */ 1938. if (robshop) 1939. remote_burglary(ox, oy); 1940. 1941. return result; 1942. } 1943. 1944. /* pickup_object()/out_container() helper; 1945. print an added-to-invent message for current object, limiting feedback 1946. about encumbrance to the first item which causes that to change */ 1947. staticfn void 1948. pickup_prinv( 1949. struct obj *obj, 1950. long count, 1951. const char *verb) 1952. { 1953. char pbuf[QBUFSZ]; 1954. const char *prefix; 1955. int nearload = near_capacity(); 1956. 1957. pbuf[0] = '\0'; 1958. if (nearload == gp.pickup_encumbrance) { 1959. prefix = (char *) 0; 1960. } else { 1961. prefix = (nearload >= EXT_ENCUMBER) ? overloadpfx 1962. : (nearload >= HVY_ENCUMBER) ? nearloadpfx 1963. : (nearload >= MOD_ENCUMBER) ? moderateloadpfx 1964. : (nearload >= SLT_ENCUMBER) ? slightloadpfx 1965. : (char *) 0; 1966. gp.pickup_encumbrance = nearload; 1967. } 1968. if (prefix) 1969. Sprintf(pbuf, "%s %s", prefix, verb); 1970. 1971. prinv(pbuf, obj, count); 1972. } 1973. 1974. /* 1975. * prints a message if encumbrance changed since the last check 1976. */ 1977. void 1978. encumber_msg(void) 1979. { 1980. int newcap = near_capacity(); 1981. 1982. if (go.oldcap < newcap) { 1983. switch (newcap) { 1984. case 1: 1985. Your("movements are slowed slightly because of your load."); 1986. break; 1987. case 2: 1988. You("rebalance your load. Movement is difficult."); 1989. break; 1990. case 3: 1991. You("%s under your heavy load. Movement is very hard.", 1992. stagger(gy.youmonst.data, "stagger")); 1993. break; 1994. default: 1995. You("%s move a handspan with this load!", 1996. newcap == 4 ? "can barely" : "can't even"); 1997. break; 1998. } 1999. disp.botl = TRUE; 2000. } else if (go.oldcap > newcap) { 2001. switch (newcap) { 2002. case 0: 2003. Your("movements are now unencumbered."); 2004. break; 2005. case 1: 2006. Your("movements are only slowed slightly by your load."); 2007. break; 2008. case 2: 2009. You("rebalance your load. Movement is still difficult."); 2010. break; 2011. case 3: 2012. You("%s under your load. Movement is still very hard.", 2013. stagger(gy.youmonst.data, "stagger")); 2014. break; 2015. } 2016. disp.botl = TRUE; 2017. } 2018. 2019. go.oldcap = newcap; 2020. } 2021. 2022. /* Is there a container at x,y. Optional: return count of containers at x,y */ 2023. int 2024. container_at(coordxy x, coordxy y, boolean countem) 2025. { 2026. struct obj *cobj, *nobj; 2027. int container_count = 0; 2028. 2029. for (cobj = svl.level.objects[x][y]; cobj; cobj = nobj) { 2030. nobj = cobj->nexthere; 2031. if (Is_container(cobj)) { 2032. container_count++; 2033. if (!countem) 2034. break; 2035. } 2036. } 2037. return container_count; 2038. } 2039. 2040. staticfn boolean 2041. able_to_loot( 2042. coordxy x, coordxy y, 2043. boolean looting) /* loot vs tip */ 2044. { 2045. const char *verb = looting ? "loot" : "tip"; 2046. struct trap *t = t_at(x, y); 2047. 2048. if (!can_reach_floor(t && is_pit(t->ttyp))) { 2049. if (u.usteed && P_SKILL(P_RIDING) < P_BASIC) 2050. rider_cant_reach(); /* not skilled enough to reach */ 2051. else 2052. cant_reach_floor(x, y, FALSE, TRUE, FALSE); 2053. return FALSE; 2054. } else if ((is_pool(x, y) && (looting || !Underwater)) || is_lava(x, y)) { 2055. /* at present, can't loot in water even when Underwater; 2056. can tip underwater, but not when over--or stuck in--lava */ 2057. You("cannot %s things that are deep in the %s.", verb, 2058. hliquid(is_lava(x, y) ? "lava" : "water")); 2059. return FALSE; 2060. } else if (nolimbs(gy.youmonst.data)) { 2061. pline("Without limbs, you cannot %s anything.", verb); 2062. return FALSE; 2063. } else if (looting && !freehand()) { 2064. pline("Without a free %s, you cannot loot anything.", 2065. body_part(HAND)); 2066. return FALSE; 2067. } 2068. return TRUE; 2069. } 2070. 2071. staticfn boolean 2072. mon_beside(coordxy x, coordxy y) 2073. { 2074. int i, j; 2075. coordxy nx, ny; 2076. 2077. for (i = -1; i <= 1; i++) 2078. for (j = -1; j <= 1; j++) { 2079. nx = x + i; 2080. ny = y + j; 2081. if (isok(nx, ny) && MON_AT(nx, ny)) 2082. return TRUE; 2083. } 2084. return FALSE; 2085. } 2086. 2087. staticfn int 2088. do_loot_cont( 2089. struct obj **cobjp, 2090. int cindex, /* index of this container (1..N)... */ 2091. int ccount) /* ...number of them (N) */ 2092. { 2093. struct obj *cobj = *cobjp; 2094. 2095. if (!cobj) 2096. return ECMD_OK; 2097. if (cobj->olocked) { 2098. int res = ECMD_OK; 2099. 2100. #if 0 2101. if (ccount < 2 && (svl.level.objects[cobj->ox][cobj->oy] == cobj)) 2102. pline("%s locked.", 2103. cobj->lknown ? "It is" : "Hmmm, it turns out to be"); 2104. else 2105. #endif 2106. if (cobj->lknown) 2107. pline("%s is locked.", The(xname(cobj))); 2108. else 2109. pline("Hmmm, %s turns out to be locked.", the(xname(cobj))); 2110. cobj->lknown = 1; 2111. 2112. if (flags.autounlock) { 2113. struct obj *otmp, *unlocktool = 0; 2114. coordxy ox = cobj->ox, oy = cobj->oy; 2115. 2116. u.dz = 0; /* might be non-zero from previous command since 2117. * #loot isn't a move command; pick_lock() cares */ 2118. /* if both the untrap and apply_key bits are set, untrap 2119. attempt will be performed first but we need to set up 2120. unlocktool in case "check for trap?" is declined */ 2121. if (((flags.autounlock & AUTOUNLOCK_APPLY_KEY) != 0 2122. && (unlocktool = autokey(TRUE)) != 0) 2123. || (flags.autounlock & AUTOUNLOCK_UNTRAP) != 0) { 2124. /* pass ox and oy to avoid direction prompt */ 2125. if (pick_lock(unlocktool, ox, oy, cobj)) 2126. res = ECMD_TIME; 2127. /* attempting to untrap or unlock might trigger a trap 2128. which destroys 'cobj'; inform caller if that happens */ 2129. for (otmp = svl.level.objects[ox][oy]; otmp; 2130. otmp = otmp->nexthere) 2131. if (otmp == cobj) 2132. break; 2133. if (!otmp) 2134. *cobjp = (struct obj *) 0; 2135. return res; 2136. } 2137. if ((flags.autounlock & AUTOUNLOCK_FORCE) != 0 2138. && res != ECMD_TIME 2139. && ccount == 1 && u_have_forceable_weapon()) { 2140. /* single container, and we could #force it open... */ 2141. /* note: doforce asks for confirmation */ 2142. cmdq_add_ec(CQ_CANNED, doforce); 2143. ga.abort_looting = TRUE; 2144. } 2145. } 2146. return res; 2147. } 2148. cobj->lknown = 1; /* floor container, so no need for update_inventory() */ 2149. 2150. if (cobj->otyp == BAG_OF_TRICKS) { 2151. int tmp; 2152. 2153. You("carefully open %s...", the(xname(cobj))); 2154. pline("It develops a huge set of teeth and bites you!"); 2155. tmp = rnd(10); 2156. losehp(Maybe_Half_Phys(tmp), "carnivorous bag", KILLED_BY_AN); 2157. makeknown(BAG_OF_TRICKS); 2158. ga.abort_looting = TRUE; 2159. return ECMD_TIME; 2160. } 2161. return use_container(cobjp, FALSE, (boolean) (cindex < ccount)); 2162. } 2163. 2164. /* #loot extended command */ 2165. int 2166. doloot(void) 2167. { 2168. int res; 2169. 2170. gl.loot_reset_justpicked = TRUE; 2171. res = doloot_core(); 2172. gl.loot_reset_justpicked = FALSE; 2173. return res; 2174. } 2175. 2176. /* loot a container on the floor or loot saddle from mon. */ 2177. staticfn int 2178. doloot_core(void) 2179. { 2180. struct obj *cobj, *nobj; 2181. int c = -1; 2182. int timepassed = 0; 2183. coord cc; 2184. boolean underfoot = TRUE; 2185. const char *dont_find_anything = "don't find anything"; 2186. struct monst *mtmp; 2187. int prev_inquiry = 0; 2188. boolean prev_loot = FALSE; 2189. int num_conts = 0; 2190. int clr = NO_COLOR; 2191. 2192. ga.abort_looting = FALSE; 2193. 2194. if (check_capacity((char *) 0)) { 2195. /* "Can't do that while carrying so much stuff." */ 2196. return ECMD_OK; 2197. } 2198. if (nohands(gy.youmonst.data)) { 2199. You("have no hands!"); /* not `body_part(HAND)' */ 2200. return ECMD_OK; 2201. } 2202. if (Confusion) { 2203. if (rn2(6) && reverse_loot()) 2204. return ECMD_TIME; 2205. if (rn2(2)) { 2206. pline("Being confused, you find nothing to loot."); 2207. return ECMD_TIME; /* costs a turn */ 2208. } /* else fallthrough to normal looting */ 2209. } 2210. cc.x = u.ux; 2211. cc.y = u.uy; 2212. 2213. if (iflags.menu_requested) 2214. goto lootmon; 2215. 2216. lootcont: 2217. if ((num_conts = container_at(cc.x, cc.y, TRUE)) > 0) { 2218. boolean anyfound = FALSE; 2219. 2220. if (!able_to_loot(cc.x, cc.y, TRUE)) 2221. return ECMD_OK; 2222. 2223. if (Blind && !uarmg) { 2224. /* if blind and without gloves, attempting to #loot at the 2225. location of a cockatrice corpse is fatal before asking 2226. whether to manipulate any containers */ 2227. for (nobj = sobj_at(CORPSE, cc.x, cc.y); nobj; 2228. nobj = nxtobj(nobj, CORPSE, TRUE)) 2229. if (will_feel_cockatrice(nobj, FALSE)) { 2230. feel_cockatrice(nobj, FALSE); 2231. /* if life-saved (or poly'd into stone golem), 2232. terminate attempt to loot */ 2233. return ECMD_TIME; 2234. } 2235. } 2236. 2237. if (num_conts > 1) { 2238. /* use a menu to loot many containers */ 2239. int n, i; 2240. winid win; 2241. anything any; 2242. menu_item *pick_list = (menu_item *) 0; 2243. 2244. any.a_void = 0; 2245. win = create_nhwindow(NHW_MENU); 2246. start_menu(win, MENU_BEHAVE_STANDARD); 2247. 2248. for (cobj = svl.level.objects[cc.x][cc.y]; cobj; 2249. cobj = cobj->nexthere) 2250. if (Is_container(cobj)) { 2251. any.a_obj = cobj; 2252. add_menu(win, &nul_glyphinfo, &any, 0, 0, ATR_NONE, clr, 2253. doname(cobj), MENU_ITEMFLAGS_NONE); 2254. } 2255. end_menu(win, "Loot which containers?"); 2256. n = select_menu(win, PICK_ANY, &pick_list); 2257. destroy_nhwindow(win); 2258. 2259. if (n > 0) { 2260. for (i = 1; i <= n; i++) { 2261. cobj = pick_list[i - 1].item.a_obj; 2262. timepassed |= do_loot_cont(&cobj, i, n); 2263. if (ga.abort_looting) { 2264. /* chest trap or magic bag explosion or <esc> */ 2265. free((genericptr_t) pick_list); 2266. return (timepassed ? ECMD_TIME : ECMD_OK); 2267. } 2268. } 2269. free((genericptr_t) pick_list); 2270. } 2271. if (n != 0) 2272. c = 'y'; 2273. } else { 2274. for (cobj = svl.level.objects[cc.x][cc.y]; cobj; cobj = nobj) { 2275. nobj = cobj->nexthere; 2276. 2277. if (Is_container(cobj)) { 2278. anyfound = TRUE; 2279. timepassed |= do_loot_cont(&cobj, 1, 1); 2280. if (ga.abort_looting) 2281. /* chest trap or magic bag explosion or <esc> */ 2282. return (timepassed ? ECMD_TIME : ECMD_OK); 2283. } 2284. } 2285. if (anyfound) 2286. c = 'y'; 2287. } 2288. } else if (IS_GRAVE(levl[cc.x][cc.y].typ)) { 2289. You("need to dig up the grave to effectively loot it..."); 2290. } 2291. 2292. /* 2293. * 3.3.1 introduced directional looting for some things. 2294. */ 2295. lootmon: 2296. if (c != 'y' && (mon_beside(u.ux, u.uy) || iflags.menu_requested)) { 2297. boolean looted_mon = FALSE; 2298. if (!get_adjacent_loc("Loot in what direction?", 2299. "Invalid loot location", u.ux, u.uy, &cc)) 2300. return ECMD_OK; 2301. underfoot = u_at(cc.x, cc.y); 2302. if (underfoot && container_at(cc.x, cc.y, FALSE)) 2303. goto lootcont; 2304. if (u.dz < 0) { 2305. You("%s to loot on the %s.", dont_find_anything, 2306. ceiling(cc.x, cc.y)); 2307. return ECMD_TIME; 2308. } 2309. mtmp = m_at(cc.x, cc.y); 2310. if (mtmp) { 2311. timepassed = loot_mon(mtmp, &prev_inquiry, &prev_loot); 2312. if (timepassed) 2313. looted_mon = TRUE; 2314. } 2315. /* always use a turn when choosing a direction is impaired, 2316. even if you've successfully targeted a saddled creature 2317. and then answered "no" to the "remove its saddle?" prompt */ 2318. if (Confusion || Stunned) 2319. timepassed = 1; 2320. 2321. /* Preserve pre-3.3.1 behavior for containers. 2322. * Adjust this if-block to allow container looting 2323. * from one square away to change that in the future. 2324. */ 2325. if (!looted_mon) { 2326. if (!underfoot && container_at(cc.x, cc.y, FALSE)) { 2327. if (mtmp) { 2328. You_cant("loot anything %sthere with %s in the way.", 2329. prev_inquiry ? "else " : "", mon_nam(mtmp)); 2330. return (timepassed ? ECMD_TIME : ECMD_OK); 2331. } else { 2332. You("have to be at a container to loot it."); 2333. } 2334. } else { 2335. You("%s %s%shere to loot.", dont_find_anything, 2336. (prev_inquiry || prev_loot) ? "else " : "", 2337. !underfoot ? "t" : ""); 2338. return (timepassed ? ECMD_TIME : ECMD_OK); 2339. } 2340. } 2341. } else if (c != 'y' && c != 'n') { 2342. You("%s %s to loot.", dont_find_anything, 2343. underfoot ? "here" : "there"); 2344. } 2345. return (timepassed ? ECMD_TIME : ECMD_OK); 2346. } 2347. 2348. /* called when attempting to #loot while confused */ 2349. staticfn boolean 2350. reverse_loot(void) 2351. { 2352. struct obj *goldob = 0, *coffers, *otmp, boxdummy; 2353. struct monst *mon; 2354. long contribution; 2355. int n, x = u.ux, y = u.uy; 2356. 2357. if (!rn2(3)) { 2358. /* n objects: 1/(n+1) chance per object, 1/(n+1) to fall off end */ 2359. for (n = inv_cnt(TRUE), otmp = gi.invent; otmp; 2360. --n, otmp = otmp->nobj) 2361. if (!rn2(n + 1)) { 2362. prinv("You find old loot:", otmp, 0L); 2363. return TRUE; 2364. } 2365. return FALSE; 2366. } 2367. 2368. /* find a money object to mess with */ 2369. for (goldob = gi.invent; goldob; goldob = goldob->nobj) 2370. if (goldob->oclass == COIN_CLASS) { 2371. contribution = ((long) rnd(5) * goldob->quan + 4L) / 5L; 2372. if (contribution < goldob->quan) 2373. goldob = splitobj(goldob, contribution); 2374. break; 2375. } 2376. if (!goldob) 2377. return FALSE; 2378. 2379. /* gold might be quivered; dropping would un-wear it, but freeinv() 2380. expects caller to do that; do so now */ 2381. remove_worn_item(goldob, FALSE); 2382. 2383. if (!IS_THRONE(levl[x][y].typ)) { 2384. dropx(goldob); 2385. /* the dropped gold might have fallen to lower level */ 2386. if (g_at(x, y)) 2387. pline("Ok, now there is loot here."); 2388. } else { 2389. /* find original coffers chest if present, otherwise use nearest */ 2390. otmp = 0; 2391. for (coffers = fobj; coffers; coffers = coffers->nobj) 2392. if (coffers->otyp == CHEST) { 2393. if (coffers->spe == 2) 2394. break; /* a throne room chest */ 2395. if (!otmp || (distu(coffers->ox, coffers->oy) 2396. < distu(otmp->ox, otmp->oy))) 2397. otmp = coffers; /* remember closest ordinary chest */ 2398. } 2399. if (!coffers) 2400. coffers = otmp; 2401. 2402. if (coffers) { 2403. SetVoice((struct monst *) 0, 0, 80, 0); 2404. verbalize("Thank you for your contribution to reduce the debt."); 2405. freeinv(goldob); 2406. (void) add_to_container(coffers, goldob); 2407. coffers->owt = weight(coffers); 2408. coffers->cknown = 0; 2409. if (!coffers->olocked) { 2410. boxdummy = cg.zeroobj, boxdummy.otyp = SPE_WIZARD_LOCK; 2411. (void) boxlock(coffers, &boxdummy); 2412. } 2413. } else if (levl[x][y].looted != T_LOOTED 2414. && (mon = makemon(courtmon(), x, y, NO_MM_FLAGS)) != 0) { 2415. freeinv(goldob); 2416. add_to_minv(mon, goldob); 2417. pline("The exchequer accepts your contribution."); 2418. if (!rn2(10)) 2419. levl[x][y].looted = T_LOOTED; 2420. } else { 2421. You("drop %s.", doname(goldob)); 2422. dropx(goldob); 2423. } 2424. } 2425. return TRUE; 2426. } 2427. 2428. /* loot_mon() returns amount of time passed. 2429. */ 2430. int 2431. loot_mon(struct monst *mtmp, int *passed_info, boolean *prev_loot) 2432. { 2433. int c = -1; 2434. int timepassed = 0; 2435. struct obj *otmp; 2436. char qbuf[QBUFSZ]; 2437. 2438. /* 3.3.1 introduced the ability to remove saddle from a steed. 2439. * *passed_info is set to TRUE if a loot query was given. 2440. * *prev_loot is set to TRUE if something was actually acquired in here. 2441. */ 2442. if (mtmp && mtmp != u.usteed && (otmp = which_armor(mtmp, W_SADDLE))) { 2443. if (passed_info) 2444. *passed_info = 1; 2445. Sprintf(qbuf, "Do you want to remove the saddle from %s?", 2446. x_monnam(mtmp, ARTICLE_THE, (char *) 0, 2447. SUPPRESS_SADDLE, FALSE)); 2448. if ((c = yn_function(qbuf, ynqchars, 'n', TRUE)) == 'y') { 2449. if (nolimbs(gy.youmonst.data)) { 2450. You_cant("do that without limbs."); /* not body_part(HAND) */ 2451. return 0; 2452. } 2453. if (otmp->cursed) { 2454. You("can't. The saddle seems to be stuck to %s.", 2455. x_monnam(mtmp, ARTICLE_THE, (char *) 0, 2456. SUPPRESS_SADDLE, FALSE)); 2457. /* the attempt costs you time */ 2458. return 1; 2459. } 2460. extract_from_minvent(mtmp, otmp, TRUE, FALSE); 2461. if (flags.verbose) 2462. You("take %s off of %s.", 2463. thesimpleoname(otmp), mon_nam(mtmp)); 2464. otmp = hold_another_object(otmp, "You drop %s!", doname(otmp), 2465. (const char *) 0); 2466. nhUse(otmp); 2467. timepassed = rnd(3); 2468. if (prev_loot) 2469. *prev_loot = TRUE; 2470. } else if (c == 'q') { 2471. return 0; 2472. } 2473. } 2474. /* 3.4.0 introduced ability to pick things up from swallower's stomach */ 2475. if (u.uswallow) { 2476. int count = passed_info ? *passed_info : 0; 2477. 2478. timepassed = pickup(count); 2479. } 2480. return timepassed; 2481. } 2482. 2483. /* 2484. * Decide whether an object being placed into a magic bag will cause 2485. * it to explode. If the object is a bag itself, check recursively. 2486. */ 2487. staticfn boolean 2488. mbag_explodes(struct obj *obj, int depthin) 2489. { 2490. /* these won't cause an explosion when they're empty */ 2491. if ((obj->otyp == WAN_CANCELLATION || obj->otyp == BAG_OF_TRICKS) 2492. && obj->spe <= 0) 2493. return FALSE; 2494. 2495. /* odds: 1/1, 2/2, 3/4, 4/8, 5/16, 6/32, 7/64, 8/128, 9/128, 10/128,... */ 2496. if ((Is_mbag(obj) || obj->otyp == WAN_CANCELLATION) 2497. && (rn2(1 << (depthin > 7 ? 7 : depthin)) <= depthin)) 2498. return TRUE; 2499. else if (Has_contents(obj)) { 2500. struct obj *otmp; 2501. 2502. for (otmp = obj->cobj; otmp; otmp = otmp->nobj) 2503. if (mbag_explodes(otmp, depthin + 1)) 2504. return TRUE; 2505. } 2506. return FALSE; 2507. } 2508. 2509. staticfn boolean 2510. is_boh_item_gone(void) 2511. { 2512. return (boolean) (!rn2(13)); 2513. } 2514. 2515. /* Scatter most of Bag of holding contents around. Some items will be 2516. destroyed with the same chance as looting a cursed bag. */ 2517. staticfn void 2518. do_boh_explosion(struct obj *boh, boolean on_floor) 2519. { 2520. struct obj *otmp, *nobj; 2521. 2522. boh->in_use = 1; /* in case scatter() leads to bones creation */ 2523. for (otmp = boh->cobj; otmp; otmp = nobj) { 2524. nobj = otmp->nobj; 2525. if (is_boh_item_gone()) { 2526. obj_extract_self(otmp); 2527. mbag_item_gone(!on_floor, otmp, TRUE); 2528. } else { 2529. otmp->ox = u.ux, otmp->oy = u.uy; 2530. (void) scatter(u.ux, u.uy, 4, MAY_HIT | MAY_DESTROY, otmp); 2531. } 2532. } 2533. /* boh is about to be deleted so no need to reset its in_use flag here */ 2534. } 2535. 2536. staticfn long 2537. boh_loss(struct obj *container, boolean held) 2538. { 2539. /* sometimes toss objects if a cursed magic bag */ 2540. if (Is_mbag(container) && container->cursed && Has_contents(container)) { 2541. long loss = 0L; 2542. struct obj *curr, *otmp; 2543. 2544. for (curr = container->cobj; curr; curr = otmp) { 2545. otmp = curr->nobj; 2546. if (is_boh_item_gone()) { 2547. obj_extract_self(curr); 2548. loss += mbag_item_gone(held, curr, FALSE); 2549. } 2550. } 2551. return loss; 2552. } 2553. return 0; 2554. } 2555. 2556. /* Returns: -1 to stop, 1 item was inserted, 0 item was not inserted. */ 2557. staticfn int 2558. in_container(struct obj *obj) 2559. { 2560. boolean floor_container = !carried(gc.current_container); 2561. boolean was_unpaid = FALSE; 2562. char buf[BUFSZ]; 2563. 2564. if (!gc.current_container) { 2565. impossible("<in> no gc.current_container?"); 2566. return 0; 2567. } else if (obj == uball || obj == uchain) { 2568. You("must be kidding."); 2569. return 0; 2570. } else if (obj == gc.current_container) { 2571. pline("That would be an interesting topological exercise."); 2572. return 0; 2573. } else if (obj->owornmask & (W_ARMOR | W_ACCESSORY)) { 2574. Norep("You cannot %s %s you are wearing.", 2575. Icebox ? "refrigerate" : "stash", something); 2576. return 0; 2577. } else if ((obj->otyp == LOADSTONE) && obj->cursed) { 2578. set_bknown(obj, 1); 2579. pline_The("stone%s won't leave your person.", plur(obj->quan)); 2580. return 0; 2581. } else if (obj->otyp == AMULET_OF_YENDOR 2582. || obj->otyp == CANDELABRUM_OF_INVOCATION 2583. || obj->otyp == BELL_OF_OPENING 2584. || obj->otyp == SPE_BOOK_OF_THE_DEAD) { 2585. /* Prohibit Amulets in containers; if you allow it, monsters can't 2586. * steal them. It also becomes a pain to check to see if someone 2587. * has the Amulet. Ditto for the Candelabrum, the Bell and the Book. 2588. */ 2589. pline("%s cannot be confined in such trappings.", The(xname(obj))); 2590. return 0; 2591. } else if (obj->otyp == LEASH && obj->leashmon != 0) { 2592. pline("%s attached to your pet.", Tobjnam(obj, "are")); 2593. return 0; 2594. } else if (obj == uwep) { 2595. if (welded(obj)) { 2596. weldmsg(obj); 2597. return 0; 2598. } 2599. setuwep((struct obj *) 0); 2600. /* This uwep check is obsolete. It dates to 3.0 and earlier when 2601. * unwielding Firebrand would be fatal in hell if hero had no other 2602. * fire resistance. Life-saving would force it to be re-wielded. 2603. */ 2604. if (uwep) 2605. return 0; /* unwielded, died, rewielded */ 2606. } else if (obj == uswapwep) { 2607. setuswapwep((struct obj *) 0); 2608. } else if (obj == uquiver) { 2609. setuqwep((struct obj *) 0); 2610. } 2611. 2612. if (fatal_corpse_mistake(obj, FALSE)) 2613. return -1; 2614. 2615. /* boxes, boulders, and big statues can't fit into any container */ 2616. if (obj->otyp == ICE_BOX || Is_box(obj) || obj->otyp == BOULDER 2617. || (obj->otyp == STATUE && bigmonst(&mons[obj->corpsenm]))) { 2618. /* consumes multiple obufs but not enough to overwrite the result */ 2619. Strcpy(buf, the(xname(obj))); 2620. You("cannot fit %s into %s.", buf, the(xname(gc.current_container))); 2621. return 0; 2622. } 2623. 2624. freeinv(obj); 2625. 2626. if (obj_is_burning(obj)) /* this used to be part of freeinv() */ 2627. (void) snuff_lit(obj); 2628. 2629. if (floor_container && costly_spot(u.ux, u.uy)) { 2630. /* defer gold until after put-in message */ 2631. if (obj->oclass != COIN_CLASS) { 2632. /* sellobj() will take an unpaid item off the shop bill */ 2633. was_unpaid = obj->unpaid ? TRUE : FALSE; 2634. if (gs.sellobj_first) { 2635. /* don't sell when putting the item into your own container, 2636. but handle billing correctly */ 2637. sellobj_state(gc.current_container->no_charge 2638. ? SELL_DONTSELL : SELL_DELIBERATE); 2639. gs.sellobj_first = FALSE; 2640. } 2641. sellobj(obj, u.ux, u.uy); 2642. } 2643. } 2644. if (Icebox && !age_is_relative(obj)) { 2645. obj->age = svm.moves - obj->age; /* actual age */ 2646. /* stop any corpse timeouts when frozen */ 2647. if (obj->otyp == CORPSE) { 2648. if (obj->timed) { 2649. (void) stop_timer(ROT_CORPSE, obj_to_any(obj)); 2650. (void) stop_timer(REVIVE_MON, obj_to_any(obj)); 2651. } 2652. /* if this is the corpse of a cancelled ice troll, uncancel it */ 2653. if (obj->corpsenm == PM_ICE_TROLL && has_omonst(obj)) 2654. OMONST(obj)->mcan = 0; 2655. } else if (obj->globby && obj->timed) { 2656. (void) stop_timer(SHRINK_GLOB, obj_to_any(obj)); 2657. } 2658. } else if (Is_mbag(gc.current_container) && mbag_explodes(obj, 0)) { 2659. livelog_printf(LL_ACHIEVE, "just blew up %s bag of holding", uhis()); 2660. /* explicitly mention what item is triggering the explosion */ 2661. urgent_pline( 2662. "As you put %s inside, you are blasted by a magical explosion!", 2663. doname(obj)); 2664. /* did not actually insert obj yet */ 2665. if (was_unpaid) 2666. addtobill(obj, FALSE, FALSE, TRUE); 2667. if (obj->otyp == BAG_OF_HOLDING) /* one bag of holding into another */ 2668. do_boh_explosion(obj, (boolean) (obj->where == OBJ_FLOOR)); 2669. obfree(obj, (struct obj *) 0); 2670. /* if carried, shop goods will be flagged 'unpaid' and obfree() will 2671. handle bill issues, but if on floor, we need to put them on bill 2672. before deleting them (non-shop items will be flagged 'no_charge')*/ 2673. if (floor_container && costly_spot(gc.current_container->ox, 2674. gc.current_container->oy)) { 2675. struct obj save_no_charge; 2676. 2677. save_no_charge.no_charge = gc.current_container->no_charge; 2678. addtobill(gc.current_container, FALSE, FALSE, FALSE); 2679. /* addtobill() clears no charge; we need to set it back 2680. so that useupf() doesn't double bill */ 2681. gc.current_container->no_charge = save_no_charge.no_charge; 2682. } 2683. do_boh_explosion(gc.current_container, floor_container); 2684. 2685. if (!floor_container) 2686. useup(gc.current_container); 2687. else if (obj_here(gc.current_container, u.ux, u.uy)) 2688. useupf(gc.current_container, gc.current_container->quan); 2689. else 2690. panic("in_container: bag not found."); 2691. 2692. losehp(d(6, 6), "magical explosion", KILLED_BY_AN); 2693. gc.current_container = 0; /* baggone = TRUE; */ 2694. } 2695. 2696. if (gc.current_container) { 2697. Strcpy(buf, the(xname(gc.current_container))); 2698. You("put %s into %s.", doname(obj), buf); 2699. 2700. /* gold in container always needs to be added to credit */ 2701. if (floor_container && obj->oclass == COIN_CLASS) 2702. sellobj(obj, gc.current_container->ox, gc.current_container->oy); 2703. (void) add_to_container(gc.current_container, obj); 2704. gc.current_container->owt = weight(gc.current_container); 2705. } 2706. /* gold needs this, and freeinv() many lines above may cause 2707. * the encumbrance to disappear from the status, so just always 2708. * update status immediately. 2709. */ 2710. bot(); 2711. return (gc.current_container ? 1 : -1); 2712. } 2713. 2714. /* askchain() filter used by in_container(); 2715. * returns True if the container is intact and 'obj' isn't it, False if 2716. * container is gone (magic bag explosion) or 'obj' is the container itself; 2717. * also used by getobj() when picking a single item to stash 2718. */ 2719. int 2720. ck_bag(struct obj *obj) 2721. { 2722. return (gc.current_container && obj != gc.current_container); 2723. } 2724. 2725. /* Returns: -1 to stop, 1 item was removed, 0 item was not removed. */ 2726. staticfn int 2727. out_container(struct obj *obj) 2728. { 2729. struct obj *otmp; 2730. int res; 2731. long count; 2732. boolean is_gold = (obj->oclass == COIN_CLASS); 2733. 2734. if (!gc.current_container) { 2735. impossible("<out> no gc.current_container?"); 2736. return -1; 2737. } else if (is_gold) { 2738. obj->owt = weight(obj); 2739. } 2740. 2741. if (obj->oartifact && !touch_artifact(obj, &gy.youmonst)) 2742. return 0; 2743. 2744. if (fatal_corpse_mistake(obj, FALSE)) 2745. return -1; 2746. 2747. count = obj->quan; 2748. if ((res = lift_object(obj, gc.current_container, &count, FALSE)) <= 0) 2749. return res; 2750. 2751. if (obj->quan != count && obj->otyp != LOADSTONE) 2752. obj = splitobj(obj, count); 2753. 2754. /* Remove the object from the list. */ 2755. obj_extract_self(obj); 2756. gc.current_container->owt = weight(gc.current_container); 2757. 2758. if (Icebox) 2759. removed_from_icebox(obj); 2760. 2761. if (!obj->unpaid && !carried(gc.current_container) 2762. && costly_spot(gc.current_container->ox, gc.current_container->oy)) { 2763. obj->ox = gc.current_container->ox; 2764. obj->oy = gc.current_container->oy; 2765. addtobill(obj, FALSE, FALSE, FALSE); 2766. } 2767. if (is_pick(obj)) 2768. pick_pick(obj); /* shopkeeper feedback */ 2769. 2770. otmp = addinv(obj); 2771. pickup_prinv(otmp, count, "removing"); 2772. 2773. if (is_gold) { 2774. bot(); /* update character's gold piece count immediately */ 2775. } 2776. return 1; 2777. } 2778. 2779. /* taking a corpse out of an ice box needs a couple of adjustments */ 2780. void 2781. removed_from_icebox(struct obj *obj) 2782. { 2783. if (!age_is_relative(obj)) { 2784. obj->age = svm.moves - obj->age; /* actual age */ 2785. if (obj->otyp == CORPSE) { 2786. struct monst *m = get_mtraits(obj, FALSE); 2787. boolean iceT = m ? (m->data == &mons[PM_ICE_TROLL]) 2788. : (obj->corpsenm == PM_ICE_TROLL); 2789. 2790. /* start a revive timer if this corpse is for an ice troll, 2791. otherwise start a rot-away timer (even for other trolls) */ 2792. obj->norevive = iceT ? 0 : 1; 2793. start_corpse_timeout(obj); 2794. } else if (obj->globby) { 2795. /* non-frozen globs gradually shrink away to nothing */ 2796. start_glob_timeout(obj, 0L); 2797. } 2798. } 2799. } 2800. 2801. /* an object inside a cursed bag of holding is being destroyed */ 2802. staticfn long 2803. mbag_item_gone(boolean held, struct obj *item, boolean silent) 2804. { 2805. struct monst *shkp; 2806. long loss = 0L; 2807. 2808. if (!silent) { 2809. if (item->dknown) 2810. pline("%s %s vanished!", Doname2(item), otense(item, "have")); 2811. else 2812. You("%s %s disappear!", Blind ? "notice" : "see", doname(item)); 2813. } 2814. 2815. if (*u.ushops && (shkp = shop_keeper(*u.ushops)) != 0) { 2816. if (held ? (boolean) item->unpaid : costly_spot(u.ux, u.uy)) 2817. loss = stolen_value(item, u.ux, u.uy, (boolean) shkp->mpeaceful, 2818. TRUE); 2819. } 2820. obfree(item, (struct obj *) 0); 2821. return loss; 2822. } 2823. 2824. /* used for #loot/apply, #tip, and final disclosure */ 2825. void 2826. observe_quantum_cat(struct obj *box, boolean makecat, boolean givemsg) 2827. { 2828. static NEARDATA const char sc[] = "Schroedinger's Cat"; 2829. struct obj *deadcat; 2830. struct monst *livecat = 0; 2831. coordxy ox, oy; 2832. boolean itsalive = !rn2(2); 2833. 2834. if (get_obj_location(box, &ox, &oy, 0)) 2835. box->ox = ox, box->oy = oy; /* in case it's being carried */ 2836. 2837. /* this isn't really right, since any form of observation 2838. (telepathic or monster/object/food detection) ought to 2839. force the determination of alive vs dead state; but basing it 2840. just on opening or disclosing the box is much simpler to cope with */ 2841. 2842. /* SchroedingersBox already has a cat corpse in it */ 2843. deadcat = box->cobj; 2844. if (itsalive) { 2845. if (makecat) 2846. livecat = makemon(&mons[PM_HOUSECAT], box->ox, box->oy, 2847. NO_MINVENT | MM_ADJACENTOK | MM_NOMSG); 2848. if (livecat) { 2849. livecat->mpeaceful = 1; 2850. set_malign(livecat); 2851. if (givemsg) { 2852. if (!canspotmon(livecat)) 2853. You("think %s brushed your %s.", something, 2854. body_part(FOOT)); 2855. else 2856. pline("%s inside the box is still alive!", 2857. Monnam(livecat)); 2858. } 2859. (void) christen_monst(livecat, sc); 2860. if (deadcat) { 2861. obj_extract_self(deadcat); 2862. obfree(deadcat, (struct obj *) 0), deadcat = 0; 2863. } 2864. box->owt = weight(box); 2865. box->spe = 0; 2866. 2867. if (!svc.context.mon_moving) { 2868. /* give experience points for releasing live cat; slightly 2869. different amount from what is given for "killing" it */ 2870. more_experienced(10, 20); /* 10:current exp; 20:score bonus */ 2871. newexplevel(); 2872. } 2873. } 2874. } else { 2875. box->spe = 0; /* now an ordinary box (with a cat corpse inside) */ 2876. if (givemsg) 2877. pline_The("%s inside the box is dead!", 2878. Hallucination ? rndmonnam((char *) 0) : "housecat"); 2879. if (deadcat) { 2880. /* set_corpsenm() will start the rot timer that was removed 2881. when makemon() created SchroedingersBox; start it from 2882. now rather than from when this special corpse got created */ 2883. deadcat->age = svm.moves; 2884. set_corpsenm(deadcat, PM_HOUSECAT); 2885. deadcat = oname(deadcat, sc, ONAME_NO_FLAGS); 2886. 2887. if (!svc.context.mon_moving) { 2888. /* give experience points for the death of the cat since 2889. that has been finalized by the hero opening the box */ 2890. more_experienced(20, 10); /* 20:current exp; 10:score bonus */ 2891. newexplevel(); 2892. } 2893. } 2894. } 2895. nhUse(deadcat); 2896. return; 2897. } 2898. 2899. #undef Icebox 2900. 2901. /* used by askchain() to check for magic bag explosion */ 2902. boolean 2903. container_gone(int (*fn)(OBJ_P)) 2904. { 2905. /* result is only meaningful while use_container() is executing */ 2906. return ((fn == in_container || fn == out_container) 2907. && !gc.current_container); 2908. } 2909. 2910. staticfn void 2911. explain_container_prompt(boolean more_containers) 2912. { 2913. static const char *const explaintext[] = { 2914. "Container actions:", 2915. "", 2916. " : -- Look: examine contents", 2917. " o -- Out: take things out", 2918. " i -- In: put things in", 2919. " b -- Both: first take things out, then put things in", 2920. " r -- Reversed: put things in, then take things out", 2921. " s -- Stash: put one item in", "", 2922. " n -- Next: loot next selected container", 2923. " q -- Quit: finished", 2924. " ? -- Help: display this text.", 2925. "", 0 2926. }; 2927. const char *const *txtpp; 2928. winid win; 2929. 2930. /* "Do what with <container>? [:oibrsq or ?] (q)" */ 2931. if ((win = create_nhwindow(NHW_TEXT)) != WIN_ERR) { 2932. for (txtpp = explaintext; *txtpp; ++txtpp) { 2933. if (!more_containers && !strncmp(*txtpp, " n ", 3)) 2934. continue; 2935. putstr(win, 0, *txtpp); 2936. } 2937. display_nhwindow(win, FALSE); 2938. destroy_nhwindow(win); 2939. } 2940. } 2941. 2942. boolean 2943. u_handsy(void) 2944. { 2945. if (nohands(gy.youmonst.data)) { 2946. You("have no hands!"); /* not `body_part(HAND)' */ 2947. return FALSE; 2948. } else if (!freehand()) { 2949. You("have no free %s.", body_part(HAND)); 2950. return FALSE; 2951. } 2952. return TRUE; 2953. } 2954. 2955. /* getobj callback for object to be stashed into a container */ 2956. staticfn int 2957. stash_ok(struct obj *obj) 2958. { 2959. if (!obj) 2960. return GETOBJ_EXCLUDE; 2961. 2962. /* downplay the container being stashed into */ 2963. if (!ck_bag(obj)) 2964. return GETOBJ_EXCLUDE_SELECTABLE; 2965. /* Possible extension: downplay things too big to fit into containers (in 2966. * which case extract in_container()'s logic.) */ 2967. 2968. return GETOBJ_SUGGEST; 2969. } 2970. 2971. int 2972. use_container( 2973. struct obj **objp, 2974. boolean held, 2975. boolean more_containers) /* True iff #loot multiple and this isn't last */ 2976. { 2977. struct obj *otmp, *obj = *objp; 2978. boolean quantum_cat, cursed_mbag, loot_out, loot_in, loot_in_first, 2979. stash_one, inokay, outokay, outmaybe; 2980. char c, emptymsg[BUFSZ], qbuf[QBUFSZ], pbuf[QBUFSZ], xbuf[QBUFSZ]; 2981. int used = ECMD_OK; 2982. long loss; 2983. 2984. ga.abort_looting = FALSE; 2985. gs.sellobj_first = TRUE; /* in_container() should call sellobj_state() */ 2986. emptymsg[0] = '\0'; 2987. 2988. if (!u_handsy()) 2989. return ECMD_OK; 2990. 2991. if (!obj->lknown) { /* do this in advance */ 2992. obj->lknown = 1; 2993. if (held) 2994. update_inventory(); 2995. } 2996. if (obj->olocked) { 2997. pline("%s locked.", Tobjnam(obj, "are")); 2998. if (held) 2999. You("must put it down to unlock."); 3000. return ECMD_OK; 3001. } else if (obj->otrapped) { 3002. if (held) 3003. You("open %s...", the(xname(obj))); 3004. (void) chest_trap(obj, HAND, FALSE); 3005. /* even if the trap fails, you've used up this turn */ 3006. if (gm.multi >= 0) { /* in case we didn't become paralyzed */ 3007. nomul(-1); 3008. gm.multi_reason = "opening a container"; 3009. gn.nomovemsg = ""; 3010. } 3011. ga.abort_looting = TRUE; 3012. return ECMD_TIME; 3013. } 3014. 3015. gc.current_container = obj; /* for use by in/out_container */ 3016. /* 3017. * From here on out, all early returns go through 'containerdone:'. 3018. */ 3019. 3020. /* check for Schroedinger's Cat */ 3021. quantum_cat = SchroedingersBox(gc.current_container); 3022. if (quantum_cat) { 3023. observe_quantum_cat(gc.current_container, TRUE, TRUE); 3024. used = ECMD_TIME; 3025. } 3026. 3027. cursed_mbag = Is_mbag(gc.current_container) 3028. && gc.current_container->cursed 3029. && Has_contents(gc.current_container); 3030. if (cursed_mbag 3031. && (loss = boh_loss(gc.current_container, held)) != 0) { 3032. used = ECMD_TIME; 3033. You("owe %ld %s for lost merchandise.", loss, currency(loss)); 3034. gc.current_container->owt = weight(gc.current_container); 3035. } 3036. /* might put something in if carrying anything other than just the 3037. container itself (invent is not the container or has a next object) */ 3038. inokay = (gi.invent != 0 && (gi.invent != gc.current_container 3039. || gi.invent->nobj)); 3040. /* might take something out if container isn't empty */ 3041. outokay = Has_contents(gc.current_container); 3042. if (!outokay) /* preformat the empty-container message */ 3043. Sprintf(emptymsg, "%s is %sempty.", 3044. Ysimple_name2(gc.current_container), 3045. (quantum_cat || cursed_mbag) ? "now " : ""); 3046. 3047. /* 3048. * What-to-do prompt's list of possible actions: 3049. * always include the look-inside choice (':'); 3050. * include the take-out choice ('o') if container 3051. * has anything in it or if player doesn't yet know 3052. * that it's empty (latter can change on subsequent 3053. * iterations if player picks ':' response); 3054. * include the put-in choices ('i','s') if hero 3055. * carries any inventory (including gold) aside from 3056. * the container itself; 3057. * include do-both when 'o' is available, even if 3058. * inventory is empty--taking out could alter that; 3059. * include do-both-reversed when 'i' is available, 3060. * even if container is empty--for similar reason; 3061. * include the next container choice ('n') when 3062. * relevant, and make it the default; 3063. * always include the quit choice ('q'), and make 3064. * it the default if there's no next container; 3065. * include the help choice (" or ?") if `cmdassist' 3066. * run-time option is set; 3067. * (Player can pick any of (o,i,b,r,n,s,?) even when 3068. * they're not listed among the available actions.) 3069. * 3070. * Do what with <the/your/Shk's container>? [:oibrs nq or ?] (q) 3071. * or 3072. * <The/Your/Shk's container> is empty. Do what with it? [:irs nq or ?] 3073. */ 3074. for (;;) { /* repeats iff '?' or ':' gets chosen */ 3075. outmaybe = (outokay || !gc.current_container->cknown); 3076. if (!outmaybe) 3077. (void) safe_qbuf(qbuf, (char *) 0, " is empty. Do what with it?", 3078. gc.current_container, Yname2, Ysimple_name2, 3079. "This"); 3080. else 3081. (void) safe_qbuf(qbuf, "Do what with ", "?", gc.current_container, 3082. yname, ysimple_name, "it"); 3083. /* ask player about what to do with this container */ 3084. if (flags.menu_style == MENU_PARTIAL 3085. || flags.menu_style == MENU_FULL) { 3086. if (!inokay && !outmaybe) { 3087. /* nothing to take out, nothing to put in; 3088. trying to do both will yield proper feedback */ 3089. c = 'b'; 3090. } else { 3091. c = in_or_out_menu(qbuf, gc.current_container, 3092. outmaybe, inokay, 3093. (boolean) (used != ECMD_OK), 3094. more_containers); 3095. } 3096. } else { /* TRADITIONAL or COMBINATION */ 3097. xbuf[0] = '\0'; /* list of extra acceptable responses */ 3098. Strcpy(pbuf, ":"); /* look inside */ 3099. Strcat(outmaybe ? pbuf : xbuf, "o"); /* take out */ 3100. Strcat(inokay ? pbuf : xbuf, "i"); /* put in */ 3101. Strcat(outmaybe ? pbuf : xbuf, "b"); /* both */ 3102. Strcat(inokay ? pbuf : xbuf, "rs"); /* reversed, stash */ 3103. Strcat(pbuf, " "); /* separator */ 3104. Strcat(more_containers ? pbuf : xbuf, "n"); /* next container */ 3105. Strcat(pbuf, "q"); /* quit */ 3106. if (iflags.cmdassist) 3107. /* this unintentionally allows user to answer with 'o' or 3108. 'r'; fortunately, those are already valid choices here */ 3109. Strcat(pbuf, " or ?"); /* help */ 3110. else 3111. Strcat(xbuf, "?"); 3112. if (*xbuf) 3113. Strcat(strcat(pbuf, "\033"), xbuf); 3114. c = yn_function(qbuf, pbuf, more_containers ? 'n' : 'q', TRUE); 3115. } /* PARTIAL|FULL vs other modes */ 3116. 3117. if (c == '?') { 3118. explain_container_prompt(more_containers); 3119. } else if (c == ':') { /* note: will set obj->cknown */ 3120. if (!gc.current_container->cknown) 3121. used = ECMD_TIME; /* gaining info */ 3122. container_contents(gc.current_container, FALSE, FALSE, TRUE); 3123. } else 3124. break; 3125. } /* loop until something other than '?' or ':' is picked */ 3126. 3127. if (c == 'q') 3128. ga.abort_looting = TRUE; 3129. if (c == 'n' || c == 'q') /* [not strictly needed; falling thru works] */ 3130. goto containerdone; 3131. loot_out = (c == 'o' || c == 'b' || c == 'r'); 3132. loot_in = (c == 'i' || c == 'b' || c == 'r'); 3133. loot_in_first = (c == 'r'); /* both, reversed */ 3134. stash_one = (c == 's'); 3135. 3136. /* out-only or out before in */ 3137. if (loot_out && !loot_in_first) { 3138. if (!Has_contents(gc.current_container)) { 3139. pline1(emptymsg); /* <whatever> is empty. */ 3140. if (!gc.current_container->cknown) 3141. used = ECMD_TIME; 3142. gc.current_container->cknown = 1; 3143. } else { 3144. add_valid_menu_class(0); /* reset */ 3145. if (flags.menu_style == MENU_TRADITIONAL) 3146. used |= traditional_loot(FALSE); 3147. else 3148. used |= (menu_loot(0, FALSE) > 0); 3149. add_valid_menu_class(0); 3150. } 3151. /* recalculate 'inokay' in case something was just taken out and 3152. inventory is no longer empty or no longer just the container */ 3153. inokay = (gi.invent && (gi.invent != gc.current_container 3154. || gi.invent->nobj)); 3155. } 3156. 3157. if ((loot_in || stash_one) && !inokay) { 3158. You("don't have anything%s to %s.", gi.invent ? " else" : "", 3159. stash_one ? "stash" : "put in"); 3160. loot_in = stash_one = FALSE; 3161. } 3162. 3163. /* 3164. * Gone: being nice about only selecting food if we know we are 3165. * putting things in an ice chest. 3166. */ 3167. if (loot_in) { 3168. add_valid_menu_class(0); /* reset */ 3169. if (flags.menu_style == MENU_TRADITIONAL) 3170. used |= traditional_loot(TRUE); 3171. else 3172. used |= (menu_loot(0, TRUE) > 0); 3173. add_valid_menu_class(0); 3174. } else if (stash_one) { 3175. /* put one item into container */ 3176. if ((otmp = getobj("stash", stash_ok, 3177. GETOBJ_PROMPT | GETOBJ_ALLOWCNT)) != 0) { 3178. if (in_container(otmp)) { 3179. used = 1; 3180. } else { 3181. /* couldn't put selected item into container for some 3182. reason; might need to undo splitobj() */ 3183. (void) unsplitobj(otmp); 3184. } 3185. } 3186. } 3187. /* putting something in might have triggered magic bag explosion */ 3188. if (!gc.current_container) 3189. loot_out = FALSE; 3190. 3191. /* out after in */ 3192. if (loot_out && loot_in_first) { 3193. if (!Has_contents(gc.current_container)) { 3194. pline1(emptymsg); /* <whatever> is empty. */ 3195. if (!gc.current_container->cknown) 3196. used = 1; 3197. gc.current_container->cknown = 1; 3198. } else { 3199. add_valid_menu_class(0); /* reset */ 3200. if (flags.menu_style == MENU_TRADITIONAL) 3201. used |= traditional_loot(FALSE); 3202. else 3203. used |= (menu_loot(0, FALSE) > 0); 3204. add_valid_menu_class(0); 3205. } 3206. } 3207. 3208. containerdone: 3209. if (used) { 3210. /* Not completely correct; if we put something in without knowing 3211. whatever was already inside, now we suddenly do. That can't 3212. be helped unless we want to track things item by item and then 3213. deal with containers whose contents are "partly known". */ 3214. if (gc.current_container) 3215. gc.current_container->cknown = 1; 3216. update_inventory(); 3217. } 3218. 3219. sellobj_state(SELL_NORMAL); /* in case in_container() set it */ 3220. *objp = gc.current_container; /* might have become null */ 3221. if (gc.current_container) 3222. gc.current_container = 0; /* avoid hanging on to stale pointer */ 3223. else 3224. ga.abort_looting = TRUE; 3225. return used; 3226. } 3227. 3228. /* loot current_container (take things out or put things in), by prompting */ 3229. staticfn int 3230. traditional_loot(boolean put_in) 3231. { 3232. int (*actionfunc)(OBJ_P), (*checkfunc)(OBJ_P); 3233. struct obj **objlist; 3234. char selection[MAXOCLASSES + 10]; /* +10: room for B,U,C,X plus slop */ 3235. const char *action; 3236. boolean one_by_one, allflag; 3237. int used = ECMD_OK, menu_on_request = 0; 3238. 3239. if (put_in) { 3240. action = "put in"; 3241. objlist = &gi.invent; 3242. actionfunc = in_container; 3243. checkfunc = ck_bag; 3244. } else { 3245. action = "take out"; 3246. objlist = &(gc.current_container->cobj); 3247. actionfunc = out_container; 3248. checkfunc = (int (*)(OBJ_P)) 0; 3249. gp.pickup_encumbrance = 0; /* used to limit verbosity */ 3250. } 3251. 3252. if (query_classes(selection, &one_by_one, &allflag, action, *objlist, 3253. FALSE, &menu_on_request)) { 3254. if (askchain(objlist, (one_by_one ? (char *) 0 : selection), allflag, 3255. actionfunc, checkfunc, 0, action)) 3256. used = ECMD_TIME; 3257. } else if (menu_on_request < 0) { 3258. used = (menu_loot(menu_on_request, put_in) > 0); 3259. } 3260. return used; 3261. } 3262. 3263. /* loot current_container (take things out or put things in), using a menu */ 3264. staticfn int 3265. menu_loot(int retry, boolean put_in) 3266. { 3267. int n, i, n_looted = 0; 3268. boolean all_categories = TRUE, loot_everything = FALSE, autopick = FALSE; 3269. char buf[BUFSZ]; 3270. boolean loot_justpicked = FALSE; 3271. const char *action = put_in ? "Put in" : "Take out"; 3272. struct obj *otmp, *otmp2; 3273. menu_item *pick_list; 3274. int mflags, res; 3275. long count = 0; 3276. 3277. gp.pickup_encumbrance = 0; /* used by out_container(); no harm in 3278. * zeroing it if about to use in_container() */ 3279. if (retry) { 3280. all_categories = (retry == -2); 3281. } else if (flags.menu_style == MENU_FULL) { 3282. all_categories = FALSE; 3283. Sprintf(buf, "%s what type of objects?", action); 3284. mflags = (ALL_TYPES | UNPAID_TYPES | BUCX_TYPES | CHOOSE_ALL 3285. | JUSTPICKED ); 3286. n = query_category(buf, 3287. put_in ? gi.invent : gc.current_container->cobj, 3288. mflags, &pick_list, PICK_ANY); 3289. /* when paranoid_confirm:A is set, 'A' by itself implies 3290. 'A'+'a' which will be followed by a confirmation prompt; 3291. when that option isn't set, 'A' by itself is rejected 3292. by query_categorry() and result here will be n==0 */ 3293. if (!n) 3294. return ECMD_OK; /* no non-autopick category filters specified */ 3295. 3296. for (i = 0; i < n; i++) { 3297. if (pick_list[i].item.a_int == 'A') { 3298. loot_everything = autopick = TRUE; 3299. } else if (put_in && pick_list[i].item.a_int == 'P') { 3300. loot_justpicked = TRUE; 3301. count = max(0, pick_list[i].count); 3302. add_valid_menu_class(pick_list[i].item.a_int); 3303. loot_everything = FALSE; 3304. } else if (pick_list[i].item.a_int == ALL_TYPES_SELECTED) { 3305. all_categories = TRUE; 3306. } else { 3307. add_valid_menu_class(pick_list[i].item.a_int); 3308. loot_everything = FALSE; 3309. } 3310. } 3311. free((genericptr_t) pick_list); 3312. } 3313. 3314. if (autopick) { 3315. int (*inout_func)(struct obj *); /* in_container or out_container */ 3316. struct obj *firstobj; 3317. 3318. if (!put_in) { 3319. gc.current_container->cknown = 1; 3320. inout_func = out_container; 3321. firstobj = gc.current_container->cobj; 3322. } else { 3323. inout_func = in_container; 3324. firstobj = gi.invent; 3325. } 3326. /* 3327. * Note: for put_in, current_container might be destroyed during 3328. * mid-traversal by a magic bag explosion. 3329. * Note too: items are processed in internal list order rather 3330. * than menu display order ('sortpack') or 'sortloot' order; 3331. * for put_in that should be item->invlet order so reasonable. 3332. */ 3333. for (otmp = firstobj; otmp && gc.current_container; otmp = otmp2) { 3334. otmp2 = otmp->nobj; 3335. if (loot_everything || all_categories || allow_category(otmp)) { 3336. res = (*inout_func)(otmp); 3337. if (res < 0) 3338. break; 3339. n_looted += res; 3340. } 3341. } 3342. } else if (put_in && loot_justpicked 3343. && count_justpicked(gi.invent) == 1) { 3344. otmp = find_justpicked(gi.invent); 3345. if (otmp) { 3346. n_looted = 1; 3347. if (count > 0 && count < otmp->quan) { 3348. otmp = splitobj(otmp, count); 3349. } 3350. (void) in_container(otmp); 3351. /* return value doesn't matter, even if container blew up */ 3352. } 3353. } else { 3354. mflags = INVORDER_SORT | INCLUDE_VENOM; 3355. if (put_in && flags.invlet_constant) 3356. mflags |= USE_INVLET; 3357. if (put_in && loot_justpicked) 3358. mflags |= JUSTPICKED; 3359. if (!put_in) 3360. gc.current_container->cknown = 1; 3361. Sprintf(buf, "%s what?", action); 3362. n = query_objlist(buf, 3363. put_in ? &gi.invent : &(gc.current_container->cobj), 3364. mflags, &pick_list, PICK_ANY, 3365. all_categories ? allow_all : allow_category); 3366. if (n) { 3367. n_looted = n; 3368. for (i = 0; i < n; i++) { 3369. otmp = pick_list[i].item.a_obj; 3370. assert(otmp != 0); 3371. count = pick_list[i].count; 3372. if (count > 0 && count < otmp->quan) { 3373. otmp = splitobj(otmp, count); 3374. /* special split case also handled by askchain() */ 3375. } 3376. res = put_in ? in_container(otmp) : out_container(otmp); 3377. if (res <= 0) { 3378. if (!gc.current_container) { 3379. /* otmp caused current_container to explode; 3380. both are now gone */ 3381. otmp = 0; /* and break loop */ 3382. } else if (otmp && otmp != pick_list[i].item.a_obj) { 3383. /* split occurred, merge again */ 3384. (void) unsplitobj(otmp); 3385. } 3386. if (res < 0) 3387. break; 3388. } 3389. } 3390. free((genericptr_t) pick_list); 3391. } 3392. } 3393. return n_looted ? ECMD_TIME : ECMD_OK; 3394. } 3395. 3396. staticfn char 3397. in_or_out_menu( 3398. const char *prompt, 3399. struct obj *obj, 3400. boolean outokay, /* can take out */ 3401. boolean inokay, /* can put in */ 3402. boolean alreadyused, /* controls phrasing of the decline choice */ 3403. boolean more_containers) 3404. { 3405. /* underscore is not a choice; it's used to skip element [0] */ 3406. static const char lootchars[] = "_:oibrsnq", abc_chars[] = "_:abcdenq"; 3407. winid win; 3408. anything any; 3409. menu_item *pick_list; 3410. char buf[BUFSZ]; 3411. int n; 3412. const char *menuselector = flags.lootabc ? abc_chars : lootchars; 3413. int clr = NO_COLOR; 3414. 3415. any = cg.zeroany; 3416. win = create_nhwindow(NHW_MENU); 3417. start_menu(win, MENU_BEHAVE_STANDARD); 3418. 3419. any.a_int = 1; /* ':' */ 3420. Sprintf(buf, "Look inside %s", thesimpleoname(obj)); 3421. add_menu(win, &nul_glyphinfo, &any, menuselector[any.a_int], 0, 3422. ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE); 3423. if (outokay) { 3424. any.a_int = 2; /* 'o' */ 3425. Sprintf(buf, "take %s out", something); 3426. add_menu(win, &nul_glyphinfo, &any, menuselector[any.a_int], 0, 3427. ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE); 3428. } 3429. if (inokay) { 3430. any.a_int = 3; /* 'i' */ 3431. Sprintf(buf, "put %s in", something); 3432. add_menu(win, &nul_glyphinfo, &any, menuselector[any.a_int], 0, 3433. ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE); 3434. } 3435. if (outokay) { 3436. any.a_int = 4; /* 'b' */ 3437. Sprintf(buf, "%stake out, then put in", inokay ? "both; " : ""); 3438. add_menu(win, &nul_glyphinfo, &any, menuselector[any.a_int], 0, 3439. ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE); 3440. } 3441. if (inokay) { 3442. any.a_int = 5; /* 'r' */ 3443. Sprintf(buf, "%sput in, then take out", 3444. outokay ? "both reversed; " : ""); 3445. add_menu(win, &nul_glyphinfo, &any, menuselector[any.a_int], 0, 3446. ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE); 3447. any.a_int = 6; /* 's' */ 3448. Sprintf(buf, "stash one item into %s", thesimpleoname(obj)); 3449. add_menu(win, &nul_glyphinfo, &any, menuselector[any.a_int], 0, 3450. ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE); 3451. } 3452. add_menu_str(win, ""); 3453. if (more_containers) { 3454. any.a_int = 7; /* 'n' */ 3455. add_menu(win, &nul_glyphinfo, &any, menuselector[any.a_int], 0, 3456. ATR_NONE, clr, "loot next container", 3457. MENU_ITEMFLAGS_SELECTED); 3458. } 3459. any.a_int = 8; /* 'q' */ 3460. Strcpy(buf, alreadyused ? "done" : "do nothing"); 3461. add_menu(win, &nul_glyphinfo, &any, menuselector[any.a_int], 0, 3462. ATR_NONE, clr, buf, 3463. more_containers ? MENU_ITEMFLAGS_NONE : MENU_ITEMFLAGS_SELECTED); 3464. 3465. end_menu(win, prompt); 3466. n = select_menu(win, PICK_ONE, &pick_list); 3467. destroy_nhwindow(win); 3468. if (n > 0) { 3469. int k = pick_list[0].item.a_int; 3470. 3471. if (n > 1 && k == (more_containers ? 7 : 8)) 3472. k = pick_list[1].item.a_int; 3473. free((genericptr_t) pick_list); 3474. return lootchars[k]; /* :,o,i,b,r,s,n,q */ 3475. } 3476. return (n == 0 && more_containers) ? 'n' : 'q'; /* next or quit */ 3477. } 3478. 3479. /* getobj callback for object to tip */ 3480. staticfn int 3481. tip_ok(struct obj *obj) 3482. { 3483. if (!obj || obj->oclass == COIN_CLASS) 3484. return GETOBJ_EXCLUDE; 3485. 3486. if (Is_container(obj)) { 3487. return GETOBJ_SUGGEST; 3488. } 3489. 3490. /* include horn of plenty if sufficiently discovered */ 3491. if (obj->otyp == HORN_OF_PLENTY && obj->dknown 3492. && objects[obj->otyp].oc_name_known) 3493. return GETOBJ_SUGGEST; 3494. 3495. /* allow trying anything else in inventory */ 3496. return GETOBJ_DOWNPLAY; 3497. } 3498. 3499. /* show a menu of containers under hero, 3500. and one extra entry for choosing an inventory. 3501. returns ECMD_CANCEL if menu was canceled, 3502. ECMD_TIME if a container was picked, 3503. otherwise returns ECMD_OK. */ 3504. staticfn int 3505. choose_tip_container_menu(void) 3506. { 3507. int n, i; 3508. winid win; 3509. anything any; 3510. menu_item *pick_list = (menu_item *) 0; 3511. struct obj dummyobj, *otmp; 3512. int clr = NO_COLOR; 3513. 3514. any = cg.zeroany; 3515. win = create_nhwindow(NHW_MENU); 3516. start_menu(win, MENU_BEHAVE_STANDARD); 3517. 3518. for (otmp = svl.level.objects[u.ux][u.uy], i = 0; otmp; 3519. otmp = otmp->nexthere) 3520. if (Is_container(otmp)) { 3521. ++i; 3522. any.a_obj = otmp; 3523. add_menu(win, &nul_glyphinfo, &any, 0, 0, ATR_NONE, 3524. clr, doname(otmp), MENU_ITEMFLAGS_NONE); 3525. } 3526. if (gi.invent) { 3527. add_menu_str(win, ""); 3528. any.a_obj = &dummyobj; 3529. /* use 'i' for inventory unless there are so many 3530. containers that it's already being used */ 3531. i = (i <= 'i' - 'a' && !flags.lootabc) ? 'i' : 0; 3532. add_menu(win, &nul_glyphinfo, &any, i, 0, ATR_NONE, 3533. clr, "tip something being carried", 3534. MENU_ITEMFLAGS_SELECTED); 3535. } 3536. end_menu(win, "Tip which container?"); 3537. n = select_menu(win, PICK_ONE, &pick_list); 3538. destroy_nhwindow(win); 3539. /* 3540. * Deal with quirk of preselected item in pick-one menu: 3541. * n == 0 => picked preselected entry, toggling it off; 3542. * n == 1 => accepted preselected choice via SPACE or RETURN; 3543. * n == 2 => picked something other than preselected entry; 3544. * n == -1 => cancelled via ESC; 3545. */ 3546. otmp = (n <= 0) ? (struct obj *) 0 : pick_list[0].item.a_obj; 3547. if (n > 1 && otmp == &dummyobj) 3548. otmp = pick_list[1].item.a_obj; 3549. if (pick_list) 3550. free((genericptr_t) pick_list); 3551. if (otmp && otmp != &dummyobj) { 3552. tipcontainer(otmp); 3553. return ECMD_TIME; 3554. } 3555. if (n == -1) 3556. return ECMD_CANCEL; 3557. return ECMD_OK; 3558. } 3559. 3560. /* #tip command -- empty container contents onto floor */ 3561. int 3562. dotip(void) 3563. { 3564. struct obj *cobj, *nobj; 3565. coord cc; 3566. int boxes; 3567. char c, buf[BUFSZ], qbuf[BUFSZ]; 3568. const char *spillage = 0; 3569. 3570. /* 3571. * Doesn't require free hands; 3572. * limbs are needed to tip floor containers. 3573. * 3574. * Note: for menustyle:Traditional, using m prefix forces a menu 3575. * of floor containers when more than one is present. For other 3576. * menustyle settings or when fewer than two floor containers are 3577. * present, using 'm' skips floor and goes straight to invent. 3578. * This somewhat unintuitive behavior is driven by the way that 3579. * context-sensitive inventory item actions use m prefix. 3580. */ 3581. 3582. /* at present, can only tip things at current spot, not adjacent ones */ 3583. cc.x = u.ux, cc.y = u.uy; 3584. 3585. /* check floor container(s) first; at most one will be accessed */ 3586. boxes = container_at(cc.x, cc.y, TRUE); 3587. /* this is iffy for menustyle:traditional; 'm' prefix is ambiguous 3588. for it: skip floor vs handle multiple containers via menu */ 3589. if (boxes > 0 3590. && (!iflags.menu_requested 3591. || (flags.menu_style == MENU_TRADITIONAL && boxes > 1))) { 3592. Sprintf(buf, "You can't tip %s while carrying so much.", 3593. !flags.verbose ? "a container" : (boxes > 1) ? "one" : "it"); 3594. if (!check_capacity(buf) && able_to_loot(cc.x, cc.y, FALSE)) { 3595. if (boxes > 1) { 3596. int res; 3597. /* pick one container via menu or ... */ 3598. if ((res = choose_tip_container_menu()) != ECMD_OK) 3599. return res; 3600. /* else pick-from-gi.invent below */ 3601. } else { 3602. for (cobj = svl.level.objects[cc.x][cc.y]; cobj; 3603. cobj = nobj) { 3604. nobj = cobj->nexthere; 3605. if (!Is_container(cobj)) 3606. continue; 3607. c = ynq(safe_qbuf(qbuf, "There is ", " here, tip it?", 3608. cobj, 3609. doname, ansimpleoname, "container")); 3610. if (c == 'q') 3611. return ECMD_OK; 3612. if (c == 'n') 3613. continue; 3614. tipcontainer(cobj); 3615. /* can only tip one container at a time */ 3616. return ECMD_TIME; 3617. } 3618. } 3619. } 3620. } 3621. 3622. /* either no floor container(s) or 'm' prefix was used to ignore such 3623. or couldn't tip one or didn't tip any */ 3624. cobj = getobj("tip", tip_ok, GETOBJ_PROMPT); 3625. if (!cobj) 3626. return ECMD_CANCEL; 3627. 3628. /* normal case */ 3629. if (Is_container(cobj) || cobj->otyp == HORN_OF_PLENTY) { 3630. tipcontainer(cobj); 3631. return ECMD_TIME; 3632. } 3633. /* assorted other cases */ 3634. if (Is_candle(cobj) && cobj->lamplit) { 3635. /* note "wax" even for tallow candles to avoid giving away info */ 3636. spillage = "wax"; 3637. } else if ((cobj->otyp == POT_OIL && cobj->lamplit) 3638. || (cobj->otyp == OIL_LAMP && cobj->age != 0L) 3639. || (cobj->otyp == MAGIC_LAMP && cobj->spe != 0)) { 3640. spillage = "oil"; 3641. /* todo: reduce potion's remaining burn timer or oil lamp's fuel */ 3642. } else if (cobj->otyp == CAN_OF_GREASE && cobj->spe > 0) { 3643. /* charged consumed below */ 3644. spillage = "grease"; 3645. } else if (cobj->otyp == FOOD_RATION || cobj->otyp == CRAM_RATION 3646. || cobj->otyp == LEMBAS_WAFER) { 3647. spillage = "crumbs"; 3648. } else if (cobj->oclass == VENOM_CLASS) { 3649. spillage = "venom"; 3650. } 3651. if (spillage) { 3652. buf[0] = '\0'; 3653. if (is_pool(u.ux, u.uy)) 3654. Sprintf(buf, " and gradually %s", vtense(spillage, "dissipate")); 3655. else if (is_lava(u.ux, u.uy)) 3656. Sprintf(buf, " and immediately %s away", 3657. vtense(spillage, "burn")); 3658. pline("Some %s %s onto the %s%s.", spillage, 3659. vtense(spillage, "spill"), surface(u.ux, u.uy), buf); 3660. /* shop usage message comes after the spill message */ 3661. if (cobj->otyp == CAN_OF_GREASE && cobj->spe > 0) { 3662. consume_obj_charge(cobj, TRUE); 3663. } 3664. /* something [useless] happened */ 3665. return ECMD_TIME; 3666. } 3667. /* anything not covered yet */ 3668. if (cobj->oclass == POTION_CLASS) /* can't pour potions... */ 3669. pline_The("%s %s securely sealed.", xname(cobj), otense(cobj, "are")); 3670. else if (uarmh && cobj == uarmh) 3671. return tiphat() ? ECMD_TIME : ECMD_OK; 3672. else if (cobj->otyp == STATUE) 3673. pline("Nothing interesting happens."); 3674. else 3675. pline1(nothing_happens); 3676. return ECMD_OK; 3677. } 3678. 3679. enum tipping_check_values { 3680. TIPCHECK_OK = 0, 3681. TIPCHECK_LOCKED, 3682. TIPCHECK_TRAPPED, 3683. TIPCHECK_CANNOT, 3684. TIPCHECK_EMPTY 3685. }; 3686. 3687. staticfn void 3688. tipcontainer(struct obj *box) /* or bag */ 3689. { 3690. coordxy ox = u.ux, oy = u.uy; /* #tip only works at hero's location */ 3691. boolean srcheld = FALSE, dstheld = FALSE, maybeshopgoods; 3692. struct obj *targetbox = (struct obj *) 0; 3693. boolean cancelled = FALSE; 3694. 3695. /* box is either held or on floor at hero's spot; no need to check for 3696. nesting; when held, we need to update its location to match hero's; 3697. for floor, the coordinate updating is redundant */ 3698. if (get_obj_location(box, &ox, &oy, 0)) 3699. box->ox = ox, box->oy = oy; 3700. 3701. /* 3702. * TODO? 3703. * if 'box' is known to be empty or known to be locked, give up 3704. * before choosing 'targetbox'. 3705. */ 3706. targetbox = tipcontainer_gettarget(box, &cancelled); 3707. if (cancelled) 3708. return; 3709. 3710. /* Shop handling: can't rely on the container's own unpaid 3711. or no_charge status because contents might differ with it. 3712. A carried container's contents will be flagged as unpaid 3713. or not, as appropriate, and need no special handling here. 3714. Items owned by the hero get sold to the shop without 3715. confirmation as with other uncontrolled drops. A floor 3716. container's contents will be marked no_charge if owned by 3717. hero, otherwise they're owned by the shop. By passing 3718. the contents through shop billing, they end up getting 3719. treated the same as in the carried case. We do so one 3720. item at a time instead of doing whole container at once 3721. to reduce the chance of exhausting shk's billing capacity. */ 3722. maybeshopgoods = !carried(box) && costly_spot(box->ox, box->oy); 3723. 3724. if (tipcontainer_checks(box, targetbox, FALSE) != TIPCHECK_OK) 3725. return; 3726. if (targetbox 3727. && tipcontainer_checks(targetbox, NULL, TRUE) != TIPCHECK_OK) 3728. return; 3729. 3730. { 3731. struct obj *otmp, *nobj; 3732. boolean terse, highdrop = !can_reach_floor(TRUE), 3733. altarizing = IS_ALTAR(levl[ox][oy].typ), 3734. cursed_mbag = (Is_mbag(box) && box->cursed); 3735. long loss = 0L; 3736. 3737. srcheld = carried(box); 3738. dstheld = (targetbox && carried(targetbox)); 3739. if (u.uswallow) 3740. highdrop = altarizing = FALSE; 3741. terse = !(highdrop || altarizing || costly_spot(box->ox, box->oy)); 3742. box->cknown = 1; 3743. /* Terse formatting is 3744. * "Objects spill out: obj1, obj2, obj3, ..., objN." 3745. * If any other messages intervene between objects, we revert to 3746. * "ObjK drops to the floor.", "ObjL drops to the floor.", &c. 3747. */ 3748. if (targetbox) 3749. pline("%s into %s.", 3750. box->cobj->nobj ? "Objects tumble" : "An object tumbles", 3751. the(xname(targetbox))); 3752. else 3753. pline("%s out%c", 3754. box->cobj->nobj ? "Objects spill" : "An object spills", 3755. terse ? ':' : '.'); 3756. 3757. for (otmp = box->cobj; otmp; otmp = nobj) { 3758. nobj = otmp->nobj; 3759. obj_extract_self(otmp); 3760. otmp->ox = box->ox, otmp->oy = box->oy; 3761. 3762. if (box->otyp == ICE_BOX) { 3763. removed_from_icebox(otmp); /* resume rotting for corpse */ 3764. } else if (cursed_mbag && is_boh_item_gone()) { 3765. loss += mbag_item_gone(srcheld, otmp, FALSE); 3766. /* abbreviated drop format is no longer appropriate */ 3767. terse = FALSE; 3768. continue; 3769. } 3770. if (maybeshopgoods) { 3771. addtobill(otmp, FALSE, FALSE, TRUE); 3772. iflags.suppress_price++; /* doname formatting */ 3773. } 3774. 3775. if (targetbox) { 3776. if (Is_mbag(targetbox) && mbag_explodes(otmp, 0)) { 3777. livelog_printf(LL_ACHIEVE, 3778. "just blew up %s bag of holding via tipping", 3779. uhis()); 3780. /* explicitly mention what item is triggering explosion */ 3781. urgent_pline( 3782. "As %s %s inside, you are blasted by a magical explosion!", 3783. doname(otmp), otense(otmp, "tumble")); 3784. 3785. /* if putting one bag of holding into another, first 3786. blow up the one going in, then (below) blow up the 3787. one it's going into */ 3788. if (otmp->otyp == BAG_OF_HOLDING) /* BoH into another */ 3789. do_boh_explosion(otmp, !srcheld); 3790. /* always delete the item which triggered the explosion */ 3791. obfree(otmp, (struct obj *) 0); /* where==OBJ_FREE */ 3792. 3793. /* [assumes targetbox is carried, otherwise shop bill 3794. handling becomes necessary here] */ 3795. do_boh_explosion(targetbox, !dstheld); 3796. if (dstheld) 3797. useup(targetbox); 3798. else 3799. useupf(targetbox, targetbox->quan); 3800. targetbox = 0; /* it's gone */ 3801. nobj = 0; /* stop tipping; want loop to exit 'normally' */ 3802. 3803. losehp(d(6, 6), "magical explosion", KILLED_BY_AN); 3804. } else { 3805. (void) add_to_container(targetbox, otmp); 3806. } 3807. } else if (highdrop) { 3808. otmp->how_lost = LOST_DROPPED; 3809. /* might break or fall down stairs; handles altars itself */ 3810. hitfloor(otmp, TRUE); 3811. } else { 3812. if (altarizing) { 3813. doaltarobj(otmp); 3814. } else if (!terse) { 3815. pline("%s %s to the %s.", Doname2(otmp), 3816. otense(otmp, "drop"), surface(ox, oy)); 3817. } else { 3818. pline("%s%c", doname(otmp), nobj ? ',' : '.'); 3819. iflags.last_msg = PLNMSG_OBJNAM_ONLY; 3820. } 3821. otmp->how_lost = LOST_DROPPED; 3822. dropy(otmp); 3823. if (iflags.last_msg != PLNMSG_OBJNAM_ONLY) 3824. terse = FALSE; /* terse formatting has been interrupted */ 3825. } 3826. 3827. if (maybeshopgoods) 3828. iflags.suppress_price--; /* reset */ 3829. } 3830. if (loss) /* magic bag lost some shop goods */ 3831. You("owe %ld %s for lost merchandise.", loss, currency(loss)); 3832. box->owt = weight(box); /* mbag_item_gone() doesn't update this */ 3833. if (targetbox) 3834. targetbox->owt = weight(targetbox); 3835. if (srcheld || dstheld) 3836. encumber_msg(); 3837. } 3838. 3839. if (srcheld || dstheld) 3840. update_inventory(); 3841. } 3842. 3843. #if 0 3844. staticfn int count_target_containers(struct obj *, struct obj *); 3845. 3846. /* returns number of containers in object chain; does not recurse into 3847. containers; skips bags of tricks when they're known */ 3848. staticfn int 3849. count_target_containers( 3850. struct obj *olist, /* list of objects (invent) */ 3851. struct obj *excludo) /* particular object to exclude if found in list */ 3852. { 3853. int ret = 0; 3854. 3855. while (olist) { 3856. if (olist != excludo && Is_container(olist) 3857. /* include bag of tricks when not known to be such */ 3858. && (box->otyp != BAG_OF_TRICKS || !box->dknown 3859. || !objects[box->otyp].oc_name_known)) 3860. ret++; 3861. olist = olist->nobj; 3862. } 3863. return ret; 3864. } 3865. #endif 3866. 3867. /* ask user for a carried container into which they want box to be emptied; 3868. cancelled is TRUE if user cancelled the menu pick; hands aren't required 3869. when tipping to the floor but are when tipping into another container */ 3870. staticfn struct obj * 3871. tipcontainer_gettarget( 3872. struct obj *box, 3873. boolean *cancelled) 3874. { 3875. int n, n_conts; 3876. winid win; 3877. anything any; 3878. char buf[BUFSZ]; 3879. menu_item *pick_list = (menu_item *) 0; 3880. struct obj dummyobj, *otmp; 3881. boolean hands_available = TRUE, exclude_it; 3882. int clr = NO_COLOR; 3883. 3884. #if 0 /* [skip potential early return so that menu response is needed 3885. * regardless of whether other containers are being carried] */ 3886. int n_conts = count_target_containers(gi.invent, box); 3887. 3888. if (n_conts < 1 || !u_handsy()) { 3889. if (n_conts >= 1) 3890. pline("Tipping contents to floor only..."); 3891. *cancelled = FALSE; 3892. return (struct obj *) 0; 3893. } 3894. #endif 3895. 3896. win = create_nhwindow(NHW_MENU); 3897. start_menu(win, MENU_BEHAVE_STANDARD); 3898. 3899. dummyobj = cg.zeroobj; /* lint suppression; only its address matters */ 3900. any = cg.zeroany; 3901. any.a_obj = &dummyobj; 3902. /* tip to floor does not require free hands */ 3903. add_menu(win, &nul_glyphinfo, &any, '-', 0, ATR_NONE, clr, 3904. /* [TODO? vary destination string depending on surface()] */ 3905. "on the floor", MENU_ITEMFLAGS_SELECTED); 3906. add_menu_str(win, ""); 3907. 3908. n_conts = 0; 3909. for (otmp = gi.invent; otmp; otmp = otmp->nobj) { 3910. if (otmp == box) 3911. continue; 3912. /* skip non-containers; bag of tricks passes Is_container() test, 3913. only include it if it isn't known to be a bag of tricks */ 3914. if (!Is_container(otmp) 3915. || (otmp->otyp == BAG_OF_TRICKS && otmp->dknown 3916. && objects[otmp->otyp].oc_name_known)) 3917. continue; 3918. if (!n_conts++) 3919. hands_available = u_handsy(); /* might issue message */ 3920. /* container-to-container tip requires free hands; 3921. exclude container as possible target when known to be locked */ 3922. exclude_it = !hands_available || (otmp->olocked && otmp->lknown); 3923. any = cg.zeroany; 3924. any.a_obj = !exclude_it ? otmp : 0; 3925. Sprintf(buf, "%s%s", !exclude_it ? "" : " ", doname(otmp)); 3926. add_menu(win, &nul_glyphinfo, &any, !exclude_it ? otmp->invlet : 0, 0, 3927. ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE); 3928. } 3929. 3930. Sprintf(buf, "Where to tip the contents of %s", doname(box)); 3931. end_menu(win, buf); 3932. n = select_menu(win, PICK_ONE, &pick_list); 3933. destroy_nhwindow(win); 3934. 3935. otmp = 0; 3936. if (pick_list) { 3937. otmp = pick_list[0].item.a_obj; 3938. /* PICK_ONE with a preselected item might return 2; 3939. if so, choose the one that wasn't preselected */ 3940. if (n > 1 && otmp == &dummyobj) 3941. otmp = pick_list[1].item.a_obj; 3942. if (otmp == &dummyobj) 3943. otmp = 0; 3944. free((genericptr_t) pick_list); 3945. } 3946. *cancelled = (boolean) (n == -1); 3947. return otmp; 3948. } 3949. 3950. /* Perform check on box if we can tip it. 3951. Returns one of TIPCHECK_foo values. 3952. If allowempty if TRUE, return TIPCHECK_OK instead of TIPCHECK_EMPTY. */ 3953. staticfn int 3954. tipcontainer_checks( 3955. struct obj *box, /* container player wants to tip */ 3956. struct obj *targetbox, /* destination (used here for horn of plenty) */ 3957. boolean allowempty) /* affects result when box is empty */ 3958. { 3959. /* undiscovered bag of tricks is acceptable as a container-to-container 3960. destination but it can't receive items; it has to be opened in 3961. preparation so apply it once before even trying to tip source box */ 3962. if (targetbox && targetbox->otyp == BAG_OF_TRICKS) { 3963. int seencount = 0; 3964. 3965. bagotricks(targetbox, FALSE, &seencount); 3966. return TIPCHECK_CANNOT; 3967. } 3968. 3969. /* caveat: this assumes that cknown, lknown, olocked, and otrapped 3970. fields haven't been overloaded to mean something special for the 3971. non-standard "container" horn of plenty */ 3972. if (!box->lknown) { 3973. box->lknown = 1; 3974. if (carried(box)) 3975. update_inventory(); /* jumping the gun slightly; hope that's ok */ 3976. } 3977. 3978. if (box->olocked) { 3979. pline("%s is locked.", upstart(thesimpleoname(box))); 3980. return TIPCHECK_LOCKED; 3981. 3982. } else if (box->otrapped) { 3983. /* we're not reaching inside but we're still handling it... */ 3984. (void) chest_trap(box, HAND, FALSE); 3985. /* even if the trap fails, you've used up this turn */ 3986. if (gm.multi >= 0) { /* in case we didn't become paralyzed */ 3987. nomul(-1); 3988. gm.multi_reason = "tipping a container"; 3989. gn.nomovemsg = ""; 3990. } 3991. return TIPCHECK_TRAPPED; 3992. 3993. } else if (box->otyp == BAG_OF_TRICKS || box->otyp == HORN_OF_PLENTY) { 3994. int res = TIPCHECK_OK; 3995. boolean bag = (box->otyp == BAG_OF_TRICKS); 3996. int old_spe = box->spe, seen, totseen; 3997. boolean maybeshopgoods = (!carried(box) 3998. && costly_spot(box->ox, box->oy)); 3999. coordxy ox = u.ux, oy = u.uy; 4000. 4001. if (targetbox 4002. && ((res = tipcontainer_checks(targetbox, NULL, TRUE)) 4003. != TIPCHECK_OK)) 4004. return res; 4005. 4006. if (get_obj_location(box, &ox, &oy, 0)) 4007. box->ox = ox, box->oy = oy; 4008. 4009. if (maybeshopgoods && !box->no_charge) 4010. addtobill(box, FALSE, FALSE, TRUE); 4011. /* apply this bag/horn until empty or monster/object creation fails 4012. (if the latter occurs, force the former...) */ 4013. seen = totseen = 0; 4014. do { 4015. if (!(bag ? bagotricks(box, TRUE, &seen) 4016. : hornoplenty(box, TRUE, targetbox))) 4017. break; 4018. totseen += seen; 4019. } while (box->spe > 0); 4020. 4021. if (box->spe < old_spe) { 4022. if (bag && !totseen) 4023. pline1(nothing_seems_to_happen); 4024. /* check_unpaid wants to see a non-zero charge count */ 4025. box->spe = old_spe; 4026. check_unpaid_usage(box, TRUE); 4027. box->spe = 0; /* empty */ 4028. box->cknown = 1; 4029. } 4030. if (maybeshopgoods && !box->no_charge) 4031. subfrombill(box, shop_keeper(*in_rooms(ox, oy, SHOPBASE))); 4032. return TIPCHECK_CANNOT; /* actually means 'already done' */ 4033. 4034. } else if (SchroedingersBox(box)) { 4035. char yourbuf[BUFSZ]; 4036. boolean empty_it = FALSE; 4037. 4038. observe_quantum_cat(box, TRUE, TRUE); 4039. if (!Has_contents(box)) /* evidently a live cat came out */ 4040. /* container type of "large box" is inferred */ 4041. pline("%sbox is now empty.", Shk_Your(yourbuf, box)); 4042. else /* holds cat corpse */ 4043. empty_it = TRUE; 4044. box->cknown = 1; 4045. return (empty_it || allowempty) ? TIPCHECK_OK : TIPCHECK_EMPTY; 4046. 4047. } else if (!allowempty && !Has_contents(box)) { 4048. box->cknown = 1; 4049. pline("%s is empty.", upstart(thesimpleoname(box))); 4050. return TIPCHECK_EMPTY; 4051. 4052. } 4053. 4054. return TIPCHECK_OK; 4055. } 4056. 4057. /*pickup.c*/