PageRenderTime 67ms CodeModel.GetById 27ms 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

Large files files are truncated, but you can click here to view the full 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

Large files files are truncated, but you can click here to view the full file