OSDN Git Service

Merge branch 'develop' into feature/Remove-Dependency-Player-Status
[hengbandforosx/hengbandosx.git] / src / action / run-execution.cpp
1 /*!
2  * @file run-execution.cpp
3  * @brief プレイヤーの走行処理実装
4  */
5
6 #include "action/run-execution.h"
7 #include "action/movement-execution.h"
8 #include "core/disturbance.h"
9 #include "floor/cave.h"
10 #include "floor/floor-util.h"
11 #include "floor/geometry.h"
12 #include "game-option/disturbance-options.h"
13 #include "grid/feature.h"
14 #include "grid/grid.h"
15 #include "main/sound-definitions-table.h"
16 #include "main/sound-of-music.h"
17 #include "object/object-mark-types.h"
18 #include "player/player-status-flags.h"
19 #include "player/player-status.h"
20 #include "system/floor-type-definition.h"
21 #include "system/monster-type-definition.h"
22 #include "system/object-type-definition.h"
23 #include "system/player-type-definition.h"
24 #include "util/bit-flags-calculator.h"
25 #include "view/display-messages.h"
26
27 bool ignore_avoid_run;
28
29 /* Allow quick "cycling" through the legal directions */
30 byte cycle[MAX_RUN_CYCLES] = { 1, 2, 3, 6, 9, 8, 7, 4, 1, 2, 3, 6, 9, 8, 7, 4, 1 };
31
32 /* Map each direction into the "middle" of the "cycle[]" array */
33 byte chome[MAX_RUN_CHOME] = { 0, 8, 9, 10, 7, 0, 11, 6, 5, 4 };
34
35 /* The direction we are running */
36 static DIRECTION find_current;
37
38 /* The direction we came from */
39 static DIRECTION find_prevdir;
40
41 static bool find_openarea;
42
43 /* We are looking for a break */
44 static bool find_breakright;
45 static bool find_breakleft;
46
47 /*!
48  * @brief ダッシュ移動処理中、移動先のマスが既知の壁かどうかを判定する /
49  * Hack -- Check for a "known wall" (see below)
50  * @param creature_ptr  プレーヤーへの参照ポインタ
51  * @param dir 想定する移動方向ID
52  * @param y 移動元のY座標
53  * @param x 移動元のX座標
54  * @return 移動先が既知の壁ならばTRUE
55  */
56 static bool see_wall(player_type *creature_ptr, DIRECTION dir, POSITION y, POSITION x)
57 {
58     y += ddy[dir];
59     x += ddx[dir];
60     floor_type *floor_ptr = creature_ptr->current_floor_ptr;
61     if (!in_bounds2(floor_ptr, y, x))
62         return FALSE;
63
64     grid_type *g_ptr;
65     g_ptr = &floor_ptr->grid_array[y][x];
66     if (!(g_ptr->info & CAVE_MARK))
67         return FALSE;
68
69     s16b feat = get_feat_mimic(g_ptr);
70     feature_type *f_ptr = &f_info[feat];
71     if (!player_can_enter(creature_ptr, feat, 0))
72         return !has_flag(f_ptr->flags, FF_DOOR);
73
74     if (has_flag(f_ptr->flags, FF_AVOID_RUN) && !ignore_avoid_run)
75         return TRUE;
76
77     if (!has_flag(f_ptr->flags, FF_MOVE) && !has_flag(f_ptr->flags, FF_CAN_FLY))
78         return !has_flag(f_ptr->flags, FF_DOOR);
79
80     return FALSE;
81 }
82
83 /*!
84  * @brief ダッシュ処理の導入 /
85  * Initialize the running algorithm for a new direction.
86  * @param creature_ptr  プレーヤーへの参照ポインタ
87  * @param dir 導入の移動先
88  * @details
89  * Diagonal Corridor -- allow diaginal entry into corridors.\n
90  *\n
91  * Blunt Corridor -- If there is a wall two spaces ahead and\n
92  * we seem to be in a corridor, then force a turn into the side\n
93  * corridor, must be moving straight into a corridor here. ???\n
94  *\n
95  * Diagonal Corridor    Blunt Corridor (?)\n
96  *       \# \#                  \#\n
97  *       \#x\#                  \@x\#\n
98  *       \@\@p.                  p\n
99  */
100 static void run_init(player_type *creature_ptr, DIRECTION dir)
101 {
102     find_current = dir;
103     find_prevdir = dir;
104     find_openarea = TRUE;
105     find_breakright = find_breakleft = FALSE;
106     bool deepleft = FALSE;
107     bool deepright = FALSE;
108     bool shortright = FALSE;
109     bool shortleft = FALSE;
110     creature_ptr->run_py = creature_ptr->y;
111     creature_ptr->run_px = creature_ptr->x;
112     int row = creature_ptr->y + ddy[dir];
113     int col = creature_ptr->x + ddx[dir];
114     ignore_avoid_run = cave_has_flag_bold(creature_ptr->current_floor_ptr, row, col, FF_AVOID_RUN);
115     int i = chome[dir];
116     if (see_wall(creature_ptr, cycle[i + 1], creature_ptr->y, creature_ptr->x)) {
117         find_breakleft = TRUE;
118         shortleft = TRUE;
119     } else if (see_wall(creature_ptr, cycle[i + 1], row, col)) {
120         find_breakleft = TRUE;
121         deepleft = TRUE;
122     }
123
124     if (see_wall(creature_ptr, cycle[i - 1], creature_ptr->y, creature_ptr->x)) {
125         find_breakright = TRUE;
126         shortright = TRUE;
127     } else if (see_wall(creature_ptr, cycle[i - 1], row, col)) {
128         find_breakright = TRUE;
129         deepright = TRUE;
130     }
131
132     if (!find_breakleft || !find_breakright)
133         return;
134
135     find_openarea = FALSE;
136     if (dir & 0x01) {
137         if (deepleft && !deepright) {
138             find_prevdir = cycle[i - 1];
139         } else if (deepright && !deepleft) {
140             find_prevdir = cycle[i + 1];
141         }
142
143         return;
144     }
145
146     if (!see_wall(creature_ptr, cycle[i], row, col))
147         return;
148
149     if (shortleft && !shortright) {
150         find_prevdir = cycle[i - 2];
151     } else if (shortright && !shortleft) {
152         find_prevdir = cycle[i + 2];
153     }
154 }
155
156 /*!
157  * @brief ダッシュ移動処理中、移動先のマスか未知の地形かどうかを判定する /
158  * Hack -- Check for an "unknown corner" (see below)
159  * @param creature_ptr  プレーヤーへの参照ポインタ
160  * @param dir 想定する移動方向ID
161  * @param y 移動元のY座標
162  * @param x 移動元のX座標
163  * @return 移動先が未知の地形ならばTRUE
164  */
165 static bool see_nothing(player_type *creature_ptr, DIRECTION dir, POSITION y, POSITION x)
166 {
167     y += ddy[dir];
168     x += ddx[dir];
169
170     floor_type *floor_ptr = creature_ptr->current_floor_ptr;
171     if (!in_bounds2(floor_ptr, y, x))
172         return TRUE;
173
174     if (floor_ptr->grid_array[y][x].info & (CAVE_MARK))
175         return FALSE;
176
177     if (player_can_see_bold(creature_ptr, y, x))
178         return FALSE;
179
180     return TRUE;
181 }
182
183 /*!
184  * @brief ダッシュ移動が継続できるかどうかの判定 /
185  * Update the current "run" path
186  * @param creature_ptr  プレーヤーへの参照ポインタ
187  * @return 立ち止まるべき条件が満たされたらTRUE
188  * ダッシュ移動が継続できるならばTRUEを返す。
189  * Return TRUE if the running should be stopped
190  */
191 static bool run_test(player_type *creature_ptr)
192 {
193     DIRECTION prev_dir = find_prevdir;
194     int max = (prev_dir & 0x01) + 1;
195     floor_type *floor_ptr = creature_ptr->current_floor_ptr;
196     if ((disturb_trap_detect || alert_trap_detect) && creature_ptr->dtrap && !(floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].info & CAVE_IN_DETECT)) {
197         creature_ptr->dtrap = FALSE;
198         if (!(floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].info & CAVE_UNSAFE)) {
199             if (alert_trap_detect) {
200                 msg_print(_("* 注意:この先はトラップの感知範囲外です! *", "*Leaving trap detect region!*"));
201             }
202
203             if (disturb_trap_detect) {
204                 /* Break Run */
205                 return TRUE;
206             }
207         }
208     }
209
210     DIRECTION check_dir = 0;
211     int option = 0, option2 = 0;
212     for (int i = -max; i <= max; i++) {
213         OBJECT_IDX this_o_idx, next_o_idx = 0;
214         DIRECTION new_dir = cycle[chome[prev_dir] + i];
215         int row = creature_ptr->y + ddy[new_dir];
216         int col = creature_ptr->x + ddx[new_dir];
217         grid_type *g_ptr;
218         g_ptr = &floor_ptr->grid_array[row][col];
219         FEAT_IDX feat = get_feat_mimic(g_ptr);
220         feature_type *f_ptr;
221         f_ptr = &f_info[feat];
222         if (g_ptr->m_idx) {
223             monster_type *m_ptr = &floor_ptr->m_list[g_ptr->m_idx];
224             if (m_ptr->ml)
225                 return TRUE;
226         }
227
228         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx) {
229             object_type *o_ptr;
230             o_ptr = &floor_ptr->o_list[this_o_idx];
231             next_o_idx = o_ptr->next_o_idx;
232             if (o_ptr->marked & OM_FOUND)
233                 return TRUE;
234         }
235
236         bool inv = TRUE;
237         if (g_ptr->info & (CAVE_MARK)) {
238             bool notice = has_flag(f_ptr->flags, FF_NOTICE);
239             if (notice && has_flag(f_ptr->flags, FF_MOVE)) {
240                 if (find_ignore_doors && has_flag(f_ptr->flags, FF_DOOR) && has_flag(f_ptr->flags, FF_CLOSE)) {
241                     notice = FALSE;
242                 } else if (find_ignore_stairs && has_flag(f_ptr->flags, FF_STAIRS)) {
243                     notice = FALSE;
244                 } else if (has_flag(f_ptr->flags, FF_LAVA) && (has_immune_fire(creature_ptr) || is_invuln(creature_ptr))) {
245                     notice = FALSE;
246                 } else if (has_flag(f_ptr->flags, FF_WATER) && has_flag(f_ptr->flags, FF_DEEP)
247                     && (creature_ptr->levitation || creature_ptr->can_swim || (calc_inventory_weight(creature_ptr) <= calc_weight_limit(creature_ptr)))) {
248                     notice = FALSE;
249                 }
250             }
251
252             if (notice)
253                 return TRUE;
254
255             inv = FALSE;
256         }
257
258         if (!inv && see_wall(creature_ptr, 0, row, col)) {
259             if (find_openarea) {
260                 if (i < 0) {
261                     find_breakright = TRUE;
262                 } else if (i > 0) {
263                     find_breakleft = TRUE;
264                 }
265             }
266
267             continue;
268         }
269
270         if (find_openarea)
271             continue;
272
273         if (!option) {
274             option = new_dir;
275             continue;
276         }
277
278         if (option2)
279             return TRUE;
280
281         if (option != cycle[chome[prev_dir] + i - 1])
282             return TRUE;
283
284         if (new_dir & 0x01) {
285             check_dir = cycle[chome[prev_dir] + i - 2];
286             option2 = new_dir;
287             continue;
288         }
289
290         check_dir = cycle[chome[prev_dir] + i + 1];
291         option2 = option;
292         option = new_dir;
293     }
294
295     if (find_openarea) {
296         for (int i = -max; i < 0; i++) {
297             if (!see_wall(creature_ptr, cycle[chome[prev_dir] + i], creature_ptr->y, creature_ptr->x)) {
298                 if (find_breakright)
299                     return TRUE;
300             } else {
301                 if (find_breakleft)
302                     return TRUE;
303             }
304         }
305
306         for (int i = max; i > 0; i--) {
307             if (!see_wall(creature_ptr, cycle[chome[prev_dir] + i], creature_ptr->y, creature_ptr->x)) {
308                 if (find_breakleft)
309                     return TRUE;
310             } else {
311                 if (find_breakright)
312                     return TRUE;
313             }
314         }
315
316         return see_wall(creature_ptr, find_current, creature_ptr->y, creature_ptr->x);
317     }
318
319     if (!option)
320         return TRUE;
321
322     if (!option2) {
323         find_current = option;
324         find_prevdir = option;
325         return see_wall(creature_ptr, find_current, creature_ptr->y, creature_ptr->x);
326     } else if (!find_cut) {
327         find_current = option;
328         find_prevdir = option2;
329         return see_wall(creature_ptr, find_current, creature_ptr->y, creature_ptr->x);
330     }
331
332     int row = creature_ptr->y + ddy[option];
333     int col = creature_ptr->x + ddx[option];
334     if (!see_wall(creature_ptr, option, row, col) || !see_wall(creature_ptr, check_dir, row, col)) {
335         if (see_nothing(creature_ptr, option, row, col) && see_nothing(creature_ptr, option2, row, col)) {
336             find_current = option;
337             find_prevdir = option2;
338             return see_wall(creature_ptr, find_current, creature_ptr->y, creature_ptr->x);
339         }
340
341         return TRUE;
342     }
343
344     if (find_cut) {
345         find_current = option2;
346         find_prevdir = option2;
347         return see_wall(creature_ptr, find_current, creature_ptr->y, creature_ptr->x);
348     }
349
350     find_current = option;
351     find_prevdir = option2;
352     return see_wall(creature_ptr, find_current, creature_ptr->y, creature_ptr->x);
353 }
354
355 /*!
356  * @brief 継続的なダッシュ処理 /
357  * Take one step along the current "run" path
358  * @param creature_ptr  プレーヤーへの参照ポインタ
359  * @param dir 移動を試みる方向ID
360  * @return なし
361  */
362 void run_step(player_type *creature_ptr, DIRECTION dir)
363 {
364     if (dir) {
365         ignore_avoid_run = TRUE;
366         if (see_wall(creature_ptr, dir, creature_ptr->y, creature_ptr->x)) {
367             sound(SOUND_HITWALL);
368             msg_print(_("その方向には走れません。", "You cannot run in that direction."));
369             disturb(creature_ptr, FALSE, FALSE);
370             return;
371         }
372
373         run_init(creature_ptr, dir);
374     } else {
375         if (run_test(creature_ptr)) {
376             disturb(creature_ptr, FALSE, FALSE);
377             return;
378         }
379     }
380
381     if (--creature_ptr->running <= 0)
382         return;
383
384     take_turn(creature_ptr, 100);
385     exe_movement(creature_ptr, find_current, FALSE, FALSE);
386     if (player_bold(creature_ptr, creature_ptr->run_py, creature_ptr->run_px)) {
387         creature_ptr->run_py = 0;
388         creature_ptr->run_px = 0;
389         disturb(creature_ptr, FALSE, FALSE);
390     }
391 }