OSDN Git Service

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