OSDN Git Service

[Refactor] #38997 clean_shot() にplayer_type * 引数を追加 / Added player_type * argument...
[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 target_ptr プレーヤーへの参照ポインタ
793  * @param m_idx モンスターの参照ID
794  * @param yp 移動先のマスのY座標を返す参照ポインタ
795  * @param xp 移動先のマスのX座標を返す参照ポインタ
796  * @return 有効なマスがあった場合TRUEを返す
797  * @details
798  * Pack monsters will use this to "ambush" the player and lure him out\n
799  * of corridors into open space so they can swarm him.\n
800  *\n
801  * Return TRUE if a good location is available.\n
802  */
803 static bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
804 {
805         floor_type *floor_ptr = target_ptr->current_floor_ptr;
806         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
807         monster_race *r_ptr = &r_info[m_ptr->r_idx];
808
809         POSITION fy = m_ptr->fy;
810         POSITION fx = m_ptr->fx;
811
812         POSITION y, x, dy, dx, d, dis, i;
813         POSITION gy = 0, gx = 0, gdis = 999;
814
815         POSITION *y_offsets, *x_offsets;
816
817         /* Start with adjacent locations, spread further */
818         for (d = 1; d < 10; d++)
819         {
820                 /* Get the lists of points with a distance d from (fx, fy) */
821                 y_offsets = dist_offsets_y[d];
822                 x_offsets = dist_offsets_x[d];
823
824                 /* Check the locations */
825                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
826                      dx != 0 || dy != 0;
827                      i++, dx = x_offsets[i], dy = y_offsets[i])
828                 {
829                         y = fy + dy;
830                         x = fx + dx;
831
832                         /* Skip illegal locations */
833                         if (!in_bounds(floor_ptr, y, x)) continue;
834
835                         /* Skip occupied locations */
836                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
837
838                         /* Check for hidden, available grid */
839                         if (!projectable(floor_ptr, target_ptr->y, target_ptr->x, y, x) && clean_shot(target_ptr, fy, fx, y, x, FALSE))
840                         {
841                                 /* Calculate distance from player */
842                                 dis = distance(y, x, target_ptr->y, target_ptr->x);
843
844                                 /* Remember if closer than previous */
845                                 if (dis < gdis && dis >= 2)
846                                 {
847                                         gy = y;
848                                         gx = x;
849                                         gdis = dis;
850                                 }
851                         }
852                 }
853
854                 /* Check for success */
855                 if (gdis < 999)
856                 {
857                         /* Good location */
858                         (*yp) = fy - gy;
859                         (*xp) = fx - gx;
860
861                         /* Found good place */
862                         return TRUE;
863                 }
864         }
865
866         /* No good place */
867         return FALSE;
868 }
869
870
871 /*!
872  * @brief モンスターの移動方向を返す /
873  * Choose "logical" directions for monster movement
874  * @param target_ptr プレーヤーへの参照ポインタ
875  * @param m_idx モンスターの参照ID
876  * @param mm 移動方向を返す方向IDの参照ポインタ
877  * @return 有効方向があった場合TRUEを返す
878  */
879 static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
880 {
881         floor_type *floor_ptr = target_ptr->current_floor_ptr;
882         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
883         monster_race *r_ptr = &r_info[m_ptr->r_idx];
884         POSITION     y = 0, ay, x = 0, ax;
885         int          move_val = 0;
886         POSITION     y2 = target_ptr->y;
887         POSITION     x2 = target_ptr->x;
888         bool         done = FALSE;
889         bool         will_run = mon_will_run(m_idx);
890         grid_type    *g_ptr;
891         bool         no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
892         bool         can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall));
893
894         /* Counter attack to an enemy monster */
895         if (!will_run && m_ptr->target_y)
896         {
897                 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
898
899                 /* The monster must be an enemy, and in LOS */
900                 if (t_m_idx &&
901                     are_enemies(m_ptr, &floor_ptr->m_list[t_m_idx]) &&
902                     los(floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
903                     projectable(floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
904                 {
905                         /* Extract the "pseudo-direction" */
906                         y = m_ptr->fy - m_ptr->target_y;
907                         x = m_ptr->fx - m_ptr->target_x;
908                         done = TRUE;
909                 }
910         }
911
912         if (!done && !will_run && is_hostile(m_ptr) &&
913             (r_ptr->flags1 & RF1_FRIENDS) &&
914             ((los(floor_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x) && projectable(floor_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x)) ||
915             (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
916         {
917         /*
918          * Animal packs try to get the player out of corridors
919          * (...unless they can move through walls -- TY)
920          */
921                 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
922                          !(r_ptr->flags2 & RF2_KILL_WALL))
923                 {
924                         int i, room = 0;
925
926                         /* Count room grids next to player */
927                         for (i = 0; i < 8; i++)
928                         {
929                                 int xx = target_ptr->x + ddx_ddd[i];
930                                 int yy = target_ptr->y + ddy_ddd[i];
931
932                                 if (!in_bounds2(floor_ptr, yy, xx)) continue;
933
934                                 g_ptr = &floor_ptr->grid_array[yy][xx];
935
936                                 /* Check grid */
937                                 if (monster_can_cross_terrain(g_ptr->feat, r_ptr, 0))
938                                 {
939                                         /* One more room grid */
940                                         room++;
941                                 }
942                         }
943                         if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
944                         if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
945
946                         /* Not in a room and strong player */
947                         if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
948                             (target_ptr->mhp + target_ptr->msp))
949                         {
950                                 /* Find hiding place */
951                                 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
952                         }
953                 }
954
955                 /* Monster groups try to surround the player */
956                 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
957                 {
958                         int i;
959
960                         /* Find an empty square near the player to fill */
961                         for (i = 0; i < 8; i++)
962                         {
963                                 /* Pick squares near player (semi-randomly) */
964                                 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
965                                 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
966
967                                 /* Already there? */
968                                 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
969                                 {
970                                         /* Attack the player */
971                                         y2 = target_ptr->y;
972                                         x2 = target_ptr->x;
973
974                                         break;
975                                 }
976
977                                 if (!in_bounds2(floor_ptr, y2, x2)) continue;
978
979                                 /* Ignore filled grids */
980                                 if (!monster_can_enter(y2, x2, r_ptr, 0)) continue;
981
982                                 /* Try to fill this hole */
983                                 break;
984                         }
985
986                         /* Extract the new "pseudo-direction" */
987                         y = m_ptr->fy - y2;
988                         x = m_ptr->fx - x2;
989
990                         done = TRUE;
991                 }
992         }
993
994         if (!done)
995         {
996                 /* Flow towards the player */
997                 (void)get_moves_aux(m_idx, &y2, &x2, no_flow);
998
999                 /* Extract the "pseudo-direction" */
1000                 y = m_ptr->fy - y2;
1001                 x = m_ptr->fx - x2;
1002
1003                 /* Not done */
1004         }
1005
1006         /* Apply fear if possible and necessary */
1007         if (is_pet(m_ptr) && will_run)
1008         {
1009                 /* XXX XXX Not very "smart" */
1010                 y = (-y), x = (-x);
1011         }
1012         else
1013         {
1014                 if (!done && will_run)
1015                 {
1016                         int tmp_x = (-x);
1017                         int tmp_y = (-y);
1018
1019                         /* Try to find safe place */
1020                         if (find_safety(m_idx, &y, &x))
1021                         {
1022                                 /* Attempt to avoid the player */
1023                                 if (!no_flow)
1024                                 {
1025                                         /* Adjust movement */
1026                                         if (get_fear_moves_aux(m_idx, &y, &x)) done = TRUE;
1027                                 }
1028                         }
1029
1030                         if (!done)
1031                         {
1032                                 /* This is not a very "smart" method XXX XXX */
1033                                 y = tmp_y;
1034                                 x = tmp_x;
1035                         }
1036                 }
1037         }
1038
1039
1040         /* Check for no move */
1041         if (!x && !y) return FALSE;
1042
1043
1044         /* Extract the "absolute distances" */
1045         ax = ABS(x);
1046         ay = ABS(y);
1047
1048         /* Do something weird */
1049         if (y < 0) move_val += 8;
1050         if (x > 0) move_val += 4;
1051
1052         /* Prevent the diamond maneuvre */
1053         if (ay > (ax << 1)) move_val += 2;
1054         else if (ax > (ay << 1)) move_val++;
1055
1056         /* Extract some directions */
1057         switch (move_val)
1058         {
1059         case 0:
1060                 mm[0] = 9;
1061                 if (ay > ax)
1062                 {
1063                         mm[1] = 8;
1064                         mm[2] = 6;
1065                         mm[3] = 7;
1066                         mm[4] = 3;
1067                 }
1068                 else
1069                 {
1070                         mm[1] = 6;
1071                         mm[2] = 8;
1072                         mm[3] = 3;
1073                         mm[4] = 7;
1074                 }
1075                 break;
1076         case 1:
1077         case 9:
1078                 mm[0] = 6;
1079                 if (y < 0)
1080                 {
1081                         mm[1] = 3;
1082                         mm[2] = 9;
1083                         mm[3] = 2;
1084                         mm[4] = 8;
1085                 }
1086                 else
1087                 {
1088                         mm[1] = 9;
1089                         mm[2] = 3;
1090                         mm[3] = 8;
1091                         mm[4] = 2;
1092                 }
1093                 break;
1094         case 2:
1095         case 6:
1096                 mm[0] = 8;
1097                 if (x < 0)
1098                 {
1099                         mm[1] = 9;
1100                         mm[2] = 7;
1101                         mm[3] = 6;
1102                         mm[4] = 4;
1103                 }
1104                 else
1105                 {
1106                         mm[1] = 7;
1107                         mm[2] = 9;
1108                         mm[3] = 4;
1109                         mm[4] = 6;
1110                 }
1111                 break;
1112         case 4:
1113                 mm[0] = 7;
1114                 if (ay > ax)
1115                 {
1116                         mm[1] = 8;
1117                         mm[2] = 4;
1118                         mm[3] = 9;
1119                         mm[4] = 1;
1120                 }
1121                 else
1122                 {
1123                         mm[1] = 4;
1124                         mm[2] = 8;
1125                         mm[3] = 1;
1126                         mm[4] = 9;
1127                 }
1128                 break;
1129         case 5:
1130         case 13:
1131                 mm[0] = 4;
1132                 if (y < 0)
1133                 {
1134                         mm[1] = 1;
1135                         mm[2] = 7;
1136                         mm[3] = 2;
1137                         mm[4] = 8;
1138                 }
1139                 else
1140                 {
1141                         mm[1] = 7;
1142                         mm[2] = 1;
1143                         mm[3] = 8;
1144                         mm[4] = 2;
1145                 }
1146                 break;
1147         case 8:
1148                 mm[0] = 3;
1149                 if (ay > ax)
1150                 {
1151                         mm[1] = 2;
1152                         mm[2] = 6;
1153                         mm[3] = 1;
1154                         mm[4] = 9;
1155                 }
1156                 else
1157                 {
1158                         mm[1] = 6;
1159                         mm[2] = 2;
1160                         mm[3] = 9;
1161                         mm[4] = 1;
1162                 }
1163                 break;
1164         case 10:
1165         case 14:
1166                 mm[0] = 2;
1167                 if (x < 0)
1168                 {
1169                         mm[1] = 3;
1170                         mm[2] = 1;
1171                         mm[3] = 6;
1172                         mm[4] = 4;
1173                 }
1174                 else
1175                 {
1176                         mm[1] = 1;
1177                         mm[2] = 3;
1178                         mm[3] = 4;
1179                         mm[4] = 6;
1180                 }
1181                 break;
1182         case 12:
1183                 mm[0] = 1;
1184                 if (ay > ax)
1185                 {
1186                         mm[1] = 2;
1187                         mm[2] = 4;
1188                         mm[3] = 3;
1189                         mm[4] = 7;
1190                 }
1191                 else
1192                 {
1193                         mm[1] = 4;
1194                         mm[2] = 2;
1195                         mm[3] = 7;
1196                         mm[4] = 3;
1197                 }
1198                 break;
1199         }
1200
1201         /* Wants to move... */
1202         return TRUE;
1203 }
1204
1205
1206 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
1207 {
1208         return !have_flag(f_ptr->flags, FF_GLASS) ||
1209                 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
1210                 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
1211 }
1212
1213
1214 /*!
1215  * @brief モンスター単体の1ターン行動処理メインルーチン /
1216  * Process a monster
1217  * @param target_ptr プレーヤーへの参照ポインタ
1218  * @param m_idx 行動モンスターの参照ID
1219  * @return なし
1220  * @details
1221  * The monster is known to be within 100 grids of the player\n
1222  *\n
1223  * In several cases, we directly update the monster lore\n
1224  *\n
1225  * Note that a monster is only allowed to "reproduce" if there\n
1226  * are a limited number of "reproducing" monsters on the current\n
1227  * level.  This should prevent the level from being "swamped" by\n
1228  * reproducing monsters.  It also allows a large mass of mice to\n
1229  * prevent a louse from multiplying, but this is a small price to\n
1230  * pay for a simple multiplication method.\n
1231  *\n
1232  * XXX Monster fear is slightly odd, in particular, monsters will\n
1233  * fixate on opening a door even if they cannot open it.  Actually,\n
1234  * the same thing happens to normal monsters when they hit a door\n
1235  *\n
1236  * In addition, monsters which *cannot* open or bash\n
1237  * down a door will still stand there trying to open it...\n
1238  *\n
1239  * XXX Technically, need to check for monster in the way\n
1240  * combined with that monster being in a wall (or door?)\n
1241  *\n
1242  * A "direction" of "5" means "pick a random direction".\n
1243  */
1244 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
1245 {
1246         monster_type    *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1247         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
1248         monster_race    *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1249
1250         int i, d;
1251         POSITION oy, ox, ny, nx;
1252
1253         DIRECTION mm[8];
1254
1255         grid_type       *g_ptr;
1256         feature_type    *f_ptr;
1257
1258         monster_type    *y_ptr;
1259
1260         bool            do_turn;
1261         bool            do_move;
1262         bool            do_view;
1263         bool            must_alter_to_move;
1264
1265         bool            did_open_door;
1266         bool            did_bash_door;
1267         bool            did_take_item;
1268         bool            did_kill_item;
1269         bool            did_move_body;
1270         bool            did_pass_wall;
1271         bool            did_kill_wall;
1272         bool            gets_angry = FALSE;
1273         bool            can_cross;
1274         bool            aware = TRUE;
1275
1276         bool fear, dead;
1277         bool is_riding_mon = (m_idx == target_ptr->riding);
1278         bool see_m = is_seen(m_ptr);
1279
1280         if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
1281         {
1282                 if (rakuba(target_ptr, 0, TRUE))
1283                 {
1284 #ifdef JP
1285                         msg_print("地面に落とされた。");
1286 #else
1287                         GAME_TEXT m_name[MAX_NLEN];
1288                         monster_desc(m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
1289                         msg_format("You have fallen from %s.", m_name);
1290 #endif
1291                 }
1292         }
1293
1294         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
1295         {
1296                 choose_new_monster(m_idx, FALSE, 0);
1297                 r_ptr = &r_info[m_ptr->r_idx];
1298         }
1299
1300         /* Players hidden in shadow are almost imperceptable. -LM- */
1301         if (target_ptr->special_defense & NINJA_S_STEALTH)
1302         {
1303                 int tmp = target_ptr->lev*6+(target_ptr->skill_stl+10)*4;
1304                 if (target_ptr->monlite) tmp /= 3;
1305                 if (target_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1306                 if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10)) tmp /= 3;
1307                 /* Low-level monsters will find it difficult to locate the player. */
1308                 if (randint0(tmp) > (r_ptr->level+20)) aware = FALSE;
1309         }
1310
1311         /* Are there its parent? */
1312         if (m_ptr->parent_m_idx && !target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
1313         {
1314                 /* Its parent have gone, it also goes away. */
1315
1316                 if (see_m)
1317                 {
1318                         GAME_TEXT m_name[MAX_NLEN];
1319                         monster_desc(m_name, m_ptr, 0);
1320                         msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
1321                 }
1322
1323                 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
1324                 {
1325                         GAME_TEXT m_name[MAX_NLEN];
1326                         monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
1327                         exe_write_diary(target_ptr, NIKKI_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
1328                 }
1329
1330                 delete_monster_idx(m_idx);
1331
1332                 return;
1333         }
1334
1335         /* Quantum monsters are odd */
1336         if (r_ptr->flags2 & (RF2_QUANTUM))
1337         {
1338                 /* Sometimes skip move */
1339                 if (!randint0(2)) return;
1340
1341                 /* Sometimes die */
1342                 if (!randint0((m_idx % 100) + 10) && !(r_ptr->flags1 & RF1_QUESTOR))
1343                 {
1344                         bool sad = FALSE;
1345
1346                         if (is_pet(m_ptr) && !(m_ptr->ml)) sad = TRUE;
1347
1348                         if (see_m)
1349                         {
1350                                 GAME_TEXT m_name[MAX_NLEN];
1351                                 monster_desc(m_name, m_ptr, 0);
1352
1353                                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
1354                         }
1355
1356                         /* Generate treasure, etc */
1357                         monster_death(m_idx, FALSE);
1358
1359                         delete_monster_idx(m_idx);
1360                         if (sad)
1361                         {
1362                                 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
1363                         }
1364
1365                         return;
1366                 }
1367         }
1368
1369         if (m_ptr->r_idx == MON_SHURYUUDAN)
1370         {
1371                 mon_take_hit_mon(m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1372                 if(dead) return;
1373         }
1374
1375         if ((is_pet(m_ptr) || is_friendly(m_ptr)) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && !target_ptr->phase_out)
1376         {
1377                 static int riding_pinch = 0;
1378
1379                 if (m_ptr->hp < m_ptr->maxhp/3)
1380                 {
1381                         GAME_TEXT m_name[MAX_NLEN];
1382                         monster_desc(m_name, m_ptr, 0);
1383
1384                         if (is_riding_mon && riding_pinch < 2)
1385                         {
1386                                 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
1387                                                          "%^s seems to be in so much pain, and trying to escape from your restriction."), m_name);
1388                                 riding_pinch++;
1389                                 disturb(target_ptr, TRUE, TRUE);
1390                         }
1391                         else
1392                         {
1393                                 if (is_riding_mon)
1394                                 {
1395                                         msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
1396                                         if (rakuba(target_ptr, -1, FALSE))
1397                                         {
1398                                                 msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
1399                                         }
1400                                 }
1401
1402                                 if (see_m)
1403                                 {
1404                                         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) &&
1405                                             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))
1406                                         {
1407                                                 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
1408                                         }
1409                                         msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s read a scroll of teleport level."), m_name);
1410                                         msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
1411                                 }
1412
1413                                 if (is_riding_mon && rakuba(target_ptr, -1, FALSE))
1414                                 {
1415                                         msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
1416                                 }
1417
1418                                 check_quest_completion(target_ptr, m_ptr);
1419                                 delete_monster_idx(m_idx);
1420                                 return;
1421                         }
1422                 }
1423                 else
1424                 {
1425                         /* Reset the counter */
1426                         if (is_riding_mon) riding_pinch = 0;
1427                 }
1428         }
1429
1430         /* Handle "sleep" */
1431         if (MON_CSLEEP(m_ptr))
1432         {
1433                 /* Handle non-aggravation - Still sleeping */
1434                 if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
1435
1436                 (void)set_monster_csleep(m_idx, 0);
1437
1438                 /* Notice the "waking up" */
1439                 if (m_ptr->ml)
1440                 {
1441                         GAME_TEXT m_name[MAX_NLEN];
1442                         monster_desc(m_name, m_ptr, 0);
1443                         msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
1444                 }
1445
1446                 /* Hack -- Count the wakings */
1447                 if (is_original_ap_and_seen(m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
1448                 {
1449                         r_ptr->r_wake++;
1450                 }
1451         }
1452
1453         /* Handle "stun" */
1454         if (MON_STUNNED(m_ptr))
1455         {
1456                 /* Sometimes skip move */
1457                 if (one_in_(2)) return;
1458         }
1459
1460         if (is_riding_mon)
1461         {
1462                 target_ptr->update |= (PU_BONUS);
1463         }
1464
1465         /* No one wants to be your friend if you're aggravating */
1466         if (is_friendly(m_ptr) && (target_ptr->cursed & TRC_AGGRAVATE))
1467                 gets_angry = TRUE;
1468
1469         /* Paranoia... no pet uniques outside wizard mode -- TY */
1470         if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
1471               monster_has_hostile_align(NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
1472         {
1473                 gets_angry = TRUE;
1474         }
1475
1476         if (target_ptr->phase_out) gets_angry = FALSE;
1477
1478         if (gets_angry)
1479         {
1480                 if (is_pet(m_ptr) || see_m)
1481                 {
1482                         GAME_TEXT m_name[MAX_NLEN];
1483                         monster_desc(m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
1484                         msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
1485                 }
1486
1487                 set_hostile(m_ptr);
1488         }
1489
1490         /* Get the origin */
1491         oy = m_ptr->fy;
1492         ox = m_ptr->fx;
1493
1494         /* Attempt to "multiply" if able and allowed */
1495         if ((r_ptr->flags2 & RF2_MULTIPLY) && (target_ptr->current_floor_ptr->num_repro < MAX_REPRO))
1496         {
1497                 int k;
1498                 POSITION y, x;
1499
1500                 /* Count the adjacent monsters */
1501                 for (k = 0, y = oy - 1; y <= oy + 1; y++)
1502                 {
1503                         for (x = ox - 1; x <= ox + 1; x++)
1504                         {
1505                                 /* Ignore locations off of edge */
1506                                 if (!in_bounds2(target_ptr->current_floor_ptr, y, x)) continue;
1507                                 if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
1508                         }
1509                 }
1510
1511                 if (multiply_barrier(target_ptr, m_idx)) k = 8;
1512
1513                 /* Hack -- multiply slower in crowded areas */
1514                 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
1515                 {
1516                         /* Try to multiply */
1517                         if (multiply_monster(m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
1518                         {
1519                                 /* Take note if visible */
1520                                 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(m_ptr))
1521                                 {
1522                                         r_ptr->r_flags2 |= (RF2_MULTIPLY);
1523                                 }
1524
1525                                 /* Multiplying takes energy */
1526                                 return;
1527                         }
1528                 }
1529         }
1530
1531         if (r_ptr->a_ability_flags2 & RF6_SPECIAL)
1532         {
1533                 /* Hack -- Ohmu scatters molds! */
1534                 if (m_ptr->r_idx == MON_OHMU)
1535                 {
1536                         if (!target_ptr->current_floor_ptr->inside_arena && !target_ptr->phase_out)
1537                         {
1538                                 if (r_ptr->freq_spell && (randint1(100) <= r_ptr->freq_spell))
1539                                 {
1540                                         int k, count = 0;
1541                                         DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1542                                         BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
1543
1544                                         for (k = 0; k < A_MAX; k++)
1545                                         {
1546                                                 if (summon_specific(m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
1547                                                 {
1548                                                         if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
1549                                                 }
1550                                         }
1551
1552                                         if (count && is_original_ap_and_seen(m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
1553                                 }
1554                         }
1555                 }
1556         }
1557
1558         if (!target_ptr->phase_out)
1559         {
1560                 /* Hack! "Cyber" monster makes noise... */
1561                 if (m_ptr->ap_r_idx == MON_CYBER &&
1562                     one_in_(CYBERNOISE) &&
1563                     !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
1564                 {
1565                         if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1566                         msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
1567                 }
1568
1569                 /* Some monsters can speak */
1570                 if ((ap_r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
1571                     one_in_(SPEAK_CHANCE) &&
1572                     player_has_los_bold(target_ptr, oy, ox) &&
1573                     projectable(target_ptr->current_floor_ptr, oy, ox, target_ptr->y, target_ptr->x))
1574                 {
1575                         GAME_TEXT m_name[MAX_NLEN];
1576                         char monmessage[1024];
1577                         concptr filename;
1578
1579                         /* Acquire the monster name/poss */
1580                         if (m_ptr->ml)
1581                                 monster_desc(m_name, m_ptr, 0);
1582                         else
1583                                 strcpy(m_name, _("それ", "It"));
1584
1585                         /* Select the file for monster quotes */
1586                         if (MON_MONFEAR(m_ptr))
1587                                 filename = _("monfear_j.txt", "monfear.txt");
1588                         else if (is_pet(m_ptr))
1589                                 filename = _("monpet_j.txt", "monpet.txt");
1590                         else if (is_friendly(m_ptr))
1591                                 filename = _("monfrien_j.txt", "monfrien.txt");
1592                         else
1593                                 filename = _("monspeak_j.txt", "monspeak.txt");
1594                         /* Get the monster line */
1595                         if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
1596                         {
1597                                 /* Say something */
1598                                 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
1599                         }
1600                 }
1601         }
1602
1603         /* Try to cast spell occasionally */
1604         if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
1605         {
1606                 bool counterattack = FALSE;
1607
1608                 /* Give priority to counter attack? */
1609                 if (m_ptr->target_y)
1610                 {
1611                         MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
1612
1613                         /* The monster must be an enemy, and projectable */
1614                         if (t_m_idx && are_enemies(m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
1615                             projectable(target_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1616                         {
1617                                 counterattack = TRUE;
1618                         }
1619                 }
1620
1621                 if (!counterattack)
1622                 {
1623                         /* Attempt to cast a spell */
1624                         if (aware && make_attack_spell(m_idx, target_ptr)) return;
1625
1626                         /*
1627                          * Attempt to cast a spell at an enemy other than the player
1628                          * (may slow the game a smidgeon, but I haven't noticed.)
1629                          */
1630                         if (monst_spell_monst(target_ptr, m_idx)) return;
1631                 }
1632                 else
1633                 {
1634                         /* Attempt to do counter attack at first */
1635                         if (monst_spell_monst(target_ptr, m_idx)) return;
1636
1637                         if (aware && make_attack_spell(m_idx, target_ptr)) return;
1638                 }
1639         }
1640
1641         /* Hack -- Assume no movement */
1642         mm[0] = mm[1] = mm[2] = mm[3] = 0;
1643         mm[4] = mm[5] = mm[6] = mm[7] = 0;
1644
1645
1646         /* Confused -- 100% random */
1647         if (MON_CONFUSED(m_ptr) || !aware)
1648         {
1649                 /* Try four "random" directions */
1650                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1651         }
1652
1653         /* 75% random movement */
1654         else if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) &&
1655                  (randint0(100) < 75))
1656         {
1657                 /* Memorize flags */
1658                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
1659
1660                 /* Try four "random" directions */
1661                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1662         }
1663
1664         /* 50% random movement */
1665         else if ((r_ptr->flags1 & RF1_RAND_50) &&
1666                                 (randint0(100) < 50))
1667         {
1668                 /* Memorize flags */
1669                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
1670
1671                 /* Try four "random" directions */
1672                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1673         }
1674
1675         /* 25% random movement */
1676         else if ((r_ptr->flags1 & RF1_RAND_25) &&
1677                                 (randint0(100) < 25))
1678         {
1679                 /* Memorize flags */
1680                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
1681
1682                 /* Try four "random" directions */
1683                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1684         }
1685
1686         /* Can't reach player - find something else to hit */
1687         else if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
1688         {
1689                 /* Try four "random" directions */
1690                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1691
1692                 /* Look for an enemy */
1693 #if 0  /* Hack - Too slow.  Mimic pits are horrible with this on. */
1694                 get_enemy_dir(m_idx, mm);
1695 #endif /* 0 */
1696         }
1697
1698         /* Pets will follow the player */
1699         else if (is_pet(m_ptr))
1700         {
1701                 /* Are we trying to avoid the player? */
1702                 bool avoid = ((target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
1703
1704                 /* Do we want to find the player? */
1705                 bool lonely = (!avoid && (m_ptr->cdis > target_ptr->pet_follow_distance));
1706
1707                 /* Should we find the player if we can't find a monster? */
1708                 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
1709
1710                 /* by default, move randomly */
1711                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1712
1713                 /* Look for an enemy */
1714                 if (!get_enemy_dir(m_idx, mm))
1715                 {
1716                         /* Find the player if necessary */
1717                         if (avoid || lonely || distant)
1718                         {
1719                                 /* Remember the leash length */
1720                                 POSITION dis = target_ptr->pet_follow_distance;
1721
1722                                 /* Hack -- adjust follow distance temporarily */
1723                                 if (target_ptr->pet_follow_distance > PET_SEEK_DIST)
1724                                 {
1725                                         target_ptr->pet_follow_distance = PET_SEEK_DIST;
1726                                 }
1727
1728                                 /* Find the player */
1729                                 (void)get_moves(target_ptr, m_idx, mm);
1730
1731                                 /* Restore the leash */
1732                                 target_ptr->pet_follow_distance = (s16b)dis;
1733                         }
1734                 }
1735         }
1736
1737         /* Friendly monster movement */
1738         else if (!is_hostile(m_ptr))
1739         {
1740                 /* by default, move randomly */
1741                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1742
1743                 /* Look for an enemy */
1744                 get_enemy_dir(m_idx, mm);
1745         }
1746         /* Normal movement */
1747         else
1748         {
1749                 /* Logical moves, may do nothing */
1750                 if (!get_moves(target_ptr, m_idx, mm)) return;
1751         }
1752
1753         /* Assume nothing */
1754         do_turn = FALSE;
1755         do_move = FALSE;
1756         do_view = FALSE;
1757         must_alter_to_move = FALSE;
1758
1759         /* Assume nothing */
1760         did_open_door = FALSE;
1761         did_bash_door = FALSE;
1762         did_take_item = FALSE;
1763         did_kill_item = FALSE;
1764         did_move_body = FALSE;
1765         did_pass_wall = FALSE;
1766         did_kill_wall = FALSE;
1767
1768         /* Take a zero-terminated array of "directions" */
1769         for (i = 0; mm[i]; i++)
1770         {
1771                 /* Get the direction */
1772                 d = mm[i];
1773
1774                 /* Hack -- allow "randomized" motion */
1775                 if (d == 5) d = ddd[randint0(8)];
1776
1777                 /* Get the destination */
1778                 ny = oy + ddy[d];
1779                 nx = ox + ddx[d];
1780
1781                 /* Ignore locations off of edge */
1782                 if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx)) continue;
1783
1784                 /* Access that grid */
1785                 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1786                 f_ptr = &f_info[g_ptr->feat];
1787                 can_cross = monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
1788
1789                 /* Access that grid's contents */
1790                 y_ptr = &target_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
1791
1792                 /* Hack -- player 'in' wall */
1793                 if (player_bold(target_ptr, ny, nx))
1794                 {
1795                         do_move = TRUE;
1796                 }
1797
1798                 /* Possibly a monster to attack */
1799                 else if (g_ptr->m_idx)
1800                 {
1801                         do_move = TRUE;
1802                 }
1803
1804                 /* Monster destroys walls (and doors) */
1805                 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
1806                          (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
1807                          have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
1808                          check_hp_for_feat_destruction(f_ptr, m_ptr))
1809                 {
1810                         /* Eat through walls/doors/rubble */
1811                         do_move = TRUE;
1812                         if (!can_cross) must_alter_to_move = TRUE;
1813
1814                         /* Monster destroyed a wall (later) */
1815                         did_kill_wall = TRUE;
1816                 }
1817
1818                 /* Floor is open? */
1819                 else if (can_cross)
1820                 {
1821                         /* Go ahead and move */
1822                         do_move = TRUE;
1823
1824                         /* Monster moves through walls (and doors) */
1825                         if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || target_ptr->pass_wall) &&
1826                             have_flag(f_ptr->flags, FF_CAN_PASS))
1827                         {
1828                                 /* Monster went through a wall */
1829                                 did_pass_wall = TRUE;
1830                         }
1831                 }
1832
1833                 /* Handle doors and secret doors */
1834                 else if (is_closed_door(g_ptr->feat))
1835                 {
1836                         bool may_bash = TRUE;
1837
1838                         /* Assume no move allowed */
1839                         do_move = FALSE;
1840
1841                         /* Creature can open doors. */
1842                         if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
1843                                  (!is_pet(m_ptr) || (target_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1844                         {
1845                                 /* Closed doors */
1846                                 if (!f_ptr->power)
1847                                 {
1848                                         /* The door is open */
1849                                         did_open_door = TRUE;
1850
1851                                         /* Do not bash the door */
1852                                         may_bash = FALSE;
1853
1854                                         do_turn = TRUE;
1855                                 }
1856
1857                                 /* Locked doors (not jammed) */
1858                                 else
1859                                 {
1860                                         /* Try to unlock it */
1861                                         if (randint0(m_ptr->hp / 10) > f_ptr->power)
1862                                         {
1863                                                 /* Unlock the door */
1864                                                 cave_alter_feat(target_ptr, ny, nx, FF_DISARM);
1865
1866                                                 /* Do not bash the door */
1867                                                 may_bash = FALSE;
1868
1869                                                 do_turn = TRUE;
1870                                         }
1871                                 }
1872                         }
1873
1874                         /* Stuck doors -- attempt to bash them down if allowed */
1875                         if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
1876                                 (!is_pet(m_ptr) || (target_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1877                         {
1878                                 /* Attempt to Bash */
1879                                 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
1880                                 {
1881                                         if (have_flag(f_ptr->flags, FF_GLASS))
1882                                                 msg_print(_("ガラスが砕ける音がした!", "You hear a glass was crashed!"));
1883                                         else
1884                                                 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
1885
1886                                         /* Disturb (sometimes) */
1887                                         if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1888
1889                                         /* The door was bashed open */
1890                                         did_bash_door = TRUE;
1891
1892                                         /* Hack -- fall into doorway */
1893                                         do_move = TRUE;
1894                                         must_alter_to_move = TRUE;
1895                                 }
1896                         }
1897
1898
1899                         /* Deal with doors in the way */
1900                         if (did_open_door || did_bash_door)
1901                         {
1902                                 /* Break down the door */
1903                                 if (did_bash_door && ((randint0(100) < 50) || (feat_state(g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
1904                                 {
1905                                         cave_alter_feat(target_ptr, ny, nx, FF_BASH);
1906
1907                                         if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
1908                                         {
1909                                                 target_ptr->update |= (PU_FLOW);
1910                                                 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1911                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1912
1913                                                 return;
1914                                         }
1915                                 }
1916
1917                                 /* Open the door */
1918                                 else
1919                                 {
1920                                         cave_alter_feat(target_ptr, ny, nx, FF_OPEN);
1921                                 }
1922
1923                                 f_ptr = &f_info[g_ptr->feat];
1924
1925                                 /* Handle viewable doors */
1926                                 do_view = TRUE;
1927                         }
1928                 }
1929
1930                 /* Hack -- check for Glyph of Warding */
1931                 if (do_move && is_glyph_grid(g_ptr) &&
1932                     !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(target_ptr, ny, nx)))
1933                 {
1934                         /* Assume no move allowed */
1935                         do_move = FALSE;
1936
1937                         /* Break the ward */
1938                         if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
1939                         {
1940                                 /* Describe observable breakage */
1941                                 if (g_ptr->info & CAVE_MARK)
1942                                 {
1943                                         msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
1944                                 }
1945
1946                                 /* Forget the rune */
1947                                 g_ptr->info &= ~(CAVE_MARK);
1948
1949                                 /* Break the rune */
1950                                 g_ptr->info &= ~(CAVE_OBJECT);
1951                                 g_ptr->mimic = 0;
1952
1953                                 /* Allow movement */
1954                                 do_move = TRUE;
1955
1956                                 note_spot(ny, nx);
1957                         }
1958                 }
1959                 else if (do_move && is_explosive_rune_grid(g_ptr) &&
1960                          !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(target_ptr, ny, nx)))
1961                 {
1962                         /* Assume no move allowed */
1963                         do_move = FALSE;
1964
1965                         /* Break the ward */
1966                         if (!is_pet(m_ptr))
1967                         {
1968                                 /* Break the ward */
1969                                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
1970                                 {
1971                                         /* Describe observable breakage */
1972                                         if (g_ptr->info & CAVE_MARK)
1973                                         {
1974                                                 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
1975                                                 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);
1976                                         }
1977                                 }
1978                                 else
1979                                 {
1980                                         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
1981                                 }
1982
1983                                 /* Forget the rune */
1984                                 g_ptr->info &= ~(CAVE_MARK);
1985
1986                                 /* Break the rune */
1987                                 g_ptr->info &= ~(CAVE_OBJECT);
1988                                 g_ptr->mimic = 0;
1989
1990                                 note_spot(ny, nx);
1991                                 lite_spot(ny, nx);
1992
1993                                 if (!monster_is_valid(m_ptr)) return;
1994                                 /* Allow movement */
1995                                 do_move = TRUE;
1996                         }
1997                 }
1998
1999                 /* The player is in the way */
2000                 if (do_move && player_bold(target_ptr, ny, nx))
2001                 {
2002                         /* Some monsters never attack */
2003                         if (r_ptr->flags1 & RF1_NEVER_BLOW)
2004                         {
2005                                 /* Hack -- memorize lack of attacks */
2006                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
2007
2008                                 /* Do not move */
2009                                 do_move = FALSE;
2010                         }
2011
2012                         /* In anti-melee dungeon, stupid or confused monster takes useless turn */
2013                         if (do_move && (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE))
2014                         {
2015                                 if (!MON_CONFUSED(m_ptr))
2016                                 {
2017                                         if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
2018                                         else
2019                                         {
2020                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2021                                         }
2022                                 }
2023                         }
2024
2025                         /* The player is in the way.  Attack him. */
2026                         if (do_move)
2027                         {
2028                                 if (!target_ptr->riding || one_in_(2))
2029                                 {
2030                                         /* Do the attack */
2031                                         (void)make_attack_normal(target_ptr, m_idx);
2032
2033                                         /* Do not move */
2034                                         do_move = FALSE;
2035
2036                                         /* Took a turn */
2037                                         do_turn = TRUE;
2038                                 }
2039                         }
2040                 }
2041
2042                 /* A monster is in the way */
2043                 if (do_move && g_ptr->m_idx)
2044                 {
2045                         monster_race *z_ptr = &r_info[y_ptr->r_idx];
2046
2047                         /* Assume no movement */
2048                         do_move = FALSE;
2049
2050                         /* Attack 'enemies' */
2051                         if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
2052                                  (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
2053                                  can_cross && (g_ptr->m_idx != target_ptr->riding)) ||
2054                                 are_enemies(m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
2055                         {
2056                                 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
2057                                 {
2058                                         if (r_ptr->flags2 & RF2_KILL_BODY)
2059                                         {
2060                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
2061                                         }
2062
2063                                         /* attack */
2064                                         if (y_ptr->r_idx && (y_ptr->hp >= 0))
2065                                         {
2066                                                 if (monst_attack_monst(target_ptr, m_idx, g_ptr->m_idx)) return;
2067
2068                                                 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
2069                                                 else if (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
2070                                                 {
2071                                                         if (MON_CONFUSED(m_ptr)) return;
2072                                                         else if (r_ptr->flags2 & RF2_STUPID)
2073                                                         {
2074                                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2075                                                                 return;
2076                                                         }
2077                                                 }
2078                                         }
2079                                 }
2080                         }
2081
2082                         /* Push past weaker monsters (unless leaving a wall) */
2083                         else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
2084                                 (r_ptr->mexp > z_ptr->mexp) &&
2085                                 can_cross && (g_ptr->m_idx != target_ptr->riding) &&
2086                                 monster_can_cross_terrain(target_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
2087                         {
2088                                 /* Allow movement */
2089                                 do_move = TRUE;
2090
2091                                 /* Monster pushed past another monster */
2092                                 did_move_body = TRUE;
2093
2094                                 /* Wake up the moved monster */
2095                                 (void)set_monster_csleep(g_ptr->m_idx, 0);
2096
2097                                 /* Message */
2098                         }
2099                 }
2100
2101                 if (is_riding_mon)
2102                 {
2103                         if (!target_ptr->riding_ryoute && !MON_MONFEAR(&target_ptr->current_floor_ptr->m_list[target_ptr->riding])) do_move = FALSE;
2104                 }
2105
2106                 if (did_kill_wall && do_move)
2107                 {
2108                         if (one_in_(GRINDNOISE))
2109                         {
2110                                 if (have_flag(f_ptr->flags, FF_GLASS))
2111                                         msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
2112                                 else
2113                                         msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
2114                         }
2115
2116                         cave_alter_feat(target_ptr, ny, nx, FF_HURT_DISI);
2117
2118                         if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
2119                         {
2120                                 target_ptr->update |= (PU_FLOW);
2121                                 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2122                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2123
2124                                 return;
2125                         }
2126
2127                         f_ptr = &f_info[g_ptr->feat];
2128
2129                         /* Note changes to viewable region */
2130                         do_view = TRUE;
2131                         do_turn = TRUE;
2132                 }
2133
2134                 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
2135                 {
2136                         if (!monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
2137                         {
2138                                 /* Assume no move allowed */
2139                                 do_move = FALSE;
2140                         }
2141                 }
2142
2143                 /*
2144                  * Check if monster can cross terrain
2145                  * This is checked after the normal attacks
2146                  * to allow monsters to attack an enemy,
2147                  * even if it can't enter the terrain.
2148                  */
2149                 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
2150                 {
2151                         /* Assume no move allowed */
2152                         do_move = FALSE;
2153                 }
2154
2155                 /* Some monsters never move */
2156                 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
2157                 {
2158                         /* Hack -- memorize lack of moves */
2159                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
2160
2161                         /* Do not move */
2162                         do_move = FALSE;
2163                 }
2164
2165                 /* Creature has been allowed move */
2166                 if (do_move)
2167                 {
2168                         do_turn = TRUE;
2169
2170                         if (have_flag(f_ptr->flags, FF_TREE))
2171                         {
2172                                 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
2173                                 {
2174                                         m_ptr->energy_need += ENERGY_NEED();
2175                                 }
2176                         }
2177
2178                         if (!is_riding_mon)
2179                         {
2180                                 /* Hack -- Update the old location */
2181                                 target_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
2182
2183                                 /* Mega-Hack -- move the old monster, if any */
2184                                 if (g_ptr->m_idx)
2185                                 {
2186                                         /* Move the old monster */
2187                                         y_ptr->fy = oy;
2188                                         y_ptr->fx = ox;
2189
2190                                         /* Update the old monster */
2191                                         update_monster(target_ptr, g_ptr->m_idx, TRUE);
2192                                 }
2193
2194                                 /* Hack -- Update the new location */
2195                                 g_ptr->m_idx = m_idx;
2196
2197                                 /* Move the monster */
2198                                 m_ptr->fy = ny;
2199                                 m_ptr->fx = nx;
2200                                 update_monster(target_ptr, m_idx, TRUE);
2201
2202                                 lite_spot(oy, ox);
2203                                 lite_spot(ny, nx);
2204                         }
2205                         else
2206                         {
2207                                 /* sound(SOUND_WALK); */
2208                                 if (!move_player_effect(target_ptr, ny, nx, MPE_DONT_PICKUP)) break;
2209                         }
2210
2211                         /* Possible disturb */
2212                         if (m_ptr->ml &&
2213                             (disturb_move ||
2214                              (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)) ||
2215                              (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
2216                         {
2217                                 if (is_hostile(m_ptr))
2218                                         disturb(target_ptr, FALSE, TRUE);
2219                         }
2220
2221                         /* Take or Kill objects on the floor */
2222                         if (g_ptr->o_idx && (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
2223                             (!is_pet(m_ptr) || ((target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) && (r_ptr->flags2 & RF2_TAKE_ITEM))))
2224                         {
2225                                 OBJECT_IDX this_o_idx, next_o_idx;
2226                                 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
2227
2228                                 /* Scan all objects in the grid */
2229                                 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
2230                                 {
2231                                         BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
2232                                         GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
2233                                         object_type *o_ptr = &target_ptr->current_floor_ptr->o_list[this_o_idx];
2234                                         next_o_idx = o_ptr->next_o_idx;
2235
2236                                         if (do_take)
2237                                         {
2238                                                 /* Skip gold */
2239                                                 if (o_ptr->tval == TV_GOLD) continue;
2240
2241                                                 /*
2242                                                  * Skip "real" corpses and statues, to avoid extreme
2243                                                  * silliness like a novice rogue pockets full of statues
2244                                                  * and corpses.
2245                                                  */
2246                                                 if ((o_ptr->tval == TV_CORPSE) ||
2247                                                     (o_ptr->tval == TV_STATUE)) continue;
2248                                         }
2249
2250                                         /* Extract some flags */
2251                                         object_flags(o_ptr, flgs);
2252
2253                                         /* Acquire the object name */
2254                                         object_desc(o_name, o_ptr, 0);
2255                                         monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
2256
2257                                         /* React to objects that hurt the monster */
2258                                         if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
2259                                         if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
2260                                         if (have_flag(flgs, TR_SLAY_TROLL))  flg3 |= (RF3_TROLL);
2261                                         if (have_flag(flgs, TR_KILL_TROLL))  flg3 |= (RF3_TROLL);
2262                                         if (have_flag(flgs, TR_SLAY_GIANT))  flg3 |= (RF3_GIANT);
2263                                         if (have_flag(flgs, TR_KILL_GIANT))  flg3 |= (RF3_GIANT);
2264                                         if (have_flag(flgs, TR_SLAY_ORC))    flg3 |= (RF3_ORC);
2265                                         if (have_flag(flgs, TR_KILL_ORC))    flg3 |= (RF3_ORC);
2266                                         if (have_flag(flgs, TR_SLAY_DEMON))  flg3 |= (RF3_DEMON);
2267                                         if (have_flag(flgs, TR_KILL_DEMON))  flg3 |= (RF3_DEMON);
2268                                         if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
2269                                         if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
2270                                         if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
2271                                         if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
2272                                         if (have_flag(flgs, TR_SLAY_EVIL))   flg3 |= (RF3_EVIL);
2273                                         if (have_flag(flgs, TR_KILL_EVIL))   flg3 |= (RF3_EVIL);
2274                                         if (have_flag(flgs, TR_SLAY_HUMAN))  flg2 |= (RF2_HUMAN);
2275                                         if (have_flag(flgs, TR_KILL_HUMAN))  flg2 |= (RF2_HUMAN);
2276                                         if (have_flag(flgs, TR_BRAND_ACID))  flgr |= (RFR_IM_ACID);
2277                                         if (have_flag(flgs, TR_BRAND_ELEC))  flgr |= (RFR_IM_ELEC);
2278                                         if (have_flag(flgs, TR_BRAND_FIRE))  flgr |= (RFR_IM_FIRE);
2279                                         if (have_flag(flgs, TR_BRAND_COLD))  flgr |= (RFR_IM_COLD);
2280                                         if (have_flag(flgs, TR_BRAND_POIS))  flgr |= (RFR_IM_POIS);
2281
2282                                         /* The object cannot be picked up by the monster */
2283                                         if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
2284                                             ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
2285                                         {
2286                                                 /* Only give a message for "take_item" */
2287                                                 if (do_take && (r_ptr->flags2 & RF2_STUPID))
2288                                                 {
2289                                                         did_take_item = TRUE;
2290
2291                                                         /* Describe observable situations */
2292                                                         if (m_ptr->ml && player_can_see_bold(target_ptr, ny, nx))
2293                                                         {
2294                                                                 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
2295                                                         }
2296                                                 }
2297                                         }
2298
2299                                         /* Pick up the item */
2300                                         else if (do_take)
2301                                         {
2302                                                 did_take_item = TRUE;
2303
2304                                                 /* Describe observable situations */
2305                                                 if (player_can_see_bold(target_ptr, ny, nx))
2306                                                 {
2307                                                         msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
2308                                                 }
2309
2310                                                 /* Excise the object */
2311                                                 excise_object_idx(target_ptr->current_floor_ptr, this_o_idx);
2312
2313                                                 /* Forget mark */
2314                                                 o_ptr->marked &= OM_TOUCHED;
2315
2316                                                 /* Forget location */
2317                                                 o_ptr->iy = o_ptr->ix = 0;
2318
2319                                                 /* Memorize monster */
2320                                                 o_ptr->held_m_idx = m_idx;
2321
2322                                                 /* Build a stack */
2323                                                 o_ptr->next_o_idx = m_ptr->hold_o_idx;
2324
2325                                                 /* Carry object */
2326                                                 m_ptr->hold_o_idx = this_o_idx;
2327                                         }
2328
2329                                         /* Destroy the item if not a pet */
2330                                         else if (!is_pet(m_ptr))
2331                                         {
2332                                                 did_kill_item = TRUE;
2333
2334                                                 /* Describe observable situations */
2335                                                 if (player_has_los_bold(target_ptr, ny, nx))
2336                                                 {
2337                                                         msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
2338                                                 }
2339
2340                                                 delete_object_idx(target_ptr->current_floor_ptr, this_o_idx);
2341                                         }
2342                                 }
2343                         }
2344                 }
2345
2346                 /* Stop when done */
2347                 if (do_turn) break;
2348         }
2349
2350         /*
2351          *  Forward movements failed, but now received LOS attack!
2352          *  Try to flow by smell.
2353          */
2354         if (target_ptr->no_flowed && i > 2 &&  m_ptr->target_y)
2355                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2356
2357         /* If we haven't done anything, try casting a spell again */
2358         if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
2359         {
2360                 /* Try to cast spell again */
2361                 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
2362                 {
2363                         if (make_attack_spell(m_idx, target_ptr)) return;
2364                 }
2365         }
2366
2367
2368         /* Notice changes in view */
2369         if (do_view)
2370         {
2371                 target_ptr->update |= (PU_FLOW);
2372                 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2373         }
2374
2375         /* Notice changes in view */
2376         if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
2377                 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !target_ptr->phase_out)))
2378         {
2379                 target_ptr->update |= (PU_MON_LITE);
2380         }
2381
2382         /* Learn things from observable monster */
2383         if (is_original_ap_and_seen(m_ptr))
2384         {
2385                 /* Monster opened a door */
2386                 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
2387
2388                 /* Monster bashed a door */
2389                 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
2390
2391                 /* Monster tried to pick something up */
2392                 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
2393
2394                 /* Monster tried to crush something */
2395                 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
2396
2397                 /* Monster pushed past another monster */
2398                 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
2399
2400                 /* Monster passed through a wall */
2401                 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
2402
2403                 /* Monster destroyed a wall */
2404                 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2405         }
2406
2407
2408         /* Hack -- get "bold" if out of options */
2409         if (!do_turn && !do_move && MON_MONFEAR(m_ptr) && aware)
2410         {
2411                 /* No longer afraid */
2412                 (void)set_monster_monfear(m_idx, 0);
2413
2414                 /* Message if seen */
2415                 if (see_m)
2416                 {
2417                         GAME_TEXT m_name[MAX_NLEN];
2418                         monster_desc(m_name, m_ptr, 0);
2419                         msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
2420                 }
2421
2422                 if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
2423
2424                 /* Actually do something now (?) */
2425         }
2426 }
2427
2428 /*!
2429  * @brief 全モンスターのターン管理メインルーチン /
2430  * Process all the "live" monsters, once per game turn.
2431  * @return なし
2432  * @details
2433  * During each game current game turn, we scan through the list of all the "live" monsters,\n
2434  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
2435  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
2436  *\n
2437  * Note that monsters can never move in the monster array (except when the\n
2438  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
2439  *\n
2440  * This function is responsible for at least half of the processor time\n
2441  * on a normal system with a "normal" amount of monsters and a player doing\n
2442  * normal things.\n
2443  *\n
2444  * When the player is resting, virtually 90% of the processor time is spent\n
2445  * in this function, and its children, "process_monster()" and "make_move()".\n
2446  *\n
2447  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
2448  * especially when the player is running.\n
2449  *\n
2450  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
2451  * monsters while they are still being "born".  A monster is "fresh" only\n
2452  * during the game turn in which it is created, and we use the "hack_m_idx" to\n
2453  * determine if the monster is yet to be processed during the game turn.\n
2454  *\n
2455  * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
2456  * move before any "nasty" monsters get to use their spell attacks.\n
2457  *\n
2458  * Note that when the "knowledge" about the currently tracked monster\n
2459  * changes (flags, attacks, spells), we induce a redraw of the monster\n
2460  * recall window.\n
2461  */
2462 void process_monsters(player_type *target_ptr)
2463 {
2464         MONSTER_IDX i;
2465         POSITION fx, fy;
2466
2467         bool            test;
2468
2469         monster_type    *m_ptr;
2470         monster_race    *r_ptr;
2471
2472         MONRACE_IDX old_monster_race_idx;
2473
2474         BIT_FLAGS old_r_flags1 = 0L;
2475         BIT_FLAGS old_r_flags2 = 0L;
2476         BIT_FLAGS old_r_flags3 = 0L;
2477         BIT_FLAGS old_r_flags4 = 0L;
2478         BIT_FLAGS old_r_flags5 = 0L;
2479         BIT_FLAGS old_r_flags6 = 0L;
2480         BIT_FLAGS old_r_flagsr = 0L;
2481
2482         byte old_r_blows0 = 0;
2483         byte old_r_blows1 = 0;
2484         byte old_r_blows2 = 0;
2485         byte old_r_blows3 = 0;
2486
2487         byte old_r_cast_spell = 0;
2488
2489         SPEED speed;
2490
2491         /* Clear monster fighting indicator */
2492         floor_type *floor_ptr = target_ptr->current_floor_ptr;
2493         floor_ptr->monster_noise = FALSE;
2494
2495         /* Memorize old race */
2496         old_monster_race_idx = target_ptr->monster_race_idx;
2497
2498         /* Acquire knowledge */
2499         if (target_ptr->monster_race_idx)
2500         {
2501                 /* Acquire current monster */
2502                 r_ptr = &r_info[target_ptr->monster_race_idx];
2503
2504                 /* Memorize flags */
2505                 old_r_flags1 = r_ptr->r_flags1;
2506                 old_r_flags2 = r_ptr->r_flags2;
2507                 old_r_flags3 = r_ptr->r_flags3;
2508                 old_r_flags4 = r_ptr->r_flags4;
2509                 old_r_flags5 = r_ptr->r_flags5;
2510                 old_r_flags6 = r_ptr->r_flags6;
2511                 old_r_flagsr = r_ptr->r_flagsr;
2512
2513                 /* Memorize blows */
2514                 old_r_blows0 = r_ptr->r_blows[0];
2515                 old_r_blows1 = r_ptr->r_blows[1];
2516                 old_r_blows2 = r_ptr->r_blows[2];
2517                 old_r_blows3 = r_ptr->r_blows[3];
2518
2519                 /* Memorize castings */
2520                 old_r_cast_spell = r_ptr->r_cast_spell;
2521         }
2522
2523
2524         /* Process the monsters (backwards) */
2525         for (i = floor_ptr->m_max - 1; i >= 1; i--)
2526         {
2527                 m_ptr = &floor_ptr->m_list[i];
2528                 r_ptr = &r_info[m_ptr->r_idx];
2529
2530                 /* Handle "leaving" */
2531                 if (target_ptr->leaving) break;
2532
2533                 /* Ignore "dead" monsters */
2534                 if (!monster_is_valid(m_ptr)) continue;
2535
2536                 if (target_ptr->wild_mode) continue;
2537
2538
2539                 /* Handle "fresh" monsters */
2540                 if (m_ptr->mflag & MFLAG_BORN)
2541                 {
2542                         /* No longer "fresh" */
2543                         m_ptr->mflag &= ~(MFLAG_BORN);
2544
2545                         /* Skip */
2546                         continue;
2547                 }
2548
2549                 /* Hack -- Require proximity */
2550                 if (m_ptr->cdis >= AAF_LIMIT) continue;
2551
2552                 fx = m_ptr->fx;
2553                 fy = m_ptr->fy;
2554
2555                 /* Flow by smell is allowed */
2556                 if (!target_ptr->no_flowed)
2557                 {
2558                         m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2559                 }
2560
2561                 /* Assume no move */
2562                 test = FALSE;
2563
2564                 /* Handle "sensing radius" */
2565                 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
2566                 {
2567                         /* We can "sense" the player */
2568                         test = TRUE;
2569                 }
2570
2571                 /* Handle "sight" and "aggravation" */
2572         else if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) &&
2573                         (player_has_los_bold(target_ptr, fy, fx) || (target_ptr->cursed & TRC_AGGRAVATE)))
2574                 {
2575                         /* We can "see" or "feel" the player */
2576                         test = TRUE;
2577                 }
2578
2579 #if 0 /* (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when == floor_ptr->grid_array[fy][fx].when) is always FALSE... */
2580                 /* Hack -- Monsters can "smell" the player from far away */
2581                 /* Note that most monsters have "aaf" of "20" or so */
2582                 else if (!(m_ptr->mflag2 & MFLAG2_NOFLOW) &&
2583                         cave_have_flag_bold(target_ptr->y, target_ptr->x, FF_MOVE) &&
2584                         (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when == floor_ptr->grid_array[fy][fx].when) &&
2585                         (floor_ptr->grid_array[fy][fx].dist < MONSTER_FLOW_DEPTH) &&
2586                         (floor_ptr->grid_array[fy][fx].dist < r_ptr->aaf))
2587                 {
2588                         /* We can "smell" the player */
2589                         test = TRUE;
2590                 }
2591 #endif
2592                 else if (m_ptr->target_y) test = TRUE;
2593
2594                 /* Do nothing */
2595                 if (!test) continue;
2596
2597
2598                 if (target_ptr->riding == i)
2599                         speed = target_ptr->pspeed;
2600                 else
2601                 {
2602                         speed = m_ptr->mspeed;
2603
2604                         /* Monsters move quickly in Nightmare mode */
2605                         if (ironman_nightmare) speed += 5;
2606
2607                         if (MON_FAST(m_ptr)) speed += 10;
2608                         if (MON_SLOW(m_ptr)) speed -= 10;
2609                 }
2610
2611                 /* Give this monster some energy */
2612                 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
2613
2614                 /* Not enough energy to move */
2615                 if (m_ptr->energy_need > 0) continue;
2616
2617                 /* Use up "some" energy */
2618                 m_ptr->energy_need += ENERGY_NEED();
2619
2620                 /* Save global index */
2621                 hack_m_idx = i;
2622
2623                 /* Process the monster */
2624                 process_monster(target_ptr, i);
2625
2626                 reset_target(m_ptr);
2627
2628                 /* Give up flow_by_smell when it might useless */
2629                 if (target_ptr->no_flowed && one_in_(3))
2630                         m_ptr->mflag2 |= MFLAG2_NOFLOW;
2631
2632                 /* Hack -- notice death or departure */
2633                 if (!target_ptr->playing || target_ptr->is_dead) break;
2634
2635                 /* Notice leaving */
2636                 if (target_ptr->leaving) break;
2637         }
2638
2639         /* Reset global index */
2640         hack_m_idx = 0;
2641
2642
2643         /* Tracking a monster race (the same one we were before) */
2644         if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
2645                 return;
2646
2647         /* Acquire monster race */
2648         r_ptr = &r_info[target_ptr->monster_race_idx];
2649
2650         /* Check for knowledge change */
2651         if ((old_r_flags1 != r_ptr->r_flags1) ||
2652                 (old_r_flags2 != r_ptr->r_flags2) ||
2653                 (old_r_flags3 != r_ptr->r_flags3) ||
2654                 (old_r_flags4 != r_ptr->r_flags4) ||
2655                 (old_r_flags5 != r_ptr->r_flags5) ||
2656                 (old_r_flags6 != r_ptr->r_flags6) ||
2657                 (old_r_flagsr != r_ptr->r_flagsr) ||
2658                 (old_r_blows0 != r_ptr->r_blows[0]) ||
2659                 (old_r_blows1 != r_ptr->r_blows[1]) ||
2660                 (old_r_blows2 != r_ptr->r_blows[2]) ||
2661                 (old_r_blows3 != r_ptr->r_blows[3]) ||
2662                 (old_r_cast_spell != r_ptr->r_cast_spell))
2663         {
2664                 target_ptr->window |= (PW_MONSTER);
2665         }
2666 }
2667