OSDN Git Service

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