PageRenderTime 76ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/object/obj-util.c

https://bitbucket.org/ekolis/jackband
C | 4266 lines | 2227 code | 911 blank | 1128 comment | 660 complexity | 86c9d7f46c718a0443cc78dab51aed02 MD5 | raw file
  1. /*
  2. * File: object2.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 "defines.h"
  20. #include "tvalsval.h"
  21. #include "effects.h"
  22. #include "game-cmd.h"
  23. /*
  24. * Hold the titles of scrolls, 6 to 14 characters each.
  25. */
  26. char scroll_adj[MAX_TITLES][16];
  27. static void flavor_assign_fixed(void)
  28. {
  29. int i, j;
  30. for (i = 0; i < z_info->flavor_max; i++)
  31. {
  32. flavor_type *flavor_ptr = &flavor_info[i];
  33. /* Skip random flavors */
  34. if (flavor_ptr->sval == SV_UNKNOWN) continue;
  35. for (j = 0; j < z_info->k_max; j++)
  36. {
  37. /* Skip other objects */
  38. if ((k_info[j].tval == flavor_ptr->tval) &&
  39. (k_info[j].sval == flavor_ptr->sval))
  40. {
  41. /* Store the flavor index */
  42. k_info[j].flavor = i;
  43. }
  44. }
  45. }
  46. }
  47. static void flavor_assign_random(byte tval)
  48. {
  49. int i, j;
  50. int flavor_count = 0;
  51. int choice;
  52. /* Count the random flavors for the given tval */
  53. for (i = 0; i < z_info->flavor_max; i++)
  54. {
  55. if ((flavor_info[i].tval == tval) &&
  56. (flavor_info[i].sval == SV_UNKNOWN))
  57. {
  58. flavor_count++;
  59. }
  60. }
  61. for (i = 0; i < z_info->k_max; i++)
  62. {
  63. /* Skip other object types */
  64. if (k_info[i].tval != tval) continue;
  65. /* Skip objects that already are flavored */
  66. if (k_info[i].flavor != 0) continue;
  67. /* HACK - Ordinary food is "boring" */
  68. if ((tval == TV_FOOD) && (k_info[i].sval < SV_FOOD_MIN_SHROOM))
  69. continue;
  70. if (!flavor_count) quit_fmt("Not enough flavors for tval %d.", tval);
  71. /* Select a flavor */
  72. choice = randint0(flavor_count);
  73. /* Find and store the flavor */
  74. for (j = 0; j < z_info->flavor_max; j++)
  75. {
  76. /* Skip other tvals */
  77. if (flavor_info[j].tval != tval) continue;
  78. /* Skip assigned svals */
  79. if (flavor_info[j].sval != SV_UNKNOWN) continue;
  80. if (choice == 0)
  81. {
  82. /* Store the flavor index */
  83. k_info[i].flavor = j;
  84. /* Mark the flavor as used */
  85. flavor_info[j].sval = k_info[i].sval;
  86. /* One less flavor to choose from */
  87. flavor_count--;
  88. break;
  89. }
  90. choice--;
  91. }
  92. }
  93. }
  94. /*
  95. * Prepare the "variable" part of the "k_info" array.
  96. *
  97. * The "color"/"metal"/"type" of an item is its "flavor".
  98. * For the most part, flavors are assigned randomly each game.
  99. *
  100. * Initialize descriptions for the "colored" objects, including:
  101. * Rings, Amulets, Staffs, Wands, Rods, Food, Potions, Scrolls.
  102. *
  103. * The first 4 entries for potions are fixed (Water, Apple Juice,
  104. * Slime Mold Juice, Unused Potion).
  105. *
  106. * Scroll titles are always between 6 and 14 letters long. This is
  107. * ensured because every title is composed of whole words, where every
  108. * word is from 2 to 8 letters long, and that no scroll is finished
  109. * until it attempts to grow beyond 15 letters. The first time this
  110. * can happen is when the current title has 6 letters and the new word
  111. * has 8 letters, which would result in a 6 letter scroll title.
  112. *
  113. * Hack -- make sure everything stays the same for each saved game
  114. * This is accomplished by the use of a saved "random seed", as in
  115. * "town_gen()". Since no other functions are called while the special
  116. * seed is in effect, so this function is pretty "safe".
  117. */
  118. void flavor_init(void)
  119. {
  120. int i, j;
  121. /* Hack -- Use the "simple" RNG */
  122. Rand_quick = TRUE;
  123. /* Hack -- Induce consistant flavors */
  124. Rand_value = seed_flavor;
  125. flavor_assign_fixed();
  126. flavor_assign_random(TV_RING);
  127. flavor_assign_random(TV_AMULET);
  128. flavor_assign_random(TV_STAFF);
  129. flavor_assign_random(TV_WAND);
  130. flavor_assign_random(TV_ROD);
  131. flavor_assign_random(TV_FOOD);
  132. flavor_assign_random(TV_POTION);
  133. flavor_assign_random(TV_SCROLL);
  134. /* Scrolls (random titles, always white) */
  135. for (i = 0; i < MAX_TITLES; i++)
  136. {
  137. char buf[24];
  138. char *end = buf;
  139. int titlelen = 0;
  140. int wordlen;
  141. bool okay = TRUE;
  142. wordlen = randname_make(RANDNAME_SCROLL, 2, 8, end, 24);
  143. while (titlelen + wordlen < (int)(sizeof(scroll_adj[0]) - 1))
  144. {
  145. end[wordlen] = ' ';
  146. titlelen += wordlen + 1;
  147. end += wordlen + 1;
  148. wordlen = randname_make(RANDNAME_SCROLL, 2, 8, end, 24 - titlelen);
  149. }
  150. buf[titlelen - 1] = '\0';
  151. /* Check the scroll name hasn't already been generated */
  152. for (j = 0; j < i; j++)
  153. {
  154. if (streq(buf, scroll_adj[j]))
  155. {
  156. okay = FALSE;
  157. break;
  158. }
  159. }
  160. if (okay)
  161. {
  162. my_strcpy(scroll_adj[i], buf, sizeof(scroll_adj[0]));
  163. }
  164. else
  165. {
  166. /* Have another go at making a name */
  167. i--;
  168. }
  169. }
  170. /* Hack -- Use the "complex" RNG */
  171. Rand_quick = FALSE;
  172. /* Analyze every object */
  173. for (i = 1; i < z_info->k_max; i++)
  174. {
  175. object_kind *k_ptr = &k_info[i];
  176. /* Skip "empty" objects */
  177. if (!k_ptr->name) continue;
  178. /* No flavor yields aware */
  179. if (!k_ptr->flavor) k_ptr->aware = TRUE;
  180. }
  181. }
  182. #ifdef ALLOW_BORG_GRAPHICS
  183. extern void init_translate_visuals(void);
  184. #endif /* ALLOW_BORG_GRAPHICS */
  185. /*
  186. * Reset the "visual" lists
  187. *
  188. * This involves resetting various things to their "default" state.
  189. *
  190. * If the "prefs" flag is TRUE, then we will also load the appropriate
  191. * "user pref file" based on the current setting of the "use_graphics"
  192. * flag. This is useful for switching "graphics" on/off.
  193. *
  194. * The features, objects, and monsters, should all be encoded in the
  195. * relevant "font.pref" and/or "graf.prf" files. XXX XXX XXX
  196. *
  197. * The "prefs" parameter is no longer meaningful. XXX XXX XXX
  198. */
  199. void reset_visuals(bool unused)
  200. {
  201. int i;
  202. /* Unused parameter */
  203. (void)unused;
  204. /* Extract default attr/char code for features */
  205. for (i = 0; i < z_info->f_max; i++)
  206. {
  207. feature_type *f_ptr = &f_info[i];
  208. /* Assume we will use the underlying values */
  209. f_ptr->x_attr = f_ptr->d_attr;
  210. f_ptr->x_char = f_ptr->d_char;
  211. }
  212. /* Extract default attr/char code for objects */
  213. for (i = 0; i < z_info->k_max; i++)
  214. {
  215. object_kind *k_ptr = &k_info[i];
  216. /* Default attr/char */
  217. k_ptr->x_attr = k_ptr->d_attr;
  218. k_ptr->x_char = k_ptr->d_char;
  219. }
  220. /* Extract default attr/char code for monsters */
  221. for (i = 0; i < z_info->r_max; i++)
  222. {
  223. monster_race *r_ptr = &r_info[i];
  224. /* Default attr/char */
  225. r_ptr->x_attr = r_ptr->d_attr;
  226. r_ptr->x_char = r_ptr->d_char;
  227. }
  228. /* Extract default attr/char code for flavors */
  229. for (i = 0; i < z_info->flavor_max; i++)
  230. {
  231. flavor_type *flavor_ptr = &flavor_info[i];
  232. /* Default attr/char */
  233. flavor_ptr->x_attr = flavor_ptr->d_attr;
  234. flavor_ptr->x_char = flavor_ptr->d_char;
  235. }
  236. /* Extract attr/chars for inventory objects (by tval) */
  237. for (i = 0; i < (int)N_ELEMENTS(tval_to_attr); i++)
  238. {
  239. /* Default to white */
  240. tval_to_attr[i] = TERM_WHITE;
  241. }
  242. /* Graphic symbols */
  243. if (use_graphics)
  244. {
  245. /* Process "graf.prf" */
  246. process_pref_file("graf.prf");
  247. }
  248. /* Normal symbols */
  249. else
  250. {
  251. /* Process "font.prf" */
  252. process_pref_file("font.prf");
  253. }
  254. #ifdef ALLOW_BORG_GRAPHICS
  255. /* Initialize the translation table for the borg */
  256. init_translate_visuals();
  257. #endif /* ALLOW_BORG_GRAPHICS */
  258. }
  259. /*
  260. * Obtain the "flags" for an item
  261. */
  262. void object_flags(const object_type *o_ptr, bitflag flags[OF_SIZE])
  263. {
  264. object_kind *k_ptr = &k_info[o_ptr->k_idx];
  265. of_wipe(flags);
  266. /* Obtain kind flags */
  267. of_union(flags, k_ptr->flags);
  268. /* Obtain artifact flags */
  269. if (o_ptr->name1)
  270. {
  271. artifact_type *a_ptr = &a_info[o_ptr->name1];
  272. of_union(flags, a_ptr->flags);
  273. }
  274. /* Obtain ego flags */
  275. if (o_ptr->name2)
  276. {
  277. ego_item_type *e_ptr = &e_info[o_ptr->name2];
  278. of_union(flags, e_ptr->flags);
  279. }
  280. /* Remove curse flags (use only the object's curse flags) */
  281. flags_clear(flags, OF_SIZE, OF_CURSE_MASK, FLAG_END);
  282. /* Obtain the object's flags */
  283. of_union(flags, o_ptr->flags);
  284. }
  285. /*
  286. * Obtain the "flags" for an item which are known to the player
  287. */
  288. void object_flags_known(const object_type *o_ptr, bitflag flags[OF_SIZE])
  289. {
  290. object_flags(o_ptr, flags);
  291. of_inter(flags, o_ptr->known_flags);
  292. if (object_flavor_is_aware(o_ptr))
  293. of_union(flags, k_info[o_ptr->k_idx].flags);
  294. if (o_ptr->name2 && easy_know(o_ptr))
  295. of_union(flags, e_info[o_ptr->name2].flags);
  296. }
  297. /*
  298. * Convert an inventory index into a one character label.
  299. *
  300. * Note that the label does NOT distinguish inven/equip.
  301. */
  302. char index_to_label(int i)
  303. {
  304. /* Indexes for "inven" are easy */
  305. if (i < INVEN_WIELD) return (I2A(i));
  306. /* Indexes for "equip" are offset */
  307. return (I2A(i - INVEN_WIELD));
  308. }
  309. /*
  310. * Convert a label into the index of an item in the "inven".
  311. *
  312. * Return "-1" if the label does not indicate a real item.
  313. */
  314. s16b label_to_inven(int c)
  315. {
  316. int i;
  317. /* Convert */
  318. i = (islower((unsigned char)c) ? A2I(c) : -1);
  319. /* Verify the index */
  320. if ((i < 0) || (i > INVEN_PACK)) return (-1);
  321. /* Empty slots can never be chosen */
  322. if (!inventory[i].k_idx) return (-1);
  323. /* Return the index */
  324. return (i);
  325. }
  326. /*
  327. * Convert a label into the index of a item in the "equip".
  328. *
  329. * Return "-1" if the label does not indicate a real item.
  330. */
  331. s16b label_to_equip(int c)
  332. {
  333. int i;
  334. /* Convert */
  335. i = (islower((unsigned char)c) ? A2I(c) : -1) + INVEN_WIELD;
  336. /* Verify the index */
  337. if ((i < INVEN_WIELD) || (i >= ALL_INVEN_TOTAL)) return (-1);
  338. if (i == INVEN_TOTAL) return (-1);
  339. /* Empty slots can never be chosen */
  340. if (!inventory[i].k_idx) return (-1);
  341. /* Return the index */
  342. return (i);
  343. }
  344. /*
  345. * Hack -- determine if an item is "wearable" (or a missile)
  346. */
  347. bool wearable_p(const object_type *o_ptr)
  348. {
  349. /* Valid "tval" codes */
  350. switch (o_ptr->tval)
  351. {
  352. case TV_SHOT:
  353. case TV_ARROW:
  354. case TV_BOLT:
  355. case TV_BOW:
  356. case TV_DIGGING:
  357. case TV_HAFTED:
  358. case TV_POLEARM:
  359. case TV_SWORD:
  360. case TV_BOOTS:
  361. case TV_GLOVES:
  362. case TV_HELM:
  363. case TV_CROWN:
  364. case TV_SHIELD:
  365. case TV_CLOAK:
  366. case TV_SOFT_ARMOR:
  367. case TV_HARD_ARMOR:
  368. case TV_DRAG_ARMOR:
  369. case TV_LIGHT:
  370. case TV_AMULET:
  371. case TV_RING: return (TRUE);
  372. }
  373. /* Nope */
  374. return (FALSE);
  375. }
  376. int get_inscribed_ammo_slot(const object_type *o_ptr)
  377. {
  378. char *s;
  379. if (!o_ptr->note) return 0;
  380. s = strchr(quark_str(o_ptr->note), 'f');
  381. if (!s || s[1] < '0' || s[1] > '9') return 0;
  382. return QUIVER_START + (s[1] - '0');
  383. }
  384. /**
  385. * Used by wield_slot() to find an appopriate slot for ammo. See wield_slot()
  386. * for information on what this returns.
  387. */
  388. s16b wield_slot_ammo(const object_type *o_ptr)
  389. {
  390. s16b i, open = 0;
  391. /* If the ammo is inscribed with a slot number, we'll try to put it in */
  392. /* that slot, if possible. */
  393. i = get_inscribed_ammo_slot(o_ptr);
  394. if (i && !inventory[i].k_idx) return i;
  395. for (i = QUIVER_START; i < QUIVER_END; i++)
  396. {
  397. if (!inventory[i].k_idx)
  398. {
  399. /* Save the open slot if we haven't found one already */
  400. if (!open) open = i;
  401. continue;
  402. }
  403. /* If ammo is cursed we can't stack it */
  404. if (cursed_p(&inventory[i])) continue;
  405. /* If they are stackable, we'll use this slot for sure */
  406. if (object_similar(&inventory[i], o_ptr)) return i;
  407. }
  408. /* If not absorbed, return an open slot (or QUIVER_START if no room) */
  409. return open ? open : QUIVER_START;
  410. }
  411. /**
  412. * Determine which equipment slot (if any) an item likes. The slot might (or
  413. * might not) be open, but it is a slot which the object could be equipped in.
  414. *
  415. * For items where multiple slots could work (e.g. ammo or rings), the function
  416. * will try to a return a stackable slot first (only for ammo), then an open
  417. * slot if possible, and finally a used (but valid) slot if necessary.
  418. */
  419. s16b wield_slot(const object_type *o_ptr)
  420. {
  421. /* Slot for equipment */
  422. switch (o_ptr->tval)
  423. {
  424. case TV_DIGGING:
  425. case TV_HAFTED:
  426. case TV_POLEARM:
  427. case TV_SWORD: return (INVEN_WIELD);
  428. case TV_BOW: return (INVEN_BOW);
  429. case TV_RING:
  430. return inventory[INVEN_RIGHT].k_idx ? INVEN_LEFT : INVEN_RIGHT;
  431. case TV_AMULET: return (INVEN_NECK);
  432. case TV_LIGHT: return (INVEN_LIGHT);
  433. case TV_DRAG_ARMOR:
  434. case TV_HARD_ARMOR:
  435. case TV_SOFT_ARMOR: return (INVEN_BODY);
  436. case TV_CLOAK: return (INVEN_OUTER);
  437. case TV_SHIELD: return (INVEN_ARM);
  438. case TV_CROWN:
  439. case TV_HELM: return (INVEN_HEAD);
  440. case TV_GLOVES: return (INVEN_HANDS);
  441. case TV_BOOTS: return (INVEN_FEET);
  442. case TV_BOLT:
  443. case TV_ARROW:
  444. case TV_SHOT: 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
  459. return (wield_slot(o_ptr) == slot) ? TRUE : FALSE;
  460. }
  461. /*
  462. * Return a string mentioning how a given item is carried
  463. */
  464. const char *mention_use(int slot)
  465. {
  466. switch (slot)
  467. {
  468. case INVEN_WIELD:
  469. {
  470. if (adj_str_hold[p_ptr->state.stat_ind[A_STR]] < inventory[slot].weight / 10)
  471. return "Just lifting";
  472. else
  473. return "Wielding";
  474. }
  475. case INVEN_BOW:
  476. {
  477. if (adj_str_hold[p_ptr->state.stat_ind[A_STR]] < inventory[slot].weight / 10)
  478. return "Just holding";
  479. else
  480. return "Shooting";
  481. }
  482. case INVEN_LEFT: return "On left hand";
  483. case INVEN_RIGHT: return "On right hand";
  484. case INVEN_NECK: return "Around neck";
  485. case INVEN_LIGHT: return "Light source";
  486. case INVEN_BODY: return "On body";
  487. case INVEN_OUTER: return "About body";
  488. case INVEN_ARM: return "On arm";
  489. case INVEN_HEAD: return "On head";
  490. case INVEN_HANDS: return "On hands";
  491. case INVEN_FEET: return "On feet";
  492. case QUIVER_START + 0: return "In quiver [f0]";
  493. case QUIVER_START + 1: return "In quiver [f1]";
  494. case QUIVER_START + 2: return "In quiver [f2]";
  495. case QUIVER_START + 3: return "In quiver [f3]";
  496. case QUIVER_START + 4: return "In quiver [f4]";
  497. case QUIVER_START + 5: return "In quiver [f5]";
  498. case QUIVER_START + 6: return "In quiver [f6]";
  499. case QUIVER_START + 7: return "In quiver [f7]";
  500. case QUIVER_START + 8: return "In quiver [f8]";
  501. case QUIVER_START + 9: return "In quiver [f9]";
  502. }
  503. /*if (slot >= QUIVER_START && slot < QUIVER_END)
  504. return "In quiver";*/
  505. return "In pack";
  506. }
  507. /*
  508. * Return a string describing how a given item is being worn.
  509. * Currently, only used for items in the equipment, not inventory.
  510. */
  511. cptr describe_use(int i)
  512. {
  513. cptr p;
  514. switch (i)
  515. {
  516. case INVEN_WIELD: p = "attacking monsters with"; break;
  517. case INVEN_BOW: p = "shooting missiles with"; break;
  518. case INVEN_LEFT: p = "wearing on your left hand"; break;
  519. case INVEN_RIGHT: p = "wearing on your right hand"; break;
  520. case INVEN_NECK: p = "wearing around your neck"; break;
  521. case INVEN_LIGHT: p = "using to light the way"; break;
  522. case INVEN_BODY: p = "wearing on your body"; break;
  523. case INVEN_OUTER: p = "wearing on your back"; break;
  524. case INVEN_ARM: p = "wearing on your arm"; break;
  525. case INVEN_HEAD: p = "wearing on your head"; break;
  526. case INVEN_HANDS: p = "wearing on your hands"; break;
  527. case INVEN_FEET: p = "wearing on your feet"; break;
  528. default: p = "carrying in your pack"; break;
  529. }
  530. /* Hack -- Heavy weapon */
  531. if (i == INVEN_WIELD)
  532. {
  533. object_type *o_ptr;
  534. o_ptr = &inventory[i];
  535. if (adj_str_hold[p_ptr->state.stat_ind[A_STR]] < o_ptr->weight / 10)
  536. {
  537. p = "just lifting";
  538. }
  539. }
  540. /* Hack -- Heavy bow */
  541. if (i == INVEN_BOW)
  542. {
  543. object_type *o_ptr;
  544. o_ptr = &inventory[i];
  545. if (adj_str_hold[p_ptr->state.stat_ind[A_STR]] < o_ptr->weight / 10)
  546. {
  547. p = "just holding";
  548. }
  549. }
  550. /* Return the result */
  551. return p;
  552. }
  553. /*
  554. * Check an item against the item tester info
  555. */
  556. bool item_tester_okay(const object_type *o_ptr)
  557. {
  558. /* Hack -- allow listing empty slots */
  559. if (item_tester_full) return (TRUE);
  560. /* Require an item */
  561. if (!o_ptr->k_idx) return (FALSE);
  562. /* Hack -- ignore "gold" */
  563. if (o_ptr->tval == TV_GOLD) return (FALSE);
  564. /* Check the tval */
  565. if (item_tester_tval)
  566. {
  567. if (item_tester_tval != o_ptr->tval) return (FALSE);
  568. }
  569. /* Check the hook */
  570. if (item_tester_hook)
  571. {
  572. if (!(*item_tester_hook)(o_ptr)) return (FALSE);
  573. }
  574. /* Assume okay */
  575. return (TRUE);
  576. }
  577. /*
  578. * Get the indexes of objects at a given floor location. -TNB-
  579. *
  580. * Return the number of object indexes acquired.
  581. *
  582. * Valid flags are any combination of the bits:
  583. * 0x01 -- Verify item tester
  584. * 0x02 -- Marked/visible items only
  585. * 0x04 -- Only the top item
  586. */
  587. int scan_floor(int *items, int max_size, int y, int x, int mode)
  588. {
  589. int this_o_idx, next_o_idx;
  590. int num = 0;
  591. /* Sanity */
  592. if (!in_bounds(y, x)) return 0;
  593. /* Scan all objects in the grid */
  594. for (this_o_idx = cave_o_idx[y][x]; this_o_idx; this_o_idx = next_o_idx)
  595. {
  596. object_type *o_ptr;
  597. /* XXX Hack -- Enforce limit */
  598. if (num >= max_size) break;
  599. /* Get the object */
  600. o_ptr = &o_list[this_o_idx];
  601. /* Get the next object */
  602. next_o_idx = o_ptr->next_o_idx;
  603. /* Item tester */
  604. if ((mode & 0x01) && !item_tester_okay(o_ptr)) continue;
  605. /* Marked */
  606. if ((mode & 0x02) && (!o_ptr->marked || squelch_hide_item(o_ptr)))
  607. continue;
  608. /* Accept this item */
  609. items[num++] = this_o_idx;
  610. /* Only one */
  611. if (mode & 0x04) break;
  612. }
  613. return num;
  614. }
  615. /*
  616. * Excise a dungeon object from any stacks
  617. */
  618. void excise_object_idx(int o_idx)
  619. {
  620. object_type *j_ptr;
  621. s16b this_o_idx, next_o_idx = 0;
  622. s16b prev_o_idx = 0;
  623. /* Object */
  624. j_ptr = &o_list[o_idx];
  625. /* Monster */
  626. if (j_ptr->held_m_idx)
  627. {
  628. monster_type *m_ptr;
  629. /* Monster */
  630. m_ptr = &mon_list[j_ptr->held_m_idx];
  631. /* Scan all objects in the grid */
  632. for (this_o_idx = m_ptr->hold_o_idx; this_o_idx; this_o_idx = next_o_idx)
  633. {
  634. object_type *o_ptr;
  635. /* Get the object */
  636. o_ptr = &o_list[this_o_idx];
  637. /* Get the next object */
  638. next_o_idx = o_ptr->next_o_idx;
  639. /* Done */
  640. if (this_o_idx == o_idx)
  641. {
  642. /* No previous */
  643. if (prev_o_idx == 0)
  644. {
  645. /* Remove from list */
  646. m_ptr->hold_o_idx = next_o_idx;
  647. }
  648. /* Real previous */
  649. else
  650. {
  651. object_type *i_ptr;
  652. /* Previous object */
  653. i_ptr = &o_list[prev_o_idx];
  654. /* Remove from list */
  655. i_ptr->next_o_idx = next_o_idx;
  656. }
  657. /* Forget next pointer */
  658. o_ptr->next_o_idx = 0;
  659. /* Done */
  660. break;
  661. }
  662. /* Save prev_o_idx */
  663. prev_o_idx = this_o_idx;
  664. }
  665. }
  666. /* Dungeon */
  667. else
  668. {
  669. int y = j_ptr->iy;
  670. int x = j_ptr->ix;
  671. /* Scan all objects in the grid */
  672. for (this_o_idx = cave_o_idx[y][x]; this_o_idx; this_o_idx = next_o_idx)
  673. {
  674. object_type *o_ptr;
  675. /* Get the object */
  676. o_ptr = &o_list[this_o_idx];
  677. /* Get the next object */
  678. next_o_idx = o_ptr->next_o_idx;
  679. /* Done */
  680. if (this_o_idx == o_idx)
  681. {
  682. /* No previous */
  683. if (prev_o_idx == 0)
  684. {
  685. /* Remove from list */
  686. cave_o_idx[y][x] = next_o_idx;
  687. }
  688. /* Real previous */
  689. else
  690. {
  691. object_type *i_ptr;
  692. /* Previous object */
  693. i_ptr = &o_list[prev_o_idx];
  694. /* Remove from list */
  695. i_ptr->next_o_idx = next_o_idx;
  696. }
  697. /* Forget next pointer */
  698. o_ptr->next_o_idx = 0;
  699. /* Done */
  700. break;
  701. }
  702. /* Save prev_o_idx */
  703. prev_o_idx = this_o_idx;
  704. }
  705. }
  706. }
  707. /*
  708. * Delete a dungeon object
  709. *
  710. * Handle "stacks" of objects correctly.
  711. */
  712. void delete_object_idx(int o_idx)
  713. {
  714. object_type *j_ptr;
  715. /* Excise */
  716. excise_object_idx(o_idx);
  717. /* Object */
  718. j_ptr = &o_list[o_idx];
  719. /* Dungeon floor */
  720. if (!(j_ptr->held_m_idx))
  721. {
  722. int y, x;
  723. /* Location */
  724. y = j_ptr->iy;
  725. x = j_ptr->ix;
  726. /* Visual update */
  727. light_spot(y, x);
  728. }
  729. /* Wipe the object */
  730. object_wipe(j_ptr);
  731. /* Count objects */
  732. o_cnt--;
  733. }
  734. /*
  735. * Deletes all objects at given location
  736. */
  737. void delete_object(int y, int x)
  738. {
  739. s16b this_o_idx, next_o_idx = 0;
  740. /* Paranoia */
  741. if (!in_bounds(y, x)) return;
  742. /* Scan all objects in the grid */
  743. for (this_o_idx = cave_o_idx[y][x]; this_o_idx; this_o_idx = next_o_idx)
  744. {
  745. object_type *o_ptr;
  746. /* Get the object */
  747. o_ptr = &o_list[this_o_idx];
  748. /* Get the next object */
  749. next_o_idx = o_ptr->next_o_idx;
  750. /* Wipe the object */
  751. object_wipe(o_ptr);
  752. /* Count objects */
  753. o_cnt--;
  754. }
  755. /* Objects are gone */
  756. cave_o_idx[y][x] = 0;
  757. /* Visual update */
  758. light_spot(y, x);
  759. }
  760. /*
  761. * Move an object from index i1 to index i2 in the object list
  762. */
  763. static void compact_objects_aux(int i1, int i2)
  764. {
  765. int i;
  766. object_type *o_ptr;
  767. /* Do nothing */
  768. if (i1 == i2) return;
  769. /* Repair objects */
  770. for (i = 1; i < o_max; i++)
  771. {
  772. /* Get the object */
  773. o_ptr = &o_list[i];
  774. /* Skip "dead" objects */
  775. if (!o_ptr->k_idx) continue;
  776. /* Repair "next" pointers */
  777. if (o_ptr->next_o_idx == i1)
  778. {
  779. /* Repair */
  780. o_ptr->next_o_idx = i2;
  781. }
  782. }
  783. /* Get the object */
  784. o_ptr = &o_list[i1];
  785. /* Monster */
  786. if (o_ptr->held_m_idx)
  787. {
  788. monster_type *m_ptr;
  789. /* Get the monster */
  790. m_ptr = &mon_list[o_ptr->held_m_idx];
  791. /* Repair monster */
  792. if (m_ptr->hold_o_idx == i1)
  793. {
  794. /* Repair */
  795. m_ptr->hold_o_idx = i2;
  796. }
  797. }
  798. /* Dungeon */
  799. else
  800. {
  801. int y, x;
  802. /* Get location */
  803. y = o_ptr->iy;
  804. x = o_ptr->ix;
  805. /* Repair grid */
  806. if (cave_o_idx[y][x] == i1)
  807. {
  808. /* Repair */
  809. cave_o_idx[y][x] = i2;
  810. }
  811. }
  812. /* Hack -- move object */
  813. COPY(&o_list[i2], &o_list[i1], object_type);
  814. /* Hack -- wipe hole */
  815. object_wipe(o_ptr);
  816. }
  817. /*
  818. * Compact and reorder the object list
  819. *
  820. * This function can be very dangerous, use with caution!
  821. *
  822. * When compacting objects, we first destroy gold, on the basis that by the
  823. * time item compaction becomes an issue, the player really won't care.
  824. * We also nuke items marked as squelch.
  825. *
  826. * When compacting other objects, we base the saving throw on a combination of
  827. * object level, distance from player, and current "desperation".
  828. *
  829. * After compacting, we "reorder" the objects into a more compact order, and we
  830. * reset the allocation info, and the "live" array.
  831. */
  832. void compact_objects(int size)
  833. {
  834. int py = p_ptr->py;
  835. int px = p_ptr->px;
  836. int i, y, x, cnt;
  837. int cur_lev, cur_dis, chance;
  838. /* Reorder objects when not passed a size */
  839. if (!size)
  840. {
  841. /* Excise dead objects (backwards!) */
  842. for (i = o_max - 1; i >= 1; i--)
  843. {
  844. object_type *o_ptr = &o_list[i];
  845. /* Skip real objects */
  846. if (o_ptr->k_idx) continue;
  847. /* Move last object into open hole */
  848. compact_objects_aux(o_max - 1, i);
  849. /* Compress "o_max" */
  850. o_max--;
  851. }
  852. return;
  853. }
  854. /* Message */
  855. msg_print("Compacting objects...");
  856. /*** Try destroying objects ***/
  857. /* First do gold */
  858. for (i = 1; (i < o_max) && (size); i++)
  859. {
  860. object_type *o_ptr = &o_list[i];
  861. /* Nuke gold or squelched items */
  862. if (o_ptr->tval == TV_GOLD || squelch_item_ok(o_ptr))
  863. {
  864. delete_object_idx(i);
  865. size--;
  866. }
  867. }
  868. /* Compact at least 'size' objects */
  869. for (cnt = 1; size; cnt++)
  870. {
  871. /* Get more vicious each iteration */
  872. cur_lev = 5 * cnt;
  873. /* Get closer each iteration */
  874. cur_dis = 5 * (20 - cnt);
  875. /* Examine the objects */
  876. for (i = 1; (i < o_max) && (size); i++)
  877. {
  878. object_type *o_ptr = &o_list[i];
  879. object_kind *k_ptr = &k_info[o_ptr->k_idx];
  880. /* Skip dead objects */
  881. if (!o_ptr->k_idx) continue;
  882. /* Hack -- High level objects start out "immune" */
  883. if (k_ptr->level > cur_lev && !k_ptr->squelch)
  884. continue;
  885. /* Monster */
  886. if (o_ptr->held_m_idx)
  887. {
  888. monster_type *m_ptr;
  889. /* Get the monster */
  890. m_ptr = &mon_list[o_ptr->held_m_idx];
  891. /* Get the location */
  892. y = m_ptr->fy;
  893. x = m_ptr->fx;
  894. /* Monsters protect their objects */
  895. if ((randint0(100) < 90) && !k_ptr->squelch)
  896. continue;
  897. }
  898. /* Dungeon */
  899. else
  900. {
  901. /* Get the location */
  902. y = o_ptr->iy;
  903. x = o_ptr->ix;
  904. }
  905. /* Nearby objects start out "immune" */
  906. if ((cur_dis > 0) && (distance(py, px, y, x) < cur_dis) && !k_ptr->squelch)
  907. continue;
  908. /* Saving throw */
  909. chance = 90;
  910. /* Hack -- only compact artifacts in emergencies */
  911. if (artifact_p(o_ptr) && (cnt < 1000)) chance = 100;
  912. /* Apply the saving throw */
  913. if (randint0(100) < chance) continue;
  914. /* Delete the object */
  915. delete_object_idx(i);
  916. size--;
  917. }
  918. }
  919. /* Reorder objects */
  920. compact_objects(0);
  921. }
  922. /*
  923. * Mention artifact preservation for peeking wizards
  924. */
  925. static void mention_preserve(const object_type *o_ptr)
  926. {
  927. char o_name[80];
  928. /* Describe */
  929. object_desc(o_name, sizeof(o_name), o_ptr, ODESC_BASE | ODESC_SPOIL);
  930. msg_format("Preserving (%s)", o_name);
  931. }
  932. /*
  933. * Delete all the items when player leaves the level
  934. *
  935. * Note -- we do NOT visually reflect these (irrelevant) changes
  936. *
  937. * Hack -- we clear the "cave_o_idx[y][x]" field for every grid,
  938. * and the "m_ptr->next_o_idx" field for every monster, since
  939. * we know we are clearing every object. Technically, we only
  940. * clear those fields for grids/monsters containing objects,
  941. * and we clear it once for every such object.
  942. */
  943. void wipe_o_list(void)
  944. {
  945. int i;
  946. /* Delete the existing objects */
  947. for (i = 1; i < o_max; i++)
  948. {
  949. object_type *o_ptr = &o_list[i];
  950. artifact_type *a_ptr = artifact_of(o_ptr);
  951. /* Skip dead objects */
  952. if (!o_ptr->k_idx) continue;
  953. /* Preserve artifacts or mark them as lost in the history */
  954. if (a_ptr) {
  955. /* Preserve if dungeon creation failed, or preserve mode, and only artifacts not seen */
  956. if ((!character_dungeon || !OPT(adult_no_preserve)) && !object_was_sensed(o_ptr))
  957. {
  958. a_ptr->created = FALSE;
  959. /* Cheat -- Mention preserving */
  960. if (OPT(cheat_peek)) mention_preserve(o_ptr);
  961. }
  962. else
  963. {
  964. /* Mark artifact as lost in logs */
  965. history_lose_artifact(o_ptr->name1);
  966. }
  967. }
  968. /* Monster */
  969. if (o_ptr->held_m_idx)
  970. {
  971. monster_type *m_ptr;
  972. /* Monster */
  973. m_ptr = &mon_list[o_ptr->held_m_idx];
  974. /* Hack -- see above */
  975. m_ptr->hold_o_idx = 0;
  976. }
  977. /* Dungeon */
  978. else
  979. {
  980. /* Get the location */
  981. int y = o_ptr->iy;
  982. int x = o_ptr->ix;
  983. /* Hack -- see above */
  984. cave_o_idx[y][x] = 0;
  985. }
  986. /* Wipe the object */
  987. (void)WIPE(o_ptr, object_type);
  988. }
  989. /* Reset "o_max" */
  990. o_max = 1;
  991. /* Reset "o_cnt" */
  992. o_cnt = 0;
  993. }
  994. /*
  995. * Get and return the index of a "free" object.
  996. *
  997. * This routine should almost never fail, but in case it does,
  998. * we must be sure to handle "failure" of this routine.
  999. */
  1000. s16b o_pop(void)
  1001. {
  1002. int i;
  1003. /* Initial allocation */
  1004. if (o_max < z_info->o_max)
  1005. {
  1006. /* Get next space */
  1007. i = o_max;
  1008. /* Expand object array */
  1009. o_max++;
  1010. /* Count objects */
  1011. o_cnt++;
  1012. /* Use this object */
  1013. return (i);
  1014. }
  1015. /* Recycle dead objects */
  1016. for (i = 1; i < o_max; i++)
  1017. {
  1018. object_type *o_ptr;
  1019. /* Get the object */
  1020. o_ptr = &o_list[i];
  1021. /* Skip live objects */
  1022. if (o_ptr->k_idx) continue;
  1023. /* Count objects */
  1024. o_cnt++;
  1025. /* Use this object */
  1026. return (i);
  1027. }
  1028. /* Warn the player (except during dungeon creation) */
  1029. if (character_dungeon) msg_print("Too many objects!");
  1030. /* Oops */
  1031. return (0);
  1032. }
  1033. /*
  1034. * Get the first object at a dungeon location
  1035. * or NULL if there isn't one.
  1036. */
  1037. object_type *get_first_object(int y, int x)
  1038. {
  1039. s16b o_idx = cave_o_idx[y][x];
  1040. if (o_idx) return (&o_list[o_idx]);
  1041. /* No object */
  1042. return (NULL);
  1043. }
  1044. /*
  1045. * Get the next object in a stack or NULL if there isn't one.
  1046. */
  1047. object_type *get_next_object(const object_type *o_ptr)
  1048. {
  1049. if (o_ptr->next_o_idx) return (&o_list[o_ptr->next_o_idx]);
  1050. /* No more objects */
  1051. return (NULL);
  1052. }
  1053. /*
  1054. * Determine if a weapon is 'blessed'
  1055. */
  1056. bool is_blessed(const object_type *o_ptr)
  1057. {
  1058. bitflag f[OF_SIZE];
  1059. /* Get the flags */
  1060. object_flags(o_ptr, f);
  1061. /* Is the object blessed? */
  1062. return (of_has(f, OF_BLESSED) ? TRUE : FALSE);
  1063. }
  1064. /*
  1065. * Return the "value" of an "unknown" item
  1066. * Make a guess at the value of non-aware items
  1067. */
  1068. static s32b object_value_base(const object_type *o_ptr)
  1069. {
  1070. object_kind *k_ptr = &k_info[o_ptr->k_idx];
  1071. /* Use template cost for aware objects */
  1072. if (object_flavor_is_aware(o_ptr) || o_ptr->ident & IDENT_STORE)
  1073. return k_ptr->cost;
  1074. /* Analyze the type */
  1075. switch (o_ptr->tval)
  1076. {
  1077. case TV_FOOD:
  1078. return 5;
  1079. case TV_POTION:
  1080. case TV_SCROLL:
  1081. return 20;
  1082. case TV_RING:
  1083. case TV_AMULET:
  1084. return 45;
  1085. case TV_WAND:
  1086. return 50;
  1087. case TV_STAFF:
  1088. return 70;
  1089. case TV_ROD:
  1090. return 90;
  1091. }
  1092. return 0;
  1093. }
  1094. /*
  1095. * Return the "real" price of a "known" item, not including discounts.
  1096. *
  1097. * Wand and staffs get cost for each charge.
  1098. *
  1099. * Wearable items (weapons, launchers, jewelry, lights, armour) and ammo
  1100. * are priced according to their power rating. All ammo, and normal (non-ego)
  1101. * torches are scaled down by AMMO_RESCALER to reflect their impermanence.
  1102. */
  1103. static s32b object_value_real(const object_type *o_ptr, int qty, int verbose,
  1104. bool known)
  1105. {
  1106. s32b value, total_value;
  1107. object_kind *k_ptr = &k_info[o_ptr->k_idx];
  1108. s32b power;
  1109. int a = 2;
  1110. int b = 1;
  1111. static file_mode pricing_mode = MODE_WRITE;
  1112. if (wearable_p(o_ptr))
  1113. {
  1114. char buf[1024];
  1115. ang_file *log_file = NULL;
  1116. if (verbose)
  1117. {
  1118. path_build(buf, sizeof(buf), ANGBAND_DIR_USER, "pricing.log");
  1119. log_file = file_open(buf, pricing_mode, FTYPE_TEXT);
  1120. if (!log_file)
  1121. {
  1122. msg_print("Error - can't open pricing.log for writing.");
  1123. exit(1);
  1124. }
  1125. pricing_mode = MODE_APPEND;
  1126. }
  1127. LOG_PRINT1("object is %s", k_name + k_ptr->name);
  1128. power = object_power(o_ptr, verbose, log_file, known);
  1129. value = sign(power) * ((a * power * power) + (b * power));
  1130. if ( (o_ptr->tval == TV_SHOT) || (o_ptr->tval == TV_ARROW) ||
  1131. (o_ptr->tval == TV_BOLT) || ((o_ptr->tval == TV_LIGHT)
  1132. && (o_ptr->sval == SV_LIGHT_TORCH) && !o_ptr->name2) )
  1133. {
  1134. value = value / AMMO_RESCALER;
  1135. if (value < 1) value = 1;
  1136. }
  1137. LOG_PRINT2("a is %d and b is %d\n", a, b);
  1138. LOG_PRINT1("value is %d\n", value);
  1139. total_value = value * qty;
  1140. if (verbose)
  1141. {
  1142. if (!file_close(log_file))
  1143. {
  1144. msg_print("Error - can't close pricing.log file.");
  1145. exit(1);
  1146. }
  1147. }
  1148. if (total_value < 0) total_value = 0;
  1149. return (total_value);
  1150. }
  1151. /* Hack -- "worthless" items */
  1152. if (!k_ptr->cost) return (0L);
  1153. /* Base cost */
  1154. value = k_ptr->cost;
  1155. /* Analyze the item type and quantity*/
  1156. switch (o_ptr->tval)
  1157. {
  1158. /* Wands/Staffs */
  1159. case TV_WAND:
  1160. case TV_STAFF:
  1161. {
  1162. int charges;
  1163. total_value = value * qty;
  1164. /* Calculate number of charges, rounded up */
  1165. charges = o_ptr->pval * qty / o_ptr->number;
  1166. if ((o_ptr->pval * qty) % o_ptr->number != 0) charges++;
  1167. /* Pay extra for charges, depending on standard number of charges */
  1168. total_value += value * charges / 20;
  1169. /* Done */
  1170. break;
  1171. }
  1172. default:
  1173. {
  1174. total_value = value * qty;
  1175. break;
  1176. }
  1177. }
  1178. /* No negative value */
  1179. if (total_value < 0) total_value = 0;
  1180. /* Return the value */
  1181. return (total_value);
  1182. }
  1183. /*
  1184. * Return the price of an item including plusses (and charges).
  1185. *
  1186. * This function returns the "value" of the given item (qty one).
  1187. *
  1188. * Never notice "unknown" bonuses or properties, including "curses",
  1189. * since that would give the player information he did not have.
  1190. *
  1191. * Note that discounted items stay discounted forever.
  1192. */
  1193. s32b object_value(const object_type *o_ptr, int qty, int verbose)
  1194. {
  1195. s32b value;
  1196. if (object_is_known(o_ptr))
  1197. {
  1198. if (cursed_p(o_ptr)) return (0L);
  1199. value = object_value_real(o_ptr, qty, verbose, TRUE);
  1200. }
  1201. else if (wearable_p(o_ptr))
  1202. {
  1203. object_type object_type_body;
  1204. object_type *j_ptr = &object_type_body;
  1205. /* Hack -- Felt cursed items */
  1206. if (object_was_sensed(o_ptr) && cursed_p(o_ptr)) return (0L);
  1207. memcpy(j_ptr, o_ptr, sizeof(object_type));
  1208. /* give j_ptr only the flags known to be in o_ptr */
  1209. object_flags_known(o_ptr, j_ptr->flags);
  1210. if (!object_attack_plusses_are_visible(o_ptr))
  1211. j_ptr->to_h = j_ptr->to_d = 0;
  1212. if (!object_defence_plusses_are_visible(o_ptr))
  1213. j_ptr->to_a = 0;
  1214. value = object_value_real(j_ptr, qty, verbose, FALSE);
  1215. }
  1216. else value = object_value_base(o_ptr) * qty;
  1217. /* Return the final value */
  1218. return (value);
  1219. }
  1220. /*
  1221. * Determine if an item can "absorb" a second item
  1222. *
  1223. * See "object_absorb()" for the actual "absorption" code.
  1224. *
  1225. * If permitted, we allow weapons/armor to stack, if "known".
  1226. *
  1227. * Missiles will combine if both stacks have the same "known" status.
  1228. * This is done to make unidentified stacks of missiles useful.
  1229. *
  1230. * Food, potions, scrolls, and "easy know" items always stack.
  1231. *
  1232. * Chests, and activatable items, except rods, never stack (for various
  1233. * reasons).
  1234. */
  1235. bool object_similar(const object_type *o_ptr, const object_type *j_ptr)
  1236. {
  1237. int total = o_ptr->number + j_ptr->number;
  1238. /* Require identical object types */
  1239. if (o_ptr->k_idx != j_ptr->k_idx) return (0);
  1240. /* Analyze the items */
  1241. switch (o_ptr->tval)
  1242. {
  1243. /* Chests */
  1244. case TV_CHEST:
  1245. {
  1246. /* Never okay */
  1247. return (0);
  1248. }
  1249. /* Gold */
  1250. case TV_GOLD:
  1251. {
  1252. /* Too much gold for one object_type */
  1253. if (o_ptr->pval + j_ptr->pval > MAX_PVAL) return 0;
  1254. }
  1255. /* Food and Potions and Scrolls */
  1256. case TV_FOOD:
  1257. case TV_POTION:
  1258. case TV_SCROLL:
  1259. {
  1260. /* Assume okay */
  1261. break;
  1262. }
  1263. /* Staves and Wands */
  1264. case TV_STAFF:
  1265. case TV_WAND:
  1266. {
  1267. /* Too many charges for one object_type */
  1268. if (o_ptr->pval + j_ptr->pval > MAX_PVAL) return 0;
  1269. }
  1270. /* Rods */
  1271. case TV_ROD:
  1272. {
  1273. /* Assume okay */
  1274. break;
  1275. }
  1276. /* Weaponsm, armour and jewelery */
  1277. case TV_BOW:
  1278. case TV_DIGGING:
  1279. case TV_HAFTED:
  1280. case TV_POLEARM:
  1281. case TV_SWORD:
  1282. case TV_BOOTS:
  1283. case TV_GLOVES:
  1284. case TV_HELM:
  1285. case TV_CROWN:
  1286. case TV_SHIELD:
  1287. case TV_CLOAK:
  1288. case TV_SOFT_ARMOR:
  1289. case TV_HARD_ARMOR:
  1290. case TV_DRAG_ARMOR:
  1291. case TV_RING:
  1292. case TV_AMULET:
  1293. case TV_LIGHT:
  1294. {
  1295. /* Fall through */
  1296. }
  1297. /* Missiles */
  1298. case TV_BOLT:
  1299. case TV_ARROW:
  1300. case TV_SHOT:
  1301. {
  1302. /* Require identical "bonuses" */
  1303. if (o_ptr->to_h != j_ptr->to_h) return (FALSE);
  1304. if (o_ptr->to_d != j_ptr->to_d) return (FALSE);
  1305. if (o_ptr->to_a != j_ptr->to_a) return (FALSE);
  1306. /* Require identical "pval" code */
  1307. if (o_ptr->pval != j_ptr->pval) return (FALSE);
  1308. /* Require identical "artifact" names */
  1309. if (o_ptr->name1 != j_ptr->name1) return (FALSE);
  1310. /* Require identical "ego-item" names */
  1311. if (o_ptr->name2 != j_ptr->name2) return (FALSE);
  1312. /* Hack - Never stack recharging items */
  1313. if ((o_ptr->timeout || j_ptr->timeout) && o_ptr->tval != TV_LIGHT)
  1314. return FALSE;
  1315. /* Lights must have same amount of fuel */
  1316. else if (o_ptr->timeout != j_ptr->timeout && o_ptr->tval == TV_LIGHT)
  1317. return FALSE;
  1318. /* Require identical "values" */
  1319. if (o_ptr->ac != j_ptr->ac) return (FALSE);
  1320. if (o_ptr->dd != j_ptr->dd) return (FALSE);
  1321. if (o_ptr->ds != j_ptr->ds) return (FALSE);
  1322. /* Probably okay */
  1323. break;
  1324. }
  1325. /* Various */
  1326. default:
  1327. {
  1328. /* Probably okay */
  1329. break;
  1330. }
  1331. }
  1332. /* Hack -- Require compatible inscriptions */
  1333. if (o_ptr->note != j_ptr->note)
  1334. {
  1335. /* Never combine different inscriptions */
  1336. if (o_ptr->note && j_ptr->note) return (0);
  1337. }
  1338. /* Different flags */
  1339. if (!of_is_equal(o_ptr->flags, j_ptr->flags))
  1340. return FALSE;
  1341. /* Maximal "stacking" limit */
  1342. if (total >= MAX_STACK_SIZE) return (0);
  1343. /* They match, so they must be similar */
  1344. return (TRUE);
  1345. }
  1346. /*
  1347. * Allow one item to "absorb" another, assuming they are similar.
  1348. *
  1349. * The blending of the "note" field assumes that either (1) one has an
  1350. * inscription and the other does not, or (2) neither has an inscription.
  1351. * In both these cases, we can simply use the existing note, unless the
  1352. * blending object has a note, in which case we use that note.
  1353. *
  1354. * The blending of the "discount" field assumes that either (1) one is a
  1355. * special inscription and one is nothing, or (2) one is a discount and
  1356. * one is a smaller discount, or (3) one is a discount and one is nothing,
  1357. * or (4) both are nothing. In all of these cases, we can simply use the
  1358. * "maximum" of the two "discount" fields.
  1359. *
  1360. * These assumptions are enforced by the "object_similar()" code.
  1361. */
  1362. void object_absorb(object_type *o_ptr, const object_type *j_ptr)
  1363. {
  1364. int total = o_ptr->number + j_ptr->number;
  1365. /* Add together the item counts */
  1366. o_ptr->number = ((total < MAX_STACK_SIZE) ? total : (MAX_STACK_SIZE - 1));
  1367. /* Blend all knowledge */
  1368. o_ptr->ident |= (j_ptr->ident & ~IDENT_EMPTY);
  1369. of_union(o_ptr->known_flags, j_ptr->known_flags);
  1370. /* Hack -- Blend "notes" */
  1371. if (j_ptr->note != 0) o_ptr->note = j_ptr->note;
  1372. /*
  1373. * Hack -- if rods are stacking, re-calculate the
  1374. * pvals (maximum timeouts) and current timeouts together
  1375. */
  1376. if (o_ptr->tval == TV_ROD)
  1377. {
  1378. o_ptr->pval = total * j_ptr->pval;
  1379. o_ptr->timeout += j_ptr->timeout;
  1380. }
  1381. /* Hack -- if wands or staves are stacking, combine the charges */
  1382. /* If gold is stacking combine the amount */
  1383. if (o_ptr->tval == TV_WAND || o_ptr->tval == TV_STAFF ||
  1384. o_ptr->tval == TV_GOLD)
  1385. {
  1386. int total = o_ptr->pval + j_ptr->pval;
  1387. o_ptr->pval = total >= MAX_PVAL ? MAX_PVAL : total;
  1388. }
  1389. if ((o_ptr->origin != j_ptr->origin) ||
  1390. (o_ptr->origin_depth != j_ptr->origin_depth) ||
  1391. (o_ptr->origin_xtra != j_ptr->origin_xtra))
  1392. {
  1393. int act = 2;
  1394. if ((o_ptr->origin == ORIGIN_DROP) && (o_ptr->origin == j_ptr->origin))
  1395. {
  1396. monster_race *r_ptr = &r_info[o_ptr->origin_xtra];
  1397. monster_race *s_ptr = &r_info[j_ptr->origin_xtra];
  1398. bool r_uniq = rf_has(r_ptr->flags, RF_UNIQUE) ? TRUE : FALSE;
  1399. bool s_uniq = rf_has(s_ptr->flags, RF_UNIQUE) ? TRUE : FALSE;
  1400. if (r_uniq && !s_uniq) act = 0;
  1401. else if (s_uniq && !r_uniq) act = 1;
  1402. else act = 2;
  1403. }
  1404. switch (act)
  1405. {
  1406. /* Overwrite with j_ptr */
  1407. case 1:
  1408. {
  1409. o_ptr->origin = j_ptr->origin;
  1410. o_ptr->origin_depth = j_ptr->origin_depth;
  1411. o_ptr->origin_xtra = j_ptr->origin_xtra;
  1412. }
  1413. /* Set as "mixed" */
  1414. case 2:
  1415. {
  1416. o_ptr->origin = ORIGIN_MIXED;
  1417. }
  1418. }
  1419. }
  1420. }
  1421. /*
  1422. * Wipe an object clean.
  1423. */
  1424. void object_wipe(object_type *o_ptr)
  1425. {
  1426. /* Wipe the structure */
  1427. (void)WIPE(o_ptr, object_type);
  1428. }
  1429. /*
  1430. * Prepare an object based on an existing object
  1431. */
  1432. void object_copy(object_type *o_ptr, const object_type *j_ptr)
  1433. {
  1434. /* Copy the structure */
  1435. COPY(o_ptr, j_ptr, object_type);
  1436. }
  1437. /*
  1438. * Prepare an object `dst` representing `amt` objects, based on an existing
  1439. * object `src` representing at least `amt` objects.
  1440. *
  1441. * Takes care of the charge redistribution concerns of stacked items.
  1442. */
  1443. void object_copy_amt(object_type *dst, object_type *src, int amt)
  1444. {
  1445. const object_kind *k_ptr = &k_info[src->k_idx];
  1446. int charge_time = randcalc(k_ptr->time, 0, AVERAGE), max_time;
  1447. /* Get a copy of the object */
  1448. object_copy(dst, src);
  1449. /* Modify quantity */
  1450. dst->number = amt;
  1451. /*
  1452. * If the item has charges/timeouts, set them to the correct level
  1453. * too. We split off the same amount as distribute_charges.
  1454. */
  1455. if (src->tval == TV_WAND || src->tval == TV_STAFF)
  1456. {
  1457. dst->pval = src->pval * amt / src->number;
  1458. }
  1459. if (src->tval == TV_ROD)
  1460. {
  1461. max_time = charge_time * amt;
  1462. if (src->timeout > max_time)
  1463. dst->timeout = max_time;
  1464. else
  1465. dst->timeout = src->timeout;
  1466. }
  1467. }
  1468. /**
  1469. * Find and return the index to the oldest object on the given grid marked as
  1470. * "squelch".
  1471. */
  1472. static s16b floor_get_idx_oldest_squelched(int y, int x)
  1473. {
  1474. s16b squelch_idx = 0;
  1475. s16b this_o_idx;
  1476. object_type *o_ptr = NULL;
  1477. for (this_o_idx = cave_o_idx[y][x]; this_o_idx; this_o_idx = o_ptr->next_o_idx)
  1478. {
  1479. o_ptr = &o_list[this_o_idx];
  1480. if (squelch_hide_item(o_ptr))
  1481. squelch_idx = this_o_idx;
  1482. }
  1483. return squelch_idx;
  1484. }
  1485. /*
  1486. * Let the floor carry an object, deleting old squelched items if necessary
  1487. */
  1488. s16b floor_carry(int y, int x, object_type *j_ptr)
  1489. {
  1490. int n = 0;
  1491. s16b o_idx;
  1492. s16b this_o_idx, next_o_idx = 0;
  1493. /* Scan objects in that grid for combination */
  1494. for (this_o_idx = cave_o_idx[y][x]; this_o_idx; this_o_idx = next_o_idx)
  1495. {
  1496. object_type *o_ptr = &o_list[this_o_idx];
  1497. /* Get the next object */
  1498. next_o_idx = o_ptr->next_o_idx;
  1499. /* Check for combination */
  1500. if (object_similar(o_ptr, j_ptr))
  1501. {
  1502. /* Combine the items */
  1503. object_absorb(o_ptr, j_ptr);
  1504. /* Result */
  1505. return (this_o_idx);
  1506. }
  1507. /* Count objects */
  1508. n++;
  1509. }
  1510. /* Option -- disallow stacking */
  1511. if (OPT(adult_no_stacking) && n) return (0);
  1512. /* The stack is already too large */
  1513. if (n >= MAX_FLOOR_STACK)
  1514. {
  1515. /* Squelch the oldest squelched object */
  1516. s16b squelch_idx = floor_get_idx_oldest_squelched(y, x);
  1517. if (squelch_idx)
  1518. delete_object_idx(squelch_idx);
  1519. else
  1520. return 0;
  1521. }
  1522. /* Make an object */
  1523. o_idx = o_pop();
  1524. /* Success */
  1525. if (o_idx)
  1526. {
  1527. object_type *o_ptr;
  1528. /* Get the object */
  1529. o_ptr = &o_list[o_idx];
  1530. /* Structure Copy */
  1531. object_copy(o_ptr, j_ptr);
  1532. /* Location */
  1533. o_ptr->iy = y;
  1534. o_ptr->ix = x;
  1535. /* Forget monster */
  1536. o_ptr->held_m_idx = 0;
  1537. /* Link the object to the pile */
  1538. o_ptr->next_o_idx = cave_o_idx[y][x];
  1539. /* Link the floor to the object */
  1540. cave_o_idx[y][x] = o_idx;
  1541. /* Notice */
  1542. note_spot(y, x);
  1543. /* Redraw */
  1544. light_spot(y, x);
  1545. }
  1546. /* Result */
  1547. return (o_idx);
  1548. }
  1549. /*
  1550. * Let an object fall to the ground at or near a location.
  1551. *
  1552. * The initial location is assumed to be "in_bounds_fully()".
  1553. *
  1554. * This function takes a parameter "chance". This is the percentage
  1555. * chance that the item will "disappear" instead of drop. If the object
  1556. * has been thrown, then this is the chance of disappearance on contact.
  1557. *
  1558. * This function will produce a description of a drop event under the player
  1559. * when "verbose" is true.
  1560. *
  1561. * We check several locations to see if we can find a location at which
  1562. * the object can combine, stack, or be placed. Artifacts will try very
  1563. * hard to be placed, including "teleporting" to a useful grid if needed.
  1564. */
  1565. void drop_near(object_type *j_ptr, int chance, int y, int x, bool verbose)
  1566. {
  1567. int i, k, n, d, s;
  1568. int bs, bn;
  1569. int by, bx;
  1570. int dy, dx;
  1571. int ty, tx;
  1572. object_type *o_ptr;
  1573. char o_name[80];
  1574. bool flag = FALSE;
  1575. bool plural = FALSE;
  1576. /* Extract plural */
  1577. if (j_ptr->number != 1) plural = TRUE;
  1578. /* Describe object */
  1579. object_desc(o_name, sizeof(o_name), j_ptr, ODESC_BASE);
  1580. /* Handle normal "breakage" */
  1581. if (!artifact_p(j_ptr) && (randint0(100) < chance))
  1582. {
  1583. /* Message */
  1584. msg_format("The %s break%s.", o_name, PLURAL(plural));
  1585. /* Failure */
  1586. return;
  1587. }
  1588. /* Score */
  1589. bs = -1;
  1590. /* Picker */
  1591. bn = 0;
  1592. /* Default */
  1593. by = y;
  1594. bx = x;
  1595. /* Scan local grids */
  1596. for (dy = -3; dy <= 3; dy++)
  1597. {
  1598. /* Scan local grids */
  1599. for (dx = -3; dx <= 3; dx++)
  1600. {
  1601. bool comb = FALSE;
  1602. /* Calculate actual distance */
  1603. d = (dy * dy) + (dx * dx);
  1604. /* Ignore distant grids */
  1605. if (d > 10) continue;
  1606. /* Location */
  1607. ty = y + dy;
  1608. tx = x + dx;
  1609. /* Skip illegal grids */
  1610. if (!in_bounds_fully(ty, tx)) continue;
  1611. /* Require line of sight */
  1612. if (!los(y, x, ty, tx)) continue;
  1613. /* Require floor space */
  1614. if (cave_feat[ty][tx] != FEAT_FLOOR) continue;
  1615. /* No objects */
  1616. k = 0;
  1617. n = 0;
  1618. /* Scan objects in that grid */
  1619. for (o_ptr = get_first_object(ty, tx); o_ptr;
  1620. o_ptr = get_next_object(o_ptr))
  1621. {
  1622. /* Check for possible combination */
  1623. if (object_similar(o_ptr, j_ptr)) comb = TRUE;
  1624. /* Count objects */
  1625. if (!squelch_hide_item(o_ptr))
  1626. k++;
  1627. else
  1628. n++;
  1629. }
  1630. /* Add new object */
  1631. if (!comb) k++;
  1632. /* Option -- disallow stacking */
  1633. if (OPT(adult_no_stacking) && (k > 1)) continue;
  1634. /* Paranoia? */
  1635. if ((k + n) > MAX_FLOOR_STACK &&
  1636. !floor_get_idx_oldest_squelched(ty, tx)) continue;
  1637. /* Calculate score */
  1638. s = 1000 - (d + k * 5);
  1639. /* Skip bad values */
  1640. if (s < bs) continue;
  1641. /* New best value */
  1642. if (s > bs) bn = 0;
  1643. /* Apply the randomizer to equivalent values */
  1644. if ((++bn >= 2) && (randint0(bn) != 0)) continue;
  1645. /* Keep score */
  1646. bs = s;
  1647. /* Track it */
  1648. by = ty;
  1649. bx = tx;
  1650. /* Okay */
  1651. flag = TRUE;
  1652. }
  1653. }
  1654. /* Handle lack of space */
  1655. if (!flag && !artifact_p(j_ptr))
  1656. {
  1657. /* Message */
  1658. msg_format("The %s disappear%s.", o_name, PLURAL(plural));
  1659. /* Debug */
  1660. if (p_ptr->wizard) msg_print("Breakage (no floor space).");
  1661. /* Failure */
  1662. return;
  1663. }
  1664. /* Find a grid */
  1665. for (i = 0; !flag; i++)
  1666. {
  1667. /* Bounce around */
  1668. if (i < 1000)
  1669. {
  1670. ty = rand_spread(by, 1);
  1671. tx = rand_spread(bx, 1);
  1672. }
  1673. /* Random locations */
  1674. else
  1675. {
  1676. ty = randint0(level_hgt);
  1677. tx = randint0(level_wid);
  1678. }
  1679. /* Require floor space */
  1680. if (cave_feat[ty][tx] != FEAT_FLOOR) continue;
  1681. /* Bounce to that location */
  1682. by = ty;
  1683. bx = tx;
  1684. /* Require floor space */
  1685. if (!cave_clean_bold(by, bx)) continue;
  1686. /* Okay */
  1687. flag = TRUE;
  1688. }
  1689. /* Give it to the floor */
  1690. if (!floor_carry(by, bx, j_ptr))
  1691. {
  1692. artifact_type *a_ptr = artifact_of(j_ptr);
  1693. /* Message */
  1694. msg_format("The %s disappear%s.", o_name, PLURAL(plural));
  1695. /* Debug */
  1696. if (p_ptr->wizard) msg_print("Breakage (too many objects).");
  1697. if (a_ptr) a_ptr->created = FALSE;
  1698. /* Failure */
  1699. return;
  1700. }
  1701. /* Sound */
  1702. sound(MSG_DROP);
  1703. /* Message when an object falls under the player */
  1704. if (verbose && (cave_m_idx[by][bx] < 0) && !squelch_item_ok(j_ptr))
  1705. {
  1706. msg_print("You feel something roll beneath your feet.");
  1707. }
  1708. }
  1709. /*
  1710. * Scatter some "great" objects near the player
  1711. */
  1712. void acquirement(int y1, int x1, int level, int num, bool great)
  1713. {
  1714. object_type *i_ptr;
  1715. object_type object_type_body;
  1716. /* Acquirement */
  1717. while (num--)
  1718. {
  1719. /* Get local object */
  1720. i_ptr = &object_type_body;
  1721. /* Wipe the object */
  1722. object_wipe(i_ptr);
  1723. /* Make a good (or great) object (if possible) */
  1724. if (!make_object(i_ptr, level, TRUE, great)) continue;
  1725. i_ptr->origin = ORIGIN_ACQUIRE;
  1726. i_ptr->origin_depth = p_ptr->depth;
  1727. /* Drop the object */
  1728. drop_near(i_ptr, 0, y1, x1, TRUE);
  1729. }
  1730. }
  1731. /*
  1732. * Describe the charges on an item in the inventory.
  1733. */
  1734. void inven_item_charges(int item)
  1735. {
  1736. object_type *o_ptr = &inventory[item];
  1737. /* Require staff/wand */
  1738. if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND)) return;
  1739. /* Require known item */
  1740. if (!object_is_known(o_ptr)) return;
  1741. /* Print a message */
  1742. msg_format("You have %d charge%s remaining.", o_ptr->pval,
  1743. (o_ptr->pval != 1) ? "s" : "");
  1744. }
  1745. /*
  1746. * Describe an item in the inventory.
  1747. */
  1748. void inven_item_describe(int item)
  1749. {
  1750. object_type *o_ptr = &inventory[item];
  1751. char o_name[80];
  1752. if (artifact_p(o_ptr) && object_is_known(o_ptr))
  1753. {
  1754. /* Get a description */
  1755. object_desc(o_name, sizeof(o_name), o_ptr, ODESC_FULL);
  1756. /* Print a message */
  1757. msg_format("You no longer have the %s (%c).", o_name, index_to_label(item));
  1758. }
  1759. else
  1760. {
  1761. /* Get a description */
  1762. object_desc(o_name, sizeof(o_name), o_ptr, ODESC_PREFIX | ODESC_FULL);
  1763. /* Print a message */
  1764. msg_format("You have %s (%c).", o_name, index_to_label(item));
  1765. }
  1766. }
  1767. /*
  1768. * Increase the "number" of an item in the inventory
  1769. */
  1770. void inven_item_increase(int item, int num)
  1771. {
  1772. object_type *o_ptr = &inventory[item];
  1773. /* Apply */
  1774. num += o_ptr->number;
  1775. /* Bounds check */
  1776. if (num > 255) num = 255;
  1777. else if (num < 0) num = 0;
  1778. /* Un-apply */
  1779. num -= o_ptr->number;
  1780. /* Change the number and weight */
  1781. if (num)
  1782. {
  1783. /* Add the number */
  1784. o_ptr->number += num;
  1785. /* Add the weight */
  1786. p_ptr->total_weight += (num * o_ptr->weight);
  1787. /* Recalculate bonuses */
  1788. p_ptr->update |= (PU_BONUS);
  1789. /* Recalculate mana XXX */
  1790. p_ptr->update |= (PU_MANA);
  1791. /* Combine the pack */
  1792. p_ptr->notice |= (PN_COMBINE);
  1793. /* Redraw stuff */
  1794. p_ptr->redraw |= (PR_INVEN | PR_EQUIP);
  1795. }
  1796. }
  1797. /**
  1798. * Save the size of the quiver.
  1799. */
  1800. void save_quiver_size(void)
  1801. {
  1802. int i, count = 0;
  1803. for (i = QUIVER_START; i < QUIVER_END; i++)
  1804. if (inventory[i].k_idx) count += inventory[i].number;
  1805. p_ptr->quiver_size = count;
  1806. p_ptr->quiver_slots = (count + 98) / 99;
  1807. p_ptr->quiver_remainder = count % 99;
  1808. }
  1809. /**
  1810. * Compare ammunition from slots (0-9); used for sorting.
  1811. *
  1812. * \returns -1 if slot1 should come first, 1 if slot2 should come first, or 0.
  1813. */
  1814. int compare_ammo(int slot1, int slot2)
  1815. {
  1816. /* Right now there is no sorting criteria */
  1817. return 0;
  1818. }
  1819. /**
  1820. * Swap ammunition between quiver slots (0-9).
  1821. */
  1822. void swap_quiver_slots(int slot1, int slot2)
  1823. {
  1824. int i = slot1 + QUIVER_START;
  1825. int j = slot2 + QUIVER_START;
  1826. object_type o;
  1827. object_copy(&o, &inventory[i]);
  1828. object_copy(&inventory[i], &inventory[j]);
  1829. object_copy(&inventory[j], &o);
  1830. }
  1831. /**
  1832. * Sorts the quiver--ammunition inscribed with @fN prefers to end up in quiver
  1833. * slot N.
  1834. */
  1835. void sort_quiver(void)
  1836. {
  1837. /* Ammo slots go from 0-9; these indices correspond to the range of
  1838. * (QUIVER_START) - (QUIVER_END-1) in inventory[].
  1839. */
  1840. bool locked[QUIVER_SIZE] = {FALSE, FALSE, FALSE, FALSE, FALSE,
  1841. FALSE, FALSE, FALSE, FALSE, FALSE};
  1842. int desired[QUIVER_SIZE] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  1843. int i, j, k;
  1844. object_type *o_ptr;
  1845. /* Here we figure out which slots have inscribed ammo, and whether that
  1846. * ammo is already in the slot it "wants" to be in or not.
  1847. */
  1848. for (i=0; i < QUIVER_SIZE; i++)
  1849. {
  1850. j = QUIVER_START + i;
  1851. o_ptr = &inventory[j];
  1852. /* Skip this slot if it doesn't have ammo */
  1853. if (!o_ptr->k_idx) continue;
  1854. /* Figure out which slot this ammo prefers, if any */
  1855. k = get_inscribed_ammo_slot(o_ptr);
  1856. if (!k) continue;
  1857. k -= QUIVER_START;
  1858. if (k == i) locked[i] = TRUE;
  1859. if (desired[k] < 0) desired[k] = i;
  1860. }
  1861. /* For items which had a preference that was not fulfilled, we will swap
  1862. * them into the slot as long as it isn't already locked.
  1863. */
  1864. for (i=0; i < QUIVER_SIZE; i++)
  1865. {
  1866. if (locked[i] || desired[i] < 0) continue;
  1867. /* item in slot 'desired[i]' desires to be in slot 'i' */
  1868. swap_quiver_slots(desired[i], i);
  1869. locked[i] = TRUE;
  1870. }
  1871. /* Now we need to compact ammo which isn't in a preferrred slot towards the
  1872. * "front" of the quiver */
  1873. for (i=0; i < QUIVER_SIZE; i++)
  1874. {
  1875. /* If the slot isn't empty, skip it */
  1876. if (inventory[QUIVER_START + i].k_idx) continue;
  1877. /* Start from the end and find an unlocked item to put here. */
  1878. for (j=QUIVER_SIZE - 1; j > i; j--)
  1879. {
  1880. if (!inventory[QUIVER_START + j].k_idx || locked[j]) continue;
  1881. swap_quiver_slots(i, j);
  1882. break;
  1883. }
  1884. }
  1885. /* Now we will sort all other ammo using a simple insertion sort */
  1886. for (i=0; i < QUIVER_SIZE; i++)
  1887. {
  1888. k = i;
  1889. if (!locked[k])
  1890. for (j = i + 1; j < QUIVER_SIZE; j++)
  1891. if (!locked[j] && compare_ammo(k, j) > 0)
  1892. swap_quiver_slots(j, k);
  1893. }
  1894. }
  1895. /*
  1896. * Shifts ammo at or above the item slot towards the end of the quiver, making
  1897. * room for a new piece of ammo.
  1898. */
  1899. void open_quiver_slot(int slot)
  1900. {
  1901. int i, pref;
  1902. int dest = QUIVER_END - 1;
  1903. /* This should only be used on ammunition */
  1904. if (slot < QUIVER_START) return;
  1905. /* Quiver is full */
  1906. if (inventory[QUIVER_END - 1].k_idx) return;
  1907. /* Find the first open quiver slot */
  1908. while (inventory[dest].k_idx) dest++;
  1909. /* Swap things with the space one higher (essentially moving the open space
  1910. * towards our goal slot. */
  1911. for (i = dest - 1; i >= slot; i--)
  1912. {
  1913. /* If we have an item with an inscribed location (and it's in */
  1914. /* that location) then we won't move it. */
  1915. pref = get_inscribed_ammo_slot(&inventory[i]);
  1916. if (i != slot && pref && pref == i) continue;
  1917. /* Copy the item up and wipe the old slot */
  1918. COPY(&inventory[dest], &inventory[i], object_type);
  1919. dest = i;
  1920. object_wipe(&inventory[dest]);
  1921. }
  1922. }
  1923. /*
  1924. * Erase an inventory slot if it has no more items
  1925. */
  1926. void inven_item_optimize(int item)
  1927. {
  1928. object_type *o_ptr = &inventory[item];
  1929. int i, j, slot, limit;
  1930. /* Save a possibly new quiver size */
  1931. if (item >= QUIVER_START) save_quiver_size();
  1932. /* Only optimize real items which are empty */
  1933. if (!o_ptr->k_idx || o_ptr->number) return;
  1934. /* Items in the pack are treated differently from other items */
  1935. if (item < INVEN_WIELD)
  1936. {
  1937. p_ptr->inven_cnt--;
  1938. p_ptr->redraw |= PR_INVEN;
  1939. limit = INVEN_MAX_PACK;
  1940. }
  1941. /* Items in the quiver and equipped items are (mostly) treated similarly */
  1942. else
  1943. {
  1944. p_ptr->equip_cnt--;
  1945. p_ptr->redraw |= PR_EQUIP;
  1946. limit = item >= QUIVER_START ? QUIVER_END : 0;
  1947. }
  1948. /* If the item is equipped (but not in the quiver), there is no need to */
  1949. /* slide other items. Bonuses and such will need to be recalculated */
  1950. if (!limit)
  1951. {
  1952. /* Erase the empty slot */
  1953. object_wipe(&inventory[item]);
  1954. /* Recalculate stuff */
  1955. p_ptr->update |= (PU_BONUS);
  1956. p_ptr->update |= (PU_TORCH);
  1957. p_ptr->update |= (PU_MANA);
  1958. return;
  1959. }
  1960. /* Slide everything down */
  1961. for (j = item, i = item + 1; i < limit; i++)
  1962. {
  1963. if (limit == QUIVER_END && inventory[i].k_idx)
  1964. {
  1965. /* If we have an item with an inscribed location (and it's in */
  1966. /* that location) then we won't move it. */
  1967. slot = get_inscribed_ammo_slot(&inventory[i]);
  1968. if (slot && slot == i)
  1969. continue;
  1970. }
  1971. COPY(&inventory[j], &inventory[i], object_type);
  1972. j = i;
  1973. }
  1974. /* Reorder the quiver if necessary */
  1975. if (item >= QUIVER_START) sort_quiver();
  1976. /* Wipe the left-over object on the end */
  1977. object_wipe(&inventory[j]);
  1978. /* Inventory has changed, so disable repeat command */
  1979. cmd_disable_repeat();
  1980. }
  1981. /*
  1982. * Describe the charges on an item on the floor.
  1983. */
  1984. void floor_item_charges(int item)
  1985. {
  1986. object_type *o_ptr = &o_list[item];
  1987. /* Require staff/wand */
  1988. if ((o_ptr->tval != TV_STAFF) && (o_ptr->tval != TV_WAND)) return;
  1989. /* Require known item */
  1990. if (!object_is_known(o_ptr)) return;
  1991. /* Print a message */
  1992. msg_format("There are %d charge%s remaining.", o_ptr->pval,
  1993. (o_ptr->pval != 1) ? "s" : "");
  1994. }
  1995. /*
  1996. * Describe an item in the inventory.
  1997. */
  1998. void floor_item_describe(int item)
  1999. {
  2000. object_type *o_ptr = &o_list[item];
  2001. char o_name[80];
  2002. /* Get a description */
  2003. object_desc(o_name, sizeof(o_name), o_ptr, ODESC_PREFIX | ODESC_FULL);
  2004. /* Print a message */
  2005. msg_format("You see %s.", o_name);
  2006. }
  2007. /*
  2008. * Increase the "number" of an item on the floor
  2009. */
  2010. void floor_item_increase(int item, int num)
  2011. {
  2012. object_type *o_ptr = &o_list[item];
  2013. /* Apply */
  2014. num += o_ptr->number;
  2015. /* Bounds check */
  2016. if (num > 255) num = 255;
  2017. else if (num < 0) num = 0;
  2018. /* Un-apply */
  2019. num -= o_ptr->number;
  2020. /* Change the number */
  2021. o_ptr->number += num;
  2022. }
  2023. /*
  2024. * Optimize an item on the floor (destroy "empty" items)
  2025. */
  2026. void floor_item_optimize(int item)
  2027. {
  2028. object_type *o_ptr = &o_list[item];
  2029. /* Paranoia -- be sure it exists */
  2030. if (!o_ptr->k_idx) return;
  2031. /* Only optimize empty items */
  2032. if (o_ptr->number) return;
  2033. /* Delete the object */
  2034. delete_object_idx(item);
  2035. }
  2036. /*
  2037. * Check if we have space for an item in the pack without overflow
  2038. */
  2039. bool inven_carry_okay(const object_type *o_ptr)
  2040. {
  2041. /* Empty slot? */
  2042. if (p_ptr->inven_cnt < INVEN_MAX_PACK) return TRUE;
  2043. /* Check if it can stack */
  2044. if (inven_stack_okay(o_ptr)) return TRUE;
  2045. /* Nope */
  2046. return FALSE;
  2047. }
  2048. /*
  2049. * Check to see if an item is stackable in the inventory
  2050. */
  2051. bool inven_stack_okay(const object_type *o_ptr)
  2052. {
  2053. /* Similar slot? */
  2054. int j;
  2055. /* If our pack is full and we're adding too many missiles, there won't be
  2056. * enough room in the quiver, so don't check it. */
  2057. int limit;
  2058. if (!pack_is_full())
  2059. /* The pack has more room */
  2060. limit = ALL_INVEN_TOTAL;
  2061. else if (p_ptr->quiver_remainder == 0)
  2062. /* Quiver already maxed out */
  2063. limit = INVEN_PACK;
  2064. else if (p_ptr->quiver_remainder + o_ptr->number > 99)
  2065. /* Too much new ammo */
  2066. limit = INVEN_PACK;
  2067. else
  2068. limit = ALL_INVEN_TOTAL;
  2069. for (j = 0; j < limit; j++)
  2070. {
  2071. object_type *j_ptr = &inventory[j];
  2072. /* Skip equipped items and non-objects */
  2073. if (j >= INVEN_PACK && j < QUIVER_START) continue;
  2074. if (!j_ptr->k_idx) continue;
  2075. /* Check if the two items can be combined */
  2076. if (object_similar(j_ptr, o_ptr)) return (TRUE);
  2077. }
  2078. return (FALSE);
  2079. }
  2080. /*
  2081. * Add an item to the players inventory, and return the slot used.
  2082. *
  2083. * If the new item can combine with an existing item in the inventory,
  2084. * it will do so, using "object_similar()" and "object_absorb()", else,
  2085. * the item will be placed into the "proper" location in the inventory.
  2086. *
  2087. * This function can be used to "over-fill" the player's pack, but only
  2088. * once, and such an action must trigger the "overflow" code immediately.
  2089. * Note that when the pack is being "over-filled", the new item must be
  2090. * placed into the "overflow" slot, and the "overflow" must take place
  2091. * before the pack is reordered, but (optionally) after the pack is
  2092. * combined. This may be tricky. See "dungeon.c" for info.
  2093. *
  2094. * Note that this code must remove any location/stack information
  2095. * from the object once it is placed into the inventory.
  2096. */
  2097. s16b inven_carry(object_type *o_ptr)
  2098. {
  2099. int i, j, k;
  2100. int n = -1;
  2101. object_type *j_ptr;
  2102. /* Apply an autoinscription */
  2103. apply_autoinscription(o_ptr);
  2104. /* Check for combining */
  2105. for (j = 0; j < INVEN_PACK; j++)
  2106. {
  2107. j_ptr = &inventory[j];
  2108. /* Skip non-objects */
  2109. if (!j_ptr->k_idx) continue;
  2110. /* Hack -- track last item */
  2111. n = j;
  2112. /* Check if the two items can be combined */
  2113. if (object_similar(j_ptr, o_ptr))
  2114. {
  2115. /* Combine the items */
  2116. object_absorb(j_ptr, o_ptr);
  2117. /* Increase the weight */
  2118. p_ptr->total_weight += (o_ptr->number * o_ptr->weight);
  2119. /* Recalculate bonuses */
  2120. p_ptr->update |= (PU_BONUS);
  2121. /* Redraw stuff */
  2122. p_ptr->redraw |= (PR_INVEN);
  2123. /* Save quiver size */
  2124. save_quiver_size();
  2125. /* Success */
  2126. return (j);
  2127. }
  2128. }
  2129. /* Paranoia */
  2130. if (p_ptr->inven_cnt > INVEN_MAX_PACK) return (-1);
  2131. /* Find an empty slot */
  2132. for (j = 0; j <= INVEN_MAX_PACK; j++)
  2133. {
  2134. j_ptr = &inventory[j];
  2135. /* Use it if found */
  2136. if (!j_ptr->k_idx) break;
  2137. }
  2138. /* Use that slot */
  2139. i = j;
  2140. /* Reorder the pack */
  2141. if (i < INVEN_MAX_PACK)
  2142. {
  2143. s32b o_value, j_value;
  2144. /* Get the "value" of the item */
  2145. o_value = k_info[o_ptr->k_idx].cost;
  2146. /* Scan every occupied slot */
  2147. for (j = 0; j < INVEN_MAX_PACK; j++)
  2148. {
  2149. j_ptr = &inventory[j];
  2150. /* Use empty slots */
  2151. if (!j_ptr->k_idx) break;
  2152. /* Hack -- readable books always come first */
  2153. if ((o_ptr->tval == cp_ptr->spell_book) &&
  2154. (j_ptr->tval != cp_ptr->spell_book)) break;
  2155. if ((j_ptr->tval == cp_ptr->spell_book) &&
  2156. (o_ptr->tval != cp_ptr->spell_book)) continue;
  2157. /* Objects sort by decreasing type */
  2158. if (o_ptr->tval > j_ptr->tval) break;
  2159. if (o_ptr->tval < j_ptr->tval) continue;
  2160. /* Non-aware (flavored) items always come last */
  2161. if (!object_flavor_is_aware(o_ptr)) continue;
  2162. if (!object_flavor_is_aware(j_ptr)) break;
  2163. /* Objects sort by increasing sval */
  2164. if (o_ptr->sval < j_ptr->sval) break;
  2165. if (o_ptr->sval > j_ptr->sval) continue;
  2166. /* Unidentified objects always come last */
  2167. if (!object_is_known(o_ptr)) continue;
  2168. if (!object_is_known(j_ptr)) break;
  2169. /* Lights sort by decreasing fuel */
  2170. if (o_ptr->tval == TV_LIGHT)
  2171. {
  2172. if (o_ptr->pval > j_ptr->pval) break;
  2173. if (o_ptr->pval < j_ptr->pval) continue;
  2174. }
  2175. /* Determine the "value" of the pack item */
  2176. j_value = k_info[j_ptr->k_idx].cost;
  2177. /* Objects sort by decreasing value */
  2178. if (o_value > j_value) break;
  2179. if (o_value < j_value) continue;
  2180. }
  2181. /* Use that slot */
  2182. i = j;
  2183. /* Slide objects */
  2184. for (k = n; k >= i; k--)
  2185. {
  2186. /* Hack -- Slide the item */
  2187. object_copy(&inventory[k+1], &inventory[k]);
  2188. }
  2189. /* Wipe the empty slot */
  2190. object_wipe(&inventory[i]);
  2191. }
  2192. /* Copy the item */
  2193. object_copy(&inventory[i], o_ptr);
  2194. /* Get the new object */
  2195. j_ptr = &inventory[i];
  2196. /* Forget stack */
  2197. j_ptr->next_o_idx = 0;
  2198. /* Forget monster */
  2199. j_ptr->held_m_idx = 0;
  2200. /* Forget location */
  2201. j_ptr->iy = j_ptr->ix = 0;
  2202. /* No longer marked */
  2203. j_ptr->marked = FALSE;
  2204. /* Increase the weight */
  2205. p_ptr->total_weight += (j_ptr->number * j_ptr->weight);
  2206. /* Count the items */
  2207. p_ptr->inven_cnt++;
  2208. /* Recalculate bonuses */
  2209. p_ptr->update |= (PU_BONUS);
  2210. /* Combine and Reorder pack */
  2211. p_ptr->notice |= (PN_COMBINE | PN_REORDER);
  2212. /* Redraw stuff */
  2213. p_ptr->redraw |= (PR_INVEN);
  2214. /* Hobbits ID mushrooms on pickup, gnomes ID wands and staffs on pickup */
  2215. if (!object_is_known(j_ptr))
  2216. {
  2217. if (player_has(PF_KNOW_MUSHROOM) && j_ptr->tval == TV_FOOD)
  2218. {
  2219. do_ident_item(i, j_ptr);
  2220. msg_print("Mushrooms for breakfast!");
  2221. }
  2222. if (player_has(PF_KNOW_ZAPPER) &&
  2223. (j_ptr->tval == TV_WAND || j_ptr->tval == TV_STAFF))
  2224. {
  2225. do_ident_item(i, j_ptr);
  2226. }
  2227. }
  2228. /* Save quiver size */
  2229. save_quiver_size();
  2230. /* Return the slot */
  2231. return (i);
  2232. }
  2233. /*
  2234. * Take off (some of) a non-cursed equipment item
  2235. *
  2236. * Note that only one item at a time can be wielded per slot.
  2237. *
  2238. * Note that taking off an item when "full" may cause that item
  2239. * to fall to the ground.
  2240. *
  2241. * Return the inventory slot into which the item is placed.
  2242. */
  2243. s16b inven_takeoff(int item, int amt)
  2244. {
  2245. int slot;
  2246. object_type *o_ptr;
  2247. object_type *i_ptr;
  2248. object_type object_type_body;
  2249. cptr act;
  2250. char o_name[80];
  2251. /* Get the item to take off */
  2252. o_ptr = &inventory[item];
  2253. /* Paranoia */
  2254. if (amt <= 0) return (-1);
  2255. /* Verify */
  2256. if (amt > o_ptr->number) amt = o_ptr->number;
  2257. /* Get local object */
  2258. i_ptr = &object_type_body;
  2259. /* Obtain a local object */
  2260. object_copy(i_ptr, o_ptr);
  2261. /* Modify quantity */
  2262. i_ptr->number = amt;
  2263. /* Describe the object */
  2264. object_desc(o_name, sizeof(o_name), i_ptr, ODESC_PREFIX | ODESC_FULL);
  2265. /* Took off weapon */
  2266. if (item == INVEN_WIELD)
  2267. {
  2268. act = "You were wielding";
  2269. }
  2270. /* Took off bow */
  2271. else if (item == INVEN_BOW)
  2272. {
  2273. act = "You were holding";
  2274. }
  2275. /* Took off light */
  2276. else if (item == INVEN_LIGHT)
  2277. {
  2278. act = "You were holding";
  2279. }
  2280. /* Took off something */
  2281. else
  2282. {
  2283. act = "You were wearing";
  2284. }
  2285. /* Modify, Optimize */
  2286. inven_item_increase(item, -amt);
  2287. inven_item_optimize(item);
  2288. /* Carry the object */
  2289. slot = inven_carry(i_ptr);
  2290. /* Message */
  2291. message_format(MSG_WIELD, 0, "%s %s (%c).", act, o_name, index_to_label(slot));
  2292. p_ptr->notice |= PN_SQUELCH;
  2293. /* Return slot */
  2294. return (slot);
  2295. }
  2296. /*
  2297. * Drop (some of) a non-cursed inventory/equipment item
  2298. *
  2299. * The object will be dropped "near" the current location
  2300. */
  2301. void inven_drop(int item, int amt)
  2302. {
  2303. int py = p_ptr->py;
  2304. int px = p_ptr->px;
  2305. object_type *o_ptr;
  2306. object_type *i_ptr;
  2307. object_type object_type_body;
  2308. char o_name[80];
  2309. /* Get the original object */
  2310. o_ptr = &inventory[item];
  2311. /* Error check */
  2312. if (amt <= 0) return;
  2313. /* Not too many */
  2314. if (amt > o_ptr->number) amt = o_ptr->number;
  2315. /* Take off equipment */
  2316. if (item >= INVEN_WIELD)
  2317. {
  2318. /* Take off first */
  2319. item = inven_takeoff(item, amt);
  2320. /* Get the original object */
  2321. o_ptr = &inventory[item];
  2322. }
  2323. /* Get local object */
  2324. i_ptr = &object_type_body;
  2325. /* Obtain local object */
  2326. object_copy(i_ptr, o_ptr);
  2327. /* Distribute charges of wands, staves, or rods */
  2328. distribute_charges(o_ptr, i_ptr, amt);
  2329. /* Modify quantity */
  2330. i_ptr->number = amt;
  2331. /* Describe local object */
  2332. object_desc(o_name, sizeof(o_name), i_ptr, ODESC_PREFIX | ODESC_FULL);
  2333. /* Message */
  2334. msg_format("You drop %s (%c).", o_name, index_to_label(item));
  2335. /* Drop it near the player */
  2336. drop_near(i_ptr, 0, py, px, FALSE);
  2337. /* Modify, Describe, Optimize */
  2338. inven_item_increase(item, -amt);
  2339. inven_item_describe(item);
  2340. inven_item_optimize(item);
  2341. }
  2342. /*
  2343. * Combine items in the pack
  2344. * Also "pick up" any gold in the inventory by accident
  2345. *
  2346. * Note special handling of the "overflow" slot
  2347. */
  2348. void combine_pack(void)
  2349. {
  2350. int i, j, k;
  2351. object_type *o_ptr;
  2352. object_type *j_ptr;
  2353. bool flag = FALSE;
  2354. /* Combine the pack (backwards) */
  2355. for (i = INVEN_PACK; i > 0; i--)
  2356. {
  2357. bool slide = FALSE;
  2358. /* Get the item */
  2359. o_ptr = &inventory[i];
  2360. /* Skip empty items */
  2361. if (!o_ptr->k_idx) continue;
  2362. /* Absorb gold */
  2363. if (o_ptr->tval == TV_GOLD)
  2364. {
  2365. /* Count the gold */
  2366. slide = TRUE;
  2367. p_ptr->au += o_ptr->pval;
  2368. }
  2369. /* Scan the items above that item */
  2370. else for (j = 0; j < i; j++)
  2371. {
  2372. /* Get the item */
  2373. j_ptr = &inventory[j];
  2374. /* Skip empty items */
  2375. if (!j_ptr->k_idx) continue;
  2376. /* Can we drop "o_ptr" onto "j_ptr"? */
  2377. if (object_similar(j_ptr, o_ptr))
  2378. {
  2379. /* Take note */
  2380. flag = slide = TRUE;
  2381. /* Add together the item counts */
  2382. object_absorb(j_ptr, o_ptr);
  2383. break;
  2384. }
  2385. }
  2386. /* Compact the inventory */
  2387. if (slide)
  2388. {
  2389. /* One object is gone */
  2390. p_ptr->inven_cnt--;
  2391. /* Slide everything down */
  2392. for (k = i; k < INVEN_PACK; k++)
  2393. {
  2394. /* Hack -- slide object */
  2395. COPY(&inventory[k], &inventory[k+1], object_type);
  2396. }
  2397. /* Hack -- wipe hole */
  2398. object_wipe(&inventory[k]);
  2399. /* Redraw stuff */
  2400. p_ptr->redraw |= (PR_INVEN);
  2401. }
  2402. }
  2403. /* Message */
  2404. if (flag)
  2405. {
  2406. msg_print("You combine some items in your pack.");
  2407. /* Stop "repeat last command" from working. */
  2408. cmd_disable_repeat();
  2409. }
  2410. }
  2411. /*
  2412. * Reorder items in the pack
  2413. *
  2414. * Note special handling of the "overflow" slot
  2415. */
  2416. void reorder_pack(void)
  2417. {
  2418. int i, j, k;
  2419. s32b o_value;
  2420. s32b j_value;
  2421. object_type *o_ptr;
  2422. object_type *j_ptr;
  2423. object_type *i_ptr;
  2424. object_type object_type_body;
  2425. bool flag = FALSE;
  2426. /* Re-order the pack (forwards) */
  2427. for (i = 0; i < INVEN_PACK; i++)
  2428. {
  2429. /* Get the item */
  2430. o_ptr = &inventory[i];
  2431. /* Skip empty slots */
  2432. if (!o_ptr->k_idx) continue;
  2433. /* Get the "value" of the item */
  2434. o_value = k_info[o_ptr->k_idx].cost;
  2435. /* Scan every occupied slot */
  2436. for (j = 0; j < INVEN_PACK; j++)
  2437. {
  2438. /* Get the item already there */
  2439. j_ptr = &inventory[j];
  2440. /* Use empty slots */
  2441. if (!j_ptr->k_idx) break;
  2442. /* Hack -- readable books always come first */
  2443. if ((o_ptr->tval == cp_ptr->spell_book) &&
  2444. (j_ptr->tval != cp_ptr->spell_book)) break;
  2445. if ((j_ptr->tval == cp_ptr->spell_book) &&
  2446. (o_ptr->tval != cp_ptr->spell_book)) continue;
  2447. /* Objects sort by decreasing type */
  2448. if (o_ptr->tval > j_ptr->tval) break;
  2449. if (o_ptr->tval < j_ptr->tval) continue;
  2450. /* Non-aware (flavored) items always come last */
  2451. if (!object_flavor_is_aware(o_ptr)) continue;
  2452. if (!object_flavor_is_aware(j_ptr)) break;
  2453. /* Objects sort by increasing sval */
  2454. if (o_ptr->sval < j_ptr->sval) break;
  2455. if (o_ptr->sval > j_ptr->sval) continue;
  2456. /* Unidentified objects always come last */
  2457. if (!object_is_known(o_ptr)) continue;
  2458. if (!object_is_known(j_ptr)) break;
  2459. /* Lights sort by decreasing fuel */
  2460. if (o_ptr->tval == TV_LIGHT)
  2461. {
  2462. if (o_ptr->pval > j_ptr->pval) break;
  2463. if (o_ptr->pval < j_ptr->pval) continue;
  2464. }
  2465. /* Determine the "value" of the pack item */
  2466. j_value = k_info[j_ptr->k_idx].cost;
  2467. /* Objects sort by decreasing value */
  2468. if (o_value > j_value) break;
  2469. if (o_value < j_value) continue;
  2470. }
  2471. /* Never move down */
  2472. if (j >= i) continue;
  2473. /* Take note */
  2474. flag = TRUE;
  2475. /* Get local object */
  2476. i_ptr = &object_type_body;
  2477. /* Save a copy of the moving item */
  2478. object_copy(i_ptr, &inventory[i]);
  2479. /* Slide the objects */
  2480. for (k = i; k > j; k--)
  2481. {
  2482. /* Slide the item */
  2483. object_copy(&inventory[k], &inventory[k-1]);
  2484. }
  2485. /* Insert the moving item */
  2486. object_copy(&inventory[j], i_ptr);
  2487. /* Redraw stuff */
  2488. p_ptr->redraw |= (PR_INVEN);
  2489. }
  2490. if (flag)
  2491. {
  2492. msg_print("You reorder some items in your pack.");
  2493. /* Stop "repeat last command" from working. */
  2494. cmd_disable_repeat();
  2495. }
  2496. }
  2497. /*
  2498. *Returns the number of times in 1000 that @ will FAIL
  2499. * - thanks to Ed Graham for the formula
  2500. */
  2501. int get_use_device_chance(const object_type *o_ptr)
  2502. {
  2503. int lev, fail, numerator, denominator;
  2504. int skill = p_ptr->state.skills[SKILL_DEVICE];
  2505. int skill_min = 10;
  2506. int skill_max = 141;
  2507. int diff_min = 1;
  2508. int diff_max = 100;
  2509. /* Extract the item level, which is the difficulty rating */
  2510. if (artifact_p(o_ptr))
  2511. lev = a_info[o_ptr->name1].level;
  2512. else
  2513. lev = k_info[o_ptr->k_idx].level;
  2514. /* TODO: maybe use something a little less convoluted? */
  2515. numerator = (skill - lev) - (skill_max - diff_min);
  2516. denominator = (lev - skill) - (diff_max - skill_min);
  2517. /* Make sure that we don't divide by zero */
  2518. if (denominator == 0) denominator = numerator > 0 ? 1 : -1;
  2519. fail = (100 * numerator) / denominator;
  2520. /* Ensure failure rate is between 1% and 75% */
  2521. if (fail > 750) fail = 750;
  2522. if (fail < 10) fail = 10;
  2523. return fail;
  2524. }
  2525. /*
  2526. * Distribute charges of rods, staves, or wands.
  2527. *
  2528. * o_ptr = source item
  2529. * q_ptr = target item, must be of the same type as o_ptr
  2530. * amt = number of items that are transfered
  2531. */
  2532. void distribute_charges(object_type *o_ptr, object_type *q_ptr, int amt)
  2533. {
  2534. const object_kind *k_ptr = &k_info[o_ptr->k_idx];
  2535. int charge_time = randcalc(k_ptr->time, 0, AVERAGE), max_time;
  2536. /*
  2537. * Hack -- If rods, staves, or wands are dropped, the total maximum
  2538. * timeout or charges need to be allocated between the two stacks.
  2539. * If all the items are being dropped, it makes for a neater message
  2540. * to leave the original stack's pval alone. -LM-
  2541. */
  2542. if ((o_ptr->tval == TV_WAND) ||
  2543. (o_ptr->tval == TV_STAFF))
  2544. {
  2545. q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
  2546. if (amt < o_ptr->number) o_ptr->pval -= q_ptr->pval;
  2547. }
  2548. /*
  2549. * Hack -- Rods also need to have their timeouts distributed.
  2550. *
  2551. * The dropped stack will accept all time remaining to charge up to
  2552. * its maximum.
  2553. */
  2554. if (o_ptr->tval == TV_ROD)
  2555. {
  2556. max_time = charge_time * amt;
  2557. if (o_ptr->timeout > max_time)
  2558. q_ptr->timeout = max_time;
  2559. else
  2560. q_ptr->timeout = o_ptr->timeout;
  2561. if (amt < o_ptr->number)
  2562. o_ptr->timeout -= q_ptr->timeout;
  2563. }
  2564. }
  2565. void reduce_charges(object_type *o_ptr, int amt)
  2566. {
  2567. /*
  2568. * Hack -- If rods or wand are destroyed, the total maximum timeout or
  2569. * charges of the stack needs to be reduced, unless all the items are
  2570. * being destroyed. -LM-
  2571. */
  2572. if (((o_ptr->tval == TV_WAND) ||
  2573. (o_ptr->tval == TV_STAFF)) &&
  2574. (amt < o_ptr->number))
  2575. {
  2576. o_ptr->pval -= o_ptr->pval * amt / o_ptr->number;
  2577. }
  2578. if ((o_ptr->tval == TV_ROD) &&
  2579. (amt < o_ptr->number))
  2580. {
  2581. o_ptr->timeout -= o_ptr->timeout * amt / o_ptr->number;
  2582. }
  2583. }
  2584. int number_charging(const object_type *o_ptr)
  2585. {
  2586. int charge_time, num_charging;
  2587. random_value timeout;
  2588. /* Artifacts have a special timeout */
  2589. if(o_ptr->name1)
  2590. timeout = a_info[o_ptr->name1].time;
  2591. else
  2592. timeout = k_info[o_ptr->k_idx].time;
  2593. charge_time = randcalc(timeout, 0, AVERAGE);
  2594. /* Item has no timeout */
  2595. if (charge_time <= 0) return 0;
  2596. /* No items are charging */
  2597. if (o_ptr->timeout <= 0) return 0;
  2598. /* Calculate number charging based on timeout */
  2599. num_charging = (o_ptr->timeout + charge_time - 1) / charge_time;
  2600. /* Number charging cannot exceed stack size */
  2601. if (num_charging > o_ptr->number) num_charging = o_ptr->number;
  2602. return num_charging;
  2603. }
  2604. bool recharge_timeout(object_type *o_ptr)
  2605. {
  2606. int charging_before, charging_after;
  2607. /* Find the number of charging items */
  2608. charging_before = number_charging(o_ptr);
  2609. /* Nothing to charge */
  2610. if (charging_before == 0)
  2611. return FALSE;
  2612. /* Decrease the timeout */
  2613. o_ptr->timeout -= MIN(charging_before, o_ptr->timeout);
  2614. /* Find the new number of charging items */
  2615. charging_after = number_charging(o_ptr);
  2616. /* Return true if at least 1 item obtained a charge */
  2617. if (charging_after < charging_before)
  2618. return TRUE;
  2619. else
  2620. return FALSE;
  2621. }
  2622. /*
  2623. * Looks if "inscrip" is present on the given object.
  2624. */
  2625. unsigned check_for_inscrip(const object_type *o_ptr, const char *inscrip)
  2626. {
  2627. unsigned i = 0;
  2628. const char *s;
  2629. if (!o_ptr->note) return 0;
  2630. s = quark_str(o_ptr->note);
  2631. do
  2632. {
  2633. s = strstr(s, inscrip);
  2634. if (!s) break;
  2635. i++;
  2636. s++;
  2637. }
  2638. while (s);
  2639. return i;
  2640. }
  2641. /*** Object kind lookup functions ***/
  2642. /**
  2643. * Return the k_idx of the object kind with the given `tval` and `sval`, or 0.
  2644. */
  2645. int lookup_kind(int tval, int sval)
  2646. {
  2647. int k;
  2648. /* Look for it */
  2649. for (k = 1; k < z_info->k_max; k++)
  2650. {
  2651. object_kind *k_ptr = &k_info[k];
  2652. /* Found a match */
  2653. if ((k_ptr->tval == tval) && (k_ptr->sval == sval)) return (k);
  2654. }
  2655. /* Failure */
  2656. msg_format("No object (%s,%d)", tval_find_name(tval), tval, sval);
  2657. return 0;
  2658. }
  2659. /**
  2660. * Find the tval and sval of object kind `k_idx`, and return via the pointers
  2661. * `tval` and `sval`.
  2662. */
  2663. bool lookup_reverse(s16b k_idx, int *tval, int *sval)
  2664. {
  2665. object_kind *k_ptr;
  2666. /* Validate k_idx */
  2667. if ((k_idx < 1) || (k_idx > z_info->k_max))
  2668. return FALSE;
  2669. /* Get pointer */
  2670. k_ptr = &k_info[k_idx];
  2671. *tval = k_ptr->tval;
  2672. *sval = k_ptr->sval;
  2673. /* Done */
  2674. return TRUE;
  2675. }
  2676. /*** Textual<->numeric conversion ***/
  2677. /**
  2678. * List of { tval, name } pairs.
  2679. */
  2680. static const grouper tval_names[] =
  2681. {
  2682. { TV_SKELETON, "skeleton" },
  2683. { TV_BOTTLE, "bottle" },
  2684. { TV_JUNK, "junk" },
  2685. { TV_SPIKE, "spike" },
  2686. { TV_CHEST, "chest" },
  2687. { TV_SHOT, "shot" },
  2688. { TV_ARROW, "arrow" },
  2689. { TV_BOLT, "bolt" },
  2690. { TV_BOW, "bow" },
  2691. { TV_DIGGING, "digger" },
  2692. { TV_HAFTED, "hafted" },
  2693. { TV_POLEARM, "polearm" },
  2694. { TV_SWORD, "sword" },
  2695. { TV_BOOTS, "boots" },
  2696. { TV_GLOVES, "gloves" },
  2697. { TV_HELM, "helm" },
  2698. { TV_CROWN, "crown" },
  2699. { TV_SHIELD, "shield" },
  2700. { TV_CLOAK, "cloak" },
  2701. { TV_SOFT_ARMOR, "soft armor" },
  2702. { TV_SOFT_ARMOR, "soft armour" },
  2703. { TV_HARD_ARMOR, "hard armor" },
  2704. { TV_HARD_ARMOR, "hard armour" },
  2705. { TV_DRAG_ARMOR, "dragon armor" },
  2706. { TV_DRAG_ARMOR, "dragon armour" },
  2707. { TV_LIGHT, "light" },
  2708. { TV_AMULET, "amulet" },
  2709. { TV_RING, "ring" },
  2710. { TV_STAFF, "staff" },
  2711. { TV_WAND, "wand" },
  2712. { TV_ROD, "rod" },
  2713. { TV_SCROLL, "scroll" },
  2714. { TV_POTION, "potion" },
  2715. { TV_FLASK, "flask" },
  2716. { TV_FOOD, "food" },
  2717. { TV_MAGIC_BOOK, "magic book" },
  2718. { TV_PRAYER_BOOK, "prayer book" },
  2719. { TV_GOLD, "gold" },
  2720. };
  2721. /**
  2722. * Return the k_idx of the object kind with the given `tval` and name `name`.
  2723. */
  2724. int lookup_name(int tval, const char *name)
  2725. {
  2726. int k;
  2727. /* Look for it */
  2728. for (k = 1; k < z_info->k_max; k++)
  2729. {
  2730. object_kind *k_ptr = &k_info[k];
  2731. const char *nm = k_name + k_ptr->name;
  2732. if (*nm == '&' && *(nm+1))
  2733. nm += 2;
  2734. /* Found a match */
  2735. if (k_ptr->tval == tval && !strcmp(name, nm))
  2736. return k;
  2737. }
  2738. msg_format("No object (\"%s\",\"%s\")", tval_find_name(tval), name);
  2739. return -1;
  2740. }
  2741. /**
  2742. * Return the a_idx of the artifact with the given name
  2743. */
  2744. int lookup_artifact_name(const char *name)
  2745. {
  2746. int i;
  2747. /* Look for it */
  2748. for (i = 1; i < z_info->a_max; i++)
  2749. {
  2750. artifact_type *a_ptr = &a_info[i];
  2751. const char *nm = a_name + a_ptr->name;
  2752. /* Found a match */
  2753. if (streq(name, nm))
  2754. return i;
  2755. }
  2756. return -1;
  2757. }
  2758. /**
  2759. * Return the numeric sval of the object kind with the given `tval` and name `name`.
  2760. */
  2761. int lookup_sval(int tval, const char *name)
  2762. {
  2763. int k;
  2764. /* Look for it */
  2765. for (k = 1; k < z_info->k_max; k++)
  2766. {
  2767. object_kind *k_ptr = &k_info[k];
  2768. const char *nm = k_name + k_ptr->name;
  2769. if (*nm == '&' && *(nm+1))
  2770. nm += 2;
  2771. /* Found a match */
  2772. if (k_ptr->tval == tval && !strcmp(name, nm))
  2773. return k_ptr->sval;
  2774. }
  2775. msg_format("No object (\"%s\",\"%s\")", tval_find_name(tval), name);
  2776. return -1;
  2777. }
  2778. /**
  2779. * Returns the numeric equivalent tval of the textual tval `name`.
  2780. */
  2781. int tval_find_idx(const char *name)
  2782. {
  2783. size_t i = 0;
  2784. for (i = 0; i < N_ELEMENTS(tval_names); i++)
  2785. {
  2786. if (!my_stricmp(name, tval_names[i].name))
  2787. return tval_names[i].tval;
  2788. }
  2789. return -1;
  2790. }
  2791. /**
  2792. * Returns the textual equivalent tval of the numeric tval `name`.
  2793. */
  2794. const char *tval_find_name(int tval)
  2795. {
  2796. size_t i = 0;
  2797. for (i = 0; i < N_ELEMENTS(tval_names); i++)
  2798. {
  2799. if (tval == tval_names[i].tval)
  2800. return tval_names[i].name;
  2801. }
  2802. return "unknown";
  2803. }
  2804. /**
  2805. * Sort comparator for objects using only tval and sval.
  2806. * -1 if o1 should be first
  2807. * 1 if o2 should be first
  2808. * 0 if it doesn't matter
  2809. */
  2810. static int compare_types(const object_type *o1, const object_type *o2)
  2811. {
  2812. if (o1->tval == o2->tval)
  2813. return CMP(o1->sval, o2->sval);
  2814. else
  2815. return CMP(o1->tval, o2->tval);
  2816. }
  2817. /* some handy macros for sorting */
  2818. #define object_is_worthless(o) (k_info[o->k_idx].cost == 0)
  2819. /**
  2820. * Sort comparator for objects
  2821. * -1 if o1 should be first
  2822. * 1 if o2 should be first
  2823. * 0 if it doesn't matter
  2824. *
  2825. * The sort order is designed with the "list items" command in mind.
  2826. */
  2827. static int compare_items(const object_type *o1, const object_type *o2)
  2828. {
  2829. /* known artifacts will sort first */
  2830. if (object_is_known_artifact(o1) && object_is_known_artifact(o2))
  2831. return compare_types(o1, o2);
  2832. if (object_is_known_artifact(o1)) return -1;
  2833. if (object_is_known_artifact(o2)) return 1;
  2834. /* unknown objects will sort next */
  2835. if (!object_flavor_is_aware(o1) && !object_flavor_is_aware(o2))
  2836. return compare_types(o1, o2);
  2837. if (!object_flavor_is_aware(o1)) return -1;
  2838. if (!object_flavor_is_aware(o2)) return 1;
  2839. /* if only one of them is worthless, the other comes first */
  2840. if (object_is_worthless(o1) && !object_is_worthless(o2)) return 1;
  2841. if (!object_is_worthless(o1) && object_is_worthless(o2)) return -1;
  2842. /* otherwise, just compare tvals and svals */
  2843. /* NOTE: arguably there could be a better order than this */
  2844. return compare_types(o1, o2);
  2845. }
  2846. /**
  2847. * Helper to draw the Object Recall subwindow; this actually does the work.
  2848. */
  2849. void display_object_recall(object_type *o_ptr)
  2850. {
  2851. clear_from(0);
  2852. prt("", 0, 0);
  2853. object_info_header(o_ptr);
  2854. if (!object_info(o_ptr, OINFO_NONE))
  2855. text_out("This item does not seem to possess any special abilities.");
  2856. }
  2857. /**
  2858. * This draws the Object Recall subwindow when displaying a particular object
  2859. * (e.g. a helmet in the backpack, or a scroll on the ground)
  2860. */
  2861. void display_object_idx_recall(s16b item)
  2862. {
  2863. object_type *o_ptr = object_from_item_idx(item);
  2864. display_object_recall(o_ptr);
  2865. }
  2866. /**
  2867. * This draws the Object Recall subwindow when displaying a recalled item kind
  2868. * (e.g. a generic ring of acid or a generic blade of chaos)
  2869. */
  2870. void display_object_kind_recall(s16b k_idx)
  2871. {
  2872. /* Initialize and prepare a fake object; it will be deallocated when we */
  2873. /* leave the function. */
  2874. object_type object;
  2875. object_type *o_ptr = &object;
  2876. object_wipe(o_ptr);
  2877. object_prep(o_ptr, k_idx, 0, EXTREMIFY);
  2878. if (k_info[k_idx].aware) o_ptr->ident |= (IDENT_STORE);
  2879. /* draw it */
  2880. display_object_recall(o_ptr);
  2881. }
  2882. /*
  2883. * Display visible items, similar to display_monlist
  2884. */
  2885. void display_itemlist(void)
  2886. {
  2887. int max;
  2888. int mx, my;
  2889. unsigned num;
  2890. int line = 1, x = 0;
  2891. int cur_x;
  2892. unsigned i;
  2893. unsigned disp_count = 0;
  2894. byte a;
  2895. char c;
  2896. object_type *types[MAX_ITEMLIST];
  2897. int counts[MAX_ITEMLIST];
  2898. int dx[MAX_ITEMLIST], dy[MAX_ITEMLIST];
  2899. unsigned counter = 0;
  2900. int dungeon_hgt = p_ptr->depth == 0 ? TOWN_HGT : DUNGEON_HGT;
  2901. int dungeon_wid = p_ptr->depth == 0 ? TOWN_WID : DUNGEON_WID;
  2902. byte attr;
  2903. char buf[80];
  2904. int floor_list[MAX_FLOOR_STACK];
  2905. /* Clear the term if in a subwindow, set x otherwise */
  2906. if (Term != angband_term[0])
  2907. {
  2908. clear_from(0);
  2909. max = Term->hgt - 1;
  2910. }
  2911. else
  2912. {
  2913. x = 13;
  2914. max = Term->hgt - 2;
  2915. }
  2916. /* Look at each square of the dungeon for items */
  2917. for (my = 0; my < dungeon_hgt; my++)
  2918. {
  2919. for (mx = 0; mx < dungeon_wid; mx++)
  2920. {
  2921. num = scan_floor(floor_list, MAX_FLOOR_STACK, my, mx, 0x02);
  2922. /* Iterate over all the items found on this square */
  2923. for (i = 0; i < num; i++)
  2924. {
  2925. object_type *o_ptr = &o_list[floor_list[i]];
  2926. unsigned j;
  2927. /* Skip gold/squelched */
  2928. if (o_ptr->tval == TV_GOLD || squelch_hide_item(o_ptr))
  2929. continue;
  2930. /* See if we've already seen a similar item; if so, just add */
  2931. /* to its count */
  2932. for (j = 0; j < counter; j++)
  2933. {
  2934. if (object_similar(o_ptr, types[j]))
  2935. {
  2936. counts[j] += o_ptr->number;
  2937. if ((my - p_ptr->py) * (my - p_ptr->py) + (mx - p_ptr->px) * (mx - p_ptr->px) < dy[j] * dy[j] + dx[j] * dx[j])
  2938. {
  2939. dy[j] = my - p_ptr->py;
  2940. dx[j] = mx - p_ptr->px;
  2941. }
  2942. break;
  2943. }
  2944. }
  2945. /* We saw a new item. So insert it at the end of the list and */
  2946. /* then sort it forward using compare_items(). The types list */
  2947. /* is always kept sorted. */
  2948. if (j == counter)
  2949. {
  2950. types[counter] = o_ptr;
  2951. counts[counter] = o_ptr->number;
  2952. dy[counter] = my - p_ptr->py;
  2953. dx[counter] = mx - p_ptr->px;
  2954. while (j > 0 && compare_items(types[j - 1], types[j]) > 0)
  2955. {
  2956. object_type *tmp_o = types[j - 1];
  2957. int tmpcount;
  2958. int tmpdx = dx[j-1];
  2959. int tmpdy = dy[j-1];
  2960. types[j - 1] = types[j];
  2961. types[j] = tmp_o;
  2962. dx[j-1] = dx[j];
  2963. dx[j] = tmpdx;
  2964. dy[j-1] = dy[j];
  2965. dy[j] = tmpdy;
  2966. tmpcount = counts[j - 1];
  2967. counts[j - 1] = counts[j];
  2968. counts[j] = tmpcount;
  2969. j--;
  2970. }
  2971. counter++;
  2972. }
  2973. }
  2974. }
  2975. }
  2976. /* Note no visible items */
  2977. if (!counter)
  2978. {
  2979. /* Clear display and print note */
  2980. c_prt(TERM_SLATE, "You see no items.", 0, 0);
  2981. if (Term == angband_term[0])
  2982. Term_addstr(-1, TERM_WHITE, " (Press any key to continue.)");
  2983. /* Done */
  2984. return;
  2985. }
  2986. else
  2987. {
  2988. /* Reprint Message */
  2989. prt(format("You can see %d item%s:",
  2990. counter, (counter > 1 ? "s" : "")), 0, 0);
  2991. }
  2992. for (i = 0; i < counter; i++)
  2993. {
  2994. /* o_name will hold the object_desc() name for the object. */
  2995. /* o_desc will also need to put a (x4) behind it. */
  2996. /* can there be more than 999 stackable items on a level? */
  2997. char o_name[80];
  2998. char o_desc[86];
  2999. object_type *o_ptr = types[i];
  3000. /* We shouldn't list coins or squelched items */
  3001. if (o_ptr->tval == TV_GOLD || squelch_hide_item(o_ptr))
  3002. continue;
  3003. object_desc(o_name, sizeof(o_name), o_ptr, ODESC_FULL);
  3004. if (counts[i] > 1)
  3005. strnfmt(o_desc, sizeof(o_desc), "%s (x%d) %d %c, %d %c", o_name, counts[i],
  3006. (dy[i] > 0) ? dy[i] : -dy[i], (dy[i] > 0) ? 'S' : 'N',
  3007. (dx[i] > 0) ? dx[i] : -dx[i], (dx[i] > 0) ? 'E' : 'W');
  3008. else
  3009. strnfmt(o_desc, sizeof(o_desc), "%s %d %c %d %c", o_name,
  3010. (dy[i] > 0) ? dy[i] : -dy[i], (dy[i] > 0) ? 'S' : 'N',
  3011. (dx[i] > 0) ? dx[i] : -dx[i], (dx[i] > 0) ? 'E' : 'W');
  3012. /* Reset position */
  3013. cur_x = x;
  3014. /* See if we need to scroll or not */
  3015. if (Term == angband_term[0] && (line == max) && disp_count != counter)
  3016. {
  3017. prt("-- more --", line, x);
  3018. anykey();
  3019. /* Clear the screen */
  3020. for (line = 1; line <= max; line++)
  3021. prt("", line, x);
  3022. /* Reprint Message */
  3023. prt(format("You can see %d item%s:",
  3024. counter, (counter > 1 ? "s" : "")), 0, 0);
  3025. /* Reset */
  3026. line = 1;
  3027. }
  3028. else if (line == max)
  3029. {
  3030. continue;
  3031. }
  3032. /* Note that the number of items actually displayed */
  3033. disp_count++;
  3034. if (artifact_p(o_ptr) && object_is_known(o_ptr))
  3035. /* known artifact */
  3036. attr = TERM_VIOLET;
  3037. else if (!object_flavor_is_aware(o_ptr))
  3038. /* unaware of kind */
  3039. attr = TERM_RED;
  3040. else if (object_is_worthless(o_ptr))
  3041. /* worthless */
  3042. attr = TERM_SLATE;
  3043. else
  3044. /* default */
  3045. attr = TERM_WHITE;
  3046. a = object_kind_attr(o_ptr->k_idx);
  3047. c = object_kind_char(o_ptr->k_idx);
  3048. /* Display the pict */
  3049. Term_putch(cur_x++, line, a, c);
  3050. if (use_bigtile) Term_putch(cur_x++, line, 255, -1);
  3051. Term_putch(cur_x++, line, TERM_WHITE, ' ');
  3052. /* Print and bump line counter */
  3053. c_prt(attr, o_desc, line, cur_x);
  3054. line++;
  3055. }
  3056. if (disp_count != counter)
  3057. {
  3058. /* Print "and others" message if we've run out of space */
  3059. strnfmt(buf, sizeof buf, " ...and %d others.", counter - disp_count);
  3060. c_prt(TERM_WHITE, buf, line, x);
  3061. }
  3062. else
  3063. {
  3064. /* Otherwise clear a line at the end, for main-term display */
  3065. prt("", line, x);
  3066. }
  3067. if (Term == angband_term[0])
  3068. Term_addstr(-1, TERM_WHITE, " (Press any key to continue.)");
  3069. }
  3070. /* Accessor functions, for prettier code */
  3071. artifact_type *artifact_of(const object_type *o_ptr)
  3072. {
  3073. if (o_ptr->name1)
  3074. return &a_info[o_ptr->name1];
  3075. return NULL;
  3076. }
  3077. object_kind *object_kind_of(const object_type *o_ptr)
  3078. {
  3079. return &k_info[o_ptr->k_idx];
  3080. }
  3081. /* Basic tval testers */
  3082. bool obj_is_staff(const object_type *o_ptr) { return o_ptr->tval == TV_STAFF; }
  3083. bool obj_is_wand(const object_type *o_ptr) { return o_ptr->tval == TV_WAND; }
  3084. bool obj_is_rod(const object_type *o_ptr) { return o_ptr->tval == TV_ROD; }
  3085. bool obj_is_potion(const object_type *o_ptr) { return o_ptr->tval == TV_POTION; }
  3086. bool obj_is_scroll(const object_type *o_ptr) { return o_ptr->tval == TV_SCROLL; }
  3087. bool obj_is_food(const object_type *o_ptr) { return o_ptr->tval == TV_FOOD; }
  3088. bool obj_is_light(const object_type *o_ptr) { return o_ptr->tval == TV_LIGHT; }
  3089. bool obj_is_ring(const object_type *o_ptr) { return o_ptr->tval == TV_RING; }
  3090. /**
  3091. * Determine whether an object is ammo
  3092. *
  3093. * \param o_ptr is the object to check
  3094. */
  3095. bool obj_is_ammo(const object_type *o_ptr)
  3096. {
  3097. switch (o_ptr->tval)
  3098. {
  3099. case TV_SHOT:
  3100. case TV_ARROW:
  3101. case TV_BOLT:
  3102. return TRUE;
  3103. default:
  3104. return FALSE;
  3105. }
  3106. }
  3107. /* Determine if an object has charges */
  3108. bool obj_has_charges(const object_type *o_ptr)
  3109. {
  3110. if (o_ptr->tval != TV_WAND && o_ptr->tval != TV_STAFF) return FALSE;
  3111. if (o_ptr->pval <= 0) return FALSE;
  3112. return TRUE;
  3113. }
  3114. /* Determine if an object is zappable */
  3115. bool obj_can_zap(const object_type *o_ptr)
  3116. {
  3117. /* Any rods not charging? */
  3118. if (o_ptr->tval == TV_ROD && number_charging(o_ptr) < o_ptr->number)
  3119. return TRUE;
  3120. return FALSE;
  3121. }
  3122. /* Determine if an object is activatable */
  3123. bool obj_is_activatable(const object_type *o_ptr)
  3124. {
  3125. return object_effect(o_ptr) ? TRUE : FALSE;
  3126. }
  3127. /* Determine if an object can be activated now */
  3128. bool obj_can_activate(const object_type *o_ptr)
  3129. {
  3130. if (obj_is_activatable(o_ptr))
  3131. {
  3132. /* Check the recharge */
  3133. if (!o_ptr->timeout) return TRUE;
  3134. }
  3135. return FALSE;
  3136. }
  3137. bool obj_can_refill(const object_type *o_ptr)
  3138. {
  3139. bitflag f[OF_SIZE];
  3140. const object_type *j_ptr = &inventory[INVEN_LIGHT];
  3141. /* Get flags */
  3142. object_flags(o_ptr, f);
  3143. if (j_ptr->sval == SV_LIGHT_LANTERN)
  3144. {
  3145. /* Flasks of oil are okay */
  3146. if (o_ptr->tval == TV_FLASK) return (TRUE);
  3147. }
  3148. /* Non-empty, non-everburning sources are okay */
  3149. if ((o_ptr->tval == TV_LIGHT) &&
  3150. (o_ptr->sval == j_ptr->sval) &&
  3151. (o_ptr->timeout > 0) &&
  3152. !of_has(f, OF_NO_FUEL))
  3153. {
  3154. return (TRUE);
  3155. }
  3156. /* Assume not okay */
  3157. return (FALSE);
  3158. }
  3159. bool obj_can_browse(const object_type *o_ptr)
  3160. {
  3161. if (o_ptr->tval != cp_ptr->spell_book) return FALSE;
  3162. return TRUE;
  3163. }
  3164. /* Can only take off non-cursed items */
  3165. bool obj_can_takeoff(const object_type *o_ptr)
  3166. {
  3167. return !cursed_p(o_ptr);
  3168. }
  3169. /* Can only put on wieldable items */
  3170. bool obj_can_wear(const object_type *o_ptr)
  3171. {
  3172. return (wield_slot(o_ptr) >= INVEN_WIELD);
  3173. }
  3174. /* Can has inscrip pls */
  3175. bool obj_has_inscrip(const object_type *o_ptr)
  3176. {
  3177. return (o_ptr->note ? TRUE : FALSE);
  3178. }
  3179. /*** Generic utility functions ***/
  3180. /*
  3181. * Return an object's effect.
  3182. */
  3183. u16b object_effect(const object_type *o_ptr)
  3184. {
  3185. if (o_ptr->name1)
  3186. return a_info[o_ptr->name1].effect;
  3187. else
  3188. return k_info[o_ptr->k_idx].effect;
  3189. }
  3190. /* Get an o_ptr from an item number */
  3191. object_type *object_from_item_idx(int item)
  3192. {
  3193. if (item >= 0)
  3194. return &inventory[item];
  3195. else
  3196. return &o_list[0 - item];
  3197. }
  3198. /*
  3199. * Does the given object need to be aimed?
  3200. */
  3201. bool obj_needs_aim(object_type *o_ptr)
  3202. {
  3203. int effect;
  3204. /* Figure out effect the object would use */
  3205. if (o_ptr->name1)
  3206. effect = a_info[o_ptr->name1].effect;
  3207. else
  3208. effect = k_info[o_ptr->k_idx].effect;
  3209. /* If the effect needs aiming, or if the object type needs
  3210. aiming, this object needs aiming. */
  3211. if (effect_aim(effect) ||
  3212. (o_ptr->tval == TV_WAND) ||
  3213. (o_ptr->tval == TV_ROD && !object_flavor_is_aware(o_ptr)))
  3214. {
  3215. return TRUE;
  3216. }
  3217. else
  3218. {
  3219. return FALSE;
  3220. }
  3221. }
  3222. /*
  3223. * Verify the "okayness" of a given item.
  3224. *
  3225. * The item can be negative to mean "item on floor".
  3226. */
  3227. bool get_item_okay(int item)
  3228. {
  3229. /* Verify the item */
  3230. return (item_tester_okay(object_from_item_idx(item)));
  3231. }
  3232. /*
  3233. * Get a list of "valid" item indexes.
  3234. *
  3235. * Fills item_list[] with items that are "okay" as defined by the
  3236. * current item_tester_hook, etc. mode determines what combination of
  3237. * inventory, equipment and player's floor location should be used
  3238. * when drawing up the list.
  3239. *
  3240. * Returns the number of items placed into the list.
  3241. *
  3242. * Maximum space that can be used is [INVEN_TOTAL + MAX_FLOOR_STACK],
  3243. * though practically speaking much smaller numbers are likely.
  3244. */
  3245. int scan_items(int *item_list, size_t item_list_max, int mode)
  3246. {
  3247. bool use_inven = ((mode & USE_INVEN) ? TRUE : FALSE);
  3248. bool use_equip = ((mode & USE_EQUIP) ? TRUE : FALSE);
  3249. bool use_floor = ((mode & USE_FLOOR) ? TRUE : FALSE);
  3250. int floor_list[MAX_FLOOR_STACK];
  3251. int floor_num;
  3252. int i;
  3253. size_t item_list_num = 0;
  3254. if (use_inven)
  3255. {
  3256. for (i = 0; i < INVEN_PACK && item_list_num < item_list_max; i++)
  3257. {
  3258. if (get_item_okay(i))
  3259. item_list[item_list_num++] = i;
  3260. }
  3261. }
  3262. if (use_equip)
  3263. {
  3264. for (i = INVEN_WIELD; i < ALL_INVEN_TOTAL && item_list_num < item_list_max; i++)
  3265. {
  3266. if (get_item_okay(i))
  3267. item_list[item_list_num++] = i;
  3268. }
  3269. }
  3270. /* Scan all non-gold objects in the grid */
  3271. if (use_floor)
  3272. {
  3273. floor_num = scan_floor(floor_list, N_ELEMENTS(floor_list), p_ptr->py, p_ptr->px, 0x03);
  3274. for (i = 0; i < floor_num && item_list_num < item_list_max; i++)
  3275. {
  3276. if (get_item_okay(-floor_list[i]))
  3277. item_list[item_list_num++] = -floor_list[i];
  3278. }
  3279. }
  3280. /* Forget the item_tester_tval and item_tester_hook restrictions */
  3281. item_tester_tval = 0;
  3282. item_tester_hook = NULL;
  3283. return item_list_num;
  3284. }
  3285. /*
  3286. * Check if the given item is available for the player to use.
  3287. *
  3288. * 'mode' defines which areas we should look at, a la scan_items().
  3289. */
  3290. bool item_is_available(int item, bool (*tester)(const object_type *), int mode)
  3291. {
  3292. int item_list[ALL_INVEN_TOTAL + MAX_FLOOR_STACK];
  3293. int item_num;
  3294. int i;
  3295. item_tester_hook = tester;
  3296. item_tester_tval = 0;
  3297. item_num = scan_items(item_list, N_ELEMENTS(item_list), mode);
  3298. for (i = 0; i < item_num; i++)
  3299. {
  3300. if (item_list[i] == item)
  3301. return TRUE;
  3302. }
  3303. return FALSE;
  3304. }
  3305. /*
  3306. * Returns whether the pack is holding the maximum number of items. The max
  3307. * size is INVEN_MAX_PACK, which is a macro since quiver size affects slots
  3308. * available.
  3309. */
  3310. bool pack_is_full(void)
  3311. {
  3312. return inventory[INVEN_MAX_PACK - 1].k_idx ? TRUE : FALSE;
  3313. }
  3314. /*
  3315. * Returns whether the pack is holding the more than the maximum number of
  3316. * items. The max size is INVEN_MAX_PACK, which is a macro since quiver size
  3317. * affects slots available. If this is true, calling pack_overflow() will
  3318. * trigger a pack overflow.
  3319. */
  3320. bool pack_is_overfull(void)
  3321. {
  3322. return inventory[INVEN_MAX_PACK].k_idx ? TRUE : FALSE;
  3323. }
  3324. /*
  3325. * Overflow an item from the pack, if it is overfull.
  3326. */
  3327. void pack_overflow(void)
  3328. {
  3329. int item = INVEN_MAX_PACK;
  3330. char o_name[80];
  3331. object_type *o_ptr;
  3332. if (!pack_is_overfull()) return;
  3333. /* Get the slot to be dropped */
  3334. o_ptr = &inventory[item];
  3335. /* Disturbing */
  3336. disturb(0, 0);
  3337. /* Warning */
  3338. msg_print("Your pack overflows!");
  3339. /* Describe */
  3340. object_desc(o_name, sizeof(o_name), o_ptr, ODESC_PREFIX | ODESC_FULL);
  3341. /* Message */
  3342. msg_format("You drop %s (%c).", o_name, index_to_label(item));
  3343. /* Drop it (carefully) near the player */
  3344. drop_near(o_ptr, 0, p_ptr->py, p_ptr->px, FALSE);
  3345. /* Modify, Describe, Optimize */
  3346. inven_item_increase(item, -255);
  3347. inven_item_describe(item);
  3348. inven_item_optimize(item);
  3349. /* Notice stuff (if needed) */
  3350. if (p_ptr->notice) notice_stuff();
  3351. /* Update stuff (if needed) */
  3352. if (p_ptr->update) update_stuff();
  3353. /* Redraw stuff (if needed) */
  3354. if (p_ptr->redraw) redraw_stuff();
  3355. }