OSDN Git Service

[Refactor] #921 Replaced positions of methods in MonsterSweepGrid
authorHourier <grapefox.whitelucifer.0408@gmail.com>
Mon, 2 Aug 2021 11:36:46 +0000 (20:36 +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 c19b0b6..fbc89dc 100644 (file)
@@ -35,20 +35,128 @@ MonsterSweepGrid::MonsterSweepGrid(player_type *target_ptr, MONSTER_IDX m_idx, D
 }
 
 /*!
+ * @brief モンスターの移動方向を返す /
+ * Choose "logical" directions for monster movement
+ * @param target_ptr プレーヤーへの参照ポインタ
+ * @param m_idx モンスターの参照ID
+ * @param mm 移動方向を返す方向IDの参照ポインタ
+ * @return 有効方向があった場合TRUEを返す
+ * @todo 分割したいが条件が多すぎて適切な関数名と詳細処理を追いきれない……
+ */
+bool MonsterSweepGrid::get_movable_grid()
+{
+    floor_type *floor_ptr = target_ptr->current_floor_ptr;
+    monster_type *m_ptr = &floor_ptr->m_list[m_idx];
+    monster_race *r_ptr = &r_info[m_ptr->r_idx];
+    POSITION y = 0, x = 0;
+    POSITION y2 = target_ptr->y;
+    POSITION x2 = target_ptr->x;
+    bool done = false;
+    bool will_run = mon_will_run();
+    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;
+    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) {
+        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(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx])
+            && los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x)
+            && projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x)) {
+            y = m_ptr->fy - m_ptr->target_y;
+            x = m_ptr->fx - m_ptr->target_x;
+            done = true;
+        }
+    }
+
+    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))) {
+        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++) {
+                int xx = target_ptr->x + ddx_ddd[i];
+                int yy = target_ptr->y + ddy_ddd[i];
+
+                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)) {
+                    room++;
+                }
+            }
+
+            if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM)
+                room -= 2;
+            if (r_ptr->ability_flags.none())
+                room -= 2;
+
+            if (room < (8 * (target_ptr->chp + target_ptr->csp)) / (target_ptr->mhp + target_ptr->msp)) {
+                if (find_hiding(target_ptr, m_idx, &y, &x))
+                    done = true;
+            }
+        }
+
+        if (!done && grid_dist(&floor_ptr->grid_array[m_ptr->fy][m_ptr->fx], 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];
+                if ((m_ptr->fy == y2) && (m_ptr->fx == x2)) {
+                    y2 = target_ptr->y;
+                    x2 = target_ptr->x;
+                    break;
+                }
+
+                if (!in_bounds2(floor_ptr, y2, x2))
+                    continue;
+                if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0))
+                    continue;
+
+                break;
+            }
+
+            y = m_ptr->fy - y2;
+            x = m_ptr->fx - x2;
+            done = true;
+        }
+    }
+
+    if (!done) {
+        sweep_movable_grid(&y2, &x2, no_flow);
+        y = m_ptr->fy - y2;
+        x = m_ptr->fx - x2;
+    }
+
+    if (is_pet(m_ptr) && will_run) {
+        y = (-y), x = (-x);
+    } else {
+        if (!done && will_run) {
+            int tmp_x = (-x);
+            int tmp_y = (-y);
+            if (find_safety(target_ptr, m_idx, &y, &x) && !no_flow) {
+                if (sweep_runnable_away_grid(&y, &x))
+                    done = true;
+            }
+
+            if (!done) {
+                y = tmp_y;
+                x = tmp_x;
+            }
+        }
+    }
+
+    if (!x && !y)
+        return false;
+
+    store_moves_val(mm, y, x);
+    return true;
+}
+
+/*!
  * @brief モンスターがプレイヤーから逃走するかどうかを返す /
  * Returns whether a given monster will try to run from the player.
  * @param m_idx 逃走するモンスターの参照ID
  * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
- * @details
- * Monsters will attempt to avoid very powerful players.  See below.\n
- *\n
- * Because this function is called so often, little details are important\n
- * for efficiency.  Like not using "mod" or "div" when possible.  And\n
- * attempting to check the conditions in an optimal order.  Note that\n
- * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
- *\n
- * Note that this function is responsible for about one to five percent\n
- * of the processor use in normal conditions...\n
  */
 bool MonsterSweepGrid::mon_will_run()
 {
@@ -86,102 +194,12 @@ bool MonsterSweepGrid::mon_will_run()
 }
 
 /*!
- * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
- * Search spell castable grid
- * @param target_ptr プレーヤーへの参照ポインタ
- * @param m_idx モンスターの参照ID
- * @param yp 適したマスのY座標を返す参照ポインタ
- * @param xp 適したマスのX座標を返す参照ポインタ
- * @return 有効なマスがあった場合TRUEを返す
- */
-bool MonsterSweepGrid::sweep_ranged_attack_grid(POSITION *yp, POSITION *xp)
-{
-    floor_type *floor_ptr = target_ptr->current_floor_ptr;
-    monster_type *m_ptr = &floor_ptr->m_list[m_idx];
-    monster_race *r_ptr = &r_info[m_ptr->r_idx];
-
-    POSITION y1 = m_ptr->fy;
-    POSITION x1 = m_ptr->fx;
-
-    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);
-    if (now_cost == 0)
-        now_cost = 999;
-
-    bool can_open_door = false;
-    if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR)) {
-        can_open_door = true;
-    }
-
-    int best = 999;
-    for (int i = 7; i >= 0; i--) {
-        POSITION y = y1 + ddy_ddd[i];
-        POSITION x = x1 + ddx_ddd[i];
-        if (!in_bounds2(floor_ptr, y, x))
-            continue;
-        if (player_bold(target_ptr, y, x))
-            return false;
-
-        grid_type *g_ptr;
-        g_ptr = &floor_ptr->grid_array[y][x];
-        int cost = grid_cost(g_ptr, 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)
-                continue;
-            if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat))
-                continue;
-        }
-
-        if (cost == 0)
-            cost = 998;
-
-        if (now_cost < cost)
-            continue;
-        if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x))
-            continue;
-        if (best < cost)
-            continue;
-
-        best = cost;
-        *yp = y1 + ddy_ddd[i];
-        *xp = x1 + ddx_ddd[i];
-    }
-
-    if (best == 999)
-        return false;
-
-    return true;
-}
-
-/*!
  * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
  * Choose the "best" direction for "flowing"
  * @param m_idx モンスターの参照ID
  * @param yp 移動先のマスのY座標を返す参照ポインタ
  * @param xp 移動先のマスのX座標を返す参照ポインタ
  * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
- * @details
- * Note that ghosts and rock-eaters are never allowed to "flow",\n
- * since they should move directly towards the player.\n
- *\n
- * Prefer "non-diagonal" directions, but twiddle them a little\n
- * to angle slightly towards the player's actual location.\n
- *\n
- * Allow very perceptive monsters to track old "spoor" left by\n
- * previous locations occupied by the player.  This will tend\n
- * to have monsters end up either near the player or on a grid\n
- * recently occupied by the player (and left via "teleport").\n
- *\n
- * Note that if "smell" is turned on, all monsters get vicious.\n
- *\n
- * Also note that teleporting away from a location will cause\n
- * the monsters who were chasing you to converge on that location\n
- * as long as you are still near enough to "annoy" them without\n
- * being close enough to chase directly.  I have no idea what will\n
- * happen if you combine "smell" with low "aaf" values.\n
  */
 void MonsterSweepGrid::sweep_movable_grid(POSITION *yp, POSITION *xp, bool no_flow)
 {
@@ -262,16 +280,83 @@ void MonsterSweepGrid::sweep_movable_grid(POSITION *yp, POSITION *xp, bool no_fl
 }
 
 /*!
+ * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
+ * Search spell castable grid
+ * @param target_ptr プレーヤーへの参照ポインタ
+ * @param m_idx モンスターの参照ID
+ * @param yp 適したマスのY座標を返す参照ポインタ
+ * @param xp 適したマスのX座標を返す参照ポインタ
+ * @return 有効なマスがあった場合TRUEを返す
+ */
+bool MonsterSweepGrid::sweep_ranged_attack_grid(POSITION *yp, POSITION *xp)
+{
+    floor_type *floor_ptr = target_ptr->current_floor_ptr;
+    monster_type *m_ptr = &floor_ptr->m_list[m_idx];
+    monster_race *r_ptr = &r_info[m_ptr->r_idx];
+
+    POSITION y1 = m_ptr->fy;
+    POSITION x1 = m_ptr->fx;
+
+    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);
+    if (now_cost == 0)
+        now_cost = 999;
+
+    bool can_open_door = false;
+    if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR)) {
+        can_open_door = true;
+    }
+
+    int best = 999;
+    for (int i = 7; i >= 0; i--) {
+        POSITION y = y1 + ddy_ddd[i];
+        POSITION x = x1 + ddx_ddd[i];
+        if (!in_bounds2(floor_ptr, y, x))
+            continue;
+        if (player_bold(target_ptr, y, x))
+            return false;
+
+        grid_type *g_ptr;
+        g_ptr = &floor_ptr->grid_array[y][x];
+        int cost = grid_cost(g_ptr, 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)
+                continue;
+            if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat))
+                continue;
+        }
+
+        if (cost == 0)
+            cost = 998;
+
+        if (now_cost < cost)
+            continue;
+        if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x))
+            continue;
+        if (best < cost)
+            continue;
+
+        best = cost;
+        *yp = y1 + ddy_ddd[i];
+        *xp = x1 + ddx_ddd[i];
+    }
+
+    if (best == 999)
+        return false;
+
+    return true;
+}
+
+/*!
  * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
  * Provide a location to flee to, but give the player a wide berth.
  * @param m_idx モンスターの参照ID
  * @param yp 移動先のマスのY座標を返す参照ポインタ
  * @param xp 移動先のマスのX座標を返す参照ポインタ
  * @return 有効なマスがあった場合TRUEを返す
- * @details
- * A monster may wish to flee to a location that is behind the player,\n
- * but instead of heading directly for it, the monster should "swerve"\n
- * around the player so that he has a smaller chance of getting hit.\n
  */
 bool MonsterSweepGrid::sweep_runnable_away_grid(POSITION *yp, POSITION *xp)
 {
@@ -314,121 +399,3 @@ bool MonsterSweepGrid::sweep_runnable_away_grid(POSITION *yp, POSITION *xp)
 
     return true;
 }
