OSDN Git Service

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