OSDN Git Service

los()やplayer_has_los_*()の使用に関する変更. 主に透明な壁の作成を想定
[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         cave_type *c_ptr;
1026
1027         /* Start with adjacent locations, spread further */
1028         for (d = 1; d < 10; d++)
1029         {
1030                 /* Get the lists of points with a distance d from (fx, fy) */
1031                 y_offsets = dist_offsets_y[d];
1032                 x_offsets = dist_offsets_x[d];
1033
1034                 /* Check the locations */
1035                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
1036                      dx != 0 || dy != 0;
1037                      i++, dx = x_offsets[i], dy = y_offsets[i])
1038                 {
1039                         y = fy + dy;
1040                         x = fx + dx;
1041                         
1042                         /* Skip illegal locations */
1043                         if (!in_bounds(y, x)) continue;
1044
1045                         c_ptr = &cave[y][x];
1046
1047                         /* Skip occupied locations */
1048                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
1049
1050                         /* Check for hidden, available grid */
1051                         if (!player_has_los_grid(c_ptr) && clean_shot(fy, fx, y, x, FALSE))
1052                         {
1053                                 /* Calculate distance from player */
1054                                 dis = distance(y, x, py, px);
1055
1056                                 /* Remember if closer than previous */
1057                                 if (dis < gdis && dis >= 2)
1058                                 {
1059                                         gy = y;
1060                                         gx = x;
1061                                         gdis = dis;
1062                                 }
1063                         }
1064                 }
1065
1066                 /* Check for success */
1067                 if (gdis < 999)
1068                 {
1069                         /* Good location */
1070                         (*yp) = fy - gy;
1071                         (*xp) = fx - gx;
1072
1073                         /* Found good place */
1074                         return (TRUE);
1075                 }
1076         }
1077
1078         /* No good place */
1079         return (FALSE);
1080 }
1081
1082
1083 /*
1084  * Choose "logical" directions for monster movement
1085  */
1086 static bool get_moves(int m_idx, int *mm)
1087 {
1088         monster_type *m_ptr = &m_list[m_idx];
1089         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1090         int          y, ay, x, ax;
1091         int          move_val = 0;
1092         int          y2 = py;
1093         int          x2 = px;
1094         bool         done = FALSE;
1095         bool         will_run = mon_will_run(m_idx);
1096         cave_type    *c_ptr;
1097         bool         no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (cave[m_ptr->fy][m_ptr->fx].cost > 2));
1098         bool         can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall));
1099
1100         /* Counter attack to an enemy monster */
1101         if (!will_run && m_ptr->target_y)
1102         {
1103                 int t_m_idx = cave[m_ptr->target_y][m_ptr->target_x].m_idx;
1104
1105                 /* The monster must be an enemy, and in LOS */
1106                 if (t_m_idx &&
1107                     are_enemies(m_ptr, &m_list[t_m_idx]) &&
1108                     los(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
1109                     projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1110                 {
1111                         /* Extract the "pseudo-direction" */
1112                         y = m_ptr->fy - m_ptr->target_y;
1113                         x = m_ptr->fx - m_ptr->target_x;
1114                         done = TRUE;
1115                 }
1116         }
1117
1118         if (!done && !will_run && is_hostile(m_ptr) &&
1119             (r_ptr->flags1 & RF1_FRIENDS) &&
1120             ((los(m_ptr->fy, m_ptr->fx, py, px) && projectable(m_ptr->fy, m_ptr->fx, py, px)) ||
1121             (cave[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
1122         {
1123         /*
1124          * Animal packs try to get the player out of corridors
1125          * (...unless they can move through walls -- TY)
1126          */
1127                 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
1128                          !(r_ptr->flags2 & RF2_KILL_WALL))
1129                 {
1130                         int i, room = 0;
1131
1132                         /* Count room grids next to player */
1133                         for (i = 0; i < 8; i++)
1134                         {
1135                                 int xx = px + ddx_ddd[i];
1136                                 int yy = py + ddy_ddd[i];
1137
1138                                 if (!in_bounds2(yy, xx)) continue;
1139
1140                                 c_ptr = &cave[yy][xx];
1141
1142                                 /* Check grid */
1143                                 if (monster_can_cross_terrain(c_ptr->feat, r_ptr, 0))
1144                                 {
1145                                         /* One more room grid */
1146                                         room++;
1147                                 }
1148                         }
1149                         if (cave[py][px].info & CAVE_ROOM) room -= 2;
1150                         if (!r_ptr->flags4 && !r_ptr->flags5 && !r_ptr->flags6) room -= 2;
1151
1152                         /* Not in a room and strong player */
1153                         if (room < (8 * (p_ptr->chp + p_ptr->csp)) /
1154                             (p_ptr->mhp + p_ptr->msp))
1155                         {
1156                                 /* Find hiding place */
1157                                 if (find_hiding(m_idx, &y, &x)) done = TRUE;
1158                         }
1159                 }
1160
1161                 /* Monster groups try to surround the player */
1162                 if (!done && (cave[m_ptr->fy][m_ptr->fx].dist < 3))
1163                 {
1164                         int i;
1165
1166                         /* Find an empty square near the player to fill */
1167                         for (i = 0; i < 8; i++)
1168                         {
1169                                 /* Pick squares near player (semi-randomly) */
1170                                 y2 = py + ddy_ddd[(m_idx + i) & 7];
1171                                 x2 = px + ddx_ddd[(m_idx + i) & 7];
1172
1173                                 /* Already there? */
1174                                 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
1175                                 {
1176                                         /* Attack the player */
1177                                         y2 = py;
1178                                         x2 = px;
1179
1180                                         break;
1181                                 }
1182
1183                                 if (!in_bounds2(y2, x2)) continue;
1184
1185                                 /* Ignore filled grids */
1186                                 if (!monster_can_enter(y2, x2, r_ptr, 0)) continue;
1187
1188                                 /* Try to fill this hole */
1189                                 break;
1190                         }
1191
1192                         /* Extract the new "pseudo-direction" */
1193                         y = m_ptr->fy - y2;
1194                         x = m_ptr->fx - x2;
1195
1196                         /* Done */
1197                         done = TRUE;
1198                 }
1199         }
1200
1201         if (!done)
1202         {
1203                 /* Flow towards the player */
1204                 (void)get_moves_aux(m_idx, &y2, &x2, no_flow);
1205
1206                 /* Extract the "pseudo-direction" */
1207                 y = m_ptr->fy - y2;
1208                 x = m_ptr->fx - x2;
1209
1210                 /* Not done */
1211         }
1212
1213         /* Apply fear if possible and necessary */
1214         if (is_pet(m_ptr) && will_run)
1215         {
1216                 /* XXX XXX Not very "smart" */
1217                 y = (-y), x = (-x);
1218         }
1219         else
1220         {
1221                 if (!done && will_run)
1222                 {
1223                         int tmp_x = (-x);
1224                         int tmp_y = (-y);
1225
1226                         /* Try to find safe place */
1227                         if (find_safety(m_idx, &y, &x))
1228                         {
1229                                 /* Attempt to avoid the player */
1230                                 if (!no_flow)
1231                                 {
1232                                         /* Adjust movement */
1233                                         if (get_fear_moves_aux(m_idx, &y, &x)) done = TRUE;
1234                                 }
1235                         }
1236
1237                         if (!done)
1238                         {
1239                                 /* This is not a very "smart" method XXX XXX */
1240                                 y = tmp_y;
1241                                 x = tmp_x;
1242                         }
1243                 }
1244         }
1245
1246
1247         /* Check for no move */
1248         if (!x && !y) return (FALSE);
1249
1250
1251         /* Extract the "absolute distances" */
1252         ax = ABS(x);
1253         ay = ABS(y);
1254
1255         /* Do something weird */
1256         if (y < 0) move_val += 8;
1257         if (x > 0) move_val += 4;
1258
1259         /* Prevent the diamond maneuvre */
1260         if (ay > (ax << 1)) move_val += 2;
1261         else if (ax > (ay << 1)) move_val++;
1262
1263         /* Extract some directions */
1264         switch (move_val)
1265         {
1266         case 0:
1267                 mm[0] = 9;
1268                 if (ay > ax)
1269                 {
1270                         mm[1] = 8;
1271                         mm[2] = 6;
1272                         mm[3] = 7;
1273                         mm[4] = 3;
1274                 }
1275                 else
1276                 {
1277                         mm[1] = 6;
1278                         mm[2] = 8;
1279                         mm[3] = 3;
1280                         mm[4] = 7;
1281                 }
1282                 break;
1283         case 1:
1284         case 9:
1285                 mm[0] = 6;
1286                 if (y < 0)
1287                 {
1288                         mm[1] = 3;
1289                         mm[2] = 9;
1290                         mm[3] = 2;
1291                         mm[4] = 8;
1292                 }
1293                 else
1294                 {
1295                         mm[1] = 9;
1296                         mm[2] = 3;
1297                         mm[3] = 8;
1298                         mm[4] = 2;
1299                 }
1300                 break;
1301         case 2:
1302         case 6:
1303                 mm[0] = 8;
1304                 if (x < 0)
1305                 {
1306                         mm[1] = 9;
1307                         mm[2] = 7;
1308                         mm[3] = 6;
1309                         mm[4] = 4;
1310                 }
1311                 else
1312                 {
1313                         mm[1] = 7;
1314                         mm[2] = 9;
1315                         mm[3] = 4;
1316                         mm[4] = 6;
1317                 }
1318                 break;
1319         case 4:
1320                 mm[0] = 7;
1321                 if (ay > ax)
1322                 {
1323                         mm[1] = 8;
1324                         mm[2] = 4;
1325                         mm[3] = 9;
1326                         mm[4] = 1;
1327                 }
1328                 else
1329                 {
1330                         mm[1] = 4;
1331                         mm[2] = 8;
1332                         mm[3] = 1;
1333                         mm[4] = 9;
1334                 }
1335                 break;
1336         case 5:
1337         case 13:
1338                 mm[0] = 4;
1339                 if (y < 0)
1340                 {
1341                         mm[1] = 1;
1342                         mm[2] = 7;
1343                         mm[3] = 2;
1344                         mm[4] = 8;
1345                 }
1346                 else
1347                 {
1348                         mm[1] = 7;
1349                         mm[2] = 1;
1350                         mm[3] = 8;
1351                         mm[4] = 2;
1352                 }
1353                 break;
1354         case 8:
1355                 mm[0] = 3;
1356                 if (ay > ax)
1357                 {
1358                         mm[1] = 2;
1359                         mm[2] = 6;
1360                         mm[3] = 1;
1361                         mm[4] = 9;
1362                 }
1363                 else
1364                 {
1365                         mm[1] = 6;
1366                         mm[2] = 2;
1367                         mm[3] = 9;
1368                         mm[4] = 1;
1369                 }
1370                 break;
1371         case 10:
1372         case 14:
1373                 mm[0] = 2;
1374                 if (x < 0)
1375                 {
1376                         mm[1] = 3;
1377                         mm[2] = 1;
1378                         mm[3] = 6;
1379                         mm[4] = 4;
1380                 }
1381                 else
1382                 {
1383                         mm[1] = 1;
1384                         mm[2] = 3;
1385                         mm[3] = 4;
1386                         mm[4] = 6;
1387                 }
1388                 break;
1389         case 12:
1390                 mm[0] = 1;
1391                 if (ay > ax)
1392                 {
1393                         mm[1] = 2;
1394                         mm[2] = 4;
1395                         mm[3] = 3;
1396                         mm[4] = 7;
1397                 }
1398                 else
1399                 {
1400                         mm[1] = 4;
1401                         mm[2] = 2;
1402                         mm[3] = 7;
1403                         mm[4] = 3;
1404                 }
1405                 break;
1406         }
1407
1408         /* Wants to move... */
1409         return (TRUE);
1410 }
1411
1412
1413 static int check_hit2(int power, int level, int ac, int stun)
1414 {
1415         int i, k;
1416
1417         /* Percentile dice */
1418         k = randint0(100);
1419
1420         if (stun && one_in_(2)) return FALSE;
1421
1422         /* Hack -- Always miss or hit */
1423         if (k < 10) return (k < 5);
1424
1425         /* Calculate the "attack quality" */
1426         i = (power + (level * 3));
1427
1428         /* Power and Level compete against Armor */
1429         if ((i > 0) && (randint1(i) > ((ac * 3) / 4))) return (TRUE);
1430
1431         /* Assume miss */
1432         return (FALSE);
1433 }
1434
1435
1436 /* Monster attacks monster */
1437 static bool monst_attack_monst(int m_idx, int t_idx)
1438 {
1439         monster_type    *m_ptr = &m_list[m_idx];
1440         monster_type    *t_ptr = &m_list[t_idx];
1441
1442         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
1443         monster_race    *tr_ptr = &r_info[t_ptr->r_idx];
1444
1445         int             ap_cnt;
1446         int             ac, rlev, pt;
1447         char            m_name[80], t_name[80];
1448         char            temp[80];
1449         bool            blinked, heal_effect;
1450         bool            explode = FALSE, touched = FALSE, fear = FALSE;
1451         int             y_saver = t_ptr->fy;
1452         int             x_saver = t_ptr->fx;
1453
1454         bool see_m = m_ptr->ml;
1455         bool see_t = t_ptr->ml;
1456         bool see_either = see_m || see_t;
1457
1458         /* Can the player be aware of this attack? */
1459         bool known = (m_ptr->cdis <= MAX_SIGHT) || (t_ptr->cdis <= MAX_SIGHT);
1460         bool do_silly_attack = (one_in_(2) && p_ptr->image);
1461
1462         /* Cannot attack self */
1463         if (m_idx == t_idx) return FALSE;
1464
1465         /* Not allowed to attack */
1466         if (r_ptr->flags1 & RF1_NEVER_BLOW) return FALSE;
1467
1468         if (d_info[dungeon_type].flags1 & DF1_NO_MELEE) return (FALSE);
1469
1470         /* Total armor */
1471         ac = tr_ptr->ac;
1472
1473         /* Extract the effective monster level */
1474         rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1475
1476         /* Get the monster name (or "it") */
1477         monster_desc(m_name, m_ptr, 0);
1478
1479         /* Get the monster name (or "it") */
1480         monster_desc(t_name, t_ptr, 0);
1481
1482         /* Assume no blink */
1483         blinked = FALSE;
1484
1485         if (!see_either && known)
1486         {
1487                 mon_fight = TRUE;
1488         }
1489
1490         if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(1, 0);
1491
1492         /* Scan through all four blows */
1493         for (ap_cnt = 0; ap_cnt < 4; ap_cnt++)
1494         {
1495                 bool obvious = FALSE;
1496
1497                 int power = 0;
1498                 int damage = 0;
1499
1500                 cptr act = NULL;
1501
1502                 /* Extract the attack infomation */
1503                 int effect = r_ptr->blow[ap_cnt].effect;
1504                 int method = r_ptr->blow[ap_cnt].method;
1505                 int d_dice = r_ptr->blow[ap_cnt].d_dice;
1506                 int d_side = r_ptr->blow[ap_cnt].d_side;
1507
1508                 if (!m_ptr->r_idx) break;
1509
1510                 /* Stop attacking if the target dies! */
1511                 if (t_ptr->fx != x_saver || t_ptr->fy != y_saver)
1512                         break;
1513
1514                 /* Hack -- no more attacks */
1515                 if (!method) break;
1516
1517                 if (blinked) /* Stop! */
1518                 {
1519                         /* break; */
1520                 }
1521
1522                 if (method == RBM_SHOOT) continue;
1523
1524                 /* Extract the attack "power" */
1525                 power = mbe_info[effect].power;
1526
1527                 /* Monster hits */
1528                 if (!effect || check_hit2(power, rlev, ac, m_ptr->stunned))
1529                 {
1530                         /* Wake it up */
1531                         t_ptr->csleep = 0;
1532
1533                         if (tr_ptr->flags7 & RF7_HAS_LD_MASK) p_ptr->update |= (PU_MON_LITE);
1534
1535                         /* Describe the attack method */
1536                         switch (method)
1537                         {
1538                         case RBM_HIT:
1539                                 {
1540 #ifdef JP
1541 act = "%s¤ò²¥¤Ã¤¿¡£";
1542 #else
1543                                         act = "hits %s.";
1544 #endif
1545
1546                                         touched = TRUE;
1547                                         break;
1548                                 }
1549
1550                         case RBM_TOUCH:
1551                                 {
1552 #ifdef JP
1553 act = "%s¤ò¿¨¤Ã¤¿¡£";
1554 #else
1555                                         act = "touches %s.";
1556 #endif
1557
1558                                         touched = TRUE;
1559                                         break;
1560                                 }
1561
1562                         case RBM_PUNCH:
1563                                 {
1564 #ifdef JP
1565 act = "%s¤ò¥Ñ¥ó¥Á¤·¤¿¡£";
1566 #else
1567                                         act = "punches %s.";
1568 #endif
1569
1570                                         touched = TRUE;
1571                                         break;
1572                                 }
1573
1574                         case RBM_KICK:
1575                                 {
1576 #ifdef JP
1577 act = "%s¤ò½³¤Ã¤¿¡£";
1578 #else
1579                                         act = "kicks %s.";
1580 #endif
1581
1582                                         touched = TRUE;
1583                                         break;
1584                                 }
1585
1586                         case RBM_CLAW:
1587                                 {
1588 #ifdef JP
1589 act = "%s¤ò¤Ò¤Ã¤«¤¤¤¿¡£";
1590 #else
1591                                         act = "claws %s.";
1592 #endif
1593
1594                                         touched = TRUE;
1595                                         break;
1596                                 }
1597
1598                         case RBM_BITE:
1599                                 {
1600 #ifdef JP
1601 act = "%s¤ò³ú¤ó¤À¡£";
1602 #else
1603                                         act = "bites %s.";
1604 #endif
1605
1606                                         touched = TRUE;
1607                                         break;
1608                                 }
1609
1610                         case RBM_STING:
1611                                 {
1612 #ifdef JP
1613 act = "%s¤ò»É¤·¤¿¡£";
1614 #else
1615                                         act = "stings %s.";
1616 #endif
1617
1618                                         touched = TRUE;
1619                                         break;
1620                                 }
1621
1622                         case RBM_SLASH:
1623                                 {
1624 #ifdef JP
1625 act = "%s¤ò»Â¤Ã¤¿¡£";
1626 #else
1627                                         act = "slashes %s.";
1628 #endif
1629
1630                                         break;
1631                                 }
1632
1633                         case RBM_BUTT:
1634                                 {
1635 #ifdef JP
1636 act = "%s¤ò³Ñ¤ÇÆͤ¤¤¿¡£";
1637 #else
1638                                         act = "butts %s.";
1639 #endif
1640
1641                                         touched = TRUE;
1642                                         break;
1643                                 }
1644
1645                         case RBM_CRUSH:
1646                                 {
1647 #ifdef JP
1648 act = "%s¤ËÂÎÅö¤ê¤·¤¿¡£";
1649 #else
1650                                         act = "crushes %s.";
1651 #endif
1652
1653                                         touched = TRUE;
1654                                         break;
1655                                 }
1656
1657                         case RBM_ENGULF:
1658                                 {
1659 #ifdef JP
1660 act = "%s¤ò°û¤ß¹þ¤ó¤À¡£";
1661 #else
1662                                         act = "engulfs %s.";
1663 #endif
1664
1665                                         touched = TRUE;
1666                                         break;
1667                                 }
1668
1669                         case RBM_CHARGE:
1670                                 {
1671 #ifdef JP
1672 act = "%s¤ËÀÁµá½ñ¤ò¤è¤³¤·¤¿¡£";
1673 #else
1674                                         act = "charges %s.";
1675 #endif
1676
1677                                         touched = TRUE;
1678                                         break;
1679                                 }
1680
1681                         case RBM_CRAWL:
1682                                 {
1683 #ifdef JP
1684 act = "%s¤ÎÂΤξå¤òÇ礤²ó¤Ã¤¿¡£";
1685 #else
1686                                         act = "crawls on %s.";
1687 #endif
1688
1689                                         touched = TRUE;
1690                                         break;
1691                                 }
1692
1693                         case RBM_DROOL:
1694                                 {
1695 #ifdef JP
1696 act = "%s¤Ë¤è¤À¤ì¤ò¤¿¤é¤·¤¿¡£";
1697 #else
1698                                         act = "drools on %s.";
1699 #endif
1700
1701                                         touched = FALSE;
1702                                         break;
1703                                 }
1704
1705                         case RBM_SPIT:
1706                                 {
1707 #ifdef JP
1708 act = "%s¤ËÂäòÅǤ¤¤¿¡£";
1709 #else
1710                                         act = "spits on %s.";
1711 #endif
1712
1713                                         touched = FALSE;
1714                                         break;
1715                                 }
1716
1717                         case RBM_EXPLODE:
1718                                 {
1719                                         if (see_either) disturb(1, 0);
1720 #ifdef JP
1721 act = "Çúȯ¤·¤¿¡£";
1722 #else
1723                                         act = "explodes.";
1724 #endif
1725
1726                                         explode = TRUE;
1727                                         touched = FALSE;
1728                                         break;
1729                                 }
1730
1731                         case RBM_GAZE:
1732                                 {
1733 #ifdef JP
1734 act = "%s¤ò¤Ë¤é¤ó¤À¡£";
1735 #else
1736                                         act = "gazes at %s.";
1737 #endif
1738
1739                                         touched = FALSE;
1740                                         break;
1741                                 }
1742
1743                         case RBM_WAIL:
1744                                 {
1745 #ifdef JP
1746 act = "%s¤Ëµã¤­¤Ä¤¤¤¿¡£";
1747 #else
1748                                         act = "wails at %s.";
1749 #endif
1750
1751                                         touched = FALSE;
1752                                         break;
1753                                 }
1754
1755                         case RBM_SPORE:
1756                                 {
1757 #ifdef JP
1758 act = "%s¤Ë˦»Ò¤òÈô¤Ð¤·¤¿¡£";
1759 #else
1760                                         act = "releases spores at %s.";
1761 #endif
1762
1763                                         touched = FALSE;
1764                                         break;
1765                                 }
1766
1767                         case RBM_XXX4:
1768                                 {
1769 #ifdef JP
1770 act = "%s¤ËXXX4¤òÈô¤Ð¤·¤¿¡£";
1771 #else
1772                                         act = "projects XXX4's at %s.";
1773 #endif
1774
1775                                         touched = FALSE;
1776                                         break;
1777                                 }
1778
1779                         case RBM_BEG:
1780                                 {
1781 #ifdef JP
1782 act = "%s¤Ë¶â¤ò¤»¤¬¤ó¤À¡£";
1783 #else
1784                                         act = "begs %s for money.";
1785 #endif
1786
1787                                         touched = FALSE;
1788                                         break;
1789                                 }
1790
1791                         case RBM_INSULT:
1792                                 {
1793 #ifdef JP
1794 act = "%s¤òÉî¿«¤·¤¿¡£";
1795 #else
1796                                         act = "insults %s.";
1797 #endif
1798
1799                                         touched = FALSE;
1800                                         break;
1801                                 }
1802
1803                         case RBM_MOAN:
1804                                 {
1805 #ifdef JP
1806 act = "%s¤Ë¤à¤«¤Ã¤Æ¤¦¤á¤¤¤¿¡£";
1807 #else
1808                                         act = "moans at %s.";
1809 #endif
1810
1811                                         touched = FALSE;
1812                                         break;
1813                                 }
1814
1815                         case RBM_SHOW:
1816                                 {
1817 #ifdef JP
1818 act = "%s¤Ë¤à¤«¤Ã¤Æ²Î¤Ã¤¿¡£";
1819 #else
1820                                         act = "sings to %s.";
1821 #endif
1822
1823                                         touched = FALSE;
1824                                         break;
1825                                 }
1826                         }
1827
1828                         /* Message */
1829                         if (act && see_either)
1830                         {
1831 #ifdef JP
1832                                 if (do_silly_attack) act = silly_attacks2[randint0(MAX_SILLY_ATTACK)];
1833                                 strfmt(temp, act, t_name);
1834                                 msg_format("%^s¤Ï%s", m_name, temp);
1835 #else
1836                                 if (do_silly_attack)
1837                                 {
1838                                         act = silly_attacks[randint0(MAX_SILLY_ATTACK)];
1839                                         strfmt(temp, "%s %s.", act, t_name);
1840                                 }
1841                                 else strfmt(temp, act, t_name);
1842                                 msg_format("%^s %s", m_name, temp);
1843 #endif
1844                         }
1845
1846                         /* Hack -- assume all attacks are obvious */
1847                         obvious = TRUE;
1848
1849                         /* Roll out the damage */
1850                         damage = damroll(d_dice, d_side);
1851
1852                         /* Assume no healing effect */
1853                         heal_effect = FALSE;
1854
1855                         pt = GF_MISSILE;
1856
1857                         /* Apply appropriate damage */
1858                         switch (effect)
1859                         {
1860                         case 0:
1861                                 {
1862                                         damage = 0;
1863                                         pt = 0;
1864                                         break;
1865                                 }
1866
1867                         case RBE_SUPERHURT:
1868                                 {
1869                                         if ((randint1(rlev*2+250) > (ac+200)) || one_in_(13)) {
1870                                                 int tmp_damage = damage-(damage*((ac < 150) ? ac : 150)/250);
1871                                                 damage = MAX(damage, tmp_damage*2);
1872                                                 break;
1873                                         }
1874                                 }
1875                         case RBE_HURT:
1876                                 {
1877                                         damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1878                                         break;
1879                                 }
1880
1881                         case RBE_POISON:
1882                         case RBE_DISEASE:
1883                                 {
1884                                         pt = GF_POIS;
1885                                         break;
1886                                 }
1887
1888                         case RBE_UN_BONUS:
1889                         case RBE_UN_POWER:
1890                                 {
1891                                         pt = GF_DISENCHANT;
1892                                         break;
1893                                 }
1894
1895                         case RBE_EAT_FOOD:
1896                         case RBE_EAT_LITE:
1897                         case RBE_DR_MANA:
1898                                 {
1899                                         pt = damage = 0;
1900                                         break;
1901                                 }
1902
1903                         case RBE_EAT_ITEM:
1904                         case RBE_EAT_GOLD:
1905                                 {
1906                                         pt = damage = 0;
1907                                         if (one_in_(2)) blinked = TRUE;
1908                                         break;
1909                                 }
1910
1911                         case RBE_ACID:
1912                                 {
1913                                         pt = GF_ACID;
1914                                         break;
1915                                 }
1916
1917                         case RBE_ELEC:
1918                                 {
1919                                         pt = GF_ELEC;
1920                                         break;
1921                                 }
1922
1923                         case RBE_FIRE:
1924                                 {
1925                                         pt = GF_FIRE;
1926                                         break;
1927                                 }
1928
1929                         case RBE_COLD:
1930                                 {
1931                                         pt = GF_COLD;
1932                                         break;
1933                                 }
1934
1935                         case RBE_BLIND:
1936                                 {
1937                                         break;
1938                                 }
1939
1940                         case RBE_CONFUSE:
1941                                 {
1942                                         pt = GF_CONFUSION;
1943                                         break;
1944                                 }
1945
1946                         case RBE_TERRIFY:
1947                                 {
1948                                         pt = GF_TURN_ALL;
1949                                         break;
1950                                 }
1951
1952                         case RBE_PARALYZE:
1953                                 {
1954                                         pt = GF_OLD_SLEEP; /* sort of close... */
1955                                         break;
1956                                 }
1957
1958                         case RBE_LOSE_STR:
1959                         case RBE_LOSE_INT:
1960                         case RBE_LOSE_WIS:
1961                         case RBE_LOSE_DEX:
1962                         case RBE_LOSE_CON:
1963                         case RBE_LOSE_CHR:
1964                         case RBE_LOSE_ALL:
1965                                 {
1966                                         break;
1967                                 }
1968                         case RBE_SHATTER:
1969                                 {
1970                                         damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1971                                         if (damage > 23)
1972                                         {
1973                                                 earthquake(m_ptr->fy, m_ptr->fx, 8);
1974                                         }
1975                                         break;
1976                                 }
1977                         case RBE_EXP_10:
1978                         case RBE_EXP_20:
1979                         case RBE_EXP_40:
1980                         case RBE_EXP_80:
1981                                 {
1982                                         pt = GF_NETHER;
1983                                         break;
1984                                 }
1985                         case RBE_TIME:
1986                                 {
1987                                         pt = GF_TIME;
1988                                         break;
1989                                 }
1990                         case RBE_EXP_VAMP:
1991                                 {
1992                                         pt = GF_OLD_DRAIN;
1993                                         heal_effect = TRUE;
1994                                         break;
1995                                 }
1996
1997                         default:
1998                                 {
1999                                         pt = 0;
2000                                         break;
2001                                 }
2002                         }
2003
2004                         if (pt)
2005                         {
2006                                 /* Do damage if not exploding */
2007                                 if (!explode)
2008                                 {
2009                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
2010                                                 (pt == GF_OLD_SLEEP ? r_ptr->level : damage), pt, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
2011                                 }
2012
2013                                 if (heal_effect)
2014                                 {
2015                                         if ((monster_living(tr_ptr)) && (damage > 2))
2016                                         {
2017                                                 bool did_heal = FALSE;
2018
2019                                                 if (m_ptr->hp < m_ptr->maxhp) did_heal = TRUE;
2020
2021                                                 /* Heal */
2022                                                 m_ptr->hp += damroll(4, damage / 6);
2023                                                 if (m_ptr->hp > m_ptr->maxhp) m_ptr->hp = m_ptr->maxhp;
2024
2025                                                 /* Redraw (later) if needed */
2026                                                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2027                                                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2028
2029                                                 /* Special message */
2030                                                 if (see_m && did_heal)
2031                                                 {
2032 #ifdef JP
2033 msg_format("%s¤ÏÂÎÎϤò²óÉü¤·¤¿¤è¤¦¤À¡£", m_name);
2034 #else
2035                                                         msg_format("%^s appears healthier.", m_name);
2036 #endif
2037
2038                                                 }
2039                                         }
2040                                 }
2041
2042                                 if (touched)
2043                                 {
2044                                         /* Aura fire */
2045                                         if ((tr_ptr->flags2 & RF2_AURA_FIRE) && m_ptr->r_idx)
2046                                         {
2047                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK))
2048                                                 {
2049                                                         if (see_either)
2050                                                         {
2051                                                                 blinked = FALSE;
2052 #ifdef JP
2053                                                                 msg_format("%^s¤ÏÆÍÁ³Ç®¤¯¤Ê¤Ã¤¿¡ª", m_name);
2054 #else
2055                                                                 msg_format("%^s is suddenly very hot!", m_name);
2056 #endif
2057
2058                                                                 if (see_t && is_original_ap(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_FIRE;
2059                                                         }
2060                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
2061                                                                 damroll (1 + ((tr_ptr->level) / 26),
2062                                                                 1 + ((tr_ptr->level) / 17)),
2063                                                                 GF_FIRE, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
2064                                                 }
2065                                                 else
2066                                                 {
2067                                                         if (see_m && is_original_ap(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK);
2068                                                 }
2069                                         }
2070
2071                                         /* Aura cold */
2072                                         if ((tr_ptr->flags3 & RF3_AURA_COLD) && m_ptr->r_idx)
2073                                         {
2074                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK))
2075                                                 {
2076                                                         if (see_either)
2077                                                         {
2078                                                                 blinked = FALSE;
2079 #ifdef JP
2080                                                                 msg_format("%^s¤ÏÆÍÁ³´¨¤¯¤Ê¤Ã¤¿¡ª", m_name);
2081 #else
2082                                                                 msg_format("%^s is suddenly very cold!", m_name);
2083 #endif
2084
2085                                                                 if (see_t && is_original_ap(t_ptr)) tr_ptr->r_flags3 |= RF3_AURA_COLD;
2086                                                         }
2087                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
2088                                                                 damroll (1 + ((tr_ptr->level) / 26),
2089                                                                 1 + ((tr_ptr->level) / 17)),
2090                                                                 GF_COLD, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
2091                                                 }
2092                                                 else
2093                                                 {
2094                                                         if (see_m && is_original_ap(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_COLD_MASK);
2095                                                 }
2096                                         }
2097
2098                                         /* Aura elec */
2099                                         if ((tr_ptr->flags2 & RF2_AURA_ELEC) && m_ptr->r_idx)
2100                                         {
2101                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK))
2102                                                 {
2103                                                         if (see_either)
2104                                                         {
2105                                                                 blinked = FALSE;
2106 #ifdef JP
2107                                                                 msg_format("%^s¤ÏÅÅ·â¤ò¿©¤é¤Ã¤¿¡ª", m_name);
2108 #else
2109                                                                 msg_format("%^s gets zapped!", m_name);
2110 #endif
2111
2112                                                                 if (see_t && is_original_ap(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_ELEC;
2113                                                         }
2114                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
2115                                                                 damroll (1 + ((tr_ptr->level) / 26),
2116                                                                 1 + ((tr_ptr->level) / 17)),
2117                                                                 GF_ELEC, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
2118                                                 }
2119                                                 else
2120                                                 {
2121                                                         if (see_m && is_original_ap(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK);
2122                                                 }
2123                                         }
2124                                 }
2125                         }
2126                 }
2127
2128                 /* Monster missed player */
2129                 else
2130                 {
2131                         /* Analyze failed attacks */
2132                         switch (method)
2133                         {
2134                         case RBM_HIT:
2135                         case RBM_TOUCH:
2136                         case RBM_PUNCH:
2137                         case RBM_KICK:
2138                         case RBM_CLAW:
2139                         case RBM_BITE:
2140                         case RBM_STING:
2141                         case RBM_SLASH:
2142                         case RBM_BUTT:
2143                         case RBM_CRUSH:
2144                         case RBM_ENGULF:
2145                         case RBM_CHARGE:
2146                                 {
2147                                         /* Wake it up */
2148                                         t_ptr->csleep = 0;
2149
2150                                         if (tr_ptr->flags7 & RF7_HAS_LD_MASK) p_ptr->update |= (PU_MON_LITE);
2151
2152                                         /* Visible monsters */
2153                                         if (see_m)
2154                                         {
2155                                                 /* Message */
2156 #ifdef JP
2157 msg_format("%s¤Ï%^s¤Î¹¶·â¤ò¤«¤ï¤·¤¿¡£", t_name,m_name);
2158 #else
2159                                                 msg_format("%^s misses %s.", m_name, t_name);
2160 #endif
2161
2162                                         }
2163
2164                                         break;
2165                                 }
2166                         }
2167                 }
2168
2169
2170                 /* Analyze "visible" monsters only */
2171                 if (see_m && !do_silly_attack)
2172                 {
2173                         /* Count "obvious" attacks (and ones that cause damage) */
2174                         if (obvious || damage || (r_ptr->r_blows[ap_cnt] > 10))
2175                         {
2176                                 /* Count attacks of this type */
2177                                 if (r_ptr->r_blows[ap_cnt] < MAX_UCHAR)
2178                                 {
2179                                         r_ptr->r_blows[ap_cnt]++;
2180                                 }
2181                         }
2182                 }
2183         }
2184
2185         if (explode)
2186         {
2187                 sound(SOUND_EXPLODE);
2188
2189                 /* Cancel Invulnerability */
2190                 if (m_ptr->invulner) m_ptr->invulner = 0;
2191
2192 #ifdef JP
2193 mon_take_hit_mon(m_idx, m_ptr->hp + 1, &fear, "¤ÏÇúȯ¤·¤ÆÊ´¡¹¤Ë¤Ê¤Ã¤¿¡£", m_idx);
2194 #else
2195                 mon_take_hit_mon(m_idx, m_ptr->hp + 1, &fear, " explodes into tiny shreds.", m_idx);
2196 #endif
2197
2198
2199                 blinked = FALSE;
2200         }
2201
2202
2203         /* Blink away */
2204         if (blinked)
2205         {
2206                 if (see_m)
2207                 {
2208 #ifdef JP
2209 msg_print("Å¥ËÀ¤Ï¾Ð¤Ã¤Æƨ¤²¤¿¡ª");
2210 #else
2211                         msg_print("The thief flees laughing!");
2212 #endif
2213
2214                 }
2215                 else if (known)
2216                 {
2217                         mon_fight = TRUE;
2218                 }
2219
2220                 teleport_away(m_idx, MAX_SIGHT * 2 + 5, FALSE);
2221         }
2222
2223         return TRUE;
2224 }
2225
2226
2227 /*
2228  * Process a monster
2229  *
2230  * The monster is known to be within 100 grids of the player
2231  *
2232  * In several cases, we directly update the monster lore
2233  *
2234  * Note that a monster is only allowed to "reproduce" if there
2235  * are a limited number of "reproducing" monsters on the current
2236  * level.  This should prevent the level from being "swamped" by
2237  * reproducing monsters.  It also allows a large mass of mice to
2238  * prevent a louse from multiplying, but this is a small price to
2239  * pay for a simple multiplication method.
2240  *
2241  * XXX Monster fear is slightly odd, in particular, monsters will
2242  * fixate on opening a door even if they cannot open it.  Actually,
2243  * the same thing happens to normal monsters when they hit a door
2244  *
2245  * XXX XXX XXX In addition, monsters which *cannot* open or bash
2246  * down a door will still stand there trying to open it...
2247  *
2248  * XXX Technically, need to check for monster in the way
2249  * combined with that monster being in a wall (or door?)
2250  *
2251  * A "direction" of "5" means "pick a random direction".
2252  */
2253 static void process_monster(int m_idx)
2254 {
2255         monster_type    *m_ptr = &m_list[m_idx];
2256         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
2257         monster_race    *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2258
2259         int             i, d, oy, ox, ny, nx;
2260
2261         int             mm[8];
2262
2263         cave_type       *c_ptr;
2264         feature_type    *f_ptr;
2265
2266         monster_type    *y_ptr;
2267
2268         bool            do_turn;
2269         bool            do_move;
2270         bool            do_view;
2271
2272         bool            did_open_door;
2273         bool            did_bash_door;
2274         bool            did_take_item;
2275         bool            did_kill_item;
2276         bool            did_move_body;
2277         bool            did_pass_wall;
2278         bool            did_kill_wall;
2279         bool            gets_angry = FALSE;
2280         bool            can_cross;
2281         bool            aware = TRUE;
2282
2283         bool            fear;
2284
2285         bool            is_riding_mon = (m_idx == p_ptr->riding);
2286
2287         if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
2288         {
2289                 if (rakuba(0, TRUE))
2290                 {
2291 #ifdef JP
2292                         msg_print("ÃÏÌ̤ËÍî¤È¤µ¤ì¤¿¡£");
2293 #else
2294                         char m_name[80];
2295                         monster_desc(m_name, &m_list[p_ptr->riding], 0);
2296                         msg_format("You have fallen from %s.", m_name);
2297 #endif
2298                 }
2299         }
2300
2301         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !m_ptr->csleep)
2302         {
2303                 choose_new_monster(m_idx, FALSE, 0);
2304                 r_ptr = &r_info[m_ptr->r_idx];
2305         }
2306
2307         /* Players hidden in shadow are almost imperceptable. -LM- */
2308         if (p_ptr->special_defense & NINJA_S_STEALTH)
2309         {
2310                 int tmp = p_ptr->lev*6+(p_ptr->skill_stl+10)*4;
2311                 if (p_ptr->monlite) tmp /= 3;
2312                 if (p_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
2313                 if (r_ptr->level > (p_ptr->lev*p_ptr->lev/20+10)) tmp /= 3;
2314                 /* Low-level monsters will find it difficult to locate the player. */
2315                 if (randint0(tmp) > (r_ptr->level+20)) aware = FALSE;
2316         }
2317
2318         /* Are there its parent? */
2319         if (m_ptr->parent_m_idx && !m_list[m_ptr->parent_m_idx].r_idx)
2320         {
2321                 /* Its parent have gone, it also goes away. */
2322
2323                 if (m_ptr->ml)
2324                 {
2325                         char m_name[80];
2326                         
2327                         /* Acquire the monster name */
2328                         monster_desc(m_name, m_ptr, 0);
2329
2330 #ifdef JP
2331                         msg_format("%s¤Ï¾Ã¤¨µî¤Ã¤¿¡ª", m_name);
2332 #else
2333                         msg_format("%^s disappears!", m_name);
2334 #endif
2335                 }
2336
2337                 /* Delete the monster */
2338                 delete_monster_idx(m_idx);
2339
2340                 return;
2341         }
2342
2343         /* Quantum monsters are odd */
2344         if (r_ptr->flags2 & (RF2_QUANTUM))
2345         {
2346                 /* Sometimes skip move */
2347                 if (!randint0(2)) return;
2348
2349                 /* Sometimes die */
2350                 if (!randint0((m_idx % 100) + 10) && !(r_ptr->flags1 & RF1_QUESTOR))
2351                 {
2352                         bool sad = FALSE;
2353
2354                         if (is_pet(m_ptr) && !(m_ptr->ml))
2355                                 sad = TRUE;
2356
2357                         if (m_ptr->ml)
2358                         {
2359                                 char m_name[80];
2360
2361                                 /* Acquire the monster name */
2362                                 monster_desc(m_name, m_ptr, 0);
2363
2364                                 /* Oops */
2365 #ifdef JP
2366 msg_format("%s¤Ï¾Ã¤¨µî¤Ã¤¿¡ª", m_name);
2367 #else
2368                                 msg_format("%^s disappears!", m_name);
2369 #endif
2370
2371                         }
2372
2373                         /* Generate treasure, etc */
2374                         monster_death(m_idx, FALSE);
2375
2376                         /* Delete the monster */
2377                         delete_monster_idx(m_idx);
2378
2379                         if (sad)
2380                         {
2381 #ifdef JP
2382 msg_print("¾¯¤·¤Î´ÖÈᤷ¤¤µ¤Ê¬¤Ë¤Ê¤Ã¤¿¡£");
2383 #else
2384                                 msg_print("You feel sad for a moment.");
2385 #endif
2386
2387                         }
2388
2389                         return;
2390                 }
2391         }
2392
2393         if (m_ptr->r_idx == MON_SHURYUUDAN)
2394 #ifdef JP
2395                 mon_take_hit_mon(m_idx, 1, &fear, "¤ÏÇúȯ¤·¤ÆÊ´¡¹¤Ë¤Ê¤Ã¤¿¡£", m_idx);
2396 #else
2397                 mon_take_hit_mon(m_idx, 1, &fear, " explodes into tiny shreds.", m_idx);
2398 #endif
2399
2400         if ((is_pet(m_ptr) || is_friendly(m_ptr)) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && !p_ptr->inside_battle)
2401         {
2402                 static int riding_pinch = 0;
2403
2404                 if (m_ptr->hp < m_ptr->maxhp/3)
2405                 {
2406                         char m_name[80];
2407                         monster_desc(m_name, m_ptr, 0);
2408
2409                         if (is_riding_mon && riding_pinch < 2)
2410                         {
2411 #ifdef JP
2412                                 msg_format("%s¤Ï½ý¤ÎÄˤµ¤Î;¤ê¤¢¤Ê¤¿¤Î«Çû¤«¤éƨ¤ì¤è¤¦¤È¤·¤Æ¤¤¤ë¡£", m_name);
2413 #else
2414                                 msg_format("%^s seems to be in so much pain, and trying to escape from your restriction.", m_name);
2415 #endif
2416                                 riding_pinch++;
2417                                 disturb(1, 0);
2418                         }
2419                         else
2420                         {
2421                                 if (is_riding_mon)
2422                                 {
2423 #ifdef JP
2424                                         msg_format("%s¤Ï¤¢¤Ê¤¿¤Î«Çû¤«¤éæ½Ð¤·¤¿¡£", m_name);
2425 #else
2426                                         msg_format("%^s succeeded to escape from your restriction!", m_name);
2427 #endif
2428                                         if (rakuba(-1, FALSE))
2429                                         {
2430 #ifdef JP
2431                                                 msg_print("ÃÏÌ̤ËÍî¤È¤µ¤ì¤¿¡£");
2432 #else
2433                                                 msg_print("You have fallen from riding pet.");
2434 #endif
2435                                         }
2436                                 }
2437
2438                                 if (m_ptr->ml)
2439                                 {
2440                                         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))
2441                                         {
2442 #ifdef JP
2443                                                 msg_format("%^s¡Ö¥Ô¥ó¥Á¤À¡ªÂàµÑ¤µ¤»¤Æ¤â¤é¤¦¡ª¡×", m_name);
2444 #else
2445                                                 msg_format("%^s says 'It is the pinch! I will retreat'.", m_name);
2446 #endif
2447                                         }
2448 #ifdef JP
2449                                         msg_format("%^s¤¬¥Æ¥ì¥Ý¡¼¥È¡¦¥ì¥Ù¥ë¤Î´¬Êª¤òÆɤó¤À¡£", m_name);
2450                                         msg_format("%^s¤¬¾Ã¤¨µî¤Ã¤¿¡£", m_name);
2451 #else
2452                                         msg_format("%^s read a scroll of teleport level.", m_name);
2453                                         msg_format("%^s disappears.", m_name);
2454 #endif
2455                                 }
2456
2457                                 if (is_riding_mon && rakuba(-1, FALSE))
2458                                 {
2459 #ifdef JP
2460                                         msg_print("ÃÏÌ̤ËÍî¤È¤µ¤ì¤¿¡£");
2461 #else
2462                                         msg_print("You have fallen from riding pet.");
2463 #endif
2464                                 }
2465
2466                                 /* Check for quest completion */
2467                                 check_quest_completion(m_ptr);
2468
2469                                 delete_monster_idx(m_idx);
2470
2471                                 return;
2472                         }
2473                 }
2474                 else
2475                 {
2476                         /* Reset the counter */
2477                         if (is_riding_mon) riding_pinch = 0;
2478                 }
2479         }
2480
2481         /* Handle "sleep" */
2482         if (m_ptr->csleep)
2483         {
2484                 /* Handle non-aggravation - Still sleeping */
2485                 if (!(p_ptr->cursed & TRC_AGGRAVATE)) return;
2486
2487                 /* Handle aggravation */
2488
2489                 /* Reset sleep counter */
2490                 m_ptr->csleep = 0;
2491
2492                 if (r_ptr->flags7 & RF7_HAS_LD_MASK) p_ptr->update |= (PU_MON_LITE);
2493
2494                 /* Notice the "waking up" */
2495                 if (m_ptr->ml)
2496                 {
2497                         char m_name[80];
2498
2499                         /* Acquire the monster name */
2500                         monster_desc(m_name, m_ptr, 0);
2501
2502                         /* Dump a message */
2503 #ifdef JP
2504                         msg_format("%^s¤¬Ìܤò³Ð¤Þ¤·¤¿¡£", m_name);
2505 #else
2506                         msg_format("%^s wakes up.", m_name);
2507 #endif
2508
2509                         /* Redraw the health bar */
2510                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2511                         if (is_riding_mon) p_ptr->redraw |= (PR_UHEALTH);
2512
2513                         /* Hack -- Count the wakings */
2514                         if (r_ptr->r_wake < MAX_UCHAR)
2515                         {
2516                                 r_ptr->r_wake++;
2517                         }
2518                 }
2519         }
2520
2521         /* Handle "stun" */
2522         if (m_ptr->stunned)
2523         {
2524                 /* Sometimes skip move */
2525                 if (one_in_(2)) return;
2526         }
2527
2528         if (is_riding_mon)
2529         {
2530                 p_ptr->update |= (PU_BONUS);
2531         }
2532
2533         /* No one wants to be your friend if you're aggravating */
2534         if (is_friendly(m_ptr) && (p_ptr->cursed & TRC_AGGRAVATE))
2535                 gets_angry = TRUE;
2536
2537         /* Paranoia... no pet uniques outside wizard mode -- TY */
2538         if (is_pet(m_ptr) &&
2539             ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
2540               monster_has_hostile_align(NULL, 10, -10, r_ptr))
2541              || (r_ptr->flagsr & RFR_RES_ALL)))
2542         {
2543                 gets_angry = TRUE;
2544         }
2545
2546         if (p_ptr->inside_battle) gets_angry = FALSE;
2547
2548         if (gets_angry)
2549         {
2550                 char m_name[80];
2551                 monster_desc(m_name, m_ptr, 0);
2552 #ifdef JP
2553 msg_format("%^s¤ÏÆÍÁ³Å¨¤Ë¤Þ¤ï¤Ã¤¿¡ª", m_name);
2554 #else
2555                 msg_format("%^s suddenly becomes hostile!", m_name);
2556 #endif
2557
2558                 set_hostile(m_ptr);
2559         }
2560
2561         /* Get the origin */
2562         oy = m_ptr->fy;
2563         ox = m_ptr->fx;
2564
2565
2566         /* Attempt to "multiply" if able and allowed */
2567         if ((r_ptr->flags2 & RF2_MULTIPLY) && (num_repro < MAX_REPRO))
2568         {
2569                 int k, y, x;
2570
2571                 /* Count the adjacent monsters */
2572                 for (k = 0, y = oy - 1; y <= oy + 1; y++)
2573                 {
2574                         for (x = ox - 1; x <= ox + 1; x++)
2575                         {
2576                                 /* Ignore locations off of edge */
2577                                 if (!in_bounds2(y, x)) continue;
2578
2579                                 if (cave[y][x].m_idx) k++;
2580                         }
2581                 }
2582
2583                 /* Hack -- multiply slower in crowded areas */
2584                 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2585                 {
2586                         /* Try to multiply */
2587                         if (multiply_monster(m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
2588                         {
2589                                 /* Take note if visible */
2590                                 if (m_ptr->ml && m_list[hack_m_idx_ii].ml && is_original_ap(m_ptr))
2591                                 {
2592                                         r_ptr->r_flags2 |= (RF2_MULTIPLY);
2593                                 }
2594
2595                                 /* Multiplying takes energy */
2596                                 return;
2597                         }
2598                 }
2599         }
2600
2601
2602         if (r_ptr->flags6 & RF6_SPECIAL)
2603         {
2604                 /* Hack -- Ohmu scatters molds! */
2605                 if (m_ptr->r_idx == MON_OHMU)
2606                 {
2607                         if (!p_ptr->inside_arena && !p_ptr->inside_battle)
2608                         {
2609                                 if (r_ptr->freq_spell && (randint1(100) <= r_ptr->freq_spell))
2610                                 {
2611                                         int  k, count = 0;
2612                                         int  rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
2613                                         u32b p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
2614
2615                                         for (k = 0; k < 6; k++)
2616                                         {
2617                                                 if (summon_specific(m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_BIZARRE1, (PM_ALLOW_GROUP | p_mode)))
2618                                                 {
2619                                                         if (m_list[hack_m_idx_ii].ml) count++;
2620                                                 }
2621                                         }
2622
2623                                         if (count && m_ptr->ml && is_original_ap(m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
2624                                 }
2625                         }
2626                 }
2627         }
2628
2629
2630         if (!p_ptr->inside_battle)
2631         {
2632                 /* Hack! "Cyber" monster makes noise... */
2633                 if (m_ptr->ap_r_idx == MON_CYBER &&
2634                     one_in_(CYBERNOISE) &&
2635                     !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2636                 {
2637                         if (disturb_minor) disturb(FALSE, FALSE);
2638 #ifdef JP
2639 msg_print("½Å¸ü¤Ê­²»¤¬Ê¹¤³¤¨¤¿¡£");
2640 #else
2641                         msg_print("You hear heavy steps.");
2642 #endif
2643
2644                 }
2645
2646                 /* Some monsters can speak */
2647                 if ((ap_r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
2648                     one_in_(SPEAK_CHANCE) &&
2649                     player_has_los_bold(oy, ox) &&
2650                     projectable(oy, ox, py, px))
2651                 {
2652                         char m_name[80];
2653                         char monmessage[1024];
2654                         cptr filename;
2655
2656                         /* Acquire the monster name/poss */
2657                         if (m_ptr->ml)
2658                                 monster_desc(m_name, m_ptr, 0);
2659                         else
2660 #ifdef JP
2661 strcpy(m_name, "¤½¤ì");
2662 #else
2663                                 strcpy(m_name, "It");
2664 #endif
2665
2666
2667                         /* Select the file for monster quotes */
2668                         if (m_ptr->monfear)
2669 #ifdef JP
2670 filename = "monfear_j.txt";
2671 #else
2672                                 filename = "monfear.txt";
2673 #endif
2674
2675                         else if (is_pet(m_ptr))
2676 #ifdef JP
2677 filename = "monpet_j.txt";
2678 #else
2679                                 filename = "monpet.txt";
2680 #endif
2681
2682                         else if (is_friendly(m_ptr))
2683 #ifdef JP
2684 filename = "monfrien_j.txt";
2685 #else
2686                                 filename = "monfrien.txt";
2687 #endif
2688
2689                         else
2690 #ifdef JP
2691                                 filename = "monspeak_j.txt";
2692 #else
2693                                 filename = "monspeak.txt";
2694 #endif
2695
2696
2697                         /* Get the monster line */
2698                         if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
2699                         {
2700                                 /* Say something */
2701 #ifdef JP
2702 msg_format("%^s%s", m_name, monmessage);
2703 #else
2704                                 msg_format("%^s %s", m_name, monmessage);
2705 #endif
2706
2707                         }
2708                 }
2709         }
2710
2711         /* Try to cast spell occasionally */
2712         if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
2713         {
2714                 bool counterattack = FALSE;
2715
2716                 /* Give priority to counter attack? */
2717                 if (m_ptr->target_y)
2718                 {
2719                         int t_m_idx = cave[m_ptr->target_y][m_ptr->target_x].m_idx;
2720
2721                         /* The monster must be an enemy, and projectable */
2722                         if (t_m_idx &&
2723                             are_enemies(m_ptr, &m_list[t_m_idx]) &&
2724                             projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
2725                         {
2726                                 counterattack = TRUE;
2727                         }
2728                 }
2729
2730                 if (!counterattack)
2731                 {
2732                         /* Attempt to cast a spell */
2733                         if (aware && make_attack_spell(m_idx)) return;
2734
2735                         /*
2736                          * Attempt to cast a spell at an enemy other than the player
2737                          * (may slow the game a smidgeon, but I haven't noticed.)
2738                          */
2739                         if (monst_spell_monst(m_idx)) return;
2740                 }
2741                 else
2742                 {
2743                         /* Attempt to do counter attack at first */
2744                         if (monst_spell_monst(m_idx)) return;
2745
2746                         if (aware && make_attack_spell(m_idx)) return;
2747                 }
2748         }
2749
2750         /* Hack -- Assume no movement */
2751         mm[0] = mm[1] = mm[2] = mm[3] = 0;
2752         mm[4] = mm[5] = mm[6] = mm[7] = 0;
2753
2754
2755         /* Confused -- 100% random */
2756         if (m_ptr->confused || !aware)
2757         {
2758                 /* Try four "random" directions */
2759                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2760         }
2761
2762         /* 75% random movement */
2763         else if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) &&
2764                  (randint0(100) < 75))
2765         {
2766                 /* Memorize flags */
2767                 if (m_ptr->ml && is_original_ap(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
2768
2769                 /* Try four "random" directions */
2770                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2771         }
2772
2773         /* 50% random movement */
2774         else if ((r_ptr->flags1 & RF1_RAND_50) &&
2775                                 (randint0(100) < 50))
2776         {
2777                 /* Memorize flags */
2778                 if (m_ptr->ml && is_original_ap(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
2779
2780                 /* Try four "random" directions */
2781                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2782         }
2783
2784         /* 25% random movement */
2785         else if ((r_ptr->flags1 & RF1_RAND_25) &&
2786                                 (randint0(100) < 25))
2787         {
2788                 /* Memorize flags */
2789                 if (m_ptr->ml && is_original_ap(m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
2790
2791                 /* Try four "random" directions */
2792                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2793         }
2794
2795         /* Can't reach player - find something else to hit */
2796         else if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2797         {
2798                 /* Try four "random" directions */
2799                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2800
2801                 /* Look for an enemy */
2802 #if 0  /* Hack - Too slow.  Mimic pits are horrible with this on. */
2803                 get_enemy_dir(m_idx, mm);
2804 #endif /* 0 */
2805         }
2806
2807         /* Pets will follow the player */
2808         else if (is_pet(m_ptr))
2809         {
2810                 /* Are we trying to avoid the player? */
2811                 bool avoid = ((p_ptr->pet_follow_distance < 0) &&
2812                                                   (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
2813
2814                 /* Do we want to find the player? */
2815                 bool lonely = (!avoid && (m_ptr->cdis > p_ptr->pet_follow_distance));
2816
2817                 /* Should we find the player if we can't find a monster? */
2818                 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
2819
2820                 /* by default, move randomly */
2821                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2822
2823                 /* Look for an enemy */
2824                 if (!get_enemy_dir(m_idx, mm))
2825                 {
2826                         /* Find the player if necessary */
2827                         if (avoid || lonely || distant)
2828                         {
2829                                 /* Remember the leash length */
2830                                 int dis = p_ptr->pet_follow_distance;
2831
2832                                 /* Hack -- adjust follow distance temporarily */
2833                                 if (p_ptr->pet_follow_distance > PET_SEEK_DIST)
2834                                 {
2835                                         p_ptr->pet_follow_distance = PET_SEEK_DIST;
2836                                 }
2837
2838                                 /* Find the player */
2839                                 (void)get_moves(m_idx, mm);
2840
2841                                 /* Restore the leash */
2842                                 p_ptr->pet_follow_distance = dis;
2843                         }
2844                 }
2845         }
2846
2847         /* Friendly monster movement */
2848         else if (!is_hostile(m_ptr))
2849         {
2850                 /* by default, move randomly */
2851                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2852
2853                 /* Look for an enemy */
2854                 get_enemy_dir(m_idx, mm);
2855         }
2856         /* Normal movement */
2857         else
2858         {
2859                 /* Logical moves, may do nothing */
2860                 if (!get_moves(m_idx, mm)) return;
2861         }
2862
2863         /* Assume nothing */
2864         do_turn = FALSE;
2865         do_move = FALSE;
2866         do_view = FALSE;
2867
2868         /* Assume nothing */
2869         did_open_door = FALSE;
2870         did_bash_door = FALSE;
2871         did_take_item = FALSE;
2872         did_kill_item = FALSE;
2873         did_move_body = FALSE;
2874         did_pass_wall = FALSE;
2875         did_kill_wall = FALSE;
2876
2877
2878         /* Take a zero-terminated array of "directions" */
2879         for (i = 0; mm[i]; i++)
2880         {
2881                 /* Get the direction */
2882                 d = mm[i];
2883
2884                 /* Hack -- allow "randomized" motion */
2885                 if (d == 5) d = ddd[randint0(8)];
2886
2887                 /* Get the destination */
2888                 ny = oy + ddy[d];
2889                 nx = ox + ddx[d];
2890
2891                 /* Ignore locations off of edge */
2892                 if (!in_bounds2(ny, nx)) continue;
2893
2894                 /* Access that cave grid */
2895                 c_ptr = &cave[ny][nx];
2896                 f_ptr = &f_info[c_ptr->feat];
2897                 can_cross = monster_can_cross_terrain(c_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
2898
2899                 /* Access that cave grid's contents */
2900                 y_ptr = &m_list[c_ptr->m_idx];
2901
2902                 /* Hack -- player 'in' wall */
2903                 if (player_bold(ny, nx))
2904                 {
2905                         do_move = TRUE;
2906                 }
2907
2908                 else if (c_ptr->m_idx)
2909                 {
2910                         /* Possibly a monster to attack */
2911                         do_move = TRUE;
2912                 }
2913
2914                 /* Floor is open? */
2915                 else if (can_cross)
2916                 {
2917                         /* Go ahead and move */
2918                         do_move = TRUE;
2919
2920                         /* Monster moves through walls (and doors) */
2921                         if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || p_ptr->pass_wall) &&
2922                             !have_flag(f_ptr->flags, FF_MOVE))
2923                         {
2924                                 /* Monster went through a wall */
2925                                 did_pass_wall = TRUE;
2926                         }
2927
2928                         if ((r_ptr->flags2 & RF2_KILL_WALL) && have_flag(f_ptr->flags, FF_TUNNEL) &&
2929                             !have_flag(f_ptr->flags, FF_LOS) && !have_flag(f_ptr->flags, FF_PERMANENT))
2930                         {
2931                                 /* Monster destroyed a wall (later) */
2932                                 did_kill_wall = TRUE;
2933                         }
2934                 }
2935
2936                 /* Monster destroys walls (and doors) */
2937                 else if ((r_ptr->flags2 & RF2_KILL_WALL) && !is_riding_mon &&
2938                          have_flag(f_ptr->flags, FF_TUNNEL) && !have_flag(f_ptr->flags, FF_PERMANENT))
2939                 {
2940                         /* Eat through walls/doors/rubble */
2941                         do_move = TRUE;
2942
2943                         /* Monster destroyed a wall (later) */
2944                         did_kill_wall = TRUE;
2945                 }
2946
2947                 /* Handle doors and secret doors */
2948                 else if (is_closed_door(c_ptr->feat))
2949                 {
2950                         bool may_bash = TRUE;
2951                         feature_type *f_ptr = &f_info[c_ptr->feat];
2952
2953                         /* Assume no move allowed */
2954                         do_move = FALSE;
2955
2956                         /* Creature can open doors. */
2957                         if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
2958                                  (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2959                         {
2960                                 /* Closed doors */
2961                                 if (!f_ptr->power)
2962                                 {
2963                                         /* The door is open */
2964                                         did_open_door = TRUE;
2965
2966                                         /* Do not bash the door */
2967                                         may_bash = FALSE;
2968
2969                                         /* Assume no move allowed */
2970                                         do_move = TRUE;
2971                                 }
2972
2973                                 /* Locked doors (not jammed) */
2974                                 else
2975                                 {
2976                                         /* Try to unlock it XXX XXX XXX */
2977                                         if (randint0(m_ptr->hp / 10) > f_ptr->power)
2978                                         {
2979                                                 /* Unlock the door */
2980                                                 cave_alter_feat(ny, nx, FF_OPEN);
2981
2982                                                 /* Do not bash the door */
2983                                                 may_bash = FALSE;
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) &&
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                                 /* XXX XXX XXX Message */
3213                         }
3214                 }
3215
3216                 /*
3217                  * Check if monster can cross terrain
3218                  * This is checked after the normal attacks
3219                  * to allow monsters to attack an enemy,
3220                  * even if it can't enter the terrain.
3221                  */
3222                 if (do_move && !can_cross && !did_kill_wall)
3223                 {
3224                         /* Assume no move allowed */
3225                         do_move = FALSE;
3226                 }
3227
3228                 /* Some monsters never move */
3229                 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
3230                 {
3231                         /* Hack -- memorize lack of attacks */
3232                         if (m_ptr->ml && is_original_ap(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
3233
3234                         /* Do not move */
3235                         do_move = FALSE;
3236                 }
3237
3238                 if (is_riding_mon)
3239                 {
3240                         if (!p_ptr->riding_ryoute && !(m_list[p_ptr->riding].monfear)) do_move = FALSE;
3241                 }
3242
3243                 /* Creature has been allowed move */
3244                 if (do_move)
3245                 {
3246                         s16b this_o_idx, next_o_idx;
3247
3248                         /* Take a turn */
3249                         do_turn = TRUE;
3250
3251                         /* Hack -- Update the old location */
3252                         cave[oy][ox].m_idx = c_ptr->m_idx;
3253
3254                         if (did_kill_wall)
3255                         {
3256                                 if (one_in_(GRINDNOISE))
3257                                 {
3258 #ifdef JP
3259                                         msg_print("¥®¥·¥®¥·¤¤¤¦²»¤¬Ê¹¤³¤¨¤ë¡£");
3260 #else
3261                                         msg_print("There is a grinding sound.");
3262 #endif
3263                                 }
3264
3265                                 cave_alter_feat(ny, nx, FF_HURT_DISI);
3266
3267                                 /* Note changes to viewable region */
3268                                 if (player_has_los_bold(ny, nx)) do_view = TRUE;
3269                         }
3270                         else if (have_flag(f_ptr->flags, FF_TREE))
3271                         {
3272                                 if (!(r_ptr->flags7 & RF7_CAN_FLY) && (!is_riding_mon || !p_ptr->ffall) && !(r_ptr->flags8 & RF8_WILD_WOOD))
3273                                 {
3274                                         m_ptr->energy_need += ENERGY_NEED();
3275                                 }
3276                         }
3277
3278                         /* Mega-Hack -- move the old monster, if any */
3279                         if (c_ptr->m_idx)
3280                         {
3281                                 /* Move the old monster */
3282                                 y_ptr->fy = oy;
3283                                 y_ptr->fx = ox;
3284
3285                                 /* Update the old monster */
3286                                 update_mon(c_ptr->m_idx, TRUE);
3287
3288                                 /* Wake up the moved monster */
3289                                 y_ptr->csleep = 0;
3290
3291                                 if (r_info[y_ptr->r_idx].flags7 & (RF7_LITE_MASK | RF7_DARK_MASK))
3292                                         p_ptr->update |= (PU_MON_LITE);
3293                         }
3294
3295                         /* Hack -- Update the new location */
3296                         c_ptr->m_idx = m_idx;
3297
3298                         /* Move the monster */
3299                         m_ptr->fy = ny;
3300                         m_ptr->fx = nx;
3301
3302                         /* Update the monster */
3303                         update_mon(m_idx, TRUE);
3304
3305                         if (is_riding_mon)
3306                         {
3307                                 py = ny;
3308                                 px = nx;
3309                         }
3310
3311                         /* Redraw the old grid */
3312                         lite_spot(oy, ox);
3313
3314                         /* Redraw the new grid */
3315                         lite_spot(ny, nx);
3316
3317                         if (is_riding_mon)
3318                         {
3319                                 verify_panel();
3320
3321                                 /* Update stuff */
3322                                 p_ptr->update |= (PU_VIEW | PU_LITE | PU_FLOW | PU_MON_LITE);
3323
3324                                 /* Update the monsters */
3325                                 p_ptr->update |= (PU_DISTANCE);
3326
3327                                 /* Update sub-windows */
3328                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3329                         }
3330
3331                         /* Possible disturb */
3332                         if (m_ptr->ml &&
3333                             (disturb_move ||
3334                              (disturb_near && (m_ptr->mflag & MFLAG_VIEW)) ||
3335                              (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)))
3336                         {
3337                                 /* Disturb */
3338                                 if (is_hostile(m_ptr))
3339                                         disturb(0, 0);
3340                         }
3341
3342                         /* Scan all objects in the grid */
3343                         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
3344                         {
3345                                 object_type *o_ptr;
3346
3347                                 /* Acquire object */
3348                                 o_ptr = &o_list[this_o_idx];
3349
3350                                 /* Acquire next object */
3351                                 next_o_idx = o_ptr->next_o_idx;
3352
3353                                 /* Skip gold */
3354                                 if (o_ptr->tval == TV_GOLD) continue;
3355
3356                                 /*
3357                                  * Skip "real" corpses and statues, to avoid extreme
3358                                  * silliness like a novice rogue pockets full of statues
3359                                  * and corpses.
3360                                  */
3361                                 if ((o_ptr->tval == TV_CORPSE) ||
3362                                     (o_ptr->tval == TV_STATUE)) continue;
3363
3364                                 /* Take or Kill objects on the floor */
3365                                 if ((r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
3366                                          (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_PICKUP_ITEMS)))
3367                                 {
3368                                         u32b flgs[TR_FLAG_SIZE];
3369
3370                                         u32b flg2 = 0L;
3371                                         u32b flg3 = 0L;
3372
3373                                         char m_name[80];
3374                                         char o_name[MAX_NLEN];
3375
3376                                         /* Extract some flags */
3377                                         object_flags(o_ptr, flgs);
3378
3379                                         /* Acquire the object name */
3380                                         object_desc(o_name, o_ptr, 0);
3381
3382                                         /* Acquire the monster name */
3383                                         monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
3384
3385                                         /* React to objects that hurt the monster */
3386                                         if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
3387                                         if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
3388                                         if (have_flag(flgs, TR_SLAY_TROLL))  flg3 |= (RF3_TROLL);
3389                                         if (have_flag(flgs, TR_KILL_TROLL))  flg3 |= (RF3_TROLL);
3390                                         if (have_flag(flgs, TR_KILL_GIANT))  flg3 |= (RF3_GIANT);
3391                                         if (have_flag(flgs, TR_SLAY_GIANT))  flg3 |= (RF3_GIANT);
3392                                         if (have_flag(flgs, TR_SLAY_ORC))    flg3 |= (RF3_ORC);
3393                                         if (have_flag(flgs, TR_KILL_ORC))    flg3 |= (RF3_ORC);
3394                                         if (have_flag(flgs, TR_SLAY_DEMON))  flg3 |= (RF3_DEMON);
3395                                         if (have_flag(flgs, TR_KILL_DEMON))  flg3 |= (RF3_DEMON);
3396                                         if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
3397                                         if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
3398                                         if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
3399                                         if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
3400                                         if (have_flag(flgs, TR_SLAY_EVIL))   flg3 |= (RF3_EVIL);
3401                                         if (have_flag(flgs, TR_KILL_EVIL))   flg3 |= (RF3_EVIL);
3402                                         if (have_flag(flgs, TR_SLAY_HUMAN))  flg2 |= (RF2_HUMAN);
3403                                         if (have_flag(flgs, TR_KILL_HUMAN))  flg2 |= (RF2_HUMAN);
3404
3405                                         /* The object cannot be picked up by the monster */
3406                                         if (artifact_p(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
3407                                                 (o_ptr->art_name))
3408                                         {
3409                                                 /* Only give a message for "take_item" */
3410                                                 if ((r_ptr->flags2 & (RF2_TAKE_ITEM)) && (r_ptr->flags2 & (RF2_STUPID)))
3411                                                 {
3412                                                         /* Take note */
3413                                                         did_take_item = TRUE;
3414
3415                                                         /* Describe observable situations */
3416                                                         if (m_ptr->ml && player_can_see_bold(ny, nx))
3417                                                         {
3418                                                                 /* Dump a message */
3419 #ifdef JP
3420                                                                 msg_format("%^s¤Ï%s¤ò½¦¤ª¤¦¤È¤·¤¿¤¬¡¢¤À¤á¤À¤Ã¤¿¡£", m_name, o_name);
3421 #else
3422                                                                 msg_format("%^s tries to pick up %s, but fails.", m_name, o_name);
3423 #endif
3424                                                         }
3425                                                 }
3426                                         }
3427
3428                                         /* Pick up the item */
3429                                         else if (r_ptr->flags2 & RF2_TAKE_ITEM)
3430                                         {
3431                                                 /* Take note */
3432                                                 did_take_item = TRUE;
3433
3434                                                 /* Describe observable situations */
3435                                                 if (player_can_see_bold(ny, nx))
3436                                                 {
3437                                                         /* Dump a message */
3438 #ifdef JP
3439                                                         msg_format("%^s¤¬%s¤ò½¦¤Ã¤¿¡£", m_name, o_name);
3440 #else
3441                                                         msg_format("%^s picks up %s.", m_name, o_name);
3442 #endif
3443                                                 }
3444
3445                                                 /* Excise the object */
3446                                                 excise_object_idx(this_o_idx);
3447
3448                                                 /* Forget mark */
3449                                                 o_ptr->marked = 0;
3450
3451                                                 /* Forget location */
3452                                                 o_ptr->iy = o_ptr->ix = 0;
3453
3454                                                 /* Memorize monster */
3455                                                 o_ptr->held_m_idx = m_idx;
3456
3457                                                 /* Build a stack */
3458                                                 o_ptr->next_o_idx = m_ptr->hold_o_idx;
3459
3460                                                 /* Carry object */
3461                                                 m_ptr->hold_o_idx = this_o_idx;
3462                                         }
3463
3464                                         /* Destroy the item if not a pet */
3465                                         else if (!is_pet(m_ptr))
3466                                         {
3467                                                 /* Take note */
3468                                                 did_kill_item = TRUE;
3469
3470                                                 /* Describe observable situations */
3471                                                 if (player_has_los_bold(ny, nx))
3472                                                 {
3473                                                         /* Dump a message */
3474 #ifdef JP
3475                                                         msg_format("%^s¤¬%s¤òÇ˲õ¤·¤¿¡£", m_name, o_name);
3476 #else
3477                                                         msg_format("%^s destroys %s.", m_name, o_name);
3478 #endif
3479                                                 }
3480
3481                                                 /* Delete the object */
3482                                                 delete_object_idx(this_o_idx);
3483                                         }
3484                                 }
3485                         }
3486                 }
3487
3488                 /* Stop when done */
3489                 if (do_turn) break;
3490         }
3491
3492         /*
3493          *  Forward movements failed, but now received LOS attack!
3494          *  Try to flow by smell.
3495          */
3496         if (p_ptr->no_flowed && i > 2 &&  m_ptr->target_y)
3497                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3498
3499         /* If we haven't done anything, try casting a spell again */
3500         if (!do_turn && !do_move && !m_ptr->monfear && !is_riding_mon && aware)
3501         {
3502                 /* Try to cast spell again */
3503                 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
3504                 {
3505                         if (make_attack_spell(m_idx)) return;
3506                 }
3507         }
3508
3509
3510         /* Notice changes in view */
3511         if (do_view)
3512         {
3513                 /* Update some things */
3514                 p_ptr->update |= (PU_VIEW | PU_LITE | PU_FLOW | PU_MONSTERS | PU_MON_LITE);
3515
3516                 /* Window stuff */
3517                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3518         }
3519
3520         /* Notice changes in view */
3521         if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
3522                 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !p_ptr->inside_battle)))
3523         {
3524                 /* Update some things */
3525                 p_ptr->update |= (PU_MON_LITE);
3526         }
3527
3528         /* Learn things from observable monster */
3529         if (m_ptr->ml && is_original_ap(m_ptr))
3530         {
3531                 /* Monster opened a door */
3532                 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
3533
3534                 /* Monster bashed a door */
3535                 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
3536
3537                 /* Monster tried to pick something up */
3538                 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
3539
3540                 /* Monster tried to crush something */
3541                 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
3542
3543                 /* Monster pushed past another monster */
3544                 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
3545
3546                 /* Monster passed through a wall */
3547                 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
3548
3549                 /* Monster destroyed a wall */
3550                 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
3551         }
3552
3553
3554         /* Hack -- get "bold" if out of options */
3555         if (!do_turn && !do_move && m_ptr->monfear && aware)
3556         {
3557                 /* No longer afraid */
3558                 m_ptr->monfear = 0;
3559
3560                 /* Message if seen */
3561                 if (m_ptr->ml)
3562                 {
3563                         char m_name[80];
3564
3565                         /* Acquire the monster name */
3566                         monster_desc(m_name, m_ptr, 0);
3567
3568                         /* Dump a message */
3569 #ifdef JP
3570 msg_format("%^s¤ÏÀ襤¤ò·è°Õ¤·¤¿¡ª", m_name);
3571 #else
3572                         msg_format("%^s turns to fight!", m_name);
3573 #endif
3574
3575                         /* Redraw (later) if needed */
3576                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
3577
3578                         chg_virtue(V_COMPASSION, -1);
3579                 }
3580
3581                 /* XXX XXX XXX Actually do something now (?) */
3582         }
3583 }
3584
3585 /*
3586  * Process all the "live" monsters, once per game turn.
3587  *
3588  * During each game turn, we scan through the list of all the "live" monsters,
3589  * (backwards, so we can excise any "freshly dead" monsters), energizing each
3590  * monster, and allowing fully energized monsters to move, attack, pass, etc.
3591  *
3592  * Note that monsters can never move in the monster array (except when the
3593  * "compact_monsters()" function is called by "dungeon()" or "save_player()").
3594  *
3595  * This function is responsible for at least half of the processor time
3596  * on a normal system with a "normal" amount of monsters and a player doing
3597  * normal things.
3598  *
3599  * When the player is resting, virtually 90% of the processor time is spent
3600  * in this function, and its children, "process_monster()" and "make_move()".
3601  *
3602  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",
3603  * especially when the player is running.
3604  *
3605  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"
3606  * monsters while they are still being "born".  A monster is "fresh" only
3607  * during the turn in which it is created, and we use the "hack_m_idx" to
3608  * determine if the monster is yet to be processed during the current turn.
3609  *
3610  * Note the special "MFLAG_NICE" flag, which allows the player to get one
3611  * move before any "nasty" monsters get to use their spell attacks.
3612  *
3613  * Note that when the "knowledge" about the currently tracked monster
3614  * changes (flags, attacks, spells), we induce a redraw of the monster
3615  * recall window.
3616  */
3617 void process_monsters(void)
3618 {
3619         int             i;
3620         int             fx, fy;
3621
3622         bool            test;
3623
3624         monster_type    *m_ptr;
3625         monster_race    *r_ptr;
3626
3627         int             old_monster_race_idx;
3628
3629         u32b    old_r_flags1 = 0L;
3630         u32b    old_r_flags2 = 0L;
3631         u32b    old_r_flags3 = 0L;
3632         u32b    old_r_flags4 = 0L;
3633         u32b    old_r_flags5 = 0L;
3634         u32b    old_r_flags6 = 0L;
3635         u32b    old_r_flagsr = 0L;
3636
3637         byte    old_r_blows0 = 0;
3638         byte    old_r_blows1 = 0;
3639         byte    old_r_blows2 = 0;
3640         byte    old_r_blows3 = 0;
3641
3642         byte    old_r_cast_spell = 0;
3643
3644         int speed;
3645
3646         /* Clear monster fighting indicator */
3647         mon_fight = FALSE;
3648
3649         /* Memorize old race */
3650         old_monster_race_idx = p_ptr->monster_race_idx;
3651
3652         /* Acquire knowledge */
3653         if (p_ptr->monster_race_idx)
3654         {
3655                 /* Acquire current monster */
3656                 r_ptr = &r_info[p_ptr->monster_race_idx];
3657
3658                 /* Memorize flags */
3659                 old_r_flags1 = r_ptr->r_flags1;
3660                 old_r_flags2 = r_ptr->r_flags2;
3661                 old_r_flags3 = r_ptr->r_flags3;
3662                 old_r_flags4 = r_ptr->r_flags4;
3663                 old_r_flags5 = r_ptr->r_flags5;
3664                 old_r_flags6 = r_ptr->r_flags6;
3665                 old_r_flagsr = r_ptr->r_flagsr;
3666
3667                 /* Memorize blows */
3668                 old_r_blows0 = r_ptr->r_blows[0];
3669                 old_r_blows1 = r_ptr->r_blows[1];
3670                 old_r_blows2 = r_ptr->r_blows[2];
3671                 old_r_blows3 = r_ptr->r_blows[3];
3672
3673                 /* Memorize castings */
3674                 old_r_cast_spell = r_ptr->r_cast_spell;
3675         }
3676
3677
3678         /* Process the monsters (backwards) */
3679         for (i = m_max - 1; i >= 1; i--)
3680         {
3681                 /* Access the monster */
3682                 m_ptr = &m_list[i];
3683                 r_ptr = &r_info[m_ptr->r_idx];
3684
3685                 /* Handle "leaving" */
3686                 if (p_ptr->leaving) break;
3687
3688                 /* Ignore "dead" monsters */
3689                 if (!m_ptr->r_idx) continue;
3690
3691                 if (p_ptr->wild_mode) continue;
3692
3693
3694                 /* Handle "fresh" monsters */
3695                 if (m_ptr->mflag & MFLAG_BORN)
3696                 {
3697                         /* No longer "fresh" */
3698                         m_ptr->mflag &= ~(MFLAG_BORN);
3699
3700                         /* Skip */
3701                         continue;
3702                 }
3703
3704                 /* Hack -- Require proximity */
3705                 if (m_ptr->cdis >= AAF_LIMIT) continue;
3706
3707
3708                 /* Access the location */
3709                 fx = m_ptr->fx;
3710                 fy = m_ptr->fy;
3711
3712                 /* Flow by smell is allowed */
3713                 if (!p_ptr->no_flowed)
3714                 {
3715                         m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3716                 }
3717
3718                 /* Assume no move */
3719                 test = FALSE;
3720
3721                 /* Handle "sensing radius" */
3722                 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
3723                 {
3724                         /* We can "sense" the player */
3725                         test = TRUE;
3726                 }
3727
3728                 /* Handle "sight" and "aggravation" */
3729                 else if ((m_ptr->cdis <= MAX_SIGHT) &&
3730                         (player_has_los_bold(fy, fx) || (p_ptr->cursed & TRC_AGGRAVATE)))
3731                 {
3732                         /* We can "see" or "feel" the player */
3733                         test = TRUE;
3734                 }
3735
3736 #if 0 /* (cave[py][px].when == cave[fy][fx].when) is always FALSE... */
3737                 /* Hack -- Monsters can "smell" the player from far away */
3738                 /* Note that most monsters have "aaf" of "20" or so */
3739                 else if (!(m_ptr->mflag2 & MFLAG2_NOFLOW) &&
3740                         have_flag(f_flags_bold(py, px), FF_MOVE) &&
3741                         (cave[py][px].when == cave[fy][fx].when) &&
3742                         (cave[fy][fx].dist < MONSTER_FLOW_DEPTH) &&
3743                         (cave[fy][fx].dist < r_ptr->aaf))
3744                 {
3745                         /* We can "smell" the player */
3746                         test = TRUE;
3747                 }
3748 #endif
3749                 else if (m_ptr->target_y) test = TRUE;
3750
3751                 /* Do nothing */
3752                 if (!test) continue;
3753
3754
3755                 if (p_ptr->riding == i)
3756                         speed = p_ptr->pspeed;
3757                 else
3758                 {
3759                         speed = m_ptr->mspeed;
3760
3761                         /* Monsters move quickly in Nightmare mode */
3762                         if (ironman_nightmare) speed += 5;
3763
3764                         if (m_ptr->fast) speed += 10;
3765                         if (m_ptr->slow) speed -= 10;
3766                 }
3767
3768                 /* Give this monster some energy */
3769                 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
3770
3771                 /* Not enough energy to move */
3772                 if (m_ptr->energy_need > 0) continue;
3773
3774                 /* Use up "some" energy */
3775                 m_ptr->energy_need += ENERGY_NEED();
3776
3777
3778                 /* Save global index */
3779                 hack_m_idx = i;
3780
3781                 /* Process the monster */
3782                 process_monster(i);
3783
3784                 reset_target(m_ptr);
3785
3786                 /* Give up flow_by_smell when it might useless */
3787                 if (p_ptr->no_flowed && one_in_(3))
3788                         m_ptr->mflag2 |= MFLAG2_NOFLOW;
3789
3790                 /* Hack -- notice death or departure */
3791                 if (!p_ptr->playing || p_ptr->is_dead) break;
3792
3793                 /* Notice leaving */
3794                 if (p_ptr->leaving) break;
3795         }
3796
3797         /* Reset global index */
3798         hack_m_idx = 0;
3799
3800
3801         /* Tracking a monster race (the same one we were before) */
3802         if (p_ptr->monster_race_idx && (p_ptr->monster_race_idx == old_monster_race_idx))
3803         {
3804                 /* Acquire monster race */
3805                 r_ptr = &r_info[p_ptr->monster_race_idx];
3806
3807                 /* Check for knowledge change */
3808                 if ((old_r_flags1 != r_ptr->r_flags1) ||
3809                         (old_r_flags2 != r_ptr->r_flags2) ||
3810                         (old_r_flags3 != r_ptr->r_flags3) ||
3811                         (old_r_flags4 != r_ptr->r_flags4) ||
3812                         (old_r_flags5 != r_ptr->r_flags5) ||
3813                         (old_r_flags6 != r_ptr->r_flags6) ||
3814                         (old_r_flagsr != r_ptr->r_flagsr) ||
3815                         (old_r_blows0 != r_ptr->r_blows[0]) ||
3816                         (old_r_blows1 != r_ptr->r_blows[1]) ||
3817                         (old_r_blows2 != r_ptr->r_blows[2]) ||
3818                         (old_r_blows3 != r_ptr->r_blows[3]) ||
3819                         (old_r_cast_spell != r_ptr->r_cast_spell))
3820                 {
3821                         /* Window stuff */
3822                         p_ptr->window |= (PW_MONSTER);
3823                 }
3824         }
3825 }
3826
3827
3828
3829 bool process_the_world(int num, int who, bool vs_player)
3830 {
3831         monster_type *m_ptr = &m_list[hack_m_idx];  /* the world monster */
3832
3833         if(world_monster) return (FALSE);
3834
3835         if(vs_player)
3836         {
3837                 char m_name[80];
3838                 monster_desc(m_name, m_ptr, 0);
3839
3840                 if (who == 1)
3841 #ifdef JP
3842                         msg_print("¡Ö¡Ø¥¶¡¦¥ï¡¼¥ë¥É¡Ù¡ª»þ¤Ï»ß¤Þ¤Ã¤¿¡ª¡×");
3843 #else
3844                         msg_format("%s yells 'The World! Time has stopped!'", m_name);
3845 #endif
3846                 else if (who == 3)
3847 #ifdef JP
3848                         msg_print("¡Ö»þ¤è¡ª¡×");
3849 #else
3850                         msg_format("%s yells 'Time!'", m_name);
3851 #endif
3852                 else msg_print("hek!");
3853
3854                 msg_print(NULL);
3855         }
3856
3857         /* This monster cast spells */
3858         world_monster = hack_m_idx;
3859
3860         if (vs_player) do_cmd_redraw();
3861
3862         while(num--)
3863         {
3864                 if(!m_ptr->r_idx) break;
3865                 process_monster(world_monster);
3866
3867                 reset_target(m_ptr);
3868
3869                 /* Notice stuff */
3870                 if (p_ptr->notice) notice_stuff();
3871
3872                 /* Update stuff */
3873                 if (p_ptr->update) update_stuff();
3874
3875                 /* Redraw stuff */
3876                 if (p_ptr->redraw) redraw_stuff();
3877
3878                 /* Redraw stuff */
3879                 if (p_ptr->window) window_stuff();
3880
3881                 /* Delay */
3882                 if (vs_player) Term_xtra(TERM_XTRA_DELAY, 500);
3883         }
3884
3885         /* Redraw map */
3886         p_ptr->redraw |= (PR_MAP);
3887
3888         /* Update monsters */
3889         p_ptr->update |= (PU_MONSTERS);
3890
3891         /* Window stuff */
3892         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3893
3894         world_monster = 0;
3895         if (vs_player || (los(py, px, m_ptr->fy, m_ptr->fx) && projectable(py, px, m_ptr->fy, m_ptr->fx)))
3896         {
3897 #ifdef JP
3898                 msg_print("¡Ö»þ¤ÏÆ°¤­¤À¤¹¡Ä¡×");
3899 #else
3900                 msg_print("You feel time flowing around you once more.");
3901 #endif
3902                 msg_print(NULL);
3903         }
3904
3905         handle_stuff();
3906
3907         return (TRUE);
3908 }
3909
3910
3911 void monster_gain_exp(int m_idx, int s_idx)
3912 {
3913         monster_type *m_ptr = &m_list[m_idx];
3914         monster_race *r_ptr = &r_info[m_ptr->r_idx];
3915         monster_race *s_ptr = &r_info[s_idx];
3916         int new_exp;
3917
3918         if (p_ptr->inside_battle) return;
3919
3920         if (!r_ptr->next_exp) return;
3921
3922         new_exp = s_ptr->mexp * s_ptr->level / (r_ptr->level + 2);
3923         if (m_idx == p_ptr->riding) new_exp = (new_exp + 1) / 2;
3924         if (!dun_level) new_exp /= 5;
3925         m_ptr->exp += new_exp;
3926         if (m_ptr->mflag2 & MFLAG2_CHAMELEON) return;
3927
3928         if (m_ptr->exp >= r_ptr->next_exp)
3929         {
3930                 char m_name[80];
3931                 int old_hp = m_ptr->hp;
3932                 int old_maxhp = m_ptr->max_maxhp;
3933                 int old_r_idx = m_ptr->r_idx;
3934                 byte old_sub_align = m_ptr->sub_align;
3935
3936                 /* Hack -- Reduce the racial counter of previous monster */
3937                 real_r_ptr(m_ptr)->cur_num--;
3938
3939                 monster_desc(m_name, m_ptr, 0);
3940                 m_ptr->r_idx = r_ptr->next_r_idx;
3941
3942                 /* Count the monsters on the level */
3943                 real_r_ptr(m_ptr)->cur_num++;
3944
3945                 m_ptr->ap_r_idx = m_ptr->r_idx;
3946                 r_ptr = &r_info[m_ptr->r_idx];
3947
3948                 if (r_ptr->flags1 & RF1_FORCE_MAXHP)
3949                 {
3950                         m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
3951                 }
3952                 else
3953                 {
3954                         m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
3955                 }
3956                 if (ironman_nightmare)
3957                 {
3958                         u32b hp = m_ptr->max_maxhp * 2L;
3959
3960                         m_ptr->max_maxhp = (s16b)MIN(30000, hp);
3961                 }
3962                 m_ptr->maxhp = m_ptr->max_maxhp;
3963                 m_ptr->hp = old_hp * m_ptr->maxhp / old_maxhp;
3964
3965                 /* Extract the monster base speed */
3966                 m_ptr->mspeed = get_mspeed(r_ptr);
3967
3968                 /* Sub-alignment of a monster */
3969                 if (!is_pet(m_ptr) && !(r_ptr->flags3 & (RF3_EVIL | RF3_GOOD)))
3970                         m_ptr->sub_align = old_sub_align;
3971                 else
3972                 {
3973                         m_ptr->sub_align = SUB_ALIGN_NEUTRAL;
3974                         if (r_ptr->flags3 & RF3_EVIL) m_ptr->sub_align |= SUB_ALIGN_EVIL;
3975                         if (r_ptr->flags3 & RF3_GOOD) m_ptr->sub_align |= SUB_ALIGN_GOOD;
3976                 }
3977
3978                 m_ptr->exp = 0;
3979
3980                 if (is_pet(m_ptr) || m_ptr->ml)
3981                 {
3982 #ifdef JP
3983                         msg_format("%s¤Ï%s¤Ë¿Ê²½¤·¤¿¡£", m_name, r_name + r_ptr->name);
3984 #else
3985                         msg_format("%^s evolved into %s.", m_name, r_name + r_ptr->name);
3986 #endif
3987                         r_info[old_r_idx].r_xtra1 |= MR1_SINKA;
3988
3989                         /* Now you feel very close to this pet. */
3990                         m_ptr->parent_m_idx = 0;
3991                 }
3992                 update_mon(m_idx, FALSE);
3993                 lite_spot(m_ptr->fy, m_ptr->fx);
3994         }
3995         if (m_idx == p_ptr->riding) p_ptr->update |= PU_BONUS;
3996 }