OSDN Git Service

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