OSDN Git Service

ハャ、ォ、熙荀ケ、、、隍ヲ、ヒ。「rand_int()、andint0()。「randint()、andint1()、ヒ、ケ、ル、ニテヨ、ュハム、ィ、ソ。」
[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                 /* Extract the attack "power" */
1546                 switch (effect)
1547                 {
1548                 case RBE_HURT:          power = 60; break;
1549                 case RBE_POISON:        power =  5; break;
1550                 case RBE_UN_BONUS:      power = 20; break;
1551                 case RBE_UN_POWER:      power = 15; break;
1552                 case RBE_EAT_GOLD:      power =  5; break;
1553                 case RBE_EAT_ITEM:      power =  5; break;
1554                 case RBE_EAT_FOOD:      power =  5; break;
1555                 case RBE_EAT_LITE:      power =  5; break;
1556                 case RBE_ACID:          power =  0; break;
1557                 case RBE_ELEC:          power = 10; break;
1558                 case RBE_FIRE:          power = 10; break;
1559                 case RBE_COLD:          power = 10; break;
1560                 case RBE_BLIND:         power =  2; break;
1561                 case RBE_CONFUSE:       power = 10; break;
1562                 case RBE_TERRIFY:       power = 10; break;
1563                 case RBE_PARALYZE:      power =  2; break;
1564                 case RBE_LOSE_STR:      power =  0; break;
1565                 case RBE_LOSE_DEX:      power =  0; break;
1566                 case RBE_LOSE_CON:      power =  0; break;
1567                 case RBE_LOSE_INT:      power =  0; break;
1568                 case RBE_LOSE_WIS:      power =  0; break;
1569                 case RBE_LOSE_CHR:      power =  0; break;
1570                 case RBE_LOSE_ALL:      power =  2; break;
1571                 case RBE_SHATTER:       power = 60; break;
1572                 case RBE_EXP_10:        power =  5; break;
1573                 case RBE_EXP_20:        power =  5; break;
1574                 case RBE_EXP_40:        power =  5; break;
1575                 case RBE_EXP_80:        power =  5; break;
1576                 case RBE_DISEASE:       power =  5; break;
1577                 case RBE_TIME:          power =  5; break;
1578                 case RBE_EXP_VAMP:      power =  5; break;
1579                 case RBE_DR_MANA:       power =  5; break;
1580                 case RBE_SUPERHURT:     power = 60; break;
1581                 }
1582
1583
1584                 /* Monster hits */
1585                 if (!effect || check_hit2(power, rlev, ac, m_ptr->stunned))
1586                 {
1587                         /* Describe the attack method */
1588                         switch (method)
1589                         {
1590                         case RBM_HIT:
1591                                 {
1592 #ifdef JP
1593 act = "%s¤ò²¥¤Ã¤¿¡£";
1594 #else
1595                                         act = "hits %s.";
1596 #endif
1597
1598                                         touched = TRUE;
1599                                         break;
1600                                 }
1601
1602                         case RBM_TOUCH:
1603                                 {
1604 #ifdef JP
1605 act = "%s¤ò¿¨¤Ã¤¿¡£";
1606 #else
1607                                         act = "touches %s.";
1608 #endif
1609
1610                                         touched = TRUE;
1611                                         break;
1612                                 }
1613
1614                         case RBM_PUNCH:
1615                                 {
1616 #ifdef JP
1617 act = "%s¤ò¥Ñ¥ó¥Á¤·¤¿¡£";
1618 #else
1619                                         act = "punches %s.";
1620 #endif
1621
1622                                         touched = TRUE;
1623                                         break;
1624                                 }
1625
1626                         case RBM_KICK:
1627                                 {
1628 #ifdef JP
1629 act = "%s¤ò½³¤Ã¤¿¡£";
1630 #else
1631                                         act = "kicks %s.";
1632 #endif
1633
1634                                         touched = TRUE;
1635                                         break;
1636                                 }
1637
1638                         case RBM_CLAW:
1639                                 {
1640 #ifdef JP
1641 act = "%s¤ò¤Ò¤Ã¤«¤¤¤¿¡£";
1642 #else
1643                                         act = "claws %s.";
1644 #endif
1645
1646                                         touched = TRUE;
1647                                         break;
1648                                 }
1649
1650                         case RBM_BITE:
1651                                 {
1652 #ifdef JP
1653 act = "%s¤ò³ú¤ó¤À¡£";
1654 #else
1655                                         act = "bites %s.";
1656 #endif
1657
1658                                         touched = TRUE;
1659                                         break;
1660                                 }
1661
1662                         case RBM_STING:
1663                                 {
1664 #ifdef JP
1665 act = "%s¤ò»É¤·¤¿¡£";
1666 #else
1667                                         act = "stings %s.";
1668 #endif
1669
1670                                         touched = TRUE;
1671                                         break;
1672                                 }
1673
1674                         case RBM_SLASH:
1675                                 {
1676 #ifdef JP
1677 act = "%s¤ò»Â¤Ã¤¿¡£";
1678 #else
1679                                         act = "slashes %s.";
1680 #endif
1681
1682                                         break;
1683                                 }
1684
1685                         case RBM_BUTT:
1686                                 {
1687 #ifdef JP
1688 act = "%s¤ò³Ñ¤ÇÆͤ¤¤¿¡£";
1689 #else
1690                                         act = "butts %s.";
1691 #endif
1692
1693                                         touched = TRUE;
1694                                         break;
1695                                 }
1696
1697                         case RBM_CRUSH:
1698                                 {
1699 #ifdef JP
1700 act = "%s¤ËÂÎÅö¤ê¤·¤¿¡£";
1701 #else
1702                                         act = "crushes %s.";
1703 #endif
1704
1705                                         touched = TRUE;
1706                                         break;
1707                                 }
1708
1709                         case RBM_ENGULF:
1710                                 {
1711 #ifdef JP
1712 act = "%s¤ò°û¤ß¹þ¤ó¤À¡£";
1713 #else
1714                                         act = "engulfs %s.";
1715 #endif
1716
1717                                         touched = TRUE;
1718                                         break;
1719                                 }
1720
1721                         case RBM_CHARGE:
1722                                 {
1723 #ifdef JP
1724 act = "%s¤ËÀÁµá½ñ¤ò¤è¤³¤·¤¿¡£";
1725 #else
1726                                         act = "charges %s.";
1727 #endif
1728
1729                                         touched = TRUE;
1730                                         break;
1731                                 }
1732
1733                         case RBM_CRAWL:
1734                                 {
1735 #ifdef JP
1736 act = "%s¤ÎÂΤξå¤òÇ礤²ó¤Ã¤¿¡£";
1737 #else
1738                                         act = "crawls on %s.";
1739 #endif
1740
1741                                         touched = TRUE;
1742                                         break;
1743                                 }
1744
1745                         case RBM_DROOL:
1746                                 {
1747 #ifdef JP
1748 act = "%s¤Ë¤è¤À¤ì¤ò¤¿¤é¤·¤¿¡£";
1749 #else
1750                                         act = "drools on %s.";
1751 #endif
1752
1753                                         touched = FALSE;
1754                                         break;
1755                                 }
1756
1757                         case RBM_SPIT:
1758                                 {
1759 #ifdef JP
1760 act = "%s¤ËÂäòÅǤ¤¤¿¡£";
1761 #else
1762                                         act = "spits on %s.";
1763 #endif
1764
1765                                         touched = FALSE;
1766                                         break;
1767                                 }
1768
1769                         case RBM_EXPLODE:
1770                                 {
1771                                         if (see_either) disturb(1, 0);
1772 #ifdef JP
1773 act = "Çúȯ¤·¤¿¡£";
1774 #else
1775                                         act = "explodes.";
1776 #endif
1777
1778                                         explode = TRUE;
1779                                         touched = FALSE;
1780                                         break;
1781                                 }
1782
1783                         case RBM_GAZE:
1784                                 {
1785 #ifdef JP
1786 act = "%s¤ò¤Ë¤é¤ó¤À¡£";
1787 #else
1788                                         act = "gazes at %s.";
1789 #endif
1790
1791                                         touched = FALSE;
1792                                         break;
1793                                 }
1794
1795                         case RBM_WAIL:
1796                                 {
1797 #ifdef JP
1798 act = "%s¤Ëµã¤­¤Ä¤¤¤¿¡£";
1799 #else
1800                                         act = "wails at %s.";
1801 #endif
1802
1803                                         touched = FALSE;
1804                                         break;
1805                                 }
1806
1807                         case RBM_SPORE:
1808                                 {
1809 #ifdef JP
1810 act = "%s¤Ë˦»Ò¤òÈô¤Ð¤·¤¿¡£";
1811 #else
1812                                         act = "releases spores at %s.";
1813 #endif
1814
1815                                         touched = FALSE;
1816                                         break;
1817                                 }
1818
1819                         case RBM_XXX4:
1820                                 {
1821 #ifdef JP
1822 act = "%s¤ËXXX4¤òÈô¤Ð¤·¤¿¡£";
1823 #else
1824                                         act = "projects XXX4's at %s.";
1825 #endif
1826
1827                                         touched = FALSE;
1828                                         break;
1829                                 }
1830
1831                         case RBM_BEG:
1832                                 {
1833 #ifdef JP
1834 act = "%s¤Ë¶â¤ò¤»¤¬¤ó¤À¡£";
1835 #else
1836                                         act = "begs %s for money.";
1837 #endif
1838
1839                                         touched = FALSE;
1840                                         break;
1841                                 }
1842
1843                         case RBM_INSULT:
1844                                 {
1845 #ifdef JP
1846 act = "%s¤òÉî¿«¤·¤¿¡£";
1847 #else
1848                                         act = "insults %s.";
1849 #endif
1850
1851                                         touched = FALSE;
1852                                         break;
1853                                 }
1854
1855                         case RBM_MOAN:
1856                                 {
1857 #ifdef JP
1858 act = "%s¤Ë¤à¤«¤Ã¤Æ¤¦¤á¤¤¤¿¡£";
1859 #else
1860                                         act = "moans at %s.";
1861 #endif
1862
1863                                         touched = FALSE;
1864                                         break;
1865                                 }
1866
1867                         case RBM_SHOW:
1868                                 {
1869 #ifdef JP
1870 act = "%s¤Ë¤à¤«¤Ã¤Æ²Î¤Ã¤¿¡£";
1871 #else
1872                                         act = "sings to %s.";
1873 #endif
1874
1875                                         touched = FALSE;
1876                                         break;
1877                                 }
1878                         }
1879
1880                         /* Message */
1881                         if (act && see_either)
1882                         {
1883                                 if (do_silly_attack)
1884                                 {
1885                                         act = silly_attacks[randint0(MAX_SILLY_ATTACK)];
1886                                 }
1887                                 strfmt(temp, act, t_name);
1888 #ifdef JP
1889                                 msg_format("%^s¤Ï%s", m_name, temp);
1890 #else
1891                                 msg_format("%^s %s", m_name, temp);
1892 #endif
1893
1894                         }
1895
1896                         /* Hack -- assume all attacks are obvious */
1897                         obvious = TRUE;
1898
1899                         /* Roll out the damage */
1900                         damage = damroll(d_dice, d_side);
1901
1902                         /* Assume no healing effect */
1903                         heal_effect = FALSE;
1904
1905                         pt = GF_MISSILE;
1906
1907                         /* Apply appropriate damage */
1908                         switch (effect)
1909                         {
1910                         case 0:
1911                                 {
1912                                         damage = 0;
1913                                         pt = 0;
1914                                         break;
1915                                 }
1916
1917                         case RBE_SUPERHURT:
1918                                 {
1919                                         if ((randint1(rlev*2+250) > (ac+200)) || one_in_(13)) {
1920                                                 int tmp_damage = damage-(damage*((ac < 150) ? ac : 150)/250);
1921                                                 damage = MAX(damage, tmp_damage*2);
1922                                                 break;
1923                                         }
1924                                 }
1925                         case RBE_HURT:
1926                                 {
1927                                         damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1928                                         break;
1929                                 }
1930
1931                         case RBE_POISON:
1932                         case RBE_DISEASE:
1933                                 {
1934                                         pt = GF_POIS;
1935                                         break;
1936                                 }
1937
1938                         case RBE_UN_BONUS:
1939                         case RBE_UN_POWER:
1940                                 {
1941                                         pt = GF_DISENCHANT;
1942                                         break;
1943                                 }
1944
1945                         case RBE_EAT_FOOD:
1946                         case RBE_EAT_LITE:
1947                         case RBE_DR_MANA:
1948                                 {
1949                                         pt = damage = 0;
1950                                         break;
1951                                 }
1952
1953                         case RBE_EAT_ITEM:
1954                         case RBE_EAT_GOLD:
1955                                 {
1956                                         pt = damage = 0;
1957                                         if (one_in_(2)) blinked = TRUE;
1958                                         break;
1959                                 }
1960
1961                         case RBE_ACID:
1962                                 {
1963                                         pt = GF_ACID;
1964                                         break;
1965                                 }
1966
1967                         case RBE_ELEC:
1968                                 {
1969                                         pt = GF_ELEC;
1970                                         break;
1971                                 }
1972
1973                         case RBE_FIRE:
1974                                 {
1975                                         pt = GF_FIRE;
1976                                         break;
1977                                 }
1978
1979                         case RBE_COLD:
1980                                 {
1981                                         pt = GF_COLD;
1982                                         break;
1983                                 }
1984
1985                         case RBE_BLIND:
1986                                 {
1987                                         break;
1988                                 }
1989
1990                         case RBE_CONFUSE:
1991                                 {
1992                                         pt = GF_CONFUSION;
1993                                         break;
1994                                 }
1995
1996                         case RBE_TERRIFY:
1997                                 {
1998                                         pt = GF_TURN_ALL;
1999                                         break;
2000                                 }
2001
2002                         case RBE_PARALYZE:
2003                                 {
2004                                         pt = GF_OLD_SLEEP; /* sort of close... */
2005                                         break;
2006                                 }
2007
2008                         case RBE_LOSE_STR:
2009                         case RBE_LOSE_INT:
2010                         case RBE_LOSE_WIS:
2011                         case RBE_LOSE_DEX:
2012                         case RBE_LOSE_CON:
2013                         case RBE_LOSE_CHR:
2014                         case RBE_LOSE_ALL:
2015                                 {
2016                                         break;
2017                                 }
2018                         case RBE_SHATTER:
2019                                 {
2020                                         if (damage > 23)
2021                                         {
2022                                                 earthquake(m_ptr->fy, m_ptr->fx, 8);
2023                                         }
2024                                         break;
2025                                 }
2026                         case RBE_EXP_10:
2027                         case RBE_EXP_20:
2028                         case RBE_EXP_40:
2029                         case RBE_EXP_80:
2030                                 {
2031                                         pt = GF_NETHER;
2032                                         break;
2033                                 }
2034                         case RBE_TIME:
2035                                 {
2036                                         pt = GF_TIME;
2037                                         break;
2038                                 }
2039                         case RBE_EXP_VAMP:
2040                                 {
2041                                         pt = GF_OLD_DRAIN;
2042                                         heal_effect = TRUE;
2043                                         break;
2044                                 }
2045
2046                         default:
2047                                 {
2048                                         pt = 0;
2049                                         break;
2050                                 }
2051                         }
2052
2053                         if (pt)
2054                         {
2055                                 /* Do damage if not exploding */
2056                                 if (!explode)
2057                                 {
2058                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
2059                                                 (pt == GF_OLD_SLEEP ? r_ptr->level : damage), pt, PROJECT_KILL | PROJECT_STOP | PROJECT_MONSTER | PROJECT_NO_REF, -1);
2060                                 }
2061
2062                                 if (heal_effect)
2063                                 {
2064                                         if ((monster_living(tr_ptr)) && (damage > 2))
2065                                         {
2066                                                 bool did_heal = FALSE;
2067
2068                                                 if (m_ptr->hp < m_ptr->maxhp) did_heal = TRUE;
2069
2070                                                 /* Heal */
2071                                                 m_ptr->hp += damroll(4, damage / 6);
2072                                                 if (m_ptr->hp > m_ptr->maxhp) m_ptr->hp = m_ptr->maxhp;
2073
2074                                                 /* Redraw (later) if needed */
2075                                                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2076                                                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2077
2078                                                 /* Special message */
2079                                                 if (see_m && did_heal)
2080                                                 {
2081 #ifdef JP
2082 msg_format("%s¤ÏÂÎÎϤò²óÉü¤·¤¿¤è¤¦¤À¡£", m_name);
2083 #else
2084                                                         msg_format("%^s appears healthier.", m_name);
2085 #endif
2086
2087                                                 }
2088                                         }
2089                                 }
2090
2091                                 if (touched)
2092                                 {
2093                                         /* Aura fire */
2094                                         if ((tr_ptr->flags2 & RF2_AURA_FIRE) &&
2095                                                 !(r_ptr->flags3 & RF3_IM_FIRE) &&
2096                                                 !(r_ptr->flags3 & RF3_RES_ALL) && m_ptr->r_idx)
2097                                         {
2098                                                 if (see_either)
2099                                                 {
2100                                                         blinked = FALSE;
2101 #ifdef JP
2102 msg_format("%^s¤ÏÆÍÁ³Ç®¤¯¤Ê¤Ã¤¿¡ª", m_name);
2103 #else
2104                                                         msg_format("%^s is suddenly very hot!", m_name);
2105 #endif
2106
2107                                                         if (t_ptr->ml)
2108                                                                 tr_ptr->r_flags2 |= RF2_AURA_FIRE;
2109                                                 }
2110                                                 project(t_idx, 0, m_ptr->fy, m_ptr->fx,
2111                                                         damroll (1 + ((tr_ptr->level) / 26),
2112                                                         1 + ((tr_ptr->level) / 17)),
2113                                                         GF_FIRE, PROJECT_KILL | PROJECT_STOP | PROJECT_MONSTER | PROJECT_NO_REF, -1);
2114                                         }
2115
2116                                         /* Aura cold */
2117                                         if ((tr_ptr->flags3 & RF3_AURA_COLD) &&
2118                                                 !(r_ptr->flags3 & RF3_IM_COLD) &&
2119                                                 !(r_ptr->flags3 & RF3_RES_ALL) && m_ptr->r_idx)
2120                                         {
2121                                                 if (see_either)
2122                                                 {
2123                                                         blinked = FALSE;
2124 #ifdef JP
2125 msg_format("%^s¤ÏÆÍÁ³´¨¤¯¤Ê¤Ã¤¿¡ª", m_name);
2126 #else
2127                                                         msg_format("%^s is suddenly very cold!", m_name);
2128 #endif
2129
2130                                                         if (t_ptr->ml)
2131                                                                 tr_ptr->r_flags3 |= RF3_AURA_COLD;
2132                                                 }
2133                                                 project(t_idx, 0, m_ptr->fy, m_ptr->fx,
2134                                                         damroll (1 + ((tr_ptr->level) / 26),
2135                                                         1 + ((tr_ptr->level) / 17)),
2136                                                         GF_COLD, PROJECT_KILL | PROJECT_STOP | PROJECT_MONSTER | PROJECT_NO_REF, -1);
2137                                         }
2138
2139                                         /* Aura elec */
2140                                         if ((tr_ptr->flags2 & (RF2_AURA_ELEC)) &&
2141                                                 !(r_ptr->flags3 & (RF3_IM_ELEC)) &&
2142                                                 !(r_ptr->flags3 & RF3_RES_ALL) && m_ptr->r_idx)
2143                                         {
2144                                                 if (see_either)
2145                                                 {
2146                                                         blinked = FALSE;
2147 #ifdef JP
2148 msg_format("%^s¤ÏÅÅ·â¤ò¿©¤é¤Ã¤¿¡ª", m_name);
2149 #else
2150                                                         msg_format("%^s gets zapped!", m_name);
2151 #endif
2152
2153                                                         if (t_ptr->ml)
2154                                                                 tr_ptr->r_flags2 |= RF2_AURA_ELEC;
2155                                                 }
2156                                                 project(t_idx, 0, m_ptr->fy, m_ptr->fx,
2157                                                         damroll (1 + ((tr_ptr->level) / 26),
2158                                                         1 + ((tr_ptr->level) / 17)),
2159                                                         GF_ELEC, PROJECT_KILL | PROJECT_STOP | PROJECT_MONSTER | PROJECT_NO_REF, -1);
2160                                         }
2161
2162                                 }
2163                         }
2164                 }
2165
2166                 /* Monster missed player */
2167                 else
2168                 {
2169                         /* Analyze failed attacks */
2170                         switch (method)
2171                         {
2172                         case RBM_HIT:
2173                         case RBM_TOUCH:
2174                         case RBM_PUNCH:
2175                         case RBM_KICK:
2176                         case RBM_CLAW:
2177                         case RBM_BITE:
2178                         case RBM_STING:
2179                         case RBM_SLASH:
2180                         case RBM_BUTT:
2181                         case RBM_CRUSH:
2182                         case RBM_ENGULF:
2183                         case RBM_CHARGE:
2184                                 {
2185                                         /* Visible monsters */
2186                                         if (see_m)
2187                                         {
2188                                                 /* Message */
2189 #ifdef JP
2190 msg_format("%s¤Ï%^s¤Î¹¶·â¤ò¤«¤ï¤·¤¿¡£", t_name,m_name);
2191 #else
2192                                                 msg_format("%^s misses %s.", m_name, t_name);
2193 #endif
2194
2195                                         }
2196
2197                                         break;
2198                                 }
2199                         }
2200                 }
2201
2202
2203                 /* Analyze "visible" monsters only */
2204                 if (see_m && !do_silly_attack)
2205                 {
2206                         /* Count "obvious" attacks (and ones that cause damage) */
2207                         if (obvious || damage || (r_ptr->r_blows[ap_cnt] > 10))
2208                         {
2209                                 /* Count attacks of this type */
2210                                 if (r_ptr->r_blows[ap_cnt] < MAX_UCHAR)
2211                                 {
2212                                         r_ptr->r_blows[ap_cnt]++;
2213                                 }
2214                         }
2215                 }
2216         }
2217
2218         if (explode)
2219         {
2220                 sound(SOUND_EXPLODE);
2221
2222                 /* Cancel Invulnerability */
2223                 if (m_ptr->invulner) m_ptr->invulner = 0;
2224
2225 #ifdef JP
2226 mon_take_hit_mon(FALSE, m_idx, m_ptr->hp + 1, &fear, "¤ÏÇúȯ¤·¤ÆÊ´¡¹¤Ë¤Ê¤Ã¤¿¡£", m_idx);
2227 #else
2228                 mon_take_hit_mon(FALSE, m_idx, m_ptr->hp + 1, &fear, " explodes into tiny shreds.", m_idx);
2229 #endif
2230
2231
2232                 blinked = FALSE;
2233         }
2234
2235
2236         /* Blink away */
2237         if (blinked)
2238         {
2239                 if (see_m)
2240                 {
2241 #ifdef JP
2242 msg_print("Å¥ËÀ¤Ï¾Ð¤Ã¤Æƨ¤²¤¿¡ª");
2243 #else
2244                         msg_print("The thief flees laughing!");
2245 #endif
2246
2247                 }
2248                 else if (known)
2249                 {
2250                         mon_fight = TRUE;
2251                 }
2252
2253                 teleport_away(m_idx, MAX_SIGHT * 2 + 5, FALSE);
2254         }
2255
2256         return TRUE;
2257 }
2258
2259
2260 /*
2261  * Hack -- local "player stealth" value (see below)
2262  */
2263 static u32b noise = 0L;
2264
2265
2266 /*
2267  * Process a monster
2268  *
2269  * The monster is known to be within 100 grids of the player
2270  *
2271  * In several cases, we directly update the monster lore
2272  *
2273  * Note that a monster is only allowed to "reproduce" if there
2274  * are a limited number of "reproducing" monsters on the current
2275  * level.  This should prevent the level from being "swamped" by
2276  * reproducing monsters.  It also allows a large mass of mice to
2277  * prevent a louse from multiplying, but this is a small price to
2278  * pay for a simple multiplication method.
2279  *
2280  * XXX Monster fear is slightly odd, in particular, monsters will
2281  * fixate on opening a door even if they cannot open it.  Actually,
2282  * the same thing happens to normal monsters when they hit a door
2283  *
2284  * XXX XXX XXX In addition, monsters which *cannot* open or bash
2285  * down a door will still stand there trying to open it...
2286  *
2287  * XXX Technically, need to check for monster in the way
2288  * combined with that monster being in a wall (or door?)
2289  *
2290  * A "direction" of "5" means "pick a random direction".
2291  */
2292 static void process_monster(int m_idx)
2293 {
2294         monster_type    *m_ptr = &m_list[m_idx];
2295         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
2296
2297         int             i, d, oy, ox, ny, nx;
2298
2299         int             mm[8];
2300
2301         cave_type       *c_ptr;
2302
2303         monster_type    *y_ptr;
2304
2305         bool            do_turn;
2306         bool            do_move;
2307         bool            do_view;
2308
2309         bool            did_open_door;
2310         bool            did_bash_door;
2311         bool            did_take_item;
2312         bool            did_kill_item;
2313         bool            did_move_body;
2314         bool            did_pass_wall;
2315         bool            did_kill_wall;
2316         bool            gets_angry = FALSE;
2317         bool            can_pass_wall;
2318         bool            aware = TRUE;
2319
2320         bool            fear;
2321
2322         if ((m_idx == p_ptr->riding) && !(r_ptr->flags7 & RF7_RIDING))
2323         {
2324                 if (rakuba(0, TRUE))
2325                 {
2326 #ifdef JP
2327                         msg_print("ÃÏÌ̤ËÍî¤È¤µ¤ì¤¿¡£");
2328 #else
2329                         char m_name[80];
2330                         monster_desc(m_name, &m_list[p_ptr->riding], 0);
2331                         msg_format("You have fallen from %s.", m_name);
2332 #endif
2333                 }
2334         }
2335
2336         if ((m_ptr->mflag2 & MFLAG_CHAMELEON) && one_in_(13) && !m_ptr->csleep)
2337         {
2338                 choose_new_monster(m_idx, FALSE, 0);
2339                 r_ptr = &r_info[m_ptr->r_idx];
2340         }
2341
2342         /* Players hidden in shadow are almost imperceptable. -LM- */
2343         if (p_ptr->special_defense & NINJA_S_STEALTH)
2344         {
2345                 int tmp = p_ptr->lev*6+(p_ptr->skill_stl+10)*4;
2346                 if (p_ptr->monlite) tmp /= 3;
2347                 if (p_ptr->aggravate) tmp /= 2;
2348                 if (r_ptr->level > (p_ptr->lev*p_ptr->lev/20+10)) tmp /= 3;
2349                 /* Low-level monsters will find it difficult to locate the player. */
2350                 if (randint0(tmp) > (r_ptr->level+20)) aware = FALSE;
2351         }
2352
2353         /* Quantum monsters are odd */
2354         if (r_ptr->flags2 & (RF2_QUANTUM))
2355         {
2356                 /* Sometimes skip move */
2357                 if (!randint0(2)) return;
2358
2359                 /* Sometimes die */
2360                 if (!randint0((m_idx % 100) + 10) && !(r_ptr->flags1 & RF1_QUESTOR))
2361                 {
2362                         bool sad = FALSE;
2363
2364                         if (is_pet(m_ptr) && !(m_ptr->ml))
2365                                 sad = TRUE;
2366
2367                         if (m_ptr->ml)
2368                         {
2369                                 char m_name[80];
2370
2371                                 /* Acquire the monster name */
2372                                 monster_desc(m_name, m_ptr, 0);
2373
2374                                 /* Oops */
2375 #ifdef JP
2376 msg_format("%s¤Ï¾Ã¤¨µî¤Ã¤¿¡ª", m_name);
2377 #else
2378                                 msg_format("%^s disappears!", m_name);
2379 #endif
2380
2381                         }
2382
2383                         /* Generate treasure, etc */
2384                         monster_death(m_idx, FALSE);
2385
2386                         /* Delete the monster */
2387                         delete_monster_idx(m_idx);
2388
2389                         if (sad)
2390                         {
2391 #ifdef JP
2392 msg_print("¾¯¤·¤Î´ÖÈᤷ¤¤µ¤Ê¬¤Ë¤Ê¤Ã¤¿¡£");
2393 #else
2394                                 msg_print("You feel sad for a moment.");
2395 #endif
2396
2397                         }
2398
2399                         return;
2400                 }
2401         }
2402
2403         if (m_ptr->r_idx == MON_SHURYUUDAN)
2404 #ifdef JP
2405                 mon_take_hit_mon(FALSE, m_idx, 1, &fear, "¤ÏÇúȯ¤·¤ÆÊ´¡¹¤Ë¤Ê¤Ã¤¿¡£", m_idx);
2406 #else
2407                 mon_take_hit_mon(FALSE, m_idx, 1, &fear, " explodes into tiny shreds.", m_idx);
2408 #endif
2409
2410         if ((is_pet(m_ptr) || is_friendly(m_ptr)) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7)) && !p_ptr->inside_battle)
2411         {
2412                 static int riding_pinch = 0;
2413
2414                 if (m_ptr->hp < m_ptr->maxhp/3)
2415                 {
2416                         bool level_teleport = TRUE;
2417                         char m_name[80];
2418                         monster_desc(m_name, m_ptr, 0);
2419
2420                         if (m_idx == p_ptr->riding && riding_pinch < 2)
2421                         {
2422 #ifdef JP
2423 msg_format("%s¤Ï½ý¤ÎÄˤµ¤Î;¤ê¤¢¤Ê¤¿¤Î«Çû¤«¤éƨ¤ì¤è¤¦¤È¤·¤Æ¤¤¤ë¡£", m_name);
2424 #else
2425                                         msg_format("%^s seems to be in so much pain, and trying to escape from your restriction.", m_name);
2426 #endif
2427                                 riding_pinch++;
2428                                 level_teleport = FALSE;
2429                                 disturb(1, 0);
2430                         }
2431                         else if (m_ptr->ml)
2432                         {
2433                                 if (m_idx == p_ptr->riding)
2434                                 {
2435 #ifdef JP
2436 msg_format("%s¤Ï¤¢¤Ê¤¿¤Î«Çû¤«¤éæ½Ð¤·¤¿¡£", m_name);
2437 #else
2438                                         msg_format("%^s succeeded to escape from your restriction!", m_name);
2439 #endif
2440                                 }
2441                                 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))
2442                                 {
2443 #ifdef JP
2444 msg_format("%^s¡Ö¥Ô¥ó¥Á¤À¡ªÂàµÑ¤µ¤»¤Æ¤â¤é¤¦¡ª¡×", m_name);
2445 #else
2446                                         msg_format("%^s says 'It is the pinch! I will retreat'.", m_name);
2447 #endif
2448                                 }
2449 #ifdef JP
2450 msg_format("%^s¤¬¥Æ¥ì¥Ý¡¼¥È¡¦¥ì¥Ù¥ë¤Î´¬Êª¤òÆɤó¤À¡£", m_name);
2451 #else
2452                                 msg_format("%^s read a scroll of teleport level.", m_name);
2453 #endif
2454 #ifdef JP
2455 msg_format("%^s¤¬¾Ã¤¨µî¤Ã¤¿¡£", m_name);
2456 #else
2457                                 msg_format("%^s disappears.", m_name);
2458 #endif
2459                         }
2460
2461                         if (level_teleport)
2462                         {
2463                                 delete_monster_idx(m_idx);
2464
2465                                 if (m_idx == p_ptr->riding)
2466                                 {
2467                                         if (rakuba(-1, FALSE))
2468                                         {
2469 #ifdef JP
2470 msg_print("ÃÏÌ̤ËÍî¤È¤µ¤ì¤¿¡£");
2471 #else
2472                                                 msg_print("You have fallen from riding pet.");
2473 #endif
2474                                         }
2475                                 }
2476                                 return;
2477                         }
2478                 }
2479                 else
2480                         if (m_idx == p_ptr->riding) riding_pinch = 0;
2481                         
2482         }
2483
2484         /* Handle "sleep" */
2485         if (m_ptr->csleep)
2486         {
2487                 u32b notice = 0;
2488
2489                 /* Hack -- handle non-aggravation */
2490                 if (!p_ptr->aggravate) notice = randint0(1024);
2491
2492                 /* Nightmare monsters are more alert */
2493                 if (ironman_nightmare) notice /= 2;
2494
2495                 /* Hack -- See if monster "notices" player */
2496                 if ((notice * notice * notice) <= noise)
2497                 {
2498                         /* Hack -- amount of "waking" */
2499                         int d = 1;
2500
2501                         /* Wake up faster near the player */
2502                         if (m_ptr->cdis < 50) d = (100 / m_ptr->cdis);
2503
2504                         /* Hack -- handle aggravation */
2505                         if (p_ptr->aggravate) d = m_ptr->csleep;
2506
2507                         /* Still asleep */
2508                         if (m_ptr->csleep > d)
2509                         {
2510                                 /* Monster wakes up "a little bit" */
2511                                 m_ptr->csleep -= d;
2512
2513                                 /* Notice the "not waking up" */
2514                                 if (m_ptr->ml)
2515                                 {
2516                                         /* Hack -- Count the ignores */
2517                                         if (r_ptr->r_ignore < MAX_UCHAR)
2518                                         {
2519                                                 r_ptr->r_ignore++;
2520                                         }
2521                                 }
2522                         }
2523
2524                         /* Just woke up */
2525                         else
2526                         {
2527                                 /* Reset sleep counter */
2528                                 m_ptr->csleep = 0;
2529
2530                                 /* Notice the "waking up" */
2531                                 if (m_ptr->ml)
2532                                 {
2533                                         char m_name[80];
2534
2535                                         /* Acquire the monster name */
2536                                         monster_desc(m_name, m_ptr, 0);
2537
2538                                         /* Dump a message */
2539 #ifdef JP
2540 msg_format("%^s¤¬Ìܤò³Ð¤Þ¤·¤¿¡£", m_name);
2541 #else
2542                                         msg_format("%^s wakes up.", m_name);
2543 #endif
2544
2545
2546                                         /* Redraw the health bar */
2547                                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2548                                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2549
2550                                         if (r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2))
2551                                                 p_ptr->update |= (PU_MON_LITE);
2552
2553                                         /* Hack -- Count the wakings */
2554                                         if (r_ptr->r_wake < MAX_UCHAR)
2555                                         {
2556                                                 r_ptr->r_wake++;
2557                                         }
2558                                 }
2559                         }
2560                 }
2561
2562                 /* Still sleeping */
2563                 if (m_ptr->csleep) return;
2564         }
2565
2566
2567         /* Handle "stun" */
2568         if (m_ptr->stunned)
2569         {
2570                 int d = 1;
2571
2572                 /* Make a "saving throw" against stun */
2573                 if (randint0(10000) <= r_ptr->level * r_ptr->level)
2574                 {
2575                         /* Recover fully */
2576                         d = m_ptr->stunned;
2577                 }
2578
2579                 /* Hack -- Recover from stun */
2580                 if (m_ptr->stunned > d)
2581                 {
2582                         /* Recover somewhat */
2583                         m_ptr->stunned -= d;
2584                 }
2585
2586                 /* Fully recover */
2587                 else
2588                 {
2589                         /* Recover fully */
2590                         m_ptr->stunned = 0;
2591
2592                         /* Message if visible */
2593                         if (m_ptr->ml)
2594                         {
2595                                 char m_name[80];
2596
2597                                 /* Acquire the monster name */
2598                                 monster_desc(m_name, m_ptr, 0);
2599
2600                                 /* Dump a message */
2601 #ifdef JP
2602 msg_format("%^s¤ÏÛ¯Û°¾õÂÖ¤«¤éΩ¤Áľ¤Ã¤¿¡£", m_name);
2603 #else
2604                                 msg_format("%^s is no longer stunned.", m_name);
2605 #endif
2606
2607                                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2608                                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2609                         }
2610                 }
2611
2612                 /* Still stunned */
2613                 if (m_ptr->stunned && one_in_(2)) return;
2614         }
2615
2616
2617         /* Handle confusion */
2618         if (m_ptr->confused)
2619         {
2620                 /* Amount of "boldness" */
2621                 int d = randint1(r_ptr->level / 20 + 1);
2622
2623                 /* Still confused */
2624                 if (m_ptr->confused > d)
2625                 {
2626                         /* Reduce the confusion */
2627                         m_ptr->confused -= d;
2628                 }
2629
2630                 /* Recovered */
2631                 else
2632                 {
2633                         /* No longer confused */
2634                         m_ptr->confused = 0;
2635
2636                         /* Message if visible */
2637                         if (m_ptr->ml)
2638                         {
2639                                 char m_name[80];
2640
2641                                 /* Acquire the monster name */
2642                                 monster_desc(m_name, m_ptr, 0);
2643
2644                                 /* Dump a message */
2645 #ifdef JP
2646 msg_format("%^s¤Ïº®Í𤫤éΩ¤Áľ¤Ã¤¿¡£", m_name);
2647 #else
2648                                 msg_format("%^s is no longer confused.", m_name);
2649 #endif
2650
2651                                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2652                                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2653                         }
2654                 }
2655         }
2656
2657         /* Handle Invulnerability */
2658         if (m_ptr->invulner)
2659         {
2660                 /* Reduce by one, note if expires */
2661                 m_ptr->invulner--;
2662
2663                 if (!(m_ptr->invulner) && m_ptr->ml)
2664                 {
2665                         char m_name[80];
2666
2667                         /* Acquire the monster name */
2668                         monster_desc(m_name, m_ptr, 0);
2669
2670                         /* Dump a message */
2671 #ifdef JP
2672 msg_format("%^s¤Ï¤â¤¦ÌµÅ¨¤Ç¤Ê¤¤¡£", m_name);
2673 #else
2674                         msg_format("%^s is no longer invulnerable.", m_name);
2675 #endif
2676
2677                         m_ptr->energy -= 100;
2678                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2679                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2680                 }
2681         }
2682
2683         /* Handle fast */
2684         if (m_ptr->fast)
2685         {
2686                 /* Reduce by one, note if expires */
2687                 m_ptr->fast--;
2688
2689                 if (!(m_ptr->fast) && m_ptr->ml)
2690                 {
2691                         char m_name[80];
2692
2693                         /* Acquire the monster name */
2694                         monster_desc(m_name, m_ptr, 0);
2695
2696                         /* Dump a message */
2697 #ifdef JP
2698 msg_format("%^s¤Ï¤â¤¦²Ã®¤µ¤ì¤Æ¤¤¤Ê¤¤¡£", m_name);
2699 #else
2700                         msg_format("%^s is no longer fast.", m_name);
2701 #endif
2702
2703                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2704                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2705                 }
2706         }
2707
2708         /* Handle slow */
2709         if (m_ptr->slow)
2710         {
2711                 /* Reduce by one, note if expires */
2712                 m_ptr->slow--;
2713
2714                 if (!(m_ptr->slow) && m_ptr->ml)
2715                 {
2716                         char m_name[80];
2717
2718                         /* Acquire the monster name */
2719                         monster_desc(m_name, m_ptr, 0);
2720
2721                         /* Dump a message */
2722 #ifdef JP
2723 msg_format("%^s¤Ï¤â¤¦¸ºÂ®¤µ¤ì¤Æ¤¤¤Ê¤¤¡£", m_name);
2724 #else
2725                         msg_format("%^s is no longer slow.", m_name);
2726 #endif
2727
2728                         if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2729                         if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
2730                 }
2731         }
2732
2733         if (p_ptr->riding == m_idx)
2734         {
2735                 p_ptr->update |= (PU_BONUS);
2736         }
2737
2738         /* No one wants to be your friend if you're aggravating */
2739         if (is_friendly(m_ptr) && p_ptr->aggravate)
2740                 gets_angry = TRUE;
2741
2742         /* Paranoia... no pet uniques outside wizard mode -- TY */
2743         if (is_pet(m_ptr) &&
2744             ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_UNIQUE_7)) &&
2745               ((p_ptr->align > 9 && (r_ptr->flags3 & RF3_EVIL)) ||
2746               (p_ptr->align < -9 && (r_ptr->flags3 & RF3_GOOD))))
2747              || (r_ptr->flags3 & RF3_RES_ALL)))
2748         {
2749                         gets_angry = TRUE;
2750         }
2751
2752         if (p_ptr->inside_battle) gets_angry = FALSE;
2753
2754         if (gets_angry)
2755         {
2756                 char m_name[80];
2757                 monster_desc(m_name, m_ptr, 0);
2758 #ifdef JP
2759 msg_format("%^s¤ÏÆÍÁ³Å¨¤Ë¤Þ¤ï¤Ã¤¿¡ª", m_name);
2760 #else
2761                 msg_format("%^s suddenly becomes hostile!", m_name);
2762 #endif
2763
2764                 set_hostile(m_ptr);
2765         }
2766
2767         /* Handle "fear" */
2768         if (m_ptr->monfear)
2769         {
2770                 /* Amount of "boldness" */
2771                 int d = randint1(r_ptr->level / 20 + 1);
2772
2773                 /* Still afraid */
2774                 if (m_ptr->monfear > d)
2775                 {
2776                         /* Reduce the fear */
2777                         m_ptr->monfear -= d;
2778                 }
2779
2780                 /* Recover from fear, take note if seen */
2781                 else
2782                 {
2783                         /* No longer afraid */
2784                         m_ptr->monfear = 0;
2785
2786                         /* Visual note */
2787                         if (m_ptr->ml)
2788                         {
2789                                 char m_name[80];
2790                                 char m_poss[80];
2791
2792                                 /* Acquire the monster name/poss */
2793                                 monster_desc(m_name, m_ptr, 0);
2794                                 monster_desc(m_poss, m_ptr, 0x22);
2795
2796                                 /* Dump a message */
2797 #ifdef JP
2798 msg_format("%^s¤Ïͦµ¤¤ò¼è¤êÌᤷ¤¿¡£", m_name);
2799 #else
2800                                 msg_format("%^s recovers %s courage.", m_name, m_poss);
2801 #endif
2802
2803                                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
2804                         }
2805                 }
2806         }
2807
2808         /* Get the origin */
2809         oy = m_ptr->fy;
2810         ox = m_ptr->fx;
2811
2812
2813         /* Attempt to "multiply" if able and allowed */
2814         if ((r_ptr->flags2 & RF2_MULTIPLY) && (num_repro < MAX_REPRO))
2815         {
2816                 int k, y, x;
2817
2818                 /* Count the adjacent monsters */
2819                 for (k = 0, y = oy - 1; y <= oy + 1; y++)
2820                 {
2821                         for (x = ox - 1; x <= ox + 1; x++)
2822                         {
2823                                 /* Ignore locations off of edge */
2824                                 if (!in_bounds2(y, x)) continue;
2825
2826                                 if (cave[y][x].m_idx) k++;
2827                         }
2828                 }
2829
2830                 /* Hack -- multiply slower in crowded areas */
2831                 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2832                 {
2833                         /* Try to multiply */
2834                         if (multiply_monster(m_idx, FALSE, is_friendly(m_ptr), is_pet(m_ptr)))
2835                         {
2836                                 /* Take note if visible */
2837                                 if (m_ptr->ml)
2838                                 {
2839                                         r_ptr->r_flags2 |= (RF2_MULTIPLY);
2840                                 }
2841
2842                                 /* Multiplying takes energy */
2843                                 return;
2844                         }
2845                 }
2846         }
2847
2848
2849         if (!p_ptr->inside_battle)
2850         {
2851                 /* Hack! "Cyber" monster makes noise... */
2852                 if (m_ptr->r_idx == MON_CYBER &&
2853                     one_in_(CYBERNOISE) &&
2854                     !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2855                 {
2856                         if (disturb_minor) disturb(FALSE, FALSE);
2857 #ifdef JP
2858 msg_print("½Å¸ü¤Ê­²»¤¬Ê¹¤³¤¨¤¿¡£");
2859 #else
2860                         msg_print("You hear heavy steps.");
2861 #endif
2862
2863                 }
2864
2865                 /* Some monsters can speak */
2866                 if ((r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
2867                         one_in_(SPEAK_CHANCE) &&
2868                         player_has_los_bold(oy, ox))
2869                 {
2870                         char m_name[80];
2871                         char monmessage[1024];
2872                         cptr filename;
2873
2874                         /* Acquire the monster name/poss */
2875                         if (m_ptr->ml)
2876                                 monster_desc(m_name, m_ptr, 0);
2877                         else
2878 #ifdef JP
2879 strcpy(m_name, "¤½¤ì");
2880 #else
2881                                 strcpy(m_name, "It");
2882 #endif
2883
2884
2885                         /* Select the file for monster quotes */
2886                         if (m_ptr->monfear)
2887 #ifdef JP
2888 filename = "monfear_j.txt";
2889 #else
2890                                 filename = "monfear.txt";
2891 #endif
2892
2893                         else if (is_pet(m_ptr))
2894 #ifdef JP
2895 filename = "monpet_j.txt";
2896 #else
2897                                 filename = "monpet.txt";
2898 #endif
2899
2900                         else if (is_friendly(m_ptr))
2901 #ifdef JP
2902 filename = "monfrien_j.txt";
2903 #else
2904                                 filename = "monfrien.txt";
2905 #endif
2906
2907                         else
2908 #ifdef JP
2909                                 filename = "monspeak_j.txt";
2910 #else
2911                                 filename = "monspeak.txt";
2912 #endif
2913
2914
2915                         /* Get the monster line */
2916                         if (get_rnd_line(filename, m_ptr->r_idx, monmessage) == 0)
2917                         {
2918                                 /* Say something */
2919 #ifdef JP
2920 msg_format("%^s%s", m_name, monmessage);
2921 #else
2922                                 msg_format("%^s %s", m_name, monmessage);
2923 #endif
2924
2925                         }
2926                 }
2927         }
2928
2929         /* Attempt to cast a spell */
2930         if (aware && make_attack_spell(m_idx)) return;
2931
2932         /*
2933          * Attempt to cast a spell at an enemy other than the player
2934          * (may slow the game a smidgeon, but I haven't noticed.)
2935          */
2936         if (monst_spell_monst(m_idx)) return;
2937
2938         can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || (p_ptr->pass_wall)));
2939
2940         /* Hack -- Assume no movement */
2941         mm[0] = mm[1] = mm[2] = mm[3] = 0;
2942         mm[4] = mm[5] = mm[6] = mm[7] = 0;
2943
2944
2945         /* Confused -- 100% random */
2946         if (m_ptr->confused || !aware)
2947         {
2948                 /* Try four "random" directions */
2949                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2950         }
2951
2952         /* 75% random movement */
2953         else if ((r_ptr->flags1 & RF1_RAND_50) &&
2954                                 (r_ptr->flags1 & RF1_RAND_25) &&
2955                  (randint0(100) < 75))
2956         {
2957                 /* Memorize flags */
2958                 if (m_ptr->ml) r_ptr->r_flags1 |= (RF1_RAND_50);
2959                 if (m_ptr->ml) r_ptr->r_flags1 |= (RF1_RAND_25);
2960
2961                 /* Try four "random" directions */
2962                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2963         }
2964
2965         /* 50% random movement */
2966         else if ((r_ptr->flags1 & RF1_RAND_50) &&
2967                                 (randint0(100) < 50))
2968         {
2969                 /* Memorize flags */
2970                 if (m_ptr->ml) r_ptr->r_flags1 |= (RF1_RAND_50);
2971
2972                 /* Try four "random" directions */
2973                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2974         }
2975
2976         /* 25% random movement */
2977         else if ((r_ptr->flags1 & RF1_RAND_25) &&
2978                                 (randint0(100) < 25))
2979         {
2980                 /* Memorize flags */
2981                 if (m_ptr->ml) r_ptr->r_flags1 |= RF1_RAND_25;
2982
2983                 /* Try four "random" directions */
2984                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2985         }
2986
2987         /* Can't reach player - find something else to hit */
2988         else if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2989         {
2990                 /* Try four "random" directions */
2991                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2992
2993                 /* Look for an enemy */
2994 #if 0  /* Hack - Too slow.  Mimic pits are horrible with this on. */
2995                 get_enemy_dir(m_idx, mm);
2996 #endif /* 0 */
2997         }
2998
2999         /* Pets will follow the player */
3000         else if (is_pet(m_ptr))
3001         {
3002                 /* Are we trying to avoid the player? */
3003                 bool avoid = ((p_ptr->pet_follow_distance < 0) &&
3004                                                   (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
3005
3006                 /* Do we want to find the player? */
3007                 bool lonely = (!avoid && (m_ptr->cdis > p_ptr->pet_follow_distance));
3008
3009                 /* Should we find the player if we can't find a monster? */
3010                 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
3011
3012                 /* by default, move randomly */
3013                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
3014
3015                 /* Look for an enemy */
3016                 if (!get_enemy_dir(m_idx, mm))
3017                 {
3018                         /* Find the player if necessary */
3019                         if (avoid || lonely || distant)
3020                         {
3021                                 /* Remember the leash length */
3022                                 int dis = p_ptr->pet_follow_distance;
3023
3024                                 /* Hack -- adjust follow distance temporarily */
3025                                 if (p_ptr->pet_follow_distance > PET_SEEK_DIST)
3026                                 {
3027                                         p_ptr->pet_follow_distance = PET_SEEK_DIST;
3028                                 }
3029
3030                                 /* Find the player */
3031                                 get_moves(m_idx, mm);
3032
3033                                 /* Restore the leash */
3034                                 p_ptr->pet_follow_distance = dis;
3035                         }
3036                 }
3037         }
3038
3039         /* Friendly monster movement */
3040         else if (!is_hostile(m_ptr))
3041         {
3042                 /* by default, move randomly */
3043                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
3044
3045                 /* Look for an enemy */
3046                 get_enemy_dir(m_idx, mm);
3047         }
3048         /* Normal movement */
3049         else if (stupid_monsters)
3050         {
3051                 /* Logical moves */
3052                 get_moves(m_idx, mm);
3053         }
3054         else
3055         {
3056                 /* Logical moves, may do nothing */
3057                 if (!get_moves(m_idx, mm)) return;
3058         }
3059
3060         /* Assume nothing */
3061         do_turn = FALSE;
3062         do_move = FALSE;
3063         do_view = FALSE;
3064
3065         /* Assume nothing */
3066         did_open_door = FALSE;
3067         did_bash_door = FALSE;
3068         did_take_item = FALSE;
3069         did_kill_item = FALSE;
3070         did_move_body = FALSE;
3071         did_pass_wall = FALSE;
3072         did_kill_wall = FALSE;
3073
3074
3075         /* Take a zero-terminated array of "directions" */
3076         for (i = 0; mm[i]; i++)
3077         {
3078                 /* Get the direction */
3079                 d = mm[i];
3080
3081                 /* Hack -- allow "randomized" motion */
3082                 if (d == 5) d = ddd[randint0(8)];
3083
3084                 /* Get the destination */
3085                 ny = oy + ddy[d];
3086                 nx = ox + ddx[d];
3087
3088                 /* Ignore locations off of edge */
3089                 if (!in_bounds2(ny, nx)) continue;
3090
3091                 /* Access that cave grid */
3092                 c_ptr = &cave[ny][nx];
3093
3094                 /* Access that cave grid's contents */
3095                 y_ptr = &m_list[c_ptr->m_idx];
3096
3097                 /* Floor is open? */
3098                 if (cave_floor_grid(c_ptr))
3099                 {
3100                         /* Go ahead and move */
3101                         do_move = TRUE;
3102                 }
3103
3104                 /* Hack -- player 'in' wall */
3105                 else if ((ny == py) && (nx == px))
3106                 {
3107                         do_move = TRUE;
3108                 }
3109
3110                 else if (c_ptr->m_idx)
3111                 {
3112                         /* Possibly a monster to attack */
3113                         do_move = TRUE;
3114                 }
3115
3116                 /* Permanent wall */
3117                 else if ((c_ptr->feat >= FEAT_PERM_EXTRA) &&
3118                         (c_ptr->feat <= FEAT_PERM_SOLID))
3119                 {
3120                         do_move = FALSE;
3121                 }
3122
3123                 /* Hack -- semi-transparent terrains are no obstacle */
3124                 else if (c_ptr->feat == FEAT_TREES)
3125                 {
3126                         do_move = TRUE;
3127                 }
3128
3129                 /* Hack -- semi-transparent terrains are no obstacle */
3130                 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)))))
3131                 {
3132                         do_move = TRUE;
3133                 }
3134
3135
3136                 /* Monster moves through walls (and doors) */
3137                 else if (can_pass_wall)
3138                 {
3139                         /* Pass through walls/doors/rubble */
3140                         do_move = TRUE;
3141
3142                         /* Monster went through a wall */
3143                         did_pass_wall = TRUE;
3144                 }
3145
3146                 /* Monster destroys walls (and doors) */
3147                 else if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding))
3148                 {
3149                         /* Eat through walls/doors/rubble */
3150                         do_move = TRUE;
3151
3152                         /* Monster destroyed a wall */
3153                         did_kill_wall = TRUE;
3154
3155                         if (one_in_(GRINDNOISE))
3156                         {
3157 #ifdef JP
3158 msg_print("¥®¥·¥®¥·¤¤¤¦²»¤¬Ê¹¤³¤¨¤ë¡£");
3159 #else
3160                                 msg_print("There is a grinding sound.");
3161 #endif
3162
3163                         }
3164
3165                         /* Forget the wall */
3166                         c_ptr->info &= ~(CAVE_MARK);
3167
3168                         /* Notice */
3169                         c_ptr->feat = floor_type[randint0(100)];
3170                         c_ptr->info &= ~(CAVE_MASK);
3171                         c_ptr->info |= CAVE_FLOOR;
3172
3173                         /* Note changes to viewable region */
3174                         if (player_has_los_bold(ny, nx)) do_view = TRUE;
3175                 }
3176
3177                 /* Handle doors and secret doors */
3178                 else if (((c_ptr->feat >= FEAT_DOOR_HEAD) &&
3179                           (c_ptr->feat <= FEAT_DOOR_TAIL)) ||
3180                           (c_ptr->feat == FEAT_SECRET))
3181                 {
3182                         bool may_bash = TRUE;
3183
3184                         /* Assume no move allowed */
3185                         do_move = FALSE;
3186
3187                         /* Creature can open doors. */
3188                         if ((r_ptr->flags2 & RF2_OPEN_DOOR) &&
3189                                  (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
3190                         {
3191                                 /* Closed doors and secret doors */
3192                                 if ((c_ptr->feat == FEAT_DOOR_HEAD) ||
3193                                         (c_ptr->feat == FEAT_SECRET))
3194                                 {
3195                                         /* The door is open */
3196                                         did_open_door = TRUE;
3197
3198                                         /* Do not bash the door */
3199                                         may_bash = FALSE;
3200
3201                                         /* Assume no move allowed */
3202                                         do_move = TRUE;
3203                                 }
3204
3205                                 /* Locked doors (not jammed) */
3206                                 else if (c_ptr->feat < FEAT_DOOR_HEAD + 0x08)
3207                                 {
3208                                         int k;
3209
3210                                         /* Door power */
3211                                         k = ((c_ptr->feat - FEAT_DOOR_HEAD) & 0x07);
3212
3213                                         /* Try to unlock it XXX XXX XXX */
3214                                         if (randint0(m_ptr->hp / 10) > k)
3215                                         {
3216                                                 /* Unlock the door */
3217                                                 cave_set_feat(ny, nx, FEAT_DOOR_HEAD + 0x00);
3218
3219                                                 /* Do not bash the door */
3220                                                 may_bash = FALSE;
3221                                         }
3222                                 }
3223                         }
3224
3225                         /* Stuck doors -- attempt to bash them down if allowed */
3226                         if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) &&
3227                                 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
3228                         {
3229                                 int k;
3230
3231                                 /* Door power */
3232                                 k = ((c_ptr->feat - FEAT_DOOR_HEAD) & 0x07);
3233
3234                                 /* Attempt to Bash XXX XXX XXX */
3235                                 if (randint0(m_ptr->hp / 10) > k)
3236                                 {
3237                                         /* Message */
3238 #ifdef JP
3239 msg_print("¥É¥¢¤ò᤭³«¤±¤ë²»¤¬¤·¤¿¡ª");
3240 #else
3241                                         msg_print("You hear a door burst open!");
3242 #endif
3243
3244
3245                                         /* Disturb (sometimes) */
3246                                         if (disturb_minor) disturb(0, 0);
3247
3248                                         /* The door was bashed open */
3249                                         did_bash_door = TRUE;
3250
3251                                         /* Hack -- fall into doorway */
3252                                         do_move = TRUE;
3253                                 }
3254                         }
3255
3256
3257                         /* Deal with doors in the way */
3258                         if (did_open_door || did_bash_door)
3259                         {
3260                                 /* Break down the door */
3261                                 if (did_bash_door && (randint0(100) < 50))
3262                                 {
3263                                         cave_set_feat(ny, nx, FEAT_BROKEN);
3264                                 }
3265
3266                                 /* Open the door */
3267                                 else
3268                                 {
3269                                         cave_set_feat(ny, nx, FEAT_OPEN);
3270                                 }
3271
3272                                 /* Handle viewable doors */
3273                                 if (player_has_los_bold(ny, nx)) do_view = TRUE;
3274                         }
3275                 }
3276
3277                 /* Hack -- check for Glyph of Warding */
3278                 if (do_move && (c_ptr->feat == FEAT_GLYPH) &&
3279                     !((r_ptr->flags1 & RF1_NEVER_BLOW) && (py == ny) && (px == nx)))
3280                 {
3281                         /* Assume no move allowed */
3282                         do_move = FALSE;
3283
3284                         /* Break the ward */
3285                         if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
3286                         {
3287                                 /* Describe observable breakage */
3288                                 if (c_ptr->info & CAVE_MARK)
3289                                 {
3290 #ifdef JP
3291 msg_print("¼é¤ê¤Î¥ë¡¼¥ó¤¬²õ¤ì¤¿¡ª");
3292 #else
3293                                         msg_print("The rune of protection is broken!");
3294 #endif
3295
3296                                 }
3297
3298                                 /* Forget the rune */
3299                                 c_ptr->info &= ~(CAVE_MARK);
3300
3301                                 /* Break the rune */
3302                                 c_ptr->feat = floor_type[randint0(100)];
3303                                 c_ptr->info &= ~(CAVE_MASK);
3304                                 c_ptr->info |= CAVE_FLOOR;
3305
3306                                 /* Allow movement */
3307                                 do_move = TRUE;
3308
3309                                 /* Notice */
3310                                 note_spot(ny, nx);
3311                         }
3312                 }
3313                 else if (do_move && (c_ptr->feat == FEAT_MINOR_GLYPH) &&
3314                          !((r_ptr->flags1 & RF1_NEVER_BLOW) && (py == ny) && (px == nx)))
3315                 {
3316                         /* Assume no move allowed */
3317                         do_move = FALSE;
3318
3319                         /* Break the ward */
3320                         if (!is_pet(m_ptr))
3321                         {
3322                                 /* Break the ward */
3323                                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
3324                                 {
3325                                         /* Describe observable breakage */
3326                                         if (c_ptr->info & CAVE_MARK)
3327                                         {
3328 #ifdef JP
3329 msg_print("¥ë¡¼¥ó¤¬Çúȯ¤·¤¿¡ª");
3330 #else
3331                                                 msg_print("The rune explodes!");
3332 #endif
3333
3334                                                 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);
3335                                         }
3336                                 }
3337                                 else
3338                                 {
3339 #ifdef JP
3340 msg_print("Çúȯ¤Î¥ë¡¼¥ó¤Ï²ò½ü¤µ¤ì¤¿¡£");
3341 #else
3342                                         msg_print("An explosive rune was disarmed.");
3343 #endif
3344                                 }
3345
3346                                 /* Forget the rune */
3347                                 c_ptr->info &= ~(CAVE_MARK);
3348
3349                                 /* Break the rune */
3350                                 c_ptr->feat = floor_type[randint0(100)];
3351                                 c_ptr->info &= ~(CAVE_MASK);
3352                                 c_ptr->info |= CAVE_FLOOR;
3353                                 note_spot(ny, nx);
3354                                 lite_spot(ny, nx);
3355
3356                                 if (!m_ptr->r_idx) return;
3357                                 /* Allow movement */
3358                                 do_move = TRUE;
3359                         }
3360                 }
3361                 if (do_move && (ny == py) && (nx == px) && (d_info[dungeon_type].flags1 & DF1_NO_MELEE))
3362                 {
3363                         do_move = FALSE;
3364                 }
3365
3366                 /* Some monsters never attack */
3367                 if (do_move && (ny == py) && (nx == px) &&
3368                         (r_ptr->flags1 & RF1_NEVER_BLOW))
3369                 {
3370                         /* Hack -- memorize lack of attacks */
3371                         if (m_ptr->ml) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
3372
3373                         /* Do not move */
3374                         do_move = FALSE;
3375                 }
3376
3377                 /* The player is in the way.  Attack him. */
3378                 if (do_move && (ny == py) && (nx == px))
3379                 {
3380                         if (!p_ptr->riding || one_in_(2))
3381                         {
3382                                 /* Do the attack */
3383                                 (void)make_attack_normal(m_idx);
3384
3385                                 /* Do not move */
3386                                 do_move = FALSE;
3387
3388                                 /* Took a turn */
3389                                 do_turn = TRUE;
3390                         }
3391                 }
3392
3393                 if ((c_ptr->feat >= FEAT_PATTERN_START) &&
3394                         (c_ptr->feat <= FEAT_PATTERN_XTRA2) &&
3395                         !do_turn && !(r_ptr->flags7 & RF7_CAN_FLY))
3396                 {
3397                         do_move = FALSE;
3398                 }
3399
3400
3401                 /* A monster is in the way */
3402                 if (do_move && c_ptr->m_idx)
3403                 {
3404                         monster_race *z_ptr = &r_info[y_ptr->r_idx];
3405                         monster_type *m2_ptr = &m_list[c_ptr->m_idx];
3406
3407                         /* Assume no movement */
3408                         do_move = FALSE;
3409
3410                         /* Attack 'enemies' */
3411                         if (((r_ptr->flags2 & (RF2_KILL_BODY)) &&
3412                                   (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
3413                                   (cave_floor_grid(c_ptr)) &&
3414                              (c_ptr->m_idx != p_ptr->riding)) ||
3415                                  are_enemies(m_ptr, m2_ptr) || m_ptr->confused)
3416                         {
3417                                 do_move = FALSE;
3418
3419                                 if (r_ptr->flags2 & RF2_KILL_BODY) r_ptr->r_flags2 |= (RF2_KILL_BODY);
3420
3421                                 /* attack */
3422                                 if ((m2_ptr->r_idx) && (m2_ptr->hp >= 0))
3423                                 {
3424                                         if (monst_attack_monst(m_idx, cave[ny][nx].m_idx))
3425                                         return;
3426                                 }
3427                         }
3428
3429                         /* Push past weaker monsters (unless leaving a wall) */
3430                         else if ((r_ptr->flags2 & RF2_MOVE_BODY) &&
3431                                 (r_ptr->mexp > z_ptr->mexp) && cave_floor_grid(c_ptr) &&
3432                                 (cave_floor_grid(&cave[m_ptr->fy][m_ptr->fx])) &&
3433                                  (c_ptr->m_idx != p_ptr->riding))
3434                         {
3435                                 /* Allow movement */
3436                                 do_move = TRUE;
3437
3438                                 /* Monster pushed past another monster */
3439                                 did_move_body = TRUE;
3440
3441                                 /* XXX XXX XXX Message */
3442                         }
3443                 }
3444
3445                 /*
3446                  * Check if monster can cross terrain
3447                  * This is checked after the normal attacks
3448                  * to allow monsters to attack an enemy,
3449                  * even if it can't enter the terrain.
3450                  */
3451                 if (do_move && !monster_can_cross_terrain(c_ptr->feat, r_ptr))
3452                 {
3453                         /* Assume no move allowed */
3454                         do_move = FALSE;
3455                 }
3456
3457                 /* Some monsters never move */
3458                 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
3459                 {
3460                         /* Hack -- memorize lack of attacks */
3461                         if (m_ptr->ml) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
3462
3463                         /* Do not move */
3464                         do_move = FALSE;
3465                 }
3466
3467                 if (m_idx == p_ptr->riding)
3468                 {
3469                         if (!p_ptr->riding_ryoute && !(m_list[p_ptr->riding].monfear)) do_move = FALSE;
3470                 }
3471
3472                 /* Creature has been allowed move */
3473                 if (do_move)
3474                 {
3475                         s16b this_o_idx, next_o_idx;
3476
3477                         /* Take a turn */
3478                         do_turn = TRUE;
3479
3480                         /* Hack -- Update the old location */
3481                         cave[oy][ox].m_idx = c_ptr->m_idx;
3482
3483                         if (cave[ny][nx].feat == FEAT_TREES)
3484                         {
3485                                 if (r_ptr->flags2 & RF2_KILL_WALL)
3486                                 {
3487                                         c_ptr->feat = FEAT_GRASS;
3488                                         c_ptr->info &= ~(CAVE_MASK);
3489                                         c_ptr->info |= CAVE_FLOOR;
3490                                 }
3491                                 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
3492                                 {
3493                                         m_ptr->energy -= 100;
3494                                 }
3495                         }
3496
3497                         /* Mega-Hack -- move the old monster, if any */
3498                         if (c_ptr->m_idx)
3499                         {
3500                                 /* Move the old monster */
3501                                 y_ptr->fy = oy;
3502                                 y_ptr->fx = ox;
3503
3504                                 /* Update the old monster */
3505                                 update_mon(c_ptr->m_idx, TRUE);
3506
3507                                 /* Wake up the moved monster */
3508                                 m_list[c_ptr->m_idx].csleep = 0;
3509                         }
3510
3511                         /* Hack -- Update the new location */
3512                         c_ptr->m_idx = m_idx;
3513
3514                         /* Move the monster */
3515                         m_ptr->fy = ny;
3516                         m_ptr->fx = nx;
3517
3518                         /* Update the monster */
3519                         update_mon(m_idx, TRUE);
3520
3521                         if (p_ptr->riding == m_idx)
3522                         {
3523                                 py = ny;
3524                                 px = nx;
3525                         }
3526
3527                         /* Redraw the old grid */
3528                         lite_spot(oy, ox);
3529
3530                         /* Redraw the new grid */
3531                         lite_spot(ny, nx);
3532
3533                         if (p_ptr->riding == m_idx)
3534                         {
3535                                 verify_panel();
3536
3537                                 /* Update stuff */
3538                                 p_ptr->update |= (PU_VIEW | PU_LITE | PU_FLOW);
3539
3540                                 /* Update the monsters */
3541                                 p_ptr->update |= (PU_DISTANCE);
3542
3543                                 /* Window stuff */
3544                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3545                         }
3546
3547                         /* Possible disturb */
3548                         if (m_ptr->ml && (disturb_move ||
3549                                 ((m_ptr->mflag & MFLAG_VIEW) &&
3550                                 disturb_near)))
3551                         {
3552                                 /* Disturb */
3553                                 if (is_hostile(m_ptr))
3554                                         disturb(0, 0);
3555                         }
3556
3557                         /* Scan all objects in the grid */
3558                         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
3559                         {
3560                                 object_type *o_ptr;
3561
3562                                 /* Acquire object */
3563                                 o_ptr = &o_list[this_o_idx];
3564
3565                                 /* Acquire next object */
3566                                 next_o_idx = o_ptr->next_o_idx;
3567
3568                                 /* Skip gold */
3569                                 if (o_ptr->tval == TV_GOLD) continue;
3570
3571                                 /*
3572                                  * Skip "real" corpses and statues, to avoid extreme
3573                                  * silliness like a novice rogue pockets full of statues
3574                                  * and corpses.
3575                                  */
3576                                 if ((o_ptr->tval == TV_CORPSE) ||
3577                                     (o_ptr->tval == TV_STATUE)) continue;
3578
3579                                 /* Take or Kill objects on the floor */
3580                                 if ((r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
3581                                          (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_PICKUP_ITEMS)))
3582                                 {
3583                                         u32b f1, f2, f3;
3584
3585                                         u32b flg3 = 0L;
3586
3587                                         char m_name[80];
3588                                         char o_name[MAX_NLEN];
3589
3590                                         /* Extract some flags */
3591                                         object_flags(o_ptr, &f1, &f2, &f3);
3592
3593                                         /* Acquire the object name */
3594                                         object_desc(o_name, o_ptr, TRUE, 3);
3595
3596                                         /* Acquire the monster name */
3597                                         monster_desc(m_name, m_ptr, 0x04);
3598
3599                                         /* React to objects that hurt the monster */
3600                                         if (f1 & TR1_KILL_DRAGON) flg3 |= (RF3_DRAGON);
3601                                         if (f1 & TR1_SLAY_DRAGON) flg3 |= (RF3_DRAGON);
3602                                         if (f1 & TR1_SLAY_TROLL)  flg3 |= (RF3_TROLL);
3603                                         if (f1 & TR1_SLAY_GIANT)  flg3 |= (RF3_GIANT);
3604                                         if (f1 & TR1_SLAY_ORC)    flg3 |= (RF3_ORC);
3605                                         if (f1 & TR1_SLAY_DEMON)  flg3 |= (RF3_DEMON);
3606                                         if (f1 & TR1_SLAY_UNDEAD) flg3 |= (RF3_UNDEAD);
3607                                         if (f1 & TR1_SLAY_ANIMAL) flg3 |= (RF3_ANIMAL);
3608                                         if (f1 & TR1_SLAY_EVIL)   flg3 |= (RF3_EVIL);
3609
3610                                         /* The object cannot be picked up by the monster */
3611                                         if (artifact_p(o_ptr) || (r_ptr->flags3 & flg3) ||
3612                                                 (o_ptr->art_name))
3613                                         {
3614                                                 /* Only give a message for "take_item" */
3615                                                 if ((r_ptr->flags2 & (RF2_TAKE_ITEM)) && (r_ptr->flags2 & (RF2_STUPID)))
3616                                                 {
3617                                                         /* Take note */
3618                                                         did_take_item = TRUE;
3619
3620                                                         /* Describe observable situations */
3621                                                         if (m_ptr->ml && player_can_see_bold(ny, nx))
3622                                                         {
3623                                                                 /* Dump a message */
3624 #ifdef JP
3625 msg_format("%^s¤Ï%s¤ò½¦¤ª¤¦¤È¤·¤¿¤¬¡¢¤À¤á¤À¤Ã¤¿¡£", m_name, o_name);
3626 #else
3627 msg_format("%^s tries to pick up %s, but fails.", m_name, o_name);
3628 #endif
3629                                                         }
3630                                                 }
3631                                         }
3632
3633                                         /* Pick up the item */
3634                                         else if (r_ptr->flags2 & RF2_TAKE_ITEM)
3635                                         {
3636                                                 /* Take note */
3637                                                 did_take_item = TRUE;
3638
3639                                                 /* Describe observable situations */
3640                                                 if (player_can_see_bold(ny, nx))
3641                                                 {
3642                                                         /* Dump a message */
3643 #ifdef JP
3644 msg_format("%^s¤¬%s¤ò½¦¤Ã¤¿¡£", m_name, o_name);
3645 #else
3646                                                         msg_format("%^s picks up %s.", m_name, o_name);
3647 #endif
3648
3649                                                 }
3650
3651                                                 /* Excise the object */
3652                                                 excise_object_idx(this_o_idx);
3653
3654                                                 /* Forget mark */
3655                                                 o_ptr->marked = FALSE;
3656
3657                                                 /* Forget location */
3658                                                 o_ptr->iy = o_ptr->ix = 0;
3659
3660                                                 /* Memorize monster */
3661                                                 o_ptr->held_m_idx = m_idx;
3662
3663                                                 /* Build a stack */
3664                                                 o_ptr->next_o_idx = m_ptr->hold_o_idx;
3665
3666                                                 /* Carry object */
3667                                                 m_ptr->hold_o_idx = this_o_idx;
3668                                         }
3669
3670                                         /* Destroy the item if not a pet */
3671                                         else if (!is_pet(m_ptr))
3672                                         {
3673                                                 /* Take note */
3674                                                 did_kill_item = TRUE;
3675
3676                                                 /* Describe observable situations */
3677                                                 if (player_has_los_bold(ny, nx))
3678                                                 {
3679                                                         /* Dump a message */
3680 #ifdef JP
3681 msg_format("%^s¤¬%s¤òÇ˲õ¤·¤¿¡£", m_name, o_name);
3682 #else
3683                                                         msg_format("%^s destroys %s.", m_name, o_name);
3684 #endif
3685
3686                                                 }
3687
3688                                                 /* Delete the object */
3689                                                 delete_object_idx(this_o_idx);
3690                                         }
3691                                 }
3692                         }
3693                 }
3694
3695                 /* Stop when done */
3696                 if (do_turn) break;
3697         }
3698
3699         /*
3700          *  Forward movements failed, but now recieved LOS attack!
3701          *  Try to flow by smell.
3702          */
3703         if (p_ptr->no_flowed && i > 2 &&  m_ptr->target_y)
3704                 m_ptr->mflag2 &= ~MFLAG_NOFLOW;
3705
3706         /* If we haven't done anything, try casting a spell again */
3707         if (!do_turn && !do_move && !m_ptr->monfear && !stupid_monsters && !(p_ptr->riding == m_idx) && aware)
3708         {
3709                 /* Cast spell */
3710                 if (make_attack_spell(m_idx)) return;
3711         }
3712
3713
3714         /* Notice changes in view */
3715         if (do_view)
3716         {
3717                 /* Update some things */
3718                 p_ptr->update |= (PU_VIEW | PU_LITE | PU_FLOW | PU_MONSTERS | PU_MON_LITE);
3719         }
3720
3721         /* Notice changes in view */
3722         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)))
3723         {
3724                 /* Update some things */
3725                 p_ptr->update |= (PU_MON_LITE);
3726         }
3727
3728         /* Learn things from observable monster */
3729         if (m_ptr->ml)
3730         {
3731                 /* Monster opened a door */
3732                 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
3733
3734                 /* Monster bashed a door */
3735                 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
3736
3737                 /* Monster tried to pick something up */
3738                 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
3739
3740                 /* Monster tried to crush something */
3741                 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
3742
3743                 /* Monster pushed past another monster */
3744                 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
3745
3746                 /* Monster passed through a wall */
3747                 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
3748
3749                 /* Monster destroyed a wall */
3750                 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
3751         }
3752
3753
3754         /* Hack -- get "bold" if out of options */
3755         if (!do_turn && !do_move && m_ptr->monfear && aware)
3756         {
3757                 /* No longer afraid */
3758                 m_ptr->monfear = 0;
3759
3760                 /* Message if seen */
3761                 if (m_ptr->ml)
3762                 {
3763                         char m_name[80];
3764
3765                         /* Acquire the monster name */
3766                         monster_desc(m_name, m_ptr, 0);
3767
3768                         /* Dump a message */
3769 #ifdef JP
3770 msg_format("%^s¤ÏÀ襤¤ò·è°Õ¤·¤¿¡ª", m_name);
3771 #else
3772                         msg_format("%^s turns to fight!", m_name);
3773 #endif
3774
3775                         chg_virtue(V_COMPASSION, -1);
3776                 }
3777
3778                 /* XXX XXX XXX Actually do something now (?) */
3779         }
3780 }
3781
3782 /*
3783  * Process all the "live" monsters, once per game turn.
3784  *
3785  * During each game turn, we scan through the list of all the "live" monsters,
3786  * (backwards, so we can excise any "freshly dead" monsters), energizing each
3787  * monster, and allowing fully energized monsters to move, attack, pass, etc.
3788  *
3789  * Note that monsters can never move in the monster array (except when the
3790  * "compact_monsters()" function is called by "dungeon()" or "save_player()").
3791  *
3792  * This function is responsible for at least half of the processor time
3793  * on a normal system with a "normal" amount of monsters and a player doing
3794  * normal things.
3795  *
3796  * When the player is resting, virtually 90% of the processor time is spent
3797  * in this function, and its children, "process_monster()" and "make_move()".
3798  *
3799  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",
3800  * especially when the player is running.
3801  *
3802  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"
3803  * monsters while they are still being "born".  A monster is "fresh" only
3804  * during the turn in which it is created, and we use the "hack_m_idx" to
3805  * determine if the monster is yet to be processed during the current turn.
3806  *
3807  * Note the special "MFLAG_NICE" flag, which allows the player to get one
3808  * move before any "nasty" monsters get to use their spell attacks.
3809  *
3810  * Note that when the "knowledge" about the currently tracked monster
3811  * changes (flags, attacks, spells), we induce a redraw of the monster
3812  * recall window.
3813  */
3814 void process_monsters(void)
3815 {
3816         int             i, e;
3817         int             fx, fy;
3818
3819         bool            test;
3820
3821         monster_type    *m_ptr;
3822         monster_race    *r_ptr;
3823
3824         int             old_monster_race_idx;
3825
3826         u32b    old_r_flags1 = 0L;
3827         u32b    old_r_flags2 = 0L;
3828         u32b    old_r_flags3 = 0L;
3829         u32b    old_r_flags4 = 0L;
3830         u32b    old_r_flags5 = 0L;
3831         u32b    old_r_flags6 = 0L;
3832
3833         byte    old_r_blows0 = 0;
3834         byte    old_r_blows1 = 0;
3835         byte    old_r_blows2 = 0;
3836         byte    old_r_blows3 = 0;
3837
3838         byte    old_r_cast_inate = 0;
3839         byte    old_r_cast_spell = 0;
3840
3841         int speed;
3842
3843         /* Clear monster fighting indicator */
3844         mon_fight = FALSE;
3845
3846         /* Memorize old race */
3847         old_monster_race_idx = p_ptr->monster_race_idx;
3848
3849         /* Acquire knowledge */
3850         if (p_ptr->monster_race_idx)
3851         {
3852                 /* Acquire current monster */
3853                 r_ptr = &r_info[p_ptr->monster_race_idx];
3854
3855                 /* Memorize flags */
3856                 old_r_flags1 = r_ptr->r_flags1;
3857                 old_r_flags2 = r_ptr->r_flags2;
3858                 old_r_flags3 = r_ptr->r_flags3;
3859                 old_r_flags4 = r_ptr->r_flags4;
3860                 old_r_flags5 = r_ptr->r_flags5;
3861                 old_r_flags6 = r_ptr->r_flags6;
3862
3863                 /* Memorize blows */
3864                 old_r_blows0 = r_ptr->r_blows[0];
3865                 old_r_blows1 = r_ptr->r_blows[1];
3866                 old_r_blows2 = r_ptr->r_blows[2];
3867                 old_r_blows3 = r_ptr->r_blows[3];
3868
3869                 /* Memorize castings */
3870                 old_r_cast_inate = r_ptr->r_cast_inate;
3871                 old_r_cast_spell = r_ptr->r_cast_spell;
3872         }
3873
3874
3875         /* Hack -- calculate the "player noise" */
3876         noise = (1L << (30 - p_ptr->skill_stl));
3877
3878
3879         /* Process the monsters (backwards) */
3880         for (i = m_max - 1; i >= 1; i--)
3881         {
3882                 /* Access the monster */
3883                 m_ptr = &m_list[i];
3884                 r_ptr = &r_info[m_ptr->r_idx];
3885
3886                 /* Handle "leaving" */
3887                 if (p_ptr->leaving) break;
3888
3889                 /* Ignore "dead" monsters */
3890                 if (!m_ptr->r_idx) continue;
3891
3892                 if (p_ptr->wild_mode) continue;
3893
3894
3895                 /* Handle "fresh" monsters */
3896                 if (m_ptr->mflag & MFLAG_BORN)
3897                 {
3898                         /* No longer "fresh" */
3899                         m_ptr->mflag &= ~(MFLAG_BORN);
3900
3901                         /* Skip */
3902                         continue;
3903                 }
3904
3905                 /* Hack -- Require proximity */
3906                 if (m_ptr->cdis >= 100) continue;
3907
3908
3909                 /* Access the location */
3910                 fx = m_ptr->fx;
3911                 fy = m_ptr->fy;
3912
3913                 /* Flow by smell is allowed */
3914                 if (!stupid_monsters && !p_ptr->no_flowed)
3915                 {
3916                         m_ptr->mflag2 &= ~MFLAG_NOFLOW;
3917                 }
3918
3919                 /* Assume no move */
3920                 test = FALSE;
3921
3922                 /* Handle "sensing radius" */
3923                 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > 20 ? 20 : r_ptr->aaf) : r_ptr->aaf))
3924                 {
3925                         /* We can "sense" the player */
3926                         test = TRUE;
3927                 }
3928
3929                 /* Handle "sight" and "aggravation" */
3930                 else if ((m_ptr->cdis <= MAX_SIGHT) &&
3931                         (player_has_los_bold(fy, fx) || p_ptr->aggravate))
3932                 {
3933                         /* We can "see" or "feel" the player */
3934                         test = TRUE;
3935                 }
3936
3937                 /* Hack -- Monsters can "smell" the player from far away */
3938                 /* Note that most monsters have "aaf" of "20" or so */
3939                 else if (!stupid_monsters && !(m_ptr->mflag2 & MFLAG_NOFLOW) &&
3940                         (cave_floor_bold(py, px) || (cave[py][px].feat == FEAT_TREES)) &&
3941                         (cave[py][px].when == cave[fy][fx].when) &&
3942                         (cave[fy][fx].dist < MONSTER_FLOW_DEPTH) &&
3943                         (cave[fy][fx].dist < r_ptr->aaf))
3944                 {
3945                         /* We can "smell" the player */
3946                         test = TRUE;
3947                 }
3948                 else if (m_ptr->target_y) test = TRUE;
3949
3950                 /* Do nothing */
3951                 if (!test) continue;
3952
3953
3954                 if (p_ptr->riding == i)
3955                         speed = p_ptr->pspeed;
3956                 else
3957                 {
3958                         speed = MIN(199, m_ptr->mspeed);
3959
3960                         /* Monsters move quickly in Nightmare mode */
3961                         if (ironman_nightmare)
3962                         {
3963                                 speed = MIN(199, m_ptr->mspeed + 5);
3964                         }
3965
3966                         if (m_ptr->fast) speed = MIN(199, speed + 10);
3967                         if (m_ptr->slow) speed = MAX(0, speed - 10);
3968                 }
3969
3970                 e = extract_energy[speed];
3971
3972                 /* Give this monster some energy */
3973                 if(randint0(60) < e)
3974                 m_ptr->energy += gain_energy();
3975
3976
3977                 /* Not enough energy to move */
3978                 if (m_ptr->energy < 100) continue;
3979
3980                 /* Use up "some" energy */
3981                 m_ptr->energy -= 100;
3982
3983
3984                 /* Save global index */
3985                 hack_m_idx = i;
3986
3987                 /* Process the monster */
3988                 process_monster(i);
3989
3990                 reset_target(m_ptr);
3991
3992                 /* Give up flow_by_smell when it might useless */
3993                 if (p_ptr->no_flowed && one_in_(3))
3994                         m_ptr->mflag2 |= MFLAG_NOFLOW;
3995
3996                 /* Hack -- notice death or departure */
3997                 if (!alive || death) break;
3998
3999                 /* Notice leaving */
4000                 if (p_ptr->leaving) break;
4001         }
4002
4003         /* Reset global index */
4004         hack_m_idx = 0;
4005
4006
4007         /* Tracking a monster race (the same one we were before) */
4008         if (p_ptr->monster_race_idx && (p_ptr->monster_race_idx == old_monster_race_idx))
4009         {
4010                 /* Acquire monster race */
4011                 r_ptr = &r_info[p_ptr->monster_race_idx];
4012
4013                 /* Check for knowledge change */
4014                 if ((old_r_flags1 != r_ptr->r_flags1) ||
4015                         (old_r_flags2 != r_ptr->r_flags2) ||
4016                         (old_r_flags3 != r_ptr->r_flags3) ||
4017                         (old_r_flags4 != r_ptr->r_flags4) ||
4018                         (old_r_flags5 != r_ptr->r_flags5) ||
4019                         (old_r_flags6 != r_ptr->r_flags6) ||
4020                         (old_r_blows0 != r_ptr->r_blows[0]) ||
4021                         (old_r_blows1 != r_ptr->r_blows[1]) ||
4022                         (old_r_blows2 != r_ptr->r_blows[2]) ||
4023                         (old_r_blows3 != r_ptr->r_blows[3]) ||
4024                         (old_r_cast_inate != r_ptr->r_cast_inate) ||
4025                         (old_r_cast_spell != r_ptr->r_cast_spell))
4026                 {
4027                         /* Window stuff */
4028                         p_ptr->window |= (PW_MONSTER);
4029                 }
4030         }
4031 }
4032
4033
4034
4035 bool process_the_world(int num, int who, bool vs_player)
4036 {
4037         monster_type *m_ptr = &m_list[hack_m_idx];  /* the world monster */
4038
4039         if(world_monster) return (FALSE);
4040
4041         if(vs_player)
4042         {
4043                 char m_name[80];
4044                 monster_desc(m_name, m_ptr, 0);
4045
4046                 if (who == 1)
4047 #ifdef JP
4048                         msg_print("¡Ö¡Ø¥¶¡¦¥ï¡¼¥ë¥É¡Ù¡ª»þ¤Ï»ß¤Þ¤Ã¤¿¡ª¡×");
4049 #else
4050                         msg_format("%s yells 'The World! Time has stopped!'", m_name);
4051 #endif
4052                 else if (who == 3)
4053 #ifdef JP
4054                         msg_print("¡Ö»þ¤è¡ª¡×");
4055 #else
4056                         msg_format("%s yells 'Time!'", m_name);
4057 #endif
4058                 else msg_print("hek!");
4059
4060                 msg_print(NULL);
4061         }
4062
4063         world_monster = TRUE;
4064
4065         if (vs_player) do_cmd_redraw();
4066
4067         while(num--)
4068         {
4069                 if(m_ptr->r_idx) return (TRUE);
4070                 process_monster(hack_m_idx);
4071
4072                 reset_target(m_ptr);
4073
4074                 /* Notice stuff */
4075                 if (p_ptr->notice) notice_stuff();
4076
4077                 /* Update stuff */
4078                 if (p_ptr->update) update_stuff();
4079
4080                 /* Redraw stuff */
4081                 if (p_ptr->redraw) redraw_stuff();
4082
4083                 /* Redraw stuff */
4084                 if (p_ptr->window) window_stuff();
4085
4086                 /* Delay */
4087                 if (vs_player) Term_xtra(TERM_XTRA_DELAY, 500);
4088         }
4089
4090         /* Redraw map */
4091         p_ptr->redraw |= (PR_MAP);
4092
4093         /* Update monsters */
4094         p_ptr->update |= (PU_MONSTERS);
4095
4096         /* Window stuff */
4097         p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
4098
4099         world_monster = FALSE;
4100         if (vs_player || los(py, px, m_ptr->fy, m_ptr->fx))
4101         {
4102 #ifdef JP
4103                 msg_print("¡Ö»þ¤ÏÆ°¤­¤À¤¹¡Ä¡×");
4104 #else
4105                 msg_print("You feel time flowing around you once more.");
4106 #endif
4107                 msg_print(NULL);
4108         }
4109
4110         handle_stuff();
4111
4112         return (TRUE);
4113 }
4114
4115
4116 void monster_gain_exp(int m_idx, int s_idx)
4117 {
4118         monster_type *m_ptr = &m_list[m_idx];
4119         monster_race *r_ptr = &r_info[m_ptr->r_idx];
4120         monster_race *s_ptr = &r_info[s_idx];
4121         int new_exp;
4122
4123         if (p_ptr->inside_battle) return;
4124
4125         if (!r_ptr->next_exp) return;
4126
4127         new_exp = s_ptr->mexp * s_ptr->level / (r_ptr->level + 2);
4128         if (m_idx == p_ptr->riding) new_exp = (new_exp + 1) / 2;
4129         if (!dun_level) new_exp /= 5;
4130         m_ptr->exp += new_exp;
4131         if (m_ptr->mflag2 & MFLAG_CHAMELEON) return;
4132
4133         if (m_ptr->exp >= r_ptr->next_exp)
4134         {
4135                 char m_name[80];
4136                 int old_hp = m_ptr->hp;
4137                 int old_maxhp = m_ptr->max_maxhp;
4138                 int old_r_idx = m_ptr->r_idx;
4139                 int i;
4140
4141                 monster_desc(m_name, m_ptr, 0);
4142                 m_ptr->r_idx = r_ptr->next_r_idx;
4143                 r_ptr = &r_info[m_ptr->r_idx];
4144                 if (r_ptr->flags1 & RF1_FORCE_MAXHP)
4145                 {
4146                         m_ptr->max_maxhp = maxroll(r_ptr->hdice, r_ptr->hside);
4147                 }
4148                 else
4149                 {
4150                         m_ptr->max_maxhp = damroll(r_ptr->hdice, r_ptr->hside);
4151                 }
4152                 if (ironman_nightmare)
4153                 {
4154                         u32b hp = m_ptr->max_maxhp * 2L;
4155
4156                         m_ptr->max_maxhp = (s16b)MIN(30000, hp);
4157                 }
4158                 m_ptr->maxhp = m_ptr->max_maxhp;
4159                 m_ptr->hp = old_hp * m_ptr->maxhp / old_maxhp;
4160
4161                 /* Extract the monster base speed */
4162                 m_ptr->mspeed = r_ptr->speed;
4163
4164                 /* Hack -- small racial variety */
4165                 if (!(r_ptr->flags1 & RF1_UNIQUE) && !p_ptr->inside_arena)
4166                 {
4167                         /* Allow some small variation per monster */
4168                   if(one_in_(4)){
4169                         i = extract_energy[r_ptr->speed] / 3;
4170                         if (i) m_ptr->mspeed += rand_spread(0, i);
4171                   }
4172                   else{
4173                         i = extract_energy[r_ptr->speed] / 10;
4174                         if (i) m_ptr->mspeed += rand_spread(0, i);
4175                   }
4176                 }
4177
4178                 if (m_ptr->mspeed > 199) m_ptr->mspeed = 199;
4179
4180                 m_ptr->exp = 0;
4181
4182                 if (is_pet(m_ptr) || m_ptr->ml)
4183                 {
4184 #ifdef JP
4185                         msg_format("%s¤Ï%s¤Ë¿Ê²½¤·¤¿¡£", m_name, r_name + r_ptr->name);
4186 #else
4187                         msg_format("%^s evolved into %s.", m_name, r_name + r_ptr->name);
4188 #endif
4189                         r_info[old_r_idx].r_xtra1 |= MR1_SINKA;
4190                 }
4191                 update_mon(m_idx, FALSE);
4192                 lite_spot(m_ptr->fy, m_ptr->fx);
4193         }
4194         if (m_idx == p_ptr->riding) p_ptr->update |= PU_BONUS;
4195 }