OSDN Git Service

[Refactor] #1479 Changed creature_ptr to player_ptr
[hengbandforosx/hengbandosx.git] / src / cmd-action / cmd-open-close.cpp
1 #include "cmd-action/cmd-open-close.h"
2 #include "action/open-close-execution.h"
3 #include "action/open-util.h"
4 #include "cmd-action/cmd-attack.h"
5 #include "core/disturbance.h"
6 #include "core/player-redraw-types.h"
7 #include "floor/geometry.h"
8 #include "game-option/disturbance-options.h"
9 #include "game-option/input-options.h"
10 #include "grid/feature.h"
11 #include "grid/grid.h"
12 #include "inventory/inventory-object.h"
13 #include "inventory/inventory-slot-types.h"
14 #include "io/input-key-requester.h"
15 #include "player-status/player-energy.h"
16 #include "player/attack-defense-types.h"
17 #include "player/special-defense-types.h"
18 #include "specific-object/chest.h"
19 #include "status/action-setter.h"
20 #include "status/experience.h"
21 #include "system/floor-type-definition.h"
22 #include "system/grid-type-definition.h"
23 #include "system/object-type-definition.h"
24 #include "system/player-type-definition.h"
25 #include "target/target-getter.h"
26 #include "term/screen-processor.h"
27 #include "util/bit-flags-calculator.h"
28 #include "view/display-messages.h"
29
30 /*!
31  * @brief 箱を開ける実行処理 /
32  * Attempt to open the given chest at the given location
33  * @param y 箱の存在するマスのY座標
34  * @param x 箱の存在するマスのX座標
35  * @param o_idx 箱のオブジェクトID
36  * @return 箱が開かなかった場合TRUE / Returns TRUE if repeated commands may continue
37  * @details
38  * Assume there is no monster blocking the destination
39  */
40 static bool exe_open_chest(player_type *player_ptr, POSITION y, POSITION x, OBJECT_IDX o_idx)
41 {
42     bool flag = true;
43     bool more = false;
44     object_type *o_ptr = &player_ptr->current_floor_ptr->o_list[o_idx];
45     PlayerEnergy(player_ptr).set_player_turn_energy(100);
46     if (o_ptr->pval > 0) {
47         flag = false;
48         int i = player_ptr->skill_dis;
49         if (player_ptr->blind || no_lite(player_ptr))
50             i = i / 10;
51
52         if (player_ptr->confused || player_ptr->image)
53             i = i / 10;
54
55         int j = i - o_ptr->pval;
56         if (j < 2)
57             j = 2;
58
59         if (randint0(100) < j) {
60             msg_print(_("鍵をはずした。", "You have picked the lock."));
61             gain_exp(player_ptr, 1);
62             flag = true;
63         } else {
64             more = true;
65             if (flush_failure)
66                 flush();
67
68             msg_print(_("鍵をはずせなかった。", "You failed to pick the lock."));
69         }
70     }
71
72     if (flag) {
73         chest_trap(player_ptr, y, x, o_idx);
74         chest_death(player_ptr, false, y, x, o_idx);
75     }
76
77     return more;
78 }
79
80 /*!
81  * @brief 「開ける」コマンドのメインルーチン /
82  * Open a closed/locked/jammed door or a closed/locked chest.
83  * @details
84  * Unlocking a locked door/chest is worth one experience point.
85  */
86 void do_cmd_open(player_type *player_ptr)
87 {
88     POSITION y, x;
89     DIRECTION dir;
90     OBJECT_IDX o_idx;
91     bool more = false;
92     if (player_ptr->wild_mode)
93         return;
94
95     if (player_ptr->special_defense & KATA_MUSOU)
96         set_action(player_ptr, ACTION_NONE);
97
98     if (easy_open) {
99         int num_doors = count_dt(player_ptr, &y, &x, is_closed_door, false);
100         int num_chests = count_chests(player_ptr, &y, &x, false);
101         if (num_doors || num_chests) {
102             bool too_many = (num_doors && num_chests) || (num_doors > 1) || (num_chests > 1);
103             if (!too_many)
104                 command_dir = coords_to_dir(player_ptr, y, x);
105         }
106     }
107
108     if (command_arg) {
109         command_rep = command_arg - 1;
110         player_ptr->redraw |= PR_STATE;
111         command_arg = 0;
112     }
113
114     if (get_rep_dir(player_ptr, &dir, true)) {
115         FEAT_IDX feat;
116         grid_type *g_ptr;
117         y = player_ptr->y + ddy[dir];
118         x = player_ptr->x + ddx[dir];
119         g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
120         feat = g_ptr->get_feat_mimic();
121         o_idx = chest_check(player_ptr->current_floor_ptr, y, x, false);
122         if (f_info[feat].flags.has_not(FF::OPEN) && !o_idx) {
123             msg_print(_("そこには開けるものが見当たらない。", "You see nothing there to open."));
124         } else if (g_ptr->m_idx && player_ptr->riding != g_ptr->m_idx) {
125             PlayerEnergy(player_ptr).set_player_turn_energy(100);
126             msg_print(_("モンスターが立ちふさがっている!", "There is a monster in the way!"));
127             do_cmd_attack(player_ptr, y, x, HISSATSU_NONE);
128         } else if (o_idx) {
129             more = exe_open_chest(player_ptr, y, x, o_idx);
130         } else {
131             more = exe_open(player_ptr, y, x);
132         }
133     }
134
135     if (!more)
136         disturb(player_ptr, false, false);
137 }
138
139 /*!
140  * @brief 「閉じる」コマンドのメインルーチン /
141  * Close an open door.
142  * @details
143  * Unlocking a locked door/chest is worth one experience point.
144  */
145 void do_cmd_close(player_type *player_ptr)
146 {
147     POSITION y, x;
148     DIRECTION dir;
149     bool more = false;
150     if (player_ptr->wild_mode)
151         return;
152
153     if (player_ptr->special_defense & KATA_MUSOU)
154         set_action(player_ptr, ACTION_NONE);
155
156     if (easy_open && (count_dt(player_ptr, &y, &x, is_open, false) == 1))
157         command_dir = coords_to_dir(player_ptr, y, x);
158
159     if (command_arg) {
160         command_rep = command_arg - 1;
161         player_ptr->redraw |= (PR_STATE);
162         command_arg = 0;
163     }
164
165     if (get_rep_dir(player_ptr, &dir, false)) {
166         grid_type *g_ptr;
167         FEAT_IDX feat;
168         y = player_ptr->y + ddy[dir];
169         x = player_ptr->x + ddx[dir];
170         g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
171         feat = g_ptr->get_feat_mimic();
172         if (f_info[feat].flags.has_not(FF::CLOSE)) {
173             msg_print(_("そこには閉じるものが見当たらない。", "You see nothing there to close."));
174         } else if (g_ptr->m_idx) {
175             PlayerEnergy(player_ptr).set_player_turn_energy(100);
176             msg_print(_("モンスターが立ちふさがっている!", "There is a monster in the way!"));
177             do_cmd_attack(player_ptr, y, x, HISSATSU_NONE);
178         } else {
179             more = exe_close(player_ptr, y, x);
180         }
181     }
182
183     if (!more)
184         disturb(player_ptr, false, false);
185 }
186
187 /*!
188  * @brief 箱、床のトラップ解除処理双方の統合メインルーチン /
189  * Disarms a trap, or chest
190  */
191 void do_cmd_disarm(player_type *player_ptr)
192 {
193     POSITION y, x;
194     DIRECTION dir;
195     OBJECT_IDX o_idx;
196     bool more = false;
197     if (player_ptr->wild_mode)
198         return;
199
200     if (player_ptr->special_defense & KATA_MUSOU)
201         set_action(player_ptr, ACTION_NONE);
202
203     if (easy_disarm) {
204         int num_traps = count_dt(player_ptr, &y, &x, is_trap, true);
205         int num_chests = count_chests(player_ptr, &y, &x, true);
206         if (num_traps || num_chests) {
207             bool too_many = (num_traps && num_chests) || (num_traps > 1) || (num_chests > 1);
208             if (!too_many)
209                 command_dir = coords_to_dir(player_ptr, y, x);
210         }
211     }
212
213     if (command_arg) {
214         command_rep = command_arg - 1;
215         player_ptr->redraw |= (PR_STATE);
216         command_arg = 0;
217     }
218
219     if (get_rep_dir(player_ptr, &dir, true)) {
220         grid_type *g_ptr;
221         FEAT_IDX feat;
222         y = player_ptr->y + ddy[dir];
223         x = player_ptr->x + ddx[dir];
224         g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
225         feat = g_ptr->get_feat_mimic();
226         o_idx = chest_check(player_ptr->current_floor_ptr, y, x, true);
227         if (!is_trap(player_ptr, feat) && !o_idx) {
228             msg_print(_("そこには解除するものが見当たらない。", "You see nothing there to disarm."));
229         } else if (g_ptr->m_idx && player_ptr->riding != g_ptr->m_idx) {
230             msg_print(_("モンスターが立ちふさがっている!", "There is a monster in the way!"));
231             do_cmd_attack(player_ptr, y, x, HISSATSU_NONE);
232         } else if (o_idx) {
233             more = exe_disarm_chest(player_ptr, y, x, o_idx);
234         } else {
235             more = exe_disarm(player_ptr, y, x, dir);
236         }
237     }
238
239     if (!more)
240         disturb(player_ptr, false, false);
241 }
242
243 /*!
244  * @brief 「打ち破る」動作コマンドのメインルーチン /
245  * Bash open a door, success based on character strength
246  * @details
247  * <pre>
248  * For a closed door, pval is positive if locked; negative if stuck.
249  *
250  * For an open door, pval is positive for a broken door.
251  *
252  * A closed door can be opened - harder if locked. Any door might be
253  * bashed open (and thereby broken). Bashing a door is (potentially)
254  * faster! You move into the door way. To open a stuck door, it must
255  * be bashed. A closed door can be jammed (see do_cmd_spike()).
256  *
257  * Creatures can also open or bash doors, see elsewhere.
258  * </pre>
259  */
260 void do_cmd_bash(player_type *player_ptr)
261 {
262     POSITION y, x;
263     DIRECTION dir;
264     grid_type *g_ptr;
265     bool more = false;
266     if (player_ptr->wild_mode)
267         return;
268
269     if (player_ptr->special_defense & KATA_MUSOU)
270         set_action(player_ptr, ACTION_NONE);
271
272     if (command_arg) {
273         command_rep = command_arg - 1;
274         player_ptr->redraw |= (PR_STATE);
275         command_arg = 0;
276     }
277
278     if (get_rep_dir(player_ptr, &dir, false)) {
279         FEAT_IDX feat;
280         y = player_ptr->y + ddy[dir];
281         x = player_ptr->x + ddx[dir];
282         g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
283         feat = g_ptr->get_feat_mimic();
284         if (f_info[feat].flags.has_not(FF::BASH)) {
285             msg_print(_("そこには体当たりするものが見当たらない。", "You see nothing there to bash."));
286         } else if (g_ptr->m_idx) {
287             PlayerEnergy(player_ptr).set_player_turn_energy(100);
288             msg_print(_("モンスターが立ちふさがっている!", "There is a monster in the way!"));
289             do_cmd_attack(player_ptr, y, x, HISSATSU_NONE);
290         } else {
291             more = exe_bash(player_ptr, y, x, dir);
292         }
293     }
294
295     if (!more)
296         disturb(player_ptr, false, false);
297 }
298
299
300 /*!
301  * @brief 「くさびを打つ」ために必要なオブジェクトを所持しているかどうかの判定を返す /
302  * Find the index of some "spikes", if possible.
303  * @param ip くさびとして打てるオブジェクトのID
304  * @return オブジェクトがある場合TRUEを返す
305  * @details
306  * <pre>
307  * Let user choose a pile of spikes, perhaps?
308  * </pre>
309  */
310 static bool get_spike(player_type *player_ptr, INVENTORY_IDX *ip)
311 {
312     for (INVENTORY_IDX i = 0; i < INVEN_PACK; i++) {
313         object_type *o_ptr = &player_ptr->inventory_list[i];
314         if (!o_ptr->k_idx)
315             continue;
316
317         if (o_ptr->tval == TV_SPIKE) {
318             *ip = i;
319             return true;
320         }
321     }
322
323     return false;
324 }
325
326 /*!
327  * @brief 「くさびを打つ」動作コマンドのメインルーチン /
328  * Jam a closed door with a spike
329  * @param player_ptr プレーヤーへの参照ポインタ
330  * @details
331  * <pre>
332  * This command may NOT be repeated
333  * </pre>
334  */
335 void do_cmd_spike(player_type *player_ptr)
336 {
337     DIRECTION dir;
338     if (player_ptr->wild_mode)
339         return;
340
341     if (player_ptr->special_defense & KATA_MUSOU)
342         set_action(player_ptr, ACTION_NONE);
343
344     if (!get_rep_dir(player_ptr, &dir, false))
345         return;
346
347     POSITION y = player_ptr->y + ddy[dir];
348     POSITION x = player_ptr->x + ddx[dir];
349     grid_type *g_ptr;
350     g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
351     FEAT_IDX feat = g_ptr->get_feat_mimic();
352     INVENTORY_IDX item;
353     if (f_info[feat].flags.has_not(FF::SPIKE)) {
354         msg_print(_("そこにはくさびを打てるものが見当たらない。", "You see nothing there to spike."));
355     } else if (!get_spike(player_ptr, &item)) {
356         msg_print(_("くさびを持っていない!", "You have no spikes!"));
357     } else if (g_ptr->m_idx) {
358         PlayerEnergy(player_ptr).set_player_turn_energy(100);
359         msg_print(_("モンスターが立ちふさがっている!", "There is a monster in the way!"));
360         do_cmd_attack(player_ptr, y, x, HISSATSU_NONE);
361     } else {
362         PlayerEnergy(player_ptr).set_player_turn_energy(100);
363         msg_format(_("%sにくさびを打ち込んだ。", "You jam the %s with a spike."), f_info[feat].name.c_str());
364         cave_alter_feat(player_ptr, y, x, FF::SPIKE);
365         vary_item(player_ptr, item, -1);
366     }
367 }