2 * @file monster-process.c
3 * @brief モンスターの特殊技能とターン経過処理 (移動等)/ Monster spells and movement for passaging a turn
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
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).
18 #include "monster/monster-attack.h"
19 #include "monster/monster-object.h"
20 #include "monster/monster-move.h"
21 #include "monster/monster-util.h"
22 #include "monster/quantum-effect.h"
29 #include "spells-floor.h"
30 #include "spells-summon.h"
33 #include "realm-hex.h"
36 #include "player-move.h"
37 #include "monster-status.h"
38 #include "monster-spell.h"
39 #include "monster-process.h"
40 #include "monster-dist-offsets.h"
41 #include "monsterrace-hook.h"
44 #include "view-mainwindow.h"
46 bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm);
47 void decide_enemy_approch_direction(player_type *target_ptr, MONSTER_IDX m_idx, int start, int plus, POSITION *y, POSITION *x);
48 bool decide_pet_approch_direction(player_type *target_ptr, monster_type *m_ptr, monster_type *t_ptr);
50 bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp);
51 coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONSTER_IDX m_idx, const POSITION *y_offsets, const POSITION *x_offsets, int d);
53 bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp);
54 void sweep_hiding_candidate(player_type *target_ptr, monster_type *m_ptr, const POSITION *y_offsets, const POSITION *x_offsets, coordinate_candidate *candidate);
56 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon);
57 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx);
58 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
59 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx);
60 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
61 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx);
62 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox);
63 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware);
64 bool random_walk(player_type *target_ptr, DIRECTION *mm, monster_type *m_ptr);
65 bool decide_pet_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx);
66 bool runaway_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
67 void escape_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, GAME_TEXT *m_name);
68 void process_special(player_type *target_ptr, MONSTER_IDX m_idx);
69 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware);
70 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware);
72 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);
73 bool process_post_dig_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
74 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);
76 void update_player_type(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_race *r_ptr);
77 void update_monster_race_flags(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr);
78 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
80 void sweep_monster_process(player_type *target_ptr);
81 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr);
84 * @brief モンスターが敵に接近するための方向を計算するメインルーチン
85 * Calculate the direction to the next enemy
86 * @param target_ptr プレーヤーへの参照ポインタ
87 * @param m_idx モンスターの参照ID
88 * @param mm 移動するべき方角IDを返す参照ポインタ
89 * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
91 bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm)
93 floor_type *floor_ptr = target_ptr->current_floor_ptr;
94 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
96 POSITION x = 0, y = 0;
97 if (target_ptr->riding_t_m_idx && player_bold(target_ptr, m_ptr->fy, m_ptr->fx))
99 y = floor_ptr->m_list[target_ptr->riding_t_m_idx].fy;
100 x = floor_ptr->m_list[target_ptr->riding_t_m_idx].fx;
102 else if (is_pet(m_ptr) && target_ptr->pet_t_m_idx)
104 y = floor_ptr->m_list[target_ptr->pet_t_m_idx].fy;
105 x = floor_ptr->m_list[target_ptr->pet_t_m_idx].fx;
111 if (target_ptr->phase_out)
113 start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
114 if (randint0(2)) plus = -1;
118 start = floor_ptr->m_max + 1;
121 decide_enemy_approch_direction(target_ptr, m_idx, start, plus, &y, &x);
123 if ((x == 0) && (y == 0)) return FALSE;
129 store_enemy_approch_direction(mm, y, x);
135 * @brief モンスターが敵に接近するための方向を決定する
136 * @param target_ptr プレーヤーへの参照ポインタ
137 * @param m_idx モンスターID
138 * @param start モンスターIDの開始
139 * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
140 * @param y モンスターの移動方向Y
141 * @param x モンスターの移動方向X
144 void decide_enemy_approch_direction(player_type *target_ptr, MONSTER_IDX m_idx, int start, int plus, POSITION *y, POSITION *x)
146 floor_type *floor_ptr = target_ptr->current_floor_ptr;
147 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
148 monster_race *r_ptr = &r_info[m_ptr->r_idx];
149 for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus)
151 MONSTER_IDX dummy = (i % floor_ptr->m_max);
152 if (dummy == 0) continue;
154 MONSTER_IDX t_idx = dummy;
156 t_ptr = &floor_ptr->m_list[t_idx];
157 if (t_ptr == m_ptr) continue;
158 if (!monster_is_valid(t_ptr)) continue;
159 if (decide_pet_approch_direction(target_ptr, m_ptr, t_ptr)) continue;
160 if (!are_enemies(target_ptr, m_ptr, t_ptr)) continue;
162 if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) ||
163 ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)))
165 if (!in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
169 if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
180 * @brief ペットが敵に接近するための方向を決定する
181 * @param target_ptr プレーヤーへの参照ポインタ
182 * @param m_ptr 移動を試みているモンスターへの参照ポインタ
183 * @param t_ptr 移動先モンスターへの参照ポインタ
184 * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
185 * @return ペットがモンスターに近づくならばTRUE
187 bool decide_pet_approch_direction(player_type *target_ptr, monster_type *m_ptr, monster_type *t_ptr)
189 monster_race *r_ptr = &r_info[m_ptr->r_idx];
190 if (!is_pet(m_ptr)) return FALSE;
192 if (target_ptr->pet_follow_distance < 0)
194 if (t_ptr->cdis <= (0 - target_ptr->pet_follow_distance))
199 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > target_ptr->pet_follow_distance))
204 return (r_ptr->aaf < t_ptr->cdis);
209 * @brief モンスターがプレイヤーから逃走するかどうかを返す /
210 * Returns whether a given monster will try to run from the player.
211 * @param m_idx 逃走するモンスターの参照ID
212 * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
214 * Monsters will attempt to avoid very powerful players. See below.\n
216 * Because this function is called so often, little details are important\n
217 * for efficiency. Like not using "mod" or "div" when possible. And\n
218 * attempting to check the conditions in an optimal order. Note that\n
219 * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
221 * Note that this function is responsible for about one to five percent\n
222 * of the processor use in normal conditions...\n
224 static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
226 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
227 monster_race *r_ptr = &r_info[m_ptr->r_idx];
231 return ((target_ptr->pet_follow_distance < 0) &&
232 (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
235 if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
236 if (MON_MONFEAR(m_ptr)) return TRUE;
237 if (m_ptr->cdis <= 5) return FALSE;
239 PLAYER_LEVEL p_lev = target_ptr->lev;
240 DEPTH m_lev = r_ptr->level + (m_idx & 0x08) + 25;
241 if (m_lev > p_lev + 4) return FALSE;
242 if (m_lev + 4 <= p_lev) return TRUE;
244 HIT_POINT p_chp = target_ptr->chp;
245 HIT_POINT p_mhp = target_ptr->mhp;
246 HIT_POINT m_chp = m_ptr->hp;
247 HIT_POINT m_mhp = m_ptr->maxhp;
248 u32b p_val = (p_lev * p_mhp) + (p_chp << 2);
249 u32b m_val = (m_lev * m_mhp) + (m_chp << 2);
250 if (p_val * m_mhp > m_val * p_mhp) return TRUE;
257 * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
258 * Search spell castable grid
259 * @param target_ptr プレーヤーへの参照ポインタ
260 * @param m_idx モンスターの参照ID
261 * @param yp 適したマスのY座標を返す参照ポインタ
262 * @param xp 適したマスのX座標を返す参照ポインタ
263 * @return 有効なマスがあった場合TRUEを返す
265 static bool get_moves_aux2(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
267 floor_type *floor_ptr = target_ptr->current_floor_ptr;
268 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
269 monster_race *r_ptr = &r_info[m_ptr->r_idx];
271 POSITION y1 = m_ptr->fy;
272 POSITION x1 = m_ptr->fx;
274 if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
276 int now_cost = floor_ptr->grid_array[y1][x1].cost;
277 if (now_cost == 0) now_cost = 999;
279 bool can_open_door = FALSE;
280 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
282 can_open_door = TRUE;
286 for (int i = 7; i >= 0; i--)
288 POSITION y = y1 + ddy_ddd[i];
289 POSITION x = x1 + ddx_ddd[i];
290 if (!in_bounds2(floor_ptr, y, x)) continue;
291 if (player_bold(target_ptr, y, x)) return FALSE;
294 g_ptr = &floor_ptr->grid_array[y][x];
295 int cost = g_ptr->cost;
296 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))))
298 if (cost == 0) continue;
299 if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat)) continue;
302 if (cost == 0) cost = 998;
304 if (now_cost < cost) continue;
305 if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x)) continue;
306 if (best < cost) continue;
309 *yp = y1 + ddy_ddd[i];
310 *xp = x1 + ddx_ddd[i];
313 if (best == 999) return FALSE;
320 * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
321 * Choose the "best" direction for "flowing"
322 * @param m_idx モンスターの参照ID
323 * @param yp 移動先のマスのY座標を返す参照ポインタ
324 * @param xp 移動先のマスのX座標を返す参照ポインタ
325 * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
326 * @return 有効なマスがあった場合TRUEを返す
328 * Note that ghosts and rock-eaters are never allowed to "flow",\n
329 * since they should move directly towards the player.\n
331 * Prefer "non-diagonal" directions, but twiddle them a little\n
332 * to angle slightly towards the player's actual location.\n
334 * Allow very perceptive monsters to track old "spoor" left by\n
335 * previous locations occupied by the player. This will tend\n
336 * to have monsters end up either near the player or on a grid\n
337 * recently occupied by the player (and left via "teleport").\n
339 * Note that if "smell" is turned on, all monsters get vicious.\n
341 * Also note that teleporting away from a location will cause\n
342 * the monsters who were chasing you to converge on that location\n
343 * as long as you are still near enough to "annoy" them without\n
344 * being close enough to chase directly. I have no idea what will\n
345 * happen if you combine "smell" with low "aaf" values.\n
347 static bool get_moves_aux(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
350 floor_type *floor_ptr = target_ptr->current_floor_ptr;
351 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
352 monster_race *r_ptr = &r_info[m_ptr->r_idx];
354 if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
355 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
356 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
358 if (get_moves_aux2(target_ptr, m_idx, yp, xp)) return TRUE;
361 if (no_flow) return FALSE;
362 if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return FALSE;
363 if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return FALSE;
365 POSITION y1 = m_ptr->fy;
366 POSITION x1 = m_ptr->fx;
367 if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return FALSE;
369 g_ptr = &floor_ptr->grid_array[y1][x1];
372 bool use_scent = FALSE;
377 else if (g_ptr->when)
379 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return FALSE;
389 for (int i = 7; i >= 0; i--)
391 POSITION y = y1 + ddy_ddd[i];
392 POSITION x = x1 + ddx_ddd[i];
394 if (!in_bounds2(floor_ptr, y, x)) continue;
396 g_ptr = &floor_ptr->grid_array[y][x];
399 int when = g_ptr->when;
400 if (best > when) continue;
407 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
416 if ((cost == 0) || (best < cost)) continue;
421 *yp = target_ptr->y + 16 * ddy_ddd[i];
422 *xp = target_ptr->x + 16 * ddx_ddd[i];
425 if (best == 999 || best == 0) return FALSE;
432 * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
433 * Provide a location to flee to, but give the player a wide berth.
434 * @param m_idx モンスターの参照ID
435 * @param yp 移動先のマスのY座標を返す参照ポインタ
436 * @param xp 移動先のマスのX座標を返す参照ポインタ
437 * @return 有効なマスがあった場合TRUEを返す
439 * A monster may wish to flee to a location that is behind the player,\n
440 * but instead of heading directly for it, the monster should "swerve"\n
441 * around the player so that he has a smaller chance of getting hit.\n
443 static bool get_fear_moves_aux(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
445 POSITION gy = 0, gx = 0;
447 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
448 POSITION fy = m_ptr->fy;
449 POSITION fx = m_ptr->fx;
451 POSITION y1 = fy - (*yp);
452 POSITION x1 = fx - (*xp);
455 for (int i = 7; i >= 0; i--)
457 POSITION y = fy + ddy_ddd[i];
458 POSITION x = fx + ddx_ddd[i];
459 if (!in_bounds2(floor_ptr, y, x)) continue;
461 POSITION dis = distance(y, x, y1, x1);
462 POSITION s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
465 if (s < score) continue;
472 if (score == -1) return FALSE;
482 * @brief モンスターが逃げ込める安全な地点を返す /
483 * Choose a "safe" location near a monster for it to run toward.
484 * @param target_ptr プレーヤーへの参照ポインタ
485 * @param m_idx モンスターの参照ID
486 * @param yp 移動先のマスのY座標を返す参照ポインタ
487 * @param xp 移動先のマスのX座標を返す参照ポインタ
488 * @return 有効なマスがあった場合TRUEを返す
490 * A location is "safe" if it can be reached quickly and the player\n
491 * is not able to fire into it (it isn't a "clean shot"). So, this will\n
492 * cause monsters to "duck" behind walls. Hopefully, monsters will also\n
493 * try to run towards corridor openings if they are in a room.\n
495 * This function may take lots of CPU time if lots of monsters are\n
498 * Return TRUE if a safe location is available.\n
500 bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
502 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
503 for (POSITION d = 1; d < 10; d++)
505 const POSITION *y_offsets;
506 y_offsets = dist_offsets_y[d];
508 const POSITION *x_offsets;
509 x_offsets = dist_offsets_x[d];
511 coordinate_candidate candidate = sweep_safe_coordinate(target_ptr, m_idx, y_offsets, x_offsets, d);
513 if (candidate.gdis <= 0) continue;
515 *yp = m_ptr->fy - candidate.gy;
516 *xp = m_ptr->fx - candidate.gx;
526 * @brief モンスターが逃げ込める地点を走査する
527 * @param target_ptr プレーヤーへの参照ポインタ
528 * @param m_idx モンスターID
531 * @param d モンスターがいる地点からの距離
532 * @return 逃げ込める地点の候補地
534 coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONSTER_IDX m_idx, const POSITION *y_offsets, const POSITION *x_offsets, int d)
536 coordinate_candidate candidate = init_coordinate_candidate();
537 floor_type *floor_ptr = target_ptr->current_floor_ptr;
538 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
539 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
541 i++, dx = x_offsets[i], dy = y_offsets[i])
543 POSITION y = m_ptr->fy + dy;
544 POSITION x = m_ptr->fx + dx;
545 if (!in_bounds(floor_ptr, y, x)) continue;
548 g_ptr = &floor_ptr->grid_array[y][x];
550 BIT_FLAGS16 riding_mode = (m_idx == target_ptr->riding) ? CEM_RIDING : 0;
551 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], riding_mode))
554 if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
556 if (g_ptr->dist == 0) continue;
557 if (g_ptr->dist > floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist + 2 * d) continue;
560 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x)) continue;
562 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
563 if (dis <= candidate.gdis) continue;
567 candidate.gdis = dis;
575 * @brief モンスターが隠れ潜める地点を返す /
576 * Choose a good hiding place near a monster for it to run toward.
577 * @param target_ptr プレーヤーへの参照ポインタ
578 * @param m_idx モンスターの参照ID
579 * @param yp 移動先のマスのY座標を返す参照ポインタ
580 * @param xp 移動先のマスのX座標を返す参照ポインタ
581 * @return 有効なマスがあった場合TRUEを返す
583 * Pack monsters will use this to "ambush" the player and lure him out\n
584 * of corridors into open space so they can swarm him.\n
586 * Return TRUE if a good location is available.\n
588 bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
590 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
591 coordinate_candidate candidate = init_coordinate_candidate();
592 candidate.gdis = 999;
594 for (POSITION d = 1; d < 10; d++)
596 const POSITION *y_offsets;
597 y_offsets = dist_offsets_y[d];
599 const POSITION *x_offsets;
600 x_offsets = dist_offsets_x[d];
602 sweep_hiding_candidate(target_ptr, m_ptr, y_offsets, x_offsets, &candidate);
603 if (candidate.gdis >= 999) continue;
605 *yp = m_ptr->fy - candidate.gy;
606 *xp = m_ptr->fx - candidate.gx;
615 * @brief モンスターが隠れられる地点を走査する
616 * @param target_ptr プレーヤーへの参照ポインタ
617 * @param m_idx モンスターID
620 * @param candidate 隠れられる地点の候補地
623 void sweep_hiding_candidate(player_type *target_ptr, monster_type *m_ptr, const POSITION *y_offsets, const POSITION *x_offsets, coordinate_candidate *candidate)
625 monster_race *r_ptr = &r_info[m_ptr->r_idx];
626 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
628 i++, dx = x_offsets[i], dy = y_offsets[i])
630 POSITION y = m_ptr->fy + dy;
631 POSITION x = m_ptr->fx + dx;
632 if (!in_bounds(target_ptr->current_floor_ptr, y, x)) continue;
633 if (!monster_can_enter(target_ptr, y, x, r_ptr, 0)) continue;
634 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x) && clean_shot(target_ptr, m_ptr->fy, m_ptr->fx, y, x, FALSE))
637 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
638 if (dis < candidate->gdis && dis >= 2)
642 candidate->gdis = dis;
649 * todo 分割したいが条件が多すぎて適切な関数名と詳細処理を追いきれない……
650 * @brief モンスターの移動方向を返す /
651 * Choose "logical" directions for monster movement
652 * @param target_ptr プレーヤーへの参照ポインタ
653 * @param m_idx モンスターの参照ID
654 * @param mm 移動方向を返す方向IDの参照ポインタ
655 * @return 有効方向があった場合TRUEを返す
657 static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
659 floor_type *floor_ptr = target_ptr->current_floor_ptr;
660 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
661 monster_race *r_ptr = &r_info[m_ptr->r_idx];
662 POSITION y = 0, ay, x = 0, ax;
663 POSITION y2 = target_ptr->y;
664 POSITION x2 = target_ptr->x;
666 bool will_run = mon_will_run(target_ptr, m_idx);
668 bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) != 0) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2);
669 bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) != 0) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall);
671 if (!will_run && m_ptr->target_y)
673 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
675 are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) &&
676 los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
677 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
679 y = m_ptr->fy - m_ptr->target_y;
680 x = m_ptr->fx - m_ptr->target_x;
685 if (!done && !will_run && is_hostile(m_ptr) &&
686 (r_ptr->flags1 & RF1_FRIENDS) &&
687 ((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)) ||
688 (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
690 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
691 !(r_ptr->flags2 & RF2_KILL_WALL))
694 for (int i = 0; i < 8; i++)
696 int xx = target_ptr->x + ddx_ddd[i];
697 int yy = target_ptr->y + ddy_ddd[i];
699 if (!in_bounds2(floor_ptr, yy, xx)) continue;
701 g_ptr = &floor_ptr->grid_array[yy][xx];
702 if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0))
708 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
709 if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
711 if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
712 (target_ptr->mhp + target_ptr->msp))
714 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
718 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
720 for (int i = 0; i < 8; i++)
722 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
723 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
724 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
731 if (!in_bounds2(floor_ptr, y2, x2)) continue;
732 if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0)) continue;
745 (void)get_moves_aux(target_ptr, m_idx, &y2, &x2, no_flow);
750 if (is_pet(m_ptr) && will_run)
756 if (!done && will_run)
760 if (find_safety(target_ptr, m_idx, &y, &x) && !no_flow)
762 if (get_fear_moves_aux(target_ptr->current_floor_ptr, m_idx, &y, &x))
774 if (!x && !y) return FALSE;
780 if (y < 0) move_val += 8;
781 if (x > 0) move_val += 4;
783 if (ay > (ax << 1)) move_val += 2;
784 else if (ax > (ay << 1)) move_val++;
935 * @brief モンスター単体の1ターン行動処理メインルーチン /
937 * @param target_ptr プレーヤーへの参照ポインタ
938 * @param m_idx 行動モンスターの参照ID
941 * The monster is known to be within 100 grids of the player\n
943 * In several cases, we directly update the monster lore\n
945 * Note that a monster is only allowed to "reproduce" if there\n
946 * are a limited number of "reproducing" monsters on the current\n
947 * level. This should prevent the level from being "swamped" by\n
948 * reproducing monsters. It also allows a large mass of mice to\n
949 * prevent a louse from multiplying, but this is a small price to\n
950 * pay for a simple multiplication method.\n
952 * XXX Monster fear is slightly odd, in particular, monsters will\n
953 * fixate on opening a door even if they cannot open it. Actually,\n
954 * the same thing happens to normal monsters when they hit a door\n
956 * In addition, monsters which *cannot* open or bash\n
957 * down a door will still stand there trying to open it...\n
959 * XXX Technically, need to check for monster in the way\n
960 * combined with that monster being in a wall (or door?)\n
962 * A "direction" of "5" means "pick a random direction".\n
964 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
966 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
967 monster_race *r_ptr = &r_info[m_ptr->r_idx];
968 turn_flags tmp_flags;
969 turn_flags *turn_flags_ptr = init_turn_flags(target_ptr->riding, m_idx, &tmp_flags);
970 turn_flags_ptr->see_m = is_seen(m_ptr);
972 decide_drop_from_monster(target_ptr, m_idx, turn_flags_ptr->is_riding_mon);
973 if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
975 choose_new_monster(target_ptr, m_idx, FALSE, 0);
976 r_ptr = &r_info[m_ptr->r_idx];
979 turn_flags_ptr->aware = process_stealth(target_ptr, m_idx);
980 if (vanish_summoned_children(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
981 if (process_quantum_effect(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
982 if (explode_grenade(target_ptr, m_idx)) return;
983 if (runaway_monster(target_ptr, turn_flags_ptr, m_idx)) return;
985 awake_monster(target_ptr, m_idx);
986 if (MON_STUNNED(m_ptr))
988 if (one_in_(2)) return;
991 if (turn_flags_ptr->is_riding_mon)
993 target_ptr->update |= (PU_BONUS);
996 process_angar(target_ptr, m_idx, turn_flags_ptr->see_m);
998 POSITION oy = m_ptr->fy;
999 POSITION ox = m_ptr->fx;
1000 if (decide_monster_multiplication(target_ptr, m_idx, oy, ox)) return;
1002 process_special(target_ptr, m_idx);
1003 process_speak_sound(target_ptr, m_idx, oy, ox, turn_flags_ptr->aware);
1004 if (cast_spell(target_ptr, m_idx, turn_flags_ptr->aware)) return;
1007 mm[0] = mm[1] = mm[2] = mm[3] = 0;
1008 mm[4] = mm[5] = mm[6] = mm[7] = 0;
1010 if (!decide_monster_movement_direction(target_ptr, mm, m_idx, turn_flags_ptr->aware)) return;
1013 if (!process_monster_movement(target_ptr, turn_flags_ptr, m_idx, mm, oy, ox, &count)) return;
1016 * Forward movements failed, but now received LOS attack!
1017 * Try to flow by smell.
1019 if (target_ptr->no_flowed && count > 2 && m_ptr->target_y)
1020 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
1022 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)
1024 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
1026 if (make_attack_spell(m_idx, target_ptr)) return;
1030 update_player_type(target_ptr, turn_flags_ptr, r_ptr);
1031 update_monster_race_flags(target_ptr, turn_flags_ptr, m_ptr);
1033 if (!process_monster_fear(target_ptr, turn_flags_ptr, m_idx)) return;
1035 if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
1041 * @param target_ptr プレーヤーへの参照ポインタ
1042 * @param m_idx モンスターID
1043 * @return モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1045 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx)
1047 if ((target_ptr->special_defense & NINJA_S_STEALTH) == 0) return TRUE;
1049 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1050 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1051 int tmp = target_ptr->lev * 6 + (target_ptr->skill_stl + 10) * 4;
1052 if (target_ptr->monlite) tmp /= 3;
1053 if (target_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1054 if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10)) tmp /= 3;
1055 return (randint0(tmp) <= (r_ptr->level + 20));
1060 * @brief 死亡したモンスターが乗馬中のモンスターだった場合に落馬処理を行う
1061 * @param target_ptr プレーヤーへの参照ポインタ
1062 * @param m_idx モンスターID
1063 * @param is_riding_mon 騎乗中であればTRUE
1066 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon)
1068 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1069 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1070 if (!is_riding_mon || ((r_ptr->flags7 & RF7_RIDING) != 0)) return;
1072 if (rakuba(target_ptr, 0, TRUE))
1075 msg_print("地面に落とされた。");
1077 GAME_TEXT m_name[MAX_NLEN];
1078 monster_desc(target_ptr, m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
1079 msg_format("You have fallen from %s.", m_name);
1086 * @brief 召喚の親元が消滅した時、子供も消滅させる
1087 * @param target_ptr プレーヤーへの参照ポインタ
1088 * @param m_idx モンスターID
1089 * @param see_m モンスターが視界内にいたらTRUE
1090 * @return 召喚モンスターが消滅したらTRUE
1092 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
1094 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1095 if ((m_ptr->parent_m_idx == 0) || (target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx > 0))
1100 GAME_TEXT m_name[MAX_NLEN];
1101 monster_desc(target_ptr, m_name, m_ptr, 0);
1102 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
1105 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
1107 GAME_TEXT m_name[MAX_NLEN];
1108 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
1109 exe_write_diary(target_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
1112 delete_monster_idx(target_ptr, m_idx);
1118 * @brief 寝ているモンスターの起床を判定する
1119 * @param target_ptr プレーヤーへの参照ポインタ
1120 * @param m_idx モンスターID
1123 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx)
1125 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1126 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1127 if (!MON_CSLEEP(m_ptr)) return;
1128 if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
1130 (void)set_monster_csleep(target_ptr, m_idx, 0);
1133 GAME_TEXT m_name[MAX_NLEN];
1134 monster_desc(target_ptr, m_name, m_ptr, 0);
1135 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
1138 if (is_original_ap_and_seen(target_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
1146 * @brief モンスターの怒り状態を判定する (起こっていたら敵に回す)
1147 * @param target_ptr プレーヤーへの参照ポインタ
1148 * @param m_idx モンスターID
1149 * @param see_m モンスターが視界内にいたらTRUE
1152 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
1154 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1155 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1156 bool gets_angry = FALSE;
1157 if (is_friendly(m_ptr) && (target_ptr->cursed & TRC_AGGRAVATE))
1160 if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
1161 monster_has_hostile_align(target_ptr, NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
1166 if (target_ptr->phase_out || !gets_angry) return;
1168 if (is_pet(m_ptr) || see_m)
1170 GAME_TEXT m_name[MAX_NLEN];
1171 monster_desc(target_ptr, m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
1172 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
1175 set_hostile(target_ptr, m_ptr);
1181 * @param target_ptr プレーヤーへの参照ポインタ
1182 * @param m_idx モンスターID
1185 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx)
1187 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1188 if (m_ptr->r_idx != MON_GRENADE) return FALSE;
1191 mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1197 * @brief モンスター依存の特別な行動を取らせる
1198 * @param target_ptr プレーヤーへの参照ポインタ
1199 * @param m_idx モンスターID
1202 void process_special(player_type *target_ptr, MONSTER_IDX m_idx)
1204 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1205 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1206 if ((r_ptr->a_ability_flags2 & RF6_SPECIAL) == 0) return;
1207 if (m_ptr->r_idx != MON_OHMU) return;
1208 if (target_ptr->current_floor_ptr->inside_arena || target_ptr->phase_out) return;
1209 if ((r_ptr->freq_spell == 0) || !(randint1(100) <= r_ptr->freq_spell)) return;
1212 DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1213 BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
1215 for (int k = 0; k < A_MAX; k++)
1217 if (summon_specific(target_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
1219 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
1223 if (count && is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
1228 * @brief モンスターを喋らせたり足音を立てたりする
1229 * @param target_ptr プレーヤーへの参照ポインタ
1230 * @param m_idx モンスターID
1231 * @param oy モンスターが元々いたY座標
1232 * @param ox モンスターが元々いたX座標
1233 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1236 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware)
1238 if (target_ptr->phase_out) return;
1240 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1241 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1242 if (m_ptr->ap_r_idx == MON_CYBER &&
1243 one_in_(CYBERNOISE) &&
1244 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
1246 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1247 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
1250 if (((ap_r_ptr->flags2 & RF2_CAN_SPEAK) == 0) || !aware ||
1251 !one_in_(SPEAK_CHANCE) ||
1252 !player_has_los_bold(target_ptr, oy, ox) ||
1253 !projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
1256 GAME_TEXT m_name[MAX_NLEN];
1257 char monmessage[1024];
1261 monster_desc(target_ptr, m_name, m_ptr, 0);
1263 strcpy(m_name, _("それ", "It"));
1265 if (MON_MONFEAR(m_ptr))
1266 filename = _("monfear_j.txt", "monfear.txt");
1267 else if (is_pet(m_ptr))
1268 filename = _("monpet_j.txt", "monpet.txt");
1269 else if (is_friendly(m_ptr))
1270 filename = _("monfrien_j.txt", "monfrien.txt");
1272 filename = _("monspeak_j.txt", "monspeak.txt");
1274 if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
1276 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
1282 * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
1283 * @param target_ptr プレーヤーへの参照ポインタ
1284 * @param m_idx モンスターID
1285 * @param oy 分裂元モンスターのY座標
1286 * @param ox 分裂元モンスターのX座標
1287 * @return 実際に分裂したらTRUEを返す
1289 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
1291 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1292 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1293 if (((r_ptr->flags2 & RF2_MULTIPLY) == 0) || (target_ptr->current_floor_ptr->num_repro >= MAX_REPRO))
1297 for (POSITION y = oy - 1; y <= oy + 1; y++)
1299 for (POSITION x = ox - 1; x <= ox + 1; x++)
1301 if (!in_bounds2(target_ptr->current_floor_ptr, y, x)) continue;
1302 if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
1306 if (multiply_barrier(target_ptr, m_idx)) k = 8;
1308 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
1310 if (multiply_monster(target_ptr, m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
1312 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(target_ptr, m_ptr))
1314 r_ptr->r_flags2 |= (RF2_MULTIPLY);
1326 * @brief モンスターの移動パターンを決定する
1327 * @param target_ptr プレーヤーへの参照ポインタ
1329 * @param m_idx モンスターID
1330 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1331 * @return 移動先が存在すればTRUE
1333 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware)
1335 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1336 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1338 if (MON_CONFUSED(m_ptr) || !aware)
1340 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1344 if (random_walk(target_ptr, mm, m_ptr)) return TRUE;
1346 if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
1348 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1352 if (decide_pet_movement_direction(target_ptr, mm, m_idx)) return TRUE;
1354 if (!is_hostile(m_ptr))
1356 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1357 get_enemy_dir(target_ptr, m_idx, mm);
1361 if (!get_moves(target_ptr, m_idx, mm)) return FALSE;
1368 * todo ↓のように書いたが、"5"とはもしかして「その場に留まる」という意味か?
1369 * @brief 不規則歩行フラグを持つモンスターの移動方向をその確率に基づいて決定する
1370 * @param target_ptr プレーヤーへの参照ポインタ
1372 * @param m_ptr モンスターへの参照ポインタ
1373 * @return 不規則な方向へ歩くことになったらTRUE
1375 bool random_walk(player_type *target_ptr, DIRECTION *mm, monster_type *m_ptr)
1377 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1378 if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) && (randint0(100) < 75))
1380 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
1382 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1386 if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 50))
1388 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_50;
1390 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1394 if ((r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 25))
1396 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
1398 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1407 * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
1408 * @param target_ptr プレーヤーへの参照ポインタ
1410 * @param m_idx モンスターID
1411 * @return モンスターがペットであればTRUE
1413 bool decide_pet_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx)
1415 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1416 if (!is_pet(m_ptr)) return FALSE;
1418 bool avoid = ((target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
1419 bool lonely = (!avoid && (m_ptr->cdis > target_ptr->pet_follow_distance));
1420 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
1421 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1422 if (get_enemy_dir(target_ptr, m_idx, mm)) return TRUE;
1423 if (!avoid && !lonely && !distant) return TRUE;
1425 POSITION dis = target_ptr->pet_follow_distance;
1426 if (target_ptr->pet_follow_distance > PET_SEEK_DIST)
1428 target_ptr->pet_follow_distance = PET_SEEK_DIST;
1431 (void)get_moves(target_ptr, m_idx, mm);
1432 target_ptr->pet_follow_distance = (s16b)dis;
1438 * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
1439 * @param target_ptr プレーヤーへの参照ポインタ
1440 * @param m_idx モンスターID
1441 * @param is_riding_mon 騎乗状態ならばTRUE
1442 * @param see_m モンスターが視界内にいたらTRUE
1443 * @return モンスターがフロアから消えたらTRUE
1445 bool runaway_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
1447 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1448 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1449 bool can_runaway = is_pet(m_ptr) || is_friendly(m_ptr);
1450 can_runaway &= ((r_ptr->flags1 & RF1_UNIQUE) != 0) || ((r_ptr->flags7 & RF7_NAZGUL) != 0);
1451 can_runaway &= !target_ptr->phase_out;
1452 if (!can_runaway) return FALSE;
1454 static int riding_pinch = 0;
1456 if (m_ptr->hp >= m_ptr->maxhp / 3)
1458 /* Reset the counter */
1459 if (turn_flags_ptr->is_riding_mon) riding_pinch = 0;
1464 GAME_TEXT m_name[MAX_NLEN];
1465 monster_desc(target_ptr, m_name, m_ptr, 0);
1466 if (turn_flags_ptr->is_riding_mon && riding_pinch < 2)
1468 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
1469 "%^s seems to be in so much pain and tries to escape from your restriction."), m_name);
1471 disturb(target_ptr, TRUE, TRUE);
1475 escape_monster(target_ptr, turn_flags_ptr, m_ptr, m_name);
1476 check_quest_completion(target_ptr, m_ptr);
1477 delete_monster_idx(target_ptr, m_idx);
1483 * @brief HPが1/3未満になった有効的なユニークモンスターの逃走処理を行う
1484 * @param target_ptr プレーヤーへの参照ポインタ
1485 * @param is_riding_mon 騎乗状態ならばTRUE
1486 * @param m_ptr モンスターへの参照ポインタ
1487 * @param m_name モンスター名称
1488 * @param see_m モンスターが視界内にいたらTRUE
1491 void escape_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, GAME_TEXT *m_name)
1493 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1494 if (turn_flags_ptr->is_riding_mon)
1496 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
1497 if (rakuba(target_ptr, -1, FALSE))
1499 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
1503 if (turn_flags_ptr->see_m)
1505 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) &&
1506 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))
1508 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
1511 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s reads a scroll of teleport level."), m_name);
1512 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
1515 if (turn_flags_ptr->is_riding_mon && rakuba(target_ptr, -1, FALSE))
1517 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
1523 * @brief モンスターに魔法を試行させる
1524 * @param target_ptr プレーヤーへの参照ポインタ
1525 * @param m_idx モンスターID
1526 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1527 * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
1529 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware)
1531 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1532 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1533 if ((r_ptr->freq_spell == 0) || (randint1(100) > r_ptr->freq_spell))
1536 bool counterattack = FALSE;
1537 if (m_ptr->target_y)
1539 MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
1540 if (t_m_idx && are_enemies(target_ptr, m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
1541 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1543 counterattack = TRUE;
1549 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
1550 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
1554 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
1555 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
1563 * todo 少し長いが、これといってブロックとしてまとまった部分もないので暫定でこのままとする
1564 * @brief モンスターの移動に関するメインルーチン
1565 * @param target_ptr プレーヤーへの参照ポインタ
1566 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1567 * @param m_idx モンスターID
1568 * @param mm モンスターの移動方向
1569 * @param oy 移動前の、モンスターのY座標
1570 * @param ox 移動前の、モンスターのX座標
1571 * @param count 移動回数 (のはず todo)
1572 * @return 移動が阻害される何か (ドア等)があったらFALSE
1574 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)
1576 for (int i = 0; mm[i]; i++)
1579 if (d == 5) d = ddd[randint0(8)];
1581 POSITION ny = oy + ddy[d];
1582 POSITION nx = ox + ddx[d];
1583 if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx)) continue;
1586 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1587 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1588 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1589 bool can_cross = monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0);
1591 if (!process_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx, can_cross))
1593 if (!process_door(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1597 if (!process_protection_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1599 if (!process_explosive_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1603 exe_monster_attack_to_player(target_ptr, turn_flags_ptr, m_idx, ny, nx);
1604 if (process_monster_attack_to_monster(target_ptr, turn_flags_ptr, m_idx, g_ptr, can_cross)) return FALSE;
1606 if (turn_flags_ptr->is_riding_mon)
1608 if (!target_ptr->riding_ryoute && !MON_MONFEAR(&target_ptr->current_floor_ptr->m_list[target_ptr->riding])) turn_flags_ptr->do_move = FALSE;
1611 if (!process_post_dig_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx)) return FALSE;
1613 if (turn_flags_ptr->must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
1615 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0))
1616 turn_flags_ptr->do_move = FALSE;
1619 if (turn_flags_ptr->do_move && !can_cross && !turn_flags_ptr->did_kill_wall && !turn_flags_ptr->did_bash_door)
1620 turn_flags_ptr->do_move = FALSE;
1622 if (turn_flags_ptr->do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
1624 if (is_original_ap_and_seen(target_ptr, m_ptr))
1625 r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
1627 turn_flags_ptr->do_move = FALSE;
1630 if (!turn_flags_ptr->do_move)
1632 if (turn_flags_ptr->do_turn) break;
1637 turn_flags_ptr->do_turn = TRUE;
1638 feature_type *f_ptr;
1639 f_ptr = &f_info[g_ptr->feat];
1640 if (have_flag(f_ptr->flags, FF_TREE))
1642 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
1644 m_ptr->energy_need += ENERGY_NEED();
1648 if (!update_riding_monster(target_ptr, turn_flags_ptr, m_idx, oy, ox, ny, nx)) break;
1650 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1653 (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
1654 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
1656 if (is_hostile(m_ptr))
1657 disturb(target_ptr, FALSE, TRUE);
1660 bool is_takable_or_killable = g_ptr->o_idx > 0;
1661 is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) != 0;
1663 bool is_pickup_items = (target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) != 0;
1664 is_pickup_items &= (r_ptr->flags2 & RF2_TAKE_ITEM) != 0;
1666 is_takable_or_killable &= !is_pet(m_ptr) || is_pickup_items;
1667 if (!is_takable_or_killable)
1669 if (turn_flags_ptr->do_turn) break;
1674 update_object_by_monster_movement(target_ptr, turn_flags_ptr, m_idx, ny, nx);
1675 if (turn_flags_ptr->do_turn) break;
1685 * @brief 騎乗中のモンスター情報を更新する
1686 * @param target_ptr プレーヤーへの参照ポインタ
1687 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1688 * @param m_idx モンスターID
1689 * @param oy 移動前の、モンスターのY座標
1690 * @param ox 移動前の、モンスターのX座標
1691 * @param ny 移動後の、モンスターのY座標
1692 * @param ox 移動後の、モンスターのX座標
1693 * @return アイテム等に影響を及ぼしたらTRUE
1695 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)
1697 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1699 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1700 monster_type *y_ptr;
1701 y_ptr = &target_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
1702 if (turn_flags_ptr->is_riding_mon)
1703 return move_player_effect(target_ptr, ny, nx, MPE_DONT_PICKUP);
1705 target_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
1710 update_monster(target_ptr, g_ptr->m_idx, TRUE);
1713 g_ptr->m_idx = m_idx;
1716 update_monster(target_ptr, m_idx, TRUE);
1718 lite_spot(target_ptr, oy, ox);
1719 lite_spot(target_ptr, ny, nx);
1725 * @brief updateフィールドを更新する
1726 * @param target_ptr プレーヤーへの参照ポインタ
1727 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1730 void update_player_type(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_race *r_ptr)
1732 if (turn_flags_ptr->do_view)
1734 target_ptr->update |= (PU_FLOW);
1735 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1738 if (turn_flags_ptr->do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
1739 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !target_ptr->phase_out)))
1741 target_ptr->update |= (PU_MON_LITE);
1747 * @brief モンスターのフラグを更新する
1748 * @param target_ptr プレーヤーへの参照ポインタ
1749 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1750 * @param m_ptr モンスターへの参照ポインタ
1753 void update_monster_race_flags(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr)
1755 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1756 if (!is_original_ap_and_seen(target_ptr, m_ptr)) return;
1758 if (turn_flags_ptr->did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
1759 if (turn_flags_ptr->did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1760 if (turn_flags_ptr->did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
1761 if (turn_flags_ptr->did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
1762 if (turn_flags_ptr->did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
1763 if (turn_flags_ptr->did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
1764 if (turn_flags_ptr->did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
1769 * @brief モンスターの恐怖状態を処理する
1770 * @param target_ptr プレーヤーへの参照ポインタ
1771 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1772 * @param m_idx モンスターID
1773 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1774 * @return モンスターが戦いを決意したらTRUE
1776 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
1778 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1779 bool is_battle_determined = !turn_flags_ptr->do_turn && !turn_flags_ptr->do_move && MON_MONFEAR(m_ptr) && turn_flags_ptr->aware;
1780 if (!is_battle_determined) return FALSE;
1782 (void)set_monster_monfear(target_ptr, m_idx, 0);
1783 if (!turn_flags_ptr->see_m) return TRUE;
1785 GAME_TEXT m_name[MAX_NLEN];
1786 monster_desc(target_ptr, m_name, m_ptr, 0);
1787 msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
1793 * @brief 全モンスターのターン管理メインルーチン /
1794 * Process all the "live" monsters, once per game turn.
1797 * During each game current game turn, we scan through the list of all the "live" monsters,\n
1798 * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
1799 * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
1801 * Note that monsters can never move in the monster array (except when the\n
1802 * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
1804 * This function is responsible for at least half of the processor time\n
1805 * on a normal system with a "normal" amount of monsters and a player doing\n
1808 * When the player is resting, virtually 90% of the processor time is spent\n
1809 * in this function, and its children, "process_monster()" and "make_move()".\n
1811 * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
1812 * especially when the player is running.\n
1814 * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
1815 * monsters while they are still being "born". A monster is "fresh" only\n
1816 * during the game turn in which it is created, and we use the "hack_m_idx" to\n
1817 * determine if the monster is yet to be processed during the game turn.\n
1819 * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
1820 * move before any "nasty" monsters get to use their spell attacks.\n
1822 * Note that when the "knowledge" about the currently tracked monster\n
1823 * changes (flags, attacks, spells), we induce a redraw of the monster\n
1826 void process_monsters(player_type *target_ptr)
1828 old_race_flags tmp_flags;
1829 old_race_flags *old_race_flags_ptr = init_old_race_flags(&tmp_flags);
1831 floor_type *floor_ptr = target_ptr->current_floor_ptr;
1832 floor_ptr->monster_noise = FALSE;
1834 MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
1835 save_old_race_flags(target_ptr->monster_race_idx, old_race_flags_ptr);
1836 sweep_monster_process(target_ptr);
1839 if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
1842 update_player_window(target_ptr->monster_race_idx, &target_ptr->window, old_race_flags_ptr);
1847 * @brief フロア内のモンスターについてターン終了時の処理を繰り返す
1848 * @param target_ptr プレーヤーへの参照ポインタ
1850 void sweep_monster_process(player_type *target_ptr)
1852 floor_type *floor_ptr = target_ptr->current_floor_ptr;
1853 for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--)
1855 monster_type *m_ptr;
1856 m_ptr = &floor_ptr->m_list[i];
1858 if (target_ptr->leaving) return;
1859 if (!monster_is_valid(m_ptr)) continue;
1860 if (target_ptr->wild_mode) continue;
1862 if (m_ptr->mflag & MFLAG_BORN)
1864 m_ptr->mflag &= ~(MFLAG_BORN);
1868 if (m_ptr->cdis >= AAF_LIMIT) continue;
1869 if (!decide_process_continue(target_ptr, m_ptr)) continue;
1871 SPEED speed = (target_ptr->riding == i) ? target_ptr->pspeed : decide_monster_speed(m_ptr);
1872 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
1873 if (m_ptr->energy_need > 0) continue;
1875 m_ptr->energy_need += ENERGY_NEED();
1877 process_monster(target_ptr, i);
1878 reset_target(m_ptr);
1880 if (target_ptr->no_flowed && one_in_(3))
1881 m_ptr->mflag2 |= MFLAG2_NOFLOW;
1883 if (!target_ptr->playing || target_ptr->is_dead) return;
1884 if (target_ptr->leaving) return;
1890 * @brief 後続のモンスター処理が必要かどうか判定する (要調査)
1891 * @param target_ptr プレーヤーへの参照ポインタ
1892 * @param m_ptr モンスターへの参照ポインタ
1893 * @return 後続処理が必要ならTRUE
1895 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr)
1897 monster_race *r_ptr;
1898 r_ptr = &r_info[m_ptr->r_idx];
1899 if (!target_ptr->no_flowed)
1901 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
1904 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
1907 if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) &&
1908 (player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) || (target_ptr->cursed & TRC_AGGRAVATE)))
1911 if (m_ptr->target_y)