OSDN Git Service

Replace sprintf() simply. Does part of the work to resolve https://github.com/hengba...
[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 "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     OBJECT_IDX floor_list[23];
57     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);
58     return floor_num != 0;
59 }
60
61 /*!
62  * @brief 床上のアイテムを拾う選択用サブルーチン
63  * @return プレイヤーによりアイテムが選択されたならTRUEを返す。
64  */
65 static bool py_pickup_floor_aux(PlayerType *player_ptr)
66 {
67     OBJECT_IDX this_o_idx;
68     OBJECT_IDX item;
69     concptr q = _("どれを拾いますか?", "Get which item? ");
70     concptr s = _("もうザックには床にあるどのアイテムも入らない。", "You no longer have any room for the objects on the floor.");
71     if (choose_object(player_ptr, &item, q, s, (USE_FLOOR), FuncItemTester(check_store_item_to_inventory, player_ptr))) {
72         this_o_idx = 0 - item;
73     } else {
74         return false;
75     }
76
77     describe_pickup_item(player_ptr, this_o_idx);
78     return true;
79 }
80
81 /*!
82  * @brief 床上のアイテムを拾うメイン処理
83  * @param pickup FALSEなら金銭の自動拾いのみを行う/ FALSE then only gold will be picked up
84  * @details
85  * This is called by py_pickup() when easy_floor is TRUE.
86  */
87 void py_pickup_floor(PlayerType *player_ptr, bool pickup)
88 {
89     GAME_TEXT o_name[MAX_NLEN];
90     ItemEntity *o_ptr;
91     int floor_num = 0;
92     OBJECT_IDX floor_o_idx = 0;
93     int can_pickup = 0;
94     auto &o_idx_list = player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x].o_idx_list;
95     for (auto it = o_idx_list.begin(); it != o_idx_list.end();) {
96         const OBJECT_IDX this_o_idx = *it++;
97         o_ptr = &player_ptr->current_floor_ptr->o_list[this_o_idx];
98         describe_flavor(player_ptr, o_name, o_ptr, 0);
99         disturb(player_ptr, false, false);
100         if (o_ptr->bi_key.tval() == ItemKindType::GOLD) {
101             msg_format(_(" $%ld の価値がある%sを見つけた。", "You have found %ld gold pieces worth of %s."), (long)o_ptr->pval, o_name);
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             o_ptr = &player_ptr->current_floor_ptr->o_list[floor_o_idx];
128             describe_flavor(player_ptr, o_name, o_ptr, 0);
129             msg_format(_("%sがある。", "You see %s."), o_name);
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             o_ptr = &player_ptr->current_floor_ptr->o_list[floor_o_idx];
140             describe_flavor(player_ptr, o_name, o_ptr, 0);
141             msg_format(_("ザックには%sを入れる隙間がない。", "You have no room for %s."), o_name);
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         char out_val[MAX_NLEN + 20];
161         o_ptr = &player_ptr->current_floor_ptr->o_list[floor_o_idx];
162         describe_flavor(player_ptr, o_name, o_ptr, 0);
163         strnfmt(out_val, sizeof(out_val), _("%sを拾いますか? ", "Pick up %s? "), o_name);
164         if (!get_check(out_val)) {
165             return;
166         }
167     }
168
169     o_ptr = &player_ptr->current_floor_ptr->o_list[floor_o_idx];
170     describe_pickup_item(player_ptr, floor_o_idx);
171 }
172
173 /*!
174  * @brief プレイヤーがオブジェクトを拾った際のメッセージ表示処理 /
175  * Helper routine for py_pickup() and py_pickup_floor().
176  * @param player_ptr プレイヤーへの参照ポインタ
177  * @param o_idx 取得したオブジェクトの参照ID
178  * @details
179  * アイテムを拾った際に「2つのケーキを持っている」
180  * "You have two cakes." とアイテムを拾った後の合計のみの表示がオリジナルだが、
181  * 違和感があるという指摘をうけたので、「~を拾った、~を持っている」という表示にかえてある。
182  * そのための配列。
183  * Add the given dungeon object to the character's inventory.\n
184  * Delete the object afterwards.\n
185  */
186 void describe_pickup_item(PlayerType *player_ptr, OBJECT_IDX o_idx)
187 {
188 #ifdef JP
189     GAME_TEXT o_name[MAX_NLEN];
190     GAME_TEXT old_name[MAX_NLEN];
191 #else
192     GAME_TEXT o_name[MAX_NLEN];
193 #endif
194
195     ItemEntity *o_ptr;
196     o_ptr = &player_ptr->current_floor_ptr->o_list[o_idx];
197
198 #ifdef JP
199     describe_flavor(player_ptr, old_name, o_ptr, OD_NAME_ONLY);
200     const auto picked_count_str = describe_count_with_counter_suffix(*o_ptr);
201     const auto picked_count = o_ptr->number;
202 #endif
203
204     INVENTORY_IDX slot = store_item_to_inventory(player_ptr, o_ptr);
205     o_ptr = &player_ptr->inventory_list[slot];
206     delete_object_idx(player_ptr, o_idx);
207     if (player_ptr->ppersonality == PERSONALITY_MUNCHKIN) {
208         bool old_known = identify_item(player_ptr, o_ptr);
209         autopick_alter_item(player_ptr, slot, (bool)(destroy_identify && !old_known));
210         if (o_ptr->marked.has(OmType::AUTODESTROY)) {
211             return;
212         }
213     }
214
215     describe_flavor(player_ptr, o_name, o_ptr, 0);
216
217 #ifdef JP
218     if (o_ptr->is_specific_artifact(FixedArtifactId::CRIMSON) && (player_ptr->ppersonality == PERSONALITY_COMBAT)) {
219         msg_format("こうして、%sは『クリムゾン』を手に入れた。", player_ptr->name);
220         msg_print("しかし今、『混沌のサーペント』の放ったモンスターが、");
221         msg_format("%sに襲いかかる...", player_ptr->name);
222     } else {
223         if (plain_pickup) {
224             msg_format("%s(%c)を持っている。", o_name, index_to_label(slot));
225         } else {
226             if (o_ptr->number > picked_count) {
227                 msg_format("%s拾って、%s(%c)を持っている。", picked_count_str.data(), o_name, index_to_label(slot));
228             } else {
229                 msg_format("%s(%c)を拾った。", o_name, index_to_label(slot));
230             }
231         }
232     }
233
234     strcpy(record_o_name, old_name);
235 #else
236     msg_format("You have %s (%c).", o_name, index_to_label(slot));
237     strcpy(record_o_name, o_name);
238 #endif
239     record_turn = w_ptr->game_turn;
240     check_find_art_quest_completion(player_ptr, o_ptr);
241 }
242
243 /*!
244  * @brief プレイヤーがオブジェクト上に乗った際の表示処理 / Player "wants" to pick up an object or gold.
245  * @param player_ptr プレイヤーへの参照ポインタ
246  * @param pickup 自動拾い処理を行うならばTRUEとする
247  */
248 void carry(PlayerType *player_ptr, bool pickup)
249 {
250     verify_panel(player_ptr);
251     player_ptr->update |= PU_MONSTERS;
252     player_ptr->redraw |= PR_MAP;
253     player_ptr->window_flags |= PW_OVERHEAD;
254     handle_stuff(player_ptr);
255     auto *g_ptr = &player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x];
256     autopick_pickup_items(player_ptr, g_ptr);
257     if (easy_floor) {
258         py_pickup_floor(player_ptr, pickup);
259         return;
260     }
261
262     for (auto it = g_ptr->o_idx_list.begin(); it != g_ptr->o_idx_list.end();) {
263         const OBJECT_IDX this_o_idx = *it++;
264         ItemEntity *o_ptr;
265         o_ptr = &player_ptr->current_floor_ptr->o_list[this_o_idx];
266         GAME_TEXT o_name[MAX_NLEN];
267         describe_flavor(player_ptr, o_name, o_ptr, 0);
268         disturb(player_ptr, false, false);
269         if (o_ptr->bi_key.tval() == ItemKindType::GOLD) {
270             int value = (long)o_ptr->pval;
271             delete_object_idx(player_ptr, this_o_idx);
272             msg_format(_(" $%ld の価値がある%sを見つけた。", "You collect %ld gold pieces worth of %s."), (long)value, o_name);
273             sound(SOUND_SELL);
274             player_ptr->au += value;
275             player_ptr->redraw |= (PR_GOLD);
276             player_ptr->window_flags |= (PW_PLAYER);
277             continue;
278         }
279
280         if (o_ptr->marked.has(OmType::SUPRESS_MESSAGE)) {
281             o_ptr->marked.reset(OmType::SUPRESS_MESSAGE);
282             continue;
283         }
284
285         if (!pickup) {
286             msg_format(_("%sがある。", "You see %s."), o_name);
287             continue;
288         }
289
290         if (!check_store_item_to_inventory(player_ptr, o_ptr)) {
291             msg_format(_("ザックには%sを入れる隙間がない。", "You have no room for %s."), o_name);
292             continue;
293         }
294
295         int is_pickup_successful = true;
296         if (carry_query_flag) {
297             char out_val[MAX_NLEN + 20];
298             strnfmt(out_val, sizeof(out_val), _("%sを拾いますか? ", "Pick up %s? "), o_name);
299             is_pickup_successful = get_check(out_val);
300         }
301
302         if (is_pickup_successful) {
303             describe_pickup_item(player_ptr, this_o_idx);
304         }
305     }
306 }