2 * @brief モンスターの逃走・隠匿に関する処理
7 #include "monster-floor/monster-safety-hiding.h"
8 #include "floor/floor.h"
9 #include "monster-floor/monster-dist-offsets.h"
10 #include "monster-race/monster-race.h"
11 #include "monster/monster-flag-types.h"
12 #include "monster/monster-info.h"
13 #include "mspell/mspells1.h"
16 * @brief モンスターが逃げ込める地点を走査する
17 * @param target_ptr プレーヤーへの参照ポインタ
18 * @param m_idx モンスターID
21 * @param d モンスターがいる地点からの距離
24 static coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONSTER_IDX m_idx, const POSITION *y_offsets, const POSITION *x_offsets, int d)
26 coordinate_candidate candidate = init_coordinate_candidate();
27 floor_type *floor_ptr = target_ptr->current_floor_ptr;
28 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
29 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
31 i++, dx = x_offsets[i], dy = y_offsets[i])
33 POSITION y = m_ptr->fy + dy;
34 POSITION x = m_ptr->fx + dx;
35 if (!in_bounds(floor_ptr, y, x)) continue;
38 g_ptr = &floor_ptr->grid_array[y][x];
40 BIT_FLAGS16 riding_mode = (m_idx == target_ptr->riding) ? CEM_RIDING : 0;
41 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], riding_mode))
44 if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
46 if (g_ptr->dist == 0) continue;
47 if (g_ptr->dist > floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist + 2 * d) continue;
50 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x)) continue;
52 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
53 if (dis <= candidate.gdis) continue;
65 * @brief モンスターが逃げ込める安全な地点を返す /
66 * Choose a "safe" location near a monster for it to run toward.
67 * @param target_ptr プレーヤーへの参照ポインタ
68 * @param m_idx モンスターの参照ID
69 * @param yp 移動先のマスのY座標を返す参照ポインタ
70 * @param xp 移動先のマスのX座標を返す参照ポインタ
71 * @return 有効なマスがあった場合TRUEを返す
73 * A location is "safe" if it can be reached quickly and the player\n
74 * is not able to fire into it (it isn't a "clean shot"). So, this will\n
75 * cause monsters to "duck" behind walls. Hopefully, monsters will also\n
76 * try to run towards corridor openings if they are in a room.\n
78 * This function may take lots of CPU time if lots of monsters are\n
81 * Return TRUE if a safe location is available.\n
83 bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
85 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
86 for (POSITION d = 1; d < 10; d++)
88 const POSITION *y_offsets;
89 y_offsets = dist_offsets_y[d];
91 const POSITION *x_offsets;
92 x_offsets = dist_offsets_x[d];
94 coordinate_candidate candidate = sweep_safe_coordinate(target_ptr, m_idx, y_offsets, x_offsets, d);
96 if (candidate.gdis <= 0) continue;
98 *yp = m_ptr->fy - candidate.gy;
99 *xp = m_ptr->fx - candidate.gx;
109 * @brief モンスターが隠れられる地点を走査する
110 * @param target_ptr プレーヤーへの参照ポインタ
111 * @param m_idx モンスターID
114 * @param candidate 隠れられる地点の候補地
117 static void sweep_hiding_candidate(player_type *target_ptr, monster_type *m_ptr, const POSITION *y_offsets, const POSITION *x_offsets, coordinate_candidate *candidate)
119 monster_race *r_ptr = &r_info[m_ptr->r_idx];
120 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
122 i++, dx = x_offsets[i], dy = y_offsets[i])
124 POSITION y = m_ptr->fy + dy;
125 POSITION x = m_ptr->fx + dx;
126 if (!in_bounds(target_ptr->current_floor_ptr, y, x)) continue;
127 if (!monster_can_enter(target_ptr, y, x, r_ptr, 0)) continue;
128 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))
131 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
132 if (dis < candidate->gdis && dis >= 2)
136 candidate->gdis = dis;
143 * @brief モンスターが隠れ潜める地点を返す /
144 * Choose a good hiding place near a monster for it to run toward.
145 * @param target_ptr プレーヤーへの参照ポインタ
146 * @param m_idx モンスターの参照ID
147 * @param yp 移動先のマスのY座標を返す参照ポインタ
148 * @param xp 移動先のマスのX座標を返す参照ポインタ
149 * @return 有効なマスがあった場合TRUEを返す
151 * Pack monsters will use this to "ambush" the player and lure him out\n
152 * of corridors into open space so they can swarm him.\n
154 * Return TRUE if a good location is available.\n
156 bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
158 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
159 coordinate_candidate candidate = init_coordinate_candidate();
160 candidate.gdis = 999;
162 for (POSITION d = 1; d < 10; d++)
164 const POSITION *y_offsets;
165 y_offsets = dist_offsets_y[d];
167 const POSITION *x_offsets;
168 x_offsets = dist_offsets_x[d];
170 sweep_hiding_candidate(target_ptr, m_ptr, y_offsets, x_offsets, &candidate);
171 if (candidate.gdis >= 999) continue;
173 *yp = m_ptr->fy - candidate.gy;
174 *xp = m_ptr->fx - candidate.gx;