OSDN Git Service

[Refactor] #37353 mon_take_hit_mon() を melee.h へ移動。
[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 "cmd-pet.h"
18 #include "monsterrace-hook.h"
19 #include "melee.h"
20 #include "spells.h"
21 #include "spells-summon.h"
22 #include "quest.h"
23 #include "avatar.h"
24 #include "realm-hex.h"
25 #include "object-flavor.h"
26 #include "object-hook.h"
27 #include "feature.h"
28 #include "grid.h"
29 #include "player-move.h"
30 #include "monster-status.h"
31 #include "monster-spell.h"
32 #include "floor.h"
33
34
35 /*!
36  * @brief モンスターが敵に接近するための方向を決める /
37  * Calculate the direction to the next enemy
38  * @param m_idx モンスターの参照ID
39  * @param mm 移動するべき方角IDを返す参照ポインタ
40  * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
41  */
42 static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
43 {
44         int i;
45         POSITION x = 0, y = 0;
46         MONSTER_IDX t_idx;
47         int start;
48         int plus = 1;
49
50         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
51         monster_race *r_ptr = &r_info[m_ptr->r_idx];
52         monster_type *t_ptr;
53
54         if (riding_t_m_idx && player_bold(m_ptr->fy, m_ptr->fx))
55         {
56                 y = current_floor_ptr->m_list[riding_t_m_idx].fy;
57                 x = current_floor_ptr->m_list[riding_t_m_idx].fx;
58         }
59         else if (is_pet(m_ptr) && pet_t_m_idx)
60         {
61                 y = current_floor_ptr->m_list[pet_t_m_idx].fy;
62                 x = current_floor_ptr->m_list[pet_t_m_idx].fx;
63         }
64         else
65         {
66                 if (p_ptr->inside_battle)
67                 {
68                         start = randint1(m_max-1)+m_max;
69                         if(randint0(2)) plus = -1;
70                 }
71                 else start = m_max + 1;
72
73                 /* Scan thru all monsters */
74                 for (i = start; ((i < start + m_max) && (i > start - m_max)); i+=plus)
75                 {
76                         MONSTER_IDX dummy = (i % m_max);
77
78                         if (!dummy) continue;
79
80                         t_idx = dummy;
81                         t_ptr = &current_floor_ptr->m_list[t_idx];
82
83                         /* The monster itself isn't a target */
84                         if (t_ptr == m_ptr) continue;
85
86                         if (!monster_is_valid(t_ptr)) continue;
87
88                         if (is_pet(m_ptr))
89                         {
90                                 /* Hack -- only fight away from player */
91                                 if (p_ptr->pet_follow_distance < 0)
92                                 {
93                                         /* No fighting near player */
94                                         if (t_ptr->cdis <= (0 - p_ptr->pet_follow_distance))
95                                         {
96                                                 continue;
97                                         }
98                                 }
99                                 /* Hack -- no fighting away from player */
100                                 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > p_ptr->pet_follow_distance))
101                                 {
102                                         continue;
103                                 }
104
105                                 if (r_ptr->aaf < t_ptr->cdis) continue;
106                         }
107
108                         /* Monster must be 'an enemy' */
109                         if (!are_enemies(m_ptr, t_ptr)) continue;
110
111                         /* Monster must be projectable if we can't pass through walls */
112                         if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) ||
113                             ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)))
114                         {
115                                 if (!in_disintegration_range(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
116                         }
117                         else
118                         {
119                                 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
120                         }
121
122                         /* OK -- we've got a target */
123                         y = t_ptr->fy;
124                         x = t_ptr->fx;
125
126                         break;
127                 }
128                 if (!x && !y) return FALSE;
129         }
130
131         /* Extract the direction */
132         x -= m_ptr->fx;
133         y -= m_ptr->fy;
134
135         /* North */
136         if ((y < 0) && (x == 0))
137         {
138                 mm[0] = 8;
139                 mm[1] = 7;
140                 mm[2] = 9;
141         }
142         /* South */
143         else if ((y > 0) && (x == 0))
144         {
145                 mm[0] = 2;
146                 mm[1] = 1;
147                 mm[2] = 3;
148         }
149         /* East */
150         else if ((x > 0) && (y == 0))
151         {
152                 mm[0] = 6;
153                 mm[1] = 9;
154                 mm[2] = 3;
155         }
156         /* West */
157         else if ((x < 0) && (y == 0))
158         {
159                 mm[0] = 4;
160                 mm[1] = 7;
161                 mm[2] = 1;
162         }
163         /* North-West */
164         else if ((y < 0) && (x < 0))
165         {
166                 mm[0] = 7;
167                 mm[1] = 4;
168                 mm[2] = 8;
169         }
170         /* North-East */
171         else if ((y < 0) && (x > 0))
172         {
173                 mm[0] = 9;
174                 mm[1] = 6;
175                 mm[2] = 8;
176         }
177         /* South-West */
178         else if ((y > 0) && (x < 0))
179         {
180                 mm[0] = 1;
181                 mm[1] = 4;
182                 mm[2] = 2;
183         }
184         /* South-East */
185         else if ((y > 0) && (x > 0))
186         {
187                 mm[0] = 3;
188                 mm[1] = 6;
189                 mm[2] = 2;
190         }
191
192         /* Found a monster */
193         return TRUE;
194 }
195
196 /*!
197  * @brief モンスターがプレイヤーから逃走するかどうかを返す /
198  * Returns whether a given monster will try to run from the player.
199  * @param m_idx 逃走するモンスターの参照ID
200  * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
201  * @details
202  * Monsters will attempt to avoid very powerful players.  See below.\n
203  *\n
204  * Because this function is called so often, little details are important\n
205  * for efficiency.  Like not using "mod" or "div" when possible.  And\n
206  * attempting to check the conditions in an optimal order.  Note that\n
207  * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
208  *\n
209  * Note that this function is responsible for about one to five percent\n
210  * of the processor use in normal conditions...\n
211  */
212 static bool mon_will_run(MONSTER_IDX m_idx)
213 {
214         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
215
216 #ifdef ALLOW_TERROR
217
218         monster_race *r_ptr = &r_info[m_ptr->r_idx];
219
220         PLAYER_LEVEL p_lev;
221         DEPTH m_lev;
222         HIT_POINT p_chp, p_mhp;
223         HIT_POINT m_chp, m_mhp;
224         u32b p_val, m_val;
225
226 #endif
227
228         /* Friends can be commanded to avoid the player */
229         if (is_pet(m_ptr))
230         {
231                 /* Are we trying to avoid the player? */
232                 return ((p_ptr->pet_follow_distance < 0) &&
233                                   (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
234         }
235
236         /* Keep monsters from running too far away */
237         if (m_ptr->cdis > MAX_SIGHT + 5) return (FALSE);
238
239         /* All "afraid" monsters will run away */
240         if (MON_MONFEAR(m_ptr)) return (TRUE);
241
242 #ifdef ALLOW_TERROR
243
244         /* Nearby monsters will not become terrified */
245         if (m_ptr->cdis <= 5) return (FALSE);
246
247         /* Examine player power (level) */
248         p_lev = p_ptr->lev;
249
250         /* Examine monster power (level plus morale) */
251         m_lev = r_ptr->level + (m_idx & 0x08) + 25;
252
253         /* Optimize extreme cases below */
254         if (m_lev > p_lev + 4) return (FALSE);
255         if (m_lev + 4 <= p_lev) return (TRUE);
256
257         /* Examine player health */
258         p_chp = p_ptr->chp;
259         p_mhp = p_ptr->mhp;
260
261         /* Examine monster health */
262         m_chp = m_ptr->hp;
263         m_mhp = m_ptr->maxhp;
264
265         /* Prepare to optimize the calculation */
266         p_val = (p_lev * p_mhp) + (p_chp << 2); /* div p_mhp */
267         m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
268
269         /* Strong players scare strong monsters */
270         if (p_val * m_mhp > m_val * p_mhp) return (TRUE);
271
272 #endif
273
274         /* Assume no terror */
275         return (FALSE);
276 }
277
278
279 /*!
280  * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
281  * Search spell castable grid
282  * @param m_idx モンスターの参照ID
283  * @param yp 適したマスのY座標を返す参照ポインタ
284  * @param xp 適したマスのX座標を返す参照ポインタ
285  * @return 有効なマスがあった場合TRUEを返す
286  */
287 static bool get_moves_aux2(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
288 {
289         int i, best = 999;
290         POSITION y, x, y1, x1;
291
292         grid_type *g_ptr;
293         bool can_open_door = FALSE;
294         int now_cost;
295
296         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
297         monster_race *r_ptr = &r_info[m_ptr->r_idx];
298
299         /* Monster location */
300         y1 = m_ptr->fy;
301         x1 = m_ptr->fx;
302
303         /* Monster can already cast spell to player */
304         if (projectable(y1, x1, p_ptr->y, p_ptr->x)) return (FALSE);
305
306         /* Set current grid cost */
307         now_cost = current_floor_ptr->grid_array[y1][x1].cost;
308         if (now_cost == 0) now_cost = 999;
309
310         /* Can monster bash or open doors? */
311         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
312         {
313                 can_open_door = TRUE;
314         }
315
316         /* Check nearby grids, diagonals first */
317         for (i = 7; i >= 0; i--)
318         {
319                 int cost;
320
321                 y = y1 + ddy_ddd[i];
322                 x = x1 + ddx_ddd[i];
323
324                 /* Ignore locations off of edge */
325                 if (!in_bounds2(y, x)) continue;
326
327                 /* Simply move to player */
328                 if (player_bold(y, x)) return (FALSE);
329
330                 g_ptr = &current_floor_ptr->grid_array[y][x];
331
332                 cost = g_ptr->cost;
333
334                 /* Monster cannot kill or pass walls */
335                 if (!(((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) || ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding))))
336                 {
337                         if (cost == 0) continue;
338                         if (!can_open_door && is_closed_door(g_ptr->feat)) continue;
339                 }
340
341                 /* Hack -- for kill or pass wall monster.. */
342                 if (cost == 0) cost = 998;
343
344                 if (now_cost < cost) continue;
345
346                 if (!projectable(y, x, p_ptr->y, p_ptr->x)) continue;
347
348                 /* Accept louder sounds */
349                 if (best < cost) continue;
350                 best = cost;
351
352                 (*yp) = y1 + ddy_ddd[i];
353                 (*xp) = x1 + ddx_ddd[i];
354         }
355
356         /* No legal move (?) */
357         if (best == 999) return (FALSE);
358
359         /* Success */
360         return (TRUE);
361 }
362
363
364 /*!
365  * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
366  * Choose the "best" direction for "flowing"
367  * @param m_idx モンスターの参照ID
368  * @param yp 移動先のマスのY座標を返す参照ポインタ
369  * @param xp 移動先のマスのX座標を返す参照ポインタ
370  * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
371  * @return 有効なマスがあった場合TRUEを返す
372  * @details
373  * Note that ghosts and rock-eaters are never allowed to "flow",\n
374  * since they should move directly towards the player.\n
375  *\n
376  * Prefer "non-diagonal" directions, but twiddle them a little\n
377  * to angle slightly towards the player's actual location.\n
378  *\n
379  * Allow very perceptive monsters to track old "spoor" left by\n
380  * previous locations occupied by the player.  This will tend\n
381  * to have monsters end up either near the player or on a grid\n
382  * recently occupied by the player (and left via "teleport").\n
383  *\n
384  * Note that if "smell" is turned on, all monsters get vicious.\n
385  *\n
386  * Also note that teleporting away from a location will cause\n
387  * the monsters who were chasing you to converge on that location\n
388  * as long as you are still near enough to "annoy" them without\n
389  * being close enough to chase directly.  I have no idea what will\n
390  * happen if you combine "smell" with low "aaf" values.\n
391  */
392 static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
393 {
394         int i, best;
395         POSITION y, x, y1, x1;
396
397         grid_type *g_ptr;
398         bool use_scent = FALSE;
399
400         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
401         monster_race *r_ptr = &r_info[m_ptr->r_idx];
402
403         /* Can monster cast attack spell? */
404         if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
405             r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
406             r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
407         {
408                 /* Can move spell castable grid? */
409                 if (get_moves_aux2(m_idx, yp, xp)) return (TRUE);
410         }
411
412         /* Monster can't flow */
413         if (no_flow) return (FALSE);
414
415         /* Monster can go through rocks */
416         if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) return (FALSE);
417         if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)) return (FALSE);
418
419         /* Monster location */
420         y1 = m_ptr->fy;
421         x1 = m_ptr->fx;
422
423         /* Hack -- Player can see us, run towards him */
424         if (player_has_los_bold(y1, x1) && projectable(p_ptr->y, p_ptr->x, y1, x1)) return (FALSE);
425
426         /* Monster grid */
427         g_ptr = &current_floor_ptr->grid_array[y1][x1];
428
429         /* If we can hear noises, advance towards them */
430         if (g_ptr->cost)
431         {
432                 best = 999;
433         }
434
435         /* Otherwise, try to follow a scent trail */
436         else if (g_ptr->when)
437         {
438                 /* Too old smell */
439                 if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when - g_ptr->when > 127) return (FALSE);
440
441                 use_scent = TRUE;
442                 best = 0;
443         }
444
445         /* Otherwise, advance blindly */
446         else
447         {
448                 return (FALSE);
449         }
450
451         /* Check nearby grids, diagonals first */
452         for (i = 7; i >= 0; i--)
453         {
454                 y = y1 + ddy_ddd[i];
455                 x = x1 + ddx_ddd[i];
456
457                 /* Ignore locations off of edge */
458                 if (!in_bounds2(y, x)) continue;
459
460                 g_ptr = &current_floor_ptr->grid_array[y][x];
461
462                 /* We're following a scent trail */
463                 if (use_scent)
464                 {
465                         int when = g_ptr->when;
466
467                         /* Accept younger scent */
468                         if (best > when) continue;
469                         best = when;
470                 }
471
472                 /* We're using sound */
473                 else
474                 {
475                         int cost;
476
477                         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
478                                 cost = g_ptr->dist;
479                         else cost = g_ptr->cost;
480
481                         /* Accept louder sounds */
482                         if ((cost == 0) || (best < cost)) continue;
483                         best = cost;
484                 }
485
486                 /* Hack -- Save the "twiddled" location */
487                 (*yp) = p_ptr->y + 16 * ddy_ddd[i];
488                 (*xp) = p_ptr->x + 16 * ddx_ddd[i];
489         }
490
491         /* No legal move (?) */
492         if (best == 999 || best == 0) return (FALSE);
493
494         /* Success */
495         return (TRUE);
496 }
497
498
499 /*!
500  * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
501  * Provide a location to flee to, but give the player a wide berth.
502  * @param m_idx モンスターの参照ID
503  * @param yp 移動先のマスのY座標を返す参照ポインタ
504  * @param xp 移動先のマスのX座標を返す参照ポインタ
505  * @return 有効なマスがあった場合TRUEを返す
506  * @details
507  * A monster may wish to flee to a location that is behind the player,\n
508  * but instead of heading directly for it, the monster should "swerve"\n
509  * around the player so that he has a smaller chance of getting hit.\n
510  */
511 static bool get_fear_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
512 {
513         POSITION y, x, y1, x1, fy, fx, gy = 0, gx = 0;
514         int score = -1;
515         int i;
516
517         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
518
519         /* Monster location */
520         fy = m_ptr->fy;
521         fx = m_ptr->fx;
522
523         /* Desired destination */
524         y1 = fy - (*yp);
525         x1 = fx - (*xp);
526
527         /* Check nearby grids, diagonals first */
528         for (i = 7; i >= 0; i--)
529         {
530                 POSITION dis, s;
531
532                 y = fy + ddy_ddd[i];
533                 x = fx + ddx_ddd[i];
534
535                 /* Ignore locations off of edge */
536                 if (!in_bounds2(y, x)) continue;
537
538                 /* Don't move toward player */
539                 /* if (current_floor_ptr->grid_array[y][x].dist < 3) continue; */ /* Hmm.. Need it? */
540
541                 /* Calculate distance of this grid from our destination */
542                 dis = distance(y, x, y1, x1);
543
544                 /* Score this grid */
545                 s = 5000 / (dis + 3) - 500 / (current_floor_ptr->grid_array[y][x].dist + 1);
546
547                 /* No negative scores */
548                 if (s < 0) s = 0;
549
550                 /* Ignore lower scores */
551                 if (s < score) continue;
552
553                 /* Save the score and time */
554                 score = s;
555
556                 /* Save the location */
557                 gy = y;
558                 gx = x;
559         }
560
561         /* No legal move (?) */
562         if (score == -1) return (FALSE);
563
564         /* Find deltas */
565         (*yp) = fy - gy;
566         (*xp) = fx - gx;
567
568         /* Success */
569         return (TRUE);
570 }
571
572 /*
573  * Hack -- Precompute a bunch of calls to distance() in find_safety() and
574  * find_hiding().
575  *
576  * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
577  * offsets of all the locations with a distance of n from a central point,
578  * with an offset of (0,0) indicating no more offsets at this distance.
579  *
580  * This is, of course, fairly unreadable, but it eliminates multiple loops
581  * from the previous version.
582  *
583  * It is probably better to replace these arrays with code to compute
584  * the relevant arrays, even if the storage is pre-allocated in hard
585  * coded sizes.  At the very least, code should be included which is
586  * able to generate and dump these arrays (ala "los()").  
587  *
588  * Also, the storage needs could be halved by using bytes.  
589  *
590  * These arrays could be combined into two big arrays, using sub-arrays
591  * to hold the offsets and lengths of each portion of the sub-arrays, and
592  * this could perhaps also be used somehow in the "look" code.  
593  */
594
595
596 static POSITION d_off_y_0[] = { 0 };
597 static POSITION d_off_x_0[] = { 0 };
598
599 static POSITION d_off_y_1[] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
600 static POSITION d_off_x_1[] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
601
602 static POSITION d_off_y_2[] = { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
603 static POSITION d_off_x_2[] = { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
604
605 static POSITION d_off_y_3[] = { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2, 3, 3, 3, 0 };
606 static POSITION d_off_x_3[] = { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2, -1, 0, 1, 0 };
607
608 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 };
609 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 };
610
611
612 static POSITION d_off_y_5[] =
613 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
614   -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
615   5, 0 };
616
617 static POSITION d_off_x_5[] =
618 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
619   -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
620   0, 1, 0 };
621
622
623 static POSITION d_off_y_6[] =
624 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
625   -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
626   5, 5, 6, 6, 6, 0 };
627
628 static POSITION d_off_x_6[] =
629 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
630   0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
631   -3, 2, 3, -1, 0, 1, 0 };
632
633
634 static POSITION d_off_y_7[] =
635 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
636   -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
637   3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
638
639 static POSITION d_off_x_7[] =
640 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
641   -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
642   6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
643   1, 0 };
644
645
646 static POSITION d_off_y_8[] =
647 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
648   -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
649   2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
650   8, 8, 8, 0 };
651
652 static POSITION d_off_x_8[] =
653 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
654   5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
655   7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
656   2, 3, -1, 0, 1, 0 };
657
658
659 static POSITION d_off_y_9[] =
660 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
661   -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
662   0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
663   7, 8, 8, 8, 8, 9, 9, 9, 0 };
664
665 static POSITION d_off_x_9[] =
666 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
667   -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
668   9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
669   4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
670
671
672 static POSITION *dist_offsets_y[10] =
673 {
674         d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
675         d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
676 };
677
678 static POSITION *dist_offsets_x[10] =
679 {
680         d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
681         d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
682 };
683
684 /*!
685  * @brief モンスターが逃げ込める安全な地点を返す /
686  * Choose a "safe" location near a monster for it to run toward.
687  * @param m_idx モンスターの参照ID
688  * @param yp 移動先のマスのY座標を返す参照ポインタ
689  * @param xp 移動先のマスのX座標を返す参照ポインタ
690  * @return 有効なマスがあった場合TRUEを返す
691  * @details
692  * A location is "safe" if it can be reached quickly and the player\n
693  * is not able to fire into it (it isn't a "clean shot").  So, this will\n
694  * cause monsters to "duck" behind walls.  Hopefully, monsters will also\n
695  * try to run towards corridor openings if they are in a room.\n
696  *\n
697  * This function may take lots of CPU time if lots of monsters are\n
698  * fleeing.\n
699  *\n
700  * Return TRUE if a safe location is available.\n
701  */
702 static bool find_safety(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
703 {
704         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
705
706         POSITION fy = m_ptr->fy;
707         POSITION fx = m_ptr->fx;
708
709         POSITION y, x, dy, dx, d, dis, i;
710         POSITION gy = 0, gx = 0, gdis = 0;
711
712         POSITION *y_offsets;
713         POSITION *x_offsets;
714
715         grid_type *g_ptr;
716
717         /* Start with adjacent locations, spread further */
718         for (d = 1; d < 10; d++)
719         {
720                 /* Get the lists of points with a distance d from (fx, fy) */
721                 y_offsets = dist_offsets_y[d];
722                 x_offsets = dist_offsets_x[d];
723
724                 /* Check the locations */
725                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
726                      dx != 0 || dy != 0;
727                      i++, dx = x_offsets[i], dy = y_offsets[i])
728                 {
729                         y = fy + dy;
730                         x = fx + dx;
731
732                         /* Skip illegal locations */
733                         if (!in_bounds(y, x)) continue;
734
735                         g_ptr = &current_floor_ptr->grid_array[y][x];
736
737                         /* Skip locations in a wall */
738                         if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == p_ptr->riding) ? CEM_RIDING : 0)) continue;
739
740                         /* Check for "availability" (if monsters can flow) */
741                         if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
742                         {
743                                 /* Ignore grids very far from the player */
744                                 if (g_ptr->dist == 0) continue;
745
746                                 /* Ignore too-distant grids */
747                                 if (g_ptr->dist > current_floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
748                         }
749
750                         /* Check for absence of shot (more or less) */
751                         if (!projectable(p_ptr->y, p_ptr->x, y, x))
752                         {
753                                 /* Calculate distance from player */
754                                 dis = distance(y, x, p_ptr->y, p_ptr->x);
755
756                                 /* Remember if further than previous */
757                                 if (dis > gdis)
758                                 {
759                                         gy = y;
760                                         gx = x;
761                                         gdis = dis;
762                                 }
763                         }
764                 }
765
766                 /* Check for success */
767                 if (gdis > 0)
768                 {
769                         /* Good location */
770                         (*yp) = fy - gy;
771                         (*xp) = fx - gx;
772
773                         /* Found safe place */
774                         return (TRUE);
775                 }
776         }
777
778         /* No safe place */
779         return (FALSE);
780 }
781
782
783 /*!
784  * @brief モンスターが隠れ潜める地点を返す /
785  * Choose a good hiding place near a monster for it to run toward.
786  * @param m_idx モンスターの参照ID
787  * @param yp 移動先のマスのY座標を返す参照ポインタ
788  * @param xp 移動先のマスのX座標を返す参照ポインタ
789  * @return 有効なマスがあった場合TRUEを返す
790  * @details
791  * Pack monsters will use this to "ambush" the player and lure him out\n
792  * of corridors into open space so they can swarm him.\n
793  *\n
794  * Return TRUE if a good location is available.\n
795  */
796 static bool find_hiding(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
797 {
798         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
799         monster_race *r_ptr = &r_info[m_ptr->r_idx];
800
801         POSITION fy = m_ptr->fy;
802         POSITION fx = m_ptr->fx;
803
804         POSITION y, x, dy, dx, d, dis, i;
805         POSITION gy = 0, gx = 0, gdis = 999;
806
807         POSITION *y_offsets, *x_offsets;
808
809         /* Start with adjacent locations, spread further */
810         for (d = 1; d < 10; d++)
811         {
812                 /* Get the lists of points with a distance d from (fx, fy) */
813                 y_offsets = dist_offsets_y[d];
814                 x_offsets = dist_offsets_x[d];
815
816                 /* Check the locations */
817                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
818                      dx != 0 || dy != 0;
819                      i++, dx = x_offsets[i], dy = y_offsets[i])
820                 {
821                         y = fy + dy;
822                         x = fx + dx;
823
824                         /* Skip illegal locations */
825                         if (!in_bounds(y, x)) continue;
826
827                         /* Skip occupied locations */
828                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
829
830                         /* Check for hidden, available grid */
831                         if (!projectable(p_ptr->y, p_ptr->x, y, x) && clean_shot(fy, fx, y, x, FALSE))
832                         {
833                                 /* Calculate distance from player */
834                                 dis = distance(y, x, p_ptr->y, p_ptr->x);
835
836                                 /* Remember if closer than previous */
837                                 if (dis < gdis && dis >= 2)
838                                 {
839                                         gy = y;
840                                         gx = x;
841                                         gdis = dis;
842                                 }
843                         }
844                 }
845
846                 /* Check for success */
847                 if (gdis < 999)
848                 {
849                         /* Good location */
850                         (*yp) = fy - gy;
851                         (*xp) = fx - gx;
852
853                         /* Found good place */
854                         return (TRUE);
855                 }
856         }
857
858         /* No good place */
859         return (FALSE);
860 }
861
862
863 /*!
864  * @brief モンスターの移動方向を返す /
865  * Choose "logical" directions for monster movement
866  * @param m_idx モンスターの参照ID
867  * @param mm 移動方向を返す方向IDの参照ポインタ
868  * @return 有効方向があった場合TRUEを返す
869  */
870 static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
871 {
872         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
873         monster_race *r_ptr = &r_info[m_ptr->r_idx];
874         POSITION     y = 0, ay, x = 0, ax;
875         int          move_val = 0;
876         POSITION     y2 = p_ptr->y;
877         POSITION     x2 = p_ptr->x;
878         bool         done = FALSE;
879         bool         will_run = mon_will_run(m_idx);
880         grid_type    *g_ptr;
881         bool         no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
882         bool         can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall));
883
884         /* Counter attack to an enemy monster */
885         if (!will_run && m_ptr->target_y)
886         {
887                 int t_m_idx = current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
888
889                 /* The monster must be an enemy, and in LOS */
890                 if (t_m_idx &&
891                     are_enemies(m_ptr, &current_floor_ptr->m_list[t_m_idx]) &&
892                     los(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
893                     projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
894                 {
895                         /* Extract the "pseudo-direction" */
896                         y = m_ptr->fy - m_ptr->target_y;
897                         x = m_ptr->fx - m_ptr->target_x;
898                         done = TRUE;
899                 }
900         }
901
902         if (!done && !will_run && is_hostile(m_ptr) &&
903             (r_ptr->flags1 & RF1_FRIENDS) &&
904             ((los(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x) && projectable(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x)) ||
905             (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
906         {
907         /*
908          * Animal packs try to get the player out of corridors
909          * (...unless they can move through walls -- TY)
910          */
911                 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
912                          !(r_ptr->flags2 & RF2_KILL_WALL))
913                 {
914                         int i, room = 0;
915
916                         /* Count room grids next to player */
917                         for (i = 0; i < 8; i++)
918                         {
919                                 int xx = p_ptr->x + ddx_ddd[i];
920                                 int yy = p_ptr->y + ddy_ddd[i];
921
922                                 if (!in_bounds2(yy, xx)) continue;
923
924                                 g_ptr = &current_floor_ptr->grid_array[yy][xx];
925
926                                 /* Check grid */
927                                 if (monster_can_cross_terrain(g_ptr->feat, r_ptr, 0))
928                                 {
929                                         /* One more room grid */
930                                         room++;
931                                 }
932                         }
933                         if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_ROOM) room -= 2;
934                         if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
935
936                         /* Not in a room and strong player */
937                         if (room < (8 * (p_ptr->chp + p_ptr->csp)) /
938                             (p_ptr->mhp + p_ptr->msp))
939                         {
940                                 /* Find hiding place */
941                                 if (find_hiding(m_idx, &y, &x)) done = TRUE;
942                         }
943                 }
944
945                 /* Monster groups try to surround the player */
946                 if (!done && (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
947                 {
948                         int i;
949
950                         /* Find an empty square near the player to fill */
951                         for (i = 0; i < 8; i++)
952                         {
953                                 /* Pick squares near player (semi-randomly) */
954                                 y2 = p_ptr->y + ddy_ddd[(m_idx + i) & 7];
955                                 x2 = p_ptr->x + ddx_ddd[(m_idx + i) & 7];
956
957                                 /* Already there? */
958                                 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
959                                 {
960                                         /* Attack the player */
961                                         y2 = p_ptr->y;
962                                         x2 = p_ptr->x;
963
964                                         break;
965                                 }
966
967                                 if (!in_bounds2(y2, x2)) continue;
968
969                                 /* Ignore filled grids */
970                                 if (!monster_can_enter(y2, x2, r_ptr, 0)) continue;
971
972                                 /* Try to fill this hole */
973                                 break;
974                         }
975
976                         /* Extract the new "pseudo-direction" */
977                         y = m_ptr->fy - y2;
978                         x = m_ptr->fx - x2;
979
980                         done = TRUE;
981                 }
982         }
983
984         if (!done)
985         {
986                 /* Flow towards the player */
987                 (void)get_moves_aux(m_idx, &y2, &x2, no_flow);
988
989                 /* Extract the "pseudo-direction" */
990                 y = m_ptr->fy - y2;
991                 x = m_ptr->fx - x2;
992
993                 /* Not done */
994         }
995
996         /* Apply fear if possible and necessary */
997         if (is_pet(m_ptr) && will_run)
998         {
999                 /* XXX XXX Not very "smart" */
1000                 y = (-y), x = (-x);
1001         }
1002         else
1003         {
1004                 if (!done && will_run)
1005                 {
1006                         int tmp_x = (-x);
1007                         int tmp_y = (-y);
1008
1009                         /* Try to find safe place */
1010                         if (find_safety(m_idx, &y, &x))
1011                         {
1012                                 /* Attempt to avoid the player */
1013                                 if (!no_flow)
1014                                 {
1015                                         /* Adjust movement */
1016                                         if (get_fear_moves_aux(m_idx, &y, &x)) done = TRUE;
1017                                 }
1018                         }
1019
1020                         if (!done)
1021                         {
1022                                 /* This is not a very "smart" method XXX XXX */
1023                                 y = tmp_y;
1024                                 x = tmp_x;
1025                         }
1026                 }
1027         }
1028
1029
1030         /* Check for no move */
1031         if (!x && !y) return (FALSE);
1032
1033
1034         /* Extract the "absolute distances" */
1035         ax = ABS(x);
1036         ay = ABS(y);
1037
1038         /* Do something weird */
1039         if (y < 0) move_val += 8;
1040         if (x > 0) move_val += 4;
1041
1042         /* Prevent the diamond maneuvre */
1043         if (ay > (ax << 1)) move_val += 2;
1044         else if (ax > (ay << 1)) move_val++;
1045
1046         /* Extract some directions */
1047         switch (move_val)
1048         {
1049         case 0:
1050                 mm[0] = 9;
1051                 if (ay > ax)
1052                 {
1053                         mm[1] = 8;
1054                         mm[2] = 6;
1055                         mm[3] = 7;
1056                         mm[4] = 3;
1057                 }
1058                 else
1059                 {
1060                         mm[1] = 6;
1061                         mm[2] = 8;
1062                         mm[3] = 3;
1063                         mm[4] = 7;
1064                 }
1065                 break;
1066         case 1:
1067         case 9:
1068                 mm[0] = 6;
1069                 if (y < 0)
1070                 {
1071                         mm[1] = 3;
1072                         mm[2] = 9;
1073                         mm[3] = 2;
1074                         mm[4] = 8;
1075                 }
1076                 else
1077                 {
1078                         mm[1] = 9;
1079                         mm[2] = 3;
1080                         mm[3] = 8;
1081                         mm[4] = 2;
1082                 }
1083                 break;
1084         case 2:
1085         case 6:
1086                 mm[0] = 8;
1087                 if (x < 0)
1088                 {
1089                         mm[1] = 9;
1090                         mm[2] = 7;
1091                         mm[3] = 6;
1092                         mm[4] = 4;
1093                 }
1094                 else
1095                 {
1096                         mm[1] = 7;
1097                         mm[2] = 9;
1098                         mm[3] = 4;
1099                         mm[4] = 6;
1100                 }
1101                 break;
1102         case 4:
1103                 mm[0] = 7;
1104                 if (ay > ax)
1105                 {
1106                         mm[1] = 8;
1107                         mm[2] = 4;
1108                         mm[3] = 9;
1109                         mm[4] = 1;
1110                 }
1111                 else
1112                 {
1113                         mm[1] = 4;
1114                         mm[2] = 8;
1115                         mm[3] = 1;
1116                         mm[4] = 9;
1117                 }
1118                 break;
1119         case 5:
1120         case 13:
1121                 mm[0] = 4;
1122                 if (y < 0)
1123                 {
1124                         mm[1] = 1;
1125                         mm[2] = 7;
1126                         mm[3] = 2;
1127                         mm[4] = 8;
1128                 }
1129                 else
1130                 {
1131                         mm[1] = 7;
1132                         mm[2] = 1;
1133                         mm[3] = 8;
1134                         mm[4] = 2;
1135                 }
1136                 break;
1137         case 8:
1138                 mm[0] = 3;
1139                 if (ay > ax)
1140                 {
1141                         mm[1] = 2;
1142                         mm[2] = 6;
1143                         mm[3] = 1;
1144                         mm[4] = 9;
1145                 }
1146                 else
1147                 {
1148                         mm[1] = 6;
1149                         mm[2] = 2;
1150                         mm[3] = 9;
1151                         mm[4] = 1;
1152                 }
1153                 break;
1154         case 10:
1155         case 14:
1156                 mm[0] = 2;
1157                 if (x < 0)
1158                 {
1159                         mm[1] = 3;
1160                         mm[2] = 1;
1161                         mm[3] = 6;
1162                         mm[4] = 4;
1163                 }
1164                 else
1165                 {
1166                         mm[1] = 1;
1167                         mm[2] = 3;
1168                         mm[3] = 4;
1169                         mm[4] = 6;
1170                 }
1171                 break;
1172         case 12:
1173                 mm[0] = 1;
1174                 if (ay > ax)
1175                 {
1176                         mm[1] = 2;
1177                         mm[2] = 4;
1178                         mm[3] = 3;
1179                         mm[4] = 7;
1180                 }
1181                 else
1182                 {
1183                         mm[1] = 4;
1184                         mm[2] = 2;
1185                         mm[3] = 7;
1186                         mm[4] = 3;
1187                 }
1188                 break;
1189         }
1190
1191         /* Wants to move... */
1192         return (TRUE);
1193 }
1194
1195
1196 /*!
1197  * @brief モンスターから敵モンスターへの命中判定
1198  * @param power 打撃属性による基本命中値
1199  * @param level 攻撃側モンスターのレベル
1200  * @param ac 目標モンスターのAC
1201  * @param stun 攻撃側モンスターが朦朧状態ならTRUEを返す
1202  * @return 命中ならばTRUEを返す
1203  */
1204 static int check_hit2(int power, DEPTH level, ARMOUR_CLASS ac, int stun)
1205 {
1206         int i, k;
1207
1208         /* Percentile dice */
1209         k = randint0(100);
1210
1211         if (stun && one_in_(2)) return FALSE;
1212
1213         /* Hack -- Always miss or hit */
1214         if (k < 10) return (k < 5);
1215
1216         /* Calculate the "attack quality" */
1217         i = (power + (level * 3));
1218
1219         /* Power and Level compete against Armor */
1220         if ((i > 0) && (randint1(i) > ((ac * 3) / 4))) return (TRUE);
1221
1222         /* Assume miss */
1223         return (FALSE);
1224 }
1225
1226
1227 #define BLOW_EFFECT_TYPE_NONE  0
1228 #define BLOW_EFFECT_TYPE_FEAR  1
1229 #define BLOW_EFFECT_TYPE_SLEEP 2
1230 #define BLOW_EFFECT_TYPE_HEAL  3
1231
1232
1233 /*!
1234  * @brief モンスターから敵モンスターへの打撃攻撃処理
1235  * @param m_idx 攻撃側モンスターの参照ID
1236  * @param t_idx 目標側モンスターの参照ID
1237  * @return 実際に打撃処理が行われた場合TRUEを返す
1238  */
1239 static bool monst_attack_monst(MONSTER_IDX m_idx, MONSTER_IDX t_idx)
1240 {
1241         monster_type    *m_ptr = &current_floor_ptr->m_list[m_idx];
1242         monster_type    *t_ptr = &current_floor_ptr->m_list[t_idx];
1243
1244         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
1245         monster_race    *tr_ptr = &r_info[t_ptr->r_idx];
1246
1247         ARMOUR_CLASS ap_cnt;
1248         ARMOUR_CLASS ac;
1249         DEPTH rlev;
1250         int pt;
1251         GAME_TEXT m_name[MAX_NLEN], t_name[MAX_NLEN];
1252         char            temp[MAX_NLEN];
1253         bool            blinked;
1254         bool            explode = FALSE, touched = FALSE, fear = FALSE, dead = FALSE;
1255         POSITION y_saver = t_ptr->fy;
1256         POSITION x_saver = t_ptr->fx;
1257         int             effect_type;
1258
1259         bool see_m = is_seen(m_ptr);
1260         bool see_t = is_seen(t_ptr);
1261         bool see_either = see_m || see_t;
1262
1263         /* Can the player be aware of this attack? */
1264         bool known = (m_ptr->cdis <= MAX_SIGHT) || (t_ptr->cdis <= MAX_SIGHT);
1265         bool do_silly_attack = (one_in_(2) && p_ptr->image);
1266
1267         /* Cannot attack self */
1268         if (m_idx == t_idx) return FALSE;
1269
1270         /* Not allowed to attack */
1271         if (r_ptr->flags1 & RF1_NEVER_BLOW) return FALSE;
1272
1273         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE) return (FALSE);
1274
1275         /* Total armor */
1276         ac = tr_ptr->ac;
1277
1278         /* Extract the effective monster level */
1279         rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1280
1281         monster_desc(m_name, m_ptr, 0);
1282         monster_desc(t_name, t_ptr, 0);
1283
1284         /* Assume no blink */
1285         blinked = FALSE;
1286
1287         if (!see_either && known)
1288         {
1289                 mon_fight = TRUE;
1290         }
1291
1292         if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
1293
1294         /* Scan through all four blows */
1295         for (ap_cnt = 0; ap_cnt < 4; ap_cnt++)
1296         {
1297                 bool obvious = FALSE;
1298
1299                 HIT_POINT power = 0;
1300                 HIT_POINT damage = 0;
1301
1302                 concptr act = NULL;
1303
1304                 /* Extract the attack infomation */
1305                 int effect = r_ptr->blow[ap_cnt].effect;
1306                 int method = r_ptr->blow[ap_cnt].method;
1307                 int d_dice = r_ptr->blow[ap_cnt].d_dice;
1308                 int d_side = r_ptr->blow[ap_cnt].d_side;
1309
1310                 if (!monster_is_valid(m_ptr)) break;
1311
1312                 /* Stop attacking if the target dies! */
1313                 if (t_ptr->fx != x_saver || t_ptr->fy != y_saver)
1314                         break;
1315
1316                 /* Hack -- no more attacks */
1317                 if (!method) break;
1318
1319                 if (method == RBM_SHOOT) continue;
1320
1321                 /* Extract the attack "power" */
1322                 power = mbe_info[effect].power;
1323
1324                 /* Monster hits */
1325                 if (!effect || check_hit2(power, rlev, ac, MON_STUNNED(m_ptr)))
1326                 {
1327                         (void)set_monster_csleep(t_idx, 0);
1328
1329                         if (t_ptr->ml)
1330                         {
1331                                 /* Redraw the health bar */
1332                                 if (p_ptr->health_who == t_idx) p_ptr->redraw |= (PR_HEALTH);
1333                                 if (p_ptr->riding == t_idx) p_ptr->redraw |= (PR_UHEALTH);
1334                         }
1335
1336                         /* Describe the attack method */
1337                         switch (method)
1338                         {
1339                         case RBM_HIT:
1340                                 {
1341                                         act = _("%sを殴った。", "hits %s.");
1342                                         touched = TRUE;
1343                                         break;
1344                                 }
1345
1346                         case RBM_TOUCH:
1347                                 {
1348                                         act = _("%sを触った。", "touches %s.");
1349                                         touched = TRUE;
1350                                         break;
1351                                 }
1352
1353                         case RBM_PUNCH:
1354                                 {
1355                                         act = _("%sをパンチした。", "punches %s.");
1356                                         touched = TRUE;
1357                                         break;
1358                                 }
1359
1360                         case RBM_KICK:
1361                                 {
1362                                         act = _("%sを蹴った。", "kicks %s.");
1363                                         touched = TRUE;
1364                                         break;
1365                                 }
1366
1367                         case RBM_CLAW:
1368                                 {
1369                                         act = _("%sをひっかいた。", "claws %s.");
1370                                         touched = TRUE;
1371                                         break;
1372                                 }
1373
1374                         case RBM_BITE:
1375                                 {
1376                                         act = _("%sを噛んだ。", "bites %s.");
1377                                         touched = TRUE;
1378                                         break;
1379                                 }
1380
1381                         case RBM_STING:
1382                                 {
1383                                         act = _("%sを刺した。", "stings %s.");
1384                                         touched = TRUE;
1385                                         break;
1386                                 }
1387
1388                         case RBM_SLASH:
1389                                 {
1390                                         act = _("%sを斬った。", "slashes %s.");
1391                                         break;
1392                                 }
1393
1394                         case RBM_BUTT:
1395                                 {
1396                                         act = _("%sを角で突いた。", "butts %s.");
1397                                         touched = TRUE;
1398                                         break;
1399                                 }
1400
1401                         case RBM_CRUSH:
1402                                 {
1403                                         act = _("%sに体当りした。", "crushes %s.");
1404                                         touched = TRUE;
1405                                         break;
1406                                 }
1407
1408                         case RBM_ENGULF:
1409                                 {
1410                                         act = _("%sを飲み込んだ。", "engulfs %s.");
1411                                         touched = TRUE;
1412                                         break;
1413                                 }
1414
1415                         case RBM_CHARGE:
1416                                 {
1417                                         act = _("%sに請求書をよこした。", "charges %s.");
1418                                         touched = TRUE;
1419                                         break;
1420                                 }
1421
1422                         case RBM_CRAWL:
1423                                 {
1424                                         act = _("%sの体の上を這い回った。", "crawls on %s.");
1425                                         touched = TRUE;
1426                                         break;
1427                                 }
1428
1429                         case RBM_DROOL:
1430                                 {
1431                                         act = _("%sによだれをたらした。", "drools on %s.");
1432                                         touched = FALSE;
1433                                         break;
1434                                 }
1435
1436                         case RBM_SPIT:
1437                                 {
1438                                         act = _("%sに唾を吐いた。", "spits on %s.");
1439                                         touched = FALSE;
1440                                         break;
1441                                 }
1442
1443                         case RBM_EXPLODE:
1444                                 {
1445                                         if (see_either) disturb(TRUE, TRUE);
1446                                         act = _("爆発した。", "explodes.");
1447                                         explode = TRUE;
1448                                         touched = FALSE;
1449                                         break;
1450                                 }
1451
1452                         case RBM_GAZE:
1453                                 {
1454                                         act = _("%sをにらんだ。", "gazes at %s.");
1455                                         touched = FALSE;
1456                                         break;
1457                                 }
1458
1459                         case RBM_WAIL:
1460                                 {
1461                                         act = _("%sに泣きついた。", "wails at %s.");
1462                                         touched = FALSE;
1463                                         break;
1464                                 }
1465
1466                         case RBM_SPORE:
1467                                 {
1468                                         act = _("%sに胞子を飛ばした。", "releases spores at %s.");
1469                                         touched = FALSE;
1470                                         break;
1471                                 }
1472
1473                         case RBM_XXX4:
1474                                 {
1475                                         act = _("%sにXXX4を飛ばした。", "projects XXX4's at %s.");
1476                                         touched = FALSE;
1477                                         break;
1478                                 }
1479
1480                         case RBM_BEG:
1481                                 {
1482                                         act = _("%sに金をせがんだ。", "begs %s for money.");
1483                                         touched = FALSE;
1484                                         break;
1485                                 }
1486
1487                         case RBM_INSULT:
1488                                 {
1489                                         act = _("%sを侮辱した。", "insults %s.");
1490                                         touched = FALSE;
1491                                         break;
1492                                 }
1493
1494                         case RBM_MOAN:
1495                                 {
1496                                         act = _("%sにむかってうめいた。", "moans at %s.");
1497                                         touched = FALSE;
1498                                         break;
1499                                 }
1500
1501                         case RBM_SHOW:
1502                                 {
1503                                         act = _("%sにむかって歌った。", "sings to %s.");
1504                                         touched = FALSE;
1505                                         break;
1506                                 }
1507                         }
1508
1509                         if (act && see_either)
1510                         {
1511 #ifdef JP
1512                                 if (do_silly_attack) act = silly_attacks2[randint0(MAX_SILLY_ATTACK)];
1513                                 strfmt(temp, act, t_name);
1514                                 msg_format("%^sは%s", m_name, temp);
1515 #else
1516                                 if (do_silly_attack)
1517                                 {
1518                                         act = silly_attacks[randint0(MAX_SILLY_ATTACK)];
1519                                         strfmt(temp, "%s %s.", act, t_name);
1520                                 }
1521                                 else strfmt(temp, act, t_name);
1522                                 msg_format("%^s %s", m_name, temp);
1523 #endif
1524                         }
1525
1526                         /* Hack -- assume all attacks are obvious */
1527                         obvious = TRUE;
1528
1529                         /* Roll out the damage */
1530                         damage = damroll(d_dice, d_side);
1531
1532                         /* Assume no effect */
1533                         effect_type = BLOW_EFFECT_TYPE_NONE;
1534
1535                         pt = GF_MISSILE;
1536
1537                         /* Apply appropriate damage */
1538                         switch (effect)
1539                         {
1540                         case 0:
1541                         case RBE_DR_MANA:
1542                                 damage = pt = 0;
1543                                 break;
1544
1545                         case RBE_SUPERHURT:
1546                                 if ((randint1(rlev*2+250) > (ac+200)) || one_in_(13))
1547                                 {
1548                                         int tmp_damage = damage - (damage * ((ac < 150) ? ac : 150) / 250);
1549                                         damage = MAX(damage, tmp_damage * 2);
1550                                         break;
1551                                 }
1552
1553                                 /* Fall through */
1554
1555                         case RBE_HURT:
1556                                 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1557                                 break;
1558
1559                         case RBE_POISON:
1560                         case RBE_DISEASE:
1561                                 pt = GF_POIS;
1562                                 break;
1563
1564                         case RBE_UN_BONUS:
1565                         case RBE_UN_POWER:
1566                                 pt = GF_DISENCHANT;
1567                                 break;
1568
1569                         case RBE_EAT_ITEM:
1570                         case RBE_EAT_GOLD:
1571                                 if ((p_ptr->riding != m_idx) && one_in_(2)) blinked = TRUE;
1572                                 break;
1573
1574                         case RBE_EAT_FOOD:
1575                         case RBE_EAT_LITE:
1576                         case RBE_BLIND:
1577                         case RBE_LOSE_STR:
1578                         case RBE_LOSE_INT:
1579                         case RBE_LOSE_WIS:
1580                         case RBE_LOSE_DEX:
1581                         case RBE_LOSE_CON:
1582                         case RBE_LOSE_CHR:
1583                         case RBE_LOSE_ALL:
1584                                 break;
1585
1586                         case RBE_ACID:
1587                                 pt = GF_ACID;
1588                                 break;
1589
1590                         case RBE_ELEC:
1591                                 pt = GF_ELEC;
1592                                 break;
1593
1594                         case RBE_FIRE:
1595                                 pt = GF_FIRE;
1596                                 break;
1597
1598                         case RBE_COLD:
1599                                 pt = GF_COLD;
1600                                 break;
1601
1602                         case RBE_CONFUSE:
1603                                 pt = GF_CONFUSION;
1604                                 break;
1605
1606                         case RBE_TERRIFY:
1607                                 effect_type = BLOW_EFFECT_TYPE_FEAR;
1608                                 break;
1609
1610                         case RBE_PARALYZE:
1611                                 effect_type = BLOW_EFFECT_TYPE_SLEEP;
1612                                 break;
1613
1614                         case RBE_SHATTER:
1615                                 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1616                                 if (damage > 23) earthquake_aux(m_ptr->fy, m_ptr->fx, 8, m_idx);
1617                                 break;
1618
1619                         case RBE_EXP_10:
1620                         case RBE_EXP_20:
1621                         case RBE_EXP_40:
1622                         case RBE_EXP_80:
1623                                 pt = GF_NETHER;
1624                                 break;
1625
1626                         case RBE_TIME:
1627                                 pt = GF_TIME;
1628                                 break;
1629
1630                         case RBE_DR_LIFE:
1631                                 pt = GF_HYPODYNAMIA;
1632                                 effect_type = BLOW_EFFECT_TYPE_HEAL;
1633                                 break;
1634
1635                         case RBE_INERTIA:
1636                                 pt = GF_INERTIAL;
1637                                 break;
1638
1639                         case RBE_STUN:
1640                                 pt = GF_SOUND;
1641                                 break;
1642
1643                         default:
1644                                 pt = 0;
1645                                 break;
1646                         }
1647
1648                         if (pt)
1649                         {
1650                                 /* Do damage if not exploding */
1651                                 if (!explode)
1652                                 {
1653                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1654                                                 damage, pt, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1655                                 }
1656
1657                                 switch (effect_type)
1658                                 {
1659                                 case BLOW_EFFECT_TYPE_FEAR:
1660                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1661                                                 damage, GF_TURN_ALL, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1662                                         break;
1663
1664                                 case BLOW_EFFECT_TYPE_SLEEP:
1665                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1666                                                 r_ptr->level, GF_OLD_SLEEP, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1667                                         break;
1668
1669                                 case BLOW_EFFECT_TYPE_HEAL:
1670                                         if ((monster_living(m_idx)) && (damage > 2))
1671                                         {
1672                                                 bool did_heal = FALSE;
1673
1674                                                 if (m_ptr->hp < m_ptr->maxhp) did_heal = TRUE;
1675
1676                                                 /* Heal */
1677                                                 m_ptr->hp += damroll(4, damage / 6);
1678                                                 if (m_ptr->hp > m_ptr->maxhp) m_ptr->hp = m_ptr->maxhp;
1679
1680                                                 /* Redraw (later) if needed */
1681                                                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
1682                                                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
1683
1684                                                 /* Special message */
1685                                                 if (see_m && did_heal)
1686                                                 {
1687                                                         msg_format(_("%sは体力を回復したようだ。", "%^s appears healthier."), m_name);
1688                                                 }
1689                                         }
1690                                         break;
1691                                 }
1692
1693                                 if (touched)
1694                                 {
1695                                         /* Aura fire */
1696                                         if ((tr_ptr->flags2 & RF2_AURA_FIRE) && m_ptr->r_idx)
1697                                         {
1698                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK))
1699                                                 {
1700                                                         if (see_either)
1701                                                         {
1702                                                                 msg_format(_("%^sは突然熱くなった!", "%^s is suddenly very hot!"), m_name);
1703                                                         }
1704                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_FIRE;
1705                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1706                                                                 damroll (1 + ((tr_ptr->level) / 26),
1707                                                                 1 + ((tr_ptr->level) / 17)),
1708                                                                 GF_FIRE, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1709                                                 }
1710                                                 else
1711                                                 {
1712                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK);
1713                                                 }
1714                                         }
1715
1716                                         /* Aura cold */
1717                                         if ((tr_ptr->flags3 & RF3_AURA_COLD) && m_ptr->r_idx)
1718                                         {
1719                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK))
1720                                                 {
1721                                                         if (see_either)
1722                                                         {
1723                                                                 msg_format(_("%^sは突然寒くなった!", "%^s is suddenly very cold!"), m_name);
1724                                                         }
1725                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags3 |= RF3_AURA_COLD;
1726                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1727                                                                 damroll (1 + ((tr_ptr->level) / 26),
1728                                                                 1 + ((tr_ptr->level) / 17)),
1729                                                                 GF_COLD, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1730                                                 }
1731                                                 else
1732                                                 {
1733                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_COLD_MASK);
1734                                                 }
1735                                         }
1736
1737                                         /* Aura elec */
1738                                         if ((tr_ptr->flags2 & RF2_AURA_ELEC) && m_ptr->r_idx)
1739                                         {
1740                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK))
1741                                                 {
1742                                                         if (see_either)
1743                                                         {
1744                                                                 msg_format(_("%^sは電撃を食らった!", "%^s gets zapped!"), m_name);
1745                                                         }
1746                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_ELEC;
1747                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1748                                                                 damroll (1 + ((tr_ptr->level) / 26),
1749                                                                 1 + ((tr_ptr->level) / 17)),
1750                                                                 GF_ELEC, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1751                                                 }
1752                                                 else
1753                                                 {
1754                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK);
1755                                                 }
1756                                         }
1757                                 }
1758                         }
1759                 }
1760
1761                 /* Monster missed player */
1762                 else
1763                 {
1764                         /* Analyze failed attacks */
1765                         switch (method)
1766                         {
1767                         case RBM_HIT:
1768                         case RBM_TOUCH:
1769                         case RBM_PUNCH:
1770                         case RBM_KICK:
1771                         case RBM_CLAW:
1772                         case RBM_BITE:
1773                         case RBM_STING:
1774                         case RBM_SLASH:
1775                         case RBM_BUTT:
1776                         case RBM_CRUSH:
1777                         case RBM_ENGULF:
1778                         case RBM_CHARGE:
1779                                 {
1780                                         (void)set_monster_csleep(t_idx, 0);
1781
1782                                         /* Visible monsters */
1783                                         if (see_m)
1784                                         {
1785 #ifdef JP
1786                                                 msg_format("%sは%^sの攻撃をかわした。", t_name, m_name);
1787 #else
1788                                                 msg_format("%^s misses %s.", m_name, t_name);
1789 #endif
1790                                         }
1791
1792                                         break;
1793                                 }
1794                         }
1795                 }
1796
1797
1798                 /* Analyze "visible" monsters only */
1799                 if (is_original_ap_and_seen(m_ptr) && !do_silly_attack)
1800                 {
1801                         /* Count "obvious" attacks (and ones that cause damage) */
1802                         if (obvious || damage || (r_ptr->r_blows[ap_cnt] > 10))
1803                         {
1804                                 /* Count attacks of this type */
1805                                 if (r_ptr->r_blows[ap_cnt] < MAX_UCHAR)
1806                                 {
1807                                         r_ptr->r_blows[ap_cnt]++;
1808                                 }
1809                         }
1810                 }
1811         }
1812
1813         if (explode)
1814         {
1815                 sound(SOUND_EXPLODE);
1816
1817                 /* Cancel Invulnerability */
1818                 (void)set_monster_invulner(m_idx, 0, FALSE);
1819                 mon_take_hit_mon(m_idx, m_ptr->hp + 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1820                 blinked = FALSE;
1821         }
1822
1823         /* Blink away */
1824         if (blinked && m_ptr->r_idx)
1825         {
1826                 if (teleport_barrier(m_idx))
1827                 {
1828                         if (see_m)
1829                         {
1830                                 msg_print(_("泥棒は笑って逃げ...ようとしたがバリアに防がれた。", "The thief flees laughing...? But magic barrier obstructs it."));
1831                         }
1832                         else if (known)
1833                         {
1834                                 mon_fight = TRUE;
1835                         }
1836                 }
1837                 else
1838                 {
1839                         if (see_m)
1840                         {
1841                                 msg_print(_("泥棒は笑って逃げた!", "The thief flees laughing!"));
1842                         }
1843                         else if (known)
1844                         {
1845                                 mon_fight = TRUE;
1846                         }
1847
1848                         teleport_away(m_idx, MAX_SIGHT * 2 + 5, 0L);
1849                 }
1850         }
1851
1852         return TRUE;
1853 }
1854
1855
1856 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
1857 {
1858         return !have_flag(f_ptr->flags, FF_GLASS) ||
1859                 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
1860                 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
1861 }
1862
1863
1864 /*!
1865  * @brief モンスター単体の1ターン行動処理メインルーチン /
1866  * Process a monster
1867  * @param m_idx 行動モンスターの参照ID
1868  * @return なし
1869  * @details
1870  * The monster is known to be within 100 grids of the player\n
1871  *\n
1872  * In several cases, we directly update the monster lore\n
1873  *\n
1874  * Note that a monster is only allowed to "reproduce" if there\n
1875  * are a limited number of "reproducing" monsters on the current\n
1876  * level.  This should prevent the level from being "swamped" by\n
1877  * reproducing monsters.  It also allows a large mass of mice to\n
1878  * prevent a louse from multiplying, but this is a small price to\n
1879  * pay for a simple multiplication method.\n
1880  *\n
1881  * XXX Monster fear is slightly odd, in particular, monsters will\n
1882  * fixate on opening a door even if they cannot open it.  Actually,\n
1883  * the same thing happens to normal monsters when they hit a door\n
1884  *\n
1885  * In addition, monsters which *cannot* open or bash\n
1886  * down a door will still stand there trying to open it...\n
1887  *\n
1888  * XXX Technically, need to check for monster in the way\n
1889  * combined with that monster being in a wall (or door?)\n
1890  *\n
1891  * A "direction" of "5" means "pick a random direction".\n
1892  */
1893 void process_monster(MONSTER_IDX m_idx)
1894 {
1895         monster_type    *m_ptr = &current_floor_ptr->m_list[m_idx];
1896         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
1897         monster_race    *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1898
1899         int i, d;
1900         POSITION oy, ox, ny, nx;
1901
1902         DIRECTION mm[8];
1903
1904         grid_type       *g_ptr;
1905         feature_type    *f_ptr;
1906
1907         monster_type    *y_ptr;
1908
1909         bool            do_turn;
1910         bool            do_move;
1911         bool            do_view;
1912         bool            must_alter_to_move;
1913
1914         bool            did_open_door;
1915         bool            did_bash_door;
1916         bool            did_take_item;
1917         bool            did_kill_item;
1918         bool            did_move_body;
1919         bool            did_pass_wall;
1920         bool            did_kill_wall;
1921         bool            gets_angry = FALSE;
1922         bool            can_cross;
1923         bool            aware = TRUE;
1924
1925         bool fear, dead;
1926         bool is_riding_mon = (m_idx == p_ptr->riding);
1927         bool see_m = is_seen(m_ptr);
1928
1929         if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
1930         {
1931                 if (rakuba(0, TRUE))
1932                 {
1933 #ifdef JP
1934                         msg_print("地面に落とされた。");
1935 #else
1936                         GAME_TEXT m_name[MAX_NLEN];
1937                         monster_desc(m_name, &current_floor_ptr->m_list[p_ptr->riding], 0);
1938                         msg_format("You have fallen from %s.", m_name);
1939 #endif
1940                 }
1941         }
1942
1943         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
1944         {
1945                 choose_new_monster(m_idx, FALSE, 0);
1946                 r_ptr = &r_info[m_ptr->r_idx];
1947         }
1948
1949         /* Players hidden in shadow are almost imperceptable. -LM- */
1950         if (p_ptr->special_defense & NINJA_S_STEALTH)
1951         {
1952                 int tmp = p_ptr->lev*6+(p_ptr->skill_stl+10)*4;
1953                 if (p_ptr->monlite) tmp /= 3;
1954                 if (p_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1955                 if (r_ptr->level > (p_ptr->lev * p_ptr->lev / 20 + 10)) tmp /= 3;
1956                 /* Low-level monsters will find it difficult to locate the player. */
1957                 if (randint0(tmp) > (r_ptr->level+20)) aware = FALSE;
1958         }
1959
1960         /* Are there its parent? */
1961         if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
1962         {
1963                 /* Its parent have gone, it also goes away. */
1964
1965                 if (see_m)
1966                 {
1967                         GAME_TEXT m_name[MAX_NLEN];
1968                         monster_desc(m_name, m_ptr, 0);
1969                         msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
1970                 }
1971
1972                 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
1973                 {
1974                         GAME_TEXT m_name[MAX_NLEN];
1975                         monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
1976                         do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
1977                 }
1978
1979                 delete_monster_idx(m_idx);
1980
1981                 return;
1982         }
1983
1984         /* Quantum monsters are odd */
1985         if (r_ptr->flags2 & (RF2_QUANTUM))
1986         {
1987                 /* Sometimes skip move */
1988                 if (!randint0(2)) return;
1989
1990                 /* Sometimes die */
1991                 if (!randint0((m_idx % 100) + 10) && !(r_ptr->flags1 & RF1_QUESTOR))
1992                 {
1993                         bool sad = FALSE;
1994
1995                         if (is_pet(m_ptr) && !(m_ptr->ml)) sad = TRUE;
1996
1997                         if (see_m)
1998                         {
1999                                 GAME_TEXT m_name[MAX_NLEN];
2000                                 monster_desc(m_name, m_ptr, 0);
2001
2002                                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2003                         }
2004
2005                         /* Generate treasure, etc */
2006                         monster_death(m_idx, FALSE);
2007
2008                         delete_monster_idx(m_idx);
2009                         if (sad)
2010                         {
2011                                 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
2012                         }
2013
2014                         return;
2015                 }
2016         }
2017
2018         if (m_ptr->r_idx == MON_SHURYUUDAN)
2019         {
2020                 mon_take_hit_mon(m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
2021                 if(dead) return;
2022         }
2023
2024         if ((is_pet(m_ptr) || is_friendly(m_ptr)) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && !p_ptr->inside_battle)
2025         {
2026                 static int riding_pinch = 0;
2027
2028                 if (m_ptr->hp < m_ptr->maxhp/3)
2029                 {
2030                         GAME_TEXT m_name[MAX_NLEN];
2031                         monster_desc(m_name, m_ptr, 0);
2032
2033                         if (is_riding_mon && riding_pinch < 2)
2034                         {
2035                                 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
2036                                                          "%^s seems to be in so much pain, and trying to escape from your restriction."), m_name);
2037                                 riding_pinch++;
2038                                 disturb(TRUE, TRUE);
2039                         }
2040                         else
2041                         {
2042                                 if (is_riding_mon)
2043                                 {
2044                                         msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
2045                                         if (rakuba(-1, FALSE))
2046                                         {
2047                                                 msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
2048                                         }
2049                                 }
2050
2051                                 if (see_m)
2052                                 {
2053                                         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) &&
2054                                             player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x))
2055                                         {
2056                                                 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
2057                                         }
2058                                         msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s read a scroll of teleport level."), m_name);
2059                                         msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
2060                                 }
2061
2062                                 if (is_riding_mon && rakuba(-1, FALSE))
2063                                 {
2064                                         msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
2065                                 }
2066
2067                                 check_quest_completion(m_ptr);
2068                                 delete_monster_idx(m_idx);
2069                                 return;
2070                         }
2071                 }
2072                 else
2073                 {
2074                         /* Reset the counter */
2075                         if (is_riding_mon) riding_pinch = 0;
2076                 }
2077         }
2078
2079         /* Handle "sleep" */
2080         if (MON_CSLEEP(m_ptr))
2081         {
2082                 /* Handle non-aggravation - Still sleeping */
2083                 if (!(p_ptr->cursed & TRC_AGGRAVATE)) return;
2084
2085                 (void)set_monster_csleep(m_idx, 0);
2086
2087                 /* Notice the "waking up" */
2088                 if (m_ptr->ml)
2089                 {
2090                         GAME_TEXT m_name[MAX_NLEN];
2091                         monster_desc(m_name, m_ptr, 0);
2092                         msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
2093                 }
2094
2095                 /* Hack -- Count the wakings */
2096                 if (is_original_ap_and_seen(m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
2097                 {
2098                         r_ptr->r_wake++;
2099                 }
2100         }
2101
2102         /* Handle "stun" */
2103         if (MON_STUNNED(m_ptr))
2104         {
2105                 /* Sometimes skip move */
2106                 if (one_in_(2)) return;
2107         }
2108
2109         if (is_riding_mon)
2110         {
2111                 p_ptr->update |= (PU_BONUS);
2112         }
2113
2114         /* No one wants to be your friend if you're aggravating */
2115         if (is_friendly(m_ptr) && (p_ptr->cursed & TRC_AGGRAVATE))
2116                 gets_angry = TRUE;
2117
2118         /* Paranoia... no pet uniques outside wizard mode -- TY */
2119         if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
2120               monster_has_hostile_align(NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
2121         {
2122                 gets_angry = TRUE;
2123         }
2124
2125         if (p_ptr->inside_battle) gets_angry = FALSE;
2126
2127         if (gets_angry)
2128         {
2129                 if (is_pet(m_ptr) || see_m)
2130                 {
2131                         GAME_TEXT m_name[MAX_NLEN];
2132                         monster_desc(m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
2133                         msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
2134                 }
2135
2136                 set_hostile(m_ptr);
2137         }
2138
2139         /* Get the origin */
2140         oy = m_ptr->fy;
2141         ox = m_ptr->fx;
2142
2143         /* Attempt to "multiply" if able and allowed */
2144         if ((r_ptr->flags2 & RF2_MULTIPLY) && (current_floor_ptr->num_repro < MAX_REPRO))
2145         {
2146                 int k;
2147                 POSITION y, x;
2148
2149                 /* Count the adjacent monsters */
2150                 for (k = 0, y = oy - 1; y <= oy + 1; y++)
2151                 {
2152                         for (x = ox - 1; x <= ox + 1; x++)
2153                         {
2154                                 /* Ignore locations off of edge */
2155                                 if (!in_bounds2(y, x)) continue;
2156                                 if (current_floor_ptr->grid_array[y][x].m_idx) k++;
2157                         }
2158                 }
2159
2160                 /* Hex */
2161                 if (multiply_barrier(m_idx)) k = 8;
2162
2163                 /* Hack -- multiply slower in crowded areas */
2164                 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2165                 {
2166                         /* Try to multiply */
2167                         if (multiply_monster(m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
2168                         {
2169                                 /* Take note if visible */
2170                                 if (current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(m_ptr))
2171                                 {
2172                                         r_ptr->r_flags2 |= (RF2_MULTIPLY);
2173                                 }
2174
2175                                 /* Multiplying takes energy */
2176                                 return;
2177                         }
2178                 }
2179         }
2180
2181         if (r_ptr->a_ability_flags2 & RF6_SPECIAL)
2182         {
2183                 /* Hack -- Ohmu scatters molds! */
2184                 if (m_ptr->r_idx == MON_OHMU)
2185                 {
2186                         if (!p_ptr->inside_arena && !p_ptr->inside_battle)
2187                         {
2188                                 if (r_ptr->freq_spell && (randint1(100) <= r_ptr->freq_spell))
2189                                 {
2190                                         int k, count = 0;
2191                                         DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
2192                                         BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
2193
2194                                         for (k = 0; k < A_MAX; k++)
2195                                         {
2196                                                 if (summon_specific(m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode), '\0'))
2197                                                 {
2198                                                         if (current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
2199                                                 }
2200                                         }
2201
2202                                         if (count && is_original_ap_and_seen(m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
2203                                 }
2204                         }
2205                 }
2206         }
2207
2208         if (!p_ptr->inside_battle)
2209         {
2210                 /* Hack! "Cyber" monster makes noise... */
2211                 if (m_ptr->ap_r_idx == MON_CYBER &&
2212                     one_in_(CYBERNOISE) &&
2213                     !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2214                 {
2215                         if (disturb_minor) disturb(FALSE, FALSE);
2216                         msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
2217                 }
2218
2219                 /* Some monsters can speak */
2220                 if ((ap_r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
2221                     one_in_(SPEAK_CHANCE) &&
2222                     player_has_los_bold(oy, ox) &&
2223                     projectable(oy, ox, p_ptr->y, p_ptr->x))
2224                 {
2225                         GAME_TEXT m_name[MAX_NLEN];
2226                         char monmessage[1024];
2227                         concptr filename;
2228
2229                         /* Acquire the monster name/poss */
2230                         if (m_ptr->ml)
2231                                 monster_desc(m_name, m_ptr, 0);
2232                         else
2233                                 strcpy(m_name, _("それ", "It"));
2234
2235                         /* Select the file for monster quotes */
2236                         if (MON_MONFEAR(m_ptr))
2237                                 filename = _("monfear_j.txt", "monfear.txt");
2238                         else if (is_pet(m_ptr))
2239                                 filename = _("monpet_j.txt", "monpet.txt");
2240                         else if (is_friendly(m_ptr))
2241                                 filename = _("monfrien_j.txt", "monfrien.txt");
2242                         else
2243                                 filename = _("monspeak_j.txt", "monspeak.txt");
2244                         /* Get the monster line */
2245                         if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
2246                         {
2247                                 /* Say something */
2248                                 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
2249                         }
2250                 }
2251         }
2252
2253         /* Try to cast spell occasionally */
2254         if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
2255         {
2256                 bool counterattack = FALSE;
2257
2258                 /* Give priority to counter attack? */
2259                 if (m_ptr->target_y)
2260                 {
2261                         MONSTER_IDX t_m_idx = current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
2262
2263                         /* The monster must be an enemy, and projectable */
2264                         if (t_m_idx && are_enemies(m_ptr, &current_floor_ptr->m_list[t_m_idx]) &&
2265                             projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
2266                         {
2267                                 counterattack = TRUE;
2268                         }
2269                 }
2270
2271                 if (!counterattack)
2272                 {
2273                         /* Attempt to cast a spell */
2274                         if (aware && make_attack_spell(m_idx)) return;
2275
2276                         /*
2277                          * Attempt to cast a spell at an enemy other than the player
2278                          * (may slow the game a smidgeon, but I haven't noticed.)
2279                          */
2280                         if (monst_spell_monst(m_idx)) return;
2281                 }
2282                 else
2283                 {
2284                         /* Attempt to do counter attack at first */
2285                         if (monst_spell_monst(m_idx)) return;
2286
2287                         if (aware && make_attack_spell(m_idx)) return;
2288                 }
2289         }
2290
2291         /* Hack -- Assume no movement */
2292         mm[0] = mm[1] = mm[2] = mm[3] = 0;
2293         mm[4] = mm[5] = mm[6] = mm[7] = 0;
2294
2295
2296         /* Confused -- 100% random */
2297         if (MON_CONFUSED(m_ptr) || !aware)
2298         {
2299                 /* Try four "random" directions */
2300                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2301         }
2302
2303         /* 75% random movement */
2304         else if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) &&
2305                  (randint0(100) < 75))
2306         {
2307                 /* Memorize flags */
2308                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
2309
2310                 /* Try four "random" directions */
2311                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2312         }
2313
2314         /* 50% random movement */
2315         else if ((r_ptr->flags1 & RF1_RAND_50) &&
2316                                 (randint0(100) < 50))
2317         {
2318                 /* Memorize flags */
2319                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
2320
2321                 /* Try four "random" directions */
2322                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2323         }
2324
2325         /* 25% random movement */
2326         else if ((r_ptr->flags1 & RF1_RAND_25) &&
2327                                 (randint0(100) < 25))
2328         {
2329                 /* Memorize flags */
2330                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
2331
2332                 /* Try four "random" directions */
2333                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2334         }
2335
2336         /* Can't reach player - find something else to hit */
2337         else if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2338         {
2339                 /* Try four "random" directions */
2340                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2341
2342                 /* Look for an enemy */
2343 #if 0  /* Hack - Too slow.  Mimic pits are horrible with this on. */
2344                 get_enemy_dir(m_idx, mm);
2345 #endif /* 0 */
2346         }
2347
2348         /* Pets will follow the player */
2349         else if (is_pet(m_ptr))
2350         {
2351                 /* Are we trying to avoid the player? */
2352                 bool avoid = ((p_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
2353
2354                 /* Do we want to find the player? */
2355                 bool lonely = (!avoid && (m_ptr->cdis > p_ptr->pet_follow_distance));
2356
2357                 /* Should we find the player if we can't find a monster? */
2358                 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
2359
2360                 /* by default, move randomly */
2361                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2362
2363                 /* Look for an enemy */
2364                 if (!get_enemy_dir(m_idx, mm))
2365                 {
2366                         /* Find the player if necessary */
2367                         if (avoid || lonely || distant)
2368                         {
2369                                 /* Remember the leash length */
2370                                 POSITION dis = p_ptr->pet_follow_distance;
2371
2372                                 /* Hack -- adjust follow distance temporarily */
2373                                 if (p_ptr->pet_follow_distance > PET_SEEK_DIST)
2374                                 {
2375                                         p_ptr->pet_follow_distance = PET_SEEK_DIST;
2376                                 }
2377
2378                                 /* Find the player */
2379                                 (void)get_moves(m_idx, mm);
2380
2381                                 /* Restore the leash */
2382                                 p_ptr->pet_follow_distance = (s16b)dis;
2383                         }
2384                 }
2385         }
2386
2387         /* Friendly monster movement */
2388         else if (!is_hostile(m_ptr))
2389         {
2390                 /* by default, move randomly */
2391                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2392
2393                 /* Look for an enemy */
2394                 get_enemy_dir(m_idx, mm);
2395         }
2396         /* Normal movement */
2397         else
2398         {
2399                 /* Logical moves, may do nothing */
2400                 if (!get_moves(m_idx, mm)) return;
2401         }
2402
2403         /* Assume nothing */
2404         do_turn = FALSE;
2405         do_move = FALSE;
2406         do_view = FALSE;
2407         must_alter_to_move = FALSE;
2408
2409         /* Assume nothing */
2410         did_open_door = FALSE;
2411         did_bash_door = FALSE;
2412         did_take_item = FALSE;
2413         did_kill_item = FALSE;
2414         did_move_body = FALSE;
2415         did_pass_wall = FALSE;
2416         did_kill_wall = FALSE;
2417
2418         /* Take a zero-terminated array of "directions" */
2419         for (i = 0; mm[i]; i++)
2420         {
2421                 /* Get the direction */
2422                 d = mm[i];
2423
2424                 /* Hack -- allow "randomized" motion */
2425                 if (d == 5) d = ddd[randint0(8)];
2426
2427                 /* Get the destination */
2428                 ny = oy + ddy[d];
2429                 nx = ox + ddx[d];
2430
2431                 /* Ignore locations off of edge */
2432                 if (!in_bounds2(ny, nx)) continue;
2433
2434                 /* Access that grid */
2435                 g_ptr = &current_floor_ptr->grid_array[ny][nx];
2436                 f_ptr = &f_info[g_ptr->feat];
2437                 can_cross = monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
2438
2439                 /* Access that grid's contents */
2440                 y_ptr = &current_floor_ptr->m_list[g_ptr->m_idx];
2441
2442                 /* Hack -- player 'in' wall */
2443                 if (player_bold(ny, nx))
2444                 {
2445                         do_move = TRUE;
2446                 }
2447
2448                 /* Possibly a monster to attack */
2449                 else if (g_ptr->m_idx)
2450                 {
2451                         do_move = TRUE;
2452                 }
2453
2454                 /* Monster destroys walls (and doors) */
2455                 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
2456                          (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
2457                          have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
2458                          check_hp_for_feat_destruction(f_ptr, m_ptr))
2459                 {
2460                         /* Eat through walls/doors/rubble */
2461                         do_move = TRUE;
2462                         if (!can_cross) must_alter_to_move = TRUE;
2463
2464                         /* Monster destroyed a wall (later) */
2465                         did_kill_wall = TRUE;
2466                 }
2467
2468                 /* Floor is open? */
2469                 else if (can_cross)
2470                 {
2471                         /* Go ahead and move */
2472                         do_move = TRUE;
2473
2474                         /* Monster moves through walls (and doors) */
2475                         if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || p_ptr->pass_wall) &&
2476                             have_flag(f_ptr->flags, FF_CAN_PASS))
2477                         {
2478                                 /* Monster went through a wall */
2479                                 did_pass_wall = TRUE;
2480                         }
2481                 }
2482
2483                 /* Handle doors and secret doors */
2484                 else if (is_closed_door(g_ptr->feat))
2485                 {
2486                         bool may_bash = TRUE;
2487
2488                         /* Assume no move allowed */
2489                         do_move = FALSE;
2490
2491                         /* Creature can open doors. */
2492                         if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
2493                                  (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2494                         {
2495                                 /* Closed doors */
2496                                 if (!f_ptr->power)
2497                                 {
2498                                         /* The door is open */
2499                                         did_open_door = TRUE;
2500
2501                                         /* Do not bash the door */
2502                                         may_bash = FALSE;
2503
2504                                         do_turn = TRUE;
2505                                 }
2506
2507                                 /* Locked doors (not jammed) */
2508                                 else
2509                                 {
2510                                         /* Try to unlock it */
2511                                         if (randint0(m_ptr->hp / 10) > f_ptr->power)
2512                                         {
2513                                                 /* Unlock the door */
2514                                                 cave_alter_feat(ny, nx, FF_DISARM);
2515
2516                                                 /* Do not bash the door */
2517                                                 may_bash = FALSE;
2518
2519                                                 do_turn = TRUE;
2520                                         }
2521                                 }
2522                         }
2523
2524                         /* Stuck doors -- attempt to bash them down if allowed */
2525                         if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
2526                                 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2527                         {
2528                                 /* Attempt to Bash */
2529                                 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
2530                                 {
2531                                         if (have_flag(f_ptr->flags, FF_GLASS))
2532                                                 msg_print(_("ガラスが砕ける音がした!", "You hear a glass was crashed!"));
2533                                         else
2534                                                 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
2535
2536                                         /* Disturb (sometimes) */
2537                                         if (disturb_minor) disturb(FALSE, FALSE);
2538
2539                                         /* The door was bashed open */
2540                                         did_bash_door = TRUE;
2541
2542                                         /* Hack -- fall into doorway */
2543                                         do_move = TRUE;
2544                                         must_alter_to_move = TRUE;
2545                                 }
2546                         }
2547
2548
2549                         /* Deal with doors in the way */
2550                         if (did_open_door || did_bash_door)
2551                         {
2552                                 /* Break down the door */
2553                                 if (did_bash_door && ((randint0(100) < 50) || (feat_state(g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
2554                                 {
2555                                         cave_alter_feat(ny, nx, FF_BASH);
2556
2557                                         if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
2558                                         {
2559                                                 p_ptr->update |= (PU_FLOW);
2560                                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2561                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
2562
2563                                                 return;
2564                                         }
2565                                 }
2566
2567                                 /* Open the door */
2568                                 else
2569                                 {
2570                                         cave_alter_feat(ny, nx, FF_OPEN);
2571                                 }
2572
2573                                 f_ptr = &f_info[g_ptr->feat];
2574
2575                                 /* Handle viewable doors */
2576                                 do_view = TRUE;
2577                         }
2578                 }
2579
2580                 /* Hack -- check for Glyph of Warding */
2581                 if (do_move && is_glyph_grid(g_ptr) &&
2582                     !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
2583                 {
2584                         /* Assume no move allowed */
2585                         do_move = FALSE;
2586
2587                         /* Break the ward */
2588                         if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
2589                         {
2590                                 /* Describe observable breakage */
2591                                 if (g_ptr->info & CAVE_MARK)
2592                                 {
2593                                         msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
2594                                 }
2595
2596                                 /* Forget the rune */
2597                                 g_ptr->info &= ~(CAVE_MARK);
2598
2599                                 /* Break the rune */
2600                                 g_ptr->info &= ~(CAVE_OBJECT);
2601                                 g_ptr->mimic = 0;
2602
2603                                 /* Allow movement */
2604                                 do_move = TRUE;
2605
2606                                 note_spot(ny, nx);
2607                         }
2608                 }
2609                 else if (do_move && is_explosive_rune_grid(g_ptr) &&
2610                          !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
2611                 {
2612                         /* Assume no move allowed */
2613                         do_move = FALSE;
2614
2615                         /* Break the ward */
2616                         if (!is_pet(m_ptr))
2617                         {
2618                                 /* Break the ward */
2619                                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
2620                                 {
2621                                         /* Describe observable breakage */
2622                                         if (g_ptr->info & CAVE_MARK)
2623                                         {
2624                                                 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
2625                                                 project(0, 2, ny, nx, 2 * (p_ptr->lev + damroll(7, 7)), GF_MANA, (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
2626                                         }
2627                                 }
2628                                 else
2629                                 {
2630                                         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
2631                                 }
2632
2633                                 /* Forget the rune */
2634                                 g_ptr->info &= ~(CAVE_MARK);
2635
2636                                 /* Break the rune */
2637                                 g_ptr->info &= ~(CAVE_OBJECT);
2638                                 g_ptr->mimic = 0;
2639
2640                                 note_spot(ny, nx);
2641                                 lite_spot(ny, nx);
2642
2643                                 if (!monster_is_valid(m_ptr)) return;
2644                                 /* Allow movement */
2645                                 do_move = TRUE;
2646                         }
2647                 }
2648
2649                 /* The player is in the way */
2650                 if (do_move && player_bold(ny, nx))
2651                 {
2652                         /* Some monsters never attack */
2653                         if (r_ptr->flags1 & RF1_NEVER_BLOW)
2654                         {
2655                                 /* Hack -- memorize lack of attacks */
2656                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
2657
2658                                 /* Do not move */
2659                                 do_move = FALSE;
2660                         }
2661
2662                         /* In anti-melee dungeon, stupid or confused monster takes useless current_world_ptr->game_turn */
2663                         if (do_move && (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE))
2664                         {
2665                                 if (!MON_CONFUSED(m_ptr))
2666                                 {
2667                                         if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
2668                                         else
2669                                         {
2670                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2671                                         }
2672                                 }
2673                         }
2674
2675                         /* The player is in the way.  Attack him. */
2676                         if (do_move)
2677                         {
2678                                 if (!p_ptr->riding || one_in_(2))
2679                                 {
2680                                         /* Do the attack */
2681                                         (void)make_attack_normal(m_idx);
2682
2683                                         /* Do not move */
2684                                         do_move = FALSE;
2685
2686                                         /* Took a current_world_ptr->game_turn */
2687                                         do_turn = TRUE;
2688                                 }
2689                         }
2690                 }
2691
2692                 /* A monster is in the way */
2693                 if (do_move && g_ptr->m_idx)
2694                 {
2695                         monster_race *z_ptr = &r_info[y_ptr->r_idx];
2696
2697                         /* Assume no movement */
2698                         do_move = FALSE;
2699
2700                         /* Attack 'enemies' */
2701                         if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
2702                                  (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
2703                                  can_cross && (g_ptr->m_idx != p_ptr->riding)) ||
2704                                 are_enemies(m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
2705                         {
2706                                 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
2707                                 {
2708                                         if (r_ptr->flags2 & RF2_KILL_BODY)
2709                                         {
2710                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
2711                                         }
2712
2713                                         /* attack */
2714                                         if (y_ptr->r_idx && (y_ptr->hp >= 0))
2715                                         {
2716                                                 if (monst_attack_monst(m_idx, g_ptr->m_idx)) return;
2717
2718                                                 /* In anti-melee dungeon, stupid or confused monster takes useless current_world_ptr->game_turn */
2719                                                 else if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
2720                                                 {
2721                                                         if (MON_CONFUSED(m_ptr)) return;
2722                                                         else if (r_ptr->flags2 & RF2_STUPID)
2723                                                         {
2724                                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2725                                                                 return;
2726                                                         }
2727                                                 }
2728                                         }
2729                                 }
2730                         }
2731
2732                         /* Push past weaker monsters (unless leaving a wall) */
2733                         else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
2734                                 (r_ptr->mexp > z_ptr->mexp) &&
2735                                 can_cross && (g_ptr->m_idx != p_ptr->riding) &&
2736                                 monster_can_cross_terrain(current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
2737                         {
2738                                 /* Allow movement */
2739                                 do_move = TRUE;
2740
2741                                 /* Monster pushed past another monster */
2742                                 did_move_body = TRUE;
2743
2744                                 /* Wake up the moved monster */
2745                                 (void)set_monster_csleep(g_ptr->m_idx, 0);
2746
2747                                 /* Message */
2748                         }
2749                 }
2750
2751                 if (is_riding_mon)
2752                 {
2753                         if (!p_ptr->riding_ryoute && !MON_MONFEAR(&current_floor_ptr->m_list[p_ptr->riding])) do_move = FALSE;
2754                 }
2755
2756                 if (did_kill_wall && do_move)
2757                 {
2758                         if (one_in_(GRINDNOISE))
2759                         {
2760                                 if (have_flag(f_ptr->flags, FF_GLASS))
2761                                         msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
2762                                 else
2763                                         msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
2764                         }
2765
2766                         cave_alter_feat(ny, nx, FF_HURT_DISI);
2767
2768                         if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
2769                         {
2770                                 p_ptr->update |= (PU_FLOW);
2771                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2772                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2773
2774                                 return;
2775                         }
2776
2777                         f_ptr = &f_info[g_ptr->feat];
2778
2779                         /* Note changes to viewable region */
2780                         do_view = TRUE;
2781                         do_turn = TRUE;
2782                 }
2783
2784                 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
2785                 {
2786                         if (!monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
2787                         {
2788                                 /* Assume no move allowed */
2789                                 do_move = FALSE;
2790                         }
2791                 }
2792
2793                 /*
2794                  * Check if monster can cross terrain
2795                  * This is checked after the normal attacks
2796                  * to allow monsters to attack an enemy,
2797                  * even if it can't enter the terrain.
2798                  */
2799                 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
2800                 {
2801                         /* Assume no move allowed */
2802                         do_move = FALSE;
2803                 }
2804
2805                 /* Some monsters never move */
2806                 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
2807                 {
2808                         /* Hack -- memorize lack of moves */
2809                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
2810
2811                         /* Do not move */
2812                         do_move = FALSE;
2813                 }
2814
2815                 /* Creature has been allowed move */
2816                 if (do_move)
2817                 {
2818                         do_turn = TRUE;
2819
2820                         if (have_flag(f_ptr->flags, FF_TREE))
2821                         {
2822                                 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
2823                                 {
2824                                         m_ptr->energy_need += ENERGY_NEED();
2825                                 }
2826                         }
2827
2828                         if (!is_riding_mon)
2829                         {
2830                                 /* Hack -- Update the old location */
2831                                 current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
2832
2833                                 /* Mega-Hack -- move the old monster, if any */
2834                                 if (g_ptr->m_idx)
2835                                 {
2836                                         /* Move the old monster */
2837                                         y_ptr->fy = oy;
2838                                         y_ptr->fx = ox;
2839
2840                                         /* Update the old monster */
2841                                         update_monster(g_ptr->m_idx, TRUE);
2842                                 }
2843
2844                                 /* Hack -- Update the new location */
2845                                 g_ptr->m_idx = m_idx;
2846
2847                                 /* Move the monster */
2848                                 m_ptr->fy = ny;
2849                                 m_ptr->fx = nx;
2850                                 update_monster(m_idx, TRUE);
2851
2852                                 lite_spot(oy, ox);
2853                                 lite_spot(ny, nx);
2854                         }
2855                         else
2856                         {
2857                                 /* sound(SOUND_WALK); */
2858                                 if (!move_player_effect(ny, nx, MPE_DONT_PICKUP)) break;
2859                         }
2860
2861                         /* Possible disturb */
2862                         if (m_ptr->ml &&
2863                             (disturb_move ||
2864                              (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
2865                              (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)))
2866                         {
2867                                 if (is_hostile(m_ptr))
2868                                         disturb(FALSE, TRUE);
2869                         }
2870
2871                         /* Take or Kill objects on the floor */
2872                         if (g_ptr->o_idx && (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
2873                             (!is_pet(m_ptr) || ((p_ptr->pet_extra_flags & PF_PICKUP_ITEMS) && (r_ptr->flags2 & RF2_TAKE_ITEM))))
2874                         {
2875                                 OBJECT_IDX this_o_idx, next_o_idx;
2876                                 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
2877
2878                                 /* Scan all objects in the grid */
2879                                 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
2880                                 {
2881                                         BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
2882                                         GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
2883                                         object_type *o_ptr = &current_floor_ptr->o_list[this_o_idx];
2884                                         next_o_idx = o_ptr->next_o_idx;
2885
2886                                         if (do_take)
2887                                         {
2888                                                 /* Skip gold */
2889                                                 if (o_ptr->tval == TV_GOLD) continue;
2890
2891                                                 /*
2892                                                  * Skip "real" corpses and statues, to avoid extreme
2893                                                  * silliness like a novice rogue pockets full of statues
2894                                                  * and corpses.
2895                                                  */
2896                                                 if ((o_ptr->tval == TV_CORPSE) ||
2897                                                     (o_ptr->tval == TV_STATUE)) continue;
2898                                         }
2899
2900                                         /* Extract some flags */
2901                                         object_flags(o_ptr, flgs);
2902
2903                                         /* Acquire the object name */
2904                                         object_desc(o_name, o_ptr, 0);
2905                                         monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
2906
2907                                         /* React to objects that hurt the monster */
2908                                         if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
2909                                         if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
2910                                         if (have_flag(flgs, TR_SLAY_TROLL))  flg3 |= (RF3_TROLL);
2911                                         if (have_flag(flgs, TR_KILL_TROLL))  flg3 |= (RF3_TROLL);
2912                                         if (have_flag(flgs, TR_SLAY_GIANT))  flg3 |= (RF3_GIANT);
2913                                         if (have_flag(flgs, TR_KILL_GIANT))  flg3 |= (RF3_GIANT);
2914                                         if (have_flag(flgs, TR_SLAY_ORC))    flg3 |= (RF3_ORC);
2915                                         if (have_flag(flgs, TR_KILL_ORC))    flg3 |= (RF3_ORC);
2916                                         if (have_flag(flgs, TR_SLAY_DEMON))  flg3 |= (RF3_DEMON);
2917                                         if (have_flag(flgs, TR_KILL_DEMON))  flg3 |= (RF3_DEMON);
2918                                         if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
2919                                         if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
2920                                         if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
2921                                         if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
2922                                         if (have_flag(flgs, TR_SLAY_EVIL))   flg3 |= (RF3_EVIL);
2923                                         if (have_flag(flgs, TR_KILL_EVIL))   flg3 |= (RF3_EVIL);
2924                                         if (have_flag(flgs, TR_SLAY_HUMAN))  flg2 |= (RF2_HUMAN);
2925                                         if (have_flag(flgs, TR_KILL_HUMAN))  flg2 |= (RF2_HUMAN);
2926                                         if (have_flag(flgs, TR_BRAND_ACID))  flgr |= (RFR_IM_ACID);
2927                                         if (have_flag(flgs, TR_BRAND_ELEC))  flgr |= (RFR_IM_ELEC);
2928                                         if (have_flag(flgs, TR_BRAND_FIRE))  flgr |= (RFR_IM_FIRE);
2929                                         if (have_flag(flgs, TR_BRAND_COLD))  flgr |= (RFR_IM_COLD);
2930                                         if (have_flag(flgs, TR_BRAND_POIS))  flgr |= (RFR_IM_POIS);
2931
2932                                         /* The object cannot be picked up by the monster */
2933                                         if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
2934                                             ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
2935                                         {
2936                                                 /* Only give a message for "take_item" */
2937                                                 if (do_take && (r_ptr->flags2 & RF2_STUPID))
2938                                                 {
2939                                                         did_take_item = TRUE;
2940
2941                                                         /* Describe observable situations */
2942                                                         if (m_ptr->ml && player_can_see_bold(ny, nx))
2943                                                         {
2944                                                                 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
2945                                                         }
2946                                                 }
2947                                         }
2948
2949                                         /* Pick up the item */
2950                                         else if (do_take)
2951                                         {
2952                                                 did_take_item = TRUE;
2953
2954                                                 /* Describe observable situations */
2955                                                 if (player_can_see_bold(ny, nx))
2956                                                 {
2957                                                         msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
2958                                                 }
2959
2960                                                 /* Excise the object */
2961                                                 excise_object_idx(this_o_idx);
2962
2963                                                 /* Forget mark */
2964                                                 o_ptr->marked &= OM_TOUCHED;
2965
2966                                                 /* Forget location */
2967                                                 o_ptr->iy = o_ptr->ix = 0;
2968
2969                                                 /* Memorize monster */
2970                                                 o_ptr->held_m_idx = m_idx;
2971
2972                                                 /* Build a stack */
2973                                                 o_ptr->next_o_idx = m_ptr->hold_o_idx;
2974
2975                                                 /* Carry object */
2976                                                 m_ptr->hold_o_idx = this_o_idx;
2977                                         }
2978
2979                                         /* Destroy the item if not a pet */
2980                                         else if (!is_pet(m_ptr))
2981                                         {
2982                                                 did_kill_item = TRUE;
2983
2984                                                 /* Describe observable situations */
2985                                                 if (player_has_los_bold(ny, nx))
2986                                                 {
2987                                                         msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
2988                                                 }
2989
2990                                                 delete_object_idx(this_o_idx);
2991                                         }
2992                                 }
2993                         }
2994                 }
2995
2996                 /* Stop when done */
2997                 if (do_turn) break;
2998         }
2999
3000         /*
3001          *  Forward movements failed, but now received LOS attack!
3002          *  Try to flow by smell.
3003          */
3004         if (p_ptr->no_flowed && i > 2 &&  m_ptr->target_y)
3005                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3006
3007         /* If we haven't done anything, try casting a spell again */
3008         if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
3009         {
3010                 /* Try to cast spell again */
3011                 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
3012                 {
3013                         if (make_attack_spell(m_idx)) return;
3014                 }
3015         }
3016
3017
3018         /* Notice changes in view */
3019         if (do_view)
3020         {
3021                 p_ptr->update |= (PU_FLOW);
3022                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3023         }
3024
3025         /* Notice changes in view */
3026         if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
3027                 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !p_ptr->inside_battle)))
3028         {
3029                 p_ptr->update |= (PU_MON_LITE);
3030         }
3031
3032         /* Learn things from observable monster */
3033         if (is_original_ap_and_seen(m_ptr))
3034         {
3035                 /* Monster opened a door */
3036                 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
3037
3038                 /* Monster bashed a door */
3039                 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
3040
3041                 /* Monster tried to pick something up */
3042                 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
3043
3044                 /* Monster tried to crush something */
3045                 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
3046
3047                 /* Monster pushed past another monster */
3048                 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
3049
3050                 /* Monster passed through a wall */
3051                 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
3052
3053                 /* Monster destroyed a wall */
3054                 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
3055         }
3056
3057
3058         /* Hack -- get "bold" if out of options */
3059         if (!do_turn && !do_move && MON_MONFEAR(m_ptr) && aware)
3060         {
3061                 /* No longer afraid */
3062                 (void)set_monster_monfear(m_idx, 0);
3063
3064                 /* Message if seen */
3065                 if (see_m)
3066                 {
3067                         GAME_TEXT m_name[MAX_NLEN];
3068                         monster_desc(m_name, m_ptr, 0);
3069                         msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
3070                 }
3071
3072                 if (m_ptr->ml) chg_virtue(V_COMPASSION, -1);
3073
3074                 /* Actually do something now (?) */
3075         }
3076 }
3077
3078 /*!
3079  * @brief 全モンスターのターン管理メインルーチン /
3080  * Process all the "live" monsters, once per game current_world_ptr->game_turn.
3081  * @return なし
3082  * @details
3083  * During each game current_world_ptr->game_turn, we scan through the list of all the "live" monsters,\n
3084  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
3085  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
3086  *\n
3087  * Note that monsters can never move in the monster array (except when the\n
3088  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
3089  *\n
3090  * This function is responsible for at least half of the processor time\n
3091  * on a normal system with a "normal" amount of monsters and a player doing\n
3092  * normal things.\n
3093  *\n
3094  * When the player is resting, virtually 90% of the processor time is spent\n
3095  * in this function, and its children, "process_monster()" and "make_move()".\n
3096  *\n
3097  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
3098  * especially when the player is running.\n
3099  *\n
3100  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
3101  * monsters while they are still being "born".  A monster is "fresh" only\n
3102  * during the current_world_ptr->game_turn in which it is created, and we use the "hack_m_idx" to\n
3103  * determine if the monster is yet to be processed during the current current_world_ptr->game_turn.\n
3104  *\n
3105  * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
3106  * move before any "nasty" monsters get to use their spell attacks.\n
3107  *\n
3108  * Note that when the "knowledge" about the currently tracked monster\n
3109  * changes (flags, attacks, spells), we induce a redraw of the monster\n
3110  * recall window.\n
3111  */
3112 void process_monsters(void)
3113 {
3114         MONSTER_IDX i;
3115         POSITION fx, fy;
3116
3117         bool            test;
3118
3119         monster_type    *m_ptr;
3120         monster_race    *r_ptr;
3121
3122         MONRACE_IDX old_monster_race_idx;
3123
3124         BIT_FLAGS old_r_flags1 = 0L;
3125         BIT_FLAGS old_r_flags2 = 0L;
3126         BIT_FLAGS old_r_flags3 = 0L;
3127         BIT_FLAGS old_r_flags4 = 0L;
3128         BIT_FLAGS old_r_flags5 = 0L;
3129         BIT_FLAGS old_r_flags6 = 0L;
3130         BIT_FLAGS old_r_flagsr = 0L;
3131
3132         byte old_r_blows0 = 0;
3133         byte old_r_blows1 = 0;
3134         byte old_r_blows2 = 0;
3135         byte old_r_blows3 = 0;
3136
3137         byte old_r_cast_spell = 0;
3138
3139         SPEED speed;
3140
3141         /* Clear monster fighting indicator */
3142         mon_fight = FALSE;
3143
3144         /* Memorize old race */
3145         old_monster_race_idx = p_ptr->monster_race_idx;
3146
3147         /* Acquire knowledge */
3148         if (p_ptr->monster_race_idx)
3149         {
3150                 /* Acquire current monster */
3151                 r_ptr = &r_info[p_ptr->monster_race_idx];
3152
3153                 /* Memorize flags */
3154                 old_r_flags1 = r_ptr->r_flags1;
3155                 old_r_flags2 = r_ptr->r_flags2;
3156                 old_r_flags3 = r_ptr->r_flags3;
3157                 old_r_flags4 = r_ptr->r_flags4;
3158                 old_r_flags5 = r_ptr->r_flags5;
3159                 old_r_flags6 = r_ptr->r_flags6;
3160                 old_r_flagsr = r_ptr->r_flagsr;
3161
3162                 /* Memorize blows */
3163                 old_r_blows0 = r_ptr->r_blows[0];
3164                 old_r_blows1 = r_ptr->r_blows[1];
3165                 old_r_blows2 = r_ptr->r_blows[2];
3166                 old_r_blows3 = r_ptr->r_blows[3];
3167
3168                 /* Memorize castings */
3169                 old_r_cast_spell = r_ptr->r_cast_spell;
3170         }
3171
3172
3173         /* Process the monsters (backwards) */
3174         for (i = m_max - 1; i >= 1; i--)
3175         {
3176                 /* Access the monster */
3177                 m_ptr = &current_floor_ptr->m_list[i];
3178                 r_ptr = &r_info[m_ptr->r_idx];
3179
3180                 /* Handle "leaving" */
3181                 if (p_ptr->leaving) break;
3182
3183                 /* Ignore "dead" monsters */
3184                 if (!monster_is_valid(m_ptr)) continue;
3185
3186                 if (p_ptr->wild_mode) continue;
3187
3188
3189                 /* Handle "fresh" monsters */
3190                 if (m_ptr->mflag & MFLAG_BORN)
3191                 {
3192                         /* No longer "fresh" */
3193                         m_ptr->mflag &= ~(MFLAG_BORN);
3194
3195                         /* Skip */
3196                         continue;
3197                 }
3198
3199                 /* Hack -- Require proximity */
3200                 if (m_ptr->cdis >= AAF_LIMIT) continue;
3201
3202                 fx = m_ptr->fx;
3203                 fy = m_ptr->fy;
3204
3205                 /* Flow by smell is allowed */
3206                 if (!p_ptr->no_flowed)
3207                 {
3208                         m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3209                 }
3210
3211                 /* Assume no move */
3212                 test = FALSE;
3213
3214                 /* Handle "sensing radius" */
3215                 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
3216                 {
3217                         /* We can "sense" the player */
3218                         test = TRUE;
3219                 }
3220
3221                 /* Handle "sight" and "aggravation" */
3222         else if ((m_ptr->cdis <= MAX_SIGHT || p_ptr->inside_battle) &&
3223                         (player_has_los_bold(fy, fx) || (p_ptr->cursed & TRC_AGGRAVATE)))
3224                 {
3225                         /* We can "see" or "feel" the player */
3226                         test = TRUE;
3227                 }
3228
3229 #if 0 /* (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when == current_floor_ptr->grid_array[fy][fx].when) is always FALSE... */
3230                 /* Hack -- Monsters can "smell" the player from far away */
3231                 /* Note that most monsters have "aaf" of "20" or so */
3232                 else if (!(m_ptr->mflag2 & MFLAG2_NOFLOW) &&
3233                         cave_have_flag_bold(p_ptr->y, p_ptr->x, FF_MOVE) &&
3234                         (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when == current_floor_ptr->grid_array[fy][fx].when) &&
3235                         (current_floor_ptr->grid_array[fy][fx].dist < MONSTER_FLOW_DEPTH) &&
3236                         (current_floor_ptr->grid_array[fy][fx].dist < r_ptr->aaf))
3237                 {
3238                         /* We can "smell" the player */
3239                         test = TRUE;
3240                 }
3241 #endif
3242                 else if (m_ptr->target_y) test = TRUE;
3243
3244                 /* Do nothing */
3245                 if (!test) continue;
3246
3247
3248                 if (p_ptr->riding == i)
3249                         speed = p_ptr->pspeed;
3250                 else
3251                 {
3252                         speed = m_ptr->mspeed;
3253
3254                         /* Monsters move quickly in Nightmare mode */
3255                         if (ironman_nightmare) speed += 5;
3256
3257                         if (MON_FAST(m_ptr)) speed += 10;
3258                         if (MON_SLOW(m_ptr)) speed -= 10;
3259                 }
3260
3261                 /* Give this monster some energy */
3262                 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
3263
3264                 /* Not enough energy to move */
3265                 if (m_ptr->energy_need > 0) continue;
3266
3267                 /* Use up "some" energy */
3268                 m_ptr->energy_need += ENERGY_NEED();
3269
3270
3271                 /* Save global index */
3272                 hack_m_idx = i;
3273
3274                 /* Process the monster */
3275                 process_monster(i);
3276
3277                 reset_target(m_ptr);
3278
3279                 /* Give up flow_by_smell when it might useless */
3280                 if (p_ptr->no_flowed && one_in_(3))
3281                         m_ptr->mflag2 |= MFLAG2_NOFLOW;
3282
3283                 /* Hack -- notice death or departure */
3284                 if (!p_ptr->playing || p_ptr->is_dead) break;
3285
3286                 /* Notice leaving */
3287                 if (p_ptr->leaving) break;
3288         }
3289
3290         /* Reset global index */
3291         hack_m_idx = 0;
3292
3293
3294         /* Tracking a monster race (the same one we were before) */
3295         if (p_ptr->monster_race_idx && (p_ptr->monster_race_idx == old_monster_race_idx))
3296         {
3297                 /* Acquire monster race */
3298                 r_ptr = &r_info[p_ptr->monster_race_idx];
3299
3300                 /* Check for knowledge change */
3301                 if ((old_r_flags1 != r_ptr->r_flags1) ||
3302                         (old_r_flags2 != r_ptr->r_flags2) ||
3303                         (old_r_flags3 != r_ptr->r_flags3) ||
3304                         (old_r_flags4 != r_ptr->r_flags4) ||
3305                         (old_r_flags5 != r_ptr->r_flags5) ||
3306                         (old_r_flags6 != r_ptr->r_flags6) ||
3307                         (old_r_flagsr != r_ptr->r_flagsr) ||
3308                         (old_r_blows0 != r_ptr->r_blows[0]) ||
3309                         (old_r_blows1 != r_ptr->r_blows[1]) ||
3310                         (old_r_blows2 != r_ptr->r_blows[2]) ||
3311                         (old_r_blows3 != r_ptr->r_blows[3]) ||
3312                         (old_r_cast_spell != r_ptr->r_cast_spell))
3313                 {
3314                         p_ptr->window |= (PW_MONSTER);
3315                 }
3316         }
3317 }
3318