OSDN Git Service

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