OSDN Git Service

Merge branch 'master' of https://github.com/hengband/hengband
[hengbandforosx/hengbandosx.git] / src / inventory / player-inventory.cpp
1 #include "inventory/player-inventory.h"
2 #include "autopick/autopick.h"
3 #include "core/asking-player.h"
4 #include "core/disturbance.h"
5 #include "core/stuff-handler.h"
6 #include "core/window-redrawer.h"
7 #include "dungeon/quest.h"
8 #include "flavor/flavor-describer.h"
9 #include "flavor/object-flavor-types.h"
10 #include "floor/floor-object.h"
11 #include "floor/object-scanner.h"
12 #include "game-option/auto-destruction-options.h"
13 #include "game-option/birth-options.h"
14 #include "game-option/input-options.h"
15 #include "game-option/play-record-options.h"
16 #include "game-option/text-display-options.h"
17 #include "inventory/inventory-object.h"
18 #include "inventory/inventory-slot-types.h"
19 #include "main/sound-definitions-table.h"
20 #include "main/sound-of-music.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-mark-types.h"
25 #include "player/player-move.h"
26 #include "spell-kind/spells-perception.h"
27 #include "system/floor-type-definition.h"
28 #include "system/grid-type-definition.h"
29 #include "system/item-entity.h"
30 #include "system/player-type-definition.h"
31 #include "system/redrawing-flags-updater.h"
32 #include "target/target-checker.h"
33 #include "term/z-form.h"
34 #include "util/string-processor.h"
35 #include "view/display-messages.h"
36 #include "world/world.h"
37 #ifdef JP
38 #include "artifact/fixed-art-types.h"
39 #include "flavor/flavor-util.h"
40 #endif
41
42 /*!
43  * @brief 規定の処理にできるアイテムがプレイヤーの利用可能範囲内にあるかどうかを返す /
44  * Determine whether get_item() can get some item or not
45  * @return アイテムを拾えるならばTRUEを返す。
46  * @details assuming mode = (USE_EQUIP | USE_INVEN | USE_FLOOR).
47  */
48 bool can_get_item(PlayerType *player_ptr, const ItemTester &item_tester)
49 {
50     for (int j = 0; j < INVEN_TOTAL; j++) {
51         if (item_tester.okay(&player_ptr->inventory_list[j])) {
52             return true;
53         }
54     }
55
56     constexpr auto mode = SCAN_FLOOR_ITEM_TESTER | SCAN_FLOOR_ONLY_MARKED;
57     OBJECT_IDX floor_list[23];
58     ITEM_NUMBER floor_num = scan_floor_items(player_ptr, floor_list, player_ptr->y, player_ptr->x, mode, item_tester);
59     return floor_num != 0;
60 }
61
62 /*!
63  * @brief 床上のアイテムを拾う選択用サブルーチン
64  * @return プレイヤーによりアイテムが選択されたならTRUEを返す。
65  */
66 static bool py_pickup_floor_aux(PlayerType *player_ptr)
67 {
68     short this_o_idx;
69     short i_idx;
70     constexpr auto q = _("どれを拾いますか?", "Get which item? ");
71     constexpr auto s = _("もうザックには床にあるどのアイテムも入らない。", "You no longer have any room for the objects on the floor.");
72     if (choose_object(player_ptr, &i_idx, q, s, (USE_FLOOR), FuncItemTester(check_store_item_to_inventory, player_ptr))) {
73         this_o_idx = 0 - i_idx;
74     } else {
75         return false;
76     }
77
78     describe_pickup_item(player_ptr, this_o_idx);
79     return true;
80 }
81
82 /*!
83  * @brief 床上のアイテムを拾うメイン処理
84  * @param pickup FALSEなら金銭の自動拾いのみを行う/ FALSE then only gold will be picked up
85  * @details
86  * This is called by py_pickup() when easy_floor is TRUE.
87  */
88 void py_pickup_floor(PlayerType *player_ptr, bool pickup)
89 {
90     int floor_num = 0;
91     OBJECT_IDX floor_o_idx = 0;
92     int can_pickup = 0;
93     auto &o_idx_list = player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x].o_idx_list;
94     auto &rfu = RedrawingFlagsUpdater::get_instance();
95     for (auto it = o_idx_list.begin(); it != o_idx_list.end();) {
96         const OBJECT_IDX this_o_idx = *it++;
97         auto *o_ptr = &player_ptr->current_floor_ptr->o_list[this_o_idx];
98         const auto item_name = describe_flavor(player_ptr, o_ptr, 0);
99         disturb(player_ptr, false, false);
100         if (o_ptr->bi_key.tval() == ItemKindType::GOLD) {
101             constexpr auto mes = _(" $%ld の価値がある%sを見つけた。", "You have found %ld gold pieces worth of %s.");
102             msg_format(mes, (long)o_ptr->pval, item_name.data());
103             sound(SOUND_SELL);
104             player_ptr->au += o_ptr->pval;
105             rfu.set_flag(MainWindowRedrawingFlag::GOLD);
106             rfu.set_flag(SubWindowRedrawingFlag::PLAYER);
107             delete_object_idx(player_ptr, this_o_idx);
108             continue;
109         } else if (o_ptr->marked.has(OmType::SUPRESS_MESSAGE)) {
110             o_ptr->marked.reset(OmType::SUPRESS_MESSAGE);
111             continue;
112         }
113
114         if (check_store_item_to_inventory(player_ptr, o_ptr)) {
115             can_pickup++;
116         }
117
118         floor_num++;
119         floor_o_idx = this_o_idx;
120     }
121
122     if (!floor_num) {
123         return;
124     }
125
126     if (!pickup) {
127         if (floor_num == 1) {
128             auto *o_ptr = &player_ptr->current_floor_ptr->o_list[floor_o_idx];
129             const auto item_name = describe_flavor(player_ptr, o_ptr, 0);
130             msg_format(_("%sがある。", "You see %s."), item_name.data());
131         } else {
132             msg_format(_("%d 個のアイテムの山がある。", "You see a pile of %d items."), floor_num);
133         }
134
135         return;
136     }
137
138     if (!can_pickup) {
139         if (floor_num == 1) {
140             auto *o_ptr = &player_ptr->current_floor_ptr->o_list[floor_o_idx];
141             const auto item_name = describe_flavor(player_ptr, o_ptr, 0);
142             msg_format(_("ザックには%sを入れる隙間がない。", "You have no room for %s."), item_name.data());
143         } else {
144             msg_print(_("ザックには床にあるどのアイテムも入らない。", "You have no room for any of the objects on the floor."));
145         }
146
147         return;
148     }
149
150     if (floor_num != 1) {
151         while (can_pickup--) {
152             if (!py_pickup_floor_aux(player_ptr)) {
153                 break;
154             }
155         }
156
157         return;
158     }
159
160     if (!carry_query_flag) {
161         describe_pickup_item(player_ptr, floor_o_idx);
162         return;
163     }
164
165     char out_val[MAX_NLEN + 20];
166     auto *o_ptr = &player_ptr->current_floor_ptr->o_list[floor_o_idx];
167     const auto item_name = describe_flavor(player_ptr, o_ptr, 0);
168     strnfmt(out_val, sizeof(out_val), _("%sを拾いますか? ", "Pick up %s? "), item_name.data());
169     if (!input_check(out_val)) {
170         return;
171     }
172
173     describe_pickup_item(player_ptr, floor_o_idx);
174 }
175
176 /*!
177  * @brief プレイヤーがオブジェクトを拾った際のメッセージ表示処理 /
178  * Helper routine for py_pickup() and py_pickup_floor().
179  * @param player_ptr プレイヤーへの参照ポインタ
180  * @param o_idx 取得したオブジェクトの参照ID
181  * @details
182  * アイテムを拾った際に「2つのケーキを持っている」
183  * "You have two cakes." とアイテムを拾った後の合計のみの表示がオリジナルだが、
184  * 違和感があるという指摘をうけたので、「~を拾った、~を持っている」という表示にかえてある。
185  * そのための配列。
186  * Add the given dungeon object to the character's inventory.\n
187  * Delete the object afterwards.\n
188  */
189 void describe_pickup_item(PlayerType *player_ptr, OBJECT_IDX o_idx)
190 {
191     auto *o_ptr = &player_ptr->current_floor_ptr->o_list[o_idx];
192 #ifdef JP
193     const auto old_item_name = describe_flavor(player_ptr, o_ptr, OD_NAME_ONLY);
194     const auto picked_count_str = describe_count_with_counter_suffix(*o_ptr);
195     const auto picked_count = o_ptr->number;
196 #else
197     (void)o_ptr;
198 #endif
199
200     auto slot = store_item_to_inventory(player_ptr, o_ptr);
201     o_ptr = &player_ptr->inventory_list[slot];
202     delete_object_idx(player_ptr, o_idx);
203     if (player_ptr->ppersonality == PERSONALITY_MUNCHKIN) {
204         bool old_known = identify_item(player_ptr, o_ptr);
205         autopick_alter_item(player_ptr, slot, (bool)(destroy_identify && !old_known));
206         if (o_ptr->marked.has(OmType::AUTODESTROY)) {
207             return;
208         }
209     }
210
211     const auto item_name = describe_flavor(player_ptr, o_ptr, 0);
212 #ifdef JP
213     if (o_ptr->is_specific_artifact(FixedArtifactId::CRIMSON) && (player_ptr->ppersonality == PERSONALITY_COMBAT)) {
214         msg_format("こうして、%sは『クリムゾン』を手に入れた。", player_ptr->name);
215         msg_print("しかし今、『混沌のサーペント』の放ったモンスターが、");
216         msg_format("%sに襲いかかる...", player_ptr->name);
217     } else {
218         if (plain_pickup) {
219             msg_format("%s(%c)を持っている。", item_name.data(), index_to_label(slot));
220         } else {
221             if (o_ptr->number > picked_count) {
222                 msg_format("%s拾って、%s(%c)を持っている。", picked_count_str.data(), item_name.data(), index_to_label(slot));
223             } else {
224                 msg_format("%s(%c)を拾った。", item_name.data(), index_to_label(slot));
225             }
226         }
227     }
228
229     angband_strcpy(record_o_name, old_item_name, old_item_name.length());
230 #else
231     msg_format("You have %s (%c).", item_name.data(), index_to_label(slot));
232     angband_strcpy(record_o_name, item_name, item_name.length());
233 #endif
234     record_turn = w_ptr->game_turn;
235     check_find_art_quest_completion(player_ptr, o_ptr);
236 }
237
238 /*!
239  * @brief プレイヤーがオブジェクト上に乗った際の表示処理 / Player "wants" to pick up an object or gold.
240  * @param player_ptr プレイヤーへの参照ポインタ
241  * @param pickup 自動拾い処理を行うならばTRUEとする
242  */
243 void carry(PlayerType *player_ptr, bool pickup)
244 {
245     verify_panel(player_ptr);
246     auto &rfu = RedrawingFlagsUpdater::get_instance();
247     rfu.set_flag(StatusRecalculatingFlag::MONSTER_STATUSES);
248     rfu.set_flag(MainWindowRedrawingFlag::MAP);
249     rfu.set_flag(SubWindowRedrawingFlag::OVERHEAD);
250     handle_stuff(player_ptr);
251     auto *g_ptr = &player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x];
252     autopick_pickup_items(player_ptr, g_ptr);
253     if (easy_floor) {
254         py_pickup_floor(player_ptr, pickup);
255         return;
256     }
257
258     for (auto it = g_ptr->o_idx_list.begin(); it != g_ptr->o_idx_list.end();) {
259         const OBJECT_IDX this_o_idx = *it++;
260         auto *o_ptr = &player_ptr->current_floor_ptr->o_list[this_o_idx];
261         const auto item_name = describe_flavor(player_ptr, o_ptr, 0);
262         disturb(player_ptr, false, false);
263         if (o_ptr->bi_key.tval() == ItemKindType::GOLD) {
264             int value = (long)o_ptr->pval;
265             delete_object_idx(player_ptr, this_o_idx);
266             msg_format(_(" $%ld の価値がある%sを見つけた。", "You collect %ld gold pieces worth of %s."), (long)value, item_name.data());
267             sound(SOUND_SELL);
268             player_ptr->au += value;
269             rfu.set_flag(MainWindowRedrawingFlag::GOLD);
270             rfu.set_flag(SubWindowRedrawingFlag::PLAYER);
271             continue;
272         }
273
274         if (o_ptr->marked.has(OmType::SUPRESS_MESSAGE)) {
275             o_ptr->marked.reset(OmType::SUPRESS_MESSAGE);
276             continue;
277         }
278
279         if (!pickup) {
280             msg_format(_("%sがある。", "You see %s."), item_name.data());
281             continue;
282         }
283
284         if (!check_store_item_to_inventory(player_ptr, o_ptr)) {
285             msg_format(_("ザックには%sを入れる隙間がない。", "You have no room for %s."), item_name.data());
286             continue;
287         }
288
289         int is_pickup_successful = true;
290         if (carry_query_flag) {
291             char out_val[MAX_NLEN + 20];
292             strnfmt(out_val, sizeof(out_val), _("%sを拾いますか? ", "Pick up %s? "), item_name.data());
293             is_pickup_successful = input_check(out_val);
294         }
295
296         if (is_pickup_successful) {
297             describe_pickup_item(player_ptr, this_o_idx);
298         }
299     }
300 }