PageRenderTime 68ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/store.c

https://bitbucket.org/ekolis/jackband
C | 3338 lines | 1837 code | 749 blank | 752 comment | 466 complexity | f8cb1c72af161f7ac1ecebef5ae8f70f MD5 | raw file

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

  1. /*
  2. * File: store.c
  3. * Purpose: Store stocking and UI
  4. *
  5. * Copyright (c) 1997 Robert A. Koeneke, James E. Wilson, Ben Harrison
  6. * Copyright (c) 2007 Andrew Sidwell, who rewrote a fair portion
  7. *
  8. * This work is free software; you can redistribute it and/or modify it
  9. * under the terms of either:
  10. *
  11. * a) the GNU General Public License as published by the Free Software
  12. * Foundation, version 2, or
  13. *
  14. * b) the "Angband licence":
  15. * This software may be copied and distributed for educational, research,
  16. * and not for profit purposes provided that this copyright and statement
  17. * are included in all such copies. Other copyrights may also apply.
  18. */
  19. #include "angband.h"
  20. #include "cmds.h"
  21. #include "ui-menu.h"
  22. #include "game-event.h"
  23. #include "object/tvalsval.h"
  24. /*** Constants and definitions ***/
  25. /* Easy names for the elements of the 'scr_places' arrays. */
  26. enum
  27. {
  28. LOC_PRICE = 0,
  29. LOC_OWNER,
  30. LOC_HEADER,
  31. LOC_ITEMS_START,
  32. LOC_ITEMS_END,
  33. LOC_MORE,
  34. LOC_HELP_CLEAR,
  35. LOC_HELP_PROMPT,
  36. LOC_AU,
  37. LOC_WEIGHT,
  38. LOC_MAX
  39. };
  40. /* Places for the various things displayed onscreen */
  41. static unsigned int scr_places_x[LOC_MAX];
  42. static unsigned int scr_places_y[LOC_MAX];
  43. /* State flags */
  44. #define STORE_GOLD_CHANGE 0x01
  45. #define STORE_FRAME_CHANGE 0x02
  46. #define STORE_SHOW_HELP 0x04
  47. #define STORE_KEEP_PROMPT 0x08
  48. /* Compound flag for the initial display of a store */
  49. #define STORE_INIT_CHANGE (STORE_FRAME_CHANGE | STORE_GOLD_CHANGE)
  50. /* Some local constants */
  51. #define STORE_TURNOVER 9 /* Normal shop turnover, per day */
  52. #define STORE_OBJ_LEVEL 5 /* Magic Level for normal stores */
  53. #define STORE_MIN_KEEP 6 /* Min slots to "always" keep full (>0) */
  54. #define STORE_MAX_KEEP 18 /* Max slots to "always" keep full (<STORE_INVEN_MAX) */
  55. /** Variables to maintain state XXX ***/
  56. /* Flags for the display */
  57. static u16b store_flags;
  58. /*** Utilities ***/
  59. /*
  60. * Return the owner struct for the given store.
  61. */
  62. static owner_type *store_owner(int st)
  63. {
  64. store_type *st_ptr = &store[st];
  65. return &b_info[(st * z_info->b_max) + st_ptr->owner];
  66. }
  67. /* Randomly select one of the entries in an array */
  68. #define ONE_OF(x) x[randint0(N_ELEMENTS(x))]
  69. /*** Flavour text stuff ***/
  70. /*
  71. * Shopkeeper welcome messages.
  72. *
  73. * The shopkeeper's name must come first, then the character's name.
  74. */
  75. static const char *comment_welcome[] =
  76. {
  77. "",
  78. "%s nods to you.",
  79. "%s says hello.",
  80. "%s: \"See anything you like, adventurer?\"",
  81. "%s: \"How may I help you, %s?\"",
  82. "%s: \"Welcome back, %s.\"",
  83. "%s: \"A pleasure to see you again, %s.\"",
  84. "%s: \"How may I be of assistance, good %s?\"",
  85. "%s: \"You do honour to my humble store, noble %s.\"",
  86. "%s: \"I and my family are entirely at your service, glorious %s.\""
  87. };
  88. /*
  89. * Messages for reacting to purchase prices.
  90. */
  91. static const char *comment_worthless[] =
  92. {
  93. "Arrgghh!",
  94. "You bastard!",
  95. "You hear someone sobbing...",
  96. "The shopkeeper howls in agony!",
  97. "The shopkeeper wails in anguish!",
  98. "The shopkeeper beats his head against the counter."
  99. };
  100. static const char *comment_bad[] =
  101. {
  102. "Damn!",
  103. "You fiend!",
  104. "The shopkeeper curses at you.",
  105. "The shopkeeper glares at you."
  106. };
  107. static const char *comment_accept[] =
  108. {
  109. "Okay.",
  110. "Fine.",
  111. "Accepted!",
  112. "Agreed!",
  113. "Done!",
  114. "Taken!"
  115. };
  116. static const char *comment_good[] =
  117. {
  118. "Cool!",
  119. "You've made my day!",
  120. "The shopkeeper sniggers.",
  121. "The shopkeeper giggles.",
  122. "The shopkeeper laughs loudly."
  123. };
  124. static const char *comment_great[] =
  125. {
  126. "Yipee!",
  127. "I think I'll retire!",
  128. "The shopkeeper jumps for joy.",
  129. "The shopkeeper smiles gleefully.",
  130. "Wow. I'm going to name my new villa in your honour."
  131. };
  132. /*
  133. * Staple definitions.
  134. */
  135. typedef enum { MAKE_SINGLE, MAKE_NORMAL, MAKE_MAX } create_mode;
  136. static struct staple_type
  137. {
  138. int tval, sval;
  139. create_mode mode;
  140. } staples[] =
  141. {
  142. { TV_FOOD, SV_FOOD_RATION, MAKE_NORMAL },
  143. { TV_LIGHT, SV_LIGHT_TORCH, MAKE_NORMAL },
  144. { TV_SCROLL, SV_SCROLL_WORD_OF_RECALL, MAKE_NORMAL },
  145. { TV_SCROLL, SV_SCROLL_PHASE_DOOR, MAKE_NORMAL },
  146. { TV_FLASK, 0, MAKE_NORMAL },
  147. { TV_SPIKE, 0, MAKE_NORMAL },
  148. { TV_SHOT, SV_AMMO_NORMAL, MAKE_MAX },
  149. { TV_ARROW, SV_AMMO_NORMAL, MAKE_MAX },
  150. { TV_BOLT, SV_AMMO_NORMAL, MAKE_MAX },
  151. { TV_DIGGING, SV_SHOVEL, MAKE_SINGLE },
  152. { TV_DIGGING, SV_PICK, MAKE_SINGLE },
  153. { TV_CLOAK, SV_CLOAK, MAKE_SINGLE }
  154. };
  155. /*
  156. * The greeting a shopkeeper gives the character says a lot about his
  157. * general attitude.
  158. *
  159. * Taken and modified from Sangband 1.0.
  160. */
  161. static void prt_welcome(const owner_type *ot_ptr)
  162. {
  163. char short_name[20];
  164. const char *player_name;
  165. const char *owner_name = &b_name[ot_ptr->owner_name];
  166. /* We go from level 1 - 50 */
  167. size_t i = ((unsigned)p_get_lev() - 1) / 5;
  168. /* Sanity check in case we increase the max level carelessly */
  169. i = MIN(i, N_ELEMENTS(comment_welcome) - 1);
  170. /* Only show the message one in four times to stop it being irritating. */
  171. if (!one_in_(4)) return;
  172. /* Welcome the character */
  173. if (i)
  174. {
  175. int j;
  176. /* Extract the first name of the store owner (stop before the first space) */
  177. for (j = 0; owner_name[j] && owner_name[j] != ' '; j++)
  178. short_name[j] = owner_name[j];
  179. /* Truncate the name */
  180. short_name[j] = '\0';
  181. /* Get a title for the character */
  182. if ((i % 2) && randint0(2)) player_name = c_text + cp_ptr->title[(p_get_lev() - 1) / 5];
  183. else if (randint0(2)) player_name = op_ptr->full_name;
  184. else player_name = (p_ptr->psex == SEX_MALE ? "sir" : "lady");
  185. /* Balthazar says "Welcome" */
  186. prt(format(comment_welcome[i], short_name, player_name), 0, 0);
  187. }
  188. }
  189. /*
  190. * Let a shop-keeper React to a purchase
  191. *
  192. * We paid "price", it was worth "value", and we thought it was worth "guess"
  193. */
  194. static void purchase_analyze(s32b price, s32b value, s32b guess)
  195. {
  196. /* Item was worthless, but we bought it */
  197. if ((value <= 0) && (price > value))
  198. message(MSG_STORE1, 0, ONE_OF(comment_worthless));
  199. /* Item was cheaper than we thought, and we paid more than necessary */
  200. else if ((value < guess) && (price > value))
  201. message(MSG_STORE2, 0, ONE_OF(comment_bad));
  202. /* Item was a good bargain, and we got away with it */
  203. else if ((value > guess) && (value < (4 * guess)) && (price < value))
  204. message(MSG_STORE3, 0, ONE_OF(comment_good));
  205. /* Item was a great bargain, and we got away with it */
  206. else if ((value > guess) && (price < value))
  207. message(MSG_STORE4, 0, ONE_OF(comment_great));
  208. }
  209. /*** Check if a store will buy an object ***/
  210. /*
  211. * Determine if the current store will purchase the given object
  212. *
  213. * Note that a shop-keeper must refuse to buy "worthless" objects
  214. */
  215. static bool store_will_buy(int store_num, const object_type *o_ptr)
  216. {
  217. /* Hack -- The Home is simple */
  218. if (store_num == STORE_HOME) return (TRUE);
  219. /* Switch on the store */
  220. switch (store_num)
  221. {
  222. /* General Store */
  223. case STORE_GENERAL:
  224. {
  225. size_t i;
  226. bool accept = FALSE;
  227. /* Accept lights and food */
  228. if (o_ptr->tval == TV_LIGHT || o_ptr->tval == TV_FOOD)
  229. accept = TRUE;
  230. /* Accept staples */
  231. for (i = 0; !accept && i < N_ELEMENTS(staples); i++)
  232. {
  233. if (staples[i].tval == o_ptr->tval &&
  234. staples[i].sval == o_ptr->sval)
  235. accept = TRUE;
  236. }
  237. if (!accept) return FALSE;
  238. break;
  239. }
  240. /* Armoury */
  241. case STORE_ARMOR:
  242. {
  243. /* Analyze the type */
  244. switch (o_ptr->tval)
  245. {
  246. case TV_BOOTS:
  247. case TV_GLOVES:
  248. case TV_CROWN:
  249. case TV_HELM:
  250. case TV_SHIELD:
  251. case TV_CLOAK:
  252. case TV_SOFT_ARMOR:
  253. case TV_HARD_ARMOR:
  254. case TV_DRAG_ARMOR:
  255. break;
  256. default:
  257. return (FALSE);
  258. }
  259. break;
  260. }
  261. /* Weapon Shop */
  262. case STORE_WEAPON:
  263. {
  264. /* Analyze the type */
  265. switch (o_ptr->tval)
  266. {
  267. case TV_SHOT:
  268. case TV_BOLT:
  269. case TV_ARROW:
  270. case TV_BOW:
  271. case TV_DIGGING:
  272. case TV_HAFTED:
  273. case TV_POLEARM:
  274. case TV_SWORD:
  275. break;
  276. default:
  277. return (FALSE);
  278. }
  279. break;
  280. }
  281. /* Temple */
  282. case STORE_TEMPLE:
  283. {
  284. /* Analyze the type */
  285. switch (o_ptr->tval)
  286. {
  287. case TV_PRAYER_BOOK:
  288. case TV_SCROLL:
  289. case TV_POTION:
  290. case TV_HAFTED:
  291. break;
  292. case TV_POLEARM:
  293. case TV_SWORD:
  294. {
  295. /* Known blessed blades are accepted too */
  296. if (object_is_known_blessed(o_ptr)) break;
  297. }
  298. default:
  299. return (FALSE);
  300. }
  301. break;
  302. }
  303. /* Alchemist */
  304. case STORE_ALCHEMY:
  305. {
  306. /* Analyze the type */
  307. switch (o_ptr->tval)
  308. {
  309. case TV_SCROLL:
  310. case TV_POTION:
  311. break;
  312. default:
  313. return (FALSE);
  314. }
  315. break;
  316. }
  317. /* Magic Shop */
  318. case STORE_MAGIC:
  319. {
  320. /* Analyze the type */
  321. switch (o_ptr->tval)
  322. {
  323. case TV_MAGIC_BOOK:
  324. case TV_AMULET:
  325. case TV_RING:
  326. case TV_STAFF:
  327. case TV_WAND:
  328. case TV_ROD:
  329. case TV_SCROLL:
  330. case TV_POTION:
  331. break;
  332. default:
  333. return (FALSE);
  334. }
  335. break;
  336. }
  337. }
  338. /* Ignore "worthless" items XXX XXX XXX */
  339. if (object_value(o_ptr, 1, FALSE) <= 0) return (FALSE);
  340. /* Assume okay */
  341. return (TRUE);
  342. }
  343. /* Get the current store number, or STORE_NONE if not in a store */
  344. static int current_store()
  345. {
  346. /* If we're displaying store knowledge whilst not in a store,
  347. * override the value returned
  348. */
  349. if (store_knowledge != STORE_NONE)
  350. return store_knowledge;
  351. if ((cave_feat[p_ptr->py][p_ptr->px] >= FEAT_SHOP_HEAD) &&
  352. (cave_feat[p_ptr->py][p_ptr->px] <= FEAT_SHOP_TAIL))
  353. return (cave_feat[p_ptr->py][p_ptr->px] - FEAT_SHOP_HEAD);
  354. return STORE_NONE;
  355. }
  356. /*** Basics: pricing, generation, etc. ***/
  357. /*
  358. * Determine the price of an object (qty one) in a store.
  359. *
  360. * store_buying == TRUE means the shop is buying, player selling
  361. * == FALSE means the shop is selling, player buying
  362. *
  363. * This function takes into account the player's charisma, but
  364. * never lets a shop-keeper lose money in a transaction.
  365. *
  366. * The "greed" value should exceed 100 when the player is "buying" the
  367. * object, and should be less than 100 when the player is "selling" it.
  368. *
  369. * Hack -- the black market always charges twice as much as it should.
  370. */
  371. s32b price_item(const object_type *o_ptr, bool store_buying, int qty)
  372. {
  373. int adjust;
  374. s32b price;
  375. int this_store = current_store();
  376. owner_type *ot_ptr;
  377. if (this_store == STORE_NONE) return 0L;
  378. ot_ptr = store_owner(this_store);
  379. /* Get the value of the stack of wands, or a single item */
  380. if ((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_STAFF))
  381. price = object_value(o_ptr, qty, FALSE);
  382. else
  383. price = object_value(o_ptr, 1, FALSE);
  384. /* Worthless items */
  385. if (price <= 0) return (0L);
  386. /* Add in the charisma factor */
  387. if (this_store == STORE_B_MARKET)
  388. adjust = 150;
  389. else
  390. adjust = adj_chr_gold[p_ptr->state.stat_ind[A_CHR]];
  391. /* Shop is buying */
  392. if (store_buying)
  393. {
  394. /* Set the factor */
  395. adjust = 100 + (100 - adjust);
  396. if (adjust > 100) adjust = 100;
  397. /* Mega-Hack -- Black market sucks */
  398. if (this_store == STORE_B_MARKET) price = price / 2;
  399. }
  400. /* Shop is selling */
  401. else
  402. {
  403. /* Fix the factor */
  404. if (adjust < 100) adjust = 100;
  405. /* Mega-Hack -- Black market sucks */
  406. if (this_store == STORE_B_MARKET) price = price * 2;
  407. }
  408. /* Compute the final price (with rounding) */
  409. price = (price * adjust + 50L) / 100L;
  410. /* Now convert price to total price for non-wands */
  411. if (!(o_ptr->tval == TV_WAND) && !(o_ptr->tval == TV_STAFF))
  412. price *= qty;
  413. /* Now limit the price to the purse limit */
  414. if (store_buying && (price > ot_ptr->max_cost * qty))
  415. price = ot_ptr->max_cost * qty;
  416. /* Note -- Never become "free" */
  417. if (price <= 0L) return (qty);
  418. /* Return the price */
  419. return (price);
  420. }
  421. /*
  422. * Special "mass production" computation.
  423. */
  424. static int mass_roll(int times, int max)
  425. {
  426. int i, t = 0;
  427. assert(max > 1);
  428. for (i = 0; i < times; i++)
  429. t += randint0(max);
  430. return (t);
  431. }
  432. /*
  433. * Some cheap objects should be created in piles.
  434. */
  435. static void mass_produce(object_type *o_ptr)
  436. {
  437. int size = 1;
  438. s32b cost = object_value(o_ptr, 1, FALSE);
  439. /* Analyze the type */
  440. switch (o_ptr->tval)
  441. {
  442. /* Food, Flasks, and Lights */
  443. case TV_FOOD:
  444. case TV_FLASK:
  445. case TV_LIGHT:
  446. {
  447. if (cost <= 5L) size += mass_roll(3, 5);
  448. if (cost <= 20L) size += mass_roll(3, 5);
  449. break;
  450. }
  451. case TV_POTION:
  452. case TV_SCROLL:
  453. {
  454. if (cost <= 60L) size += mass_roll(3, 5);
  455. if (cost <= 240L) size += mass_roll(1, 5);
  456. break;
  457. }
  458. case TV_MAGIC_BOOK:
  459. case TV_PRAYER_BOOK:
  460. {
  461. if (cost <= 50L) size += mass_roll(2, 3);
  462. if (cost <= 500L) size += mass_roll(1, 3);
  463. break;
  464. }
  465. case TV_SOFT_ARMOR:
  466. case TV_HARD_ARMOR:
  467. case TV_SHIELD:
  468. case TV_GLOVES:
  469. case TV_BOOTS:
  470. case TV_CLOAK:
  471. case TV_HELM:
  472. case TV_CROWN:
  473. case TV_SWORD:
  474. case TV_POLEARM:
  475. case TV_HAFTED:
  476. case TV_DIGGING:
  477. case TV_BOW:
  478. {
  479. if (o_ptr->name2) break;
  480. if (cost <= 10L) size += mass_roll(3, 5);
  481. if (cost <= 100L) size += mass_roll(3, 5);
  482. break;
  483. }
  484. case TV_SPIKE:
  485. case TV_SHOT:
  486. case TV_ARROW:
  487. case TV_BOLT:
  488. {
  489. if (cost <= 5L)
  490. size = randint1(3) * 20; /* 20-60 in 20s */
  491. else if (cost > 5L && cost <= 50L)
  492. size = randint1(4) * 10; /* 10-40 in 10s */
  493. else if (cost > 50 && cost <= 500L)
  494. size = randint1(4) * 5; /* 5-20 in 5s */
  495. else
  496. size = 1;
  497. break;
  498. }
  499. }
  500. /* Save the total pile size */
  501. o_ptr->number = size;
  502. }
  503. /*
  504. * Determine if a store object can "absorb" another object.
  505. *
  506. * See "object_similar()" for the same function for the "player".
  507. *
  508. * This function can ignore many of the checks done for the player,
  509. * since stores (but not the home) only get objects under certain
  510. * restricted circumstances.
  511. */
  512. static bool store_object_similar(const object_type *o_ptr, const object_type *j_ptr)
  513. {
  514. /* Hack -- Identical items cannot be stacked */
  515. if (o_ptr == j_ptr) return (0);
  516. /* Different objects cannot be stacked */
  517. if (o_ptr->k_idx != j_ptr->k_idx) return (0);
  518. /* Different pvals cannot be stacked, except for wands, staves, or rods */
  519. if ((o_ptr->pval != j_ptr->pval) &&
  520. (o_ptr->tval != TV_WAND) &&
  521. (o_ptr->tval != TV_STAFF) &&
  522. (o_ptr->tval != TV_ROD)) return (0);
  523. /* Require many identical values */
  524. if (o_ptr->to_h != j_ptr->to_h) return (0);
  525. if (o_ptr->to_d != j_ptr->to_d) return (0);
  526. if (o_ptr->to_a != j_ptr->to_a) return (0);
  527. /* Require identical "artifact" names */
  528. if (o_ptr->name1 != j_ptr->name1) return (0);
  529. /* Require identical "ego-item" names */
  530. if (o_ptr->name2 != j_ptr->name2) return (0);
  531. /* Hack -- Never stack recharging items */
  532. if ((o_ptr->timeout || j_ptr->timeout) && o_ptr->tval != TV_LIGHT)
  533. return (0);
  534. /* Never stack items with different fuel */
  535. else if ((o_ptr->timeout != j_ptr->timeout) && o_ptr->tval == TV_LIGHT)
  536. return (0);
  537. /* Require many identical values */
  538. if (o_ptr->ac != j_ptr->ac) return (0);
  539. if (o_ptr->dd != j_ptr->dd) return (0);
  540. if (o_ptr->ds != j_ptr->ds) return (0);
  541. /* Hack -- Never stack chests */
  542. if (o_ptr->tval == TV_CHEST) return (0);
  543. /* Different flags */
  544. if (!of_is_equal(o_ptr->flags, j_ptr->flags))
  545. return FALSE;
  546. /* They match, so they must be similar */
  547. return (TRUE);
  548. }
  549. /*
  550. * Allow a store object to absorb another object
  551. */
  552. static void store_object_absorb(object_type *o_ptr, object_type *j_ptr)
  553. {
  554. int total = o_ptr->number + j_ptr->number;
  555. /* Combine quantity, lose excess items */
  556. o_ptr->number = (total > 99) ? 99 : total;
  557. /* Hack -- if rods are stacking, add the charging timeouts */
  558. if (o_ptr->tval == TV_ROD)
  559. o_ptr->timeout += j_ptr->timeout;
  560. /* Hack -- if wands/staves are stacking, combine the charges */
  561. if ((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_STAFF))
  562. {
  563. o_ptr->pval += j_ptr->pval;
  564. }
  565. if ((o_ptr->origin != j_ptr->origin) ||
  566. (o_ptr->origin_depth != j_ptr->origin_depth) ||
  567. (o_ptr->origin_xtra != j_ptr->origin_xtra))
  568. {
  569. int act = 2;
  570. if ((o_ptr->origin == ORIGIN_DROP) && (o_ptr->origin == j_ptr->origin))
  571. {
  572. monster_race *r_ptr = &r_info[o_ptr->origin_xtra];
  573. monster_race *s_ptr = &r_info[j_ptr->origin_xtra];
  574. bool r_uniq = rf_has(r_ptr->flags, RF_UNIQUE) ? TRUE : FALSE;
  575. bool s_uniq = rf_has(s_ptr->flags, RF_UNIQUE) ? TRUE : FALSE;
  576. if (r_uniq && !s_uniq) act = 0;
  577. else if (s_uniq && !r_uniq) act = 1;
  578. else act = 2;
  579. }
  580. switch (act)
  581. {
  582. /* Overwrite with j_ptr */
  583. case 1:
  584. {
  585. o_ptr->origin = j_ptr->origin;
  586. o_ptr->origin_depth = j_ptr->origin_depth;
  587. o_ptr->origin_xtra = j_ptr->origin_xtra;
  588. }
  589. /* Set as "mixed" */
  590. case 2:
  591. {
  592. o_ptr->origin = ORIGIN_MIXED;
  593. }
  594. }
  595. }
  596. }
  597. /*
  598. * Check to see if the shop will be carrying too many objects
  599. *
  600. * Note that the shop, just like a player, will not accept things
  601. * it cannot hold. Before, one could "nuke" objects this way, by
  602. * adding them to a pile which was already full.
  603. */
  604. static bool store_check_num(int st, const object_type *o_ptr)
  605. {
  606. int i;
  607. object_type *j_ptr;
  608. store_type *st_ptr = &store[st];
  609. /* Free space is always usable */
  610. if (st_ptr->stock_num < st_ptr->stock_size) return TRUE;
  611. /* The "home" acts like the player */
  612. if (st == STORE_HOME)
  613. {
  614. /* Check all the objects */
  615. for (i = 0; i < st_ptr->stock_num; i++)
  616. {
  617. /* Get the existing object */
  618. j_ptr = &st_ptr->stock[i];
  619. /* Can the new object be combined with the old one? */
  620. if (object_similar(j_ptr, o_ptr)) return (TRUE);
  621. }
  622. }
  623. /* Normal stores do special stuff */
  624. else
  625. {
  626. /* Check all the objects */
  627. for (i = 0; i < st_ptr->stock_num; i++)
  628. {
  629. /* Get the existing object */
  630. j_ptr = &st_ptr->stock[i];
  631. /* Can the new object be combined with the old one? */
  632. if (store_object_similar(j_ptr, o_ptr)) return (TRUE);
  633. }
  634. }
  635. /* But there was no room at the inn... */
  636. return (FALSE);
  637. }
  638. /*
  639. * Add an object to the inventory of the Home.
  640. *
  641. * In all cases, return the slot (or -1) where the object was placed.
  642. *
  643. * Note that this is a hacked up version of "inven_carry()".
  644. *
  645. * Also note that it may not correctly "adapt" to "knowledge" becoming
  646. * known: the player may have to pick stuff up and drop it again.
  647. */
  648. static int home_carry(object_type *o_ptr)
  649. {
  650. int i, slot;
  651. u32b value, j_value;
  652. object_type *j_ptr;
  653. store_type *st_ptr = &store[STORE_HOME];
  654. /* Check each existing object (try to combine) */
  655. for (slot = 0; slot < st_ptr->stock_num; slot++)
  656. {
  657. /* Get the existing object */
  658. j_ptr = &st_ptr->stock[slot];
  659. /* The home acts just like the player */
  660. if (object_similar(j_ptr, o_ptr))
  661. {
  662. /* Save the new number of items */
  663. object_absorb(j_ptr, o_ptr);
  664. /* All done */
  665. return (slot);
  666. }
  667. }
  668. /* No space? */
  669. if (st_ptr->stock_num >= st_ptr->stock_size) return (-1);
  670. /* Determine the "value" of the object */
  671. value = object_value(o_ptr, 1, FALSE);
  672. /* Check existing slots to see if we must "slide" */
  673. for (slot = 0; slot < st_ptr->stock_num; slot++)
  674. {
  675. /* Get that object */
  676. j_ptr = &st_ptr->stock[slot];
  677. /* Hack -- readable books always come first */
  678. if ((o_ptr->tval == cp_ptr->spell_book) &&
  679. (j_ptr->tval != cp_ptr->spell_book)) break;
  680. if ((j_ptr->tval == cp_ptr->spell_book) &&
  681. (o_ptr->tval != cp_ptr->spell_book)) continue;
  682. /* Objects sort by decreasing type */
  683. if (o_ptr->tval > j_ptr->tval) break;
  684. if (o_ptr->tval < j_ptr->tval) continue;
  685. /* Can happen in the home */
  686. if (!object_flavor_is_aware(o_ptr)) continue;
  687. if (!object_flavor_is_aware(j_ptr)) break;
  688. /* Objects sort by increasing sval */
  689. if (o_ptr->sval < j_ptr->sval) break;
  690. if (o_ptr->sval > j_ptr->sval) continue;
  691. /* Objects in the home can be unknown */
  692. if (!object_is_known(o_ptr)) continue;
  693. if (!object_is_known(j_ptr)) break;
  694. /* Objects sort by decreasing value */
  695. j_value = object_value(j_ptr, 1, FALSE);
  696. if (value > j_value) break;
  697. if (value < j_value) continue;
  698. }
  699. /* Slide the others up */
  700. for (i = st_ptr->stock_num; i > slot; i--)
  701. {
  702. /* Hack -- slide the objects */
  703. object_copy(&st_ptr->stock[i], &st_ptr->stock[i-1]);
  704. }
  705. /* More stuff now */
  706. st_ptr->stock_num++;
  707. /* Hack -- Insert the new object */
  708. object_copy(&st_ptr->stock[slot], o_ptr);
  709. /* Return the location */
  710. return (slot);
  711. }
  712. /*
  713. * Add an object to a real stores inventory.
  714. *
  715. * If the object is "worthless", it is thrown away (except in the home).
  716. *
  717. * If the object cannot be combined with an object already in the inventory,
  718. * make a new slot for it, and calculate its "per item" price. Note that
  719. * this price will be negative, since the price will not be "fixed" yet.
  720. * Adding an object to a "fixed" price stack will not change the fixed price.
  721. *
  722. * In all cases, return the slot (or -1) where the object was placed
  723. */
  724. static int store_carry(int st, object_type *o_ptr)
  725. {
  726. int i, slot;
  727. u32b value, j_value;
  728. object_type *j_ptr;
  729. store_type *st_ptr = &store[st];
  730. object_kind *k_ptr = &k_info[o_ptr->k_idx];
  731. /* Evaluate the object */
  732. value = object_value(o_ptr, 1, FALSE);
  733. /* Cursed/Worthless items "disappear" when sold */
  734. if (value <= 0) return (-1);
  735. /* Erase the inscription & pseudo-ID bit */
  736. o_ptr->note = 0;
  737. /* Some item types require maintenance */
  738. switch (o_ptr->tval)
  739. {
  740. /* Refuel lights to the standard amount */
  741. case TV_LIGHT:
  742. {
  743. bitflag f[OF_SIZE];
  744. object_flags(o_ptr, f);
  745. if (!of_has(f, OF_NO_FUEL))
  746. {
  747. if (o_ptr->sval == SV_LIGHT_TORCH)
  748. o_ptr->timeout = DEFAULT_TORCH;
  749. else if (o_ptr->sval == SV_LIGHT_LANTERN)
  750. o_ptr->timeout = DEFAULT_LAMP;
  751. }
  752. break;
  753. }
  754. /* Recharge rods */
  755. case TV_ROD:
  756. {
  757. o_ptr->timeout = 0;
  758. break;
  759. }
  760. /* Possibly recharge wands and staves */
  761. case TV_STAFF:
  762. case TV_WAND:
  763. {
  764. bool recharge = FALSE;
  765. /* Recharge without fail if the store normally carries that type */
  766. for (i = 0; i < st_ptr->table_num; i++)
  767. {
  768. if (st_ptr->table[i] == o_ptr->k_idx)
  769. recharge = TRUE;
  770. }
  771. if (recharge)
  772. {
  773. int charges = 0;
  774. /* Calculate the recharged number of charges */
  775. for (i = 0; i < o_ptr->number; i++)
  776. charges += randcalc(k_ptr->charge, 0, RANDOMISE);
  777. /* Use recharged value only if greater */
  778. if (charges > o_ptr->pval)
  779. o_ptr->pval = charges;
  780. }
  781. break;
  782. }
  783. }
  784. /* Check each existing object (try to combine) */
  785. for (slot = 0; slot < st_ptr->stock_num; slot++)
  786. {
  787. /* Get the existing object */
  788. j_ptr = &st_ptr->stock[slot];
  789. /* Can the existing items be incremented? */
  790. if (store_object_similar(j_ptr, o_ptr))
  791. {
  792. /* Absorb (some of) the object */
  793. store_object_absorb(j_ptr, o_ptr);
  794. /* All done */
  795. return (slot);
  796. }
  797. }
  798. /* No space? */
  799. if (st_ptr->stock_num >= st_ptr->stock_size) return (-1);
  800. /* Check existing slots to see if we must "slide" */
  801. for (slot = 0; slot < st_ptr->stock_num; slot++)
  802. {
  803. /* Get that object */
  804. j_ptr = &st_ptr->stock[slot];
  805. /* Objects sort by decreasing type */
  806. if (o_ptr->tval > j_ptr->tval) break;
  807. if (o_ptr->tval < j_ptr->tval) continue;
  808. /* Objects sort by increasing sval */
  809. if (o_ptr->sval < j_ptr->sval) break;
  810. if (o_ptr->sval > j_ptr->sval) continue;
  811. /* Evaluate that slot */
  812. j_value = object_value(j_ptr, 1, FALSE);
  813. /* Objects sort by decreasing value */
  814. if (value > j_value) break;
  815. if (value < j_value) continue;
  816. }
  817. /* Slide the others up */
  818. for (i = st_ptr->stock_num; i > slot; i--)
  819. {
  820. /* Hack -- slide the objects */
  821. object_copy(&st_ptr->stock[i], &st_ptr->stock[i-1]);
  822. }
  823. /* More stuff now */
  824. st_ptr->stock_num++;
  825. /* Hack -- Insert the new object */
  826. object_copy(&st_ptr->stock[slot], o_ptr);
  827. /* Return the location */
  828. return (slot);
  829. }
  830. /*
  831. * Increase, by a 'num', the number of an item 'item' in store 'st'.
  832. * This can result in zero items.
  833. */
  834. static void store_item_increase(int st, int item, int num)
  835. {
  836. int cnt;
  837. object_type *o_ptr;
  838. store_type *st_ptr = &store[st];
  839. /* Get the object */
  840. o_ptr = &st_ptr->stock[item];
  841. /* Verify the number */
  842. cnt = o_ptr->number + num;
  843. if (cnt > 255) cnt = 255;
  844. else if (cnt < 0) cnt = 0;
  845. /* Save the new number */
  846. o_ptr->number = cnt;
  847. }
  848. /*
  849. * Remove a slot if it is empty, in store 'st'.
  850. */
  851. static void store_item_optimize(int st, int item)
  852. {
  853. int j;
  854. object_type *o_ptr;
  855. store_type *st_ptr = &store[st];
  856. /* Get the object */
  857. o_ptr = &st_ptr->stock[item];
  858. /* Must exist */
  859. if (!o_ptr->k_idx) return;
  860. /* Must have no items */
  861. if (o_ptr->number) return;
  862. /* One less object */
  863. st_ptr->stock_num--;
  864. /* Slide everyone */
  865. for (j = item; j < st_ptr->stock_num; j++)
  866. {
  867. st_ptr->stock[j] = st_ptr->stock[j + 1];
  868. }
  869. /* Nuke the final slot */
  870. object_wipe(&st_ptr->stock[j]);
  871. }
  872. /*
  873. * Delete an object from store 'st', or, if it is a stack, perhaps only
  874. * partially delete it.
  875. */
  876. static void store_delete_index(int st, int what)
  877. {
  878. int num;
  879. object_type *o_ptr;
  880. store_type *st_ptr = &store[st];
  881. /* Paranoia */
  882. if (st_ptr->stock_num <= 0) return;
  883. /* Get the object */
  884. o_ptr = &st_ptr->stock[what];
  885. /* Determine how many objects are in the slot */
  886. num = o_ptr->number;
  887. /* Deal with stacks */
  888. if (num > 1)
  889. {
  890. /* Special behaviour for arrows, bolts &tc. */
  891. switch (o_ptr->tval)
  892. {
  893. case TV_SPIKE:
  894. case TV_SHOT:
  895. case TV_ARROW:
  896. case TV_BOLT:
  897. {
  898. /* 50% of the time, destroy the entire stack */
  899. if (randint0(100) < 50 || num < 10)
  900. num = o_ptr->number;
  901. /* 50% of the time, reduce the size to a multiple of 5 */
  902. else
  903. num = randint1(num / 5) * 5 + (num % 5);
  904. break;
  905. }
  906. default:
  907. {
  908. /* 50% of the time, destroy a single object */
  909. if (randint0(100) < 50) num = 1;
  910. /* 25% of the time, destroy half the objects */
  911. else if (randint0(100) < 50) num = (num + 1) / 2;
  912. /* 25% of the time, destroy all objects */
  913. else num = o_ptr->number;
  914. /* Hack -- decrement the total charges of staves and wands. */
  915. if (o_ptr->tval == TV_STAFF || o_ptr->tval == TV_WAND)
  916. {
  917. o_ptr->pval -= num * o_ptr->pval / o_ptr->number;
  918. }
  919. }
  920. }
  921. }
  922. /* Is the item an artifact? Mark it as lost if the player has it in history list */
  923. if (artifact_p(o_ptr))
  924. history_lose_artifact(o_ptr->name1);
  925. /* Delete the item */
  926. store_item_increase(st, what, -num);
  927. store_item_optimize(st, what);
  928. }
  929. /*
  930. * Delete a random object from store 'st', or, if it is a stack, perhaps only
  931. * partially delete it.
  932. *
  933. * This function is used when store maintainance occurs, and is designed to
  934. * imitate non-PC purchasers making purchases from the store.
  935. */
  936. static void store_delete_random(int st)
  937. {
  938. int what;
  939. store_type *st_ptr = &store[st];
  940. /* Paranoia */
  941. if (st_ptr->stock_num <= 0) return;
  942. /* Pick a random slot */
  943. what = randint0(st_ptr->stock_num);
  944. store_delete_index(st, what);
  945. }
  946. /*
  947. * Delete a percentage of a store's inventory
  948. */
  949. static void store_prune(int st, int chance_in_1000)
  950. {
  951. int i;
  952. store_type *st_ptr = &store[st];
  953. for (i = 0; i < st_ptr->stock_num; i++)
  954. {
  955. if (randint0(1000) < chance_in_1000)
  956. store_delete_index(st, i);
  957. }
  958. }
  959. /*
  960. * This makes sure that the black market doesn't stock any object that other
  961. * stores have, unless it is an ego-item or has various bonuses.
  962. *
  963. * Based on a suggestion by Lee Vogt <lvogt@cig.mcel.mot.com>.
  964. */
  965. static bool black_market_ok(const object_type *o_ptr)
  966. {
  967. int i, j;
  968. /* Ego items are always fine */
  969. if (ego_item_p(o_ptr)) return (TRUE);
  970. /* Good items are normally fine */
  971. if (o_ptr->to_a > 2) return (TRUE);
  972. if (o_ptr->to_h > 1) return (TRUE);
  973. if (o_ptr->to_d > 2) return (TRUE);
  974. /* No cheap items */
  975. if (object_value(o_ptr, 1, FALSE) < 10) return (FALSE);
  976. /* Check the other stores */
  977. for (i = 0; i < MAX_STORES; i++)
  978. {
  979. /* Skip home and black market */
  980. if (i == STORE_B_MARKET || i == STORE_HOME)
  981. continue;
  982. /* Check every object in the store */
  983. for (j = 0; j < store[i].stock_num; j++)
  984. {
  985. object_type *j_ptr = &store[i].stock[j];
  986. /* Compare object kinds */
  987. if (o_ptr->k_idx == j_ptr->k_idx)
  988. return (FALSE);
  989. }
  990. }
  991. /* Otherwise fine */
  992. return (TRUE);
  993. }
  994. /*
  995. * Get a choice from the store allocation table, in tables.c
  996. */
  997. static s16b store_get_choice(int st)
  998. {
  999. int r;
  1000. store_type *st_ptr = &store[st];
  1001. /* Choose a random entry from the store's table */
  1002. r = randint0(st_ptr->table_num);
  1003. /* Return it */
  1004. return st_ptr->table[r];
  1005. }
  1006. /*
  1007. * Creates a random object and gives it to store 'st'
  1008. */
  1009. static bool store_create_random(int st)
  1010. {
  1011. int k_idx, tries, level, tval, sval;
  1012. object_type *i_ptr;
  1013. object_type object_type_body;
  1014. int min_level, max_level;
  1015. /*
  1016. * Decide min/max levels
  1017. */
  1018. if (st == STORE_B_MARKET)
  1019. {
  1020. min_level = p_ptr->max_depth + 5;
  1021. max_level = p_ptr->max_depth + 20;
  1022. }
  1023. else
  1024. {
  1025. min_level = 1;
  1026. max_level = STORE_OBJ_LEVEL + MAX(p_ptr->max_depth - 20, 0);
  1027. }
  1028. if (min_level > 55) min_level = 55;
  1029. if (max_level > 70) max_level = 70;
  1030. /* Consider up to six items */
  1031. for (tries = 0; tries < 6; tries++)
  1032. {
  1033. /* Work out the level for objects to be generated at */
  1034. level = rand_range(min_level, max_level);
  1035. /* Black Markets have a random object, of a given level */
  1036. if (st == STORE_B_MARKET) k_idx = get_obj_num(level, FALSE);
  1037. /* Normal stores use a big table of choices */
  1038. else k_idx = store_get_choice(st);
  1039. /* Get tval/sval; if not found, item isn't real, so try again */
  1040. if (!lookup_reverse(k_idx, &tval, &sval))
  1041. {
  1042. msg_print("Invalid object index in store_create_random()!");
  1043. continue;
  1044. }
  1045. /*** Pre-generation filters ***/
  1046. /* No chests in stores XXX */
  1047. if (tval == TV_CHEST) continue;
  1048. /*** Generate the item ***/
  1049. /* Get local object */
  1050. i_ptr = &object_type_body;
  1051. /* Create a new object of the chosen kind */
  1052. object_prep(i_ptr, k_idx, level, RANDOMISE);
  1053. /* Apply some "low-level" magic (no artifacts) */
  1054. apply_magic(i_ptr, level, FALSE, FALSE, FALSE);
  1055. /* Reject if item is 'damaged' (i.e. negative mods) */
  1056. switch (i_ptr->tval)
  1057. {
  1058. case TV_DIGGING:
  1059. case TV_HAFTED:
  1060. case TV_POLEARM:
  1061. case TV_SWORD:
  1062. case TV_BOW:
  1063. case TV_SHOT:
  1064. case TV_ARROW:
  1065. case TV_BOLT:
  1066. {
  1067. if ((i_ptr->to_h < 0) || (i_ptr->to_d < 0))
  1068. continue;
  1069. }
  1070. case TV_DRAG_ARMOR:
  1071. case TV_HARD_ARMOR:
  1072. case TV_SOFT_ARMOR:
  1073. case TV_SHIELD:
  1074. case TV_HELM:
  1075. case TV_CROWN:
  1076. case TV_CLOAK:
  1077. case TV_GLOVES:
  1078. case TV_BOOTS:
  1079. {
  1080. if (i_ptr->to_a < 0) continue;
  1081. }
  1082. default:
  1083. {
  1084. /* nothing to do */
  1085. }
  1086. }
  1087. /* The object is "known" and belongs to a store */
  1088. i_ptr->ident |= IDENT_STORE;
  1089. i_ptr->origin = ORIGIN_STORE;
  1090. /*** Post-generation filters ***/
  1091. /* Black markets have expensive tastes */
  1092. if ((st == STORE_B_MARKET) && !black_market_ok(i_ptr))
  1093. continue;
  1094. /* No "worthless" items */
  1095. if (object_value(i_ptr, 1, FALSE) < 1) continue;
  1096. /* Mass produce and/or apply discount */
  1097. mass_produce(i_ptr);
  1098. /* Attempt to carry the object */
  1099. (void)store_carry(st, i_ptr);
  1100. /* Definitely done */
  1101. return TRUE;
  1102. }
  1103. return FALSE;
  1104. }
  1105. /*
  1106. * Helper function: create an item with the given tval,sval pair, add it to the
  1107. * store st. Return the slot in the inventory.
  1108. */
  1109. static int store_create_item(int st, int tval, int sval)
  1110. {
  1111. object_type object;
  1112. int k_idx;
  1113. /* Resolve tval,sval pair into an index */
  1114. k_idx = lookup_kind(tval, sval);
  1115. /* Validation - do something more substantial here? XXX */
  1116. if (!k_idx)
  1117. {
  1118. msg_print("No object in store_create_item().");
  1119. return -1;
  1120. }
  1121. /* Wipe this object */
  1122. object_wipe(&object);
  1123. /* Create a new object of the chosen kind */
  1124. object_prep(&object, k_idx, 0, RANDOMISE);
  1125. /* Item belongs to a store */
  1126. object.ident |= IDENT_STORE;
  1127. object.origin = ORIGIN_STORE;
  1128. /* Attempt to carry the object */
  1129. return store_carry(st, &object);
  1130. }
  1131. /*
  1132. * Create all staple items.
  1133. */
  1134. static void store_create_staples(void)
  1135. {
  1136. const store_type *st_ptr = &store[STORE_GENERAL];
  1137. size_t i;
  1138. /* Make sure there's enough room for staples */
  1139. while (st_ptr->stock_num >= STORE_INVEN_MAX - N_ELEMENTS(staples))
  1140. store_delete_random(STORE_GENERAL);
  1141. /* Iterate through staples */
  1142. for (i = 0; i < N_ELEMENTS(staples); i++)
  1143. {
  1144. struct staple_type *staple = &staples[i];
  1145. /* Create the staple and combine it into the store inventory */
  1146. int idx = store_create_item(STORE_GENERAL, staple->tval, staple->sval);
  1147. object_type *o_ptr = &st_ptr->stock[idx];
  1148. /* Tweak the quantities */
  1149. switch (staple->mode)
  1150. {
  1151. case MAKE_SINGLE:
  1152. o_ptr->number = 1;
  1153. break;
  1154. case MAKE_NORMAL:
  1155. mass_produce(o_ptr);
  1156. break;
  1157. case MAKE_MAX:
  1158. o_ptr->number = 99;
  1159. break;
  1160. }
  1161. }
  1162. }
  1163. /*
  1164. * Maintain the inventory at the stores.
  1165. */
  1166. void store_maint(int which)
  1167. {
  1168. int j;
  1169. int stock;
  1170. int old_rating = rating;
  1171. store_type *st_ptr;
  1172. /* Ignore home */
  1173. if (which == STORE_HOME) return;
  1174. /* General Store gets special treatment */
  1175. if (which == STORE_GENERAL)
  1176. {
  1177. /* Sell off 30% of the inventory */
  1178. store_prune(which, 300);
  1179. /* Acquire staple items */
  1180. store_create_staples();
  1181. return;
  1182. }
  1183. /* Activate that store */
  1184. st_ptr = &store[which];
  1185. /* XXX Prune the black market */
  1186. if (which == STORE_B_MARKET)
  1187. {
  1188. /* Destroy crappy black market items */
  1189. for (j = st_ptr->stock_num - 1; j >= 0; j--)
  1190. {
  1191. object_type *o_ptr = &st_ptr->stock[j];
  1192. /* Destroy crappy items */
  1193. if (!black_market_ok(o_ptr))
  1194. {
  1195. /* Destroy the object */
  1196. store_item_increase(which, j, 0 - o_ptr->number);
  1197. store_item_optimize(which, j);
  1198. }
  1199. }
  1200. }
  1201. /*** "Sell" various items */
  1202. /* Sell a few items */
  1203. stock = st_ptr->stock_num;
  1204. stock -= randint1(STORE_TURNOVER);
  1205. /* Keep stock between STORE_MAX_KEEP and STORE_MIN_KEEP slots */
  1206. if (stock > STORE_MAX_KEEP) stock = STORE_MAX_KEEP;
  1207. if (stock < STORE_MIN_KEEP) stock = STORE_MIN_KEEP;
  1208. /* Destroy objects until only "j" slots are left */
  1209. while (st_ptr->stock_num > stock) store_delete_random(which);
  1210. /*** "Buy in" various items */
  1211. /* Buy a few items */
  1212. stock = st_ptr->stock_num;
  1213. stock += randint1(STORE_TURNOVER);
  1214. /* Keep stock between STORE_MAX_KEEP and STORE_MIN_KEEP slots */
  1215. if (stock > STORE_MAX_KEEP) stock = STORE_MAX_KEEP;
  1216. if (stock < STORE_MIN_KEEP) stock = STORE_MIN_KEEP;
  1217. /* For the rest, we just choose items randomlyish */
  1218. while (st_ptr->stock_num < stock) store_create_random(which);
  1219. /* Hack -- Restore the rating */
  1220. rating = old_rating;
  1221. }
  1222. /*
  1223. * Initialize the stores. Used at birth-time.
  1224. */
  1225. void store_init(void)
  1226. {
  1227. int n, i;
  1228. store_type *st_ptr;
  1229. /* Initialise all the stores */
  1230. for (n = 0; n < MAX_STORES; n++)
  1231. {
  1232. /* Activate this store */
  1233. st_ptr = &store[n];
  1234. /* Pick an owner */
  1235. st_ptr->owner = (byte)randint0(z_info->b_max);
  1236. /* Nothing in stock */
  1237. st_ptr->stock_num = 0;
  1238. /* Clear any old items */
  1239. for (i = 0; i < st_ptr->stock_size; i++)
  1240. {
  1241. object_wipe(&st_ptr->stock[i]);
  1242. }
  1243. /* Ignore home */
  1244. if (n == STORE_HOME) continue;
  1245. /* Maintain the shop (ten times) */
  1246. for (i = 0; i < 10; i++) store_maint(n);
  1247. }
  1248. }
  1249. /*
  1250. * Shuffle one of the stores.
  1251. */
  1252. void store_shuffle(int which)
  1253. {
  1254. int i;
  1255. store_type *st_ptr = &store[which];
  1256. /* Ignore home */
  1257. if (which == STORE_HOME) return;
  1258. /* Pick a new owner */
  1259. i = st_ptr->owner;
  1260. while (i == st_ptr->owner)
  1261. i = randint0(z_info->b_max);
  1262. st_ptr->owner = i;
  1263. }
  1264. /*** Display code ***/
  1265. /*
  1266. * This function sets up screen locations based on the current term size.
  1267. *
  1268. * Current screen layout:
  1269. * line 0: reserved for messages
  1270. * line 1: shopkeeper and their purse / item buying price
  1271. * line 2: empty
  1272. * line 3: table headers
  1273. *
  1274. * line 4: Start of items
  1275. *
  1276. * If help is turned off, then the rest of the display goes as:
  1277. *
  1278. * line (height - 4): end of items
  1279. * line (height - 3): "more" prompt
  1280. * line (height - 2): empty
  1281. * line (height - 1): Help prompt and remaining gold
  1282. *
  1283. * If help is turned on, then the rest of the display goes as:
  1284. *
  1285. * line (height - 7): end of items
  1286. * line (height - 6): "more" prompt
  1287. * line (height - 4): gold remaining
  1288. * line (height - 3): command help
  1289. */
  1290. static void store_display_recalc(void)
  1291. {
  1292. int wid, hgt;
  1293. Term_get_size(&wid, &hgt);
  1294. /* Clip the width at a maximum of 104 (enough room for an 80-char item name) */
  1295. if (wid > 104) wid = 104;
  1296. /* Clip the text_out function at two smaller than the screen width */
  1297. text_out_wrap = wid - 2;
  1298. /* X co-ords first */
  1299. scr_places_x[LOC_PRICE] = wid - 14;
  1300. scr_places_x[LOC_AU] = wid - 26;
  1301. scr_places_x[LOC_OWNER] = wid - 2;
  1302. scr_places_x[LOC_WEIGHT] = wid - 14;
  1303. /* Add space for for prices */
  1304. if (current_store() != STORE_HOME)
  1305. scr_places_x[LOC_WEIGHT] -= 10;
  1306. /* Then Y */
  1307. scr_places_y[LOC_OWNER] = 1;
  1308. scr_places_y[LOC_HEADER] = 3;
  1309. scr_places_y[LOC_ITEMS_START] = 4;
  1310. /* If we are displaying help, make the height smaller */
  1311. if (store_flags & (STORE_SHOW_HELP))
  1312. hgt -= 3;
  1313. scr_places_y[LOC_ITEMS_END] = hgt - 4;
  1314. scr_places_y[LOC_MORE] = hgt - 3;
  1315. scr_places_y[LOC_AU] = hgt - 2;
  1316. /* If we're displaying the help, then put it with a line of padding */
  1317. if (!(store_flags & (STORE_SHOW_HELP)))
  1318. {
  1319. hgt -= 2;
  1320. }
  1321. scr_places_y[LOC_HELP_CLEAR] = hgt - 1;
  1322. scr_places_y[LOC_HELP_PROMPT] = hgt;
  1323. }
  1324. /*
  1325. * Redisplay a single store entry
  1326. */
  1327. static void store_display_entry(menu_type *menu, int oid, bool cursor, int row, int col, int width)
  1328. {
  1329. object_type *o_ptr;
  1330. s32b x;
  1331. odesc_detail_t desc = ODESC_PREFIX;
  1332. char o_name[80];
  1333. char out_val[160];
  1334. byte colour;
  1335. int this_store = current_store();
  1336. store_type *st_ptr = &store[this_store];
  1337. (void)menu;
  1338. (void)cursor;
  1339. (void)width;
  1340. /* Get the object */
  1341. o_ptr = &st_ptr->stock[oid];
  1342. /* Describe the object - preserving insriptions in the home */
  1343. if (this_store == STORE_HOME) desc = ODESC_FULL;
  1344. else desc = ODESC_FULL | ODESC_STORE;
  1345. object_desc(o_name, sizeof(o_name), o_ptr, ODESC_PREFIX | desc);
  1346. /* Display the object */
  1347. c_put_str(tval_to_attr[o_ptr->tval & 0x7F], o_name, row, col);
  1348. /* Show weights */
  1349. colour = curs_attrs[CURS_KNOWN][(int)cursor];
  1350. strnfmt(out_val, sizeof out_val, "%3d.%d lb", o_ptr->weight / 10, o_ptr->weight % 10);
  1351. c_put_str(colour, out_val, row, scr_places_x[LOC_WEIGHT]);
  1352. /* Describe an object (fully) in a store */
  1353. if (this_store != STORE_HOME)
  1354. {
  1355. /* Extract the "minimum" price */
  1356. x = price_item(o_ptr, FALSE, 1);
  1357. /* Make sure the player can afford it */
  1358. if ((int) p_ptr->au < (int) x)
  1359. colour = curs_attrs[CURS_UNKNOWN][(int)cursor];
  1360. /* Actually draw the price */
  1361. if (((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_STAFF)) &&
  1362. (o_ptr->number > 1))
  1363. strnfmt(out_val, sizeof out_val, "%9ld avg", (long)x);
  1364. else
  1365. strnfmt(out_val, sizeof out_val, "%9ld ", (long)x);
  1366. c_put_str(colour, out_val, row, scr_places_x[LOC_PRICE]);
  1367. }
  1368. }
  1369. /*
  1370. * Display store (after clearing screen)
  1371. */
  1372. static void store_display_frame(void)
  1373. {
  1374. char buf[80];
  1375. int this_store = current_store();
  1376. owner_type *ot_ptr = store_owner(this_store);
  1377. /* Clear screen */
  1378. Term_clear();
  1379. /* The "Home" is special */
  1380. if (this_store == STORE_HOME)
  1381. {
  1382. /* Put the owner name */
  1383. put_str("Your Home", scr_places_y[LOC_OWNER], 1);
  1384. /* Label the object descriptions */
  1385. put_str("Home Inventory", scr_places_y[LOC_HEADER], 1);
  1386. /* Show weight header */
  1387. put_str("Weight", 5, scr_places_x[LOC_WEIGHT] + 2);
  1388. }
  1389. /* Normal stores */
  1390. else
  1391. {
  1392. const char *store_name = (f_name + f_info[FEAT_SHOP_HEAD + this_store].name);
  1393. const char *owner_name = &b_name[ot_ptr->owner_name];
  1394. /* Put the owner name */
  1395. put_str(owner_name, scr_places_y[LOC_OWNER], 1);
  1396. /* Show the max price in the store (above prices) */
  1397. strnfmt(buf, sizeof(buf), "%s (%ld)", store_name, (long)(ot_ptr->max_cost));
  1398. prt(buf, scr_places_y[LOC_OWNER], scr_places_x[LOC_OWNER] - strlen(buf));
  1399. /* Label the object descriptions */
  1400. put_str("Store Inventory", scr_places_y[LOC_HEADER], 1);
  1401. /* Showing weight label */
  1402. put_str("Weight", scr_places_y[LOC_HEADER], scr_places_x[LOC_WEIGHT] + 2);
  1403. /* Label the asking price (in stores) */
  1404. put_str("Price", scr_places_y[LOC_HEADER], scr_places_x[LOC_PRICE] + 4);
  1405. }
  1406. }
  1407. /*
  1408. * Display help.
  1409. */
  1410. static void store_display_help(void)
  1411. {
  1412. int help_loc = scr_places_y[LOC_HELP_PROMPT];
  1413. /* Clear */
  1414. clear_from(scr_places_y[LOC_HELP_CLEAR]);
  1415. /* Prepare help hooks */
  1416. text_out_hook = text_out_to_screen;
  1417. text_out_indent = 1;
  1418. Term_gotoxy(1, help_loc);
  1419. text_out("Use the ");
  1420. text_out_c(TERM_L_GREEN, "movement keys");
  1421. text_out(" to navigate, or ");
  1422. text_out_c(TERM_L_GREEN, "Space");
  1423. text_out(" to advance to the next page. '");
  1424. if (OPT(rogue_like_commands))
  1425. text_out_c(TERM_L_GREEN, "x");
  1426. else
  1427. text_out_c(TERM_L_GREEN, "l");
  1428. text_out("' examines");
  1429. if (store_knowledge == STORE_NONE)
  1430. {
  1431. text_out(" and '");
  1432. text_out_c(TERM_L_GREEN, "p");
  1433. if (current_store() == STORE_HOME) text_out("' picks up");
  1434. else text_out("' purchases");
  1435. }
  1436. text_out(" the selected item. '");
  1437. if (store_knowledge == STORE_NONE)
  1438. {
  1439. text_out_c(TERM_L_GREEN, "d");
  1440. if (current_store() == STORE_HOME) text_out("' drops");
  1441. else text_out("' sells");
  1442. }
  1443. else
  1444. {
  1445. text_out_c(TERM_L_GREEN, "I");
  1446. text_out("' inspects");
  1447. }
  1448. text_out(" an item from your inventory. ");
  1449. text_out_c(TERM_L_GREEN, "ESC");
  1450. if (store_knowledge == STORE_NONE)
  1451. {
  1452. text_out(" exits the building.");
  1453. }
  1454. else
  1455. {
  1456. text_out(" exits this screen.");
  1457. }
  1458. text_out_indent = 0;
  1459. }
  1460. /*
  1461. * Decides what parts of the store display to redraw. Called on terminal
  1462. * resizings and the redraw command.
  1463. */
  1464. static void store_redraw(void)
  1465. {
  1466. if (store_flags & (STORE_FRAME_CHANGE))
  1467. {
  1468. store_display_frame();
  1469. if (store_flags & STORE_SHOW_HELP)
  1470. store_display_help();
  1471. else
  1472. prt("Press '?' for help.", scr_places_y[LOC_HELP_PROMPT], 1);
  1473. store_flags &= ~(STORE_FRAME_CHANGE);
  1474. }
  1475. if (store_flags & (STORE_GOLD_CHANGE))
  1476. {
  1477. prt(format("Gold Remaining: %9ld", (long)p_ptr->au),
  1478. scr_places_y[LOC_AU], scr_places_x[LOC_AU]);
  1479. store_flags &= ~(STORE_GOLD_CHANGE);
  1480. }
  1481. }
  1482. /*** Higher-level code ***/
  1483. static bool store_get_check(const char *prompt)
  1484. {
  1485. char ch;
  1486. /* Prompt for it */
  1487. prt(prompt, 0, 0);
  1488. /* Get an answer */
  1489. ch = inkey();
  1490. /* Erase the prompt */
  1491. prt("", 0, 0);
  1492. if (ch == ESCAPE) return (FALSE);
  1493. if (strchr("Nn", ch)) return (FALSE);
  1494. /* Success */
  1495. return (TRUE);
  1496. }
  1497. /*
  1498. * Return the quantity of a given item in the pack (include quiver).
  1499. */
  1500. static int find_inven(const object_type *o_ptr)
  1501. {
  1502. int j;
  1503. int num = 0;
  1504. /* Similar slot? */
  1505. for (j = 0; j < QUIVER_END; j++)
  1506. {
  1507. object_type *j_ptr = &inventory[j];
  1508. /* Check only the inventory and the quiver */
  1509. if (j >= INVEN_WIELD && j < QUIVER_START) continue;
  1510. /* Require identical object types */
  1511. if (!j_ptr->k_idx || o_ptr->k_idx != j_ptr->k_idx) continue;
  1512. /* Analyze the items */
  1513. switch (o_ptr->tval)
  1514. {
  1515. /* Chests */
  1516. case TV_CHEST:
  1517. {
  1518. /* Never okay */
  1519. return 0;
  1520. }
  1521. /* Food and Potions and Scrolls */
  1522. case TV_FOOD:
  1523. case TV_POTION:
  1524. case TV_SCROLL:
  1525. {
  1526. /* Assume okay */
  1527. break;
  1528. }
  1529. /* Staves and Wands */
  1530. case TV_STAFF:
  1531. case TV_WAND:
  1532. {
  1533. /* Assume okay */
  1534. break;
  1535. }
  1536. /* Rods */
  1537. case TV_ROD:
  1538. {
  1539. /* Assume okay */
  1540. break;
  1541. }
  1542. /* Weapons and Armor */
  1543. case TV_BOW:
  1544. case TV_DIGGING:
  1545. case TV_HAFTED:
  1546. case TV_POLEARM:
  1547. case TV_SWORD:
  1548. case TV_BOOTS:
  1549. case TV_GLOVES:
  1550. case TV_HELM:
  1551. case TV_CROWN:
  1552. case TV_SHIELD:
  1553. case TV_CLOAK:
  1554. case TV_SOFT_ARMOR:
  1555. case TV_HARD_ARMOR:
  1556. case TV_DRAG_ARMOR:
  1557. {
  1558. /* Fall through */
  1559. }
  1560. /* Rings, Amulets, Lights */
  1561. case TV_RING:
  1562. case TV_AMULET:
  1563. case TV_LIGHT:
  1564. {
  1565. /* Require both items to be known */
  1566. if (!object_is_known(o_ptr) || !object_is_known(j_ptr)) continue;
  1567. /* Fall through */
  1568. }
  1569. /* Missiles */
  1570. case TV_BOLT:
  1571. case TV_ARROW:
  1572. case TV_SHOT:
  1573. {
  1574. /* Require identical knowledge of both items */
  1575. if (object_is_known(o_ptr) != object_is_known(j_ptr)) continue;
  1576. /* Require identical "bonuses" */
  1577. if (o_ptr->to_h != j_ptr->to_h) continue;
  1578. if (o_ptr->to_d != j_ptr->to_d) continue;
  1579. if (o_ptr->to_a != j_ptr->to_a) continue;
  1580. /* Require identical "pval" code */
  1581. if (o_ptr->pval != j_ptr->pval) continue;
  1582. /* Require identical "artifact" names */
  1583. if (o_ptr->name1 != j_ptr->name1) continue;
  1584. /* Require identical "ego-item" names */
  1585. if (o_ptr->name2 != j_ptr->name2) continue;
  1586. /* Lights must have same amount of fuel */
  1587. else if (o_ptr->timeout != j_ptr->timeout && o_ptr->tval == TV_LIGHT)
  1588. continue;
  1589. /* Require identical "values" */
  1590. if (o_ptr->ac != j_ptr->ac) continue;
  1591. if (o_ptr->dd != j_ptr->dd) continue;
  1592. if (o_ptr->ds != j_ptr->ds) continue;
  1593. /* Probably okay */
  1594. break;
  1595. }
  1596. /* Various */
  1597. default:
  1598. {
  1599. /* Require knowledge */
  1600. if (!object_is_known(o_ptr) || !object_is_known(j_ptr)) continue;
  1601. /* Probably okay */
  1602. break;
  1603. }
  1604. }
  1605. /* Different flags */
  1606. if (!of_is_equal(o_ptr->flags, j_ptr->flags))
  1607. continue;
  1608. /* They match, so add up */
  1609. num += j_ptr->number;
  1610. }
  1611. return num;
  1612. }
  1613. /*
  1614. * Buy the item with the given index from the current store's inventory.
  1615. */
  1616. void do_cmd_buy(cmd_code code, cmd_arg args[])
  1617. {
  1618. int item = args[0].item;
  1619. int amt = args[1].number;
  1620. object_type *o_ptr;
  1621. object_type object_type_body;
  1622. object_type *i_ptr = &object_type_body;
  1623. char o_name[80];
  1624. int price, item_new;
  1625. store_type *st_ptr;
  1626. int this_store = current_store();
  1627. if (this_store == STORE_NONE)
  1628. {
  1629. msg_print("You cannot purchase items when not in a store.");
  1630. return;
  1631. }
  1632. st_ptr = &store[this_store];
  1633. /* Get the actual object */
  1634. o_ptr = &st_ptr->stock[item];
  1635. /* Get desired object */
  1636. object_copy_amt(i_ptr, o_ptr, amt);
  1637. /* Ensure we have room */
  1638. if (!inven_carry_okay(i_ptr))
  1639. {
  1640. msg_print("You cannot carry that many items.");
  1641. return;
  1642. }
  1643. /* Describe the object (fully) */
  1644. object_desc(o_name, sizeof(o_name), i_ptr, ODESC_PREFIX | ODESC_FULL);
  1645. /* Extract the price for the entire stack */
  1646. price = price_item(i_ptr, FALSE, i_ptr->number);
  1647. if (price > p_ptr->au)
  1648. {
  1649. msg_print("You cannot afford that purchase.");
  1650. return;
  1651. }
  1652. /* Spend the money */
  1653. p_ptr->au -= price;
  1654. /* Update the display */
  1655. store_flags |= STORE_GOLD_CHANGE;
  1656. /* ID objects on buy */
  1657. object_notice_everything(i_ptr);
  1658. /* Combine / Reorder the pack (later) */
  1659. p_ptr->notice |= (PN_COMBINE | PN_REORDER | PN_SORT_QUIVER | PN_SQUELCH);
  1660. /* The object no longer belongs to the store */
  1661. i_ptr->ident &= ~(IDENT_STORE);
  1662. /* Message */
  1663. if (one_in_(3)) message(MSG_STORE5, 0, ONE_OF(comment_accept));
  1664. msg_format("You bought %s for %ld gold.", o_name, (long)price);
  1665. /* Erase the inscription */
  1666. i_ptr->note = 0;
  1667. /* Give it to the player */
  1668. item_new = inven_carry(i_ptr);
  1669. /* Message */
  1670. object_desc(o_name, sizeof(o_name), &inventory[item_new],
  1671. ODESC_PREFIX | ODESC_FULL);
  1672. msg_format("You have %s (%c).", o_name, index_to_label(item_new));
  1673. /* Hack - Reduce the number of charges in the original stack */
  1674. if (o_ptr->tval == TV_WAND || o_ptr->tval == TV_STAFF)
  1675. {
  1676. o_ptr->pval -= i_ptr->pval;
  1677. }
  1678. /* Handle stuff */
  1679. handle_stuff();
  1680. /* Remove the bought objects from the store */
  1681. store_item_increase(this_store, item, -amt);
  1682. store_item_optimize(this_store, item);
  1683. /* Store is empty */
  1684. if (st_ptr->stock_num == 0)
  1685. {
  1686. int i;
  1687. /* Shuffle */
  1688. if (one_in_(STORE_SHUFFLE))
  1689. {
  1690. /* Message */
  1691. msg_print("The shopkeeper retires.");
  1692. /* Shuffle the store */
  1693. store_shuffle(this_store);
  1694. store_flags |= STORE_FRAME_CHANGE;
  1695. }
  1696. /* Maintain */
  1697. else
  1698. {
  1699. /* Message */
  1700. msg_print("The shopkeeper brings out some new stock.");
  1701. }
  1702. /* New inventory */
  1703. for (i = 0; i < 10; ++i)
  1704. {
  1705. /* Maintain the store */
  1706. store_maint(this_store);
  1707. }
  1708. }
  1709. event_signal(EVENT_INVENTORY);
  1710. event_signal(EVENT_EQUIPMENT);
  1711. }
  1712. /*
  1713. * Retrieve the item with the given index from the home's inventory.
  1714. */
  1715. void do_cmd_retrieve(cmd_code code, cmd_arg args[])
  1716. {
  1717. int item = args[0].item;
  1718. int amt = args[1].number;
  1719. object_type *o_ptr;
  1720. object_type picked_item

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