OSDN Git Service

[Refactor] #37353 マジックナンバー修正(A_MAX) / Fix magic number (A_MAX).
[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 "cmd-pet.h"
18 #include "monsterrace-hook.h"
19 #include "melee.h"
20
21
22 /*!
23  * @brief モンスターが敵に接近するための方向を決める /
24  * Calculate the direction to the next enemy
25  * @param m_idx モンスターの参照ID
26  * @param mm 移動するべき方角IDを返す参照ポインタ
27  * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
28  */
29 static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
30 {
31         int i;
32         POSITION x = 0, y = 0;
33         IDX t_idx;
34         int start;
35         int plus = 1;
36
37         monster_type *m_ptr = &m_list[m_idx];
38         monster_race *r_ptr = &r_info[m_ptr->r_idx];
39         monster_type *t_ptr;
40
41         if (riding_t_m_idx && player_bold(m_ptr->fy, m_ptr->fx))
42         {
43                 y = m_list[riding_t_m_idx].fy;
44                 x = m_list[riding_t_m_idx].fx;
45         }
46         else if (is_pet(m_ptr) && pet_t_m_idx)
47         {
48                 y = m_list[pet_t_m_idx].fy;
49                 x = m_list[pet_t_m_idx].fx;
50         }
51         else
52         {
53                 if (p_ptr->inside_battle)
54                 {
55                         start = randint1(m_max-1)+m_max;
56                         if(randint0(2)) plus = -1;
57                 }
58                 else start = m_max + 1;
59
60                 /* Scan thru all monsters */
61                 for (i = start; ((i < start + m_max) && (i > start - m_max)); i+=plus)
62                 {
63                         IDX dummy = (i % m_max);
64
65                         if (!dummy) continue;
66
67                         t_idx = dummy;
68                         t_ptr = &m_list[t_idx];
69
70                         /* The monster itself isn't a target */
71                         if (t_ptr == m_ptr) continue;
72
73                         /* Paranoia -- Skip dead monsters */
74                         if (!t_ptr->r_idx) continue;
75
76                         if (is_pet(m_ptr))
77                         {
78                                 /* Hack -- only fight away from player */
79                                 if (p_ptr->pet_follow_distance < 0)
80                                 {
81                                         /* No fighting near player */
82                                         if (t_ptr->cdis <= (0 - p_ptr->pet_follow_distance))
83                                         {
84                                                 continue;
85                                         }
86                                 }
87                                 /* Hack -- no fighting away from player */
88                                 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > p_ptr->pet_follow_distance))
89                                 {
90                                         continue;
91                                 }
92
93                                 if (r_ptr->aaf < t_ptr->cdis) continue;
94                         }
95
96                         /* Monster must be 'an enemy' */
97                         if (!are_enemies(m_ptr, t_ptr)) continue;
98
99                         /* Monster must be projectable if we can't pass through walls */
100                         if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) ||
101                             ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)))
102                         {
103                                 if (!in_disintegration_range(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
104                         }
105                         else
106                         {
107                                 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
108                         }
109
110                         /* OK -- we've got a target */
111                         y = t_ptr->fy;
112                         x = t_ptr->fx;
113
114                         break;
115                 }
116                 if (!x && !y) return FALSE;
117         }
118
119         /* Extract the direction */
120         x -= m_ptr->fx;
121         y -= m_ptr->fy;
122
123         /* North */
124         if ((y < 0) && (x == 0))
125         {
126                 mm[0] = 8;
127                 mm[1] = 7;
128                 mm[2] = 9;
129         }
130         /* South */
131         else if ((y > 0) && (x == 0))
132         {
133                 mm[0] = 2;
134                 mm[1] = 1;
135                 mm[2] = 3;
136         }
137         /* East */
138         else if ((x > 0) && (y == 0))
139         {
140                 mm[0] = 6;
141                 mm[1] = 9;
142                 mm[2] = 3;
143         }
144         /* West */
145         else if ((x < 0) && (y == 0))
146         {
147                 mm[0] = 4;
148                 mm[1] = 7;
149                 mm[2] = 1;
150         }
151         /* North-West */
152         else if ((y < 0) && (x < 0))
153         {
154                 mm[0] = 7;
155                 mm[1] = 4;
156                 mm[2] = 8;
157         }
158         /* North-East */
159         else if ((y < 0) && (x > 0))
160         {
161                 mm[0] = 9;
162                 mm[1] = 6;
163                 mm[2] = 8;
164         }
165         /* South-West */
166         else if ((y > 0) && (x < 0))
167         {
168                 mm[0] = 1;
169                 mm[1] = 4;
170                 mm[2] = 2;
171         }
172         /* South-East */
173         else if ((y > 0) && (x > 0))
174         {
175                 mm[0] = 3;
176                 mm[1] = 6;
177                 mm[2] = 2;
178         }
179
180         /* Found a monster */
181         return TRUE;
182 }
183
184
185 /*!
186  * @brief モンスターが敵モンスターに行う打撃処理 /
187  * Hack, based on mon_take_hit... perhaps all monster attacks on other monsters should use this?
188  * @param m_idx 目標となるモンスターの参照ID
189  * @param dam ダメージ量
190  * @param dead 目標となったモンスターの死亡状態を返す参照ポインタ
191  * @param fear 目標となったモンスターの恐慌状態を返す参照ポインタ
192  * @param note 目標モンスターが死亡した場合の特別メッセージ(NULLならば標準表示を行う)
193  * @param who 打撃を行ったモンスターの参照ID
194  * @return なし
195  */
196 void mon_take_hit_mon(MONSTER_IDX m_idx, HIT_POINT dam, bool *dead, bool *fear, cptr note, IDX who)
197 {
198         monster_type *m_ptr = &m_list[m_idx];
199         monster_race *r_ptr = &r_info[m_ptr->r_idx];
200         GAME_TEXT m_name[160];
201         bool seen = is_seen(m_ptr);
202
203         /* Can the player be aware of this attack? */
204         bool known = (m_ptr->cdis <= MAX_SIGHT);
205
206         /* Extract monster name */
207         monster_desc(m_name, m_ptr, 0);
208
209         /* Redraw (later) if needed */
210         if (m_ptr->ml)
211         {
212                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
213                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
214         }
215
216         (void)set_monster_csleep(m_idx, 0);
217
218         if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
219
220         if (MON_INVULNER(m_ptr) && randint0(PENETRATE_INVULNERABILITY))
221         {
222                 if (seen)
223                 {
224                         msg_format(_("%^sはダメージを受けない。", "%^s is unharmed."), m_name);
225                 }
226                 return;
227         }
228
229         if (r_ptr->flagsr & RFR_RES_ALL)
230         {
231                 if(dam > 0)
232                 {
233                         dam /= 100;
234                         if((dam == 0) && one_in_(3)) dam = 1;
235                 }
236                 if(dam == 0)
237                 {
238                         if (seen)
239                         {
240                                 msg_format(_("%^sはダメージを受けない。", "%^s is unharmed."), m_name);
241                         }
242                         return;
243                 }
244         }
245
246         /* Hurt it */
247         m_ptr->hp -= dam;
248
249         /* It is dead now... or is it? */
250         if (m_ptr->hp < 0)
251         {
252                 if (((r_ptr->flags1 & (RF1_UNIQUE | RF1_QUESTOR)) ||
253                     (r_ptr->flags7 & RF7_NAZGUL)) &&
254                     !p_ptr->inside_battle)
255                 {
256                         m_ptr->hp = 1;
257                 }
258                 else
259                 {
260                         /* Make a sound */
261                         if (!monster_living(m_ptr->r_idx))
262                         {
263                                 sound(SOUND_N_KILL);
264                         }
265                         else
266                         {
267                                 sound(SOUND_KILL);
268                         }
269
270                         *dead = TRUE;
271
272                         if (known)
273                         {
274                                 monster_desc(m_name, m_ptr, MD_TRUE_NAME);
275                                 /* Unseen death by normal attack */
276                                 if (!seen)
277                                 {
278                                         mon_fight = TRUE;
279                                 }
280                                 /* Death by special attack */
281                                 else if (note)
282                                 {
283                                         msg_format(_("%^s%s", "%^s%s"), m_name, note);
284                                 }
285                                 /* Death by normal attack -- nonliving monster */
286                                 else if (!monster_living(m_ptr->r_idx))
287                                 {
288                                         msg_format(_("%^sは破壊された。", "%^s is destroyed."), m_name);
289                                 }
290                                 /* Death by normal attack -- living monster */
291                                 else
292                                 {
293                                         msg_format(_("%^sは殺された。", "%^s is killed."), m_name);
294                                 }
295                         }
296
297                         monster_gain_exp(who, m_ptr->r_idx);
298                         monster_death(m_idx, FALSE);
299                         delete_monster_idx(m_idx);
300
301                         /* Not afraid */
302                         (*fear) = FALSE;
303
304                         /* Monster is dead */
305                         return;
306                 }
307         }
308
309         *dead = FALSE;
310
311 #ifdef ALLOW_FEAR
312
313         /* Mega-Hack -- Pain cancels fear */
314         if (MON_MONFEAR(m_ptr) && (dam > 0))
315         {
316                 /* Cure fear */
317                 if (set_monster_monfear(m_idx, MON_MONFEAR(m_ptr) - randint1(dam / 4)))
318                 {
319                         /* No more fear */
320                         (*fear) = FALSE;
321                 }
322         }
323
324         /* Sometimes a monster gets scared by damage */
325         if (!MON_MONFEAR(m_ptr) && !(r_ptr->flags3 & RF3_NO_FEAR))
326         {
327                 /* Percentage of fully healthy */
328                 int percentage = (100L * m_ptr->hp) / m_ptr->maxhp;
329
330                 /*
331                 * Run (sometimes) if at 10% or less of max hit points,
332                 * or (usually) when hit for half its current hit points
333                  */
334                 if (((percentage <= 10) && (randint0(10) < percentage)) ||
335                         ((dam >= m_ptr->hp) && (randint0(100) < 80)))
336                 {
337                         /* Hack -- note fear */
338                         (*fear) = TRUE;
339
340                         /* Hack -- Add some timed fear */
341                         (void)set_monster_monfear(m_idx, (randint1(10) +
342                                 (((dam >= m_ptr->hp) && (percentage > 7)) ?
343                                 20 : ((11 - percentage) * 5))));
344                 }
345         }
346
347 #endif /* ALLOW_FEAR */
348
349         if ((dam > 0) && !is_pet(m_ptr) && !is_friendly(m_ptr) && (who != m_idx))
350         {
351                 if (is_pet(&m_list[who]) && !player_bold(m_ptr->target_y, m_ptr->target_x))
352                 {
353                         set_target(m_ptr, m_list[who].fy, m_list[who].fx);
354                 }
355         }
356
357         if (p_ptr->riding && (p_ptr->riding == m_idx) && (dam > 0))
358         {
359                 /* Extract monster name */
360                 monster_desc(m_name, m_ptr, 0);
361
362                 if (m_ptr->hp > m_ptr->maxhp/3) dam = (dam + 1) / 2;
363                 if (rakuba((dam > 200) ? 200 : dam, FALSE))
364                 {
365                         msg_format(_("%^sに振り落とされた!", "You have thrown off from %s!"), m_name);
366                 }
367         }
368
369         /* Not dead yet */
370         return;
371 }
372
373
374 /*!
375  * @brief モンスターがプレイヤーから逃走するかどうかを返す /
376  * Returns whether a given monster will try to run from the player.
377  * @param m_idx 逃走するモンスターの参照ID
378  * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
379  * @details
380  * Monsters will attempt to avoid very powerful players.  See below.\n
381  *\n
382  * Because this function is called so often, little details are important\n
383  * for efficiency.  Like not using "mod" or "div" when possible.  And\n
384  * attempting to check the conditions in an optimal order.  Note that\n
385  * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
386  *\n
387  * Note that this function is responsible for about one to five percent\n
388  * of the processor use in normal conditions...\n
389  */
390 static bool mon_will_run(MONSTER_IDX m_idx)
391 {
392         monster_type *m_ptr = &m_list[m_idx];
393
394 #ifdef ALLOW_TERROR
395
396         monster_race *r_ptr = &r_info[m_ptr->r_idx];
397
398         PLAYER_LEVEL p_lev;
399         DEPTH m_lev;
400         HIT_POINT p_chp, p_mhp;
401         HIT_POINT m_chp, m_mhp;
402         u32b p_val, m_val;
403
404 #endif
405
406         /* Friends can be commanded to avoid the player */
407         if (is_pet(m_ptr))
408         {
409                 /* Are we trying to avoid the player? */
410                 return ((p_ptr->pet_follow_distance < 0) &&
411                                   (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
412         }
413
414         /* Keep monsters from running too far away */
415         if (m_ptr->cdis > MAX_SIGHT + 5) return (FALSE);
416
417         /* All "afraid" monsters will run away */
418         if (MON_MONFEAR(m_ptr)) return (TRUE);
419
420 #ifdef ALLOW_TERROR
421
422         /* Nearby monsters will not become terrified */
423         if (m_ptr->cdis <= 5) return (FALSE);
424
425         /* Examine player power (level) */
426         p_lev = p_ptr->lev;
427
428         /* Examine monster power (level plus morale) */
429         m_lev = r_ptr->level + (m_idx & 0x08) + 25;
430
431         /* Optimize extreme cases below */
432         if (m_lev > p_lev + 4) return (FALSE);
433         if (m_lev + 4 <= p_lev) return (TRUE);
434
435         /* Examine player health */
436         p_chp = p_ptr->chp;
437         p_mhp = p_ptr->mhp;
438
439         /* Examine monster health */
440         m_chp = m_ptr->hp;
441         m_mhp = m_ptr->maxhp;
442
443         /* Prepare to optimize the calculation */
444         p_val = (p_lev * p_mhp) + (p_chp << 2); /* div p_mhp */
445         m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
446
447         /* Strong players scare strong monsters */
448         if (p_val * m_mhp > m_val * p_mhp) return (TRUE);
449
450 #endif
451
452         /* Assume no terror */
453         return (FALSE);
454 }
455
456
457 /*!
458  * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
459  * Search spell castable grid
460  * @param m_idx モンスターの参照ID
461  * @param yp 適したマスのY座標を返す参照ポインタ
462  * @param xp 適したマスのX座標を返す参照ポインタ
463  * @return 有効なマスがあった場合TRUEを返す
464  */
465 static bool get_moves_aux2(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
466 {
467         int i, best = 999;
468         POSITION y, x, y1, x1;
469
470         cave_type *c_ptr;
471         bool can_open_door = FALSE;
472         int now_cost;
473
474         monster_type *m_ptr = &m_list[m_idx];
475         monster_race *r_ptr = &r_info[m_ptr->r_idx];
476
477         /* Monster location */
478         y1 = m_ptr->fy;
479         x1 = m_ptr->fx;
480
481         /* Monster can already cast spell to player */
482         if (projectable(y1, x1, p_ptr->y, p_ptr->x)) return (FALSE);
483
484         /* Set current grid cost */
485         now_cost = cave[y1][x1].cost;
486         if (now_cost == 0) now_cost = 999;
487
488         /* Can monster bash or open doors? */
489         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
490         {
491                 can_open_door = TRUE;
492         }
493
494         /* Check nearby grids, diagonals first */
495         for (i = 7; i >= 0; i--)
496         {
497                 int cost;
498
499                 y = y1 + ddy_ddd[i];
500                 x = x1 + ddx_ddd[i];
501
502                 /* Ignore locations off of edge */
503                 if (!in_bounds2(y, x)) continue;
504
505                 /* Simply move to player */
506                 if (player_bold(y, x)) return (FALSE);
507
508                 c_ptr = &cave[y][x];
509
510                 cost = c_ptr->cost;
511
512                 /* Monster cannot kill or pass walls */
513                 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))))
514                 {
515                         if (cost == 0) continue;
516                         if (!can_open_door && is_closed_door(c_ptr->feat)) continue;
517                 }
518
519                 /* Hack -- for kill or pass wall monster.. */
520                 if (cost == 0) cost = 998;
521
522                 if (now_cost < cost) continue;
523
524                 if (!projectable(y, x, p_ptr->y, p_ptr->x)) continue;
525
526                 /* Accept louder sounds */
527                 if (best < cost) continue;
528                 best = cost;
529
530                 (*yp) = y1 + ddy_ddd[i];
531                 (*xp) = x1 + ddx_ddd[i];
532         }
533
534         /* No legal move (?) */
535         if (best == 999) return (FALSE);
536
537         /* Success */
538         return (TRUE);
539 }
540
541
542 /*!
543  * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
544  * Choose the "best" direction for "flowing"
545  * @param m_idx モンスターの参照ID
546  * @param yp 移動先のマスのY座標を返す参照ポインタ
547  * @param xp 移動先のマスのX座標を返す参照ポインタ
548  * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
549  * @return 有効なマスがあった場合TRUEを返す
550  * @details
551  * Note that ghosts and rock-eaters are never allowed to "flow",\n
552  * since they should move directly towards the player.\n
553  *\n
554  * Prefer "non-diagonal" directions, but twiddle them a little\n
555  * to angle slightly towards the player's actual location.\n
556  *\n
557  * Allow very perceptive monsters to track old "spoor" left by\n
558  * previous locations occupied by the player.  This will tend\n
559  * to have monsters end up either near the player or on a grid\n
560  * recently occupied by the player (and left via "teleport").\n
561  *\n
562  * Note that if "smell" is turned on, all monsters get vicious.\n
563  *\n
564  * Also note that teleporting away from a location will cause\n
565  * the monsters who were chasing you to converge on that location\n
566  * as long as you are still near enough to "annoy" them without\n
567  * being close enough to chase directly.  I have no idea what will\n
568  * happen if you combine "smell" with low "aaf" values.\n
569  */
570 static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
571 {
572         int i, best;
573         POSITION y, x, y1, x1;
574
575         cave_type *c_ptr;
576         bool use_scent = FALSE;
577
578         monster_type *m_ptr = &m_list[m_idx];
579         monster_race *r_ptr = &r_info[m_ptr->r_idx];
580
581         /* Can monster cast attack spell? */
582         if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
583             r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
584             r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
585         {
586                 /* Can move spell castable grid? */
587                 if (get_moves_aux2(m_idx, yp, xp)) return (TRUE);
588         }
589
590         /* Monster can't flow */
591         if (no_flow) return (FALSE);
592
593         /* Monster can go through rocks */
594         if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) return (FALSE);
595         if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)) return (FALSE);
596
597         /* Monster location */
598         y1 = m_ptr->fy;
599         x1 = m_ptr->fx;
600
601         /* Hack -- Player can see us, run towards him */
602         if (player_has_los_bold(y1, x1) && projectable(p_ptr->y, p_ptr->x, y1, x1)) return (FALSE);
603
604         /* Monster grid */
605         c_ptr = &cave[y1][x1];
606
607         /* If we can hear noises, advance towards them */
608         if (c_ptr->cost)
609         {
610                 best = 999;
611         }
612
613         /* Otherwise, try to follow a scent trail */
614         else if (c_ptr->when)
615         {
616                 /* Too old smell */
617                 if (cave[p_ptr->y][p_ptr->x].when - c_ptr->when > 127) return (FALSE);
618
619                 use_scent = TRUE;
620                 best = 0;
621         }
622
623         /* Otherwise, advance blindly */
624         else
625         {
626                 return (FALSE);
627         }
628
629         /* Check nearby grids, diagonals first */
630         for (i = 7; i >= 0; i--)
631         {
632                 y = y1 + ddy_ddd[i];
633                 x = x1 + ddx_ddd[i];
634
635                 /* Ignore locations off of edge */
636                 if (!in_bounds2(y, x)) continue;
637
638                 c_ptr = &cave[y][x];
639
640                 /* We're following a scent trail */
641                 if (use_scent)
642                 {
643                         int when = c_ptr->when;
644
645                         /* Accept younger scent */
646                         if (best > when) continue;
647                         best = when;
648                 }
649
650                 /* We're using sound */
651                 else
652                 {
653                         int cost;
654
655                         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
656                                 cost = c_ptr->dist;
657                         else cost = c_ptr->cost;
658
659                         /* Accept louder sounds */
660                         if ((cost == 0) || (best < cost)) continue;
661                         best = cost;
662                 }
663
664                 /* Hack -- Save the "twiddled" location */
665                 (*yp) = p_ptr->y + 16 * ddy_ddd[i];
666                 (*xp) = p_ptr->x + 16 * ddx_ddd[i];
667         }
668
669         /* No legal move (?) */
670         if (best == 999 || best == 0) return (FALSE);
671
672         /* Success */
673         return (TRUE);
674 }
675
676
677 /*!
678  * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
679  * Provide a location to flee to, but give the player a wide berth.
680  * @param m_idx モンスターの参照ID
681  * @param yp 移動先のマスのY座標を返す参照ポインタ
682  * @param xp 移動先のマスのX座標を返す参照ポインタ
683  * @return 有効なマスがあった場合TRUEを返す
684  * @details
685  * A monster may wish to flee to a location that is behind the player,\n
686  * but instead of heading directly for it, the monster should "swerve"\n
687  * around the player so that he has a smaller chance of getting hit.\n
688  */
689 static bool get_fear_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
690 {
691         POSITION y, x, y1, x1, fy, fx, gy = 0, gx = 0;
692         int score = -1;
693         int i;
694
695         monster_type *m_ptr = &m_list[m_idx];
696
697         /* Monster location */
698         fy = m_ptr->fy;
699         fx = m_ptr->fx;
700
701         /* Desired destination */
702         y1 = fy - (*yp);
703         x1 = fx - (*xp);
704
705         /* Check nearby grids, diagonals first */
706         for (i = 7; i >= 0; i--)
707         {
708                 POSITION dis, s;
709
710                 y = fy + ddy_ddd[i];
711                 x = fx + ddx_ddd[i];
712
713                 /* Ignore locations off of edge */
714                 if (!in_bounds2(y, x)) continue;
715
716                 /* Don't move toward player */
717                 /* if (cave[y][x].dist < 3) continue; */ /* Hmm.. Need it? */
718
719                 /* Calculate distance of this grid from our destination */
720                 dis = distance(y, x, y1, x1);
721
722                 /* Score this grid */
723                 s = 5000 / (dis + 3) - 500 / (cave[y][x].dist + 1);
724
725                 /* No negative scores */
726                 if (s < 0) s = 0;
727
728                 /* Ignore lower scores */
729                 if (s < score) continue;
730
731                 /* Save the score and time */
732                 score = s;
733
734                 /* Save the location */
735                 gy = y;
736                 gx = x;
737         }
738
739         /* No legal move (?) */
740         if (score == -1) return (FALSE);
741
742         /* Find deltas */
743         (*yp) = fy - gy;
744         (*xp) = fx - gx;
745
746         /* Success */
747         return (TRUE);
748 }
749
750 /*
751  * Hack -- Precompute a bunch of calls to distance() in find_safety() and
752  * find_hiding().
753  *
754  * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
755  * offsets of all the locations with a distance of n from a central point,
756  * with an offset of (0,0) indicating no more offsets at this distance.
757  *
758  * This is, of course, fairly unreadable, but it eliminates multiple loops
759  * from the previous version.
760  *
761  * It is probably better to replace these arrays with code to compute
762  * the relevant arrays, even if the storage is pre-allocated in hard
763  * coded sizes.  At the very least, code should be included which is
764  * able to generate and dump these arrays (ala "los()").  
765  *
766  * Also, the storage needs could be halved by using bytes.  
767  *
768  * These arrays could be combined into two big arrays, using sub-arrays
769  * to hold the offsets and lengths of each portion of the sub-arrays, and
770  * this could perhaps also be used somehow in the "look" code.  
771  */
772
773
774 static POSITION d_off_y_0[] = { 0 };
775 static POSITION d_off_x_0[] = { 0 };
776
777 static POSITION d_off_y_1[] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
778 static POSITION d_off_x_1[] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
779
780 static POSITION d_off_y_2[] = { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
781 static POSITION d_off_x_2[] = { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
782
783 static POSITION d_off_y_3[] = { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2, 3, 3, 3, 0 };
784 static POSITION d_off_x_3[] = { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2, -1, 0, 1, 0 };
785
786 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 };
787 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 };
788
789
790 static POSITION d_off_y_5[] =
791 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
792   -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
793   5, 0 };
794
795 static POSITION d_off_x_5[] =
796 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
797   -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
798   0, 1, 0 };
799
800
801 static POSITION d_off_y_6[] =
802 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
803   -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
804   5, 5, 6, 6, 6, 0 };
805
806 static POSITION d_off_x_6[] =
807 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
808   0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
809   -3, 2, 3, -1, 0, 1, 0 };
810
811
812 static POSITION d_off_y_7[] =
813 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
814   -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
815   3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
816
817 static POSITION d_off_x_7[] =
818 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
819   -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
820   6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
821   1, 0 };
822
823
824 static POSITION d_off_y_8[] =
825 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
826   -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
827   2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
828   8, 8, 8, 0 };
829
830 static POSITION d_off_x_8[] =
831 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
832   5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
833   7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
834   2, 3, -1, 0, 1, 0 };
835
836
837 static POSITION d_off_y_9[] =
838 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
839   -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
840   0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
841   7, 8, 8, 8, 8, 9, 9, 9, 0 };
842
843 static POSITION d_off_x_9[] =
844 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
845   -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
846   9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
847   4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
848
849
850 static POSITION *dist_offsets_y[10] =
851 {
852         d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
853         d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
854 };
855
856 static POSITION *dist_offsets_x[10] =
857 {
858         d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
859         d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
860 };
861
862 /*!
863  * @brief モンスターが逃げ込める安全な地点を返す /
864  * Choose a "safe" location near a monster for it to run toward.
865  * @param m_idx モンスターの参照ID
866  * @param yp 移動先のマスのY座標を返す参照ポインタ
867  * @param xp 移動先のマスのX座標を返す参照ポインタ
868  * @return 有効なマスがあった場合TRUEを返す
869  * @details
870  * A location is "safe" if it can be reached quickly and the player\n
871  * is not able to fire into it (it isn't a "clean shot").  So, this will\n
872  * cause monsters to "duck" behind walls.  Hopefully, monsters will also\n
873  * try to run towards corridor openings if they are in a room.\n
874  *\n
875  * This function may take lots of CPU time if lots of monsters are\n
876  * fleeing.\n
877  *\n
878  * Return TRUE if a safe location is available.\n
879  */
880 static bool find_safety(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
881 {
882         monster_type *m_ptr = &m_list[m_idx];
883
884         POSITION fy = m_ptr->fy;
885         POSITION fx = m_ptr->fx;
886
887         POSITION y, x, dy, dx, d, dis, i;
888         POSITION gy = 0, gx = 0, gdis = 0;
889
890         POSITION *y_offsets;
891         POSITION *x_offsets;
892
893         cave_type *c_ptr;
894
895         /* Start with adjacent locations, spread further */
896         for (d = 1; d < 10; d++)
897         {
898                 /* Get the lists of points with a distance d from (fx, fy) */
899                 y_offsets = dist_offsets_y[d];
900                 x_offsets = dist_offsets_x[d];
901
902                 /* Check the locations */
903                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
904                      dx != 0 || dy != 0;
905                      i++, dx = x_offsets[i], dy = y_offsets[i])
906                 {
907                         y = fy + dy;
908                         x = fx + dx;
909
910                         /* Skip illegal locations */
911                         if (!in_bounds(y, x)) continue;
912
913                         c_ptr = &cave[y][x];
914
915                         /* Skip locations in a wall */
916                         if (!monster_can_cross_terrain(c_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == p_ptr->riding) ? CEM_RIDING : 0)) continue;
917
918                         /* Check for "availability" (if monsters can flow) */
919                         if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
920                         {
921                                 /* Ignore grids very far from the player */
922                                 if (c_ptr->dist == 0) continue;
923
924                                 /* Ignore too-distant grids */
925                                 if (c_ptr->dist > cave[fy][fx].dist + 2 * d) continue;
926                         }
927
928                         /* Check for absence of shot (more or less) */
929                         if (!projectable(p_ptr->y, p_ptr->x, y, x))
930                         {
931                                 /* Calculate distance from player */
932                                 dis = distance(y, x, p_ptr->y, p_ptr->x);
933
934                                 /* Remember if further than previous */
935                                 if (dis > gdis)
936                                 {
937                                         gy = y;
938                                         gx = x;
939                                         gdis = dis;
940                                 }
941                         }
942                 }
943
944                 /* Check for success */
945                 if (gdis > 0)
946                 {
947                         /* Good location */
948                         (*yp) = fy - gy;
949                         (*xp) = fx - gx;
950
951                         /* Found safe place */
952                         return (TRUE);
953                 }
954         }
955
956         /* No safe place */
957         return (FALSE);
958 }
959
960
961 /*!
962  * @brief モンスターが隠れ潜める地点を返す /
963  * Choose a good hiding place near a monster for it to run toward.
964  * @param m_idx モンスターの参照ID
965  * @param yp 移動先のマスのY座標を返す参照ポインタ
966  * @param xp 移動先のマスのX座標を返す参照ポインタ
967  * @return 有効なマスがあった場合TRUEを返す
968  * @details
969  * Pack monsters will use this to "ambush" the player and lure him out\n
970  * of corridors into open space so they can swarm him.\n
971  *\n
972  * Return TRUE if a good location is available.\n
973  */
974 static bool find_hiding(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
975 {
976         monster_type *m_ptr = &m_list[m_idx];
977         monster_race *r_ptr = &r_info[m_ptr->r_idx];
978
979         POSITION fy = m_ptr->fy;
980         POSITION fx = m_ptr->fx;
981
982         POSITION y, x, dy, dx, d, dis, i;
983         POSITION gy = 0, gx = 0, gdis = 999;
984
985         POSITION *y_offsets, *x_offsets;
986
987         /* Start with adjacent locations, spread further */
988         for (d = 1; d < 10; d++)
989         {
990                 /* Get the lists of points with a distance d from (fx, fy) */
991                 y_offsets = dist_offsets_y[d];
992                 x_offsets = dist_offsets_x[d];
993
994                 /* Check the locations */
995                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
996                      dx != 0 || dy != 0;
997                      i++, dx = x_offsets[i], dy = y_offsets[i])
998                 {
999                         y = fy + dy;
1000                         x = fx + dx;
1001
1002                         /* Skip illegal locations */
1003                         if (!in_bounds(y, x)) continue;
1004
1005                         /* Skip occupied locations */
1006                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
1007
1008                         /* Check for hidden, available grid */
1009                         if (!projectable(p_ptr->y, p_ptr->x, y, x) && clean_shot(fy, fx, y, x, FALSE))
1010                         {
1011                                 /* Calculate distance from player */
1012                                 dis = distance(y, x, p_ptr->y, p_ptr->x);
1013
1014                                 /* Remember if closer than previous */
1015                                 if (dis < gdis && dis >= 2)
1016                                 {
1017                                         gy = y;
1018                                         gx = x;
1019                                         gdis = dis;
1020                                 }
1021                         }
1022                 }
1023
1024                 /* Check for success */
1025                 if (gdis < 999)
1026                 {
1027                         /* Good location */
1028                         (*yp) = fy - gy;
1029                         (*xp) = fx - gx;
1030
1031                         /* Found good place */
1032                         return (TRUE);
1033                 }
1034         }
1035
1036         /* No good place */
1037         return (FALSE);
1038 }
1039
1040
1041 /*!
1042  * @brief モンスターの移動方向を返す /
1043  * Choose "logical" directions for monster movement
1044  * @param m_idx モンスターの参照ID
1045  * @param mm 移動方向を返す方向IDの参照ポインタ
1046  * @return 有効方向があった場合TRUEを返す
1047  */
1048 static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
1049 {
1050         monster_type *m_ptr = &m_list[m_idx];
1051         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1052         POSITION     y = 0, ay, x = 0, ax;
1053         int          move_val = 0;
1054         POSITION     y2 = p_ptr->y;
1055         POSITION     x2 = p_ptr->x;
1056         bool         done = FALSE;
1057         bool         will_run = mon_will_run(m_idx);
1058         cave_type    *c_ptr;
1059         bool         no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (cave[m_ptr->fy][m_ptr->fx].cost > 2));
1060         bool         can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall));
1061
1062         /* Counter attack to an enemy monster */
1063         if (!will_run && m_ptr->target_y)
1064         {
1065                 int t_m_idx = cave[m_ptr->target_y][m_ptr->target_x].m_idx;
1066
1067                 /* The monster must be an enemy, and in LOS */
1068                 if (t_m_idx &&
1069                     are_enemies(m_ptr, &m_list[t_m_idx]) &&
1070                     los(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
1071                     projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1072                 {
1073                         /* Extract the "pseudo-direction" */
1074                         y = m_ptr->fy - m_ptr->target_y;
1075                         x = m_ptr->fx - m_ptr->target_x;
1076                         done = TRUE;
1077                 }
1078         }
1079
1080         if (!done && !will_run && is_hostile(m_ptr) &&
1081             (r_ptr->flags1 & RF1_FRIENDS) &&
1082             ((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)) ||
1083             (cave[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
1084         {
1085         /*
1086          * Animal packs try to get the player out of corridors
1087          * (...unless they can move through walls -- TY)
1088          */
1089                 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
1090                          !(r_ptr->flags2 & RF2_KILL_WALL))
1091                 {
1092                         int i, room = 0;
1093
1094                         /* Count room grids next to player */
1095                         for (i = 0; i < 8; i++)
1096                         {
1097                                 int xx = p_ptr->x + ddx_ddd[i];
1098                                 int yy = p_ptr->y + ddy_ddd[i];
1099
1100                                 if (!in_bounds2(yy, xx)) continue;
1101
1102                                 c_ptr = &cave[yy][xx];
1103
1104                                 /* Check grid */
1105                                 if (monster_can_cross_terrain(c_ptr->feat, r_ptr, 0))
1106                                 {
1107                                         /* One more room grid */
1108                                         room++;
1109                                 }
1110                         }
1111                         if (cave[p_ptr->y][p_ptr->x].info & CAVE_ROOM) room -= 2;
1112                         if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
1113
1114                         /* Not in a room and strong player */
1115                         if (room < (8 * (p_ptr->chp + p_ptr->csp)) /
1116                             (p_ptr->mhp + p_ptr->msp))
1117                         {
1118                                 /* Find hiding place */
1119                                 if (find_hiding(m_idx, &y, &x)) done = TRUE;
1120                         }
1121                 }
1122
1123                 /* Monster groups try to surround the player */
1124                 if (!done && (cave[m_ptr->fy][m_ptr->fx].dist < 3))
1125                 {
1126                         int i;
1127
1128                         /* Find an empty square near the player to fill */
1129                         for (i = 0; i < 8; i++)
1130                         {
1131                                 /* Pick squares near player (semi-randomly) */
1132                                 y2 = p_ptr->y + ddy_ddd[(m_idx + i) & 7];
1133                                 x2 = p_ptr->x + ddx_ddd[(m_idx + i) & 7];
1134
1135                                 /* Already there? */
1136                                 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
1137                                 {
1138                                         /* Attack the player */
1139                                         y2 = p_ptr->y;
1140                                         x2 = p_ptr->x;
1141
1142                                         break;
1143                                 }
1144
1145                                 if (!in_bounds2(y2, x2)) continue;
1146
1147                                 /* Ignore filled grids */
1148                                 if (!monster_can_enter(y2, x2, r_ptr, 0)) continue;
1149
1150                                 /* Try to fill this hole */
1151                                 break;
1152                         }
1153
1154                         /* Extract the new "pseudo-direction" */
1155                         y = m_ptr->fy - y2;
1156                         x = m_ptr->fx - x2;
1157
1158                         done = TRUE;
1159                 }
1160         }
1161
1162         if (!done)
1163         {
1164                 /* Flow towards the player */
1165                 (void)get_moves_aux(m_idx, &y2, &x2, no_flow);
1166
1167                 /* Extract the "pseudo-direction" */
1168                 y = m_ptr->fy - y2;
1169                 x = m_ptr->fx - x2;
1170
1171                 /* Not done */
1172         }
1173
1174         /* Apply fear if possible and necessary */
1175         if (is_pet(m_ptr) && will_run)
1176         {
1177                 /* XXX XXX Not very "smart" */
1178                 y = (-y), x = (-x);
1179         }
1180         else
1181         {
1182                 if (!done && will_run)
1183                 {
1184                         int tmp_x = (-x);
1185                         int tmp_y = (-y);
1186
1187                         /* Try to find safe place */
1188                         if (find_safety(m_idx, &y, &x))
1189                         {
1190                                 /* Attempt to avoid the player */
1191                                 if (!no_flow)
1192                                 {
1193                                         /* Adjust movement */
1194                                         if (get_fear_moves_aux(m_idx, &y, &x)) done = TRUE;
1195                                 }
1196                         }
1197
1198                         if (!done)
1199                         {
1200                                 /* This is not a very "smart" method XXX XXX */
1201                                 y = tmp_y;
1202                                 x = tmp_x;
1203                         }
1204                 }
1205         }
1206
1207
1208         /* Check for no move */
1209         if (!x && !y) return (FALSE);
1210
1211
1212         /* Extract the "absolute distances" */
1213         ax = ABS(x);
1214         ay = ABS(y);
1215
1216         /* Do something weird */
1217         if (y < 0) move_val += 8;
1218         if (x > 0) move_val += 4;
1219
1220         /* Prevent the diamond maneuvre */
1221         if (ay > (ax << 1)) move_val += 2;
1222         else if (ax > (ay << 1)) move_val++;
1223
1224         /* Extract some directions */
1225         switch (move_val)
1226         {
1227         case 0:
1228                 mm[0] = 9;
1229                 if (ay > ax)
1230                 {
1231                         mm[1] = 8;
1232                         mm[2] = 6;
1233                         mm[3] = 7;
1234                         mm[4] = 3;
1235                 }
1236                 else
1237                 {
1238                         mm[1] = 6;
1239                         mm[2] = 8;
1240                         mm[3] = 3;
1241                         mm[4] = 7;
1242                 }
1243                 break;
1244         case 1:
1245         case 9:
1246                 mm[0] = 6;
1247                 if (y < 0)
1248                 {
1249                         mm[1] = 3;
1250                         mm[2] = 9;
1251                         mm[3] = 2;
1252                         mm[4] = 8;
1253                 }
1254                 else
1255                 {
1256                         mm[1] = 9;
1257                         mm[2] = 3;
1258                         mm[3] = 8;
1259                         mm[4] = 2;
1260                 }
1261                 break;
1262         case 2:
1263         case 6:
1264                 mm[0] = 8;
1265                 if (x < 0)
1266                 {
1267                         mm[1] = 9;
1268                         mm[2] = 7;
1269                         mm[3] = 6;
1270                         mm[4] = 4;
1271                 }
1272                 else
1273                 {
1274                         mm[1] = 7;
1275                         mm[2] = 9;
1276                         mm[3] = 4;
1277                         mm[4] = 6;
1278                 }
1279                 break;
1280         case 4:
1281                 mm[0] = 7;
1282                 if (ay > ax)
1283                 {
1284                         mm[1] = 8;
1285                         mm[2] = 4;
1286                         mm[3] = 9;
1287                         mm[4] = 1;
1288                 }
1289                 else
1290                 {
1291                         mm[1] = 4;
1292                         mm[2] = 8;
1293                         mm[3] = 1;
1294                         mm[4] = 9;
1295                 }
1296                 break;
1297         case 5:
1298         case 13:
1299                 mm[0] = 4;
1300                 if (y < 0)
1301                 {
1302                         mm[1] = 1;
1303                         mm[2] = 7;
1304                         mm[3] = 2;
1305                         mm[4] = 8;
1306                 }
1307                 else
1308                 {
1309                         mm[1] = 7;
1310                         mm[2] = 1;
1311                         mm[3] = 8;
1312                         mm[4] = 2;
1313                 }
1314                 break;
1315         case 8:
1316                 mm[0] = 3;
1317                 if (ay > ax)
1318                 {
1319                         mm[1] = 2;
1320                         mm[2] = 6;
1321                         mm[3] = 1;
1322                         mm[4] = 9;
1323                 }
1324                 else
1325                 {
1326                         mm[1] = 6;
1327                         mm[2] = 2;
1328                         mm[3] = 9;
1329                         mm[4] = 1;
1330                 }
1331                 break;
1332         case 10:
1333         case 14:
1334                 mm[0] = 2;
1335                 if (x < 0)
1336                 {
1337                         mm[1] = 3;
1338                         mm[2] = 1;
1339                         mm[3] = 6;
1340                         mm[4] = 4;
1341                 }
1342                 else
1343                 {
1344                         mm[1] = 1;
1345                         mm[2] = 3;
1346                         mm[3] = 4;
1347                         mm[4] = 6;
1348                 }
1349                 break;
1350         case 12:
1351                 mm[0] = 1;
1352                 if (ay > ax)
1353                 {
1354                         mm[1] = 2;
1355                         mm[2] = 4;
1356                         mm[3] = 3;
1357                         mm[4] = 7;
1358                 }
1359                 else
1360                 {
1361                         mm[1] = 4;
1362                         mm[2] = 2;
1363                         mm[3] = 7;
1364                         mm[4] = 3;
1365                 }
1366                 break;
1367         }
1368
1369         /* Wants to move... */
1370         return (TRUE);
1371 }
1372
1373
1374 /*!
1375  * @brief モンスターから敵モンスターへの命中判定
1376  * @param power 打撃属性による基本命中値
1377  * @param level 攻撃側モンスターのレベル
1378  * @param ac 目標モンスターのAC
1379  * @param stun 攻撃側モンスターが朦朧状態ならTRUEを返す
1380  * @return 命中ならばTRUEを返す
1381  */
1382 static int check_hit2(int power, DEPTH level, ARMOUR_CLASS ac, int stun)
1383 {
1384         int i, k;
1385
1386         /* Percentile dice */
1387         k = randint0(100);
1388
1389         if (stun && one_in_(2)) return FALSE;
1390
1391         /* Hack -- Always miss or hit */
1392         if (k < 10) return (k < 5);
1393
1394         /* Calculate the "attack quality" */
1395         i = (power + (level * 3));
1396
1397         /* Power and Level compete against Armor */
1398         if ((i > 0) && (randint1(i) > ((ac * 3) / 4))) return (TRUE);
1399
1400         /* Assume miss */
1401         return (FALSE);
1402 }
1403
1404
1405 #define BLOW_EFFECT_TYPE_NONE  0
1406 #define BLOW_EFFECT_TYPE_FEAR  1
1407 #define BLOW_EFFECT_TYPE_SLEEP 2
1408 #define BLOW_EFFECT_TYPE_HEAL  3
1409
1410
1411 /*!
1412  * @brief モンスターから敵モンスターへの打撃攻撃処理
1413  * @param m_idx 攻撃側モンスターの参照ID
1414  * @param t_idx 目標側モンスターの参照ID
1415  * @return 実際に打撃処理が行われた場合TRUEを返す
1416  */
1417 static bool monst_attack_monst(MONSTER_IDX m_idx, MONSTER_IDX t_idx)
1418 {
1419         monster_type    *m_ptr = &m_list[m_idx];
1420         monster_type    *t_ptr = &m_list[t_idx];
1421
1422         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
1423         monster_race    *tr_ptr = &r_info[t_ptr->r_idx];
1424
1425         ARMOUR_CLASS ap_cnt;
1426         ARMOUR_CLASS ac;
1427         DEPTH rlev;
1428         int pt;
1429         GAME_TEXT m_name[MAX_NLEN], t_name[MAX_NLEN];
1430         char            temp[MAX_NLEN];
1431         bool            blinked;
1432         bool            explode = FALSE, touched = FALSE, fear = FALSE, dead = FALSE;
1433         POSITION y_saver = t_ptr->fy;
1434         POSITION x_saver = t_ptr->fx;
1435         int             effect_type;
1436
1437         bool see_m = is_seen(m_ptr);
1438         bool see_t = is_seen(t_ptr);
1439         bool see_either = see_m || see_t;
1440
1441         /* Can the player be aware of this attack? */
1442         bool known = (m_ptr->cdis <= MAX_SIGHT) || (t_ptr->cdis <= MAX_SIGHT);
1443         bool do_silly_attack = (one_in_(2) && p_ptr->image);
1444
1445         /* Cannot attack self */
1446         if (m_idx == t_idx) return FALSE;
1447
1448         /* Not allowed to attack */
1449         if (r_ptr->flags1 & RF1_NEVER_BLOW) return FALSE;
1450
1451         if (d_info[dungeon_type].flags1 & DF1_NO_MELEE) return (FALSE);
1452
1453         /* Total armor */
1454         ac = tr_ptr->ac;
1455
1456         /* Extract the effective monster level */
1457         rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1458
1459         monster_desc(m_name, m_ptr, 0);
1460         monster_desc(t_name, t_ptr, 0);
1461
1462         /* Assume no blink */
1463         blinked = FALSE;
1464
1465         if (!see_either && known)
1466         {
1467                 mon_fight = TRUE;
1468         }
1469
1470         if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
1471
1472         /* Scan through all four blows */
1473         for (ap_cnt = 0; ap_cnt < 4; ap_cnt++)
1474         {
1475                 bool obvious = FALSE;
1476
1477                 HIT_POINT power = 0;
1478                 HIT_POINT damage = 0;
1479
1480                 cptr act = NULL;
1481
1482                 /* Extract the attack infomation */
1483                 int effect = r_ptr->blow[ap_cnt].effect;
1484                 int method = r_ptr->blow[ap_cnt].method;
1485                 int d_dice = r_ptr->blow[ap_cnt].d_dice;
1486                 int d_side = r_ptr->blow[ap_cnt].d_side;
1487
1488                 if (!m_ptr->r_idx) break;
1489
1490                 /* Stop attacking if the target dies! */
1491                 if (t_ptr->fx != x_saver || t_ptr->fy != y_saver)
1492                         break;
1493
1494                 /* Hack -- no more attacks */
1495                 if (!method) break;
1496
1497                 if (method == RBM_SHOOT) continue;
1498
1499                 /* Extract the attack "power" */
1500                 power = mbe_info[effect].power;
1501
1502                 /* Monster hits */
1503                 if (!effect || check_hit2(power, rlev, ac, MON_STUNNED(m_ptr)))
1504                 {
1505                         (void)set_monster_csleep(t_idx, 0);
1506
1507                         if (t_ptr->ml)
1508                         {
1509                                 /* Redraw the health bar */
1510                                 if (p_ptr->health_who == t_idx) p_ptr->redraw |= (PR_HEALTH);
1511                                 if (p_ptr->riding == t_idx) p_ptr->redraw |= (PR_UHEALTH);
1512                         }
1513
1514                         /* Describe the attack method */
1515                         switch (method)
1516                         {
1517                         case RBM_HIT:
1518                                 {
1519                                         act = _("%sを殴った。", "hits %s.");
1520                                         touched = TRUE;
1521                                         break;
1522                                 }
1523
1524                         case RBM_TOUCH:
1525                                 {
1526                                         act = _("%sを触った。", "touches %s.");
1527                                         touched = TRUE;
1528                                         break;
1529                                 }
1530
1531                         case RBM_PUNCH:
1532                                 {
1533                                         act = _("%sをパンチした。", "punches %s.");
1534                                         touched = TRUE;
1535                                         break;
1536                                 }
1537
1538                         case RBM_KICK:
1539                                 {
1540                                         act = _("%sを蹴った。", "kicks %s.");
1541                                         touched = TRUE;
1542                                         break;
1543                                 }
1544
1545                         case RBM_CLAW:
1546                                 {
1547                                         act = _("%sをひっかいた。", "claws %s.");
1548                                         touched = TRUE;
1549                                         break;
1550                                 }
1551
1552                         case RBM_BITE:
1553                                 {
1554                                         act = _("%sを噛んだ。", "bites %s.");
1555                                         touched = TRUE;
1556                                         break;
1557                                 }
1558
1559                         case RBM_STING:
1560                                 {
1561                                         act = _("%sを刺した。", "stings %s.");
1562                                         touched = TRUE;
1563                                         break;
1564                                 }
1565
1566                         case RBM_SLASH:
1567                                 {
1568                                         act = _("%sを斬った。", "slashes %s.");
1569                                         break;
1570                                 }
1571
1572                         case RBM_BUTT:
1573                                 {
1574                                         act = _("%sを角で突いた。", "butts %s.");
1575                                         touched = TRUE;
1576                                         break;
1577                                 }
1578
1579                         case RBM_CRUSH:
1580                                 {
1581                                         act = _("%sに体当りした。", "crushes %s.");
1582                                         touched = TRUE;
1583                                         break;
1584                                 }
1585
1586                         case RBM_ENGULF:
1587                                 {
1588                                         act = _("%sを飲み込んだ。", "engulfs %s.");
1589                                         touched = TRUE;
1590                                         break;
1591                                 }
1592
1593                         case RBM_CHARGE:
1594                                 {
1595                                         act = _("%sに請求書をよこした。", "charges %s.");
1596                                         touched = TRUE;
1597                                         break;
1598                                 }
1599
1600                         case RBM_CRAWL:
1601                                 {
1602                                         act = _("%sの体の上を這い回った。", "crawls on %s.");
1603                                         touched = TRUE;
1604                                         break;
1605                                 }
1606
1607                         case RBM_DROOL:
1608                                 {
1609                                         act = _("%sによだれをたらした。", "drools on %s.");
1610                                         touched = FALSE;
1611                                         break;
1612                                 }
1613
1614                         case RBM_SPIT:
1615                                 {
1616                                         act = _("%sに唾を吐いた。", "spits on %s.");
1617                                         touched = FALSE;
1618                                         break;
1619                                 }
1620
1621                         case RBM_EXPLODE:
1622                                 {
1623                                         if (see_either) disturb(TRUE, TRUE);
1624                                         act = _("爆発した。", "explodes.");
1625                                         explode = TRUE;
1626                                         touched = FALSE;
1627                                         break;
1628                                 }
1629
1630                         case RBM_GAZE:
1631                                 {
1632                                         act = _("%sをにらんだ。", "gazes at %s.");
1633                                         touched = FALSE;
1634                                         break;
1635                                 }
1636
1637                         case RBM_WAIL:
1638                                 {
1639                                         act = _("%sに泣きついた。", "wails at %s.");
1640                                         touched = FALSE;
1641                                         break;
1642                                 }
1643
1644                         case RBM_SPORE:
1645                                 {
1646                                         act = _("%sに胞子を飛ばした。", "releases spores at %s.");
1647                                         touched = FALSE;
1648                                         break;
1649                                 }
1650
1651                         case RBM_XXX4:
1652                                 {
1653                                         act = _("%sにXXX4を飛ばした。", "projects XXX4's at %s.");
1654                                         touched = FALSE;
1655                                         break;
1656                                 }
1657
1658                         case RBM_BEG:
1659                                 {
1660                                         act = _("%sに金をせがんだ。", "begs %s for money.");
1661                                         touched = FALSE;
1662                                         break;
1663                                 }
1664
1665                         case RBM_INSULT:
1666                                 {
1667                                         act = _("%sを侮辱した。", "insults %s.");
1668                                         touched = FALSE;
1669                                         break;
1670                                 }
1671
1672                         case RBM_MOAN:
1673                                 {
1674                                         act = _("%sにむかってうめいた。", "moans at %s.");
1675                                         touched = FALSE;
1676                                         break;
1677                                 }
1678
1679                         case RBM_SHOW:
1680                                 {
1681                                         act = _("%sにむかって歌った。", "sings to %s.");
1682                                         touched = FALSE;
1683                                         break;
1684                                 }
1685                         }
1686
1687                         if (act && see_either)
1688                         {
1689 #ifdef JP
1690                                 if (do_silly_attack) act = silly_attacks2[randint0(MAX_SILLY_ATTACK)];
1691                                 strfmt(temp, act, t_name);
1692                                 msg_format("%^sは%s", m_name, temp);
1693 #else
1694                                 if (do_silly_attack)
1695                                 {
1696                                         act = silly_attacks[randint0(MAX_SILLY_ATTACK)];
1697                                         strfmt(temp, "%s %s.", act, t_name);
1698                                 }
1699                                 else strfmt(temp, act, t_name);
1700                                 msg_format("%^s %s", m_name, temp);
1701 #endif
1702                         }
1703
1704                         /* Hack -- assume all attacks are obvious */
1705                         obvious = TRUE;
1706
1707                         /* Roll out the damage */
1708                         damage = damroll(d_dice, d_side);
1709
1710                         /* Assume no effect */
1711                         effect_type = BLOW_EFFECT_TYPE_NONE;
1712
1713                         pt = GF_MISSILE;
1714
1715                         /* Apply appropriate damage */
1716                         switch (effect)
1717                         {
1718                         case 0:
1719                         case RBE_DR_MANA:
1720                                 damage = pt = 0;
1721                                 break;
1722
1723                         case RBE_SUPERHURT:
1724                                 if ((randint1(rlev*2+250) > (ac+200)) || one_in_(13))
1725                                 {
1726                                         int tmp_damage = damage - (damage * ((ac < 150) ? ac : 150) / 250);
1727                                         damage = MAX(damage, tmp_damage * 2);
1728                                         break;
1729                                 }
1730
1731                                 /* Fall through */
1732
1733                         case RBE_HURT:
1734                                 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1735                                 break;
1736
1737                         case RBE_POISON:
1738                         case RBE_DISEASE:
1739                                 pt = GF_POIS;
1740                                 break;
1741
1742                         case RBE_UN_BONUS:
1743                         case RBE_UN_POWER:
1744                                 pt = GF_DISENCHANT;
1745                                 break;
1746
1747                         case RBE_EAT_ITEM:
1748                         case RBE_EAT_GOLD:
1749                                 if ((p_ptr->riding != m_idx) && one_in_(2)) blinked = TRUE;
1750                                 break;
1751
1752                         case RBE_EAT_FOOD:
1753                         case RBE_EAT_LITE:
1754                         case RBE_BLIND:
1755                         case RBE_LOSE_STR:
1756                         case RBE_LOSE_INT:
1757                         case RBE_LOSE_WIS:
1758                         case RBE_LOSE_DEX:
1759                         case RBE_LOSE_CON:
1760                         case RBE_LOSE_CHR:
1761                         case RBE_LOSE_ALL:
1762                                 break;
1763
1764                         case RBE_ACID:
1765                                 pt = GF_ACID;
1766                                 break;
1767
1768                         case RBE_ELEC:
1769                                 pt = GF_ELEC;
1770                                 break;
1771
1772                         case RBE_FIRE:
1773                                 pt = GF_FIRE;
1774                                 break;
1775
1776                         case RBE_COLD:
1777                                 pt = GF_COLD;
1778                                 break;
1779
1780                         case RBE_CONFUSE:
1781                                 pt = GF_CONFUSION;
1782                                 break;
1783
1784                         case RBE_TERRIFY:
1785                                 effect_type = BLOW_EFFECT_TYPE_FEAR;
1786                                 break;
1787
1788                         case RBE_PARALYZE:
1789                                 effect_type = BLOW_EFFECT_TYPE_SLEEP;
1790                                 break;
1791
1792                         case RBE_SHATTER:
1793                                 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1794                                 if (damage > 23) earthquake_aux(m_ptr->fy, m_ptr->fx, 8, m_idx);
1795                                 break;
1796
1797                         case RBE_EXP_10:
1798                         case RBE_EXP_20:
1799                         case RBE_EXP_40:
1800                         case RBE_EXP_80:
1801                                 pt = GF_NETHER;
1802                                 break;
1803
1804                         case RBE_TIME:
1805                                 pt = GF_TIME;
1806                                 break;
1807
1808                         case RBE_DR_LIFE:
1809                                 pt = GF_HYPODYNAMIA;
1810                                 effect_type = BLOW_EFFECT_TYPE_HEAL;
1811                                 break;
1812
1813                         case RBE_INERTIA:
1814                                 pt = GF_INERTIAL;
1815                                 break;
1816
1817                         case RBE_STUN:
1818                                 pt = GF_SOUND;
1819                                 break;
1820
1821                         default:
1822                                 pt = 0;
1823                                 break;
1824                         }
1825
1826                         if (pt)
1827                         {
1828                                 /* Do damage if not exploding */
1829                                 if (!explode)
1830                                 {
1831                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1832                                                 damage, pt, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1833                                 }
1834
1835                                 switch (effect_type)
1836                                 {
1837                                 case BLOW_EFFECT_TYPE_FEAR:
1838                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1839                                                 damage, GF_TURN_ALL, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1840                                         break;
1841
1842                                 case BLOW_EFFECT_TYPE_SLEEP:
1843                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1844                                                 r_ptr->level, GF_OLD_SLEEP, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1845                                         break;
1846
1847                                 case BLOW_EFFECT_TYPE_HEAL:
1848                                         if ((monster_living(m_idx)) && (damage > 2))
1849                                         {
1850                                                 bool did_heal = FALSE;
1851
1852                                                 if (m_ptr->hp < m_ptr->maxhp) did_heal = TRUE;
1853
1854                                                 /* Heal */
1855                                                 m_ptr->hp += damroll(4, damage / 6);
1856                                                 if (m_ptr->hp > m_ptr->maxhp) m_ptr->hp = m_ptr->maxhp;
1857
1858                                                 /* Redraw (later) if needed */
1859                                                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
1860                                                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
1861
1862                                                 /* Special message */
1863                                                 if (see_m && did_heal)
1864                                                 {
1865                                                         msg_format(_("%sは体力を回復したようだ。", "%^s appears healthier."), m_name);
1866                                                 }
1867                                         }
1868                                         break;
1869                                 }
1870
1871                                 if (touched)
1872                                 {
1873                                         /* Aura fire */
1874                                         if ((tr_ptr->flags2 & RF2_AURA_FIRE) && m_ptr->r_idx)
1875                                         {
1876                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK))
1877                                                 {
1878                                                         if (see_either)
1879                                                         {
1880                                                                 msg_format(_("%^sは突然熱くなった!", "%^s is suddenly very hot!"), m_name);
1881                                                         }
1882                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_FIRE;
1883                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1884                                                                 damroll (1 + ((tr_ptr->level) / 26),
1885                                                                 1 + ((tr_ptr->level) / 17)),
1886                                                                 GF_FIRE, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1887                                                 }
1888                                                 else
1889                                                 {
1890                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK);
1891                                                 }
1892                                         }
1893
1894                                         /* Aura cold */
1895                                         if ((tr_ptr->flags3 & RF3_AURA_COLD) && m_ptr->r_idx)
1896                                         {
1897                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK))
1898                                                 {
1899                                                         if (see_either)
1900                                                         {
1901                                                                 msg_format(_("%^sは突然寒くなった!", "%^s is suddenly very cold!"), m_name);
1902                                                         }
1903                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags3 |= RF3_AURA_COLD;
1904                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1905                                                                 damroll (1 + ((tr_ptr->level) / 26),
1906                                                                 1 + ((tr_ptr->level) / 17)),
1907                                                                 GF_COLD, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1908                                                 }
1909                                                 else
1910                                                 {
1911                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_COLD_MASK);
1912                                                 }
1913                                         }
1914
1915                                         /* Aura elec */
1916                                         if ((tr_ptr->flags2 & RF2_AURA_ELEC) && m_ptr->r_idx)
1917                                         {
1918                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK))
1919                                                 {
1920                                                         if (see_either)
1921                                                         {
1922                                                                 msg_format(_("%^sは電撃を食らった!", "%^s gets zapped!"), m_name);
1923                                                         }
1924                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_ELEC;
1925                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1926                                                                 damroll (1 + ((tr_ptr->level) / 26),
1927                                                                 1 + ((tr_ptr->level) / 17)),
1928                                                                 GF_ELEC, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1929                                                 }
1930                                                 else
1931                                                 {
1932                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK);
1933                                                 }
1934                                         }
1935                                 }
1936                         }
1937                 }
1938
1939                 /* Monster missed player */
1940                 else
1941                 {
1942                         /* Analyze failed attacks */
1943                         switch (method)
1944                         {
1945                         case RBM_HIT:
1946                         case RBM_TOUCH:
1947                         case RBM_PUNCH:
1948                         case RBM_KICK:
1949                         case RBM_CLAW:
1950                         case RBM_BITE:
1951                         case RBM_STING:
1952                         case RBM_SLASH:
1953                         case RBM_BUTT:
1954                         case RBM_CRUSH:
1955                         case RBM_ENGULF:
1956                         case RBM_CHARGE:
1957                                 {
1958                                         (void)set_monster_csleep(t_idx, 0);
1959
1960                                         /* Visible monsters */
1961                                         if (see_m)
1962                                         {
1963 #ifdef JP
1964                                                 msg_format("%sは%^sの攻撃をかわした。", t_name, m_name);
1965 #else
1966                                                 msg_format("%^s misses %s.", m_name, t_name);
1967 #endif
1968                                         }
1969
1970                                         break;
1971                                 }
1972                         }
1973                 }
1974
1975
1976                 /* Analyze "visible" monsters only */
1977                 if (is_original_ap_and_seen(m_ptr) && !do_silly_attack)
1978                 {
1979                         /* Count "obvious" attacks (and ones that cause damage) */
1980                         if (obvious || damage || (r_ptr->r_blows[ap_cnt] > 10))
1981                         {
1982                                 /* Count attacks of this type */
1983                                 if (r_ptr->r_blows[ap_cnt] < MAX_UCHAR)
1984                                 {
1985                                         r_ptr->r_blows[ap_cnt]++;
1986                                 }
1987                         }
1988                 }
1989         }
1990
1991         if (explode)
1992         {
1993                 sound(SOUND_EXPLODE);
1994
1995                 /* Cancel Invulnerability */
1996                 (void)set_monster_invulner(m_idx, 0, FALSE);
1997                 mon_take_hit_mon(m_idx, m_ptr->hp + 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1998                 blinked = FALSE;
1999         }
2000
2001         /* Blink away */
2002         if (blinked && m_ptr->r_idx)
2003         {
2004                 if (teleport_barrier(m_idx))
2005                 {
2006                         if (see_m)
2007                         {
2008                                 msg_print(_("泥棒は笑って逃げ...ようとしたがバリアに防がれた。", "The thief flees laughing...? But magic barrier obstructs it."));
2009                         }
2010                         else if (known)
2011                         {
2012                                 mon_fight = TRUE;
2013                         }
2014                 }
2015                 else
2016                 {
2017                         if (see_m)
2018                         {
2019                                 msg_print(_("泥棒は笑って逃げた!", "The thief flees laughing!"));
2020                         }
2021                         else if (known)
2022                         {
2023                                 mon_fight = TRUE;
2024                         }
2025
2026                         teleport_away(m_idx, MAX_SIGHT * 2 + 5, 0L);
2027                 }
2028         }
2029
2030         return TRUE;
2031 }
2032
2033
2034 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
2035 {
2036         return !have_flag(f_ptr->flags, FF_GLASS) ||
2037                 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
2038                 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
2039 }
2040
2041
2042 /*!
2043  * @brief モンスター単体の1ターン行動処理メインルーチン /
2044  * Process a monster
2045  * @param m_idx 行動モンスターの参照ID
2046  * @return なし
2047  * @details
2048  * The monster is known to be within 100 grids of the player\n
2049  *\n
2050  * In several cases, we directly update the monster lore\n
2051  *\n
2052  * Note that a monster is only allowed to "reproduce" if there\n
2053  * are a limited number of "reproducing" monsters on the current\n
2054  * level.  This should prevent the level from being "swamped" by\n
2055  * reproducing monsters.  It also allows a large mass of mice to\n
2056  * prevent a louse from multiplying, but this is a small price to\n
2057  * pay for a simple multiplication method.\n
2058  *\n
2059  * XXX Monster fear is slightly odd, in particular, monsters will\n
2060  * fixate on opening a door even if they cannot open it.  Actually,\n
2061  * the same thing happens to normal monsters when they hit a door\n
2062  *\n
2063  * In addition, monsters which *cannot* open or bash\n
2064  * down a door will still stand there trying to open it...\n
2065  *\n
2066  * XXX Technically, need to check for monster in the way\n
2067  * combined with that monster being in a wall (or door?)\n
2068  *\n
2069  * A "direction" of "5" means "pick a random direction".\n
2070  */
2071 void process_monster(MONSTER_IDX m_idx)
2072 {
2073         monster_type    *m_ptr = &m_list[m_idx];
2074         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
2075         monster_race    *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2076
2077         int i, d;
2078         POSITION oy, ox, ny, nx;
2079
2080         DIRECTION mm[8];
2081
2082         cave_type       *c_ptr;
2083         feature_type    *f_ptr;
2084
2085         monster_type    *y_ptr;
2086
2087         bool            do_turn;
2088         bool            do_move;
2089         bool            do_view;
2090         bool            must_alter_to_move;
2091
2092         bool            did_open_door;
2093         bool            did_bash_door;
2094         bool            did_take_item;
2095         bool            did_kill_item;
2096         bool            did_move_body;
2097         bool            did_pass_wall;
2098         bool            did_kill_wall;
2099         bool            gets_angry = FALSE;
2100         bool            can_cross;
2101         bool            aware = TRUE;
2102
2103         bool fear, dead;
2104         bool is_riding_mon = (m_idx == p_ptr->riding);
2105         bool see_m = is_seen(m_ptr);
2106
2107         if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
2108         {
2109                 if (rakuba(0, TRUE))
2110                 {
2111 #ifdef JP
2112                         msg_print("地面に落とされた。");
2113 #else
2114                         GAME_TEXT m_name[MAX_NLEN];
2115                         monster_desc(m_name, &m_list[p_ptr->riding], 0);
2116                         msg_format("You have fallen from %s.", m_name);
2117 #endif
2118                 }
2119         }
2120
2121         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
2122         {
2123                 choose_new_monster(m_idx, FALSE, 0);
2124                 r_ptr = &r_info[m_ptr->r_idx];
2125         }
2126
2127         /* Players hidden in shadow are almost imperceptable. -LM- */
2128         if (p_ptr->special_defense & NINJA_S_STEALTH)
2129         {
2130                 int tmp = p_ptr->lev*6+(p_ptr->skill_stl+10)*4;
2131                 if (p_ptr->monlite) tmp /= 3;
2132                 if (p_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
2133                 if (r_ptr->level > (p_ptr->lev * p_ptr->lev / 20 + 10)) tmp /= 3;
2134                 /* Low-level monsters will find it difficult to locate the player. */
2135                 if (randint0(tmp) > (r_ptr->level+20)) aware = FALSE;
2136         }
2137
2138         /* Are there its parent? */
2139         if (m_ptr->parent_m_idx && !m_list[m_ptr->parent_m_idx].r_idx)
2140         {
2141                 /* Its parent have gone, it also goes away. */
2142
2143                 if (see_m)
2144                 {
2145                         GAME_TEXT m_name[MAX_NLEN];
2146                         monster_desc(m_name, m_ptr, 0);
2147                         msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2148                 }
2149
2150                 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
2151                 {
2152                         GAME_TEXT m_name[MAX_NLEN];
2153                         monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
2154                         do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
2155                 }
2156
2157                 delete_monster_idx(m_idx);
2158
2159                 return;
2160         }
2161
2162         /* Quantum monsters are odd */
2163         if (r_ptr->flags2 & (RF2_QUANTUM))
2164         {
2165                 /* Sometimes skip move */
2166                 if (!randint0(2)) return;
2167
2168                 /* Sometimes die */
2169                 if (!randint0((m_idx % 100) + 10) && !(r_ptr->flags1 & RF1_QUESTOR))
2170                 {
2171                         bool sad = FALSE;
2172
2173                         if (is_pet(m_ptr) && !(m_ptr->ml)) sad = TRUE;
2174
2175                         if (see_m)
2176                         {
2177                                 GAME_TEXT m_name[MAX_NLEN];
2178                                 monster_desc(m_name, m_ptr, 0);
2179
2180                                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2181                         }
2182
2183                         /* Generate treasure, etc */
2184                         monster_death(m_idx, FALSE);
2185
2186                         delete_monster_idx(m_idx);
2187                         if (sad)
2188                         {
2189                                 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
2190                         }
2191
2192                         return;
2193                 }
2194         }
2195
2196         if (m_ptr->r_idx == MON_SHURYUUDAN)
2197         {
2198                 mon_take_hit_mon(m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
2199                 if(dead) return;
2200         }
2201
2202         if ((is_pet(m_ptr) || is_friendly(m_ptr)) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && !p_ptr->inside_battle)
2203         {
2204                 static int riding_pinch = 0;
2205
2206                 if (m_ptr->hp < m_ptr->maxhp/3)
2207                 {
2208                         GAME_TEXT m_name[MAX_NLEN];
2209                         monster_desc(m_name, m_ptr, 0);
2210
2211                         if (is_riding_mon && riding_pinch < 2)
2212                         {
2213                                 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
2214                                                          "%^s seems to be in so much pain, and trying to escape from your restriction."), m_name);
2215                                 riding_pinch++;
2216                                 disturb(TRUE, TRUE);
2217                         }
2218                         else
2219                         {
2220                                 if (is_riding_mon)
2221                                 {
2222                                         msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
2223                                         if (rakuba(-1, FALSE))
2224                                         {
2225                                                 msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
2226                                         }
2227                                 }
2228
2229                                 if (see_m)
2230                                 {
2231                                         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) &&
2232                                             player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x))
2233                                         {
2234                                                 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
2235                                         }
2236                                         msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s read a scroll of teleport level."), m_name);
2237                                         msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
2238                                 }
2239
2240                                 if (is_riding_mon && rakuba(-1, FALSE))
2241                                 {
2242                                         msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
2243                                 }
2244
2245                                 /* Check for quest completion */
2246                                 check_quest_completion(m_ptr);
2247
2248                                 delete_monster_idx(m_idx);
2249
2250                                 return;
2251                         }
2252                 }
2253                 else
2254                 {
2255                         /* Reset the counter */
2256                         if (is_riding_mon) riding_pinch = 0;
2257                 }
2258         }
2259
2260         /* Handle "sleep" */
2261         if (MON_CSLEEP(m_ptr))
2262         {
2263                 /* Handle non-aggravation - Still sleeping */
2264                 if (!(p_ptr->cursed & TRC_AGGRAVATE)) return;
2265
2266                 /* Reset sleep counter */
2267                 (void)set_monster_csleep(m_idx, 0);
2268
2269                 /* Notice the "waking up" */
2270                 if (m_ptr->ml)
2271                 {
2272                         GAME_TEXT m_name[MAX_NLEN];
2273                         monster_desc(m_name, m_ptr, 0);
2274                         msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
2275                 }
2276
2277                 /* Hack -- Count the wakings */
2278                 if (is_original_ap_and_seen(m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
2279                 {
2280                         r_ptr->r_wake++;
2281                 }
2282         }
2283
2284         /* Handle "stun" */
2285         if (MON_STUNNED(m_ptr))
2286         {
2287                 /* Sometimes skip move */
2288                 if (one_in_(2)) return;
2289         }
2290
2291         if (is_riding_mon)
2292         {
2293                 p_ptr->update |= (PU_BONUS);
2294         }
2295
2296         /* No one wants to be your friend if you're aggravating */
2297         if (is_friendly(m_ptr) && (p_ptr->cursed & TRC_AGGRAVATE))
2298                 gets_angry = TRUE;
2299
2300         /* Paranoia... no pet uniques outside wizard mode -- TY */
2301         if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
2302               monster_has_hostile_align(NULL, 10, -10, r_ptr))
2303              || (r_ptr->flagsr & RFR_RES_ALL)))
2304         {
2305                 gets_angry = TRUE;
2306         }
2307
2308         if (p_ptr->inside_battle) gets_angry = FALSE;
2309
2310         if (gets_angry)
2311         {
2312                 if (is_pet(m_ptr) || see_m)
2313                 {
2314                         GAME_TEXT m_name[MAX_NLEN];
2315                         monster_desc(m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
2316                         msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
2317                 }
2318
2319                 set_hostile(m_ptr);
2320         }
2321
2322         /* Get the origin */
2323         oy = m_ptr->fy;
2324         ox = m_ptr->fx;
2325
2326         /* Attempt to "multiply" if able and allowed */
2327         if ((r_ptr->flags2 & RF2_MULTIPLY) && (num_repro < MAX_REPRO))
2328         {
2329                 int k;
2330                 POSITION y, x;
2331
2332                 /* Count the adjacent monsters */
2333                 for (k = 0, y = oy - 1; y <= oy + 1; y++)
2334                 {
2335                         for (x = ox - 1; x <= ox + 1; x++)
2336                         {
2337                                 /* Ignore locations off of edge */
2338                                 if (!in_bounds2(y, x)) continue;
2339                                 if (cave[y][x].m_idx) k++;
2340                         }
2341                 }
2342
2343                 /* Hex */
2344                 if (multiply_barrier(m_idx)) k = 8;
2345
2346                 /* Hack -- multiply slower in crowded areas */
2347                 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2348                 {
2349                         /* Try to multiply */
2350                         if (multiply_monster(m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
2351                         {
2352                                 /* Take note if visible */
2353                                 if (m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(m_ptr))
2354                                 {
2355                                         r_ptr->r_flags2 |= (RF2_MULTIPLY);
2356                                 }
2357
2358                                 /* Multiplying takes energy */
2359                                 return;
2360                         }
2361                 }
2362         }
2363
2364         if (r_ptr->a_ability_flags2 & RF6_SPECIAL)
2365         {
2366                 /* Hack -- Ohmu scatters molds! */
2367                 if (m_ptr->r_idx == MON_OHMU)
2368                 {
2369                         if (!p_ptr->inside_arena && !p_ptr->inside_battle)
2370                         {
2371                                 if (r_ptr->freq_spell && (randint1(100) <= r_ptr->freq_spell))
2372                                 {
2373                                         int k, count = 0;
2374                                         DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
2375                                         BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
2376
2377                                         for (k = 0; k < A_MAX; k++)
2378                                         {
2379                                                 if (summon_specific(m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode), '\0'))
2380                                                 {
2381                                                         if (m_list[hack_m_idx_ii].ml) count++;
2382                                                 }
2383                                         }
2384
2385                                         if (count && is_original_ap_and_seen(m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
2386                                 }
2387                         }
2388                 }
2389         }
2390
2391         if (!p_ptr->inside_battle)
2392         {
2393                 /* Hack! "Cyber" monster makes noise... */
2394                 if (m_ptr->ap_r_idx == MON_CYBER &&
2395                     one_in_(CYBERNOISE) &&
2396                     !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2397                 {
2398                         if (disturb_minor) disturb(FALSE, FALSE);
2399                         msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
2400                 }
2401
2402                 /* Some monsters can speak */
2403                 if ((ap_r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
2404                     one_in_(SPEAK_CHANCE) &&
2405                     player_has_los_bold(oy, ox) &&
2406                     projectable(oy, ox, p_ptr->y, p_ptr->x))
2407                 {
2408                         GAME_TEXT m_name[MAX_NLEN];
2409                         char monmessage[1024];
2410                         cptr filename;
2411
2412                         /* Acquire the monster name/poss */
2413                         if (m_ptr->ml)
2414                                 monster_desc(m_name, m_ptr, 0);
2415                         else
2416                                 strcpy(m_name, _("それ", "It"));
2417
2418                         /* Select the file for monster quotes */
2419                         if (MON_MONFEAR(m_ptr))
2420                                 filename = _("monfear_j.txt", "monfear.txt");
2421                         else if (is_pet(m_ptr))
2422                                 filename = _("monpet_j.txt", "monpet.txt");
2423                         else if (is_friendly(m_ptr))
2424                                 filename = _("monfrien_j.txt", "monfrien.txt");
2425                         else
2426                                 filename = _("monspeak_j.txt", "monspeak.txt");
2427                         /* Get the monster line */
2428                         if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
2429                         {
2430                                 /* Say something */
2431                                 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
2432                         }
2433                 }
2434         }
2435
2436         /* Try to cast spell occasionally */
2437         if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
2438         {
2439                 bool counterattack = FALSE;
2440
2441                 /* Give priority to counter attack? */
2442                 if (m_ptr->target_y)
2443                 {
2444                         MONSTER_IDX t_m_idx = cave[m_ptr->target_y][m_ptr->target_x].m_idx;
2445
2446                         /* The monster must be an enemy, and projectable */
2447                         if (t_m_idx && are_enemies(m_ptr, &m_list[t_m_idx]) &&
2448                             projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
2449                         {
2450                                 counterattack = TRUE;
2451                         }
2452                 }
2453
2454                 if (!counterattack)
2455                 {
2456                         /* Attempt to cast a spell */
2457                         if (aware && make_attack_spell(m_idx)) return;
2458
2459                         /*
2460                          * Attempt to cast a spell at an enemy other than the player
2461                          * (may slow the game a smidgeon, but I haven't noticed.)
2462                          */
2463                         if (monst_spell_monst(m_idx)) return;
2464                 }
2465                 else
2466                 {
2467                         /* Attempt to do counter attack at first */
2468                         if (monst_spell_monst(m_idx)) return;
2469
2470                         if (aware && make_attack_spell(m_idx)) return;
2471                 }
2472         }
2473
2474         /* Hack -- Assume no movement */
2475         mm[0] = mm[1] = mm[2] = mm[3] = 0;
2476         mm[4] = mm[5] = mm[6] = mm[7] = 0;
2477
2478
2479         /* Confused -- 100% random */
2480         if (MON_CONFUSED(m_ptr) || !aware)
2481         {
2482                 /* Try four "random" directions */
2483                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2484         }
2485
2486         /* 75% random movement */
2487         else if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) &&
2488                  (randint0(100) < 75))
2489         {
2490                 /* Memorize flags */
2491                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
2492
2493                 /* Try four "random" directions */
2494                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2495         }
2496
2497         /* 50% random movement */
2498         else if ((r_ptr->flags1 & RF1_RAND_50) &&
2499                                 (randint0(100) < 50))
2500         {
2501                 /* Memorize flags */
2502                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
2503
2504                 /* Try four "random" directions */
2505                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2506         }
2507
2508         /* 25% random movement */
2509         else if ((r_ptr->flags1 & RF1_RAND_25) &&
2510                                 (randint0(100) < 25))
2511         {
2512                 /* Memorize flags */
2513                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
2514
2515                 /* Try four "random" directions */
2516                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2517         }
2518
2519         /* Can't reach player - find something else to hit */
2520         else if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2521         {
2522                 /* Try four "random" directions */
2523                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2524
2525                 /* Look for an enemy */
2526 #if 0  /* Hack - Too slow.  Mimic pits are horrible with this on. */
2527                 get_enemy_dir(m_idx, mm);
2528 #endif /* 0 */
2529         }
2530
2531         /* Pets will follow the player */
2532         else if (is_pet(m_ptr))
2533         {
2534                 /* Are we trying to avoid the player? */
2535                 bool avoid = ((p_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
2536
2537                 /* Do we want to find the player? */
2538                 bool lonely = (!avoid && (m_ptr->cdis > p_ptr->pet_follow_distance));
2539
2540                 /* Should we find the player if we can't find a monster? */
2541                 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
2542
2543                 /* by default, move randomly */
2544                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2545
2546                 /* Look for an enemy */
2547                 if (!get_enemy_dir(m_idx, mm))
2548                 {
2549                         /* Find the player if necessary */
2550                         if (avoid || lonely || distant)
2551                         {
2552                                 /* Remember the leash length */
2553                                 POSITION dis = p_ptr->pet_follow_distance;
2554
2555                                 /* Hack -- adjust follow distance temporarily */
2556                                 if (p_ptr->pet_follow_distance > PET_SEEK_DIST)
2557                                 {
2558                                         p_ptr->pet_follow_distance = PET_SEEK_DIST;
2559                                 }
2560
2561                                 /* Find the player */
2562                                 (void)get_moves(m_idx, mm);
2563
2564                                 /* Restore the leash */
2565                                 p_ptr->pet_follow_distance = (s16b)dis;
2566                         }
2567                 }
2568         }
2569
2570         /* Friendly monster movement */
2571         else if (!is_hostile(m_ptr))
2572         {
2573                 /* by default, move randomly */
2574                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2575
2576                 /* Look for an enemy */
2577                 get_enemy_dir(m_idx, mm);
2578         }
2579         /* Normal movement */
2580         else
2581         {
2582                 /* Logical moves, may do nothing */
2583                 if (!get_moves(m_idx, mm)) return;
2584         }
2585
2586         /* Assume nothing */
2587         do_turn = FALSE;
2588         do_move = FALSE;
2589         do_view = FALSE;
2590         must_alter_to_move = FALSE;
2591
2592         /* Assume nothing */
2593         did_open_door = FALSE;
2594         did_bash_door = FALSE;
2595         did_take_item = FALSE;
2596         did_kill_item = FALSE;
2597         did_move_body = FALSE;
2598         did_pass_wall = FALSE;
2599         did_kill_wall = FALSE;
2600
2601         /* Take a zero-terminated array of "directions" */
2602         for (i = 0; mm[i]; i++)
2603         {
2604                 /* Get the direction */
2605                 d = mm[i];
2606
2607                 /* Hack -- allow "randomized" motion */
2608                 if (d == 5) d = ddd[randint0(8)];
2609
2610                 /* Get the destination */
2611                 ny = oy + ddy[d];
2612                 nx = ox + ddx[d];
2613
2614                 /* Ignore locations off of edge */
2615                 if (!in_bounds2(ny, nx)) continue;
2616
2617                 /* Access that cave grid */
2618                 c_ptr = &cave[ny][nx];
2619                 f_ptr = &f_info[c_ptr->feat];
2620                 can_cross = monster_can_cross_terrain(c_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
2621
2622                 /* Access that cave grid's contents */
2623                 y_ptr = &m_list[c_ptr->m_idx];
2624
2625                 /* Hack -- player 'in' wall */
2626                 if (player_bold(ny, nx))
2627                 {
2628                         do_move = TRUE;
2629                 }
2630
2631                 /* Possibly a monster to attack */
2632                 else if (c_ptr->m_idx)
2633                 {
2634                         do_move = TRUE;
2635                 }
2636
2637                 /* Monster destroys walls (and doors) */
2638                 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
2639                          (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
2640                          have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
2641                          check_hp_for_feat_destruction(f_ptr, m_ptr))
2642                 {
2643                         /* Eat through walls/doors/rubble */
2644                         do_move = TRUE;
2645                         if (!can_cross) must_alter_to_move = TRUE;
2646
2647                         /* Monster destroyed a wall (later) */
2648                         did_kill_wall = TRUE;
2649                 }
2650
2651                 /* Floor is open? */
2652                 else if (can_cross)
2653                 {
2654                         /* Go ahead and move */
2655                         do_move = TRUE;
2656
2657                         /* Monster moves through walls (and doors) */
2658                         if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || p_ptr->pass_wall) &&
2659                             have_flag(f_ptr->flags, FF_CAN_PASS))
2660                         {
2661                                 /* Monster went through a wall */
2662                                 did_pass_wall = TRUE;
2663                         }
2664                 }
2665
2666                 /* Handle doors and secret doors */
2667                 else if (is_closed_door(c_ptr->feat))
2668                 {
2669                         bool may_bash = TRUE;
2670
2671                         /* Assume no move allowed */
2672                         do_move = FALSE;
2673
2674                         /* Creature can open doors. */
2675                         if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
2676                                  (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2677                         {
2678                                 /* Closed doors */
2679                                 if (!f_ptr->power)
2680                                 {
2681                                         /* The door is open */
2682                                         did_open_door = TRUE;
2683
2684                                         /* Do not bash the door */
2685                                         may_bash = FALSE;
2686
2687                                         do_turn = TRUE;
2688                                 }
2689
2690                                 /* Locked doors (not jammed) */
2691                                 else
2692                                 {
2693                                         /* Try to unlock it */
2694                                         if (randint0(m_ptr->hp / 10) > f_ptr->power)
2695                                         {
2696                                                 /* Unlock the door */
2697                                                 cave_alter_feat(ny, nx, FF_DISARM);
2698
2699                                                 /* Do not bash the door */
2700                                                 may_bash = FALSE;
2701
2702                                                 do_turn = TRUE;
2703                                         }
2704                                 }
2705                         }
2706
2707                         /* Stuck doors -- attempt to bash them down if allowed */
2708                         if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
2709                                 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2710                         {
2711                                 /* Attempt to Bash */
2712                                 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
2713                                 {
2714                                         if (have_flag(f_ptr->flags, FF_GLASS))
2715                                                 msg_print(_("ガラスが砕ける音がした!", "You hear a glass was crashed!"));
2716                                         else
2717                                                 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
2718
2719                                         /* Disturb (sometimes) */
2720                                         if (disturb_minor) disturb(FALSE, FALSE);
2721
2722                                         /* The door was bashed open */
2723                                         did_bash_door = TRUE;
2724
2725                                         /* Hack -- fall into doorway */
2726                                         do_move = TRUE;
2727                                         must_alter_to_move = TRUE;
2728                                 }
2729                         }
2730
2731
2732                         /* Deal with doors in the way */
2733                         if (did_open_door || did_bash_door)
2734                         {
2735                                 /* Break down the door */
2736                                 if (did_bash_door && ((randint0(100) < 50) || (feat_state(c_ptr->feat, FF_OPEN) == c_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
2737                                 {
2738                                         cave_alter_feat(ny, nx, FF_BASH);
2739
2740                                         if (!m_ptr->r_idx) /* Killed by shards of glass, etc. */
2741                                         {
2742                                                 /* Update some things */
2743                                                 p_ptr->update |= (PU_FLOW);
2744                                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2745                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
2746
2747                                                 return;
2748                                         }
2749                                 }
2750
2751                                 /* Open the door */
2752                                 else
2753                                 {
2754                                         cave_alter_feat(ny, nx, FF_OPEN);
2755                                 }
2756
2757                                 f_ptr = &f_info[c_ptr->feat];
2758
2759                                 /* Handle viewable doors */
2760                                 do_view = TRUE;
2761                         }
2762                 }
2763
2764                 /* Hack -- check for Glyph of Warding */
2765                 if (do_move && is_glyph_grid(c_ptr) &&
2766                     !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
2767                 {
2768                         /* Assume no move allowed */
2769                         do_move = FALSE;
2770
2771                         /* Break the ward */
2772                         if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
2773                         {
2774                                 /* Describe observable breakage */
2775                                 if (c_ptr->info & CAVE_MARK)
2776                                 {
2777                                         msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
2778                                 }
2779
2780                                 /* Forget the rune */
2781                                 c_ptr->info &= ~(CAVE_MARK);
2782
2783                                 /* Break the rune */
2784                                 c_ptr->info &= ~(CAVE_OBJECT);
2785                                 c_ptr->mimic = 0;
2786
2787                                 /* Allow movement */
2788                                 do_move = TRUE;
2789
2790                                 note_spot(ny, nx);
2791                         }
2792                 }
2793                 else if (do_move && is_explosive_rune_grid(c_ptr) &&
2794                          !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
2795                 {
2796                         /* Assume no move allowed */
2797                         do_move = FALSE;
2798
2799                         /* Break the ward */
2800                         if (!is_pet(m_ptr))
2801                         {
2802                                 /* Break the ward */
2803                                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
2804                                 {
2805                                         /* Describe observable breakage */
2806                                         if (c_ptr->info & CAVE_MARK)
2807                                         {
2808                                                 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
2809                                                 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);
2810                                         }
2811                                 }
2812                                 else
2813                                 {
2814                                         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
2815                                 }
2816
2817                                 /* Forget the rune */
2818                                 c_ptr->info &= ~(CAVE_MARK);
2819
2820                                 /* Break the rune */
2821                                 c_ptr->info &= ~(CAVE_OBJECT);
2822                                 c_ptr->mimic = 0;
2823
2824                                 note_spot(ny, nx);
2825                                 lite_spot(ny, nx);
2826
2827                                 if (!m_ptr->r_idx) return;
2828                                 /* Allow movement */
2829                                 do_move = TRUE;
2830                         }
2831                 }
2832
2833                 /* The player is in the way */
2834                 if (do_move && player_bold(ny, nx))
2835                 {
2836                         /* Some monsters never attack */
2837                         if (r_ptr->flags1 & RF1_NEVER_BLOW)
2838                         {
2839                                 /* Hack -- memorize lack of attacks */
2840                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
2841
2842                                 /* Do not move */
2843                                 do_move = FALSE;
2844                         }
2845
2846                         /* In anti-melee dungeon, stupid or confused monster takes useless turn */
2847                         if (do_move && (d_info[dungeon_type].flags1 & DF1_NO_MELEE))
2848                         {
2849                                 if (!MON_CONFUSED(m_ptr))
2850                                 {
2851                                         if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
2852                                         else
2853                                         {
2854                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2855                                         }
2856                                 }
2857                         }
2858
2859                         /* The player is in the way.  Attack him. */
2860                         if (do_move)
2861                         {
2862                                 if (!p_ptr->riding || one_in_(2))
2863                                 {
2864                                         /* Do the attack */
2865                                         (void)make_attack_normal(m_idx);
2866
2867                                         /* Do not move */
2868                                         do_move = FALSE;
2869
2870                                         /* Took a turn */
2871                                         do_turn = TRUE;
2872                                 }
2873                         }
2874                 }
2875
2876                 /* A monster is in the way */
2877                 if (do_move && c_ptr->m_idx)
2878                 {
2879                         monster_race *z_ptr = &r_info[y_ptr->r_idx];
2880
2881                         /* Assume no movement */
2882                         do_move = FALSE;
2883
2884                         /* Attack 'enemies' */
2885                         if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
2886                                  (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
2887                                  can_cross && (c_ptr->m_idx != p_ptr->riding)) ||
2888                                 are_enemies(m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
2889                         {
2890                                 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
2891                                 {
2892                                         if (r_ptr->flags2 & RF2_KILL_BODY)
2893                                         {
2894                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
2895                                         }
2896
2897                                         /* attack */
2898                                         if (y_ptr->r_idx && (y_ptr->hp >= 0))
2899                                         {
2900                                                 if (monst_attack_monst(m_idx, c_ptr->m_idx)) return;
2901
2902                                                 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
2903                                                 else if (d_info[dungeon_type].flags1 & DF1_NO_MELEE)
2904                                                 {
2905                                                         if (MON_CONFUSED(m_ptr)) return;
2906                                                         else if (r_ptr->flags2 & RF2_STUPID)
2907                                                         {
2908                                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2909                                                                 return;
2910                                                         }
2911                                                 }
2912                                         }
2913                                 }
2914                         }
2915
2916                         /* Push past weaker monsters (unless leaving a wall) */
2917                         else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
2918                                 (r_ptr->mexp > z_ptr->mexp) &&
2919                                 can_cross && (c_ptr->m_idx != p_ptr->riding) &&
2920                                 monster_can_cross_terrain(cave[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
2921                         {
2922                                 /* Allow movement */
2923                                 do_move = TRUE;
2924
2925                                 /* Monster pushed past another monster */
2926                                 did_move_body = TRUE;
2927
2928                                 /* Wake up the moved monster */
2929                                 (void)set_monster_csleep(c_ptr->m_idx, 0);
2930
2931                                 /* Message */
2932                         }
2933                 }
2934
2935                 if (is_riding_mon)
2936                 {
2937                         if (!p_ptr->riding_ryoute && !MON_MONFEAR(&m_list[p_ptr->riding])) do_move = FALSE;
2938                 }
2939
2940                 if (did_kill_wall && do_move)
2941                 {
2942                         if (one_in_(GRINDNOISE))
2943                         {
2944                                 if (have_flag(f_ptr->flags, FF_GLASS))
2945                                         msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
2946                                 else
2947                                         msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
2948                         }
2949
2950                         cave_alter_feat(ny, nx, FF_HURT_DISI);
2951
2952                         if (!m_ptr->r_idx) /* Killed by shards of glass, etc. */
2953                         {
2954                                 /* Update some things */
2955                                 p_ptr->update |= (PU_FLOW);
2956                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2957                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2958
2959                                 return;
2960                         }
2961
2962                         f_ptr = &f_info[c_ptr->feat];
2963
2964                         /* Note changes to viewable region */
2965                         do_view = TRUE;
2966                         do_turn = TRUE;
2967                 }
2968
2969                 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
2970                 {
2971                         if (!monster_can_cross_terrain(c_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
2972                         {
2973                                 /* Assume no move allowed */
2974                                 do_move = FALSE;
2975                         }
2976                 }
2977
2978                 /*
2979                  * Check if monster can cross terrain
2980                  * This is checked after the normal attacks
2981                  * to allow monsters to attack an enemy,
2982                  * even if it can't enter the terrain.
2983                  */
2984                 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
2985                 {
2986                         /* Assume no move allowed */
2987                         do_move = FALSE;
2988                 }
2989
2990                 /* Some monsters never move */
2991                 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
2992                 {
2993                         /* Hack -- memorize lack of moves */
2994                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
2995
2996                         /* Do not move */
2997                         do_move = FALSE;
2998                 }
2999
3000                 /* Creature has been allowed move */
3001                 if (do_move)
3002                 {
3003                         do_turn = TRUE;
3004
3005                         if (have_flag(f_ptr->flags, FF_TREE))
3006                         {
3007                                 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
3008                                 {
3009                                         m_ptr->energy_need += ENERGY_NEED();
3010                                 }
3011                         }
3012
3013                         if (!is_riding_mon)
3014                         {
3015                                 /* Hack -- Update the old location */
3016                                 cave[oy][ox].m_idx = c_ptr->m_idx;
3017
3018                                 /* Mega-Hack -- move the old monster, if any */
3019                                 if (c_ptr->m_idx)
3020                                 {
3021                                         /* Move the old monster */
3022                                         y_ptr->fy = oy;
3023                                         y_ptr->fx = ox;
3024
3025                                         /* Update the old monster */
3026                                         update_monster(c_ptr->m_idx, TRUE);
3027                                 }
3028
3029                                 /* Hack -- Update the new location */
3030                                 c_ptr->m_idx = m_idx;
3031
3032                                 /* Move the monster */
3033                                 m_ptr->fy = ny;
3034                                 m_ptr->fx = nx;
3035                                 update_monster(m_idx, TRUE);
3036
3037                                 lite_spot(oy, ox);
3038                                 lite_spot(ny, nx);
3039                         }
3040                         else
3041                         {
3042                                 /* sound(SOUND_WALK); */
3043
3044                                 /* Move the player */
3045                                 if (!move_player_effect(ny, nx, MPE_DONT_PICKUP)) break;
3046                         }
3047
3048                         /* Possible disturb */
3049                         if (m_ptr->ml &&
3050                             (disturb_move ||
3051                              (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
3052                              (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)))
3053                         {
3054                                 if (is_hostile(m_ptr))
3055                                         disturb(FALSE, TRUE);
3056                         }
3057
3058                         /* Take or Kill objects on the floor */
3059                         if (c_ptr->o_idx && (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
3060                             (!is_pet(m_ptr) || ((p_ptr->pet_extra_flags & PF_PICKUP_ITEMS) && (r_ptr->flags2 & RF2_TAKE_ITEM))))
3061                         {
3062                                 OBJECT_IDX this_o_idx, next_o_idx;
3063                                 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
3064
3065                                 /* Scan all objects in the grid */
3066                                 for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
3067                                 {
3068                                         BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
3069                                         GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
3070                                         object_type *o_ptr = &o_list[this_o_idx];
3071
3072                                         /* Acquire next object */
3073                                         next_o_idx = o_ptr->next_o_idx;
3074
3075                                         if (do_take)
3076                                         {
3077                                                 /* Skip gold */
3078                                                 if (o_ptr->tval == TV_GOLD) continue;
3079
3080                                                 /*
3081                                                  * Skip "real" corpses and statues, to avoid extreme
3082                                                  * silliness like a novice rogue pockets full of statues
3083                                                  * and corpses.
3084                                                  */
3085                                                 if ((o_ptr->tval == TV_CORPSE) ||
3086                                                     (o_ptr->tval == TV_STATUE)) continue;
3087                                         }
3088
3089                                         /* Extract some flags */
3090                                         object_flags(o_ptr, flgs);
3091
3092                                         /* Acquire the object name */
3093                                         object_desc(o_name, o_ptr, 0);
3094                                         monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
3095
3096                                         /* React to objects that hurt the monster */
3097                                         if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
3098                                         if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
3099                                         if (have_flag(flgs, TR_SLAY_TROLL))  flg3 |= (RF3_TROLL);
3100                                         if (have_flag(flgs, TR_KILL_TROLL))  flg3 |= (RF3_TROLL);
3101                                         if (have_flag(flgs, TR_SLAY_GIANT))  flg3 |= (RF3_GIANT);
3102                                         if (have_flag(flgs, TR_KILL_GIANT))  flg3 |= (RF3_GIANT);
3103                                         if (have_flag(flgs, TR_SLAY_ORC))    flg3 |= (RF3_ORC);
3104                                         if (have_flag(flgs, TR_KILL_ORC))    flg3 |= (RF3_ORC);
3105                                         if (have_flag(flgs, TR_SLAY_DEMON))  flg3 |= (RF3_DEMON);
3106                                         if (have_flag(flgs, TR_KILL_DEMON))  flg3 |= (RF3_DEMON);
3107                                         if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
3108                                         if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
3109                                         if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
3110                                         if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
3111                                         if (have_flag(flgs, TR_SLAY_EVIL))   flg3 |= (RF3_EVIL);
3112                                         if (have_flag(flgs, TR_KILL_EVIL))   flg3 |= (RF3_EVIL);
3113                                         if (have_flag(flgs, TR_SLAY_HUMAN))  flg2 |= (RF2_HUMAN);
3114                                         if (have_flag(flgs, TR_KILL_HUMAN))  flg2 |= (RF2_HUMAN);
3115                                         if (have_flag(flgs, TR_BRAND_ACID))  flgr |= (RFR_IM_ACID);
3116                                         if (have_flag(flgs, TR_BRAND_ELEC))  flgr |= (RFR_IM_ELEC);
3117                                         if (have_flag(flgs, TR_BRAND_FIRE))  flgr |= (RFR_IM_FIRE);
3118                                         if (have_flag(flgs, TR_BRAND_COLD))  flgr |= (RFR_IM_COLD);
3119                                         if (have_flag(flgs, TR_BRAND_POIS))  flgr |= (RFR_IM_POIS);
3120
3121                                         /* The object cannot be picked up by the monster */
3122                                         if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
3123                                             ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
3124                                         {
3125                                                 /* Only give a message for "take_item" */
3126                                                 if (do_take && (r_ptr->flags2 & RF2_STUPID))
3127                                                 {
3128                                                         did_take_item = TRUE;
3129
3130                                                         /* Describe observable situations */
3131                                                         if (m_ptr->ml && player_can_see_bold(ny, nx))
3132                                                         {
3133                                                                 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
3134                                                         }
3135                                                 }
3136                                         }
3137
3138                                         /* Pick up the item */
3139                                         else if (do_take)
3140                                         {
3141                                                 did_take_item = TRUE;
3142
3143                                                 /* Describe observable situations */
3144                                                 if (player_can_see_bold(ny, nx))
3145                                                 {
3146                                                         msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
3147                                                 }
3148
3149                                                 /* Excise the object */
3150                                                 excise_object_idx(this_o_idx);
3151
3152                                                 /* Forget mark */
3153                                                 o_ptr->marked &= OM_TOUCHED;
3154
3155                                                 /* Forget location */
3156                                                 o_ptr->iy = o_ptr->ix = 0;
3157
3158                                                 /* Memorize monster */
3159                                                 o_ptr->held_m_idx = m_idx;
3160
3161                                                 /* Build a stack */
3162                                                 o_ptr->next_o_idx = m_ptr->hold_o_idx;
3163
3164                                                 /* Carry object */
3165                                                 m_ptr->hold_o_idx = this_o_idx;
3166                                         }
3167
3168                                         /* Destroy the item if not a pet */
3169                                         else if (!is_pet(m_ptr))
3170                                         {
3171                                                 did_kill_item = TRUE;
3172
3173                                                 /* Describe observable situations */
3174                                                 if (player_has_los_bold(ny, nx))
3175                                                 {
3176                                                         msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
3177                                                 }
3178
3179                                                 delete_object_idx(this_o_idx);
3180                                         }
3181                                 }
3182                         }
3183                 }
3184
3185                 /* Stop when done */
3186                 if (do_turn) break;
3187         }
3188
3189         /*
3190          *  Forward movements failed, but now received LOS attack!
3191          *  Try to flow by smell.
3192          */
3193         if (p_ptr->no_flowed && i > 2 &&  m_ptr->target_y)
3194                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3195
3196         /* If we haven't done anything, try casting a spell again */
3197         if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
3198         {
3199                 /* Try to cast spell again */
3200                 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
3201                 {
3202                         if (make_attack_spell(m_idx)) return;
3203                 }
3204         }
3205
3206
3207         /* Notice changes in view */
3208         if (do_view)
3209         {
3210                 p_ptr->update |= (PU_FLOW);
3211                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3212         }
3213
3214         /* Notice changes in view */
3215         if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
3216                 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !p_ptr->inside_battle)))
3217         {
3218                 /* Update some things */
3219                 p_ptr->update |= (PU_MON_LITE);
3220         }
3221
3222         /* Learn things from observable monster */
3223         if (is_original_ap_and_seen(m_ptr))
3224         {
3225                 /* Monster opened a door */
3226                 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
3227
3228                 /* Monster bashed a door */
3229                 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
3230
3231                 /* Monster tried to pick something up */
3232                 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
3233
3234                 /* Monster tried to crush something */
3235                 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
3236
3237                 /* Monster pushed past another monster */
3238                 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
3239
3240                 /* Monster passed through a wall */
3241                 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
3242
3243                 /* Monster destroyed a wall */
3244                 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
3245         }
3246
3247
3248         /* Hack -- get "bold" if out of options */
3249         if (!do_turn && !do_move && MON_MONFEAR(m_ptr) && aware)
3250         {
3251                 /* No longer afraid */
3252                 (void)set_monster_monfear(m_idx, 0);
3253
3254                 /* Message if seen */
3255                 if (see_m)
3256                 {
3257                         GAME_TEXT m_name[MAX_NLEN];
3258                         monster_desc(m_name, m_ptr, 0);
3259                         msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
3260                 }
3261
3262                 if (m_ptr->ml) chg_virtue(V_COMPASSION, -1);
3263
3264                 /* Actually do something now (?) */
3265         }
3266 }
3267
3268 /*!
3269  * @brief 全モンスターのターン管理メインルーチン /
3270  * Process all the "live" monsters, once per game turn.
3271  * @return なし
3272  * @details
3273  * During each game turn, we scan through the list of all the "live" monsters,\n
3274  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
3275  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
3276  *\n
3277  * Note that monsters can never move in the monster array (except when the\n
3278  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
3279  *\n
3280  * This function is responsible for at least half of the processor time\n
3281  * on a normal system with a "normal" amount of monsters and a player doing\n
3282  * normal things.\n
3283  *\n
3284  * When the player is resting, virtually 90% of the processor time is spent\n
3285  * in this function, and its children, "process_monster()" and "make_move()".\n
3286  *\n
3287  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
3288  * especially when the player is running.\n
3289  *\n
3290  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
3291  * monsters while they are still being "born".  A monster is "fresh" only\n
3292  * during the turn in which it is created, and we use the "hack_m_idx" to\n
3293  * determine if the monster is yet to be processed during the current turn.\n
3294  *\n
3295  * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
3296  * move before any "nasty" monsters get to use their spell attacks.\n
3297  *\n
3298  * Note that when the "knowledge" about the currently tracked monster\n
3299  * changes (flags, attacks, spells), we induce a redraw of the monster\n
3300  * recall window.\n
3301  */
3302 void process_monsters(void)
3303 {
3304         IDX i;
3305         POSITION fx, fy;
3306
3307         bool            test;
3308
3309         monster_type    *m_ptr;
3310         monster_race    *r_ptr;
3311
3312         MONRACE_IDX old_monster_race_idx;
3313
3314         BIT_FLAGS old_r_flags1 = 0L;
3315         BIT_FLAGS old_r_flags2 = 0L;
3316         BIT_FLAGS old_r_flags3 = 0L;
3317         BIT_FLAGS old_r_flags4 = 0L;
3318         BIT_FLAGS old_r_flags5 = 0L;
3319         BIT_FLAGS old_r_flags6 = 0L;
3320         BIT_FLAGS old_r_flagsr = 0L;
3321
3322         byte old_r_blows0 = 0;
3323         byte old_r_blows1 = 0;
3324         byte old_r_blows2 = 0;
3325         byte old_r_blows3 = 0;
3326
3327         byte old_r_cast_spell = 0;
3328
3329         SPEED speed;
3330
3331         /* Clear monster fighting indicator */
3332         mon_fight = FALSE;
3333
3334         /* Memorize old race */
3335         old_monster_race_idx = p_ptr->monster_race_idx;
3336
3337         /* Acquire knowledge */
3338         if (p_ptr->monster_race_idx)
3339         {
3340                 /* Acquire current monster */
3341                 r_ptr = &r_info[p_ptr->monster_race_idx];
3342
3343                 /* Memorize flags */
3344                 old_r_flags1 = r_ptr->r_flags1;
3345                 old_r_flags2 = r_ptr->r_flags2;
3346                 old_r_flags3 = r_ptr->r_flags3;
3347                 old_r_flags4 = r_ptr->r_flags4;
3348                 old_r_flags5 = r_ptr->r_flags5;
3349                 old_r_flags6 = r_ptr->r_flags6;
3350                 old_r_flagsr = r_ptr->r_flagsr;
3351
3352                 /* Memorize blows */
3353                 old_r_blows0 = r_ptr->r_blows[0];
3354                 old_r_blows1 = r_ptr->r_blows[1];
3355                 old_r_blows2 = r_ptr->r_blows[2];
3356                 old_r_blows3 = r_ptr->r_blows[3];
3357
3358                 /* Memorize castings */
3359                 old_r_cast_spell = r_ptr->r_cast_spell;
3360         }
3361
3362
3363         /* Process the monsters (backwards) */
3364         for (i = m_max - 1; i >= 1; i--)
3365         {
3366                 /* Access the monster */
3367                 m_ptr = &m_list[i];
3368                 r_ptr = &r_info[m_ptr->r_idx];
3369
3370                 /* Handle "leaving" */
3371                 if (p_ptr->leaving) break;
3372
3373                 /* Ignore "dead" monsters */
3374                 if (!m_ptr->r_idx) continue;
3375
3376                 if (p_ptr->wild_mode) continue;
3377
3378
3379                 /* Handle "fresh" monsters */
3380                 if (m_ptr->mflag & MFLAG_BORN)
3381                 {
3382                         /* No longer "fresh" */
3383                         m_ptr->mflag &= ~(MFLAG_BORN);
3384
3385                         /* Skip */
3386                         continue;
3387                 }
3388
3389                 /* Hack -- Require proximity */
3390                 if (m_ptr->cdis >= AAF_LIMIT) continue;
3391
3392
3393                 /* Access the location */
3394                 fx = m_ptr->fx;
3395                 fy = m_ptr->fy;
3396
3397                 /* Flow by smell is allowed */
3398                 if (!p_ptr->no_flowed)
3399                 {
3400                         m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3401                 }
3402
3403                 /* Assume no move */
3404                 test = FALSE;
3405
3406                 /* Handle "sensing radius" */
3407                 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
3408                 {
3409                         /* We can "sense" the player */
3410                         test = TRUE;
3411                 }
3412
3413                 /* Handle "sight" and "aggravation" */
3414         else if ((m_ptr->cdis <= MAX_SIGHT || p_ptr->inside_battle) &&
3415                         (player_has_los_bold(fy, fx) || (p_ptr->cursed & TRC_AGGRAVATE)))
3416                 {
3417                         /* We can "see" or "feel" the player */
3418                         test = TRUE;
3419                 }
3420
3421 #if 0 /* (cave[p_ptr->y][p_ptr->x].when == cave[fy][fx].when) is always FALSE... */
3422                 /* Hack -- Monsters can "smell" the player from far away */
3423                 /* Note that most monsters have "aaf" of "20" or so */
3424                 else if (!(m_ptr->mflag2 & MFLAG2_NOFLOW) &&
3425                         cave_have_flag_bold(p_ptr->y, p_ptr->x, FF_MOVE) &&
3426                         (cave[p_ptr->y][p_ptr->x].when == cave[fy][fx].when) &&
3427                         (cave[fy][fx].dist < MONSTER_FLOW_DEPTH) &&
3428                         (cave[fy][fx].dist < r_ptr->aaf))
3429                 {
3430                         /* We can "smell" the player */
3431                         test = TRUE;
3432                 }
3433 #endif
3434                 else if (m_ptr->target_y) test = TRUE;
3435
3436                 /* Do nothing */
3437                 if (!test) continue;
3438
3439
3440                 if (p_ptr->riding == i)
3441                         speed = p_ptr->pspeed;
3442                 else
3443                 {
3444                         speed = m_ptr->mspeed;
3445
3446                         /* Monsters move quickly in Nightmare mode */
3447                         if (ironman_nightmare) speed += 5;
3448
3449                         if (MON_FAST(m_ptr)) speed += 10;
3450                         if (MON_SLOW(m_ptr)) speed -= 10;
3451                 }
3452
3453                 /* Give this monster some energy */
3454                 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
3455
3456                 /* Not enough energy to move */
3457                 if (m_ptr->energy_need > 0) continue;
3458
3459                 /* Use up "some" energy */
3460                 m_ptr->energy_need += ENERGY_NEED();
3461
3462
3463                 /* Save global index */
3464                 hack_m_idx = i;
3465
3466                 /* Process the monster */
3467                 process_monster(i);
3468
3469                 reset_target(m_ptr);
3470
3471                 /* Give up flow_by_smell when it might useless */
3472                 if (p_ptr->no_flowed && one_in_(3))
3473                         m_ptr->mflag2 |= MFLAG2_NOFLOW;
3474
3475                 /* Hack -- notice death or departure */
3476                 if (!p_ptr->playing || p_ptr->is_dead) break;
3477
3478                 /* Notice leaving */
3479                 if (p_ptr->leaving) break;
3480         }
3481
3482         /* Reset global index */
3483         hack_m_idx = 0;
3484
3485
3486         /* Tracking a monster race (the same one we were before) */
3487         if (p_ptr->monster_race_idx && (p_ptr->monster_race_idx == old_monster_race_idx))
3488         {
3489                 /* Acquire monster race */
3490                 r_ptr = &r_info[p_ptr->monster_race_idx];
3491
3492                 /* Check for knowledge change */
3493                 if ((old_r_flags1 != r_ptr->r_flags1) ||
3494                         (old_r_flags2 != r_ptr->r_flags2) ||
3495                         (old_r_flags3 != r_ptr->r_flags3) ||
3496                         (old_r_flags4 != r_ptr->r_flags4) ||
3497                         (old_r_flags5 != r_ptr->r_flags5) ||
3498                         (old_r_flags6 != r_ptr->r_flags6) ||
3499                         (old_r_flagsr != r_ptr->r_flagsr) ||
3500                         (old_r_blows0 != r_ptr->r_blows[0]) ||
3501                         (old_r_blows1 != r_ptr->r_blows[1]) ||
3502                         (old_r_blows2 != r_ptr->r_blows[2]) ||
3503                         (old_r_blows3 != r_ptr->r_blows[3]) ||
3504                         (old_r_cast_spell != r_ptr->r_cast_spell))
3505                 {
3506                         p_ptr->window |= (PW_MONSTER);
3507                 }
3508         }
3509 }
3510