OSDN Git Service

[Refactor] #4158 ang_sort_comp_importance() をTargetSorter のオブジェクトメソッドに繰り込んだ
authorHourier <66951241+Hourier@users.noreply.github.com>
Sun, 26 May 2024 02:26:07 +0000 (11:26 +0900)
committerHourier <66951241+Hourier@users.noreply.github.com>
Sun, 26 May 2024 13:34:54 +0000 (22:34 +0900)
src/target/target-sorter.cpp
src/target/target-sorter.h

index d4578df..2751167 100644 (file)
@@ -18,7 +18,7 @@ namespace {
  * We use "u" and "v" to point to arrays of "x" and "y" positions,
  * and sort the arrays by double-distance to the player.
  */
-bool ang_sort_comp_distance(const Pos2D &p_pos, std::vector<int> &ys, std::vector<int> &xs, int a, int b)
+bool ang_sort_comp_distance(const Pos2D &p_pos, const std::vector<int> &ys, const std::vector<int> &xs, int a, int b)
 {
     /* Absolute distance components */
     auto xa = xs[a];
@@ -44,103 +44,6 @@ bool ang_sort_comp_distance(const Pos2D &p_pos, std::vector<int> &ys, std::vecto
     return distance_a <= distance_b;
 }
 
-/*
- * Sorting hook -- comp function -- by importance level of grids
- *
- * We use "u" and "v" to point to arrays of "x" and "y" positions,
- * and sort the arrays by level of monster
- */
-bool ang_sort_comp_importance(const FloorType &floor, const Pos2D &p_pos, std::vector<int> &ys, std::vector<int> &xs, int a, int b)
-{
-    const auto &grid1 = floor.get_grid({ ys[a], xs[a] });
-    const auto &grid2 = floor.get_grid({ ys[b], xs[b] });
-    const auto &monster_a = floor.m_list[grid1.m_idx];
-    const auto &monster_b = floor.m_list[grid2.m_idx];
-    if (p_pos == Pos2D(ys[a], xs[a])) {
-        return true;
-    }
-
-    if (p_pos == Pos2D(ys[b], xs[b])) {
-        return false;
-    }
-
-    const auto can_see_grid1 = grid1.has_monster() && monster_a.ml;
-    const auto can_see_grid2 = grid2.has_monster() && monster_b.ml;
-    if (can_see_grid1 && !can_see_grid2) {
-        return true;
-    }
-
-    if (!can_see_grid1 && can_see_grid2) {
-        return false;
-    }
-
-    if (can_see_grid1 && can_see_grid2) {
-        const auto &appearent_monrace1 = monster_a.get_appearance_monrace();
-        const auto &appearent_monrace2 = monster_b.get_appearance_monrace();
-        if (appearent_monrace1.kind_flags.has(MonsterKindType::UNIQUE) && appearent_monrace2.kind_flags.has_not(MonsterKindType::UNIQUE)) {
-            return true;
-        }
-
-        if (appearent_monrace1.kind_flags.has_not(MonsterKindType::UNIQUE) && appearent_monrace2.kind_flags.has(MonsterKindType::UNIQUE)) {
-            return false;
-        }
-
-        if (monster_a.mflag2.has(MonsterConstantFlagType::KAGE) && monster_b.mflag2.has_not(MonsterConstantFlagType::KAGE)) {
-            return true;
-        }
-
-        if (monster_a.mflag2.has_not(MonsterConstantFlagType::KAGE) && monster_b.mflag2.has(MonsterConstantFlagType::KAGE)) {
-            return false;
-        }
-
-        if (!appearent_monrace1.r_tkills && appearent_monrace2.r_tkills) {
-            return true;
-        }
-
-        if (appearent_monrace1.r_tkills && !appearent_monrace2.r_tkills) {
-            return false;
-        }
-
-        if (appearent_monrace1.r_tkills && appearent_monrace2.r_tkills) {
-            if (appearent_monrace1.level > appearent_monrace2.level) {
-                return true;
-            }
-
-            if (appearent_monrace1.level < appearent_monrace2.level) {
-                return false;
-            }
-        }
-
-        if (monster_a.ap_r_idx > monster_b.ap_r_idx) {
-            return true;
-        }
-
-        if (monster_a.ap_r_idx < monster_b.ap_r_idx) {
-            return false;
-        }
-    }
-
-    if (!grid1.o_idx_list.empty() && grid2.o_idx_list.empty()) {
-        return true;
-    }
-
-    if (grid1.o_idx_list.empty() && !grid2.o_idx_list.empty()) {
-        return false;
-    }
-
-    const auto &terrain_a = grid1.get_terrain();
-    const auto &terrain_b = grid2.get_terrain();
-    if (terrain_a.priority > terrain_b.priority) {
-        return true;
-    }
-
-    if (terrain_a.priority < terrain_b.priority) {
-        return false;
-    }
-
-    return ang_sort_comp_distance(p_pos, ys, xs, a, b);
-}
-
 }
 
 TargetSorter::TargetSorter(const Pos2D &p_pos, const std::vector<int> &ys, const std::vector<int> &xs, SortKind kind)
@@ -194,7 +97,7 @@ void TargetSorter::exe_sort(const FloorType &floor, int a, int b)
                 is_less_i2 = ang_sort_comp_distance(this->p_pos, this->ys, this->xs, q, z);
                 break;
             case SortKind::IMPORTANCE:
-                is_less_i2 = ang_sort_comp_importance(floor, this->p_pos, this->ys, this->xs, q, z);
+                is_less_i2 = this->compare_importance(floor, q, z);
                 break;
             default:
                 THROW_EXCEPTION(std::logic_error, "Invalid Sort Kind was specified!");
@@ -213,7 +116,7 @@ void TargetSorter::exe_sort(const FloorType &floor, int a, int b)
                 is_less_i1 = ang_sort_comp_distance(this->p_pos, this->ys, this->xs, z, p);
                 break;
             case SortKind::IMPORTANCE:
-                is_less_i1 = ang_sort_comp_importance(floor, this->p_pos, this->ys, this->xs, z, p);
+                is_less_i1 = this->compare_importance(floor, z, p);
                 break;
             default:
                 THROW_EXCEPTION(std::logic_error, "Invalid Sort Kind was specified!");
@@ -239,3 +142,100 @@ void TargetSorter::exe_sort(const FloorType &floor, int a, int b)
     /* Recurse right side */
     this->exe_sort(floor, q + 1, b);
 }
