OSDN Git Service

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