OSDN Git Service

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