OSDN Git Service

[Refactor] #40030 Separated monster-update.c/h from monster-process.c
[hengband/hengband.git] / src / monster-process.c
1 /*!
2  * @file monster-process.c
3  * @brief モンスターの特殊技能とターン経過処理 (移動等)/ Monster spells and movement for passaging a turn
4  * @date 2014/01/17
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  * This software may be copied and distributed for educational, research,\n
8  * and not for profit purposes provided that this copyright and statement\n
9  * are included in all such copies.  Other copyrights may also apply.\n
10  * 2014 Deskull rearranged comment for Doxygen.\n
11  * @details
12  * This file has several additions to it by Keldon Jones (keldon@umr.edu)
13  * to improve the general quality of the AI (version 0.1.1).
14  */
15
16 #include "angband.h"
17 #include "util.h"
18 #include "monster/monster-attack.h"
19 #include "monster/monster-object.h"
20 #include "monster/monster-move.h"
21 #include "monster/monster-util.h"
22 #include "monster/monster-update.h"
23 #include "monster/quantum-effect.h"
24
25 #include "cmd-dump.h"
26 #include "cmd-pet.h"
27 #include "creature.h"
28 #include "melee.h"
29 #include "spells.h"
30 #include "spells-floor.h"
31 #include "spells-summon.h"
32 #include "quest.h"
33 #include "avatar.h"
34 #include "realm-hex.h"
35 #include "feature.h"
36 #include "grid.h"
37 #include "player-move.h"
38 #include "monster-status.h"
39 #include "monster-spell.h"
40 #include "monster-process.h"
41 #include "monster-dist-offsets.h"
42 #include "monsterrace-hook.h"
43 #include "floor.h"
44 #include "files.h"
45 #include "view-mainwindow.h"
46
47 bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm);
48 void decide_enemy_approch_direction(player_type *target_ptr, MONSTER_IDX m_idx, int start, int plus, POSITION *y, POSITION *x);
49 bool decide_pet_approch_direction(player_type *target_ptr, monster_type *m_ptr, monster_type *t_ptr);
50
51 bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp);
52 coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONSTER_IDX m_idx, const POSITION *y_offsets, const POSITION *x_offsets, int d);
53
54 bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp);
55 void sweep_hiding_candidate(player_type *target_ptr, monster_type *m_ptr, const POSITION *y_offsets, const POSITION *x_offsets, coordinate_candidate *candidate);
56
57 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon);
58 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx);
59 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
60 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx);
61 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
62 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx);
63 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox);
64 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware);
65 bool random_walk(player_type *target_ptr, DIRECTION *mm, monster_type *m_ptr);
66 bool decide_pet_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx);
67 bool runaway_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
68 void escape_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, GAME_TEXT *m_name);
69 void process_special(player_type *target_ptr, MONSTER_IDX m_idx);
70 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware);
71 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware);
72
73 bool process_monster_movement(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, DIRECTION *mm, POSITION oy, POSITION ox, int *count);
74 bool process_post_dig_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
75 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
76
77 void sweep_monster_process(player_type *target_ptr);
78 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr);
79
80 /*!
81  * @brief モンスターが敵に接近するための方向を計算するメインルーチン
82  * Calculate the direction to the next enemy
83  * @param target_ptr プレーヤーへの参照ポインタ
84  * @param m_idx モンスターの参照ID
85  * @param mm 移動するべき方角IDを返す参照ポインタ
86  * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
87  */
88 bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm)
89 {
90         floor_type *floor_ptr = target_ptr->current_floor_ptr;
91         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
92
93         POSITION x = 0, y = 0;
94         if (target_ptr->riding_t_m_idx && player_bold(target_ptr, m_ptr->fy, m_ptr->fx))
95         {
96                 y = floor_ptr->m_list[target_ptr->riding_t_m_idx].fy;
97                 x = floor_ptr->m_list[target_ptr->riding_t_m_idx].fx;
98         }
99         else if (is_pet(m_ptr) && target_ptr->pet_t_m_idx)
100         {
101                 y = floor_ptr->m_list[target_ptr->pet_t_m_idx].fy;
102                 x = floor_ptr->m_list[target_ptr->pet_t_m_idx].fx;
103         }
104         else
105         {
106                 int start;
107                 int plus = 1;
108                 if (target_ptr->phase_out)
109                 {
110                         start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
111                         if (randint0(2)) plus = -1;
112                 }
113                 else
114                 {
115                         start = floor_ptr->m_max + 1;
116                 }
117
118                 decide_enemy_approch_direction(target_ptr, m_idx, start, plus, &y, &x);
119
120                 if ((x == 0) && (y == 0)) return FALSE;
121         }
122
123         x -= m_ptr->fx;
124         y -= m_ptr->fy;
125
126         store_enemy_approch_direction(mm, y, x);
127         return TRUE;
128 }
129
130
131 /*!
132  * @brief モンスターが敵に接近するための方向を決定する
133  * @param target_ptr プレーヤーへの参照ポインタ
134  * @param m_idx モンスターID
135  * @param start モンスターIDの開始
136  * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
137  * @param y モンスターの移動方向Y
138  * @param x モンスターの移動方向X
139  * @return なし
140  */
141 void decide_enemy_approch_direction(player_type *target_ptr, MONSTER_IDX m_idx, int start, int plus, POSITION *y, POSITION *x)
142 {
143         floor_type *floor_ptr = target_ptr->current_floor_ptr;
144         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
145         monster_race *r_ptr = &r_info[m_ptr->r_idx];
146         for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus)
147         {
148                 MONSTER_IDX dummy = (i % floor_ptr->m_max);
149                 if (dummy == 0) continue;
150
151                 MONSTER_IDX t_idx = dummy;
152                 monster_type *t_ptr;
153                 t_ptr = &floor_ptr->m_list[t_idx];
154                 if (t_ptr == m_ptr) continue;
155                 if (!monster_is_valid(t_ptr)) continue;
156                 if (decide_pet_approch_direction(target_ptr, m_ptr, t_ptr)) continue;
157                 if (!are_enemies(target_ptr, m_ptr, t_ptr)) continue;
158
159                 if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) ||
160                         ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)))
161                 {
162                         if (!in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
163                 }
164                 else
165                 {
166                         if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
167                 }
168
169                 *y = t_ptr->fy;
170                 *x = t_ptr->fx;
171                 return;
172         }
173 }
174
175
176 /*!
177  * @brief ペットが敵に接近するための方向を決定する
178  * @param target_ptr プレーヤーへの参照ポインタ
179  * @param m_ptr 移動を試みているモンスターへの参照ポインタ
180  * @param t_ptr 移動先モンスターへの参照ポインタ
181  * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
182  * @return ペットがモンスターに近づくならばTRUE
183  */
184 bool decide_pet_approch_direction(player_type *target_ptr, monster_type *m_ptr, monster_type *t_ptr)
185 {
186         monster_race *r_ptr = &r_info[m_ptr->r_idx];
187         if (!is_pet(m_ptr)) return FALSE;
188
189         if (target_ptr->pet_follow_distance < 0)
190         {
191                 if (t_ptr->cdis <= (0 - target_ptr->pet_follow_distance))
192                 {
193                         return TRUE;
194                 }
195         }
196         else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > target_ptr->pet_follow_distance))
197         {
198                 return TRUE;
199         }
200
201         return (r_ptr->aaf < t_ptr->cdis);
202 }
203
204
205 /*!
206  * @brief モンスターがプレイヤーから逃走するかどうかを返す /
207  * Returns whether a given monster will try to run from the player.
208  * @param m_idx 逃走するモンスターの参照ID
209  * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
210  * @details
211  * Monsters will attempt to avoid very powerful players.  See below.\n
212  *\n
213  * Because this function is called so often, little details are important\n
214  * for efficiency.  Like not using "mod" or "div" when possible.  And\n
215  * attempting to check the conditions in an optimal order.  Note that\n
216  * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
217  *\n
218  * Note that this function is responsible for about one to five percent\n
219  * of the processor use in normal conditions...\n
220  */
221 static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
222 {
223         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
224         monster_race *r_ptr = &r_info[m_ptr->r_idx];
225
226         if (is_pet(m_ptr))
227         {
228                 return ((target_ptr->pet_follow_distance < 0) &&
229                         (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
230         }
231
232         if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
233         if (MON_MONFEAR(m_ptr)) return TRUE;
234         if (m_ptr->cdis <= 5) return FALSE;
235
236         PLAYER_LEVEL p_lev = target_ptr->lev;
237         DEPTH m_lev = r_ptr->level + (m_idx & 0x08) + 25;
238         if (m_lev > p_lev + 4) return FALSE;
239         if (m_lev + 4 <= p_lev) return TRUE;
240
241         HIT_POINT p_chp = target_ptr->chp;
242         HIT_POINT p_mhp = target_ptr->mhp;
243         HIT_POINT m_chp = m_ptr->hp;
244         HIT_POINT m_mhp = m_ptr->maxhp;
245         u32b p_val = (p_lev * p_mhp) + (p_chp << 2);
246         u32b m_val = (m_lev * m_mhp) + (m_chp << 2);
247         if (p_val * m_mhp > m_val * p_mhp) return TRUE;
248
249         return FALSE;
250 }
251
252
253 /*!
254  * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
255  * Search spell castable grid
256  * @param target_ptr プレーヤーへの参照ポインタ
257  * @param m_idx モンスターの参照ID
258  * @param yp 適したマスのY座標を返す参照ポインタ
259  * @param xp 適したマスのX座標を返す参照ポインタ
260  * @return 有効なマスがあった場合TRUEを返す
261  */
262 static bool get_moves_aux2(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
263 {
264         floor_type *floor_ptr = target_ptr->current_floor_ptr;
265         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
266         monster_race *r_ptr = &r_info[m_ptr->r_idx];
267
268         POSITION y1 = m_ptr->fy;
269         POSITION x1 = m_ptr->fx;
270
271         if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
272
273         int now_cost = floor_ptr->grid_array[y1][x1].cost;
274         if (now_cost == 0) now_cost = 999;
275
276         bool can_open_door = FALSE;
277         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
278         {
279                 can_open_door = TRUE;
280         }
281
282         int best = 999;
283         for (int i = 7; i >= 0; i--)
284         {
285                 POSITION y = y1 + ddy_ddd[i];
286                 POSITION x = x1 + ddx_ddd[i];
287                 if (!in_bounds2(floor_ptr, y, x)) continue;
288                 if (player_bold(target_ptr, y, x)) return FALSE;
289
290                 grid_type *g_ptr;
291                 g_ptr = &floor_ptr->grid_array[y][x];
292                 int cost = g_ptr->cost;
293                 if (!(((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) || ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding))))
294                 {
295                         if (cost == 0) continue;
296                         if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat)) continue;
297                 }
298
299                 if (cost == 0) cost = 998;
300
301                 if (now_cost < cost) continue;
302                 if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x)) continue;
303                 if (best < cost) continue;
304
305                 best = cost;
306                 *yp = y1 + ddy_ddd[i];
307                 *xp = x1 + ddx_ddd[i];
308         }
309
310         if (best == 999) return FALSE;
311
312         return TRUE;
313 }
314
315
316 /*!
317  * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
318  * Choose the "best" direction for "flowing"
319  * @param m_idx モンスターの参照ID
320  * @param yp 移動先のマスのY座標を返す参照ポインタ
321  * @param xp 移動先のマスのX座標を返す参照ポインタ
322  * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
323  * @return 有効なマスがあった場合TRUEを返す
324  * @details
325  * Note that ghosts and rock-eaters are never allowed to "flow",\n
326  * since they should move directly towards the player.\n
327  *\n
328  * Prefer "non-diagonal" directions, but twiddle them a little\n
329  * to angle slightly towards the player's actual location.\n
330  *\n
331  * Allow very perceptive monsters to track old "spoor" left by\n
332  * previous locations occupied by the player.  This will tend\n
333  * to have monsters end up either near the player or on a grid\n
334  * recently occupied by the player (and left via "teleport").\n
335  *\n
336  * Note that if "smell" is turned on, all monsters get vicious.\n
337  *\n
338  * Also note that teleporting away from a location will cause\n
339  * the monsters who were chasing you to converge on that location\n
340  * as long as you are still near enough to "annoy" them without\n
341  * being close enough to chase directly.  I have no idea what will\n
342  * happen if you combine "smell" with low "aaf" values.\n
343  */
344 static bool get_moves_aux(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
345 {
346         grid_type *g_ptr;
347         floor_type *floor_ptr = target_ptr->current_floor_ptr;
348         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
349         monster_race *r_ptr = &r_info[m_ptr->r_idx];
350
351         if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
352                 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
353                 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
354         {
355                 if (get_moves_aux2(target_ptr, m_idx, yp, xp)) return TRUE;
356         }
357
358         if (no_flow) return FALSE;
359         if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return FALSE;
360         if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return FALSE;
361
362         POSITION y1 = m_ptr->fy;
363         POSITION x1 = m_ptr->fx;
364         if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return FALSE;
365
366         g_ptr = &floor_ptr->grid_array[y1][x1];
367
368         int best;
369         bool use_scent = FALSE;
370         if (g_ptr->cost)
371         {
372                 best = 999;
373         }
374         else if (g_ptr->when)
375         {
376                 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return FALSE;
377
378                 use_scent = TRUE;
379                 best = 0;
380         }
381         else
382         {
383                 return FALSE;
384         }
385
386         for (int i = 7; i >= 0; i--)
387         {
388                 POSITION y = y1 + ddy_ddd[i];
389                 POSITION x = x1 + ddx_ddd[i];
390
391                 if (!in_bounds2(floor_ptr, y, x)) continue;
392
393                 g_ptr = &floor_ptr->grid_array[y][x];
394                 if (use_scent)
395                 {
396                         int when = g_ptr->when;
397                         if (best > when) continue;
398
399                         best = when;
400                 }
401                 else
402                 {
403                         int cost;
404                         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
405                         {
406                                 cost = g_ptr->dist;
407                         }
408                         else
409                         {
410                                 cost = g_ptr->cost;
411                         }
412
413                         if ((cost == 0) || (best < cost)) continue;
414
415                         best = cost;
416                 }
417
418                 *yp = target_ptr->y + 16 * ddy_ddd[i];
419                 *xp = target_ptr->x + 16 * ddx_ddd[i];
420         }
421
422         if (best == 999 || best == 0) return FALSE;
423
424         return TRUE;
425 }
426
427
428 /*!
429  * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
430  * Provide a location to flee to, but give the player a wide berth.
431  * @param m_idx モンスターの参照ID
432  * @param yp 移動先のマスのY座標を返す参照ポインタ
433  * @param xp 移動先のマスのX座標を返す参照ポインタ
434  * @return 有効なマスがあった場合TRUEを返す
435  * @details
436  * A monster may wish to flee to a location that is behind the player,\n
437  * but instead of heading directly for it, the monster should "swerve"\n
438  * around the player so that he has a smaller chance of getting hit.\n
439  */
440 static bool get_fear_moves_aux(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
441 {
442         POSITION gy = 0, gx = 0;
443
444         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
445         POSITION fy = m_ptr->fy;
446         POSITION fx = m_ptr->fx;
447
448         POSITION y1 = fy - (*yp);
449         POSITION x1 = fx - (*xp);
450
451         int score = -1;
452         for (int i = 7; i >= 0; i--)
453         {
454                 POSITION y = fy + ddy_ddd[i];
455                 POSITION x = fx + ddx_ddd[i];
456                 if (!in_bounds2(floor_ptr, y, x)) continue;
457
458                 POSITION dis = distance(y, x, y1, x1);
459                 POSITION s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
460                 if (s < 0) s = 0;
461
462                 if (s < score) continue;
463
464                 score = s;
465                 gy = y;
466                 gx = x;
467         }
468
469         if (score == -1) return FALSE;
470
471         (*yp) = fy - gy;
472         (*xp) = fx - gx;
473
474         return TRUE;
475 }
476
477
478 /*!
479  * @brief モンスターが逃げ込める安全な地点を返す /
480  * Choose a "safe" location near a monster for it to run toward.
481  * @param target_ptr プレーヤーへの参照ポインタ
482  * @param m_idx モンスターの参照ID
483  * @param yp 移動先のマスのY座標を返す参照ポインタ
484  * @param xp 移動先のマスのX座標を返す参照ポインタ
485  * @return 有効なマスがあった場合TRUEを返す
486  * @details
487  * A location is "safe" if it can be reached quickly and the player\n
488  * is not able to fire into it (it isn't a "clean shot").  So, this will\n
489  * cause monsters to "duck" behind walls.  Hopefully, monsters will also\n
490  * try to run towards corridor openings if they are in a room.\n
491  *\n
492  * This function may take lots of CPU time if lots of monsters are\n
493  * fleeing.\n
494  *\n
495  * Return TRUE if a safe location is available.\n
496  */
497 bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
498 {
499         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
500         for (POSITION d = 1; d < 10; d++)
501         {
502                 const POSITION *y_offsets;
503                 y_offsets = dist_offsets_y[d];
504
505                 const POSITION *x_offsets;
506                 x_offsets = dist_offsets_x[d];
507
508                 coordinate_candidate candidate = sweep_safe_coordinate(target_ptr, m_idx, y_offsets, x_offsets, d);
509
510                 if (candidate.gdis <= 0) continue;
511
512                 *yp = m_ptr->fy - candidate.gy;
513                 *xp = m_ptr->fx - candidate.gx;
514
515                 return TRUE;
516         }
517
518         return FALSE;
519 }
520
521
522 /*!
523  * @brief モンスターが逃げ込める地点を走査する
524  * @param target_ptr プレーヤーへの参照ポインタ
525  * @param m_idx モンスターID
526  * @param y_offsets
527  * @param x_offsets
528  * @param d モンスターがいる地点からの距離
529  * @return 逃げ込める地点の候補地
530  */
531 coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONSTER_IDX m_idx, const POSITION *y_offsets, const POSITION *x_offsets, int d)
532 {
533         coordinate_candidate candidate = init_coordinate_candidate();
534         floor_type *floor_ptr = target_ptr->current_floor_ptr;
535         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
536         for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
537                 dx != 0 || dy != 0;
538                 i++, dx = x_offsets[i], dy = y_offsets[i])
539         {
540                 POSITION y = m_ptr->fy + dy;
541                 POSITION x = m_ptr->fx + dx;
542                 if (!in_bounds(floor_ptr, y, x)) continue;
543
544                 grid_type *g_ptr;
545                 g_ptr = &floor_ptr->grid_array[y][x];
546
547                 BIT_FLAGS16 riding_mode = (m_idx == target_ptr->riding) ? CEM_RIDING : 0;
548                 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], riding_mode))
549                         continue;
550
551                 if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
552                 {
553                         if (g_ptr->dist == 0) continue;
554                         if (g_ptr->dist > floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist + 2 * d) continue;
555                 }
556
557                 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x)) continue;
558
559                 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
560                 if (dis <= candidate.gdis) continue;
561
562                 candidate.gy = y;
563                 candidate.gx = x;
564                 candidate.gdis = dis;
565         }
566
567         return candidate;
568 }
569
570
571 /*!
572  * @brief モンスターが隠れ潜める地点を返す /
573  * Choose a good hiding place near a monster for it to run toward.
574  * @param target_ptr プレーヤーへの参照ポインタ
575  * @param m_idx モンスターの参照ID
576  * @param yp 移動先のマスのY座標を返す参照ポインタ
577  * @param xp 移動先のマスのX座標を返す参照ポインタ
578  * @return 有効なマスがあった場合TRUEを返す
579  * @details
580  * Pack monsters will use this to "ambush" the player and lure him out\n
581  * of corridors into open space so they can swarm him.\n
582  *\n
583  * Return TRUE if a good location is available.\n
584  */
585 bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
586 {
587         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
588         coordinate_candidate candidate = init_coordinate_candidate();
589         candidate.gdis = 999;
590
591         for (POSITION d = 1; d < 10; d++)
592         {
593                 const POSITION *y_offsets;
594                 y_offsets = dist_offsets_y[d];
595
596                 const POSITION *x_offsets;
597                 x_offsets = dist_offsets_x[d];
598
599                 sweep_hiding_candidate(target_ptr, m_ptr, y_offsets, x_offsets, &candidate);
600                 if (candidate.gdis >= 999) continue;
601
602                 *yp = m_ptr->fy - candidate.gy;
603                 *xp = m_ptr->fx - candidate.gx;
604                 return TRUE;
605         }
606
607         return FALSE;
608 }
609
610
611 /*!
612  * @brief モンスターが隠れられる地点を走査する
613  * @param target_ptr プレーヤーへの参照ポインタ
614  * @param m_idx モンスターID
615  * @param y_offsets
616  * @param x_offsets
617  * @param candidate 隠れられる地点の候補地
618  * @return なし
619  */
620 void sweep_hiding_candidate(player_type *target_ptr, monster_type *m_ptr, const POSITION *y_offsets, const POSITION *x_offsets, coordinate_candidate *candidate)
621 {
622         monster_race *r_ptr = &r_info[m_ptr->r_idx];
623         for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
624                 dx != 0 || dy != 0;
625                 i++, dx = x_offsets[i], dy = y_offsets[i])
626         {
627                 POSITION y = m_ptr->fy + dy;
628                 POSITION x = m_ptr->fx + dx;
629                 if (!in_bounds(target_ptr->current_floor_ptr, y, x)) continue;
630                 if (!monster_can_enter(target_ptr, y, x, r_ptr, 0)) continue;
631                 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x) && clean_shot(target_ptr, m_ptr->fy, m_ptr->fx, y, x, FALSE))
632                         continue;
633
634                 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
635                 if (dis < candidate->gdis && dis >= 2)
636                 {
637                         candidate->gy = y;
638                         candidate->gx = x;
639                         candidate->gdis = dis;
640                 }
641         }
642 }
643
644
645 /*!
646  * todo 分割したいが条件が多すぎて適切な関数名と詳細処理を追いきれない……
647  * @brief モンスターの移動方向を返す /
648  * Choose "logical" directions for monster movement
649  * @param target_ptr プレーヤーへの参照ポインタ
650  * @param m_idx モンスターの参照ID
651  * @param mm 移動方向を返す方向IDの参照ポインタ
652  * @return 有効方向があった場合TRUEを返す
653  */
654 static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
655 {
656         floor_type *floor_ptr = target_ptr->current_floor_ptr;
657         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
658         monster_race *r_ptr = &r_info[m_ptr->r_idx];
659         POSITION y = 0, x = 0;
660         POSITION y2 = target_ptr->y;
661         POSITION x2 = target_ptr->x;
662         bool done = FALSE;
663         bool will_run = mon_will_run(target_ptr, m_idx);
664         grid_type *g_ptr;
665         bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) != 0) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2);
666         bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) != 0) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall);
667
668         if (!will_run && m_ptr->target_y)
669         {
670                 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
671                 if ((t_m_idx > 0) &&
672                         are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) &&
673                         los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
674                         projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
675                 {
676                         y = m_ptr->fy - m_ptr->target_y;
677                         x = m_ptr->fx - m_ptr->target_x;
678                         done = TRUE;
679                 }
680         }
681
682         if (!done && !will_run && is_hostile(m_ptr) &&
683                 (r_ptr->flags1 & RF1_FRIENDS) &&
684                 ((los(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x) && projectable(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x)) ||
685                 (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
686         {
687                 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
688                         !(r_ptr->flags2 & RF2_KILL_WALL))
689                 {
690                         int room = 0;
691                         for (int i = 0; i < 8; i++)
692                         {
693                                 int xx = target_ptr->x + ddx_ddd[i];
694                                 int yy = target_ptr->y + ddy_ddd[i];
695
696                                 if (!in_bounds2(floor_ptr, yy, xx)) continue;
697
698                                 g_ptr = &floor_ptr->grid_array[yy][xx];
699                                 if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0))
700                                 {
701                                         room++;
702                                 }
703                         }
704
705                         if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
706                         if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
707
708                         if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
709                                 (target_ptr->mhp + target_ptr->msp))
710                         {
711                                 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
712                         }
713                 }
714
715                 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
716                 {
717                         for (int i = 0; i < 8; i++)
718                         {
719                                 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
720                                 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
721                                 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
722                                 {
723                                         y2 = target_ptr->y;
724                                         x2 = target_ptr->x;
725                                         break;
726                                 }
727
728                                 if (!in_bounds2(floor_ptr, y2, x2)) continue;
729                                 if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0)) continue;
730
731                                 break;
732                         }
733
734                         y = m_ptr->fy - y2;
735                         x = m_ptr->fx - x2;
736                         done = TRUE;
737                 }
738         }
739
740         if (!done)
741         {
742                 (void)get_moves_aux(target_ptr, m_idx, &y2, &x2, no_flow);
743                 y = m_ptr->fy - y2;
744                 x = m_ptr->fx - x2;
745         }
746
747         if (is_pet(m_ptr) && will_run)
748         {
749                 y = (-y), x = (-x);
750         }
751         else
752         {
753                 if (!done && will_run)
754                 {
755                         int tmp_x = (-x);
756                         int tmp_y = (-y);
757                         if (find_safety(target_ptr, m_idx, &y, &x) && !no_flow)
758                         {
759                                 if (get_fear_moves_aux(target_ptr->current_floor_ptr, m_idx, &y, &x))
760                                         done = TRUE;
761                         }
762
763                         if (!done)
764                         {
765                                 y = tmp_y;
766                                 x = tmp_x;
767                         }
768                 }
769         }
770
771         if (!x && !y) return FALSE;
772
773         store_moves_val(mm, y, x);
774         return TRUE;
775 }
776
777
778 /*!
779  * @brief モンスター単体の1ターン行動処理メインルーチン /
780  * Process a monster
781  * @param target_ptr プレーヤーへの参照ポインタ
782  * @param m_idx 行動モンスターの参照ID
783  * @return なし
784  * @details
785  * The monster is known to be within 100 grids of the player\n
786  *\n
787  * In several cases, we directly update the monster lore\n
788  *\n
789  * Note that a monster is only allowed to "reproduce" if there\n
790  * are a limited number of "reproducing" monsters on the current\n
791  * level.  This should prevent the level from being "swamped" by\n
792  * reproducing monsters.  It also allows a large mass of mice to\n
793  * prevent a louse from multiplying, but this is a small price to\n
794  * pay for a simple multiplication method.\n
795  *\n
796  * XXX Monster fear is slightly odd, in particular, monsters will\n
797  * fixate on opening a door even if they cannot open it.  Actually,\n
798  * the same thing happens to normal monsters when they hit a door\n
799  *\n
800  * In addition, monsters which *cannot* open or bash\n
801  * down a door will still stand there trying to open it...\n
802  *\n
803  * XXX Technically, need to check for monster in the way\n
804  * combined with that monster being in a wall (or door?)\n
805  *\n
806  * A "direction" of "5" means "pick a random direction".\n
807  */
808 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
809 {
810         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
811         monster_race *r_ptr = &r_info[m_ptr->r_idx];
812         turn_flags tmp_flags;
813         turn_flags *turn_flags_ptr = init_turn_flags(target_ptr->riding, m_idx, &tmp_flags);
814         turn_flags_ptr->see_m = is_seen(m_ptr);
815
816         decide_drop_from_monster(target_ptr, m_idx, turn_flags_ptr->is_riding_mon);
817         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
818         {
819                 choose_new_monster(target_ptr, m_idx, FALSE, 0);
820                 r_ptr = &r_info[m_ptr->r_idx];
821         }
822
823         turn_flags_ptr->aware = process_stealth(target_ptr, m_idx);
824         if (vanish_summoned_children(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
825         if (process_quantum_effect(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
826         if (explode_grenade(target_ptr, m_idx)) return;
827         if (runaway_monster(target_ptr, turn_flags_ptr, m_idx)) return;
828
829         awake_monster(target_ptr, m_idx);
830         if (MON_STUNNED(m_ptr))
831         {
832                 if (one_in_(2)) return;
833         }
834
835         if (turn_flags_ptr->is_riding_mon)
836         {
837                 target_ptr->update |= (PU_BONUS);
838         }
839
840         process_angar(target_ptr, m_idx, turn_flags_ptr->see_m);
841
842         POSITION oy = m_ptr->fy;
843         POSITION ox = m_ptr->fx;
844         if (decide_monster_multiplication(target_ptr, m_idx, oy, ox)) return;
845
846         process_special(target_ptr, m_idx);
847         process_speak_sound(target_ptr, m_idx, oy, ox, turn_flags_ptr->aware);
848         if (cast_spell(target_ptr, m_idx, turn_flags_ptr->aware)) return;
849
850         DIRECTION mm[8];
851         mm[0] = mm[1] = mm[2] = mm[3] = 0;
852         mm[4] = mm[5] = mm[6] = mm[7] = 0;
853
854         if (!decide_monster_movement_direction(target_ptr, mm, m_idx, turn_flags_ptr->aware)) return;
855
856         int count = 0;
857         if (!process_monster_movement(target_ptr, turn_flags_ptr, m_idx, mm, oy, ox, &count)) return;
858
859         /*
860          *  Forward movements failed, but now received LOS attack!
861          *  Try to flow by smell.
862          */
863         if (target_ptr->no_flowed && count > 2 && m_ptr->target_y)
864                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
865
866         if (!turn_flags_ptr->do_turn && !turn_flags_ptr->do_move && !MON_MONFEAR(m_ptr) && !turn_flags_ptr->is_riding_mon && turn_flags_ptr->aware)
867         {
868                 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
869                 {
870                         if (make_attack_spell(m_idx, target_ptr)) return;
871                 }
872         }
873
874         update_player_type(target_ptr, turn_flags_ptr, r_ptr);
875         update_monster_race_flags(target_ptr, turn_flags_ptr, m_ptr);
876
877         if (!process_monster_fear(target_ptr, turn_flags_ptr, m_idx)) return;
878
879         if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
880 }
881
882
883 /*!
884  * @brief 超隠密処理
885  * @param target_ptr プレーヤーへの参照ポインタ
886  * @param m_idx モンスターID
887  * @return モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
888  */
889 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx)
890 {
891         if ((target_ptr->special_defense & NINJA_S_STEALTH) == 0) return TRUE;
892
893         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
894         monster_race *r_ptr = &r_info[m_ptr->r_idx];
895         int tmp = target_ptr->lev * 6 + (target_ptr->skill_stl + 10) * 4;
896         if (target_ptr->monlite) tmp /= 3;
897         if (target_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
898         if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10)) tmp /= 3;
899         return (randint0(tmp) <= (r_ptr->level + 20));
900 }
901
902
903 /*!
904  * @brief 死亡したモンスターが乗馬中のモンスターだった場合に落馬処理を行う
905  * @param target_ptr プレーヤーへの参照ポインタ
906  * @param m_idx モンスターID
907  * @param is_riding_mon 騎乗中であればTRUE
908  * @return なし
909  */
910 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon)
911 {
912         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
913         monster_race *r_ptr = &r_info[m_ptr->r_idx];
914         if (!is_riding_mon || ((r_ptr->flags7 & RF7_RIDING) != 0)) return;
915
916         if (rakuba(target_ptr, 0, TRUE))
917         {
918 #ifdef JP
919                 msg_print("地面に落とされた。");
920 #else
921                 GAME_TEXT m_name[MAX_NLEN];
922                 monster_desc(target_ptr, m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
923                 msg_format("You have fallen from %s.", m_name);
924 #endif
925         }
926 }
927
928
929 /*!
930  * @brief 召喚の親元が消滅した時、子供も消滅させる
931  * @param target_ptr プレーヤーへの参照ポインタ
932  * @param m_idx モンスターID
933  * @param see_m モンスターが視界内にいたらTRUE
934  * @return 召喚モンスターが消滅したらTRUE
935  */
936 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
937 {
938         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
939         if ((m_ptr->parent_m_idx == 0) || (target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx > 0))
940                 return FALSE;
941
942         if (see_m)
943         {
944                 GAME_TEXT m_name[MAX_NLEN];
945                 monster_desc(target_ptr, m_name, m_ptr, 0);
946                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
947         }
948
949         if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
950         {
951                 GAME_TEXT m_name[MAX_NLEN];
952                 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
953                 exe_write_diary(target_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
954         }
955
956         delete_monster_idx(target_ptr, m_idx);
957         return TRUE;
958 }
959
960
961 /*!
962  * @brief 寝ているモンスターの起床を判定する
963  * @param target_ptr プレーヤーへの参照ポインタ
964  * @param m_idx モンスターID
965  * @return なし
966  */
967 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx)
968 {
969         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
970         monster_race *r_ptr = &r_info[m_ptr->r_idx];
971         if (!MON_CSLEEP(m_ptr)) return;
972         if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
973
974         (void)set_monster_csleep(target_ptr, m_idx, 0);
975         if (m_ptr->ml)
976         {
977                 GAME_TEXT m_name[MAX_NLEN];
978                 monster_desc(target_ptr, m_name, m_ptr, 0);
979                 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
980         }
981
982         if (is_original_ap_and_seen(target_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
983         {
984                 r_ptr->r_wake++;
985         }
986 }
987
988
989 /*!
990  * @brief モンスターの怒り状態を判定する (起こっていたら敵に回す)
991  * @param target_ptr プレーヤーへの参照ポインタ
992  * @param m_idx モンスターID
993  * @param see_m モンスターが視界内にいたらTRUE
994  * @return なし
995  */
996 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
997 {
998         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
999         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1000         bool gets_angry = FALSE;
1001         if (is_friendly(m_ptr) && (target_ptr->cursed & TRC_AGGRAVATE))
1002                 gets_angry = TRUE;
1003
1004         if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
1005                 monster_has_hostile_align(target_ptr, NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
1006         {
1007                 gets_angry = TRUE;
1008         }
1009
1010         if (target_ptr->phase_out || !gets_angry) return;
1011
1012         if (is_pet(m_ptr) || see_m)
1013         {
1014                 GAME_TEXT m_name[MAX_NLEN];
1015                 monster_desc(target_ptr, m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
1016                 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
1017         }
1018
1019         set_hostile(target_ptr, m_ptr);
1020 }
1021
1022
1023 /*!
1024  * @brief 手榴弾の爆発処理
1025  * @param target_ptr プレーヤーへの参照ポインタ
1026  * @param m_idx モンスターID
1027  * @return 爆死したらTRUE
1028  */
1029 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx)
1030 {
1031         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1032         if (m_ptr->r_idx != MON_GRENADE) return FALSE;
1033
1034         bool fear, dead;
1035         mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1036         return dead;
1037 }
1038
1039
1040 /*!
1041  * @brief モンスター依存の特別な行動を取らせる
1042  * @param target_ptr プレーヤーへの参照ポインタ
1043  * @param m_idx モンスターID
1044  * @return なし
1045  */
1046 void process_special(player_type *target_ptr, MONSTER_IDX m_idx)
1047 {
1048         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1049         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1050         if ((r_ptr->a_ability_flags2 & RF6_SPECIAL) == 0) return;
1051         if (m_ptr->r_idx != MON_OHMU) return;
1052         if (target_ptr->current_floor_ptr->inside_arena || target_ptr->phase_out) return;
1053         if ((r_ptr->freq_spell == 0) || !(randint1(100) <= r_ptr->freq_spell)) return;
1054
1055         int count = 0;
1056         DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1057         BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
1058
1059         for (int k = 0; k < A_MAX; k++)
1060         {
1061                 if (summon_specific(target_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
1062                 {
1063                         if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
1064                 }
1065         }
1066
1067         if (count && is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
1068 }
1069
1070
1071 /*!
1072  * @brief モンスターを喋らせたり足音を立てたりする
1073  * @param target_ptr プレーヤーへの参照ポインタ
1074  * @param m_idx モンスターID
1075  * @param oy モンスターが元々いたY座標
1076  * @param ox モンスターが元々いたX座標
1077  * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1078  * @return なし
1079  */
1080 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware)
1081 {
1082         if (target_ptr->phase_out) return;
1083
1084         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1085         monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1086         if (m_ptr->ap_r_idx == MON_CYBER &&
1087                 one_in_(CYBERNOISE) &&
1088                 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
1089         {
1090                 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1091                 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
1092         }
1093
1094         if (((ap_r_ptr->flags2 & RF2_CAN_SPEAK) == 0) || !aware ||
1095                 !one_in_(SPEAK_CHANCE) ||
1096                 !player_has_los_bold(target_ptr, oy, ox) ||
1097                 !projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
1098                 return;
1099
1100         GAME_TEXT m_name[MAX_NLEN];
1101         char monmessage[1024];
1102         concptr filename;
1103
1104         if (m_ptr->ml)
1105                 monster_desc(target_ptr, m_name, m_ptr, 0);
1106         else
1107                 strcpy(m_name, _("それ", "It"));
1108
1109         if (MON_MONFEAR(m_ptr))
1110                 filename = _("monfear_j.txt", "monfear.txt");
1111         else if (is_pet(m_ptr))
1112                 filename = _("monpet_j.txt", "monpet.txt");
1113         else if (is_friendly(m_ptr))
1114                 filename = _("monfrien_j.txt", "monfrien.txt");
1115         else
1116                 filename = _("monspeak_j.txt", "monspeak.txt");
1117
1118         if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
1119         {
1120                 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
1121         }
1122 }
1123
1124
1125 /*!
1126  * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
1127  * @param target_ptr プレーヤーへの参照ポインタ
1128  * @param m_idx モンスターID
1129  * @param oy 分裂元モンスターのY座標
1130  * @param ox 分裂元モンスターのX座標
1131  * @return 実際に分裂したらTRUEを返す
1132  */
1133 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
1134 {
1135         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1136         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1137         if (((r_ptr->flags2 & RF2_MULTIPLY) == 0) || (target_ptr->current_floor_ptr->num_repro >= MAX_REPRO))
1138                 return FALSE;
1139
1140         int k = 0;
1141         for (POSITION y = oy - 1; y <= oy + 1; y++)
1142         {
1143                 for (POSITION x = ox - 1; x <= ox + 1; x++)
1144                 {
1145                         if (!in_bounds2(target_ptr->current_floor_ptr, y, x)) continue;
1146                         if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
1147                 }
1148         }
1149
1150         if (multiply_barrier(target_ptr, m_idx)) k = 8;
1151
1152         if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
1153         {
1154                 if (multiply_monster(target_ptr, m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
1155                 {
1156                         if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(target_ptr, m_ptr))
1157                         {
1158                                 r_ptr->r_flags2 |= (RF2_MULTIPLY);
1159                         }
1160
1161                         return TRUE;
1162                 }
1163         }
1164
1165         return FALSE;
1166 }
1167
1168
1169 /*!
1170  * @brief モンスターの移動パターンを決定する
1171  * @param target_ptr プレーヤーへの参照ポインタ
1172  * @param mm 移動方向
1173  * @param m_idx モンスターID
1174  * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1175  * @return 移動先が存在すればTRUE
1176  */
1177 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware)
1178 {
1179         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1180         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1181
1182         if (MON_CONFUSED(m_ptr) || !aware)
1183         {
1184                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1185                 return TRUE;
1186         }
1187
1188         if (random_walk(target_ptr, mm, m_ptr)) return TRUE;
1189
1190         if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
1191         {
1192                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1193                 return TRUE;
1194         }
1195
1196         if (decide_pet_movement_direction(target_ptr, mm, m_idx)) return TRUE;
1197
1198         if (!is_hostile(m_ptr))
1199         {
1200                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1201                 get_enemy_dir(target_ptr, m_idx, mm);
1202                 return TRUE;
1203         }
1204
1205         if (!get_moves(target_ptr, m_idx, mm)) return FALSE;
1206
1207         return TRUE;
1208 }
1209
1210
1211 /*!
1212  * todo ↓のように書いたが、"5"とはもしかして「その場に留まる」という意味か?
1213  * @brief 不規則歩行フラグを持つモンスターの移動方向をその確率に基づいて決定する
1214  * @param target_ptr プレーヤーへの参照ポインタ
1215  * @param mm 移動方向
1216  * @param m_ptr モンスターへの参照ポインタ
1217  * @return 不規則な方向へ歩くことになったらTRUE
1218  */
1219 bool random_walk(player_type *target_ptr, DIRECTION *mm, monster_type *m_ptr)
1220 {
1221         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1222         if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) && (randint0(100) < 75))
1223         {
1224                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
1225
1226                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1227                 return TRUE;
1228         }
1229
1230         if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 50))
1231         {
1232                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_50;
1233
1234                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1235                 return TRUE;
1236         }
1237
1238         if ((r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 25))
1239         {
1240                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
1241
1242                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1243                 return TRUE;
1244         }
1245
1246         return FALSE;
1247 }
1248
1249
1250 /*!
1251  * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
1252  * @param target_ptr プレーヤーへの参照ポインタ
1253  * @param mm 移動方向
1254  * @param m_idx モンスターID
1255  * @return モンスターがペットであればTRUE
1256  */
1257 bool decide_pet_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx)
1258 {
1259         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1260         if (!is_pet(m_ptr)) return FALSE;
1261
1262         bool avoid = ((target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
1263         bool lonely = (!avoid && (m_ptr->cdis > target_ptr->pet_follow_distance));
1264         bool distant = (m_ptr->cdis > PET_SEEK_DIST);
1265         mm[0] = mm[1] = mm[2] = mm[3] = 5;
1266         if (get_enemy_dir(target_ptr, m_idx, mm)) return TRUE;
1267         if (!avoid && !lonely && !distant) return TRUE;
1268
1269         POSITION dis = target_ptr->pet_follow_distance;
1270         if (target_ptr->pet_follow_distance > PET_SEEK_DIST)
1271         {
1272                 target_ptr->pet_follow_distance = PET_SEEK_DIST;
1273         }
1274
1275         (void)get_moves(target_ptr, m_idx, mm);
1276         target_ptr->pet_follow_distance = (s16b)dis;
1277         return TRUE;
1278 }
1279
1280
1281 /*!
1282  * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
1283  * @param target_ptr プレーヤーへの参照ポインタ
1284  * @param m_idx モンスターID
1285  * @param is_riding_mon 騎乗状態ならばTRUE
1286  * @param see_m モンスターが視界内にいたらTRUE
1287  * @return モンスターがフロアから消えたらTRUE
1288  */
1289 bool runaway_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
1290 {
1291         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1292         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1293         bool can_runaway = is_pet(m_ptr) || is_friendly(m_ptr);
1294         can_runaway &= ((r_ptr->flags1 & RF1_UNIQUE) != 0) || ((r_ptr->flags7 & RF7_NAZGUL) != 0);
1295         can_runaway &= !target_ptr->phase_out;
1296         if (!can_runaway) return FALSE;
1297
1298         static int riding_pinch = 0;
1299
1300         if (m_ptr->hp >= m_ptr->maxhp / 3)
1301         {
1302                 /* Reset the counter */
1303                 if (turn_flags_ptr->is_riding_mon) riding_pinch = 0;
1304
1305                 return FALSE;
1306         }
1307
1308         GAME_TEXT m_name[MAX_NLEN];
1309         monster_desc(target_ptr, m_name, m_ptr, 0);
1310         if (turn_flags_ptr->is_riding_mon && riding_pinch < 2)
1311         {
1312                 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
1313                         "%^s seems to be in so much pain and tries to escape from your restriction."), m_name);
1314                 riding_pinch++;
1315                 disturb(target_ptr, TRUE, TRUE);
1316                 return FALSE;
1317         }
1318
1319         escape_monster(target_ptr, turn_flags_ptr, m_ptr, m_name);
1320         check_quest_completion(target_ptr, m_ptr);
1321         delete_monster_idx(target_ptr, m_idx);
1322         return TRUE;
1323 }
1324
1325
1326 /*!
1327  * @brief HPが1/3未満になった有効的なユニークモンスターの逃走処理を行う
1328  * @param target_ptr プレーヤーへの参照ポインタ
1329  * @param is_riding_mon 騎乗状態ならばTRUE
1330  * @param m_ptr モンスターへの参照ポインタ
1331  * @param m_name モンスター名称
1332  * @param see_m モンスターが視界内にいたらTRUE
1333  * @return なし
1334  */
1335 void escape_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, GAME_TEXT *m_name)
1336 {
1337         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1338         if (turn_flags_ptr->is_riding_mon)
1339         {
1340                 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
1341                 if (rakuba(target_ptr, -1, FALSE))
1342                 {
1343                         msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
1344                 }
1345         }
1346
1347         if (turn_flags_ptr->see_m)
1348         {
1349                 if ((r_ptr->flags2 & RF2_CAN_SPEAK) && (m_ptr->r_idx != MON_GRIP) && (m_ptr->r_idx != MON_WOLF) && (m_ptr->r_idx != MON_FANG) &&
1350                         player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) && projectable(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x))
1351                 {
1352                         msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
1353                 }
1354
1355                 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s reads a scroll of teleport level."), m_name);
1356                 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
1357         }
1358
1359         if (turn_flags_ptr->is_riding_mon && rakuba(target_ptr, -1, FALSE))
1360         {
1361                 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
1362         }
1363 }
1364
1365
1366 /*!
1367  * @brief モンスターに魔法を試行させる
1368  * @param target_ptr プレーヤーへの参照ポインタ
1369  * @param m_idx モンスターID
1370  * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1371  * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
1372  */
1373 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware)
1374 {
1375         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1376         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1377         if ((r_ptr->freq_spell == 0) || (randint1(100) > r_ptr->freq_spell))
1378                 return FALSE;
1379
1380         bool counterattack = FALSE;
1381         if (m_ptr->target_y)
1382         {
1383                 MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
1384                 if (t_m_idx && are_enemies(target_ptr, m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
1385                         projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1386                 {
1387                         counterattack = TRUE;
1388                 }
1389         }
1390
1391         if (counterattack)
1392         {
1393                 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
1394                 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
1395         }
1396         else
1397         {
1398                 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
1399                 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
1400         }
1401
1402         return FALSE;
1403 }
1404
1405
1406 /*!
1407  * todo 少し長いが、これといってブロックとしてまとまった部分もないので暫定でこのままとする
1408  * @brief モンスターの移動に関するメインルーチン
1409  * @param target_ptr プレーヤーへの参照ポインタ
1410  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1411  * @param m_idx モンスターID
1412  * @param mm モンスターの移動方向
1413  * @param oy 移動前の、モンスターのY座標
1414  * @param ox 移動前の、モンスターのX座標
1415  * @param count 移動回数 (のはず todo)
1416  * @return 移動が阻害される何か (ドア等)があったらFALSE
1417  */
1418 bool process_monster_movement(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, DIRECTION *mm, POSITION oy, POSITION ox, int *count)
1419 {
1420         for (int i = 0; mm[i]; i++)
1421         {
1422                 int d = mm[i];
1423                 if (d == 5) d = ddd[randint0(8)];
1424
1425                 POSITION ny = oy + ddy[d];
1426                 POSITION nx = ox + ddx[d];
1427                 if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx)) continue;
1428
1429                 grid_type *g_ptr;
1430                 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1431                 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1432                 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1433                 bool can_cross = monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0);
1434
1435                 if (!process_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx, can_cross))
1436                 {
1437                         if (!process_door(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1438                                 return FALSE;
1439                 }
1440
1441                 if (!process_protection_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1442                 {
1443                         if (!process_explosive_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1444                                 return FALSE;
1445                 }
1446
1447                 exe_monster_attack_to_player(target_ptr, turn_flags_ptr, m_idx, ny, nx);
1448                 if (process_monster_attack_to_monster(target_ptr, turn_flags_ptr, m_idx, g_ptr, can_cross)) return FALSE;
1449
1450                 if (turn_flags_ptr->is_riding_mon)
1451                 {
1452                         if (!target_ptr->riding_ryoute && !MON_MONFEAR(&target_ptr->current_floor_ptr->m_list[target_ptr->riding])) turn_flags_ptr->do_move = FALSE;
1453                 }
1454
1455                 if (!process_post_dig_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx)) return FALSE;
1456
1457                 if (turn_flags_ptr->must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
1458                 {
1459                         if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0))
1460                                 turn_flags_ptr->do_move = FALSE;
1461                 }
1462
1463                 if (turn_flags_ptr->do_move && !can_cross && !turn_flags_ptr->did_kill_wall && !turn_flags_ptr->did_bash_door)
1464                         turn_flags_ptr->do_move = FALSE;
1465
1466                 if (turn_flags_ptr->do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
1467                 {
1468                         if (is_original_ap_and_seen(target_ptr, m_ptr))
1469                                 r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
1470
1471                         turn_flags_ptr->do_move = FALSE;
1472                 }
1473
1474                 if (!turn_flags_ptr->do_move)
1475                 {
1476                         if (turn_flags_ptr->do_turn) break;
1477
1478                         continue;
1479                 }
1480
1481                 turn_flags_ptr->do_turn = TRUE;
1482                 feature_type *f_ptr;
1483                 f_ptr = &f_info[g_ptr->feat];
1484                 if (have_flag(f_ptr->flags, FF_TREE))
1485                 {
1486                         if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
1487                         {
1488                                 m_ptr->energy_need += ENERGY_NEED();
1489                         }
1490                 }
1491
1492                 if (!update_riding_monster(target_ptr, turn_flags_ptr, m_idx, oy, ox, ny, nx)) break;
1493
1494                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1495                 if (m_ptr->ml &&
1496                         (disturb_move ||
1497                         (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
1498                                 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
1499                 {
1500                         if (is_hostile(m_ptr))
1501                                 disturb(target_ptr, FALSE, TRUE);
1502                 }
1503
1504                 bool is_takable_or_killable = g_ptr->o_idx > 0;
1505                 is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) != 0;
1506
1507                 bool is_pickup_items = (target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) != 0;
1508                 is_pickup_items &= (r_ptr->flags2 & RF2_TAKE_ITEM) != 0;
1509
1510                 is_takable_or_killable &= !is_pet(m_ptr) || is_pickup_items;
1511                 if (!is_takable_or_killable)
1512                 {
1513                         if (turn_flags_ptr->do_turn) break;
1514
1515                         continue;
1516                 }
1517
1518                 update_object_by_monster_movement(target_ptr, turn_flags_ptr, m_idx, ny, nx);
1519                 if (turn_flags_ptr->do_turn) break;
1520
1521                 (*count)++;
1522         }
1523
1524         return TRUE;
1525 }
1526
1527
1528 /*!
1529  * @brief モンスターの恐怖状態を処理する
1530  * @param target_ptr プレーヤーへの参照ポインタ
1531  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1532  * @param m_idx モンスターID
1533  * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1534  * @return モンスターが戦いを決意したらTRUE
1535  */
1536 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
1537 {
1538         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1539         bool is_battle_determined = !turn_flags_ptr->do_turn && !turn_flags_ptr->do_move && MON_MONFEAR(m_ptr) && turn_flags_ptr->aware;
1540         if (!is_battle_determined) return FALSE;
1541
1542         (void)set_monster_monfear(target_ptr, m_idx, 0);
1543         if (!turn_flags_ptr->see_m) return TRUE;
1544
1545         GAME_TEXT m_name[MAX_NLEN];
1546         monster_desc(target_ptr, m_name, m_ptr, 0);
1547         msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
1548         return TRUE;
1549 }
1550
1551
1552 /*!
1553  * @brief 全モンスターのターン管理メインルーチン /
1554  * Process all the "live" monsters, once per game turn.
1555  * @return なし
1556  * @details
1557  * During each game current game turn, we scan through the list of all the "live" monsters,\n
1558  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
1559  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
1560  *\n
1561  * Note that monsters can never move in the monster array (except when the\n
1562  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
1563  *\n
1564  * This function is responsible for at least half of the processor time\n
1565  * on a normal system with a "normal" amount of monsters and a player doing\n
1566  * normal things.\n
1567  *\n
1568  * When the player is resting, virtually 90% of the processor time is spent\n
1569  * in this function, and its children, "process_monster()" and "make_move()".\n
1570  *\n
1571  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
1572  * especially when the player is running.\n
1573  *\n
1574  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
1575  * monsters while they are still being "born".  A monster is "fresh" only\n
1576  * during the game turn in which it is created, and we use the "hack_m_idx" to\n
1577  * determine if the monster is yet to be processed during the game turn.\n
1578  *\n
1579  * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
1580  * move before any "nasty" monsters get to use their spell attacks.\n
1581  *\n
1582  * Note that when the "knowledge" about the currently tracked monster\n
1583  * changes (flags, attacks, spells), we induce a redraw of the monster\n
1584  * recall window.\n
1585  */
1586 void process_monsters(player_type *target_ptr)
1587 {
1588         old_race_flags tmp_flags;
1589         old_race_flags *old_race_flags_ptr = init_old_race_flags(&tmp_flags);
1590
1591         floor_type *floor_ptr = target_ptr->current_floor_ptr;
1592         floor_ptr->monster_noise = FALSE;
1593
1594         MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
1595         save_old_race_flags(target_ptr->monster_race_idx, old_race_flags_ptr);
1596         sweep_monster_process(target_ptr);
1597
1598         hack_m_idx = 0;
1599         if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
1600                 return;
1601
1602         update_player_window(target_ptr, old_race_flags_ptr);
1603 }
1604
1605
1606 /*!
1607  * @brief フロア内のモンスターについてターン終了時の処理を繰り返す
1608  * @param target_ptr プレーヤーへの参照ポインタ
1609  */
1610 void sweep_monster_process(player_type *target_ptr)
1611 {
1612         floor_type *floor_ptr = target_ptr->current_floor_ptr;
1613         for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--)
1614         {
1615                 monster_type *m_ptr;
1616                 m_ptr = &floor_ptr->m_list[i];
1617
1618                 if (target_ptr->leaving) return;
1619                 if (!monster_is_valid(m_ptr)) continue;
1620                 if (target_ptr->wild_mode) continue;
1621
1622                 if (m_ptr->mflag & MFLAG_BORN)
1623                 {
1624                         m_ptr->mflag &= ~(MFLAG_BORN);
1625                         continue;
1626                 }
1627
1628                 if (m_ptr->cdis >= AAF_LIMIT) continue;
1629                 if (!decide_process_continue(target_ptr, m_ptr)) continue;
1630
1631                 SPEED speed = (target_ptr->riding == i) ? target_ptr->pspeed : decide_monster_speed(m_ptr);
1632                 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
1633                 if (m_ptr->energy_need > 0) continue;
1634
1635                 m_ptr->energy_need += ENERGY_NEED();
1636                 hack_m_idx = i;
1637                 process_monster(target_ptr, i);
1638                 reset_target(m_ptr);
1639
1640                 if (target_ptr->no_flowed && one_in_(3))
1641                         m_ptr->mflag2 |= MFLAG2_NOFLOW;
1642
1643                 if (!target_ptr->playing || target_ptr->is_dead) return;
1644                 if (target_ptr->leaving) return;
1645         }
1646 }
1647
1648
1649 /*!
1650  * @brief 後続のモンスター処理が必要かどうか判定する (要調査)
1651  * @param target_ptr プレーヤーへの参照ポインタ
1652  * @param m_ptr モンスターへの参照ポインタ
1653  * @return 後続処理が必要ならTRUE
1654  */
1655 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr)
1656 {
1657         monster_race *r_ptr;
1658         r_ptr = &r_info[m_ptr->r_idx];
1659         if (!target_ptr->no_flowed)
1660         {
1661                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
1662         }
1663
1664         if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
1665                 return TRUE;
1666
1667         if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) &&
1668                 (player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) || (target_ptr->cursed & TRC_AGGRAVATE)))
1669                 return TRUE;
1670
1671         if (m_ptr->target_y)
1672                 return TRUE;
1673
1674         return FALSE;
1675 }