OSDN Git Service

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