OSDN Git Service

d990dd89115746a04b4588bf3b3c3ea2e2509784
[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-util.h"
21 #include "monster/quantum-effect.h"
22
23 #include "cmd-dump.h"
24 #include "cmd-pet.h"
25 #include "creature.h"
26 #include "melee.h"
27 #include "spells.h"
28 #include "spells-floor.h"
29 #include "spells-summon.h"
30 #include "quest.h"
31 #include "avatar.h"
32 #include "realm-hex.h"
33 #include "feature.h"
34 #include "grid.h"
35 #include "player-move.h"
36 #include "monster-status.h"
37 #include "monster-spell.h"
38 #include "monster-process.h"
39 #include "monster-dist-offsets.h"
40 #include "monsterrace-hook.h"
41 #include "dungeon.h"
42 #include "floor.h"
43 #include "files.h"
44 #include "view-mainwindow.h"
45
46 bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm);
47 void decide_enemy_approch_direction(player_type *target_ptr, MONSTER_IDX m_idx, int start, int plus, POSITION *y, POSITION *x);
48 bool decide_pet_approch_direction(player_type *target_ptr, monster_type *m_ptr, monster_type *t_ptr);
49
50 bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp);
51 coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONSTER_IDX m_idx, const POSITION *y_offsets, const POSITION *x_offsets, int d);
52
53 bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp);
54 void sweep_hiding_candidate(player_type *target_ptr, monster_type *m_ptr, const POSITION *y_offsets, const POSITION *x_offsets, coordinate_candidate *candidate);
55
56 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon);
57 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx);
58 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
59 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx);
60 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
61 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx);
62 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox);
63 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware);
64 bool random_walk(player_type *target_ptr, DIRECTION *mm, monster_type *m_ptr);
65 bool decide_pet_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx);
66 bool runaway_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
67 void escape_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, GAME_TEXT *m_name);
68 void process_special(player_type *target_ptr, MONSTER_IDX m_idx);
69 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware);
70 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware);
71
72 bool process_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx, bool can_cross);
73 bool process_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
74 bool bash_normal_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
75 void bash_glass_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, feature_type *f_ptr, bool may_bash);
76 bool process_protection_rune(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
77 bool process_explosive_rune(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
78
79 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);
80 bool process_post_dig_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
81 bool update_riding_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, POSITION ny, POSITION nx);
82
83 void update_player_type(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_race *r_ptr);
84 void update_monster_race_flags(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr);
85 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
86
87 void sweep_monster_process(player_type *target_ptr);
88 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr);
89 SPEED decide_monster_speed(player_type *target_ptr, monster_type *m_ptr, int monster_number);
90 void update_player_window(player_type *target_ptr, old_race_flags *old_race_flags_ptr);
91
92 /*!
93  * @brief モンスターが敵に接近するための方向を計算するメインルーチン
94  * Calculate the direction to the next enemy
95  * @param target_ptr プレーヤーへの参照ポインタ
96  * @param m_idx モンスターの参照ID
97  * @param mm 移動するべき方角IDを返す参照ポインタ
98  * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
99  */
100 bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm)
101 {
102         floor_type *floor_ptr = target_ptr->current_floor_ptr;
103         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
104
105         POSITION x = 0, y = 0;
106         if (target_ptr->riding_t_m_idx && player_bold(target_ptr, m_ptr->fy, m_ptr->fx))
107         {
108                 y = floor_ptr->m_list[target_ptr->riding_t_m_idx].fy;
109                 x = floor_ptr->m_list[target_ptr->riding_t_m_idx].fx;
110         }
111         else if (is_pet(m_ptr) && target_ptr->pet_t_m_idx)
112         {
113                 y = floor_ptr->m_list[target_ptr->pet_t_m_idx].fy;
114                 x = floor_ptr->m_list[target_ptr->pet_t_m_idx].fx;
115         }
116         else
117         {
118                 int start;
119                 int plus = 1;
120                 if (target_ptr->phase_out)
121                 {
122                         start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
123                         if (randint0(2)) plus = -1;
124                 }
125                 else
126                 {
127                         start = floor_ptr->m_max + 1;
128                 }
129
130                 decide_enemy_approch_direction(target_ptr, m_idx, start, plus, &y, &x);
131
132                 if ((x == 0) && (y == 0)) return FALSE;
133         }
134
135         x -= m_ptr->fx;
136         y -= m_ptr->fy;
137
138         store_enemy_approch_direction(mm, y, x);
139         return TRUE;
140 }
141
142
143 /*!
144  * @brief モンスターが敵に接近するための方向を決定する
145  * @param target_ptr プレーヤーへの参照ポインタ
146  * @param m_idx モンスターID
147  * @param start モンスターIDの開始
148  * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
149  * @param y モンスターの移動方向Y
150  * @param x モンスターの移動方向X
151  * @return なし
152  */
153 void decide_enemy_approch_direction(player_type *target_ptr, MONSTER_IDX m_idx, int start, int plus, POSITION *y, POSITION *x)
154 {
155         floor_type *floor_ptr = target_ptr->current_floor_ptr;
156         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
157         monster_race *r_ptr = &r_info[m_ptr->r_idx];
158         for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus)
159         {
160                 MONSTER_IDX dummy = (i % floor_ptr->m_max);
161                 if (dummy == 0) continue;
162
163                 MONSTER_IDX t_idx = dummy;
164                 monster_type *t_ptr;
165                 t_ptr = &floor_ptr->m_list[t_idx];
166                 if (t_ptr == m_ptr) continue;
167                 if (!monster_is_valid(t_ptr)) continue;
168                 if (decide_pet_approch_direction(target_ptr, m_ptr, t_ptr)) continue;
169                 if (!are_enemies(target_ptr, m_ptr, t_ptr)) continue;
170
171                 if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) ||
172                         ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)))
173                 {
174                         if (!in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
175                 }
176                 else
177                 {
178                         if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
179                 }
180
181                 *y = t_ptr->fy;
182                 *x = t_ptr->fx;
183                 return;
184         }
185 }
186
187
188 /*!
189  * @brief ペットが敵に接近するための方向を決定する
190  * @param target_ptr プレーヤーへの参照ポインタ
191  * @param m_ptr 移動を試みているモンスターへの参照ポインタ
192  * @param t_ptr 移動先モンスターへの参照ポインタ
193  * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
194  * @return ペットがモンスターに近づくならばTRUE
195  */
196 bool decide_pet_approch_direction(player_type *target_ptr, monster_type *m_ptr, monster_type *t_ptr)
197 {
198         monster_race *r_ptr = &r_info[m_ptr->r_idx];
199         if (!is_pet(m_ptr)) return FALSE;
200
201         if (target_ptr->pet_follow_distance < 0)
202         {
203                 if (t_ptr->cdis <= (0 - target_ptr->pet_follow_distance))
204                 {
205                         return TRUE;
206                 }
207         }
208         else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > target_ptr->pet_follow_distance))
209         {
210                 return TRUE;
211         }
212
213         return (r_ptr->aaf < t_ptr->cdis);
214 }
215
216
217 /*!
218  * @brief モンスターがプレイヤーから逃走するかどうかを返す /
219  * Returns whether a given monster will try to run from the player.
220  * @param m_idx 逃走するモンスターの参照ID
221  * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
222  * @details
223  * Monsters will attempt to avoid very powerful players.  See below.\n
224  *\n
225  * Because this function is called so often, little details are important\n
226  * for efficiency.  Like not using "mod" or "div" when possible.  And\n
227  * attempting to check the conditions in an optimal order.  Note that\n
228  * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
229  *\n
230  * Note that this function is responsible for about one to five percent\n
231  * of the processor use in normal conditions...\n
232  */
233 static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
234 {
235         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
236         monster_race *r_ptr = &r_info[m_ptr->r_idx];
237
238         if (is_pet(m_ptr))
239         {
240                 return ((target_ptr->pet_follow_distance < 0) &&
241                         (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
242         }
243
244         if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
245         if (MON_MONFEAR(m_ptr)) return TRUE;
246         if (m_ptr->cdis <= 5) return FALSE;
247
248         PLAYER_LEVEL p_lev = target_ptr->lev;
249         DEPTH m_lev = r_ptr->level + (m_idx & 0x08) + 25;
250         if (m_lev > p_lev + 4) return FALSE;
251         if (m_lev + 4 <= p_lev) return TRUE;
252
253         HIT_POINT p_chp = target_ptr->chp;
254         HIT_POINT p_mhp = target_ptr->mhp;
255         HIT_POINT m_chp = m_ptr->hp;
256         HIT_POINT m_mhp = m_ptr->maxhp;
257         u32b p_val = (p_lev * p_mhp) + (p_chp << 2);
258         u32b m_val = (m_lev * m_mhp) + (m_chp << 2);
259         if (p_val * m_mhp > m_val * p_mhp) return TRUE;
260
261         return FALSE;
262 }
263
264
265 /*!
266  * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
267  * Search spell castable grid
268  * @param target_ptr プレーヤーへの参照ポインタ
269  * @param m_idx モンスターの参照ID
270  * @param yp 適したマスのY座標を返す参照ポインタ
271  * @param xp 適したマスのX座標を返す参照ポインタ
272  * @return 有効なマスがあった場合TRUEを返す
273  */
274 static bool get_moves_aux2(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
275 {
276         floor_type *floor_ptr = target_ptr->current_floor_ptr;
277         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
278         monster_race *r_ptr = &r_info[m_ptr->r_idx];
279
280         POSITION y1 = m_ptr->fy;
281         POSITION x1 = m_ptr->fx;
282
283         if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
284
285         int now_cost = floor_ptr->grid_array[y1][x1].cost;
286         if (now_cost == 0) now_cost = 999;
287
288         bool can_open_door = FALSE;
289         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
290         {
291                 can_open_door = TRUE;
292         }
293
294         int best = 999;
295         for (int i = 7; i >= 0; i--)
296         {
297                 POSITION y = y1 + ddy_ddd[i];
298                 POSITION x = x1 + ddx_ddd[i];
299                 if (!in_bounds2(floor_ptr, y, x)) continue;
300                 if (player_bold(target_ptr, y, x)) return FALSE;
301
302                 grid_type *g_ptr;
303                 g_ptr = &floor_ptr->grid_array[y][x];
304                 int cost = g_ptr->cost;
305                 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))))
306                 {
307                         if (cost == 0) continue;
308                         if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat)) continue;
309                 }
310
311                 if (cost == 0) cost = 998;
312
313                 if (now_cost < cost) continue;
314                 if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x)) continue;
315                 if (best < cost) continue;
316
317                 best = cost;
318                 *yp = y1 + ddy_ddd[i];
319                 *xp = x1 + ddx_ddd[i];
320         }
321
322         if (best == 999) return FALSE;
323
324         return TRUE;
325 }
326
327
328 /*!
329  * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
330  * Choose the "best" direction for "flowing"
331  * @param m_idx モンスターの参照ID
332  * @param yp 移動先のマスのY座標を返す参照ポインタ
333  * @param xp 移動先のマスのX座標を返す参照ポインタ
334  * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
335  * @return 有効なマスがあった場合TRUEを返す
336  * @details
337  * Note that ghosts and rock-eaters are never allowed to "flow",\n
338  * since they should move directly towards the player.\n
339  *\n
340  * Prefer "non-diagonal" directions, but twiddle them a little\n
341  * to angle slightly towards the player's actual location.\n
342  *\n
343  * Allow very perceptive monsters to track old "spoor" left by\n
344  * previous locations occupied by the player.  This will tend\n
345  * to have monsters end up either near the player or on a grid\n
346  * recently occupied by the player (and left via "teleport").\n
347  *\n
348  * Note that if "smell" is turned on, all monsters get vicious.\n
349  *\n
350  * Also note that teleporting away from a location will cause\n
351  * the monsters who were chasing you to converge on that location\n
352  * as long as you are still near enough to "annoy" them without\n
353  * being close enough to chase directly.  I have no idea what will\n
354  * happen if you combine "smell" with low "aaf" values.\n
355  */
356 static bool get_moves_aux(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
357 {
358         grid_type *g_ptr;
359         floor_type *floor_ptr = target_ptr->current_floor_ptr;
360         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
361         monster_race *r_ptr = &r_info[m_ptr->r_idx];
362
363         if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
364                 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
365                 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
366         {
367                 if (get_moves_aux2(target_ptr, m_idx, yp, xp)) return TRUE;
368         }
369
370         if (no_flow) return FALSE;
371         if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return FALSE;
372         if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return FALSE;
373
374         POSITION y1 = m_ptr->fy;
375         POSITION x1 = m_ptr->fx;
376         if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return FALSE;
377
378         g_ptr = &floor_ptr->grid_array[y1][x1];
379
380         int best;
381         bool use_scent = FALSE;
382         if (g_ptr->cost)
383         {
384                 best = 999;
385         }
386         else if (g_ptr->when)
387         {
388                 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return FALSE;
389
390                 use_scent = TRUE;
391                 best = 0;
392         }
393         else
394         {
395                 return FALSE;
396         }
397
398         for (int i = 7; i >= 0; i--)
399         {
400                 POSITION y = y1 + ddy_ddd[i];
401                 POSITION x = x1 + ddx_ddd[i];
402
403                 if (!in_bounds2(floor_ptr, y, x)) continue;
404
405                 g_ptr = &floor_ptr->grid_array[y][x];
406                 if (use_scent)
407                 {
408                         int when = g_ptr->when;
409                         if (best > when) continue;
410
411                         best = when;
412                 }
413                 else
414                 {
415                         int cost;
416                         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
417                         {
418                                 cost = g_ptr->dist;
419                         }
420                         else
421                         {
422                                 cost = g_ptr->cost;
423                         }
424
425                         if ((cost == 0) || (best < cost)) continue;
426
427                         best = cost;
428                 }
429
430                 *yp = target_ptr->y + 16 * ddy_ddd[i];
431                 *xp = target_ptr->x + 16 * ddx_ddd[i];
432         }
433
434         if (best == 999 || best == 0) return FALSE;
435
436         return TRUE;
437 }
438
439
440 /*!
441  * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
442  * Provide a location to flee to, but give the player a wide berth.
443  * @param m_idx モンスターの参照ID
444  * @param yp 移動先のマスのY座標を返す参照ポインタ
445  * @param xp 移動先のマスのX座標を返す参照ポインタ
446  * @return 有効なマスがあった場合TRUEを返す
447  * @details
448  * A monster may wish to flee to a location that is behind the player,\n
449  * but instead of heading directly for it, the monster should "swerve"\n
450  * around the player so that he has a smaller chance of getting hit.\n
451  */
452 static bool get_fear_moves_aux(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
453 {
454         POSITION gy = 0, gx = 0;
455
456         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
457         POSITION fy = m_ptr->fy;
458         POSITION fx = m_ptr->fx;
459
460         POSITION y1 = fy - (*yp);
461         POSITION x1 = fx - (*xp);
462
463         int score = -1;
464         for (int i = 7; i >= 0; i--)
465         {
466                 POSITION y = fy + ddy_ddd[i];
467                 POSITION x = fx + ddx_ddd[i];
468                 if (!in_bounds2(floor_ptr, y, x)) continue;
469
470                 POSITION dis = distance(y, x, y1, x1);
471                 POSITION s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
472                 if (s < 0) s = 0;
473
474                 if (s < score) continue;
475
476                 score = s;
477                 gy = y;
478                 gx = x;
479         }
480
481         if (score == -1) return FALSE;
482
483         (*yp) = fy - gy;
484         (*xp) = fx - gx;
485
486         return TRUE;
487 }
488
489
490 /*!
491  * @brief モンスターが逃げ込める安全な地点を返す /
492  * Choose a "safe" location near a monster for it to run toward.
493  * @param target_ptr プレーヤーへの参照ポインタ
494  * @param m_idx モンスターの参照ID
495  * @param yp 移動先のマスのY座標を返す参照ポインタ
496  * @param xp 移動先のマスのX座標を返す参照ポインタ
497  * @return 有効なマスがあった場合TRUEを返す
498  * @details
499  * A location is "safe" if it can be reached quickly and the player\n
500  * is not able to fire into it (it isn't a "clean shot").  So, this will\n
501  * cause monsters to "duck" behind walls.  Hopefully, monsters will also\n
502  * try to run towards corridor openings if they are in a room.\n
503  *\n
504  * This function may take lots of CPU time if lots of monsters are\n
505  * fleeing.\n
506  *\n
507  * Return TRUE if a safe location is available.\n
508  */
509 bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
510 {
511         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
512         for (POSITION d = 1; d < 10; d++)
513         {
514                 const POSITION *y_offsets;
515                 y_offsets = dist_offsets_y[d];
516
517                 const POSITION *x_offsets;
518                 x_offsets = dist_offsets_x[d];
519
520                 coordinate_candidate candidate = sweep_safe_coordinate(target_ptr, m_idx, y_offsets, x_offsets, d);
521
522                 if (candidate.gdis <= 0) continue;
523
524                 *yp = m_ptr->fy - candidate.gy;
525                 *xp = m_ptr->fx - candidate.gx;
526
527                 return TRUE;
528         }
529
530         return FALSE;
531 }
532
533
534 /*!
535  * @brief モンスターが逃げ込める地点を走査する
536  * @param target_ptr プレーヤーへの参照ポインタ
537  * @param m_idx モンスターID
538  * @param y_offsets
539  * @param x_offsets
540  * @param d モンスターがいる地点からの距離
541  * @return 逃げ込める地点の候補地
542  */
543 coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONSTER_IDX m_idx, const POSITION *y_offsets, const POSITION *x_offsets, int d)
544 {
545         coordinate_candidate candidate = init_coordinate_candidate();
546         floor_type *floor_ptr = target_ptr->current_floor_ptr;
547         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
548         for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
549                 dx != 0 || dy != 0;
550                 i++, dx = x_offsets[i], dy = y_offsets[i])
551         {
552                 POSITION y = m_ptr->fy + dy;
553                 POSITION x = m_ptr->fx + dx;
554                 if (!in_bounds(floor_ptr, y, x)) continue;
555
556                 grid_type *g_ptr;
557                 g_ptr = &floor_ptr->grid_array[y][x];
558
559                 BIT_FLAGS16 riding_mode = (m_idx == target_ptr->riding) ? CEM_RIDING : 0;
560                 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], riding_mode))
561                         continue;
562
563                 if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
564                 {
565                         if (g_ptr->dist == 0) continue;
566                         if (g_ptr->dist > floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist + 2 * d) continue;
567                 }
568
569                 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x)) continue;
570
571                 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
572                 if (dis <= candidate.gdis) continue;
573
574                 candidate.gy = y;
575                 candidate.gx = x;
576                 candidate.gdis = dis;
577         }
578
579         return candidate;
580 }
581
582
583 /*!
584  * @brief モンスターが隠れ潜める地点を返す /
585  * Choose a good hiding place near a monster for it to run toward.
586  * @param target_ptr プレーヤーへの参照ポインタ
587  * @param m_idx モンスターの参照ID
588  * @param yp 移動先のマスのY座標を返す参照ポインタ
589  * @param xp 移動先のマスのX座標を返す参照ポインタ
590  * @return 有効なマスがあった場合TRUEを返す
591  * @details
592  * Pack monsters will use this to "ambush" the player and lure him out\n
593  * of corridors into open space so they can swarm him.\n
594  *\n
595  * Return TRUE if a good location is available.\n
596  */
597 bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
598 {
599         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
600         coordinate_candidate candidate = init_coordinate_candidate();
601         candidate.gdis = 999;
602
603         for (POSITION d = 1; d < 10; d++)
604         {
605                 const POSITION *y_offsets;
606                 y_offsets = dist_offsets_y[d];
607
608                 const POSITION *x_offsets;
609                 x_offsets = dist_offsets_x[d];
610
611                 sweep_hiding_candidate(target_ptr, m_ptr, y_offsets, x_offsets, &candidate);
612                 if (candidate.gdis >= 999) continue;
613
614                 *yp = m_ptr->fy - candidate.gy;
615                 *xp = m_ptr->fx - candidate.gx;
616                 return TRUE;
617         }
618
619         return FALSE;
620 }
621
622
623 /*!
624  * @brief モンスターが隠れられる地点を走査する
625  * @param target_ptr プレーヤーへの参照ポインタ
626  * @param m_idx モンスターID
627  * @param y_offsets
628  * @param x_offsets
629  * @param candidate 隠れられる地点の候補地
630  * @return なし
631  */
632 void sweep_hiding_candidate(player_type *target_ptr, monster_type *m_ptr, const POSITION *y_offsets, const POSITION *x_offsets, coordinate_candidate *candidate)
633 {
634         monster_race *r_ptr = &r_info[m_ptr->r_idx];
635         for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
636                 dx != 0 || dy != 0;
637                 i++, dx = x_offsets[i], dy = y_offsets[i])
638         {
639                 POSITION y = m_ptr->fy + dy;
640                 POSITION x = m_ptr->fx + dx;
641                 if (!in_bounds(target_ptr->current_floor_ptr, y, x)) continue;
642                 if (!monster_can_enter(target_ptr, y, x, r_ptr, 0)) continue;
643                 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))
644                         continue;
645
646                 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
647                 if (dis < candidate->gdis && dis >= 2)
648                 {
649                         candidate->gy = y;
650                         candidate->gx = x;
651                         candidate->gdis = dis;
652                 }
653         }
654 }
655
656
657 /*!
658  * todo 分割したいが条件が多すぎて適切な関数名と詳細処理を追いきれない……
659  * @brief モンスターの移動方向を返す /
660  * Choose "logical" directions for monster movement
661  * @param target_ptr プレーヤーへの参照ポインタ
662  * @param m_idx モンスターの参照ID
663  * @param mm 移動方向を返す方向IDの参照ポインタ
664  * @return 有効方向があった場合TRUEを返す
665  */
666 static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
667 {
668         floor_type *floor_ptr = target_ptr->current_floor_ptr;
669         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
670         monster_race *r_ptr = &r_info[m_ptr->r_idx];
671         POSITION y = 0, ay, x = 0, ax;
672         POSITION y2 = target_ptr->y;
673         POSITION x2 = target_ptr->x;
674         bool done = FALSE;
675         bool will_run = mon_will_run(target_ptr, m_idx);
676         grid_type *g_ptr;
677         bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) != 0) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2);
678         bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) != 0) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall);
679
680         if (!will_run && m_ptr->target_y)
681         {
682                 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
683                 if ((t_m_idx > 0) &&
684                         are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) &&
685                         los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
686                         projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
687                 {
688                         y = m_ptr->fy - m_ptr->target_y;
689                         x = m_ptr->fx - m_ptr->target_x;
690                         done = TRUE;
691                 }
692         }
693
694         if (!done && !will_run && is_hostile(m_ptr) &&
695                 (r_ptr->flags1 & RF1_FRIENDS) &&
696                 ((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)) ||
697                 (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
698         {
699                 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
700                         !(r_ptr->flags2 & RF2_KILL_WALL))
701                 {
702                         int room = 0;
703                         for (int i = 0; i < 8; i++)
704                         {
705                                 int xx = target_ptr->x + ddx_ddd[i];
706                                 int yy = target_ptr->y + ddy_ddd[i];
707
708                                 if (!in_bounds2(floor_ptr, yy, xx)) continue;
709
710                                 g_ptr = &floor_ptr->grid_array[yy][xx];
711                                 if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0))
712                                 {
713                                         room++;
714                                 }
715                         }
716
717                         if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
718                         if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
719
720                         if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
721                                 (target_ptr->mhp + target_ptr->msp))
722                         {
723                                 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
724                         }
725                 }
726
727                 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
728                 {
729                         for (int i = 0; i < 8; i++)
730                         {
731                                 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
732                                 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
733                                 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
734                                 {
735                                         y2 = target_ptr->y;
736                                         x2 = target_ptr->x;
737                                         break;
738                                 }
739
740                                 if (!in_bounds2(floor_ptr, y2, x2)) continue;
741                                 if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0)) continue;
742
743                                 break;
744                         }
745
746                         y = m_ptr->fy - y2;
747                         x = m_ptr->fx - x2;
748                         done = TRUE;
749                 }
750         }
751
752         if (!done)
753         {
754                 (void)get_moves_aux(target_ptr, m_idx, &y2, &x2, no_flow);
755                 y = m_ptr->fy - y2;
756                 x = m_ptr->fx - x2;
757         }
758
759         if (is_pet(m_ptr) && will_run)
760         {
761                 y = (-y), x = (-x);
762         }
763         else
764         {
765                 if (!done && will_run)
766                 {
767                         int tmp_x = (-x);
768                         int tmp_y = (-y);
769                         if (find_safety(target_ptr, m_idx, &y, &x) && !no_flow)
770                         {
771                                 if (get_fear_moves_aux(target_ptr->current_floor_ptr, m_idx, &y, &x))
772                                         done = TRUE;
773                         }
774
775                         if (!done)
776                         {
777                                 y = tmp_y;
778                                 x = tmp_x;
779                         }
780                 }
781         }
782
783         if (!x && !y) return FALSE;
784
785         ax = ABS(x);
786         ay = ABS(y);
787
788         int move_val = 0;
789         if (y < 0) move_val += 8;
790         if (x > 0) move_val += 4;
791
792         if (ay > (ax << 1)) move_val += 2;
793         else if (ax > (ay << 1)) move_val++;
794
795         switch (move_val)
796         {
797         case 0:
798                 mm[0] = 9;
799                 if (ay > ax)
800                 {
801                         mm[1] = 8;
802                         mm[2] = 6;
803                         mm[3] = 7;
804                         mm[4] = 3;
805                 }
806                 else
807                 {
808                         mm[1] = 6;
809                         mm[2] = 8;
810                         mm[3] = 3;
811                         mm[4] = 7;
812                 }
813                 break;
814         case 1:
815         case 9:
816                 mm[0] = 6;
817                 if (y < 0)
818                 {
819                         mm[1] = 3;
820                         mm[2] = 9;
821                         mm[3] = 2;
822                         mm[4] = 8;
823                 }
824                 else
825                 {
826                         mm[1] = 9;
827                         mm[2] = 3;
828                         mm[3] = 8;
829                         mm[4] = 2;
830                 }
831                 break;
832         case 2:
833         case 6:
834                 mm[0] = 8;
835                 if (x < 0)
836                 {
837                         mm[1] = 9;
838                         mm[2] = 7;
839                         mm[3] = 6;
840                         mm[4] = 4;
841                 }
842                 else
843                 {
844                         mm[1] = 7;
845                         mm[2] = 9;
846                         mm[3] = 4;
847                         mm[4] = 6;
848                 }
849                 break;
850         case 4:
851                 mm[0] = 7;
852                 if (ay > ax)
853                 {
854                         mm[1] = 8;
855                         mm[2] = 4;
856                         mm[3] = 9;
857                         mm[4] = 1;
858                 }
859                 else
860                 {
861                         mm[1] = 4;
862                         mm[2] = 8;
863                         mm[3] = 1;
864                         mm[4] = 9;
865                 }
866                 break;
867         case 5:
868         case 13:
869                 mm[0] = 4;
870                 if (y < 0)
871                 {
872                         mm[1] = 1;
873                         mm[2] = 7;
874                         mm[3] = 2;
875                         mm[4] = 8;
876                 }
877                 else
878                 {
879                         mm[1] = 7;
880                         mm[2] = 1;
881                         mm[3] = 8;
882                         mm[4] = 2;
883                 }
884                 break;
885         case 8:
886                 mm[0] = 3;
887                 if (ay > ax)
888                 {
889                         mm[1] = 2;
890                         mm[2] = 6;
891                         mm[3] = 1;
892                         mm[4] = 9;
893                 }
894                 else
895                 {
896                         mm[1] = 6;
897                         mm[2] = 2;
898                         mm[3] = 9;
899                         mm[4] = 1;
900                 }
901                 break;
902         case 10:
903         case 14:
904                 mm[0] = 2;
905                 if (x < 0)
906                 {
907                         mm[1] = 3;
908                         mm[2] = 1;
909                         mm[3] = 6;
910                         mm[4] = 4;
911                 }
912                 else
913                 {
914                         mm[1] = 1;
915                         mm[2] = 3;
916                         mm[3] = 4;
917                         mm[4] = 6;
918                 }
919                 break;
920         case 12:
921                 mm[0] = 1;
922                 if (ay > ax)
923                 {
924                         mm[1] = 2;
925                         mm[2] = 4;
926                         mm[3] = 3;
927                         mm[4] = 7;
928                 }
929                 else
930                 {
931                         mm[1] = 4;
932                         mm[2] = 2;
933                         mm[3] = 7;
934                         mm[4] = 3;
935                 }
936                 break;
937         }
938
939         return TRUE;
940 }
941
942
943 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
944 {
945         return !have_flag(f_ptr->flags, FF_GLASS) ||
946                 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
947                 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
948 }
949
950
951 /*!
952  * @brief モンスター単体の1ターン行動処理メインルーチン /
953  * Process a monster
954  * @param target_ptr プレーヤーへの参照ポインタ
955  * @param m_idx 行動モンスターの参照ID
956  * @return なし
957  * @details
958  * The monster is known to be within 100 grids of the player\n
959  *\n
960  * In several cases, we directly update the monster lore\n
961  *\n
962  * Note that a monster is only allowed to "reproduce" if there\n
963  * are a limited number of "reproducing" monsters on the current\n
964  * level.  This should prevent the level from being "swamped" by\n
965  * reproducing monsters.  It also allows a large mass of mice to\n
966  * prevent a louse from multiplying, but this is a small price to\n
967  * pay for a simple multiplication method.\n
968  *\n
969  * XXX Monster fear is slightly odd, in particular, monsters will\n
970  * fixate on opening a door even if they cannot open it.  Actually,\n
971  * the same thing happens to normal monsters when they hit a door\n
972  *\n
973  * In addition, monsters which *cannot* open or bash\n
974  * down a door will still stand there trying to open it...\n
975  *\n
976  * XXX Technically, need to check for monster in the way\n
977  * combined with that monster being in a wall (or door?)\n
978  *\n
979  * A "direction" of "5" means "pick a random direction".\n
980  */
981 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
982 {
983         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
984         monster_race *r_ptr = &r_info[m_ptr->r_idx];
985         turn_flags tmp_flags;
986         turn_flags *turn_flags_ptr = init_turn_flags(target_ptr->riding, m_idx, &tmp_flags);
987         turn_flags_ptr->see_m = is_seen(m_ptr);
988
989         decide_drop_from_monster(target_ptr, m_idx, turn_flags_ptr->is_riding_mon);
990         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
991         {
992                 choose_new_monster(target_ptr, m_idx, FALSE, 0);
993                 r_ptr = &r_info[m_ptr->r_idx];
994         }
995
996         turn_flags_ptr->aware = process_stealth(target_ptr, m_idx);
997         if (vanish_summoned_children(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
998         if (process_quantum_effect(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
999         if (explode_grenade(target_ptr, m_idx)) return;
1000         if (runaway_monster(target_ptr, turn_flags_ptr, m_idx)) return;
1001
1002         awake_monster(target_ptr, m_idx);
1003         if (MON_STUNNED(m_ptr))
1004         {
1005                 if (one_in_(2)) return;
1006         }
1007
1008         if (turn_flags_ptr->is_riding_mon)
1009         {
1010                 target_ptr->update |= (PU_BONUS);
1011         }
1012
1013         process_angar(target_ptr, m_idx, turn_flags_ptr->see_m);
1014
1015         POSITION oy = m_ptr->fy;
1016         POSITION ox = m_ptr->fx;
1017         if (decide_monster_multiplication(target_ptr, m_idx, oy, ox)) return;
1018
1019         process_special(target_ptr, m_idx);
1020         process_speak_sound(target_ptr, m_idx, oy, ox, turn_flags_ptr->aware);
1021         if (cast_spell(target_ptr, m_idx, turn_flags_ptr->aware)) return;
1022
1023         DIRECTION mm[8];
1024         mm[0] = mm[1] = mm[2] = mm[3] = 0;
1025         mm[4] = mm[5] = mm[6] = mm[7] = 0;
1026
1027         if (!decide_monster_movement_direction(target_ptr, mm, m_idx, turn_flags_ptr->aware)) return;
1028
1029         int count = 0;
1030         if (!process_monster_movement(target_ptr, turn_flags_ptr, m_idx, mm, oy, ox, &count)) return;
1031
1032         /*
1033          *  Forward movements failed, but now received LOS attack!
1034          *  Try to flow by smell.
1035          */
1036         if (target_ptr->no_flowed && count > 2 && m_ptr->target_y)
1037                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
1038
1039         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)
1040         {
1041                 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
1042                 {
1043                         if (make_attack_spell(m_idx, target_ptr)) return;
1044                 }
1045         }
1046
1047         update_player_type(target_ptr, turn_flags_ptr, r_ptr);
1048         update_monster_race_flags(target_ptr, turn_flags_ptr, m_ptr);
1049
1050         if (!process_monster_fear(target_ptr, turn_flags_ptr, m_idx)) return;
1051
1052         if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
1053 }
1054
1055
1056 /*!
1057  * @brief 超隠密処理
1058  * @param target_ptr プレーヤーへの参照ポインタ
1059  * @param m_idx モンスターID
1060  * @return モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1061  */
1062 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx)
1063 {
1064         if ((target_ptr->special_defense & NINJA_S_STEALTH) == 0) return TRUE;
1065
1066         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1067         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1068         int tmp = target_ptr->lev * 6 + (target_ptr->skill_stl + 10) * 4;
1069         if (target_ptr->monlite) tmp /= 3;
1070         if (target_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1071         if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10)) tmp /= 3;
1072         return (randint0(tmp) <= (r_ptr->level + 20));
1073 }
1074
1075
1076 /*!
1077  * @brief 死亡したモンスターが乗馬中のモンスターだった場合に落馬処理を行う
1078  * @param target_ptr プレーヤーへの参照ポインタ
1079  * @param m_idx モンスターID
1080  * @param is_riding_mon 騎乗中であればTRUE
1081  * @return なし
1082  */
1083 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon)
1084 {
1085         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1086         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1087         if (!is_riding_mon || ((r_ptr->flags7 & RF7_RIDING) != 0)) return;
1088
1089         if (rakuba(target_ptr, 0, TRUE))
1090         {
1091 #ifdef JP
1092                 msg_print("地面に落とされた。");
1093 #else
1094                 GAME_TEXT m_name[MAX_NLEN];
1095                 monster_desc(target_ptr, m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
1096                 msg_format("You have fallen from %s.", m_name);
1097 #endif
1098         }
1099 }
1100
1101
1102 /*!
1103  * @brief 召喚の親元が消滅した時、子供も消滅させる
1104  * @param target_ptr プレーヤーへの参照ポインタ
1105  * @param m_idx モンスターID
1106  * @param see_m モンスターが視界内にいたらTRUE
1107  * @return 召喚モンスターが消滅したらTRUE
1108  */
1109 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
1110 {
1111         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1112         if ((m_ptr->parent_m_idx == 0) || (target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx > 0))
1113                 return FALSE;
1114
1115         if (see_m)
1116         {
1117                 GAME_TEXT m_name[MAX_NLEN];
1118                 monster_desc(target_ptr, m_name, m_ptr, 0);
1119                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
1120         }
1121
1122         if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
1123         {
1124                 GAME_TEXT m_name[MAX_NLEN];
1125                 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
1126                 exe_write_diary(target_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
1127         }
1128
1129         delete_monster_idx(target_ptr, m_idx);
1130         return TRUE;
1131 }
1132
1133
1134 /*!
1135  * @brief 寝ているモンスターの起床を判定する
1136  * @param target_ptr プレーヤーへの参照ポインタ
1137  * @param m_idx モンスターID
1138  * @return なし
1139  */
1140 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx)
1141 {
1142         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1143         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1144         if (!MON_CSLEEP(m_ptr)) return;
1145         if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
1146
1147         (void)set_monster_csleep(target_ptr, m_idx, 0);
1148         if (m_ptr->ml)
1149         {
1150                 GAME_TEXT m_name[MAX_NLEN];
1151                 monster_desc(target_ptr, m_name, m_ptr, 0);
1152                 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
1153         }
1154
1155         if (is_original_ap_and_seen(target_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
1156         {
1157                 r_ptr->r_wake++;
1158         }
1159 }
1160
1161
1162 /*!
1163  * @brief モンスターの怒り状態を判定する (起こっていたら敵に回す)
1164  * @param target_ptr プレーヤーへの参照ポインタ
1165  * @param m_idx モンスターID
1166  * @param see_m モンスターが視界内にいたらTRUE
1167  * @return なし
1168  */
1169 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
1170 {
1171         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1172         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1173         bool gets_angry = FALSE;
1174         if (is_friendly(m_ptr) && (target_ptr->cursed & TRC_AGGRAVATE))
1175                 gets_angry = TRUE;
1176
1177         if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
1178                 monster_has_hostile_align(target_ptr, NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
1179         {
1180                 gets_angry = TRUE;
1181         }
1182
1183         if (target_ptr->phase_out || !gets_angry) return;
1184
1185         if (is_pet(m_ptr) || see_m)
1186         {
1187                 GAME_TEXT m_name[MAX_NLEN];
1188                 monster_desc(target_ptr, m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
1189                 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
1190         }
1191
1192         set_hostile(target_ptr, m_ptr);
1193 }
1194
1195
1196 /*!
1197  * @brief 手榴弾の爆発処理
1198  * @param target_ptr プレーヤーへの参照ポインタ
1199  * @param m_idx モンスターID
1200  * @return 爆死したらTRUE
1201  */
1202 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx)
1203 {
1204         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1205         if (m_ptr->r_idx != MON_GRENADE) return FALSE;
1206
1207         bool fear, dead;
1208         mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1209         return dead;
1210 }
1211
1212
1213 /*!
1214  * @brief モンスター依存の特別な行動を取らせる
1215  * @param target_ptr プレーヤーへの参照ポインタ
1216  * @param m_idx モンスターID
1217  * @return なし
1218  */
1219 void process_special(player_type *target_ptr, MONSTER_IDX m_idx)
1220 {
1221         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1222         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1223         if ((r_ptr->a_ability_flags2 & RF6_SPECIAL) == 0) return;
1224         if (m_ptr->r_idx != MON_OHMU) return;
1225         if (target_ptr->current_floor_ptr->inside_arena || target_ptr->phase_out) return;
1226         if ((r_ptr->freq_spell == 0) || !(randint1(100) <= r_ptr->freq_spell)) return;
1227
1228         int count = 0;
1229         DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1230         BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
1231
1232         for (int k = 0; k < A_MAX; k++)
1233         {
1234                 if (summon_specific(target_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
1235                 {
1236                         if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
1237                 }
1238         }
1239
1240         if (count && is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
1241 }
1242
1243
1244 /*!
1245  * @brief モンスターを喋らせたり足音を立てたりする
1246  * @param target_ptr プレーヤーへの参照ポインタ
1247  * @param m_idx モンスターID
1248  * @param oy モンスターが元々いたY座標
1249  * @param ox モンスターが元々いたX座標
1250  * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1251  * @return なし
1252  */
1253 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware)
1254 {
1255         if (target_ptr->phase_out) return;
1256
1257         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1258         monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1259         if (m_ptr->ap_r_idx == MON_CYBER &&
1260                 one_in_(CYBERNOISE) &&
1261                 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
1262         {
1263                 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1264                 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
1265         }
1266
1267         if (((ap_r_ptr->flags2 & RF2_CAN_SPEAK) == 0) || !aware ||
1268                 !one_in_(SPEAK_CHANCE) ||
1269                 !player_has_los_bold(target_ptr, oy, ox) ||
1270                 !projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
1271                 return;
1272
1273         GAME_TEXT m_name[MAX_NLEN];
1274         char monmessage[1024];
1275         concptr filename;
1276
1277         if (m_ptr->ml)
1278                 monster_desc(target_ptr, m_name, m_ptr, 0);
1279         else
1280                 strcpy(m_name, _("それ", "It"));
1281
1282         if (MON_MONFEAR(m_ptr))
1283                 filename = _("monfear_j.txt", "monfear.txt");
1284         else if (is_pet(m_ptr))
1285                 filename = _("monpet_j.txt", "monpet.txt");
1286         else if (is_friendly(m_ptr))
1287                 filename = _("monfrien_j.txt", "monfrien.txt");
1288         else
1289                 filename = _("monspeak_j.txt", "monspeak.txt");
1290
1291         if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
1292         {
1293                 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
1294         }
1295 }
1296
1297
1298 /*!
1299  * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
1300  * @param target_ptr プレーヤーへの参照ポインタ
1301  * @param m_idx モンスターID
1302  * @param oy 分裂元モンスターのY座標
1303  * @param ox 分裂元モンスターのX座標
1304  * @return 実際に分裂したらTRUEを返す
1305  */
1306 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
1307 {
1308         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1309         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1310         if (((r_ptr->flags2 & RF2_MULTIPLY) == 0) || (target_ptr->current_floor_ptr->num_repro >= MAX_REPRO))
1311                 return FALSE;
1312
1313         int k = 0;
1314         for (POSITION y = oy - 1; y <= oy + 1; y++)
1315         {
1316                 for (POSITION x = ox - 1; x <= ox + 1; x++)
1317                 {
1318                         if (!in_bounds2(target_ptr->current_floor_ptr, y, x)) continue;
1319                         if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
1320                 }
1321         }
1322
1323         if (multiply_barrier(target_ptr, m_idx)) k = 8;
1324
1325         if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
1326         {
1327                 if (multiply_monster(target_ptr, m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
1328                 {
1329                         if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(target_ptr, m_ptr))
1330                         {
1331                                 r_ptr->r_flags2 |= (RF2_MULTIPLY);
1332                         }
1333
1334                         return TRUE;
1335                 }
1336         }
1337
1338         return FALSE;
1339 }
1340
1341
1342 /*!
1343  * @brief モンスターの移動パターンを決定する
1344  * @param target_ptr プレーヤーへの参照ポインタ
1345  * @param mm 移動方向
1346  * @param m_idx モンスターID
1347  * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1348  * @return 移動先が存在すればTRUE
1349  */
1350 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware)
1351 {
1352         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1353         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1354
1355         if (MON_CONFUSED(m_ptr) || !aware)
1356         {
1357                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1358                 return TRUE;
1359         }
1360
1361         if (random_walk(target_ptr, mm, m_ptr)) return TRUE;
1362
1363         if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
1364         {
1365                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1366                 return TRUE;
1367         }
1368
1369         if (decide_pet_movement_direction(target_ptr, mm, m_idx)) return TRUE;
1370
1371         if (!is_hostile(m_ptr))
1372         {
1373                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1374                 get_enemy_dir(target_ptr, m_idx, mm);
1375                 return TRUE;
1376         }
1377
1378         if (!get_moves(target_ptr, m_idx, mm)) return FALSE;
1379
1380         return TRUE;
1381 }
1382
1383
1384 /*!
1385  * todo ↓のように書いたが、"5"とはもしかして「その場に留まる」という意味か?
1386  * @brief 不規則歩行フラグを持つモンスターの移動方向をその確率に基づいて決定する
1387  * @param target_ptr プレーヤーへの参照ポインタ
1388  * @param mm 移動方向
1389  * @param m_ptr モンスターへの参照ポインタ
1390  * @return 不規則な方向へ歩くことになったらTRUE
1391  */
1392 bool random_walk(player_type *target_ptr, DIRECTION *mm, monster_type *m_ptr)
1393 {
1394         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1395         if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) && (randint0(100) < 75))
1396         {
1397                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
1398
1399                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1400                 return TRUE;
1401         }
1402
1403         if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 50))
1404         {
1405                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_50;
1406
1407                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1408                 return TRUE;
1409         }
1410
1411         if ((r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 25))
1412         {
1413                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
1414
1415                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1416                 return TRUE;
1417         }
1418
1419         return FALSE;
1420 }
1421
1422
1423 /*!
1424  * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
1425  * @param target_ptr プレーヤーへの参照ポインタ
1426  * @param mm 移動方向
1427  * @param m_idx モンスターID
1428  * @return モンスターがペットであればTRUE
1429  */
1430 bool decide_pet_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx)
1431 {
1432         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1433         if (!is_pet(m_ptr)) return FALSE;
1434
1435         bool avoid = ((target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
1436         bool lonely = (!avoid && (m_ptr->cdis > target_ptr->pet_follow_distance));
1437         bool distant = (m_ptr->cdis > PET_SEEK_DIST);
1438         mm[0] = mm[1] = mm[2] = mm[3] = 5;
1439         if (get_enemy_dir(target_ptr, m_idx, mm)) return TRUE;
1440         if (!avoid && !lonely && !distant) return TRUE;
1441
1442         POSITION dis = target_ptr->pet_follow_distance;
1443         if (target_ptr->pet_follow_distance > PET_SEEK_DIST)
1444         {
1445                 target_ptr->pet_follow_distance = PET_SEEK_DIST;
1446         }
1447
1448         (void)get_moves(target_ptr, m_idx, mm);
1449         target_ptr->pet_follow_distance = (s16b)dis;
1450         return TRUE;
1451 }
1452
1453
1454 /*!
1455  * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
1456  * @param target_ptr プレーヤーへの参照ポインタ
1457  * @param m_idx モンスターID
1458  * @param is_riding_mon 騎乗状態ならばTRUE
1459  * @param see_m モンスターが視界内にいたらTRUE
1460  * @return モンスターがフロアから消えたらTRUE
1461  */
1462 bool runaway_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
1463 {
1464         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1465         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1466         bool can_runaway = is_pet(m_ptr) || is_friendly(m_ptr);
1467         can_runaway &= ((r_ptr->flags1 & RF1_UNIQUE) != 0) || ((r_ptr->flags7 & RF7_NAZGUL) != 0);
1468         can_runaway &= !target_ptr->phase_out;
1469         if (!can_runaway) return FALSE;
1470
1471         static int riding_pinch = 0;
1472
1473         if (m_ptr->hp >= m_ptr->maxhp / 3)
1474         {
1475                 /* Reset the counter */
1476                 if (turn_flags_ptr->is_riding_mon) riding_pinch = 0;
1477
1478                 return FALSE;
1479         }
1480
1481         GAME_TEXT m_name[MAX_NLEN];
1482         monster_desc(target_ptr, m_name, m_ptr, 0);
1483         if (turn_flags_ptr->is_riding_mon && riding_pinch < 2)
1484         {
1485                 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
1486                         "%^s seems to be in so much pain and tries to escape from your restriction."), m_name);
1487                 riding_pinch++;
1488                 disturb(target_ptr, TRUE, TRUE);
1489                 return FALSE;
1490         }
1491
1492         escape_monster(target_ptr, turn_flags_ptr, m_ptr, m_name);
1493         check_quest_completion(target_ptr, m_ptr);
1494         delete_monster_idx(target_ptr, m_idx);
1495         return TRUE;
1496 }
1497
1498
1499 /*!
1500  * @brief HPが1/3未満になった有効的なユニークモンスターの逃走処理を行う
1501  * @param target_ptr プレーヤーへの参照ポインタ
1502  * @param is_riding_mon 騎乗状態ならばTRUE
1503  * @param m_ptr モンスターへの参照ポインタ
1504  * @param m_name モンスター名称
1505  * @param see_m モンスターが視界内にいたらTRUE
1506  * @return なし
1507  */
1508 void escape_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, GAME_TEXT *m_name)
1509 {
1510         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1511         if (turn_flags_ptr->is_riding_mon)
1512         {
1513                 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
1514                 if (rakuba(target_ptr, -1, FALSE))
1515                 {
1516                         msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
1517                 }
1518         }
1519
1520         if (turn_flags_ptr->see_m)
1521         {
1522                 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) &&
1523                         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))
1524                 {
1525                         msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
1526                 }
1527
1528                 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s reads a scroll of teleport level."), m_name);
1529                 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
1530         }
1531
1532         if (turn_flags_ptr->is_riding_mon && rakuba(target_ptr, -1, FALSE))
1533         {
1534                 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
1535         }
1536 }
1537
1538
1539 /*!
1540  * @brief モンスターに魔法を試行させる
1541  * @param target_ptr プレーヤーへの参照ポインタ
1542  * @param m_idx モンスターID
1543  * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1544  * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
1545  */
1546 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware)
1547 {
1548         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1549         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1550         if ((r_ptr->freq_spell == 0) || (randint1(100) > r_ptr->freq_spell))
1551                 return FALSE;
1552
1553         bool counterattack = FALSE;
1554         if (m_ptr->target_y)
1555         {
1556                 MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
1557                 if (t_m_idx && are_enemies(target_ptr, m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
1558                         projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1559                 {
1560                         counterattack = TRUE;
1561                 }
1562         }
1563
1564         if (counterattack)
1565         {
1566                 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
1567                 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
1568         }
1569         else
1570         {
1571                 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
1572                 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
1573         }
1574
1575         return FALSE;
1576 }
1577
1578
1579 /*!
1580  * todo 少し長いが、これといってブロックとしてまとまった部分もないので暫定でこのままとする
1581  * @brief モンスターの移動に関するメインルーチン
1582  * @param target_ptr プレーヤーへの参照ポインタ
1583  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1584  * @param m_idx モンスターID
1585  * @param mm モンスターの移動方向
1586  * @param oy 移動前の、モンスターのY座標
1587  * @param ox 移動前の、モンスターのX座標
1588  * @param count 移動回数 (のはず todo)
1589  * @return 移動が阻害される何か (ドア等)があったらFALSE
1590  */
1591 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)
1592 {
1593         for (int i = 0; mm[i]; i++)
1594         {
1595                 int d = mm[i];
1596                 if (d == 5) d = ddd[randint0(8)];
1597
1598                 POSITION ny = oy + ddy[d];
1599                 POSITION nx = ox + ddx[d];
1600                 if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx)) continue;
1601
1602                 grid_type *g_ptr;
1603                 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1604                 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1605                 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1606                 bool can_cross = monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0);
1607
1608                 if (!process_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx, can_cross))
1609                 {
1610                         if (!process_door(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1611                                 return FALSE;
1612                 }
1613
1614                 if (!process_protection_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1615                 {
1616                         if (!process_explosive_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1617                                 return FALSE;
1618                 }
1619
1620                 exe_monster_attack_to_player(target_ptr, turn_flags_ptr, m_idx, ny, nx);
1621                 if (process_monster_attack_to_monster(target_ptr, turn_flags_ptr, m_idx, g_ptr, can_cross)) return FALSE;
1622
1623                 if (turn_flags_ptr->is_riding_mon)
1624                 {
1625                         if (!target_ptr->riding_ryoute && !MON_MONFEAR(&target_ptr->current_floor_ptr->m_list[target_ptr->riding])) turn_flags_ptr->do_move = FALSE;
1626                 }
1627
1628                 if (!process_post_dig_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx)) return FALSE;
1629
1630                 if (turn_flags_ptr->must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
1631                 {
1632                         if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0))
1633                                 turn_flags_ptr->do_move = FALSE;
1634                 }
1635
1636                 if (turn_flags_ptr->do_move && !can_cross && !turn_flags_ptr->did_kill_wall && !turn_flags_ptr->did_bash_door)
1637                         turn_flags_ptr->do_move = FALSE;
1638
1639                 if (turn_flags_ptr->do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
1640                 {
1641                         if (is_original_ap_and_seen(target_ptr, m_ptr))
1642                                 r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
1643
1644                         turn_flags_ptr->do_move = FALSE;
1645                 }
1646
1647                 if (!turn_flags_ptr->do_move)
1648                 {
1649                         if (turn_flags_ptr->do_turn) break;
1650
1651                         continue;
1652                 }
1653
1654                 turn_flags_ptr->do_turn = TRUE;
1655                 feature_type *f_ptr;
1656                 f_ptr = &f_info[g_ptr->feat];
1657                 if (have_flag(f_ptr->flags, FF_TREE))
1658                 {
1659                         if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
1660                         {
1661                                 m_ptr->energy_need += ENERGY_NEED();
1662                         }
1663                 }
1664
1665                 if (!update_riding_monster(target_ptr, turn_flags_ptr, m_idx, oy, ox, ny, nx)) break;
1666
1667                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1668                 if (m_ptr->ml &&
1669                         (disturb_move ||
1670                         (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
1671                                 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
1672                 {
1673                         if (is_hostile(m_ptr))
1674                                 disturb(target_ptr, FALSE, TRUE);
1675                 }
1676
1677                 bool is_takable_or_killable = g_ptr->o_idx > 0;
1678                 is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) != 0;
1679
1680                 bool is_pickup_items = (target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) != 0;
1681                 is_pickup_items &= (r_ptr->flags2 & RF2_TAKE_ITEM) != 0;
1682
1683                 is_takable_or_killable &= !is_pet(m_ptr) || is_pickup_items;
1684                 if (!is_takable_or_killable)
1685                 {
1686                         if (turn_flags_ptr->do_turn) break;
1687
1688                         continue;
1689                 }
1690
1691                 update_object_by_monster_movement(target_ptr, turn_flags_ptr, m_idx, ny, nx);
1692                 if (turn_flags_ptr->do_turn) break;
1693
1694                 (*count)++;
1695         }
1696
1697         return TRUE;
1698 }
1699
1700
1701 /*!
1702  * @brief モンスターによる壁の透過・破壊を行う
1703  * @param target_ptr プレーヤーへの参照ポインタ
1704  * @param m_ptr モンスターへの参照ポインタ
1705  * @param ny モンスターのY座標
1706  * @param nx モンスターのX座標
1707  * @param can_cross モンスターが地形を踏破できるならばTRUE
1708  * @return 透過も破壊もしなかった場合はFALSE、それ以外はTRUE
1709  */
1710 bool process_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx, bool can_cross)
1711 {
1712         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1713         grid_type *g_ptr;
1714         g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1715         feature_type *f_ptr;
1716         f_ptr = &f_info[g_ptr->feat];
1717         if (player_bold(target_ptr, ny, nx))
1718         {
1719                 turn_flags_ptr->do_move = TRUE;
1720                 return TRUE;
1721         }
1722
1723         if (g_ptr->m_idx > 0)
1724         {
1725                 turn_flags_ptr->do_move = TRUE;
1726                 return TRUE;
1727         }
1728
1729         if (((r_ptr->flags2 & RF2_KILL_WALL) != 0) &&
1730                 (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !turn_flags_ptr->is_riding_mon) &&
1731                 have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
1732                 check_hp_for_feat_destruction(f_ptr, m_ptr))
1733         {
1734                 turn_flags_ptr->do_move = TRUE;
1735                 if (!can_cross) turn_flags_ptr->must_alter_to_move = TRUE;
1736
1737                 turn_flags_ptr->did_kill_wall = TRUE;
1738                 return TRUE;
1739         }
1740
1741         if (!can_cross) return FALSE;
1742
1743         turn_flags_ptr->do_move = TRUE;
1744         if (((r_ptr->flags2 & RF2_PASS_WALL) != 0) && (!turn_flags_ptr->is_riding_mon || target_ptr->pass_wall) &&
1745                 have_flag(f_ptr->flags, FF_CAN_PASS))
1746         {
1747                 turn_flags_ptr->did_pass_wall = TRUE;
1748         }
1749
1750         return TRUE;
1751 }
1752
1753
1754 /*!
1755  * @brief モンスターによるドアの開放・破壊を行う
1756  * @param target_ptr プレーヤーへの参照ポインタ
1757  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1758  * @param m_ptr モンスターへの参照ポインタ
1759  * @param ny モンスターのY座標
1760  * @param nx モンスターのX座標
1761  * @return モンスターが死亡した場合のみFALSE
1762  */
1763 bool process_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
1764 {
1765         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1766         grid_type *g_ptr;
1767         g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1768         if (!is_closed_door(target_ptr, g_ptr->feat)) return TRUE;
1769
1770         feature_type *f_ptr;
1771         f_ptr = &f_info[g_ptr->feat];
1772         bool may_bash = bash_normal_door(target_ptr, turn_flags_ptr, m_ptr, ny, nx);
1773         bash_glass_door(target_ptr, turn_flags_ptr, m_ptr, f_ptr, may_bash);
1774
1775         if (!turn_flags_ptr->did_open_door && !turn_flags_ptr->did_bash_door) return TRUE;
1776
1777         if (turn_flags_ptr->did_bash_door &&
1778                 ((randint0(100) < 50) || (feat_state(target_ptr, g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
1779         {
1780                 cave_alter_feat(target_ptr, ny, nx, FF_BASH);
1781                 if (!monster_is_valid(m_ptr))
1782                 {
1783                         target_ptr->update |= (PU_FLOW);
1784                         target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1785                         if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1786
1787                         return FALSE;
1788                 }
1789         }
1790         else
1791         {
1792                 cave_alter_feat(target_ptr, ny, nx, FF_OPEN);
1793         }
1794
1795         f_ptr = &f_info[g_ptr->feat];
1796         turn_flags_ptr->do_view = TRUE;
1797         return TRUE;
1798 }
1799
1800
1801 /*!
1802  * @brief モンスターが普通のドアを開ける処理
1803  * @param target_ptr プレーヤーへの参照ポインタ
1804  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1805  * @param m_ptr モンスターへの参照ポインタ
1806  * @param ny モンスターのY座標
1807  * @param nx モンスターのX座標
1808  * @return ここではドアを開けず、ガラスのドアを開ける可能性があるならTRUE
1809  */
1810 bool bash_normal_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
1811 {
1812         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1813         grid_type *g_ptr;
1814         g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1815         feature_type *f_ptr;
1816         f_ptr = &f_info[g_ptr->feat];
1817         turn_flags_ptr->do_move = FALSE;
1818         if (((r_ptr->flags2 & RF2_OPEN_DOOR) == 0) || !have_flag(f_ptr->flags, FF_OPEN) ||
1819                 (is_pet(m_ptr) && ((target_ptr->pet_extra_flags & PF_OPEN_DOORS) == 0)))
1820                 return TRUE;
1821
1822         if (f_ptr->power == 0)
1823         {
1824                 turn_flags_ptr->did_open_door = TRUE;
1825                 turn_flags_ptr->do_turn = TRUE;
1826                 return FALSE;
1827         }
1828
1829         if (randint0(m_ptr->hp / 10) > f_ptr->power)
1830         {
1831                 cave_alter_feat(target_ptr, ny, nx, FF_DISARM);
1832                 turn_flags_ptr->do_turn = TRUE;
1833                 return FALSE;
1834         }
1835
1836         return TRUE;
1837 }
1838
1839
1840 /*!
1841  * @brief モンスターがガラスのドアを開ける処理
1842  * @param target_ptr プレーヤーへの参照ポインタ
1843  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1844  * @param m_ptr モンスターへの参照ポインタ
1845  * @param g_ptr グリッドへの参照ポインタ
1846  * @param f_ptr 地形への参照ポインタ
1847  * @return なし
1848  */
1849 void bash_glass_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, feature_type *f_ptr, bool may_bash)
1850 {
1851         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1852         if (!may_bash || ((r_ptr->flags2 & RF2_BASH_DOOR) == 0) || !have_flag(f_ptr->flags, FF_BASH) ||
1853                 (is_pet(m_ptr) && ((target_ptr->pet_extra_flags & PF_OPEN_DOORS) == 0)))
1854                 return;
1855
1856         if (!check_hp_for_feat_destruction(f_ptr, m_ptr) || (randint0(m_ptr->hp / 10) <= f_ptr->power))
1857                 return;
1858
1859         if (have_flag(f_ptr->flags, FF_GLASS))
1860                 msg_print(_("ガラスが砕ける音がした!", "You hear glass breaking!"));
1861         else
1862                 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
1863
1864         if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1865
1866         turn_flags_ptr->did_bash_door = TRUE;
1867         turn_flags_ptr->do_move = TRUE;
1868         turn_flags_ptr->must_alter_to_move = TRUE;
1869 }
1870
1871
1872 /*!
1873  * @brief 守りのルーンによるモンスターの移動制限を処理する
1874  * @param target_ptr プレーヤーへの参照ポインタ
1875  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1876  * @param m_ptr モンスターへの参照ポインタ
1877  * @param ny モンスターのY座標
1878  * @param nx モンスターのX座標
1879  * @return ルーンのある/なし
1880  */
1881 bool process_protection_rune(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
1882 {
1883         grid_type *g_ptr;
1884         g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1885         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1886         if (!turn_flags_ptr->do_move || !is_glyph_grid(g_ptr) ||
1887                 (((r_ptr->flags1 & RF1_NEVER_BLOW) != 0) && player_bold(target_ptr, ny, nx)))
1888                 return FALSE;
1889
1890         turn_flags_ptr->do_move = FALSE;
1891         if (is_pet(m_ptr) || (randint1(BREAK_GLYPH) >= r_ptr->level))
1892                 return TRUE;
1893
1894         if (g_ptr->info & CAVE_MARK)
1895         {
1896                 msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
1897         }
1898
1899         g_ptr->info &= ~(CAVE_MARK);
1900         g_ptr->info &= ~(CAVE_OBJECT);
1901         g_ptr->mimic = 0;
1902         turn_flags_ptr->do_move = TRUE;
1903         note_spot(target_ptr, ny, nx);
1904         return TRUE;
1905 }
1906
1907
1908 /*!
1909  * @brief 爆発のルーンにを処理する
1910  * @param target_ptr プレーヤーへの参照ポインタ
1911  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1912  * @param m_ptr モンスターへの参照ポインタ
1913  * @param ny モンスターのY座標
1914  * @param nx モンスターのX座標
1915  * @return モンスターが死亡した場合のみFALSE
1916  */
1917 bool process_explosive_rune(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
1918 {
1919         grid_type *g_ptr;
1920         g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1921         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1922         if (!turn_flags_ptr->do_move || !is_explosive_rune_grid(g_ptr) ||
1923                 (((r_ptr->flags1 & RF1_NEVER_BLOW) != 0) && player_bold(target_ptr, ny, nx)))
1924                 return TRUE;
1925
1926         turn_flags_ptr->do_move = FALSE;
1927         if (is_pet(m_ptr)) return TRUE;
1928
1929         if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
1930         {
1931                 if (g_ptr->info & CAVE_MARK)
1932                 {
1933                         msg_print(_("ルーンが爆発した!", "The rune explodes!"));
1934                         BIT_FLAGS project_flags = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI;
1935                         project(target_ptr, 0, 2, ny, nx, 2 * (target_ptr->lev + damroll(7, 7)), GF_MANA, project_flags, -1);
1936                 }
1937         }
1938         else
1939         {
1940                 msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
1941         }
1942
1943         g_ptr->info &= ~(CAVE_MARK);
1944         g_ptr->info &= ~(CAVE_OBJECT);
1945         g_ptr->mimic = 0;
1946
1947         note_spot(target_ptr, ny, nx);
1948         lite_spot(target_ptr, ny, nx);
1949
1950         if (!monster_is_valid(m_ptr)) return FALSE;
1951
1952         turn_flags_ptr->do_move = TRUE;
1953         return TRUE;
1954 }
1955
1956
1957 /*!
1958  * @brief モンスターが壁を掘った後続処理を実行する
1959  * @param target_ptr プレーヤーへの参照ポインタ
1960  * @turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1961  * @param m_ptr モンスターへの参照ポインタ
1962  * @param ny モンスターのY座標
1963  * @param nx モンスターのX座標
1964  * @return モンスターが死亡した場合のみFALSE
1965  */
1966 bool process_post_dig_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
1967 {
1968         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1969         grid_type *g_ptr;
1970         g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1971         feature_type *f_ptr;
1972         f_ptr = &f_info[g_ptr->feat];
1973         if (!turn_flags_ptr->did_kill_wall || !turn_flags_ptr->do_move) return TRUE;
1974
1975         if (one_in_(GRINDNOISE))
1976         {
1977                 if (have_flag(f_ptr->flags, FF_GLASS))
1978                         msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
1979                 else
1980                         msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
1981         }
1982
1983         cave_alter_feat(target_ptr, ny, nx, FF_HURT_DISI);
1984
1985         if (!monster_is_valid(m_ptr))
1986         {
1987                 target_ptr->update |= (PU_FLOW);
1988                 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1989                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
1990
1991                 return FALSE;
1992         }
1993
1994         f_ptr = &f_info[g_ptr->feat];
1995         turn_flags_ptr->do_view = TRUE;
1996         turn_flags_ptr->do_turn = TRUE;
1997         return TRUE;
1998 }
1999
2000
2001 /*!
2002  * @brief 騎乗中のモンスター情報を更新する
2003  * @param target_ptr プレーヤーへの参照ポインタ
2004  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
2005  * @param m_idx モンスターID
2006  * @param oy 移動前の、モンスターのY座標
2007  * @param ox 移動前の、モンスターのX座標
2008  * @param ny 移動後の、モンスターのY座標
2009  * @param ox 移動後の、モンスターのX座標
2010  * @return アイテム等に影響を及ぼしたらTRUE
2011  */
2012 bool update_riding_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, POSITION ny, POSITION nx)
2013 {
2014         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2015         grid_type *g_ptr;
2016         g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
2017         monster_type *y_ptr;
2018         y_ptr = &target_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
2019         if (turn_flags_ptr->is_riding_mon)
2020                 return move_player_effect(target_ptr, ny, nx, MPE_DONT_PICKUP);
2021
2022         target_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
2023         if (g_ptr->m_idx)
2024         {
2025                 y_ptr->fy = oy;
2026                 y_ptr->fx = ox;
2027                 update_monster(target_ptr, g_ptr->m_idx, TRUE);
2028         }
2029
2030         g_ptr->m_idx = m_idx;
2031         m_ptr->fy = ny;
2032         m_ptr->fx = nx;
2033         update_monster(target_ptr, m_idx, TRUE);
2034
2035         lite_spot(target_ptr, oy, ox);
2036         lite_spot(target_ptr, ny, nx);
2037         return TRUE;
2038 }
2039
2040
2041 /*!
2042  * @brief updateフィールドを更新する
2043  * @param target_ptr プレーヤーへの参照ポインタ
2044  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
2045  * @return なし
2046  */
2047 void update_player_type(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_race *r_ptr)
2048 {
2049         if (turn_flags_ptr->do_view)
2050         {
2051                 target_ptr->update |= (PU_FLOW);
2052                 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2053         }
2054
2055         if (turn_flags_ptr->do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
2056                 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !target_ptr->phase_out)))
2057         {
2058                 target_ptr->update |= (PU_MON_LITE);
2059         }
2060 }
2061
2062
2063 /*!
2064  * @brief モンスターのフラグを更新する
2065  * @param target_ptr プレーヤーへの参照ポインタ
2066  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
2067  * @param m_ptr モンスターへの参照ポインタ
2068  * @return なし
2069  */
2070 void update_monster_race_flags(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr)
2071 {
2072         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2073         if (!is_original_ap_and_seen(target_ptr, m_ptr)) return;
2074
2075         if (turn_flags_ptr->did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
2076         if (turn_flags_ptr->did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
2077         if (turn_flags_ptr->did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
2078         if (turn_flags_ptr->did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
2079         if (turn_flags_ptr->did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
2080         if (turn_flags_ptr->did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
2081         if (turn_flags_ptr->did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2082 }
2083
2084
2085 /*!
2086  * @brief モンスターの恐怖状態を処理する
2087  * @param target_ptr プレーヤーへの参照ポインタ
2088  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
2089  * @param m_idx モンスターID
2090  * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
2091  * @return モンスターが戦いを決意したらTRUE
2092  */
2093 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
2094 {
2095         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2096         bool is_battle_determined = !turn_flags_ptr->do_turn && !turn_flags_ptr->do_move && MON_MONFEAR(m_ptr) && turn_flags_ptr->aware;
2097         if (!is_battle_determined) return FALSE;
2098
2099         (void)set_monster_monfear(target_ptr, m_idx, 0);
2100         if (!turn_flags_ptr->see_m) return TRUE;
2101
2102         GAME_TEXT m_name[MAX_NLEN];
2103         monster_desc(target_ptr, m_name, m_ptr, 0);
2104         msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
2105         return TRUE;
2106 }
2107
2108
2109 /*!
2110  * @brief 全モンスターのターン管理メインルーチン /
2111  * Process all the "live" monsters, once per game turn.
2112  * @return なし
2113  * @details
2114  * During each game current game turn, we scan through the list of all the "live" monsters,\n
2115  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
2116  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
2117  *\n
2118  * Note that monsters can never move in the monster array (except when the\n
2119  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
2120  *\n
2121  * This function is responsible for at least half of the processor time\n
2122  * on a normal system with a "normal" amount of monsters and a player doing\n
2123  * normal things.\n
2124  *\n
2125  * When the player is resting, virtually 90% of the processor time is spent\n
2126  * in this function, and its children, "process_monster()" and "make_move()".\n
2127  *\n
2128  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
2129  * especially when the player is running.\n
2130  *\n
2131  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
2132  * monsters while they are still being "born".  A monster is "fresh" only\n
2133  * during the game turn in which it is created, and we use the "hack_m_idx" to\n
2134  * determine if the monster is yet to be processed during the game turn.\n
2135  *\n
2136  * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
2137  * move before any "nasty" monsters get to use their spell attacks.\n
2138  *\n
2139  * Note that when the "knowledge" about the currently tracked monster\n
2140  * changes (flags, attacks, spells), we induce a redraw of the monster\n
2141  * recall window.\n
2142  */
2143 void process_monsters(player_type *target_ptr)
2144 {
2145         old_race_flags tmp_flags;
2146         old_race_flags *old_race_flags_ptr = init_old_race_flags(&tmp_flags);
2147
2148         floor_type *floor_ptr = target_ptr->current_floor_ptr;
2149         floor_ptr->monster_noise = FALSE;
2150
2151         MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
2152         save_old_race_flags(target_ptr->monster_race_idx, old_race_flags_ptr);
2153         sweep_monster_process(target_ptr);
2154
2155         hack_m_idx = 0;
2156         if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
2157                 return;
2158
2159         update_player_window(target_ptr, old_race_flags_ptr);
2160 }
2161
2162
2163 /*!
2164  * @brief フロア内のモンスターについてターン終了時の処理を繰り返す
2165  * @param target_ptr プレーヤーへの参照ポインタ
2166  */
2167 void sweep_monster_process(player_type *target_ptr)
2168 {
2169         floor_type *floor_ptr = target_ptr->current_floor_ptr;
2170         for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--)
2171         {
2172                 monster_type *m_ptr;
2173                 m_ptr = &floor_ptr->m_list[i];
2174
2175                 if (target_ptr->leaving) return;
2176                 if (!monster_is_valid(m_ptr)) continue;
2177                 if (target_ptr->wild_mode) continue;
2178
2179                 if (m_ptr->mflag & MFLAG_BORN)
2180                 {
2181                         m_ptr->mflag &= ~(MFLAG_BORN);
2182                         continue;
2183                 }
2184
2185                 if (m_ptr->cdis >= AAF_LIMIT) continue;
2186                 if (!decide_process_continue(target_ptr, m_ptr)) continue;
2187
2188                 SPEED speed = decide_monster_speed(target_ptr, m_ptr, i);
2189                 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
2190                 if (m_ptr->energy_need > 0) continue;
2191
2192                 m_ptr->energy_need += ENERGY_NEED();
2193                 hack_m_idx = i;
2194                 process_monster(target_ptr, i);
2195                 reset_target(m_ptr);
2196
2197                 if (target_ptr->no_flowed && one_in_(3))
2198                         m_ptr->mflag2 |= MFLAG2_NOFLOW;
2199
2200                 if (!target_ptr->playing || target_ptr->is_dead) return;
2201                 if (target_ptr->leaving) return;
2202         }
2203 }
2204
2205
2206 /*!
2207  * @brief 後続のモンスター処理が必要かどうか判定する (要調査)
2208  * @param target_ptr プレーヤーへの参照ポインタ
2209  * @param m_ptr モンスターへの参照ポインタ
2210  * @return 後続処理が必要ならTRUE
2211  */
2212 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr)
2213 {
2214         monster_race *r_ptr;
2215         r_ptr = &r_info[m_ptr->r_idx];
2216         if (!target_ptr->no_flowed)
2217         {
2218                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2219         }
2220
2221         if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
2222                 return TRUE;
2223
2224         if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) &&
2225                 (player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) || (target_ptr->cursed & TRC_AGGRAVATE)))
2226                 return TRUE;
2227
2228         if (m_ptr->target_y)
2229                 return TRUE;
2230
2231         return FALSE;
2232 }
2233
2234
2235 /*!
2236  * @brief モンスターの加速値を決定する
2237  * @param target_ptr プレーヤーへの参照ポインタ
2238  * @param m_ptr モンスターへの参照ポインタ
2239  * @param monster_number 走査中のモンスター番号
2240  * return モンスターの加速値
2241  */
2242 SPEED decide_monster_speed(player_type *target_ptr, monster_type *m_ptr, int monster_number)
2243 {
2244         SPEED speed;
2245         if (target_ptr->riding == monster_number)
2246         {
2247                 speed = target_ptr->pspeed;
2248                 return speed;
2249         }
2250
2251         speed = m_ptr->mspeed;
2252         if (ironman_nightmare) speed += 5;
2253
2254         if (MON_FAST(m_ptr)) speed += 10;
2255         if (MON_SLOW(m_ptr)) speed -= 10;
2256
2257         return speed;
2258 }
2259
2260
2261 /*!
2262  * @brief モンスターフラグの更新に基づき、モンスター表示を更新する
2263  * @param target_ptr プレーヤーへの参照ポインタ
2264  * @param old_race_flags_ptr モンスターフラグへの参照ポインタ
2265  * @return なし
2266  */
2267 void update_player_window(player_type *target_ptr, old_race_flags *old_race_flags_ptr)
2268 {
2269         monster_race *r_ptr;
2270         r_ptr = &r_info[target_ptr->monster_race_idx];
2271         if ((old_race_flags_ptr->old_r_flags1 != r_ptr->r_flags1) ||
2272                 (old_race_flags_ptr->old_r_flags2 != r_ptr->r_flags2) ||
2273                 (old_race_flags_ptr->old_r_flags3 != r_ptr->r_flags3) ||
2274                 (old_race_flags_ptr->old_r_flags4 != r_ptr->r_flags4) ||
2275                 (old_race_flags_ptr->old_r_flags5 != r_ptr->r_flags5) ||
2276                 (old_race_flags_ptr->old_r_flags6 != r_ptr->r_flags6) ||
2277                 (old_race_flags_ptr->old_r_flagsr != r_ptr->r_flagsr) ||
2278                 (old_race_flags_ptr->old_r_blows0 != r_ptr->r_blows[0]) ||
2279                 (old_race_flags_ptr->old_r_blows1 != r_ptr->r_blows[1]) ||
2280                 (old_race_flags_ptr->old_r_blows2 != r_ptr->r_blows[2]) ||
2281                 (old_race_flags_ptr->old_r_blows3 != r_ptr->r_blows[3]) ||
2282                 (old_race_flags_ptr->old_r_cast_spell != r_ptr->r_cast_spell))
2283         {
2284                 target_ptr->window |= (PW_MONSTER);
2285         }
2286 }