+
+/*
+ * @brief 座標の重要度でソートする
+ * @param a ソート対象の座標番号1. 座標そのものはPos2D(ys[a], xs[a])
+ * @param b ソート対象の座標番号2. 座標そのものはPos2D(ys[b], xs[b])
+ * @return aの座標がbの座標よりプレイヤー座標に近いか同一ならtrue、遠いならfalse
+ */
+bool TargetSorter::compare_importance(const FloorType &floor, int a, int b) const
+{
+    const auto &grid1 = floor.get_grid({ this->ys[a], this->xs[a] });
+    const auto &grid2 = floor.get_grid({ this->ys[b], this->xs[b] });
+    const auto &monster_a = floor.m_list[grid1.m_idx];
+    const auto &monster_b = floor.m_list[grid2.m_idx];
+    if (this->p_pos == Pos2D(this->ys[a], this->xs[a])) {
+        return true;
+    }
+
+    if (this->p_pos == Pos2D(this->ys[b], this->xs[b])) {
+        return false;
+    }
+
+    const auto can_see_grid1 = grid1.has_monster() && monster_a.ml;
+    const auto can_see_grid2 = grid2.has_monster() && monster_b.ml;
+    if (can_see_grid1 && !can_see_grid2) {
+        return true;
+    }
+
+    if (!can_see_grid1 && can_see_grid2) {
+        return false;
+    }
+
+    if (can_see_grid1 && can_see_grid2) {
+        const auto &appearent_monrace1 = monster_a.get_appearance_monrace();
+        const auto &appearent_monrace2 = monster_b.get_appearance_monrace();
+        if (appearent_monrace1.kind_flags.has(MonsterKindType::UNIQUE) && appearent_monrace2.kind_flags.has_not(MonsterKindType::UNIQUE)) {
+            return true;
+        }
+
+        if (appearent_monrace1.kind_flags.has_not(MonsterKindType::UNIQUE) && appearent_monrace2.kind_flags.has(MonsterKindType::UNIQUE)) {
+            return false;
+        }
+
+        if (monster_a.mflag2.has(MonsterConstantFlagType::KAGE) && monster_b.mflag2.has_not(MonsterConstantFlagType::KAGE)) {
+            return true;
+        }
+
+        if (monster_a.mflag2.has_not(MonsterConstantFlagType::KAGE) && monster_b.mflag2.has(MonsterConstantFlagType::KAGE)) {
+            return false;
+        }
+
+        if (!appearent_monrace1.r_tkills && appearent_monrace2.r_tkills) {
+            return true;
+        }
+
+        if (appearent_monrace1.r_tkills && !appearent_monrace2.r_tkills) {
+            return false;
+        }
+
+        if (appearent_monrace1.r_tkills && appearent_monrace2.r_tkills) {
+            if (appearent_monrace1.level > appearent_monrace2.level) {
+                return true;
+            }
+
+            if (appearent_monrace1.level < appearent_monrace2.level) {
+                return false;
+            }
+        }
+
+        if (monster_a.ap_r_idx > monster_b.ap_r_idx) {
+            return true;
+        }
+
+        if (monster_a.ap_r_idx < monster_b.ap_r_idx) {
+            return false;
+        }
+    }
+
+    if (!grid1.o_idx_list.empty() && grid2.o_idx_list.empty()) {
+        return true;
+    }
+
+    if (grid1.o_idx_list.empty() && !grid2.o_idx_list.empty()) {
+        return false;
+    }
+
+    const auto &terrain_a = grid1.get_terrain();
+    const auto &terrain_b = grid2.get_terrain();
+    if (terrain_a.priority > terrain_b.priority) {
+        return true;
+    }
+
+    if (terrain_a.priority < terrain_b.priority) {
+        return false;
+    }
+
+    return ang_sort_comp_distance(this->p_pos, this->ys, this->xs, a, b);
+}
index 14bcb7b..51d15e6 100644 (file)
@@ -23,4 +23,5 @@ private:
     SortKind kind;
 
     void exe_sort(const FloorType &floor, int a, int b);
+    bool compare_importance(const FloorType &floor, int a, int b) const;
 };