OSDN Git Service

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