OSDN Git Service

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