2 * @brief モンスターの移動方向を走査する処理
7 #include "monster-floor/monster-sweep-grid.h"
8 #include "floor/cave.h"
9 #include "floor/geometry.h"
10 #include "floor/line-of-sight.h"
11 #include "grid/feature.h"
12 #include "grid/grid.h"
13 #include "monster-floor/monster-safety-hiding.h"
14 #include "monster-race/monster-race.h"
15 #include "monster-race/race-ability-mask.h"
16 #include "monster-race/race-flags1.h"
17 #include "monster-race/race-flags2.h"
18 #include "monster-race/race-flags3.h"
19 #include "monster/monster-flag-types.h"
20 #include "monster/monster-info.h"
21 #include "monster/monster-processor-util.h"
22 #include "monster/monster-status.h"
23 #include "player/player-status-flags.h"
24 #include "system/floor-type-definition.h"
25 #include "system/grid-type-definition.h"
26 #include "system/monster-race-definition.h"
27 #include "system/monster-type-definition.h"
28 #include "system/player-type-definition.h"
29 #include "target/projection-path-calculator.h"
30 #include "util/bit-flags-calculator.h"
34 * @param player_ptr プレイヤーへの参照ポインタ
35 * @param m_idx 逃走するモンスターの参照ID
36 * @param mm 移動方向を返す方向IDの参照ポインタ
38 MonsterSweepGrid::MonsterSweepGrid(PlayerType *player_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
39 : player_ptr(player_ptr)
46 * @brief モンスターの移動方向を返す /
47 * Choose "logical" directions for monster movement
48 * @return 有効方向があった場合TRUEを返す
49 * @todo 分割したいが条件が多すぎて適切な関数名と詳細処理を追いきれない……
51 bool MonsterSweepGrid::get_movable_grid()
53 auto *floor_ptr = this->player_ptr->current_floor_ptr;
54 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
55 auto *r_ptr = &monraces_info[m_ptr->r_idx];
58 auto y2 = this->player_ptr->y;
59 auto x2 = this->player_ptr->x;
60 this->will_run = this->mon_will_run();
61 const auto no_flow = m_ptr->mflag2.has(MonsterConstantFlagType::NOFLOW) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].get_cost(r_ptr) > 2);
62 this->can_pass_wall = r_ptr->feature_flags.has(MonsterFeatureType::PASS_WALL) && ((this->m_idx != this->player_ptr->riding) || has_pass_wall(this->player_ptr));
63 if (!this->will_run && m_ptr->target_y) {
64 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
66 const auto is_enemies = are_enemies(this->player_ptr, *m_ptr, floor_ptr->m_list[t_m_idx]);
67 const auto is_los = los(this->player_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x);
68 const auto is_projectable = projectable(this->player_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x);
69 if (is_enemies && is_los && is_projectable) {
70 y = m_ptr->fy - m_ptr->target_y;
71 x = m_ptr->fx - m_ptr->target_x;
77 this->check_hiding_grid(&y, &x, &y2, &x2);
79 this->sweep_movable_grid(&y2, &x2, no_flow);
84 this->search_pet_runnable_grid(&y, &x, no_flow);
89 store_moves_val(this->mm, y, x);
94 * @brief モンスターがプレイヤーから逃走するかどうかを返す /
95 * Returns whether a given monster will try to run from the player.
96 * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
98 bool MonsterSweepGrid::mon_will_run()
100 auto *m_ptr = &this->player_ptr->current_floor_ptr->m_list[this->m_idx];
101 auto *r_ptr = &monraces_info[m_ptr->r_idx];
102 if (m_ptr->is_pet()) {
103 return (this->player_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - this->player_ptr->pet_follow_distance));
106 if (m_ptr->cdis > MAX_PLAYER_SIGHT + 5) {
110 if (m_ptr->is_fearful()) {
114 if (m_ptr->cdis <= 5) {
118 auto p_lev = this->player_ptr->lev;
119 auto m_lev = r_ptr->level + (this->m_idx & 0x08) + 25;
120 if (m_lev > p_lev + 4) {
124 if (m_lev + 4 <= p_lev) {
128 auto p_chp = this->player_ptr->chp;
129 auto p_mhp = this->player_ptr->mhp;
130 auto m_chp = m_ptr->hp;
131 auto m_mhp = m_ptr->maxhp;
132 uint32_t p_val = (p_lev * p_mhp) + (p_chp << 2);
133 uint32_t m_val = (m_lev * m_mhp) + (m_chp << 2);
134 return p_val * m_mhp > m_val * p_mhp;
137 void MonsterSweepGrid::check_hiding_grid(POSITION *y, POSITION *x, POSITION *y2, POSITION *x2)
139 auto *floor_ptr = this->player_ptr->current_floor_ptr;
140 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
141 auto *r_ptr = &monraces_info[m_ptr->r_idx];
142 if (this->done || this->will_run || !m_ptr->is_hostile() || none_bits(r_ptr->flags1, RF1_FRIENDS)) {
146 if ((!los(this->player_ptr, m_ptr->fy, m_ptr->fx, this->player_ptr->y, this->player_ptr->x) || !projectable(this->player_ptr, m_ptr->fy, m_ptr->fx, this->player_ptr->y, this->player_ptr->x))) {
147 if (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].get_distance(r_ptr) >= MAX_PLAYER_SIGHT / 2) {
152 this->search_room_to_run(y, x);
153 if (this->done || (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].get_distance(r_ptr) >= 3)) {
157 for (auto i = 0; i < 8; i++) {
158 *y2 = this->player_ptr->y + ddy_ddd[(this->m_idx + i) & 7];
159 *x2 = this->player_ptr->x + ddx_ddd[(this->m_idx + i) & 7];
160 if ((m_ptr->fy == *y2) && (m_ptr->fx == *x2)) {
161 *y2 = this->player_ptr->y;
162 *x2 = this->player_ptr->x;
166 if (!in_bounds2(floor_ptr, *y2, *x2) || !monster_can_enter(this->player_ptr, *y2, *x2, r_ptr, 0)) {
173 *y = m_ptr->fy - *y2;
174 *x = m_ptr->fx - *x2;
178 void MonsterSweepGrid::search_room_to_run(POSITION *y, POSITION *x)
180 auto *floor_ptr = this->player_ptr->current_floor_ptr;
181 auto *r_ptr = &monraces_info[floor_ptr->m_list[this->m_idx].r_idx];
182 if (r_ptr->kind_flags.has_not(MonsterKindType::ANIMAL) || this->can_pass_wall || r_ptr->feature_flags.has(MonsterFeatureType::KILL_WALL)) {
187 for (auto i = 0; i < 8; i++) {
188 auto xx = this->player_ptr->x + ddx_ddd[i];
189 auto yy = this->player_ptr->y + ddy_ddd[i];
190 if (!in_bounds2(floor_ptr, yy, xx)) {
194 auto *g_ptr = &floor_ptr->grid_array[yy][xx];
195 if (monster_can_cross_terrain(this->player_ptr, g_ptr->feat, r_ptr, 0)) {
200 if (floor_ptr->grid_array[this->player_ptr->y][this->player_ptr->x].is_room()) {
204 if (r_ptr->ability_flags.none()) {
208 if (room >= (8 * (this->player_ptr->chp + this->player_ptr->csp)) / (this->player_ptr->mhp + this->player_ptr->msp)) {
212 if (find_hiding(this->player_ptr, this->m_idx, y, x)) {
217 void MonsterSweepGrid::search_pet_runnable_grid(POSITION *y, POSITION *x, bool no_flow)
219 auto *floor_ptr = this->player_ptr->current_floor_ptr;
220 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
221 if (m_ptr->is_pet() && this->will_run) {
227 if (this->done || !this->will_run) {
233 if (find_safety(this->player_ptr, this->m_idx, y, x) && !no_flow && this->sweep_runnable_away_grid(y, x)) {
246 * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
247 * Choose the "best" direction for "flowing"
248 * @param yp 移動先のマスのY座標を返す参照ポインタ
249 * @param xp 移動先のマスのX座標を返す参照ポインタ
250 * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
252 void MonsterSweepGrid::sweep_movable_grid(POSITION *yp, POSITION *xp, bool no_flow)
254 auto *floor_ptr = this->player_ptr->current_floor_ptr;
255 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
256 auto *r_ptr = &monraces_info[m_ptr->r_idx];
257 if (!this->check_movable_grid(yp, xp, no_flow)) {
263 auto *g_ptr = &floor_ptr->grid_array[y1][x1];
264 if (player_has_los_bold(this->player_ptr, y1, x1) && projectable(this->player_ptr, this->player_ptr->y, this->player_ptr->x, y1, x1)) {
265 if ((distance(y1, x1, this->player_ptr->y, this->player_ptr->x) == 1) || (r_ptr->freq_spell > 0) || (g_ptr->get_cost(r_ptr) > 5)) {
270 auto use_scent = false;
271 if (g_ptr->get_cost(r_ptr)) {
273 } else if (g_ptr->when) {
274 if (floor_ptr->grid_array[this->player_ptr->y][this->player_ptr->x].when - g_ptr->when > 127) {
284 this->determine_when_cost(yp, xp, y1, x1, use_scent);
287 bool MonsterSweepGrid::check_movable_grid(POSITION *yp, POSITION *xp, const bool no_flow)
289 auto *r_ptr = &monraces_info[this->player_ptr->current_floor_ptr->m_list[this->m_idx].r_idx];
290 if ((r_ptr->ability_flags.has_any_of(RF_ABILITY_ATTACK_MASK)) && (sweep_ranged_attack_grid(yp, xp))) {
298 if (r_ptr->feature_flags.has(MonsterFeatureType::PASS_WALL) && ((this->m_idx != this->player_ptr->riding) || has_pass_wall(this->player_ptr))) {
302 if (r_ptr->feature_flags.has(MonsterFeatureType::KILL_WALL) && (this->m_idx != this->player_ptr->riding)) {
310 * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
311 * Search spell castable grid
312 * @param yp 適したマスのY座標を返す参照ポインタ
313 * @param xp 適したマスのX座標を返す参照ポインタ
314 * @return 有効なマスがあった場合TRUEを返す
316 bool MonsterSweepGrid::sweep_ranged_attack_grid(POSITION *yp, POSITION *xp)
318 auto *floor_ptr = this->player_ptr->current_floor_ptr;
319 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
320 auto *r_ptr = &monraces_info[m_ptr->r_idx];
323 if (projectable(this->player_ptr, y1, x1, this->player_ptr->y, this->player_ptr->x)) {
327 auto now_cost = (int)floor_ptr->grid_array[y1][x1].get_cost(r_ptr);
332 if (r_ptr->behavior_flags.has_any_of({ MonsterBehaviorType::BASH_DOOR, MonsterBehaviorType::OPEN_DOOR })) {
333 this->can_open_door = true;
336 for (auto i = 7; i >= 0; i--) {
337 auto y = y1 + ddy_ddd[i];
338 auto x = x1 + ddx_ddd[i];
339 if (!in_bounds2(floor_ptr, y, x)) {
343 if (player_bold(this->player_ptr, y, x)) {
347 auto *g_ptr = &floor_ptr->grid_array[y][x];
348 this->cost = (int)g_ptr->get_cost(r_ptr);
349 if (!this->is_best_cost(y, x, now_cost)) {
353 this->best = this->cost;
354 *yp = y1 + ddy_ddd[i];
355 *xp = x1 + ddx_ddd[i];
358 return this->best != 999;
361 bool MonsterSweepGrid::is_best_cost(const POSITION y, const POSITION x, const int now_cost)
363 auto *floor_ptr = this->player_ptr->current_floor_ptr;
364 auto *r_ptr = &monraces_info[floor_ptr->m_list[this->m_idx].r_idx];
365 auto is_riding = this->m_idx == this->player_ptr->riding;
366 if ((r_ptr->feature_flags.has_not(MonsterFeatureType::PASS_WALL) || (is_riding && !has_pass_wall(this->player_ptr))) && (r_ptr->feature_flags.has_not(MonsterFeatureType::KILL_WALL) || is_riding)) {
367 if (this->cost == 0) {
371 auto *g_ptr = &floor_ptr->grid_array[y][x];
372 if (!this->can_open_door && is_closed_door(this->player_ptr, g_ptr->feat)) {
377 if (this->cost == 0) {
381 if (now_cost < this->cost) {
385 if (!projectable(this->player_ptr, y, x, this->player_ptr->y, this->player_ptr->x)) {
389 if (this->best < this->cost) {
397 * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
398 * Provide a location to flee to, but give the player a wide berth.
399 * @param yp 移動先のマスのY座標を返す参照ポインタ
400 * @param xp 移動先のマスのX座標を返す参照ポインタ
401 * @return 有効なマスがあった場合TRUEを返す
403 bool MonsterSweepGrid::sweep_runnable_away_grid(POSITION *yp, POSITION *xp)
407 auto *floor_ptr = this->player_ptr->current_floor_ptr;
408 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
409 auto *r_ptr = &monraces_info[m_ptr->r_idx];
415 for (auto i = 7; i >= 0; i--) {
416 auto y = fy + ddy_ddd[i];
417 auto x = fx + ddx_ddd[i];
418 if (!in_bounds2(floor_ptr, y, x)) {
422 auto dis = distance(y, x, y1, x1);
423 auto s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].get_distance(r_ptr) + 1);
446 void MonsterSweepGrid::determine_when_cost(POSITION *yp, POSITION *xp, POSITION y1, POSITION x1, const bool use_scent)
448 auto *floor_ptr = this->player_ptr->current_floor_ptr;
449 for (auto i = 7; i >= 0; i--) {
450 auto y = y1 + ddy_ddd[i];
451 auto x = x1 + ddx_ddd[i];
452 if (!in_bounds2(floor_ptr, y, x)) {
456 auto *g_ptr = &floor_ptr->grid_array[y][x];
458 int when = g_ptr->when;
459 if (this->best > when) {
465 auto *r_ptr = &monraces_info[floor_ptr->m_list[this->m_idx].r_idx];
466 this->cost = r_ptr->behavior_flags.has_any_of({ MonsterBehaviorType::BASH_DOOR, MonsterBehaviorType::OPEN_DOOR }) ? g_ptr->get_distance(r_ptr) : g_ptr->get_cost(r_ptr);
467 if ((this->cost == 0) || (this->best < this->cost)) {
471 this->best = this->cost;
474 *yp = this->player_ptr->y + 16 * ddy_ddd[i];
475 *xp = this->player_ptr->x + 16 * ddx_ddd[i];