OSDN Git Service

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