-
-/*!
- * @brief モンスターの移動方向を返す /
- * Choose "logical" directions for monster movement
- * @param target_ptr プレーヤーへの参照ポインタ
- * @param m_idx モンスターの参照ID
- * @param mm 移動方向を返す方向IDの参照ポインタ
- * @return 有効方向があった場合TRUEを返す
- * @todo 分割したいが条件が多すぎて適切な関数名と詳細処理を追いきれない……
- */
-bool MonsterSweepGrid::get_movable_grid()
-{
-    floor_type *floor_ptr = target_ptr->current_floor_ptr;
-    monster_type *m_ptr = &floor_ptr->m_list[m_idx];
-    monster_race *r_ptr = &r_info[m_ptr->r_idx];
-    POSITION y = 0, x = 0;
-    POSITION y2 = target_ptr->y;
-    POSITION x2 = target_ptr->x;
-    bool done = false;
-    bool will_run = mon_will_run();
-    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;
-    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) {
-        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(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx])
-            && los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x)
-            && projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x)) {
-            y = m_ptr->fy - m_ptr->target_y;
-            x = m_ptr->fx - m_ptr->target_x;
-            done = true;
-        }
-    }
-
-    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))) {
-        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++) {
-                int xx = target_ptr->x + ddx_ddd[i];
-                int yy = target_ptr->y + ddy_ddd[i];
-
-                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)) {
-                    room++;
-                }
-            }
-
-            if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM)
-                room -= 2;
-            if (r_ptr->ability_flags.none())
-                room -= 2;
-
-            if (room < (8 * (target_ptr->chp + target_ptr->csp)) / (target_ptr->mhp + target_ptr->msp)) {
-                if (find_hiding(target_ptr, m_idx, &y, &x))
-                    done = true;
-            }
-        }
-
-        if (!done && grid_dist(&floor_ptr->grid_array[m_ptr->fy][m_ptr->fx], 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];
-                if ((m_ptr->fy == y2) && (m_ptr->fx == x2)) {
-                    y2 = target_ptr->y;
-                    x2 = target_ptr->x;
-                    break;
-                }
-
-                if (!in_bounds2(floor_ptr, y2, x2))
-                    continue;
-                if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0))
-                    continue;
-
-                break;
-            }
-
-            y = m_ptr->fy - y2;
-            x = m_ptr->fx - x2;
-            done = true;
-        }
-    }
-
-    if (!done) {
-        sweep_movable_grid(&y2, &x2, no_flow);
-        y = m_ptr->fy - y2;
-        x = m_ptr->fx - x2;
-    }
-
-    if (is_pet(m_ptr) && will_run) {
-        y = (-y), x = (-x);
-    } else {
-        if (!done && will_run) {
-            int tmp_x = (-x);
-            int tmp_y = (-y);
-            if (find_safety(target_ptr, m_idx, &y, &x) && !no_flow) {
-                if (sweep_runnable_away_grid(&y, &x))
-                    done = true;
-            }
-
-            if (!done) {
-                y = tmp_y;
-                x = tmp_x;
-            }
-        }
-    }
-
-    if (!x && !y)
-        return false;
-
-    store_moves_val(mm, y, x);
-    return true;
-}
index 2b93c19..ffee58d 100644 (file)
@@ -15,8 +15,8 @@ public:
     bool get_movable_grid();
 
 private:
-    bool sweep_runnable_away_grid(POSITION *yp, POSITION *xp);
+    bool mon_will_run();
     void sweep_movable_grid(POSITION *yp, POSITION *xp, bool no_flow);
     bool sweep_ranged_attack_grid(POSITION *yp, POSITION *xp);
-    bool mon_will_run();
+    bool sweep_runnable_away_grid(POSITION *yp, POSITION *xp);
 };