1 /**
2 * \file store.c
3 * \brief Store stocking
4 *
5 * Copyright (c) 1997 Robert A. Koeneke, James E. Wilson, Ben Harrison
6 * Copyright (c) 2007 Andi Sidwell
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
20 #include "angband.h"
21 #include "cave.h"
22 #include "cmds.h"
23 #include "game-event.h"
24 #include "game-world.h"
25 #include "hint.h"
26 #include "init.h"
27 #include "monster.h"
28 #include "obj-desc.h"
29 #include "obj-gear.h"
30 #include "obj-ignore.h"
31 #include "obj-info.h"
32 #include "obj-knowledge.h"
33 #include "obj-make.h"
34 #include "obj-pile.h"
35 #include "obj-power.h"
36 #include "obj-slays.h"
37 #include "obj-tval.h"
38 #include "obj-util.h"
39 #include "player-calcs.h"
40 #include "player-history.h"
41 #include "player-spell.h"
42 #include "store.h"
43 #include "target.h"
44 #include "debug.h"
45
46
47 static void store_maint(struct store *s);
48
49 /**
50 * ------------------------------------------------------------------------
51 * Constants and definitions
52 * ------------------------------------------------------------------------ */
53
54
55 /**
56 * Array[MAX_STORES] of stores
57 */
58 struct store *stores;
59
60 /**
61 * The hints array
62 */
63 struct hint *hints;
64
65
66 static const char *obj_flags[] = {
67 "NONE",
68 #define OF(a) #a,
69 #include "list-object-flags.h"
70 #undef OF
71 NULL
72 };
73
74 /**
75 * Return the store instance at the given location
76 */
store_at(struct chunk * c,struct loc grid)77 struct store *store_at(struct chunk *c, struct loc grid)
78 {
79 if (square_isshop(c, grid))
80 return &stores[square_shopnum(cave, grid)];
81
82 return NULL;
83 }
84
85
86 /**
87 * Create a new store.
88 */
store_new(int idx)89 static struct store *store_new(int idx) {
90 struct store *s = mem_zalloc(sizeof *s);
91 s->sidx = idx;
92 s->stock_size = z_info->store_inven_max;
93 return s;
94 }
95
96 /**
97 * Get rid of stores at cleanup. Gets rid of everything.
98 */
cleanup_stores(void)99 void cleanup_stores(void)
100 {
101 struct owner *o, *o_next;
102 struct object_buy *buy, *buy_next;
103 int i;
104
105 if (!stores)
106 return;
107
108 /* Free the store inventories */
109 for (i = 0; i < MAX_STORES; i++) {
110 /* Get the store */
111 struct store *store = &stores[i];
112
113 /* Free the store inventory */
114 object_pile_free(store->stock_k);
115 object_pile_free(store->stock);
116 mem_free(store->always_table);
117 mem_free(store->normal_table);
118
119 for (o = store->owners; o; o = o_next) {
120 o_next = o->next;
121 string_free(o->name);
122 mem_free(o);
123 }
124
125 for (buy = store->buy; buy; buy = buy_next) {
126 buy_next = buy->next;
127 mem_free(buy);
128 }
129
130 string_free((void *)store->name);
131 }
132 mem_free(stores);
133 }
134
135
136 /**
137 * ------------------------------------------------------------------------
138 * Edit file parsing
139 * ------------------------------------------------------------------------ */
140
141
142 /** store.txt **/
143
parse_store(struct parser * p)144 static enum parser_error parse_store(struct parser *p) {
145 struct store *h = parser_priv(p);
146 struct store *s;
147 unsigned int idx = parser_getuint(p, "index") - 1;
148
149 if (idx >= MAX_STORES)
150 return PARSE_ERROR_OUT_OF_BOUNDS;
151
152 s = store_new(parser_getuint(p, "index") - 1);
153 s->name = string_make(parser_getstr(p, "name"));
154 s->next = h;
155 parser_setpriv(p, s);
156 return PARSE_ERROR_NONE;
157 }
158
parse_slots(struct parser * p)159 static enum parser_error parse_slots(struct parser *p) {
160 struct store *s = parser_priv(p);
161 s->normal_stock_min = parser_getuint(p, "min");
162 s->normal_stock_max = parser_getuint(p, "max");
163 return PARSE_ERROR_NONE;
164 }
165
parse_turnover(struct parser * p)166 static enum parser_error parse_turnover(struct parser *p) {
167 struct store *s = parser_priv(p);
168 s->turnover = parser_getuint(p, "turnover");
169 return PARSE_ERROR_NONE;
170 }
171
parse_normal(struct parser * p)172 static enum parser_error parse_normal(struct parser *p) {
173 struct store *s = parser_priv(p);
174 int tval = tval_find_idx(parser_getsym(p, "tval"));
175 int sval = lookup_sval(tval, parser_getsym(p, "sval"));
176
177 struct object_kind *kind = lookup_kind(tval, sval);
178 if (!kind)
179 return PARSE_ERROR_UNRECOGNISED_SVAL;
180
181 /* Expand if necessary */
182 if (!s->normal_num) {
183 s->normal_size = 16;
184 s->normal_table = mem_zalloc(s->normal_size * sizeof *s->normal_table);
185 } else if (s->normal_num >= s->normal_size) {
186 s->normal_size += 8;
187 s->normal_table = mem_realloc(s->normal_table, s->normal_size * sizeof *s->normal_table);
188 }
189
190 s->normal_table[s->normal_num++] = kind;
191
192 return PARSE_ERROR_NONE;
193 }
194
parse_always(struct parser * p)195 static enum parser_error parse_always(struct parser *p) {
196 struct store *s = parser_priv(p);
197 int tval = tval_find_idx(parser_getsym(p, "tval"));
198 struct object_kind *kind = NULL;
199
200 /* Mostly svals are given, but special handling is needed for books */
201 if (parser_hasval(p, "sval")) {
202 int sval = lookup_sval(tval, parser_getsym(p, "sval"));
203 kind = lookup_kind(tval, sval);
204 if (!kind) {
205 return PARSE_ERROR_UNRECOGNISED_SVAL;
206 }
207
208 /* Expand if necessary */
209 if (!s->always_num) {
210 s->always_size = 8;
211 s->always_table = mem_zalloc(s->always_size * sizeof *s->always_table);
212 } else if (s->always_num >= s->always_size) {
213 s->always_size += 8;
214 s->always_table = mem_realloc(s->always_table, s->always_size * sizeof *s->always_table);
215 }
216
217 s->always_table[s->always_num++] = kind;
218 } else {
219 /* Books */
220 struct object_base *book_base = &kb_info[tval];
221 int i;
222
223 /* Run across all the books for this type, add the town books */
224 for (i = 1; i <= book_base->num_svals; i++) {
225 const struct class_book *book = NULL;
226 kind = lookup_kind(tval, i);
227 book = object_kind_to_book(kind);
228 if (!book->dungeon) {
229 /* Expand if necessary */
230 if (!s->always_num) {
231 s->always_size = 8;
232 s->always_table = mem_zalloc(s->always_size * sizeof *s->always_table);
233 } else if (s->always_num >= s->always_size) {
234 s->always_size += 8;
235 s->always_table = mem_realloc(s->always_table, s->always_size * sizeof *s->always_table);
236 }
237
238 s->always_table[s->always_num++] = kind;
239 }
240 }
241 }
242
243 return PARSE_ERROR_NONE;
244 }
245
parse_owner(struct parser * p)246 static enum parser_error parse_owner(struct parser *p) {
247 struct store *s = parser_priv(p);
248 unsigned int maxcost = parser_getuint(p, "purse");
249 char *name = string_make(parser_getstr(p, "name"));
250 struct owner *o;
251
252 if (!s)
253 return PARSE_ERROR_MISSING_RECORD_HEADER;
254
255 o = mem_zalloc(sizeof *o);
256 o->oidx = (s->owners ? s->owners->oidx + 1 : 0);
257 o->next = s->owners;
258 o->name = name;
259 o->max_cost = maxcost;
260 s->owners = o;
261 return PARSE_ERROR_NONE;
262 }
263
parse_buy(struct parser * p)264 static enum parser_error parse_buy(struct parser *p) {
265 struct store *s = parser_priv(p);
266 struct object_buy *buy;
267
268 if (!s)
269 return PARSE_ERROR_MISSING_RECORD_HEADER;
270
271 buy = mem_zalloc(sizeof(*buy));
272 buy->tval = tval_find_idx(parser_getstr(p, "base"));
273 buy->next = s->buy;
274 s->buy = buy;
275 return PARSE_ERROR_NONE;
276 }
277
parse_buy_flag(struct parser * p)278 static enum parser_error parse_buy_flag(struct parser *p) {
279 struct store *s = parser_priv(p);
280 int flag;
281
282 if (!s)
283 return PARSE_ERROR_MISSING_RECORD_HEADER;
284
285 flag = lookup_flag(obj_flags, parser_getsym(p, "flag"));
286
287 if (flag == FLAG_END) {
288 return PARSE_ERROR_INVALID_FLAG;
289 } else {
290 struct object_buy *buy = mem_zalloc(sizeof(*buy));
291
292 buy->flag = flag;
293 buy->tval = tval_find_idx(parser_getstr(p, "base"));
294 buy->next = s->buy;
295 s->buy = buy;
296
297 return PARSE_ERROR_NONE;
298 }
299 }
300
init_parse_stores(void)301 struct parser *init_parse_stores(void) {
302 struct parser *p = parser_new();
303 parser_setpriv(p, NULL);
304 parser_reg(p, "store uint index str name", parse_store);
305 parser_reg(p, "owner uint purse str name", parse_owner);
306 parser_reg(p, "slots uint min uint max", parse_slots);
307 parser_reg(p, "turnover uint turnover", parse_turnover);
308 parser_reg(p, "normal sym tval sym sval", parse_normal);
309 parser_reg(p, "always sym tval ?sym sval", parse_always);
310 parser_reg(p, "buy str base", parse_buy);
311 parser_reg(p, "buy-flag sym flag str base", parse_buy_flag);
312 return p;
313 }
314
run_parse_stores(struct parser * p)315 static errr run_parse_stores(struct parser *p) {
316 return parse_file_quit_not_found(p, "store");
317 }
318
finish_parse_stores(struct parser * p)319 static errr finish_parse_stores(struct parser *p) {
320 stores = parser_priv(p);
321 parser_destroy(p);
322 return 0;
323 }
324
325 static struct file_parser store_parser = {
326 "store",
327 init_parse_stores,
328 run_parse_stores,
329 finish_parse_stores,
330 NULL
331 };
332
333
334 /**
335 * ------------------------------------------------------------------------
336 * Other init stuff
337 * ------------------------------------------------------------------------ */
338
339
flatten_stores(struct store * store_list)340 static struct store *flatten_stores(struct store *store_list) {
341 struct store *s;
342 struct store *stores_local = mem_zalloc(MAX_STORES * sizeof(*stores_local));
343
344 for (s = store_list; s; s = s->next) {
345 if (s->sidx < MAX_STORES)
346 memcpy(&stores_local[s->sidx], s, sizeof(*s));
347 }
348
349 while (store_list) {
350 s = store_list->next;
351 /* No need to free the sub-allocated memory, as this is passed on
352 * to the array of stores */
353 mem_free(store_list);
354 store_list = s;
355 }
356
357 return stores_local;
358 }
359
store_init(void)360 void store_init(void)
361 {
362 event_signal_message(EVENT_INITSTATUS, 0, "Initializing stores...");
363 if (run_parser(&store_parser)) quit("Can't initialize stores");
364 stores = flatten_stores(stores);
365 }
366
store_reset(void)367 void store_reset(void) {
368 int i, j;
369 struct store *s;
370
371 for (i = 0; i < MAX_STORES; i++) {
372 s = &stores[i];
373 s->stock_num = 0;
374 store_shuffle(s);
375 object_pile_free(s->stock);
376 s->stock = NULL;
377 if (i == STORE_HOME)
378 continue;
379 for (j = 0; j < 10; j++)
380 store_maint(s);
381 }
382 }
383
384
385 struct init_module store_module = {
386 .name = "store",
387 .init = store_init,
388 .cleanup = cleanup_stores
389 };
390
391
392
393
394
395 /**
396 * Check if a given item kind is an always-stocked item.
397 */
store_is_staple(struct store * s,struct object_kind * k)398 static bool store_is_staple(struct store *s, struct object_kind *k) {
399 size_t i;
400
401 assert(s);
402 assert(k);
403
404 for (i = 0; i < s->always_num; i++) {
405 struct object_kind *l = s->always_table[i];
406 if (k == l)
407 return true;
408 }
409
410 return false;
411 }
412
413 /**
414 * Check if a given item kind is an always-stocked or sometimes-stocked item.
415 */
store_can_carry(struct store * store,struct object_kind * kind)416 static bool store_can_carry(struct store *store, struct object_kind *kind) {
417 size_t i;
418
419 for (i = 0; i < store->normal_num; i++) {
420 if (store->normal_table[i] == kind)
421 return true;
422 }
423
424 return store_is_staple(store, kind);
425 }
426
427
428
429
430 /**
431 * ------------------------------------------------------------------------
432 * Utilities
433 * ------------------------------------------------------------------------ */
434
435
436 /* Randomly select one of the entries in an array */
437 #define ONE_OF(x) x[randint0(N_ELEMENTS(x))]
438
439
440 /**
441 * ------------------------------------------------------------------------
442 * Flavour text stuff
443 * ------------------------------------------------------------------------ */
444
445
446 /**
447 * Messages for reacting to purchase prices.
448 */
449 static const char *comment_worthless[] =
450 {
451 "Arrgghh!",
452 "You bastard!",
453 "You hear someone sobbing...",
454 "The shopkeeper howls in agony!",
455 "The shopkeeper wails in anguish!",
456 "The shopkeeper beats his head against the counter."
457 };
458
459 static const char *comment_bad[] =
460 {
461 "Damn!",
462 "You fiend!",
463 "The shopkeeper curses at you.",
464 "The shopkeeper glares at you."
465 };
466
467 static const char *comment_accept[] =
468 {
469 "Okay.",
470 "Fine.",
471 "Accepted!",
472 "Agreed!",
473 "Done!",
474 "Taken!"
475 };
476
477 static const char *comment_good[] =
478 {
479 "Cool!",
480 "You've made my day!",
481 "The shopkeeper sniggers.",
482 "The shopkeeper giggles.",
483 "The shopkeeper laughs loudly."
484 };
485
486 static const char *comment_great[] =
487 {
488 "Yipee!",
489 "I think I'll retire!",
490 "The shopkeeper jumps for joy.",
491 "The shopkeeper smiles gleefully.",
492 "Wow. I'm going to name my new villa in your honour."
493 };
494
495
496
497
498
499
500 /**
501 * Let a shop-keeper React to a purchase
502 *
503 * We paid "price", it was worth "value", and we thought it was worth "guess"
504 */
purchase_analyze(int price,int value,int guess)505 static void purchase_analyze(int price, int value, int guess)
506 {
507 /* Item was worthless, but we bought it */
508 if ((value <= 0) && (price > value))
509 msgt(MSG_STORE1, "%s", ONE_OF(comment_worthless));
510
511 /* Item was cheaper than we thought, and we paid more than necessary */
512 else if ((value < guess) && (price > value))
513 msgt(MSG_STORE2, "%s", ONE_OF(comment_bad));
514
515 /* Item was a good bargain, and we got away with it */
516 else if ((value > guess) && (value < (4 * guess)) && (price < value))
517 msgt(MSG_STORE3, "%s", ONE_OF(comment_good));
518
519 /* Item was a great bargain, and we got away with it */
520 else if ((value > guess) && (price < value))
521 msgt(MSG_STORE4, "%s", ONE_OF(comment_great));
522 }
523
524
525
526
527 /**
528 * ------------------------------------------------------------------------
529 * Check if a store will buy an object
530 * ------------------------------------------------------------------------ */
531
532
533 /**
534 * Determine if the current store will purchase the given object
535 *
536 * Note that a shop-keeper must refuse to buy "worthless" objects
537 */
store_will_buy(struct store * store,const struct object * obj)538 static bool store_will_buy(struct store *store, const struct object *obj)
539 {
540 struct object_buy *buy;
541
542 /* Home accepts anything */
543 if (store->sidx == STORE_HOME) return true;
544
545 /* Ignore apparently worthless items, except no-selling {??} items */
546 if (object_value(obj, 1) <= 0 && !(OPT(player, birth_no_selling) &&
547 tval_has_variable_power(obj) &&
548 !object_runes_known(obj))) {
549 return false;
550 }
551
552 /* No buy list means we buy anything */
553 if (!store->buy) return true;
554
555 /* Run through the buy list */
556 for (buy = store->buy; buy; buy = buy->next) {
557 /* Wrong tval */
558 if (buy->tval != obj->tval) continue;
559
560 /* No flag means we're good */
561 if (!buy->flag) return true;
562
563 /* OK if the object is known to have the flag */
564 if (of_has(obj->flags, buy->flag) &&
565 object_flag_is_known(obj, buy->flag))
566 return true;
567 }
568
569 /* Not on the list */
570 return false;
571 }
572
573
574 /**
575 * ------------------------------------------------------------------------
576 * Basics: pricing, generation, etc.
577 * ------------------------------------------------------------------------ */
578
579
580 /**
581 * Determine the price of an object (qty one) in a store.
582 *
583 * store_buying == true means the shop is buying, player selling
584 * == false means the shop is selling, player buying
585 *
586 * This function never lets a shop-keeper lose money in a transaction.
587 *
588 * The "greed" value should exceed 100 when the player is "buying" the
589 * object, and should be less than 100 when the player is "selling" it.
590 *
591 * Hack -- the black market always charges twice as much as it should.
592 */
price_item(struct store * store,const struct object * obj,bool store_buying,int qty)593 int price_item(struct store *store, const struct object *obj,
594 bool store_buying, int qty)
595 {
596 int adjust = 100;
597 int price;
598 struct owner *proprietor;
599
600 if (!store) {
601 return 0;
602 }
603
604 proprietor = store->owner;
605
606 /* Get the value of the stack of wands, or a single item */
607 if (tval_can_have_charges(obj)) {
608 price = MIN(object_value_real(obj, qty), object_value(obj, qty));
609 } else {
610 price = MIN(object_value_real(obj, 1), object_value(obj, 1));
611 }
612
613 /* Worthless items */
614 if (price <= 0) {
615 return 0;
616 }
617
618 /* The black market is always a worse deal */
619 if (store->sidx == STORE_B_MARKET)
620 adjust = 150;
621
622 /* Shop is buying */
623 if (store_buying) {
624 /* Set the factor */
625 adjust = 100 + (100 - adjust);
626 if (adjust > 100) {
627 adjust = 100;
628 }
629
630 /* Shops now pay 2/3 of true value */
631 price = price * 2 / 3;
632
633 /* Black market sucks */
634 if (store->sidx == STORE_B_MARKET) {
635 price = price / 2;
636 }
637
638 /* Check for no_selling option */
639 if (OPT(player, birth_no_selling)) {
640 return 0;
641 }
642 } else {
643 /* Re-evaluate if we're selling */
644 if (tval_can_have_charges(obj)) {
645 price = object_value_real(obj, qty);
646 } else {
647 price = object_value_real(obj, 1);
648 }
649
650 /* Black market sucks */
651 if (store->sidx == STORE_B_MARKET) {
652 price = price * 2;
653 }
654 }
655
656 /* Compute the final price (with rounding) */
657 price = (price * adjust + 50L) / 100L;
658
659 /* Now convert price to total price for non-wands */
660 if (!tval_can_have_charges(obj)) {
661 price *= qty;
662 }
663
664 /* Now limit the price to the purse limit */
665 if (store_buying && (price > proprietor->max_cost * qty)) {
666 price = proprietor->max_cost * qty;
667 }
668
669 /* Note -- Never become "free" */
670 if (price <= 0) {
671 return qty;
672 }
673
674 /* Return the price */
675 return price;
676 }
677
678
679 /**
680 * Special "mass production" computation.
681 */
mass_roll(int times,int max)682 static int mass_roll(int times, int max)
683 {
684 int i, t = 0;
685
686 assert(max > 1);
687
688 for (i = 0; i < times; i++)
689 t += randint0(max);
690
691 return (t);
692 }
693
694
695 /**
696 * Some cheap objects should be created in piles.
697 */
mass_produce(struct object * obj)698 static void mass_produce(struct object *obj)
699 {
700 int size = 1;
701 int cost = object_value_real(obj, 1);
702
703 /* Analyze the type */
704 switch (obj->tval)
705 {
706 /* Food, Flasks, and Lights */
707 case TV_FOOD:
708 case TV_MUSHROOM:
709 case TV_FLASK:
710 case TV_LIGHT:
711 {
712 if (cost <= 5L) size += mass_roll(3, 5);
713 if (cost <= 20L) size += mass_roll(3, 5);
714 break;
715 }
716
717 case TV_POTION:
718 case TV_SCROLL:
719 {
720 if (cost <= 60L) size += mass_roll(3, 5);
721 if (cost <= 240L) size += mass_roll(1, 5);
722 break;
723 }
724
725 case TV_MAGIC_BOOK:
726 case TV_PRAYER_BOOK:
727 case TV_NATURE_BOOK:
728 case TV_SHADOW_BOOK:
729 case TV_OTHER_BOOK:
730 {
731 if (cost <= 50L) size += mass_roll(2, 3);
732 if (cost <= 500L) size += mass_roll(1, 3);
733 break;
734 }
735
736 case TV_SOFT_ARMOR:
737 case TV_HARD_ARMOR:
738 case TV_SHIELD:
739 case TV_GLOVES:
740 case TV_BOOTS:
741 case TV_CLOAK:
742 case TV_HELM:
743 case TV_CROWN:
744 case TV_SWORD:
745 case TV_POLEARM:
746 case TV_HAFTED:
747 case TV_DIGGING:
748 case TV_BOW:
749 {
750 if (obj->ego) break;
751 if (cost <= 10L) size += mass_roll(3, 5);
752 if (cost <= 100L) size += mass_roll(3, 5);
753 break;
754 }
755
756 case TV_SHOT:
757 case TV_ARROW:
758 case TV_BOLT:
759 {
760 if (cost <= 5L)
761 size = randint1(2) * 20; /* 20-40 in 20s */
762 else if (cost > 5L && cost <= 50L)
763 size = randint1(4) * 10; /* 10-40 in 10s */
764 else if (cost > 50 && cost <= 500L)
765 size = randint1(4) * 5; /* 5-20 in 5s */
766 else
767 size = 1;
768
769 break;
770 }
771 }
772
773 /* Save the total pile size */
774 obj->number = MIN(size, obj->kind->base->max_stack);
775 }
776
777
778 /**
779 * Sort the store inventory into an ordered array.
780 */
store_stock_list(struct store * store,struct object ** list,int n)781 void store_stock_list(struct store *store, struct object **list, int n)
782 {
783 bool home = (store->sidx != STORE_HOME);
784 int list_num;
785 int num = 0;
786
787 for (list_num = 0; list_num < n; list_num++) {
788 struct object *current, *first = NULL;
789 for (current = store->stock; current; current = current->next) {
790 int i;
791 bool possible = true;
792
793 /* Skip objects already allocated */
794 for (i = 0; i < num; i++)
795 if (list[i] == current)
796 possible = false;
797
798 /* If still possible, choose the first in order */
799 if (!possible)
800 continue;
801 else if (earlier_object(first, current, home))
802 first = current;
803 }
804
805 /* Allocate and count the stock */
806 list[list_num] = first;
807 if (first)
808 num++;
809 }
810 }
811
812 /**
813 * Allow a store object to absorb another object
814 */
store_object_absorb(struct object * old,struct object * new)815 static void store_object_absorb(struct object *old, struct object *new)
816 {
817 int total = old->number + new->number;
818
819 /* Combine quantity, lose excess items */
820 old->number = MIN(total, old->kind->base->max_stack);
821
822 /* If rods are stacking, add the charging timeouts */
823 if (tval_can_have_timeout(old))
824 old->timeout += new->timeout;
825
826 /* If wands/staves are stacking, combine the charges */
827 if (tval_can_have_charges(old))
828 old->pval += new->pval;
829
830 object_origin_combine(old, new);
831
832 /* Fully absorbed */
833 object_delete(&new);
834 }
835
836
837 /**
838 * Check to see if the shop will be carrying too many objects
839 *
840 * Note that the shop, just like a player, will not accept things
841 * it cannot hold. Before, one could "nuke" objects this way, by
842 * adding them to a pile which was already full.
843 */
store_check_num(struct store * store,const struct object * obj)844 bool store_check_num(struct store *store, const struct object *obj)
845 {
846 struct object *stock_obj;
847
848 /* Free space is always usable */
849 if (store->stock_num < store->stock_size) return true;
850
851 /* The "home" acts like the player */
852 if (store->sidx == STORE_HOME) {
853 for (stock_obj = store->stock; stock_obj; stock_obj = stock_obj->next) {
854 /* Can the new object be combined with the old one? */
855 if (object_similar(stock_obj, obj, OSTACK_PACK))
856 return true;
857 }
858 } else {
859 /* Normal stores do special stuff */
860 for (stock_obj = store->stock; stock_obj; stock_obj = stock_obj->next) {
861 /* Can the new object be combined with the old one? */
862 if (object_similar(stock_obj, obj, OSTACK_STORE))
863 return true;
864 }
865 }
866
867 /* But there was no room at the inn... */
868 return false;
869 }
870
871
872 /**
873 * Add an object to the inventory of the Home.
874 *
875 * Also note that it may not correctly "adapt" to "knowledge" becoming
876 * known: the player may have to pick stuff up and drop it again.
877 */
home_carry(struct object * obj)878 void home_carry(struct object *obj)
879 {
880 struct object *temp_obj;
881 struct store *store = &stores[STORE_HOME];
882
883 /* Check each existing object (try to combine) */
884 for (temp_obj = store->stock; temp_obj; temp_obj = temp_obj->next) {
885 /* The home acts just like the player */
886 if (object_similar(temp_obj, obj, OSTACK_PACK)) {
887 /* Save the new number of items */
888 object_absorb(temp_obj, obj);
889 return;
890 }
891 }
892
893 /* No space? */
894 if (store->stock_num >= store->stock_size) return;
895
896 /* Insert the new object */
897 pile_insert(&store->stock, obj);
898 pile_insert(&store->stock_k, obj->known);
899 store->stock_num++;
900 }
901
902
903 /**
904 * Add an object to a real stores inventory.
905 *
906 * If the object is "worthless", it is thrown away (except in the home).
907 *
908 * If the object cannot be combined with an object already in the inventory,
909 * make a new slot for it, and calculate its "per item" price. Note that
910 * this price will be negative, since the price will not be "fixed" yet.
911 * Adding an object to a "fixed" price stack will not change the fixed price.
912 *
913 * Returns the object inserted (for ease of use) or NULL if it disappears
914 */
store_carry(struct store * store,struct object * obj)915 struct object *store_carry(struct store *store, struct object *obj)
916 {
917 unsigned int i;
918 u32b value;
919 struct object *temp_obj, *known_obj = obj->known;
920
921 struct object_kind *kind = obj->kind;
922
923 /* Evaluate the object */
924 if (object_is_carried(player, obj))
925 value = object_value(obj, 1);
926 else
927 value = object_value_real(obj, 1);
928
929 /* Cursed/Worthless items "disappear" when sold */
930 if (value <= 0)
931 return NULL;
932
933 /* Erase the inscription */
934 obj->note = 0;
935 known_obj->note = 0;
936
937 /* Some item types require maintenance */
938 if (tval_is_light(obj)) {
939 if (!of_has(obj->flags, OF_NO_FUEL)) {
940 if (of_has(obj->flags, OF_BURNS_OUT))
941 obj->timeout = z_info->fuel_torch;
942
943 else if (of_has(obj->flags, OF_TAKES_FUEL))
944 obj->timeout = z_info->default_lamp;
945 }
946 } else if (tval_can_have_timeout(obj)) {
947 obj->timeout = 0;
948 } else if (tval_is_launcher(obj)) {
949 obj->known->pval = obj->pval;
950 } else if (tval_can_have_charges(obj)) {
951 /* If the store can stock this item kind, we recharge */
952 if (store_can_carry(store, obj->kind)) {
953 int charges = 0;
954
955 /* Calculate the recharged number of charges */
956 for (i = 0; i < obj->number; i++)
957 charges += randcalc(kind->charge, 0, RANDOMISE);
958
959 /* Use recharged value only if greater */
960 if (charges > obj->pval)
961 obj->pval = charges;
962 }
963 }
964
965 for (temp_obj = store->stock; temp_obj; temp_obj = temp_obj->next) {
966 /* Can the existing items be incremented? */
967 if (object_similar(temp_obj, obj, OSTACK_STORE)) {
968 /* Absorb (some of) the object */
969 store_object_absorb(temp_obj->known, known_obj);
970 obj->known = NULL;
971 store_object_absorb(temp_obj, obj);
972
973 /* All done */
974 return temp_obj;
975 }
976 }
977
978 /* No space? */
979 if (store->stock_num >= store->stock_size)
980 return NULL;
981
982 /* Insert the new object */
983 pile_insert(&store->stock, obj);
984 pile_insert(&store->stock_k, known_obj);
985 store->stock_num++;
986
987 return obj;
988 }
989
990
store_delete(struct store * s,struct object * obj,int amt)991 void store_delete(struct store *s, struct object *obj, int amt)
992 {
993 struct object *known_obj = obj->known;
994
995 if (obj->number > amt) {
996 obj->number -= amt;
997 known_obj->number -= amt;
998 } else {
999 pile_excise(&s->stock, obj);
1000 object_delete(&obj);
1001 pile_excise(&s->stock_k, known_obj);
1002 object_delete(&known_obj);
1003 assert(s->stock_num);
1004 s->stock_num--;
1005 }
1006 }
1007
1008
1009 /**
1010 * Find a given object kind in the store.
1011 */
store_find_kind(struct store * s,struct object_kind * k)1012 static struct object *store_find_kind(struct store *s, struct object_kind *k) {
1013 struct object *obj;
1014
1015 assert(s);
1016 assert(k);
1017
1018 /* Check if it's already in stock */
1019 for (obj = s->stock; obj; obj = obj->next) {
1020 if (obj->kind == k && !obj->ego)
1021 return obj;
1022 }
1023
1024 return NULL;
1025 }
1026
1027
1028 /**
1029 * Delete an object from store 'store', or, if it is a stack, perhaps only
1030 * partially delete it.
1031 *
1032 * This function is used when store maintainance occurs, and is designed to
1033 * imitate non-PC purchasers making purchases from the store.
1034 *
1035 * The reason this doesn't check for "staple" items and refuse to
1036 * delete them is that a store could conceviably have two stacks of a
1037 * single staple item, in which case, you could have a store which had
1038 * more stacks than staple items, but all stacks are staple items.
1039 */
store_delete_random(struct store * store)1040 static void store_delete_random(struct store *store)
1041 {
1042 int what;
1043 int num;
1044 struct object *obj;
1045
1046 assert(store->stock_num > 0);
1047
1048 /* Pick a random slot */
1049 what = randint0(store->stock_num);
1050
1051 /* Walk through list until we find our item */
1052 obj = store->stock;
1053 while (what--) {
1054 assert(obj);
1055 obj = obj->next;
1056 }
1057
1058 /* Determine how many objects are in the slot */
1059 num = obj->number;
1060
1061 /* Deal with stacks */
1062 if (num > 1) {
1063 /* Special behaviour for arrows, bolts &tc. */
1064 if (tval_is_ammo(obj)) {
1065 /* 50% of the time, destroy the entire stack */
1066 if (randint0(100) < 50 || num < 10)
1067 num = obj->number;
1068
1069 /* 50% of the time, reduce the size to a multiple of 5 */
1070 else
1071 num = randint1(num / 5) * 5 + (num % 5);
1072 } else {
1073 /* 50% of the time, destroy a single object */
1074 if (randint0(100) < 50) num = 1;
1075
1076 /* 25% of the time, destroy half the objects */
1077 else if (randint0(100) < 50) num = (num + 1) / 2;
1078
1079 /* 25% of the time, destroy all objects */
1080 else num = obj->number;
1081
1082 /* Hack -- decrement the total charges of staves and wands. */
1083 if (tval_can_have_charges(obj))
1084 obj->pval -= num * obj->pval / obj->number;
1085 }
1086 }
1087
1088 assert (num <= obj->number);
1089
1090 if (obj->artifact) {
1091 history_lose_artifact(player, obj->artifact);
1092 }
1093
1094 /* Delete the item, wholly or in part */
1095 store_delete(store, obj, num);
1096 }
1097
1098
1099 /**
1100 * This makes sure that the black market doesn't stock any object that other
1101 * stores have, unless it is an ego-item or has various bonuses.
1102 *
1103 * Based on a suggestion by Lee Vogt <lvogt@cig.mcel.mot.com>.
1104 */
black_market_ok(const struct object * obj)1105 static bool black_market_ok(const struct object *obj)
1106 {
1107 int i;
1108
1109 /* Ego items are always fine */
1110 if (obj->ego) return true;
1111
1112 /* Good items are normally fine */
1113 if (obj->to_a > 2) return true;
1114 if (obj->to_h > 1) return true;
1115 if (obj->to_d > 2) return true;
1116
1117 /* No cheap items */
1118 if (object_value_real(obj, 1) < 10) return (false);
1119
1120 /* Check the other stores */
1121 for (i = 0; i < MAX_STORES; i++) {
1122 struct object *stock_obj;
1123
1124 /* Skip home and black market */
1125 if (i == STORE_B_MARKET || i == STORE_HOME)
1126 continue;
1127
1128 /* Check every object in the store */
1129 for (stock_obj = stores[i].stock; stock_obj; stock_obj = stock_obj->next) {
1130 /* Compare object kinds */
1131 if (obj->kind == stock_obj->kind)
1132 return false;
1133 }
1134 }
1135
1136 /* Otherwise fine */
1137 return true;
1138 }
1139
1140
1141
1142 /**
1143 * Get a choice from the store allocation table, in tables.c
1144 */
store_get_choice(struct store * store)1145 static struct object_kind *store_get_choice(struct store *store)
1146 {
1147 /* Choose a random entry from the store's table */
1148 return store->normal_table[randint0(store->normal_num)];
1149 }
1150
1151
1152 /**
1153 * Creates a random object and gives it to store 'store'
1154 */
store_create_random(struct store * store)1155 static bool store_create_random(struct store *store)
1156 {
1157 int tries, level;
1158
1159 int min_level, max_level;
1160
1161 /* Decide min/max levels */
1162 if (store->sidx == STORE_B_MARKET) {
1163 min_level = player->max_depth + 5;
1164 max_level = player->max_depth + 20;
1165 } else {
1166 min_level = 1;
1167 max_level = z_info->store_magic_level + MAX(player->max_depth - 20, 0);
1168 }
1169
1170 if (min_level > 55) min_level = 55;
1171 if (max_level > 70) max_level = 70;
1172
1173 /* Consider up to six items */
1174 for (tries = 0; tries < 6; tries++) {
1175 struct object_kind *kind;
1176 struct object *obj, *known_obj;
1177
1178 /* Work out the level for objects to be generated at */
1179 level = rand_range(min_level, max_level);
1180
1181 /* Black Markets have a random object, of a given level */
1182 if (store->sidx == STORE_B_MARKET)
1183 kind = get_obj_num(level, false, 0);
1184 else
1185 kind = store_get_choice(store);
1186
1187 /*** Pre-generation filters ***/
1188
1189 /* No chests in stores XXX */
1190 if (kind->tval == TV_CHEST) continue;
1191
1192 /*** Generate the item ***/
1193
1194 /* Create a new object of the chosen kind */
1195 obj = object_new();
1196 object_prep(obj, kind, level, RANDOMISE);
1197
1198 /* Apply some "low-level" magic (no artifacts) */
1199 apply_magic(obj, level, false, false, false, false);
1200
1201 /* Reject if item is 'damaged' (negative combat mods, curses) */
1202 if ((tval_is_weapon(obj) && ((obj->to_h < 0) || (obj->to_d < 0)))
1203 || (tval_is_armor(obj) && (obj->to_a < 0)) || (obj->curses)) {
1204 object_delete(&obj);
1205 continue;
1206 }
1207
1208 /*** Post-generation filters ***/
1209
1210 /* Make a known object */
1211 known_obj = object_new();
1212 obj->known = known_obj;
1213
1214 /* Know everything the player knows, no origin yet */
1215 obj->known->notice |= OBJ_NOTICE_ASSESSED;
1216 object_set_base_known(obj);
1217 obj->known->notice |= OBJ_NOTICE_ASSESSED;
1218 player_know_object(player, obj);
1219 obj->origin = ORIGIN_NONE;
1220
1221 /* Black markets have expensive tastes */
1222 if ((store->sidx == STORE_B_MARKET) && !black_market_ok(obj)) {
1223 object_delete(&known_obj);
1224 obj->known = NULL;
1225 object_delete(&obj);
1226 continue;
1227 }
1228
1229 /* No "worthless" items */
1230 if (object_value_real(obj, 1) < 1) {
1231 object_delete(&known_obj);
1232 obj->known = NULL;
1233 object_delete(&obj);
1234 continue;
1235 }
1236
1237 /* Mass produce and/or apply discount */
1238 mass_produce(obj);
1239
1240 /* Attempt to carry the object */
1241 if (!store_carry(store, obj)) {
1242 object_delete(&known_obj);
1243 obj->known = NULL;
1244 object_delete(&obj);
1245 continue;
1246 }
1247
1248 /* Definitely done */
1249 return true;
1250 }
1251
1252 return false;
1253 }
1254
1255
1256 /**
1257 * Helper function: create an item with the given tval,sval pair, add it to the
1258 * store st. Return the item in the inventory.
1259 */
store_create_item(struct store * store,struct object_kind * kind)1260 static struct object *store_create_item(struct store *store,
1261 struct object_kind *kind)
1262 {
1263 struct object *obj = object_new();
1264 struct object *known_obj = object_new();
1265
1266 /* Create a new object of the chosen kind */
1267 object_prep(obj, kind, 0, RANDOMISE);
1268
1269 /* Know everything the player knows, no origin yet */
1270 obj->known = known_obj;
1271 obj->known->notice |= OBJ_NOTICE_ASSESSED;
1272 object_set_base_known(obj);
1273 obj->known->notice |= OBJ_NOTICE_ASSESSED;
1274 player_know_object(player, obj);
1275 obj->origin = ORIGIN_NONE;
1276
1277 /* Attempt to carry the object */
1278 return store_carry(store, obj);
1279 }
1280
1281 /**
1282 * Maintain the inventory at the stores.
1283 */
store_maint(struct store * s)1284 static void store_maint(struct store *s)
1285 {
1286 /* Ignore home */
1287 if (s->sidx == STORE_HOME)
1288 return;
1289
1290 /* Destroy crappy black market items */
1291 if (s->sidx == STORE_B_MARKET) {
1292 struct object *obj = s->stock;
1293 while (obj) {
1294 struct object *next = obj->next;
1295 if (!black_market_ok(obj))
1296 store_delete(s, obj, obj->number);
1297 obj = next;
1298 }
1299 }
1300
1301 /* We want to make sure stores have staple items. If there's
1302 * turnover, we also want to delete a few items, and add a few
1303 * items.
1304 *
1305 * If we create staple items, then delete items, then create new
1306 * items, we are stuck with one of three choices:
1307 * 1. We can risk deleting staple items, and not having any left.
1308 * 2. We can refuse to delete staple items, and risk having that
1309 * become an infinite loop.
1310 * 3. We can do a ton of extra bookkeeping to make sure we delete
1311 * staple items only if there's duplicates of them.
1312 *
1313 * What if we change the order? First sell a handful of random items,
1314 * then create any missing staples, then create new items. This
1315 * has two tests for s->turnover, but simplifies everything else
1316 * dramatically.
1317 */
1318
1319 if (s->turnover) {
1320 int restock_attempts = 100000;
1321 int stock = s->stock_num - randint1(s->turnover);
1322
1323 /* We'll end up adding staples for sure, maybe plus other
1324 * items. It's fine if we sell out completely, though, if
1325 * turnover is high. The cap doesn't include always_num,
1326 * because otherwise the addition of missing staples could
1327 * put us over (if the store was full of player-sold loot).
1328 */
1329 int min = 0;
1330 int max = s->normal_stock_max;
1331
1332 if (stock < min) stock = min;
1333 if (stock > max) stock = max;
1334
1335 /* Destroy random objects until only "stock" slots are left */
1336 while (s->stock_num > stock && --restock_attempts)
1337 store_delete_random(s);
1338
1339 if (!restock_attempts)
1340 quit_fmt("Unable to (de-)stock store %d. Please report this bug",
1341 s->sidx + 1);
1342 } else {
1343 /* For the Bookseller, occasionally sell a book */
1344 if (s->always_num && s->stock_num) {
1345 int sales = randint1(s->stock_num);
1346 while (sales--) {
1347 store_delete_random(s);
1348 }
1349 }
1350 }
1351
1352 /* Ensure staples are created */
1353 if (s->always_num) {
1354 size_t i;
1355 for (i = 0; i < s->always_num; i++) {
1356 struct object_kind *kind = s->always_table[i];
1357 struct object *obj = store_find_kind(s, kind);
1358
1359 /* Create the item if it doesn't exist */
1360 if (!obj)
1361 obj = store_create_item(s, kind);
1362
1363 /* Ensure a full stack */
1364 obj->number = obj->kind->base->max_stack;
1365 obj->known->number = obj->kind->base->max_stack;
1366 }
1367 }
1368
1369 if (s->turnover) {
1370 int restock_attempts = 100000;
1371 int stock = s->stock_num + randint1(s->turnover);
1372
1373 /* Now that the staples exist, we want to add more
1374 * items, at least enough to get us to normal_stock_min
1375 * items that aren't necessarily staples.
1376 */
1377
1378 int min = s->normal_stock_min + s->always_num;
1379 int max = s->normal_stock_max + s->always_num;
1380
1381 /* Buy a few items */
1382
1383 /* Keep stock between specified min and max slots */
1384 if (stock > max) stock = max;
1385 if (stock < min) stock = min;
1386
1387 /* For the rest, we just choose items randomlyish */
1388 /* The (huge) restock_attempts will only go to zero (otherwise
1389 * infinite loop) if stores don't have enough items they can stock! */
1390 while (s->stock_num < stock && --restock_attempts)
1391 store_create_random(s);
1392
1393 if (!restock_attempts)
1394 quit_fmt("Unable to (re-)stock store %d. Please report this bug",
1395 s->sidx + 1);
1396 }
1397 }
1398
1399 /**
1400 * Update the stores on the return to town.
1401 */
store_update(void)1402 void store_update(void)
1403 {
1404 if (OPT(player, cheat_xtra)) msg("Updating Shops...");
1405 while (daycount--) {
1406 int n;
1407
1408 /* Maintain each shop (except home) */
1409 for (n = 0; n < MAX_STORES; n++) {
1410 /* Skip the home */
1411 if (n == STORE_HOME) continue;
1412
1413 /* Maintain */
1414 store_maint(&stores[n]);
1415 }
1416
1417 /* Sometimes, shuffle the shop-keepers */
1418 if (one_in_(z_info->store_shuffle)) {
1419 /* Message */
1420 if (OPT(player, cheat_xtra)) msg("Shuffling a Shopkeeper...");
1421
1422 /* Pick a random shop (except home) */
1423 while (1) {
1424 n = randint0(MAX_STORES);
1425 if (n != STORE_HOME) break;
1426 }
1427
1428 /* Shuffle it */
1429 store_shuffle(&stores[n]);
1430 }
1431 }
1432 daycount = 0;
1433 if (OPT(player, cheat_xtra)) msg("Done.");
1434 }
1435
1436 /** Owner stuff **/
1437
store_ownerbyidx(struct store * s,unsigned int idx)1438 struct owner *store_ownerbyidx(struct store *s, unsigned int idx) {
1439 struct owner *o;
1440 for (o = s->owners; o; o = o->next) {
1441 if (o->oidx == idx)
1442 return o;
1443 }
1444
1445 quit_fmt("Bad call to store_ownerbyidx: idx is %d\n", idx);
1446 return 0; /* Needed to avoid Windows compiler warning */
1447 }
1448
store_choose_owner(struct store * s)1449 static struct owner *store_choose_owner(struct store *s) {
1450 struct owner *o;
1451 unsigned int n = 0;
1452
1453 for (o = s->owners; o; o = o->next) {
1454 n++;
1455 }
1456
1457 n = randint0(n);
1458 return store_ownerbyidx(s, n);
1459 }
1460
1461 /**
1462 * Shuffle one of the stores.
1463 */
store_shuffle(struct store * store)1464 void store_shuffle(struct store *store)
1465 {
1466 struct owner *o = store->owner;
1467
1468 while (o == store->owner)
1469 o = store_choose_owner(store);
1470
1471 store->owner = o;
1472 }
1473
1474
1475
1476
1477 /**
1478 * ------------------------------------------------------------------------
1479 * Higher-level code
1480 * ------------------------------------------------------------------------ */
1481
1482
1483 /**
1484 * Return the quantity of a given item in the pack (include quiver).
1485 */
find_inven(const struct object * obj)1486 int find_inven(const struct object *obj)
1487 {
1488 int i;
1489 struct object *gear_obj;
1490 int num = 0;
1491
1492 /* Similar slot? */
1493 for (gear_obj = player->gear; gear_obj; gear_obj = gear_obj->next) {
1494 /* Check only the inventory and the quiver */
1495 if (object_is_equipped(player->body, gear_obj))
1496 continue;
1497
1498 /* Require identical object types */
1499 if (obj->kind != gear_obj->kind)
1500 continue;
1501
1502 /* Analyze the items */
1503 switch (obj->tval)
1504 {
1505 /* Chests */
1506 case TV_CHEST:
1507 {
1508 /* Never okay */
1509 return 0;
1510 }
1511
1512 /* Food and Potions and Scrolls */
1513 case TV_FOOD:
1514 case TV_MUSHROOM:
1515 case TV_POTION:
1516 case TV_SCROLL:
1517 {
1518 /* Assume okay */
1519 break;
1520 }
1521
1522 /* Staves and Wands */
1523 case TV_STAFF:
1524 case TV_WAND:
1525 {
1526 /* Assume okay */
1527 break;
1528 }
1529
1530 /* Rods */
1531 case TV_ROD:
1532 {
1533 /* Assume okay */
1534 break;
1535 }
1536
1537 /* Wearables */
1538 case TV_BOW:
1539 case TV_DIGGING:
1540 case TV_HAFTED:
1541 case TV_POLEARM:
1542 case TV_SWORD:
1543 case TV_BOOTS:
1544 case TV_GLOVES:
1545 case TV_HELM:
1546 case TV_CROWN:
1547 case TV_SHIELD:
1548 case TV_CLOAK:
1549 case TV_SOFT_ARMOR:
1550 case TV_HARD_ARMOR:
1551 case TV_DRAG_ARMOR:
1552 case TV_RING:
1553 case TV_AMULET:
1554 case TV_LIGHT:
1555 case TV_BOLT:
1556 case TV_ARROW:
1557 case TV_SHOT:
1558 {
1559 /* Require identical "bonuses" */
1560 if (obj->to_h != gear_obj->to_h)
1561 continue;
1562 if (obj->to_d != gear_obj->to_d)
1563 continue;
1564 if (obj->to_a != gear_obj->to_a)
1565 continue;
1566
1567 /* Require identical modifiers */
1568 for (i = 0; i < OBJ_MOD_MAX; i++)
1569 if (obj->modifiers[i] != gear_obj->modifiers[i])
1570 continue;
1571
1572 /* Require identical "artifact" names */
1573 if (obj->artifact != gear_obj->artifact)
1574 continue;
1575
1576 /* Require identical "ego-item" names */
1577 if (obj->ego != gear_obj->ego)
1578 continue;
1579
1580 /* Lights must have same amount of fuel */
1581 else if (obj->timeout != gear_obj->timeout &&
1582 tval_is_light(obj))
1583 continue;
1584
1585 /* Require identical "values" */
1586 if (obj->ac != gear_obj->ac)
1587 continue;
1588 if (obj->dd != gear_obj->dd)
1589 continue;
1590 if (obj->ds != gear_obj->ds)
1591 continue;
1592
1593 /* Probably okay */
1594 break;
1595 }
1596
1597 /* Various */
1598 default:
1599 {
1600 /* Probably okay */
1601 break;
1602 }
1603 }
1604
1605
1606 /* Different flags */
1607 if (!of_is_equal(obj->flags, gear_obj->flags))
1608 continue;
1609
1610 /* They match, so add up */
1611 num += gear_obj->number;
1612 }
1613
1614 return num;
1615 }
1616
1617
1618 /**
1619 * Buy the item with the given index from the current store's inventory.
1620 */
do_cmd_buy(struct command * cmd)1621 void do_cmd_buy(struct command *cmd)
1622 {
1623 int amt;
1624
1625 struct object *obj, *bought, *known_obj;
1626
1627 char o_name[80];
1628 int price;
1629
1630 struct store *store = store_at(cave, player->grid);
1631
1632 if (!store) {
1633 msg("You cannot purchase items when not in a store.");
1634 return;
1635 }
1636
1637 /* Get arguments */
1638 /* XXX-AS fill this out, split into cmd-store.c */
1639 if (cmd_get_arg_item(cmd, "item", &obj) != CMD_OK)
1640 return;
1641
1642 if (!pile_contains(store->stock, obj)) {
1643 msg("You cannot buy that item because it's not in the store.");
1644 return;
1645 }
1646
1647 if (cmd_get_arg_number(cmd, "quantity", &amt) != CMD_OK)
1648 return;
1649
1650 /* Get desired object */
1651 bought = object_new();
1652 object_copy_amt(bought, obj, amt);
1653
1654 /* Ensure we have room */
1655 if (bought->number > inven_carry_num(bought, false)) {
1656 msg("You cannot carry that many items.");
1657 object_delete(&bought);
1658 return;
1659 }
1660
1661 /* Describe the object (fully) */
1662 object_desc(o_name, sizeof(o_name), bought, ODESC_PREFIX | ODESC_FULL);
1663
1664 /* Extract the price for the entire stack */
1665 price = price_item(store, bought, false, bought->number);
1666
1667 if (price > player->au) {
1668 msg("You cannot afford that purchase.");
1669 object_delete(&bought);
1670 return;
1671 }
1672
1673 /* Spend the money */
1674 player->au -= price;
1675
1676 /* Update the gear */
1677 player->upkeep->update |= (PU_INVEN);
1678
1679 /* Combine the pack (later) */
1680 player->upkeep->notice |= (PN_COMBINE | PN_IGNORE);
1681
1682 /* Describe the object (fully) again for the message */
1683 object_desc(o_name, sizeof(o_name), bought, ODESC_PREFIX | ODESC_FULL);
1684
1685 /* Message */
1686 if (one_in_(3)) msgt(MSG_STORE5, "%s", ONE_OF(comment_accept));
1687 msg("You bought %s for %d gold.", o_name, price);
1688
1689 /* Erase the inscription */
1690 bought->note = 0;
1691
1692 /* Give it an origin if it doesn't have one */
1693 if (bought->origin == ORIGIN_NONE)
1694 bought->origin = ORIGIN_STORE;
1695
1696 /* Hack - Reduce the number of charges in the original stack */
1697 if (tval_can_have_charges(obj))
1698 obj->pval -= bought->pval;
1699
1700 /* Make a known object */
1701 known_obj = object_new();
1702 object_copy(known_obj, obj->known);
1703 bought->known = known_obj;
1704
1705 /* Learn flavor, any effect and all the runes */
1706 object_flavor_aware(bought);
1707 bought->known->effect = bought->effect;
1708 while (!object_fully_known(bought)) {
1709 object_learn_unknown_rune(player, bought);
1710 player_know_object(player, bought);
1711 }
1712
1713 /* Give it to the player */
1714 inven_carry(player, bought, true, true);
1715
1716 /* Handle stuff */
1717 handle_stuff(player);
1718
1719 /* Remove the bought objects from the store if it's not a staple */
1720 if (!store_is_staple(store, obj->kind)) {
1721 /* Reduce or remove the item */
1722 store_delete(store, obj, amt);
1723
1724 /* Store is empty */
1725 if (store->stock_num == 0) {
1726 int i;
1727
1728 /* Sometimes shuffle the shopkeeper */
1729 if (one_in_(z_info->store_shuffle)) {
1730 /* Shuffle */
1731 msg("The shopkeeper retires.");
1732 store_shuffle(store);
1733 } else
1734 /* Maintain */
1735 msg("The shopkeeper brings out some new stock.");
1736
1737 /* New inventory */
1738 for (i = 0; i < 10; ++i)
1739 store_maint(store);
1740 }
1741 }
1742
1743 event_signal(EVENT_STORECHANGED);
1744 event_signal(EVENT_INVENTORY);
1745 event_signal(EVENT_EQUIPMENT);
1746 }
1747
1748 /**
1749 * Retrieve the item with the given index from the home's inventory.
1750 */
do_cmd_retrieve(struct command * cmd)1751 void do_cmd_retrieve(struct command *cmd)
1752 {
1753 int amt;
1754
1755 struct object *obj, *known_obj, *picked_item;
1756
1757 struct store *store = store_at(cave, player->grid);
1758 if (!store) return;
1759
1760 if (store->sidx != STORE_HOME) {
1761 msg("You are not currently at home.");
1762 return;
1763 }
1764
1765 /* Get arguments */
1766 if (cmd_get_arg_item(cmd, "item", &obj) != CMD_OK)
1767 return;
1768
1769 if (!pile_contains(store->stock, obj)) {
1770 msg("You cannot retrieve that item because it's not in the home.");
1771 return;
1772 }
1773
1774 if (cmd_get_arg_number(cmd, "quantity", &amt) != CMD_OK)
1775 return;
1776
1777 /* Get desired object */
1778 picked_item = object_new();
1779 object_copy_amt(picked_item, obj, amt);
1780
1781 /* Ensure we have room */
1782 if (picked_item->number > inven_carry_num(picked_item, false)) {
1783 msg("You cannot carry that many items.");
1784 object_delete(&picked_item);
1785 return;
1786 }
1787
1788 /* Distribute charges of wands, staves, or rods */
1789 distribute_charges(obj, picked_item, amt);
1790
1791 /* Make a known object */
1792 known_obj = object_new();
1793 object_copy(known_obj, obj->known);
1794 picked_item->known = known_obj;
1795
1796 /* Give it to the player */
1797 inven_carry(player, picked_item, true, true);
1798
1799 /* Handle stuff */
1800 handle_stuff(player);
1801
1802 /* Reduce or remove the item */
1803 store_delete(store, obj, amt);
1804
1805 event_signal(EVENT_STORECHANGED);
1806 event_signal(EVENT_INVENTORY);
1807 event_signal(EVENT_EQUIPMENT);
1808 }
1809
1810
1811 /**
1812 * Determine if the current store will purchase the given object
1813 */
store_will_buy_tester(const struct object * obj)1814 bool store_will_buy_tester(const struct object *obj)
1815 {
1816 struct store *store = store_at(cave, player->grid);
1817 if (!store) return false;
1818
1819 return store_will_buy(store, obj);
1820 }
1821
1822 /**
1823 * Sell an item to the current store.
1824 */
do_cmd_sell(struct command * cmd)1825 void do_cmd_sell(struct command *cmd)
1826 {
1827 int amt;
1828 struct object dummy_item;
1829 struct store *store = store_at(cave, player->grid);
1830 int price, dummy, value;
1831 char o_name[120];
1832 char label;
1833
1834 struct object *obj, *sold_item;
1835 bool none_left = false;
1836
1837 /* Get arguments */
1838 /* XXX-AS fill this out, split into cmd-store.c */
1839 if (cmd_get_arg_item(cmd, "item", &obj) != CMD_OK)
1840 return;
1841
1842 if (cmd_get_quantity(cmd, "quantity", &amt, obj->number) != CMD_OK)
1843 return;
1844
1845 /* Cannot remove stickied objects */
1846 if (object_is_equipped(player->body, obj) && !obj_can_takeoff(obj)) {
1847 msg("Hmmm, it seems to be stuck.");
1848 return;
1849 }
1850
1851 /* Check we are somewhere we can sell the items. */
1852 if (!store) {
1853 msg("You cannot sell items when not in a store.");
1854 return;
1855 }
1856
1857 /* Check the store wants the items being sold */
1858 if (!store_will_buy(store, obj)) {
1859 msg("I do not wish to purchase this item.");
1860 return;
1861 }
1862
1863 /* Get a copy of the object representing the number being sold */
1864 object_copy_amt(&dummy_item, obj, amt);
1865
1866 /* Check if the store has space for the items */
1867 if (!store_check_num(store, &dummy_item)) {
1868 object_wipe(&dummy_item);
1869 msg("I have not the room in my store to keep it.");
1870 return;
1871 }
1872
1873 /* Get the label */
1874 label = gear_to_label(obj);
1875
1876 price = price_item(store, &dummy_item, true, amt);
1877
1878 /* Get some money */
1879 player->au += price;
1880
1881 /* Update the auto-history if selling an artifact that was previously
1882 * un-IDed. (Ouch!) */
1883 if (obj->artifact)
1884 history_find_artifact(player, obj->artifact);
1885
1886 /* Update the gear */
1887 player->upkeep->update |= (PU_INVEN);
1888
1889 /* Combine the pack (later) */
1890 player->upkeep->notice |= (PN_COMBINE);
1891
1892 /* Redraw stuff */
1893 player->upkeep->redraw |= (PR_INVEN | PR_EQUIP);
1894
1895 /* Get the "apparent" value */
1896 dummy = object_value(&dummy_item, amt);
1897 /*
1898 * Do not need the dummy any more so release the memory allocated
1899 * within it.
1900 */
1901 object_wipe(&dummy_item);
1902
1903 /* Know flavor of consumables */
1904 object_flavor_aware(obj);
1905 obj->known->effect = obj->effect;
1906 while (!object_fully_known(obj)) {
1907 object_learn_unknown_rune(player, obj);
1908 player_know_object(player, obj);
1909 }
1910
1911 /* Take a proper copy of the now known-about object. */
1912 sold_item = gear_object_for_use(obj, amt, false, &none_left);
1913
1914 /* Get the "actual" value */
1915 value = object_value_real(sold_item, amt);
1916
1917 /* Get the description all over again */
1918 object_desc(o_name, sizeof(o_name), sold_item, ODESC_PREFIX | ODESC_FULL);
1919
1920 /* Describe the result (in message buffer) */
1921 if (OPT(player, birth_no_selling)) {
1922 msg("You had %s (%c).", o_name, label);
1923 } else {
1924 msg("You sold %s (%c) for %d gold.", o_name, label, price);
1925
1926 /* Analyze the prices (and comment verbally) */
1927 purchase_analyze(price, value, dummy);
1928 }
1929
1930 /* Autoinscribe if we still have any */
1931 if (!none_left)
1932 apply_autoinscription(obj);
1933
1934 /* Set ignore flag */
1935 player->upkeep->notice |= PN_IGNORE;
1936
1937 /* Notice if pack items need to be combined or reordered */
1938 notice_stuff(player);
1939
1940 /* Handle stuff */
1941 handle_stuff(player);
1942
1943 /* The store gets that (known) object */
1944 if (! store_carry(store, sold_item)) {
1945 /* The store rejected it; delete. */
1946 if (sold_item->known) {
1947 object_delete(&sold_item->known);
1948 sold_item->known = NULL;
1949 }
1950 object_delete(&sold_item);
1951 }
1952
1953 event_signal(EVENT_STORECHANGED);
1954 event_signal(EVENT_INVENTORY);
1955 event_signal(EVENT_EQUIPMENT);
1956 }
1957
1958 /**
1959 * Stash an item in the home.
1960 */
do_cmd_stash(struct command * cmd)1961 void do_cmd_stash(struct command *cmd)
1962 {
1963 int amt;
1964 struct object dummy;
1965 struct store *store = store_at(cave, player->grid);
1966 char o_name[120];
1967 char label;
1968
1969 struct object *obj, *dropped;
1970 bool none_left = false;
1971 bool no_room;
1972
1973 if (cmd_get_arg_item(cmd, "item", &obj))
1974 return;
1975
1976 if (cmd_get_quantity(cmd, "quantity", &amt, obj->number) != CMD_OK)
1977 return;
1978
1979 /* Check we are somewhere we can stash items. */
1980 if (store->sidx != STORE_HOME) {
1981 msg("You are not in your home.");
1982 return;
1983 }
1984
1985 /* Cannot remove stickied objects */
1986 if (object_is_equipped(player->body, obj) && !obj_can_takeoff(obj)) {
1987 msg("Hmmm, it seems to be stuck.");
1988 return;
1989 }
1990
1991 /* Get a copy of the object representing the number being sold */
1992 object_copy_amt(&dummy, obj, amt);
1993
1994 no_room = !store_check_num(store, &dummy);
1995 /*
1996 * Do not need the dummy any more so release the memory allocated
1997 * within it.
1998 */
1999 object_wipe(&dummy);
2000 if (no_room) {
2001 msg("Your home is full.");
2002 return;
2003 }
2004
2005 /* Get where the object is now */
2006 label = gear_to_label(obj);
2007
2008 /* Now get the real item */
2009 dropped = gear_object_for_use(obj, amt, false, &none_left);
2010
2011 /* Describe */
2012 object_desc(o_name, sizeof(o_name), dropped, ODESC_PREFIX | ODESC_FULL);
2013
2014 /* Message */
2015 msg("You drop %s (%c).", o_name, label);
2016
2017 /* Handle stuff */
2018 handle_stuff(player);
2019
2020 /* Let the home carry it */
2021 home_carry(dropped);
2022
2023 event_signal(EVENT_STORECHANGED);
2024 event_signal(EVENT_INVENTORY);
2025 event_signal(EVENT_EQUIPMENT);
2026 }
2027