OSDN Git Service

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