OSDN Git Service

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