OSDN Git Service

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