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/monster-update.h"
23 #include "monster/quantum-effect.h"
30 #include "spells-floor.h"
31 #include "spells-summon.h"
34 #include "realm-hex.h"
37 #include "player-move.h"
38 #include "monster-status.h"
39 #include "monster-spell.h"
40 #include "monster-process.h"
41 #include "monster-dist-offsets.h"
42 #include "monsterrace-hook.h"
45 #include "view-mainwindow.h"
47 bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm);
48 void decide_enemy_approch_direction(player_type *target_ptr, MONSTER_IDX m_idx, int start, int plus, POSITION *y, POSITION *x);
49 bool decide_pet_approch_direction(player_type *target_ptr, monster_type *m_ptr, monster_type *t_ptr);
51 bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp);
52 coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONSTER_IDX m_idx, const POSITION *y_offsets, const POSITION *x_offsets, int d);
54 bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp);
55 void sweep_hiding_candidate(player_type *target_ptr, monster_type *m_ptr, const POSITION *y_offsets, const POSITION *x_offsets, coordinate_candidate *candidate);
57 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon);
58 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx);
59 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
60 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx);
61 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
62 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx);
63 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox);
64 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware);
65 bool random_walk(player_type *target_ptr, DIRECTION *mm, monster_type *m_ptr);
66 bool decide_pet_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx);
67 bool runaway_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
68 void escape_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, GAME_TEXT *m_name);
69 void process_special(player_type *target_ptr, MONSTER_IDX m_idx);
70 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware);
71 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware);
73 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);
74 bool process_post_dig_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
75 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
77 void sweep_monster_process(player_type *target_ptr);
78 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr);
81 * @brief モンスターが敵に接近するための方向を計算するメインルーチン
82 * Calculate the direction to the next enemy
83 * @param target_ptr プレーヤーへの参照ポインタ
84 * @param m_idx モンスターの参照ID
85 * @param mm 移動するべき方角IDを返す参照ポインタ
86 * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
88 bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm)
90 floor_type *floor_ptr = target_ptr->current_floor_ptr;
91 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
93 POSITION x = 0, y = 0;
94 if (target_ptr->riding_t_m_idx && player_bold(target_ptr, m_ptr->fy, m_ptr->fx))
96 y = floor_ptr->m_list[target_ptr->riding_t_m_idx].fy;
97 x = floor_ptr->m_list[target_ptr->riding_t_m_idx].fx;
99 else if (is_pet(m_ptr) && target_ptr->pet_t_m_idx)
101 y = floor_ptr->m_list[target_ptr->pet_t_m_idx].fy;
102 x = floor_ptr->m_list[target_ptr->pet_t_m_idx].fx;
108 if (target_ptr->phase_out)
110 start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
111 if (randint0(2)) plus = -1;
115 start = floor_ptr->m_max + 1;
118 decide_enemy_approch_direction(target_ptr, m_idx, start, plus, &y, &x);
120 if ((x == 0) && (y == 0)) return FALSE;
126 store_enemy_approch_direction(mm, y, x);
132 * @brief モンスターが敵に接近するための方向を決定する
133 * @param target_ptr プレーヤーへの参照ポインタ
134 * @param m_idx モンスターID
135 * @param start モンスターIDの開始
136 * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
137 * @param y モンスターの移動方向Y
138 * @param x モンスターの移動方向X
141 void decide_enemy_approch_direction(player_type *target_ptr, MONSTER_IDX m_idx, int start, int plus, POSITION *y, POSITION *x)
143 floor_type *floor_ptr = target_ptr->current_floor_ptr;
144 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
145 monster_race *r_ptr = &r_info[m_ptr->r_idx];
146 for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus)
148 MONSTER_IDX dummy = (i % floor_ptr->m_max);
149 if (dummy == 0) continue;
151 MONSTER_IDX t_idx = dummy;
153 t_ptr = &floor_ptr->m_list[t_idx];
154 if (t_ptr == m_ptr) continue;
155 if (!monster_is_valid(t_ptr)) continue;
156 if (decide_pet_approch_direction(target_ptr, m_ptr, t_ptr)) continue;
157 if (!are_enemies(target_ptr, m_ptr, t_ptr)) continue;
159 if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) ||
160 ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)))
162 if (!in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
166 if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
177 * @brief ペットが敵に接近するための方向を決定する
178 * @param target_ptr プレーヤーへの参照ポインタ
179 * @param m_ptr 移動を試みているモンスターへの参照ポインタ
180 * @param t_ptr 移動先モンスターへの参照ポインタ
181 * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
182 * @return ペットがモンスターに近づくならばTRUE
184 bool decide_pet_approch_direction(player_type *target_ptr, monster_type *m_ptr, monster_type *t_ptr)
186 monster_race *r_ptr = &r_info[m_ptr->r_idx];
187 if (!is_pet(m_ptr)) return FALSE;
189 if (target_ptr->pet_follow_distance < 0)
191 if (t_ptr->cdis <= (0 - target_ptr->pet_follow_distance))
196 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > target_ptr->pet_follow_distance))
201 return (r_ptr->aaf < t_ptr->cdis);
206 * @brief モンスターがプレイヤーから逃走するかどうかを返す /
207 * Returns whether a given monster will try to run from the player.
208 * @param m_idx 逃走するモンスターの参照ID
209 * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
211 * Monsters will attempt to avoid very powerful players. See below.\n
213 * Because this function is called so often, little details are important\n
214 * for efficiency. Like not using "mod" or "div" when possible. And\n
215 * attempting to check the conditions in an optimal order. Note that\n
216 * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
218 * Note that this function is responsible for about one to five percent\n
219 * of the processor use in normal conditions...\n
221 static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
223 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
224 monster_race *r_ptr = &r_info[m_ptr->r_idx];
228 return ((target_ptr->pet_follow_distance < 0) &&
229 (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
232 if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
233 if (MON_MONFEAR(m_ptr)) return TRUE;
234 if (m_ptr->cdis <= 5) return FALSE;
236 PLAYER_LEVEL p_lev = target_ptr->lev;
237 DEPTH m_lev = r_ptr->level + (m_idx & 0x08) + 25;
238 if (m_lev > p_lev + 4) return FALSE;
239 if (m_lev + 4 <= p_lev) return TRUE;
241 HIT_POINT p_chp = target_ptr->chp;
242 HIT_POINT p_mhp = target_ptr->mhp;
243 HIT_POINT m_chp = m_ptr->hp;
244 HIT_POINT m_mhp = m_ptr->maxhp;
245 u32b p_val = (p_lev * p_mhp) + (p_chp << 2);
246 u32b m_val = (m_lev * m_mhp) + (m_chp << 2);
247 if (p_val * m_mhp > m_val * p_mhp) return TRUE;
254 * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
255 * Search spell castable grid
256 * @param target_ptr プレーヤーへの参照ポインタ
257 * @param m_idx モンスターの参照ID
258 * @param yp 適したマスのY座標を返す参照ポインタ
259 * @param xp 適したマスのX座標を返す参照ポインタ
260 * @return 有効なマスがあった場合TRUEを返す
262 static bool get_moves_aux2(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
264 floor_type *floor_ptr = target_ptr->current_floor_ptr;
265 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
266 monster_race *r_ptr = &r_info[m_ptr->r_idx];
268 POSITION y1 = m_ptr->fy;
269 POSITION x1 = m_ptr->fx;
271 if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
273 int now_cost = floor_ptr->grid_array[y1][x1].cost;
274 if (now_cost == 0) now_cost = 999;
276 bool can_open_door = FALSE;
277 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
279 can_open_door = TRUE;
283 for (int i = 7; i >= 0; i--)
285 POSITION y = y1 + ddy_ddd[i];
286 POSITION x = x1 + ddx_ddd[i];
287 if (!in_bounds2(floor_ptr, y, x)) continue;
288 if (player_bold(target_ptr, y, x)) return FALSE;
291 g_ptr = &floor_ptr->grid_array[y][x];
292 int cost = g_ptr->cost;
293 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))))
295 if (cost == 0) continue;
296 if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat)) continue;
299 if (cost == 0) cost = 998;
301 if (now_cost < cost) continue;
302 if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x)) continue;
303 if (best < cost) continue;
306 *yp = y1 + ddy_ddd[i];
307 *xp = x1 + ddx_ddd[i];
310 if (best == 999) return FALSE;
317 * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
318 * Choose the "best" direction for "flowing"
319 * @param m_idx モンスターの参照ID
320 * @param yp 移動先のマスのY座標を返す参照ポインタ
321 * @param xp 移動先のマスのX座標を返す参照ポインタ
322 * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
323 * @return 有効なマスがあった場合TRUEを返す
325 * Note that ghosts and rock-eaters are never allowed to "flow",\n
326 * since they should move directly towards the player.\n
328 * Prefer "non-diagonal" directions, but twiddle them a little\n
329 * to angle slightly towards the player's actual location.\n
331 * Allow very perceptive monsters to track old "spoor" left by\n
332 * previous locations occupied by the player. This will tend\n
333 * to have monsters end up either near the player or on a grid\n
334 * recently occupied by the player (and left via "teleport").\n
336 * Note that if "smell" is turned on, all monsters get vicious.\n
338 * Also note that teleporting away from a location will cause\n
339 * the monsters who were chasing you to converge on that location\n
340 * as long as you are still near enough to "annoy" them without\n
341 * being close enough to chase directly. I have no idea what will\n
342 * happen if you combine "smell" with low "aaf" values.\n
344 static bool get_moves_aux(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
347 floor_type *floor_ptr = target_ptr->current_floor_ptr;
348 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
349 monster_race *r_ptr = &r_info[m_ptr->r_idx];
351 if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
352 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
353 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
355 if (get_moves_aux2(target_ptr, m_idx, yp, xp)) return TRUE;
358 if (no_flow) return FALSE;
359 if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return FALSE;
360 if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return FALSE;
362 POSITION y1 = m_ptr->fy;
363 POSITION x1 = m_ptr->fx;
364 if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return FALSE;
366 g_ptr = &floor_ptr->grid_array[y1][x1];
369 bool use_scent = FALSE;
374 else if (g_ptr->when)
376 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return FALSE;
386 for (int i = 7; i >= 0; i--)
388 POSITION y = y1 + ddy_ddd[i];
389 POSITION x = x1 + ddx_ddd[i];
391 if (!in_bounds2(floor_ptr, y, x)) continue;
393 g_ptr = &floor_ptr->grid_array[y][x];
396 int when = g_ptr->when;
397 if (best > when) continue;
404 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
413 if ((cost == 0) || (best < cost)) continue;
418 *yp = target_ptr->y + 16 * ddy_ddd[i];
419 *xp = target_ptr->x + 16 * ddx_ddd[i];
422 if (best == 999 || best == 0) return FALSE;
429 * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
430 * Provide a location to flee to, but give the player a wide berth.
431 * @param m_idx モンスターの参照ID
432 * @param yp 移動先のマスのY座標を返す参照ポインタ
433 * @param xp 移動先のマスのX座標を返す参照ポインタ
434 * @return 有効なマスがあった場合TRUEを返す
436 * A monster may wish to flee to a location that is behind the player,\n
437 * but instead of heading directly for it, the monster should "swerve"\n
438 * around the player so that he has a smaller chance of getting hit.\n
440 static bool get_fear_moves_aux(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
442 POSITION gy = 0, gx = 0;
444 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
445 POSITION fy = m_ptr->fy;
446 POSITION fx = m_ptr->fx;
448 POSITION y1 = fy - (*yp);
449 POSITION x1 = fx - (*xp);
452 for (int i = 7; i >= 0; i--)
454 POSITION y = fy + ddy_ddd[i];
455 POSITION x = fx + ddx_ddd[i];
456 if (!in_bounds2(floor_ptr, y, x)) continue;
458 POSITION dis = distance(y, x, y1, x1);
459 POSITION s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
462 if (s < score) continue;
469 if (score == -1) return FALSE;
479 * @brief モンスターが逃げ込める安全な地点を返す /
480 * Choose a "safe" location near a monster for it to run toward.
481 * @param target_ptr プレーヤーへの参照ポインタ
482 * @param m_idx モンスターの参照ID
483 * @param yp 移動先のマスのY座標を返す参照ポインタ
484 * @param xp 移動先のマスのX座標を返す参照ポインタ
485 * @return 有効なマスがあった場合TRUEを返す
487 * A location is "safe" if it can be reached quickly and the player\n
488 * is not able to fire into it (it isn't a "clean shot"). So, this will\n
489 * cause monsters to "duck" behind walls. Hopefully, monsters will also\n
490 * try to run towards corridor openings if they are in a room.\n
492 * This function may take lots of CPU time if lots of monsters are\n
495 * Return TRUE if a safe location is available.\n
497 bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
499 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
500 for (POSITION d = 1; d < 10; d++)
502 const POSITION *y_offsets;
503 y_offsets = dist_offsets_y[d];
505 const POSITION *x_offsets;
506 x_offsets = dist_offsets_x[d];
508 coordinate_candidate candidate = sweep_safe_coordinate(target_ptr, m_idx, y_offsets, x_offsets, d);
510 if (candidate.gdis <= 0) continue;
512 *yp = m_ptr->fy - candidate.gy;
513 *xp = m_ptr->fx - candidate.gx;
523 * @brief モンスターが逃げ込める地点を走査する
524 * @param target_ptr プレーヤーへの参照ポインタ
525 * @param m_idx モンスターID
528 * @param d モンスターがいる地点からの距離
529 * @return 逃げ込める地点の候補地
531 coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONSTER_IDX m_idx, const POSITION *y_offsets, const POSITION *x_offsets, int d)
533 coordinate_candidate candidate = init_coordinate_candidate();
534 floor_type *floor_ptr = target_ptr->current_floor_ptr;
535 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
536 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
538 i++, dx = x_offsets[i], dy = y_offsets[i])
540 POSITION y = m_ptr->fy + dy;
541 POSITION x = m_ptr->fx + dx;
542 if (!in_bounds(floor_ptr, y, x)) continue;
545 g_ptr = &floor_ptr->grid_array[y][x];
547 BIT_FLAGS16 riding_mode = (m_idx == target_ptr->riding) ? CEM_RIDING : 0;
548 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], riding_mode))
551 if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
553 if (g_ptr->dist == 0) continue;
554 if (g_ptr->dist > floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist + 2 * d) continue;
557 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x)) continue;
559 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
560 if (dis <= candidate.gdis) continue;
564 candidate.gdis = dis;
572 * @brief モンスターが隠れ潜める地点を返す /
573 * Choose a good hiding place near a monster for it to run toward.
574 * @param target_ptr プレーヤーへの参照ポインタ
575 * @param m_idx モンスターの参照ID
576 * @param yp 移動先のマスのY座標を返す参照ポインタ
577 * @param xp 移動先のマスのX座標を返す参照ポインタ
578 * @return 有効なマスがあった場合TRUEを返す
580 * Pack monsters will use this to "ambush" the player and lure him out\n
581 * of corridors into open space so they can swarm him.\n
583 * Return TRUE if a good location is available.\n
585 bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
587 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
588 coordinate_candidate candidate = init_coordinate_candidate();
589 candidate.gdis = 999;
591 for (POSITION d = 1; d < 10; d++)
593 const POSITION *y_offsets;
594 y_offsets = dist_offsets_y[d];
596 const POSITION *x_offsets;
597 x_offsets = dist_offsets_x[d];
599 sweep_hiding_candidate(target_ptr, m_ptr, y_offsets, x_offsets, &candidate);
600 if (candidate.gdis >= 999) continue;
602 *yp = m_ptr->fy - candidate.gy;
603 *xp = m_ptr->fx - candidate.gx;
612 * @brief モンスターが隠れられる地点を走査する
613 * @param target_ptr プレーヤーへの参照ポインタ
614 * @param m_idx モンスターID
617 * @param candidate 隠れられる地点の候補地
620 void sweep_hiding_candidate(player_type *target_ptr, monster_type *m_ptr, const POSITION *y_offsets, const POSITION *x_offsets, coordinate_candidate *candidate)
622 monster_race *r_ptr = &r_info[m_ptr->r_idx];
623 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
625 i++, dx = x_offsets[i], dy = y_offsets[i])
627 POSITION y = m_ptr->fy + dy;
628 POSITION x = m_ptr->fx + dx;
629 if (!in_bounds(target_ptr->current_floor_ptr, y, x)) continue;
630 if (!monster_can_enter(target_ptr, y, x, r_ptr, 0)) continue;
631 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))
634 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
635 if (dis < candidate->gdis && dis >= 2)
639 candidate->gdis = dis;
646 * todo 分割したいが条件が多すぎて適切な関数名と詳細処理を追いきれない……
647 * @brief モンスターの移動方向を返す /
648 * Choose "logical" directions for monster movement
649 * @param target_ptr プレーヤーへの参照ポインタ
650 * @param m_idx モンスターの参照ID
651 * @param mm 移動方向を返す方向IDの参照ポインタ
652 * @return 有効方向があった場合TRUEを返す
654 static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
656 floor_type *floor_ptr = target_ptr->current_floor_ptr;
657 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
658 monster_race *r_ptr = &r_info[m_ptr->r_idx];
659 POSITION y = 0, x = 0;
660 POSITION y2 = target_ptr->y;
661 POSITION x2 = target_ptr->x;
663 bool will_run = mon_will_run(target_ptr, m_idx);
665 bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) != 0) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2);
666 bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) != 0) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall);
668 if (!will_run && m_ptr->target_y)
670 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
672 are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) &&
673 los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
674 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
676 y = m_ptr->fy - m_ptr->target_y;
677 x = m_ptr->fx - m_ptr->target_x;
682 if (!done && !will_run && is_hostile(m_ptr) &&
683 (r_ptr->flags1 & RF1_FRIENDS) &&
684 ((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)) ||
685 (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
687 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
688 !(r_ptr->flags2 & RF2_KILL_WALL))
691 for (int i = 0; i < 8; i++)
693 int xx = target_ptr->x + ddx_ddd[i];
694 int yy = target_ptr->y + ddy_ddd[i];
696 if (!in_bounds2(floor_ptr, yy, xx)) continue;
698 g_ptr = &floor_ptr->grid_array[yy][xx];
699 if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0))
705 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
706 if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
708 if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
709 (target_ptr->mhp + target_ptr->msp))
711 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
715 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
717 for (int i = 0; i < 8; i++)
719 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
720 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
721 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
728 if (!in_bounds2(floor_ptr, y2, x2)) continue;
729 if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0)) continue;
742 (void)get_moves_aux(target_ptr, m_idx, &y2, &x2, no_flow);
747 if (is_pet(m_ptr) && will_run)
753 if (!done && will_run)
757 if (find_safety(target_ptr, m_idx, &y, &x) && !no_flow)
759 if (get_fear_moves_aux(target_ptr->current_floor_ptr, m_idx, &y, &x))
771 if (!x && !y) return FALSE;
773 store_moves_val(mm, y, x);
779 * @brief モンスター単体の1ターン行動処理メインルーチン /
781 * @param target_ptr プレーヤーへの参照ポインタ
782 * @param m_idx 行動モンスターの参照ID
785 * The monster is known to be within 100 grids of the player\n
787 * In several cases, we directly update the monster lore\n
789 * Note that a monster is only allowed to "reproduce" if there\n
790 * are a limited number of "reproducing" monsters on the current\n
791 * level. This should prevent the level from being "swamped" by\n
792 * reproducing monsters. It also allows a large mass of mice to\n
793 * prevent a louse from multiplying, but this is a small price to\n
794 * pay for a simple multiplication method.\n
796 * XXX Monster fear is slightly odd, in particular, monsters will\n
797 * fixate on opening a door even if they cannot open it. Actually,\n
798 * the same thing happens to normal monsters when they hit a door\n
800 * In addition, monsters which *cannot* open or bash\n
801 * down a door will still stand there trying to open it...\n
803 * XXX Technically, need to check for monster in the way\n
804 * combined with that monster being in a wall (or door?)\n
806 * A "direction" of "5" means "pick a random direction".\n
808 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
810 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
811 monster_race *r_ptr = &r_info[m_ptr->r_idx];
812 turn_flags tmp_flags;
813 turn_flags *turn_flags_ptr = init_turn_flags(target_ptr->riding, m_idx, &tmp_flags);
814 turn_flags_ptr->see_m = is_seen(m_ptr);
816 decide_drop_from_monster(target_ptr, m_idx, turn_flags_ptr->is_riding_mon);
817 if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
819 choose_new_monster(target_ptr, m_idx, FALSE, 0);
820 r_ptr = &r_info[m_ptr->r_idx];
823 turn_flags_ptr->aware = process_stealth(target_ptr, m_idx);
824 if (vanish_summoned_children(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
825 if (process_quantum_effect(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
826 if (explode_grenade(target_ptr, m_idx)) return;
827 if (runaway_monster(target_ptr, turn_flags_ptr, m_idx)) return;
829 awake_monster(target_ptr, m_idx);
830 if (MON_STUNNED(m_ptr))
832 if (one_in_(2)) return;
835 if (turn_flags_ptr->is_riding_mon)
837 target_ptr->update |= (PU_BONUS);
840 process_angar(target_ptr, m_idx, turn_flags_ptr->see_m);
842 POSITION oy = m_ptr->fy;
843 POSITION ox = m_ptr->fx;
844 if (decide_monster_multiplication(target_ptr, m_idx, oy, ox)) return;
846 process_special(target_ptr, m_idx);
847 process_speak_sound(target_ptr, m_idx, oy, ox, turn_flags_ptr->aware);
848 if (cast_spell(target_ptr, m_idx, turn_flags_ptr->aware)) return;
851 mm[0] = mm[1] = mm[2] = mm[3] = 0;
852 mm[4] = mm[5] = mm[6] = mm[7] = 0;
854 if (!decide_monster_movement_direction(target_ptr, mm, m_idx, turn_flags_ptr->aware)) return;
857 if (!process_monster_movement(target_ptr, turn_flags_ptr, m_idx, mm, oy, ox, &count)) return;
860 * Forward movements failed, but now received LOS attack!
861 * Try to flow by smell.
863 if (target_ptr->no_flowed && count > 2 && m_ptr->target_y)
864 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
866 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)
868 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
870 if (make_attack_spell(m_idx, target_ptr)) return;
874 update_player_type(target_ptr, turn_flags_ptr, r_ptr);
875 update_monster_race_flags(target_ptr, turn_flags_ptr, m_ptr);
877 if (!process_monster_fear(target_ptr, turn_flags_ptr, m_idx)) return;
879 if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
885 * @param target_ptr プレーヤーへの参照ポインタ
886 * @param m_idx モンスターID
887 * @return モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
889 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx)
891 if ((target_ptr->special_defense & NINJA_S_STEALTH) == 0) return TRUE;
893 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
894 monster_race *r_ptr = &r_info[m_ptr->r_idx];
895 int tmp = target_ptr->lev * 6 + (target_ptr->skill_stl + 10) * 4;
896 if (target_ptr->monlite) tmp /= 3;
897 if (target_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
898 if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10)) tmp /= 3;
899 return (randint0(tmp) <= (r_ptr->level + 20));
904 * @brief 死亡したモンスターが乗馬中のモンスターだった場合に落馬処理を行う
905 * @param target_ptr プレーヤーへの参照ポインタ
906 * @param m_idx モンスターID
907 * @param is_riding_mon 騎乗中であればTRUE
910 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon)
912 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
913 monster_race *r_ptr = &r_info[m_ptr->r_idx];
914 if (!is_riding_mon || ((r_ptr->flags7 & RF7_RIDING) != 0)) return;
916 if (rakuba(target_ptr, 0, TRUE))
919 msg_print("地面に落とされた。");
921 GAME_TEXT m_name[MAX_NLEN];
922 monster_desc(target_ptr, m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
923 msg_format("You have fallen from %s.", m_name);
930 * @brief 召喚の親元が消滅した時、子供も消滅させる
931 * @param target_ptr プレーヤーへの参照ポインタ
932 * @param m_idx モンスターID
933 * @param see_m モンスターが視界内にいたらTRUE
934 * @return 召喚モンスターが消滅したらTRUE
936 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
938 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
939 if ((m_ptr->parent_m_idx == 0) || (target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx > 0))
944 GAME_TEXT m_name[MAX_NLEN];
945 monster_desc(target_ptr, m_name, m_ptr, 0);
946 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
949 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
951 GAME_TEXT m_name[MAX_NLEN];
952 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
953 exe_write_diary(target_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
956 delete_monster_idx(target_ptr, m_idx);
962 * @brief 寝ているモンスターの起床を判定する
963 * @param target_ptr プレーヤーへの参照ポインタ
964 * @param m_idx モンスターID
967 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx)
969 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
970 monster_race *r_ptr = &r_info[m_ptr->r_idx];
971 if (!MON_CSLEEP(m_ptr)) return;
972 if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
974 (void)set_monster_csleep(target_ptr, m_idx, 0);
977 GAME_TEXT m_name[MAX_NLEN];
978 monster_desc(target_ptr, m_name, m_ptr, 0);
979 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
982 if (is_original_ap_and_seen(target_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
990 * @brief モンスターの怒り状態を判定する (起こっていたら敵に回す)
991 * @param target_ptr プレーヤーへの参照ポインタ
992 * @param m_idx モンスターID
993 * @param see_m モンスターが視界内にいたらTRUE
996 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
998 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
999 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1000 bool gets_angry = FALSE;
1001 if (is_friendly(m_ptr) && (target_ptr->cursed & TRC_AGGRAVATE))
1004 if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
1005 monster_has_hostile_align(target_ptr, NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
1010 if (target_ptr->phase_out || !gets_angry) return;
1012 if (is_pet(m_ptr) || see_m)
1014 GAME_TEXT m_name[MAX_NLEN];
1015 monster_desc(target_ptr, m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
1016 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
1019 set_hostile(target_ptr, m_ptr);
1025 * @param target_ptr プレーヤーへの参照ポインタ
1026 * @param m_idx モンスターID
1029 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx)
1031 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1032 if (m_ptr->r_idx != MON_GRENADE) return FALSE;
1035 mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1041 * @brief モンスター依存の特別な行動を取らせる
1042 * @param target_ptr プレーヤーへの参照ポインタ
1043 * @param m_idx モンスターID
1046 void process_special(player_type *target_ptr, MONSTER_IDX m_idx)
1048 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1049 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1050 if ((r_ptr->a_ability_flags2 & RF6_SPECIAL) == 0) return;
1051 if (m_ptr->r_idx != MON_OHMU) return;
1052 if (target_ptr->current_floor_ptr->inside_arena || target_ptr->phase_out) return;
1053 if ((r_ptr->freq_spell == 0) || !(randint1(100) <= r_ptr->freq_spell)) return;
1056 DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1057 BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
1059 for (int k = 0; k < A_MAX; k++)
1061 if (summon_specific(target_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
1063 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
1067 if (count && is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
1072 * @brief モンスターを喋らせたり足音を立てたりする
1073 * @param target_ptr プレーヤーへの参照ポインタ
1074 * @param m_idx モンスターID
1075 * @param oy モンスターが元々いたY座標
1076 * @param ox モンスターが元々いたX座標
1077 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1080 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware)
1082 if (target_ptr->phase_out) return;
1084 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1085 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1086 if (m_ptr->ap_r_idx == MON_CYBER &&
1087 one_in_(CYBERNOISE) &&
1088 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
1090 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1091 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
1094 if (((ap_r_ptr->flags2 & RF2_CAN_SPEAK) == 0) || !aware ||
1095 !one_in_(SPEAK_CHANCE) ||
1096 !player_has_los_bold(target_ptr, oy, ox) ||
1097 !projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
1100 GAME_TEXT m_name[MAX_NLEN];
1101 char monmessage[1024];
1105 monster_desc(target_ptr, m_name, m_ptr, 0);
1107 strcpy(m_name, _("それ", "It"));
1109 if (MON_MONFEAR(m_ptr))
1110 filename = _("monfear_j.txt", "monfear.txt");
1111 else if (is_pet(m_ptr))
1112 filename = _("monpet_j.txt", "monpet.txt");
1113 else if (is_friendly(m_ptr))
1114 filename = _("monfrien_j.txt", "monfrien.txt");
1116 filename = _("monspeak_j.txt", "monspeak.txt");
1118 if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
1120 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
1126 * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
1127 * @param target_ptr プレーヤーへの参照ポインタ
1128 * @param m_idx モンスターID
1129 * @param oy 分裂元モンスターのY座標
1130 * @param ox 分裂元モンスターのX座標
1131 * @return 実際に分裂したらTRUEを返す
1133 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
1135 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1136 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1137 if (((r_ptr->flags2 & RF2_MULTIPLY) == 0) || (target_ptr->current_floor_ptr->num_repro >= MAX_REPRO))
1141 for (POSITION y = oy - 1; y <= oy + 1; y++)
1143 for (POSITION x = ox - 1; x <= ox + 1; x++)
1145 if (!in_bounds2(target_ptr->current_floor_ptr, y, x)) continue;
1146 if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
1150 if (multiply_barrier(target_ptr, m_idx)) k = 8;
1152 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
1154 if (multiply_monster(target_ptr, m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
1156 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(target_ptr, m_ptr))
1158 r_ptr->r_flags2 |= (RF2_MULTIPLY);
1170 * @brief モンスターの移動パターンを決定する
1171 * @param target_ptr プレーヤーへの参照ポインタ
1173 * @param m_idx モンスターID
1174 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1175 * @return 移動先が存在すればTRUE
1177 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware)
1179 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1180 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1182 if (MON_CONFUSED(m_ptr) || !aware)
1184 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1188 if (random_walk(target_ptr, mm, m_ptr)) return TRUE;
1190 if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
1192 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1196 if (decide_pet_movement_direction(target_ptr, mm, m_idx)) return TRUE;
1198 if (!is_hostile(m_ptr))
1200 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1201 get_enemy_dir(target_ptr, m_idx, mm);
1205 if (!get_moves(target_ptr, m_idx, mm)) return FALSE;
1212 * todo ↓のように書いたが、"5"とはもしかして「その場に留まる」という意味か?
1213 * @brief 不規則歩行フラグを持つモンスターの移動方向をその確率に基づいて決定する
1214 * @param target_ptr プレーヤーへの参照ポインタ
1216 * @param m_ptr モンスターへの参照ポインタ
1217 * @return 不規則な方向へ歩くことになったらTRUE
1219 bool random_walk(player_type *target_ptr, DIRECTION *mm, monster_type *m_ptr)
1221 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1222 if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) && (randint0(100) < 75))
1224 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
1226 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1230 if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 50))
1232 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_50;
1234 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1238 if ((r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 25))
1240 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
1242 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1251 * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
1252 * @param target_ptr プレーヤーへの参照ポインタ
1254 * @param m_idx モンスターID
1255 * @return モンスターがペットであればTRUE
1257 bool decide_pet_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx)
1259 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1260 if (!is_pet(m_ptr)) return FALSE;
1262 bool avoid = ((target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
1263 bool lonely = (!avoid && (m_ptr->cdis > target_ptr->pet_follow_distance));
1264 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
1265 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1266 if (get_enemy_dir(target_ptr, m_idx, mm)) return TRUE;
1267 if (!avoid && !lonely && !distant) return TRUE;
1269 POSITION dis = target_ptr->pet_follow_distance;
1270 if (target_ptr->pet_follow_distance > PET_SEEK_DIST)
1272 target_ptr->pet_follow_distance = PET_SEEK_DIST;
1275 (void)get_moves(target_ptr, m_idx, mm);
1276 target_ptr->pet_follow_distance = (s16b)dis;
1282 * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
1283 * @param target_ptr プレーヤーへの参照ポインタ
1284 * @param m_idx モンスターID
1285 * @param is_riding_mon 騎乗状態ならばTRUE
1286 * @param see_m モンスターが視界内にいたらTRUE
1287 * @return モンスターがフロアから消えたらTRUE
1289 bool runaway_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
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 bool can_runaway = is_pet(m_ptr) || is_friendly(m_ptr);
1294 can_runaway &= ((r_ptr->flags1 & RF1_UNIQUE) != 0) || ((r_ptr->flags7 & RF7_NAZGUL) != 0);
1295 can_runaway &= !target_ptr->phase_out;
1296 if (!can_runaway) return FALSE;
1298 static int riding_pinch = 0;
1300 if (m_ptr->hp >= m_ptr->maxhp / 3)
1302 /* Reset the counter */
1303 if (turn_flags_ptr->is_riding_mon) riding_pinch = 0;
1308 GAME_TEXT m_name[MAX_NLEN];
1309 monster_desc(target_ptr, m_name, m_ptr, 0);
1310 if (turn_flags_ptr->is_riding_mon && riding_pinch < 2)
1312 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
1313 "%^s seems to be in so much pain and tries to escape from your restriction."), m_name);
1315 disturb(target_ptr, TRUE, TRUE);
1319 escape_monster(target_ptr, turn_flags_ptr, m_ptr, m_name);
1320 check_quest_completion(target_ptr, m_ptr);
1321 delete_monster_idx(target_ptr, m_idx);
1327 * @brief HPが1/3未満になった有効的なユニークモンスターの逃走処理を行う
1328 * @param target_ptr プレーヤーへの参照ポインタ
1329 * @param is_riding_mon 騎乗状態ならばTRUE
1330 * @param m_ptr モンスターへの参照ポインタ
1331 * @param m_name モンスター名称
1332 * @param see_m モンスターが視界内にいたらTRUE
1335 void escape_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, GAME_TEXT *m_name)
1337 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1338 if (turn_flags_ptr->is_riding_mon)
1340 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
1341 if (rakuba(target_ptr, -1, FALSE))
1343 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
1347 if (turn_flags_ptr->see_m)
1349 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) &&
1350 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))
1352 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
1355 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s reads a scroll of teleport level."), m_name);
1356 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
1359 if (turn_flags_ptr->is_riding_mon && rakuba(target_ptr, -1, FALSE))
1361 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
1367 * @brief モンスターに魔法を試行させる
1368 * @param target_ptr プレーヤーへの参照ポインタ
1369 * @param m_idx モンスターID
1370 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1371 * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
1373 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware)
1375 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1376 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1377 if ((r_ptr->freq_spell == 0) || (randint1(100) > r_ptr->freq_spell))
1380 bool counterattack = FALSE;
1381 if (m_ptr->target_y)
1383 MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
1384 if (t_m_idx && are_enemies(target_ptr, m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
1385 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1387 counterattack = TRUE;
1393 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
1394 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
1398 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
1399 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
1407 * todo 少し長いが、これといってブロックとしてまとまった部分もないので暫定でこのままとする
1408 * @brief モンスターの移動に関するメインルーチン
1409 * @param target_ptr プレーヤーへの参照ポインタ
1410 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1411 * @param m_idx モンスターID
1412 * @param mm モンスターの移動方向
1413 * @param oy 移動前の、モンスターのY座標
1414 * @param ox 移動前の、モンスターのX座標
1415 * @param count 移動回数 (のはず todo)
1416 * @return 移動が阻害される何か (ドア等)があったらFALSE
1418 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)
1420 for (int i = 0; mm[i]; i++)
1423 if (d == 5) d = ddd[randint0(8)];
1425 POSITION ny = oy + ddy[d];
1426 POSITION nx = ox + ddx[d];
1427 if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx)) continue;
1430 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1431 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1432 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1433 bool can_cross = monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0);
1435 if (!process_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx, can_cross))
1437 if (!process_door(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1441 if (!process_protection_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1443 if (!process_explosive_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1447 exe_monster_attack_to_player(target_ptr, turn_flags_ptr, m_idx, ny, nx);
1448 if (process_monster_attack_to_monster(target_ptr, turn_flags_ptr, m_idx, g_ptr, can_cross)) return FALSE;
1450 if (turn_flags_ptr->is_riding_mon)
1452 if (!target_ptr->riding_ryoute && !MON_MONFEAR(&target_ptr->current_floor_ptr->m_list[target_ptr->riding])) turn_flags_ptr->do_move = FALSE;
1455 if (!process_post_dig_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx)) return FALSE;
1457 if (turn_flags_ptr->must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
1459 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0))
1460 turn_flags_ptr->do_move = FALSE;
1463 if (turn_flags_ptr->do_move && !can_cross && !turn_flags_ptr->did_kill_wall && !turn_flags_ptr->did_bash_door)
1464 turn_flags_ptr->do_move = FALSE;
1466 if (turn_flags_ptr->do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
1468 if (is_original_ap_and_seen(target_ptr, m_ptr))
1469 r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
1471 turn_flags_ptr->do_move = FALSE;
1474 if (!turn_flags_ptr->do_move)
1476 if (turn_flags_ptr->do_turn) break;
1481 turn_flags_ptr->do_turn = TRUE;
1482 feature_type *f_ptr;
1483 f_ptr = &f_info[g_ptr->feat];
1484 if (have_flag(f_ptr->flags, FF_TREE))
1486 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
1488 m_ptr->energy_need += ENERGY_NEED();
1492 if (!update_riding_monster(target_ptr, turn_flags_ptr, m_idx, oy, ox, ny, nx)) break;
1494 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1497 (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
1498 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
1500 if (is_hostile(m_ptr))
1501 disturb(target_ptr, FALSE, TRUE);
1504 bool is_takable_or_killable = g_ptr->o_idx > 0;
1505 is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) != 0;
1507 bool is_pickup_items = (target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) != 0;
1508 is_pickup_items &= (r_ptr->flags2 & RF2_TAKE_ITEM) != 0;
1510 is_takable_or_killable &= !is_pet(m_ptr) || is_pickup_items;
1511 if (!is_takable_or_killable)
1513 if (turn_flags_ptr->do_turn) break;
1518 update_object_by_monster_movement(target_ptr, turn_flags_ptr, m_idx, ny, nx);
1519 if (turn_flags_ptr->do_turn) break;
1529 * @brief モンスターの恐怖状態を処理する
1530 * @param target_ptr プレーヤーへの参照ポインタ
1531 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1532 * @param m_idx モンスターID
1533 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1534 * @return モンスターが戦いを決意したらTRUE
1536 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
1538 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1539 bool is_battle_determined = !turn_flags_ptr->do_turn && !turn_flags_ptr->do_move && MON_MONFEAR(m_ptr) && turn_flags_ptr->aware;
1540 if (!is_battle_determined) return FALSE;
1542 (void)set_monster_monfear(target_ptr, m_idx, 0);
1543 if (!turn_flags_ptr->see_m) return TRUE;
1545 GAME_TEXT m_name[MAX_NLEN];
1546 monster_desc(target_ptr, m_name, m_ptr, 0);
1547 msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
1553 * @brief 全モンスターのターン管理メインルーチン /
1554 * Process all the "live" monsters, once per game turn.
1557 * During each game current game turn, we scan through the list of all the "live" monsters,\n
1558 * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
1559 * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
1561 * Note that monsters can never move in the monster array (except when the\n
1562 * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
1564 * This function is responsible for at least half of the processor time\n
1565 * on a normal system with a "normal" amount of monsters and a player doing\n
1568 * When the player is resting, virtually 90% of the processor time is spent\n
1569 * in this function, and its children, "process_monster()" and "make_move()".\n
1571 * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
1572 * especially when the player is running.\n
1574 * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
1575 * monsters while they are still being "born". A monster is "fresh" only\n
1576 * during the game turn in which it is created, and we use the "hack_m_idx" to\n
1577 * determine if the monster is yet to be processed during the game turn.\n
1579 * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
1580 * move before any "nasty" monsters get to use their spell attacks.\n
1582 * Note that when the "knowledge" about the currently tracked monster\n
1583 * changes (flags, attacks, spells), we induce a redraw of the monster\n
1586 void process_monsters(player_type *target_ptr)
1588 old_race_flags tmp_flags;
1589 old_race_flags *old_race_flags_ptr = init_old_race_flags(&tmp_flags);
1591 floor_type *floor_ptr = target_ptr->current_floor_ptr;
1592 floor_ptr->monster_noise = FALSE;
1594 MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
1595 save_old_race_flags(target_ptr->monster_race_idx, old_race_flags_ptr);
1596 sweep_monster_process(target_ptr);
1599 if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
1602 update_player_window(target_ptr, old_race_flags_ptr);
1607 * @brief フロア内のモンスターについてターン終了時の処理を繰り返す
1608 * @param target_ptr プレーヤーへの参照ポインタ
1610 void sweep_monster_process(player_type *target_ptr)
1612 floor_type *floor_ptr = target_ptr->current_floor_ptr;
1613 for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--)
1615 monster_type *m_ptr;
1616 m_ptr = &floor_ptr->m_list[i];
1618 if (target_ptr->leaving) return;
1619 if (!monster_is_valid(m_ptr)) continue;
1620 if (target_ptr->wild_mode) continue;
1622 if (m_ptr->mflag & MFLAG_BORN)
1624 m_ptr->mflag &= ~(MFLAG_BORN);
1628 if (m_ptr->cdis >= AAF_LIMIT) continue;
1629 if (!decide_process_continue(target_ptr, m_ptr)) continue;
1631 SPEED speed = (target_ptr->riding == i) ? target_ptr->pspeed : decide_monster_speed(m_ptr);
1632 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
1633 if (m_ptr->energy_need > 0) continue;
1635 m_ptr->energy_need += ENERGY_NEED();
1637 process_monster(target_ptr, i);
1638 reset_target(m_ptr);
1640 if (target_ptr->no_flowed && one_in_(3))
1641 m_ptr->mflag2 |= MFLAG2_NOFLOW;
1643 if (!target_ptr->playing || target_ptr->is_dead) return;
1644 if (target_ptr->leaving) return;
1650 * @brief 後続のモンスター処理が必要かどうか判定する (要調査)
1651 * @param target_ptr プレーヤーへの参照ポインタ
1652 * @param m_ptr モンスターへの参照ポインタ
1653 * @return 後続処理が必要ならTRUE
1655 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr)
1657 monster_race *r_ptr;
1658 r_ptr = &r_info[m_ptr->r_idx];
1659 if (!target_ptr->no_flowed)
1661 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
1664 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
1667 if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) &&
1668 (player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) || (target_ptr->cursed & TRC_AGGRAVATE)))
1671 if (m_ptr->target_y)