OSDN Git Service

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