OSDN Git Service

[Refactor] format 関数の戻り値を std::string にする
[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/player-update-types.h"
7 #include "core/stuff-handler.h"
8 #include "core/window-redrawer.h"
9 #include "flavor/flavor-describer.h"
10 #include "flavor/object-flavor-types.h"
11 #include "floor/floor-object.h"
12 #include "game-option/birth-options.h"
13 #include "game-option/play-record-options.h"
14 #include "inventory/inventory-object.h"
15 #include "inventory/inventory-slot-types.h"
16 #include "io/write-diary.h"
17 #include "main/sound-definitions-table.h"
18 #include "main/sound-of-music.h"
19 #include "object-enchant/item-feeling.h"
20 #include "object-enchant/special-object-flags.h"
21 #include "object/item-tester-hooker.h"
22 #include "object/item-use-flags.h"
23 #include "object/object-info.h"
24 #include "object/object-stack.h"
25 #include "object/object-value.h"
26 #include "racial/racial-android.h"
27 #include "spell-kind/spells-perception.h"
28 #include "store/home.h"
29 #include "store/pricing.h"
30 #include "store/say-comments.h"
31 #include "store/service-checker.h"
32 #include "store/store-owners.h"
33 #include "store/store-util.h"
34 #include "store/store.h"
35 #include "system/item-entity.h"
36 #include "system/player-type-definition.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     GAME_TEXT o_name[MAX_NLEN];
123     describe_flavor(player_ptr, o_name, q_ptr, 0);
124     if ((store_num != StoreSaleType::HOME) && (store_num != StoreSaleType::MUSEUM)) {
125         q_ptr->inscription = 0;
126         q_ptr->feeling = FEEL_NONE;
127     }
128
129     if (!store_check_num(q_ptr, store_num)) {
130         msg_print(s_full);
131         return;
132     }
133
134     bool placed = false;
135     if ((store_num != StoreSaleType::HOME) && (store_num != StoreSaleType::MUSEUM)) {
136         msg_format(_("%s(%c)を売却する。", "Selling %s (%c)."), o_name, index_to_label(item));
137         msg_print(nullptr);
138
139         auto res = prompt_to_sell(player_ptr, q_ptr, store_num);
140         placed = res.has_value();
141         if (placed) {
142             const auto price = res.value();
143             store_owner_says_comment(player_ptr, store_num);
144
145             sound(SOUND_SELL);
146             if (store_num == StoreSaleType::BLACK) {
147                 chg_virtue(player_ptr, V_JUSTICE, -1);
148             }
149
150             const auto tval = o_ptr->bi_key.tval();
151             if ((tval == ItemKindType::BOTTLE) && (store_num != StoreSaleType::HOME)) {
152                 chg_virtue(player_ptr, V_NATURE, 1);
153             }
154
155             player_ptr->au += price;
156             store_prt_gold(player_ptr);
157             const auto dummy = q_ptr->get_price() * q_ptr->number;
158
159             identify_item(player_ptr, o_ptr);
160             q_ptr = &forge;
161             q_ptr->copy_from(o_ptr);
162             q_ptr->number = amt;
163             q_ptr->ident |= IDENT_STORE;
164
165             if (o_ptr->is_wand_rod()) {
166                 q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
167             }
168
169             const auto value = q_ptr->get_price() * q_ptr->number;
170             describe_flavor(player_ptr, o_name, q_ptr, 0);
171             msg_format(_("%sを $%ldで売却しました。", "You sold %s for %ld gold."), o_name, static_cast<long>(price));
172
173             if (record_sell) {
174                 exe_write_diary(player_ptr, DIARY_SELL, 0, o_name);
175             }
176
177             if (!((tval == ItemKindType::FIGURINE) && (value > 0))) {
178                 purchase_analyze(player_ptr, price, value, dummy);
179             }
180
181             distribute_charges(o_ptr, q_ptr, amt);
182             q_ptr->timeout = 0;
183             inven_item_increase(player_ptr, item, -amt);
184             inven_item_describe(player_ptr, item);
185             if (o_ptr->number > 0) {
186                 autopick_alter_item(player_ptr, item, false);
187             }
188
189             inven_item_optimize(player_ptr, item);
190             int item_pos = store_carry(q_ptr);
191             if (item_pos >= 0) {
192                 store_top = (item_pos / store_bottom) * store_bottom;
193                 display_store_inventory(player_ptr, store_num);
194             }
195         }
196     } else if (store_num == StoreSaleType::MUSEUM) {
197         char o2_name[MAX_NLEN];
198         describe_flavor(player_ptr, o2_name, q_ptr, OD_NAME_ONLY);
199
200         if (-1 == store_check_num(q_ptr, store_num)) {
201             msg_print(_("それと同じ品物は既に博物館にあるようです。", "The Museum already has one of those items."));
202         } else {
203             msg_print(_("博物館に寄贈したものは取り出すことができません!!", "You cannot take back items which have been donated to the Museum!!"));
204         }
205
206         if (!get_check(format(_("本当に%sを寄贈しますか?", "Really give %s to the Museum? "), o2_name))) {
207             return;
208         }
209
210         identify_item(player_ptr, q_ptr);
211         q_ptr->ident |= IDENT_FULL_KNOWN;
212
213         distribute_charges(o_ptr, q_ptr, amt);
214         msg_format(_("%sを置いた。(%c)", "You drop %s (%c)."), o_name, index_to_label(item));
215         placed = true;
216
217         vary_item(player_ptr, item, -amt);
218
219         int item_pos = home_carry(player_ptr, q_ptr, store_num);
220         if (item_pos >= 0) {
221             store_top = (item_pos / store_bottom) * store_bottom;
222             display_store_inventory(player_ptr, store_num);
223         }
224     } else {
225         distribute_charges(o_ptr, q_ptr, amt);
226         msg_format(_("%sを置いた。(%c)", "You drop %s (%c)."), o_name, index_to_label(item));
227         placed = true;
228         vary_item(player_ptr, item, -amt);
229         int item_pos = home_carry(player_ptr, q_ptr, store_num);
230         if (item_pos >= 0) {
231             store_top = (item_pos / store_bottom) * store_bottom;
232             display_store_inventory(player_ptr, store_num);
233         }
234     }
235
236     set_bits(player_ptr->update, PU_BONUS);
237     set_bits(player_ptr->window_flags, PW_PLAYER);
238     handle_stuff(player_ptr);
239
240     if (placed && (item >= INVEN_MAIN_HAND)) {
241         calc_android_exp(player_ptr);
242         verify_equip_slot(player_ptr, item);
243     }
244 }