OSDN Git Service

[Refacotr] #37353 monster-process.c の整形 / Reshaped monster-process.c
[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-floor.h"
25 #include "spells-summon.h"
26 #include "quest.h"
27 #include "avatar.h"
28 #include "realm-hex.h"
29 #include "object-flavor.h"
30 #include "object-hook.h"
31 #include "feature.h"
32 #include "grid.h"
33 #include "player-move.h"
34 #include "monster-status.h"
35 #include "monster-spell.h"
36 #include "monster-process.h"
37 #include "monsterrace-hook.h"
38 #include "dungeon.h"
39 #include "floor.h"
40 #include "files.h"
41 #include "view-mainwindow.h"
42
43 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
44 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx);
45 bool process_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
46 void vanish_nonunique(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
47 void produce_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
48 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox);
49 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware);
50 bool runaway_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon, bool see_m);
51 void process_special(player_type *target_ptr, MONSTER_IDX m_idx);
52 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware);
53 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware);
54
55  /*!
56   * @brief モンスターが敵に接近するための方向を決める /
57   * Calculate the direction to the next enemy
58   * @param target_ptr プレーヤーへの参照ポインタ
59   * @param m_idx モンスターの参照ID
60   * @param mm 移動するべき方角IDを返す参照ポインタ
61   * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
62   */
63 static bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm)
64 {
65         floor_type *floor_ptr = target_ptr->current_floor_ptr;
66         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
67         monster_race *r_ptr = &r_info[m_ptr->r_idx];
68         monster_type *t_ptr;
69
70         POSITION x = 0, y = 0;
71         if (target_ptr->riding_t_m_idx && player_bold(target_ptr, m_ptr->fy, m_ptr->fx))
72         {
73                 y = floor_ptr->m_list[target_ptr->riding_t_m_idx].fy;
74                 x = floor_ptr->m_list[target_ptr->riding_t_m_idx].fx;
75         }
76         else if (is_pet(m_ptr) && target_ptr->pet_t_m_idx)
77         {
78                 y = floor_ptr->m_list[target_ptr->pet_t_m_idx].fy;
79                 x = floor_ptr->m_list[target_ptr->pet_t_m_idx].fx;
80         }
81         else
82         {
83                 int start;
84                 int plus = 1;
85                 if (target_ptr->phase_out)
86                 {
87                         start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
88                         if (randint0(2)) plus = -1;
89                 }
90                 else start = floor_ptr->m_max + 1;
91
92                 /* Scan thru all monsters */
93                 for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus)
94                 {
95                         MONSTER_IDX dummy = (i % floor_ptr->m_max);
96
97                         if (!dummy) continue;
98
99                         MONSTER_IDX t_idx = dummy;
100                         t_ptr = &floor_ptr->m_list[t_idx];
101
102                         /* The monster itself isn't a target */
103                         if (t_ptr == m_ptr) continue;
104
105                         if (!monster_is_valid(t_ptr)) continue;
106
107                         if (is_pet(m_ptr))
108                         {
109                                 /* Hack -- only fight away from player */
110                                 if (target_ptr->pet_follow_distance < 0)
111                                 {
112                                         /* No fighting near player */
113                                         if (t_ptr->cdis <= (0 - target_ptr->pet_follow_distance))
114                                         {
115                                                 continue;
116                                         }
117                                 }
118                                 /* Hack -- no fighting away from player */
119                                 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > target_ptr->pet_follow_distance))
120                                 {
121                                         continue;
122                                 }
123
124                                 if (r_ptr->aaf < t_ptr->cdis) continue;
125                         }
126
127                         /* Monster must be 'an enemy' */
128                         if (!are_enemies(target_ptr, m_ptr, t_ptr)) continue;
129
130                         /* Monster must be projectable if we can't pass through walls */
131                         if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) ||
132                                 ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)))
133                         {
134                                 if (!in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
135                         }
136                         else
137                         {
138                                 if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
139                         }
140
141                         /* OK -- we've got a target */
142                         y = t_ptr->fy;
143                         x = t_ptr->fx;
144
145                         break;
146                 }
147
148                 if (!x && !y) return FALSE;
149         }
150
151         /* Extract the direction */
152         x -= m_ptr->fx;
153         y -= m_ptr->fy;
154
155         /* North */
156         if ((y < 0) && (x == 0))
157         {
158                 mm[0] = 8;
159                 mm[1] = 7;
160                 mm[2] = 9;
161         }
162         /* South */
163         else if ((y > 0) && (x == 0))
164         {
165                 mm[0] = 2;
166                 mm[1] = 1;
167                 mm[2] = 3;
168         }
169         /* East */
170         else if ((x > 0) && (y == 0))
171         {
172                 mm[0] = 6;
173                 mm[1] = 9;
174                 mm[2] = 3;
175         }
176         /* West */
177         else if ((x < 0) && (y == 0))
178         {
179                 mm[0] = 4;
180                 mm[1] = 7;
181                 mm[2] = 1;
182         }
183         /* North-West */
184         else if ((y < 0) && (x < 0))
185         {
186                 mm[0] = 7;
187                 mm[1] = 4;
188                 mm[2] = 8;
189         }
190         /* North-East */
191         else if ((y < 0) && (x > 0))
192         {
193                 mm[0] = 9;
194                 mm[1] = 6;
195                 mm[2] = 8;
196         }
197         /* South-West */
198         else if ((y > 0) && (x < 0))
199         {
200                 mm[0] = 1;
201                 mm[1] = 4;
202                 mm[2] = 2;
203         }
204         /* South-East */
205         else if ((y > 0) && (x > 0))
206         {
207                 mm[0] = 3;
208                 mm[1] = 6;
209                 mm[2] = 2;
210         }
211
212         return TRUE;
213 }
214
215 /*!
216  * @brief モンスターがプレイヤーから逃走するかどうかを返す /
217  * Returns whether a given monster will try to run from the player.
218  * @param m_idx 逃走するモンスターの参照ID
219  * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
220  * @details
221  * Monsters will attempt to avoid very powerful players.  See below.\n
222  *\n
223  * Because this function is called so often, little details are important\n
224  * for efficiency.  Like not using "mod" or "div" when possible.  And\n
225  * attempting to check the conditions in an optimal order.  Note that\n
226  * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
227  *\n
228  * Note that this function is responsible for about one to five percent\n
229  * of the processor use in normal conditions...\n
230  */
231 static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
232 {
233         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
234         monster_race *r_ptr = &r_info[m_ptr->r_idx];
235
236         PLAYER_LEVEL p_lev;
237         DEPTH m_lev;
238         HIT_POINT p_chp, p_mhp;
239         HIT_POINT m_chp, m_mhp;
240         u32b p_val, m_val;
241
242         /* Friends can be commanded to avoid the player */
243         if (is_pet(m_ptr))
244         {
245                 /* Are we trying to avoid the player? */
246                 return ((target_ptr->pet_follow_distance < 0) &&
247                         (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
248         }
249
250         /* Keep monsters from running too far away */
251         if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
252
253         /* All "afraid" monsters will run away */
254         if (MON_MONFEAR(m_ptr)) return TRUE;
255
256         /* Nearby monsters will not become terrified */
257         if (m_ptr->cdis <= 5) return FALSE;
258
259         /* Examine player power (level) */
260         p_lev = target_ptr->lev;
261
262         /* Examine monster power (level plus morale) */
263         m_lev = r_ptr->level + (m_idx & 0x08) + 25;
264
265         /* Optimize extreme cases below */
266         if (m_lev > p_lev + 4) return FALSE;
267         if (m_lev + 4 <= p_lev) return TRUE;
268
269         /* Examine player health */
270         p_chp = target_ptr->chp;
271         p_mhp = target_ptr->mhp;
272
273         /* Examine monster health */
274         m_chp = m_ptr->hp;
275         m_mhp = m_ptr->maxhp;
276
277         /* Prepare to optimize the calculation */
278         p_val = (p_lev * p_mhp) + (p_chp << 2); /* div p_mhp */
279         m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
280
281         /* Strong players scare strong monsters */
282         if (p_val * m_mhp > m_val * p_mhp) return TRUE;
283
284         return FALSE;
285 }
286
287
288 /*!
289  * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
290  * Search spell castable grid
291  * @param target_ptr プレーヤーへの参照ポインタ
292  * @param m_idx モンスターの参照ID
293  * @param yp 適したマスのY座標を返す参照ポインタ
294  * @param xp 適したマスのX座標を返す参照ポインタ
295  * @return 有効なマスがあった場合TRUEを返す
296  */
297 static bool get_moves_aux2(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
298 {
299         floor_type *floor_ptr = target_ptr->current_floor_ptr;
300         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
301         monster_race *r_ptr = &r_info[m_ptr->r_idx];
302
303         /* Monster location */
304         POSITION y1 = m_ptr->fy;
305         POSITION x1 = m_ptr->fx;
306
307         /* Monster can already cast spell to player */
308         if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
309
310         /* Set current grid cost */
311         int now_cost = floor_ptr->grid_array[y1][x1].cost;
312         if (now_cost == 0) now_cost = 999;
313
314         /* Can monster bash or open doors? */
315         bool can_open_door = FALSE;
316         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
317         {
318                 can_open_door = TRUE;
319         }
320
321         /* Check nearby grids, diagonals first */
322         int best = 999;
323         for (int i = 7; i >= 0; i--)
324         {
325                 int cost;
326
327                 POSITION y = y1 + ddy_ddd[i];
328                 POSITION x = x1 + ddx_ddd[i];
329
330                 /* Ignore locations off of edge */
331                 if (!in_bounds2(floor_ptr, y, x)) continue;
332
333                 /* Simply move to player */
334                 if (player_bold(target_ptr, y, x)) return FALSE;
335
336                 grid_type *g_ptr;
337                 g_ptr = &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 != target_ptr->riding) || target_ptr->pass_wall)) || ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding))))
343                 {
344                         if (cost == 0) continue;
345                         if (!can_open_door && is_closed_door(target_ptr, 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(target_ptr, y, x, target_ptr->y, target_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         if (best == 999) return FALSE;
364
365         return TRUE;
366 }
367
368
369 /*!
370  * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
371  * Choose the "best" direction for "flowing"
372  * @param m_idx モンスターの参照ID
373  * @param yp 移動先のマスのY座標を返す参照ポインタ
374  * @param xp 移動先のマスのX座標を返す参照ポインタ
375  * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
376  * @return 有効なマスがあった場合TRUEを返す
377  * @details
378  * Note that ghosts and rock-eaters are never allowed to "flow",\n
379  * since they should move directly towards the player.\n
380  *\n
381  * Prefer "non-diagonal" directions, but twiddle them a little\n
382  * to angle slightly towards the player's actual location.\n
383  *\n
384  * Allow very perceptive monsters to track old "spoor" left by\n
385  * previous locations occupied by the player.  This will tend\n
386  * to have monsters end up either near the player or on a grid\n
387  * recently occupied by the player (and left via "teleport").\n
388  *\n
389  * Note that if "smell" is turned on, all monsters get vicious.\n
390  *\n
391  * Also note that teleporting away from a location will cause\n
392  * the monsters who were chasing you to converge on that location\n
393  * as long as you are still near enough to "annoy" them without\n
394  * being close enough to chase directly.  I have no idea what will\n
395  * happen if you combine "smell" with low "aaf" values.\n
396  */
397 static bool get_moves_aux(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
398 {
399         grid_type *g_ptr;
400         floor_type *floor_ptr = target_ptr->current_floor_ptr;
401         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
402         monster_race *r_ptr = &r_info[m_ptr->r_idx];
403
404         /* Can monster cast attack spell? */
405         if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
406                 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
407                 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
408         {
409                 /* Can move spell castable grid? */
410                 if (get_moves_aux2(target_ptr, m_idx, yp, xp)) return TRUE;
411         }
412
413         /* Monster can't flow */
414         if (no_flow) return FALSE;
415
416         /* Monster can go through rocks */
417         if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return FALSE;
418         if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return FALSE;
419
420         /* Monster location */
421         POSITION y1 = m_ptr->fy;
422         POSITION x1 = m_ptr->fx;
423
424         /* Hack -- Player can see us, run towards him */
425         if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return FALSE;
426
427         /* Monster grid */
428         g_ptr = &floor_ptr->grid_array[y1][x1];
429
430         /* If we can hear noises, advance towards them */
431         int best;
432         bool use_scent = FALSE;
433         if (g_ptr->cost)
434         {
435                 best = 999;
436         }
437
438         /* Otherwise, try to follow a scent trail */
439         else if (g_ptr->when)
440         {
441                 /* Too old smell */
442                 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return FALSE;
443
444                 use_scent = TRUE;
445                 best = 0;
446         }
447
448         /* Otherwise, advance blindly */
449         else
450         {
451                 return FALSE;
452         }
453
454         /* Check nearby grids, diagonals first */
455         for (int i = 7; i >= 0; i--)
456         {
457                 POSITION y = y1 + ddy_ddd[i];
458                 POSITION x = x1 + ddx_ddd[i];
459
460                 /* Ignore locations off of edge */
461                 if (!in_bounds2(floor_ptr, y, x)) continue;
462
463                 g_ptr = &floor_ptr->grid_array[y][x];
464
465                 /* We're following a scent trail */
466                 if (use_scent)
467                 {
468                         int when = g_ptr->when;
469
470                         /* Accept younger scent */
471                         if (best > when) continue;
472                         best = when;
473                 }
474
475                 /* We're using sound */
476                 else
477                 {
478                         int cost;
479
480                         if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
481                                 cost = g_ptr->dist;
482                         else cost = g_ptr->cost;
483
484                         /* Accept louder sounds */
485                         if ((cost == 0) || (best < cost)) continue;
486                         best = cost;
487                 }
488
489                 /* Hack -- Save the "twiddled" location */
490                 (*yp) = target_ptr->y + 16 * ddy_ddd[i];
491                 (*xp) = target_ptr->x + 16 * ddx_ddd[i];
492         }
493
494         if (best == 999 || best == 0) return FALSE;
495
496         return TRUE;
497 }
498
499
500 /*!
501  * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
502  * Provide a location to flee to, but give the player a wide berth.
503  * @param m_idx モンスターの参照ID
504  * @param yp 移動先のマスのY座標を返す参照ポインタ
505  * @param xp 移動先のマスのX座標を返す参照ポインタ
506  * @return 有効なマスがあった場合TRUEを返す
507  * @details
508  * A monster may wish to flee to a location that is behind the player,\n
509  * but instead of heading directly for it, the monster should "swerve"\n
510  * around the player so that he has a smaller chance of getting hit.\n
511  */
512 static bool get_fear_moves_aux(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
513 {
514         POSITION gy = 0, gx = 0;
515
516         /* Monster location */
517         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
518         POSITION fy = m_ptr->fy;
519         POSITION fx = m_ptr->fx;
520
521         /* Desired destination */
522         POSITION y1 = fy - (*yp);
523         POSITION x1 = fx - (*xp);
524
525         /* Check nearby grids, diagonals first */
526         int score = -1;
527         for (int i = 7; i >= 0; i--)
528         {
529                 POSITION dis, s;
530                 POSITION y = fy + ddy_ddd[i];
531                 POSITION x = fx + ddx_ddd[i];
532
533                 /* Ignore locations off of edge */
534                 if (!in_bounds2(floor_ptr, y, x)) continue;
535
536                 /* Calculate distance of this grid from our destination */
537                 dis = distance(y, x, y1, x1);
538
539                 /* Score this grid */
540                 s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
541
542                 /* No negative scores */
543                 if (s < 0) s = 0;
544
545                 /* Ignore lower scores */
546                 if (s < score) continue;
547
548                 /* Save the score and time */
549                 score = s;
550
551                 /* Save the location */
552                 gy = y;
553                 gx = x;
554         }
555
556         /* No legal move (?) */
557         if (score == -1) return FALSE;
558
559         /* Find deltas */
560         (*yp) = fy - gy;
561         (*xp) = fx - gx;
562
563         /* Success */
564         return TRUE;
565 }
566
567 /*
568  * Hack -- Precompute a bunch of calls to distance() in find_safety() and
569  * find_hiding().
570  *
571  * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
572  * offsets of all the locations with a distance of n from a central point,
573  * with an offset of (0,0) indicating no more offsets at this distance.
574  *
575  * This is, of course, fairly unreadable, but it eliminates multiple loops
576  * from the previous version.
577  *
578  * It is probably better to replace these arrays with code to compute
579  * the relevant arrays, even if the storage is pre-allocated in hard
580  * coded sizes.  At the very least, code should be included which is
581  * able to generate and dump these arrays (ala "los()").
582  *
583  * Also, the storage needs could be halved by using bytes.
584  *
585  * These arrays could be combined into two big arrays, using sub-arrays
586  * to hold the offsets and lengths of each portion of the sub-arrays, and
587  * this could perhaps also be used somehow in the "look" code.
588  */
589
590
591 static POSITION d_off_y_0[] = { 0 };
592 static POSITION d_off_x_0[] = { 0 };
593
594 static POSITION d_off_y_1[] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
595 static POSITION d_off_x_1[] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
596
597 static POSITION d_off_y_2[] = { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
598 static POSITION d_off_x_2[] = { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
599
600 static POSITION d_off_y_3[] = { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2, 3, 3, 3, 0 };
601 static POSITION d_off_x_3[] = { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2, -1, 0, 1, 0 };
602
603 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 };
604 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 };
605
606
607 static POSITION d_off_y_5[] =
608 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
609   -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
610   5, 0 };
611
612 static POSITION d_off_x_5[] =
613 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
614   -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
615   0, 1, 0 };
616
617
618 static POSITION d_off_y_6[] =
619 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
620   -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
621   5, 5, 6, 6, 6, 0 };
622
623 static POSITION d_off_x_6[] =
624 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
625   0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
626   -3, 2, 3, -1, 0, 1, 0 };
627
628
629 static POSITION d_off_y_7[] =
630 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
631   -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
632   3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
633
634 static POSITION d_off_x_7[] =
635 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
636   -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
637   6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
638   1, 0 };
639
640
641 static POSITION d_off_y_8[] =
642 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
643   -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
644   2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
645   8, 8, 8, 0 };
646
647 static POSITION d_off_x_8[] =
648 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
649   5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
650   7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
651   2, 3, -1, 0, 1, 0 };
652
653
654 static POSITION d_off_y_9[] =
655 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
656   -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
657   0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
658   7, 8, 8, 8, 8, 9, 9, 9, 0 };
659
660 static POSITION d_off_x_9[] =
661 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
662   -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
663   9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
664   4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
665
666
667 static POSITION *dist_offsets_y[10] =
668 {
669         d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
670         d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
671 };
672
673 static POSITION *dist_offsets_x[10] =
674 {
675         d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
676         d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
677 };
678
679 /*!
680  * @brief モンスターが逃げ込める安全な地点を返す /
681  * Choose a "safe" location near a monster for it to run toward.
682  * @param target_ptr プレーヤーへの参照ポインタ
683  * @param m_idx モンスターの参照ID
684  * @param yp 移動先のマスのY座標を返す参照ポインタ
685  * @param xp 移動先のマスのX座標を返す参照ポインタ
686  * @return 有効なマスがあった場合TRUEを返す
687  * @details
688  * A location is "safe" if it can be reached quickly and the player\n
689  * is not able to fire into it (it isn't a "clean shot").  So, this will\n
690  * cause monsters to "duck" behind walls.  Hopefully, monsters will also\n
691  * try to run towards corridor openings if they are in a room.\n
692  *\n
693  * This function may take lots of CPU time if lots of monsters are\n
694  * fleeing.\n
695  *\n
696  * Return TRUE if a safe location is available.\n
697  */
698 static bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
699 {
700         floor_type *floor_ptr = target_ptr->current_floor_ptr;
701         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
702
703         POSITION fy = m_ptr->fy;
704         POSITION fx = m_ptr->fx;
705
706         POSITION gy = 0, gx = 0, gdis = 0;
707
708         /* Start with adjacent locations, spread further */
709         for (POSITION d = 1; d < 10; d++)
710         {
711                 /* Get the lists of points with a distance d from (fx, fy) */
712                 POSITION *y_offsets;
713                 y_offsets = dist_offsets_y[d];
714
715                 POSITION *x_offsets;
716                 x_offsets = dist_offsets_x[d];
717
718                 /* Check the locations */
719                 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
720                         dx != 0 || dy != 0;
721                         i++, dx = x_offsets[i], dy = y_offsets[i])
722                 {
723                         POSITION y = fy + dy;
724                         POSITION x = fx + dx;
725
726                         /* Skip illegal locations */
727                         if (!in_bounds(floor_ptr, y, x)) continue;
728
729                         grid_type *g_ptr;
730                         g_ptr = &floor_ptr->grid_array[y][x];
731
732                         /* Skip locations in a wall */
733                         if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == target_ptr->riding) ? CEM_RIDING : 0)) continue;
734
735                         /* Check for "availability" (if monsters can flow) */
736                         if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
737                         {
738                                 /* Ignore grids very far from the player */
739                                 if (g_ptr->dist == 0) continue;
740
741                                 /* Ignore too-distant grids */
742                                 if (g_ptr->dist > floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
743                         }
744
745                         /* Check for absence of shot (more or less) */
746                         if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x)) continue;
747
748                         /* Calculate distance from player */
749                         POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
750
751                         /* Remember if further than previous */
752                         if (dis <= gdis) continue;
753
754                         gy = y;
755                         gx = x;
756                         gdis = dis;
757                 }
758
759                 /* Check for success */
760                 if (gdis <= 0) continue;
761
762                 /* Good location */
763                 (*yp) = fy - gy;
764                 (*xp) = fx - gx;
765
766                 return TRUE;
767         }
768
769         return FALSE;
770 }
771
772
773 /*!
774  * @brief モンスターが隠れ潜める地点を返す /
775  * Choose a good hiding place near a monster for it to run toward.
776  * @param target_ptr プレーヤーへの参照ポインタ
777  * @param m_idx モンスターの参照ID
778  * @param yp 移動先のマスのY座標を返す参照ポインタ
779  * @param xp 移動先のマスのX座標を返す参照ポインタ
780  * @return 有効なマスがあった場合TRUEを返す
781  * @details
782  * Pack monsters will use this to "ambush" the player and lure him out\n
783  * of corridors into open space so they can swarm him.\n
784  *\n
785  * Return TRUE if a good location is available.\n
786  */
787 static bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
788 {
789         floor_type *floor_ptr = target_ptr->current_floor_ptr;
790         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
791         monster_race *r_ptr = &r_info[m_ptr->r_idx];
792
793         POSITION fy = m_ptr->fy;
794         POSITION fx = m_ptr->fx;
795
796         POSITION gy = 0, gx = 0, gdis = 999;
797
798         /* Start with adjacent locations, spread further */
799         for (POSITION d = 1; d < 10; d++)
800         {
801                 /* Get the lists of points with a distance d from (fx, fy) */
802                 POSITION *y_offsets;
803                 y_offsets = dist_offsets_y[d];
804
805                 POSITION *x_offsets;
806                 x_offsets = dist_offsets_x[d];
807
808                 /* Check the locations */
809                 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
810                         dx != 0 || dy != 0;
811                         i++, dx = x_offsets[i], dy = y_offsets[i])
812                 {
813                         POSITION y = fy + dy;
814                         POSITION x = fx + dx;
815
816                         /* Skip illegal locations */
817                         if (!in_bounds(floor_ptr, y, x)) continue;
818
819                         /* Skip occupied locations */
820                         if (!monster_can_enter(target_ptr, y, x, r_ptr, 0)) continue;
821
822                         /* Check for hidden, available grid */
823                         if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x) && clean_shot(target_ptr, fy, fx, y, x, FALSE))
824                                 continue;
825
826                         /* Calculate distance from player */
827                         POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
828
829                         /* Remember if closer than previous */
830                         if (dis < gdis && dis >= 2)
831                         {
832                                 gy = y;
833                                 gx = x;
834                                 gdis = dis;
835                         }
836                 }
837
838                 if (gdis >= 999) continue;
839
840                 *yp = fy - gy;
841                 *xp = fx - gx;
842
843                 return TRUE;
844         }
845
846         return FALSE;
847 }
848
849
850 /*!
851  * @brief モンスターの移動方向を返す /
852  * Choose "logical" directions for monster movement
853  * @param target_ptr プレーヤーへの参照ポインタ
854  * @param m_idx モンスターの参照ID
855  * @param mm 移動方向を返す方向IDの参照ポインタ
856  * @return 有効方向があった場合TRUEを返す
857  */
858 static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
859 {
860         floor_type *floor_ptr = target_ptr->current_floor_ptr;
861         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
862         monster_race *r_ptr = &r_info[m_ptr->r_idx];
863         POSITION y = 0, ay, x = 0, ax;
864         int move_val = 0;
865         POSITION y2 = target_ptr->y;
866         POSITION x2 = target_ptr->x;
867         bool done = FALSE;
868         bool will_run = mon_will_run(target_ptr, m_idx);
869         grid_type *g_ptr;
870         bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
871         bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall));
872
873         /* Counter attack to an enemy monster */
874         if (!will_run && m_ptr->target_y)
875         {
876                 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
877
878                 /* The monster must be an enemy, and in LOS */
879                 if (t_m_idx &&
880                         are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) &&
881                         los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
882                         projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
883                 {
884                         /* Extract the "pseudo-direction" */
885                         y = m_ptr->fy - m_ptr->target_y;
886                         x = m_ptr->fx - m_ptr->target_x;
887                         done = TRUE;
888                 }
889         }
890
891         if (!done && !will_run && is_hostile(m_ptr) &&
892                 (r_ptr->flags1 & RF1_FRIENDS) &&
893                 ((los(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x) && projectable(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x)) ||
894                 (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
895         {
896                 /*
897                  * Animal packs try to get the player out of corridors
898                  * (...unless they can move through walls -- TY)
899                  */
900                 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
901                         !(r_ptr->flags2 & RF2_KILL_WALL))
902                 {
903                         int i, room = 0;
904
905                         /* Count room grids next to player */
906                         for (i = 0; i < 8; i++)
907                         {
908                                 int xx = target_ptr->x + ddx_ddd[i];
909                                 int yy = target_ptr->y + ddy_ddd[i];
910
911                                 if (!in_bounds2(floor_ptr, yy, xx)) continue;
912
913                                 g_ptr = &floor_ptr->grid_array[yy][xx];
914
915                                 /* Check grid */
916                                 if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0))
917                                 {
918                                         /* One more room grid */
919                                         room++;
920                                 }
921                         }
922                         if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
923                         if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
924
925                         /* Not in a room and strong player */
926                         if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
927                                 (target_ptr->mhp + target_ptr->msp))
928                         {
929                                 /* Find hiding place */
930                                 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
931                         }
932                 }
933
934                 /* Monster groups try to surround the player */
935                 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
936                 {
937                         int i;
938
939                         /* Find an empty square near the player to fill */
940                         for (i = 0; i < 8; i++)
941                         {
942                                 /* Pick squares near player (semi-randomly) */
943                                 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
944                                 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
945
946                                 /* Already there? */
947                                 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
948                                 {
949                                         /* Attack the player */
950                                         y2 = target_ptr->y;
951                                         x2 = target_ptr->x;
952
953                                         break;
954                                 }
955
956                                 if (!in_bounds2(floor_ptr, y2, x2)) continue;
957
958                                 /* Ignore filled grids */
959                                 if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0)) continue;
960
961                                 /* Try to fill this hole */
962                                 break;
963                         }
964
965                         /* Extract the new "pseudo-direction" */
966                         y = m_ptr->fy - y2;
967                         x = m_ptr->fx - x2;
968
969                         done = TRUE;
970                 }
971         }
972
973         if (!done)
974         {
975                 /* Flow towards the player */
976                 (void)get_moves_aux(target_ptr, m_idx, &y2, &x2, no_flow);
977
978                 /* Extract the "pseudo-direction" */
979                 y = m_ptr->fy - y2;
980                 x = m_ptr->fx - x2;
981
982                 /* Not done */
983         }
984
985         /* Apply fear if possible and necessary */
986         if (is_pet(m_ptr) && will_run)
987         {
988                 /* XXX XXX Not very "smart" */
989                 y = (-y), x = (-x);
990         }
991         else
992         {
993                 if (!done && will_run)
994                 {
995                         int tmp_x = (-x);
996                         int tmp_y = (-y);
997
998                         /* Try to find safe place */
999                         if (find_safety(target_ptr, m_idx, &y, &x))
1000                         {
1001                                 /* Attempt to avoid the player */
1002                                 if (!no_flow)
1003                                 {
1004                                         /* Adjust movement */
1005                                         if (get_fear_moves_aux(target_ptr->current_floor_ptr, m_idx, &y, &x)) done = TRUE;
1006                                 }
1007                         }
1008
1009                         if (!done)
1010                         {
1011                                 /* This is not a very "smart" method XXX XXX */
1012                                 y = tmp_y;
1013                                 x = tmp_x;
1014                         }
1015                 }
1016         }
1017
1018
1019         /* Check for no move */
1020         if (!x && !y) return FALSE;
1021
1022
1023         /* Extract the "absolute distances" */
1024         ax = ABS(x);
1025         ay = ABS(y);
1026
1027         /* Do something weird */
1028         if (y < 0) move_val += 8;
1029         if (x > 0) move_val += 4;
1030
1031         /* Prevent the diamond maneuvre */
1032         if (ay > (ax << 1)) move_val += 2;
1033         else if (ax > (ay << 1)) move_val++;
1034
1035         /* Extract some directions */
1036         switch (move_val)
1037         {
1038         case 0:
1039                 mm[0] = 9;
1040                 if (ay > ax)
1041                 {
1042                         mm[1] = 8;
1043                         mm[2] = 6;
1044                         mm[3] = 7;
1045                         mm[4] = 3;
1046                 }
1047                 else
1048                 {
1049                         mm[1] = 6;
1050                         mm[2] = 8;
1051                         mm[3] = 3;
1052                         mm[4] = 7;
1053                 }
1054                 break;
1055         case 1:
1056         case 9:
1057                 mm[0] = 6;
1058                 if (y < 0)
1059                 {
1060                         mm[1] = 3;
1061                         mm[2] = 9;
1062                         mm[3] = 2;
1063                         mm[4] = 8;
1064                 }
1065                 else
1066                 {
1067                         mm[1] = 9;
1068                         mm[2] = 3;
1069                         mm[3] = 8;
1070                         mm[4] = 2;
1071                 }
1072                 break;
1073         case 2:
1074         case 6:
1075                 mm[0] = 8;
1076                 if (x < 0)
1077                 {
1078                         mm[1] = 9;
1079                         mm[2] = 7;
1080                         mm[3] = 6;
1081                         mm[4] = 4;
1082                 }
1083                 else
1084                 {
1085                         mm[1] = 7;
1086                         mm[2] = 9;
1087                         mm[3] = 4;
1088                         mm[4] = 6;
1089                 }
1090                 break;
1091         case 4:
1092                 mm[0] = 7;
1093                 if (ay > ax)
1094                 {
1095                         mm[1] = 8;
1096                         mm[2] = 4;
1097                         mm[3] = 9;
1098                         mm[4] = 1;
1099                 }
1100                 else
1101                 {
1102                         mm[1] = 4;
1103                         mm[2] = 8;
1104                         mm[3] = 1;
1105                         mm[4] = 9;
1106                 }
1107                 break;
1108         case 5:
1109         case 13:
1110                 mm[0] = 4;
1111                 if (y < 0)
1112                 {
1113                         mm[1] = 1;
1114                         mm[2] = 7;
1115                         mm[3] = 2;
1116                         mm[4] = 8;
1117                 }
1118                 else
1119                 {
1120                         mm[1] = 7;
1121                         mm[2] = 1;
1122                         mm[3] = 8;
1123                         mm[4] = 2;
1124                 }
1125                 break;
1126         case 8:
1127                 mm[0] = 3;
1128                 if (ay > ax)
1129                 {
1130                         mm[1] = 2;
1131                         mm[2] = 6;
1132                         mm[3] = 1;
1133                         mm[4] = 9;
1134                 }
1135                 else
1136                 {
1137                         mm[1] = 6;
1138                         mm[2] = 2;
1139                         mm[3] = 9;
1140                         mm[4] = 1;
1141                 }
1142                 break;
1143         case 10:
1144         case 14:
1145                 mm[0] = 2;
1146                 if (x < 0)
1147                 {
1148                         mm[1] = 3;
1149                         mm[2] = 1;
1150                         mm[3] = 6;
1151                         mm[4] = 4;
1152                 }
1153                 else
1154                 {
1155                         mm[1] = 1;
1156                         mm[2] = 3;
1157                         mm[3] = 4;
1158                         mm[4] = 6;
1159                 }
1160                 break;
1161         case 12:
1162                 mm[0] = 1;
1163                 if (ay > ax)
1164                 {
1165                         mm[1] = 2;
1166                         mm[2] = 4;
1167                         mm[3] = 3;
1168                         mm[4] = 7;
1169                 }
1170                 else
1171                 {
1172                         mm[1] = 4;
1173                         mm[2] = 2;
1174                         mm[3] = 7;
1175                         mm[4] = 3;
1176                 }
1177                 break;
1178         }
1179
1180         /* Wants to move... */
1181         return TRUE;
1182 }
1183
1184
1185 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
1186 {
1187         return !have_flag(f_ptr->flags, FF_GLASS) ||
1188                 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
1189                 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
1190 }
1191
1192
1193 /*!
1194  * @brief モンスター単体の1ターン行動処理メインルーチン /
1195  * Process a monster
1196  * @param target_ptr プレーヤーへの参照ポインタ
1197  * @param m_idx 行動モンスターの参照ID
1198  * @return なし
1199  * @details
1200  * The monster is known to be within 100 grids of the player\n
1201  *\n
1202  * In several cases, we directly update the monster lore\n
1203  *\n
1204  * Note that a monster is only allowed to "reproduce" if there\n
1205  * are a limited number of "reproducing" monsters on the current\n
1206  * level.  This should prevent the level from being "swamped" by\n
1207  * reproducing monsters.  It also allows a large mass of mice to\n
1208  * prevent a louse from multiplying, but this is a small price to\n
1209  * pay for a simple multiplication method.\n
1210  *\n
1211  * XXX Monster fear is slightly odd, in particular, monsters will\n
1212  * fixate on opening a door even if they cannot open it.  Actually,\n
1213  * the same thing happens to normal monsters when they hit a door\n
1214  *\n
1215  * In addition, monsters which *cannot* open or bash\n
1216  * down a door will still stand there trying to open it...\n
1217  *\n
1218  * XXX Technically, need to check for monster in the way\n
1219  * combined with that monster being in a wall (or door?)\n
1220  *\n
1221  * A "direction" of "5" means "pick a random direction".\n
1222  */
1223 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
1224 {
1225         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1226         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1227         monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1228
1229         int i, d;
1230         DIRECTION mm[8];
1231
1232         grid_type *g_ptr;
1233         feature_type *f_ptr;
1234         monster_type *y_ptr;
1235
1236         bool do_turn;
1237         bool do_move;
1238         bool do_view;
1239         bool must_alter_to_move;
1240
1241         bool did_open_door;
1242         bool did_bash_door;
1243         bool did_take_item;
1244         bool did_kill_item;
1245         bool did_move_body;
1246         bool did_pass_wall;
1247         bool did_kill_wall;
1248         bool can_cross;
1249         bool aware = TRUE;
1250
1251         bool fear, dead;
1252         bool is_riding_mon = (m_idx == target_ptr->riding);
1253         bool see_m = is_seen(m_ptr);
1254
1255         if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
1256         {
1257                 if (rakuba(target_ptr, 0, TRUE))
1258                 {
1259 #ifdef JP
1260                         msg_print("地面に落とされた。");
1261 #else
1262                         GAME_TEXT m_name[MAX_NLEN];
1263                         monster_desc(target_ptr, m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
1264                         msg_format("You have fallen from %s.", m_name);
1265 #endif
1266                 }
1267         }
1268
1269         if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
1270         {
1271                 choose_new_monster(target_ptr, m_idx, FALSE, 0);
1272                 r_ptr = &r_info[m_ptr->r_idx];
1273         }
1274
1275         /* Players hidden in shadow are almost imperceptable. -LM- */
1276         if (target_ptr->special_defense & NINJA_S_STEALTH)
1277         {
1278                 int tmp = target_ptr->lev * 6 + (target_ptr->skill_stl + 10) * 4;
1279                 if (target_ptr->monlite) tmp /= 3;
1280                 if (target_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1281                 if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10)) tmp /= 3;
1282                 /* Low-level monsters will find it difficult to locate the player. */
1283                 if (randint0(tmp) > (r_ptr->level + 20)) aware = FALSE;
1284         }
1285
1286         if (vanish_summoned_children(target_ptr, m_idx, see_m)) return;
1287         if (process_quantum_effect(target_ptr,m_idx, see_m)) return;
1288
1289         if (m_ptr->r_idx == MON_SHURYUUDAN)
1290         {
1291                 mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1292                 if (dead) return;
1293         }
1294
1295         if (runaway_monster(target_ptr, m_idx, is_riding_mon, see_m)) return;
1296
1297         awake_monster(target_ptr, m_idx);
1298
1299         /* Handle "stun" */
1300         if (MON_STUNNED(m_ptr))
1301         {
1302                 /* Sometimes skip move */
1303                 if (one_in_(2)) return;
1304         }
1305
1306         if (is_riding_mon)
1307         {
1308                 target_ptr->update |= (PU_BONUS);
1309         }
1310
1311         /* No one wants to be your friend if you're aggravating */
1312         bool gets_angry = FALSE;
1313         if (is_friendly(m_ptr) && (target_ptr->cursed & TRC_AGGRAVATE))
1314                 gets_angry = TRUE;
1315
1316         /* Paranoia... no pet uniques outside wizard mode -- TY */
1317         if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
1318                 monster_has_hostile_align(target_ptr, NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
1319         {
1320                 gets_angry = TRUE;
1321         }
1322
1323         if (target_ptr->phase_out) gets_angry = FALSE;
1324
1325         if (gets_angry)
1326         {
1327                 if (is_pet(m_ptr) || see_m)
1328                 {
1329                         GAME_TEXT m_name[MAX_NLEN];
1330                         monster_desc(target_ptr, m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
1331                         msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
1332                 }
1333
1334                 set_hostile(target_ptr, m_ptr);
1335         }
1336
1337         /* Get the origin */
1338         POSITION oy = m_ptr->fy;
1339         POSITION ox = m_ptr->fx;
1340
1341         if (decide_monster_multiplication(target_ptr, m_idx, oy, ox)) return;
1342
1343         process_special(target_ptr, m_idx);
1344         process_speak_sound(target_ptr, m_idx, oy, ox, aware);
1345         cast_spell(target_ptr, m_idx, aware);
1346
1347         /* Hack -- Assume no movement */
1348         mm[0] = mm[1] = mm[2] = mm[3] = 0;
1349         mm[4] = mm[5] = mm[6] = mm[7] = 0;
1350
1351         if (!decide_monster_movement_direction(target_ptr, mm, m_idx, aware)) return;
1352
1353         /* Assume nothing */
1354         do_turn = FALSE;
1355         do_move = FALSE;
1356         do_view = FALSE;
1357         must_alter_to_move = FALSE;
1358
1359         /* Assume nothing */
1360         did_open_door = FALSE;
1361         did_bash_door = FALSE;
1362         did_take_item = FALSE;
1363         did_kill_item = FALSE;
1364         did_move_body = FALSE;
1365         did_pass_wall = FALSE;
1366         did_kill_wall = FALSE;
1367
1368         /* Take a zero-terminated array of "directions" */
1369         for (i = 0; mm[i]; i++)
1370         {
1371                 /* Get the direction */
1372                 d = mm[i];
1373
1374                 /* Hack -- allow "randomized" motion */
1375                 if (d == 5) d = ddd[randint0(8)];
1376
1377                 /* Get the destination */
1378                 POSITION ny = oy + ddy[d];
1379                 POSITION nx = ox + ddx[d];
1380
1381                 /* Ignore locations off of edge */
1382                 if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx)) continue;
1383
1384                 /* Access that grid */
1385                 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1386                 f_ptr = &f_info[g_ptr->feat];
1387                 can_cross = monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
1388
1389                 /* Access that grid's contents */
1390                 y_ptr = &target_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
1391
1392                 /* Hack -- player 'in' wall */
1393                 if (player_bold(target_ptr, ny, nx))
1394                 {
1395                         do_move = TRUE;
1396                 }
1397
1398                 /* Possibly a monster to attack */
1399                 else if (g_ptr->m_idx)
1400                 {
1401                         do_move = TRUE;
1402                 }
1403
1404                 /* Monster destroys walls (and doors) */
1405                 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
1406                         (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
1407                         have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
1408                         check_hp_for_feat_destruction(f_ptr, m_ptr))
1409                 {
1410                         /* Eat through walls/doors/rubble */
1411                         do_move = TRUE;
1412                         if (!can_cross) must_alter_to_move = TRUE;
1413
1414                         /* Monster destroyed a wall (later) */
1415                         did_kill_wall = TRUE;
1416                 }
1417
1418                 /* Floor is open? */
1419                 else if (can_cross)
1420                 {
1421                         /* Go ahead and move */
1422                         do_move = TRUE;
1423
1424                         /* Monster moves through walls (and doors) */
1425                         if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || target_ptr->pass_wall) &&
1426                                 have_flag(f_ptr->flags, FF_CAN_PASS))
1427                         {
1428                                 /* Monster went through a wall */
1429                                 did_pass_wall = TRUE;
1430                         }
1431                 }
1432
1433                 /* Handle doors and secret doors */
1434                 else if (is_closed_door(target_ptr, g_ptr->feat))
1435                 {
1436                         bool may_bash = TRUE;
1437
1438                         /* Assume no move allowed */
1439                         do_move = FALSE;
1440
1441                         /* Creature can open doors. */
1442                         if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
1443                                 (!is_pet(m_ptr) || (target_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1444                         {
1445                                 /* Closed doors */
1446                                 if (!f_ptr->power)
1447                                 {
1448                                         /* The door is open */
1449                                         did_open_door = TRUE;
1450
1451                                         /* Do not bash the door */
1452                                         may_bash = FALSE;
1453
1454                                         do_turn = TRUE;
1455                                 }
1456
1457                                 /* Locked doors (not jammed) */
1458                                 else
1459                                 {
1460                                         /* Try to unlock it */
1461                                         if (randint0(m_ptr->hp / 10) > f_ptr->power)
1462                                         {
1463                                                 /* Unlock the door */
1464                                                 cave_alter_feat(target_ptr, ny, nx, FF_DISARM);
1465
1466                                                 /* Do not bash the door */
1467                                                 may_bash = FALSE;
1468
1469                                                 do_turn = TRUE;
1470                                         }
1471                                 }
1472                         }
1473
1474                         /* Stuck doors -- attempt to bash them down if allowed */
1475                         if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
1476                                 (!is_pet(m_ptr) || (target_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1477                         {
1478                                 /* Attempt to Bash */
1479                                 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
1480                                 {
1481                                         if (have_flag(f_ptr->flags, FF_GLASS))
1482                                                 msg_print(_("ガラスが砕ける音がした!", "You hear glass breaking!"));
1483                                         else
1484                                                 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
1485
1486                                         /* Disturb (sometimes) */
1487                                         if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1488
1489                                         /* The door was bashed open */
1490                                         did_bash_door = TRUE;
1491
1492                                         /* Hack -- fall into doorway */
1493                                         do_move = TRUE;
1494                                         must_alter_to_move = TRUE;
1495                                 }
1496                         }
1497
1498
1499                         /* Deal with doors in the way */
1500                         if (did_open_door || did_bash_door)
1501                         {
1502                                 /* Break down the door */
1503                                 if (did_bash_door && ((randint0(100) < 50) || (feat_state(target_ptr, g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
1504                                 {
1505                                         cave_alter_feat(target_ptr, ny, nx, FF_BASH);
1506
1507                                         if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
1508                                         {
1509                                                 target_ptr->update |= (PU_FLOW);
1510                                                 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1511                                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1512
1513                                                 return;
1514                                         }
1515                                 }
1516
1517                                 /* Open the door */
1518                                 else
1519                                 {
1520                                         cave_alter_feat(target_ptr, ny, nx, FF_OPEN);
1521                                 }
1522
1523                                 f_ptr = &f_info[g_ptr->feat];
1524
1525                                 /* Handle viewable doors */
1526                                 do_view = TRUE;
1527                         }
1528                 }
1529
1530                 /* Hack -- check for Glyph of Warding */
1531                 if (do_move && is_glyph_grid(g_ptr) &&
1532                         !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(target_ptr, ny, nx)))
1533                 {
1534                         /* Assume no move allowed */
1535                         do_move = FALSE;
1536
1537                         /* Break the ward */
1538                         if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
1539                         {
1540                                 /* Describe observable breakage */
1541                                 if (g_ptr->info & CAVE_MARK)
1542                                 {
1543                                         msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
1544                                 }
1545
1546                                 /* Forget the rune */
1547                                 g_ptr->info &= ~(CAVE_MARK);
1548
1549                                 /* Break the rune */
1550                                 g_ptr->info &= ~(CAVE_OBJECT);
1551                                 g_ptr->mimic = 0;
1552
1553                                 /* Allow movement */
1554                                 do_move = TRUE;
1555
1556                                 note_spot(target_ptr, ny, nx);
1557                         }
1558                 }
1559                 else if (do_move && is_explosive_rune_grid(g_ptr) &&
1560                         !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(target_ptr, ny, nx)))
1561                 {
1562                         /* Assume no move allowed */
1563                         do_move = FALSE;
1564
1565                         /* Break the ward */
1566                         if (!is_pet(m_ptr))
1567                         {
1568                                 /* Break the ward */
1569                                 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
1570                                 {
1571                                         /* Describe observable breakage */
1572                                         if (g_ptr->info & CAVE_MARK)
1573                                         {
1574                                                 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
1575                                                 project(target_ptr, 0, 2, ny, nx, 2 * (target_ptr->lev + damroll(7, 7)), GF_MANA, (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
1576                                         }
1577                                 }
1578                                 else
1579                                 {
1580                                         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
1581                                 }
1582
1583                                 /* Forget the rune */
1584                                 g_ptr->info &= ~(CAVE_MARK);
1585
1586                                 /* Break the rune */
1587                                 g_ptr->info &= ~(CAVE_OBJECT);
1588                                 g_ptr->mimic = 0;
1589
1590                                 note_spot(target_ptr, ny, nx);
1591                                 lite_spot(target_ptr, ny, nx);
1592
1593                                 if (!monster_is_valid(m_ptr)) return;
1594                                 /* Allow movement */
1595                                 do_move = TRUE;
1596                         }
1597                 }
1598
1599                 /* The player is in the way */
1600                 if (do_move && player_bold(target_ptr, ny, nx))
1601                 {
1602                         /* Some monsters never attack */
1603                         if (r_ptr->flags1 & RF1_NEVER_BLOW)
1604                         {
1605                                 /* Hack -- memorize lack of attacks */
1606                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
1607
1608                                 /* Do not move */
1609                                 do_move = FALSE;
1610                         }
1611
1612                         /* In anti-melee dungeon, stupid or confused monster takes useless turn */
1613                         if (do_move && (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE))
1614                         {
1615                                 if (!MON_CONFUSED(m_ptr))
1616                                 {
1617                                         if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
1618                                         else
1619                                         {
1620                                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
1621                                         }
1622                                 }
1623                         }
1624
1625                         /* The player is in the way.  Attack him. */
1626                         if (do_move)
1627                         {
1628                                 if (!target_ptr->riding || one_in_(2))
1629                                 {
1630                                         /* Do the attack */
1631                                         (void)make_attack_normal(target_ptr, m_idx);
1632
1633                                         /* Do not move */
1634                                         do_move = FALSE;
1635
1636                                         /* Took a turn */
1637                                         do_turn = TRUE;
1638                                 }
1639                         }
1640                 }
1641
1642                 /* A monster is in the way */
1643                 if (do_move && g_ptr->m_idx)
1644                 {
1645                         monster_race *z_ptr = &r_info[y_ptr->r_idx];
1646
1647                         /* Assume no movement */
1648                         do_move = FALSE;
1649
1650                         /* Attack 'enemies' */
1651                         if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
1652                                 (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
1653                                 can_cross && (g_ptr->m_idx != target_ptr->riding)) ||
1654                                 are_enemies(target_ptr, m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
1655                         {
1656                                 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
1657                                 {
1658                                         if (r_ptr->flags2 & RF2_KILL_BODY)
1659                                         {
1660                                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
1661                                         }
1662
1663                                         /* attack */
1664                                         if (y_ptr->r_idx && (y_ptr->hp >= 0))
1665                                         {
1666                                                 if (monst_attack_monst(target_ptr, m_idx, g_ptr->m_idx)) return;
1667
1668                                                 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
1669                                                 else if (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
1670                                                 {
1671                                                         if (MON_CONFUSED(m_ptr)) return;
1672                                                         else if (r_ptr->flags2 & RF2_STUPID)
1673                                                         {
1674                                                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
1675                                                                 return;
1676                                                         }
1677                                                 }
1678                                         }
1679                                 }
1680                         }
1681
1682                         /* Push past weaker monsters (unless leaving a wall) */
1683                         else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
1684                                 (r_ptr->mexp > z_ptr->mexp) &&
1685                                 can_cross && (g_ptr->m_idx != target_ptr->riding) &&
1686                                 monster_can_cross_terrain(target_ptr, target_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
1687                         {
1688                                 /* Allow movement */
1689                                 do_move = TRUE;
1690
1691                                 /* Monster pushed past another monster */
1692                                 did_move_body = TRUE;
1693
1694                                 /* Wake up the moved monster */
1695                                 (void)set_monster_csleep(target_ptr, g_ptr->m_idx, 0);
1696
1697                                 /* Message */
1698                         }
1699                 }
1700
1701                 if (is_riding_mon)
1702                 {
1703                         if (!target_ptr->riding_ryoute && !MON_MONFEAR(&target_ptr->current_floor_ptr->m_list[target_ptr->riding])) do_move = FALSE;
1704                 }
1705
1706                 if (did_kill_wall && do_move)
1707                 {
1708                         if (one_in_(GRINDNOISE))
1709                         {
1710                                 if (have_flag(f_ptr->flags, FF_GLASS))
1711                                         msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
1712                                 else
1713                                         msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
1714                         }
1715
1716                         cave_alter_feat(target_ptr, ny, nx, FF_HURT_DISI);
1717
1718                         if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
1719                         {
1720                                 target_ptr->update |= (PU_FLOW);
1721                                 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1722                                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
1723
1724                                 return;
1725                         }
1726
1727                         f_ptr = &f_info[g_ptr->feat];
1728
1729                         /* Note changes to viewable region */
1730                         do_view = TRUE;
1731                         do_turn = TRUE;
1732                 }
1733
1734                 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
1735                 {
1736                         if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
1737                         {
1738                                 /* Assume no move allowed */
1739                                 do_move = FALSE;
1740                         }
1741                 }
1742
1743                 /*
1744                  * Check if monster can cross terrain
1745                  * This is checked after the normal attacks
1746                  * to allow monsters to attack an enemy,
1747                  * even if it can't enter the terrain.
1748                  */
1749                 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
1750                 {
1751                         /* Assume no move allowed */
1752                         do_move = FALSE;
1753                 }
1754
1755                 /* Some monsters never move */
1756                 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
1757                 {
1758                         /* Hack -- memorize lack of moves */
1759                         if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
1760
1761                         /* Do not move */
1762                         do_move = FALSE;
1763                 }
1764
1765                 /* Creature has been allowed move */
1766                 if (!do_move)
1767                 {
1768                         if (do_turn) break;
1769                         continue;
1770                 }
1771
1772                 do_turn = TRUE;
1773
1774                 if (have_flag(f_ptr->flags, FF_TREE))
1775                 {
1776                         if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
1777                         {
1778                                 m_ptr->energy_need += ENERGY_NEED();
1779                         }
1780                 }
1781
1782                 if (!is_riding_mon)
1783                 {
1784                         /* Hack -- Update the old location */
1785                         target_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
1786
1787                         /* Mega-Hack -- move the old monster, if any */
1788                         if (g_ptr->m_idx)
1789                         {
1790                                 /* Move the old monster */
1791                                 y_ptr->fy = oy;
1792                                 y_ptr->fx = ox;
1793
1794                                 /* Update the old monster */
1795                                 update_monster(target_ptr, g_ptr->m_idx, TRUE);
1796                         }
1797
1798                         /* Hack -- Update the new location */
1799                         g_ptr->m_idx = m_idx;
1800
1801                         /* Move the monster */
1802                         m_ptr->fy = ny;
1803                         m_ptr->fx = nx;
1804                         update_monster(target_ptr, m_idx, TRUE);
1805
1806                         lite_spot(target_ptr, oy, ox);
1807                         lite_spot(target_ptr, ny, nx);
1808                 }
1809                 else
1810                 {
1811                         /* sound(SOUND_WALK); */
1812                         if (!move_player_effect(target_ptr, ny, nx, MPE_DONT_PICKUP)) break;
1813                 }
1814
1815                 /* Possible disturb */
1816                 if (m_ptr->ml &&
1817                         (disturb_move ||
1818                         (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
1819                                 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
1820                 {
1821                         if (is_hostile(m_ptr))
1822                                 disturb(target_ptr, FALSE, TRUE);
1823                 }
1824
1825                 /* Take or Kill objects on the floor */
1826                 bool is_takable_or_killable = g_ptr->o_idx > 0;
1827                 is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) != 0;
1828                 bool is_pickup_items = (target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) != 0;
1829                 is_pickup_items &= (r_ptr->flags2 & RF2_TAKE_ITEM) != 0;
1830                 is_takable_or_killable &= !is_pet(m_ptr) || is_pickup_items;
1831                 if (!is_takable_or_killable)
1832                 {
1833                         if (do_turn) break;
1834                         continue;
1835                 }
1836
1837                 OBJECT_IDX this_o_idx, next_o_idx;
1838                 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
1839
1840                 /* Scan all objects in the grid */
1841                 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1842                 {
1843                         BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
1844                         GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
1845                         object_type *o_ptr = &target_ptr->current_floor_ptr->o_list[this_o_idx];
1846                         next_o_idx = o_ptr->next_o_idx;
1847
1848                         if (do_take)
1849                         {
1850                                 /* Skip gold */
1851                                 if (o_ptr->tval == TV_GOLD) continue;
1852
1853                                 /*
1854                                  * Skip "real" corpses and statues, to avoid extreme
1855                                  * silliness like a novice rogue pockets full of statues
1856                                  * and corpses.
1857                                  */
1858                                 if ((o_ptr->tval == TV_CORPSE) ||
1859                                         (o_ptr->tval == TV_STATUE)) continue;
1860                         }
1861
1862                         /* Extract some flags */
1863                         object_flags(o_ptr, flgs);
1864
1865                         /* Acquire the object name */
1866                         object_desc(target_ptr, o_name, o_ptr, 0);
1867                         monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_HIDDEN);
1868
1869                         /* React to objects that hurt the monster */
1870                         if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
1871                         if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
1872                         if (have_flag(flgs, TR_SLAY_TROLL))  flg3 |= (RF3_TROLL);
1873                         if (have_flag(flgs, TR_KILL_TROLL))  flg3 |= (RF3_TROLL);
1874                         if (have_flag(flgs, TR_SLAY_GIANT))  flg3 |= (RF3_GIANT);
1875                         if (have_flag(flgs, TR_KILL_GIANT))  flg3 |= (RF3_GIANT);
1876                         if (have_flag(flgs, TR_SLAY_ORC))    flg3 |= (RF3_ORC);
1877                         if (have_flag(flgs, TR_KILL_ORC))    flg3 |= (RF3_ORC);
1878                         if (have_flag(flgs, TR_SLAY_DEMON))  flg3 |= (RF3_DEMON);
1879                         if (have_flag(flgs, TR_KILL_DEMON))  flg3 |= (RF3_DEMON);
1880                         if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
1881                         if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
1882                         if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
1883                         if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
1884                         if (have_flag(flgs, TR_SLAY_EVIL))   flg3 |= (RF3_EVIL);
1885                         if (have_flag(flgs, TR_KILL_EVIL))   flg3 |= (RF3_EVIL);
1886                         if (have_flag(flgs, TR_SLAY_HUMAN))  flg2 |= (RF2_HUMAN);
1887                         if (have_flag(flgs, TR_KILL_HUMAN))  flg2 |= (RF2_HUMAN);
1888                         if (have_flag(flgs, TR_BRAND_ACID))  flgr |= (RFR_IM_ACID);
1889                         if (have_flag(flgs, TR_BRAND_ELEC))  flgr |= (RFR_IM_ELEC);
1890                         if (have_flag(flgs, TR_BRAND_FIRE))  flgr |= (RFR_IM_FIRE);
1891                         if (have_flag(flgs, TR_BRAND_COLD))  flgr |= (RFR_IM_COLD);
1892                         if (have_flag(flgs, TR_BRAND_POIS))  flgr |= (RFR_IM_POIS);
1893
1894                         /* The object cannot be picked up by the monster */
1895                         if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
1896                                 ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
1897                         {
1898                                 /* Only give a message for "take_item" */
1899                                 if (do_take && (r_ptr->flags2 & RF2_STUPID))
1900                                 {
1901                                         did_take_item = TRUE;
1902
1903                                         /* Describe observable situations */
1904                                         if (m_ptr->ml && player_can_see_bold(target_ptr, ny, nx))
1905                                         {
1906                                                 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
1907                                         }
1908                                 }
1909                         }
1910
1911                         /* Pick up the item */
1912                         else if (do_take)
1913                         {
1914                                 did_take_item = TRUE;
1915
1916                                 /* Describe observable situations */
1917                                 if (player_can_see_bold(target_ptr, ny, nx))
1918                                 {
1919                                         msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
1920                                 }
1921
1922                                 /* Excise the object */
1923                                 excise_object_idx(target_ptr->current_floor_ptr, this_o_idx);
1924
1925                                 /* Forget mark */
1926                                 o_ptr->marked &= OM_TOUCHED;
1927
1928                                 /* Forget location */
1929                                 o_ptr->iy = o_ptr->ix = 0;
1930
1931                                 /* Memorize monster */
1932                                 o_ptr->held_m_idx = m_idx;
1933
1934                                 /* Build a stack */
1935                                 o_ptr->next_o_idx = m_ptr->hold_o_idx;
1936
1937                                 /* Carry object */
1938                                 m_ptr->hold_o_idx = this_o_idx;
1939                         }
1940
1941                         /* Destroy the item if not a pet */
1942                         else if (!is_pet(m_ptr))
1943                         {
1944                                 did_kill_item = TRUE;
1945
1946                                 /* Describe observable situations */
1947                                 if (player_has_los_bold(target_ptr, ny, nx))
1948                                 {
1949                                         msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
1950                                 }
1951
1952                                 delete_object_idx(target_ptr, this_o_idx);
1953                         }
1954                 }
1955
1956                 if (do_turn) break;
1957         }
1958
1959         /*
1960          *  Forward movements failed, but now received LOS attack!
1961          *  Try to flow by smell.
1962          */
1963         if (target_ptr->no_flowed && i > 2 && m_ptr->target_y)
1964                 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
1965
1966         /* If we haven't done anything, try casting a spell again */
1967         if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
1968         {
1969                 /* Try to cast spell again */
1970                 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
1971                 {
1972                         if (make_attack_spell(m_idx, target_ptr)) return;
1973                 }
1974         }
1975
1976         /* Notice changes in view */
1977         if (do_view)
1978         {
1979                 target_ptr->update |= (PU_FLOW);
1980                 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1981         }
1982
1983         /* Notice changes in view */
1984         if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
1985                 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !target_ptr->phase_out)))
1986         {
1987                 target_ptr->update |= (PU_MON_LITE);
1988         }
1989
1990         /* Learn things from observable monster */
1991         if (is_original_ap_and_seen(target_ptr, m_ptr))
1992         {
1993                 /* Monster opened a door */
1994                 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
1995
1996                 /* Monster bashed a door */
1997                 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1998
1999                 /* Monster tried to pick something up */
2000                 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
2001
2002                 /* Monster tried to crush something */
2003                 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
2004
2005                 /* Monster pushed past another monster */
2006                 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
2007
2008                 /* Monster passed through a wall */
2009                 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
2010
2011                 /* Monster destroyed a wall */
2012                 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2013         }
2014
2015         bool is_battle_determined = !do_turn && !do_move && MON_MONFEAR(m_ptr) && aware;
2016         if (!is_battle_determined) return;
2017
2018         /* No longer afraid */
2019         (void)set_monster_monfear(target_ptr, m_idx, 0);
2020
2021         /* Message if seen */
2022         if (see_m)
2023         {
2024                 GAME_TEXT m_name[MAX_NLEN];
2025                 monster_desc(target_ptr, m_name, m_ptr, 0);
2026                 msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
2027         }
2028
2029         if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
2030 }
2031
2032
2033 /*!
2034  * @brief 召喚の親元が消滅した時、子供も消滅させる
2035  * @param target_ptr プレーヤーへの参照ポインタ
2036  * @param m_idx モンスターID
2037  * @param see_m モンスターが視界内にいたらTRUE
2038  * @return 召喚モンスターが消滅したらTRUE
2039  */
2040 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2041 {
2042         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2043         if ((m_ptr->parent_m_idx == 0) || target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
2044                 return FALSE;
2045
2046         if (see_m)
2047         {
2048                 GAME_TEXT m_name[MAX_NLEN];
2049                 monster_desc(target_ptr, m_name, m_ptr, 0);
2050                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2051         }
2052
2053         if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
2054         {
2055                 GAME_TEXT m_name[MAX_NLEN];
2056                 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
2057                 exe_write_diary(target_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
2058         }
2059
2060         delete_monster_idx(target_ptr, m_idx);
2061         return TRUE;
2062 }
2063
2064
2065 /*!
2066  * @brief 寝ているモンスターの起床を判定する
2067  * @param target_ptr プレーヤーへの参照ポインタ
2068  * @param m_idx モンスターID
2069  * @return なし
2070  */
2071 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx)
2072 {
2073         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2074         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2075         if (!MON_CSLEEP(m_ptr)) return;
2076         if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
2077
2078         (void)set_monster_csleep(target_ptr, m_idx, 0);
2079         if (m_ptr->ml)
2080         {
2081                 GAME_TEXT m_name[MAX_NLEN];
2082                 monster_desc(target_ptr, m_name, m_ptr, 0);
2083                 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
2084         }
2085
2086         if (is_original_ap_and_seen(target_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
2087         {
2088                 r_ptr->r_wake++;
2089         }
2090 }
2091
2092
2093 /*!
2094  * @brief 量子生物の量子的効果を実行する
2095  * @param target_ptr プレーヤーへの参照ポインタ
2096  * @param m_idx モンスターID
2097  * @param see_m モンスターが視界内にいたらTRUE
2098  * @return モンスターが量子的効果により消滅したらTRUE
2099  */
2100 bool process_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2101 {
2102         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2103         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2104         if ((r_ptr->flags2 & RF2_QUANTUM) == 0) return FALSE;
2105         if (!randint0(2)) return FALSE;
2106         if (randint0((m_idx % 100) + 10)) return FALSE;
2107
2108         bool can_disappear = (r_ptr->flags1 & RF1_UNIQUE) == 0;
2109         can_disappear &= (r_ptr->flags1 & RF1_QUESTOR) == 0;
2110         if (can_disappear)
2111         {
2112                 vanish_nonunique(target_ptr, m_idx, see_m);
2113                 return TRUE;
2114         }
2115         
2116         produce_quantum_effect(target_ptr, m_idx, see_m);
2117         return FALSE;
2118 }
2119
2120
2121 /*!
2122  * @brief ユニークでない量子生物を消滅させる
2123  * @param target_ptr プレーヤーへの参照ポインタ
2124  * @param m_idx モンスターID
2125  * @param see_m モンスターが視界内にいたらTRUE
2126  * @return なし
2127  */
2128 void vanish_nonunique(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2129 {
2130         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2131         if (see_m)
2132         {
2133                 GAME_TEXT m_name[MAX_NLEN];
2134                 monster_desc(target_ptr, m_name, m_ptr, 0);
2135                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2136         }
2137
2138         monster_death(target_ptr, m_idx, FALSE);
2139         delete_monster_idx(target_ptr, m_idx);
2140         if (is_pet(m_ptr) && !(m_ptr->ml))
2141         {
2142                 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
2143         }
2144 }
2145
2146
2147 /*!
2148  * @brief 量子生物ユニークの量子的効果 (ショート・テレポートまたは距離10のテレポート・アウェイ)を実行する
2149  * @param target_ptr プレーヤーへの参照ポインタ
2150  * @param m_idx モンスターID
2151  * @param see_m モンスターが視界内にいたらTRUE
2152  * @return なし
2153  * @details
2154  * プレーヤーが量子生物を観測しているか、量子生物がプレーヤーを観測している場合、互いの相対的な位置を確定させる
2155  * 波動関数の収縮はテレポートではないので反テレポート無効
2156  * todo パターンは収縮どころか拡散しているが、この際気にしてはいけない
2157  */
2158 void produce_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2159 {
2160         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2161         bool coherent = los(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x);
2162         if (!see_m && !coherent) return;
2163
2164         if (see_m)
2165         {
2166                 GAME_TEXT m_name[MAX_NLEN];
2167                 monster_desc(target_ptr, m_name, m_ptr, 0);
2168                 msg_format(_("%sは量子的効果を起こした!", "%^s produced a decoherence!"), m_name);
2169         }
2170         else
2171         {
2172                 msg_print(_("量子的効果が起こった!", "A decoherence was produced!"));
2173         }
2174
2175         bool target = one_in_(2);
2176         const int blink = 32 * 5 + 4;
2177         if (target)
2178         {
2179                 (void)monspell_to_monster(target_ptr, blink, m_ptr->fy, m_ptr->fx, m_idx, m_idx);
2180
2181         }
2182         else
2183         {
2184                 teleport_player_away(m_idx, target_ptr, 10, TRUE);
2185         }
2186 }
2187
2188
2189 void process_special(player_type *target_ptr, MONSTER_IDX m_idx)
2190 {
2191         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2192         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2193         if ((r_ptr->a_ability_flags2 & RF6_SPECIAL) == 0) return;
2194         if (m_ptr->r_idx != MON_OHMU) return;
2195         if (target_ptr->current_floor_ptr->inside_arena || target_ptr->phase_out) return;
2196         if ((r_ptr->freq_spell == 0) || !(randint1(100) <= r_ptr->freq_spell)) return;
2197
2198         int count = 0;
2199         DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
2200         BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
2201
2202         for (int k = 0; k < A_MAX; k++)
2203         {
2204                 if (summon_specific(target_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
2205                 {
2206                         if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
2207                 }
2208         }
2209
2210         if (count && is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
2211 }
2212
2213
2214 /*!
2215  * @brief モンスターを喋らせたり足音を立てたりする
2216  * @param target_ptr プレーヤーへの参照ポインタ
2217  * @param 
2218  * @param
2219  * @param
2220  * @param aware 起きていればTRUE
2221  * @return なし
2222  */
2223 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware)
2224 {
2225         if (target_ptr->phase_out) return;
2226
2227         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2228         monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2229         if (m_ptr->ap_r_idx == MON_CYBER &&
2230                 one_in_(CYBERNOISE) &&
2231                 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2232         {
2233                 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
2234                 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
2235         }
2236
2237         if (((ap_r_ptr->flags2 & RF2_CAN_SPEAK) == 0) || !aware ||
2238                 !one_in_(SPEAK_CHANCE) ||
2239                 !player_has_los_bold(target_ptr, oy, ox) ||
2240                 !projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
2241                 return;
2242
2243         GAME_TEXT m_name[MAX_NLEN];
2244         char monmessage[1024];
2245         concptr filename;
2246
2247         if (m_ptr->ml)
2248                 monster_desc(target_ptr, m_name, m_ptr, 0);
2249         else
2250                 strcpy(m_name, _("それ", "It"));
2251
2252         if (MON_MONFEAR(m_ptr))
2253                 filename = _("monfear_j.txt", "monfear.txt");
2254         else if (is_pet(m_ptr))
2255                 filename = _("monpet_j.txt", "monpet.txt");
2256         else if (is_friendly(m_ptr))
2257                 filename = _("monfrien_j.txt", "monfrien.txt");
2258         else
2259                 filename = _("monspeak_j.txt", "monspeak.txt");
2260
2261         if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
2262         {
2263                 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
2264         }
2265 }
2266
2267
2268 /*!
2269  * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
2270  * @param target_ptr プレーヤーへの参照ポインタ
2271  * @param m_idx モンスターID
2272  * @param oy 分裂元モンスターのY座標
2273  * @param ox 分裂元モンスターのX座標
2274  * @return 実際に分裂したらTRUEを返す
2275  */
2276 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
2277 {
2278         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2279         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2280         if (((r_ptr->flags2 & RF2_MULTIPLY) == 0) && (target_ptr->current_floor_ptr->num_repro >= MAX_REPRO))
2281                 return FALSE;
2282
2283         int k = 0;
2284         for (POSITION y = oy - 1; y <= oy + 1; y++)
2285         {
2286                 for (POSITION x = ox - 1; x <= ox + 1; x++)
2287                 {
2288                         if (!in_bounds2(target_ptr->current_floor_ptr, y, x)) continue;
2289                         if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
2290                 }
2291         }
2292
2293         if (multiply_barrier(target_ptr, m_idx)) k = 8;
2294
2295         if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2296         {
2297                 if (multiply_monster(target_ptr, m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
2298                 {
2299                         if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(target_ptr, m_ptr))
2300                         {
2301                                 r_ptr->r_flags2 |= (RF2_MULTIPLY);
2302                         }
2303
2304                         return TRUE;
2305                 }
2306         }
2307
2308         return FALSE;
2309 }
2310
2311
2312 /*!
2313  * @brief モンスターの移動パターンを決定する
2314  * @param target_ptr プレーヤーへの参照ポインタ
2315  * @param mm 移動方向
2316  * @param m_idx モンスターID
2317  * @param aware 起きていればTRUE
2318  * @return 移動先が存在すればTRUE
2319  */
2320 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware)
2321 {
2322         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2323         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2324
2325         if (MON_CONFUSED(m_ptr) || !aware)
2326         {
2327                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2328                 return TRUE;
2329         }
2330
2331         if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) && (randint0(100) < 75))
2332         {
2333                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
2334
2335                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2336                 return TRUE;
2337         }
2338
2339         if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 50))
2340         {
2341                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
2342
2343                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2344                 return TRUE;
2345         }
2346
2347          if ((r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 25))
2348          {
2349                 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
2350
2351                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2352                 return TRUE;
2353          }
2354
2355         if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2356         {
2357                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2358                 return TRUE;
2359         }
2360
2361         if (is_pet(m_ptr))
2362         {
2363                 bool avoid = ((target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
2364                 bool lonely = (!avoid && (m_ptr->cdis > target_ptr->pet_follow_distance));
2365                 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
2366                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2367                 if (!get_enemy_dir(target_ptr, m_idx, mm))
2368                 {
2369                         if (avoid || lonely || distant)
2370                         {
2371                                 POSITION dis = target_ptr->pet_follow_distance;
2372                                 if (target_ptr->pet_follow_distance > PET_SEEK_DIST)
2373                                 {
2374                                         target_ptr->pet_follow_distance = PET_SEEK_DIST;
2375                                 }
2376
2377                                 (void)get_moves(target_ptr, m_idx, mm);
2378                                 target_ptr->pet_follow_distance = (s16b)dis;
2379                         }
2380                 }
2381
2382                 return TRUE;
2383         }
2384
2385         if (!is_hostile(m_ptr))
2386         {
2387                 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2388                 get_enemy_dir(target_ptr, m_idx, mm);
2389                 return TRUE;
2390         }
2391
2392         if (!get_moves(target_ptr, m_idx, mm)) return FALSE;
2393
2394         return TRUE;
2395 }
2396
2397
2398 /*!
2399  * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
2400  * @param target_ptr プレーヤーへの参照ポインタ
2401  * @param m_idx モンスターID
2402  * @param is_riding_mon 騎乗状態ならばTRUE
2403  * @param see_m モンスターが視界内にいたらTRUE
2404  * @return モンスターがフロアから消えたらTRUE
2405  */
2406 bool runaway_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon, bool see_m)
2407 {
2408         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2409         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2410         bool can_runaway = is_pet(m_ptr) || is_friendly(m_ptr);
2411         can_runaway &= ((r_ptr->flags1 & RF1_UNIQUE) != 0) || ((r_ptr->flags7 & RF7_NAZGUL) != 0);
2412         can_runaway &= !target_ptr->phase_out;
2413         if (!can_runaway) return FALSE;
2414
2415         static int riding_pinch = 0;
2416
2417         if (m_ptr->hp >= m_ptr->maxhp / 3)
2418         {
2419                 /* Reset the counter */
2420                 if (is_riding_mon) riding_pinch = 0;
2421                 
2422                 return FALSE;
2423         }
2424
2425         GAME_TEXT m_name[MAX_NLEN];
2426         monster_desc(target_ptr, m_name, m_ptr, 0);
2427         if (is_riding_mon && riding_pinch < 2)
2428         {
2429                 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
2430                         "%^s seems to be in so much pain and tries to escape from your restriction."), m_name);
2431                 riding_pinch++;
2432                 disturb(target_ptr, TRUE, TRUE);
2433                 return FALSE;
2434         }
2435
2436         if (is_riding_mon)
2437         {
2438                 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
2439                 if (rakuba(target_ptr, -1, FALSE))
2440                 {
2441                         msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
2442                 }
2443         }
2444
2445         if (see_m)
2446         {
2447                 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) &&
2448                         player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) && projectable(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x))
2449                 {
2450                         msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
2451                 }
2452
2453                 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s reads a scroll of teleport level."), m_name);
2454                 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
2455         }
2456
2457         if (is_riding_mon && rakuba(target_ptr, -1, FALSE))
2458         {
2459                 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
2460         }
2461
2462         check_quest_completion(target_ptr, m_ptr);
2463         delete_monster_idx(target_ptr, m_idx);
2464         return TRUE;
2465 }
2466
2467
2468 /*!
2469  * @brief モンスターに魔法を試行させる
2470  * @param target_ptr プレーヤーへの参照ポインタ
2471  * @param m_idx モンスターID
2472  * @param aware 起きていればTRUE
2473  * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
2474  */
2475 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware)
2476 {
2477         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2478         monster_race *r_ptr = &r_info[m_ptr->r_idx];
2479         if ((r_ptr->freq_spell == 0) || !randint1(100) <= r_ptr->freq_spell)
2480                 return FALSE;
2481
2482         bool counterattack = FALSE;
2483         if (m_ptr->target_y)
2484         {
2485                 MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
2486                 if (t_m_idx && are_enemies(target_ptr, m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
2487                         projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
2488                 {
2489                         counterattack = TRUE;
2490                 }
2491         }
2492
2493         if (counterattack)
2494         {
2495                 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
2496                 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
2497         }
2498         else
2499         {
2500                 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
2501                 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
2502         }
2503
2504         return FALSE;
2505 }
2506
2507
2508 /*!
2509  * @brief 全モンスターのターン管理メインルーチン /
2510  * Process all the "live" monsters, once per game turn.
2511  * @return なし
2512  * @details
2513  * During each game current game turn, we scan through the list of all the "live" monsters,\n
2514  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
2515  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
2516  *\n
2517  * Note that monsters can never move in the monster array (except when the\n
2518  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
2519  *\n
2520  * This function is responsible for at least half of the processor time\n
2521  * on a normal system with a "normal" amount of monsters and a player doing\n
2522  * normal things.\n
2523  *\n
2524  * When the player is resting, virtually 90% of the processor time is spent\n
2525  * in this function, and its children, "process_monster()" and "make_move()".\n
2526  *\n
2527  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
2528  * especially when the player is running.\n
2529  *\n
2530  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
2531  * monsters while they are still being "born".  A monster is "fresh" only\n
2532  * during the game turn in which it is created, and we use the "hack_m_idx" to\n
2533  * determine if the monster is yet to be processed during the game turn.\n
2534  *\n
2535  * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
2536  * move before any "nasty" monsters get to use their spell attacks.\n
2537  *\n
2538  * Note that when the "knowledge" about the currently tracked monster\n
2539  * changes (flags, attacks, spells), we induce a redraw of the monster\n
2540  * recall window.\n
2541  */
2542 void process_monsters(player_type *target_ptr)
2543 {
2544         BIT_FLAGS old_r_flags1 = 0L;
2545         BIT_FLAGS old_r_flags2 = 0L;
2546         BIT_FLAGS old_r_flags3 = 0L;
2547         BIT_FLAGS old_r_flags4 = 0L;
2548         BIT_FLAGS old_r_flags5 = 0L;
2549         BIT_FLAGS old_r_flags6 = 0L;
2550         BIT_FLAGS old_r_flagsr = 0L;
2551
2552         byte old_r_blows0 = 0;
2553         byte old_r_blows1 = 0;
2554         byte old_r_blows2 = 0;
2555         byte old_r_blows3 = 0;
2556
2557         /* Clear monster fighting indicator */
2558         floor_type *floor_ptr = target_ptr->current_floor_ptr;
2559         floor_ptr->monster_noise = FALSE;
2560
2561         /* Memorize old race */
2562         MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
2563
2564         /* Acquire knowledge */
2565         byte old_r_cast_spell = 0;
2566         if (target_ptr->monster_race_idx)
2567         {
2568                 /* Acquire current monster */
2569                 monster_race *r_ptr;
2570                 r_ptr = &r_info[target_ptr->monster_race_idx];
2571
2572                 /* Memorize flags */
2573                 old_r_flags1 = r_ptr->r_flags1;
2574                 old_r_flags2 = r_ptr->r_flags2;
2575                 old_r_flags3 = r_ptr->r_flags3;
2576                 old_r_flags4 = r_ptr->r_flags4;
2577                 old_r_flags5 = r_ptr->r_flags5;
2578                 old_r_flags6 = r_ptr->r_flags6;
2579                 old_r_flagsr = r_ptr->r_flagsr;
2580
2581                 /* Memorize blows */
2582                 old_r_blows0 = r_ptr->r_blows[0];
2583                 old_r_blows1 = r_ptr->r_blows[1];
2584                 old_r_blows2 = r_ptr->r_blows[2];
2585                 old_r_blows3 = r_ptr->r_blows[3];
2586
2587                 /* Memorize castings */
2588                 old_r_cast_spell = r_ptr->r_cast_spell;
2589         }
2590
2591         /* Process the monsters (backwards) */
2592         bool test;
2593         for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--)
2594         {
2595                 monster_type *m_ptr;
2596                 monster_race *r_ptr;
2597                 m_ptr = &floor_ptr->m_list[i];
2598                 r_ptr = &r_info[m_ptr->r_idx];
2599
2600                 /* Handle "leaving" */
2601                 if (target_ptr->leaving) break;
2602
2603                 /* Ignore "dead" monsters */
2604                 if (!monster_is_valid(m_ptr)) continue;
2605
2606                 if (target_ptr->wild_mode) continue;
2607
2608
2609                 /* Handle "fresh" monsters */
2610                 if (m_ptr->mflag & MFLAG_BORN)
2611                 {
2612                         /* No longer "fresh" */
2613                         m_ptr->mflag &= ~(MFLAG_BORN);
2614
2615                         /* Skip */
2616                         continue;
2617                 }
2618
2619                 /* Hack -- Require proximity */
2620                 if (m_ptr->cdis >= AAF_LIMIT) continue;
2621
2622                 POSITION fx = m_ptr->fx;
2623                 POSITION fy = m_ptr->fy;
2624
2625                 /* Flow by smell is allowed */
2626                 if (!target_ptr->no_flowed)
2627                 {
2628                         m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2629                 }
2630
2631                 /* Assume no move */
2632                 test = FALSE;
2633
2634                 /* Handle "sensing radius" */
2635                 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
2636                 {
2637                         /* We can "sense" the player */
2638                         test = TRUE;
2639                 }
2640
2641                 /* Handle "sight" and "aggravation" */
2642                 else if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) &&
2643                         (player_has_los_bold(target_ptr, fy, fx) || (target_ptr->cursed & TRC_AGGRAVATE)))
2644                 {
2645                         /* We can "see" or "feel" the player */
2646                         test = TRUE;
2647                 }
2648
2649                 else if (m_ptr->target_y) test = TRUE;
2650
2651                 /* Do nothing */
2652                 if (!test) continue;
2653
2654                 SPEED speed;
2655                 if (target_ptr->riding == i)
2656                         speed = target_ptr->pspeed;
2657                 else
2658                 {
2659                         speed = m_ptr->mspeed;
2660
2661                         /* Monsters move quickly in Nightmare mode */
2662                         if (ironman_nightmare) speed += 5;
2663
2664                         if (MON_FAST(m_ptr)) speed += 10;
2665                         if (MON_SLOW(m_ptr)) speed -= 10;
2666                 }
2667
2668                 /* Give this monster some energy */
2669                 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
2670
2671                 /* Not enough energy to move */
2672                 if (m_ptr->energy_need > 0) continue;
2673
2674                 /* Use up "some" energy */
2675                 m_ptr->energy_need += ENERGY_NEED();
2676
2677                 /* Save global index */
2678                 hack_m_idx = i;
2679
2680                 /* Process the monster */
2681                 process_monster(target_ptr, i);
2682
2683                 reset_target(m_ptr);
2684
2685                 /* Give up flow_by_smell when it might useless */
2686                 if (target_ptr->no_flowed && one_in_(3))
2687                         m_ptr->mflag2 |= MFLAG2_NOFLOW;
2688
2689                 /* Hack -- notice death or departure */
2690                 if (!target_ptr->playing || target_ptr->is_dead) break;
2691
2692                 /* Notice leaving */
2693                 if (target_ptr->leaving) break;
2694         }
2695
2696         /* Reset global index */
2697         hack_m_idx = 0;
2698
2699
2700         /* Tracking a monster race (the same one we were before) */
2701         if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
2702                 return;
2703
2704         /* Acquire monster race */
2705         monster_race *r_ptr;
2706         r_ptr = &r_info[target_ptr->monster_race_idx];
2707
2708         /* Check for knowledge change */
2709         if ((old_r_flags1 != r_ptr->r_flags1) ||
2710                 (old_r_flags2 != r_ptr->r_flags2) ||
2711                 (old_r_flags3 != r_ptr->r_flags3) ||
2712                 (old_r_flags4 != r_ptr->r_flags4) ||
2713                 (old_r_flags5 != r_ptr->r_flags5) ||
2714                 (old_r_flags6 != r_ptr->r_flags6) ||
2715                 (old_r_flagsr != r_ptr->r_flagsr) ||
2716                 (old_r_blows0 != r_ptr->r_blows[0]) ||
2717                 (old_r_blows1 != r_ptr->r_blows[1]) ||
2718                 (old_r_blows2 != r_ptr->r_blows[2]) ||
2719                 (old_r_blows3 != r_ptr->r_blows[3]) ||
2720                 (old_r_cast_spell != r_ptr->r_cast_spell))
2721         {
2722                 target_ptr->window |= (PW_MONSTER);
2723         }
2724 }