OSDN Git Service

[fix] #37353 コメント修正.
[hengband/hengband.git] / src / monster-process.c
1 /*!
2  * @file monster-process.c
3  * @brief モンスターの特殊技能と移動処理/ Monster spells and movement
4  * @date 2014/01/17
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  * This software may be copied and distributed for educational, research,\n
8  * and not for profit purposes provided that this copyright and statement\n
9  * are included in all such copies.  Other copyrights may also apply.\n
10  * 2014 Deskull rearranged comment for Doxygen.\n
11  * @details
12  * This file has several additions to it by Keldon Jones (keldon@umr.edu)
13  * to improve the general quality of the AI (version 0.1.1).
14  */
15
16 #include "angband.h"
17 #include "util.h"
18
19 #include "cmd-dump.h"
20 #include "cmd-pet.h"
21 #include "creature.h"
22 #include "melee.h"
23 #include "spells.h"
24 #include "spells-summon.h"
25 #include "quest.h"
26 #include "avatar.h"
27 #include "realm-hex.h"
28 #include "object-flavor.h"
29 #include "object-hook.h"
30 #include "feature.h"
31 #include "grid.h"
32 #include "player-move.h"
33 #include "monster-status.h"
34 #include "monster-spell.h"
35 #include "monster-process.h"
36 #include "monsterrace-hook.h"
37 #include "dungeon.h"
38 #include "floor.h"
39 #include "files.h"
40
41
42 /*!
43  * @brief モンスターが敵に接近するための方向を決める /
44  * Calculate the direction to the next enemy
45  * @param m_idx モンスターの参照ID
46  * @param mm 移動するべき方角IDを返す参照ポインタ
47  * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
48  */
49 static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
50 {
51         int i;
52         POSITION x = 0, y = 0;
53         MONSTER_IDX t_idx;
54         int start;
55         int plus = 1;
56
57         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
58         monster_race *r_ptr = &r_info[m_ptr->r_idx];
59         monster_type *t_ptr;
60
61         if (riding_t_m_idx && player_bold(m_ptr->fy, m_ptr->fx))
62         {
63                 y = current_floor_ptr->m_list[riding_t_m_idx].fy;
64                 x = current_floor_ptr->m_list[riding_t_m_idx].fx;
65         }
66         else if (is_pet(m_ptr) && pet_t_m_idx)
67         {
68                 y = current_floor_ptr->m_list[pet_t_m_idx].fy;
69                 x = current_floor_ptr->m_list[pet_t_m_idx].fx;
70         }
71         else
72         {
73                 if (p_ptr->inside_battle)
74                 {
75                         start = randint1(current_floor_ptr->m_max-1)+current_floor_ptr->m_max;
76                         if(randint0(2)) plus = -1;
77                 }
78                 else start = current_floor_ptr->m_max + 1;
79
80                 /* Scan thru all monsters */
81                 for (i = start; ((i < start + current_floor_ptr->m_max) && (i > start - current_floor_ptr->m_max)); i+=plus)
82                 {
83                         MONSTER_IDX dummy = (i % current_floor_ptr->m_max);
84
85                         if (!dummy) continue;
86
87                         t_idx = dummy;
88                         t_ptr = &current_floor_ptr->m_list[t_idx];
89
90                         /* The monster itself isn't a target */
91                         if (t_ptr == m_ptr) continue;
92
93                         if (!monster_is_valid(t_ptr)) continue;
94
95                         if (is_pet(m_ptr))
96                         {
97                                 /* Hack -- only fight away from player */
98                                 if (p_ptr->pet_follow_distance < 0)
99                                 {
100                                         /* No fighting near player */
101                                         if (t_ptr->cdis <= (0 - p_ptr->pet_follow_distance))
102                                         {
103                                                 continue;
104                                         }
105                                 }
106                                 /* Hack -- no fighting away from player */
107                                 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > p_ptr->pet_follow_distance))
108                                 {
109                                         continue;
110                                 }
111
112                                 if (r_ptr->aaf < t_ptr->cdis) continue;
113                         }
114
115                         /* Monster must be 'an enemy' */
116                         if (!are_enemies(m_ptr, t_ptr)) continue;
117
118                         /* Monster must be projectable if we can't pass through walls */
119                         if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) ||
120                             ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)))
121                         {
122                                 if (!in_disintegration_range(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
123                         }
124                         else
125                         {
126                                 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
127                         }
128
129                         /* OK -- we've got a target */
130                         y = t_ptr->fy;
131                         x = t_ptr->fx;
132
133                         break;
134                 }
135                 if (!x && !y) return FALSE;
136         }
137
138         /* Extract the direction */
139         x -= m_ptr->fx;
140         y -= m_ptr->fy;
141
142         /* North */
143         if ((y < 0) && (x == 0))
144         {
145                 mm[0] = 8;
146                 mm[1] = 7;
147                 mm[2] = 9;
148         }
149         /* South */
150         else if ((y > 0) && (x == 0))
151         {
152                 mm[0] = 2;
153                 mm[1] = 1;
154                 mm[2] = 3;
155         }
156         /* East */
157         else if ((x > 0) && (y == 0))
158         {
159                 mm[0] = 6;
160                 mm[1] = 9;
161                 mm[2] = 3;
162         }
163         /* West */
164         else if ((x < 0) && (y == 0))
165         {
166                 mm[0] = 4;
167                 mm[1] = 7;
168                 mm[2] = 1;
169         }
170         /* North-West */
171         else if ((y < 0) && (x < 0))
172         {
173                 mm[0] = 7;
174                 mm[1] = 4;
175                 mm[2] = 8;
176         }
177         /* North-East */
178         else if ((y < 0) && (x > 0))
179         {
180                 mm[0] = 9;
181                 mm[1] = 6;
182                 mm[2] = 8;
183         }
184         /* South-West */
185         else if ((y > 0) && (x < 0))
186         {
187                 mm[0] = 1;
188                 mm[1] = 4;
189                 mm[2] = 2;
190         }
191         /* South-East */
192         else if ((y > 0) && (x > 0))
193         {
194                 mm[0] = 3;
195                 mm[1] = 6;
196                 mm[2] = 2;
197         }
198
199         /* Found a monster */
200         return TRUE;
201 }
202
203 /*!
204  * @brief モンスターがプレイヤーから逃走するかどうかを返す /
205  * Returns whether a given monster will try to run from the player.
206  * @param m_idx 逃走するモンスターの参照ID
207  * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
208  * @details
209  * Monsters will attempt to avoid very powerful players.  See below.\n
210  *\n
211  * Because this function is called so often, little details are important\n
212  * for efficiency.  Like not using "mod" or "div" when possible.  And\n
213  * attempting to check the conditions in an optimal order.  Note that\n
214  * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
215  *\n
216  * Note that this function is responsible for about one to five percent\n
217  * of the processor use in normal conditions...\n
218  */
219 static bool mon_will_run(MONSTER_IDX m_idx)
220 {
221         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
222
223 #ifdef ALLOW_TERROR
224
225         monster_race *r_ptr = &r_info[m_ptr->r_idx];
226
227         PLAYER_LEVEL p_lev;
228         DEPTH m_lev;
229         HIT_POINT p_chp, p_mhp;
230         HIT_POINT m_chp, m_mhp;
231         u32b p_val, m_val;
232
233 #endif
234
235         /* Friends can be commanded to avoid the player */
236         if (is_pet(m_ptr))
237         {
238                 /* Are we trying to avoid the player? */
239                 return ((p_ptr->pet_follow_distance < 0) &&
240                                   (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
241         }
242
243         /* Keep monsters from running too far away */
244         if (m_ptr->cdis > MAX_SIGHT + 5) return (FALSE);
245
246         /* All "afraid" monsters will run away */
247         if (MON_MONFEAR(m_ptr)) return (TRUE);
248
249 #ifdef ALLOW_TERROR
250
251         /* Nearby monsters will not become terrified */
252         if (m_ptr->cdis <= 5) return (FALSE);
253
254         /* Examine player power (level) */
255         p_lev = p_ptr->lev;
256
257         /* Examine monster power (level plus morale) */
258         m_lev = r_ptr->level + (m_idx & 0x08) + 25;
259
260         /* Optimize extreme cases below */
261         if (m_lev > p_lev + 4) return (FALSE);
262         if (m_lev + 4 <= p_lev) return (TRUE);
263
264         /* Examine player health */
265         p_chp = p_ptr->chp;
266         p_mhp = p_ptr->mhp;
267
268         /* Examine monster health */
269         m_chp = m_ptr->hp;
270         m_mhp = m_ptr->maxhp;
271
272         /* Prepare to optimize the calculation */
273         p_val = (p_lev * p_mhp) + (p_chp << 2); /* div p_mhp */
274         m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
275
276         /* Strong players scare strong monsters */
277         if (p_val * m_mhp > m_val * p_mhp) return (TRUE);
278
279 #endif
280
281         /* Assume no terror */
282         return (FALSE);
283 }
284
285
286 /*!
287  * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
288  * Search spell castable grid
289  * @param m_idx モンスターの参照ID
290  * @param yp 適したマスのY座標を返す参照ポインタ
291  * @param xp 適したマスのX座標を返す参照ポインタ
292  * @return 有効なマスがあった場合TRUEを返す
293  */
294 static bool get_moves_aux2(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
295 {
296         int i, best = 999;
297         POSITION y, x, y1, x1;
298
299         grid_type *g_ptr;
300         bool can_open_door = FALSE;
301         int now_cost;
302
303         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
304         monster_race *r_ptr = &r_info[m_ptr->r_idx];
305
306         /* Monster location */
307         y1 = m_ptr->fy;
308         x1 = m_ptr->fx;
309
310         /* Monster can already cast spell to player */
311         if (projectable(y1, x1, p_ptr->y, p_ptr->x)) return (FALSE);
312
313         /* Set current grid cost */
314         now_cost = current_floor_ptr->grid_array[y1][x1].cost;
315         if (now_cost == 0) now_cost = 999;
316
317         /* Can monster bash or open doors? */
318         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
319         {
320                 can_open_door = TRUE;
321         }
322
323         /* Check nearby grids, diagonals first */
324         for (i = 7; i >= 0; i--)
325         {
326                 int cost;
327
328                 y = y1 + ddy_ddd[i];
329                 x = x1 + ddx_ddd[i];
330
331                 /* Ignore locations off of edge */
332                 if (!in_bounds2(y, x)) continue;
333
334                 /* Simply move to player */
335                 if (player_bold(y, x)) return (FALSE);
336
337                 g_ptr = &current_floor_ptr->grid_array[y][x];
338
339                 cost = g_ptr->cost;
340
341                 /* Monster cannot kill or pass walls */
342                 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))))
343                 {
344                         if (cost == 0) continue;
345                         if (!can_open_door && is_closed_door(g_ptr->feat)) continue;
346                 }
347
348                 /* Hack -- for kill or pass wall monster.. */
349                 if (cost == 0) cost = 998;
350
351                 if (now_cost < cost) continue;
352
353                 if (!projectable(y, x, p_ptr->y, p_ptr->x)) continue;
354
355                 /* Accept louder sounds */
356                 if (best < cost) continue;
357                 best = cost;
358
359                 (*yp) = y1 + ddy_ddd[i];
360                 (*xp) = x1 + ddx_ddd[i];
361         }
362
363         /* No legal move (?) */
364         if (best == 999) return (FALSE);
365
366         /* Success */
367         return (TRUE);
368 }
369
370
371 /*!
372  * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
373  * Choose the "best" direction for "flowing"
374  * @param m_idx モンスターの参照ID
375  * @param yp 移動先のマスのY座標を返す参照ポインタ
376  * @param xp 移動先のマスのX座標を返す参照ポインタ
377  * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
378  * @return 有効なマスがあった場合TRUEを返す
379  * @details
380  * Note that ghosts and rock-eaters are never allowed to "flow",\n
381  * since they should move directly towards the player.\n
382  *\n
383  * Prefer "non-diagonal" directions, but twiddle them a little\n
384  * to angle slightly towards the player's actual location.\n
385  *\n
386  * Allow very perceptive monsters to track old "spoor" left by\n
387  * previous locations occupied by the player.  This will tend\n
388  * to have monsters end up either near the player or on a grid\n
389  * recently occupied by the player (and left via "teleport").\n
390  *\n
391  * Note that if "smell" is turned on, all monsters get vicious.\n
392  *\n
393  * Also note that teleporting away from a location will cause\n
394  * the monsters who were chasing you to converge on that location\n
395  * as long as you are still near enough to "annoy" them without\n
396  * being close enough to chase directly.  I have no idea what will\n
397  * happen if you combine "smell" with low "aaf" values.\n
398  */
399 static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
400 {
401         int i, best;
402         POSITION y, x, y1, x1;
403
404         grid_type *g_ptr;
405         bool use_scent = FALSE;
406
407         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
408         monster_race *r_ptr = &r_info[m_ptr->r_idx];
409
410         /* Can monster cast attack spell? */
411         if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
412             r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
413             r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
414         {
415                 /* Can move spell castable grid? */
416                 if (get_moves_aux2(m_idx, yp, xp)) return (TRUE);
417         }
418
419         /* Monster can't flow */
420         if (no_flow) return (FALSE);
421
422         /* Monster can go through rocks */
423         if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) return (FALSE);
424         if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)) return (FALSE);
425
426         /* Monster location */
427         y1 = m_ptr->fy;
428         x1 = m_ptr->fx;
429
430         /* Hack -- Player can see us, run towards him */
431         if (player_has_los_bold(y1, x1) && projectable(p_ptr->y, p_ptr->x, y1, x1)) return (FALSE);
432
433         /* Monster grid */
434         g_ptr = &current_floor_ptr->grid_array[y1][x1];
435
436         /* If we can hear noises, advance towards them */
437         if (g_ptr->cost)
438         {
439                 best = 999;
440         }
441
442         /* Otherwise, try to follow a scent trail */
443         else if (g_ptr->when)
444         {
445                 /* Too old smell */
446                 if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when - g_ptr->when > 127) return (FALSE);
447
448                 use_scent = TRUE;
449                 best = 0;
450         }
451
452         /* Otherwise, advance blindly */
453         else
454         {
455                 return (FALSE);
456         }
457
458         /* Check nearby grids, diagonals first */
459         for (i = 7; i >= 0; i--)
460         {
461                 y = y1 + ddy_ddd[i];
462                 x = x1 + ddx_ddd[i];
463
464                 /* Ignore locations off of edge */
465                 if (!in_bounds2(y, x)) continue;
466
467                 g_ptr = &current_floor_ptr->grid_array[y][x];
468
469                 /* We're following a scent trail */
470                 if (use_scent)
471                 {
472                         int when = g_ptr->when;
473
474                         /* Accept younger scent */
475                         if (best > when) continue;
476                         best = when;
477                 }
478
479                 /* We're using sound */
480                 else
481                 {
482                         int cost;
483
484                         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
485                                 cost = g_ptr->dist;
486                         else cost = g_ptr->cost;
487
488                         /* Accept louder sounds */
489                         if ((cost == 0) || (best < cost)) continue;
490                         best = cost;
491                 }
492
493                 /* Hack -- Save the "twiddled" location */
494                 (*yp) = p_ptr->y + 16 * ddy_ddd[i];
495                 (*xp) = p_ptr->x + 16 * ddx_ddd[i];
496         }
497
498         /* No legal move (?) */
499         if (best == 999 || best == 0) return (FALSE);
500
501         /* Success */
502         return (TRUE);
503 }
504
505
506 /*!
507  * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
508  * Provide a location to flee to, but give the player a wide berth.
509  * @param m_idx モンスターの参照ID
510  * @param yp 移動先のマスのY座標を返す参照ポインタ
511  * @param xp 移動先のマスのX座標を返す参照ポインタ
512  * @return 有効なマスがあった場合TRUEを返す
513  * @details
514  * A monster may wish to flee to a location that is behind the player,\n
515  * but instead of heading directly for it, the monster should "swerve"\n
516  * around the player so that he has a smaller chance of getting hit.\n
517  */
518 static bool get_fear_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
519 {
520         POSITION y, x, y1, x1, fy, fx, gy = 0, gx = 0;
521         int score = -1;
522         int i;
523
524         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
525
526         /* Monster location */
527         fy = m_ptr->fy;
528         fx = m_ptr->fx;
529
530         /* Desired destination */
531         y1 = fy - (*yp);
532         x1 = fx - (*xp);
533
534         /* Check nearby grids, diagonals first */
535         for (i = 7; i >= 0; i--)
536         {
537                 POSITION dis, s;
538
539                 y = fy + ddy_ddd[i];
540                 x = fx + ddx_ddd[i];
541
542                 /* Ignore locations off of edge */
543                 if (!in_bounds2(y, x)) continue;
544
545                 /* Don't move toward player */
546                 /* if (current_floor_ptr->grid_array[y][x].dist < 3) continue; */ /* Hmm.. Need it? */
547
548                 /* Calculate distance of this grid from our destination */
549                 dis = distance(y, x, y1, x1);
550
551                 /* Score this grid */
552                 s = 5000 / (dis + 3) - 500 / (current_floor_ptr->grid_array[y][x].dist + 1);
553
554                 /* No negative scores */
555                 if (s < 0) s = 0;
556
557                 /* Ignore lower scores */
558                 if (s < score) continue;
559
560                 /* Save the score and time */
561                 score = s;
562
563                 /* Save the location */
564                 gy = y;
565                 gx = x;
566         }
567
568         /* No legal move (?) */
569         if (score == -1) return (FALSE);
570
571         /* Find deltas */
572         (*yp) = fy - gy;
573         (*xp) = fx - gx;
574
575         /* Success */
576         return (TRUE);
577 }
578
579 /*
580  * Hack -- Precompute a bunch of calls to distance() in find_safety() and
581  * find_hiding().
582  *
583  * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
584  * offsets of all the locations with a distance of n from a central point,
585  * with an offset of (0,0) indicating no more offsets at this distance.
586  *
587  * This is, of course, fairly unreadable, but it eliminates multiple loops
588  * from the previous version.
589  *
590  * It is probably better to replace these arrays with code to compute
591  * the relevant arrays, even if the storage is pre-allocated in hard
592  * coded sizes.  At the very least, code should be included which is
593  * able to generate and dump these arrays (ala "los()").  
594  *
595  * Also, the storage needs could be halved by using bytes.  
596  *
597  * These arrays could be combined into two big arrays, using sub-arrays
598  * to hold the offsets and lengths of each portion of the sub-arrays, and
599  * this could perhaps also be used somehow in the "look" code.  
600  */
601
602
603 static POSITION d_off_y_0[] = { 0 };
604 static POSITION d_off_x_0[] = { 0 };
605
606 static POSITION d_off_y_1[] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
607 static POSITION d_off_x_1[] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
608
609 static POSITION d_off_y_2[] = { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
610 static POSITION d_off_x_2[] = { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
611
612 static POSITION d_off_y_3[] = { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2, 3, 3, 3, 0 };
613 static POSITION d_off_x_3[] = { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2, -1, 0, 1, 0 };
614
615 static POSITION d_off_y_4[] = { -1, -1, -2, -2, -3, -3, -3, -3, -4, -4, -4, 0, 0, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 0 };
616 static POSITION d_off_x_4[] = { -4, 4, -3, 3, -2, -3, 2, 3, -1, 0, 1, -4, 4, -4, 4, -3, 3, -2, -3, 2, 3, -1, 0, 1, 0 };
617
618
619 static POSITION d_off_y_5[] =
620 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
621   -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
622   5, 0 };
623
624 static POSITION d_off_x_5[] =
625 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
626   -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
627   0, 1, 0 };
628
629
630 static POSITION d_off_y_6[] =
631 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
632   -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
633   5, 5, 6, 6, 6, 0 };
634
635 static POSITION d_off_x_6[] =
636 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
637   0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
638   -3, 2, 3, -1, 0, 1, 0 };
639
640
641 static POSITION d_off_y_7[] =
642 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
643   -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
644   3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
645
646 static POSITION d_off_x_7[] =
647 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
648   -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
649   6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
650   1, 0 };
651
652
653 static POSITION d_off_y_8[] =
654 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
655   -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
656   2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
657   8, 8, 8, 0 };
658
659 static POSITION d_off_x_8[] =
660 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
661   5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
662   7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
663   2, 3, -1, 0, 1, 0 };
664
665
666 static POSITION d_off_y_9[] =
667 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
668   -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
669   0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
670   7, 8, 8, 8, 8, 9, 9, 9, 0 };
671
672 static POSITION d_off_x_9[] =
673 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
674   -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
675   9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
676   4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
677
678
679 static POSITION *dist_offsets_y[10] =
680 {
681         d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
682         d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
683 };
684
685 static POSITION *dist_offsets_x[10] =
686 {
687         d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
688         d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
689 };
690
691 /*!
692  * @brief モンスターが逃げ込める安全な地点を返す /
693  * Choose a "safe" location near a monster for it to run toward.
694  * @param m_idx モンスターの参照ID
695  * @param yp 移動先のマスのY座標を返す参照ポインタ
696  * @param xp 移動先のマスのX座標を返す参照ポインタ
697  * @return 有効なマスがあった場合TRUEを返す
698  * @details
699  * A location is "safe" if it can be reached quickly and the player\n
700  * is not able to fire into it (it isn't a "clean shot").  So, this will\n
701  * cause monsters to "duck" behind walls.  Hopefully, monsters will also\n
702  * try to run towards corridor openings if they are in a room.\n
703  *\n
704  * This function may take lots of CPU time if lots of monsters are\n
705  * fleeing.\n
706  *\n
707  * Return TRUE if a safe location is available.\n
708  */
709 static bool find_safety(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
710 {
711         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
712
713         POSITION fy = m_ptr->fy;
714         POSITION fx = m_ptr->fx;
715
716         POSITION y, x, dy, dx, d, dis, i;
717         POSITION gy = 0, gx = 0, gdis = 0;
718
719         POSITION *y_offsets;
720         POSITION *x_offsets;
721
722         grid_type *g_ptr;
723
724         /* Start with adjacent locations, spread further */
725         for (d = 1; d < 10; d++)
726         {
727                 /* Get the lists of points with a distance d from (fx, fy) */
728                 y_offsets = dist_offsets_y[d];
729                 x_offsets = dist_offsets_x[d];
730
731                 /* Check the locations */
732                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
733                      dx != 0 || dy != 0;
734                      i++, dx = x_offsets[i], dy = y_offsets[i])
735                 {
736                         y = fy + dy;
737                         x = fx + dx;
738
739                         /* Skip illegal locations */
740                         if (!in_bounds(y, x)) continue;
741
742                         g_ptr = &current_floor_ptr->grid_array[y][x];
743
744                         /* Skip locations in a wall */
745                         if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == p_ptr->riding) ? CEM_RIDING : 0)) continue;
746
747                         /* Check for "availability" (if monsters can flow) */
748                         if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
749                         {
750                                 /* Ignore grids very far from the player */
751                                 if (g_ptr->dist == 0) continue;
752
753                                 /* Ignore too-distant grids */
754                                 if (g_ptr->dist > current_floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
755                         }
756
757                         /* Check for absence of shot (more or less) */
758                         if (!projectable(p_ptr->y, p_ptr->x, y, x))
759                         {
760                                 /* Calculate distance from player */
761                                 dis = distance(y, x, p_ptr->y, p_ptr->x);
762
763                                 /* Remember if further than previous */
764                                 if (dis > gdis)
765                                 {
766                                         gy = y;
767                                         gx = x;
768                                         gdis = dis;
769                                 }
770                         }
771                 }
772
773                 /* Check for success */
774                 if (gdis > 0)
775                 {
776                         /* Good location */
777                         (*yp) = fy - gy;
778                         (*xp) = fx - gx;
779
780                         /* Found safe place */
781                         return (TRUE);
782                 }
783         }
784
785         /* No safe place */
786         return (FALSE);
787 }
788
789
790 /*!
791  * @brief モンスターが隠れ潜める地点を返す /
792  * Choose a good hiding place near a monster for it to run toward.
793  * @param m_idx モンスターの参照ID
794  * @param yp 移動先のマスのY座標を返す参照ポインタ
795  * @param xp 移動先のマスのX座標を返す参照ポインタ
796  * @return 有効なマスがあった場合TRUEを返す
797  * @details
798  * Pack monsters will use this to "ambush" the player and lure him out\n
799  * of corridors into open space so they can swarm him.\n
800  *\n
801  * Return TRUE if a good location is available.\n
802  */
803 static bool find_hiding(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
804 {
805         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
806         monster_race *r_ptr = &r_info[m_ptr->r_idx];
807
808         POSITION fy = m_ptr->fy;
809         POSITION fx = m_ptr->fx;
810
811         POSITION y, x, dy, dx, d, dis, i;
812         POSITION gy = 0, gx = 0, gdis = 999;
813
814         POSITION *y_offsets, *x_offsets;
815
816         /* Start with adjacent locations, spread further */
817         for (d = 1; d < 10; d++)
818         {
819                 /* Get the lists of points with a distance d from (fx, fy) */
820                 y_offsets = dist_offsets_y[d];
821                 x_offsets = dist_offsets_x[d];
822
823                 /* Check the locations */
824                 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
825                      dx != 0 || dy != 0;
826                      i++, dx = x_offsets[i], dy = y_offsets[i])
827                 {
828                         y = fy + dy;
829                         x = fx + dx;
830
831                         /* Skip illegal locations */
832                         if (!in_bounds(y, x)) continue;
833
834                         /* Skip occupied locations */
835                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
836
837                         /* Check for hidden, available grid */
838                         if (!projectable(p_ptr->y, p_ptr->x, y, x) && clean_shot(fy, fx, y, x, FALSE))
839                         {
840                                 /* Calculate distance from player */
841                                 dis = distance(y, x, p_ptr->y, p_ptr->x);
842
843                                 /* Remember if closer than previous */
844                                 if (dis < gdis && dis >= 2)
845                                 {
846                                         gy = y;
847                                         gx = x;
848                                         gdis = dis;
849                                 }
850                         }
851                 }
852
853                 /* Check for success */
854                 if (gdis < 999)
855                 {
856                         /* Good location */
857                         (*yp) = fy - gy;
858                         (*xp) = fx - gx;
859
860                         /* Found good place */
861                         return (TRUE);
862                 }
863         }
864
865         /* No good place */
866         return (FALSE);
867 }
868
869
870 /*!
871  * @brief モンスターの移動方向を返す /
872  * Choose "logical" directions for monster movement
873  * @param m_idx モンスターの参照ID
874  * @param mm 移動方向を返す方向IDの参照ポインタ
875  * @return 有効方向があった場合TRUEを返す
876  */
877 static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
878 {
879         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
880         monster_race *r_ptr = &r_info[m_ptr->r_idx];
881         POSITION     y = 0, ay, x = 0, ax;
882         int          move_val = 0;
883         POSITION     y2 = p_ptr->y;
884         POSITION     x2 = p_ptr->x;
885         bool         done = FALSE;
886         bool         will_run = mon_will_run(m_idx);
887         grid_type    *g_ptr;
888         bool         no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
889         bool         can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall));
890
891         /* Counter attack to an enemy monster */
892         if (!will_run && m_ptr->target_y)
893         {
894                 int t_m_idx = current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
895
896                 /* The monster must be an enemy, and in LOS */
897                 if (t_m_idx &&
898                     are_enemies(m_ptr, &current_floor_ptr->m_list[t_m_idx]) &&
899                     los(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
900                     projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
901                 {
902                         /* Extract the "pseudo-direction" */
903                         y = m_ptr->fy - m_ptr->target_y;
904                         x = m_ptr->fx - m_ptr->target_x;
905                         done = TRUE;
906                 }
907         }
908
909         if (!done && !will_run && is_hostile(m_ptr) &&
910             (r_ptr->flags1 & RF1_FRIENDS) &&
911             ((los(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x) && projectable(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x)) ||
912             (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
913         {
914         /*
915          * Animal packs try to get the player out of corridors
916          * (...unless they can move through walls -- TY)
917          */
918                 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
919                          !(r_ptr->flags2 & RF2_KILL_WALL))
920                 {
921                         int i, room = 0;
922
923                         /* Count room grids next to player */
924                         for (i = 0; i < 8; i++)
925                         {
926                                 int xx = p_ptr->x + ddx_ddd[i];
927                                 int yy = p_ptr->y + ddy_ddd[i];
928
929                                 if (!in_bounds2(yy, xx)) continue;
930
931                                 g_ptr = &current_floor_ptr->grid_array[yy][xx];
932
933                                 /* Check grid */
934                                 if (monster_can_cross_terrain(g_ptr->feat, r_ptr, 0))
935                                 {
936                                         /* One more room grid */
937                                         room++;
938                                 }
939                         }
940                         if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_ROOM) room -= 2;
941                         if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
942
943                         /* Not in a room and strong player */
944                         if (room < (8 * (p_ptr->chp + p_ptr->csp)) /
945                             (p_ptr->mhp + p_ptr->msp))
946                         {
947                                 /* Find hiding place */
948                                 if (find_hiding(m_idx, &y, &x)) done = TRUE;
949                         }
950                 }
951
952                 /* Monster groups try to surround the player */
953                 if (!done && (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
954                 {
955                         int i;
956
957                         /* Find an empty square near the player to fill */
958                         for (i = 0; i < 8; i++)
959                         {
960                                 /* Pick squares near player (semi-randomly) */
961                                 y2 = p_ptr->y + ddy_ddd[(m_idx + i) & 7];
962                                 x2 = p_ptr->x + ddx_ddd[(m_idx + i) & 7];
963
964                                 /* Already there? */
965                                 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
966                                 {
967                                         /* Attack the player */
968                                         y2 = p_ptr->y;
969                                         x2 = p_ptr->x;
970
971                                         break;
972                                 }
973
974                                 if (!in_bounds2(y2, x2)) continue;
975
976                                 /* Ignore filled grids */
977                                 if (!monster_can_enter(y2, x2, r_ptr, 0)) continue;
978
979                                 /* Try to fill this hole */
980                                 break;
981                         }
982
983                         /* Extract the new "pseudo-direction" */
984                         y = m_ptr->fy - y2;
985                         x = m_ptr->fx - x2;
986
987                         done = TRUE;
988                 }
989         }
990
991         if (!done)
992         {
993                 /* Flow towards the player */
994                 (void)get_moves_aux(m_idx, &y2, &x2, no_flow);
995
996                 /* Extract the "pseudo-direction" */
997                 y = m_ptr->fy - y2;
998                 x = m_ptr->fx - x2;
999
1000                 /* Not done */
1001         }
1002
1003         /* Apply fear if possible and necessary */
1004         if (is_pet(m_ptr) && will_run)
1005         {
1006                 /* XXX XXX Not very "smart" */
1007                 y = (-y), x = (-x);
1008         }
1009         else
1010         {
1011                 if (!done && will_run)
1012                 {
1013                         int tmp_x = (-x);
1014                         int tmp_y = (-y);
1015
1016                         /* Try to find safe place */
1017                         if (find_safety(m_idx, &y, &x))
1018                         {
1019                                 /* Attempt to avoid the player */
1020                                 if (!no_flow)
1021                                 {
1022                                         /* Adjust movement */
1023                                         if (get_fear_moves_aux(m_idx, &y, &x)) done = TRUE;
1024                                 }
1025                         }
1026
1027                         if (!done)
1028                         {
1029                                 /* This is not a very "smart" method XXX XXX */
1030                                 y = tmp_y;
1031                                 x = tmp_x;
1032                         }
1033                 }
1034         }
1035
1036
1037         /* Check for no move */
1038         if (!x && !y) return (FALSE);
1039
1040
1041         /* Extract the "absolute distances" */
1042         ax = ABS(x);
1043         ay = ABS(y);
1044
1045         /* Do something weird */
1046         if (y < 0) move_val += 8;
1047         if (x > 0) move_val += 4;
1048
1049         /* Prevent the diamond maneuvre */
1050         if (ay > (ax << 1)) move_val += 2;
1051         else if (ax > (ay << 1)) move_val++;
1052
1053         /* Extract some directions */
1054         switch (move_val)
1055         {
1056         case 0:
1057                 mm[0] = 9;
1058                 if (ay > ax)
1059                 {
1060                         mm[1] = 8;
1061                         mm[2] = 6;
1062                         mm[3] = 7;
1063                         mm[4] = 3;
1064                 }
1065                 else
1066                 {
1067                         mm[1] = 6;
1068                         mm[2] = 8;
1069                         mm[3] = 3;
1070                         mm[4] = 7;
1071                 }
1072                 break;
1073         case 1:
1074         case 9:
1075                 mm[0] = 6;
1076                 if (y < 0)
1077                 {
1078                         mm[1] = 3;
1079                         mm[2] = 9;
1080                         mm[3] = 2;
1081                         mm[4] = 8;
1082                 }
1083                 else
1084                 {
1085                         mm[1] = 9;
1086                         mm[2] = 3;
1087                         mm[3] = 8;
1088                         mm[4] = 2;
1089                 }
1090                 break;
1091         case 2:
1092         case 6:
1093                 mm[0] = 8;
1094                 if (x < 0)
1095                 {
1096                         mm[1] = 9;
1097                         mm[2] = 7;
1098                         mm[3] = 6;
1099                         mm[4] = 4;
1100                 }
1101                 else
1102                 {
1103                         mm[1] = 7;
1104                         mm[2] = 9;
1105                         mm[3] = 4;
1106                         mm[4] = 6;
1107                 }
1108                 break;
1109         case 4:
1110                 mm[0] = 7;
1111                 if (ay > ax)
1112                 {
1113                         mm[1] = 8;
1114                         mm[2] = 4;
1115                         mm[3] = 9;
1116                         mm[4] = 1;
1117                 }
1118                 else
1119                 {
1120                         mm[1] = 4;
1121                         mm[2] = 8;
1122                         mm[3] = 1;
1123                         mm[4] = 9;
1124                 }
1125                 break;
1126         case 5:
1127         case 13:
1128                 mm[0] = 4;
1129                 if (y < 0)
1130                 {
1131                         mm[1] = 1;
1132                         mm[2] = 7;
1133                         mm[3] = 2;
1134                         mm[4] = 8;
1135                 }
1136                 else
1137                 {
1138                         mm[1] = 7;
1139                         mm[2] = 1;
1140                         mm[3] = 8;
1141                         mm[4] = 2;
1142                 }
1143                 break;
1144         case 8:
1145                 mm[0] = 3;
1146                 if (ay > ax)
1147                 {
1148                         mm[1] = 2;
1149                         mm[2] = 6;
1150                         mm[3] = 1;
1151                         mm[4] = 9;
1152                 }
1153                 else
1154                 {
1155                         mm[1] = 6;
1156                         mm[2] = 2;
1157                         mm[3] = 9;
1158                         mm[4] = 1;
1159                 }
1160                 break;
1161         case 10:
1162         case 14:
1163                 mm[0] = 2;
1164                 if (x < 0)
1165                 {
1166                         mm[1] = 3;
1167                         mm[2] = 1;
1168                         mm[3] = 6;
1169                         mm[4] = 4;
1170                 }
1171                 else
1172                 {
1173                         mm[1] = 1;
1174                         mm[2] = 3;
1175                         mm[3] = 4;
1176                         mm[4] = 6;
1177                 }
1178                 break;
1179         case 12:
1180                 mm[0] = 1;
1181                 if (ay > ax)
1182                 {
1183                         mm[1] = 2;
1184                         mm[2] = 4;
1185                         mm[3] = 3;
1186                         mm[4] = 7;
1187                 }
1188                 else
1189                 {
1190                         mm[1] = 4;
1191                         mm[2] = 2;
1192                         mm[3] = 7;
1193                         mm[4] = 3;
1194                 }
1195                 break;
1196         }
1197
1198         /* Wants to move... */
1199         return (TRUE);
1200 }
1201
1202
1203 /*!
1204  * @brief モンスターから敵モンスターへの命中判定
1205  * @param power 打撃属性による基本命中値
1206  * @param level 攻撃側モンスターのレベル
1207  * @param ac 目標モンスターのAC
1208  * @param stun 攻撃側モンスターが朦朧状態ならTRUEを返す
1209  * @return 命中ならばTRUEを返す
1210  */
1211 static int check_hit2(int power, DEPTH level, ARMOUR_CLASS ac, int stun)
1212 {
1213         int i, k;
1214
1215         /* Percentile dice */
1216         k = randint0(100);
1217
1218         if (stun && one_in_(2)) return FALSE;
1219
1220         /* Hack -- Always miss or hit */
1221         if (k < 10) return (k < 5);
1222
1223         /* Calculate the "attack quality" */
1224         i = (power + (level * 3));
1225
1226         /* Power and Level compete against Armor */
1227         if ((i > 0) && (randint1(i) > ((ac * 3) / 4))) return (TRUE);
1228
1229         /* Assume miss */
1230         return (FALSE);
1231 }
1232
1233
1234 #define BLOW_EFFECT_TYPE_NONE  0
1235 #define BLOW_EFFECT_TYPE_FEAR  1
1236 #define BLOW_EFFECT_TYPE_SLEEP 2
1237 #define BLOW_EFFECT_TYPE_HEAL  3
1238
1239
1240 /*!
1241  * @brief モンスターから敵モンスターへの打撃攻撃処理
1242  * @param m_idx 攻撃側モンスターの参照ID
1243  * @param t_idx 目標側モンスターの参照ID
1244  * @return 実際に打撃処理が行われた場合TRUEを返す
1245  */
1246 static bool monst_attack_monst(MONSTER_IDX m_idx, MONSTER_IDX t_idx)
1247 {
1248         monster_type    *m_ptr = &current_floor_ptr->m_list[m_idx];
1249         monster_type    *t_ptr = &current_floor_ptr->m_list[t_idx];
1250
1251         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
1252         monster_race    *tr_ptr = &r_info[t_ptr->r_idx];
1253
1254         ARMOUR_CLASS ap_cnt;
1255         ARMOUR_CLASS ac;
1256         DEPTH rlev;
1257         int pt;
1258         GAME_TEXT m_name[MAX_NLEN], t_name[MAX_NLEN];
1259         char            temp[MAX_NLEN];
1260         bool            blinked;
1261         bool            explode = FALSE, touched = FALSE, fear = FALSE, dead = FALSE;
1262         POSITION y_saver = t_ptr->fy;
1263         POSITION x_saver = t_ptr->fx;
1264         int             effect_type;
1265
1266         bool see_m = is_seen(m_ptr);
1267         bool see_t = is_seen(t_ptr);
1268         bool see_either = see_m || see_t;
1269
1270         /* Can the player be aware of this attack? */
1271         bool known = (m_ptr->cdis <= MAX_SIGHT) || (t_ptr->cdis <= MAX_SIGHT);
1272         bool do_silly_attack = (one_in_(2) && p_ptr->image);
1273
1274         /* Cannot attack self */
1275         if (m_idx == t_idx) return FALSE;
1276
1277         /* Not allowed to attack */
1278         if (r_ptr->flags1 & RF1_NEVER_BLOW) return FALSE;
1279
1280         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE) return (FALSE);
1281
1282         /* Total armor */
1283         ac = tr_ptr->ac;
1284
1285         /* Extract the effective monster level */
1286         rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1287
1288         monster_desc(m_name, m_ptr, 0);
1289         monster_desc(t_name, t_ptr, 0);
1290
1291         /* Assume no blink */
1292         blinked = FALSE;
1293
1294         if (!see_either && known)
1295         {
1296                 current_floor_ptr->monster_noise = TRUE;
1297         }
1298
1299         if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
1300
1301         /* Scan through all four blows */
1302         for (ap_cnt = 0; ap_cnt < 4; ap_cnt++)
1303         {
1304                 bool obvious = FALSE;
1305
1306                 HIT_POINT power = 0;
1307                 HIT_POINT damage = 0;
1308
1309                 concptr act = NULL;
1310
1311                 /* Extract the attack infomation */
1312                 int effect = r_ptr->blow[ap_cnt].effect;
1313                 int method = r_ptr->blow[ap_cnt].method;
1314                 int d_dice = r_ptr->blow[ap_cnt].d_dice;
1315                 int d_side = r_ptr->blow[ap_cnt].d_side;
1316
1317                 if (!monster_is_valid(m_ptr)) break;
1318
1319                 /* Stop attacking if the target dies! */
1320                 if (t_ptr->fx != x_saver || t_ptr->fy != y_saver)
1321                         break;
1322
1323                 /* Hack -- no more attacks */
1324                 if (!method) break;
1325
1326                 if (method == RBM_SHOOT) continue;
1327
1328                 /* Extract the attack "power" */
1329                 power = mbe_info[effect].power;
1330
1331                 /* Monster hits */
1332                 if (!effect || check_hit2(power, rlev, ac, MON_STUNNED(m_ptr)))
1333                 {
1334                         (void)set_monster_csleep(t_idx, 0);
1335
1336                         if (t_ptr->ml)
1337                         {
1338                                 /* Redraw the health bar */
1339                                 if (p_ptr->health_who == t_idx) p_ptr->redraw |= (PR_HEALTH);
1340                                 if (p_ptr->riding == t_idx) p_ptr->redraw |= (PR_UHEALTH);
1341                         }
1342
1343                         /* Describe the attack method */
1344                         switch (method)
1345                         {
1346                         case RBM_HIT:
1347                                 {
1348                                         act = _("%sを殴った。", "hits %s.");
1349                                         touched = TRUE;
1350                                         break;
1351                                 }
1352
1353                         case RBM_TOUCH:
1354                                 {
1355                                         act = _("%sを触った。", "touches %s.");
1356                                         touched = TRUE;
1357                                         break;
1358                                 }
1359
1360                         case RBM_PUNCH:
1361                                 {
1362                                         act = _("%sをパンチした。", "punches %s.");
1363                                         touched = TRUE;
1364                                         break;
1365                                 }
1366
1367                         case RBM_KICK:
1368                                 {
1369                                         act = _("%sを蹴った。", "kicks %s.");
1370                                         touched = TRUE;
1371                                         break;
1372                                 }
1373
1374                         case RBM_CLAW:
1375                                 {
1376                                         act = _("%sをひっかいた。", "claws %s.");
1377                                         touched = TRUE;
1378                                         break;
1379                                 }
1380
1381                         case RBM_BITE:
1382                                 {
1383                                         act = _("%sを噛んだ。", "bites %s.");
1384                                         touched = TRUE;
1385                                         break;
1386                                 }
1387
1388                         case RBM_STING:
1389                                 {
1390                                         act = _("%sを刺した。", "stings %s.");
1391                                         touched = TRUE;
1392                                         break;
1393                                 }
1394
1395                         case RBM_SLASH:
1396                                 {
1397                                         act = _("%sを斬った。", "slashes %s.");
1398                                         break;
1399                                 }
1400
1401                         case RBM_BUTT:
1402                                 {
1403                                         act = _("%sを角で突いた。", "butts %s.");
1404                                         touched = TRUE;
1405                                         break;
1406                                 }
1407
1408                         case RBM_CRUSH:
1409                                 {
1410                                         act = _("%sに体当りした。", "crushes %s.");
1411                                         touched = TRUE;
1412                                         break;
1413                                 }
1414
1415                         case RBM_ENGULF:
1416                                 {
1417                                         act = _("%sを飲み込んだ。", "engulfs %s.");
1418                                         touched = TRUE;
1419                                         break;
1420                                 }
1421
1422                         case RBM_CHARGE:
1423                                 {
1424                                         act = _("%sに請求書をよこした。", "charges %s.");
1425                                         touched = TRUE;
1426                                         break;
1427                                 }
1428
1429                         case RBM_CRAWL:
1430                                 {
1431                                         act = _("%sの体の上を這い回った。", "crawls on %s.");
1432                                         touched = TRUE;
1433                                         break;
1434                                 }
1435
1436                         case RBM_DROOL:
1437                                 {
1438                                         act = _("%sによだれをたらした。", "drools on %s.");
1439                                         touched = FALSE;
1440                                         break;
1441                                 }
1442
1443                         case RBM_SPIT:
1444                                 {
1445                                         act = _("%sに唾を吐いた。", "spits on %s.");
1446                                         touched = FALSE;
1447                                         break;
1448                                 }
1449
1450                         case RBM_EXPLODE:
1451                                 {
1452                                         if (see_either) disturb(TRUE, TRUE);
1453                                         act = _("爆発した。", "explodes.");
1454                                         explode = TRUE;
1455                                         touched = FALSE;
1456                                         break;
1457                                 }
1458
1459                         case RBM_GAZE:
1460                                 {
1461                                         act = _("%sをにらんだ。", "gazes at %s.");
1462                                         touched = FALSE;
1463                                         break;
1464                                 }
1465
1466                         case RBM_WAIL:
1467                                 {
1468                                         act = _("%sに泣きついた。", "wails at %s.");
1469                                         touched = FALSE;
1470                                         break;
1471                                 }
1472
1473                         case RBM_SPORE:
1474                                 {
1475                                         act = _("%sに胞子を飛ばした。", "releases spores at %s.");
1476                                         touched = FALSE;
1477                                         break;
1478                                 }
1479
1480                         case RBM_XXX4:
1481                                 {
1482                                         act = _("%sにXXX4を飛ばした。", "projects XXX4's at %s.");
1483                                         touched = FALSE;
1484                                         break;
1485                                 }
1486
1487                         case RBM_BEG:
1488                                 {
1489                                         act = _("%sに金をせがんだ。", "begs %s for money.");
1490                                         touched = FALSE;
1491                                         break;
1492                                 }
1493
1494                         case RBM_INSULT:
1495                                 {
1496                                         act = _("%sを侮辱した。", "insults %s.");
1497                                         touched = FALSE;
1498                                         break;
1499                                 }
1500
1501                         case RBM_MOAN:
1502                                 {
1503                                         act = _("%sにむかってうめいた。", "moans at %s.");
1504                                         touched = FALSE;
1505                                         break;
1506                                 }
1507
1508                         case RBM_SHOW:
1509                                 {
1510                                         act = _("%sにむかって歌った。", "sings to %s.");
1511                                         touched = FALSE;
1512                                         break;
1513                                 }
1514                         }
1515
1516                         if (act && see_either)
1517                         {
1518 #ifdef JP
1519                                 if (do_silly_attack) act = silly_attacks2[randint0(MAX_SILLY_ATTACK)];
1520                                 strfmt(temp, act, t_name);
1521                                 msg_format("%^sは%s", m_name, temp);
1522 #else
1523                                 if (do_silly_attack)
1524                                 {
1525                                         act = silly_attacks[randint0(MAX_SILLY_ATTACK)];
1526                                         strfmt(temp, "%s %s.", act, t_name);
1527                                 }
1528                                 else strfmt(temp, act, t_name);
1529                                 msg_format("%^s %s", m_name, temp);
1530 #endif
1531                         }
1532
1533                         /* Hack -- assume all attacks are obvious */
1534                         obvious = TRUE;
1535
1536                         /* Roll out the damage */
1537                         damage = damroll(d_dice, d_side);
1538
1539                         /* Assume no effect */
1540                         effect_type = BLOW_EFFECT_TYPE_NONE;
1541
1542                         pt = GF_MISSILE;
1543
1544                         /* Apply appropriate damage */
1545                         switch (effect)
1546                         {
1547                         case 0:
1548                         case RBE_DR_MANA:
1549                                 damage = pt = 0;
1550                                 break;
1551
1552                         case RBE_SUPERHURT:
1553                                 if ((randint1(rlev*2+250) > (ac+200)) || one_in_(13))
1554                                 {
1555                                         int tmp_damage = damage - (damage * ((ac < 150) ? ac : 150) / 250);
1556                                         damage = MAX(damage, tmp_damage * 2);
1557                                         break;
1558                                 }
1559
1560                                 /* Fall through */
1561
1562                         case RBE_HURT:
1563                                 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1564                                 break;
1565
1566                         case RBE_POISON:
1567                         case RBE_DISEASE:
1568                                 pt = GF_POIS;
1569                                 break;
1570
1571                         case RBE_UN_BONUS:
1572                         case RBE_UN_POWER:
1573                                 pt = GF_DISENCHANT;
1574                                 break;
1575
1576                         case RBE_EAT_ITEM:
1577                         case RBE_EAT_GOLD:
1578                                 if ((p_ptr->riding != m_idx) && one_in_(2)) blinked = TRUE;
1579                                 break;
1580
1581                         case RBE_EAT_FOOD:
1582                         case RBE_EAT_LITE:
1583                         case RBE_BLIND:
1584                         case RBE_LOSE_STR:
1585                         case RBE_LOSE_INT:
1586                         case RBE_LOSE_WIS:
1587                         case RBE_LOSE_DEX:
1588                         case RBE_LOSE_CON:
1589                         case RBE_LOSE_CHR:
1590                         case RBE_LOSE_ALL:
1591                                 break;
1592
1593                         case RBE_ACID:
1594                                 pt = GF_ACID;
1595                                 break;
1596
1597                         case RBE_ELEC:
1598                                 pt = GF_ELEC;
1599                                 break;
1600
1601                         case RBE_FIRE:
1602                                 pt = GF_FIRE;
1603                                 break;
1604
1605                         case RBE_COLD:
1606                                 pt = GF_COLD;
1607                                 break;
1608
1609                         case RBE_CONFUSE:
1610                                 pt = GF_CONFUSION;
1611                                 break;
1612
1613                         case RBE_TERRIFY:
1614                                 effect_type = BLOW_EFFECT_TYPE_FEAR;
1615                                 break;
1616
1617                         case RBE_PARALYZE:
1618                                 effect_type = BLOW_EFFECT_TYPE_SLEEP;
1619                                 break;
1620
1621                         case RBE_SHATTER:
1622                                 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1623                                 if (damage > 23) earthquake_aux(m_ptr->fy, m_ptr->fx, 8, m_idx);
1624                                 break;
1625
1626                         case RBE_EXP_10:
1627                         case RBE_EXP_20:
1628                         case RBE_EXP_40:
1629                         case RBE_EXP_80:
1630                                 pt = GF_NETHER;
1631                                 break;
1632
1633                         case RBE_TIME:
1634                                 pt = GF_TIME;
1635                                 break;
1636
1637                         case RBE_DR_LIFE:
1638                                 pt = GF_HYPODYNAMIA;
1639                                 effect_type = BLOW_EFFECT_TYPE_HEAL;
1640                                 break;
1641
1642                         case RBE_INERTIA:
1643                                 pt = GF_INERTIAL;
1644                                 break;
1645
1646                         case RBE_STUN:
1647                                 pt = GF_SOUND;
1648                                 break;
1649
1650                         default:
1651                                 pt = 0;
1652                                 break;
1653                         }
1654
1655                         if (pt)
1656                         {
1657                                 /* Do damage if not exploding */
1658                                 if (!explode)
1659                                 {
1660                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1661                                                 damage, pt, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1662                                 }
1663
1664                                 switch (effect_type)
1665                                 {
1666                                 case BLOW_EFFECT_TYPE_FEAR:
1667                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1668                                                 damage, GF_TURN_ALL, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1669                                         break;
1670
1671                                 case BLOW_EFFECT_TYPE_SLEEP:
1672                                         project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1673                                                 r_ptr->level, GF_OLD_SLEEP, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1674                                         break;
1675
1676                                 case BLOW_EFFECT_TYPE_HEAL:
1677                                         if ((monster_living(m_idx)) && (damage > 2))
1678                                         {
1679                                                 bool did_heal = FALSE;
1680
1681                                                 if (m_ptr->hp < m_ptr->maxhp) did_heal = TRUE;
1682
1683                                                 /* Heal */
1684                                                 m_ptr->hp += damroll(4, damage / 6);
1685                                                 if (m_ptr->hp > m_ptr->maxhp) m_ptr->hp = m_ptr->maxhp;
1686
1687                                                 /* Redraw (later) if needed */
1688                                                 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
1689                                                 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
1690
1691                                                 /* Special message */
1692                                                 if (see_m && did_heal)
1693                                                 {
1694                                                         msg_format(_("%sは体力を回復したようだ。", "%^s appears healthier."), m_name);
1695                                                 }
1696                                         }
1697                                         break;
1698                                 }
1699
1700                                 if (touched)
1701                                 {
1702                                         /* Aura fire */
1703                                         if ((tr_ptr->flags2 & RF2_AURA_FIRE) && m_ptr->r_idx)
1704                                         {
1705                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK))
1706                                                 {
1707                                                         if (see_either)
1708                                                         {
1709                                                                 msg_format(_("%^sは突然熱くなった!", "%^s is suddenly very hot!"), m_name);
1710                                                         }
1711                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_FIRE;
1712                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1713                                                                 damroll (1 + ((tr_ptr->level) / 26),
1714                                                                 1 + ((tr_ptr->level) / 17)),
1715                                                                 GF_FIRE, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1716                                                 }
1717                                                 else
1718                                                 {
1719                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK);
1720                                                 }
1721                                         }
1722
1723                                         /* Aura cold */
1724                                         if ((tr_ptr->flags3 & RF3_AURA_COLD) && m_ptr->r_idx)
1725                                         {
1726                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK))
1727                                                 {
1728                                                         if (see_either)
1729                                                         {
1730                                                                 msg_format(_("%^sは突然寒くなった!", "%^s is suddenly very cold!"), m_name);
1731                                                         }
1732                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags3 |= RF3_AURA_COLD;
1733                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1734                                                                 damroll (1 + ((tr_ptr->level) / 26),
1735                                                                 1 + ((tr_ptr->level) / 17)),
1736                                                                 GF_COLD, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1737                                                 }
1738                                                 else
1739                                                 {
1740                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_COLD_MASK);
1741                                                 }
1742                                         }
1743
1744                                         /* Aura elec */
1745                                         if ((tr_ptr->flags2 & RF2_AURA_ELEC) && m_ptr->r_idx)
1746                                         {
1747                                                 if (!(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK))
1748                                                 {
1749                                                         if (see_either)
1750                                                         {
1751                                                                 msg_format(_("%^sは電撃を食らった!", "%^s gets zapped!"), m_name);
1752                                                         }
1753                                                         if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_ELEC;
1754                                                         project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1755                                                                 damroll (1 + ((tr_ptr->level) / 26),
1756                                                                 1 + ((tr_ptr->level) / 17)),
1757                                                                 GF_ELEC, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1758                                                 }
1759                                                 else
1760                                                 {
1761                                                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK);
1762                                                 }
1763                                         }
1764                                 }
1765                         }
1766                 }
1767
1768                 /* Monster missed player */
1769                 else
1770                 {
1771                         /* Analyze failed attacks */
1772                         switch (method)
1773                         {
1774                         case RBM_HIT:
1775                         case RBM_TOUCH:
1776                         case RBM_PUNCH:
1777                         case RBM_KICK:
1778                         case RBM_CLAW:
1779                         case RBM_BITE:
1780                         case RBM_STING:
1781                         case RBM_SLASH:
1782                         case RBM_BUTT:
1783                         case RBM_CRUSH:
1784                         case RBM_ENGULF:
1785                         case RBM_CHARGE:
1786                                 {
1787                                         (void)set_monster_csleep(t_idx, 0);
1788
1789                                         /* Visible monsters */
1790                                         if (see_m)
1791                                         {
1792 #ifdef JP
1793                                                 msg_format("%sは%^sの攻撃をかわした。", t_name, m_name);
1794 #else
1795                                                 msg_format("%^s misses %s.", m_name, t_name);
1796 #endif
1797                                         }
1798
1799                                         break;
1800                                 }
1801                         }
1802                 }
1803
1804
1805                 /* Analyze "visible" monsters only */
1806                 if (is_original_ap_and_seen(m_ptr) && !do_silly_attack)
1807                 {
1808                         /* Count "obvious" attacks (and ones that cause damage) */
1809                         if (obvious || damage || (r_ptr->r_blows[ap_cnt] > 10))
1810                         {
1811                                 /* Count attacks of this type */
1812                                 if (r_ptr->r_blows[ap_cnt] < MAX_UCHAR)
1813                                 {
1814                                         r_ptr->r_blows[ap_cnt]++;
1815                                 }
1816                         }
1817                 }
1818         }
1819
1820         if (explode)
1821         {
1822                 sound(SOUND_EXPLODE);
1823
1824                 /* Cancel Invulnerability */
1825                 (void)set_monster_invulner(m_idx, 0, FALSE);
1826                 mon_take_hit_mon(m_idx, m_ptr->hp + 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1827                 blinked = FALSE;
1828         }
1829
1830         /* Blink away */
1831         if (blinked && m_ptr->r_idx)
1832         {
1833                 if (teleport_barrier(m_idx))
1834                 {
1835                         if (see_m)
1836                         {
1837                                 msg_print(_("泥棒は笑って逃げ...ようとしたがバリアに防がれた。", "The thief flees laughing...? But magic barrier obstructs it."));
1838                         }
1839                         else if (known)
1840                         {
1841                                 current_floor_ptr->monster_noise = TRUE;
1842                         }
1843                 }
1844                 else
1845                 {
1846                         if (see_m)
1847                         {
1848                                 msg_print(_("泥棒は笑って逃げた!", "The thief flees laughing!"));
1849                         }
1850                         else if (known)
1851                         {
1852                                 current_floor_ptr->monster_noise = TRUE;
1853                         }
1854
1855                         teleport_away(m_idx, MAX_SIGHT * 2 + 5, 0L);
1856                 }
1857         }
1858
1859         return TRUE;
1860 }
1861
1862
1863 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
1864 {
1865         return !have_flag(f_ptr->flags, FF_GLASS) ||
1866                 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
1867                 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
1868 }
1869
1870
1871 /*!
1872  * @brief モンスター単体の1ターン行動処理メインルーチン /
1873  * Process a monster
1874  * @param m_idx 行動モンスターの参照ID
1875  * @return なし
1876  * @details
1877  * The monster is known to be within 100 grids of the player\n
1878  *\n
1879  * In several cases, we directly update the monster lore\n
1880  *\n
1881  * Note that a monster is only allowed to "reproduce" if there\n
1882  * are a limited number of "reproducing" monsters on the current\n
1883  * level.  This should prevent the level from being "swamped" by\n
1884  * reproducing monsters.  It also allows a large mass of mice to\n
1885  * prevent a louse from multiplying, but this is a small price to\n
1886  * pay for a simple multiplication method.\n
1887  *\n
1888  * XXX Monster fear is slightly odd, in particular, monsters will\n
1889  * fixate on opening a door even if they cannot open it.  Actually,\n
1890  * the same thing happens to normal monsters when they hit a door\n
1891  *\n
1892  * In addition, monsters which *cannot* open or bash\n
1893  * down a door will still stand there trying to open it...\n
1894  *\n
1895  * XXX Technically, need to check for monster in the way\n
1896  * combined with that monster being in a wall (or door?)\n
1897  *\n
1898  * A "direction" of "5" means "pick a random direction".\n
1899  */
1900 void process_monster(MONSTER_IDX m_idx)
1901 {
1902         monster_type    *m_ptr = &current_floor_ptr->m_list[m_idx];
1903         monster_race    *r_ptr = &r_info[m_ptr->r_idx];
1904         monster_race    *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1905
1906         int i, d;
1907         POSITION oy, ox, ny, nx;
1908
1909         DIRECTION mm[8];
1910
1911         grid_type       *g_ptr;
1912         feature_type    *f_ptr;
1913
1914         monster_type    *y_ptr;
1915
1916         bool            do_turn;
1917         bool            do_move;
1918         bool            do_view;
1919         bool            must_alter_to_move;
1920
1921         bool            did_open_door;
1922         bool            did_bash_door;
1923         bool            did_take_item;
1924         bool            did_kill_item;
1925         bool            did_move_body;
1926         bool            did_pass_wall;
1927         bool            did_kill_wall;
1928         bool            gets_angry = FALSE;
1929         bool            can_cross;
1930         bool            aware = TRUE;
1931
1932         bool fear, dead;
1933         bool is_riding_mon = (m_idx == p_ptr->riding);
1934         bool see_m = is_seen(m_ptr);
1935
1936         if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
1937         {
1938                 if (rakuba(0, TRUE))
1939                 {
1940 #ifdef JP
1941                         msg_print("地面に落とされた。");
1942 #else
1943                         GAME_TEXT m_name[MAX_NLEN];
1944                         monster_desc(m_name, &current_floor_ptr->m_list[p_ptr->riding], 0);
1945                         msg_format("You have fallen from %s.", m_name);
1946 #endif
1947                 }
1948         }
1949
1950         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
1951         {
1952                 choose_new_monster(m_idx, FALSE, 0);
1953                 r_ptr = &r_info[m_ptr->r_idx];
1954         }
1955
1956         /* Players hidden in shadow are almost imperceptable. -LM- */
1957         if (p_ptr->special_defense & NINJA_S_STEALTH)
1958         {
1959                 int tmp = p_ptr->lev*6+(p_ptr->skill_stl+10)*4;
1960                 if (p_ptr->monlite) tmp /= 3;
1961                 if (p_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1962                 if (r_ptr->level > (p_ptr->lev * p_ptr->lev / 20 + 10)) tmp /= 3;
1963                 /* Low-level monsters will find it difficult to locate the player. */
1964                 if (randint0(tmp) > (r_ptr->level+20)) aware = FALSE;
1965         }
1966
1967         /* Are there its parent? */
1968         if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
1969         {
1970                 /* Its parent have gone, it also goes away. */
1971
1972                 if (see_m)
1973                 {
1974                         GAME_TEXT m_name[MAX_NLEN];
1975                         monster_desc(m_name, m_ptr, 0);
1976                         msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
1977                 }
1978
1979                 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
1980                 {
1981                         GAME_TEXT m_name[MAX_NLEN];
1982                         monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
1983                         do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
1984                 }
1985
1986                 delete_monster_idx(m_idx);
1987
1988                 return;
1989         }
1990
1991         /* Quantum monsters are odd */
1992         if (r_ptr->flags2 & (RF2_QUANTUM))
1993         {
1994                 /* Sometimes skip move */
1995                 if (!randint0(2)) return;
1996
1997                 /* Sometimes die */
1998                 if (!randint0((m_idx % 100) + 10) && !(r_ptr->flags1 & RF1_QUESTOR))
1999                 {
2000                         bool sad = FALSE;
2001
2002                         if (is_pet(m_ptr) && !(m_ptr->ml)) sad = TRUE;
2003
2004                         if (see_m)
2005                         {
2006                                 GAME_TEXT m_name[MAX_NLEN];
2007                                 monster_desc(m_name, m_ptr, 0);
2008
2009                                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2010                         }
2011
2012                         /* Generate treasure, etc */
2013                         monster_death(m_idx, FALSE);
2014
2015                         delete_monster_idx(m_idx);
2016                         if (sad)
2017                         {
2018                                 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
2019                         }
2020
2021                         return;
2022                 }
2023         }
2024
2025         if (m_ptr->r_idx == MON_SHURYUUDAN)
2026         {
2027                 mon_take_hit_mon(m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
2028                 if(dead) return;
2029         }
2030
2031         if ((is_pet(m_ptr) || is_friendly(m_ptr)) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && !p_ptr->inside_battle)
2032         {
2033                 static int riding_pinch = 0;
2034
2035                 if (m_ptr->hp < m_ptr->maxhp/3)
2036                 {
2037                         GAME_TEXT m_name[MAX_NLEN];
2038                         monster_desc(m_name, m_ptr, 0);
2039
2040                         if (is_riding_mon && riding_pinch < 2)
2041                         {
2042                                 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
2043                                                          "%^s seems to be in so much pain, and trying to escape from your restriction."), m_name);
2044                                 riding_pinch++;
2045                                 disturb(TRUE, TRUE);
2046                         }
2047                         else
2048                         {
2049                                 if (is_riding_mon)
2050                                 {
2051                                         msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
2052                                         if (rakuba(-1, FALSE))
2053                                         {
2054                                                 msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
2055                                         }
2056                                 }
2057
2058                                 if (see_m)
2059                                 {
2060                                         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) &&
2061                                             player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x))
2062                                         {
2063                                                 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
2064                                         }
2065                                         msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s read a scroll of teleport level."), m_name);
2066                                         msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
2067                                 }
2068
2069                                 if (is_riding_mon && rakuba(-1, FALSE))
2070                                 {
2071                                         msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
2072                                 }
2073
2074                                 check_quest_completion(m_ptr);
2075                                 delete_monster_idx(m_idx);
2076                                 return;
2077                         }
2078                 }
2079                 else
2080                 {
2081                         /* Reset the counter */
2082                         if (is_riding_mon) riding_pinch = 0;
2083                 }
2084         }
2085
2086         /* Handle "sleep" */
2087         if (MON_CSLEEP(m_ptr))
2088         {
2089                 /* Handle non-aggravation - Still sleeping */
2090                 if (!(p_ptr->cursed & TRC_AGGRAVATE)) return;
2091
2092                 (void)set_monster_csleep(m_idx, 0);
2093
2094                 /* Notice the "waking up" */
2095                 if (m_ptr->ml)
2096                 {
2097                         GAME_TEXT m_name[MAX_NLEN];
2098                         monster_desc(m_name, m_ptr, 0);
2099                         msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
2100                 }
2101
2102                 /* Hack -- Count the wakings */
2103                 if (is_original_ap_and_seen(m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
2104                 {
2105                         r_ptr->r_wake++;
2106                 }
2107         }
2108
2109         /* Handle "stun" */
2110         if (MON_STUNNED(m_ptr))
2111         {
2112                 /* Sometimes skip move */
2113                 if (one_in_(2)) return;
2114         }
2115
2116         if (is_riding_mon)
2117         {
2118                 p_ptr->update |= (PU_BONUS);
2119         }
2120
2121         /* No one wants to be your friend if you're aggravating */
2122         if (is_friendly(m_ptr) && (p_ptr->cursed & TRC_AGGRAVATE))
2123                 gets_angry = TRUE;
2124
2125         /* Paranoia... no pet uniques outside wizard mode -- TY */
2126         if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
2127               monster_has_hostile_align(NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
2128         {
2129                 gets_angry = TRUE;
2130         }
2131
2132         if (p_ptr->inside_battle) gets_angry = FALSE;
2133
2134         if (gets_angry)
2135         {
2136                 if (is_pet(m_ptr) || see_m)
2137                 {
2138                         GAME_TEXT m_name[MAX_NLEN];
2139                         monster_desc(m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
2140                         msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
2141                 }
2142
2143                 set_hostile(m_ptr);
2144         }
2145
2146         /* Get the origin */
2147         oy = m_ptr->fy;
2148         ox = m_ptr->fx;
2149
2150         /* Attempt to "multiply" if able and allowed */
2151         if ((r_ptr->flags2 & RF2_MULTIPLY) && (current_floor_ptr->num_repro < MAX_REPRO))
2152         {
2153                 int k;
2154                 POSITION y, x;
2155
2156                 /* Count the adjacent monsters */
2157                 for (k = 0, y = oy - 1; y <= oy + 1; y++)
2158                 {
2159                         for (x = ox - 1; x <= ox + 1; x++)
2160                         {
2161                                 /* Ignore locations off of edge */
2162                                 if (!in_bounds2(y, x)) continue;
2163                                 if (current_floor_ptr->grid_array[y][x].m_idx) k++;
2164                         }
2165                 }
2166
2167                 /* Hex */
2168                 if (multiply_barrier(m_idx)) k = 8;
2169
2170                 /* Hack -- multiply slower in crowded areas */
2171                 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2172                 {
2173                         /* Try to multiply */
2174                         if (multiply_monster(m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
2175                         {
2176                                 /* Take note if visible */
2177                                 if (current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(m_ptr))
2178                                 {
2179                                         r_ptr->r_flags2 |= (RF2_MULTIPLY);
2180                                 }
2181
2182                                 /* Multiplying takes energy */
2183                                 return;
2184                         }
2185                 }
2186         }
2187
2188         if (r_ptr->a_ability_flags2 & RF6_SPECIAL)
2189         {
2190                 /* Hack -- Ohmu scatters molds! */
2191                 if (m_ptr->r_idx == MON_OHMU)
2192                 {
2193                         if (!p_ptr->inside_arena && !p_ptr->inside_battle)
2194                         {
2195                                 if (r_ptr->freq_spell && (randint1(100) <= r_ptr->freq_spell))
2196                                 {
2197                                         int k, count = 0;
2198                                         DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
2199                                         BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
2200
2201                                         for (k = 0; k < A_MAX; k++)
2202                                         {
2203                                                 if (summon_specific(m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
2204                                                 {
2205                                                         if (current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
2206                                                 }
2207                                         }
2208
2209                                         if (count && is_original_ap_and_seen(m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
2210                                 }
2211                         }
2212                 }
2213         }
2214
2215         if (!p_ptr->inside_battle)
2216         {
2217                 /* Hack! "Cyber" monster makes noise... */
2218                 if (m_ptr->ap_r_idx == MON_CYBER &&
2219                     one_in_(CYBERNOISE) &&
2220                     !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2221                 {
2222                         if (disturb_minor) disturb(FALSE, FALSE);
2223                         msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
2224                 }
2225
2226                 /* Some monsters can speak */
2227                 if ((ap_r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
2228                     one_in_(SPEAK_CHANCE) &&
2229                     player_has_los_bold(oy, ox) &&
2230                     projectable(oy, ox, p_ptr->y, p_ptr->x))
2231                 {
2232                         GAME_TEXT m_name[MAX_NLEN];
2233                         char monmessage[1024];
2234                         concptr filename;
2235
2236                         /* Acquire the monster name/poss */
2237                         if (m_ptr->ml)
2238                                 monster_desc(m_name, m_ptr, 0);
2239                         else
2240                                 strcpy(m_name, _("それ", "It"));
2241
2242                         /* Select the file for monster quotes */
2243                         if (MON_MONFEAR(m_ptr))
2244                                 filename = _("monfear_j.txt", "monfear.txt");
2245                         else if (is_pet(m_ptr))
2246                                 filename = _("monpet_j.txt", "monpet.txt");
2247                         else if (is_friendly(m_ptr))
2248                                 filename = _("monfrien_j.txt", "monfrien.txt");
2249                         else
2250                                 filename = _("monspeak_j.txt", "monspeak.txt");
2251                         /* Get the monster line */
2252                         if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
2253                         {
2254                                 /* Say something */
2255                                 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
2256                         }
2257                 }
2258         }
2259
2260         /* Try to cast spell occasionally */
2261         if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
2262         {
2263                 bool counterattack = FALSE;
2264
2265                 /* Give priority to counter attack? */
2266                 if (m_ptr->target_y)
2267                 {
2268                         MONSTER_IDX t_m_idx = current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
2269
2270                         /* The monster must be an enemy, and projectable */
2271                         if (t_m_idx && are_enemies(m_ptr, &current_floor_ptr->m_list[t_m_idx]) &&
2272                             projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
2273                         {
2274                                 counterattack = TRUE;
2275                         }
2276                 }
2277
2278                 if (!counterattack)
2279                 {
2280                         /* Attempt to cast a spell */
2281                         if (aware && make_attack_spell(m_idx)) return;
2282
2283                         /*
2284                          * Attempt to cast a spell at an enemy other than the player
2285                          * (may slow the game a smidgeon, but I haven't noticed.)
2286                          */
2287                         if (monst_spell_monst(m_idx)) return;
2288                 }
2289                 else
2290                 {
2291                         /* Attempt to do counter attack at first */
2292                         if (monst_spell_monst(m_idx)) return;
2293
2294                         if (aware && make_attack_spell(m_idx)) return;
2295                 }
2296         }
2297
2298         /* Hack -- Assume no movement */
2299         mm[0] = mm[1] = mm[2] = mm[3] = 0;
2300         mm[4] = mm[5] = mm[6] = mm[7] = 0;
2301
2302
2303         /* Confused -- 100% random */
2304         if (MON_CONFUSED(m_ptr) || !aware)
2305         {
2306                 /* Try four "random" directions */
2307                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2308         }
2309
2310         /* 75% random movement */
2311         else if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) &&
2312                  (randint0(100) < 75))
2313         {
2314                 /* Memorize flags */
2315                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
2316
2317                 /* Try four "random" directions */
2318                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2319         }
2320
2321         /* 50% random movement */
2322         else if ((r_ptr->flags1 & RF1_RAND_50) &&
2323                                 (randint0(100) < 50))
2324         {
2325                 /* Memorize flags */
2326                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
2327
2328                 /* Try four "random" directions */
2329                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2330         }
2331
2332         /* 25% random movement */
2333         else if ((r_ptr->flags1 & RF1_RAND_25) &&
2334                                 (randint0(100) < 25))
2335         {
2336                 /* Memorize flags */
2337                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
2338
2339                 /* Try four "random" directions */
2340                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2341         }
2342
2343         /* Can't reach player - find something else to hit */
2344         else if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2345         {
2346                 /* Try four "random" directions */
2347                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2348
2349                 /* Look for an enemy */
2350 #if 0  /* Hack - Too slow.  Mimic pits are horrible with this on. */
2351                 get_enemy_dir(m_idx, mm);
2352 #endif /* 0 */
2353         }
2354
2355         /* Pets will follow the player */
2356         else if (is_pet(m_ptr))
2357         {
2358                 /* Are we trying to avoid the player? */
2359                 bool avoid = ((p_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
2360
2361                 /* Do we want to find the player? */
2362                 bool lonely = (!avoid && (m_ptr->cdis > p_ptr->pet_follow_distance));
2363
2364                 /* Should we find the player if we can't find a monster? */
2365                 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
2366
2367                 /* by default, move randomly */
2368                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2369
2370                 /* Look for an enemy */
2371                 if (!get_enemy_dir(m_idx, mm))
2372                 {
2373                         /* Find the player if necessary */
2374                         if (avoid || lonely || distant)
2375                         {
2376                                 /* Remember the leash length */
2377                                 POSITION dis = p_ptr->pet_follow_distance;
2378
2379                                 /* Hack -- adjust follow distance temporarily */
2380                                 if (p_ptr->pet_follow_distance > PET_SEEK_DIST)
2381                                 {
2382                                         p_ptr->pet_follow_distance = PET_SEEK_DIST;
2383                                 }
2384
2385                                 /* Find the player */
2386                                 (void)get_moves(m_idx, mm);
2387
2388                                 /* Restore the leash */
2389                                 p_ptr->pet_follow_distance = (s16b)dis;
2390                         }
2391                 }
2392         }
2393
2394         /* Friendly monster movement */
2395         else if (!is_hostile(m_ptr))
2396         {
2397                 /* by default, move randomly */
2398                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2399
2400                 /* Look for an enemy */
2401                 get_enemy_dir(m_idx, mm);
2402         }
2403         /* Normal movement */
2404         else
2405         {
2406                 /* Logical moves, may do nothing */
2407                 if (!get_moves(m_idx, mm)) return;
2408         }
2409
2410         /* Assume nothing */
2411         do_turn = FALSE;
2412         do_move = FALSE;
2413         do_view = FALSE;
2414         must_alter_to_move = FALSE;
2415
2416         /* Assume nothing */
2417         did_open_door = FALSE;
2418         did_bash_door = FALSE;
2419         did_take_item = FALSE;
2420         did_kill_item = FALSE;
2421         did_move_body = FALSE;
2422         did_pass_wall = FALSE;
2423         did_kill_wall = FALSE;
2424
2425         /* Take a zero-terminated array of "directions" */
2426         for (i = 0; mm[i]; i++)
2427         {
2428                 /* Get the direction */
2429                 d = mm[i];
2430
2431                 /* Hack -- allow "randomized" motion */
2432                 if (d == 5) d = ddd[randint0(8)];
2433
2434                 /* Get the destination */
2435                 ny = oy + ddy[d];
2436                 nx = ox + ddx[d];
2437
2438                 /* Ignore locations off of edge */
2439                 if (!in_bounds2(ny, nx)) continue;
2440
2441                 /* Access that grid */
2442                 g_ptr = &current_floor_ptr->grid_array[ny][nx];
2443                 f_ptr = &f_info[g_ptr->feat];
2444                 can_cross = monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
2445
2446                 /* Access that grid's contents */
2447                 y_ptr = &current_floor_ptr->m_list[g_ptr->m_idx];
2448
2449                 /* Hack -- player 'in' wall */
2450                 if (player_bold(ny, nx))
2451                 {
2452                         do_move = TRUE;
2453                 }
2454
2455                 /* Possibly a monster to attack */
2456                 else if (g_ptr->m_idx)
2457                 {
2458                         do_move = TRUE;
2459                 }
2460
2461                 /* Monster destroys walls (and doors) */
2462                 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
2463                          (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
2464                          have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
2465                          check_hp_for_feat_destruction(f_ptr, m_ptr))
2466                 {
2467                         /* Eat through walls/doors/rubble */
2468                         do_move = TRUE;
2469                         if (!can_cross) must_alter_to_move = TRUE;
2470
2471                         /* Monster destroyed a wall (later) */
2472                         did_kill_wall = TRUE;
2473                 }
2474
2475                 /* Floor is open? */
2476                 else if (can_cross)
2477                 {
2478                         /* Go ahead and move */
2479                         do_move = TRUE;
2480
2481                         /* Monster moves through walls (and doors) */
2482                         if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || p_ptr->pass_wall) &&
2483                             have_flag(f_ptr->flags, FF_CAN_PASS))
2484                         {
2485                                 /* Monster went through a wall */
2486                                 did_pass_wall = TRUE;
2487                         }
2488                 }
2489
2490                 /* Handle doors and secret doors */
2491                 else if (is_closed_door(g_ptr->feat))
2492                 {
2493                         bool may_bash = TRUE;
2494
2495                         /* Assume no move allowed */
2496                         do_move = FALSE;
2497
2498                         /* Creature can open doors. */
2499                         if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
2500                                  (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2501                         {
2502                                 /* Closed doors */
2503                                 if (!f_ptr->power)
2504                                 {
2505                                         /* The door is open */
2506                                         did_open_door = TRUE;
2507
2508                                         /* Do not bash the door */
2509                                         may_bash = FALSE;
2510
2511                                         do_turn = TRUE;
2512                                 }
2513
2514                                 /* Locked doors (not jammed) */
2515                                 else
2516                                 {
2517                                         /* Try to unlock it */
2518                                         if (randint0(m_ptr->hp / 10) > f_ptr->power)
2519                                         {
2520                                                 /* Unlock the door */
2521                                                 cave_alter_feat(ny, nx, FF_DISARM);
2522
2523                                                 /* Do not bash the door */
2524                                                 may_bash = FALSE;
2525
2526                                                 do_turn = TRUE;
2527                                         }
2528                                 }
2529                         }
2530
2531                         /* Stuck doors -- attempt to bash them down if allowed */
2532                         if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
2533                                 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2534                         {
2535                                 /* Attempt to Bash */
2536                                 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
2537                                 {
2538                                         if (have_flag(f_ptr->flags, FF_GLASS))
2539                                                 msg_print(_("ガラスが砕ける音がした!", "You hear a glass was crashed!"));
2540                                         else
2541                                                 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
2542
2543                                         /* Disturb (sometimes) */
2544                                         if (disturb_minor) disturb(FALSE, FALSE);
2545
2546                                         /* The door was bashed open */
2547                                         did_bash_door = TRUE;
2548
2549                                         /* Hack -- fall into doorway */
2550                                         do_move = TRUE;
2551                                         must_alter_to_move = TRUE;
2552                                 }
2553                         }
2554
2555
2556                         /* Deal with doors in the way */
2557                         if (did_open_door || did_bash_door)
2558                         {
2559                                 /* Break down the door */
2560                                 if (did_bash_door && ((randint0(100) < 50) || (feat_state(g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
2561                                 {
2562                                         cave_alter_feat(ny, nx, FF_BASH);
2563
2564                                         if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
2565                                         {
2566                                                 p_ptr->update |= (PU_FLOW);
2567                                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2568                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
2569
2570                                                 return;
2571                                         }
2572                                 }
2573
2574                                 /* Open the door */
2575                                 else
2576                                 {
2577                                         cave_alter_feat(ny, nx, FF_OPEN);
2578                                 }
2579
2580                                 f_ptr = &f_info[g_ptr->feat];
2581
2582                                 /* Handle viewable doors */
2583                                 do_view = TRUE;
2584                         }
2585                 }
2586
2587                 /* Hack -- check for Glyph of Warding */
2588                 if (do_move && is_glyph_grid(g_ptr) &&
2589                     !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
2590                 {
2591                         /* Assume no move allowed */
2592                         do_move = FALSE;
2593
2594                         /* Break the ward */
2595                         if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
2596                         {
2597                                 /* Describe observable breakage */
2598                                 if (g_ptr->info & CAVE_MARK)
2599                                 {
2600                                         msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
2601                                 }
2602
2603                                 /* Forget the rune */
2604                                 g_ptr->info &= ~(CAVE_MARK);
2605
2606                                 /* Break the rune */
2607                                 g_ptr->info &= ~(CAVE_OBJECT);
2608                                 g_ptr->mimic = 0;
2609
2610                                 /* Allow movement */
2611                                 do_move = TRUE;
2612
2613                                 note_spot(ny, nx);
2614                         }
2615                 }
2616                 else if (do_move && is_explosive_rune_grid(g_ptr) &&
2617                          !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
2618                 {
2619                         /* Assume no move allowed */
2620                         do_move = FALSE;
2621
2622                         /* Break the ward */
2623                         if (!is_pet(m_ptr))
2624                         {
2625                                 /* Break the ward */
2626                                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
2627                                 {
2628                                         /* Describe observable breakage */
2629                                         if (g_ptr->info & CAVE_MARK)
2630                                         {
2631                                                 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
2632                                                 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);
2633                                         }
2634                                 }
2635                                 else
2636                                 {
2637                                         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
2638                                 }
2639
2640                                 /* Forget the rune */
2641                                 g_ptr->info &= ~(CAVE_MARK);
2642
2643                                 /* Break the rune */
2644                                 g_ptr->info &= ~(CAVE_OBJECT);
2645                                 g_ptr->mimic = 0;
2646
2647                                 note_spot(ny, nx);
2648                                 lite_spot(ny, nx);
2649
2650                                 if (!monster_is_valid(m_ptr)) return;
2651                                 /* Allow movement */
2652                                 do_move = TRUE;
2653                         }
2654                 }
2655
2656                 /* The player is in the way */
2657                 if (do_move && player_bold(ny, nx))
2658                 {
2659                         /* Some monsters never attack */
2660                         if (r_ptr->flags1 & RF1_NEVER_BLOW)
2661                         {
2662                                 /* Hack -- memorize lack of attacks */
2663                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
2664
2665                                 /* Do not move */
2666                                 do_move = FALSE;
2667                         }
2668
2669                         /* In anti-melee dungeon, stupid or confused monster takes useless current_world_ptr->game_turn */
2670                         if (do_move && (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE))
2671                         {
2672                                 if (!MON_CONFUSED(m_ptr))
2673                                 {
2674                                         if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
2675                                         else
2676                                         {
2677                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2678                                         }
2679                                 }
2680                         }
2681
2682                         /* The player is in the way.  Attack him. */
2683                         if (do_move)
2684                         {
2685                                 if (!p_ptr->riding || one_in_(2))
2686                                 {
2687                                         /* Do the attack */
2688                                         (void)make_attack_normal(m_idx);
2689
2690                                         /* Do not move */
2691                                         do_move = FALSE;
2692
2693                                         /* Took a current_world_ptr->game_turn */
2694                                         do_turn = TRUE;
2695                                 }
2696                         }
2697                 }
2698
2699                 /* A monster is in the way */
2700                 if (do_move && g_ptr->m_idx)
2701                 {
2702                         monster_race *z_ptr = &r_info[y_ptr->r_idx];
2703
2704                         /* Assume no movement */
2705                         do_move = FALSE;
2706
2707                         /* Attack 'enemies' */
2708                         if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
2709                                  (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
2710                                  can_cross && (g_ptr->m_idx != p_ptr->riding)) ||
2711                                 are_enemies(m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
2712                         {
2713                                 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
2714                                 {
2715                                         if (r_ptr->flags2 & RF2_KILL_BODY)
2716                                         {
2717                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
2718                                         }
2719
2720                                         /* attack */
2721                                         if (y_ptr->r_idx && (y_ptr->hp >= 0))
2722                                         {
2723                                                 if (monst_attack_monst(m_idx, g_ptr->m_idx)) return;
2724
2725                                                 /* In anti-melee dungeon, stupid or confused monster takes useless current_world_ptr->game_turn */
2726                                                 else if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
2727                                                 {
2728                                                         if (MON_CONFUSED(m_ptr)) return;
2729                                                         else if (r_ptr->flags2 & RF2_STUPID)
2730                                                         {
2731                                                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2732                                                                 return;
2733                                                         }
2734                                                 }
2735                                         }
2736                                 }
2737                         }
2738
2739                         /* Push past weaker monsters (unless leaving a wall) */
2740                         else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
2741                                 (r_ptr->mexp > z_ptr->mexp) &&
2742                                 can_cross && (g_ptr->m_idx != p_ptr->riding) &&
2743                                 monster_can_cross_terrain(current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
2744                         {
2745                                 /* Allow movement */
2746                                 do_move = TRUE;
2747
2748                                 /* Monster pushed past another monster */
2749                                 did_move_body = TRUE;
2750
2751                                 /* Wake up the moved monster */
2752                                 (void)set_monster_csleep(g_ptr->m_idx, 0);
2753
2754                                 /* Message */
2755                         }
2756                 }
2757
2758                 if (is_riding_mon)
2759                 {
2760                         if (!p_ptr->riding_ryoute && !MON_MONFEAR(&current_floor_ptr->m_list[p_ptr->riding])) do_move = FALSE;
2761                 }
2762
2763                 if (did_kill_wall && do_move)
2764                 {
2765                         if (one_in_(GRINDNOISE))
2766                         {
2767                                 if (have_flag(f_ptr->flags, FF_GLASS))
2768                                         msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
2769                                 else
2770                                         msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
2771                         }
2772
2773                         cave_alter_feat(ny, nx, FF_HURT_DISI);
2774
2775                         if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
2776                         {
2777                                 p_ptr->update |= (PU_FLOW);
2778                                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2779                                 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2780
2781                                 return;
2782                         }
2783
2784                         f_ptr = &f_info[g_ptr->feat];
2785
2786                         /* Note changes to viewable region */
2787                         do_view = TRUE;
2788                         do_turn = TRUE;
2789                 }
2790
2791                 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
2792                 {
2793                         if (!monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
2794                         {
2795                                 /* Assume no move allowed */
2796                                 do_move = FALSE;
2797                         }
2798                 }
2799
2800                 /*
2801                  * Check if monster can cross terrain
2802                  * This is checked after the normal attacks
2803                  * to allow monsters to attack an enemy,
2804                  * even if it can't enter the terrain.
2805                  */
2806                 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
2807                 {
2808                         /* Assume no move allowed */
2809                         do_move = FALSE;
2810                 }
2811
2812                 /* Some monsters never move */
2813                 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
2814                 {
2815                         /* Hack -- memorize lack of moves */
2816                         if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
2817
2818                         /* Do not move */
2819                         do_move = FALSE;
2820                 }
2821
2822                 /* Creature has been allowed move */
2823                 if (do_move)
2824                 {
2825                         do_turn = TRUE;
2826
2827                         if (have_flag(f_ptr->flags, FF_TREE))
2828                         {
2829                                 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
2830                                 {
2831                                         m_ptr->energy_need += ENERGY_NEED();
2832                                 }
2833                         }
2834
2835                         if (!is_riding_mon)
2836                         {
2837                                 /* Hack -- Update the old location */
2838                                 current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
2839
2840                                 /* Mega-Hack -- move the old monster, if any */
2841                                 if (g_ptr->m_idx)
2842                                 {
2843                                         /* Move the old monster */
2844                                         y_ptr->fy = oy;
2845                                         y_ptr->fx = ox;
2846
2847                                         /* Update the old monster */
2848                                         update_monster(g_ptr->m_idx, TRUE);
2849                                 }
2850
2851                                 /* Hack -- Update the new location */
2852                                 g_ptr->m_idx = m_idx;
2853
2854                                 /* Move the monster */
2855                                 m_ptr->fy = ny;
2856                                 m_ptr->fx = nx;
2857                                 update_monster(m_idx, TRUE);
2858
2859                                 lite_spot(oy, ox);
2860                                 lite_spot(ny, nx);
2861                         }
2862                         else
2863                         {
2864                                 /* sound(SOUND_WALK); */
2865                                 if (!move_player_effect(ny, nx, MPE_DONT_PICKUP)) break;
2866                         }
2867
2868                         /* Possible disturb */
2869                         if (m_ptr->ml &&
2870                             (disturb_move ||
2871                              (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
2872                              (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)))
2873                         {
2874                                 if (is_hostile(m_ptr))
2875                                         disturb(FALSE, TRUE);
2876                         }
2877
2878                         /* Take or Kill objects on the floor */
2879                         if (g_ptr->o_idx && (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
2880                             (!is_pet(m_ptr) || ((p_ptr->pet_extra_flags & PF_PICKUP_ITEMS) && (r_ptr->flags2 & RF2_TAKE_ITEM))))
2881                         {
2882                                 OBJECT_IDX this_o_idx, next_o_idx;
2883                                 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
2884
2885                                 /* Scan all objects in the grid */
2886                                 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
2887                                 {
2888                                         BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
2889                                         GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
2890                                         object_type *o_ptr = &current_floor_ptr->o_list[this_o_idx];
2891                                         next_o_idx = o_ptr->next_o_idx;
2892
2893                                         if (do_take)
2894                                         {
2895                                                 /* Skip gold */
2896                                                 if (o_ptr->tval == TV_GOLD) continue;
2897
2898                                                 /*
2899                                                  * Skip "real" corpses and statues, to avoid extreme
2900                                                  * silliness like a novice rogue pockets full of statues
2901                                                  * and corpses.
2902                                                  */
2903                                                 if ((o_ptr->tval == TV_CORPSE) ||
2904                                                     (o_ptr->tval == TV_STATUE)) continue;
2905                                         }
2906
2907                                         /* Extract some flags */
2908                                         object_flags(o_ptr, flgs);
2909
2910                                         /* Acquire the object name */
2911                                         object_desc(o_name, o_ptr, 0);
2912                                         monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
2913
2914                                         /* React to objects that hurt the monster */
2915                                         if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
2916                                         if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
2917                                         if (have_flag(flgs, TR_SLAY_TROLL))  flg3 |= (RF3_TROLL);
2918                                         if (have_flag(flgs, TR_KILL_TROLL))  flg3 |= (RF3_TROLL);
2919                                         if (have_flag(flgs, TR_SLAY_GIANT))  flg3 |= (RF3_GIANT);
2920                                         if (have_flag(flgs, TR_KILL_GIANT))  flg3 |= (RF3_GIANT);
2921                                         if (have_flag(flgs, TR_SLAY_ORC))    flg3 |= (RF3_ORC);
2922                                         if (have_flag(flgs, TR_KILL_ORC))    flg3 |= (RF3_ORC);
2923                                         if (have_flag(flgs, TR_SLAY_DEMON))  flg3 |= (RF3_DEMON);
2924                                         if (have_flag(flgs, TR_KILL_DEMON))  flg3 |= (RF3_DEMON);
2925                                         if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
2926                                         if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
2927                                         if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
2928                                         if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
2929                                         if (have_flag(flgs, TR_SLAY_EVIL))   flg3 |= (RF3_EVIL);
2930                                         if (have_flag(flgs, TR_KILL_EVIL))   flg3 |= (RF3_EVIL);
2931                                         if (have_flag(flgs, TR_SLAY_HUMAN))  flg2 |= (RF2_HUMAN);
2932                                         if (have_flag(flgs, TR_KILL_HUMAN))  flg2 |= (RF2_HUMAN);
2933                                         if (have_flag(flgs, TR_BRAND_ACID))  flgr |= (RFR_IM_ACID);
2934                                         if (have_flag(flgs, TR_BRAND_ELEC))  flgr |= (RFR_IM_ELEC);
2935                                         if (have_flag(flgs, TR_BRAND_FIRE))  flgr |= (RFR_IM_FIRE);
2936                                         if (have_flag(flgs, TR_BRAND_COLD))  flgr |= (RFR_IM_COLD);
2937                                         if (have_flag(flgs, TR_BRAND_POIS))  flgr |= (RFR_IM_POIS);
2938
2939                                         /* The object cannot be picked up by the monster */
2940                                         if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
2941                                             ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
2942                                         {
2943                                                 /* Only give a message for "take_item" */
2944                                                 if (do_take && (r_ptr->flags2 & RF2_STUPID))
2945                                                 {
2946                                                         did_take_item = TRUE;
2947
2948                                                         /* Describe observable situations */
2949                                                         if (m_ptr->ml && player_can_see_bold(ny, nx))
2950                                                         {
2951                                                                 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
2952                                                         }
2953                                                 }
2954                                         }
2955
2956                                         /* Pick up the item */
2957                                         else if (do_take)
2958                                         {
2959                                                 did_take_item = TRUE;
2960
2961                                                 /* Describe observable situations */
2962                                                 if (player_can_see_bold(ny, nx))
2963                                                 {
2964                                                         msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
2965                                                 }
2966
2967                                                 /* Excise the object */
2968                                                 excise_object_idx(this_o_idx);
2969
2970                                                 /* Forget mark */
2971                                                 o_ptr->marked &= OM_TOUCHED;
2972
2973                                                 /* Forget location */
2974                                                 o_ptr->iy = o_ptr->ix = 0;
2975
2976                                                 /* Memorize monster */
2977                                                 o_ptr->held_m_idx = m_idx;
2978
2979                                                 /* Build a stack */
2980                                                 o_ptr->next_o_idx = m_ptr->hold_o_idx;
2981
2982                                                 /* Carry object */
2983                                                 m_ptr->hold_o_idx = this_o_idx;
2984                                         }
2985
2986                                         /* Destroy the item if not a pet */
2987                                         else if (!is_pet(m_ptr))
2988                                         {
2989                                                 did_kill_item = TRUE;
2990
2991                                                 /* Describe observable situations */
2992                                                 if (player_has_los_bold(ny, nx))
2993                                                 {
2994                                                         msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
2995                                                 }
2996
2997                                                 delete_object_idx(this_o_idx);
2998                                         }
2999                                 }
3000                         }
3001                 }
3002
3003                 /* Stop when done */
3004                 if (do_turn) break;
3005         }
3006
3007         /*
3008          *  Forward movements failed, but now received LOS attack!
3009          *  Try to flow by smell.
3010          */
3011         if (p_ptr->no_flowed && i > 2 &&  m_ptr->target_y)
3012                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3013
3014         /* If we haven't done anything, try casting a spell again */
3015         if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
3016         {
3017                 /* Try to cast spell again */
3018                 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
3019                 {
3020                         if (make_attack_spell(m_idx)) return;
3021                 }
3022         }
3023
3024
3025         /* Notice changes in view */
3026         if (do_view)
3027         {
3028                 p_ptr->update |= (PU_FLOW);
3029                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3030         }
3031
3032         /* Notice changes in view */
3033         if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
3034                 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !p_ptr->inside_battle)))
3035         {
3036                 p_ptr->update |= (PU_MON_LITE);
3037         }
3038
3039         /* Learn things from observable monster */
3040         if (is_original_ap_and_seen(m_ptr))
3041         {
3042                 /* Monster opened a door */
3043                 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
3044
3045                 /* Monster bashed a door */
3046                 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
3047
3048                 /* Monster tried to pick something up */
3049                 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
3050
3051                 /* Monster tried to crush something */
3052                 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
3053
3054                 /* Monster pushed past another monster */
3055                 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
3056
3057                 /* Monster passed through a wall */
3058                 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
3059
3060                 /* Monster destroyed a wall */
3061                 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
3062         }
3063
3064
3065         /* Hack -- get "bold" if out of options */
3066         if (!do_turn && !do_move && MON_MONFEAR(m_ptr) && aware)
3067         {
3068                 /* No longer afraid */
3069                 (void)set_monster_monfear(m_idx, 0);
3070
3071                 /* Message if seen */
3072                 if (see_m)
3073                 {
3074                         GAME_TEXT m_name[MAX_NLEN];
3075                         monster_desc(m_name, m_ptr, 0);
3076                         msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
3077                 }
3078
3079                 if (m_ptr->ml) chg_virtue(V_COMPASSION, -1);
3080
3081                 /* Actually do something now (?) */
3082         }
3083 }
3084
3085 /*!
3086  * @brief 全モンスターのターン管理メインルーチン /
3087  * Process all the "live" monsters, once per game turn.
3088  * @return なし
3089  * @details
3090  * During each game current game turn, we scan through the list of all the "live" monsters,\n
3091  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
3092  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
3093  *\n
3094  * Note that monsters can never move in the monster array (except when the\n
3095  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
3096  *\n
3097  * This function is responsible for at least half of the processor time\n
3098  * on a normal system with a "normal" amount of monsters and a player doing\n
3099  * normal things.\n
3100  *\n
3101  * When the player is resting, virtually 90% of the processor time is spent\n
3102  * in this function, and its children, "process_monster()" and "make_move()".\n
3103  *\n
3104  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
3105  * especially when the player is running.\n
3106  *\n
3107  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
3108  * monsters while they are still being "born".  A monster is "fresh" only\n
3109  * during the game turn in which it is created, and we use the "hack_m_idx" to\n
3110  * determine if the monster is yet to be processed during the game turn.\n
3111  *\n
3112  * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
3113  * move before any "nasty" monsters get to use their spell attacks.\n
3114  *\n
3115  * Note that when the "knowledge" about the currently tracked monster\n
3116  * changes (flags, attacks, spells), we induce a redraw of the monster\n
3117  * recall window.\n
3118  */
3119 void process_monsters(void)
3120 {
3121         MONSTER_IDX i;
3122         POSITION fx, fy;
3123
3124         bool            test;
3125
3126         monster_type    *m_ptr;
3127         monster_race    *r_ptr;
3128
3129         MONRACE_IDX old_monster_race_idx;
3130
3131         BIT_FLAGS old_r_flags1 = 0L;
3132         BIT_FLAGS old_r_flags2 = 0L;
3133         BIT_FLAGS old_r_flags3 = 0L;
3134         BIT_FLAGS old_r_flags4 = 0L;
3135         BIT_FLAGS old_r_flags5 = 0L;
3136         BIT_FLAGS old_r_flags6 = 0L;
3137         BIT_FLAGS old_r_flagsr = 0L;
3138
3139         byte old_r_blows0 = 0;
3140         byte old_r_blows1 = 0;
3141         byte old_r_blows2 = 0;
3142         byte old_r_blows3 = 0;
3143
3144         byte old_r_cast_spell = 0;
3145
3146         SPEED speed;
3147
3148         /* Clear monster fighting indicator */
3149         current_floor_ptr->monster_noise = FALSE;
3150
3151         /* Memorize old race */
3152         old_monster_race_idx = p_ptr->monster_race_idx;
3153
3154         /* Acquire knowledge */
3155         if (p_ptr->monster_race_idx)
3156         {
3157                 /* Acquire current monster */
3158                 r_ptr = &r_info[p_ptr->monster_race_idx];
3159
3160                 /* Memorize flags */
3161                 old_r_flags1 = r_ptr->r_flags1;
3162                 old_r_flags2 = r_ptr->r_flags2;
3163                 old_r_flags3 = r_ptr->r_flags3;
3164                 old_r_flags4 = r_ptr->r_flags4;
3165                 old_r_flags5 = r_ptr->r_flags5;
3166                 old_r_flags6 = r_ptr->r_flags6;
3167                 old_r_flagsr = r_ptr->r_flagsr;
3168
3169                 /* Memorize blows */
3170                 old_r_blows0 = r_ptr->r_blows[0];
3171                 old_r_blows1 = r_ptr->r_blows[1];
3172                 old_r_blows2 = r_ptr->r_blows[2];
3173                 old_r_blows3 = r_ptr->r_blows[3];
3174
3175                 /* Memorize castings */
3176                 old_r_cast_spell = r_ptr->r_cast_spell;
3177         }
3178
3179
3180         /* Process the monsters (backwards) */
3181         for (i = current_floor_ptr->m_max - 1; i >= 1; i--)
3182         {
3183                 /* Access the monster */
3184                 m_ptr = &current_floor_ptr->m_list[i];
3185                 r_ptr = &r_info[m_ptr->r_idx];
3186
3187                 /* Handle "leaving" */
3188                 if (p_ptr->leaving) break;
3189
3190                 /* Ignore "dead" monsters */
3191                 if (!monster_is_valid(m_ptr)) continue;
3192
3193                 if (p_ptr->wild_mode) continue;
3194
3195
3196                 /* Handle "fresh" monsters */
3197                 if (m_ptr->mflag & MFLAG_BORN)
3198                 {
3199                         /* No longer "fresh" */
3200                         m_ptr->mflag &= ~(MFLAG_BORN);
3201
3202                         /* Skip */
3203                         continue;
3204                 }
3205
3206                 /* Hack -- Require proximity */
3207                 if (m_ptr->cdis >= AAF_LIMIT) continue;
3208
3209                 fx = m_ptr->fx;
3210                 fy = m_ptr->fy;
3211
3212                 /* Flow by smell is allowed */
3213                 if (!p_ptr->no_flowed)
3214                 {
3215                         m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3216                 }
3217
3218                 /* Assume no move */
3219                 test = FALSE;
3220
3221                 /* Handle "sensing radius" */
3222                 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
3223                 {
3224                         /* We can "sense" the player */
3225                         test = TRUE;
3226                 }
3227
3228                 /* Handle "sight" and "aggravation" */
3229         else if ((m_ptr->cdis <= MAX_SIGHT || p_ptr->inside_battle) &&
3230                         (player_has_los_bold(fy, fx) || (p_ptr->cursed & TRC_AGGRAVATE)))
3231                 {
3232                         /* We can "see" or "feel" the player */
3233                         test = TRUE;
3234                 }
3235
3236 #if 0 /* (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when == current_floor_ptr->grid_array[fy][fx].when) is always FALSE... */
3237                 /* Hack -- Monsters can "smell" the player from far away */
3238                 /* Note that most monsters have "aaf" of "20" or so */
3239                 else if (!(m_ptr->mflag2 & MFLAG2_NOFLOW) &&
3240                         cave_have_flag_bold(p_ptr->y, p_ptr->x, FF_MOVE) &&
3241                         (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when == current_floor_ptr->grid_array[fy][fx].when) &&
3242                         (current_floor_ptr->grid_array[fy][fx].dist < MONSTER_FLOW_DEPTH) &&
3243                         (current_floor_ptr->grid_array[fy][fx].dist < r_ptr->aaf))
3244                 {
3245                         /* We can "smell" the player */
3246                         test = TRUE;
3247                 }
3248 #endif
3249                 else if (m_ptr->target_y) test = TRUE;
3250
3251                 /* Do nothing */
3252                 if (!test) continue;
3253
3254
3255                 if (p_ptr->riding == i)
3256                         speed = p_ptr->pspeed;
3257                 else
3258                 {
3259                         speed = m_ptr->mspeed;
3260
3261                         /* Monsters move quickly in Nightmare mode */
3262                         if (ironman_nightmare) speed += 5;
3263
3264                         if (MON_FAST(m_ptr)) speed += 10;
3265                         if (MON_SLOW(m_ptr)) speed -= 10;
3266                 }
3267
3268                 /* Give this monster some energy */
3269                 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
3270
3271                 /* Not enough energy to move */
3272                 if (m_ptr->energy_need > 0) continue;
3273
3274                 /* Use up "some" energy */
3275                 m_ptr->energy_need += ENERGY_NEED();
3276
3277
3278                 /* Save global index */
3279                 hack_m_idx = i;
3280
3281                 /* Process the monster */
3282                 process_monster(i);
3283
3284                 reset_target(m_ptr);
3285
3286                 /* Give up flow_by_smell when it might useless */
3287                 if (p_ptr->no_flowed && one_in_(3))
3288                         m_ptr->mflag2 |= MFLAG2_NOFLOW;
3289
3290                 /* Hack -- notice death or departure */
3291                 if (!p_ptr->playing || p_ptr->is_dead) break;
3292
3293                 /* Notice leaving */
3294                 if (p_ptr->leaving) break;
3295         }
3296
3297         /* Reset global index */
3298         hack_m_idx = 0;
3299
3300
3301         /* Tracking a monster race (the same one we were before) */
3302         if (p_ptr->monster_race_idx && (p_ptr->monster_race_idx == old_monster_race_idx))
3303         {
3304                 /* Acquire monster race */
3305                 r_ptr = &r_info[p_ptr->monster_race_idx];
3306
3307                 /* Check for knowledge change */
3308                 if ((old_r_flags1 != r_ptr->r_flags1) ||
3309                         (old_r_flags2 != r_ptr->r_flags2) ||
3310                         (old_r_flags3 != r_ptr->r_flags3) ||
3311                         (old_r_flags4 != r_ptr->r_flags4) ||
3312                         (old_r_flags5 != r_ptr->r_flags5) ||
3313                         (old_r_flags6 != r_ptr->r_flags6) ||
3314                         (old_r_flagsr != r_ptr->r_flagsr) ||
3315                         (old_r_blows0 != r_ptr->r_blows[0]) ||
3316                         (old_r_blows1 != r_ptr->r_blows[1]) ||
3317                         (old_r_blows2 != r_ptr->r_blows[2]) ||
3318                         (old_r_blows3 != r_ptr->r_blows[3]) ||
3319                         (old_r_cast_spell != r_ptr->r_cast_spell))
3320                 {
3321                         p_ptr->window |= (PW_MONSTER);
3322                 }
3323         }
3324 }
3325