OSDN Git Service

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