OSDN Git Service

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