OSDN Git Service

e55bd227af34659e094e0cc5a341f4dc95ff0db8
[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  * @return なし
86  */
87 void store_sell(player_type *owner_ptr)
88 {
89     concptr q; //!< @note プロンプトメッセージ
90     concptr s_none; //!< @note 売る/置くものがない場合のメッセージ
91     concptr s_full; //!< @note もう置けない場合のメッセージ
92     switch (cur_store_num) {
93     case STORE_HOME:
94         q = _("どのアイテムを置きますか? ", "Drop which item? ");
95         s_none = _("置けるアイテムを持っていません。", "You don't have any items to drop.");
96         s_full = _("我が家にはもう置く場所がない。", "Your home is full.");
97         break;
98     case STORE_MUSEUM:
99         q = _("どのアイテムを寄贈しますか? ", "Give which item? ");
100         s_none = _("寄贈できるアイテムを持っていません。", "You don't have any items to give.");
101         s_full = _("博物館はもう満杯だ。", "The Museum is full.");
102         break;
103     default:
104         q = _("どのアイテムを売りますか? ", "Sell which item? ");
105         s_none = _("欲しい物がないですねえ。", "You have nothing that I want.");
106         s_full = _("すいませんが、店にはもう置く場所がありません。", "I have not the room in my store to keep it.");
107         break;
108     }
109
110     item_tester_hook = store_will_buy;
111
112     OBJECT_IDX item;
113     object_type *o_ptr;
114     o_ptr = choose_object(owner_ptr, &item, q, s_none, USE_EQUIP | USE_INVEN | USE_FLOOR | IGNORE_BOTHHAND_SLOT, TV_NONE);
115     if (!o_ptr)
116         return;
117
118     if ((item >= INVEN_MAIN_HAND) && object_is_cursed(o_ptr)) {
119         msg_print(_("ふーむ、どうやらそれは呪われているようだね。", "Hmmm, it seems to be cursed."));
120         return;
121     }
122
123     int amt = 1;
124     if (o_ptr->number > 1) {
125         amt = get_quantity(NULL, o_ptr->number);
126         if (amt <= 0)
127             return;
128     }
129
130     object_type forge;
131     object_type *q_ptr = &forge;
132     object_copy(q_ptr, o_ptr);
133     q_ptr->number = amt;
134
135     if ((o_ptr->tval == TV_ROD) || (o_ptr->tval == TV_WAND))
136         q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
137
138     GAME_TEXT o_name[MAX_NLEN];
139     describe_flavor(owner_ptr, o_name, q_ptr, 0);
140     if ((cur_store_num != STORE_HOME) && (cur_store_num != STORE_MUSEUM)) {
141         q_ptr->inscription = 0;
142         q_ptr->feeling = FEEL_NONE;
143     }
144
145     if (!store_check_num(q_ptr)) {
146         msg_print(s_full);
147         return;
148     }
149
150     bool placed = false;
151     if ((cur_store_num != STORE_HOME) && (cur_store_num != STORE_MUSEUM)) {
152         msg_format(_("%s(%c)を売却する。", "Selling %s (%c)."), o_name, index_to_label(item));
153         msg_print(NULL);
154
155         auto res = prompt_to_sell(owner_ptr, q_ptr);
156         placed = res.has_value();
157         if (placed) {
158             PRICE price = res.value();
159             store_owner_says_comment(owner_ptr);
160
161             sound(SOUND_SELL);
162             if (cur_store_num == STORE_BLACK)
163                 chg_virtue(owner_ptr, V_JUSTICE, -1);
164
165             if ((o_ptr->tval == TV_BOTTLE) && (cur_store_num != STORE_HOME))
166                 chg_virtue(owner_ptr, V_NATURE, 1);
167
168             owner_ptr->au += price;
169             store_prt_gold(owner_ptr);
170             PRICE dummy = object_value(owner_ptr, q_ptr) * q_ptr->number;
171
172             identify_item(owner_ptr, o_ptr);
173             q_ptr = &forge;
174             object_copy(q_ptr, o_ptr);
175             q_ptr->number = amt;
176             q_ptr->ident |= IDENT_STORE;
177
178             if ((o_ptr->tval == TV_ROD) || (o_ptr->tval == TV_WAND))
179                 q_ptr->pval = o_ptr->pval * amt / o_ptr->number;
180
181             PRICE value = object_value(owner_ptr, q_ptr) * q_ptr->number;
182             describe_flavor(owner_ptr, o_name, q_ptr, 0);
183             msg_format(_("%sを $%ldで売却しました。", "You sold %s for %ld gold."), o_name, static_cast<long>(price));
184
185             if (record_sell)
186                 exe_write_diary(owner_ptr, DIARY_SELL, 0, o_name);
187
188             if (!((o_ptr->tval == TV_FIGURINE) && (value > 0)))
189                 purchase_analyze(owner_ptr, price, value, dummy);
190
191             distribute_charges(o_ptr, q_ptr, amt);
192             q_ptr->timeout = 0;
193             inven_item_increase(owner_ptr, item, -amt);
194             inven_item_describe(owner_ptr, item);
195             if (o_ptr->number > 0)
196                 autopick_alter_item(owner_ptr, item, FALSE);
197
198             inven_item_optimize(owner_ptr, item);
199             int item_pos = store_carry(owner_ptr, q_ptr);
200             if (item_pos >= 0) {
201                 store_top = (item_pos / store_bottom) * store_bottom;
202                 display_store_inventory(owner_ptr);
203             }
204         }
205     } else if (cur_store_num == STORE_MUSEUM) {
206         char o2_name[MAX_NLEN];
207         describe_flavor(owner_ptr, o2_name, q_ptr, OD_NAME_ONLY);
208
209         if (-1 == store_check_num(q_ptr))
210             msg_print(_("それと同じ品物は既に博物館にあるようです。", "The Museum already has one of those items."));
211         else
212             msg_print(_("博物館に寄贈したものは取り出すことができません!!", "You cannot take back items which have been donated to the Museum!!"));
213
214         if (!get_check(format(_("本当に%sを寄贈しますか?", "Really give %s to the Museum? "), o2_name)))
215             return;
216
217         identify_item(owner_ptr, q_ptr);
218         q_ptr->ident |= IDENT_FULL_KNOWN;
219
220         distribute_charges(o_ptr, q_ptr, amt);
221         msg_format(_("%sを置いた。(%c)", "You drop %s (%c)."), o_name, index_to_label(item));
222         placed = true;
223
224         vary_item(owner_ptr, item, -amt);
225
226         int item_pos = home_carry(owner_ptr, q_ptr);
227         if (item_pos >= 0) {
228             store_top = (item_pos / store_bottom) * store_bottom;
229             display_store_inventory(owner_ptr);
230         }
231     } else {
232         distribute_charges(o_ptr, q_ptr, amt);
233         msg_format(_("%sを置いた。(%c)", "You drop %s (%c)."), o_name, index_to_label(item));
234         placed = true;
235         vary_item(owner_ptr, item, -amt);
236         int item_pos = home_carry(owner_ptr, q_ptr);
237         if (item_pos >= 0) {
238             store_top = (item_pos / store_bottom) * store_bottom;
239             display_store_inventory(owner_ptr);
240         }
241     }
242
243     set_bits(owner_ptr->update, PU_BONUS);
244     set_bits(owner_ptr->window_flags, PW_PLAYER);
245     handle_stuff(owner_ptr);
246
247     if (placed && (item >= INVEN_MAIN_HAND)) {
248         calc_android_exp(owner_ptr);
249         verify_equip_slot(owner_ptr, item);
250     }
251 }