OSDN Git Service

Merge branch 'experimental-utf8nize-convert-to-utf8'
[hengband/hengband.git] / src / melee2.c
1 /*!
2  * @file melee2.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
18 #define SPEAK_CHANCE 8
19 #define GRINDNOISE 20
20 #define CYBERNOISE 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(int m_idx, int *mm)
30 {
31         int i;
32         int x = 0, y = 0;
33         int 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
40         monster_type *t_ptr;
41
42         if (riding_t_m_idx && player_bold(m_ptr->fy, m_ptr->fx))
43         {
44                 y = m_list[riding_t_m_idx].fy;
45                 x = m_list[riding_t_m_idx].fx;
46         }
47         else if (is_pet(m_ptr) && pet_t_m_idx)
48         {
49                 y = m_list[pet_t_m_idx].fy;
50                 x = m_list[pet_t_m_idx].fx;
51         }
52         else
53         {
54                 if (p_ptr->inside_battle)
55                 {
56                         start = randint1(m_max-1)+m_max;
57                         if(randint0(2)) plus = -1;
58                 }
59                 else start = m_max + 1;
60
61                 /* Scan thru all monsters */
62                 for (i = start; ((i < start + m_max) && (i > start - m_max)); i+=plus)
63                 {
64                         int dummy = (i % m_max);
65
66                         if (!dummy) continue;
67
68                         t_idx = dummy;
69                         t_ptr = &m_list[t_idx];
70
71                         /* The monster itself isn't a target */
72                         if (t_ptr == m_ptr) continue;
73
74                         /* Paranoia -- Skip dead monsters */
75                         if (!t_ptr->r_idx) continue;
76
77                         if (is_pet(m_ptr))
78                         {
79                                 /* Hack -- only fight away from player */
80                                 if (p_ptr->pet_follow_distance < 0)
81                                 {
82                                         /* No fighting near player */
83                                         if (t_ptr->cdis <= (0 - p_ptr->pet_follow_distance))
84                                         {
85                                                 continue;
86                                         }
87                                 }
88                                 /* Hack -- no fighting away from player */
89                                 else if ((m_ptr->cdis < t_ptr->cdis) &&
90                                                         (t_ptr->cdis > p_ptr->pet_follow_distance))
91                                 {
92                                         continue;
93                                 }
94
95                                 if (r_ptr->aaf < t_ptr->cdis) continue;
96                         }
97
98                         /* Monster must be 'an enemy' */
99                         if (!are_enemies(m_ptr, t_ptr)) continue;
100
101                         /* Monster must be projectable if we can't pass through walls */
102                         if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) ||
103                             ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)))
104                         {
105                                 if (!in_disintegration_range(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
106                         }
107                         else
108                         {
109                                 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
110                         }
111
112                         /* OK -- we've got a target */
113                         y = t_ptr->fy;
114                         x = t_ptr->fx;
115
116                         break;
117                 }
118                 if (!x && !y) return FALSE;
119         }
120
121         /* Extract the direction */
122         x -= m_ptr->fx;
123         y -= m_ptr->fy;
124
125         /* North */
126         if ((y < 0) && (x == 0))
127         {
128                 mm[0] = 8;
129                 mm[1] = 7;
130                 mm[2] = 9;
131         }
132         /* South */
133         else if ((y > 0) && (x == 0))
134         {
135                 mm[0] = 2;
136                 mm[1] = 1;
137                 mm[2] = 3;
138         }
139         /* East */
140         else if ((x > 0) && (y == 0))
141         {
142                 mm[0] = 6;
143                 mm[1] = 9;
144                 mm[2] = 3;
145         }
146         /* West */
147         else if ((x < 0) && (y == 0))
148         {
149                 mm[0] = 4;
150                 mm[1] = 7;
151                 mm[2] = 1;
152         }
153         /* North-West */
154         else if ((y < 0) && (x < 0))
155         {
156                 mm[0] = 7;
157                 mm[1] = 4;
158                 mm[2] = 8;
159         }
160         /* North-East */
161         else if ((y < 0) && (x > 0))
162         {
163                 mm[0] = 9;
164                 mm[1] = 6;
165                 mm[2] = 8;
166         }
167         /* South-West */
168         else if ((y > 0) && (x < 0))
169         {
170                 mm[0] = 1;
171                 mm[1] = 4;
172                 mm[2] = 2;
173         }
174         /* South-East */
175         else if ((y > 0) && (x > 0))
176         {
177                 mm[0] = 3;
178                 mm[1] = 6;
179                 mm[2] = 2;
180         }
181
182         /* Found a monster */
183         return TRUE;
184 }
185
186
187 /*!
188  * @brief モンスターが敵モンスターに行う打撃処理 /
189  * Hack, based on mon_take_hit... perhaps all monster attacks on other monsters should use this?
190  * @param m_idx 目標となるモンスターの参照ID
191  * @param dam ダメージ量
192  * @param fear 目標となるモンスターの恐慌状態を返す参照ポインタ
193  * @param note 目標モンスターが死亡した場合の特別メッセージ(NULLならば標準表示を行う)
194  * @param who 打撃を行ったモンスターの参照ID
195  * @return なし
196  */
197 void mon_take_hit_mon(int m_idx, int dam, bool *fear, cptr note, int who)
198 {
199         monster_type    *m_ptr = &m_list[m_idx];
200
201         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
202
203         char m_name[160];
204
205         bool seen = is_seen(m_ptr);
206
207         /* Can the player be aware of this attack? */
208         bool known = (m_ptr->cdis <= MAX_SIGHT);
209
210         /* Extract monster name */
211         monster_desc(m_name, m_ptr, 0);
212
213         /* Redraw (later) if needed */
214         if (m_ptr->ml)
215         {
216                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
217                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
218         }
219
220         /* Wake it up */
221         (void)set_monster_csleep(m_idx, 0);
222
223         if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(1, 1);
224
225         if (MON_INVULNER(m_ptr) && randint0(PENETRATE_INVULNERABILITY))
226         {
227                 if (seen)
228                 {
229 #ifdef JP
230 msg_format("%^sはダメージを受けない。", m_name);
231 #else
232                         msg_format("%^s is unharmed.", m_name);
233 #endif
234
235                 }
236
237                 return;
238         }
239
240         if (r_ptr->flagsr & RFR_RES_ALL)
241         {
242                 if(dam > 0)
243                 {
244                         dam /= 100;
245                         if((dam == 0) && one_in_(3)) dam = 1;
246                 }
247                 if (dam==0)
248                 {
249                         if (seen)
250                         {
251 #ifdef JP
252 msg_format("%^sはダメージを受けない。", m_name);
253 #else
254                                 msg_format("%^s is unharmed.", m_name);
255 #endif
256
257                         }
258                         return;
259                 }
260         }
261
262         /* Hurt it */
263         m_ptr->hp -= dam;
264
265         /* It is dead now... or is it? */
266         if (m_ptr->hp < 0)
267         {
268                 if (((r_ptr->flags1 & (RF1_UNIQUE | RF1_QUESTOR)) ||
269                     (r_ptr->flags7 & RF7_NAZGUL)) &&
270                     !p_ptr->inside_battle)
271                 {
272                         m_ptr->hp = 1;
273                 }
274                 else
275                 {
276                         /* Make a sound */
277                         if (!monster_living(r_ptr))
278                         {
279                                 sound(SOUND_N_KILL);
280                         }
281                         else
282                         {
283                                 sound(SOUND_KILL);
284                         }
285
286                         if (known)
287                         {
288                                 monster_desc(m_name, m_ptr, MD_TRUE_NAME);
289                                 /* Unseen death by normal attack */
290                                 if (!seen)
291                                 {
292                                         mon_fight = TRUE;
293                                 }
294                                 /* Death by special attack */
295                                 else if (note)
296                                 {
297 #ifdef JP
298 msg_format("%^s%s", m_name, note);
299 #else
300                                         msg_format("%^s%s", m_name, note);
301 #endif
302
303                                 }
304                                 /* Death by normal attack -- nonliving monster */
305                                 else if (!monster_living(r_ptr))
306                                 {
307 #ifdef JP
308 msg_format("%^sは破壊された。", m_name);
309 #else
310                                         msg_format("%^s is destroyed.", m_name);
311 #endif
312
313                                 }
314                                 /* Death by normal attack -- living monster */
315                                 else
316                                 {
317 #ifdef JP
318 msg_format("%^sは殺された。", m_name);
319 #else
320                                         msg_format("%^s is killed.", m_name);
321 #endif
322
323                                 }
324                         }
325
326                         monster_gain_exp(who, m_ptr->r_idx);
327
328                         /* Generate treasure */
329                         monster_death(m_idx, FALSE);
330
331                         /* Delete the monster */
332                         delete_monster_idx(m_idx);
333
334                         /* Not afraid */
335                         (*fear) = FALSE;
336
337                         /* Monster is dead */
338                         return;
339                 }
340         }
341
342 #ifdef ALLOW_FEAR
343
344         /* Mega-Hack -- Pain cancels fear */
345         if (MON_MONFEAR(m_ptr) && (dam > 0))
346         {
347                 /* Cure fear */
348                 if (set_monster_monfear(m_idx, MON_MONFEAR(m_ptr) - randint1(dam / 4)))
349                 {
350                         /* No more fear */
351                         (*fear) = FALSE;
352                 }
353         }
354
355         /* Sometimes a monster gets scared by damage */
356         if (!MON_MONFEAR(m_ptr) && !(r_ptr->flags3 & RF3_NO_FEAR))
357         {
358                 /* Percentage of fully healthy */
359                 int percentage = (100L * m_ptr->hp) / m_ptr->maxhp;
360
361                 /*
362                 * Run (sometimes) if at 10% or less of max hit points,
363                 * or (usually) when hit for half its current hit points
364                  */
365                 if (((percentage <= 10) && (randint0(10) < percentage)) ||
366                         ((dam >= m_ptr->hp) && (randint0(100) < 80)))
367                 {
368                         /* Hack -- note fear */
369                         (*fear) = TRUE;
370
371                         /* XXX XXX XXX Hack -- Add some timed fear */
372                         (void)set_monster_monfear(m_idx, (randint1(10) +
373                                 (((dam >= m_ptr->hp) && (percentage > 7)) ?
374                                 20 : ((11 - percentage) * 5))));
375                 }
376         }
377
378 #endif /* ALLOW_FEAR */
379
380         if ((dam > 0) && !is_pet(m_ptr) && !is_friendly(m_ptr) && (who != m_idx))
381         {
382                 if (is_pet(&m_list[who]) && !player_bold(m_ptr->target_y, m_ptr->target_x))
383                 {
384                         set_target(m_ptr, m_list[who].fy, m_list[who].fx);
385                 }
386         }
387
388         if (p_ptr->riding && (p_ptr->riding == m_idx) && (dam > 0))
389         {
390                 char m_name[80];
391
392                 /* Extract monster name */
393                 monster_desc(m_name, m_ptr, 0);
394
395                 if (m_ptr->hp > m_ptr->maxhp/3) dam = (dam + 1) / 2;
396                 if (rakuba((dam > 200) ? 200 : dam, FALSE))
397                 {
398 #ifdef JP
399 msg_format("%^sに振り落とされた!", m_name);
400 #else
401                                 msg_format("You have thrown off from %s!", m_name);
402 #endif
403                 }
404         }
405
406         /* Not dead yet */
407         return;
408 }
409
410
411 /*!
412  * @brief モンスターがプレイヤーから逃走するかどうかを返す /
413  * Returns whether a given monster will try to run from the player.
414  * @param m_idx 逃走するモンスターの参照ID
415  * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
416  * @details
417  * Monsters will attempt to avoid very powerful players.  See below.\n
418  *\n
419  * Because this function is called so often, little details are important\n
420  * for efficiency.  Like not using "mod" or "div" when possible.  And\n
421  * attempting to check the conditions in an optimal order.  Note that\n
422  * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
423  *\n
424  * Note that this function is responsible for about one to five percent\n
425  * of the processor use in normal conditions...\n
426  */
427 static int mon_will_run(int m_idx)
428 {
429         monster_type *m_ptr = &m_list[m_idx];
430
431 #ifdef ALLOW_TERROR
432
433         monster_race *r_ptr = &r_info[m_ptr->r_idx];
434
435         u16b p_lev, m_lev;
436         u16b p_chp, p_mhp;
437         u16b m_chp, m_mhp;
438         u32b p_val, m_val;
439
440 #endif
441
442         /* Friends can be commanded to avoid the player */
443         if (is_pet(m_ptr))
444         {
445                 /* Are we trying to avoid the player? */
446                 return ((p_ptr->pet_follow_distance < 0) &&
447                                   (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
448         }
449
450         /* Keep monsters from running too far away */
451         if (m_ptr->cdis > MAX_SIGHT + 5) return (FALSE);
452
453         /* All "afraid" monsters will run away */
454         if (MON_MONFEAR(m_ptr)) return (TRUE);
455
456 #ifdef ALLOW_TERROR
457
458         /* Nearby monsters will not become terrified */
459         if (m_ptr->cdis <= 5) return (FALSE);
460
461         /* Examine player power (level) */
462         p_lev = p_ptr->lev;
463
464         /* Examine monster power (level plus morale) */
465         m_lev = r_ptr->level + (m_idx & 0x08) + 25;
466
467         /* Optimize extreme cases below */
468         if (m_lev > p_lev + 4) return (FALSE);
469         if (m_lev + 4 <= p_lev) return (TRUE);
470
471         /* Examine player health */
472         p_chp = p_ptr->chp;
473         p_mhp = p_ptr->mhp;
474
475         /* Examine monster health */
476         m_chp = m_ptr->hp;
477         m_mhp = m_ptr->maxhp;
478
479         /* Prepare to optimize the calculation */
480         p_val = (p_lev * p_mhp) + (p_chp << 2); /* div p_mhp */
481         m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
482
483         /* Strong players scare strong monsters */
484         if (p_val * m_mhp > m_val * p_mhp) return (TRUE);
485
486 #endif
487
488         /* Assume no terror */
489         return (FALSE);
490 }
491
492
493 /*!
494  * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
495  * Search spell castable grid
496  * @param m_idx モンスターの参照ID
497  * @param yp 適したマスのY座標を返す参照ポインタ
498  * @param xp 適したマスのX座標を返す参照ポインタ
499  * @return 有効なマスがあった場合TRUEを返す
500  */
501 static bool get_moves_aux2(int m_idx, int *yp, int *xp)
502 {
503         int i, y, x, y1, x1, best = 999;
504
505         cave_type *c_ptr;
506         bool can_open_door = FALSE;
507         int now_cost;
508
509         monster_type *m_ptr = &m_list[m_idx];
510         monster_race *r_ptr = &r_info[m_ptr->r_idx];
511
512         /* Monster location */
513         y1 = m_ptr->fy;
514         x1 = m_ptr->fx;
515
516         /* Monster can already cast spell to player */
517         if (projectable(y1, x1, py, px)) return (FALSE);
518
519         /* Set current grid cost */
520         now_cost = cave[y1][x1].cost;
521         if (now_cost == 0) now_cost = 999;
522
523         /* Can monster bash or open doors? */
524         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
525         {
526                 can_open_door = TRUE;
527         }
528
529         /* Check nearby grids, diagonals first */
530         for (i = 7; i >= 0; i--)
531         {
532                 int cost;
533
534                 /* Get the location */
535                 y = y1 + ddy_ddd[i];
536                 x = x1 + ddx_ddd[i];
537
538                 /* Ignore locations off of edge */
539                 if (!in_bounds2(y, x)) continue;
540
541                 /* Simply move to player */
542                 if (player_bold(y, x)) return (FALSE);
543
544                 c_ptr = &cave[y][x];
545
546                 cost = c_ptr->cost;
547
548                 /* Monster cannot kill or pass walls */
549                 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))))
550                 {
551                         if (cost == 0) continue;
552                         if (!can_open_door && is_closed_door(c_ptr->feat)) continue;
553                 }
554
555                 /* Hack -- for kill or pass wall monster.. */
556                 if (cost == 0) cost = 998;
557
558                 if (now_cost < cost) continue;
559
560                 if (!projectable(y, x, py, px)) continue;
561
562                 /* Accept louder sounds */
563                 if (best < cost) continue;
564                 best = cost;
565
566                 (*yp) = y1 + ddy_ddd[i];
567                 (*xp) = x1 + ddx_ddd[i];
568         }
569
570         /* No legal move (?) */
571         if (best == 999) return (FALSE);
572
573         /* Success */
574         return (TRUE);
575 }
576
577
578 /*!
579  * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
580  * Choose the "best" direction for "flowing"
581  * @param m_idx モンスターの参照ID
582  * @param yp 移動先のマスのY座標を返す参照ポインタ
583  * @param xp 移動先のマスのX座標を返す参照ポインタ
584  * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
585  * @return 有効なマスがあった場合TRUEを返す
586  * @details
587  * Note that ghosts and rock-eaters are never allowed to "flow",\n
588  * since they should move directly towards the player.\n
589  *\n
590  * Prefer "non-diagonal" directions, but twiddle them a little\n
591  * to angle slightly towards the player's actual location.\n
592  *\n
593  * Allow very perceptive monsters to track old "spoor" left by\n
594  * previous locations occupied by the player.  This will tend\n
595  * to have monsters end up either near the player or on a grid\n
596  * recently occupied by the player (and left via "teleport").\n
597  *\n
598  * Note that if "smell" is turned on, all monsters get vicious.\n
599  *\n
600  * Also note that teleporting away from a location will cause\n
601  * the monsters who were chasing you to converge on that location\n
602  * as long as you are still near enough to "annoy" them without\n
603  * being close enough to chase directly.  I have no idea what will\n
604  * happen if you combine "smell" with low "aaf" values.\n
605  */
606 static bool get_moves_aux(int m_idx, int *yp, int *xp, bool no_flow)
607 {
608         int i, y, x, y1, x1, best;
609
610         cave_type *c_ptr;
611         bool use_scent = FALSE;
612
613         monster_type *m_ptr = &m_list[m_idx];
614         monster_race *r_ptr = &r_info[m_ptr->r_idx];
615
616         /* Can monster cast attack spell? */
617         if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
618             r_ptr->flags5 & (RF5_ATTACK_MASK) ||
619             r_ptr->flags6 & (RF6_ATTACK_MASK))
620         {
621                 /* Can move spell castable grid? */
622                 if (get_moves_aux2(m_idx, yp, xp)) return (TRUE);
623         }
624
625         /* Monster can't flow */
626         if (no_flow) return (FALSE);
627
628         /* Monster can go through rocks */
629         if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) return (FALSE);
630         if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)) return (FALSE);
631
632         /* Monster location */
633         y1 = m_ptr->fy;
634         x1 = m_ptr->fx;
635
636         /* Hack -- Player can see us, run towards him */
637         if (player_has_los_bold(y1, x1) && projectable(py, px, y1, x1)) return (FALSE);
638
639         /* Monster grid */
640         c_ptr = &cave[y1][x1];
641
642         /* If we can hear noises, advance towards them */
643         if (c_ptr->cost)
644         {
645                 best = 999;
646         }
647
648         /* Otherwise, try to follow a scent trail */
649         else if (c_ptr->when)
650         {
651                 /* Too old smell */
652                 if (cave[py][px].when - c_ptr->when > 127) return (FALSE);
653
654                 use_scent = TRUE;
655                 best = 0;
656         }
657
658         /* Otherwise, advance blindly */
659         else
660         {
661                 return (FALSE);
662         }
663
664         /* Check nearby grids, diagonals first */
665         for (i = 7; i >= 0; i--)
666         {
667                 /* Get the location */
668                 y = y1 + ddy_ddd[i];
669                 x = x1 + ddx_ddd[i];
670
671                 /* Ignore locations off of edge */
672                 if (!in_bounds2(y, x)) continue;
673
674                 c_ptr = &cave[y][x];
675
676                 /* We're following a scent trail */
677                 if (use_scent)
678                 {
679                         int when = c_ptr->when;
680
681                         /* Accept younger scent */
682                         if (best > when) continue;
683                         best = when;
684                 }
685
686                 /* We're using sound */
687                 else
688                 {
689                         int cost;
690
691                         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
692                                 cost = c_ptr->dist;
693                         else cost = c_ptr->cost;
694
695                         /* Accept louder sounds */
696                         if ((cost == 0) || (best < cost)) continue;
697                         best = cost;
698                 }
699
700                 /* Hack -- Save the "twiddled" location */
701                 (*yp) = py + 16 * ddy_ddd[i];
702                 (*xp) = px + 16 * ddx_ddd[i];
703         }
704
705         /* No legal move (?) */
706         if (best == 999 || best == 0) return (FALSE);
707
708         /* Success */
709         return (TRUE);
710 }
711
712
713 /*!
714  * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
715  * Provide a location to flee to, but give the player a wide berth.
716  * @param m_idx モンスターの参照ID
717  * @param yp 移動先のマスのY座標を返す参照ポインタ
718  * @param xp 移動先のマスのX座標を返す参照ポインタ
719  * @return 有効なマスがあった場合TRUEを返す
720  * @details
721  * A monster may wish to flee to a location that is behind the player,\n
722  * but instead of heading directly for it, the monster should "swerve"\n
723  * around the player so that he has a smaller chance of getting hit.\n
724  */
725 static bool get_fear_moves_aux(int m_idx, int *yp, int *xp)
726 {
727         int y, x, y1, x1, fy, fx, gy = 0, gx = 0;
728         int score = -1;
729         int i;
730
731         monster_type *m_ptr = &m_list[m_idx];
732
733         /* Monster location */
734         fy = m_ptr->fy;
735         fx = m_ptr->fx;
736
737         /* Desired destination */
738         y1 = fy - (*yp);
739         x1 = fx - (*xp);
740
741         /* Check nearby grids, diagonals first */
742         for (i = 7; i >= 0; i--)
743         {
744                 int dis, s;
745
746                 /* Get the location */
747                 y = fy + ddy_ddd[i];
748                 x = fx + ddx_ddd[i];
749
750                 /* Ignore locations off of edge */
751                 if (!in_bounds2(y, x)) continue;
752
753                 /* Don't move toward player */
754                 /* if (cave[y][x].dist < 3) continue; */ /* Hmm.. Need it? */
755
756                 /* Calculate distance of this grid from our destination */
757                 dis = distance(y, x, y1, x1);
758
759                 /* Score this grid */
760                 s = 5000 / (dis + 3) - 500 / (cave[y][x].dist + 1);
761
762                 /* No negative scores */
763                 if (s < 0) s = 0;
764
765                 /* Ignore lower scores */
766                 if (s < score) continue;
767
768                 /* Save the score and time */
769                 score = s;
770
771                 /* Save the location */
772                 gy = y;
773                 gx = x;
774         }
775
776         /* No legal move (?) */
777         if (score == -1) return (FALSE);
778
779         /* Find deltas */
780         (*yp) = fy - gy;
781         (*xp) = fx - gx;
782
783         /* Success */
784         return (TRUE);
785 }
786
787 /*
788  * Hack -- Precompute a bunch of calls to distance() in find_safety() and
789  * find_hiding().
790  *
791  * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
792  * offsets of all the locations with a distance of n from a central point,
793  * with an offset of (0,0) indicating no more offsets at this distance.
794  *
795  * This is, of course, fairly unreadable, but it eliminates multiple loops
796  * from the previous version.
797  *
798  * It is probably better to replace these arrays with code to compute
799  * the relevant arrays, even if the storage is pre-allocated in hard
800  * coded sizes.  At the very least, code should be included which is
801  * able to generate and dump these arrays (ala "los()").  XXX XXX XXX
802  *
803  * Also, the storage needs could be halved by using bytes.  XXX XXX XXX
804  *
805  * These arrays could be combined into two big arrays, using sub-arrays
806  * to hold the offsets and lengths of each portion of the sub-arrays, and
807  * this could perhaps also be used somehow in the "look" code.  XXX XXX XXX
808  */
809
810
811 static sint d_off_y_0[] =
812 { 0 };
813
814 static sint d_off_x_0[] =
815 { 0 };
816
817
818 static sint d_off_y_1[] =
819 { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
820
821 static sint d_off_x_1[] =
822 { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
823
824
825 static sint d_off_y_2[] =
826 { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
827
828 static sint d_off_x_2[] =
829 { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
830
831
832 static sint d_off_y_3[] =
833 { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2,
834   3, 3, 3, 0 };
835
836 static sint d_off_x_3[] =
837 { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2,
838   -1, 0, 1, 0 };
839
840
841 static sint d_off_y_4[] =
842 { -1, -1, -2, -2, -3, -3, -3, -3, -4, -4, -4, 0,
843   0, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 0 };
844
845 static sint d_off_x_4[] =
846 { -4, 4, -3, 3, -2, -3, 2, 3, -1, 0, 1, -4, 4,
847   -4, 4, -3, 3, -2, -3, 2, 3, -1, 0, 1, 0 };
848
849
850 static sint d_off_y_5[] =
851 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
852   -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
853   5, 0 };
854
855 static sint d_off_x_5[] =
856 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
857   -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
858   0, 1, 0 };
859
860
861 static sint d_off_y_6[] =
862 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
863   -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
864   5, 5, 6, 6, 6, 0 };
865
866 static sint d_off_x_6[] =
867 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
868   0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
869   -3, 2, 3, -1, 0, 1, 0 };
870
871
872 static sint d_off_y_7[] =
873 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
874   -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
875   3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
876
877 static sint d_off_x_7[] =
878 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
879   -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
880   6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
881   1, 0 };
882
883
884 static sint d_off_y_8[] =
885 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
886   -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
887   2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
888   8, 8, 8, 0 };
889
890 static sint d_off_x_8[] =
891 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
892   5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
893   7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
894   2, 3, -1, 0, 1, 0 };
895
896
897 static sint d_off_y_9[] =
898 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
899   -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
900   0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
901   7, 8, 8, 8, 8, 9, 9, 9, 0 };
902
903 static sint d_off_x_9[] =
904 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
905   -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
906   9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
907   4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
908
909
910 static sint *dist_offsets_y[10] =
911 {
912         d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
913         d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
914 };
915
916 static sint *dist_offsets_x[10] =
917 {
918         d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
919         d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
920 };
921
922 /*!
923  * @brief モンスターが逃げ込める安全な地点を返す /
924  * Choose a "safe" location near a monster for it to run toward.
925  * @param m_idx モンスターの参照ID
926  * @param yp 移動先のマスのY座標を返す参照ポインタ
927  * @param xp 移動先のマスのX座標を返す参照ポインタ
928  * @return 有効なマスがあった場合TRUEを返す
929  * @details
930  * A location is "safe" if it can be reached quickly and the player\n
931  * is not able to fire into it (it isn't a "clean shot").  So, this will\n
932  * cause monsters to "duck" behind walls.  Hopefully, monsters will also\n
933  * try to run towards corridor openings if they are in a room.\n
934  *\n
935  * This function may take lots of CPU time if lots of monsters are\n
936  * fleeing.\n
937  *\n
938  * Return TRUE if a safe location is available.\n
939  */
940 static bool find_safety(int m_idx, int *yp, int *xp)
941 {
942         monster_type *m_ptr = &m_list[m_idx];
943
944         int fy = m_ptr->fy;
945         int fx = m_ptr->fx;
946
947         int y, x, dy, dx, d, dis, i;
948         int gy = 0, gx = 0, gdis = 0;
949
950         sint *y_offsets;
951         sint *x_offsets;
952
953         cave_type *c_ptr;
954
955         /* Start with adjacent locations, spread further */
956         for (d = 1; d < 10; d++)
957         {
958                 /* Get the lists of points with a distance d from (fx, fy) */
959                 y_offsets = dist_offsets_y[d];
960                 x_offsets = dist_offsets_x[d];
961
962                 /* Check the locations */
963                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
964                      dx != 0 || dy != 0;
965                      i++, dx = x_offsets[i], dy = y_offsets[i])
966                 {
967                         y = fy + dy;
968                         x = fx + dx;
969
970                         /* Skip illegal locations */
971                         if (!in_bounds(y, x)) continue;
972
973                         c_ptr = &cave[y][x];
974
975                         /* Skip locations in a wall */
976                         if (!monster_can_cross_terrain(c_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == p_ptr->riding) ? CEM_RIDING : 0)) continue;
977
978                         /* Check for "availability" (if monsters can flow) */
979                         if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
980                         {
981                                 /* Ignore grids very far from the player */
982                                 if (c_ptr->dist == 0) continue;
983
984                                 /* Ignore too-distant grids */
985                                 if (c_ptr->dist > cave[fy][fx].dist + 2 * d) continue;
986                         }
987
988                         /* Check for absence of shot (more or less) */
989                         if (!projectable(py, px, y, x))
990                         {
991                                 /* Calculate distance from player */
992                                 dis = distance(y, x, py, px);
993
994                                 /* Remember if further than previous */
995                                 if (dis > gdis)
996                                 {
997                                         gy = y;
998                                         gx = x;
999                                         gdis = dis;
1000                                 }
1001                         }
1002                 }
1003
1004                 /* Check for success */
1005                 if (gdis > 0)
1006                 {
1007                         /* Good location */
1008                         (*yp) = fy - gy;
1009                         (*xp) = fx - gx;
1010
1011                         /* Found safe place */
1012                         return (TRUE);
1013                 }
1014         }
1015
1016         /* No safe place */
1017         return (FALSE);
1018 }
1019
1020
1021 /*!
1022  * @brief モンスターが隠れ潜める地点を返す /
1023  * Choose a good hiding place near a monster for it to run toward.
1024  * @param m_idx モンスターの参照ID
1025  * @param yp 移動先のマスのY座標を返す参照ポインタ
1026  * @param xp 移動先のマスのX座標を返す参照ポインタ
1027  * @return 有効なマスがあった場合TRUEを返す
1028  * @details
1029  * Pack monsters will use this to "ambush" the player and lure him out\n
1030  * of corridors into open space so they can swarm him.\n
1031  *\n
1032  * Return TRUE if a good location is available.\n
1033  */
1034 static bool find_hiding(int m_idx, int *yp, int *xp)
1035 {
1036         monster_type *m_ptr = &m_list[m_idx];
1037         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1038
1039         int fy = m_ptr->fy;
1040         int fx = m_ptr->fx;
1041
1042         int y, x, dy, dx, d, dis, i;
1043         int gy = 0, gx = 0, gdis = 999;
1044
1045         sint *y_offsets, *x_offsets;
1046
1047         /* Start with adjacent locations, spread further */
1048         for (d = 1; d < 10; d++)
1049         {
1050                 /* Get the lists of points with a distance d from (fx, fy) */
1051                 y_offsets = dist_offsets_y[d];
1052                 x_offsets = dist_offsets_x[d];
1053
1054                 /* Check the locations */
1055                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
1056                      dx != 0 || dy != 0;
1057                      i++, dx = x_offsets[i], dy = y_offsets[i])
1058                 {
1059                         y = fy + dy;
1060                         x = fx + dx;
1061
1062                         /* Skip illegal locations */
1063                         if (!in_bounds(y, x)) continue;
1064
1065                         /* Skip occupied locations */
1066                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
1067
1068                         /* Check for hidden, available grid */
1069                         if (!projectable(py, px, y, x) && clean_shot(fy, fx, y, x, FALSE))
1070                         {
1071                                 /* Calculate distance from player */
1072                                 dis = distance(y, x, py, px);
1073
1074                                 /* Remember if closer than previous */
1075                                 if (dis < gdis && dis >= 2)
1076                                 {
1077                                         gy = y;
1078                                         gx = x;
1079                                         gdis = dis;
1080                                 }
1081                         }
1082                 }
1083
1084                 /* Check for success */
1085                 if (gdis < 999)
1086                 {
1087                         /* Good location */
1088                         (*yp) = fy - gy;
1089                         (*xp) = fx - gx;
1090
1091                         /* Found good place */
1092                         return (TRUE);
1093                 }
1094         }
1095
1096         /* No good place */
1097         return (FALSE);
1098 }
1099
1100
1101 /*!
1102  * @brief モンスターの移動方向を返す /
1103  * Choose "logical" directions for monster movement
1104  * @param m_idx モンスターの参照ID
1105  * @param mm 移動方向を返す方向IDの参照ポインタ
1106  * @return 有効方向があった場合TRUEを返す
1107  */
1108 static bool get_moves(int m_idx, int *mm)
1109 {
1110         monster_type *m_ptr = &m_list[m_idx];
1111         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1112         int          y, ay, x, ax;
1113         int          move_val = 0;
1114         int          y2 = py;
1115         int          x2 = px;
1116         bool         done = FALSE;
1117         bool         will_run = mon_will_run(m_idx);
1118         cave_type    *c_ptr;
1119         bool         no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (cave[m_ptr->fy][m_ptr->fx].cost > 2));
1120         bool         can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall));
1121
1122         /* Counter attack to an enemy monster */
1123         if (!will_run && m_ptr->target_y)
1124         {
1125                 int t_m_idx = cave[m_ptr->target_y][m_ptr->target_x].m_idx;
1126
1127                 /* The monster must be an enemy, and in LOS */
1128                 if (t_m_idx &&
1129                     are_enemies(m_ptr, &m_list[t_m_idx]) &&
1130                     los(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
1131                     projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1132                 {
1133                         /* Extract the "pseudo-direction" */
1134                         y = m_ptr->fy - m_ptr->target_y;
1135                         x = m_ptr->fx - m_ptr->target_x;
1136                         done = TRUE;
1137                 }
1138         }
1139
1140         if (!done && !will_run && is_hostile(m_ptr) &&
1141             (r_ptr->flags1 & RF1_FRIENDS) &&
1142             ((los(m_ptr->fy, m_ptr->fx, py, px) && projectable(m_ptr->fy, m_ptr->fx, py, px)) ||
1143             (cave[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
1144         {
1145         /*
1146          * Animal packs try to get the player out of corridors
1147          * (...unless they can move through walls -- TY)
1148          */
1149                 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
1150                          !(r_ptr->flags2 & RF2_KILL_WALL))
1151                 {
1152                         int i, room = 0;
1153
1154                         /* Count room grids next to player */
1155                         for (i = 0; i < 8; i++)
1156                         {
1157                                 int xx = px + ddx_ddd[i];
1158                                 int yy = py + ddy_ddd[i];
1159
1160                                 if (!in_bounds2(yy, xx)) continue;
1161
1162                                 c_ptr = &cave[yy][xx];
1163
1164                                 /* Check grid */
1165                                 if (monster_can_cross_terrain(c_ptr->feat, r_ptr, 0))
1166                                 {
1167                                         /* One more room grid */
1168                                         room++;
1169                                 }
1170                         }
1171                         if (cave[py][px].info & CAVE_ROOM) room -= 2;
1172                         if (!r_ptr->flags4 && !r_ptr->flags5 && !r_ptr->flags6) room -= 2;
1173
1174                         /* Not in a room and strong player */
1175                         if (room < (8 * (p_ptr->chp + p_ptr->csp)) /
1176                             (p_ptr->mhp + p_ptr->msp))
1177                         {
1178                                 /* Find hiding place */
1179                                 if (find_hiding(m_idx, &y, &x)) done = TRUE;
1180                         }
1181                 }
1182
1183                 /* Monster groups try to surround the player */
1184                 if (!done && (cave[m_ptr->fy][m_ptr->fx].dist < 3))
1185                 {
1186                         int i;
1187
1188                         /* Find an empty square near the player to fill */
1189                         for (i = 0; i < 8; i++)
1190                         {
1191                                 /* Pick squares near player (semi-randomly) */
1192                                 y2 = py + ddy_ddd[(m_idx + i) & 7];
1193                                 x2 = px + ddx_ddd[(m_idx + i) & 7];
1194
1195                                 /* Already there? */
1196                                 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
1197                                 {
1198                                         /* Attack the player */
1199                                         y2 = py;
1200                                         x2 = px;
1201
1202                                         break;
1203                                 }
1204
1205                                 if (!in_bounds2(y2, x2)) continue;
1206
1207                                 /* Ignore filled grids */
1208                                 if (!monster_can_enter(y2, x2, r_ptr, 0)) continue;
1209
1210                                 /* Try to fill this hole */
1211                                 break;
1212                         }
1213
1214                         /* Extract the new "pseudo-direction" */
1215                         y = m_ptr->fy - y2;
1216                         x = m_ptr->fx - x2;
1217
1218                         /* Done */
1219                         done = TRUE;
1220                 }
1221         }
1222
1223         if (!done)
1224         {
1225                 /* Flow towards the player */
1226                 (void)get_moves_aux(m_idx, &y2, &x2, no_flow);
1227
1228                 /* Extract the "pseudo-direction" */
1229                 y = m_ptr->fy - y2;
1230                 x = m_ptr->fx - x2;
1231
1232                 /* Not done */
1233         }
1234
1235         /* Apply fear if possible and necessary */
1236         if (is_pet(m_ptr) && will_run)
1237         {
1238                 /* XXX XXX Not very "smart" */
1239                 y = (-y), x = (-x);
1240         }
1241         else
1242         {
1243                 if (!done && will_run)
1244                 {
1245                         int tmp_x = (-x);
1246                         int tmp_y = (-y);
1247
1248                         /* Try to find safe place */
1249                         if (find_safety(m_idx, &y, &x))
1250                         {
1251                                 /* Attempt to avoid the player */
1252                                 if (!no_flow)
1253                                 {
1254                                         /* Adjust movement */
1255                                         if (get_fear_moves_aux(m_idx, &y, &x)) done = TRUE;
1256                                 }
1257                         }
1258
1259                         if (!done)
1260                         {
1261                                 /* This is not a very "smart" method XXX XXX */
1262                                 y = tmp_y;
1263                                 x = tmp_x;
1264                         }
1265                 }
1266         }
1267
1268
1269         /* Check for no move */
1270         if (!x && !y) return (FALSE);
1271
1272
1273         /* Extract the "absolute distances" */
1274         ax = ABS(x);
1275         ay = ABS(y);
1276
1277         /* Do something weird */
1278         if (y < 0) move_val += 8;
1279         if (x > 0) move_val += 4;
1280
1281         /* Prevent the diamond maneuvre */
1282         if (ay > (ax << 1)) move_val += 2;
1283         else if (ax > (ay << 1)) move_val++;
1284
1285         /* Extract some directions */
1286         switch (move_val)
1287         {
1288         case 0:
1289                 mm[0] = 9;
1290                 if (ay > ax)
1291                 {
1292                         mm[1] = 8;
1293                         mm[2] = 6;
1294                         mm[3] = 7;
1295                         mm[4] = 3;
1296                 }
1297                 else
1298                 {
1299                         mm[1] = 6;
1300                         mm[2] = 8;
1301                         mm[3] = 3;
1302                         mm[4] = 7;
1303                 }
1304                 break;
1305         case 1:
1306         case 9:
1307                 mm[0] = 6;
1308                 if (y < 0)
1309                 {
1310                         mm[1] = 3;
1311                         mm[2] = 9;
1312                         mm[3] = 2;
1313                         mm[4] = 8;
1314                 }
1315                 else
1316                 {
1317                         mm[1] = 9;
1318                         mm[2] = 3;
1319                         mm[3] = 8;
1320                         mm[4] = 2;
1321                 }
1322                 break;
1323         case 2:
1324         case 6:
1325                 mm[0] = 8;
1326                 if (x < 0)
1327                 {
1328                         mm[1] = 9;
1329                         mm[2] = 7;
1330                         mm[3] = 6;
1331                         mm[4] = 4;
1332                 }
1333                 else
1334                 {
1335                         mm[1] = 7;
1336                         mm[2] = 9;
1337                         mm[3] = 4;
1338                         mm[4] = 6;
1339                 }
1340                 break;
1341         case 4:
1342                 mm[0] = 7;
1343                 if (ay > ax)
1344                 {
1345                         mm[1] = 8;
1346                         mm[2] = 4;
1347                         mm[3] = 9;
1348                         mm[4] = 1;
1349                 }
1350                 else
1351                 {
1352                         mm[1] = 4;
1353                         mm[2] = 8;
1354                         mm[3] = 1;
1355                         mm[4] = 9;
1356                 }
1357                 break;
1358         case 5:
1359         case 13:
1360                 mm[0] = 4;
1361                 if (y < 0)
1362                 {
1363                         mm[1] = 1;
1364                         mm[2] = 7;
1365                         mm[3] = 2;
1366                         mm[4] = 8;
1367                 }
1368                 else
1369                 {
1370                         mm[1] = 7;
1371                         mm[2] = 1;
1372                         mm[3] = 8;
1373                         mm[4] = 2;
1374                 }
1375                 break;
1376         case 8:
1377                 mm[0] = 3;
1378                 if (ay > ax)
1379                 {
1380                         mm[1] = 2;
1381                         mm[2] = 6;
1382                         mm[3] = 1;
1383                         mm[4] = 9;
1384                 }
1385                 else
1386                 {
1387                         mm[1] = 6;
1388                         mm[2] = 2;
1389                         mm[3] = 9;
1390                         mm[4] = 1;
1391                 }
1392                 break;
1393         case 10:
1394         case 14:
1395                 mm[0] = 2;
1396                 if (x < 0)
1397                 {
1398                         mm[1] = 3;
1399                         mm[2] = 1;
1400                         mm[3] = 6;
1401                         mm[4] = 4;
1402                 }
1403                 else
1404                 {
1405                         mm[1] = 1;
1406                         mm[2] = 3;
1407                         mm[3] = 4;
1408                         mm[4] = 6;
1409                 }
1410                 break;
1411         case 12:
1412                 mm[0] = 1;
1413                 if (ay > ax)
1414                 {
1415                         mm[1] = 2;
1416                         mm[2] = 4;
1417                         mm[3] = 3;
1418                         mm[4] = 7;
1419                 }
1420                 else
1421                 {
1422                         mm[1] = 4;
1423                         mm[2] = 2;
1424                         mm[3] = 7;
1425                         mm[4] = 3;
1426                 }
1427                 break;
1428         }
1429
1430         /* Wants to move... */
1431         return (TRUE);
1432 }
1433
1434
1435 /*!
1436  * @brief モンスターから敵モンスターへの命中判定
1437  * @param power 打撃属性による基本命中値
1438  * @param level 攻撃側モンスターのレベル
1439  * @param ac 目標モンスターのAC
1440  * @param stun 攻撃側モンスターが朦朧状態ならTRUEを返す
1441  * @return 命中ならばTRUEを返す
1442  */
1443 static int check_hit2(int power, int level, int ac, int stun)
1444 {
1445         int i, k;
1446
1447         /* Percentile dice */
1448         k = randint0(100);
1449
1450         if (stun && one_in_(2)) return FALSE;
1451
1452         /* Hack -- Always miss or hit */
1453         if (k < 10) return (k < 5);
1454
1455         /* Calculate the "attack quality" */
1456         i = (power + (level * 3));
1457
1458         /* Power and Level compete against Armor */
1459         if ((i > 0) && (randint1(i) > ((ac * 3) / 4))) return (TRUE);
1460
1461         /* Assume miss */
1462         return (FALSE);
1463 }
1464
1465
1466 #define BLOW_EFFECT_TYPE_NONE  0
1467 #define BLOW_EFFECT_TYPE_FEAR  1
1468 #define BLOW_EFFECT_TYPE_SLEEP 2
1469 #define BLOW_EFFECT_TYPE_HEAL  3
1470
1471
1472 /*!
1473  * @brief モンスターから敵モンスターへの打撃攻撃処理
1474  * @param m_idx 攻撃側モンスターの参照ID
1475  * @param t_idx 目標側モンスターの参照ID
1476  * @return 実際に打撃処理が行われた場合TRUEを返す
1477  */
1478 static bool monst_attack_monst(int m_idx, int t_idx)
1479 {
1480         monster_type    *m_ptr = &m_list[m_idx];
1481         monster_type    *t_ptr = &m_list[t_idx];
1482
1483         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
1484         monster_race    *tr_ptr = &r_info[t_ptr->r_idx];
1485
1486         int             ap_cnt;
1487         int             ac, rlev, pt;
1488         char            m_name[80], t_name[80];
1489         char            temp[MAX_NLEN];
1490         bool            blinked;
1491         bool            explode = FALSE, touched = FALSE, fear = FALSE;
1492         int             y_saver = t_ptr->fy;
1493         int             x_saver = t_ptr->fx;
1494         int             effect_type;
1495
1496         bool see_m = is_seen(m_ptr);
1497         bool see_t = is_seen(t_ptr);
1498         bool see_either = see_m || see_t;
1499
1500         /* Can the player be aware of this attack? */
1501         bool known = (m_ptr->cdis <= MAX_SIGHT) || (t_ptr->cdis <= MAX_SIGHT);
1502         bool do_silly_attack = (one_in_(2) && p_ptr->image);
1503
1504         /* Cannot attack self */
1505         if (m_idx == t_idx) return FALSE;
1506
1507         /* Not allowed to attack */
1508         if (r_ptr->flags1 & RF1_NEVER_BLOW) return FALSE;
1509
1510         if (d_info[dungeon_type].flags1 & DF1_NO_MELEE) return (FALSE);
1511
1512         /* Total armor */
1513         ac = tr_ptr->ac;
1514
1515         /* Extract the effective monster level */
1516         rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1517
1518         /* Get the monster name (or "it") */
1519         monster_desc(m_name, m_ptr, 0);
1520
1521         /* Get the monster name (or "it") */
1522         monster_desc(t_name, t_ptr, 0);
1523
1524         /* Assume no blink */
1525         blinked = FALSE;
1526
1527         if (!see_either && known)
1528         {
1529                 mon_fight = TRUE;
1530         }
1531
1532         if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(1, 1);
1533
1534         /* Scan through all four blows */
1535         for (ap_cnt = 0; ap_cnt < 4; ap_cnt++)
1536         {
1537                 bool obvious = FALSE;
1538
1539                 int power = 0;
1540                 int damage = 0;
1541
1542                 cptr act = NULL;
1543
1544                 /* Extract the attack infomation */
1545                 int effect = r_ptr->blow[ap_cnt].effect;
1546                 int method = r_ptr->blow[ap_cnt].method;
1547                 int d_dice = r_ptr->blow[ap_cnt].d_dice;
1548                 int d_side = r_ptr->blow[ap_cnt].d_side;
1549
1550                 if (!m_ptr->r_idx) break;
1551
1552                 /* Stop attacking if the target dies! */
1553                 if (t_ptr->fx != x_saver || t_ptr->fy != y_saver)
1554                         break;
1555
1556                 /* Hack -- no more attacks */
1557                 if (!method) break;
1558
1559                 if (method == RBM_SHOOT) continue;
1560
1561                 /* Extract the attack "power" */
1562                 power = mbe_info[effect].power;
1563
1564                 /* Monster hits */
1565                 if (!effect || check_hit2(power, rlev, ac, MON_STUNNED(m_ptr)))
1566                 {
1567                         /* Wake it up */
1568                         (void)set_monster_csleep(t_idx, 0);
1569
1570                         if (t_ptr->ml)
1571                         {
1572                                 /* Redraw the health bar */
1573                                 if (p_ptr->health_who == t_idx) p_ptr->redraw |= (PR_HEALTH);
1574                                 if (p_ptr->riding == t_idx) p_ptr->redraw |= (PR_UHEALTH);
1575                         }
1576
1577                         /* Describe the attack method */
1578                         switch (method)
1579                         {
1580                         case RBM_HIT:
1581                                 {
1582 #ifdef JP
1583 act = "%sを殴った。";
1584 #else
1585                                         act = "hits %s.";
1586 #endif
1587
1588                                         touched = TRUE;
1589                                         break;
1590                                 }
1591
1592                         case RBM_TOUCH:
1593                                 {
1594 #ifdef JP
1595 act = "%sを触った。";
1596 #else
1597                                         act = "touches %s.";
1598 #endif
1599
1600                                         touched = TRUE;
1601                                         break;
1602                                 }
1603
1604                         case RBM_PUNCH:
1605                                 {
1606 #ifdef JP
1607 act = "%sをパンチした。";
1608 #else
1609                                         act = "punches %s.";
1610 #endif
1611
1612                                         touched = TRUE;
1613                                         break;
1614                                 }
1615
1616                         case RBM_KICK:
1617                                 {
1618 #ifdef JP
1619 act = "%sを蹴った。";
1620 #else
1621                                         act = "kicks %s.";
1622 #endif
1623
1624                                         touched = TRUE;
1625                                         break;
1626                                 }
1627
1628                         case RBM_CLAW:
1629                                 {
1630 #ifdef JP
1631 act = "%sをひっかいた。";
1632 #else
1633                                         act = "claws %s.";
1634 #endif
1635
1636                                         touched = TRUE;
1637                                         break;
1638                                 }
1639
1640                         case RBM_BITE:
1641                                 {
1642 #ifdef JP
1643 act = "%sを噛んだ。";
1644 #else
1645                                         act = "bites %s.";
1646 #endif
1647
1648                                         touched = TRUE;
1649                                         break;
1650                                 }
1651
1652                         case RBM_STING:
1653                                 {
1654 #ifdef JP
1655 act = "%sを刺した。";
1656 #else
1657                                         act = "stings %s.";
1658 #endif
1659
1660                                         touched = TRUE;
1661                                         break;
1662                                 }
1663
1664                         case RBM_SLASH:
1665                                 {
1666 #ifdef JP
1667 act = "%sを斬った。";
1668 #else
1669                                         act = "slashes %s.";
1670 #endif
1671
1672                                         break;
1673                                 }
1674
1675                         case RBM_BUTT:
1676                                 {
1677 #ifdef JP
1678 act = "%sを角で突いた。";
1679 #else
1680                                         act = "butts %s.";
1681 #endif
1682
1683                                         touched = TRUE;
1684                                         break;
1685                                 }
1686
1687                         case RBM_CRUSH:
1688                                 {
1689 #ifdef JP
1690 act = "%sに体当りした。";
1691 #else
1692                                         act = "crushes %s.";
1693 #endif
1694
1695                                         touched = TRUE;
1696                                         break;
1697                                 }
1698
1699                         case RBM_ENGULF:
1700                                 {
1701 #ifdef JP
1702 act = "%sを飲み込んだ。";
1703 #else
1704                                         act = "engulfs %s.";
1705 #endif
1706
1707                                         touched = TRUE;
1708                                         break;
1709                                 }
1710
1711                         case RBM_CHARGE:
1712                                 {
1713 #ifdef JP
1714 act = "%sに請求書をよこした。";
1715 #else
1716                                         act = "charges %s.";
1717 #endif
1718
1719                                         touched = TRUE;
1720                                         break;
1721                                 }
1722
1723                         case RBM_CRAWL:
1724                                 {
1725 #ifdef JP
1726 act = "%sの体の上を這い回った。";
1727 #else
1728                                         act = "crawls on %s.";
1729 #endif
1730
1731                                         touched = TRUE;
1732                                         break;
1733                                 }
1734
1735                         case RBM_DROOL:
1736                                 {
1737 #ifdef JP
1738 act = "%sによだれをたらした。";
1739 #else
1740                                         act = "drools on %s.";
1741 #endif
1742
1743                                         touched = FALSE;
1744                                         break;
1745                                 }
1746
1747                         case RBM_SPIT:
1748                                 {
1749 #ifdef JP
1750 act = "%sに唾を吐いた。";
1751 #else
1752                                         act = "spits on %s.";
1753 #endif
1754
1755                                         touched = FALSE;
1756                                         break;
1757                                 }
1758
1759                         case RBM_EXPLODE:
1760                                 {
1761                                         if (see_either) disturb(1, 1);
1762 #ifdef JP
1763 act = "爆発した。";
1764 #else
1765                                         act = "explodes.";
1766 #endif
1767
1768                                         explode = TRUE;
1769                                         touched = FALSE;
1770                                         break;
1771                                 }
1772
1773                         case RBM_GAZE:
1774                                 {
1775 #ifdef JP
1776 act = "%sをにらんだ。";
1777 #else
1778                                         act = "gazes at %s.";
1779 #endif
1780
1781                                         touched = FALSE;
1782                                         break;
1783                                 }
1784
1785                         case RBM_WAIL:
1786                                 {
1787 #ifdef JP
1788 act = "%sに泣きついた。";
1789 #else
1790                                         act = "wails at %s.";
1791 #endif
1792
1793                                         touched = FALSE;
1794                                         break;
1795                                 }
1796
1797                         case RBM_SPORE:
1798                                 {
1799 #ifdef JP
1800 act = "%sに胞子を飛ばした。";
1801 #else
1802                                         act = "releases spores at %s.";
1803 #endif
1804
1805                                         touched = FALSE;
1806                                         break;
1807                                 }
1808
1809                         case RBM_XXX4:
1810                                 {
1811 #ifdef JP
1812 act = "%sにXXX4を飛ばした。";
1813 #else
1814                                         act = "projects XXX4's at %s.";
1815 #endif
1816
1817                                         touched = FALSE;
1818                                         break;
1819                                 }
1820
1821                         case RBM_BEG:
1822                                 {
1823 #ifdef JP
1824 act = "%sに金をせがんだ。";
1825 #else
1826                                         act = "begs %s for money.";
1827 #endif
1828
1829                                         touched = FALSE;
1830                                         break;
1831                                 }
1832
1833                         case RBM_INSULT:
1834                                 {
1835 #ifdef JP
1836 act = "%sを侮辱した。";
1837 #else
1838                                         act = "insults %s.";
1839 #endif
1840
1841                                         touched = FALSE;
1842                                         break;
1843                                 }
1844
1845                         case RBM_MOAN:
1846                                 {
1847 #ifdef JP
1848 act = "%sにむかってうめいた。";
1849 #else
1850                                         act = "moans at %s.";
1851 #endif
1852
1853                                         touched = FALSE;
1854                                         break;
1855                                 }
1856
1857                         case RBM_SHOW:
1858                                 {
1859 #ifdef JP
1860 act = "%sにむかって歌った。";
1861 #else
1862                                         act = "sings to %s.";
1863 #endif
1864
1865                                         touched = FALSE;
1866                                         break;
1867                                 }
1868                         }
1869
1870                         /* Message */
1871                         if (act && see_either)
1872                         {
1873 #ifdef JP
1874                                 if (do_silly_attack) act = silly_attacks2[randint0(MAX_SILLY_ATTACK)];
1875                                 strfmt(temp, act, t_name);
1876                                 msg_format("%^sは%s", m_name, temp);
1877 #else
1878                                 if (do_silly_attack)
1879                                 {
1880                                         act = silly_attacks[randint0(MAX_SILLY_ATTACK)];
1881                                         strfmt(temp, "%s %s.", act, t_name);
1882                                 }
1883                                 else strfmt(temp, act, t_name);
1884                                 msg_format("%^s %s", m_name, temp);
1885 #endif
1886                         }
1887
1888                         /* Hack -- assume all attacks are obvious */
1889                         obvious = TRUE;
1890
1891                         /* Roll out the damage */
1892                         damage = damroll(d_dice, d_side);
1893
1894                         /* Assume no effect */
1895                         effect_type = BLOW_EFFECT_TYPE_NONE;
1896
1897                         pt = GF_MISSILE;
1898
1899                         /* Apply appropriate damage */
1900                         switch (effect)
1901                         {
1902                         case 0:
1903                         case RBE_DR_MANA:
1904                                 damage = pt = 0;
1905                                 break;
1906
1907                         case RBE_SUPERHURT:
1908                                 if ((randint1(rlev*2+250) > (ac+200)) || one_in_(13))
1909                                 {
1910                                         int tmp_damage = damage - (damage * ((ac < 150) ? ac : 150) / 250);
1911                                         damage = MAX(damage, tmp_damage * 2);
1912                                         break;
1913                                 }
1914
1915                                 /* Fall through */
1916
1917                         case RBE_HURT:
1918                                 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1919                                 break;
1920
1921                         case RBE_POISON:
1922                         case RBE_DISEASE:
1923                                 pt = GF_POIS;
1924                                 break;
1925
1926                         case RBE_UN_BONUS:
1927                         case RBE_UN_POWER:
1928                                 pt = GF_DISENCHANT;
1929                                 break;
1930
1931                         case RBE_EAT_ITEM:
1932                         case RBE_EAT_GOLD:
1933                                 if ((p_ptr->riding != m_idx) && one_in_(2)) blinked = TRUE;
1934                                 break;
1935
1936                         case RBE_EAT_FOOD:
1937                         case RBE_EAT_LITE:
1938                         case RBE_BLIND:
1939                         case RBE_LOSE_STR:
1940                         case RBE_LOSE_INT:
1941                         case RBE_LOSE_WIS:
1942                         case RBE_LOSE_DEX:
1943                         case RBE_LOSE_CON:
1944                         case RBE_LOSE_CHR:
1945                         case RBE_LOSE_ALL:
1946                                 break;
1947
1948                         case RBE_ACID:
1949                                 pt = GF_ACID;
1950                                 break;
1951
1952                         case RBE_ELEC:
1953                                 pt = GF_ELEC;
1954                                 break;
1955
1956                         case RBE_FIRE:
1957                                 pt = GF_FIRE;
1958                                 break;
1959
1960                         case RBE_COLD:
1961                                 pt = GF_COLD;
1962                                 break;
1963
1964                         case RBE_CONFUSE:
1965                                 pt = GF_CONFUSION;
1966                                 break;
1967
1968                         case RBE_TERRIFY:
1969                                 effect_type = BLOW_EFFECT_TYPE_FEAR;
1970                                 break;
1971
1972                         case RBE_PARALYZE:
1973                                 effect_type = BLOW_EFFECT_TYPE_SLEEP;
1974                                 break;
1975
1976                         case RBE_SHATTER:
1977                                 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1978                                 if (damage > 23) earthquake_aux(m_ptr->fy, m_ptr->fx, 8, m_idx);
1979                                 break;
1980
1981                         case RBE_EXP_10:
1982                         case RBE_EXP_20:
1983                         case RBE_EXP_40:
1984                         case RBE_EXP_80:
1985                                 pt = GF_NETHER;
1986                                 break;
1987
1988                         case RBE_TIME:
1989                                 pt = GF_TIME;
1990                                 break;
1991
1992                         case RBE_DR_LIFE:
1993                                 pt = GF_OLD_DRAIN;
1994                                 effect_type = BLOW_EFFECT_TYPE_HEAL;
1995                                 break;
1996
1997                         case RBE_INERTIA:
1998                                 pt = GF_INERTIA;
1999                                 break;
2000
2001                         case RBE_STUN:
2002                                 pt = GF_SOUND;
2003                                 break;
2004
2005                         default:
2006                                 pt = 0;
2007                                 break;
2008                         }
2009
2010                         if (pt)
2011                         {
2012                                 /* Do damage if not exploding */
2013                                 if (!explode)
2014                                 {
2015                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
2016                                                 damage, pt, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
2017                                 }
2018
2019                                 switch (effect_type)
2020                                 {
2021                                 case BLOW_EFFECT_TYPE_FEAR:
2022                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
2023                                                 damage, GF_TURN_ALL, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
2024                                         break;
2025
2026                                 case BLOW_EFFECT_TYPE_SLEEP:
2027                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
2028                                                 r_ptr->level, GF_OLD_SLEEP, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
2029                                         break;
2030
2031                                 case BLOW_EFFECT_TYPE_HEAL:
2032                                         if ((monster_living(tr_ptr)) && (damage > 2))
2033                                         {
2034                                                 bool did_heal = FALSE;
2035
2036                                                 if (m_ptr->hp < m_ptr->maxhp) did_heal = TRUE;
2037
2038                                                 /* Heal */
2039                                                 m_ptr->hp += damroll(4, damage / 6);
2040                                                 if (m_ptr->hp > m_ptr->maxhp) m_ptr->hp = m_ptr->maxhp;
2041
2042                                                 /* Redraw (later) if needed */
2043                                                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2044                                                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2045
2046                                                 /* Special message */
2047                                                 if (see_m && did_heal)
2048                                                 {
2049 #ifdef JP
2050                                                         msg_format("%sは体力を回復したようだ。", m_name);
2051 #else
2052                                                         msg_format("%^s appears healthier.", m_name);
2053 #endif
2054                                                 }
2055                                         }
2056                                         break;
2057                                 }
2058
2059                                 if (touched)
2060                                 {
2061                                         /* Aura fire */
2062                                         if ((tr_ptr->flags2 & RF2_AURA_FIRE) && m_ptr->r_idx)
2063                                         {
2064                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK))
2065                                                 {
2066                                                         if (see_either)
2067                                                         {
2068 #ifdef JP
2069                                                                 msg_format("%^sは突然熱くなった!", m_name);
2070 #else
2071                                                                 msg_format("%^s is suddenly very hot!", m_name);
2072 #endif
2073                                                         }
2074                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_FIRE;
2075                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
2076                                                                 damroll (1 + ((tr_ptr->level) / 26),
2077                                                                 1 + ((tr_ptr->level) / 17)),
2078                                                                 GF_FIRE, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
2079                                                 }
2080                                                 else
2081                                                 {
2082                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK);
2083                                                 }
2084                                         }
2085
2086                                         /* Aura cold */
2087                                         if ((tr_ptr->flags3 & RF3_AURA_COLD) && m_ptr->r_idx)
2088                                         {
2089                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK))
2090                                                 {
2091                                                         if (see_either)
2092                                                         {
2093 #ifdef JP
2094                                                                 msg_format("%^sは突然寒くなった!", m_name);
2095 #else
2096                                                                 msg_format("%^s is suddenly very cold!", m_name);
2097 #endif
2098                                                         }
2099                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags3 |= RF3_AURA_COLD;
2100                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
2101                                                                 damroll (1 + ((tr_ptr->level) / 26),
2102                                                                 1 + ((tr_ptr->level) / 17)),
2103                                                                 GF_COLD, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
2104                                                 }
2105                                                 else
2106                                                 {
2107                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_COLD_MASK);
2108                                                 }
2109                                         }
2110
2111                                         /* Aura elec */
2112                                         if ((tr_ptr->flags2 & RF2_AURA_ELEC) && m_ptr->r_idx)
2113                                         {
2114                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK))
2115                                                 {
2116                                                         if (see_either)
2117                                                         {
2118 #ifdef JP
2119                                                                 msg_format("%^sは電撃を食らった!", m_name);
2120 #else
2121                                                                 msg_format("%^s gets zapped!", m_name);
2122 #endif
2123                                                         }
2124                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_ELEC;
2125                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
2126                                                                 damroll (1 + ((tr_ptr->level) / 26),
2127                                                                 1 + ((tr_ptr->level) / 17)),
2128                                                                 GF_ELEC, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
2129                                                 }
2130                                                 else
2131                                                 {
2132                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK);
2133                                                 }
2134                                         }
2135                                 }
2136                         }
2137                 }
2138
2139                 /* Monster missed player */
2140                 else
2141                 {
2142                         /* Analyze failed attacks */
2143                         switch (method)
2144                         {
2145                         case RBM_HIT:
2146                         case RBM_TOUCH:
2147                         case RBM_PUNCH:
2148                         case RBM_KICK:
2149                         case RBM_CLAW:
2150                         case RBM_BITE:
2151                         case RBM_STING:
2152                         case RBM_SLASH:
2153                         case RBM_BUTT:
2154                         case RBM_CRUSH:
2155                         case RBM_ENGULF:
2156                         case RBM_CHARGE:
2157                                 {
2158                                         /* Wake it up */
2159                                         (void)set_monster_csleep(t_idx, 0);
2160
2161                                         /* Visible monsters */
2162                                         if (see_m)
2163                                         {
2164                                                 /* Message */
2165 #ifdef JP
2166                                                 msg_format("%sは%^sの攻撃をかわした。", t_name,m_name);
2167 #else
2168                                                 msg_format("%^s misses %s.", m_name, t_name);
2169 #endif
2170                                         }
2171
2172                                         break;
2173                                 }
2174                         }
2175                 }
2176
2177
2178                 /* Analyze "visible" monsters only */
2179                 if (is_original_ap_and_seen(m_ptr) && !do_silly_attack)
2180                 {
2181                         /* Count "obvious" attacks (and ones that cause damage) */
2182                         if (obvious || damage || (r_ptr->r_blows[ap_cnt] > 10))
2183                         {
2184                                 /* Count attacks of this type */
2185                                 if (r_ptr->r_blows[ap_cnt] < MAX_UCHAR)
2186                                 {
2187                                         r_ptr->r_blows[ap_cnt]++;
2188                                 }
2189                         }
2190                 }
2191         }
2192
2193         if (explode)
2194         {
2195                 sound(SOUND_EXPLODE);
2196
2197                 /* Cancel Invulnerability */
2198                 (void)set_monster_invulner(m_idx, 0, FALSE);
2199
2200 #ifdef JP
2201                 mon_take_hit_mon(m_idx, m_ptr->hp + 1, &fear, "は爆発して粉々になった。", m_idx);
2202 #else
2203                 mon_take_hit_mon(m_idx, m_ptr->hp + 1, &fear, " explodes into tiny shreds.", m_idx);
2204 #endif
2205
2206                 blinked = FALSE;
2207         }
2208
2209         /* Blink away */
2210         if (blinked && m_ptr->r_idx)
2211         {
2212                 if (teleport_barrier(m_idx))
2213                 {
2214                         if (see_m)
2215                         {
2216 #ifdef JP
2217                                 msg_print("泥棒は笑って逃げ...ようとしたがバリアに防がれた。");
2218 #else
2219                                 msg_print("The thief flees laughing...? But magic barrier obstructs it.");
2220 #endif
2221                         }
2222                         else if (known)
2223                         {
2224                                 mon_fight = TRUE;
2225                         }
2226                 }
2227                 else
2228                 {
2229                         if (see_m)
2230                         {
2231 #ifdef JP
2232                                 msg_print("泥棒は笑って逃げた!");
2233 #else
2234                                 msg_print("The thief flees laughing!");
2235 #endif
2236                         }
2237                         else if (known)
2238                         {
2239                                 mon_fight = TRUE;
2240                         }
2241
2242                         teleport_away(m_idx, MAX_SIGHT * 2 + 5, 0L);
2243                 }
2244         }
2245
2246         return TRUE;
2247 }
2248
2249
2250 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
2251 {
2252         return !have_flag(f_ptr->flags, FF_GLASS) ||
2253                (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
2254                (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
2255 }
2256
2257
2258 /*!
2259  * @brief モンスター単体の1ターン行動処理メインルーチン /
2260  * Process a monster
2261  * @param m_idx 行動モンスターの参照ID
2262  * @return なし
2263  * @details
2264  * The monster is known to be within 100 grids of the player\n
2265  *\n
2266  * In several cases, we directly update the monster lore\n
2267  *\n
2268  * Note that a monster is only allowed to "reproduce" if there\n
2269  * are a limited number of "reproducing" monsters on the current\n
2270  * level.  This should prevent the level from being "swamped" by\n
2271  * reproducing monsters.  It also allows a large mass of mice to\n
2272  * prevent a louse from multiplying, but this is a small price to\n
2273  * pay for a simple multiplication method.\n
2274  *\n
2275  * XXX Monster fear is slightly odd, in particular, monsters will\n
2276  * fixate on opening a door even if they cannot open it.  Actually,\n
2277  * the same thing happens to normal monsters when they hit a door\n
2278  *\n
2279  * XXX XXX XXX In addition, monsters which *cannot* open or bash\n
2280  * down a door will still stand there trying to open it...\n
2281  *\n
2282  * XXX Technically, need to check for monster in the way\n
2283  * combined with that monster being in a wall (or door?)\n
2284  *\n
2285  * A "direction" of "5" means "pick a random direction".\n
2286  */
2287 static void process_monster(int m_idx)
2288 {
2289         monster_type    *m_ptr = &m_list[m_idx];
2290         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
2291         monster_race    *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2292
2293         int             i, d, oy, ox, ny, nx;
2294
2295         int             mm[8];
2296
2297         cave_type       *c_ptr;
2298         feature_type    *f_ptr;
2299
2300         monster_type    *y_ptr;
2301
2302         bool            do_turn;
2303         bool            do_move;
2304         bool            do_view;
2305         bool            must_alter_to_move;
2306
2307         bool            did_open_door;
2308         bool            did_bash_door;
2309         bool            did_take_item;
2310         bool            did_kill_item;
2311         bool            did_move_body;
2312         bool            did_pass_wall;
2313         bool            did_kill_wall;
2314         bool            gets_angry = FALSE;
2315         bool            can_cross;
2316         bool            aware = TRUE;
2317
2318         bool            fear;
2319
2320         bool            is_riding_mon = (m_idx == p_ptr->riding);
2321
2322         bool            see_m = is_seen(m_ptr);
2323
2324         if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
2325         {
2326                 if (rakuba(0, TRUE))
2327                 {
2328 #ifdef JP
2329                         msg_print("地面に落とされた。");
2330 #else
2331                         char m_name[80];
2332                         monster_desc(m_name, &m_list[p_ptr->riding], 0);
2333                         msg_format("You have fallen from %s.", m_name);
2334 #endif
2335                 }
2336         }
2337
2338         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
2339         {
2340                 choose_new_monster(m_idx, FALSE, 0);
2341                 r_ptr = &r_info[m_ptr->r_idx];
2342         }
2343
2344         /* Players hidden in shadow are almost imperceptable. -LM- */
2345         if (p_ptr->special_defense & NINJA_S_STEALTH)
2346         {
2347                 int tmp = p_ptr->lev*6+(p_ptr->skill_stl+10)*4;
2348                 if (p_ptr->monlite) tmp /= 3;
2349                 if (p_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
2350                 if (r_ptr->level > (p_ptr->lev*p_ptr->lev/20+10)) tmp /= 3;
2351                 /* Low-level monsters will find it difficult to locate the player. */
2352                 if (randint0(tmp) > (r_ptr->level+20)) aware = FALSE;
2353         }
2354
2355         /* Are there its parent? */
2356         if (m_ptr->parent_m_idx && !m_list[m_ptr->parent_m_idx].r_idx)
2357         {
2358                 /* Its parent have gone, it also goes away. */
2359
2360                 if (see_m)
2361                 {
2362                         char m_name[80];
2363
2364                         /* Acquire the monster name */
2365                         monster_desc(m_name, m_ptr, 0);
2366
2367 #ifdef JP
2368                         msg_format("%sは消え去った!", m_name);
2369 #else
2370                         msg_format("%^s disappears!", m_name);
2371 #endif
2372                 }
2373
2374                 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
2375                 {
2376                         char m_name[80];
2377
2378                         monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
2379                         do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
2380                 }
2381
2382                 /* Delete the monster */
2383                 delete_monster_idx(m_idx);
2384
2385                 return;
2386         }
2387
2388         /* Quantum monsters are odd */
2389         if (r_ptr->flags2 & (RF2_QUANTUM))
2390         {
2391                 /* Sometimes skip move */
2392                 if (!randint0(2)) return;
2393
2394                 /* Sometimes die */
2395                 if (!randint0((m_idx % 100) + 10) && !(r_ptr->flags1 & RF1_QUESTOR))
2396                 {
2397                         bool sad = FALSE;
2398
2399                         if (is_pet(m_ptr) && !(m_ptr->ml))
2400                                 sad = TRUE;
2401
2402                         if (see_m)
2403                         {
2404                                 char m_name[80];
2405
2406                                 /* Acquire the monster name */
2407                                 monster_desc(m_name, m_ptr, 0);
2408
2409                                 /* Oops */
2410 #ifdef JP
2411                                 msg_format("%sは消え去った!", m_name);
2412 #else
2413                                 msg_format("%^s disappears!", m_name);
2414 #endif
2415                         }
2416
2417                         /* Generate treasure, etc */
2418                         monster_death(m_idx, FALSE);
2419
2420                         /* Delete the monster */
2421                         delete_monster_idx(m_idx);
2422
2423                         if (sad)
2424                         {
2425 #ifdef JP
2426                                 msg_print("少しの間悲しい気分になった。");
2427 #else
2428                                 msg_print("You feel sad for a moment.");
2429 #endif
2430                         }
2431
2432                         return;
2433                 }
2434         }
2435
2436         if (m_ptr->r_idx == MON_SHURYUUDAN)
2437 #ifdef JP
2438                 mon_take_hit_mon(m_idx, 1, &fear, "は爆発して粉々になった。", m_idx);
2439 #else
2440                 mon_take_hit_mon(m_idx, 1, &fear, " explodes into tiny shreds.", m_idx);
2441 #endif
2442
2443         if ((is_pet(m_ptr) || is_friendly(m_ptr)) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && !p_ptr->inside_battle)
2444         {
2445                 static int riding_pinch = 0;
2446
2447                 if (m_ptr->hp < m_ptr->maxhp/3)
2448                 {
2449                         char m_name[80];
2450                         monster_desc(m_name, m_ptr, 0);
2451
2452                         if (is_riding_mon && riding_pinch < 2)
2453                         {
2454 #ifdef JP
2455                                 msg_format("%sは傷の痛さの余りあなたの束縛から逃れようとしている。", m_name);
2456 #else
2457                                 msg_format("%^s seems to be in so much pain, and trying to escape from your restriction.", m_name);
2458 #endif
2459                                 riding_pinch++;
2460                                 disturb(1, 1);
2461                         }
2462                         else
2463                         {
2464                                 if (is_riding_mon)
2465                                 {
2466 #ifdef JP
2467                                         msg_format("%sはあなたの束縛から脱出した。", m_name);
2468 #else
2469                                         msg_format("%^s succeeded to escape from your restriction!", m_name);
2470 #endif
2471                                         if (rakuba(-1, FALSE))
2472                                         {
2473 #ifdef JP
2474                                                 msg_print("地面に落とされた。");
2475 #else
2476                                                 msg_print("You have fallen from riding pet.");
2477 #endif
2478                                         }
2479                                 }
2480
2481                                 if (see_m)
2482                                 {
2483                                         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) &&
2484                                             player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(m_ptr->fy, m_ptr->fx, py, px))
2485                                         {
2486 #ifdef JP
2487                                                 msg_format("%^s「ピンチだ!退却させてもらう!」", m_name);
2488 #else
2489                                                 msg_format("%^s says 'It is the pinch! I will retreat'.", m_name);
2490 #endif
2491                                         }
2492 #ifdef JP
2493                                         msg_format("%^sがテレポート・レベルの巻物を読んだ。", m_name);
2494                                         msg_format("%^sが消え去った。", m_name);
2495 #else
2496                                         msg_format("%^s read a scroll of teleport level.", m_name);
2497                                         msg_format("%^s disappears.", m_name);
2498 #endif
2499                                 }
2500
2501                                 if (is_riding_mon && rakuba(-1, FALSE))
2502                                 {
2503 #ifdef JP
2504                                         msg_print("地面に落とされた。");
2505 #else
2506                                         msg_print("You have fallen from riding pet.");
2507 #endif
2508                                 }
2509
2510                                 /* Check for quest completion */
2511                                 check_quest_completion(m_ptr);
2512
2513                                 delete_monster_idx(m_idx);
2514
2515                                 return;
2516                         }
2517                 }
2518                 else
2519                 {
2520                         /* Reset the counter */
2521                         if (is_riding_mon) riding_pinch = 0;
2522                 }
2523         }
2524
2525         /* Handle "sleep" */
2526         if (MON_CSLEEP(m_ptr))
2527         {
2528                 /* Handle non-aggravation - Still sleeping */
2529                 if (!(p_ptr->cursed & TRC_AGGRAVATE)) return;
2530
2531                 /* Handle aggravation */
2532
2533                 /* Reset sleep counter */
2534                 (void)set_monster_csleep(m_idx, 0);
2535
2536                 /* Notice the "waking up" */
2537                 if (m_ptr->ml)
2538                 {
2539                         char m_name[80];
2540
2541                         /* Acquire the monster name */
2542                         monster_desc(m_name, m_ptr, 0);
2543
2544                         /* Dump a message */
2545 #ifdef JP
2546                         msg_format("%^sが目を覚ました。", m_name);
2547 #else
2548                         msg_format("%^s wakes up.", m_name);
2549 #endif
2550                 }
2551
2552                 /* Hack -- Count the wakings */
2553                 if (is_original_ap_and_seen(m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
2554                 {
2555                         r_ptr->r_wake++;
2556                 }
2557         }
2558
2559         /* Handle "stun" */
2560         if (MON_STUNNED(m_ptr))
2561         {
2562                 /* Sometimes skip move */
2563                 if (one_in_(2)) return;
2564         }
2565
2566         if (is_riding_mon)
2567         {
2568                 p_ptr->update |= (PU_BONUS);
2569         }
2570
2571         /* No one wants to be your friend if you're aggravating */
2572         if (is_friendly(m_ptr) && (p_ptr->cursed & TRC_AGGRAVATE))
2573                 gets_angry = TRUE;
2574
2575         /* Paranoia... no pet uniques outside wizard mode -- TY */
2576         if (is_pet(m_ptr) &&
2577             ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
2578               monster_has_hostile_align(NULL, 10, -10, r_ptr))
2579              || (r_ptr->flagsr & RFR_RES_ALL)))
2580         {
2581                 gets_angry = TRUE;
2582         }
2583
2584         if (p_ptr->inside_battle) gets_angry = FALSE;
2585
2586         if (gets_angry)
2587         {
2588                 if (is_pet(m_ptr) || see_m)
2589                 {
2590                         char m_name[80];
2591                         monster_desc(m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
2592 #ifdef JP
2593                         msg_format("%^sは突然敵にまわった!", m_name);
2594 #else
2595                         msg_format("%^s suddenly becomes hostile!", m_name);
2596 #endif
2597                 }
2598
2599                 set_hostile(m_ptr);
2600         }
2601
2602         /* Get the origin */
2603         oy = m_ptr->fy;
2604         ox = m_ptr->fx;
2605
2606
2607         /* Attempt to "multiply" if able and allowed */
2608         if ((r_ptr->flags2 & RF2_MULTIPLY) && (num_repro < MAX_REPRO))
2609         {
2610                 int k, y, x;
2611
2612                 /* Count the adjacent monsters */
2613                 for (k = 0, y = oy - 1; y <= oy + 1; y++)
2614                 {
2615                         for (x = ox - 1; x <= ox + 1; x++)
2616                         {
2617                                 /* Ignore locations off of edge */
2618                                 if (!in_bounds2(y, x)) continue;
2619
2620                                 if (cave[y][x].m_idx) k++;
2621                         }
2622                 }
2623
2624                 /* Hex */
2625                 if (multiply_barrier(m_idx)) k = 8;
2626
2627                 /* Hack -- multiply slower in crowded areas */
2628                 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2629                 {
2630                         /* Try to multiply */
2631                         if (multiply_monster(m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
2632                         {
2633                                 /* Take note if visible */
2634                                 if (m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(m_ptr))
2635                                 {
2636                                         r_ptr->r_flags2 |= (RF2_MULTIPLY);
2637                                 }
2638
2639                                 /* Multiplying takes energy */
2640                                 return;
2641                         }
2642                 }
2643         }
2644
2645
2646         if (r_ptr->flags6 & RF6_SPECIAL)
2647         {
2648                 /* Hack -- Ohmu scatters molds! */
2649                 if (m_ptr->r_idx == MON_OHMU)
2650                 {
2651                         if (!p_ptr->inside_arena && !p_ptr->inside_battle)
2652                         {
2653                                 if (r_ptr->freq_spell && (randint1(100) <= r_ptr->freq_spell))
2654                                 {
2655                                         int  k, count = 0;
2656                                         int  rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
2657                                         u32b p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
2658
2659                                         for (k = 0; k < 6; k++)
2660                                         {
2661                                                 if (summon_specific(m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_BIZARRE1, (PM_ALLOW_GROUP | p_mode)))
2662                                                 {
2663                                                         if (m_list[hack_m_idx_ii].ml) count++;
2664                                                 }
2665                                         }
2666
2667                                         if (count && is_original_ap_and_seen(m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
2668                                 }
2669                         }
2670                 }
2671         }
2672
2673
2674         if (!p_ptr->inside_battle)
2675         {
2676                 /* Hack! "Cyber" monster makes noise... */
2677                 if (m_ptr->ap_r_idx == MON_CYBER &&
2678                     one_in_(CYBERNOISE) &&
2679                     !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2680                 {
2681                         if (disturb_minor) disturb(FALSE, FALSE);
2682 #ifdef JP
2683                         msg_print("重厚な足音が聞こえた。");
2684 #else
2685                         msg_print("You hear heavy steps.");
2686 #endif
2687                 }
2688
2689                 /* Some monsters can speak */
2690                 if ((ap_r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
2691                     one_in_(SPEAK_CHANCE) &&
2692                     player_has_los_bold(oy, ox) &&
2693                     projectable(oy, ox, py, px))
2694                 {
2695                         char m_name[80];
2696                         char monmessage[1024];
2697                         cptr filename;
2698
2699                         /* Acquire the monster name/poss */
2700                         if (m_ptr->ml)
2701                                 monster_desc(m_name, m_ptr, 0);
2702                         else
2703 #ifdef JP
2704                                 strcpy(m_name, "それ");
2705 #else
2706                                 strcpy(m_name, "It");
2707 #endif
2708
2709                         /* Select the file for monster quotes */
2710                         if (MON_MONFEAR(m_ptr))
2711 #ifdef JP
2712                                 filename = "monfear_j.txt";
2713 #else
2714                                 filename = "monfear.txt";
2715 #endif
2716                         else if (is_pet(m_ptr))
2717 #ifdef JP
2718                                 filename = "monpet_j.txt";
2719 #else
2720                                 filename = "monpet.txt";
2721 #endif
2722                         else if (is_friendly(m_ptr))
2723 #ifdef JP
2724                                 filename = "monfrien_j.txt";
2725 #else
2726                                 filename = "monfrien.txt";
2727 #endif
2728                         else
2729 #ifdef JP
2730                                 filename = "monspeak_j.txt";
2731 #else
2732                                 filename = "monspeak.txt";
2733 #endif
2734                         /* Get the monster line */
2735                         if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
2736                         {
2737                                 /* Say something */
2738 #ifdef JP
2739 msg_format("%^s%s", m_name, monmessage);
2740 #else
2741                                 msg_format("%^s %s", m_name, monmessage);
2742 #endif
2743
2744                         }
2745                 }
2746         }
2747
2748         /* Try to cast spell occasionally */
2749         if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
2750         {
2751                 bool counterattack = FALSE;
2752
2753                 /* Give priority to counter attack? */
2754                 if (m_ptr->target_y)
2755                 {
2756                         int t_m_idx = cave[m_ptr->target_y][m_ptr->target_x].m_idx;
2757
2758                         /* The monster must be an enemy, and projectable */
2759                         if (t_m_idx &&
2760                             are_enemies(m_ptr, &m_list[t_m_idx]) &&
2761                             projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
2762                         {
2763                                 counterattack = TRUE;
2764                         }
2765                 }
2766
2767                 if (!counterattack)
2768                 {
2769                         /* Attempt to cast a spell */
2770                         if (aware && make_attack_spell(m_idx)) return;
2771
2772                         /*
2773                          * Attempt to cast a spell at an enemy other than the player
2774                          * (may slow the game a smidgeon, but I haven't noticed.)
2775                          */
2776                         if (monst_spell_monst(m_idx)) return;
2777                 }
2778                 else
2779                 {
2780                         /* Attempt to do counter attack at first */
2781                         if (monst_spell_monst(m_idx)) return;
2782
2783                         if (aware && make_attack_spell(m_idx)) return;
2784                 }
2785         }
2786
2787         /* Hack -- Assume no movement */
2788         mm[0] = mm[1] = mm[2] = mm[3] = 0;
2789         mm[4] = mm[5] = mm[6] = mm[7] = 0;
2790
2791
2792         /* Confused -- 100% random */
2793         if (MON_CONFUSED(m_ptr) || !aware)
2794         {
2795                 /* Try four "random" directions */
2796                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2797         }
2798
2799         /* 75% random movement */
2800         else if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) &&
2801                  (randint0(100) < 75))
2802         {
2803                 /* Memorize flags */
2804                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
2805
2806                 /* Try four "random" directions */
2807                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2808         }
2809
2810         /* 50% random movement */
2811         else if ((r_ptr->flags1 & RF1_RAND_50) &&
2812                                 (randint0(100) < 50))
2813         {
2814                 /* Memorize flags */
2815                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
2816
2817                 /* Try four "random" directions */
2818                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2819         }
2820
2821         /* 25% random movement */
2822         else if ((r_ptr->flags1 & RF1_RAND_25) &&
2823                                 (randint0(100) < 25))
2824         {
2825                 /* Memorize flags */
2826                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
2827
2828                 /* Try four "random" directions */
2829                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2830         }
2831
2832         /* Can't reach player - find something else to hit */
2833         else if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2834         {
2835                 /* Try four "random" directions */
2836                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2837
2838                 /* Look for an enemy */
2839 #if 0  /* Hack - Too slow.  Mimic pits are horrible with this on. */
2840                 get_enemy_dir(m_idx, mm);
2841 #endif /* 0 */
2842         }
2843
2844         /* Pets will follow the player */
2845         else if (is_pet(m_ptr))
2846         {
2847                 /* Are we trying to avoid the player? */
2848                 bool avoid = ((p_ptr->pet_follow_distance < 0) &&
2849                                                   (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
2850
2851                 /* Do we want to find the player? */
2852                 bool lonely = (!avoid && (m_ptr->cdis > p_ptr->pet_follow_distance));
2853
2854                 /* Should we find the player if we can't find a monster? */
2855                 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
2856
2857                 /* by default, move randomly */
2858                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2859
2860                 /* Look for an enemy */
2861                 if (!get_enemy_dir(m_idx, mm))
2862                 {
2863                         /* Find the player if necessary */
2864                         if (avoid || lonely || distant)
2865                         {
2866                                 /* Remember the leash length */
2867                                 int dis = p_ptr->pet_follow_distance;
2868
2869                                 /* Hack -- adjust follow distance temporarily */
2870                                 if (p_ptr->pet_follow_distance > PET_SEEK_DIST)
2871                                 {
2872                                         p_ptr->pet_follow_distance = PET_SEEK_DIST;
2873                                 }
2874
2875                                 /* Find the player */
2876                                 (void)get_moves(m_idx, mm);
2877
2878                                 /* Restore the leash */
2879                                 p_ptr->pet_follow_distance = dis;
2880                         }
2881                 }
2882         }
2883
2884         /* Friendly monster movement */
2885         else if (!is_hostile(m_ptr))
2886         {
2887                 /* by default, move randomly */
2888                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2889
2890                 /* Look for an enemy */
2891                 get_enemy_dir(m_idx, mm);
2892         }
2893         /* Normal movement */
2894         else
2895         {
2896                 /* Logical moves, may do nothing */
2897                 if (!get_moves(m_idx, mm)) return;
2898         }
2899
2900         /* Assume nothing */
2901         do_turn = FALSE;
2902         do_move = FALSE;
2903         do_view = FALSE;
2904         must_alter_to_move = FALSE;
2905
2906         /* Assume nothing */
2907         did_open_door = FALSE;
2908         did_bash_door = FALSE;
2909         did_take_item = FALSE;
2910         did_kill_item = FALSE;
2911         did_move_body = FALSE;
2912         did_pass_wall = FALSE;
2913         did_kill_wall = FALSE;
2914
2915
2916         /* Take a zero-terminated array of "directions" */
2917         for (i = 0; mm[i]; i++)
2918         {
2919                 /* Get the direction */
2920                 d = mm[i];
2921
2922                 /* Hack -- allow "randomized" motion */
2923                 if (d == 5) d = ddd[randint0(8)];
2924
2925                 /* Get the destination */
2926                 ny = oy + ddy[d];
2927                 nx = ox + ddx[d];
2928
2929                 /* Ignore locations off of edge */
2930                 if (!in_bounds2(ny, nx)) continue;
2931
2932                 /* Access that cave grid */
2933                 c_ptr = &cave[ny][nx];
2934                 f_ptr = &f_info[c_ptr->feat];
2935                 can_cross = monster_can_cross_terrain(c_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
2936
2937                 /* Access that cave grid's contents */
2938                 y_ptr = &m_list[c_ptr->m_idx];
2939
2940                 /* Hack -- player 'in' wall */
2941                 if (player_bold(ny, nx))
2942                 {
2943                         do_move = TRUE;
2944                 }
2945
2946                 /* Possibly a monster to attack */
2947                 else if (c_ptr->m_idx)
2948                 {
2949                         do_move = TRUE;
2950                 }
2951
2952                 /* Monster destroys walls (and doors) */
2953                 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
2954                          (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
2955                          have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
2956                          check_hp_for_feat_destruction(f_ptr, m_ptr))
2957                 {
2958                         /* Eat through walls/doors/rubble */
2959                         do_move = TRUE;
2960                         if (!can_cross) must_alter_to_move = TRUE;
2961
2962                         /* Monster destroyed a wall (later) */
2963                         did_kill_wall = TRUE;
2964                 }
2965
2966                 /* Floor is open? */
2967                 else if (can_cross)
2968                 {
2969                         /* Go ahead and move */
2970                         do_move = TRUE;
2971
2972                         /* Monster moves through walls (and doors) */
2973                         if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || p_ptr->pass_wall) &&
2974                             have_flag(f_ptr->flags, FF_CAN_PASS))
2975                         {
2976                                 /* Monster went through a wall */
2977                                 did_pass_wall = TRUE;
2978                         }
2979                 }
2980
2981                 /* Handle doors and secret doors */
2982                 else if (is_closed_door(c_ptr->feat))
2983                 {
2984                         bool may_bash = TRUE;
2985
2986                         /* Assume no move allowed */
2987                         do_move = FALSE;
2988
2989                         /* Creature can open doors. */
2990                         if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
2991                                  (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2992                         {
2993                                 /* Closed doors */
2994                                 if (!f_ptr->power)
2995                                 {
2996                                         /* The door is open */
2997                                         did_open_door = TRUE;
2998
2999                                         /* Do not bash the door */
3000                                         may_bash = FALSE;
3001
3002                                         /* Take a turn */
3003                                         do_turn = TRUE;
3004                                 }
3005
3006                                 /* Locked doors (not jammed) */
3007                                 else
3008                                 {
3009                                         /* Try to unlock it XXX XXX XXX */
3010                                         if (randint0(m_ptr->hp / 10) > f_ptr->power)
3011                                         {
3012                                                 /* Unlock the door */
3013                                                 cave_alter_feat(ny, nx, FF_DISARM);
3014
3015                                                 /* Do not bash the door */
3016                                                 may_bash = FALSE;
3017
3018                                                 /* Take a turn */
3019                                                 do_turn = TRUE;
3020                                         }
3021                                 }
3022                         }
3023
3024                         /* Stuck doors -- attempt to bash them down if allowed */
3025                         if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
3026                                 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
3027                         {
3028                                 /* Attempt to Bash XXX XXX XXX */
3029                                 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
3030                                 {
3031                                         /* Message */
3032                                         if (have_flag(f_ptr->flags, FF_GLASS))
3033 #ifdef JP
3034                                                 msg_print("ガラスが砕ける音がした!");
3035 #else
3036                                                 msg_print("You hear a glass was crashed!");
3037 #endif
3038                                         else
3039 #ifdef JP
3040                                                 msg_print("ドアを叩き開ける音がした!");
3041 #else
3042                                                 msg_print("You hear a door burst open!");
3043 #endif
3044
3045                                         /* Disturb (sometimes) */
3046                                         if (disturb_minor) disturb(0, 0);
3047
3048                                         /* The door was bashed open */
3049                                         did_bash_door = TRUE;
3050
3051                                         /* Hack -- fall into doorway */
3052                                         do_move = TRUE;
3053                                         must_alter_to_move = TRUE;
3054                                 }
3055                         }
3056
3057
3058                         /* Deal with doors in the way */
3059                         if (did_open_door || did_bash_door)
3060                         {
3061                                 /* Break down the door */
3062                                 if (did_bash_door && ((randint0(100) < 50) || (feat_state(c_ptr->feat, FF_OPEN) == c_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
3063                                 {
3064                                         cave_alter_feat(ny, nx, FF_BASH);
3065
3066                                         if (!m_ptr->r_idx) /* Killed by shards of glass, etc. */
3067                                         {
3068                                                 /* Update some things */
3069                                                 p_ptr->update |= (PU_FLOW);
3070                                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3071                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
3072
3073                                                 return;
3074                                         }
3075                                 }
3076
3077                                 /* Open the door */
3078                                 else
3079                                 {
3080                                         cave_alter_feat(ny, nx, FF_OPEN);
3081                                 }
3082
3083                                 f_ptr = &f_info[c_ptr->feat];
3084
3085                                 /* Handle viewable doors */
3086                                 do_view = TRUE;
3087                         }
3088                 }
3089
3090                 /* Hack -- check for Glyph of Warding */
3091                 if (do_move && is_glyph_grid(c_ptr) &&
3092                     !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
3093                 {
3094                         /* Assume no move allowed */
3095                         do_move = FALSE;
3096
3097                         /* Break the ward */
3098                         if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
3099                         {
3100                                 /* Describe observable breakage */
3101                                 if (c_ptr->info & CAVE_MARK)
3102                                 {
3103 #ifdef JP
3104                                         msg_print("守りのルーンが壊れた!");
3105 #else
3106                                         msg_print("The rune of protection is broken!");
3107 #endif
3108                                 }
3109
3110                                 /* Forget the rune */
3111                                 c_ptr->info &= ~(CAVE_MARK);
3112
3113                                 /* Break the rune */
3114                                 c_ptr->info &= ~(CAVE_OBJECT);
3115                                 c_ptr->mimic = 0;
3116
3117                                 /* Allow movement */
3118                                 do_move = TRUE;
3119
3120                                 /* Notice */
3121                                 note_spot(ny, nx);
3122                         }
3123                 }
3124                 else if (do_move && is_explosive_rune_grid(c_ptr) &&
3125                          !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
3126                 {
3127                         /* Assume no move allowed */
3128                         do_move = FALSE;
3129
3130                         /* Break the ward */
3131                         if (!is_pet(m_ptr))
3132                         {
3133                                 /* Break the ward */
3134                                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
3135                                 {
3136                                         /* Describe observable breakage */
3137                                         if (c_ptr->info & CAVE_MARK)
3138                                         {
3139 #ifdef JP
3140                                                 msg_print("ルーンが爆発した!");
3141 #else
3142                                                 msg_print("The rune explodes!");
3143 #endif
3144
3145                                                 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);
3146                                         }
3147                                 }
3148                                 else
3149                                 {
3150 #ifdef JP
3151                                         msg_print("爆発のルーンは解除された。");
3152 #else
3153                                         msg_print("An explosive rune was disarmed.");
3154 #endif
3155                                 }
3156
3157                                 /* Forget the rune */
3158                                 c_ptr->info &= ~(CAVE_MARK);
3159
3160                                 /* Break the rune */
3161                                 c_ptr->info &= ~(CAVE_OBJECT);
3162                                 c_ptr->mimic = 0;
3163
3164                                 note_spot(ny, nx);
3165                                 lite_spot(ny, nx);
3166
3167                                 if (!m_ptr->r_idx) return;
3168                                 /* Allow movement */
3169                                 do_move = TRUE;
3170                         }
3171                 }
3172
3173                 /* The player is in the way */
3174                 if (do_move && player_bold(ny, nx))
3175                 {
3176                         /* Some monsters never attack */
3177                         if (r_ptr->flags1 & RF1_NEVER_BLOW)
3178                         {
3179                                 /* Hack -- memorize lack of attacks */
3180                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
3181
3182                                 /* Do not move */
3183                                 do_move = FALSE;
3184                         }
3185
3186                         /* In anti-melee dungeon, stupid or confused monster takes useless turn */
3187                         if (do_move && (d_info[dungeon_type].flags1 & DF1_NO_MELEE))
3188                         {
3189                                 if (!MON_CONFUSED(m_ptr))
3190                                 {
3191                                         if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
3192                                         else
3193                                         {
3194                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
3195                                         }
3196                                 }
3197                         }
3198
3199                         /* The player is in the way.  Attack him. */
3200                         if (do_move)
3201                         {
3202                                 if (!p_ptr->riding || one_in_(2))
3203                                 {
3204                                         /* Do the attack */
3205                                         (void)make_attack_normal(m_idx);
3206
3207                                         /* Do not move */
3208                                         do_move = FALSE;
3209
3210                                         /* Took a turn */
3211                                         do_turn = TRUE;
3212                                 }
3213                         }
3214                 }
3215
3216                 /* A monster is in the way */
3217                 if (do_move && c_ptr->m_idx)
3218                 {
3219                         monster_race *z_ptr = &r_info[y_ptr->r_idx];
3220
3221                         /* Assume no movement */
3222                         do_move = FALSE;
3223
3224                         /* Attack 'enemies' */
3225                         if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
3226                                  (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
3227                                  can_cross && (c_ptr->m_idx != p_ptr->riding)) ||
3228                                 are_enemies(m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
3229                         {
3230                                 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
3231                                 {
3232                                         if (r_ptr->flags2 & RF2_KILL_BODY)
3233                                         {
3234                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
3235                                         }
3236
3237                                         /* attack */
3238                                         if (y_ptr->r_idx && (y_ptr->hp >= 0))
3239                                         {
3240                                                 if (monst_attack_monst(m_idx, c_ptr->m_idx)) return;
3241
3242                                                 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
3243                                                 else if (d_info[dungeon_type].flags1 & DF1_NO_MELEE)
3244                                                 {
3245                                                         if (MON_CONFUSED(m_ptr)) return;
3246                                                         else if (r_ptr->flags2 & RF2_STUPID)
3247                                                         {
3248                                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
3249                                                                 return;
3250                                                         }
3251                                                 }
3252                                         }
3253                                 }
3254                         }
3255
3256                         /* Push past weaker monsters (unless leaving a wall) */
3257                         else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
3258                                 (r_ptr->mexp > z_ptr->mexp) &&
3259                                 can_cross && (c_ptr->m_idx != p_ptr->riding) &&
3260                                 monster_can_cross_terrain(cave[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
3261                         {
3262                                 /* Allow movement */
3263                                 do_move = TRUE;
3264
3265                                 /* Monster pushed past another monster */
3266                                 did_move_body = TRUE;
3267
3268                                 /* Wake up the moved monster */
3269                                 (void)set_monster_csleep(c_ptr->m_idx, 0);
3270
3271                                 /* XXX XXX XXX Message */
3272                         }
3273                 }
3274
3275                 if (is_riding_mon)
3276                 {
3277                         if (!p_ptr->riding_ryoute && !MON_MONFEAR(&m_list[p_ptr->riding])) do_move = FALSE;
3278                 }
3279
3280                 if (did_kill_wall && do_move)
3281                 {
3282                         if (one_in_(GRINDNOISE))
3283                         {
3284                                 if (have_flag(f_ptr->flags, FF_GLASS))
3285 #ifdef JP
3286                                         msg_print("何かの砕ける音が聞こえる。");
3287 #else
3288                                         msg_print("There is a crashing sound.");
3289 #endif
3290                                 else
3291 #ifdef JP
3292                                         msg_print("ギシギシいう音が聞こえる。");
3293 #else
3294                                         msg_print("There is a grinding sound.");
3295 #endif
3296                         }
3297
3298                         cave_alter_feat(ny, nx, FF_HURT_DISI);
3299
3300                         if (!m_ptr->r_idx) /* Killed by shards of glass, etc. */
3301                         {
3302                                 /* Update some things */
3303                                 p_ptr->update |= (PU_FLOW);
3304                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3305                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
3306
3307                                 return;
3308                         }
3309
3310                         f_ptr = &f_info[c_ptr->feat];
3311
3312                         /* Note changes to viewable region */
3313                         do_view = TRUE;
3314
3315                         /* Take a turn */
3316                         do_turn = TRUE;
3317                 }
3318
3319                 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
3320                 {
3321                         if (!monster_can_cross_terrain(c_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
3322                         {
3323                                 /* Assume no move allowed */
3324                                 do_move = FALSE;
3325                         }
3326                 }
3327
3328                 /*
3329                  * Check if monster can cross terrain
3330                  * This is checked after the normal attacks
3331                  * to allow monsters to attack an enemy,
3332                  * even if it can't enter the terrain.
3333                  */
3334                 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
3335                 {
3336                         /* Assume no move allowed */
3337                         do_move = FALSE;
3338                 }
3339
3340                 /* Some monsters never move */
3341                 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
3342                 {
3343                         /* Hack -- memorize lack of moves */
3344                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
3345
3346                         /* Do not move */
3347                         do_move = FALSE;
3348                 }
3349
3350                 /* Creature has been allowed move */
3351                 if (do_move)
3352                 {
3353                         /* Take a turn */
3354                         do_turn = TRUE;
3355
3356                         if (have_flag(f_ptr->flags, FF_TREE))
3357                         {
3358                                 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
3359                                 {
3360                                         m_ptr->energy_need += ENERGY_NEED();
3361                                 }
3362                         }
3363
3364                         if (!is_riding_mon)
3365                         {
3366                                 /* Hack -- Update the old location */
3367                                 cave[oy][ox].m_idx = c_ptr->m_idx;
3368
3369                                 /* Mega-Hack -- move the old monster, if any */
3370                                 if (c_ptr->m_idx)
3371                                 {
3372                                         /* Move the old monster */
3373                                         y_ptr->fy = oy;
3374                                         y_ptr->fx = ox;
3375
3376                                         /* Update the old monster */
3377                                         update_mon(c_ptr->m_idx, TRUE);
3378                                 }
3379
3380                                 /* Hack -- Update the new location */
3381                                 c_ptr->m_idx = m_idx;
3382
3383                                 /* Move the monster */
3384                                 m_ptr->fy = ny;
3385                                 m_ptr->fx = nx;
3386
3387                                 /* Update the monster */
3388                                 update_mon(m_idx, TRUE);
3389
3390                                 /* Redraw the old grid */
3391                                 lite_spot(oy, ox);
3392
3393                                 /* Redraw the new grid */
3394                                 lite_spot(ny, nx);
3395                         }
3396                         else
3397                         {
3398                                 /* Sound */
3399                                 /* sound(SOUND_WALK); */
3400
3401                                 /* Move the player */
3402                                 if (!move_player_effect(ny, nx, MPE_DONT_PICKUP)) break;
3403                         }
3404
3405                         /* Possible disturb */
3406                         if (m_ptr->ml &&
3407                             (disturb_move ||
3408                              (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(py, px, m_ptr->fy, m_ptr->fx)) ||
3409                              (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)))
3410                         {
3411                                 /* Disturb */
3412                                 if (is_hostile(m_ptr))
3413                                         disturb(0, 1);
3414                         }
3415
3416                         /* Take or Kill objects on the floor */
3417                         if (c_ptr->o_idx && (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
3418                             (!is_pet(m_ptr) || ((p_ptr->pet_extra_flags & PF_PICKUP_ITEMS) && (r_ptr->flags2 & RF2_TAKE_ITEM))))
3419                         {
3420                                 s16b this_o_idx, next_o_idx;
3421                                 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
3422
3423                                 /* Scan all objects in the grid */
3424                                 for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
3425                                 {
3426                                         u32b flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
3427                                         char m_name[80], o_name[MAX_NLEN];
3428
3429                                         /* Acquire object */
3430                                         object_type *o_ptr = &o_list[this_o_idx];
3431
3432                                         /* Acquire next object */
3433                                         next_o_idx = o_ptr->next_o_idx;
3434
3435                                         if (do_take)
3436                                         {
3437                                                 /* Skip gold */
3438                                                 if (o_ptr->tval == TV_GOLD) continue;
3439
3440                                                 /*
3441                                                  * Skip "real" corpses and statues, to avoid extreme
3442                                                  * silliness like a novice rogue pockets full of statues
3443                                                  * and corpses.
3444                                                  */
3445                                                 if ((o_ptr->tval == TV_CORPSE) ||
3446                                                     (o_ptr->tval == TV_STATUE)) continue;
3447                                         }
3448
3449                                         /* Extract some flags */
3450                                         object_flags(o_ptr, flgs);
3451
3452                                         /* Acquire the object name */
3453                                         object_desc(o_name, o_ptr, 0);
3454
3455                                         /* Acquire the monster name */
3456                                         monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
3457
3458                                         /* React to objects that hurt the monster */
3459                                         if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
3460                                         if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
3461                                         if (have_flag(flgs, TR_SLAY_TROLL))  flg3 |= (RF3_TROLL);
3462                                         if (have_flag(flgs, TR_KILL_TROLL))  flg3 |= (RF3_TROLL);
3463                                         if (have_flag(flgs, TR_SLAY_GIANT))  flg3 |= (RF3_GIANT);
3464                                         if (have_flag(flgs, TR_KILL_GIANT))  flg3 |= (RF3_GIANT);
3465                                         if (have_flag(flgs, TR_SLAY_ORC))    flg3 |= (RF3_ORC);
3466                                         if (have_flag(flgs, TR_KILL_ORC))    flg3 |= (RF3_ORC);
3467                                         if (have_flag(flgs, TR_SLAY_DEMON))  flg3 |= (RF3_DEMON);
3468                                         if (have_flag(flgs, TR_KILL_DEMON))  flg3 |= (RF3_DEMON);
3469                                         if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
3470                                         if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
3471                                         if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
3472                                         if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
3473                                         if (have_flag(flgs, TR_SLAY_EVIL))   flg3 |= (RF3_EVIL);
3474                                         if (have_flag(flgs, TR_KILL_EVIL))   flg3 |= (RF3_EVIL);
3475                                         if (have_flag(flgs, TR_SLAY_HUMAN))  flg2 |= (RF2_HUMAN);
3476                                         if (have_flag(flgs, TR_KILL_HUMAN))  flg2 |= (RF2_HUMAN);
3477                                         if (have_flag(flgs, TR_BRAND_ACID))  flgr |= (RFR_IM_ACID);
3478                                         if (have_flag(flgs, TR_BRAND_ELEC))  flgr |= (RFR_IM_ELEC);
3479                                         if (have_flag(flgs, TR_BRAND_FIRE))  flgr |= (RFR_IM_FIRE);
3480                                         if (have_flag(flgs, TR_BRAND_COLD))  flgr |= (RFR_IM_COLD);
3481                                         if (have_flag(flgs, TR_BRAND_POIS))  flgr |= (RFR_IM_POIS);
3482
3483                                         /* The object cannot be picked up by the monster */
3484                                         if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
3485                                             ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
3486                                         {
3487                                                 /* Only give a message for "take_item" */
3488                                                 if (do_take && (r_ptr->flags2 & RF2_STUPID))
3489                                                 {
3490                                                         /* Take note */
3491                                                         did_take_item = TRUE;
3492
3493                                                         /* Describe observable situations */
3494                                                         if (m_ptr->ml && player_can_see_bold(ny, nx))
3495                                                         {
3496                                                                 /* Dump a message */
3497 #ifdef JP
3498                                                                 msg_format("%^sは%sを拾おうとしたが、だめだった。", m_name, o_name);
3499 #else
3500                                                                 msg_format("%^s tries to pick up %s, but fails.", m_name, o_name);
3501 #endif
3502                                                         }
3503                                                 }
3504                                         }
3505
3506                                         /* Pick up the item */
3507                                         else if (do_take)
3508                                         {
3509                                                 /* Take note */
3510                                                 did_take_item = TRUE;
3511
3512                                                 /* Describe observable situations */
3513                                                 if (player_can_see_bold(ny, nx))
3514                                                 {
3515                                                         /* Dump a message */
3516 #ifdef JP
3517                                                         msg_format("%^sが%sを拾った。", m_name, o_name);
3518 #else
3519                                                         msg_format("%^s picks up %s.", m_name, o_name);
3520 #endif
3521                                                 }
3522
3523                                                 /* Excise the object */
3524                                                 excise_object_idx(this_o_idx);
3525
3526                                                 /* Forget mark */
3527                                                 o_ptr->marked &= OM_TOUCHED;
3528
3529                                                 /* Forget location */
3530                                                 o_ptr->iy = o_ptr->ix = 0;
3531
3532                                                 /* Memorize monster */
3533                                                 o_ptr->held_m_idx = m_idx;
3534
3535                                                 /* Build a stack */
3536                                                 o_ptr->next_o_idx = m_ptr->hold_o_idx;
3537
3538                                                 /* Carry object */
3539                                                 m_ptr->hold_o_idx = this_o_idx;
3540                                         }
3541
3542                                         /* Destroy the item if not a pet */
3543                                         else if (!is_pet(m_ptr))
3544                                         {
3545                                                 /* Take note */
3546                                                 did_kill_item = TRUE;
3547
3548                                                 /* Describe observable situations */
3549                                                 if (player_has_los_bold(ny, nx))
3550                                                 {
3551                                                         /* Dump a message */
3552 #ifdef JP
3553                                                         msg_format("%^sが%sを破壊した。", m_name, o_name);
3554 #else
3555                                                         msg_format("%^s destroys %s.", m_name, o_name);
3556 #endif
3557                                                 }
3558
3559                                                 /* Delete the object */
3560                                                 delete_object_idx(this_o_idx);
3561                                         }
3562                                 }
3563                         }
3564                 }
3565
3566                 /* Stop when done */
3567                 if (do_turn) break;
3568         }
3569
3570         /*
3571          *  Forward movements failed, but now received LOS attack!
3572          *  Try to flow by smell.
3573          */
3574         if (p_ptr->no_flowed && i > 2 &&  m_ptr->target_y)
3575                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3576
3577         /* If we haven't done anything, try casting a spell again */
3578         if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
3579         {
3580                 /* Try to cast spell again */
3581                 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
3582                 {
3583                         if (make_attack_spell(m_idx)) return;
3584                 }
3585         }
3586
3587
3588         /* Notice changes in view */
3589         if (do_view)
3590         {
3591                 /* Update some things */
3592                 p_ptr->update |= (PU_FLOW);
3593
3594                 /* Window stuff */
3595                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3596         }
3597
3598         /* Notice changes in view */
3599         if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
3600                 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !p_ptr->inside_battle)))
3601         {
3602                 /* Update some things */
3603                 p_ptr->update |= (PU_MON_LITE);
3604         }
3605
3606         /* Learn things from observable monster */
3607         if (is_original_ap_and_seen(m_ptr))
3608         {
3609                 /* Monster opened a door */
3610                 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
3611
3612                 /* Monster bashed a door */
3613                 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
3614
3615                 /* Monster tried to pick something up */
3616                 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
3617
3618                 /* Monster tried to crush something */
3619                 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
3620
3621                 /* Monster pushed past another monster */
3622                 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
3623
3624                 /* Monster passed through a wall */
3625                 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
3626
3627                 /* Monster destroyed a wall */
3628                 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
3629         }
3630
3631
3632         /* Hack -- get "bold" if out of options */
3633         if (!do_turn && !do_move && MON_MONFEAR(m_ptr) && aware)
3634         {
3635                 /* No longer afraid */
3636                 (void)set_monster_monfear(m_idx, 0);
3637
3638                 /* Message if seen */
3639                 if (see_m)
3640                 {
3641                         char m_name[80];
3642
3643                         /* Acquire the monster name */
3644                         monster_desc(m_name, m_ptr, 0);
3645
3646                         /* Dump a message */
3647 #ifdef JP
3648                         msg_format("%^sは戦いを決意した!", m_name);
3649 #else
3650                         msg_format("%^s turns to fight!", m_name);
3651 #endif
3652                 }
3653
3654                 if (m_ptr->ml) chg_virtue(V_COMPASSION, -1);
3655
3656                 /* XXX XXX XXX Actually do something now (?) */
3657         }
3658 }
3659
3660 /*!
3661  * @brief 全モンスターのターン管理メインルーチン /
3662  * Process all the "live" monsters, once per game turn.
3663  * @return なし
3664  * @details
3665  * During each game turn, we scan through the list of all the "live" monsters,\n
3666  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
3667  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
3668  *\n
3669  * Note that monsters can never move in the monster array (except when the\n
3670  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
3671  *\n
3672  * This function is responsible for at least half of the processor time\n
3673  * on a normal system with a "normal" amount of monsters and a player doing\n
3674  * normal things.\n
3675  *\n
3676  * When the player is resting, virtually 90% of the processor time is spent\n
3677  * in this function, and its children, "process_monster()" and "make_move()".\n
3678  *\n
3679  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
3680  * especially when the player is running.\n
3681  *\n
3682  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
3683  * monsters while they are still being "born".  A monster is "fresh" only\n
3684  * during the turn in which it is created, and we use the "hack_m_idx" to\n
3685  * determine if the monster is yet to be processed during the current turn.\n
3686  *\n
3687  * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
3688  * move before any "nasty" monsters get to use their spell attacks.\n
3689  *\n
3690  * Note that when the "knowledge" about the currently tracked monster\n
3691  * changes (flags, attacks, spells), we induce a redraw of the monster\n
3692  * recall window.\n
3693  */
3694 void process_monsters(void)
3695 {
3696         int             i;
3697         int             fx, fy;
3698
3699         bool            test;
3700
3701         monster_type    *m_ptr;
3702         monster_race    *r_ptr;
3703
3704         int             old_monster_race_idx;
3705
3706         u32b    old_r_flags1 = 0L;
3707         u32b    old_r_flags2 = 0L;
3708         u32b    old_r_flags3 = 0L;
3709         u32b    old_r_flags4 = 0L;
3710         u32b    old_r_flags5 = 0L;
3711         u32b    old_r_flags6 = 0L;
3712         u32b    old_r_flagsr = 0L;
3713
3714         byte    old_r_blows0 = 0;
3715         byte    old_r_blows1 = 0;
3716         byte    old_r_blows2 = 0;
3717         byte    old_r_blows3 = 0;
3718
3719         byte    old_r_cast_spell = 0;
3720
3721         int speed;
3722
3723         /* Clear monster fighting indicator */
3724         mon_fight = FALSE;
3725
3726         /* Memorize old race */
3727         old_monster_race_idx = p_ptr->monster_race_idx;
3728
3729         /* Acquire knowledge */
3730         if (p_ptr->monster_race_idx)
3731         {
3732                 /* Acquire current monster */
3733                 r_ptr = &r_info[p_ptr->monster_race_idx];
3734
3735                 /* Memorize flags */
3736                 old_r_flags1 = r_ptr->r_flags1;
3737                 old_r_flags2 = r_ptr->r_flags2;
3738                 old_r_flags3 = r_ptr->r_flags3;
3739                 old_r_flags4 = r_ptr->r_flags4;
3740                 old_r_flags5 = r_ptr->r_flags5;
3741                 old_r_flags6 = r_ptr->r_flags6;
3742                 old_r_flagsr = r_ptr->r_flagsr;
3743
3744                 /* Memorize blows */
3745                 old_r_blows0 = r_ptr->r_blows[0];
3746                 old_r_blows1 = r_ptr->r_blows[1];
3747                 old_r_blows2 = r_ptr->r_blows[2];
3748                 old_r_blows3 = r_ptr->r_blows[3];
3749
3750                 /* Memorize castings */
3751                 old_r_cast_spell = r_ptr->r_cast_spell;
3752         }
3753
3754
3755         /* Process the monsters (backwards) */
3756         for (i = m_max - 1; i >= 1; i--)
3757         {
3758                 /* Access the monster */
3759                 m_ptr = &m_list[i];
3760                 r_ptr = &r_info[m_ptr->r_idx];
3761
3762                 /* Handle "leaving" */
3763                 if (p_ptr->leaving) break;
3764
3765                 /* Ignore "dead" monsters */
3766                 if (!m_ptr->r_idx) continue;
3767
3768                 if (p_ptr->wild_mode) continue;
3769
3770
3771                 /* Handle "fresh" monsters */
3772                 if (m_ptr->mflag & MFLAG_BORN)
3773                 {
3774                         /* No longer "fresh" */
3775                         m_ptr->mflag &= ~(MFLAG_BORN);
3776
3777                         /* Skip */
3778                         continue;
3779                 }
3780
3781                 /* Hack -- Require proximity */
3782                 if (m_ptr->cdis >= AAF_LIMIT) continue;
3783
3784
3785                 /* Access the location */
3786                 fx = m_ptr->fx;
3787                 fy = m_ptr->fy;
3788
3789                 /* Flow by smell is allowed */
3790                 if (!p_ptr->no_flowed)
3791                 {
3792                         m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3793                 }
3794
3795                 /* Assume no move */
3796                 test = FALSE;
3797
3798                 /* Handle "sensing radius" */
3799                 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
3800                 {
3801                         /* We can "sense" the player */
3802                         test = TRUE;
3803                 }
3804
3805                 /* Handle "sight" and "aggravation" */
3806                 else if ((m_ptr->cdis <= MAX_SIGHT) &&
3807                         (player_has_los_bold(fy, fx) || (p_ptr->cursed & TRC_AGGRAVATE)))
3808                 {
3809                         /* We can "see" or "feel" the player */
3810                         test = TRUE;
3811                 }
3812
3813 #if 0 /* (cave[py][px].when == cave[fy][fx].when) is always FALSE... */
3814                 /* Hack -- Monsters can "smell" the player from far away */
3815                 /* Note that most monsters have "aaf" of "20" or so */
3816                 else if (!(m_ptr->mflag2 & MFLAG2_NOFLOW) &&
3817                         cave_have_flag_bold(py, px, FF_MOVE) &&
3818                         (cave[py][px].when == cave[fy][fx].when) &&
3819                         (cave[fy][fx].dist < MONSTER_FLOW_DEPTH) &&
3820                         (cave[fy][fx].dist < r_ptr->aaf))
3821                 {
3822                         /* We can "smell" the player */
3823                         test = TRUE;
3824                 }
3825 #endif
3826                 else if (m_ptr->target_y) test = TRUE;
3827
3828                 /* Do nothing */
3829                 if (!test) continue;
3830
3831
3832                 if (p_ptr->riding == i)
3833                         speed = p_ptr->pspeed;
3834                 else
3835                 {
3836                         speed = m_ptr->mspeed;
3837
3838                         /* Monsters move quickly in Nightmare mode */
3839                         if (ironman_nightmare) speed += 5;
3840
3841                         if (MON_FAST(m_ptr)) speed += 10;
3842                         if (MON_SLOW(m_ptr)) speed -= 10;
3843                 }
3844
3845                 /* Give this monster some energy */
3846                 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
3847
3848                 /* Not enough energy to move */
3849                 if (m_ptr->energy_need > 0) continue;
3850
3851                 /* Use up "some" energy */
3852                 m_ptr->energy_need += ENERGY_NEED();
3853
3854
3855                 /* Save global index */
3856                 hack_m_idx = i;
3857
3858                 /* Process the monster */
3859                 process_monster(i);
3860
3861                 reset_target(m_ptr);
3862
3863                 /* Give up flow_by_smell when it might useless */
3864                 if (p_ptr->no_flowed && one_in_(3))
3865                         m_ptr->mflag2 |= MFLAG2_NOFLOW;
3866
3867                 /* Hack -- notice death or departure */
3868                 if (!p_ptr->playing || p_ptr->is_dead) break;
3869
3870                 /* Notice leaving */
3871                 if (p_ptr->leaving) break;
3872         }
3873
3874         /* Reset global index */
3875         hack_m_idx = 0;
3876
3877
3878         /* Tracking a monster race (the same one we were before) */
3879         if (p_ptr->monster_race_idx && (p_ptr->monster_race_idx == old_monster_race_idx))
3880         {
3881                 /* Acquire monster race */
3882                 r_ptr = &r_info[p_ptr->monster_race_idx];
3883
3884                 /* Check for knowledge change */
3885                 if ((old_r_flags1 != r_ptr->r_flags1) ||
3886                         (old_r_flags2 != r_ptr->r_flags2) ||
3887                         (old_r_flags3 != r_ptr->r_flags3) ||
3888                         (old_r_flags4 != r_ptr->r_flags4) ||
3889                         (old_r_flags5 != r_ptr->r_flags5) ||
3890                         (old_r_flags6 != r_ptr->r_flags6) ||
3891                         (old_r_flagsr != r_ptr->r_flagsr) ||
3892                         (old_r_blows0 != r_ptr->r_blows[0]) ||
3893                         (old_r_blows1 != r_ptr->r_blows[1]) ||
3894                         (old_r_blows2 != r_ptr->r_blows[2]) ||
3895                         (old_r_blows3 != r_ptr->r_blows[3]) ||
3896                         (old_r_cast_spell != r_ptr->r_cast_spell))
3897                 {
3898                         /* Window stuff */
3899                         p_ptr->window |= (PW_MONSTER);
3900                 }
3901         }
3902 }
3903
3904 /*!
3905  * @brief モンスターの時限ステータスを取得する
3906  * @return m_idx モンスターの参照ID
3907  * @return mproc_type モンスターの時限ステータスID
3908  * @return 残りターン値
3909  */
3910 int get_mproc_idx(int m_idx, int mproc_type)
3911 {
3912         s16b *cur_mproc_list = mproc_list[mproc_type];
3913         int i;
3914
3915         for (i = mproc_max[mproc_type] - 1; i >= 0; i--)
3916         {
3917                 if (cur_mproc_list[i] == m_idx) return i;
3918         }
3919
3920         return -1;
3921 }
3922
3923 /*!
3924  * @brief モンスターの時限ステータスリストを追加する
3925  * @return m_idx モンスターの参照ID
3926  * @return mproc_type 追加したいモンスターの時限ステータスID
3927  * @return なし
3928  */
3929 static void mproc_add(int m_idx, int mproc_type)
3930 {
3931         if (mproc_max[mproc_type] < max_m_idx) mproc_list[mproc_type][mproc_max[mproc_type]++] = m_idx;
3932 }
3933
3934
3935 /*!
3936  * @brief モンスターの時限ステータスリストを削除
3937  * @return m_idx モンスターの参照ID
3938  * @return mproc_type 削除したいモンスターの時限ステータスID
3939  * @return なし
3940  */
3941 static void mproc_remove(int m_idx, int mproc_type)
3942 {
3943         int mproc_idx = get_mproc_idx(m_idx, mproc_type);
3944         if (mproc_idx >= 0) mproc_list[mproc_type][mproc_idx] = mproc_list[mproc_type][--mproc_max[mproc_type]];
3945 }
3946
3947
3948 /*!
3949  * @brief モンスターの時限ステータスリストを初期化する / Initialize monster process
3950  * @return なし
3951  */
3952 void mproc_init(void)
3953 {
3954         monster_type *m_ptr;
3955         int          i, cmi;
3956
3957         /* Reset "mproc_max[]" */
3958         for (cmi = 0; cmi < MAX_MTIMED; cmi++) mproc_max[cmi] = 0;
3959
3960         /* Process the monsters (backwards) */
3961         for (i = m_max - 1; i >= 1; i--)
3962         {
3963                 /* Access the monster */
3964                 m_ptr = &m_list[i];
3965
3966                 /* Ignore "dead" monsters */
3967                 if (!m_ptr->r_idx) continue;
3968
3969                 for (cmi = 0; cmi < MAX_MTIMED; cmi++)
3970                 {
3971                         if (m_ptr->mtimed[cmi]) mproc_add(i, cmi);
3972                 }
3973         }
3974 }
3975
3976
3977 /*!
3978  * @brief モンスターの睡眠状態値をセットする /
3979  * Set "m_ptr->mtimed[MTIMED_CSLEEP]", notice observable changes
3980  * @param m_idx モンスター参照ID
3981  * @param v セットする値
3982  * @return 別途更新処理が必要な場合TRUEを返す
3983  */
3984 bool set_monster_csleep(int m_idx, int v)
3985 {
3986         monster_type *m_ptr = &m_list[m_idx];
3987         bool notice = FALSE;
3988
3989         /* Hack -- Force good values */
3990         v = (v > 10000) ? 10000 : (v < 0) ? 0 : v;
3991
3992         /* Open */
3993         if (v)
3994         {
3995                 if (!MON_CSLEEP(m_ptr))
3996                 {
3997                         mproc_add(m_idx, MTIMED_CSLEEP);
3998                         notice = TRUE;
3999                 }
4000         }
4001
4002         /* Shut */
4003         else
4004         {
4005                 if (MON_CSLEEP(m_ptr))
4006                 {
4007                         mproc_remove(m_idx, MTIMED_CSLEEP);
4008                         notice = TRUE;
4009                 }
4010         }
4011
4012         /* Use the value */
4013         m_ptr->mtimed[MTIMED_CSLEEP] = v;
4014
4015         if (!notice) return FALSE;
4016
4017         if (m_ptr->ml)
4018         {
4019                 /* Update health bar as needed */
4020                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
4021                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
4022         }
4023
4024         if (r_info[m_ptr->r_idx].flags7 & RF7_HAS_LD_MASK) p_ptr->update |= (PU_MON_LITE);
4025
4026         return TRUE;
4027 }
4028
4029
4030 /*!
4031  * @brief モンスターの加速状態値をセット /
4032  * Set "m_ptr->mtimed[MTIMED_FAST]", notice observable changes
4033  * @param m_idx モンスター参照ID
4034  * @param v セットする値
4035  * @return 別途更新処理が必要な場合TRUEを返す
4036  */
4037 bool set_monster_fast(int m_idx, int v)
4038 {
4039         monster_type *m_ptr = &m_list[m_idx];
4040         bool notice = FALSE;
4041
4042         /* Hack -- Force good values */
4043         v = (v > 200) ? 200 : (v < 0) ? 0 : v;
4044
4045         /* Open */
4046         if (v)
4047         {
4048                 if (!MON_FAST(m_ptr))
4049                 {
4050                         mproc_add(m_idx, MTIMED_FAST);
4051                         notice = TRUE;
4052                 }
4053         }
4054
4055         /* Shut */
4056         else
4057         {
4058                 if (MON_FAST(m_ptr))
4059                 {
4060                         mproc_remove(m_idx, MTIMED_FAST);
4061                         notice = TRUE;
4062                 }
4063         }
4064
4065         /* Use the value */
4066         m_ptr->mtimed[MTIMED_FAST] = v;
4067
4068         if (!notice) return FALSE;
4069
4070         if ((p_ptr->riding == m_idx) && !p_ptr->leaving) p_ptr->update |= (PU_BONUS);
4071
4072         return TRUE;
4073 }
4074
4075
4076 /*
4077  * Set "m_ptr->mtimed[MTIMED_SLOW]", notice observable changes
4078  */
4079 bool set_monster_slow(int m_idx, int v)
4080 {
4081         monster_type *m_ptr = &m_list[m_idx];
4082         bool notice = FALSE;
4083
4084         /* Hack -- Force good values */
4085         v = (v > 200) ? 200 : (v < 0) ? 0 : v;
4086
4087         /* Open */
4088         if (v)
4089         {
4090                 if (!MON_SLOW(m_ptr))
4091                 {
4092                         mproc_add(m_idx, MTIMED_SLOW);
4093                         notice = TRUE;
4094                 }
4095         }
4096
4097         /* Shut */
4098         else
4099         {
4100                 if (MON_SLOW(m_ptr))
4101                 {
4102                         mproc_remove(m_idx, MTIMED_SLOW);
4103                         notice = TRUE;
4104                 }
4105         }
4106
4107         /* Use the value */
4108         m_ptr->mtimed[MTIMED_SLOW] = v;
4109
4110         if (!notice) return FALSE;
4111
4112         if ((p_ptr->riding == m_idx) && !p_ptr->leaving) p_ptr->update |= (PU_BONUS);
4113
4114         return TRUE;
4115 }
4116
4117
4118 /*!
4119  * @brief モンスターの朦朧状態値をセット /
4120  * Set "m_ptr->mtimed[MTIMED_STUNNED]", notice observable changes
4121  * @param m_idx モンスター参照ID
4122  * @param v セットする値
4123  * @return 別途更新処理が必要な場合TRUEを返す
4124  */
4125 bool set_monster_stunned(int m_idx, int v)
4126 {
4127         monster_type *m_ptr = &m_list[m_idx];
4128         bool notice = FALSE;
4129
4130         /* Hack -- Force good values */
4131         v = (v > 200) ? 200 : (v < 0) ? 0 : v;
4132
4133         /* Open */
4134         if (v)
4135         {
4136                 if (!MON_STUNNED(m_ptr))
4137                 {
4138                         mproc_add(m_idx, MTIMED_STUNNED);
4139                         notice = TRUE;
4140                 }
4141         }
4142
4143         /* Shut */
4144         else
4145         {
4146                 if (MON_STUNNED(m_ptr))
4147                 {
4148                         mproc_remove(m_idx, MTIMED_STUNNED);
4149                         notice = TRUE;
4150                 }
4151         }
4152
4153         /* Use the value */
4154         m_ptr->mtimed[MTIMED_STUNNED] = v;
4155
4156         return notice;
4157 }
4158
4159
4160 /*!
4161  * @brief モンスターの混乱状態値をセット /
4162  * Set "m_ptr->mtimed[MTIMED_CONFUSED]", notice observable changes
4163  * @param m_idx モンスター参照ID
4164  * @param v セットする値
4165  * @return 別途更新処理が必要な場合TRUEを返す
4166  */
4167 bool set_monster_confused(int m_idx, int v)
4168 {
4169         monster_type *m_ptr = &m_list[m_idx];
4170         bool notice = FALSE;
4171
4172         /* Hack -- Force good values */
4173         v = (v > 200) ? 200 : (v < 0) ? 0 : v;
4174
4175         /* Open */
4176         if (v)
4177         {
4178                 if (!MON_CONFUSED(m_ptr))
4179                 {
4180                         mproc_add(m_idx, MTIMED_CONFUSED);
4181                         notice = TRUE;
4182                 }
4183         }
4184
4185         /* Shut */
4186         else
4187         {
4188                 if (MON_CONFUSED(m_ptr))
4189                 {
4190                         mproc_remove(m_idx, MTIMED_CONFUSED);
4191                         notice = TRUE;
4192                 }
4193         }
4194
4195         /* Use the value */
4196         m_ptr->mtimed[MTIMED_CONFUSED] = v;
4197
4198         return notice;
4199 }
4200
4201
4202 /*!
4203  * @brief モンスターの恐慌状態値をセット /
4204  * Set "m_ptr->mtimed[MTIMED_MONFEAR]", notice observable changes
4205  * @param m_idx モンスター参照ID
4206  * @param v セットする値
4207  * @return 別途更新処理が必要な場合TRUEを返す
4208  */
4209 bool set_monster_monfear(int m_idx, int v)
4210 {
4211         monster_type *m_ptr = &m_list[m_idx];
4212         bool notice = FALSE;
4213
4214         /* Hack -- Force good values */
4215         v = (v > 200) ? 200 : (v < 0) ? 0 : v;
4216
4217         /* Open */
4218         if (v)
4219         {
4220                 if (!MON_MONFEAR(m_ptr))
4221                 {
4222                         mproc_add(m_idx, MTIMED_MONFEAR);
4223                         notice = TRUE;
4224                 }
4225         }
4226
4227         /* Shut */
4228         else
4229         {
4230                 if (MON_MONFEAR(m_ptr))
4231                 {
4232                         mproc_remove(m_idx, MTIMED_MONFEAR);
4233                         notice = TRUE;
4234                 }
4235         }
4236
4237         /* Use the value */
4238         m_ptr->mtimed[MTIMED_MONFEAR] = v;
4239
4240         if (!notice) return FALSE;
4241
4242         if (m_ptr->ml)
4243         {
4244                 /* Update health bar as needed */
4245                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
4246                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
4247         }
4248
4249         return TRUE;
4250 }
4251
4252
4253 /*!
4254  * @brief モンスターの無敵状態値をセット /
4255  * Set "m_ptr->mtimed[MTIMED_INVULNER]", notice observable changes
4256  * @param m_idx モンスター参照ID
4257  * @param v セットする値
4258  * @param energy_need TRUEならば無敵解除時に行動ターン消費を行う
4259  * @return 別途更新処理が必要な場合TRUEを返す
4260  */
4261 bool set_monster_invulner(int m_idx, int v, bool energy_need)
4262 {
4263         monster_type *m_ptr = &m_list[m_idx];
4264         bool notice = FALSE;
4265
4266         /* Hack -- Force good values */
4267         v = (v > 200) ? 200 : (v < 0) ? 0 : v;
4268
4269         /* Open */
4270         if (v)
4271         {
4272                 if (!MON_INVULNER(m_ptr))
4273                 {
4274                         mproc_add(m_idx, MTIMED_INVULNER);
4275                         notice = TRUE;
4276                 }
4277         }
4278
4279         /* Shut */
4280         else
4281         {
4282                 if (MON_INVULNER(m_ptr))
4283                 {
4284                         mproc_remove(m_idx, MTIMED_INVULNER);
4285                         if (energy_need && !p_ptr->wild_mode) m_ptr->energy_need += ENERGY_NEED();
4286                         notice = TRUE;
4287                 }
4288         }
4289
4290         /* Use the value */
4291         m_ptr->mtimed[MTIMED_INVULNER] = v;
4292
4293         if (!notice) return FALSE;
4294
4295         if (m_ptr->ml)
4296         {
4297                 /* Update health bar as needed */
4298                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
4299                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
4300         }
4301
4302         return TRUE;
4303 }
4304
4305
4306 static u32b csleep_noise;
4307
4308 /*!
4309  * @brief モンスターの各種状態値を時間経過により更新するサブルーチン
4310  * @param m_idx モンスター参照ID
4311  * @param mtimed_idx 更新するモンスターの時限ステータスID
4312  * @return なし
4313  */
4314 static void process_monsters_mtimed_aux(int m_idx, int mtimed_idx)
4315 {
4316         monster_type *m_ptr = &m_list[m_idx];
4317
4318         switch (mtimed_idx)
4319         {
4320         case MTIMED_CSLEEP:
4321         {
4322                 monster_race *r_ptr = &r_info[m_ptr->r_idx];
4323
4324                 /* Assume does not wake up */
4325                 bool test = FALSE;
4326
4327                 /* Hack -- Require proximity */
4328                 if (m_ptr->cdis < AAF_LIMIT)
4329                 {
4330                         /* Handle "sensing radius" */
4331                         if (m_ptr->cdis <= (is_pet(m_ptr) ? ((r_ptr->aaf > MAX_SIGHT) ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
4332                         {
4333                                 /* We may wake up */
4334                                 test = TRUE;
4335                         }
4336
4337                         /* Handle "sight" and "aggravation" */
4338                         else if ((m_ptr->cdis <= MAX_SIGHT) && (player_has_los_bold(m_ptr->fy, m_ptr->fx)))
4339                         {
4340                                 /* We may wake up */
4341                                 test = TRUE;
4342                         }
4343                 }
4344
4345                 if (test)
4346                 {
4347                         u32b notice = randint0(1024);
4348
4349                         /* Nightmare monsters are more alert */
4350                         if (ironman_nightmare) notice /= 2;
4351
4352                         /* Hack -- See if monster "notices" player */
4353                         if ((notice * notice * notice) <= csleep_noise)
4354                         {
4355                                 /* Hack -- amount of "waking" */
4356                                 /* Wake up faster near the player */
4357                                 int d = (m_ptr->cdis < AAF_LIMIT / 2) ? (AAF_LIMIT / m_ptr->cdis) : 1;
4358
4359                                 /* Hack -- amount of "waking" is affected by speed of player */
4360                                 d = (d * SPEED_TO_ENERGY(p_ptr->pspeed)) / 10;
4361                                 if (d < 0) d = 1;
4362
4363                                 /* Monster wakes up "a little bit" */
4364
4365                                 /* Still asleep */
4366                                 if (!set_monster_csleep(m_idx, MON_CSLEEP(m_ptr) - d))
4367                                 {
4368                                         /* Notice the "not waking up" */
4369                                         if (is_original_ap_and_seen(m_ptr))
4370                                         {
4371                                                 /* Hack -- Count the ignores */
4372                                                 if (r_ptr->r_ignore < MAX_UCHAR) r_ptr->r_ignore++;
4373                                         }
4374                                 }
4375
4376                                 /* Just woke up */
4377                                 else
4378                                 {
4379                                         /* Notice the "waking up" */
4380                                         if (m_ptr->ml)
4381                                         {
4382                                                 char m_name[80];
4383
4384                                                 /* Acquire the monster name */
4385                                                 monster_desc(m_name, m_ptr, 0);
4386
4387                                                 /* Dump a message */
4388 #ifdef JP
4389                                                 msg_format("%^sが目を覚ました。", m_name);
4390 #else
4391                                                 msg_format("%^s wakes up.", m_name);
4392 #endif
4393                                         }
4394
4395                                         if (is_original_ap_and_seen(m_ptr))
4396                                         {
4397                                                 /* Hack -- Count the wakings */
4398                                                 if (r_ptr->r_wake < MAX_UCHAR) r_ptr->r_wake++;
4399                                         }
4400                                 }
4401                         }
4402                 }
4403                 break;
4404         }
4405
4406         case MTIMED_FAST:
4407                 /* Reduce by one, note if expires */
4408                 if (set_monster_fast(m_idx, MON_FAST(m_ptr) - 1))
4409                 {
4410                         if (is_seen(m_ptr))
4411                         {
4412                                 char m_name[80];
4413
4414                                 /* Acquire the monster name */
4415                                 monster_desc(m_name, m_ptr, 0);
4416
4417                                 /* Dump a message */
4418 #ifdef JP
4419                                 msg_format("%^sはもう加速されていない。", m_name);
4420 #else
4421                                 msg_format("%^s is no longer fast.", m_name);
4422 #endif
4423                         }
4424                 }
4425                 break;
4426
4427         case MTIMED_SLOW:
4428                 /* Reduce by one, note if expires */
4429                 if (set_monster_slow(m_idx, MON_SLOW(m_ptr) - 1))
4430                 {
4431                         if (is_seen(m_ptr))
4432                         {
4433                                 char m_name[80];
4434
4435                                 /* Acquire the monster name */
4436                                 monster_desc(m_name, m_ptr, 0);
4437
4438                                 /* Dump a message */
4439 #ifdef JP
4440                                 msg_format("%^sはもう減速されていない。", m_name);
4441 #else
4442                                 msg_format("%^s is no longer slow.", m_name);
4443 #endif
4444                         }
4445                 }
4446                 break;
4447
4448         case MTIMED_STUNNED:
4449         {
4450                 int rlev = r_info[m_ptr->r_idx].level;
4451
4452                 /* Recover from stun */
4453                 if (set_monster_stunned(m_idx, (randint0(10000) <= rlev * rlev) ? 0 : (MON_STUNNED(m_ptr) - 1)))
4454                 {
4455                         /* Message if visible */
4456                         if (is_seen(m_ptr))
4457                         {
4458                                 char m_name[80];
4459
4460                                 /* Acquire the monster name */
4461                                 monster_desc(m_name, m_ptr, 0);
4462
4463                                 /* Dump a message */
4464 #ifdef JP
4465                                 msg_format("%^sは朦朧状態から立ち直った。", m_name);
4466 #else
4467                                 msg_format("%^s is no longer stunned.", m_name);
4468 #endif
4469                         }
4470                 }
4471                 break;
4472         }
4473
4474         case MTIMED_CONFUSED:
4475                 /* Reduce the confusion */
4476                 if (set_monster_confused(m_idx, MON_CONFUSED(m_ptr) - randint1(r_info[m_ptr->r_idx].level / 20 + 1)))
4477                 {
4478                         /* Message if visible */
4479                         if (is_seen(m_ptr))
4480                         {
4481                                 char m_name[80];
4482
4483                                 /* Acquire the monster name */
4484                                 monster_desc(m_name, m_ptr, 0);
4485
4486                                 /* Dump a message */
4487 #ifdef JP
4488                                 msg_format("%^sは混乱から立ち直った。", m_name);
4489 #else
4490                                 msg_format("%^s is no longer confused.", m_name);
4491 #endif
4492                         }
4493                 }
4494                 break;
4495
4496         case MTIMED_MONFEAR:
4497                 /* Reduce the fear */
4498                 if (set_monster_monfear(m_idx, MON_MONFEAR(m_ptr) - randint1(r_info[m_ptr->r_idx].level / 20 + 1)))
4499                 {
4500                         /* Visual note */
4501                         if (is_seen(m_ptr))
4502                         {
4503                                 char m_name[80];
4504 #ifndef JP
4505                                 char m_poss[80];
4506
4507                                 /* Acquire the monster possessive */
4508                                 monster_desc(m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
4509 #endif
4510
4511                                 /* Acquire the monster name */
4512                                 monster_desc(m_name, m_ptr, 0);
4513
4514                                 /* Dump a message */
4515 #ifdef JP
4516                                 msg_format("%^sは勇気を取り戻した。", m_name);
4517 #else
4518                                 msg_format("%^s recovers %s courage.", m_name, m_poss);
4519 #endif
4520                         }
4521                 }
4522                 break;
4523
4524         case MTIMED_INVULNER:
4525                 /* Reduce by one, note if expires */
4526                 if (set_monster_invulner(m_idx, MON_INVULNER(m_ptr) - 1, TRUE))
4527                 {
4528                         if (is_seen(m_ptr))
4529                         {
4530                                 char m_name[80];
4531
4532                                 /* Acquire the monster name */
4533                                 monster_desc(m_name, m_ptr, 0);
4534
4535                                 /* Dump a message */
4536 #ifdef JP
4537                                 msg_format("%^sはもう無敵でない。", m_name);
4538 #else
4539                                 msg_format("%^s is no longer invulnerable.", m_name);
4540 #endif
4541                         }
4542                 }
4543                 break;
4544         }
4545 }
4546
4547
4548 /*!
4549  * @brief 全モンスターの各種状態値を時間経過により更新するメインルーチン
4550  * @param mtimed_idx 更新するモンスターの時限ステータスID
4551  * @return なし
4552  * @details
4553  * Process the counters of monsters (once per 10 game turns)\n
4554  * These functions are to process monsters' counters same as player's.
4555  */
4556 void process_monsters_mtimed(int mtimed_idx)
4557 {
4558         int  i;
4559         s16b *cur_mproc_list = mproc_list[mtimed_idx];
4560
4561         /* Hack -- calculate the "player noise" */
4562         if (mtimed_idx == MTIMED_CSLEEP) csleep_noise = (1L << (30 - p_ptr->skill_stl));
4563
4564         /* Process the monsters (backwards) */
4565         for (i = mproc_max[mtimed_idx] - 1; i >= 0; i--)
4566         {
4567                 /* Access the monster */
4568                 process_monsters_mtimed_aux(cur_mproc_list[i], mtimed_idx);
4569         }
4570 }
4571
4572 /*!
4573  * @brief モンスターへの魔力消去処理
4574  * @param m_idx 魔力消去を受けるモンスターの参照ID
4575  * @return なし
4576  */
4577 void dispel_monster_status(int m_idx)
4578 {
4579         monster_type *m_ptr = &m_list[m_idx];
4580         char         m_name[80];
4581
4582         monster_desc(m_name, m_ptr, 0);
4583         if (set_monster_invulner(m_idx, 0, TRUE))
4584         {
4585 #ifdef JP
4586                 if (m_ptr->ml) msg_format("%sはもう無敵ではない。", m_name);
4587 #else
4588                 if (m_ptr->ml) msg_format("%^s is no longer invulnerable.", m_name);
4589 #endif
4590         }
4591         if (set_monster_fast(m_idx, 0))
4592         {
4593 #ifdef JP
4594                 if (m_ptr->ml) msg_format("%sはもう加速されていない。", m_name);
4595 #else
4596                 if (m_ptr->ml) msg_format("%^s is no longer fast.", m_name);
4597 #endif
4598         }
4599         if (set_monster_slow(m_idx, 0))
4600         {
4601 #ifdef JP
4602                 if (m_ptr->ml) msg_format("%sはもう減速されていない。", m_name);
4603 #else
4604                 if (m_ptr->ml) msg_format("%^s is no longer slow.", m_name);
4605 #endif
4606         }
4607 }
4608
4609 /*!
4610  * @brief モンスターの時間停止処理
4611  * @param num 時間停止を行った敵が行動できる回数
4612  * @param who 時間停止処理の主体ID
4613  * @param vs_player TRUEならば時間停止開始処理を行う
4614  * @return 時間停止が行われている状態ならばTRUEを返す
4615  */
4616 bool process_the_world(int num, int who, bool vs_player)
4617 {
4618         monster_type *m_ptr = &m_list[hack_m_idx];  /* the world monster */
4619
4620         if(world_monster) return (FALSE);
4621
4622         if(vs_player)
4623         {
4624                 char m_name[80];
4625                 monster_desc(m_name, m_ptr, 0);
4626
4627                 if (who == 1)
4628 #ifdef JP
4629                         msg_print("「『ザ・ワールド』!時は止まった!」");
4630 #else
4631                         msg_format("%s yells 'The World! Time has stopped!'", m_name);
4632 #endif
4633                 else if (who == 3)
4634 #ifdef JP
4635                         msg_print("「時よ!」");
4636 #else
4637                         msg_format("%s yells 'Time!'", m_name);
4638 #endif
4639                 else msg_print("hek!");
4640
4641                 msg_print(NULL);
4642         }
4643
4644         /* This monster cast spells */
4645         world_monster = hack_m_idx;
4646
4647         if (vs_player) do_cmd_redraw();
4648
4649         while(num--)
4650         {
4651                 if(!m_ptr->r_idx) break;
4652                 process_monster(world_monster);
4653
4654                 reset_target(m_ptr);
4655
4656                 /* Notice stuff */
4657                 if (p_ptr->notice) notice_stuff();
4658
4659                 /* Update stuff */
4660                 if (p_ptr->update) update_stuff();
4661
4662                 /* Redraw stuff */
4663                 if (p_ptr->redraw) redraw_stuff();
4664
4665                 /* Redraw stuff */
4666                 if (p_ptr->window) window_stuff();
4667
4668                 /* Delay */
4669                 if (vs_player) Term_xtra(TERM_XTRA_DELAY, 500);
4670         }
4671
4672         /* Redraw map */
4673         p_ptr->redraw |= (PR_MAP);
4674
4675         /* Update monsters */
4676         p_ptr->update |= (PU_MONSTERS);
4677
4678         /* Window stuff */
4679         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
4680
4681         world_monster = 0;
4682         if (vs_player || (player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(py, px, m_ptr->fy, m_ptr->fx)))
4683         {
4684 #ifdef JP
4685                 msg_print("「時は動きだす…」");
4686 #else
4687                 msg_print("You feel time flowing around you once more.");
4688 #endif
4689                 msg_print(NULL);
4690         }
4691
4692         handle_stuff();
4693
4694         return (TRUE);
4695 }
4696
4697 /*!
4698  * @brief モンスターの経験値取得処理
4699  * @param m_idx 経験値を得るモンスターの参照ID
4700  * @param s_idx 撃破されたモンスター種族の参照ID
4701  * @return なし
4702  */
4703 void monster_gain_exp(int m_idx, int s_idx)
4704 {
4705         monster_type *m_ptr;
4706         monster_race *r_ptr;
4707         monster_race *s_ptr;
4708         int new_exp;
4709
4710         /* Paranoia */
4711         if (m_idx <= 0 || s_idx <= 0) return;
4712
4713         m_ptr = &m_list[m_idx];
4714
4715         /* Paranoia -- Skip dead monsters */
4716         if (!m_ptr->r_idx) return;
4717
4718         r_ptr = &r_info[m_ptr->r_idx];
4719         s_ptr = &r_info[s_idx];
4720
4721         if (p_ptr->inside_battle) return;
4722
4723         if (!r_ptr->next_exp) return;
4724
4725         new_exp = s_ptr->mexp * s_ptr->level / (r_ptr->level + 2);
4726         if (m_idx == p_ptr->riding) new_exp = (new_exp + 1) / 2;
4727         if (!dun_level) new_exp /= 5;
4728         m_ptr->exp += new_exp;
4729         if (m_ptr->mflag2 & MFLAG2_CHAMELEON) return;
4730
4731         if (m_ptr->exp >= r_ptr->next_exp)
4732         {
4733                 char m_name[80];
4734                 int old_hp = m_ptr->hp;
4735                 int old_maxhp = m_ptr->max_maxhp;
4736                 int old_r_idx = m_ptr->r_idx;
4737                 byte old_sub_align = m_ptr->sub_align;
4738
4739                 /* Hack -- Reduce the racial counter of previous monster */
4740                 real_r_ptr(m_ptr)->cur_num--;
4741
4742                 monster_desc(m_name, m_ptr, 0);
4743                 m_ptr->r_idx = r_ptr->next_r_idx;
4744
4745                 /* Count the monsters on the level */
4746                 real_r_ptr(m_ptr)->cur_num++;
4747
4748                 m_ptr->ap_r_idx = m_ptr->r_idx;
4749                 r_ptr = &r_info[m_ptr->r_idx];
4750
4751                 if (r_ptr->flags1 & RF1_FORCE_MAXHP)
4752                 {
4753                         m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
4754                 }
4755                 else
4756                 {
4757                         m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
4758                 }
4759                 if (ironman_nightmare)
4760                 {
4761                         u32b hp = m_ptr->max_maxhp * 2L;
4762
4763                         m_ptr->max_maxhp = (s16b)MIN(30000, hp);
4764                 }
4765                 m_ptr->maxhp = m_ptr->max_maxhp;
4766                 m_ptr->hp = old_hp * m_ptr->maxhp / old_maxhp;
4767                 
4768                 /* dealt damage is 0 at initial*/
4769                 m_ptr->dealt_damage = 0;
4770
4771                 /* Extract the monster base speed */
4772                 m_ptr->mspeed = get_mspeed(r_ptr);
4773
4774                 /* Sub-alignment of a monster */
4775                 if (!is_pet(m_ptr) && !(r_ptr->flags3 & (RF3_EVIL | RF3_GOOD)))
4776                         m_ptr->sub_align = old_sub_align;
4777                 else
4778                 {
4779                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
4780                         if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
4781                         if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
4782                 }
4783
4784                 m_ptr->exp = 0;
4785
4786                 if (is_pet(m_ptr) || m_ptr->ml)
4787                 {
4788                         if (!ignore_unview || player_can_see_bold(m_ptr->fy, m_ptr->fx))
4789                         {
4790                                 if (p_ptr->image)
4791                                 {
4792                                         monster_race *hallu_race;
4793
4794                                         do
4795                                         {
4796                                                 hallu_race = &r_info[randint1(max_r_idx - 1)];
4797                                         }
4798                                         while (!hallu_race->name || (hallu_race->flags1 & RF1_UNIQUE));
4799
4800 #ifdef JP
4801                                         msg_format("%sは%sに進化した。", m_name, r_name + hallu_race->name);
4802 #else
4803                                         msg_format("%^s evolved into %s.", m_name, r_name + hallu_race->name);
4804 #endif
4805                                 }
4806                                 else
4807                                 {
4808 #ifdef JP
4809                                         msg_format("%sは%sに進化した。", m_name, r_name + r_ptr->name);
4810 #else
4811                                         msg_format("%^s evolved into %s.", m_name, r_name + r_ptr->name);
4812 #endif
4813                                 }
4814                         }
4815
4816                         if (!p_ptr->image) r_info[old_r_idx].r_xtra1 |= MR1_SINKA;
4817
4818                         /* Now you feel very close to this pet. */
4819                         m_ptr->parent_m_idx = 0;
4820                 }
4821                 update_mon(m_idx, FALSE);
4822                 lite_spot(m_ptr->fy, m_ptr->fx);
4823         }
4824         if (m_idx == p_ptr->riding) p_ptr->update |= PU_BONUS;
4825 }