OSDN Git Service

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