2 * @brief モンスターの移動方向を走査する処理
7 #include "monster/monster-sweep-grid.h"
8 #include "monster/monster-safety-hiding.h"
9 #include "spell/monster-spell.h"
12 * @brief モンスターがプレイヤーから逃走するかどうかを返す /
13 * Returns whether a given monster will try to run from the player.
14 * @param m_idx 逃走するモンスターの参照ID
15 * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
17 * Monsters will attempt to avoid very powerful players. See below.\n
19 * Because this function is called so often, little details are important\n
20 * for efficiency. Like not using "mod" or "div" when possible. And\n
21 * attempting to check the conditions in an optimal order. Note that\n
22 * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
24 * Note that this function is responsible for about one to five percent\n
25 * of the processor use in normal conditions...\n
27 static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
29 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
30 monster_race *r_ptr = &r_info[m_ptr->r_idx];
34 return ((target_ptr->pet_follow_distance < 0) &&
35 (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
38 if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
39 if (MON_MONFEAR(m_ptr)) return TRUE;
40 if (m_ptr->cdis <= 5) return FALSE;
42 PLAYER_LEVEL p_lev = target_ptr->lev;
43 DEPTH m_lev = r_ptr->level + (m_idx & 0x08) + 25;
44 if (m_lev > p_lev + 4) return FALSE;
45 if (m_lev + 4 <= p_lev) return TRUE;
47 HIT_POINT p_chp = target_ptr->chp;
48 HIT_POINT p_mhp = target_ptr->mhp;
49 HIT_POINT m_chp = m_ptr->hp;
50 HIT_POINT m_mhp = m_ptr->maxhp;
51 u32b p_val = (p_lev * p_mhp) + (p_chp << 2);
52 u32b m_val = (m_lev * m_mhp) + (m_chp << 2);
53 if (p_val * m_mhp > m_val * p_mhp) return TRUE;
60 * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
61 * Search spell castable grid
62 * @param target_ptr プレーヤーへの参照ポインタ
63 * @param m_idx モンスターの参照ID
64 * @param yp 適したマスのY座標を返す参照ポインタ
65 * @param xp 適したマスのX座標を返す参照ポインタ
66 * @return 有効なマスがあった場合TRUEを返す
68 static bool sweep_ranged_attack_grid(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
70 floor_type *floor_ptr = target_ptr->current_floor_ptr;
71 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
72 monster_race *r_ptr = &r_info[m_ptr->r_idx];
74 POSITION y1 = m_ptr->fy;
75 POSITION x1 = m_ptr->fx;
77 if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
79 int now_cost = floor_ptr->grid_array[y1][x1].cost;
80 if (now_cost == 0) now_cost = 999;
82 bool can_open_door = FALSE;
83 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
89 for (int i = 7; i >= 0; i--)
91 POSITION y = y1 + ddy_ddd[i];
92 POSITION x = x1 + ddx_ddd[i];
93 if (!in_bounds2(floor_ptr, y, x)) continue;
94 if (player_bold(target_ptr, y, x)) return FALSE;
97 g_ptr = &floor_ptr->grid_array[y][x];
98 int cost = g_ptr->cost;
99 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))))
101 if (cost == 0) continue;
102 if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat)) continue;
105 if (cost == 0) cost = 998;
107 if (now_cost < cost) continue;
108 if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x)) continue;
109 if (best < cost) continue;
112 *yp = y1 + ddy_ddd[i];
113 *xp = x1 + ddx_ddd[i];
116 if (best == 999) return FALSE;
123 * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
124 * Choose the "best" direction for "flowing"
125 * @param m_idx モンスターの参照ID
126 * @param yp 移動先のマスのY座標を返す参照ポインタ
127 * @param xp 移動先のマスのX座標を返す参照ポインタ
128 * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
131 * Note that ghosts and rock-eaters are never allowed to "flow",\n
132 * since they should move directly towards the player.\n
134 * Prefer "non-diagonal" directions, but twiddle them a little\n
135 * to angle slightly towards the player's actual location.\n
137 * Allow very perceptive monsters to track old "spoor" left by\n
138 * previous locations occupied by the player. This will tend\n
139 * to have monsters end up either near the player or on a grid\n
140 * recently occupied by the player (and left via "teleport").\n
142 * Note that if "smell" is turned on, all monsters get vicious.\n
144 * Also note that teleporting away from a location will cause\n
145 * the monsters who were chasing you to converge on that location\n
146 * as long as you are still near enough to "annoy" them without\n
147 * being close enough to chase directly. I have no idea what will\n
148 * happen if you combine "smell" with low "aaf" values.\n
150 static void sweep_movable_grid(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
153 floor_type *floor_ptr = target_ptr->current_floor_ptr;
154 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
155 monster_race *r_ptr = &r_info[m_ptr->r_idx];
157 if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
158 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
159 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
161 if (sweep_ranged_attack_grid(target_ptr, m_idx, yp, xp)) return;
165 if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return;
166 if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return;
168 POSITION y1 = m_ptr->fy;
169 POSITION x1 = m_ptr->fx;
170 if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return;
172 g_ptr = &floor_ptr->grid_array[y1][x1];
175 bool use_scent = FALSE;
180 else if (g_ptr->when)
182 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return;
192 for (int i = 7; i >= 0; i--)
194 POSITION y = y1 + ddy_ddd[i];
195 POSITION x = x1 + ddx_ddd[i];
197 if (!in_bounds2(floor_ptr, y, x)) continue;
199 g_ptr = &floor_ptr->grid_array[y][x];
202 int when = g_ptr->when;
203 if (best > when) continue;
210 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
219 if ((cost == 0) || (best < cost)) continue;
224 *yp = target_ptr->y + 16 * ddy_ddd[i];
225 *xp = target_ptr->x + 16 * ddx_ddd[i];
231 * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
232 * Provide a location to flee to, but give the player a wide berth.
233 * @param m_idx モンスターの参照ID
234 * @param yp 移動先のマスのY座標を返す参照ポインタ
235 * @param xp 移動先のマスのX座標を返す参照ポインタ
236 * @return 有効なマスがあった場合TRUEを返す
238 * A monster may wish to flee to a location that is behind the player,\n
239 * but instead of heading directly for it, the monster should "swerve"\n
240 * around the player so that he has a smaller chance of getting hit.\n
242 static bool sweep_runnable_away_grid(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
244 POSITION gy = 0, gx = 0;
246 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
247 POSITION fy = m_ptr->fy;
248 POSITION fx = m_ptr->fx;
250 POSITION y1 = fy - (*yp);
251 POSITION x1 = fx - (*xp);
254 for (int i = 7; i >= 0; i--)
256 POSITION y = fy + ddy_ddd[i];
257 POSITION x = fx + ddx_ddd[i];
258 if (!in_bounds2(floor_ptr, y, x)) continue;
260 POSITION dis = distance(y, x, y1, x1);
261 POSITION s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
264 if (s < score) continue;
271 if (score == -1) return FALSE;
281 * todo 分割したいが条件が多すぎて適切な関数名と詳細処理を追いきれない……
282 * @brief モンスターの移動方向を返す /
283 * Choose "logical" directions for monster movement
284 * @param target_ptr プレーヤーへの参照ポインタ
285 * @param m_idx モンスターの参照ID
286 * @param mm 移動方向を返す方向IDの参照ポインタ
287 * @return 有効方向があった場合TRUEを返す
289 bool get_movable_grid(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
291 floor_type *floor_ptr = target_ptr->current_floor_ptr;
292 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
293 monster_race *r_ptr = &r_info[m_ptr->r_idx];
294 POSITION y = 0, x = 0;
295 POSITION y2 = target_ptr->y;
296 POSITION x2 = target_ptr->x;
298 bool will_run = mon_will_run(target_ptr, m_idx);
300 bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) != 0) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2);
301 bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) != 0) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall);
303 if (!will_run && m_ptr->target_y)
305 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
307 are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) &&
308 los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
309 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
311 y = m_ptr->fy - m_ptr->target_y;
312 x = m_ptr->fx - m_ptr->target_x;
317 if (!done && !will_run && is_hostile(m_ptr) &&
318 (r_ptr->flags1 & RF1_FRIENDS) &&
319 ((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)) ||
320 (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
322 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
323 !(r_ptr->flags2 & RF2_KILL_WALL))
326 for (int i = 0; i < 8; i++)
328 int xx = target_ptr->x + ddx_ddd[i];
329 int yy = target_ptr->y + ddy_ddd[i];
331 if (!in_bounds2(floor_ptr, yy, xx)) continue;
333 g_ptr = &floor_ptr->grid_array[yy][xx];
334 if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0))
340 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
341 if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
343 if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
344 (target_ptr->mhp + target_ptr->msp))
346 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
350 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
352 for (int i = 0; i < 8; i++)
354 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
355 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
356 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
363 if (!in_bounds2(floor_ptr, y2, x2)) continue;
364 if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0)) continue;
377 sweep_movable_grid(target_ptr, m_idx, &y2, &x2, no_flow);
382 if (is_pet(m_ptr) && will_run)
388 if (!done && will_run)
392 if (find_safety(target_ptr, m_idx, &y, &x) && !no_flow)
394 if (sweep_runnable_away_grid(target_ptr->current_floor_ptr, m_idx, &y, &x))
406 if (!x && !y) return FALSE;
408 store_moves_val(mm, y, x);