OSDN Git Service

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