OSDN Git Service

[Refactor] #40634 Made ae_type for the preparation to divide exe_activate()
[hengband/hengband.git] / src / action / movement-execution.c
1 #include "action/movement-execution.h"
2 #include "action/open-close-execution.h"
3 #include "art-definition/art-sword-types.h"
4 #include "cmd-action/cmd-attack.h"
5 #include "core/disturbance.h"
6 #include "core/player-update-types.h"
7 #include "core/stuff-handler.h"
8 #include "floor/floor.h"
9 #include "floor/pattern-walk.h"
10 #include "game-option/input-options.h"
11 #include "grid/feature.h"
12 #include "grid/grid.h"
13 #include "inventory/inventory-slot-types.h"
14 #include "main/sound-definitions-table.h"
15 #include "main/sound-of-music.h"
16 #include "monster-race/monster-race.h"
17 #include "monster-race/race-flags-resistance.h"
18 #include "monster-race/race-flags1.h"
19 #include "monster-race/race-flags2.h"
20 #include "monster-race/race-flags7.h"
21 #include "monster-race/race-flags8.h"
22 #include "monster/monster-describer.h"
23 #include "monster/monster-info.h"
24 #include "monster/monster-status-setter.h"
25 #include "monster/monster-status.h"
26 #include "mutation/mutation-flag-types.h"
27 #include "object/warning.h"
28 #include "player/player-move.h"
29 #include "system/floor-type-definition.h"
30 #include "system/object-type-definition.h"
31 #include "util/bit-flags-calculator.h"
32 #include "view/display-messages.h"
33 #ifdef JP
34 #else
35 #include "locale/english.h"
36 #endif
37
38 /*
39  * todo 負論理なので反転させたい
40  * Determine if a "boundary" grid is "floor mimic"
41  * @param grid_type *g_ptr
42  * @param feature_type *f_ptr
43  * @param feature_type  *mimic_f_ptr
44  * @return 移動不能であればTRUE
45  */
46 static bool boundary_floor(grid_type *g_ptr, feature_type *f_ptr, feature_type *mimic_f_ptr)
47 {
48     bool is_boundary_floor = g_ptr->mimic > 0;
49     is_boundary_floor &= permanent_wall(f_ptr);
50     is_boundary_floor &= have_flag((mimic_f_ptr)->flags, FF_MOVE) || have_flag((mimic_f_ptr)->flags, FF_CAN_FLY);
51     is_boundary_floor &= have_flag((mimic_f_ptr)->flags, FF_PROJECT);
52     is_boundary_floor &= !have_flag((mimic_f_ptr)->flags, FF_OPEN);
53     return is_boundary_floor;
54 }
55
56 /*!
57  * @brief 該当地形のトラップがプレイヤーにとって無効かどうかを判定して返す /
58  * Move player in the given direction, with the given "pickup" flag.
59  * @param creature_ptr プレーヤーへの参照ポインタ
60  * @param dir 移動方向ID
61  * @param do_pickup 罠解除を試みながらの移動ならばTRUE
62  * @param break_trap トラップ粉砕処理を行うならばTRUE
63  * @return 実際に移動が行われたならばTRUEを返す。
64  * @note
65  * This routine should (probably) always induce energy expenditure.\n
66  * @details
67  * Note that moving will *always* take a turn, and will *always* hit\n
68  * any monster which might be in the destination grid.  Previously,\n
69  * moving into walls was "free" and did NOT hit invisible monsters.\n
70  */
71 void exe_movement(player_type *creature_ptr, DIRECTION dir, bool do_pickup, bool break_trap)
72 {
73     POSITION y = creature_ptr->y + ddy[dir];
74     POSITION x = creature_ptr->x + ddx[dir];
75     floor_type *floor_ptr = creature_ptr->current_floor_ptr;
76     grid_type *g_ptr = &floor_ptr->grid_array[y][x];
77     bool p_can_enter = player_can_enter(creature_ptr, g_ptr->feat, CEM_P_CAN_ENTER_PATTERN);
78     bool stormbringer = FALSE;
79     if (!floor_ptr->dun_level && !creature_ptr->wild_mode && ((x == 0) || (x == MAX_WID - 1) || (y == 0) || (y == MAX_HGT - 1))) {
80         if (g_ptr->mimic && player_can_enter(creature_ptr, g_ptr->mimic, 0)) {
81             if ((y == 0) && (x == 0)) {
82                 creature_ptr->wilderness_y--;
83                 creature_ptr->wilderness_x--;
84                 creature_ptr->oldpy = floor_ptr->height - 2;
85                 creature_ptr->oldpx = floor_ptr->width - 2;
86                 creature_ptr->ambush_flag = FALSE;
87             } else if ((y == 0) && (x == MAX_WID - 1)) {
88                 creature_ptr->wilderness_y--;
89                 creature_ptr->wilderness_x++;
90                 creature_ptr->oldpy = floor_ptr->height - 2;
91                 creature_ptr->oldpx = 1;
92                 creature_ptr->ambush_flag = FALSE;
93             } else if ((y == MAX_HGT - 1) && (x == 0)) {
94                 creature_ptr->wilderness_y++;
95                 creature_ptr->wilderness_x--;
96                 creature_ptr->oldpy = 1;
97                 creature_ptr->oldpx = floor_ptr->width - 2;
98                 creature_ptr->ambush_flag = FALSE;
99             } else if ((y == MAX_HGT - 1) && (x == MAX_WID - 1)) {
100                 creature_ptr->wilderness_y++;
101                 creature_ptr->wilderness_x++;
102                 creature_ptr->oldpy = 1;
103                 creature_ptr->oldpx = 1;
104                 creature_ptr->ambush_flag = FALSE;
105             } else if (y == 0) {
106                 creature_ptr->wilderness_y--;
107                 creature_ptr->oldpy = floor_ptr->height - 2;
108                 creature_ptr->oldpx = x;
109                 creature_ptr->ambush_flag = FALSE;
110             } else if (y == MAX_HGT - 1) {
111                 creature_ptr->wilderness_y++;
112                 creature_ptr->oldpy = 1;
113                 creature_ptr->oldpx = x;
114                 creature_ptr->ambush_flag = FALSE;
115             } else if (x == 0) {
116                 creature_ptr->wilderness_x--;
117                 creature_ptr->oldpx = floor_ptr->width - 2;
118                 creature_ptr->oldpy = y;
119                 creature_ptr->ambush_flag = FALSE;
120             } else if (x == MAX_WID - 1) {
121                 creature_ptr->wilderness_x++;
122                 creature_ptr->oldpx = 1;
123                 creature_ptr->oldpy = y;
124                 creature_ptr->ambush_flag = FALSE;
125             }
126
127             creature_ptr->leaving = TRUE;
128             take_turn(creature_ptr, 100);
129             return;
130         }
131
132         p_can_enter = FALSE;
133     }
134
135     monster_type *m_ptr;
136     m_ptr = &floor_ptr->m_list[g_ptr->m_idx];
137     if (creature_ptr->inventory_list[INVEN_RARM].name1 == ART_STORMBRINGER)
138         stormbringer = TRUE;
139
140     if (creature_ptr->inventory_list[INVEN_LARM].name1 == ART_STORMBRINGER)
141         stormbringer = TRUE;
142
143     feature_type *f_ptr = &f_info[g_ptr->feat];
144     bool p_can_kill_walls = creature_ptr->kill_wall && have_flag(f_ptr->flags, FF_HURT_DISI) && (!p_can_enter || !have_flag(f_ptr->flags, FF_LOS))
145         && !have_flag(f_ptr->flags, FF_PERMANENT);
146     GAME_TEXT m_name[MAX_NLEN];
147     bool can_move = TRUE;
148     bool do_past = FALSE;
149     if (g_ptr->m_idx && (m_ptr->ml || p_can_enter || p_can_kill_walls)) {
150         monster_race *r_ptr = &r_info[m_ptr->r_idx];
151         if (!is_hostile(m_ptr)
152             && !(creature_ptr->confused || creature_ptr->image || !m_ptr->ml || creature_ptr->stun
153                 || ((creature_ptr->muta2 & MUT2_BERS_RAGE) && creature_ptr->shero))
154             && pattern_seq(creature_ptr, creature_ptr->y, creature_ptr->x, y, x) && (p_can_enter || p_can_kill_walls)) {
155             (void)set_monster_csleep(creature_ptr, g_ptr->m_idx, 0);
156             monster_desc(creature_ptr, m_name, m_ptr, 0);
157             if (m_ptr->ml) {
158                 if (!creature_ptr->image)
159                     monster_race_track(creature_ptr, m_ptr->ap_r_idx);
160
161                 health_track(creature_ptr, g_ptr->m_idx);
162             }
163
164             if ((stormbringer && (randint1(1000) > 666)) || (creature_ptr->pclass == CLASS_BERSERKER)) {
165                 do_cmd_attack(creature_ptr, y, x, 0);
166                 can_move = FALSE;
167             } else if (monster_can_cross_terrain(creature_ptr, floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].feat, r_ptr, 0)) {
168                 do_past = TRUE;
169             } else {
170                 msg_format(_("%^sが邪魔だ!", "%^s is in your way!"), m_name);
171                 free_turn(creature_ptr);
172                 can_move = FALSE;
173             }
174         } else {
175             do_cmd_attack(creature_ptr, y, x, 0);
176             can_move = FALSE;
177         }
178     }
179
180     monster_type *riding_m_ptr = &floor_ptr->m_list[creature_ptr->riding];
181     monster_race *riding_r_ptr = &r_info[creature_ptr->riding ? riding_m_ptr->r_idx : 0];
182     if (can_move && creature_ptr->riding) {
183         if (riding_r_ptr->flags1 & RF1_NEVER_MOVE) {
184             msg_print(_("動けない!", "Can't move!"));
185             free_turn(creature_ptr);
186             can_move = FALSE;
187             disturb(creature_ptr, FALSE, TRUE);
188         } else if (monster_fear_remaining(riding_m_ptr)) {
189             GAME_TEXT steed_name[MAX_NLEN];
190             monster_desc(creature_ptr, steed_name, riding_m_ptr, 0);
191             msg_format(_("%sが恐怖していて制御できない。", "%^s is too scared to control."), steed_name);
192             can_move = FALSE;
193             disturb(creature_ptr, FALSE, TRUE);
194         } else if (creature_ptr->riding_ryoute) {
195             can_move = FALSE;
196             disturb(creature_ptr, FALSE, TRUE);
197         } else if (have_flag(f_ptr->flags, FF_CAN_FLY) && (riding_r_ptr->flags7 & RF7_CAN_FLY)) {
198             /* Allow moving */
199         } else if (have_flag(f_ptr->flags, FF_CAN_SWIM) && (riding_r_ptr->flags7 & RF7_CAN_SWIM)) {
200             /* Allow moving */
201         } else if (have_flag(f_ptr->flags, FF_WATER) && !(riding_r_ptr->flags7 & RF7_AQUATIC)
202             && (have_flag(f_ptr->flags, FF_DEEP) || (riding_r_ptr->flags2 & RF2_AURA_FIRE))) {
203             msg_format(_("%sの上に行けない。", "Can't swim."), f_name + f_info[get_feat_mimic(g_ptr)].name);
204             free_turn(creature_ptr);
205             can_move = FALSE;
206             disturb(creature_ptr, FALSE, TRUE);
207         } else if (!have_flag(f_ptr->flags, FF_WATER) && (riding_r_ptr->flags7 & RF7_AQUATIC)) {
208             msg_format(_("%sから上がれない。", "Can't land."), f_name + f_info[get_feat_mimic(&floor_ptr->grid_array[creature_ptr->y][creature_ptr->x])].name);
209             free_turn(creature_ptr);
210             can_move = FALSE;
211             disturb(creature_ptr, FALSE, TRUE);
212         } else if (have_flag(f_ptr->flags, FF_LAVA) && !(riding_r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK)) {
213             msg_format(_("%sの上に行けない。", "Too hot to go through."), f_name + f_info[get_feat_mimic(g_ptr)].name);
214             free_turn(creature_ptr);
215             can_move = FALSE;
216             disturb(creature_ptr, FALSE, TRUE);
217         }
218
219         if (can_move && monster_stunned_remaining(riding_m_ptr) && one_in_(2)) {
220             GAME_TEXT steed_name[MAX_NLEN];
221             monster_desc(creature_ptr, steed_name, riding_m_ptr, 0);
222             msg_format(_("%sが朦朧としていてうまく動けない!", "You cannot control stunned %s!"), steed_name);
223             can_move = FALSE;
224             disturb(creature_ptr, FALSE, TRUE);
225         }
226     }
227
228     if (!can_move) {
229     } else if (!have_flag(f_ptr->flags, FF_MOVE) && have_flag(f_ptr->flags, FF_CAN_FLY) && !creature_ptr->levitation) {
230         msg_format(_("空を飛ばないと%sの上には行けない。", "You need to fly to go through the %s."), f_name + f_info[get_feat_mimic(g_ptr)].name);
231         free_turn(creature_ptr);
232         creature_ptr->running = 0;
233         can_move = FALSE;
234     } else if (have_flag(f_ptr->flags, FF_TREE) && !p_can_kill_walls) {
235         if ((creature_ptr->pclass != CLASS_RANGER) && !creature_ptr->levitation && (!creature_ptr->riding || !(riding_r_ptr->flags8 & RF8_WILD_WOOD)))
236             creature_ptr->energy_use *= 2;
237     } else if ((do_pickup != easy_disarm) && have_flag(f_ptr->flags, FF_DISARM) && !g_ptr->mimic) {
238         if (!trap_can_be_ignored(creature_ptr, g_ptr->feat)) {
239             (void)exe_disarm(creature_ptr, y, x, dir);
240             return;
241         }
242     } else if (!p_can_enter && !p_can_kill_walls) {
243         FEAT_IDX feat = get_feat_mimic(g_ptr);
244         feature_type *mimic_f_ptr = &f_info[feat];
245         concptr name = f_name + mimic_f_ptr->name;
246         can_move = FALSE;
247         if (!(g_ptr->info & CAVE_MARK) && !player_can_see_bold(creature_ptr, y, x)) {
248             if (boundary_floor(g_ptr, f_ptr, mimic_f_ptr))
249                 msg_print(_("それ以上先には進めないようだ。", "You feel you cannot go any more."));
250             else {
251 #ifdef JP
252                 msg_format("%sが行く手をはばんでいるようだ。", name);
253 #else
254                 msg_format("You feel %s %s blocking your way.", is_a_vowel(name[0]) ? "an" : "a", name);
255 #endif
256                 g_ptr->info |= (CAVE_MARK);
257                 lite_spot(creature_ptr, y, x);
258             }
259         } else {
260             if (boundary_floor(g_ptr, f_ptr, mimic_f_ptr)) {
261                 msg_print(_("それ以上先には進めない。", "You cannot go any more."));
262                 if (!(creature_ptr->confused || creature_ptr->stun || creature_ptr->image))
263                     free_turn(creature_ptr);
264             } else {
265                 if (easy_open && is_closed_door(creature_ptr, feat) && easy_open_door(creature_ptr, y, x))
266                     return;
267
268 #ifdef JP
269                 msg_format("%sが行く手をはばんでいる。", name);
270 #else
271                 msg_format("There is %s %s blocking your way.", is_a_vowel(name[0]) ? "an" : "a", name);
272 #endif
273                 if (!(creature_ptr->confused || creature_ptr->stun || creature_ptr->image))
274                     free_turn(creature_ptr);
275             }
276         }
277
278         disturb(creature_ptr, FALSE, TRUE);
279         if (!boundary_floor(g_ptr, f_ptr, mimic_f_ptr))
280             sound(SOUND_HITWALL);
281     }
282
283     if (can_move && !pattern_seq(creature_ptr, creature_ptr->y, creature_ptr->x, y, x)) {
284         if (!(creature_ptr->confused || creature_ptr->stun || creature_ptr->image))
285             free_turn(creature_ptr);
286
287         disturb(creature_ptr, FALSE, TRUE);
288         can_move = FALSE;
289     }
290
291     if (!can_move)
292         return;
293
294     if (creature_ptr->warning && (!process_warning(creature_ptr, x, y))) {
295         creature_ptr->energy_use = 25;
296         return;
297     }
298
299     if (do_past)
300         msg_format(_("%sを押し退けた。", "You push past %s."), m_name);
301
302     if (creature_ptr->wild_mode) {
303         if (ddy[dir] > 0)
304             creature_ptr->oldpy = 1;
305
306         if (ddy[dir] < 0)
307             creature_ptr->oldpy = MAX_HGT - 2;
308
309         if (ddy[dir] == 0)
310             creature_ptr->oldpy = MAX_HGT / 2;
311
312         if (ddx[dir] > 0)
313             creature_ptr->oldpx = 1;
314
315         if (ddx[dir] < 0)
316             creature_ptr->oldpx = MAX_WID - 2;
317
318         if (ddx[dir] == 0)
319             creature_ptr->oldpx = MAX_WID / 2;
320     }
321
322     if (p_can_kill_walls) {
323         cave_alter_feat(creature_ptr, y, x, FF_HURT_DISI);
324         creature_ptr->update |= PU_FLOW;
325     }
326
327     u32b mpe_mode = MPE_ENERGY_USE;
328     if (do_pickup != always_pickup)
329         mpe_mode |= MPE_DO_PICKUP;
330
331     if (break_trap)
332         mpe_mode |= MPE_BREAK_TRAP;
333
334     (void)move_player_effect(creature_ptr, y, x, mpe_mode);
335 }