From ff6954943fd3c849434c67c66651f4a7ce330b56 Mon Sep 17 00:00:00 2001 From: Hourier Date: Sat, 7 Aug 2021 13:24:21 +0900 Subject: [PATCH] [Refactor] #919 Moved grid_cost() and grid_dist() from grid.cpp/h to grid-type-definition.cpp/h and renamed get_cost() and get_distance() each --- src/grid/grid.cpp | 17 ----------------- src/grid/grid.h | 2 -- src/monster-floor/monster-safety-hiding.cpp | 4 ++-- src/monster-floor/monster-sweep-grid.cpp | 26 +++++++++++++------------- src/system/grid-type-definition.cpp | 17 +++++++++++++++++ src/system/grid-type-definition.h | 7 +++++++ 6 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/grid/grid.cpp b/src/grid/grid.cpp index 1e579277a..c75960feb 100644 --- a/src/grid/grid.cpp +++ b/src/grid/grid.cpp @@ -858,23 +858,6 @@ void update_flow(player_type *subject_ptr) } } -static flow_type get_grid_flow_type(monster_race *r_ptr) -{ - if (any_bits(r_ptr->flags7, RF7_CAN_FLY)) - return FLOW_CAN_FLY; - return FLOW_NORMAL; -} - -byte grid_cost(grid_type *g_ptr, monster_race *r_ptr) -{ - return g_ptr->costs[get_grid_flow_type(r_ptr)]; -} - -byte grid_dist(grid_type *g_ptr, monster_race *r_ptr) -{ - return g_ptr->dists[get_grid_flow_type(r_ptr)]; -} - /* * Take a feature, determine what that feature becomes * through applying the given action. diff --git a/src/grid/grid.h b/src/grid/grid.h index b14353c5f..c36d20e0f 100644 --- a/src/grid/grid.h +++ b/src/grid/grid.h @@ -54,8 +54,6 @@ void print_rel(player_type *subject_ptr, SYMBOL_CODE c, TERM_COLOR a, POSITION y void note_spot(player_type *player_ptr, POSITION y, POSITION x); void lite_spot(player_type *player_ptr, POSITION y, POSITION x); void update_flow(player_type *subject_ptr); -byte grid_cost(grid_type *g_ptr, monster_race *r_ptr); -byte grid_dist(grid_type *g_ptr, monster_race *r_ptr); FEAT_IDX feat_state(floor_type *floor_ptr, FEAT_IDX feat, int action); void cave_alter_feat(player_type *player_ptr, POSITION y, POSITION x, int action); void remove_mirror(player_type *caster_ptr, POSITION y, POSITION x); diff --git a/src/monster-floor/monster-safety-hiding.cpp b/src/monster-floor/monster-safety-hiding.cpp index 8ceb2e74f..dea947f21 100644 --- a/src/monster-floor/monster-safety-hiding.cpp +++ b/src/monster-floor/monster-safety-hiding.cpp @@ -50,10 +50,10 @@ static coordinate_candidate sweep_safe_coordinate(player_type *target_ptr, MONST continue; if (m_ptr->mflag2.has_not(MFLAG2::NOFLOW)) { - byte dist = grid_dist(g_ptr, r_ptr); + byte dist = g_ptr->get_distance(r_ptr); if (dist == 0) continue; - if (dist > grid_dist(&floor_ptr->grid_array[m_ptr->fy][m_ptr->fx], r_ptr) + 2 * d) + if (dist > floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].get_distance(r_ptr) + 2 * d) continue; } diff --git a/src/monster-floor/monster-sweep-grid.cpp b/src/monster-floor/monster-sweep-grid.cpp index 5cadb9097..f975e8ab5 100644 --- a/src/monster-floor/monster-sweep-grid.cpp +++ b/src/monster-floor/monster-sweep-grid.cpp @@ -100,7 +100,7 @@ static bool sweep_ranged_attack_grid(player_type *target_ptr, MONSTER_IDX m_idx, if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return false; - int now_cost = grid_cost(&floor_ptr->grid_array[y1][x1], r_ptr); + int now_cost = floor_ptr->grid_array[y1][x1].get_cost(r_ptr); if (now_cost == 0) now_cost = 999; @@ -120,7 +120,7 @@ static bool sweep_ranged_attack_grid(player_type *target_ptr, MONSTER_IDX m_idx, grid_type *g_ptr; g_ptr = &floor_ptr->grid_array[y][x]; - int cost = grid_cost(g_ptr, r_ptr); + int cost = g_ptr->get_cost(r_ptr); if (!(((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || has_pass_wall(target_ptr))) || ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)))) { if (cost == 0) @@ -204,13 +204,13 @@ static void sweep_movable_grid(player_type *target_ptr, MONSTER_IDX m_idx, POSIT return; if (r_ptr->freq_spell > 0) return; - if (grid_cost(g_ptr, r_ptr) > 5) + if (g_ptr->get_cost(r_ptr) > 5) return; } int best; bool use_scent = false; - if (grid_cost(g_ptr, r_ptr)) { + if (g_ptr->get_cost(r_ptr)) { best = 999; } else if (g_ptr->when) { if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) @@ -239,9 +239,9 @@ static void sweep_movable_grid(player_type *target_ptr, MONSTER_IDX m_idx, POSIT } else { int cost; if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR)) { - cost = grid_dist(g_ptr, r_ptr); + cost = g_ptr->get_distance(r_ptr); } else { - cost = grid_cost(g_ptr, r_ptr); + cost = g_ptr->get_cost(r_ptr); } if ((cost == 0) || (best < cost)) @@ -287,7 +287,7 @@ static bool sweep_runnable_away_grid(floor_type *floor_ptr, MONSTER_IDX m_idx, P continue; POSITION dis = distance(y, x, y1, x1); - POSITION s = 5000 / (dis + 3) - 500 / (grid_dist(&floor_ptr->grid_array[y][x], r_ptr) + 1); + POSITION s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].get_distance(r_ptr) + 1); if (s < 0) s = 0; @@ -327,8 +327,8 @@ bool get_movable_grid(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm) POSITION x2 = target_ptr->x; bool done = false; bool will_run = mon_will_run(target_ptr, m_idx); - grid_type *g_ptr; - bool no_flow = m_ptr->mflag2.has(MFLAG2::NOFLOW) && grid_cost(&floor_ptr->grid_array[m_ptr->fy][m_ptr->fx], r_ptr) > 2; + grid_type *g_ptr = &floor_ptr->grid_array[m_ptr->fy][m_ptr->fx]; + bool no_flow = m_ptr->mflag2.has(MFLAG2::NOFLOW) && g_ptr->get_cost(r_ptr) > 2; bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) != 0) && ((m_idx != target_ptr->riding) || has_pass_wall(target_ptr)); if (!will_run && m_ptr->target_y) { @@ -344,7 +344,7 @@ bool get_movable_grid(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm) if (!done && !will_run && is_hostile(m_ptr) && (r_ptr->flags1 & RF1_FRIENDS) && ((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)) - || (grid_dist(&floor_ptr->grid_array[m_ptr->fy][m_ptr->fx], r_ptr) < MAX_SIGHT / 2))) { + || (g_ptr->get_distance(r_ptr) < MAX_SIGHT / 2))) { if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall && !(r_ptr->flags2 & RF2_KILL_WALL)) { int room = 0; for (int i = 0; i < 8; i++) { @@ -354,8 +354,8 @@ bool get_movable_grid(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm) if (!in_bounds2(floor_ptr, yy, xx)) continue; - g_ptr = &floor_ptr->grid_array[yy][xx]; - if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0)) { + auto *gg_ptr = &floor_ptr->grid_array[yy][xx]; + if (monster_can_cross_terrain(target_ptr, gg_ptr->feat, r_ptr, 0)) { room++; } } @@ -371,7 +371,7 @@ bool get_movable_grid(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm) } } - if (!done && grid_dist(&floor_ptr->grid_array[m_ptr->fy][m_ptr->fx], r_ptr) < 3) { + if (!done && g_ptr->get_distance(r_ptr) < 3) { for (int i = 0; i < 8; i++) { y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7]; x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7]; diff --git a/src/system/grid-type-definition.cpp b/src/system/grid-type-definition.cpp index ab2efb785..abd5fd56f 100644 --- a/src/system/grid-type-definition.cpp +++ b/src/system/grid-type-definition.cpp @@ -1,5 +1,7 @@ #include "system/grid-type-definition.h" #include "grid/feature.h" // @todo 相互依存している. 後で何とかする. +#include "monster-race/race-flags7.h" +#include "system/monster-race-definition.h" #include "util/bit-flags-calculator.h" /*! @@ -88,3 +90,18 @@ bool grid_type::is_rune_explosion() { return this->is_object() && has_flag(f_info[this->mimic].flags, FF_RUNE_EXPLOSION); } + +byte grid_type::get_cost(monster_race *r_ptr) +{ + return this->costs[get_grid_flow_type(r_ptr)]; +} + +byte grid_type::get_distance(monster_race *r_ptr) +{ + return this->dists[get_grid_flow_type(r_ptr)]; +} + +flow_type grid_type::get_grid_flow_type(monster_race *r_ptr) +{ + return any_bits(r_ptr->flags7, RF7_CAN_FLY) ? FLOW_CAN_FLY : FLOW_NORMAL; +} diff --git a/src/system/grid-type-definition.h b/src/system/grid-type-definition.h index 7af63ced5..402dec797 100644 --- a/src/system/grid-type-definition.h +++ b/src/system/grid-type-definition.h @@ -44,7 +44,9 @@ enum flow_type { FLOW_MAX = 2, }; +struct monster_race; struct grid_type { +public: BIT_FLAGS info{}; /* Hack -- grid flags */ FEAT_IDX feat{}; /* Hack -- feature type */ @@ -79,4 +81,9 @@ struct grid_type { bool is_mirror(); bool is_rune_protection(); bool is_rune_explosion(); + byte get_cost(monster_race *r_ptr); + byte get_distance(monster_race *r_ptr); + +private: + flow_type get_grid_flow_type(monster_race *r_ptr); }; -- 2.11.0