PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/obj-util.c

https://github.com/NickMcConnell/Beleriand
C | 5086 lines | 4994 code | 30 blank | 62 comment | 33 complexity | 92006bf21b23c76cd62e33dbe5efdab9 MD5 | raw file
  1. /*
  2. * File: obj-util.c
  3. * Purpose: Object list maintenance and other object utilities
  4. *
  5. * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
  6. *
  7. * This work is free software; you can redistribute it and/or modify it
  8. * under the terms of either:
  9. *
  10. * a) the GNU General Public License as published by the Free Software
  11. * Foundation, version 2, or
  12. *
  13. * b) the "Angband licence":
  14. * This software may be copied and distributed for educational, research,
  15. * and not for profit purposes provided that this copyright and statement
  16. * are included in all such copies. Other copyrights may also apply.
  17. */
  18. #include "angband.h"
  19. #include "cave.h"
  20. #include "defines.h"
  21. #include "effects.h"
  22. #include "game-cmd.h"
  23. #include "generate.h"
  24. #include "history.h"
  25. #include "prefs.h"
  26. #include "randname.h"
  27. #include "spells.h"
  28. #include "squelch.h"
  29. #include "tvalsval.h"
  30. /*
  31. * Hold the titles of scrolls, 6 to 14 characters each, plus quotes.
  32. */
  33. char scroll_adj[MAX_TITLES][18];
  34. static void flavor_assign_fixed(void)
  35. {
  36. int i, j;
  37. for (i = 0; i < z_info->flavor_max; i++) {
  38. flavor_type *flavor_ptr = &flavor_info[i];
  39. /* Skip random flavors */
  40. if (flavor_ptr->sval == SV_UNKNOWN)
  41. continue;
  42. for (j = 0; j < z_info->k_max; j++) {
  43. /* Skip other objects */
  44. if ((k_info[j].tval == flavor_ptr->tval)
  45. && (k_info[j].sval == flavor_ptr->sval)) {
  46. /* Store the flavor index */
  47. k_info[j].flavor = i;
  48. }
  49. }
  50. }
  51. }
  52. static void flavor_assign_random(byte tval)
  53. {
  54. int i, j;
  55. int flavor_count = 0;
  56. int choice;
  57. /* Count the random flavors for the given tval */
  58. for (i = 0; i < z_info->flavor_max; i++) {
  59. if ((flavor_info[i].tval == tval)
  60. && (flavor_info[i].sval == SV_UNKNOWN)) {
  61. flavor_count++;
  62. }
  63. }
  64. for (i = 0; i < z_info->k_max; i++) {
  65. /* Skip other object types */
  66. if (k_info[i].tval != tval)
  67. continue;
  68. /* Skip objects that already are flavored */
  69. if (k_info[i].flavor != 0)
  70. continue;
  71. /* HACK - Ordinary food is "boring" */
  72. if ((tval == TV_FOOD) && (k_info[i].sval >= SV_FOOD_MIN_FOOD))
  73. continue;
  74. if (!flavor_count)
  75. quit_fmt("Not enough flavors for tval %d.", tval);
  76. /* Select a flavor */
  77. choice = randint0(flavor_count);
  78. /* Find and store the flavor */
  79. for (j = 0; j < z_info->flavor_max; j++) {
  80. /* Skip other tvals */
  81. if (flavor_info[j].tval != tval)
  82. continue;
  83. /* Skip assigned svals */
  84. if (flavor_info[j].sval != SV_UNKNOWN)
  85. continue;
  86. if (choice == 0) {
  87. /* Store the flavor index */
  88. k_info[i].flavor = j;
  89. /* Mark the flavor as used */
  90. flavor_info[j].sval = k_info[i].sval;
  91. /* Hack - set the scroll name if it's a scroll */
  92. if (tval == TV_SCROLL) {
  93. flavor_info[j].text = scroll_adj[k_info[i].sval];
  94. }
  95. /* One less flavor to choose from */
  96. flavor_count--;
  97. break;
  98. }
  99. choice--;
  100. }
  101. }
  102. }
  103. /*
  104. * Prepare the "variable" part of the "k_info" array.
  105. *
  106. * The "color"/"metal"/"type" of an item is its "flavor".
  107. * For the most part, flavors are assigned randomly each game.
  108. *
  109. * Initialize descriptions for the "colored" objects, including:
  110. * Rings, Amulets, Staffs, Wands, Rods, Food, Potions, Scrolls.
  111. *
  112. * The first 4 entries for potions are fixed (Water, Apple Juice,
  113. * Slime Mold Juice, Unused Potion).
  114. *
  115. * Scroll titles are always between 6 and 14 letters long. This is
  116. * ensured because every title is composed of whole words, where every
  117. * word is from 2 to 8 letters long, and that no scroll is finished
  118. * until it attempts to grow beyond 15 letters. The first time this
  119. * can happen is when the current title has 6 letters and the new word
  120. * has 8 letters, which would result in a 6 letter scroll title.
  121. *
  122. * Hack -- make sure everything stays the same for each saved game
  123. * This is accomplished by the use of a saved "random seed", as in
  124. * "town_gen()". Since no other functions are called while the special
  125. * seed is in effect, so this function is pretty "safe".
  126. */
  127. void flavor_init(void)
  128. {
  129. int i, j;
  130. /* Hack -- Use the "simple" RNG */
  131. Rand_quick = TRUE;
  132. /* Hack -- Induce consistant flavors */
  133. Rand_value = seed_flavor;
  134. flavor_assign_fixed();
  135. flavor_assign_random(TV_STAFF);
  136. flavor_assign_random(TV_WAND);
  137. flavor_assign_random(TV_ROD);
  138. flavor_assign_random(TV_FOOD);
  139. flavor_assign_random(TV_POTION);
  140. /* Scrolls (random titles, always white) */
  141. for (i = 0; i < MAX_TITLES; i++) {
  142. char buf[26] = "\"";
  143. char *end = buf + 1;
  144. int titlelen = 0;
  145. int wordlen;
  146. bool okay = TRUE;
  147. wordlen =
  148. randname_make(RANDNAME_SCROLL, 2, 8, end, 24, name_sections);
  149. while (titlelen + wordlen < (int) (sizeof(scroll_adj[0]) - 3)) {
  150. end[wordlen] = ' ';
  151. titlelen += wordlen + 1;
  152. end += wordlen + 1;
  153. wordlen =
  154. randname_make(RANDNAME_SCROLL, 2, 8, end, 24 - titlelen,
  155. name_sections);
  156. }
  157. buf[titlelen] = '"';
  158. buf[titlelen + 1] = '\0';
  159. /* Check the scroll name hasn't already been generated */
  160. for (j = 0; j < i; j++) {
  161. if (streq(buf, scroll_adj[j])) {
  162. okay = FALSE;
  163. break;
  164. }
  165. }
  166. if (okay) {
  167. my_strcpy(scroll_adj[i], buf, sizeof(scroll_adj[0]));
  168. } else {
  169. /* Have another go at making a name */
  170. i--;
  171. }
  172. }
  173. flavor_assign_random(TV_SCROLL);
  174. /* Hack -- Use the "complex" RNG */
  175. Rand_quick = FALSE;
  176. /* Analyze every object */
  177. for (i = 1; i < z_info->k_max; i++) {
  178. object_kind *k_ptr = &k_info[i];
  179. /* Skip "empty" objects */
  180. if (!k_ptr->name)
  181. continue;
  182. /* No flavor yields aware */
  183. if (!k_ptr->flavor)
  184. k_ptr->aware = TRUE;
  185. }
  186. }
  187. /**
  188. * Get the inventory color for an object.
  189. *
  190. * Hack - set the listing color of a spellbook with no useable spells
  191. * to grey -LM-
  192. */
  193. byte proc_list_color_hack(object_type * o_ptr)
  194. {
  195. /* Hack -- Spellbooks with no useable spells are grey. */
  196. if ((mp_ptr->spell_book == o_ptr->tval)
  197. && (mp_ptr->book_start_index[o_ptr->sval] ==
  198. mp_ptr->book_start_index[o_ptr->sval + 1])) {
  199. return (TERM_SLATE);
  200. }
  201. /* Otherwise, get the color normally. */
  202. else
  203. return (tval_to_attr[o_ptr->tval]);
  204. }
  205. #ifdef ALLOW_BORG_GRAPHICS
  206. extern void init_translate_visuals(void);
  207. #endif /* ALLOW_BORG_GRAPHICS */
  208. /*
  209. * Reset the "visual" lists
  210. *
  211. * This involves resetting various things to their "default" state.
  212. *
  213. * If the "prefs" flag is TRUE, then we will also load the appropriate
  214. * "user pref file" based on the current setting of the "use_graphics"
  215. * flag. This is useful for switching "graphics" on/off.
  216. */
  217. /* XXX this does not belong here */
  218. void reset_visuals(bool load_prefs)
  219. {
  220. int i;
  221. /* Extract default attr/char code for features */
  222. for (i = 0; i < z_info->f_max; i++) {
  223. int j;
  224. feature_type *f_ptr = &f_info[i];
  225. /* Assume we will use the underlying values */
  226. for (j = 0; j < FEAT_LIGHTING_MAX; j++) {
  227. f_ptr->x_attr[j] = f_ptr->d_attr;
  228. f_ptr->x_char[j] = f_ptr->d_char;
  229. }
  230. }
  231. /* Extract default attr/char code for objects */
  232. for (i = 0; i < z_info->k_max; i++) {
  233. object_kind *k_ptr = &k_info[i];
  234. /* Default attr/char */
  235. k_ptr->x_attr = k_ptr->d_attr;
  236. k_ptr->x_char = k_ptr->d_char;
  237. }
  238. /* Extract default attr/char code for monsters */
  239. for (i = 0; i < z_info->r_max; i++) {
  240. monster_race *r_ptr = &r_info[i];
  241. /* Default attr/char */
  242. r_ptr->x_attr = r_ptr->d_attr;
  243. r_ptr->x_char = r_ptr->d_char;
  244. }
  245. /* Extract default attr/char code for flavors */
  246. for (i = 0; i < z_info->flavor_max; i++) {
  247. flavor_type *flavor_ptr = &flavor_info[i];
  248. /* Default attr/char */
  249. flavor_ptr->x_attr = flavor_ptr->d_attr;
  250. flavor_ptr->x_char = flavor_ptr->d_char;
  251. }
  252. /* Extract attr/chars for inventory objects (by tval) */
  253. for (i = 0; i < (int) N_ELEMENTS(tval_to_attr); i++) {
  254. /* Default to white */
  255. tval_to_attr[i] = TERM_WHITE;
  256. }
  257. if (!load_prefs)
  258. return;
  259. /* Graphic symbols */
  260. if (use_graphics) {
  261. /* Process "graf.prf" */
  262. process_pref_file("graf.prf", FALSE, FALSE);
  263. }
  264. /* Normal symbols */
  265. else {
  266. /* Process "font.prf" */
  267. process_pref_file("font.prf", FALSE, FALSE);
  268. }
  269. #ifdef ALLOW_BORG_GRAPHICS
  270. /* Initialize the translation table for the borg */
  271. init_translate_visuals();
  272. #endif /* ALLOW_BORG_GRAPHICS */
  273. }
  274. /*
  275. * Convert an inventory index into a one character label.
  276. *
  277. * Note that the label does NOT distinguish inven/equip.
  278. */
  279. char index_to_label(int i)
  280. {
  281. /* Indexes for "inven" are easy */
  282. if (i < INVEN_WIELD)
  283. return (I2A(i));
  284. /* Indexes for "equip" are offset */
  285. return (I2A(i - INVEN_WIELD));
  286. }
  287. /*
  288. * Convert a label into the index of an item in the "inven".
  289. *
  290. * Return "-1" if the label does not indicate a real item.
  291. */
  292. s16b label_to_inven(int c)
  293. {
  294. int i;
  295. /* Convert */
  296. i = (islower((unsigned char) c) ? A2I(c) : -1);
  297. /* Verify the index */
  298. if ((i < 0) || (i > INVEN_PACK))
  299. return (-1);
  300. /* Empty slots can never be chosen */
  301. if (!p_ptr->inventory[i].k_idx)
  302. return (-1);
  303. /* Return the index */
  304. return (i);
  305. }
  306. /*
  307. * Convert a label into the index of a item in the "equip".
  308. *
  309. * Return "-1" if the label does not indicate a real item.
  310. */
  311. s16b label_to_equip(int c)
  312. {
  313. int i;
  314. /* Convert */
  315. i = (islower((unsigned char) c) ? A2I(c) : -1) + INVEN_WIELD;
  316. /* Verify the index */
  317. if ((i < INVEN_WIELD) || (i >= ALL_INVEN_TOTAL) || (i == INVEN_TOTAL))
  318. return (-1);
  319. /* Empty slots can never be chosen */
  320. if (!p_ptr->inventory[i].k_idx)
  321. return (-1);
  322. /* Return the index */
  323. return (i);
  324. }
  325. /*
  326. * Hack -- determine if an item is "wearable" (or a missile)
  327. */
  328. bool wearable_p(const object_type * o_ptr)
  329. {
  330. /* Valid "tval" codes */
  331. switch (o_ptr->tval) {
  332. case TV_SHOT:
  333. case TV_ARROW:
  334. case TV_BOLT:
  335. case TV_BOW:
  336. case TV_DIGGING:
  337. case TV_HAFTED:
  338. case TV_POLEARM:
  339. case TV_SWORD:
  340. case TV_BOOTS:
  341. case TV_GLOVES:
  342. case TV_HELM:
  343. case TV_CROWN:
  344. case TV_SHIELD:
  345. case TV_CLOAK:
  346. case TV_SOFT_ARMOR:
  347. case TV_HARD_ARMOR:
  348. case TV_DRAG_ARMOR:
  349. case TV_LIGHT:
  350. case TV_AMULET:
  351. case TV_RING:
  352. return (TRUE);
  353. }
  354. /* Nope */
  355. return (FALSE);
  356. }
  357. int get_inscribed_ammo_slot(const object_type * o_ptr)
  358. {
  359. char *s = NULL, *t = NULL;
  360. if (!o_ptr->note)
  361. return 0;
  362. s = strchr(quark_str(o_ptr->note), 'v');
  363. t = strchr(quark_str(o_ptr->note), 'f');
  364. /* Prefer throwing weapons */
  365. if (s && (s[1] >= '0') && (s[1] <= '9'))
  366. return QUIVER_START + (s[1] - '0');
  367. else if (t && (t[1] >= '0') && (t[1] <= '9'))
  368. return QUIVER_START + (t[1] - '0');
  369. return 0;
  370. }
  371. /**
  372. * Used by wield_slot() to find an appopriate slot for ammo. See wield_slot()
  373. * for information on what this returns.
  374. */
  375. s16b wield_slot_ammo(const object_type * o_ptr)
  376. {
  377. s16b i, open = 0;
  378. /* If the ammo is inscribed with a slot number, we'll try to put it in */
  379. /* that slot, if possible. */
  380. i = get_inscribed_ammo_slot(o_ptr);
  381. if (i && !p_ptr->inventory[i].k_idx)
  382. return i;
  383. for (i = QUIVER_START; i < QUIVER_END; i++) {
  384. if (!p_ptr->inventory[i].k_idx) {
  385. /* Save the open slot if we haven't found one already */
  386. if (!open)
  387. open = i;
  388. continue;
  389. }
  390. /* If ammo is cursed we can't stack it */
  391. if (cf_has(p_ptr->inventory[i].flags_curse, CF_STICKY_WIELD))
  392. continue;
  393. /* If they are stackable, we'll use this slot for sure */
  394. if (object_similar(&p_ptr->inventory[i], o_ptr, OSTACK_QUIVER))
  395. return i;
  396. }
  397. /* If not absorbed, return an open slot (or QUIVER_START if no room) */
  398. return open ? open : QUIVER_START;
  399. }
  400. /**
  401. * Determine which equipment slot (if any) an item likes. The slot might (or
  402. * might not) be open, but it is a slot which the object could be equipped in.
  403. *
  404. * For items where multiple slots could work (e.g. ammo or rings), the function
  405. * will try to a return a stackable slot first (only for ammo), then an open
  406. * slot if possible, and finally a used (but valid) slot if necessary.
  407. */
  408. s16b wield_slot(const object_type * o_ptr)
  409. {
  410. /* Slot for equipment */
  411. switch (o_ptr->tval) {
  412. case TV_DIGGING:
  413. case TV_HAFTED:
  414. case TV_POLEARM:
  415. case TV_SWORD:
  416. return (INVEN_WIELD);
  417. case TV_BOW:
  418. return (INVEN_BOW);
  419. case TV_RING:
  420. return p_ptr->inventory[INVEN_RIGHT].
  421. k_idx ? INVEN_LEFT : INVEN_RIGHT;
  422. case TV_AMULET:
  423. return (INVEN_NECK);
  424. case TV_LIGHT:
  425. return (INVEN_LIGHT);
  426. case TV_DRAG_ARMOR:
  427. case TV_HARD_ARMOR:
  428. case TV_SOFT_ARMOR:
  429. return (INVEN_BODY);
  430. case TV_CLOAK:
  431. return (INVEN_OUTER);
  432. case TV_SHIELD:
  433. return (INVEN_ARM);
  434. case TV_CROWN:
  435. case TV_HELM:
  436. return (INVEN_HEAD);
  437. case TV_GLOVES:
  438. return (INVEN_HANDS);
  439. case TV_BOOTS:
  440. return (INVEN_FEET);
  441. case TV_BOLT:
  442. case TV_ARROW:
  443. case TV_SHOT:
  444. return wield_slot_ammo(o_ptr);
  445. }
  446. /* No slot available */
  447. return (-1);
  448. }
  449. /*
  450. * \returns whether item o_ptr will fit in slot 'slot'
  451. */
  452. bool slot_can_wield_item(int slot, const object_type * o_ptr)
  453. {
  454. if (o_ptr->tval == TV_RING)
  455. return (slot == INVEN_LEFT || slot == INVEN_RIGHT) ? TRUE : FALSE;
  456. else if (obj_is_ammo(o_ptr))
  457. return (slot >= QUIVER_START && slot < QUIVER_END) ? TRUE : FALSE;
  458. else if (wield_slot(o_ptr) == slot)
  459. return TRUE;
  460. else if (of_has(o_ptr->flags_obj, OF_THROWING))
  461. return (slot >= QUIVER_START && slot < QUIVER_END) ? TRUE : FALSE;
  462. else
  463. return FALSE;
  464. }
  465. /*
  466. * Return a string mentioning how a given item is carried
  467. */
  468. const char *mention_use(int slot)
  469. {
  470. char tag[5] = "";
  471. if (slot >= QUIVER_START) {
  472. if (obj_is_ammo(&p_ptr->inventory[slot]))
  473. strnfmt(tag, sizeof(tag), "[f%d]", slot - QUIVER_START);
  474. else if (obj_is_quiver_obj(&p_ptr->inventory[slot]))
  475. strnfmt(tag, sizeof(tag), "[v%d]", slot - QUIVER_START);
  476. }
  477. switch (slot) {
  478. case INVEN_WIELD:
  479. {
  480. if (adj_str_hold[p_ptr->state.stat_ind[A_STR]] <
  481. p_ptr->inventory[slot].weight / 10)
  482. return "Just lifting";
  483. else
  484. return "Wielding";
  485. }
  486. case INVEN_BOW:{
  487. if (adj_str_hold[p_ptr->state.stat_ind[A_STR]] <
  488. p_ptr->inventory[slot].weight / 10)
  489. return "Just holding";
  490. else
  491. return "Shooting";
  492. }
  493. case INVEN_LEFT:
  494. return "On left hand";
  495. case INVEN_RIGHT:
  496. return "On right hand";
  497. case INVEN_NECK:
  498. return "Around neck";
  499. case INVEN_LIGHT:
  500. return "Light source";
  501. case INVEN_BODY:
  502. return "On body";
  503. case INVEN_OUTER:
  504. return "About body";
  505. case INVEN_ARM:
  506. return "On arm";
  507. case INVEN_HEAD:
  508. return "On head";
  509. case INVEN_HANDS:
  510. return "On hands";
  511. case INVEN_FEET:
  512. return "On feet";
  513. case QUIVER_START + 0:
  514. case QUIVER_START + 1:
  515. case QUIVER_START + 2:
  516. case QUIVER_START + 3:
  517. case QUIVER_START + 4:
  518. case QUIVER_START + 5:
  519. case QUIVER_START + 6:
  520. case QUIVER_START + 7:
  521. case QUIVER_START + 8:
  522. case QUIVER_START + 9:
  523. return format("In quiver %s", tag);
  524. }
  525. return "In pack";
  526. }
  527. /*
  528. * Return a string describing how a given item is being worn.
  529. * Currently, only used for items in the equipment, not inventory.
  530. */
  531. const char *describe_use(int i)
  532. {
  533. const char *p;
  534. switch (i) {
  535. case INVEN_WIELD:
  536. p = "attacking monsters with";
  537. break;
  538. case INVEN_BOW:
  539. p = "shooting missiles with";
  540. break;
  541. case INVEN_LEFT:
  542. p = "wearing on your left hand";
  543. break;
  544. case INVEN_RIGHT:
  545. p = "wearing on your right hand";
  546. break;
  547. case INVEN_NECK:
  548. p = "wearing around your neck";
  549. break;
  550. case INVEN_LIGHT:
  551. p = "using to light the way";
  552. break;
  553. case INVEN_BODY:
  554. p = "wearing on your body";
  555. break;
  556. case INVEN_OUTER:
  557. p = "wearing on your back";
  558. break;
  559. case INVEN_ARM:{
  560. if (p_ptr->state.shield_on_back)
  561. p = "carrying on your back";
  562. else
  563. p = "wearing on your arm";
  564. break;
  565. }
  566. case INVEN_HEAD:
  567. p = "wearing on your head";
  568. break;
  569. case INVEN_HANDS:
  570. p = "wearing on your hands";
  571. break;
  572. case INVEN_FEET:
  573. p = "wearing on your feet";
  574. break;
  575. case QUIVER_START + 0:
  576. case QUIVER_START + 1:
  577. case QUIVER_START + 2:
  578. case QUIVER_START + 3:
  579. case QUIVER_START + 4:
  580. case QUIVER_START + 5:
  581. case QUIVER_START + 6:
  582. case QUIVER_START + 7:
  583. case QUIVER_START + 8:
  584. case QUIVER_START + 9:
  585. {
  586. p = "have in your quiver";
  587. break;
  588. }
  589. default:
  590. p = "carrying in your pack";
  591. break;
  592. }
  593. /* Hack -- Heavy weapon */
  594. if (i == INVEN_WIELD) {
  595. object_type *o_ptr;
  596. o_ptr = &p_ptr->inventory[i];
  597. if (adj_str_hold[p_ptr->state.stat_ind[A_STR]] <
  598. o_ptr->weight / 10) {
  599. p = "just lifting";
  600. }
  601. }
  602. /* Hack -- Heavy bow */
  603. if (i == INVEN_BOW) {
  604. object_type *o_ptr;
  605. o_ptr = &p_ptr->inventory[i];
  606. if (adj_str_hold[p_ptr->state.stat_ind[A_STR]] <
  607. o_ptr->weight / 10) {
  608. p = "just holding";
  609. }
  610. }
  611. /* Return the result */
  612. return p;
  613. }
  614. /*
  615. * Check an item against the item tester info
  616. */
  617. bool item_tester_okay(const object_type * o_ptr)
  618. {
  619. /* Hack -- allow listing empty slots */
  620. if (item_tester_full)
  621. return (TRUE);
  622. /* Require an item */
  623. if (!o_ptr->k_idx)
  624. return (FALSE);
  625. /* Hack -- ignore "gold" */
  626. if (o_ptr->tval == TV_GOLD)
  627. return (FALSE);
  628. /* Check the tval */
  629. if (item_tester_tval) {
  630. if (item_tester_tval != o_ptr->tval)
  631. return (FALSE);
  632. }
  633. /* Check the hook */
  634. if (item_tester_hook) {
  635. if (!(*item_tester_hook) (o_ptr))
  636. return (FALSE);
  637. }
  638. /* Assume okay */
  639. return (TRUE);
  640. }
  641. /*
  642. * Get the indexes of objects at a given floor location. -TNB-
  643. *
  644. * Return the number of object indexes acquired.
  645. *
  646. * Valid flags are any combination of the bits:
  647. * 0x01 -- Verify item tester
  648. * 0x02 -- Marked/visible items only
  649. * 0x04 -- Only the top item
  650. */
  651. int scan_floor(int *items, int max_size, int y, int x, int mode)
  652. {
  653. int this_o_idx, next_o_idx;
  654. int num = 0;
  655. /* Sanity */
  656. if (!in_bounds(y, x))
  657. return 0;
  658. /* Scan all objects in the grid */
  659. for (this_o_idx = cave_o_idx[y][x]; this_o_idx;
  660. this_o_idx = next_o_idx) {
  661. object_type *o_ptr;
  662. /* XXX Hack -- Enforce limit */
  663. if (num >= max_size)
  664. break;
  665. /* Get the object */
  666. o_ptr = &o_list[this_o_idx];
  667. /* Get the next object */
  668. next_o_idx = o_ptr->next_o_idx;
  669. /* Item tester */
  670. if ((mode & 0x01) && !item_tester_okay(o_ptr))
  671. continue;
  672. /* Marked */
  673. if ((mode & 0x02) && (!o_ptr->marked || squelch_hide_item(o_ptr)))
  674. continue;
  675. /* Accept this item */
  676. items[num++] = this_o_idx;
  677. /* Only one */
  678. if (mode & 0x04)
  679. break;
  680. }
  681. return num;
  682. }
  683. /*
  684. * Excise a dungeon object from any stacks
  685. */
  686. void excise_object_idx(int o_idx)
  687. {
  688. object_type *j_ptr;
  689. s16b this_o_idx, next_o_idx = 0;
  690. s16b prev_o_idx = 0;
  691. /* Object */
  692. j_ptr = &o_list[o_idx];
  693. /* Monster */
  694. if (j_ptr->held_m_idx) {
  695. monster_type *m_ptr;
  696. /* Monster */
  697. m_ptr = &m_list[j_ptr->held_m_idx];
  698. /* Scan all objects in the grid */
  699. for (this_o_idx = m_ptr->hold_o_idx; this_o_idx;
  700. this_o_idx = next_o_idx) {
  701. object_type *o_ptr;
  702. /* Get the object */
  703. o_ptr = &o_list[this_o_idx];
  704. /* Get the next object */
  705. next_o_idx = o_ptr->next_o_idx;
  706. /* Done */
  707. if (this_o_idx == o_idx) {
  708. /* No previous */
  709. if (prev_o_idx == 0) {
  710. /* Remove from list */
  711. m_ptr->hold_o_idx = next_o_idx;
  712. }
  713. /* Real previous */
  714. else {
  715. object_type *i_ptr;
  716. /* Previous object */
  717. i_ptr = &o_list[prev_o_idx];
  718. /* Remove from list */
  719. i_ptr->next_o_idx = next_o_idx;
  720. }
  721. /* Forget next pointer */
  722. o_ptr->next_o_idx = 0;
  723. /* Done */
  724. break;
  725. }
  726. /* Save prev_o_idx */
  727. prev_o_idx = this_o_idx;
  728. }
  729. }
  730. /* Dungeon */
  731. else {
  732. int y = j_ptr->iy;
  733. int x = j_ptr->ix;
  734. /* Scan all objects in the grid */
  735. for (this_o_idx = cave_o_idx[y][x]; this_o_idx;
  736. this_o_idx = next_o_idx) {
  737. object_type *o_ptr;
  738. /* Get the object */
  739. o_ptr = &o_list[this_o_idx];
  740. /* Get the next object */
  741. next_o_idx = o_ptr->next_o_idx;
  742. /* Done */
  743. if (this_o_idx == o_idx) {
  744. /* No previous */
  745. if (prev_o_idx == 0) {
  746. /* Remove from list */
  747. cave_o_idx[y][x] = next_o_idx;
  748. }
  749. /* Real previous */
  750. else {
  751. object_type *i_ptr;
  752. /* Previous object */
  753. i_ptr = &o_list[prev_o_idx];
  754. /* Remove from list */
  755. i_ptr->next_o_idx = next_o_idx;
  756. }
  757. /* Forget next pointer */
  758. o_ptr->next_o_idx = 0;
  759. /* Done */
  760. break;
  761. }
  762. /* Save prev_o_idx */
  763. prev_o_idx = this_o_idx;
  764. }
  765. }
  766. }
  767. /*
  768. * Delete a dungeon object
  769. *
  770. * Handle "stacks" of objects correctly.
  771. */
  772. void delete_object_idx(int o_idx)
  773. {
  774. object_type *j_ptr;
  775. /* Excise */
  776. excise_object_idx(o_idx);
  777. /* Object */
  778. j_ptr = &o_list[o_idx];
  779. /* Dungeon floor */
  780. if (!(j_ptr->held_m_idx)) {
  781. int y, x;
  782. /* Location */
  783. y = j_ptr->iy;
  784. x = j_ptr->ix;
  785. /* Visual update */
  786. light_spot(y, x);
  787. }
  788. /* Wipe the object */
  789. object_wipe(j_ptr);
  790. /* Count objects */
  791. o_cnt--;
  792. }
  793. /*
  794. * Deletes all objects at given location
  795. */
  796. void delete_object(int y, int x)
  797. {
  798. s16b this_o_idx, next_o_idx = 0;
  799. /* Paranoia */
  800. if (!in_bounds(y, x))
  801. return;
  802. /* Scan all objects in the grid */
  803. for (this_o_idx = cave_o_idx[y][x]; this_o_idx;
  804. this_o_idx = next_o_idx) {
  805. object_type *o_ptr;
  806. /* Get the object */
  807. o_ptr = &o_list[this_o_idx];
  808. /* Get the next object */
  809. next_o_idx = o_ptr->next_o_idx;
  810. /* Wipe the object */
  811. object_wipe(o_ptr);
  812. /* Count objects */
  813. o_cnt--;
  814. }
  815. /* Objects are gone */ cave_o_idx[y][x] = 0;
  816. /* Visual update */
  817. light_spot(y, x);
  818. }
  819. /*
  820. * Move an object from index i1 to index i2 in the object list
  821. */
  822. static void compact_objects_aux(int i1, int i2)
  823. {
  824. int i;
  825. object_type *o_ptr;
  826. /* Do nothing */
  827. if (i1 == i2)
  828. return;
  829. /* Repair objects */
  830. for (i = 1; i < o_max; i++) {
  831. /* Get the object */
  832. o_ptr = &o_list[i];
  833. /* Skip "dead" objects */
  834. if (!o_ptr->k_idx)
  835. continue;
  836. /* Repair "next" pointers */
  837. if (o_ptr->next_o_idx == i1) {
  838. /* Repair */
  839. o_ptr->next_o_idx = i2;
  840. }
  841. }
  842. /* Get the object */ o_ptr = &o_list[i1];
  843. /* Monster */
  844. if (o_ptr->held_m_idx) {
  845. monster_type *m_ptr;
  846. /* Get the monster */
  847. m_ptr = &m_list[o_ptr->held_m_idx];
  848. /* Repair monster */
  849. if (m_ptr->hold_o_idx == i1) {
  850. /* Repair */
  851. m_ptr->hold_o_idx = i2;
  852. }
  853. }
  854. /* Dungeon */
  855. else {
  856. int y, x;
  857. /* Get location */
  858. y = o_ptr->iy;
  859. x = o_ptr->ix;
  860. /* Repair grid */
  861. if (cave_o_idx[y][x] == i1) {
  862. /* Repair */
  863. cave_o_idx[y][x] = i2;
  864. }
  865. }
  866. /* Hack -- move object */
  867. COPY(&o_list[i2], &o_list[i1], object_type);
  868. /* Hack -- wipe hole */
  869. object_wipe(o_ptr);
  870. }
  871. /*
  872. * Compact and reorder the object list
  873. *
  874. * This function can be very dangerous, use with caution!
  875. *
  876. * When compacting objects, we first destroy gold, on the basis that by the
  877. * time item compaction becomes an issue, the player really won't care.
  878. * We also nuke items marked as squelch.
  879. *
  880. * When compacting other objects, we base the saving throw on a combination of
  881. * object level, distance from player, and current "desperation".
  882. *
  883. * After compacting, we "reorder" the objects into a more compact order, and we
  884. * reset the allocation info, and the "live" array.
  885. */
  886. void compact_objects(int size)
  887. {
  888. int py = p_ptr->py;
  889. int px = p_ptr->px;
  890. int i, y, x, cnt;
  891. int cur_lev, cur_dis, chance;
  892. /* Reorder objects when not passed a size */
  893. if (!size) {
  894. /* Excise dead objects (backwards!) */
  895. for (i = o_max - 1; i >= 1; i--) {
  896. object_type *o_ptr = &o_list[i];
  897. /* Skip real objects */
  898. if (o_ptr->k_idx)
  899. continue;
  900. /* Move last object into open hole */
  901. compact_objects_aux(o_max - 1, i);
  902. /* Compress "o_max" */
  903. o_max--;
  904. }
  905. return;
  906. }
  907. /* Message */
  908. msg("Compacting objects...");
  909. /*** Try destroying objects ***/
  910. /* First do gold */
  911. for (i = 1; (i < o_max) && (size); i++) {
  912. object_type *o_ptr = &o_list[i];
  913. /* Nuke gold or squelched items */
  914. if (o_ptr->tval == TV_GOLD || squelch_item_ok(o_ptr)) {
  915. delete_object_idx(i);
  916. size--;
  917. }
  918. }
  919. /* Compact at least 'size' objects */
  920. for (cnt = 1; size; cnt++) {
  921. /* Get more vicious each iteration */
  922. cur_lev = 5 * cnt;
  923. /* Get closer each iteration */
  924. cur_dis = 5 * (20 - cnt);
  925. /* Examine the objects */
  926. for (i = 1; (i < o_max) && (size); i++) {
  927. object_type *o_ptr = &o_list[i];
  928. object_kind *k_ptr = &k_info[o_ptr->k_idx];
  929. /* Skip dead objects */
  930. if (!o_ptr->k_idx)
  931. continue;
  932. /* Hack -- High level objects start out "immune" */
  933. if (k_ptr->level > cur_lev && !k_ptr->squelch)
  934. continue;
  935. /* Monster */
  936. if (o_ptr->held_m_idx) {
  937. monster_type *m_ptr;
  938. /* Get the monster */
  939. m_ptr = &m_list[o_ptr->held_m_idx];
  940. /* Get the location */
  941. y = m_ptr->fy;
  942. x = m_ptr->fx;
  943. /* Monsters protect their objects */
  944. if ((randint0(100) < 90) && !k_ptr->squelch)
  945. continue;
  946. }
  947. /* Dungeon */
  948. else {
  949. /* Get the location */
  950. y = o_ptr->iy;
  951. x = o_ptr->ix;
  952. }
  953. /* Nearby objects start out "immune" */
  954. if ((cur_dis > 0) && (distance(py, px, y, x) < cur_dis)
  955. && !k_ptr->squelch)
  956. continue;
  957. /* Saving throw */
  958. chance = 90;
  959. /* Hack -- only compact artifacts in emergencies */
  960. if (artifact_p(o_ptr) && (cnt < 1000))
  961. chance = 100;
  962. /* Apply the saving throw */
  963. if (randint0(100) < chance)
  964. continue;
  965. /* Delete the object */
  966. delete_object_idx(i);
  967. size--;
  968. }
  969. }
  970. /* Reorder objects */
  971. compact_objects(0);
  972. }
  973. /*
  974. * Delete all the items when player leaves the level
  975. *
  976. * Note -- we do NOT visually reflect these (irrelevant) changes
  977. *
  978. * Hack -- we clear the "cave_o_idx[y][x]" field for every grid,
  979. * and the "m_ptr->next_o_idx" field for every monster, since
  980. * we know we are clearing every object. Technically, we only
  981. * clear those fields for grids/monsters containing objects,
  982. * and we clear it once for every such object.
  983. */
  984. void wipe_o_list(void)
  985. {
  986. int i;
  987. /* Delete the existing objects */
  988. for (i = 1; i < o_max; i++) {
  989. object_type *o_ptr = &o_list[i];
  990. /* Skip dead objects */
  991. if (!o_ptr->k_idx)
  992. continue;
  993. /* Hack -- Preserve unknown artifacts */
  994. if (artifact_p(o_ptr) && !object_known_p(o_ptr)) {
  995. /* Mega-Hack -- Preserve the artifact */
  996. a_info[o_ptr->name1].created = FALSE;
  997. }
  998. /* Monster */
  999. if (o_ptr->held_m_idx) {
  1000. monster_type *m_ptr;
  1001. /* Monster */
  1002. m_ptr = &m_list[o_ptr->held_m_idx];
  1003. /* Hack -- see above */
  1004. m_ptr->hold_o_idx = 0;
  1005. }
  1006. /* Dungeon */
  1007. else {
  1008. /* Get the location */
  1009. int y = o_ptr->iy;
  1010. int x = o_ptr->ix;
  1011. /* Hack -- see above */
  1012. cave_o_idx[y][x] = 0;
  1013. }
  1014. /* Wipe the object */
  1015. (void) WIPE(o_ptr, object_type);
  1016. }
  1017. /* Reset "o_max" */
  1018. o_max = 1;
  1019. /* Reset "o_cnt" */
  1020. o_cnt = 0;
  1021. }
  1022. /*
  1023. * Get and return the index of a "free" object.
  1024. *
  1025. * This routine should almost never fail, but in case it does,
  1026. * we must be sure to handle "failure" of this routine.
  1027. */
  1028. s16b o_pop(void)
  1029. {
  1030. int i;
  1031. /* Initial allocation */
  1032. if (o_max < z_info->o_max) {
  1033. /* Get next space */
  1034. i = o_max;
  1035. /* Expand object array */
  1036. o_max++;
  1037. /* Count objects */
  1038. o_cnt++;
  1039. /* Use this object */
  1040. return (i);
  1041. }
  1042. /* Recycle dead objects */
  1043. for (i = 1; i < o_max; i++) {
  1044. object_type *o_ptr;
  1045. /* Get the object */
  1046. o_ptr = &o_list[i];
  1047. /* Skip live objects */
  1048. if (o_ptr->k_idx)
  1049. continue;
  1050. /* Count objects */
  1051. o_cnt++;
  1052. /* Use this object */
  1053. return (i);
  1054. }
  1055. /* Warn the player (except during dungeon creation) */
  1056. if (character_dungeon)
  1057. msg("Too many objects!");
  1058. /* Oops */
  1059. return (0);
  1060. }
  1061. /*
  1062. * Get the first object at a dungeon location
  1063. * or NULL if there isn't one.
  1064. */
  1065. object_type *get_first_object(int y, int x)
  1066. {
  1067. s16b o_idx = cave_o_idx[y][x];
  1068. if (o_idx)
  1069. return (&o_list[o_idx]);
  1070. /* No object */
  1071. return (NULL);
  1072. }
  1073. /*
  1074. * Get the next object in a stack or NULL if there isn't one.
  1075. */
  1076. object_type *get_next_object(const object_type * o_ptr)
  1077. {
  1078. if (o_ptr->next_o_idx)
  1079. return (&o_list[o_ptr->next_o_idx]);
  1080. /* No more objects */
  1081. return (NULL);
  1082. }
  1083. /**
  1084. * Return the "value" of an "unknown" item
  1085. * Make a guess at the value of non-aware items
  1086. */
  1087. static s32b object_value_base(const object_type * o_ptr)
  1088. {
  1089. object_kind *k_ptr = &k_info[o_ptr->k_idx];
  1090. /* For most items, the "aware" value is simply that of the object kind. */
  1091. s32b aware_cost = k_ptr->cost;
  1092. /* Because weapons and ammo may have enhanced damage dice, their aware
  1093. * value may vary. -LM- */
  1094. switch (o_ptr->tval) {
  1095. case TV_SHOT:
  1096. case TV_ARROW:
  1097. case TV_BOLT:
  1098. {
  1099. if (o_ptr->ds > k_ptr->ds) {
  1100. aware_cost +=
  1101. (1 + o_ptr->ds - k_ptr->ds) * (2 + o_ptr->ds -
  1102. k_ptr->ds) * 1L;
  1103. }
  1104. if (o_ptr->dd > k_ptr->dd) {
  1105. aware_cost +=
  1106. (1 + o_ptr->dd - k_ptr->dd) * (2 + o_ptr->dd -
  1107. k_ptr->dd) * 1L;
  1108. }
  1109. break;
  1110. }
  1111. case TV_HAFTED:
  1112. case TV_POLEARM:
  1113. case TV_SWORD:
  1114. {
  1115. if (o_ptr->ds > k_ptr->ds) {
  1116. aware_cost +=
  1117. (o_ptr->ds - k_ptr->ds) * (o_ptr->ds -
  1118. k_ptr->ds) * o_ptr->dd *
  1119. o_ptr->dd * 16L;
  1120. }
  1121. if (o_ptr->dd > k_ptr->dd) {
  1122. aware_cost +=
  1123. (o_ptr->dd - k_ptr->dd) * (o_ptr->ds -
  1124. k_ptr->dd) * o_ptr->ds *
  1125. o_ptr->ds * 16L;
  1126. }
  1127. break;
  1128. }
  1129. }
  1130. /* Aware item -- use template cost, modified by enhanced dice if needed. */
  1131. if (object_aware_p(o_ptr))
  1132. return (aware_cost);
  1133. /* Analyze the type */
  1134. switch (o_ptr->tval) {
  1135. /* Un-aware Food */
  1136. case TV_FOOD:
  1137. return (5L);
  1138. /* Un-aware Potions */
  1139. case TV_POTION:
  1140. return (20L);
  1141. /* Un-aware Scrolls */
  1142. case TV_SCROLL:
  1143. return (20L);
  1144. /* Un-aware Staffs */
  1145. case TV_STAFF:
  1146. return (80L);
  1147. /* Un-aware Wands */
  1148. case TV_WAND:
  1149. return (80L);
  1150. /* Un-aware Rods */
  1151. case TV_ROD:
  1152. return (200L);
  1153. /* Un-aware Rings (shouldn't happen) */
  1154. case TV_RING:
  1155. return (45L);
  1156. /* Un-aware Amulets (shouldn't happen) */
  1157. case TV_AMULET:
  1158. return (45L);
  1159. }
  1160. /* Paranoia -- Oops */
  1161. return (0L);
  1162. }
  1163. /**
  1164. * Return the "real" price of a "known" item, not including discounts
  1165. *
  1166. * Wand and staffs get cost for each charge
  1167. *
  1168. * Armor is worth an extra 100 gold per bonus point to armor class.
  1169. *
  1170. * Weapons are worth an extra 100 gold per bonus point (AC,TH,TD).
  1171. *
  1172. * Missiles are only worth 5 gold per bonus point, since they
  1173. * usually appear in groups of 20, and we want the player to get
  1174. * the same amount of cash for any "equivalent" item. Note that
  1175. * missiles never have any of the "pval" flags, and in fact, they
  1176. * only have a few of the available flags, primarily of the "slay"
  1177. * and "brand" and "ignore" variety.
  1178. *
  1179. * Now reworked to handle all item properties -NRM-
  1180. */
  1181. static s32b object_value_real(const object_type * o_ptr)
  1182. {
  1183. int i;
  1184. s32b value;
  1185. object_kind *k_ptr = &k_info[o_ptr->k_idx];
  1186. /* Hack -- "worthless" items */
  1187. if (!k_ptr->cost)
  1188. return (0L);
  1189. /* Base cost */
  1190. value = k_ptr->cost;
  1191. /* Artifact */
  1192. if (o_ptr->name1) {
  1193. artifact_type *a_ptr = &a_info[o_ptr->name1];
  1194. /* Hack -- Use the artifact cost instead */
  1195. return (a_ptr->cost);
  1196. }
  1197. /* Ego-Item */
  1198. else if (has_ego_properties(o_ptr)) {
  1199. ego_item_type *e_ptr = &e_info[o_ptr->name2];
  1200. /* Hack -- Reward the ego-item with a bonus */
  1201. value += e_ptr->cost;
  1202. }
  1203. /* Analyze resists, bonuses and multiples */
  1204. switch (o_ptr->tval) {
  1205. case TV_BOW:
  1206. case TV_DIGGING:
  1207. case TV_HAFTED:
  1208. case TV_POLEARM:
  1209. case TV_SWORD:
  1210. case TV_BOOTS:
  1211. case TV_GLOVES:
  1212. case TV_HELM:
  1213. case TV_CROWN:
  1214. case TV_SHIELD:
  1215. case TV_CLOAK:
  1216. case TV_SOFT_ARMOR:
  1217. case TV_HARD_ARMOR:
  1218. case TV_DRAG_ARMOR:
  1219. case TV_LIGHT:
  1220. case TV_AMULET:
  1221. case TV_RING:
  1222. {
  1223. /* Give credit for resists */
  1224. if (if_has(o_ptr->id_other, IF_RES_ACID))
  1225. value +=
  1226. (RES_LEVEL_BASE -
  1227. o_ptr->percent_res[P_RES_ACID]) * 60L;
  1228. if (if_has(o_ptr->id_other, IF_RES_ELEC))
  1229. value +=
  1230. (RES_LEVEL_BASE -
  1231. o_ptr->percent_res[P_RES_ELEC]) * 60L;
  1232. if (if_has(o_ptr->id_other, IF_RES_FIRE))
  1233. value +=
  1234. (RES_LEVEL_BASE -
  1235. o_ptr->percent_res[P_RES_FIRE]) * 60L;
  1236. if (if_has(o_ptr->id_other, IF_RES_COLD))
  1237. value +=
  1238. (RES_LEVEL_BASE -
  1239. o_ptr->percent_res[P_RES_COLD]) * 60L;
  1240. if (if_has(o_ptr->id_other, IF_RES_POIS))
  1241. value +=
  1242. (RES_LEVEL_BASE -
  1243. o_ptr->percent_res[P_RES_POIS]) * 120L;
  1244. if (if_has(o_ptr->id_other, IF_RES_LIGHT))
  1245. value +=
  1246. (RES_LEVEL_BASE -
  1247. o_ptr->percent_res[P_RES_LIGHT]) * 60L;
  1248. if (if_has(o_ptr->id_other, IF_RES_DARK))
  1249. value +=
  1250. (RES_LEVEL_BASE -
  1251. o_ptr->percent_res[P_RES_DARK]) * 60L;
  1252. if (if_has(o_ptr->id_other, IF_RES_CONFU))
  1253. value +=
  1254. (RES_LEVEL_BASE -
  1255. o_ptr->percent_res[P_RES_CONFU]) * 90L;
  1256. if (if_has(o_ptr->id_other, IF_RES_SOUND))
  1257. value +=
  1258. (RES_LEVEL_BASE -
  1259. o_ptr->percent_res[P_RES_SOUND]) * 60L;
  1260. if (if_has(o_ptr->id_other, IF_RES_SHARD))
  1261. value +=
  1262. (RES_LEVEL_BASE -
  1263. o_ptr->percent_res[P_RES_SHARD]) * 60L;
  1264. if (if_has(o_ptr->id_other, IF_RES_NEXUS))
  1265. value +=
  1266. (RES_LEVEL_BASE -
  1267. o_ptr->percent_res[P_RES_NEXUS]) * 60L;
  1268. if (if_has(o_ptr->id_other, IF_RES_NETHR))
  1269. value +=
  1270. (RES_LEVEL_BASE -
  1271. o_ptr->percent_res[P_RES_NETHR]) * 90L;
  1272. if (if_has(o_ptr->id_other, IF_RES_CHAOS))
  1273. value +=
  1274. (RES_LEVEL_BASE -
  1275. o_ptr->percent_res[P_RES_CHAOS]) * 90L;
  1276. if (if_has(o_ptr->id_other, IF_RES_DISEN))
  1277. value +=
  1278. (RES_LEVEL_BASE -
  1279. o_ptr->percent_res[P_RES_DISEN]) * 120L;
  1280. /* Wearing shows all bonuses */
  1281. if (o_ptr->ident & IDENT_WORN) {
  1282. /* Give credit for stat bonuses. */
  1283. for (i = 0; i < A_MAX; i++) {
  1284. if (o_ptr->bonus_stat[i] > BONUS_BASE)
  1285. value +=
  1286. (((o_ptr->bonus_stat[i] +
  1287. 1) * (o_ptr->bonus_stat[i] + 1)) * 100L);
  1288. else if (o_ptr->bonus_stat[i] < BONUS_BASE)
  1289. value -=
  1290. (o_ptr->bonus_stat[i] * o_ptr->bonus_stat[i] *
  1291. 100L);
  1292. }
  1293. /* Give credit for magic items bonus. */
  1294. value += (o_ptr->bonus_other[P_BONUS_M_MASTERY] * 600L);
  1295. /* Give credit for stealth and searching */
  1296. value += (o_ptr->bonus_other[P_BONUS_STEALTH] * 100L);
  1297. value += (o_ptr->bonus_other[P_BONUS_SEARCH] * 40L);
  1298. /* Give credit for infra-vision */
  1299. value += (o_ptr->bonus_other[P_BONUS_INFRA] * 50L);
  1300. /* Give credit for tunneling bonus above that which a digger
  1301. * possesses intrinsically. */
  1302. value +=
  1303. ((o_ptr->bonus_other[P_BONUS_TUNNEL] -
  1304. k_ptr->bonus_other[P_BONUS_TUNNEL]) * 50L);
  1305. /* Give credit for speed bonus. Formula changed to avoid
  1306. * excessively valuable low-speed items. */
  1307. if (o_ptr->bonus_other[P_BONUS_SPEED] > 0)
  1308. value +=
  1309. (o_ptr->bonus_other[P_BONUS_SPEED] *
  1310. o_ptr->bonus_other[P_BONUS_SPEED] * 5000L);
  1311. else if (o_ptr->bonus_other[P_BONUS_SPEED] < 0)
  1312. value -=
  1313. (o_ptr->bonus_other[P_BONUS_SPEED] *
  1314. o_ptr->bonus_other[P_BONUS_SPEED] * 2000L);
  1315. /* Give credit for extra shots */
  1316. value += (o_ptr->bonus_other[P_BONUS_SHOTS] * 8000L);
  1317. /* Give credit for extra might */
  1318. value += (o_ptr->bonus_other[P_BONUS_MIGHT] * 6000L);
  1319. }
  1320. /* Give credit for slays */
  1321. if (if_has(o_ptr->id_other, IF_SLAY_ANIMAL))
  1322. value +=
  1323. (o_ptr->multiple_slay[P_SLAY_ANIMAL] -
  1324. MULTIPLE_BASE) * 150L;
  1325. if (if_has(o_ptr->id_other, IF_SLAY_EVIL))
  1326. value +=
  1327. (o_ptr->multiple_slay[P_SLAY_EVIL] -
  1328. MULTIPLE_BASE) * 500L;
  1329. if (if_has(o_ptr->id_other, IF_SLAY_UNDEAD))
  1330. value +=
  1331. (o_ptr->multiple_slay[P_SLAY_UNDEAD] -
  1332. MULTIPLE_BASE) * 200L;
  1333. if (if_has(o_ptr->id_other, IF_SLAY_DEMON))
  1334. value +=
  1335. (o_ptr->multiple_slay[P_SLAY_DEMON] -
  1336. MULTIPLE_BASE) * 200L;
  1337. if (if_has(o_ptr->id_other, IF_SLAY_ORC))
  1338. value +=
  1339. (o_ptr->multiple_slay[P_SLAY_ORC] -
  1340. MULTIPLE_BASE) * 100L;
  1341. if (if_has(o_ptr->id_other, IF_SLAY_TROLL))
  1342. value +=
  1343. (o_ptr->multiple_slay[P_SLAY_TROLL] -
  1344. MULTIPLE_BASE) * 150L;
  1345. if (if_has(o_ptr->id_other, IF_SLAY_GIANT))
  1346. value +=
  1347. (o_ptr->multiple_slay[P_SLAY_GIANT] -
  1348. MULTIPLE_BASE) * 100L;
  1349. if (if_has(o_ptr->id_other, IF_SLAY_DRAGON))
  1350. value +=
  1351. (o_ptr->multiple_slay[P_SLAY_DRAGON] -
  1352. MULTIPLE_BASE) * 200L;
  1353. /* Give credit for brands */
  1354. if (if_has(o_ptr->id_other, IF_BRAND_ACID))
  1355. value +=
  1356. (o_ptr->multiple_brand[P_BRAND_ACID] -
  1357. MULTIPLE_BASE) * 300L;
  1358. if (if_has(o_ptr->id_other, IF_BRAND_ELEC))
  1359. value +=
  1360. (o_ptr->multiple_brand[P_BRAND_ELEC] -
  1361. MULTIPLE_BASE) * 300L;
  1362. if (if_has(o_ptr->id_other, IF_BRAND_FIRE))
  1363. value +=
  1364. (o_ptr->multiple_brand[P_BRAND_FIRE] -
  1365. MULTIPLE_BASE) * 200L;
  1366. if (if_has(o_ptr->id_other, IF_BRAND_COLD))
  1367. value +=
  1368. (o_ptr->multiple_brand[P_BRAND_COLD] -
  1369. MULTIPLE_BASE) * 200L;
  1370. if (if_has(o_ptr->id_other, IF_BRAND_POIS))
  1371. value +=
  1372. (o_ptr->multiple_brand[P_BRAND_POIS] -
  1373. MULTIPLE_BASE) * 150L;
  1374. /* Give credit for object flags */
  1375. if (of_has(o_ptr->id_obj, OF_SUSTAIN_STR))
  1376. value += 500L;
  1377. if (of_has(o_ptr->id_obj, OF_SUSTAIN_INT))
  1378. value += 300L;
  1379. if (of_has(o_ptr->id_obj, OF_SUSTAIN_WIS))
  1380. value += 300L;
  1381. if (of_has(o_ptr->id_obj, OF_SUSTAIN_DEX))
  1382. value += 400L;
  1383. if (of_has(o_ptr->id_obj, OF_SUSTAIN_CON))
  1384. value += 400L;
  1385. if (of_has(o_ptr->id_obj, OF_SUSTAIN_CHR))
  1386. value += 50L;
  1387. if (of_has(o_ptr->id_obj, OF_SLOW_DIGEST))
  1388. value += 200L;
  1389. if (of_has(o_ptr->id_obj, OF_FEATHER))
  1390. value += 200L;
  1391. if (of_has(o_ptr->id_obj, OF_REGEN))
  1392. value += 400L;
  1393. if (of_has(o_ptr->id_obj, OF_TELEPATHY))
  1394. value += 5000L;
  1395. if (of_has(o_ptr->id_obj, OF_SEE_INVIS))
  1396. value += 700L;
  1397. if (of_has(o_ptr->id_obj, OF_FREE_ACT))
  1398. value += 800L;
  1399. if (of_has(o_ptr->id_obj, OF_HOLD_LIFE))
  1400. value += 700L;
  1401. if (of_has(o_ptr->id_obj, OF_SEEING))
  1402. value += 700L;
  1403. if (of_has(o_ptr->id_obj, OF_FEARLESS))
  1404. value += 500L;
  1405. if (of_has(o_ptr->id_obj, OF_LIGHT))
  1406. value += 300L;
  1407. if (of_has(o_ptr->id_obj, OF_BLESSED))
  1408. value += 1000L;
  1409. if (of_has(o_ptr->id_obj, OF_IMPACT))
  1410. value += 50L;
  1411. if (of_has(o_ptr->id_obj, OF_ACID_PROOF))
  1412. value += 100L;
  1413. if (of_has(o_ptr->id_obj, OF_ELEC_PROOF))
  1414. value += 100L;
  1415. if (of_has(o_ptr->id_obj, OF_FIRE_PROOF))
  1416. value += 100L;
  1417. if (of_has(o_ptr->id_obj, OF_COLD_PROOF))
  1418. value += 100L;
  1419. if (of_has(o_ptr->id_obj, OF_DARKNESS))
  1420. value += 1000L;
  1421. /* Give 'credit' for curse flags */
  1422. if (cf_has(o_ptr->id_curse, CF_TELEPORT))
  1423. value -= 200L;
  1424. if (cf_has(o_ptr->id_curse, CF_NO_TELEPORT))
  1425. value -= 1000L;
  1426. if (cf_has(o_ptr->id_curse, CF_AGGRO_PERM))
  1427. value -= 2000L;
  1428. if (cf_has(o_ptr->id_curse, CF_AGGRO_RAND))
  1429. value -= 700L;
  1430. if (cf_has(o_ptr->id_curse, CF_SLOW_REGEN))
  1431. value -= 200L;
  1432. if (cf_has(o_ptr->id_curse, CF_AFRAID))
  1433. value -= 500L;
  1434. if (cf_has(o_ptr->id_curse, CF_HUNGRY))
  1435. value -= 200L;
  1436. if (cf_has(o_ptr->id_curse, CF_POIS_RAND))
  1437. value -= 200L;
  1438. if (cf_has(o_ptr->id_curse, CF_POIS_RAND_BAD))
  1439. value -= 500L;
  1440. if (cf_has(o_ptr->id_curse, CF_CUT_RAND))
  1441. value -= 200L;
  1442. if (cf_has(o_ptr->id_curse, CF_CUT_RAND_BAD))
  1443. value -= 400L;
  1444. if (cf_has(o_ptr->id_curse, CF_HALLU_RAND))
  1445. value -= 800L;
  1446. if (cf_has(o_ptr->id_curse, CF_DROP_WEAPON))
  1447. value -= 500L;
  1448. if (cf_has(o_ptr->id_curse, CF_ATTRACT_DEMON))
  1449. value -= 800L;
  1450. if (cf_has(o_ptr->id_curse, CF_ATTRACT_UNDEAD))
  1451. value -= 900L;
  1452. if (cf_has(o_ptr->id_curse, CF_STICKY_WIELD))
  1453. value -= 2500L;
  1454. if (cf_has(o_ptr->id_curse, CF_STICKY_CARRY))
  1455. value -= 1000L;
  1456. if (cf_has(o_ptr->id_curse, CF_PARALYZE))
  1457. value -= 800L;
  1458. if (cf_has(o_ptr->id_curse, CF_PARALYZE_ALL))
  1459. value -= 2000L;
  1460. if (cf_has(o_ptr->id_curse, CF_DRAIN_EXP))
  1461. value -= 800L;
  1462. if (cf_has(o_ptr->id_curse, CF_DRAIN_MANA))
  1463. value -= 1000L;
  1464. if (cf_has(o_ptr->id_curse, CF_DRAIN_STAT))
  1465. value -= 1500L;
  1466. if (cf_has(o_ptr->id_curse, CF_DRAIN_CHARGE))
  1467. value -= 1500L;
  1468. break;
  1469. }
  1470. case TV_SHOT:
  1471. case TV_ARROW:
  1472. case TV_BOLT:
  1473. {
  1474. /* Give credit for slays */
  1475. if (if_has(o_ptr->id_other, IF_SLAY_ANIMAL))
  1476. value +=
  1477. (o_ptr->multiple_slay[P_SLAY_ANIMAL] -
  1478. MULTIPLE_BASE) * 15L;
  1479. if (if_has(o_ptr->id_other, IF_SLAY_EVIL))
  1480. value +=
  1481. (o_ptr->multiple_slay[P_SLAY_EVIL] -
  1482. MULTIPLE_BASE) * 50L;
  1483. if (if_has(o_ptr->id_other, IF_SLAY_UNDEAD))
  1484. value +=
  1485. (o_ptr->multiple_slay[P_SLAY_UNDEAD] -
  1486. MULTIPLE_BASE) * 20L;
  1487. if (if_has(o_ptr->id_other, IF_SLAY_DEMON))
  1488. value +=
  1489. (o_ptr->multiple_slay[P_SLAY_DEMON] -
  1490. MULTIPLE_BASE) * 20L;
  1491. if (if_has(o_ptr->id_other, IF_SLAY_ORC))
  1492. value +=
  1493. (o_ptr->multiple_slay[P_SLAY_ORC] -
  1494. MULTIPLE_BASE) * 10L;
  1495. if (if_has(o_ptr->id_other, IF_SLAY_TROLL))
  1496. value +=
  1497. (o_ptr->multiple_slay[P_SLAY_TROLL] -
  1498. MULTIPLE_BASE) * 15L;
  1499. if (if_has(o_ptr->id_other, IF_SLAY_GIANT))
  1500. value +=
  1501. (o_ptr->multiple_slay[P_SLAY_GIANT] -
  1502. MULTIPLE_BASE) * 10L;
  1503. if (if_has(o_ptr->id_other, IF_SLAY_DRAGON))
  1504. value +=
  1505. (o_ptr->multiple_slay[P_SLAY_DRAGON] -
  1506. MULTIPLE_BASE) * 20L;
  1507. /* Give credit for brands */
  1508. if (if_has(o_ptr->id_other, IF_BRAND_ACID))
  1509. value +=
  1510. (o_ptr->multiple_brand[P_BRAND_ACID] -
  1511. MULTIPLE_BASE) * 30L;
  1512. if (if_has(o_ptr->id_other, IF_BRAND_ELEC))
  1513. value +=
  1514. (o_ptr->multiple_brand[P_BRAND_ELEC] -
  1515. MULTIPLE_BASE) * 30L;
  1516. if (if_has(o_ptr->id_other, IF_BRAND_FIRE))
  1517. value +=
  1518. (o_ptr->multiple_brand[P_BRAND_FIRE] -
  1519. MULTIPLE_BASE) * 20L;
  1520. if (if_has(o_ptr->id_other, IF_BRAND_COLD))
  1521. value +=
  1522. (o_ptr->multiple_brand[P_BRAND_COLD] -
  1523. MULTIPLE_BASE) * 20L;
  1524. if (if_has(o_ptr->id_other, IF_BRAND_POIS))
  1525. value +=
  1526. (o_ptr->multiple_brand[P_BRAND_POIS] -
  1527. MULTIPLE_BASE) * 15L;
  1528. break;
  1529. }
  1530. }
  1531. /* Analyze the item */
  1532. switch (o_ptr->tval) {
  1533. /* Wands/Staffs */
  1534. case TV_WAND:
  1535. {
  1536. int temp_pval = randcalc(k_ptr->pval, k_ptr->level, RANDOMISE);
  1537. /* Pay extra for charges, depending on standard number of charges.
  1538. * Handle new-style wands correctly. */
  1539. value +=
  1540. (value * o_ptr->pval / o_ptr->number / (temp_pval * 2));
  1541. /* Done */
  1542. break;
  1543. }
  1544. case TV_STAFF:
  1545. {
  1546. /* Pay extra for charges, depending on standard number of charges */
  1547. int temp_pval = randcalc(k_ptr->pval, k_ptr->level, RANDOMISE);
  1548. value += (value * o_ptr->pval / (temp_pval * 2));
  1549. /* Done */
  1550. break;
  1551. }
  1552. /* Rings/Amulets */
  1553. case TV_RING:
  1554. case TV_AMULET:
  1555. {
  1556. /* Give credit for bonuses */
  1557. if (if_has(o_ptr->id_other, IF_TO_H))
  1558. value += o_ptr->to_h * 100L;
  1559. if (if_has(o_ptr->id_other, IF_TO_D))
  1560. value += o_ptr->to_d * 100L;
  1561. if (if_has(o_ptr->id_other, IF_TO_A))
  1562. value += o_ptr->to_a * 100L;
  1563. /* Done */
  1564. break;
  1565. }
  1566. /* Armor */
  1567. case TV_BOOTS:
  1568. case TV_GLOVES:
  1569. case TV_CLOAK:
  1570. case TV_CROWN:
  1571. case TV_HELM:
  1572. case TV_SHIELD:
  1573. case TV_SOFT_ARMOR:
  1574. case TV_HARD_ARMOR:
  1575. case TV_DRAG_ARMOR:
  1576. {
  1577. /* If it differs from the object kind's base armour Skill penalty
  1578. * (this penalty must be in the range +0 to -12), give a debit or
  1579. * credit for any modification to Skill. -LM- */
  1580. if (((o_ptr->to_h != k_ptr->to_h.base) || (o_ptr->to_h < -12)
  1581. || (o_ptr->to_h > 0))
  1582. && (if_has(o_ptr->id_other, IF_TO_H)))
  1583. value += (o_ptr->to_h * 100L);
  1584. /* Standard debit or credit for to_d and to_a bonuses. */
  1585. if (if_has(o_ptr->id_other, IF_TO_D))
  1586. value += (o_ptr->to_d) * 100L;
  1587. if (if_has(o_ptr->id_other, IF_TO_A))
  1588. value += (o_ptr->to_a) * 100L;
  1589. /* Done */
  1590. break;
  1591. }
  1592. /* Sharp weapons */
  1593. case TV_DIGGING:
  1594. case TV_SWORD:
  1595. case TV_POLEARM:
  1596. {
  1597. /* Blessed */
  1598. if (of_has(o_ptr->id_obj, OF_BLESSED))
  1599. value += 400L;
  1600. /* Fall through */
  1601. }
  1602. /* Bows/Weapons */
  1603. case TV_BOW:
  1604. case TV_HAFTED:
  1605. {
  1606. /* Because +to_hit and +to_dam work differently now, this formula
  1607. * also needed to change. Although long, it now yields results
  1608. * that match the increase in the weapon's combat power. -LM- */
  1609. int hit = (if_has(o_ptr->id_other, IF_TO_H)) ? o_ptr->to_h : 0;
  1610. int dam = (if_has(o_ptr->id_other, IF_TO_D)) ? o_ptr->to_d : 0;
  1611. int arm = (if_has(o_ptr->id_other, IF_TO_A)) ? o_ptr->to_a : 0;
  1612. value +=
  1613. (((hit + 10) * (dam + 10) - 100) * (k_ptr->cost +
  1614. 1000L) / 250) +
  1615. (arm * 100);
  1616. /* Hack -- Factor in improved damage dice. If you want to buy one
  1617. * of the truly dangerous weapons, be prepared to pay for it. */
  1618. if ((o_ptr->ds > k_ptr->ds) && (o_ptr->dd == k_ptr->dd)
  1619. && (if_has(o_ptr->id_other, IF_DD_DS))) {
  1620. value += (o_ptr->ds - k_ptr->ds) * (o_ptr->ds - k_ptr->ds)
  1621. * o_ptr->dd * o_ptr->dd * 16L;
  1622. /* Naturally, ego-items with high base dice should be very
  1623. * expensive. */
  1624. if (has_ego_properties(o_ptr)) {
  1625. value += (5 * value / 2);
  1626. }
  1627. }
  1628. /* Give credit for perfect balance. */
  1629. if (of_has(o_ptr->id_obj, OF_PERFECT_BALANCE))
  1630. value += o_ptr->dd * 200L;
  1631. /* Done */
  1632. break;
  1633. }
  1634. /* Ammo */
  1635. case TV_SHOT:
  1636. case TV_ARROW:
  1637. case TV_BOLT:
  1638. {
  1639. /* Factor in the bonuses */
  1640. if (if_has(o_ptr->id_other, IF_TO_H))
  1641. value += o_ptr->to_h * 2L;
  1642. if (if_has(o_ptr->id_other, IF_TO_D))
  1643. value += o_ptr->to_d * 2L;
  1644. /* Hack -- Factor in improved damage dice. -LM- */
  1645. if ((o_ptr->ds > k_ptr->ds)
  1646. && (if_has(o_ptr->id_other, IF_DD_DS))) {
  1647. value +=
  1648. (1 + o_ptr->ds - k_ptr->ds) * (2 + o_ptr->ds -
  1649. k_ptr->ds) * 1L;
  1650. /* Naturally, ego-items with high base dice should be
  1651. * expensive. */
  1652. if (has_ego_properties(o_ptr)) {
  1653. value += (5 * value / 2);
  1654. }
  1655. }
  1656. /* Done */
  1657. break;
  1658. }
  1659. /* Who wants an empty chest? */
  1660. case TV_CHEST:
  1661. {
  1662. if (o_ptr->pval == 0)
  1663. return (0L);
  1664. break;
  1665. }
  1666. }
  1667. /* Return the value */
  1668. return (value < 0 ? 0L : value);
  1669. }
  1670. /**
  1671. * Return the price of an item including plusses (and charges)
  1672. *
  1673. * This function returns the "value" of the given item (qty one)
  1674. *
  1675. * Never notice "unknown" bonuses or properties, including "curses",
  1676. * since that would give the player information he did not have.
  1677. *
  1678. * Note that discounted items stay discounted forever, even if
  1679. * the discount is "forgotten" by the player via memory loss.
  1680. */
  1681. s32b object_value(const object_type * o_ptr)
  1682. {
  1683. s32b value;
  1684. /* Known items -- acquire the actual value */
  1685. if (object_known_p(o_ptr)
  1686. || ((wield_slot(o_ptr) > 0) && !artifact_p(o_ptr))) {
  1687. /* Real value (see above) */
  1688. value = object_value_real(o_ptr);
  1689. }
  1690. /* Unknown items -- acquire a base value */
  1691. else {
  1692. /* Base value (see above) */
  1693. value = object_value_base(o_ptr);
  1694. }
  1695. /* Apply discount (if any) */
  1696. if (o_ptr->discount)
  1697. value -= (value * o_ptr->discount / 100L);
  1698. /* Return the final value */
  1699. return (value);
  1700. }
  1701. /*
  1702. * Determine if an item can "absorb" a second item
  1703. *
  1704. * See "object_absorb()" for the actual "absorption" code.
  1705. *
  1706. * If permitted, we allow weapons/armor to stack, if "known".
  1707. *
  1708. * Missiles will combine if both stacks have the same "known" status.
  1709. * This is done to make unidentified stacks of missiles useful.
  1710. *
  1711. * Food, potions, scrolls, and "easy know" items always stack.
  1712. *
  1713. * Chests, and activatable items, except rods, never stack (for various
  1714. * reasons).
  1715. */
  1716. bool object_similar(const object_type * o_ptr, const object_type * j_ptr,
  1717. object_stack_t mode)
  1718. {
  1719. int i;
  1720. int total = o_ptr->number + j_ptr->number;
  1721. /* Check against stacking limit */
  1722. if (total >= MAX_STACK_SIZE)
  1723. return FALSE;
  1724. /* Hack -- identical items cannot be stacked */
  1725. if (o_ptr == j_ptr)
  1726. return FALSE;
  1727. /* Require identical object kinds */
  1728. if (o_ptr->k_idx != j_ptr->k_idx)
  1729. return FALSE;
  1730. /* Require identical effects */
  1731. if (o_ptr->effect != j_ptr->effect)
  1732. return (FALSE);
  1733. /* Different flags don't stack */
  1734. if (!of_is_equal(o_ptr->flags_obj, j_ptr->flags_obj))
  1735. return FALSE;
  1736. if (!cf_is_equal(o_ptr->flags_curse, j_ptr->flags_curse))
  1737. return FALSE;
  1738. /* Require identical resists, etc */
  1739. for (i = 0; i < MAX_P_RES; i++)
  1740. if (o_ptr->percent_res[i] != j_ptr->percent_res[i])
  1741. return (FALSE);
  1742. for (i = 0; i < A_MAX; i++)
  1743. if (o_ptr->bonus_stat[i] != j_ptr->bonus_stat[i])
  1744. return (FALSE);
  1745. for (i = 0; i < MAX_P_BONUS; i++)
  1746. if (o_ptr->bonus_other[i] != j_ptr->bonus_other[i])
  1747. return (FALSE);
  1748. for (i = 0; i < MAX_P_SLAY; i++)
  1749. if (o_ptr->multiple_slay[i] != j_ptr->multiple_slay[i])
  1750. return (FALSE);
  1751. for (i = 0; i < MAX_P_BRAND; i++)
  1752. if (o_ptr->multiple_brand[i] != j_ptr->multiple_brand[i])
  1753. return (FALSE);
  1754. /* Artifacts never stack */
  1755. if (o_ptr->name1 || j_ptr->name1)
  1756. return FALSE;
  1757. /* Analyze the items */
  1758. switch (o_ptr->tval) {
  1759. /* Chests never stack */
  1760. case TV_CHEST:
  1761. {
  1762. /* Never okay */
  1763. return FALSE;
  1764. }
  1765. /* Food, potions, scrolls and rods all stack nicely */
  1766. case TV_FOOD:
  1767. case TV_POTION:
  1768. case TV_SCROLL:
  1769. case TV_ROD:
  1770. {
  1771. /* Since the kinds are identical, either both will be aware or both
  1772. * will be unaware */
  1773. break;
  1774. }
  1775. /* Gold, staves and wands stack most of the time */
  1776. case TV_STAFF:
  1777. case TV_WAND:
  1778. case TV_GOLD:
  1779. {
  1780. /* Too much gold or too many charges */
  1781. if (o_ptr->pval + j_ptr->pval > MAX_PVAL)
  1782. return FALSE;
  1783. /* ... otherwise ok */
  1784. else
  1785. break;
  1786. }
  1787. /* Weapons, ammo, armour, jewelry, lights */
  1788. case TV_BOW:
  1789. case TV_DIGGING:
  1790. case TV_HAFTED:
  1791. case TV_POLEARM:
  1792. case TV_SWORD:
  1793. case TV_BOOTS:
  1794. case TV_GLOVES:
  1795. case TV_HELM:
  1796. case TV_CROWN:
  1797. case TV_SHIELD:
  1798. case TV_CLOAK:
  1799. case TV_SOFT_ARMOR:
  1800. case TV_HARD_ARMOR:
  1801. case TV_DRAG_ARMOR:
  1802. case TV_RING:
  1803. case TV_AMULET:
  1804. case TV_LIGHT:
  1805. case TV_BOLT:
  1806. case TV_ARROW:
  1807. case TV_SHOT:
  1808. {
  1809. /* Require identical values */
  1810. if (o_ptr->ac != j_ptr->ac)
  1811. return FALSE;
  1812. if (o_ptr->dd != j_ptr->dd)
  1813. return FALSE;
  1814. if (o_ptr->ds != j_ptr->ds)
  1815. return FALSE;
  1816. /* Require identical bonuses */
  1817. if (o_ptr->to_h != j_ptr->to_h)
  1818. return FALSE;
  1819. if (o_ptr->to_d != j_ptr->to_d)
  1820. return FALSE;
  1821. if (o_ptr->to_a != j_ptr->to_a)
  1822. return FALSE;
  1823. /* Require identical pval */
  1824. if (o_ptr->pval != j_ptr->pval)
  1825. return (FALSE);
  1826. /* Require identical ego-item types */
  1827. if (o_ptr->name2 != j_ptr->name2)
  1828. return (FALSE);
  1829. /* Hack - Never stack recharging wearables ... */
  1830. if ((o_ptr->timeout || j_ptr->timeout)
  1831. && (o_ptr->tval != TV_LIGHT))
  1832. return FALSE;
  1833. /* ... and lights must have same amount of fuel */
  1834. else if ((o_ptr->timeout != j_ptr->timeout)
  1835. && (o_ptr->tval == TV_LIGHT))
  1836. return FALSE;
  1837. /* Probably okay */
  1838. break;
  1839. }
  1840. /* Anything else */
  1841. default:
  1842. {
  1843. /* Require knowledge */
  1844. //if (!object_known_p(o_ptr) || !object_known_p(j_ptr))
  1845. //return (FALSE);
  1846. /* Probably okay */
  1847. break;
  1848. }
  1849. }
  1850. /* Hack -- Require compatible inscriptions */
  1851. if (o_ptr->note != j_ptr->note) {
  1852. /* Never combine different inscriptions */
  1853. if (o_ptr->note && j_ptr->note)
  1854. return (FALSE);
  1855. }
  1856. /* They must be similar enough */
  1857. return (TRUE);
  1858. }
  1859. /*
  1860. * Allow one item to "absorb" another, assuming they are similar.
  1861. *
  1862. * The blending of the "note" field assumes that either (1) one has an
  1863. * inscription and the other does not, or (2) neither has an inscription.
  1864. * In both these cases, we can simply use the existing note, unless the
  1865. * blending object has a note, in which case we use that note.
  1866. *
  1867. * The blending of the "discount" field assumes that either (1) one is a
  1868. * special inscription and one is nothing, or (2) one is a discount and
  1869. * one is a smaller discount, or (3) one is a discount and one is nothing,
  1870. * or (4) both are nothing. In all of these cases, we can simply use the
  1871. * "maximum" of the two "discount" fields.
  1872. *
  1873. * These assumptions are enforced by the "object_similar()" code.
  1874. */
  1875. void object_absorb(object_type * o_ptr, const object_type * j_ptr)
  1876. {
  1877. int total = o_ptr->number + j_ptr->number;
  1878. /* Add together the item counts */
  1879. o_ptr->number =
  1880. ((total < MAX_STACK_SIZE) ? total : (MAX_STACK_SIZE - 1));
  1881. /* Blend all knowledge */
  1882. o_ptr->ident |= (j_ptr->ident & ~IDENT_EMPTY);
  1883. of_union(o_ptr->id_obj, j_ptr->id_obj);
  1884. cf_union(o_ptr->id_curse, j_ptr->id_curse);
  1885. if_union(o_ptr->id_other, j_ptr->id_other);
  1886. /* Hack -- Blend "notes" */
  1887. if (j_ptr->note != 0)
  1888. o_ptr->note = j_ptr->note;
  1889. /* Hack -- save largest discount XXX XXX XXX */
  1890. if (o_ptr->discount < j_ptr->discount)
  1891. o_ptr->discount = j_ptr->discount;
  1892. /* Hack -- blend "feelings" */
  1893. if (o_ptr->feel && j_ptr->feel)
  1894. o_ptr->feel = j_ptr->feel;
  1895. /* Hack -- if rods are stacking, re-calculate the timeouts */
  1896. if (o_ptr->tval == TV_ROD) {
  1897. o_ptr->timeout += j_ptr->timeout;
  1898. }
  1899. /* Hack -- if wands or staves are stacking, combine the charges */
  1900. /* If gold is stacking combine the amount */
  1901. if ((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_STAFF)
  1902. || (o_ptr->tval == TV_GOLD)) {
  1903. int total = o_ptr->pval + j_ptr->pval;
  1904. o_ptr->pval = total >= MAX_PVAL ? MAX_PVAL : total;
  1905. }
  1906. if ((o_ptr->origin != j_ptr->origin)
  1907. || (o_ptr->origin_z != j_ptr->origin_z)
  1908. || (o_ptr->origin_y != j_ptr->origin_y)
  1909. || (o_ptr->origin_x != j_ptr->origin_x)
  1910. || (o_ptr->origin_xtra != j_ptr->origin_xtra)) {
  1911. int act = 2;
  1912. if ((o_ptr->origin == ORIGIN_DROP)
  1913. && (o_ptr->origin == j_ptr->origin)) {
  1914. monster_race *r_ptr = &r_info[o_ptr->origin_xtra];
  1915. monster_race *s_ptr = &r_info[j_ptr->origin_xtra];
  1916. bool r_uniq = rf_has(r_ptr->flags, RF_UNIQUE) ? TRUE : FALSE;
  1917. bool s_uniq = rf_has(s_ptr->flags, RF_UNIQUE) ? TRUE : FALSE;
  1918. if (r_uniq && !s_uniq)
  1919. act = 0;
  1920. else if (s_uniq && !r_uniq)
  1921. act = 1;
  1922. else
  1923. act = 2;
  1924. }
  1925. switch (act) {
  1926. /* Overwrite with j_ptr */
  1927. case 1:
  1928. {
  1929. o_ptr->origin = j_ptr->origin;
  1930. o_ptr->origin_z = j_ptr->origin_z;
  1931. o_ptr->origin_y = j_ptr->origin_y;
  1932. o_ptr->origin_x = j_ptr->origin_x;
  1933. o_ptr->origin_xtra = j_ptr->origin_xtra;
  1934. }
  1935. /* Set as "mixed" */
  1936. case 2:
  1937. {
  1938. o_ptr->origin = ORIGIN_MIXED;
  1939. }
  1940. }
  1941. }
  1942. }
  1943. /*
  1944. * Wipe an object clean.
  1945. */
  1946. void object_wipe(object_type * o_ptr)
  1947. {
  1948. /* Wipe the structure */
  1949. (void) WIPE(o_ptr, object_type);
  1950. }
  1951. /*
  1952. * Prepare an object based on an existing object
  1953. */
  1954. void object_copy(object_type * o_ptr, const object_type * j_ptr)
  1955. {
  1956. /* Copy the structure */
  1957. COPY(o_ptr, j_ptr, object_type);
  1958. }
  1959. /*
  1960. * Prepare an object `dst` representing `amt` objects, based on an existing
  1961. * object `src` representing at least `amt` objects.
  1962. *
  1963. * Takes care of the charge redistribution concerns of stacked items.
  1964. */
  1965. void object_copy_amt(object_type * dst, object_type * src, int amt)
  1966. {
  1967. const object_kind *k_ptr = &k_info[src->k_idx];
  1968. int charge_time = randcalc(k_ptr->time, 0, AVERAGE), max_time;
  1969. /* Get a copy of the object */
  1970. object_copy(dst, src);
  1971. /* Modify quantity */
  1972. dst->number = amt;
  1973. /*
  1974. * If the item has charges/timeouts, set them to the correct level
  1975. * too. We split off the same amount as distribute_charges.
  1976. */
  1977. if (src->tval == TV_WAND || src->tval == TV_STAFF) {
  1978. dst->pval = src->pval * amt / src->number;
  1979. }
  1980. if (src->tval == TV_ROD) {
  1981. max_time = charge_time * amt;
  1982. if (src->timeout > max_time)
  1983. dst->timeout = max_time;
  1984. else
  1985. dst->timeout = src->timeout;
  1986. }
  1987. }
  1988. /**
  1989. * Find and return the index to the oldest object on the given grid marked as
  1990. * "squelch".
  1991. */
  1992. static s16b floor_get_idx_oldest_squelched(int y, int x)
  1993. {
  1994. s16b squelch_idx = 0;
  1995. s16b this_o_idx;
  1996. object_type *o_ptr = NULL;
  1997. for (this_o_idx = cave_o_idx[y][x]; this_o_idx;
  1998. this_o_idx = o_ptr->next_o_idx) {
  1999. o_ptr = &o_list[this_o_idx];
  2000. if (squelch_hide_item(o_ptr))
  2001. squelch_idx = this_o_idx;
  2002. }
  2003. return squelch_idx;
  2004. }
  2005. /*
  2006. * Let the floor carry an object, deleting old squelched items if necessary
  2007. */
  2008. s16b floor_carry(int y, int x, object_type * j_ptr)
  2009. {
  2010. int n = 0;
  2011. s16b o_idx;
  2012. s16b this_o_idx, next_o_idx = 0;
  2013. /* Scan objects in that grid for combination */
  2014. for (this_o_idx = cave_o_idx[y][x]; this_o_idx;
  2015. this_o_idx = next_o_idx) {
  2016. object_type *o_ptr = &o_list[this_o_idx];
  2017. /* Get the next object */
  2018. next_o_idx = o_ptr->next_o_idx;
  2019. /* Check for combination */
  2020. if (object_similar(o_ptr, j_ptr, OSTACK_FLOOR)) {
  2021. /* Combine the items */
  2022. object_absorb(o_ptr, j_ptr);
  2023. /* Result */
  2024. return (this_o_idx);
  2025. }
  2026. /* Count objects */
  2027. n++;
  2028. }
  2029. /* The stack is already too large */
  2030. if (n >= MAX_FLOOR_STACK) {
  2031. /* Squelch the oldest squelched object */
  2032. s16b squelch_idx = floor_get_idx_oldest_squelched(y, x);
  2033. if (squelch_idx)
  2034. delete_object_idx(squelch_idx);
  2035. else
  2036. return 0;
  2037. }
  2038. /* Make an object */
  2039. o_idx = o_pop();
  2040. /* Success */
  2041. if (o_idx) {
  2042. object_type *o_ptr;
  2043. /* Get the object */
  2044. o_ptr = &o_list[o_idx];
  2045. /* Structure Copy */
  2046. object_copy(o_ptr, j_ptr);
  2047. /* Location */
  2048. o_ptr->iy = y;
  2049. o_ptr->ix = x;
  2050. /* Forget monster */
  2051. o_ptr->held_m_idx = 0;
  2052. /* Link the object to the pile */
  2053. o_ptr->next_o_idx = cave_o_idx[y][x];
  2054. /* Link the floor to the object */
  2055. cave_o_idx[y][x] = o_idx;
  2056. /* Notice */
  2057. note_spot(y, x);
  2058. /* Redraw */
  2059. light_spot(y, x);
  2060. }
  2061. /* Result */
  2062. return (o_idx);
  2063. }
  2064. /*
  2065. * Let an object fall to the ground at or near a location.
  2066. *
  2067. * The initial location is assumed to be "in_bounds_fully()".
  2068. *
  2069. * This function takes a parameter "chance". This is the percentage
  2070. * chance that the item will "disappear" instead of drop. If the object
  2071. * has been thrown, then this is the chance of disappearance on contact.
  2072. *
  2073. * This function will produce a description of a drop event under the player
  2074. * when "verbose" is true.
  2075. *
  2076. * We check several locations to see if we can find a location at which
  2077. * the object can combine, stack, or be placed. Artifacts will try very
  2078. * hard to be placed, including "teleporting" to a useful grid if needed.
  2079. */
  2080. void drop_near(object_type * j_ptr, int chance, int y, int x, bool verbose)
  2081. {
  2082. int i, k, n, d, s;
  2083. int bs, bn;
  2084. int by, bx;
  2085. int dy, dx;
  2086. int ty, tx;
  2087. object_type *o_ptr;
  2088. feature_type *f_ptr;
  2089. char o_name[80];
  2090. bool flag = FALSE;
  2091. bool plural = FALSE;
  2092. /* Extract plural */
  2093. if (j_ptr->number != 1)
  2094. plural = TRUE;
  2095. /* Describe object */
  2096. object_desc(o_name, sizeof(o_name), j_ptr, ODESC_BASE);
  2097. /* Handle normal "breakage" */
  2098. if (!artifact_p(j_ptr) && (randint0(100) < chance)) {
  2099. /* Message */
  2100. msg("The %s break%s.", o_name, PLURAL(plural));
  2101. /* Failure */
  2102. return;
  2103. }
  2104. /* Score */
  2105. bs = -1;
  2106. /* Picker */
  2107. bn = 0;
  2108. /* Default */
  2109. by = y;
  2110. bx = x;
  2111. /* Scan local grids */
  2112. for (dy = -3; dy <= 3; dy++) {
  2113. /* Scan local grids */
  2114. for (dx = -3; dx <= 3; dx++) {
  2115. bool comb = FALSE;
  2116. /* Calculate actual distance */
  2117. d = (dy * dy) + (dx * dx);
  2118. /* Ignore distant grids */
  2119. if (d > 10)
  2120. continue;
  2121. /* Location */
  2122. ty = y + dy;
  2123. tx = x + dx;
  2124. f_ptr = &f_info[cave_feat[ty][tx]];
  2125. /* Skip illegal grids */
  2126. if (!in_bounds_fully(ty, tx))
  2127. continue;
  2128. /* Require line of sight */
  2129. if (!los(y, x, ty, tx))
  2130. continue;
  2131. /* Require floor space */
  2132. if (!tf_has(f_ptr->flags, TF_OBJECT))
  2133. continue;
  2134. /* No objects */
  2135. k = 0;
  2136. n = 0;
  2137. /* Scan objects in that grid */
  2138. for (o_ptr = get_first_object(ty, tx); o_ptr;
  2139. o_ptr = get_next_object(o_ptr)) {
  2140. /* Check for possible combination */
  2141. if (object_similar(o_ptr, j_ptr, OSTACK_FLOOR))
  2142. comb = TRUE;
  2143. /* Count objects */
  2144. if (!squelch_hide_item(o_ptr))
  2145. k++;
  2146. else
  2147. n++;
  2148. }
  2149. /* Add new object */
  2150. if (!comb)
  2151. k++;
  2152. /* Paranoia? */
  2153. if ((k + n) > MAX_FLOOR_STACK
  2154. && !floor_get_idx_oldest_squelched(ty, tx))
  2155. continue;
  2156. /* Calculate goodness of location, given distance from source of
  2157. * drop and number of objects. Hack - If the player is dropping
  2158. * the item, encourage it to pile with up to 19 other items. */
  2159. if (cave_m_idx[y][x] < 0) {
  2160. s = 1000 - (d + (k > 20 ? k * 5 : 0));
  2161. } else {
  2162. s = 1000 - (d + k * 5);
  2163. }
  2164. /* Skip bad values */
  2165. if (s < bs)
  2166. continue;
  2167. /* New best value */
  2168. if (s > bs)
  2169. bn = 0;
  2170. /* Apply the randomizer to equivalent values */
  2171. if ((++bn >= 2) && (randint0(bn) != 0))
  2172. continue;
  2173. /* Keep score */
  2174. bs = s;
  2175. /* Track it */
  2176. by = ty;
  2177. bx = tx;
  2178. /* Okay */
  2179. flag = TRUE;
  2180. }
  2181. }
  2182. /* Handle lack of space */
  2183. if (!flag && !artifact_p(j_ptr)) {
  2184. /* Message */
  2185. msg("The %s disappear%s.", o_name, PLURAL(plural));
  2186. /* Debug */
  2187. if (p_ptr->wizard)
  2188. msg("Breakage (no floor space).");
  2189. /* Failure */
  2190. return;
  2191. }
  2192. /* Find a grid */
  2193. for (i = 0; !flag; i++) {
  2194. /* Bounce around */
  2195. if (i < 1000) {
  2196. ty = rand_spread(by, 1);
  2197. tx = rand_spread(bx, 1);
  2198. }
  2199. /* Random locations */
  2200. else {
  2201. ty = randint0(level_hgt);
  2202. tx = randint0(level_wid);
  2203. }
  2204. f_ptr = &f_info[cave_feat[ty][tx]];
  2205. /* Require floor space */
  2206. if (!tf_has(f_ptr->flags, TF_OBJECT))
  2207. continue;
  2208. /* Bounce to that location */
  2209. by = ty;
  2210. bx = tx;
  2211. f_ptr = &f_info[cave_feat[by][bx]];
  2212. /* Okay */
  2213. flag = TRUE;
  2214. }
  2215. /* Give it to the floor */
  2216. if (!floor_carry(by, bx, j_ptr)) {
  2217. artifact_type *a_ptr = artifact_of(j_ptr);
  2218. /* Message */
  2219. msg("The %s disappear%s.", o_name, PLURAL(plural));
  2220. /* Debug */
  2221. if (p_ptr->wizard)
  2222. msg("Breakage (too many objects).");
  2223. if (a_ptr)
  2224. a_ptr->created = FALSE;
  2225. /* Failure */
  2226. return;
  2227. }
  2228. /* Sound */
  2229. sound(MSG_DROP);
  2230. /* Confirm location */
  2231. f_ptr = &f_info[cave_feat[by][bx]];
  2232. /* Message when an object falls under the player */
  2233. if (verbose && (cave_m_idx[by][bx] < 0) && !squelch_item_ok(j_ptr)) {
  2234. msg("You feel something roll beneath your feet.");
  2235. }
  2236. /* Message when an object falls under the player or in trees or rubble */
  2237. else if (tf_has(f_ptr->flags, TF_HIDE_OBJ) && !p_ptr->timed[TMD_BLIND])
  2238. msg("The %s disappear%s from view.", o_name, PLURAL(plural));
  2239. }
  2240. /*
  2241. * Scatter some "great" objects near the player
  2242. */
  2243. void acquirement(int y1, int x1, int num, bool great)
  2244. {
  2245. object_type *i_ptr;
  2246. object_type object_type_body;
  2247. /* Acquirement */
  2248. while (num--) {
  2249. /* Get local object */
  2250. i_ptr = &object_type_body;
  2251. /* Wipe the object */
  2252. object_wipe(i_ptr);
  2253. /* Make a good (or great) object (if possible) */
  2254. if (!make_object(i_ptr, TRUE, great, FALSE))
  2255. continue;
  2256. i_ptr->origin = ORIGIN_ACQUIRE;
  2257. i_ptr->origin_z = chunk_list[p_ptr->stage].z_pos;
  2258. i_ptr->origin_y = chunk_list[p_ptr->stage].y_pos;
  2259. i_ptr->origin_x = chunk_list[p_ptr->stage].x_pos;
  2260. /* Drop the object */
  2261. drop_near(i_ptr, 0, y1, x1, TRUE);
  2262. }
  2263. }
  2264. /*
  2265. * Describe the charges on an item in the inventory.
  2266. */ void inven_item_charges(int item)
  2267. {
  2268. object_type *o_ptr = &p_ptr->inventory[item];
  2269. /* Require staff/wand */
  2270. if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND))
  2271. return;
  2272. /* Require known item */
  2273. if (!object_known_p(o_ptr))
  2274. return;
  2275. /* Print a message */
  2276. msg("You have %d charge%s remaining.", o_ptr->pval,
  2277. (o_ptr->pval != 1) ? "s" : "");
  2278. }
  2279. /*
  2280. * Describe an item in the inventory.
  2281. */ void inven_item_describe(int item)
  2282. {
  2283. object_type *o_ptr = &p_ptr->inventory[item];
  2284. char o_name[80];
  2285. if (artifact_p(o_ptr) && object_known_p(o_ptr)) {
  2286. /* Get a description */
  2287. object_desc(o_name, sizeof(o_name), o_ptr, ODESC_FULL);
  2288. /* Print a message */
  2289. msg("You no longer have the %s (%c).", o_name,
  2290. index_to_label(item));
  2291. } else {
  2292. /* Get a description */
  2293. object_desc(o_name, sizeof(o_name), o_ptr,
  2294. ODESC_PREFIX | ODESC_FULL);
  2295. /* Print a message */
  2296. msg("You have %s (%c).", o_name, index_to_label(item));
  2297. }
  2298. }
  2299. /*
  2300. * Increase the "number" of an item in the inventory
  2301. */
  2302. void inven_item_increase(int item, int num)
  2303. {
  2304. object_type *o_ptr = &p_ptr->inventory[item];
  2305. /* Apply */
  2306. num += o_ptr->number;
  2307. /* Bounds check */
  2308. if (num > 255)
  2309. num = 255;
  2310. else if (num < 0)
  2311. num = 0;
  2312. /* Un-apply */
  2313. num -= o_ptr->number;
  2314. /* Change the number and weight */
  2315. if (num) {
  2316. /* Add the number */
  2317. o_ptr->number += num;
  2318. /* Add the weight */
  2319. p_ptr->total_weight += (num * o_ptr->weight);
  2320. /* Recalculate bonuses */
  2321. p_ptr->update |= (PU_BONUS);
  2322. /* Recalculate mana XXX */
  2323. p_ptr->update |= (PU_MANA);
  2324. /* Combine the pack */
  2325. p_ptr->notice |= (PN_COMBINE);
  2326. /* Redraw stuff */
  2327. p_ptr->redraw |= (PR_INVEN | PR_EQUIP);
  2328. }
  2329. }
  2330. /**
  2331. * Save the size of the quiver.
  2332. */
  2333. void save_quiver_size(struct player *p)
  2334. {
  2335. int i, count = 0;
  2336. for (i = QUIVER_START; i < QUIVER_END; i++)
  2337. if (p->inventory[i].k_idx) {
  2338. if (is_missile(&p->inventory[i]))
  2339. count += p->inventory[i].number;
  2340. else {
  2341. int equiv = p->inventory[i].number * THROWER_AMMO_FACTOR;
  2342. count += equiv;
  2343. if ((QUIVER_SLOT_SIZE - equiv) < THROWER_AMMO_FACTOR)
  2344. count += QUIVER_SLOT_SIZE - equiv;
  2345. }
  2346. }
  2347. p->quiver_size = count;
  2348. p->quiver_slots = (count + QUIVER_SLOT_SIZE - 1) / QUIVER_SLOT_SIZE;
  2349. p->quiver_remainder = count % QUIVER_SLOT_SIZE;
  2350. }
  2351. /**
  2352. * Compare ammunition from slots (0-9); used for sorting.
  2353. *
  2354. * \returns -1 if slot1 should come first, 1 if slot2 should come first, or 0.
  2355. */
  2356. int compare_ammo(int slot1, int slot2)
  2357. {
  2358. object_type *o1 = &p_ptr->inventory[slot1 + QUIVER_START];
  2359. object_type *o2 = &p_ptr->inventory[slot2 + QUIVER_START];
  2360. bool throw1 = !is_missile(o1) && o1->kind;
  2361. bool throw2 = !is_missile(o2) && o2->kind;
  2362. if (throw1 && !throw2)
  2363. return (-1);
  2364. else if (!throw1 && throw2)
  2365. return (1);
  2366. else
  2367. return 0;
  2368. }
  2369. /**
  2370. * Swap ammunition between quiver slots (0-9).
  2371. */
  2372. void swap_quiver_slots(int slot1, int slot2)
  2373. {
  2374. int i = slot1 + QUIVER_START;
  2375. int j = slot2 + QUIVER_START;
  2376. object_type o;
  2377. object_copy(&o, &p_ptr->inventory[i]);
  2378. object_copy(&p_ptr->inventory[i], &p_ptr->inventory[j]);
  2379. object_copy(&p_ptr->inventory[j], &o);
  2380. }
  2381. /**
  2382. * Sorts the quiver--ammunition inscribed with @fN prefers to end up in quiver
  2383. * slot N.
  2384. */
  2385. void sort_quiver(void)
  2386. {
  2387. /* Ammo slots go from 0-9; these indices correspond to the range of
  2388. * (QUIVER_START) - (QUIVER_END-1) in inventory[].
  2389. */
  2390. int locked[QUIVER_SIZE] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  2391. int desired[QUIVER_SIZE] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
  2392. int i, j, k;
  2393. object_type *o_ptr, *o1_ptr;
  2394. /* Here we figure out which slots have inscribed ammo, and whether that
  2395. * ammo is already in the slot it "wants" to be in or not.
  2396. */
  2397. for (i = 0; i < QUIVER_SIZE; i++) {
  2398. bool throwing = FALSE;
  2399. j = QUIVER_START + i;
  2400. o_ptr = &p_ptr->inventory[j];
  2401. throwing = of_has(o_ptr->flags_obj, OF_THROWING);
  2402. /* Skip this slot if it doesn't have ammo */
  2403. if (!o_ptr->k_idx)
  2404. continue;
  2405. /* Figure out which slot this ammo prefers, if any */
  2406. k = get_inscribed_ammo_slot(o_ptr);
  2407. if (!k)
  2408. continue;
  2409. k -= QUIVER_START;
  2410. if (k == i)
  2411. locked[i] = throwing ? 2 : 1;
  2412. if (desired[i] < 0)
  2413. desired[i] = k;
  2414. }
  2415. /* For items which had a preference that was not fulfilled, we will swap
  2416. * them into the slot as long as it isn't already locked.
  2417. */
  2418. for (i = 0; i < QUIVER_SIZE; i++) {
  2419. bool throwing = FALSE;
  2420. int old_lock = -1;
  2421. /* Doesn't want to move */
  2422. if (desired[i] < 0)
  2423. continue;
  2424. /* Throwing weapons can replace ammo */
  2425. if (locked[desired[i]]) {
  2426. o_ptr = &p_ptr->inventory[QUIVER_START + i];
  2427. throwing = of_has(o_ptr->flags_obj, OF_THROWING);
  2428. o1_ptr = &p_ptr->inventory[QUIVER_START + desired[i]];
  2429. /* Break the lock */
  2430. if (!of_has(o1_ptr->flags_obj, OF_THROWING) && throwing) {
  2431. old_lock = locked[desired[i]];
  2432. locked[desired[i]] = 0;
  2433. }
  2434. }
  2435. /* Still locked, can't move */
  2436. if (locked[desired[i]])
  2437. continue;
  2438. /* item in slot 'desired[i]' desires to be in slot 'i' */
  2439. swap_quiver_slots(desired[i], i);
  2440. locked[desired[i]] = throwing ? 2 : 1;
  2441. locked[i] = old_lock;
  2442. }
  2443. /* Now we need to compact ammo which isn't in a preferrred slot towards the
  2444. * "front" of the quiver */
  2445. for (i = 0; i < QUIVER_SIZE; i++) {
  2446. /* If the slot isn't empty, skip it */
  2447. if (p_ptr->inventory[QUIVER_START + i].k_idx)
  2448. continue;
  2449. /* Start from the end and find an unlocked item to put here. */
  2450. for (j = QUIVER_SIZE - 1; j > i; j--) {
  2451. if (!p_ptr->inventory[QUIVER_START + j].k_idx || locked[j])
  2452. continue;
  2453. swap_quiver_slots(i, j);
  2454. break;
  2455. }
  2456. }
  2457. /* Now we will sort all other ammo using a simple insertion sort */
  2458. for (i = 0; i < QUIVER_SIZE; i++) {
  2459. k = i;
  2460. if (!locked[k])
  2461. for (j = i + 1; j < QUIVER_SIZE; j++)
  2462. if (!locked[j] && compare_ammo(k, j) > 0)
  2463. swap_quiver_slots(j, k);
  2464. }
  2465. }
  2466. /*
  2467. * Shifts ammo at or above the item slot towards the end of the quiver, making
  2468. * room for a new piece of ammo.
  2469. */
  2470. void open_quiver_slot(int slot)
  2471. {
  2472. int i, pref;
  2473. int dest = QUIVER_END - 1;
  2474. /* This should only be used on ammunition */
  2475. if (slot < QUIVER_START)
  2476. return;
  2477. /* Quiver is full */
  2478. if (p_ptr->inventory[QUIVER_END - 1].k_idx)
  2479. return;
  2480. /* Find the first open quiver slot */
  2481. while (p_ptr->inventory[dest].k_idx)
  2482. dest++;
  2483. /* Swap things with the space one higher (essentially moving the open space
  2484. * towards our goal slot. */
  2485. for (i = dest - 1; i >= slot; i--) {
  2486. /* If we have an item with an inscribed location (and it's in */
  2487. /* that location) then we won't move it. */
  2488. pref = get_inscribed_ammo_slot(&p_ptr->inventory[i]);
  2489. if (i != slot && pref && pref == i)
  2490. continue;
  2491. /* Copy the item up and wipe the old slot */
  2492. COPY(&p_ptr->inventory[dest], &p_ptr->inventory[i], object_type);
  2493. dest = i;
  2494. object_wipe(&p_ptr->inventory[dest]);
  2495. }
  2496. }
  2497. /**
  2498. * Erase an inventory slot if it has no more items
  2499. */
  2500. void inven_item_optimize(int item)
  2501. {
  2502. object_type *o_ptr = &p_ptr->inventory[item];
  2503. int i, j, slot, limit;
  2504. /* Save a possibly new quiver size */
  2505. if (item >= QUIVER_START)
  2506. save_quiver_size(p_ptr);
  2507. /* Only optimize real items which are empty */
  2508. if (!o_ptr->k_idx || o_ptr->number)
  2509. return;
  2510. /* Items in the pack are treated differently from other items */
  2511. if (item < INVEN_WIELD) {
  2512. p_ptr->inven_cnt--;
  2513. p_ptr->redraw |= PR_INVEN;
  2514. limit = INVEN_MAX_PACK;
  2515. }
  2516. /* Items in the quiver and equipped items are (mostly) treated similarly */
  2517. else {
  2518. p_ptr->equip_cnt--;
  2519. p_ptr->redraw |= PR_EQUIP;
  2520. limit = item >= QUIVER_START ? QUIVER_END : 0;
  2521. }
  2522. /* If the item is equipped (but not in the quiver), there is no need to */
  2523. /* slide other items. Bonuses and such will need to be recalculated */
  2524. if (!limit) {
  2525. /* Erase the empty slot */
  2526. object_wipe(&p_ptr->inventory[item]);
  2527. /* Recalculate stuff */
  2528. p_ptr->update |= (PU_BONUS);
  2529. p_ptr->update |= (PU_TORCH);
  2530. p_ptr->update |= (PU_MANA);
  2531. return;
  2532. }
  2533. /* Slide everything down */
  2534. for (j = item, i = item + 1; i < limit; i++) {
  2535. if (limit == QUIVER_END && p_ptr->inventory[i].kind) {
  2536. /* If we have an item with an inscribed location (and it's in */
  2537. /* that location) then we won't move it. */
  2538. slot = get_inscribed_ammo_slot(&p_ptr->inventory[i]);
  2539. if (slot && slot == i)
  2540. continue;
  2541. }
  2542. COPY(&p_ptr->inventory[j], &p_ptr->inventory[i], object_type);
  2543. j = i;
  2544. }
  2545. /* Reorder the quiver if necessary */
  2546. if (item >= QUIVER_START)
  2547. sort_quiver();
  2548. /* Wipe the left-over object on the end */
  2549. object_wipe(&p_ptr->inventory[j]);
  2550. /* Inventory has changed, so disable repeat command */
  2551. cmd_disable_repeat();
  2552. }
  2553. /*
  2554. * Describe the charges on an item on the floor.
  2555. */
  2556. void floor_item_charges(int item)
  2557. {
  2558. object_type *o_ptr = &o_list[item];
  2559. /* Require staff/wand */
  2560. if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND))
  2561. return;
  2562. /* Require known item */
  2563. if (!object_known_p(o_ptr))
  2564. return;
  2565. /* Print a message */
  2566. msg("There are %d charge%s remaining.", o_ptr->pval,
  2567. (o_ptr->pval != 1) ? "s" : "");
  2568. }
  2569. /*
  2570. * Describe an item in the inventory.
  2571. */ void floor_item_describe(int item)
  2572. {
  2573. object_type *o_ptr = &o_list[item];
  2574. char o_name[80];
  2575. /* Get a description */
  2576. object_desc(o_name, sizeof(o_name), o_ptr, ODESC_PREFIX | ODESC_FULL);
  2577. /* Print a message */
  2578. msg("You see %s.", o_name);
  2579. }
  2580. /*
  2581. * Increase the "number" of an item on the floor
  2582. */ void floor_item_increase(int item, int num)
  2583. {
  2584. object_type *o_ptr = &o_list[item];
  2585. /* Apply */
  2586. num += o_ptr->number;
  2587. /* Bounds check */
  2588. if (num > 255)
  2589. num = 255;
  2590. else if (num < 0)
  2591. num = 0;
  2592. /* Un-apply */
  2593. num -= o_ptr->number;
  2594. /* Change the number */
  2595. o_ptr->number += num;
  2596. }
  2597. /*
  2598. * Optimize an item on the floor (destroy "empty" items)
  2599. */ void floor_item_optimize(int item)
  2600. {
  2601. object_type *o_ptr = &o_list[item];
  2602. /* Paranoia -- be sure it exists */
  2603. if (!o_ptr->k_idx)
  2604. return;
  2605. /* Only optimize empty items */
  2606. if (o_ptr->number)
  2607. return;
  2608. /* Delete the object */
  2609. delete_object_idx(item);
  2610. }
  2611. /**
  2612. * Check if we have space for an item in the pack without overflow
  2613. */
  2614. bool inven_carry_okay(const object_type * o_ptr)
  2615. {
  2616. /* Empty slot? */
  2617. if (p_ptr->inven_cnt < INVEN_MAX_PACK)
  2618. return TRUE;
  2619. /* Check if it can stack */
  2620. if (inven_stack_okay(o_ptr))
  2621. return TRUE;
  2622. /* Nope */
  2623. return (FALSE);
  2624. }
  2625. /*
  2626. * Check to see if an item is stackable in the inventory
  2627. */
  2628. bool inven_stack_okay(const object_type * o_ptr)
  2629. {
  2630. /* Similar slot? */
  2631. int j;
  2632. /* If our pack is full and we're adding too many missiles, there won't be
  2633. * enough room in the quiver, so don't check it. */
  2634. int limit;
  2635. if (!pack_is_full())
  2636. /* The pack has more room */
  2637. limit = ALL_INVEN_TOTAL;
  2638. else if (p_ptr->quiver_remainder == 0)
  2639. /* Quiver already maxed out */
  2640. limit = INVEN_PACK;
  2641. else if (p_ptr->quiver_remainder + o_ptr->number > 99)
  2642. /* Too much new ammo */
  2643. limit = INVEN_PACK;
  2644. else
  2645. limit = ALL_INVEN_TOTAL;
  2646. for (j = 0; j < limit; j++) {
  2647. object_type *j_ptr = &p_ptr->inventory[j];
  2648. /* Skip equipped items and non-objects */
  2649. if (j >= INVEN_PACK && j < QUIVER_START)
  2650. continue;
  2651. if (!j_ptr->k_idx)
  2652. continue;
  2653. /* Check if the two items can be combined */
  2654. if (object_similar(j_ptr, o_ptr, OSTACK_PACK))
  2655. return (TRUE);
  2656. }
  2657. return (FALSE);
  2658. }
  2659. /*
  2660. * Add an item to the players inventory, and return the slot used.
  2661. *
  2662. * If the new item can combine with an existing item in the inventory,
  2663. * it will do so, using "object_similar()" and "object_absorb()", else,
  2664. * the item will be placed into the "proper" location in the inventory.
  2665. *
  2666. * This function can be used to "over-fill" the player's pack, but only
  2667. * once, and such an action must trigger the "overflow" code immediately.
  2668. * Note that when the pack is being "over-filled", the new item must be
  2669. * placed into the "overflow" slot, and the "overflow" must take place
  2670. * before the pack is reordered, but (optionally) after the pack is
  2671. * combined. This may be tricky. See "dungeon.c" for info.
  2672. *
  2673. * Note that this code must remove any location/stack information
  2674. * from the object once it is placed into the inventory.
  2675. */
  2676. s16b inven_carry(struct player * p, struct object * o)
  2677. {
  2678. int i, j, k;
  2679. int n = -1;
  2680. object_type *j_ptr;
  2681. /* Apply an autoinscription */
  2682. apply_autoinscription(o);
  2683. /* Check for combining */
  2684. for (j = 0; j < INVEN_PACK; j++) {
  2685. j_ptr = &p->inventory[j];
  2686. /* Skip non-objects */
  2687. if (!j_ptr->k_idx)
  2688. continue;
  2689. /* Hack -- track last item */
  2690. n = j;
  2691. /* Check if the two items can be combined */
  2692. if (object_similar(j_ptr, o, OSTACK_PACK)) {
  2693. /* Combine the items */
  2694. object_absorb(j_ptr, o);
  2695. /* Increase the weight */
  2696. p->total_weight += (o->number * o->weight);
  2697. /* Recalculate bonuses */
  2698. p->update |= (PU_BONUS);
  2699. /* Redraw stuff */
  2700. p->redraw |= (PR_INVEN);
  2701. /* Save quiver size */
  2702. save_quiver_size(p);
  2703. /* Success */
  2704. return (j);
  2705. }
  2706. }
  2707. /* Paranoia */ if (p->inven_cnt > INVEN_MAX_PACK)
  2708. return (-1);
  2709. /* Find an empty slot */
  2710. for (j = 0; j <= INVEN_MAX_PACK; j++) {
  2711. j_ptr = &p->inventory[j];
  2712. /* Use it if found */
  2713. if (!j_ptr->k_idx)
  2714. break;
  2715. }
  2716. /* Use that slot */
  2717. i = j;
  2718. /* Reorder the pack */
  2719. if (i < INVEN_MAX_PACK) {
  2720. s32b o_value, j_value;
  2721. /* Get the "value" of the item */
  2722. o_value = object_value(o);
  2723. /* Scan every occupied slot */
  2724. for (j = 0; j < INVEN_MAX_PACK; j++) {
  2725. j_ptr = &p->inventory[j];
  2726. /* Use empty slots */
  2727. if (!j_ptr->k_idx)
  2728. break;
  2729. /* Hack -- readable books always come first */
  2730. if ((o->tval == mp_ptr->spell_book)
  2731. && (j_ptr->tval != mp_ptr->spell_book))
  2732. break;
  2733. if ((j_ptr->tval == mp_ptr->spell_book)
  2734. && (o->tval != mp_ptr->spell_book))
  2735. continue;
  2736. /* Objects sort by decreasing type */
  2737. if (o->tval > j_ptr->tval)
  2738. break;
  2739. if (o->tval < j_ptr->tval)
  2740. continue;
  2741. /* Non-aware (flavored) items always come last */
  2742. if (!object_aware_p(o))
  2743. continue;
  2744. if (!object_aware_p(j_ptr))
  2745. break;
  2746. /* Objects sort by increasing sval */
  2747. if (o->sval < j_ptr->sval)
  2748. break;
  2749. if (o->sval > j_ptr->sval)
  2750. continue;
  2751. /* Unidentified objects always come last */
  2752. if (!object_known_p(o))
  2753. continue;
  2754. if (!object_known_p(j_ptr))
  2755. break;
  2756. /* Lights sort by decreasing fuel */
  2757. if (o->tval == TV_LIGHT) {
  2758. if (o->pval > j_ptr->pval)
  2759. break;
  2760. if (o->pval < j_ptr->pval)
  2761. continue;
  2762. }
  2763. /* Determine the "value" of the pack item */
  2764. j_value = object_value(j_ptr);
  2765. /* Objects sort by decreasing value */
  2766. if (o_value > j_value)
  2767. break;
  2768. if (o_value < j_value)
  2769. continue;
  2770. }
  2771. /* Use that slot */
  2772. i = j;
  2773. /* Slide objects */
  2774. for (k = n; k >= i; k--) {
  2775. /* Hack -- Slide the item */
  2776. object_copy(&p->inventory[k + 1], &p->inventory[k]);
  2777. }
  2778. /* Wipe the empty slot */
  2779. object_wipe(&p->inventory[i]);
  2780. }
  2781. object_copy(&p->inventory[i], o);
  2782. j_ptr = &p->inventory[i];
  2783. j_ptr->next_o_idx = 0;
  2784. j_ptr->held_m_idx = 0;
  2785. j_ptr->iy = j_ptr->ix = 0;
  2786. j_ptr->marked = FALSE;
  2787. p->total_weight += (j_ptr->number * j_ptr->weight);
  2788. p->inven_cnt++;
  2789. p->update |= (PU_BONUS);
  2790. p->notice |= (PN_COMBINE | PN_REORDER);
  2791. p->redraw |= (PR_INVEN);
  2792. /* Save quiver size */
  2793. save_quiver_size(p);
  2794. /* Return the slot */
  2795. return (i);
  2796. }
  2797. /*
  2798. * Take off (some of) a non-cursed equipment item
  2799. *
  2800. * Note that only one item at a time can be wielded per slot.
  2801. *
  2802. * Note that taking off an item when "full" may cause that item
  2803. * to fall to the ground.
  2804. *
  2805. * Return the inventory slot into which the item is placed.
  2806. */
  2807. s16b inven_takeoff(int item, int amt)
  2808. {
  2809. int slot;
  2810. object_type *o_ptr;
  2811. object_type *i_ptr;
  2812. object_type object_type_body;
  2813. const char *act;
  2814. char o_name[80];
  2815. /* Get the item to take off */
  2816. o_ptr = &p_ptr->inventory[item];
  2817. /* Paranoia */
  2818. if (amt <= 0)
  2819. return (-1);
  2820. /* Verify */
  2821. if (amt > o_ptr->number)
  2822. amt = o_ptr->number;
  2823. /* Check to see if a Set Item is being removed */
  2824. if (o_ptr->name1) {
  2825. artifact_type *a_ptr = &a_info[o_ptr->name1];
  2826. if (a_ptr->set_no != 0) {
  2827. /* The object is part of a set. Check to see if rest of set is
  2828. * equiped and if so remove bonuses -GS- */
  2829. if (check_set(a_ptr->set_no)) {
  2830. remove_set(a_ptr->set_no);
  2831. }
  2832. }
  2833. }
  2834. /* Get local object */
  2835. i_ptr = &object_type_body;
  2836. /* Obtain a local object */
  2837. object_copy(i_ptr, o_ptr);
  2838. /* Modify quantity */
  2839. i_ptr->number = amt;
  2840. /* Describe the object */
  2841. object_desc(o_name, sizeof(o_name), i_ptr, ODESC_PREFIX | ODESC_FULL);
  2842. /* Took off weapon */
  2843. if (item == INVEN_WIELD) {
  2844. act = "You were wielding";
  2845. }
  2846. /* Took off bow */
  2847. else if (item == INVEN_BOW) {
  2848. act = "You were holding";
  2849. }
  2850. /* Took off light */
  2851. else if (item == INVEN_LIGHT) {
  2852. act = "You were holding";
  2853. }
  2854. /* Took off something */
  2855. else {
  2856. act = "You were wearing";
  2857. }
  2858. /* Modify, Optimize */
  2859. inven_item_increase(item, -amt);
  2860. inven_item_optimize(item);
  2861. /* Carry the object */
  2862. slot = inven_carry(p_ptr, i_ptr);
  2863. /* Message */
  2864. msgt(MSG_WIELD, "%s %s (%c).", act, o_name, index_to_label(slot));
  2865. p_ptr->notice |= PN_SQUELCH;
  2866. /* Return slot */
  2867. return (slot);
  2868. }
  2869. /*
  2870. * Drop (some of) a non-cursed inventory/equipment item
  2871. *
  2872. * The object will be dropped "near" the current location
  2873. */
  2874. void inven_drop(int item, int amt)
  2875. {
  2876. int py = p_ptr->py;
  2877. int px = p_ptr->px;
  2878. object_type *o_ptr;
  2879. object_type *i_ptr;
  2880. object_type object_type_body;
  2881. char o_name[80];
  2882. /* Get the original object */
  2883. o_ptr = &p_ptr->inventory[item];
  2884. /* Error check */
  2885. if (amt <= 0)
  2886. return;
  2887. /* Not too many */
  2888. if (amt > o_ptr->number)
  2889. amt = o_ptr->number;
  2890. /* Take off equipment */
  2891. if (item >= INVEN_WIELD) {
  2892. /* Take off first */
  2893. item = inven_takeoff(item, amt);
  2894. /* Get the original object */
  2895. o_ptr = &p_ptr->inventory[item];
  2896. }
  2897. /* Get local object */
  2898. i_ptr = &object_type_body;
  2899. /* Obtain local object */
  2900. object_copy(i_ptr, o_ptr);
  2901. /* Distribute charges of wands, staves, or rods */
  2902. distribute_charges(o_ptr, i_ptr, amt);
  2903. /* Modify quantity */
  2904. i_ptr->number = amt;
  2905. /* Describe local object */
  2906. object_desc(o_name, sizeof(o_name), i_ptr, ODESC_PREFIX | ODESC_FULL);
  2907. /* Message */
  2908. msg("You drop %s (%c).", o_name, index_to_label(item));
  2909. /* Drop it near the player */
  2910. drop_near(i_ptr, 0, py, px, FALSE);
  2911. /* Modify, Describe, Optimize */
  2912. inven_item_increase(item, -amt);
  2913. inven_item_describe(item);
  2914. inven_item_optimize(item);
  2915. }
  2916. /*
  2917. * Combine items in the pack
  2918. * Also "pick up" any gold in the inventory by accident
  2919. *
  2920. * Note special handling of the "overflow" slot
  2921. */
  2922. void combine_pack(void)
  2923. {
  2924. int i, j, k;
  2925. object_type *o_ptr;
  2926. object_type *j_ptr;
  2927. bool flag = FALSE;
  2928. /* Combine the pack (backwards) */
  2929. for (i = INVEN_PACK; i > 0; i--) {
  2930. bool slide = FALSE;
  2931. /* Get the item */
  2932. o_ptr = &p_ptr->inventory[i];
  2933. /* Skip empty items */
  2934. if (!o_ptr->k_idx)
  2935. continue;
  2936. /* Absorb gold */
  2937. if (o_ptr->tval == TV_GOLD) {
  2938. /* Count the gold */
  2939. slide = TRUE;
  2940. p_ptr->au += o_ptr->pval;
  2941. }
  2942. /* Scan the items above that item */
  2943. else
  2944. for (j = 0; j < i; j++) {
  2945. /* Get the item */
  2946. j_ptr = &p_ptr->inventory[j];
  2947. /* Skip empty items */
  2948. if (!j_ptr->k_idx)
  2949. continue;
  2950. /* Can we drop "o_ptr" onto "j_ptr"? */
  2951. if (object_similar(j_ptr, o_ptr, OSTACK_PACK)) {
  2952. /* Take note */
  2953. flag = slide = TRUE;
  2954. /* Add together the item counts */
  2955. object_absorb(j_ptr, o_ptr);
  2956. break;
  2957. }
  2958. }
  2959. /* Compact the inventory */
  2960. if (slide) {
  2961. /* One object is gone */
  2962. p_ptr->inven_cnt--;
  2963. /* Slide everything down */
  2964. for (k = i; k < INVEN_PACK; k++) {
  2965. /* Hack -- slide object */
  2966. COPY(&p_ptr->inventory[k], &p_ptr->inventory[k + 1],
  2967. object_type);
  2968. }
  2969. /* Hack -- wipe hole */
  2970. object_wipe(&p_ptr->inventory[k]);
  2971. /* Redraw stuff */
  2972. p_ptr->redraw |= (PR_INVEN);
  2973. }
  2974. }
  2975. /* Message */
  2976. if (flag) {
  2977. msg("You combine some items in your pack.");
  2978. /* Stop "repeat last command" from working. */
  2979. cmd_disable_repeat();
  2980. }
  2981. }
  2982. /*
  2983. * Reorder items in the pack
  2984. *
  2985. * Note special handling of the "overflow" slot
  2986. */
  2987. void reorder_pack(void)
  2988. {
  2989. int i, j, k;
  2990. s32b o_value;
  2991. s32b j_value;
  2992. object_type *o_ptr;
  2993. object_type *j_ptr;
  2994. object_type *i_ptr;
  2995. object_type object_type_body;
  2996. bool flag = FALSE;
  2997. /* Re-order the pack (forwards) */
  2998. for (i = 0; i < INVEN_PACK; i++) {
  2999. /* Get the item */
  3000. o_ptr = &p_ptr->inventory[i];
  3001. /* Skip empty slots */
  3002. if (!o_ptr->k_idx)
  3003. continue;
  3004. /* Get the "value" of the item */
  3005. o_value = object_value(o_ptr);
  3006. /* Scan every occupied slot */
  3007. for (j = 0; j < INVEN_PACK; j++) {
  3008. /* Get the item already there */
  3009. j_ptr = &p_ptr->inventory[j];
  3010. /* Use empty slots */
  3011. if (!j_ptr->k_idx)
  3012. break;
  3013. /* Hack -- readable books always come first */
  3014. if ((o_ptr->tval == mp_ptr->spell_book)
  3015. && (j_ptr->tval != mp_ptr->spell_book))
  3016. break;
  3017. if ((j_ptr->tval == mp_ptr->spell_book)
  3018. && (o_ptr->tval != mp_ptr->spell_book))
  3019. continue;
  3020. /* Objects sort by decreasing type */
  3021. if (o_ptr->tval > j_ptr->tval)
  3022. break;
  3023. if (o_ptr->tval < j_ptr->tval)
  3024. continue;
  3025. /* Non-aware (flavored) items always come last */
  3026. if (!object_aware_p(o_ptr))
  3027. continue;
  3028. if (!object_aware_p(j_ptr))
  3029. break;
  3030. /* Objects sort by increasing sval */
  3031. if (o_ptr->sval < j_ptr->sval)
  3032. break;
  3033. if (o_ptr->sval > j_ptr->sval)
  3034. continue;
  3035. /* Unidentified objects always come last */
  3036. if (!object_known_p(o_ptr))
  3037. continue;
  3038. if (!object_known_p(j_ptr))
  3039. break;
  3040. /* Lights sort by decreasing fuel */
  3041. if (o_ptr->tval == TV_LIGHT) {
  3042. if (o_ptr->pval > j_ptr->pval)
  3043. break;
  3044. if (o_ptr->pval < j_ptr->pval)
  3045. continue;
  3046. }
  3047. /* Determine the "value" of the pack item */
  3048. j_value = object_value(j_ptr);
  3049. /* Objects sort by decreasing value */
  3050. if (o_value > j_value)
  3051. break;
  3052. if (o_value < j_value)
  3053. continue;
  3054. }
  3055. /* Never move down */
  3056. if (j >= i)
  3057. continue;
  3058. /* Take note */
  3059. flag = TRUE;
  3060. /* Get local object */
  3061. i_ptr = &object_type_body;
  3062. /* Save a copy of the moving item */
  3063. object_copy(i_ptr, &p_ptr->inventory[i]);
  3064. /* Slide the objects */
  3065. for (k = i; k > j; k--) {
  3066. /* Slide the item */
  3067. object_copy(&p_ptr->inventory[k], &p_ptr->inventory[k - 1]);
  3068. }
  3069. /* Insert the moving item */
  3070. object_copy(&p_ptr->inventory[j], i_ptr);
  3071. /* Redraw stuff */
  3072. p_ptr->redraw |= (PR_INVEN);
  3073. }
  3074. if (flag) {
  3075. msg("You reorder some items in your pack.");
  3076. /* Stop "repeat last command" from working. */
  3077. cmd_disable_repeat();
  3078. }
  3079. }
  3080. /**
  3081. *Returns the number of times in 1000 that @ will FAIL
  3082. */
  3083. int get_use_device_chance(const object_type * o_ptr)
  3084. {
  3085. int lev;
  3086. /* Base chance of success */
  3087. int chance = p_ptr->state.skills[SKILL_DEVICE];
  3088. /* Final "probability" */
  3089. int prob = 10000;
  3090. /* Extract the item level, which is the difficulty rating */
  3091. if (artifact_p(o_ptr))
  3092. lev = a_info[o_ptr->name1].level;
  3093. else
  3094. lev = k_info[o_ptr->k_idx].level;
  3095. /* Confusion hurts skill */
  3096. if (p_ptr->timed[TMD_CONFUSED])
  3097. chance = chance / 2;
  3098. /* High level objects are harder */
  3099. chance = chance - ((lev > 50) ? 50 : lev);
  3100. /* Calculate the chance */
  3101. if (chance < USE_DEVICE) {
  3102. prob *= 2;
  3103. prob /= (USE_DEVICE * (USE_DEVICE + 1 - chance));
  3104. } else {
  3105. prob -= prob * (USE_DEVICE - 1) / chance;
  3106. }
  3107. prob /= 10;
  3108. return 1000 - prob;
  3109. }
  3110. /**
  3111. * Distribute charges of rods, staves, or wands.
  3112. *
  3113. * o_ptr = source item
  3114. * q_ptr = target item, must be of the same type as o_ptr
  3115. * amt = number of items that are transfered
  3116. */
  3117. void distribute_charges(object_type * o_ptr, object_type * q_ptr, int amt)
  3118. {
  3119. const object_kind *k_ptr = &k_info[o_ptr->k_idx];
  3120. int charge_time = randcalc(k_ptr->time, 0, AVERAGE), max_time;
  3121. /*
  3122. * Hack -- If rods, staves, or wands are dropped, the total maximum
  3123. * timeout or charges need to be allocated between the two stacks.
  3124. * If all the items are being dropped, it makes for a neater message
  3125. * to leave the original stack's pval alone. -LM-
  3126. */
  3127. if ((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_STAFF)) {
  3128. q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
  3129. if (amt < o_ptr->number)
  3130. o_ptr->pval -= q_ptr->pval;
  3131. }
  3132. /*
  3133. * Hack -- Rods also need to have their timeouts distributed.
  3134. *
  3135. * The dropped stack will accept all time remaining to charge up to
  3136. * its maximum.
  3137. */
  3138. if (o_ptr->tval == TV_ROD) {
  3139. max_time = charge_time * amt;
  3140. if (o_ptr->timeout > max_time)
  3141. q_ptr->timeout = max_time;
  3142. else
  3143. q_ptr->timeout = o_ptr->timeout;
  3144. if (amt < o_ptr->number)
  3145. o_ptr->timeout -= q_ptr->timeout;
  3146. }
  3147. }
  3148. void reduce_charges(object_type * o_ptr, int amt)
  3149. {
  3150. /*
  3151. * Hack -- If rods or wand are destroyed, the total maximum timeout or
  3152. * charges of the stack needs to be reduced, unless all the items are
  3153. * being destroyed. -LM-
  3154. */
  3155. if (((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_STAFF))
  3156. && (amt < o_ptr->number)) {
  3157. o_ptr->pval -= o_ptr->pval * amt / o_ptr->number;
  3158. }
  3159. if ((o_ptr->tval == TV_ROD) && (amt < o_ptr->number)) {
  3160. o_ptr->timeout -= o_ptr->timeout * amt / o_ptr->number;
  3161. }
  3162. }
  3163. int number_charging(const object_type * o_ptr)
  3164. {
  3165. int charge_time, num_charging;
  3166. charge_time = randcalc(o_ptr->time, 0, AVERAGE);
  3167. /* Item has no timeout */
  3168. if (charge_time <= 0)
  3169. return 0;
  3170. /* No items are charging */
  3171. if (o_ptr->timeout <= 0)
  3172. return 0;
  3173. /* Calculate number charging based on timeout */
  3174. num_charging = (o_ptr->timeout + charge_time - 1) / charge_time;
  3175. /* Number charging cannot exceed stack size */
  3176. if (num_charging > o_ptr->number)
  3177. num_charging = o_ptr->number;
  3178. return num_charging;
  3179. }
  3180. bool recharge_timeout(object_type * o_ptr)
  3181. {
  3182. int charging_before, charging_after;
  3183. /* Find the number of charging items */
  3184. charging_before = number_charging(o_ptr);
  3185. /* Nothing to charge */
  3186. if (charging_before == 0)
  3187. return FALSE;
  3188. /* Decrease the timeout */
  3189. o_ptr->timeout -= MIN(charging_before, o_ptr->timeout);
  3190. /* Find the new number of charging items */
  3191. charging_after = number_charging(o_ptr);
  3192. /* Return true if at least 1 item obtained a charge */
  3193. if (charging_after < charging_before)
  3194. return TRUE;
  3195. else
  3196. return FALSE;
  3197. }
  3198. /*
  3199. * Looks if "inscrip" is present on the given object.
  3200. */
  3201. unsigned check_for_inscrip(const object_type * o_ptr, const char *inscrip)
  3202. {
  3203. unsigned i = 0;
  3204. const char *s;
  3205. if (!o_ptr->note)
  3206. return 0;
  3207. s = quark_str(o_ptr->note);
  3208. do {
  3209. s = strstr(s, inscrip);
  3210. if (!s)
  3211. break;
  3212. i++;
  3213. s++;
  3214. } while (s);
  3215. return i;
  3216. }
  3217. /*** Object kind lookup functions ***/
  3218. /**
  3219. * Return the k_idx of the object kind with the given `tval` and `sval`, or 0.
  3220. */
  3221. int lookup_kind(int tval, int sval)
  3222. {
  3223. int k;
  3224. /* Look for it */
  3225. for (k = 1; k < z_info->k_max; k++) {
  3226. object_kind *k_ptr = &k_info[k];
  3227. /* Found a match */
  3228. if ((k_ptr->tval == tval) && (k_ptr->sval == sval))
  3229. return (k);
  3230. }
  3231. /* Failure */
  3232. msg("No object (%s,%d)", tval_find_name(tval), tval, sval);
  3233. return 0;
  3234. }
  3235. struct object_kind *objkind_get(int tval, int sval)
  3236. {
  3237. int k = lookup_kind(tval, sval);
  3238. return &k_info[k];
  3239. }
  3240. /**
  3241. * Find the tval and sval of object kind `k_idx`, and return via the pointers
  3242. * `tval` and `sval`.
  3243. */ bool lookup_reverse(s16b k_idx, int *tval, int *sval)
  3244. {
  3245. object_kind *k_ptr;
  3246. /* Validate k_idx */
  3247. if ((k_idx < 1) || (k_idx > z_info->k_max))
  3248. return FALSE;
  3249. /* Get pointer */
  3250. k_ptr = &k_info[k_idx];
  3251. *tval = k_ptr->tval;
  3252. *sval = k_ptr->sval;
  3253. /* Done */
  3254. return TRUE;
  3255. }
  3256. /*** Textual<->numeric conversion ***/
  3257. /**
  3258. * List of { tval, name } pairs.
  3259. */
  3260. static const grouper tval_names[] = {
  3261. {TV_SKELETON, "skeleton"},
  3262. {TV_BOTTLE, "bottle"},
  3263. {TV_JUNK, "junk"},
  3264. {TV_SPIKE, "spike"},
  3265. {TV_CHEST, "chest"},
  3266. {TV_SHOT, "shot"},
  3267. {TV_ARROW, "arrow"},
  3268. {TV_BOLT, "bolt"},
  3269. {TV_BOW, "bow"},
  3270. {TV_DIGGING, "digger"},
  3271. {TV_HAFTED, "hafted"},
  3272. {TV_POLEARM, "polearm"},
  3273. {TV_SWORD, "sword"},
  3274. {TV_BOOTS, "boots"},
  3275. {TV_GLOVES, "gloves"},
  3276. {TV_HELM, "helm"},
  3277. {TV_CROWN, "crown"},
  3278. {TV_SHIELD, "shield"},
  3279. {TV_CLOAK, "cloak"},
  3280. {TV_SOFT_ARMOR, "soft armor"},
  3281. {TV_SOFT_ARMOR, "soft armour"},
  3282. {TV_HARD_ARMOR, "hard armor"},
  3283. {TV_HARD_ARMOR, "hard armour"},
  3284. {TV_DRAG_ARMOR, "dragon armor"},
  3285. {TV_DRAG_ARMOR, "dragon armour"},
  3286. {TV_LIGHT, "light"},
  3287. {TV_AMULET, "amulet"},
  3288. {TV_RING, "ring"},
  3289. {TV_STAFF, "staff"},
  3290. {TV_WAND, "wand"},
  3291. {TV_ROD, "rod"},
  3292. {TV_SCROLL, "scroll"},
  3293. {TV_POTION, "potion"},
  3294. {TV_FLASK, "flask"},
  3295. {TV_FOOD, "food"},
  3296. {TV_MAGIC_BOOK, "magic book"},
  3297. {TV_PRAYER_BOOK, "prayer book"},
  3298. {TV_DRUID_BOOK, "stone of lore"},
  3299. {TV_NECRO_BOOK, "necromantic tome"},
  3300. {TV_GOLD, "gold"},
  3301. };
  3302. /**
  3303. * Return the k_idx of the object kind with the given `tval` and name `name`.
  3304. */
  3305. int lookup_name(int tval, const char *name)
  3306. {
  3307. int k;
  3308. /* Look for it */
  3309. for (k = 1; k < z_info->k_max; k++) {
  3310. object_kind *k_ptr = &k_info[k];
  3311. const char *nm = k_ptr->name;
  3312. if (!nm)
  3313. continue;
  3314. if (*nm == '&' && *(nm + 1))
  3315. nm += 2;
  3316. /* Found a match */
  3317. if (k_ptr->tval == tval && !strcmp(name, nm))
  3318. return k;
  3319. }
  3320. msg("No object (\"%s\",\"%s\")", tval_find_name(tval), name);
  3321. return -1;
  3322. }
  3323. /**
  3324. * Return the a_idx of the artifact with the given name
  3325. */
  3326. int lookup_artifact_name(const char *name)
  3327. {
  3328. int i;
  3329. /* Look for it */
  3330. for (i = 1; i < z_info->a_max; i++) {
  3331. artifact_type *a_ptr = &a_info[i];
  3332. /* Found a match */
  3333. if (a_ptr->name && streq(name, a_ptr->name))
  3334. return i;
  3335. }
  3336. return -1;
  3337. }
  3338. /**
  3339. * Return the numeric sval of the object kind with the given `tval` and name `name`.
  3340. */
  3341. int lookup_sval(int tval, const char *name)
  3342. {
  3343. int k;
  3344. unsigned int r;
  3345. if (sscanf(name, "%u", &r) == 1)
  3346. return r;
  3347. /* Look for it */
  3348. for (k = 1; k < z_info->k_max; k++) {
  3349. object_kind *k_ptr = &k_info[k];
  3350. const char *nm = k_ptr->name;
  3351. if (!nm)
  3352. continue;
  3353. if (*nm == '&' && *(nm + 1))
  3354. nm += 2;
  3355. /* Found a match */
  3356. if (k_ptr->tval == tval && !strcmp(name, nm))
  3357. return k_ptr->sval;
  3358. }
  3359. return -1;
  3360. }
  3361. /**
  3362. * Returns the numeric equivalent tval of the textual tval `name`.
  3363. */
  3364. int tval_find_idx(const char *name)
  3365. {
  3366. size_t i = 0;
  3367. unsigned int r;
  3368. if (sscanf(name, "%u", &r) == 1)
  3369. return r;
  3370. for (i = 0; i < N_ELEMENTS(tval_names); i++) {
  3371. if (!my_stricmp(name, tval_names[i].name))
  3372. return tval_names[i].tval;
  3373. }
  3374. return -1;
  3375. }
  3376. /**
  3377. * Returns the textual equivalent tval of the numeric tval `name`.
  3378. */
  3379. const char *tval_find_name(int tval)
  3380. {
  3381. size_t i = 0;
  3382. for (i = 0; i < N_ELEMENTS(tval_names); i++) {
  3383. if (tval == tval_names[i].tval)
  3384. return tval_names[i].name;
  3385. }
  3386. return "unknown";
  3387. }
  3388. /**
  3389. * Sort comparator for objects using only tval and sval.
  3390. * -1 if o1 should be first
  3391. * 1 if o2 should be first
  3392. * 0 if it doesn't matter
  3393. */
  3394. static int compare_types(const object_type * o1, const object_type * o2)
  3395. {
  3396. if (o1->tval == o2->tval)
  3397. return CMP(o1->sval, o2->sval);
  3398. else
  3399. return CMP(o1->tval, o2->tval);
  3400. }
  3401. /* some handy macros for sorting */
  3402. #define object_is_worthless(o) (object_value(o) == 0)
  3403. /**
  3404. * Sort comparator for objects
  3405. * -1 if o1 should be first
  3406. * 1 if o2 should be first
  3407. * 0 if it doesn't matter
  3408. *
  3409. * The sort order is designed with the "list items" command in mind.
  3410. */ static int compare_items(const object_type * o1,
  3411. const object_type * o2)
  3412. {
  3413. /* known artifacts will sort first */
  3414. if (object_known_p(o1) && object_known_p(o2) && artifact_p(o1) &&
  3415. artifact_p(o2))
  3416. return compare_types(o1, o2);
  3417. if (object_known_p(o1) && artifact_p(o1))
  3418. return -1;
  3419. if (object_known_p(o2) && artifact_p(o2))
  3420. return 1;
  3421. /* unknown objects will sort next */
  3422. if (!object_aware_p(o1) && !object_aware_p(o2))
  3423. return compare_types(o1, o2);
  3424. if (!object_aware_p(o1))
  3425. return -1;
  3426. if (!object_aware_p(o2))
  3427. return 1;
  3428. /* if only one of them is worthless, the other comes first */
  3429. if (object_is_worthless(o1) && !object_is_worthless(o2))
  3430. return 1;
  3431. if (!object_is_worthless(o1) && object_is_worthless(o2))
  3432. return -1;
  3433. /* otherwise, just compare tvals and svals */
  3434. /* NOTE: arguably there could be a better order than this */
  3435. return compare_types(o1, o2);
  3436. }
  3437. /**
  3438. * Helper to draw the Object Recall subwindow; this actually does the work.
  3439. */ void display_object_recall(object_type * o_ptr)
  3440. {
  3441. char header[120];
  3442. textblock *tb = object_info(o_ptr, OINFO_NONE);
  3443. object_desc(header, sizeof(header), o_ptr, ODESC_PREFIX | ODESC_FULL);
  3444. clear_from(0);
  3445. textui_textblock_place(tb, SCREEN_REGION, header);
  3446. textblock_free(tb);
  3447. }
  3448. /**
  3449. * This draws the Object Recall subwindow when displaying a particular object
  3450. * (e.g. a helmet in the backpack, or a scroll on the ground)
  3451. */
  3452. void display_object_idx_recall(s16b item)
  3453. {
  3454. object_type *o_ptr = object_from_item_idx(item);
  3455. display_object_recall(o_ptr);
  3456. }
  3457. /**
  3458. * This draws the Object Recall subwindow when displaying a recalled item kind
  3459. * (e.g. a generic ring of acid or a generic blade of chaos)
  3460. */
  3461. void display_object_kind_recall(s16b k_idx)
  3462. {
  3463. object_type object = { 0 };
  3464. object_prep(&object, k_idx, EXTREMIFY);
  3465. display_object_recall(&object);
  3466. }
  3467. /*
  3468. * Display visible items, similar to display_monlist
  3469. */
  3470. void display_itemlist(void)
  3471. {
  3472. int max;
  3473. int mx, my;
  3474. unsigned num;
  3475. int line = 1, x = 0;
  3476. int cur_x;
  3477. unsigned i;
  3478. unsigned disp_count = 0;
  3479. byte a;
  3480. wchar_t c;
  3481. object_type *types[MAX_ITEMLIST];
  3482. int counts[MAX_ITEMLIST];
  3483. int dx[MAX_ITEMLIST], dy[MAX_ITEMLIST];
  3484. unsigned counter = 0;
  3485. int dungeon_hgt = ARENA_HGT;
  3486. int dungeon_wid = ARENA_WID;
  3487. byte attr;
  3488. char buf[80];
  3489. int floor_list[MAX_FLOOR_STACK];
  3490. /* Clear the term if in a subwindow, set x otherwise */
  3491. if (Term != angband_term[0]) {
  3492. clear_from(0);
  3493. max = Term->hgt - 1;
  3494. } else {
  3495. x = 13;
  3496. max = Term->hgt - 2;
  3497. }
  3498. /* Look at each square of the dungeon for items */
  3499. for (my = 0; my < dungeon_hgt; my++) {
  3500. for (mx = 0; mx < dungeon_wid; mx++) {
  3501. num = scan_floor(floor_list, MAX_FLOOR_STACK, my, mx, 0x02);
  3502. /* Iterate over all the items found on this square */
  3503. for (i = 0; i < num; i++) {
  3504. object_type *o_ptr = &o_list[floor_list[i]];
  3505. unsigned j;
  3506. /* Skip gold/squelched */
  3507. if (o_ptr->tval == TV_GOLD || squelch_hide_item(o_ptr))
  3508. continue;
  3509. /* See if we've already seen a similar item; if so, just add */
  3510. /* to its count */
  3511. for (j = 0; j < counter; j++) {
  3512. if (object_similar(o_ptr, types[j], OSTACK_LIST)) {
  3513. counts[j] += o_ptr->number;
  3514. if ((my - p_ptr->py) * (my - p_ptr->py) +
  3515. (mx - p_ptr->px) * (mx - p_ptr->px) <
  3516. dy[j] * dy[j] + dx[j] * dx[j]) {
  3517. dy[j] = my - p_ptr->py;
  3518. dx[j] = mx - p_ptr->px;
  3519. }
  3520. break;
  3521. }
  3522. }
  3523. /* We saw a new item. So insert it at the end of the list and */
  3524. /* then sort it forward using compare_items(). The types list */
  3525. /* is always kept sorted. */
  3526. /* If we have too many items, replace the last (normally least important) */
  3527. /* item in the list. */
  3528. if (j == counter) {
  3529. if (counter == MAX_ITEMLIST) {
  3530. counter -= 1;
  3531. j -= 1;
  3532. }
  3533. types[counter] = o_ptr;
  3534. counts[counter] = o_ptr->number;
  3535. dy[counter] = my - p_ptr->py;
  3536. dx[counter] = mx - p_ptr->px;
  3537. while (j > 0
  3538. && compare_items(types[j - 1], types[j]) > 0) {
  3539. object_type *tmp_o = types[j - 1];
  3540. int tmpcount;
  3541. int tmpdx = dx[j - 1];
  3542. int tmpdy = dy[j - 1];
  3543. types[j - 1] = types[j];
  3544. types[j] = tmp_o;
  3545. dx[j - 1] = dx[j];
  3546. dx[j] = tmpdx;
  3547. dy[j - 1] = dy[j];
  3548. dy[j] = tmpdy;
  3549. tmpcount = counts[j - 1];
  3550. counts[j - 1] = counts[j];
  3551. counts[j] = tmpcount;
  3552. j--;
  3553. }
  3554. counter++;
  3555. }
  3556. }
  3557. }
  3558. }
  3559. /* Note no visible items */
  3560. if (!counter) {
  3561. /* Clear display and print note */
  3562. c_prt(TERM_SLATE, "You see no items.", 0, 0);
  3563. if (Term == angband_term[0])
  3564. Term_addstr(-1, TERM_WHITE, " (Press any key to continue.)");
  3565. /* Done */
  3566. return;
  3567. } else if (counter == MAX_ITEMLIST) {
  3568. c_prt(TERM_SLATE, "You see many items.", 0, 0);
  3569. } else {
  3570. /* Reprint Message */
  3571. prt(format
  3572. ("You can see %d item%s:", counter, (counter > 1 ? "s" : "")),
  3573. 0, 0);
  3574. }
  3575. for (i = 0; i < counter; i++) {
  3576. /* o_name will hold the object_desc() name for the object. */
  3577. /* o_desc will also need to put a (x4) behind it. */
  3578. /* can there be more than 999 stackable items on a level? */
  3579. char o_name[80];
  3580. char o_desc[86];
  3581. object_type *o_ptr = types[i];
  3582. /* We shouldn't list coins or squelched items */
  3583. if (o_ptr->tval == TV_GOLD || squelch_hide_item(o_ptr))
  3584. continue;
  3585. object_desc(o_name, sizeof(o_name), o_ptr, ODESC_FULL);
  3586. if (counts[i] > 1)
  3587. strnfmt(o_desc, sizeof(o_desc), "%s (x%d) %d %c, %d %c",
  3588. o_name, counts[i], (dy[i] > 0) ? dy[i] : -dy[i],
  3589. (dy[i] > 0) ? 'S' : 'N', (dx[i] > 0) ? dx[i] : -dx[i],
  3590. (dx[i] > 0) ? 'E' : 'W');
  3591. else
  3592. strnfmt(o_desc, sizeof(o_desc), "%s %d %c %d %c", o_name,
  3593. (dy[i] > 0) ? dy[i] : -dy[i], (dy[i] > 0) ? 'S' : 'N',
  3594. (dx[i] > 0) ? dx[i] : -dx[i], (dx[i] > 0) ? 'E' : 'W');
  3595. /* Reset position */
  3596. cur_x = x;
  3597. /* See if we need to scroll or not */
  3598. if (Term == angband_term[0] && (line == max)
  3599. && disp_count != counter) {
  3600. prt("-- more --", line, x);
  3601. anykey();
  3602. /* Clear the screen */
  3603. for (line = 1; line <= max; line++)
  3604. prt("", line, x);
  3605. /* Reprint Message */
  3606. if (counter == MAX_ITEMLIST) {
  3607. prt(format("You can see many items:"), 0, 0);
  3608. } else {
  3609. prt(format
  3610. ("You can see %d item%s:", counter,
  3611. (counter > 1 ? "s" : "")), 0, 0);
  3612. }
  3613. /* Reset */
  3614. line = 1;
  3615. } else if (line == max) {
  3616. continue;
  3617. }
  3618. /* Note that the number of items actually displayed */
  3619. disp_count++;
  3620. if (artifact_p(o_ptr) && object_known_p(o_ptr))
  3621. /* known artifact */
  3622. attr = TERM_VIOLET;
  3623. else if (!object_aware_p(o_ptr))
  3624. /* unaware of kind */
  3625. attr = TERM_RED;
  3626. else if (object_is_worthless(o_ptr))
  3627. /* worthless */
  3628. attr = TERM_SLATE;
  3629. else
  3630. /* default */
  3631. attr = TERM_WHITE;
  3632. a = object_kind_attr(o_ptr->k_idx);
  3633. c = object_kind_char(o_ptr->k_idx);
  3634. /* Display the pict */
  3635. if ((tile_width == 1) && (tile_height == 1)) {
  3636. Term_putch(cur_x++, line, a, c);
  3637. Term_putch(cur_x++, line, TERM_WHITE, ' ');
  3638. }
  3639. /* Print and bump line counter */
  3640. c_prt(attr, o_desc, line, cur_x);
  3641. line++;
  3642. }
  3643. if (disp_count != counter) {
  3644. /* Print "and others" message if we've run out of space */
  3645. if (counter == MAX_ITEMLIST) {
  3646. strnfmt(buf, sizeof buf, " ...and many others.");
  3647. } else {
  3648. strnfmt(buf, sizeof buf, " ...and %d others.",
  3649. counter - disp_count);
  3650. }
  3651. c_prt(TERM_WHITE, buf, line, x);
  3652. } else {
  3653. /* Otherwise clear a line at the end, for main-term display */
  3654. prt("", line, x);
  3655. }
  3656. if (Term == angband_term[0])
  3657. Term_addstr(-1, TERM_WHITE, " (Press any key to continue.)");
  3658. }
  3659. /* Accessor functions, for prettier code */
  3660. artifact_type *artifact_of(const object_type * o_ptr)
  3661. {
  3662. if (o_ptr->name1)
  3663. return &a_info[o_ptr->name1];
  3664. return NULL;
  3665. }
  3666. object_kind *object_kind_of(const object_type * o_ptr)
  3667. {
  3668. return &k_info[o_ptr->k_idx];
  3669. }
  3670. /* Basic tval testers */
  3671. bool obj_is_staff(const object_type * o_ptr)
  3672. {
  3673. return o_ptr->tval == TV_STAFF;
  3674. }
  3675. bool obj_is_wand(const object_type * o_ptr)
  3676. {
  3677. return o_ptr->tval == TV_WAND;
  3678. }
  3679. bool obj_is_rod(const object_type * o_ptr)
  3680. {
  3681. return o_ptr->tval == TV_ROD;
  3682. }
  3683. bool obj_is_potion(const object_type * o_ptr)
  3684. {
  3685. return o_ptr->tval == TV_POTION;
  3686. }
  3687. bool obj_is_scroll(const object_type * o_ptr)
  3688. {
  3689. return o_ptr->tval == TV_SCROLL;
  3690. }
  3691. bool obj_is_food(const object_type * o_ptr)
  3692. {
  3693. return o_ptr->tval == TV_FOOD;
  3694. }
  3695. bool obj_is_light(const object_type * o_ptr)
  3696. {
  3697. return o_ptr->tval == TV_LIGHT;
  3698. }
  3699. bool obj_is_ring(const object_type * o_ptr)
  3700. {
  3701. return o_ptr->tval == TV_RING;
  3702. }
  3703. /**
  3704. * Determine whether an object is ammo
  3705. *
  3706. * \param o_ptr is the object to check
  3707. */
  3708. bool obj_is_ammo(const object_type * o_ptr)
  3709. {
  3710. switch (o_ptr->tval) {
  3711. case TV_SHOT:
  3712. case TV_ARROW:
  3713. case TV_BOLT:
  3714. return TRUE;
  3715. default:
  3716. return FALSE;
  3717. }
  3718. }
  3719. /**
  3720. * Determine whether an object goes in the quiver
  3721. *
  3722. * \param o_ptr is the object to check
  3723. */
  3724. bool obj_is_quiver_obj(const object_type * o_ptr)
  3725. {
  3726. if (of_has(o_ptr->flags_obj, OF_THROWING))
  3727. return TRUE;
  3728. if (obj_is_ammo(o_ptr))
  3729. return TRUE;
  3730. return FALSE;
  3731. }
  3732. /* Determine if an object has charges */
  3733. bool obj_has_charges(const object_type * o_ptr)
  3734. {
  3735. if (o_ptr->tval != TV_WAND && o_ptr->tval != TV_STAFF)
  3736. return FALSE;
  3737. if (o_ptr->pval <= 0)
  3738. return FALSE;
  3739. return TRUE;
  3740. }
  3741. /* Determine if an object is zappable */
  3742. bool obj_can_zap(const object_type * o_ptr)
  3743. {
  3744. /* Any rods not charging? */
  3745. if (o_ptr->tval == TV_ROD && number_charging(o_ptr) < o_ptr->number)
  3746. return TRUE;
  3747. return FALSE;
  3748. }
  3749. /* Determine if an object is activatable */
  3750. bool obj_is_activatable(const object_type * o_ptr)
  3751. {
  3752. return object_effect(o_ptr) ? TRUE : FALSE;
  3753. }
  3754. /* Determine if an object can be activated now */
  3755. bool obj_can_activate(const object_type * o_ptr)
  3756. {
  3757. if (obj_is_activatable(o_ptr)) {
  3758. /* Check the recharge */
  3759. if (!o_ptr->timeout)
  3760. return TRUE;
  3761. }
  3762. return FALSE;
  3763. }
  3764. bool obj_can_refill(const object_type * o_ptr)
  3765. {
  3766. const object_type *j_ptr = &p_ptr->inventory[INVEN_LIGHT];
  3767. /* Other lights of the same type are OK */
  3768. if ((o_ptr->tval == TV_LIGHT) && (o_ptr->sval == j_ptr->sval))
  3769. return (TRUE);
  3770. if (j_ptr->sval == SV_LIGHT_LANTERN) {
  3771. /* Flasks of oil are okay */
  3772. if (o_ptr->tval == TV_FLASK)
  3773. return (TRUE);
  3774. }
  3775. /* Assume not okay */
  3776. return (FALSE);
  3777. }
  3778. bool obj_can_browse(const object_type * o_ptr)
  3779. {
  3780. return o_ptr->tval == mp_ptr->spell_book;
  3781. }
  3782. bool obj_can_cast_from(const object_type * o_ptr)
  3783. {
  3784. return obj_can_browse(o_ptr)
  3785. && spell_book_count_spells(o_ptr, spell_okay_to_cast) > 0;
  3786. }
  3787. bool obj_can_study(const object_type * o_ptr)
  3788. {
  3789. return obj_can_browse(o_ptr)
  3790. && spell_book_count_spells(o_ptr, spell_okay_to_study) > 0;
  3791. }
  3792. /* Can only take off non-cursed items */
  3793. bool obj_can_takeoff(const object_type * o_ptr)
  3794. {
  3795. if (cf_has(o_ptr->flags_curse, CF_STICKY_WIELD)) {
  3796. int i;
  3797. for (i = INVEN_WIELD; i < ALL_INVEN_TOTAL; i++)
  3798. if (o_ptr == &p_ptr->inventory[i])
  3799. break;
  3800. notice_curse(CF_STICKY_WIELD, i + 1);
  3801. return FALSE;
  3802. }
  3803. return TRUE;
  3804. }
  3805. bool obj_think_can_takeoff(const object_type * o_ptr)
  3806. {
  3807. if (cf_has(o_ptr->id_curse, CF_STICKY_WIELD))
  3808. return FALSE;
  3809. return TRUE;
  3810. }
  3811. /* Can only put on wieldable items */
  3812. bool obj_can_wear(const object_type * o_ptr)
  3813. {
  3814. return (wield_slot(o_ptr) >= INVEN_WIELD);
  3815. }
  3816. /* Can only fire an item with the right tval */
  3817. bool obj_can_fire(const object_type * o_ptr)
  3818. {
  3819. return o_ptr->tval == p_ptr->state.ammo_tval;
  3820. }
  3821. /* Can only fire an item with the right tval */
  3822. bool obj_can_throw(const object_type * o_ptr)
  3823. {
  3824. int i;
  3825. for (i = INVEN_WIELD; i < INVEN_TOTAL; i++)
  3826. if (o_ptr == &p_ptr->inventory[i])
  3827. return FALSE;
  3828. return TRUE;
  3829. }
  3830. /* Can has inscrip pls */
  3831. bool obj_has_inscrip(const object_type * o_ptr)
  3832. {
  3833. return (o_ptr->note ? TRUE : FALSE);
  3834. }
  3835. /*** Generic utility functions ***/
  3836. /*
  3837. * Return an object's effect.
  3838. */
  3839. u16b object_effect(const object_type * o_ptr)
  3840. {
  3841. return o_ptr->effect;
  3842. }
  3843. /* Get an o_ptr from an item number */
  3844. object_type *object_from_item_idx(int item)
  3845. {
  3846. if (item >= 0)
  3847. return &p_ptr->inventory[item];
  3848. else
  3849. return &o_list[0 - item];
  3850. }
  3851. /*
  3852. * Does the given object need to be aimed?
  3853. */
  3854. bool obj_needs_aim(object_type * o_ptr)
  3855. {
  3856. int effect = o_ptr->effect;
  3857. /* If the effect needs aiming, or if the object type needs aiming, this
  3858. * object needs aiming. */
  3859. if (effect_aim(effect) || o_ptr->tval == TV_BOLT
  3860. || o_ptr->tval == TV_SHOT || o_ptr->tval == TV_ARROW
  3861. || o_ptr->tval == TV_WAND || (o_ptr->tval == TV_ROD
  3862. && !object_aware_p(o_ptr)))
  3863. return TRUE;
  3864. else
  3865. return FALSE;
  3866. }
  3867. /*
  3868. * Verify the "okayness" of a given item.
  3869. *
  3870. * The item can be negative to mean "item on floor".
  3871. */
  3872. bool get_item_okay(int item)
  3873. {
  3874. /* Verify the item */
  3875. return (item_tester_okay(object_from_item_idx(item)));
  3876. }
  3877. /*
  3878. * Get a list of "valid" item indexes.
  3879. *
  3880. * Fills item_list[] with items that are "okay" as defined by the
  3881. * current item_tester_hook, etc. mode determines what combination of
  3882. * inventory, equipment and player's floor location should be used
  3883. * when drawing up the list.
  3884. *
  3885. * Returns the number of items placed into the list.
  3886. *
  3887. * Maximum space that can be used is [INVEN_TOTAL + MAX_FLOOR_STACK],
  3888. * though practically speaking much smaller numbers are likely.
  3889. */
  3890. int scan_items(int *item_list, size_t item_list_max, int mode)
  3891. {
  3892. bool use_inven = ((mode & USE_INVEN) ? TRUE : FALSE);
  3893. bool use_equip = ((mode & USE_EQUIP) ? TRUE : FALSE);
  3894. bool use_floor = ((mode & USE_FLOOR) ? TRUE : FALSE);
  3895. int floor_list[MAX_FLOOR_STACK];
  3896. int floor_num;
  3897. int i;
  3898. size_t item_list_num = 0;
  3899. if (use_inven) {
  3900. for (i = 0; i < INVEN_PACK && item_list_num < item_list_max; i++) {
  3901. if (get_item_okay(i))
  3902. item_list[item_list_num++] = i;
  3903. }
  3904. }
  3905. if (use_equip) {
  3906. for (i = INVEN_WIELD;
  3907. i < ALL_INVEN_TOTAL && item_list_num < item_list_max; i++) {
  3908. if (get_item_okay(i))
  3909. item_list[item_list_num++] = i;
  3910. }
  3911. }
  3912. /* Scan all non-gold objects in the grid */
  3913. if (use_floor) {
  3914. floor_num =
  3915. scan_floor(floor_list, N_ELEMENTS(floor_list), p_ptr->py,
  3916. p_ptr->px, 0x03);
  3917. for (i = 0; i < floor_num && item_list_num < item_list_max; i++) {
  3918. if (get_item_okay(-floor_list[i]))
  3919. item_list[item_list_num++] = -floor_list[i];
  3920. }
  3921. }
  3922. /* Forget the item_tester_tval and item_tester_hook restrictions */
  3923. item_tester_tval = 0;
  3924. item_tester_hook = NULL;
  3925. return item_list_num;
  3926. }
  3927. /*
  3928. * Check if the given item is available for the player to use.
  3929. *
  3930. * 'mode' defines which areas we should look at, a la scan_items().
  3931. */
  3932. bool item_is_available(int item, bool(*tester) (const object_type *),
  3933. int mode)
  3934. {
  3935. int item_list[ALL_INVEN_TOTAL + MAX_FLOOR_STACK];
  3936. int item_num;
  3937. int i;
  3938. item_tester_hook = tester;
  3939. item_tester_tval = 0;
  3940. item_num = scan_items(item_list, N_ELEMENTS(item_list), mode);
  3941. for (i = 0; i < item_num; i++) {
  3942. if (item_list[i] == item)
  3943. return TRUE;
  3944. }
  3945. return FALSE;
  3946. }
  3947. /*
  3948. * Returns whether the pack is holding the maximum number of items. The max
  3949. * size is INVEN_MAX_PACK, which is a macro since quiver size affects slots
  3950. * available.
  3951. */
  3952. bool pack_is_full(void)
  3953. {
  3954. return p_ptr->inventory[INVEN_MAX_PACK - 1].k_idx ? TRUE : FALSE;
  3955. }
  3956. /*
  3957. * Returns whether the pack is holding the more than the maximum number of
  3958. * items. The max size is INVEN_MAX_PACK, which is a macro since quiver size
  3959. * affects slots available. If this is true, calling pack_overflow() will
  3960. * trigger a pack overflow.
  3961. */
  3962. bool pack_is_overfull(void)
  3963. {
  3964. return p_ptr->inventory[INVEN_MAX_PACK].k_idx ? TRUE : FALSE;
  3965. }
  3966. /*
  3967. * Overflow an item from the pack, if it is overfull.
  3968. */
  3969. void pack_overflow(void)
  3970. {
  3971. int item = INVEN_MAX_PACK;
  3972. char o_name[80];
  3973. object_type *o_ptr;
  3974. if (!pack_is_overfull())
  3975. return;
  3976. /* Get the slot to be dropped */
  3977. o_ptr = &p_ptr->inventory[item];
  3978. /* Disturbing */
  3979. disturb(0, 0);
  3980. /* Warning */
  3981. msg("Your pack overflows!");
  3982. /* Describe */
  3983. object_desc(o_name, sizeof(o_name), o_ptr, ODESC_PREFIX | ODESC_FULL);
  3984. /* Message */
  3985. msg("You drop %s (%c).", o_name, index_to_label(item));
  3986. /* Drop it (carefully) near the player */
  3987. drop_near(o_ptr, 0, p_ptr->py, p_ptr->px, FALSE);
  3988. /* Modify, Describe, Optimize */
  3989. inven_item_increase(item, -255);
  3990. inven_item_describe(item);
  3991. inven_item_optimize(item);
  3992. /* Notice stuff (if needed) */
  3993. if (p_ptr->notice)
  3994. notice_stuff(p_ptr);
  3995. /* Update stuff (if needed) */
  3996. if (p_ptr->update)
  3997. update_stuff(p_ptr);
  3998. /* Redraw stuff (if needed) */
  3999. if (p_ptr->redraw)
  4000. redraw_stuff(p_ptr);
  4001. }
  4002. /* Set Item Code */
  4003. /**
  4004. * Determin if a given Set of artifacts is being used by the player.
  4005. */
  4006. bool check_set(byte set_idx)
  4007. {
  4008. byte count = 0;
  4009. byte i;
  4010. set_type *set_ptr = &set_info[set_idx];;
  4011. for (i = INVEN_WIELD; i <= INVEN_FEET; i++) {
  4012. object_type *o_ptr = &p_ptr->inventory[i];
  4013. if (o_ptr->name1) {
  4014. artifact_type *a_ptr = &a_info[o_ptr->name1];
  4015. if (a_ptr->set_no == set_idx) {
  4016. count++;
  4017. }
  4018. }
  4019. }
  4020. return (count >= set_ptr->no_of_items);
  4021. }
  4022. /**
  4023. * Apply bonuses for complete artifact sets.
  4024. */
  4025. void apply_set(int set_idx)
  4026. {
  4027. set_type *set_ptr = &set_info[set_idx];
  4028. bool bonus_applied = FALSE;
  4029. byte i, j, k;
  4030. for (i = INVEN_WIELD; i <= INVEN_FEET; i++) {
  4031. object_type *o_ptr = &p_ptr->inventory[i];
  4032. /* Is it an artifact? */
  4033. if (o_ptr->name1) {
  4034. artifact_type *a_ptr = &a_info[o_ptr->name1];
  4035. /* Is it in the correct set? */
  4036. if (a_ptr->set_no == set_idx) {
  4037. /* Loop through set elements */
  4038. for (j = 0; j < (set_ptr->no_of_items); j++) {
  4039. set_element *se_ptr = &set_ptr->set_items[j];
  4040. /* Correct Element? */
  4041. if (se_ptr->a_idx == o_ptr->name1) {
  4042. /* Bonus already applied? */
  4043. if (!(a_ptr->set_bonus)) {
  4044. of_union(o_ptr->flags_obj, se_ptr->flags_obj);
  4045. cf_union(o_ptr->flags_curse,
  4046. se_ptr->flags_curse);
  4047. for (k = 0; k < MAX_P_RES; k++)
  4048. if (se_ptr->percent_res[k] !=
  4049. RES_LEVEL_BASE)
  4050. o_ptr->percent_res[k] =
  4051. se_ptr->percent_res[k];
  4052. for (k = 0; k < A_MAX; k++)
  4053. if (se_ptr->bonus_stat[k] != BONUS_BASE)
  4054. o_ptr->bonus_stat[k] =
  4055. se_ptr->bonus_stat[k];
  4056. for (k = 0; k < MAX_P_BONUS; k++)
  4057. if (se_ptr->bonus_other[k] != BONUS_BASE)
  4058. o_ptr->bonus_other[k] =
  4059. se_ptr->bonus_other[k];
  4060. for (k = 0; k < MAX_P_SLAY; k++)
  4061. if (se_ptr->multiple_slay[k] !=
  4062. MULTIPLE_BASE)
  4063. o_ptr->multiple_slay[k] =
  4064. se_ptr->multiple_slay[k];
  4065. for (k = 0; k < MAX_P_BRAND; k++)
  4066. if (se_ptr->multiple_brand[k] !=
  4067. MULTIPLE_BASE)
  4068. o_ptr->multiple_brand[k] =
  4069. se_ptr->multiple_brand[k];
  4070. a_ptr->set_bonus = TRUE;
  4071. bonus_applied = TRUE;
  4072. }
  4073. }
  4074. }
  4075. }
  4076. }
  4077. }
  4078. /* Notify */
  4079. if (bonus_applied)
  4080. msg("Item set completed!");
  4081. }
  4082. /**
  4083. * Remove bonuses for no-longer-complete artifact sets.
  4084. */
  4085. void remove_set(int set_idx)
  4086. {
  4087. set_type *set_ptr = &set_info[set_idx];
  4088. bool bonus_removed = FALSE;
  4089. byte i, j, k;
  4090. for (i = INVEN_WIELD; i <= INVEN_FEET; i++) {
  4091. object_type *o_ptr = &p_ptr->inventory[i];
  4092. /* Is it an artifact? */
  4093. if (o_ptr->name1) {
  4094. artifact_type *a_ptr = &a_info[o_ptr->name1];
  4095. /* Is it in the correct set? */
  4096. if (a_ptr->set_no == set_idx) {
  4097. /* Loop through set elements */
  4098. for (j = 0; j < (set_ptr->no_of_items); j++) {
  4099. set_element *se_ptr = &set_ptr->set_items[j];
  4100. /* Correct Element? */
  4101. if (se_ptr->a_idx == o_ptr->name1) {
  4102. /* Is the bonus really there? */
  4103. if (a_ptr->set_bonus) {
  4104. of_diff(o_ptr->flags_obj, se_ptr->flags_obj);
  4105. cf_diff(o_ptr->flags_curse,
  4106. se_ptr->flags_curse);
  4107. a_ptr->set_bonus = FALSE;
  4108. for (k = 0; k < MAX_P_RES; k++)
  4109. o_ptr->percent_res[k] =
  4110. a_ptr->percent_res[k];
  4111. for (k = 0; k < A_MAX; k++)
  4112. o_ptr->bonus_stat[k] =
  4113. a_ptr->bonus_stat[k];
  4114. for (k = 0; k < MAX_P_BONUS; k++)
  4115. o_ptr->bonus_other[k] =
  4116. a_ptr->bonus_other[k];
  4117. for (k = 0; k < MAX_P_SLAY; k++)
  4118. o_ptr->multiple_slay[k] =
  4119. a_ptr->multiple_slay[k];
  4120. for (k = 0; k < MAX_P_BRAND; k++)
  4121. o_ptr->multiple_brand[k] =
  4122. a_ptr->multiple_brand[k];
  4123. bonus_removed = TRUE;
  4124. }
  4125. }
  4126. }
  4127. }
  4128. }
  4129. }
  4130. /* Notify */
  4131. if (bonus_removed)
  4132. msg("Item set no longer completed.");
  4133. }