OSDN Git Service

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