OSDN Git Service

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