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 target_ptr プレーヤーへの参照ポインタ
35 * @param m_idx 逃走するモンスターの参照ID
36 * @param mm 移動方向を返す方向IDの参照ポインタ
38 MonsterSweepGrid::MonsterSweepGrid(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
39 : target_ptr(target_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->target_ptr->current_floor_ptr;
54 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
55 auto *r_ptr = &r_info[m_ptr->r_idx];
58 auto y2 = this->target_ptr->y;
59 auto x2 = this->target_ptr->x;
60 this->will_run = this->mon_will_run();
61 auto no_flow = m_ptr->mflag2.has(MFLAG2::NOFLOW) && floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].get_cost(r_ptr) > 2;
62 this->can_pass_wall = any_bits(r_ptr->flags2, RF2_PASS_WALL) && ((this->m_idx != this->target_ptr->riding) || has_pass_wall(this->target_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;
65 if ((t_m_idx > 0) && are_enemies(this->target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx])
66 && los(this->target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x)
67 && projectable(this->target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x)) {
68 y = m_ptr->fy - m_ptr->target_y;
69 x = m_ptr->fx - m_ptr->target_x;
74 this->check_hiding_grid(&y, &x, &y2, &x2);
76 this->sweep_movable_grid(&y2, &x2, no_flow);
81 this->search_pet_runnable_grid(&y, &x, no_flow);
86 store_moves_val(this->mm, y, x);
91 * @brief モンスターがプレイヤーから逃走するかどうかを返す /
92 * Returns whether a given monster will try to run from the player.
93 * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
95 bool MonsterSweepGrid::mon_will_run()
97 auto *m_ptr = &this->target_ptr->current_floor_ptr->m_list[this->m_idx];
98 auto *r_ptr = &r_info[m_ptr->r_idx];
100 return ((this->target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - this->target_ptr->pet_follow_distance)));
103 if (m_ptr->cdis > MAX_SIGHT + 5) {
107 if (monster_fear_remaining(m_ptr)) {
111 if (m_ptr->cdis <= 5) {
115 auto p_lev = this->target_ptr->lev;
116 auto m_lev = r_ptr->level + (this->m_idx & 0x08) + 25;
117 if (m_lev > p_lev + 4) {
121 if (m_lev + 4 <= p_lev) {
125 auto p_chp = this->target_ptr->chp;
126 auto p_mhp = this->target_ptr->mhp;
127 auto m_chp = m_ptr->hp;
128 auto m_mhp = m_ptr->maxhp;
129 u32b p_val = (p_lev * p_mhp) + (p_chp << 2);
130 u32b m_val = (m_lev * m_mhp) + (m_chp << 2);
131 return p_val * m_mhp > m_val * p_mhp;
134 void MonsterSweepGrid::check_hiding_grid(POSITION *y, POSITION *x, POSITION *y2, POSITION *x2)
136 auto *floor_ptr = this->target_ptr->current_floor_ptr;
137 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
138 auto *r_ptr = &r_info[m_ptr->r_idx];
139 if (this->done || this->will_run || !is_hostile(m_ptr) || none_bits(r_ptr->flags1, RF1_FRIENDS)) {
143 if ((!los(this->target_ptr, m_ptr->fy, m_ptr->fx, this->target_ptr->y, this->target_ptr->x)
144 || !projectable(this->target_ptr, m_ptr->fy, m_ptr->fx, this->target_ptr->y, this->target_ptr->x))) {
145 if (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].get_distance(r_ptr) >= MAX_SIGHT / 2) {
150 this->search_room_to_run(y, x);
151 if (this->done || (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].get_distance(r_ptr) >= 3)) {
155 for (auto i = 0; i < 8; i++) {
156 *y2 = this->target_ptr->y + ddy_ddd[(this->m_idx + i) & 7];
157 *x2 = this->target_ptr->x + ddx_ddd[(this->m_idx + i) & 7];
158 if ((m_ptr->fy == *y2) && (m_ptr->fx == *x2)) {
159 *y2 = this->target_ptr->y;
160 *x2 = this->target_ptr->x;
164 if (!in_bounds2(floor_ptr, *y2, *x2) || !monster_can_enter(this->target_ptr, *y2, *x2, r_ptr, 0)) {
171 *y = m_ptr->fy - *y2;
172 *x = m_ptr->fx - *x2;
176 void MonsterSweepGrid::search_room_to_run(POSITION *y, POSITION *x)
178 auto *floor_ptr = this->target_ptr->current_floor_ptr;
179 auto *r_ptr = &r_info[floor_ptr->m_list[this->m_idx].r_idx];
180 if (none_bits(r_ptr->flags3, RF3_ANIMAL) || this->can_pass_wall || any_bits(r_ptr->flags2, RF2_KILL_WALL)) {
185 for (auto i = 0; i < 8; i++) {
186 auto xx = this->target_ptr->x + ddx_ddd[i];
187 auto yy = this->target_ptr->y + ddy_ddd[i];
188 if (!in_bounds2(floor_ptr, yy, xx)) {
192 auto *g_ptr = &floor_ptr->grid_array[yy][xx];
193 if (monster_can_cross_terrain(this->target_ptr, g_ptr->feat, r_ptr, 0)) {
198 if (floor_ptr->grid_array[this->target_ptr->y][this->target_ptr->x].is_room()) {
202 if (r_ptr->ability_flags.none()) {
206 if (room >= (8 * (this->target_ptr->chp + this->target_ptr->csp)) / (this->target_ptr->mhp + this->target_ptr->msp)) {
210 if (find_hiding(this->target_ptr, this->m_idx, y, x)) {
215 void MonsterSweepGrid::search_pet_runnable_grid(POSITION *y, POSITION *x, bool no_flow)
217 auto *floor_ptr = this->target_ptr->current_floor_ptr;
218 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
219 if (is_pet(m_ptr) && this->will_run) {
225 if (this->done || !this->will_run) {
231 if (find_safety(this->target_ptr, this->m_idx, y, x) && !no_flow && this->sweep_runnable_away_grid(y, x)) {
244 * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
245 * Choose the "best" direction for "flowing"
246 * @param yp 移動先のマスのY座標を返す参照ポインタ
247 * @param xp 移動先のマスのX座標を返す参照ポインタ
248 * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
250 void MonsterSweepGrid::sweep_movable_grid(POSITION *yp, POSITION *xp, bool no_flow)
252 auto *floor_ptr = this->target_ptr->current_floor_ptr;
253 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
254 auto *r_ptr = &r_info[m_ptr->r_idx];
255 if (!this->check_movable_grid(yp, xp, no_flow)) {
261 auto *g_ptr = &floor_ptr->grid_array[y1][x1];
262 if (player_has_los_bold(this->target_ptr, y1, x1) && projectable(this->target_ptr, this->target_ptr->y, this->target_ptr->x, y1, x1)) {
263 if ((distance(y1, x1, this->target_ptr->y, this->target_ptr->x) == 1) || (r_ptr->freq_spell > 0) || (g_ptr->get_cost(r_ptr) > 5)) {
268 auto use_scent = false;
269 if (g_ptr->get_cost(r_ptr)) {
271 } else if (g_ptr->when) {
272 if (floor_ptr->grid_array[this->target_ptr->y][this->target_ptr->x].when - g_ptr->when > 127) {
282 this->determine_when_cost(yp, xp, y1, x1, use_scent);
285 bool MonsterSweepGrid::check_movable_grid(POSITION *yp, POSITION *xp, const bool no_flow)
287 auto *r_ptr = &r_info[this->target_ptr->current_floor_ptr->m_list[this->m_idx].r_idx];
288 if ((r_ptr->ability_flags.has_any_of(RF_ABILITY_ATTACK_MASK)) && (sweep_ranged_attack_grid(yp, xp))) {
296 if (any_bits(r_ptr->flags2, RF2_PASS_WALL) && ((this->m_idx != this->target_ptr->riding) || has_pass_wall(this->target_ptr))) {
300 if (any_bits(r_ptr->flags2, RF2_KILL_WALL) && (this->m_idx != this->target_ptr->riding)) {
308 * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
309 * Search spell castable grid
310 * @param yp 適したマスのY座標を返す参照ポインタ
311 * @param xp 適したマスのX座標を返す参照ポインタ
312 * @return 有効なマスがあった場合TRUEを返す
314 bool MonsterSweepGrid::sweep_ranged_attack_grid(POSITION *yp, POSITION *xp)
316 auto *floor_ptr = this->target_ptr->current_floor_ptr;
317 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
318 auto *r_ptr = &r_info[m_ptr->r_idx];
321 if (projectable(this->target_ptr, y1, x1, this->target_ptr->y, this->target_ptr->x)) {
325 auto now_cost = (int)floor_ptr->grid_array[y1][x1].get_cost(r_ptr);
330 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR)) {
331 this->can_open_door = true;
334 for (auto i = 7; i >= 0; i--) {
335 auto y = y1 + ddy_ddd[i];
336 auto x = x1 + ddx_ddd[i];
337 if (!in_bounds2(floor_ptr, y, x)) {
341 if (player_bold(this->target_ptr, y, x)) {
345 auto *g_ptr = &floor_ptr->grid_array[y][x];
346 this->cost = (int)g_ptr->get_cost(r_ptr);
347 if (!this->is_best_cost(y, x, now_cost)) {
351 this->best = this->cost;
352 *yp = y1 + ddy_ddd[i];
353 *xp = x1 + ddx_ddd[i];
356 return this->best != 999;
359 bool MonsterSweepGrid::is_best_cost(const POSITION y, const POSITION x, const int now_cost)
361 auto *floor_ptr = this->target_ptr->current_floor_ptr;
362 auto *r_ptr = &r_info[floor_ptr->m_list[this->m_idx].r_idx];
363 auto is_riding = this->m_idx == this->target_ptr->riding;
364 if ((none_bits(r_ptr->flags2, RF2_PASS_WALL) || (is_riding && !has_pass_wall(this->target_ptr)))
365 && (none_bits(r_ptr->flags2, RF2_KILL_WALL) || is_riding)) {
366 if (this->cost == 0) {
370 auto *g_ptr = &floor_ptr->grid_array[y][x];
371 if (!this->can_open_door && is_closed_door(this->target_ptr, g_ptr->feat)) {
376 if (this->cost == 0) {
380 if (now_cost < this->cost) {
384 if (!projectable(this->target_ptr, y, x, this->target_ptr->y, this->target_ptr->x)) {
388 if (this->best < this->cost) {
396 * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
397 * Provide a location to flee to, but give the player a wide berth.
398 * @param yp 移動先のマスのY座標を返す参照ポインタ
399 * @param xp 移動先のマスのX座標を返す参照ポインタ
400 * @return 有効なマスがあった場合TRUEを返す
402 bool MonsterSweepGrid::sweep_runnable_away_grid(POSITION *yp, POSITION *xp)
406 auto *floor_ptr = this->target_ptr->current_floor_ptr;
407 auto *m_ptr = &floor_ptr->m_list[this->m_idx];
408 auto *r_ptr = &r_info[m_ptr->r_idx];
414 for (auto i = 7; i >= 0; i--) {
415 auto y = fy + ddy_ddd[i];
416 auto x = fx + ddx_ddd[i];
417 if (!in_bounds2(floor_ptr, y, x)) {
421 auto dis = distance(y, x, y1, x1);
422 auto s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].get_distance(r_ptr) + 1);
445 void MonsterSweepGrid::determine_when_cost(POSITION *yp, POSITION *xp, POSITION y1, POSITION x1, const bool use_scent)
447 auto *floor_ptr = this->target_ptr->current_floor_ptr;
448 for (auto i = 7; i >= 0; i--) {
449 auto y = y1 + ddy_ddd[i];
450 auto x = x1 + ddx_ddd[i];
451 if (!in_bounds2(floor_ptr, y, x)) {
455 auto *g_ptr = &floor_ptr->grid_array[y][x];
457 int when = g_ptr->when;
458 if (this->best > when) {
464 auto *r_ptr = &r_info[floor_ptr->m_list[this->m_idx].r_idx];
465 this->cost = any_bits(r_ptr->flags2, RF2_BASH_DOOR | RF2_OPEN_DOOR) ? g_ptr->get_distance(r_ptr) : g_ptr->get_cost(r_ptr);
466 if ((this->cost == 0) || (this->best < this->cost)) {
470 this->best = this->cost;
473 *yp = this->target_ptr->y + 16 * ddy_ddd[i];
474 *xp = this->target_ptr->x + 16 * ddx_ddd[i];