OSDN Git Service

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