OSDN Git Service

Merge pull request #41491 (taotao/hengband/fix-impure_calc_num_blow into develop).
[hengband/hengband.git] / src / inventory / player-inventory.c
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 "target/target-checker.h"
32 #include "view/display-messages.h"
33 #include "world/world.h"
34 #ifdef JP
35 #include "artifact/fixed-art-types.h"
36 #include "flavor/flavor-util.h"
37 #endif
38
39 /*!
40  * @brief 規定の処理にできるアイテムがプレイヤーの利用可能範囲内にあるかどうかを返す /
41  * Determine whether get_item() can get some item or not
42  * @return アイテムを拾えるならばTRUEを返す。
43  * @details assuming mode = (USE_EQUIP | USE_INVEN | USE_FLOOR).
44  */
45 bool can_get_item(player_type *owner_ptr, tval_type tval)
46 {
47     for (int j = 0; j < INVEN_TOTAL; j++)
48         if (item_tester_okay(owner_ptr, &owner_ptr->inventory_list[j], tval))
49             return TRUE;
50
51     OBJECT_IDX floor_list[23];
52     ITEM_NUMBER floor_num = scan_floor_items(owner_ptr, floor_list, owner_ptr->y, owner_ptr->x, 0x03, tval);
53     return floor_num != 0;
54 }
55
56 /*!
57  * @brief 床上のアイテムを拾う選択用サブルーチン
58  * @return プレイヤーによりアイテムが選択されたならTRUEを返す。
59  */
60 static bool py_pickup_floor_aux(player_type *owner_ptr)
61 {
62     OBJECT_IDX this_o_idx;
63     OBJECT_IDX item;
64     item_tester_hook = check_store_item_to_inventory;
65     concptr q = _("どれを拾いますか?", "Get which item? ");
66     concptr s = _("もうザックには床にあるどのアイテムも入らない。", "You no longer have any room for the objects on the floor.");
67     if (choose_object(owner_ptr, &item, q, s, (USE_FLOOR), 0))
68         this_o_idx = 0 - item;
69     else
70         return FALSE;
71
72     describe_pickup_item(owner_ptr, this_o_idx);
73     return TRUE;
74 }
75
76 /*!
77  * @brief 床上のアイテムを拾うメイン処理
78  * @param pickup FALSEなら金銭の自動拾いのみを行う/ FALSE then only gold will be picked up
79  * @return なし
80  * @details
81  * This is called by py_pickup() when easy_floor is TRUE.
82  */
83 void py_pickup_floor(player_type *owner_ptr, bool pickup)
84 {
85     OBJECT_IDX this_o_idx, next_o_idx = 0;
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     for (this_o_idx = owner_ptr->current_floor_ptr->grid_array[owner_ptr->y][owner_ptr->x].o_idx; this_o_idx; this_o_idx = next_o_idx) {
92         o_ptr = &owner_ptr->current_floor_ptr->o_list[this_o_idx];
93         describe_flavor(owner_ptr, o_name, o_ptr, 0);
94         next_o_idx = o_ptr->next_o_idx;
95         disturb(owner_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             owner_ptr->au += o_ptr->pval;
99             owner_ptr->redraw |= (PR_GOLD);
100             owner_ptr->window |= (PW_PLAYER);
101             delete_object_idx(owner_ptr, this_o_idx);
102             continue;
103         } else if (o_ptr->marked & OM_NOMSG) {
104             o_ptr->marked &= ~(OM_NOMSG);
105             continue;
106         }
107
108         if (check_store_item_to_inventory(owner_ptr, o_ptr))
109             can_pickup++;
110
111         floor_num++;
112         floor_o_idx = this_o_idx;
113     }
114
115     if (!floor_num)
116         return;
117
118     if (!pickup) {
119         if (floor_num == 1) {
120             o_ptr = &owner_ptr->current_floor_ptr->o_list[floor_o_idx];
121             describe_flavor(owner_ptr, o_name, o_ptr, 0);
122             msg_format(_("%sがある。", "You see %s."), o_name);
123         } else
124             msg_format(_("%d 個のアイテムの山がある。", "You see a pile of %d items."), floor_num);
125
126         return;
127     }
128
129     if (!can_pickup) {
130         if (floor_num == 1) {
131             o_ptr = &owner_ptr->current_floor_ptr->o_list[floor_o_idx];
132             describe_flavor(owner_ptr, o_name, o_ptr, 0);
133             msg_format(_("ザックには%sを入れる隙間がない。", "You have no room for %s."), o_name);
134         } else
135             msg_print(_("ザックには床にあるどのアイテムも入らない。", "You have no room for any of the objects on the floor."));
136
137         return;
138     }
139
140     if (floor_num != 1) {
141         while (can_pickup--)
142             if (!py_pickup_floor_aux(owner_ptr))
143                 break;
144
145         return;
146     }
147
148     if (carry_query_flag) {
149         char out_val[MAX_NLEN + 20];
150         o_ptr = &owner_ptr->current_floor_ptr->o_list[floor_o_idx];
151         describe_flavor(owner_ptr, o_name, o_ptr, 0);
152         (void)sprintf(out_val, _("%sを拾いますか? ", "Pick up %s? "), o_name);
153         if (!get_check(out_val))
154             return;
155     }
156
157     o_ptr = &owner_ptr->current_floor_ptr->o_list[floor_o_idx];
158     describe_pickup_item(owner_ptr, floor_o_idx);
159 }
160
161 /*!
162  * @brief プレイヤーがオブジェクトを拾った際のメッセージ表示処理 /
163  * Helper routine for py_pickup() and py_pickup_floor().
164  * @param creature_ptr プレーヤーへの参照ポインタ
165  * @param o_idx 取得したオブジェクトの参照ID
166  * @return なし
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 *owner_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 = &owner_ptr->current_floor_ptr->o_list[o_idx];
188
189 #ifdef JP
190     describe_flavor(owner_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(owner_ptr, o_ptr);
196     o_ptr = &owner_ptr->inventory_list[slot];
197     delete_object_idx(owner_ptr, o_idx);
198     if (owner_ptr->pseikaku == PERSONALITY_MUNCHKIN) {
199         bool old_known = identify_item(owner_ptr, o_ptr);
200         autopick_alter_item(owner_ptr, slot, (bool)(destroy_identify && !old_known));
201         if (o_ptr->marked & OM_AUTODESTROY)
202             return;
203     }
204
205     describe_flavor(owner_ptr, o_name, o_ptr, 0);
206
207 #ifdef JP
208     if ((o_ptr->name1 == ART_CRIMSON) && (owner_ptr->pseikaku == PERSONALITY_COMBAT)) {
209         msg_format("こうして、%sは『クリムゾン』を手に入れた。", owner_ptr->name);
210         msg_print("しかし今、『混沌のサーペント』の放ったモンスターが、");
211         msg_format("%sに襲いかかる...", owner_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(owner_ptr, o_ptr);
231 }
232
233 /*!
234  * @brief プレイヤーがオブジェクト上に乗った際の表示処理 / Player "wants" to pick up an object or gold.
235  * @param creature_ptr プレーヤーへの参照ポインタ
236  * @param pickup 自動拾い処理を行うならばTRUEとする
237  * @return なし
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 |= 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     OBJECT_IDX next_o_idx = 0;
254     for (OBJECT_IDX this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx) {
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         next_o_idx = o_ptr->next_o_idx;
260         disturb(creature_ptr, FALSE, FALSE);
261         if (o_ptr->tval == TV_GOLD) {
262             int value = (long)o_ptr->pval;
263             delete_object_idx(creature_ptr, this_o_idx);
264             msg_format(_(" $%ld の価値がある%sを見つけた。", "You collect %ld gold pieces worth of %s."), (long)value, o_name);
265             sound(SOUND_SELL);
266             creature_ptr->au += value;
267             creature_ptr->redraw |= (PR_GOLD);
268             creature_ptr->window |= (PW_PLAYER);
269             continue;
270         }
271
272         if (o_ptr->marked & OM_NOMSG) {
273             o_ptr->marked &= ~OM_NOMSG;
274             continue;
275         }
276
277         if (!pickup) {
278             msg_format(_("%sがある。", "You see %s."), o_name);
279             continue;
280         }
281
282         if (!check_store_item_to_inventory(creature_ptr, o_ptr)) {
283             msg_format(_("ザックには%sを入れる隙間がない。", "You have no room for %s."), o_name);
284             continue;
285         }
286
287         int is_pickup_successful = TRUE;
288         if (carry_query_flag) {
289             char out_val[MAX_NLEN + 20];
290             sprintf(out_val, _("%sを拾いますか? ", "Pick up %s? "), o_name);
291             is_pickup_successful = get_check(out_val);
292         }
293
294         if (is_pickup_successful) {
295             describe_pickup_item(creature_ptr, this_o_idx);
296         }
297     }
298 }