OSDN Git Service

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