From 659f2e6ca8efb65ea414770e257dc76926720cae Mon Sep 17 00:00:00 2001 From: Hourier Date: Mon, 2 Aug 2021 22:54:08 +0900 Subject: [PATCH] [Refactor] #921 Separated determine_when_cost() from sweep_movable_grid() --- src/monster-floor/monster-sweep-grid.cpp | 65 ++++++++++++++++++-------------- src/monster-floor/monster-sweep-grid.h | 1 + 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/monster-floor/monster-sweep-grid.cpp b/src/monster-floor/monster-sweep-grid.cpp index 07338c416..89b67296b 100644 --- a/src/monster-floor/monster-sweep-grid.cpp +++ b/src/monster-floor/monster-sweep-grid.cpp @@ -35,7 +35,7 @@ * @param mm 移動方向を返す方向IDの参照ポインタ */ MonsterSweepGrid::MonsterSweepGrid(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm) - : target_ptr(this->target_ptr) + : target_ptr(target_ptr) , m_idx(m_idx) , mm(mm) { @@ -58,7 +58,7 @@ bool MonsterSweepGrid::get_movable_grid() auto x2 = this->target_ptr->x; this->will_run = this->mon_will_run(); auto no_flow = m_ptr->mflag2.has(MFLAG2::NOFLOW) && grid_cost(&floor_ptr->grid_array[m_ptr->fy][m_ptr->fx], r_ptr) > 2; - auto can_pass_wall = any_bits(r_ptr->flags2, RF2_PASS_WALL) && ((this->m_idx != this->target_ptr->riding) || has_pass_wall(this->target_ptr)); + 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)); if (!this->will_run && m_ptr->target_y) { int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx; if ((t_m_idx > 0) && are_enemies(this->target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) @@ -280,33 +280,7 @@ void MonsterSweepGrid::sweep_movable_grid(POSITION *yp, POSITION *xp, bool no_fl return; } - for (auto i = 7; i >= 0; i--) { - auto y = y1 + ddy_ddd[i]; - auto x = x1 + ddx_ddd[i]; - if (!in_bounds2(floor_ptr, y, x)) { - continue; - } - - g_ptr = &floor_ptr->grid_array[y][x]; - if (use_scent) { - int when = g_ptr->when; - if (best > when) { - continue; - } - - best = when; - } else { - auto cost = any_bits(r_ptr->flags2, RF2_BASH_DOOR | RF2_OPEN_DOOR) ? grid_dist(g_ptr, r_ptr) : grid_cost(g_ptr, r_ptr); - if ((cost == 0) || (best < cost)) { - continue; - } - - best = cost; - } - - *yp = this->target_ptr->y + 16 * ddy_ddd[i]; - *xp = this->target_ptr->x + 16 * ddx_ddd[i]; - } + this->determine_when_cost(yp, xp, y1, x1, use_scent, &best); } bool MonsterSweepGrid::check_movable_grid(POSITION *yp, POSITION *xp, const bool no_flow) @@ -456,3 +430,36 @@ bool MonsterSweepGrid::sweep_runnable_away_grid(POSITION *yp, POSITION *xp) *xp = fx - gx; return true; } + +void MonsterSweepGrid::determine_when_cost(POSITION *yp, POSITION *xp, POSITION y1, POSITION x1, const bool use_scent, int *best) +{ + auto *floor_ptr = this->target_ptr->current_floor_ptr; + for (auto i = 7; i >= 0; i--) { + auto y = y1 + ddy_ddd[i]; + auto x = x1 + ddx_ddd[i]; + if (!in_bounds2(floor_ptr, y, x)) { + continue; + } + + auto *g_ptr = &floor_ptr->grid_array[y][x]; + if (use_scent) { + int when = g_ptr->when; + if (*best > when) { + continue; + } + + *best = when; + } else { + auto *r_ptr = &r_info[floor_ptr->m_list[this->m_idx].r_idx]; + auto cost = any_bits(r_ptr->flags2, RF2_BASH_DOOR | RF2_OPEN_DOOR) ? grid_dist(g_ptr, r_ptr) : grid_cost(g_ptr, r_ptr); + if ((cost == 0) || (*best < cost)) { + continue; + } + + *best = cost; + } + + *yp = this->target_ptr->y + 16 * ddy_ddd[i]; + *xp = this->target_ptr->x + 16 * ddx_ddd[i]; + } +} diff --git a/src/monster-floor/monster-sweep-grid.h b/src/monster-floor/monster-sweep-grid.h index 506cee449..d5d7bb177 100644 --- a/src/monster-floor/monster-sweep-grid.h +++ b/src/monster-floor/monster-sweep-grid.h @@ -26,4 +26,5 @@ private: void check_hiding_grid(POSITION *y, POSITION *x, POSITION *y2, POSITION *x2); void search_room_to_run(POSITION *y, POSITION *x); void search_pet_runnable_grid(POSITION *y, POSITION *x, bool no_flow); + void determine_when_cost(POSITION *yp, POSITION *xp, POSITION y1, POSITION x1, const bool use_scent, int *best); }; -- 2.11.0