OSDN Git Service

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