OSDN Git Service

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