OSDN Git Service

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