OSDN Git Service

Merge branch 'master' of https://github.com/hengband/hengband
[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 "avatar/avatar.h"
5 #include "core/asking-player.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/item-tester-hooker.h"
21 #include "object/item-use-flags.h"
22 #include "object/object-info.h"
23 #include "object/object-stack.h"
24 #include "object/object-value.h"
25 #include "racial/racial-android.h"
26 #include "spell-kind/spells-perception.h"
27 #include "store/home.h"
28 #include "store/pricing.h"
29 #include "store/say-comments.h"
30 #include "store/service-checker.h"
31 #include "store/store-owners.h"
32 #include "store/store-util.h"
33 #include "store/store.h"
34 #include "system/item-entity.h"
35 #include "system/player-type-definition.h"
36 #include "system/redrawing-flags-updater.h"
37 #include "term/screen-processor.h"
38 #include "util/bit-flags-calculator.h"
39 #include "view/display-messages.h"
40 #include "view/display-store.h"
41 #include "view/object-describer.h"
42 #include "world/world.h"
43 #include <optional>
44
45 /*!
46  * @brief プレイヤーが売却する時の確認プロンプト / Prompt to sell for the price
47  * @param player_ptr プレイヤーへの参照ポインタ
48  * @param o_ptr オブジェクトの構造体参照ポインタ
49  * @return 売るなら(true,売値)、売らないなら(false,0)のタプル
50  */
51 static std::optional<PRICE> prompt_to_sell(PlayerType *player_ptr, ItemEntity *o_ptr, StoreSaleType store_num)
52 {
53     auto price_ask = price_item(player_ptr, o_ptr, ot_ptr->inflate, true, store_num);
54
55     price_ask = std::min(price_ask, ot_ptr->max_cost);
56     price_ask *= o_ptr->number;
57     const auto s = format(_("売値 $%ld で売りますか?", "Do you sell for $%ld? "), static_cast<long>(price_ask));
58     if (get_check_strict(player_ptr, s, CHECK_DEFAULT_Y)) {
59         return price_ask;
60     }
61
62     return std::nullopt;
63 }
64
65 /*!
66  * @brief 店からの売却処理のメインルーチン /
67  * Sell an item to the store (or home)
68  * @param player_ptr プレイヤーへの参照ポインタ
69  */
70 void store_sell(PlayerType *player_ptr, StoreSaleType store_num)
71 {
72     concptr q; //!< @note プロンプトメッセージ
73     concptr s_none; //!< @note 売る/置くものがない場合のメッセージ
74     concptr s_full; //!< @note もう置けない場合のメッセージ
75     switch (store_num) {
76     case StoreSaleType::HOME:
77         q = _("どのアイテムを置きますか? ", "Drop which item? ");
78         s_none = _("置けるアイテムを持っていません。", "You don't have any items to drop.");
79         s_full = _("我が家にはもう置く場所がない。", "Your home is full.");
80         break;
81     case StoreSaleType::MUSEUM:
82         q = _("どのアイテムを寄贈しますか? ", "Give which item? ");
83         s_none = _("寄贈できるアイテムを持っていません。", "You don't have any items to give.");
84         s_full = _("博物館はもう満杯だ。", "The Museum is full.");
85         break;
86     default:
87         q = _("どのアイテムを売りますか? ", "Sell which item? ");
88         s_none = _("欲しい物がないですねえ。", "You have nothing that I want.");
89         s_full = _("すいませんが、店にはもう置く場所がありません。", "I have not the room in my store to keep it.");
90         break;
91     }
92
93     OBJECT_IDX item;
94     const auto options = USE_EQUIP | USE_INVEN | USE_FLOOR | IGNORE_BOTHHAND_SLOT;
95     auto *o_ptr = choose_object(player_ptr, &item, q, s_none, options, FuncItemTester(store_will_buy, player_ptr, store_num));
96     if (o_ptr == nullptr) {
97         return;
98     }
99
100     if ((item >= INVEN_MAIN_HAND) && o_ptr->is_cursed()) {
101         msg_print(_("ふーむ、どうやらそれは呪われているようだね。", "Hmmm, it seems to be cursed."));
102         return;
103     }
104
105     int amt = 1;
106     if (o_ptr->number > 1) {
107         amt = get_quantity(std::nullopt, o_ptr->number);
108         if (amt <= 0) {
109             return;
110         }
111     }
112
113     ItemEntity forge;
114     auto *q_ptr = &forge;
115     q_ptr->copy_from(o_ptr);
116     q_ptr->number = amt;
117
118     if (o_ptr->is_wand_rod()) {
119         q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
120     }
121
122     if ((store_num != StoreSaleType::HOME) && (store_num != StoreSaleType::MUSEUM)) {
123         q_ptr->inscription.reset();
124         q_ptr->feeling = FEEL_NONE;
125     }
126
127     if (!store_check_num(q_ptr, store_num)) {
128         msg_print(s_full);
129         return;
130     }
131
132     bool placed = false;
133     if ((store_num != StoreSaleType::HOME) && (store_num != StoreSaleType::MUSEUM)) {
134         const auto item_name = describe_flavor(player_ptr, q_ptr, 0);
135         msg_format(_("%s(%c)を売却する。", "Selling %s (%c)."), item_name.data(), index_to_label(item));
136         msg_print(nullptr);
137
138         auto res = prompt_to_sell(player_ptr, q_ptr, store_num);
139         placed = res.has_value();
140         if (placed) {
141             const auto price = res.value();
142             store_owner_says_comment(player_ptr, store_num);
143
144             sound(SOUND_SELL);
145             if (store_num == StoreSaleType::BLACK) {
146                 chg_virtue(player_ptr, Virtue::JUSTICE, -1);
147             }
148
149             const auto tval = o_ptr->bi_key.tval();
150             if ((tval == ItemKindType::BOTTLE) && (store_num != StoreSaleType::HOME)) {
151                 chg_virtue(player_ptr, Virtue::NATURE, 1);
152             }
153
154             player_ptr->au += price;
155             store_prt_gold(player_ptr);
156             const auto dummy = q_ptr->get_price() * q_ptr->number;
157
158             identify_item(player_ptr, o_ptr);
159             q_ptr = &forge;
160             q_ptr->copy_from(o_ptr);
161             q_ptr->number = amt;
162             q_ptr->ident |= IDENT_STORE;
163
164             if (o_ptr->is_wand_rod()) {
165                 q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
166             }
167
168             const auto value = q_ptr->get_price() * q_ptr->number;
169             const auto sold_item_name = describe_flavor(player_ptr, q_ptr, 0);
170             msg_format(_("%sを $%dで売却しました。", "You sold %s for %d gold."), sold_item_name.data(), price);
171
172             if (record_sell) {
173                 exe_write_diary(player_ptr, DIARY_SELL, 0, sold_item_name);
174             }
175
176             if (!((tval == ItemKindType::FIGURINE) && (value > 0))) {
177                 purchase_analyze(player_ptr, price, value, dummy);
178             }
179
180             distribute_charges(o_ptr, q_ptr, amt);
181             q_ptr->timeout = 0;
182             inven_item_increase(player_ptr, item, -amt);
183             inven_item_describe(player_ptr, item);
184             if (o_ptr->number > 0) {
185                 autopick_alter_item(player_ptr, item, false);
186             }
187
188             inven_item_optimize(player_ptr, item);
189             int item_pos = store_carry(q_ptr);
190             if (item_pos >= 0) {
191                 store_top = (item_pos / store_bottom) * store_bottom;
192                 display_store_inventory(player_ptr, store_num);
193             }
194         }
195     } else if (store_num == StoreSaleType::MUSEUM) {
196         const auto museum_item_name = describe_flavor(player_ptr, q_ptr, OD_NAME_ONLY);
197         if (-1 == store_check_num(q_ptr, store_num)) {
198             msg_print(_("それと同じ品物は既に博物館にあるようです。", "The Museum already has one of those items."));
199         } else {
200             msg_print(_("博物館に寄贈したものは取り出すことができません!!", "You cannot take back items which have been donated to the Museum!!"));
201         }
202
203         if (!get_check(format(_("本当に%sを寄贈しますか?", "Really give %s to the Museum? "), museum_item_name.data()))) {
204             return;
205         }
206
207         identify_item(player_ptr, q_ptr);
208         q_ptr->ident |= IDENT_FULL_KNOWN;
209
210         distribute_charges(o_ptr, q_ptr, amt);
211         msg_format(_("%sを置いた。(%c)", "You drop %s (%c)."), museum_item_name.data(), index_to_label(item));
212         placed = true;
213
214         vary_item(player_ptr, item, -amt);
215
216         int item_pos = home_carry(player_ptr, q_ptr, store_num);
217         if (item_pos >= 0) {
218             store_top = (item_pos / store_bottom) * store_bottom;
219             display_store_inventory(player_ptr, store_num);
220         }
221     } else {
222         distribute_charges(o_ptr, q_ptr, amt);
223         const auto item_name = describe_flavor(player_ptr, q_ptr, 0);
224         msg_format(_("%sを置いた。(%c)", "You drop %s (%c)."), item_name.data(), index_to_label(item));
225         placed = true;
226         vary_item(player_ptr, item, -amt);
227         int item_pos = home_carry(player_ptr, q_ptr, store_num);
228         if (item_pos >= 0) {
229             store_top = (item_pos / store_bottom) * store_bottom;
230             display_store_inventory(player_ptr, store_num);
231         }
232     }
233
234     auto &rfu = RedrawingFlagsUpdater::get_instance();
235     rfu.set_flag(StatusRedrawingFlag::BONUS);
236     set_bits(player_ptr->window_flags, PW_PLAYER);
237     handle_stuff(player_ptr);
238
239     if (placed && (item >= INVEN_MAIN_HAND)) {
240         calc_android_exp(player_ptr);
241         verify_equip_slot(player_ptr, item);
242     }
243 }