OSDN Git Service

[Refactor] #921 Separated determine_when_cost() from sweep_movable_grid()
authorHourier <grapefox.whitelucifer.0408@gmail.com>
Mon, 2 Aug 2021 13:54:08 +0000 (22:54 +0900)
committerHourier <grapefox.whitelucifer.0408@gmail.com>
Sun, 8 Aug 2021 06:00:09 +0000 (15:00 +0900)
src/monster-floor/monster-sweep-grid.cpp
src/monster-floor/monster-sweep-grid.h

index 07338c4..89b6729 100644 (file)
@@ -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];
+    }
+}
index 506cee4..d5d7bb1 100644 (file)
@@ -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);
 };