OSDN Git Service

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