OSDN Git Service

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