OSDN Git Service

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