OSDN Git Service

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