OSDN Git Service

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