OSDN Git Service

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