OSDN Git Service

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