OSDN Git Service

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