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-object.h"
19 #include "monster/monster-util.h"
20 #include "monster/quantum-effect.h"
27 #include "spells-floor.h"
28 #include "spells-summon.h"
31 #include "realm-hex.h"
34 #include "player-move.h"
35 #include "monster-status.h"
36 #include "monster-spell.h"
37 #include "monster-process.h"
38 #include "monster-dist-offsets.h"
39 #include "monsterrace-hook.h"
43 #include "view-mainwindow.h"
45 bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm);
46 void decide_enemy_approch_direction(player_type *target_ptr, MONSTER_IDX m_idx, int start, int plus, POSITION *y, POSITION *x);
47 bool decide_pet_approch_direction(player_type *target_ptr, monster_type *m_ptr, monster_type *t_ptr);
49 bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp);
50 coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONSTER_IDX m_idx, const POSITION *y_offsets, const POSITION *x_offsets, int d);
52 bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp);
53 void sweep_hiding_candidate(player_type *target_ptr, monster_type *m_ptr, const POSITION *y_offsets, const POSITION *x_offsets, coordinate_candidate *candidate);
55 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon);
56 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx);
57 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
58 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx);
59 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
60 bool explode_monster(player_type *target_ptr, MONSTER_IDX m_idx);
61 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox);
62 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware);
63 bool random_walk(player_type *target_ptr, DIRECTION *mm, monster_type *m_ptr);
64 bool decide_pet_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx);
65 bool runaway_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
66 void escape_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, GAME_TEXT *m_name);
67 void process_special(player_type *target_ptr, MONSTER_IDX m_idx);
68 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware);
69 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware);
71 bool process_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx, bool can_cross);
72 bool process_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
73 bool bash_normal_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
74 void bash_glass_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, feature_type *f_ptr, bool may_bash);
75 bool process_protection_rune(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
76 bool process_explosive_rune(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
78 void exe_monster_attack_to_player(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, POSITION ny, POSITION nx);
79 bool process_monster_attack_to_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, grid_type *g_ptr, bool can_cross);
80 bool exe_monster_attack_to_monster(player_type *target_ptr, MONSTER_IDX m_idx, grid_type *g_ptr);
82 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);
83 bool process_post_dig_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
84 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);
86 void update_player_type(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_race *r_ptr);
87 void update_monster_race_flags(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr);
88 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
90 void save_old_race_flags(player_type *target_ptr, old_race_flags *old_race_flags_ptr);
91 void sweep_monster_process(player_type *target_ptr);
92 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr);
93 SPEED decide_monster_speed(player_type *target_ptr, monster_type *m_ptr, int monster_number);
94 void update_player_window(player_type *target_ptr, old_race_flags *old_race_flags_ptr);
97 * @brief モンスターが敵に接近するための方向を計算するメインルーチン
98 * Calculate the direction to the next enemy
99 * @param target_ptr プレーヤーへの参照ポインタ
100 * @param m_idx モンスターの参照ID
101 * @param mm 移動するべき方角IDを返す参照ポインタ
102 * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
104 bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm)
106 floor_type *floor_ptr = target_ptr->current_floor_ptr;
107 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
109 POSITION x = 0, y = 0;
110 if (target_ptr->riding_t_m_idx && player_bold(target_ptr, m_ptr->fy, m_ptr->fx))
112 y = floor_ptr->m_list[target_ptr->riding_t_m_idx].fy;
113 x = floor_ptr->m_list[target_ptr->riding_t_m_idx].fx;
115 else if (is_pet(m_ptr) && target_ptr->pet_t_m_idx)
117 y = floor_ptr->m_list[target_ptr->pet_t_m_idx].fy;
118 x = floor_ptr->m_list[target_ptr->pet_t_m_idx].fx;
124 if (target_ptr->phase_out)
126 start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
127 if (randint0(2)) plus = -1;
131 start = floor_ptr->m_max + 1;
134 decide_enemy_approch_direction(target_ptr, m_idx, start, plus, &y, &x);
136 if ((x == 0) && (y == 0)) return FALSE;
142 store_enemy_approch_direction(mm, y, x);
148 * @brief モンスターが敵に接近するための方向を決定する
149 * @param target_ptr プレーヤーへの参照ポインタ
150 * @param m_idx モンスターID
151 * @param start モンスターIDの開始
152 * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
153 * @param y モンスターの移動方向Y
154 * @param x モンスターの移動方向X
157 void decide_enemy_approch_direction(player_type *target_ptr, MONSTER_IDX m_idx, int start, int plus, POSITION *y, POSITION *x)
159 floor_type *floor_ptr = target_ptr->current_floor_ptr;
160 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
161 monster_race *r_ptr = &r_info[m_ptr->r_idx];
162 for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus)
164 MONSTER_IDX dummy = (i % floor_ptr->m_max);
165 if (dummy == 0) continue;
167 MONSTER_IDX t_idx = dummy;
169 t_ptr = &floor_ptr->m_list[t_idx];
170 if (t_ptr == m_ptr) continue;
171 if (!monster_is_valid(t_ptr)) continue;
172 if (decide_pet_approch_direction(target_ptr, m_ptr, t_ptr)) continue;
173 if (!are_enemies(target_ptr, m_ptr, t_ptr)) continue;
175 if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) ||
176 ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)))
178 if (!in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
182 if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
193 * @brief ペットが敵に接近するための方向を決定する
194 * @param target_ptr プレーヤーへの参照ポインタ
195 * @param m_ptr 移動を試みているモンスターへの参照ポインタ
196 * @param t_ptr 移動先モンスターへの参照ポインタ
197 * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
198 * @return ペットがモンスターに近づくならばTRUE
200 bool decide_pet_approch_direction(player_type *target_ptr, monster_type *m_ptr, monster_type *t_ptr)
202 monster_race *r_ptr = &r_info[m_ptr->r_idx];
203 if (!is_pet(m_ptr)) return FALSE;
205 if (target_ptr->pet_follow_distance < 0)
207 if (t_ptr->cdis <= (0 - target_ptr->pet_follow_distance))
212 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > target_ptr->pet_follow_distance))
217 return (r_ptr->aaf < t_ptr->cdis);
222 * @brief モンスターがプレイヤーから逃走するかどうかを返す /
223 * Returns whether a given monster will try to run from the player.
224 * @param m_idx 逃走するモンスターの参照ID
225 * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
227 * Monsters will attempt to avoid very powerful players. See below.\n
229 * Because this function is called so often, little details are important\n
230 * for efficiency. Like not using "mod" or "div" when possible. And\n
231 * attempting to check the conditions in an optimal order. Note that\n
232 * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
234 * Note that this function is responsible for about one to five percent\n
235 * of the processor use in normal conditions...\n
237 static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
239 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
240 monster_race *r_ptr = &r_info[m_ptr->r_idx];
244 return ((target_ptr->pet_follow_distance < 0) &&
245 (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
248 if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
249 if (MON_MONFEAR(m_ptr)) return TRUE;
250 if (m_ptr->cdis <= 5) return FALSE;
252 PLAYER_LEVEL p_lev = target_ptr->lev;
253 DEPTH m_lev = r_ptr->level + (m_idx & 0x08) + 25;
254 if (m_lev > p_lev + 4) return FALSE;
255 if (m_lev + 4 <= p_lev) return TRUE;
257 HIT_POINT p_chp = target_ptr->chp;
258 HIT_POINT p_mhp = target_ptr->mhp;
259 HIT_POINT m_chp = m_ptr->hp;
260 HIT_POINT m_mhp = m_ptr->maxhp;
261 u32b p_val = (p_lev * p_mhp) + (p_chp << 2);
262 u32b m_val = (m_lev * m_mhp) + (m_chp << 2);
263 if (p_val * m_mhp > m_val * p_mhp) return TRUE;
270 * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
271 * Search spell castable grid
272 * @param target_ptr プレーヤーへの参照ポインタ
273 * @param m_idx モンスターの参照ID
274 * @param yp 適したマスのY座標を返す参照ポインタ
275 * @param xp 適したマスのX座標を返す参照ポインタ
276 * @return 有効なマスがあった場合TRUEを返す
278 static bool get_moves_aux2(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
280 floor_type *floor_ptr = target_ptr->current_floor_ptr;
281 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
282 monster_race *r_ptr = &r_info[m_ptr->r_idx];
284 POSITION y1 = m_ptr->fy;
285 POSITION x1 = m_ptr->fx;
287 if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
289 int now_cost = floor_ptr->grid_array[y1][x1].cost;
290 if (now_cost == 0) now_cost = 999;
292 bool can_open_door = FALSE;
293 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
295 can_open_door = TRUE;
299 for (int i = 7; i >= 0; i--)
301 POSITION y = y1 + ddy_ddd[i];
302 POSITION x = x1 + ddx_ddd[i];
303 if (!in_bounds2(floor_ptr, y, x)) continue;
304 if (player_bold(target_ptr, y, x)) return FALSE;
307 g_ptr = &floor_ptr->grid_array[y][x];
308 int cost = g_ptr->cost;
309 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))))
311 if (cost == 0) continue;
312 if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat)) continue;
315 if (cost == 0) cost = 998;
317 if (now_cost < cost) continue;
318 if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x)) continue;
319 if (best < cost) continue;
322 *yp = y1 + ddy_ddd[i];
323 *xp = x1 + ddx_ddd[i];
326 if (best == 999) return FALSE;
333 * todo まだ分割せず放置 (このままいくと3000行を超えかねない)
334 * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
335 * Choose the "best" direction for "flowing"
336 * @param m_idx モンスターの参照ID
337 * @param yp 移動先のマスのY座標を返す参照ポインタ
338 * @param xp 移動先のマスのX座標を返す参照ポインタ
339 * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
340 * @return 有効なマスがあった場合TRUEを返す
342 * Note that ghosts and rock-eaters are never allowed to "flow",\n
343 * since they should move directly towards the player.\n
345 * Prefer "non-diagonal" directions, but twiddle them a little\n
346 * to angle slightly towards the player's actual location.\n
348 * Allow very perceptive monsters to track old "spoor" left by\n
349 * previous locations occupied by the player. This will tend\n
350 * to have monsters end up either near the player or on a grid\n
351 * recently occupied by the player (and left via "teleport").\n
353 * Note that if "smell" is turned on, all monsters get vicious.\n
355 * Also note that teleporting away from a location will cause\n
356 * the monsters who were chasing you to converge on that location\n
357 * as long as you are still near enough to "annoy" them without\n
358 * being close enough to chase directly. I have no idea what will\n
359 * happen if you combine "smell" with low "aaf" values.\n
361 static bool get_moves_aux(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
364 floor_type *floor_ptr = target_ptr->current_floor_ptr;
365 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
366 monster_race *r_ptr = &r_info[m_ptr->r_idx];
368 if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
369 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
370 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
372 if (get_moves_aux2(target_ptr, m_idx, yp, xp)) return TRUE;
375 if (no_flow) return FALSE;
376 if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return FALSE;
377 if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return FALSE;
379 POSITION y1 = m_ptr->fy;
380 POSITION x1 = m_ptr->fx;
381 if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return FALSE;
383 g_ptr = &floor_ptr->grid_array[y1][x1];
386 bool use_scent = FALSE;
391 else if (g_ptr->when)
393 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return FALSE;
403 for (int i = 7; i >= 0; i--)
405 POSITION y = y1 + ddy_ddd[i];
406 POSITION x = x1 + ddx_ddd[i];
408 if (!in_bounds2(floor_ptr, y, x)) continue;
410 g_ptr = &floor_ptr->grid_array[y][x];
413 int when = g_ptr->when;
414 if (best > when) continue;
421 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
430 if ((cost == 0) || (best < cost)) continue;
435 *yp = target_ptr->y + 16 * ddy_ddd[i];
436 *xp = target_ptr->x + 16 * ddx_ddd[i];
439 if (best == 999 || best == 0) return FALSE;
446 * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
447 * Provide a location to flee to, but give the player a wide berth.
448 * @param m_idx モンスターの参照ID
449 * @param yp 移動先のマスのY座標を返す参照ポインタ
450 * @param xp 移動先のマスのX座標を返す参照ポインタ
451 * @return 有効なマスがあった場合TRUEを返す
453 * A monster may wish to flee to a location that is behind the player,\n
454 * but instead of heading directly for it, the monster should "swerve"\n
455 * around the player so that he has a smaller chance of getting hit.\n
457 static bool get_fear_moves_aux(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
459 POSITION gy = 0, gx = 0;
461 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
462 POSITION fy = m_ptr->fy;
463 POSITION fx = m_ptr->fx;
465 POSITION y1 = fy - (*yp);
466 POSITION x1 = fx - (*xp);
469 for (int i = 7; i >= 0; i--)
471 POSITION y = fy + ddy_ddd[i];
472 POSITION x = fx + ddx_ddd[i];
473 if (!in_bounds2(floor_ptr, y, x)) continue;
475 POSITION dis = distance(y, x, y1, x1);
476 POSITION s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
479 if (s < score) continue;
486 if (score == -1) return FALSE;
496 * @brief モンスターが逃げ込める安全な地点を返す /
497 * Choose a "safe" location near a monster for it to run toward.
498 * @param target_ptr プレーヤーへの参照ポインタ
499 * @param m_idx モンスターの参照ID
500 * @param yp 移動先のマスのY座標を返す参照ポインタ
501 * @param xp 移動先のマスのX座標を返す参照ポインタ
502 * @return 有効なマスがあった場合TRUEを返す
504 * A location is "safe" if it can be reached quickly and the player\n
505 * is not able to fire into it (it isn't a "clean shot"). So, this will\n
506 * cause monsters to "duck" behind walls. Hopefully, monsters will also\n
507 * try to run towards corridor openings if they are in a room.\n
509 * This function may take lots of CPU time if lots of monsters are\n
512 * Return TRUE if a safe location is available.\n
514 bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
516 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
517 for (POSITION d = 1; d < 10; d++)
519 const POSITION *y_offsets;
520 y_offsets = dist_offsets_y[d];
522 const POSITION *x_offsets;
523 x_offsets = dist_offsets_x[d];
525 coordinate_candidate candidate = sweep_safe_coordinate(target_ptr, m_idx, y_offsets, x_offsets, d);
527 if (candidate.gdis <= 0) continue;
529 *yp = m_ptr->fy - candidate.gy;
530 *xp = m_ptr->fx - candidate.gx;
540 * @brief モンスターが逃げ込める地点を走査する
541 * @param target_ptr プレーヤーへの参照ポインタ
542 * @param m_idx モンスターID
545 * @param d モンスターがいる地点からの距離
546 * @return 逃げ込める地点の候補地
548 coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONSTER_IDX m_idx, const POSITION *y_offsets, const POSITION *x_offsets, int d)
550 coordinate_candidate candidate = init_coordinate_candidate();
551 floor_type *floor_ptr = target_ptr->current_floor_ptr;
552 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
553 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
555 i++, dx = x_offsets[i], dy = y_offsets[i])
557 POSITION y = m_ptr->fy + dy;
558 POSITION x = m_ptr->fx + dx;
559 if (!in_bounds(floor_ptr, y, x)) continue;
562 g_ptr = &floor_ptr->grid_array[y][x];
564 BIT_FLAGS16 riding_mode = (m_idx == target_ptr->riding) ? CEM_RIDING : 0;
565 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], riding_mode))
568 if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
570 if (g_ptr->dist == 0) continue;
571 if (g_ptr->dist > floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist + 2 * d) continue;
574 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x)) continue;
576 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
577 if (dis <= candidate.gdis) continue;
581 candidate.gdis = dis;
589 * @brief モンスターが隠れ潜める地点を返す /
590 * Choose a good hiding place near a monster for it to run toward.
591 * @param target_ptr プレーヤーへの参照ポインタ
592 * @param m_idx モンスターの参照ID
593 * @param yp 移動先のマスのY座標を返す参照ポインタ
594 * @param xp 移動先のマスのX座標を返す参照ポインタ
595 * @return 有効なマスがあった場合TRUEを返す
597 * Pack monsters will use this to "ambush" the player and lure him out\n
598 * of corridors into open space so they can swarm him.\n
600 * Return TRUE if a good location is available.\n
602 bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
604 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
605 coordinate_candidate candidate = init_coordinate_candidate();
606 candidate.gdis = 999;
608 for (POSITION d = 1; d < 10; d++)
610 const POSITION *y_offsets;
611 y_offsets = dist_offsets_y[d];
613 const POSITION *x_offsets;
614 x_offsets = dist_offsets_x[d];
616 sweep_hiding_candidate(target_ptr, m_ptr, y_offsets, x_offsets, &candidate);
617 if (candidate.gdis >= 999) continue;
619 *yp = m_ptr->fy - candidate.gy;
620 *xp = m_ptr->fx - candidate.gx;
629 * @brief モンスターが隠れられる地点を走査する
630 * @param target_ptr プレーヤーへの参照ポインタ
631 * @param m_idx モンスターID
634 * @param candidate 隠れられる地点の候補地
637 void sweep_hiding_candidate(player_type *target_ptr, monster_type *m_ptr, const POSITION *y_offsets, const POSITION *x_offsets, coordinate_candidate *candidate)
639 monster_race *r_ptr = &r_info[m_ptr->r_idx];
640 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
642 i++, dx = x_offsets[i], dy = y_offsets[i])
644 POSITION y = m_ptr->fy + dy;
645 POSITION x = m_ptr->fx + dx;
646 if (!in_bounds(target_ptr->current_floor_ptr, y, x)) continue;
647 if (!monster_can_enter(target_ptr, y, x, r_ptr, 0)) continue;
648 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))
651 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
652 if (dis < candidate->gdis && dis >= 2)
656 candidate->gdis = dis;
663 * todo 分割したいが条件が多すぎて適切な関数名と詳細処理を追いきれない……
664 * @brief モンスターの移動方向を返す /
665 * Choose "logical" directions for monster movement
666 * @param target_ptr プレーヤーへの参照ポインタ
667 * @param m_idx モンスターの参照ID
668 * @param mm 移動方向を返す方向IDの参照ポインタ
669 * @return 有効方向があった場合TRUEを返す
671 static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
673 floor_type *floor_ptr = target_ptr->current_floor_ptr;
674 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
675 monster_race *r_ptr = &r_info[m_ptr->r_idx];
676 POSITION y = 0, ay, x = 0, ax;
677 POSITION y2 = target_ptr->y;
678 POSITION x2 = target_ptr->x;
680 bool will_run = mon_will_run(target_ptr, m_idx);
682 bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) != 0) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2);
683 bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) != 0) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall);
685 if (!will_run && m_ptr->target_y)
687 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
689 are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) &&
690 los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
691 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
693 y = m_ptr->fy - m_ptr->target_y;
694 x = m_ptr->fx - m_ptr->target_x;
699 if (!done && !will_run && is_hostile(m_ptr) &&
700 (r_ptr->flags1 & RF1_FRIENDS) &&
701 ((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)) ||
702 (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
704 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
705 !(r_ptr->flags2 & RF2_KILL_WALL))
708 for (int i = 0; i < 8; i++)
710 int xx = target_ptr->x + ddx_ddd[i];
711 int yy = target_ptr->y + ddy_ddd[i];
713 if (!in_bounds2(floor_ptr, yy, xx)) continue;
715 g_ptr = &floor_ptr->grid_array[yy][xx];
716 if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0))
722 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
723 if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
725 if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
726 (target_ptr->mhp + target_ptr->msp))
728 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
732 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
734 for (int i = 0; i < 8; i++)
736 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
737 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
738 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
745 if (!in_bounds2(floor_ptr, y2, x2)) continue;
746 if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0)) continue;
759 (void)get_moves_aux(target_ptr, m_idx, &y2, &x2, no_flow);
764 if (is_pet(m_ptr) && will_run)
770 if (!done && will_run)
774 if (find_safety(target_ptr, m_idx, &y, &x) && !no_flow)
776 if (get_fear_moves_aux(target_ptr->current_floor_ptr, m_idx, &y, &x))
788 if (!x && !y) return FALSE;
794 if (y < 0) move_val += 8;
795 if (x > 0) move_val += 4;
797 if (ay > (ax << 1)) move_val += 2;
798 else if (ax > (ay << 1)) move_val++;
948 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
950 return !have_flag(f_ptr->flags, FF_GLASS) ||
951 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
952 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
957 * @brief モンスター単体の1ターン行動処理メインルーチン /
959 * @param target_ptr プレーヤーへの参照ポインタ
960 * @param m_idx 行動モンスターの参照ID
963 * The monster is known to be within 100 grids of the player\n
965 * In several cases, we directly update the monster lore\n
967 * Note that a monster is only allowed to "reproduce" if there\n
968 * are a limited number of "reproducing" monsters on the current\n
969 * level. This should prevent the level from being "swamped" by\n
970 * reproducing monsters. It also allows a large mass of mice to\n
971 * prevent a louse from multiplying, but this is a small price to\n
972 * pay for a simple multiplication method.\n
974 * XXX Monster fear is slightly odd, in particular, monsters will\n
975 * fixate on opening a door even if they cannot open it. Actually,\n
976 * the same thing happens to normal monsters when they hit a door\n
978 * In addition, monsters which *cannot* open or bash\n
979 * down a door will still stand there trying to open it...\n
981 * XXX Technically, need to check for monster in the way\n
982 * combined with that monster being in a wall (or door?)\n
984 * A "direction" of "5" means "pick a random direction".\n
986 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
988 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
989 monster_race *r_ptr = &r_info[m_ptr->r_idx];
990 turn_flags tmp_flags;
991 turn_flags *turn_flags_ptr = init_turn_flags(target_ptr->riding, m_idx, &tmp_flags);
992 turn_flags_ptr->see_m = is_seen(m_ptr);
994 decide_drop_from_monster(target_ptr, m_idx, turn_flags_ptr->is_riding_mon);
995 if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
997 choose_new_monster(target_ptr, m_idx, FALSE, 0);
998 r_ptr = &r_info[m_ptr->r_idx];
1001 turn_flags_ptr->aware = process_stealth(target_ptr, m_idx);
1002 if (vanish_summoned_children(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
1003 if (process_quantum_effect(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
1004 if (explode_monster(target_ptr, m_idx)) return;
1005 if (runaway_monster(target_ptr, turn_flags_ptr, m_idx)) return;
1007 awake_monster(target_ptr, m_idx);
1008 if (MON_STUNNED(m_ptr))
1010 if (one_in_(2)) return;
1013 if (turn_flags_ptr->is_riding_mon)
1015 target_ptr->update |= (PU_BONUS);
1018 process_angar(target_ptr, m_idx, turn_flags_ptr->see_m);
1020 POSITION oy = m_ptr->fy;
1021 POSITION ox = m_ptr->fx;
1022 if (decide_monster_multiplication(target_ptr, m_idx, oy, ox)) return;
1024 process_special(target_ptr, m_idx);
1025 process_speak_sound(target_ptr, m_idx, oy, ox, turn_flags_ptr->aware);
1026 if (cast_spell(target_ptr, m_idx, turn_flags_ptr->aware)) return;
1029 mm[0] = mm[1] = mm[2] = mm[3] = 0;
1030 mm[4] = mm[5] = mm[6] = mm[7] = 0;
1032 if (!decide_monster_movement_direction(target_ptr, mm, m_idx, turn_flags_ptr->aware)) return;
1035 if (!process_monster_movement(target_ptr, turn_flags_ptr, m_idx, mm, oy, ox, &count)) return;
1038 * Forward movements failed, but now received LOS attack!
1039 * Try to flow by smell.
1041 if (target_ptr->no_flowed && count > 2 && m_ptr->target_y)
1042 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
1044 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)
1046 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
1048 if (make_attack_spell(m_idx, target_ptr)) return;
1052 update_player_type(target_ptr, turn_flags_ptr, r_ptr);
1053 update_monster_race_flags(target_ptr, turn_flags_ptr, m_ptr);
1055 if (!process_monster_fear(target_ptr, turn_flags_ptr, m_idx)) return;
1057 if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
1063 * @param target_ptr プレーヤーへの参照ポインタ
1064 * @param m_idx モンスターID
1065 * @return モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1067 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx)
1069 if ((target_ptr->special_defense & NINJA_S_STEALTH) == 0) return TRUE;
1071 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1072 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1073 int tmp = target_ptr->lev * 6 + (target_ptr->skill_stl + 10) * 4;
1074 if (target_ptr->monlite) tmp /= 3;
1075 if (target_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1076 if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10)) tmp /= 3;
1077 return (randint0(tmp) <= (r_ptr->level + 20));
1082 * @brief 死亡したモンスターが乗馬中のモンスターだった場合に落馬処理を行う
1083 * @param target_ptr プレーヤーへの参照ポインタ
1084 * @param m_idx モンスターID
1085 * @param is_riding_mon 騎乗中であればTRUE
1088 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon)
1090 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1091 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1092 if (!is_riding_mon || ((r_ptr->flags7 & RF7_RIDING) != 0)) return;
1094 if (rakuba(target_ptr, 0, TRUE))
1097 msg_print("地面に落とされた。");
1099 GAME_TEXT m_name[MAX_NLEN];
1100 monster_desc(target_ptr, m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
1101 msg_format("You have fallen from %s.", m_name);
1108 * @brief 召喚の親元が消滅した時、子供も消滅させる
1109 * @param target_ptr プレーヤーへの参照ポインタ
1110 * @param m_idx モンスターID
1111 * @param see_m モンスターが視界内にいたらTRUE
1112 * @return 召喚モンスターが消滅したらTRUE
1114 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
1116 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1117 if ((m_ptr->parent_m_idx == 0) || (target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx > 0))
1122 GAME_TEXT m_name[MAX_NLEN];
1123 monster_desc(target_ptr, m_name, m_ptr, 0);
1124 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
1127 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
1129 GAME_TEXT m_name[MAX_NLEN];
1130 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
1131 exe_write_diary(target_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
1134 delete_monster_idx(target_ptr, m_idx);
1140 * @brief 寝ているモンスターの起床を判定する
1141 * @param target_ptr プレーヤーへの参照ポインタ
1142 * @param m_idx モンスターID
1145 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx)
1147 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1148 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1149 if (!MON_CSLEEP(m_ptr)) return;
1150 if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
1152 (void)set_monster_csleep(target_ptr, m_idx, 0);
1155 GAME_TEXT m_name[MAX_NLEN];
1156 monster_desc(target_ptr, m_name, m_ptr, 0);
1157 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
1160 if (is_original_ap_and_seen(target_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
1168 * @brief モンスターの怒り状態を判定する (起こっていたら敵に回す)
1169 * @param target_ptr プレーヤーへの参照ポインタ
1170 * @param m_idx モンスターID
1171 * @param see_m モンスターが視界内にいたらTRUE
1174 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
1176 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1177 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1178 bool gets_angry = FALSE;
1179 if (is_friendly(m_ptr) && (target_ptr->cursed & TRC_AGGRAVATE))
1182 if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
1183 monster_has_hostile_align(target_ptr, NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
1188 if (target_ptr->phase_out || !gets_angry) return;
1190 if (is_pet(m_ptr) || see_m)
1192 GAME_TEXT m_name[MAX_NLEN];
1193 monster_desc(target_ptr, m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
1194 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
1197 set_hostile(target_ptr, m_ptr);
1203 * @param target_ptr プレーヤーへの参照ポインタ
1204 * @param m_idx モンスターID
1207 bool explode_monster(player_type *target_ptr, MONSTER_IDX m_idx)
1209 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1210 if (m_ptr->r_idx != MON_SHURYUUDAN) return FALSE;
1213 mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1219 * @brief モンスター依存の特別な行動を取らせる
1220 * @param target_ptr プレーヤーへの参照ポインタ
1221 * @param m_idx モンスターID
1224 void process_special(player_type *target_ptr, MONSTER_IDX m_idx)
1226 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1227 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1228 if ((r_ptr->a_ability_flags2 & RF6_SPECIAL) == 0) return;
1229 if (m_ptr->r_idx != MON_OHMU) return;
1230 if (target_ptr->current_floor_ptr->inside_arena || target_ptr->phase_out) return;
1231 if ((r_ptr->freq_spell == 0) || !(randint1(100) <= r_ptr->freq_spell)) return;
1234 DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1235 BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
1237 for (int k = 0; k < A_MAX; k++)
1239 if (summon_specific(target_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
1241 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
1245 if (count && is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
1250 * @brief モンスターを喋らせたり足音を立てたりする
1251 * @param target_ptr プレーヤーへの参照ポインタ
1252 * @param m_idx モンスターID
1253 * @param oy モンスターが元々いたY座標
1254 * @param ox モンスターが元々いたX座標
1255 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1258 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware)
1260 if (target_ptr->phase_out) return;
1262 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1263 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1264 if (m_ptr->ap_r_idx == MON_CYBER &&
1265 one_in_(CYBERNOISE) &&
1266 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
1268 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1269 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
1272 if (((ap_r_ptr->flags2 & RF2_CAN_SPEAK) == 0) || !aware ||
1273 !one_in_(SPEAK_CHANCE) ||
1274 !player_has_los_bold(target_ptr, oy, ox) ||
1275 !projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
1278 GAME_TEXT m_name[MAX_NLEN];
1279 char monmessage[1024];
1283 monster_desc(target_ptr, m_name, m_ptr, 0);
1285 strcpy(m_name, _("それ", "It"));
1287 if (MON_MONFEAR(m_ptr))
1288 filename = _("monfear_j.txt", "monfear.txt");
1289 else if (is_pet(m_ptr))
1290 filename = _("monpet_j.txt", "monpet.txt");
1291 else if (is_friendly(m_ptr))
1292 filename = _("monfrien_j.txt", "monfrien.txt");
1294 filename = _("monspeak_j.txt", "monspeak.txt");
1296 if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
1298 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
1304 * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
1305 * @param target_ptr プレーヤーへの参照ポインタ
1306 * @param m_idx モンスターID
1307 * @param oy 分裂元モンスターのY座標
1308 * @param ox 分裂元モンスターのX座標
1309 * @return 実際に分裂したらTRUEを返す
1311 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
1313 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1314 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1315 if (((r_ptr->flags2 & RF2_MULTIPLY) == 0) || (target_ptr->current_floor_ptr->num_repro >= MAX_REPRO))
1319 for (POSITION y = oy - 1; y <= oy + 1; y++)
1321 for (POSITION x = ox - 1; x <= ox + 1; x++)
1323 if (!in_bounds2(target_ptr->current_floor_ptr, y, x)) continue;
1324 if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
1328 if (multiply_barrier(target_ptr, m_idx)) k = 8;
1330 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
1332 if (multiply_monster(target_ptr, m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
1334 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(target_ptr, m_ptr))
1336 r_ptr->r_flags2 |= (RF2_MULTIPLY);
1348 * @brief モンスターの移動パターンを決定する
1349 * @param target_ptr プレーヤーへの参照ポインタ
1351 * @param m_idx モンスターID
1352 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1353 * @return 移動先が存在すればTRUE
1355 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware)
1357 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1358 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1360 if (MON_CONFUSED(m_ptr) || !aware)
1362 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1366 if (random_walk(target_ptr, mm, m_ptr)) return TRUE;
1368 if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
1370 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1374 if (decide_pet_movement_direction(target_ptr, mm, m_idx)) return TRUE;
1376 if (!is_hostile(m_ptr))
1378 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1379 get_enemy_dir(target_ptr, m_idx, mm);
1383 if (!get_moves(target_ptr, m_idx, mm)) return FALSE;
1390 * todo ↓のように書いたが、"5"とはもしかして「その場に留まる」という意味か?
1391 * @brief 不規則歩行フラグを持つモンスターの移動方向をその確率に基づいて決定する
1392 * @param target_ptr プレーヤーへの参照ポインタ
1394 * @param m_ptr モンスターへの参照ポインタ
1395 * @return 不規則な方向へ歩くことになったらTRUE
1397 bool random_walk(player_type *target_ptr, DIRECTION *mm, monster_type *m_ptr)
1399 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1400 if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) && (randint0(100) < 75))
1402 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
1404 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1408 if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 50))
1410 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_50;
1412 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1416 if ((r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 25))
1418 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
1420 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1429 * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
1430 * @param target_ptr プレーヤーへの参照ポインタ
1432 * @param m_idx モンスターID
1433 * @return モンスターがペットであればTRUE
1435 bool decide_pet_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx)
1437 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1438 if (!is_pet(m_ptr)) return FALSE;
1440 bool avoid = ((target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
1441 bool lonely = (!avoid && (m_ptr->cdis > target_ptr->pet_follow_distance));
1442 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
1443 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1444 if (get_enemy_dir(target_ptr, m_idx, mm)) return TRUE;
1445 if (!avoid && !lonely && !distant) return TRUE;
1447 POSITION dis = target_ptr->pet_follow_distance;
1448 if (target_ptr->pet_follow_distance > PET_SEEK_DIST)
1450 target_ptr->pet_follow_distance = PET_SEEK_DIST;
1453 (void)get_moves(target_ptr, m_idx, mm);
1454 target_ptr->pet_follow_distance = (s16b)dis;
1460 * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
1461 * @param target_ptr プレーヤーへの参照ポインタ
1462 * @param m_idx モンスターID
1463 * @param is_riding_mon 騎乗状態ならばTRUE
1464 * @param see_m モンスターが視界内にいたらTRUE
1465 * @return モンスターがフロアから消えたらTRUE
1467 bool runaway_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
1469 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1470 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1471 bool can_runaway = is_pet(m_ptr) || is_friendly(m_ptr);
1472 can_runaway &= ((r_ptr->flags1 & RF1_UNIQUE) != 0) || ((r_ptr->flags7 & RF7_NAZGUL) != 0);
1473 can_runaway &= !target_ptr->phase_out;
1474 if (!can_runaway) return FALSE;
1476 static int riding_pinch = 0;
1478 if (m_ptr->hp >= m_ptr->maxhp / 3)
1480 /* Reset the counter */
1481 if (turn_flags_ptr->is_riding_mon) riding_pinch = 0;
1486 GAME_TEXT m_name[MAX_NLEN];
1487 monster_desc(target_ptr, m_name, m_ptr, 0);
1488 if (turn_flags_ptr->is_riding_mon && riding_pinch < 2)
1490 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
1491 "%^s seems to be in so much pain and tries to escape from your restriction."), m_name);
1493 disturb(target_ptr, TRUE, TRUE);
1497 escape_monster(target_ptr, turn_flags_ptr, m_ptr, m_name);
1498 check_quest_completion(target_ptr, m_ptr);
1499 delete_monster_idx(target_ptr, m_idx);
1505 * @brief HPが1/3未満になった有効的なユニークモンスターの逃走処理を行う
1506 * @param target_ptr プレーヤーへの参照ポインタ
1507 * @param is_riding_mon 騎乗状態ならばTRUE
1508 * @param m_ptr モンスターへの参照ポインタ
1509 * @param m_name モンスター名称
1510 * @param see_m モンスターが視界内にいたらTRUE
1513 void escape_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, GAME_TEXT *m_name)
1515 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1516 if (turn_flags_ptr->is_riding_mon)
1518 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
1519 if (rakuba(target_ptr, -1, FALSE))
1521 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
1525 if (turn_flags_ptr->see_m)
1527 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) &&
1528 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))
1530 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
1533 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s reads a scroll of teleport level."), m_name);
1534 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
1537 if (turn_flags_ptr->is_riding_mon && rakuba(target_ptr, -1, FALSE))
1539 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
1545 * @brief モンスターに魔法を試行させる
1546 * @param target_ptr プレーヤーへの参照ポインタ
1547 * @param m_idx モンスターID
1548 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1549 * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
1551 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware)
1553 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1554 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1555 if ((r_ptr->freq_spell == 0) || (randint1(100) > r_ptr->freq_spell))
1558 bool counterattack = FALSE;
1559 if (m_ptr->target_y)
1561 MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
1562 if (t_m_idx && are_enemies(target_ptr, m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
1563 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1565 counterattack = TRUE;
1571 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
1572 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
1576 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
1577 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
1585 * todo 少し長いが、これといってブロックとしてまとまった部分もないので暫定でこのままとする
1586 * @brief モンスターの移動に関するメインルーチン
1587 * @param target_ptr プレーヤーへの参照ポインタ
1588 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1589 * @param m_idx モンスターID
1590 * @param mm モンスターの移動方向
1591 * @param oy 移動前の、モンスターのY座標
1592 * @param ox 移動前の、モンスターのX座標
1593 * @param count 移動回数 (のはず todo)
1594 * @return 移動が阻害される何か (ドア等)があったらFALSE
1596 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)
1598 for (int i = 0; mm[i]; i++)
1601 if (d == 5) d = ddd[randint0(8)];
1603 POSITION ny = oy + ddy[d];
1604 POSITION nx = ox + ddx[d];
1605 if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx)) continue;
1608 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1609 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1610 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1611 bool can_cross = monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0);
1613 if (!process_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx, can_cross))
1615 if (!process_door(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1619 if (!process_protection_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1621 if (!process_explosive_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
1625 exe_monster_attack_to_player(target_ptr, turn_flags_ptr, m_idx, ny, nx);
1626 if (process_monster_attack_to_monster(target_ptr, turn_flags_ptr, m_idx, g_ptr, can_cross)) return FALSE;
1628 if (turn_flags_ptr->is_riding_mon)
1630 if (!target_ptr->riding_ryoute && !MON_MONFEAR(&target_ptr->current_floor_ptr->m_list[target_ptr->riding])) turn_flags_ptr->do_move = FALSE;
1633 if (!process_post_dig_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx)) return FALSE;
1635 if (turn_flags_ptr->must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
1637 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0))
1638 turn_flags_ptr->do_move = FALSE;
1641 if (turn_flags_ptr->do_move && !can_cross && !turn_flags_ptr->did_kill_wall && !turn_flags_ptr->did_bash_door)
1642 turn_flags_ptr->do_move = FALSE;
1644 if (turn_flags_ptr->do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
1646 if (is_original_ap_and_seen(target_ptr, m_ptr))
1647 r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
1649 turn_flags_ptr->do_move = FALSE;
1652 if (!turn_flags_ptr->do_move)
1654 if (turn_flags_ptr->do_turn) break;
1659 turn_flags_ptr->do_turn = TRUE;
1660 feature_type *f_ptr;
1661 f_ptr = &f_info[g_ptr->feat];
1662 if (have_flag(f_ptr->flags, FF_TREE))
1664 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
1666 m_ptr->energy_need += ENERGY_NEED();
1670 if (!update_riding_monster(target_ptr, turn_flags_ptr, m_idx, oy, ox, ny, nx)) break;
1672 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1675 (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
1676 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
1678 if (is_hostile(m_ptr))
1679 disturb(target_ptr, FALSE, TRUE);
1682 bool is_takable_or_killable = g_ptr->o_idx > 0;
1683 is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) != 0;
1685 bool is_pickup_items = (target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) != 0;
1686 is_pickup_items &= (r_ptr->flags2 & RF2_TAKE_ITEM) != 0;
1688 is_takable_or_killable &= !is_pet(m_ptr) || is_pickup_items;
1689 if (!is_takable_or_killable)
1691 if (turn_flags_ptr->do_turn) break;
1696 update_object_by_monster_movement(target_ptr, turn_flags_ptr, m_idx, ny, nx);
1697 if (turn_flags_ptr->do_turn) break;
1707 * @brief モンスターによる壁の透過・破壊を行う
1708 * @param target_ptr プレーヤーへの参照ポインタ
1709 * @param m_ptr モンスターへの参照ポインタ
1710 * @param ny モンスターのY座標
1711 * @param nx モンスターのX座標
1712 * @param can_cross モンスターが地形を踏破できるならばTRUE
1713 * @return 透過も破壊もしなかった場合はFALSE、それ以外はTRUE
1715 bool process_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx, bool can_cross)
1717 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1719 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1720 feature_type *f_ptr;
1721 f_ptr = &f_info[g_ptr->feat];
1722 if (player_bold(target_ptr, ny, nx))
1724 turn_flags_ptr->do_move = TRUE;
1728 if (g_ptr->m_idx > 0)
1730 turn_flags_ptr->do_move = TRUE;
1734 if (((r_ptr->flags2 & RF2_KILL_WALL) != 0) &&
1735 (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !turn_flags_ptr->is_riding_mon) &&
1736 have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
1737 check_hp_for_feat_destruction(f_ptr, m_ptr))
1739 turn_flags_ptr->do_move = TRUE;
1740 if (!can_cross) turn_flags_ptr->must_alter_to_move = TRUE;
1742 turn_flags_ptr->did_kill_wall = TRUE;
1746 if (!can_cross) return FALSE;
1748 turn_flags_ptr->do_move = TRUE;
1749 if (((r_ptr->flags2 & RF2_PASS_WALL) != 0) && (!turn_flags_ptr->is_riding_mon || target_ptr->pass_wall) &&
1750 have_flag(f_ptr->flags, FF_CAN_PASS))
1752 turn_flags_ptr->did_pass_wall = TRUE;
1760 * @brief モンスターによるドアの開放・破壊を行う
1761 * @param target_ptr プレーヤーへの参照ポインタ
1762 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1763 * @param m_ptr モンスターへの参照ポインタ
1764 * @param ny モンスターのY座標
1765 * @param nx モンスターのX座標
1766 * @return モンスターが死亡した場合のみFALSE
1768 bool process_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
1770 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1772 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1773 if (!is_closed_door(target_ptr, g_ptr->feat)) return TRUE;
1775 feature_type *f_ptr;
1776 f_ptr = &f_info[g_ptr->feat];
1777 bool may_bash = bash_normal_door(target_ptr, turn_flags_ptr, m_ptr, ny, nx);
1778 bash_glass_door(target_ptr, turn_flags_ptr, m_ptr, f_ptr, may_bash);
1780 if (!turn_flags_ptr->did_open_door && !turn_flags_ptr->did_bash_door) return TRUE;
1782 if (turn_flags_ptr->did_bash_door &&
1783 ((randint0(100) < 50) || (feat_state(target_ptr, g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
1785 cave_alter_feat(target_ptr, ny, nx, FF_BASH);
1786 if (!monster_is_valid(m_ptr))
1788 target_ptr->update |= (PU_FLOW);
1789 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1790 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1797 cave_alter_feat(target_ptr, ny, nx, FF_OPEN);
1800 f_ptr = &f_info[g_ptr->feat];
1801 turn_flags_ptr->do_view = TRUE;
1807 * @brief モンスターが普通のドアを開ける処理
1808 * @param target_ptr プレーヤーへの参照ポインタ
1809 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1810 * @param m_ptr モンスターへの参照ポインタ
1811 * @param ny モンスターのY座標
1812 * @param nx モンスターのX座標
1813 * @return ここではドアを開けず、ガラスのドアを開ける可能性があるならTRUE
1815 bool bash_normal_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
1817 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1819 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1820 feature_type *f_ptr;
1821 f_ptr = &f_info[g_ptr->feat];
1822 turn_flags_ptr->do_move = FALSE;
1823 if (((r_ptr->flags2 & RF2_OPEN_DOOR) == 0) || !have_flag(f_ptr->flags, FF_OPEN) ||
1824 (is_pet(m_ptr) && ((target_ptr->pet_extra_flags & PF_OPEN_DOORS) == 0)))
1827 if (f_ptr->power == 0)
1829 turn_flags_ptr->did_open_door = TRUE;
1830 turn_flags_ptr->do_turn = TRUE;
1834 if (randint0(m_ptr->hp / 10) > f_ptr->power)
1836 cave_alter_feat(target_ptr, ny, nx, FF_DISARM);
1837 turn_flags_ptr->do_turn = TRUE;
1846 * @brief モンスターがガラスのドアを開ける処理
1847 * @param target_ptr プレーヤーへの参照ポインタ
1848 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1849 * @param m_ptr モンスターへの参照ポインタ
1850 * @param g_ptr グリッドへの参照ポインタ
1851 * @param f_ptr 地形への参照ポインタ
1854 void bash_glass_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, feature_type *f_ptr, bool may_bash)
1856 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1857 if (!may_bash || ((r_ptr->flags2 & RF2_BASH_DOOR) == 0) || !have_flag(f_ptr->flags, FF_BASH) ||
1858 (is_pet(m_ptr) && ((target_ptr->pet_extra_flags & PF_OPEN_DOORS) == 0)))
1861 if (!check_hp_for_feat_destruction(f_ptr, m_ptr) || (randint0(m_ptr->hp / 10) <= f_ptr->power))
1864 if (have_flag(f_ptr->flags, FF_GLASS))
1865 msg_print(_("ガラスが砕ける音がした!", "You hear glass breaking!"));
1867 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
1869 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1871 turn_flags_ptr->did_bash_door = TRUE;
1872 turn_flags_ptr->do_move = TRUE;
1873 turn_flags_ptr->must_alter_to_move = TRUE;
1878 * @brief 守りのルーンによるモンスターの移動制限を処理する
1879 * @param target_ptr プレーヤーへの参照ポインタ
1880 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1881 * @param m_ptr モンスターへの参照ポインタ
1882 * @param ny モンスターのY座標
1883 * @param nx モンスターのX座標
1886 bool process_protection_rune(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
1889 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1890 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1891 if (!turn_flags_ptr->do_move || !is_glyph_grid(g_ptr) ||
1892 (((r_ptr->flags1 & RF1_NEVER_BLOW) != 0) && player_bold(target_ptr, ny, nx)))
1895 turn_flags_ptr->do_move = FALSE;
1896 if (is_pet(m_ptr) || (randint1(BREAK_GLYPH) >= r_ptr->level))
1899 if (g_ptr->info & CAVE_MARK)
1901 msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
1904 g_ptr->info &= ~(CAVE_MARK);
1905 g_ptr->info &= ~(CAVE_OBJECT);
1907 turn_flags_ptr->do_move = TRUE;
1908 note_spot(target_ptr, ny, nx);
1914 * @brief 爆発のルーンにを処理する
1915 * @param target_ptr プレーヤーへの参照ポインタ
1916 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1917 * @param m_ptr モンスターへの参照ポインタ
1918 * @param ny モンスターのY座標
1919 * @param nx モンスターのX座標
1920 * @return モンスターが死亡した場合のみFALSE
1922 bool process_explosive_rune(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
1925 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1926 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1927 if (!turn_flags_ptr->do_move || !is_explosive_rune_grid(g_ptr) ||
1928 (((r_ptr->flags1 & RF1_NEVER_BLOW) != 0) && player_bold(target_ptr, ny, nx)))
1931 turn_flags_ptr->do_move = FALSE;
1932 if (is_pet(m_ptr)) return TRUE;
1934 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
1936 if (g_ptr->info & CAVE_MARK)
1938 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
1939 BIT_FLAGS project_flags = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI;
1940 project(target_ptr, 0, 2, ny, nx, 2 * (target_ptr->lev + damroll(7, 7)), GF_MANA, project_flags, -1);
1945 msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
1948 g_ptr->info &= ~(CAVE_MARK);
1949 g_ptr->info &= ~(CAVE_OBJECT);
1952 note_spot(target_ptr, ny, nx);
1953 lite_spot(target_ptr, ny, nx);
1955 if (!monster_is_valid(m_ptr)) return FALSE;
1957 turn_flags_ptr->do_move = TRUE;
1963 * @brief モンスターが移動した結果、そこにプレーヤーがいたら直接攻撃を行う
1964 * @param target_ptr プレーヤーへの参照ポインタ
1965 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1966 * @param m_idx モンスターID
1967 * @param ny 移動後の、モンスターのY座標
1968 * @param nx 移動後の、モンスターのX座標
1971 * 反攻撃の洞窟など、直接攻撃ができない場所では処理をスキップする
1973 void exe_monster_attack_to_player(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, POSITION ny, POSITION nx)
1975 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1976 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1977 if (!turn_flags_ptr->do_move || !player_bold(target_ptr, ny, nx))
1980 if (r_ptr->flags1 & RF1_NEVER_BLOW)
1982 if (is_original_ap_and_seen(target_ptr, m_ptr))
1983 r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
1985 turn_flags_ptr->do_move = FALSE;
1988 if (turn_flags_ptr->do_move && ((d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE) != 0) && !MON_CONFUSED(m_ptr))
1990 if (!(r_ptr->flags2 & RF2_STUPID))
1991 turn_flags_ptr->do_move = FALSE;
1992 else if (is_original_ap_and_seen(target_ptr, m_ptr))
1993 r_ptr->r_flags2 |= (RF2_STUPID);
1996 if (!turn_flags_ptr->do_move) return;
1998 if (!target_ptr->riding || one_in_(2))
2000 (void)make_attack_normal(target_ptr, m_idx);
2001 turn_flags_ptr->do_move = FALSE;
2002 turn_flags_ptr->do_turn = TRUE;
2008 * @brief モンスターからモンスターへの攻撃処理
2009 * @param target_ptr プレーヤーへの参照ポインタ
2010 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
2011 * @param m_idx モンスターID
2012 * @param g_ptr グリッドへの参照ポインタ
2013 * @param can_cross モンスターが地形を踏破できるならばTRUE
2014 * @return ターン消費が発生したらTRUE
2016 bool process_monster_attack_to_monster(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, grid_type *g_ptr, bool can_cross)
2018 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2019 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2020 monster_type *y_ptr;
2021 y_ptr = &target_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
2022 if (!turn_flags_ptr->do_move || (g_ptr->m_idx == 0)) return FALSE;
2024 monster_race *z_ptr = &r_info[y_ptr->r_idx];
2025 turn_flags_ptr->do_move = FALSE;
2026 if ((((r_ptr->flags2 & RF2_KILL_BODY) != 0) && ((r_ptr->flags1 & RF1_NEVER_BLOW) == 0) &&
2027 (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
2028 can_cross && (g_ptr->m_idx != target_ptr->riding)) ||
2029 are_enemies(target_ptr, m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
2031 return exe_monster_attack_to_monster(target_ptr, m_idx, g_ptr);
2034 if (((r_ptr->flags2 & RF2_MOVE_BODY) != 0) && ((r_ptr->flags1 & RF1_NEVER_MOVE) == 0) &&
2035 (r_ptr->mexp > z_ptr->mexp) &&
2036 can_cross && (g_ptr->m_idx != target_ptr->riding) &&
2037 monster_can_cross_terrain(target_ptr, target_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
2039 turn_flags_ptr->do_move = TRUE;
2040 turn_flags_ptr->did_move_body = TRUE;
2041 (void)set_monster_csleep(target_ptr, g_ptr->m_idx, 0);
2049 * @brief モンスターからモンスターへの直接攻撃を実行する
2050 * @param target_ptr プレーヤーへの参照ポインタ
2051 * @param m_idx モンスターID
2052 * @param g_ptr グリッドへの参照ポインタ
2054 bool exe_monster_attack_to_monster(player_type *target_ptr, MONSTER_IDX m_idx, grid_type *g_ptr)
2056 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2057 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2058 monster_type *y_ptr;
2059 y_ptr = &target_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
2060 if ((r_ptr->flags1 & RF1_NEVER_BLOW) != 0) return FALSE;
2062 if (((r_ptr->flags2 & RF2_KILL_BODY) == 0) && is_original_ap_and_seen(target_ptr, m_ptr))
2063 r_ptr->r_flags2 |= (RF2_KILL_BODY);
2065 if ((y_ptr->r_idx == 0) || (y_ptr->hp < 0)) return FALSE;
2066 if (monst_attack_monst(target_ptr, m_idx, g_ptr->m_idx)) return TRUE;
2067 if ((d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE) == 0) return FALSE;
2068 if (MON_CONFUSED(m_ptr)) return TRUE;
2069 if ((r_ptr->flags2 & RF2_STUPID) == 0) return FALSE;
2071 if (is_original_ap_and_seen(target_ptr, m_ptr))
2072 r_ptr->r_flags2 |= (RF2_STUPID);
2079 * @brief モンスターが壁を掘った後続処理を実行する
2080 * @param target_ptr プレーヤーへの参照ポインタ
2081 * @turn_flags_ptr ターン経過処理フラグへの参照ポインタ
2082 * @param m_ptr モンスターへの参照ポインタ
2083 * @param ny モンスターのY座標
2084 * @param nx モンスターのX座標
2085 * @return モンスターが死亡した場合のみFALSE
2087 bool process_post_dig_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
2089 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2091 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
2092 feature_type *f_ptr;
2093 f_ptr = &f_info[g_ptr->feat];
2094 if (!turn_flags_ptr->did_kill_wall || !turn_flags_ptr->do_move) return TRUE;
2096 if (one_in_(GRINDNOISE))
2098 if (have_flag(f_ptr->flags, FF_GLASS))
2099 msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
2101 msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
2104 cave_alter_feat(target_ptr, ny, nx, FF_HURT_DISI);
2106 if (!monster_is_valid(m_ptr))
2108 target_ptr->update |= (PU_FLOW);
2109 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2110 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2115 f_ptr = &f_info[g_ptr->feat];
2116 turn_flags_ptr->do_view = TRUE;
2117 turn_flags_ptr->do_turn = TRUE;
2123 * @brief 騎乗中のモンスター情報を更新する
2124 * @param target_ptr プレーヤーへの参照ポインタ
2125 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
2126 * @param m_idx モンスターID
2127 * @param oy 移動前の、モンスターのY座標
2128 * @param ox 移動前の、モンスターのX座標
2129 * @param ny 移動後の、モンスターのY座標
2130 * @param ox 移動後の、モンスターのX座標
2131 * @return アイテム等に影響を及ぼしたらTRUE
2133 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)
2135 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2137 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
2138 monster_type *y_ptr;
2139 y_ptr = &target_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
2140 if (turn_flags_ptr->is_riding_mon)
2141 return move_player_effect(target_ptr, ny, nx, MPE_DONT_PICKUP);
2143 target_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
2148 update_monster(target_ptr, g_ptr->m_idx, TRUE);
2151 g_ptr->m_idx = m_idx;
2154 update_monster(target_ptr, m_idx, TRUE);
2156 lite_spot(target_ptr, oy, ox);
2157 lite_spot(target_ptr, ny, nx);
2163 * @brief updateフィールドを更新する
2164 * @param target_ptr プレーヤーへの参照ポインタ
2165 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
2168 void update_player_type(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_race *r_ptr)
2170 if (turn_flags_ptr->do_view)
2172 target_ptr->update |= (PU_FLOW);
2173 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2176 if (turn_flags_ptr->do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
2177 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !target_ptr->phase_out)))
2179 target_ptr->update |= (PU_MON_LITE);
2185 * @brief モンスターのフラグを更新する
2186 * @param target_ptr プレーヤーへの参照ポインタ
2187 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
2188 * @param m_ptr モンスターへの参照ポインタ
2191 void update_monster_race_flags(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr)
2193 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2194 if (!is_original_ap_and_seen(target_ptr, m_ptr)) return;
2196 if (turn_flags_ptr->did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
2197 if (turn_flags_ptr->did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
2198 if (turn_flags_ptr->did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
2199 if (turn_flags_ptr->did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
2200 if (turn_flags_ptr->did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
2201 if (turn_flags_ptr->did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
2202 if (turn_flags_ptr->did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2207 * @brief モンスターの恐怖状態を処理する
2208 * @param target_ptr プレーヤーへの参照ポインタ
2209 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
2210 * @param m_idx モンスターID
2211 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
2212 * @return モンスターが戦いを決意したらTRUE
2214 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
2216 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2217 bool is_battle_determined = !turn_flags_ptr->do_turn && !turn_flags_ptr->do_move && MON_MONFEAR(m_ptr) && turn_flags_ptr->aware;
2218 if (!is_battle_determined) return FALSE;
2220 (void)set_monster_monfear(target_ptr, m_idx, 0);
2221 if (!turn_flags_ptr->see_m) return TRUE;
2223 GAME_TEXT m_name[MAX_NLEN];
2224 monster_desc(target_ptr, m_name, m_ptr, 0);
2225 msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
2231 * @brief 全モンスターのターン管理メインルーチン /
2232 * Process all the "live" monsters, once per game turn.
2235 * During each game current game turn, we scan through the list of all the "live" monsters,\n
2236 * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
2237 * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
2239 * Note that monsters can never move in the monster array (except when the\n
2240 * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
2242 * This function is responsible for at least half of the processor time\n
2243 * on a normal system with a "normal" amount of monsters and a player doing\n
2246 * When the player is resting, virtually 90% of the processor time is spent\n
2247 * in this function, and its children, "process_monster()" and "make_move()".\n
2249 * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
2250 * especially when the player is running.\n
2252 * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
2253 * monsters while they are still being "born". A monster is "fresh" only\n
2254 * during the game turn in which it is created, and we use the "hack_m_idx" to\n
2255 * determine if the monster is yet to be processed during the game turn.\n
2257 * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
2258 * move before any "nasty" monsters get to use their spell attacks.\n
2260 * Note that when the "knowledge" about the currently tracked monster\n
2261 * changes (flags, attacks, spells), we induce a redraw of the monster\n
2264 void process_monsters(player_type *target_ptr)
2266 old_race_flags tmp_flags;
2267 old_race_flags *old_race_flags_ptr = init_old_race_flags(&tmp_flags);
2269 floor_type *floor_ptr = target_ptr->current_floor_ptr;
2270 floor_ptr->monster_noise = FALSE;
2272 MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
2273 save_old_race_flags(target_ptr, old_race_flags_ptr);
2274 sweep_monster_process(target_ptr);
2277 if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
2280 update_player_window(target_ptr, old_race_flags_ptr);
2285 * @brief 古いモンスター情報の保存
2286 * @param target_ptr プレーヤーへの参照ポインタ
2287 * @param old_race_flags_ptr モンスターフラグへの参照ポインタ
2290 void save_old_race_flags(player_type *target_ptr, old_race_flags *old_race_flags_ptr)
2292 if (target_ptr->monster_race_idx == 0) return;
2294 monster_race *r_ptr;
2295 r_ptr = &r_info[target_ptr->monster_race_idx];
2297 old_race_flags_ptr->old_r_flags1 = r_ptr->r_flags1;
2298 old_race_flags_ptr->old_r_flags2 = r_ptr->r_flags2;
2299 old_race_flags_ptr->old_r_flags3 = r_ptr->r_flags3;
2300 old_race_flags_ptr->old_r_flags4 = r_ptr->r_flags4;
2301 old_race_flags_ptr->old_r_flags5 = r_ptr->r_flags5;
2302 old_race_flags_ptr->old_r_flags6 = r_ptr->r_flags6;
2303 old_race_flags_ptr->old_r_flagsr = r_ptr->r_flagsr;
2305 old_race_flags_ptr->old_r_blows0 = r_ptr->r_blows[0];
2306 old_race_flags_ptr->old_r_blows1 = r_ptr->r_blows[1];
2307 old_race_flags_ptr->old_r_blows2 = r_ptr->r_blows[2];
2308 old_race_flags_ptr->old_r_blows3 = r_ptr->r_blows[3];
2310 old_race_flags_ptr->old_r_cast_spell = r_ptr->r_cast_spell;
2315 * @brief フロア内のモンスターについてターン終了時の処理を繰り返す
2316 * @param target_ptr プレーヤーへの参照ポインタ
2318 void sweep_monster_process(player_type *target_ptr)
2320 floor_type *floor_ptr = target_ptr->current_floor_ptr;
2321 for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--)
2323 monster_type *m_ptr;
2324 m_ptr = &floor_ptr->m_list[i];
2326 if (target_ptr->leaving) return;
2327 if (!monster_is_valid(m_ptr)) continue;
2328 if (target_ptr->wild_mode) continue;
2330 if (m_ptr->mflag & MFLAG_BORN)
2332 m_ptr->mflag &= ~(MFLAG_BORN);
2336 if (m_ptr->cdis >= AAF_LIMIT) continue;
2337 if (!decide_process_continue(target_ptr, m_ptr)) continue;
2339 SPEED speed = decide_monster_speed(target_ptr, m_ptr, i);
2340 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
2341 if (m_ptr->energy_need > 0) continue;
2343 m_ptr->energy_need += ENERGY_NEED();
2345 process_monster(target_ptr, i);
2346 reset_target(m_ptr);
2348 if (target_ptr->no_flowed && one_in_(3))
2349 m_ptr->mflag2 |= MFLAG2_NOFLOW;
2351 if (!target_ptr->playing || target_ptr->is_dead) return;
2352 if (target_ptr->leaving) return;
2358 * @brief 後続のモンスター処理が必要かどうか判定する (要調査)
2359 * @param target_ptr プレーヤーへの参照ポインタ
2360 * @param m_ptr モンスターへの参照ポインタ
2361 * @return 後続処理が必要ならTRUE
2363 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr)
2365 monster_race *r_ptr;
2366 r_ptr = &r_info[m_ptr->r_idx];
2367 if (!target_ptr->no_flowed)
2369 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2372 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
2375 if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) &&
2376 (player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) || (target_ptr->cursed & TRC_AGGRAVATE)))
2379 if (m_ptr->target_y)
2387 * @brief モンスターの加速値を決定する
2388 * @param target_ptr プレーヤーへの参照ポインタ
2389 * @param m_ptr モンスターへの参照ポインタ
2390 * @param monster_number 走査中のモンスター番号
2393 SPEED decide_monster_speed(player_type *target_ptr, monster_type *m_ptr, int monster_number)
2396 if (target_ptr->riding == monster_number)
2398 speed = target_ptr->pspeed;
2402 speed = m_ptr->mspeed;
2403 if (ironman_nightmare) speed += 5;
2405 if (MON_FAST(m_ptr)) speed += 10;
2406 if (MON_SLOW(m_ptr)) speed -= 10;
2413 * @brief モンスターフラグの更新に基づき、モンスター表示を更新する
2414 * @param target_ptr プレーヤーへの参照ポインタ
2415 * @param old_race_flags_ptr モンスターフラグへの参照ポインタ
2418 void update_player_window(player_type *target_ptr, old_race_flags *old_race_flags_ptr)
2420 monster_race *r_ptr;
2421 r_ptr = &r_info[target_ptr->monster_race_idx];
2422 if ((old_race_flags_ptr->old_r_flags1 != r_ptr->r_flags1) ||
2423 (old_race_flags_ptr->old_r_flags2 != r_ptr->r_flags2) ||
2424 (old_race_flags_ptr->old_r_flags3 != r_ptr->r_flags3) ||
2425 (old_race_flags_ptr->old_r_flags4 != r_ptr->r_flags4) ||
2426 (old_race_flags_ptr->old_r_flags5 != r_ptr->r_flags5) ||
2427 (old_race_flags_ptr->old_r_flags6 != r_ptr->r_flags6) ||
2428 (old_race_flags_ptr->old_r_flagsr != r_ptr->r_flagsr) ||
2429 (old_race_flags_ptr->old_r_blows0 != r_ptr->r_blows[0]) ||
2430 (old_race_flags_ptr->old_r_blows1 != r_ptr->r_blows[1]) ||
2431 (old_race_flags_ptr->old_r_blows2 != r_ptr->r_blows[2]) ||
2432 (old_race_flags_ptr->old_r_blows3 != r_ptr->r_blows[3]) ||
2433 (old_race_flags_ptr->old_r_cast_spell != r_ptr->r_cast_spell))
2435 target_ptr->window |= (PW_MONSTER);