OSDN Git Service

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