OSDN Git Service

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