OSDN Git Service

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