OSDN Git Service

[Refactor] #933 Renamed accessory-enchanter-base.h to enchanter-base.h
[hengbandforosx/hengbandosx.git] / src / store / sell-order.cpp
1 #include "store/sell-order.h"
2 #include "action/weapon-shield.h"
3 #include "autopick/autopick.h"
4 #include "core/asking-player.h"
5 #include "core/player-update-types.h"
6 #include "core/stuff-handler.h"
7 #include "core/window-redrawer.h"
8 #include "flavor/flavor-describer.h"
9 #include "flavor/object-flavor-types.h"
10 #include "floor/floor-object.h"
11 #include "game-option/birth-options.h"
12 #include "game-option/play-record-options.h"
13 #include "inventory/inventory-object.h"
14 #include "inventory/inventory-slot-types.h"
15 #include "io/write-diary.h"
16 #include "main/sound-definitions-table.h"
17 #include "main/sound-of-music.h"
18 #include "object-enchant/item-feeling.h"
19 #include "object-enchant/special-object-flags.h"
20 #include "object-hook/hook-checker.h"
21 #include "object/item-tester-hooker.h"
22 #include "object/item-use-flags.h"
23 #include "object/object-generator.h"
24 #include "object/object-info.h"
25 #include "object/object-stack.h"
26 #include "object/object-value.h"
27 #include "player-info/avatar.h"
28 #include "racial/racial-android.h"
29 #include "spell-kind/spells-perception.h"
30 #include "store/home.h"
31 #include "store/pricing.h"
32 #include "store/say-comments.h"
33 #include "store/service-checker.h"
34 #include "store/store-owners.h"
35 #include "store/store-util.h"
36 #include "store/store.h"
37 #include "system/object-type-definition.h"
38 #include "system/player-type-definition.h"
39 #include "term/screen-processor.h"
40 #include "util/bit-flags-calculator.h"
41 #include "view/display-messages.h"
42 #include "view/display-store.h"
43 #include "view/object-describer.h"
44 #include "world/world.h"
45 #include <optional>
46
47 /*!
48  * @brief プレイヤーが売却する時の確認プロンプト / Prompt to sell for the price
49  * @param player_ptr プレーヤーへの参照ポインタ
50  * @param o_ptr オブジェクトの構造体参照ポインタ
51  * @return 売るなら(true,売値)、売らないなら(false,0)のタプル
52  */
53 static std::optional<PRICE> prompt_to_sell(player_type *player_ptr, object_type *o_ptr)
54 {
55     auto price_ask = price_item(player_ptr, o_ptr, ot_ptr->inflate, TRUE);
56     auto is_low_price = price_ask < LOW_PRICE_THRESHOLD;
57
58     if (!is_low_price)
59         price_ask -= price_ask / 10;
60
61     auto max_price = (PRICE)ot_ptr->max_cost;
62
63     if (price_ask > max_price) {
64         msg_print(_("即座にこの金額にまとまった。", "You instantly agree upon the price."));
65         msg_print(NULL);
66         price_ask = max_price;
67     } else {
68         msg_print(_("すんなりとこの金額にまとまった。", "You quickly agree upon the price."));
69         msg_print(NULL);
70     }
71
72     price_ask *= o_ptr->number;
73     concptr s = format(_("売値 $%ld で売りますか?", "Do you sell for $%ld? "), static_cast<long>(price_ask));
74     if (get_check_strict(player_ptr, s, CHECK_DEFAULT_Y)) {
75         return price_ask;
76     }
77
78     return std::nullopt;
79 }
80
81 /*!
82  * @brief 店からの売却処理のメインルーチン /
83  * Sell an item to the store (or home)
84  * @param owner_ptr プレーヤーへの参照ポインタ
85  */
86 void store_sell(player_type *owner_ptr)
87 {
88     concptr q; //!< @note プロンプトメッセージ
89     concptr s_none; //!< @note 売る/置くものがない場合のメッセージ
90     concptr s_full; //!< @note もう置けない場合のメッセージ
91     switch (cur_store_num) {
92     case STORE_HOME:
93         q = _("どのアイテムを置きますか? ", "Drop which item? ");
94         s_none = _("置けるアイテムを持っていません。", "You don't have any items to drop.");
95         s_full = _("我が家にはもう置く場所がない。", "Your home is full.");
96         break;
97     case STORE_MUSEUM:
98         q = _("どのアイテムを寄贈しますか? ", "Give which item? ");
99         s_none = _("寄贈できるアイテムを持っていません。", "You don't have any items to give.");
100         s_full = _("博物館はもう満杯だ。", "The Museum is full.");
101         break;
102     default:
103         q = _("どのアイテムを売りますか? ", "Sell which item? ");
104         s_none = _("欲しい物がないですねえ。", "You have nothing that I want.");
105         s_full = _("すいませんが、店にはもう置く場所がありません。", "I have not the room in my store to keep it.");
106         break;
107     }
108
109     item_tester_hook = store_will_buy;
110
111     OBJECT_IDX item;
112     object_type *o_ptr;
113     o_ptr = choose_object(owner_ptr, &item, q, s_none, USE_EQUIP | USE_INVEN | USE_FLOOR | IGNORE_BOTHHAND_SLOT, TV_NONE);
114     if (!o_ptr)
115         return;
116
117     if ((item >= INVEN_MAIN_HAND) && object_is_cursed(o_ptr)) {
118         msg_print(_("ふーむ、どうやらそれは呪われているようだね。", "Hmmm, it seems to be cursed."));
119         return;
120     }
121
122     int amt = 1;
123     if (o_ptr->number > 1) {
124         amt = get_quantity(NULL, o_ptr->number);
125         if (amt <= 0)
126             return;
127     }
128
129     object_type forge;
130     object_type *q_ptr = &forge;
131     object_copy(q_ptr, o_ptr);
132     q_ptr->number = amt;
133
134     if ((o_ptr->tval == TV_ROD) || (o_ptr->tval == TV_WAND))
135         q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
136
137     GAME_TEXT o_name[MAX_NLEN];
138     describe_flavor(owner_ptr, o_name, q_ptr, 0);
139     if ((cur_store_num != STORE_HOME) && (cur_store_num != STORE_MUSEUM)) {
140         q_ptr->inscription = 0;
141         q_ptr->feeling = FEEL_NONE;
142     }
143
144     if (!store_check_num(q_ptr)) {
145         msg_print(s_full);
146         return;
147     }
148
149     bool placed = false;
150     if ((cur_store_num != STORE_HOME) && (cur_store_num != STORE_MUSEUM)) {
151         msg_format(_("%s(%c)を売却する。", "Selling %s (%c)."), o_name, index_to_label(item));
152         msg_print(NULL);
153
154         auto res = prompt_to_sell(owner_ptr, q_ptr);
155         placed = res.has_value();
156         if (placed) {
157             PRICE price = res.value();
158             store_owner_says_comment(owner_ptr);
159
160             sound(SOUND_SELL);
161             if (cur_store_num == STORE_BLACK)
162                 chg_virtue(owner_ptr, V_JUSTICE, -1);
163
164             if ((o_ptr->tval == TV_BOTTLE) && (cur_store_num != STORE_HOME))
165                 chg_virtue(owner_ptr, V_NATURE, 1);
166
167             owner_ptr->au += price;
168             store_prt_gold(owner_ptr);
169             PRICE dummy = object_value(owner_ptr, q_ptr) * q_ptr->number;
170
171             identify_item(owner_ptr, o_ptr);
172             q_ptr = &forge;
173             object_copy(q_ptr, o_ptr);
174             q_ptr->number = amt;
175             q_ptr->ident |= IDENT_STORE;
176
177             if ((o_ptr->tval == TV_ROD) || (o_ptr->tval == TV_WAND))
178                 q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
179
180             PRICE value = object_value(owner_ptr, q_ptr) * q_ptr->number;
181             describe_flavor(owner_ptr, o_name, q_ptr, 0);
182             msg_format(_("%sを $%ldで売却しました。", "You sold %s for %ld gold."), o_name, static_cast<long>(price));
183
184             if (record_sell)
185                 exe_write_diary(owner_ptr, DIARY_SELL, 0, o_name);
186
187             if (!((o_ptr->tval == TV_FIGURINE) && (value > 0)))
188                 purchase_analyze(owner_ptr, price, value, dummy);
189
190             distribute_charges(o_ptr, q_ptr, amt);
191             q_ptr->timeout = 0;
192             inven_item_increase(owner_ptr, item, -amt);
193             inven_item_describe(owner_ptr, item);
194             if (o_ptr->number > 0)
195                 autopick_alter_item(owner_ptr, item, FALSE);
196
197             inven_item_optimize(owner_ptr, item);
198             int item_pos = store_carry(owner_ptr, q_ptr);
199             if (item_pos >= 0) {
200                 store_top = (item_pos / store_bottom) * store_bottom;
201                 display_store_inventory(owner_ptr);
202             }
203         }
204     } else if (cur_store_num == STORE_MUSEUM) {
205         char o2_name[MAX_NLEN];
206         describe_flavor(owner_ptr, o2_name, q_ptr, OD_NAME_ONLY);
207
208         if (-1 == store_check_num(q_ptr))
209             msg_print(_("それと同じ品物は既に博物館にあるようです。", "The Museum already has one of those items."));
210         else
211             msg_print(_("博物館に寄贈したものは取り出すことができません!!", "You cannot take back items which have been donated to the Museum!!"));
212
213         if (!get_check(format(_("本当に%sを寄贈しますか?", "Really give %s to the Museum? "), o2_name)))
214             return;
215
216         identify_item(owner_ptr, q_ptr);
217         q_ptr->ident |= IDENT_FULL_KNOWN;
218
219         distribute_charges(o_ptr, q_ptr, amt);
220         msg_format(_("%sを置いた。(%c)", "You drop %s (%c)."), o_name, index_to_label(item));
221         placed = true;
222
223         vary_item(owner_ptr, item, -amt);
224
225         int item_pos = home_carry(owner_ptr, q_ptr);
226         if (item_pos >= 0) {
227             store_top = (item_pos / store_bottom) * store_bottom;
228             display_store_inventory(owner_ptr);
229         }
230     } else {
231         distribute_charges(o_ptr, q_ptr, amt);
232         msg_format(_("%sを置いた。(%c)", "You drop %s (%c)."), o_name, index_to_label(item));
233         placed = true;
234         vary_item(owner_ptr, item, -amt);
235         int item_pos = home_carry(owner_ptr, q_ptr);
236         if (item_pos >= 0) {
237             store_top = (item_pos / store_bottom) * store_bottom;
238             display_store_inventory(owner_ptr);
239         }
240     }
241
242     set_bits(owner_ptr->update, PU_BONUS);
243     set_bits(owner_ptr->window_flags, PW_PLAYER);
244     handle_stuff(owner_ptr);
245
246     if (placed && (item >= INVEN_MAIN_HAND)) {
247         calc_android_exp(owner_ptr);
248         verify_equip_slot(owner_ptr, item);
249     }
250 }