refactor: polish code

This commit is contained in:
Weiyou Wang
2025-07-23 18:29:32 +10:00
committed by uye
parent f443ce6580
commit 37e1477948
6 changed files with 38 additions and 49 deletions

View File

@@ -414,7 +414,7 @@
"| specialParams[4]: 节点 Rect.height",
"| specialParams[5]: 两列节点之间的距离",
"| specialParams[6]: 节点 Rect 下边缘到节点铭牌下边缘的距离",
"| specialParams[7]: roi 的 margin offset",
"| specialParams[7]: roi 的 margin offset",
"| specialParams[8]: 节点间连线方向判定的阈值"
],
"template": "empty.png",

View File

@@ -546,7 +546,7 @@
"| specialParams[4]: 节点 Rect.height",
"| specialParams[5]: 两列节点之间的距离",
"| specialParams[6]: 节点 Rect 下边缘到节点铭牌下边缘的距离",
"| specialParams[7]: roi 的 margin offset",
"| specialParams[7]: roi 的 margin offset",
"| specialParams[8]: 节点间连线方向判定的阈值"
],
"template": "empty.png",

View File

@@ -8,7 +8,7 @@
asst::RoguelikeMap::RoguelikeMap()
{
m_curr_pos = create_and_insert_node(RoguelikeNodeType::Init, init_index, 0).value();
m_curr_pos = create_and_insert_node(RoguelikeNodeType::Init, INIT_INDEX, 0).value();
}
// ————————————————————————————————————————————————————————————————————————————————
@@ -64,7 +64,7 @@ void asst::RoguelikeMap::reset()
{
m_nodes.clear();
m_column_indices.clear();
m_curr_pos = create_and_insert_node(RoguelikeNodeType::Init, init_index, 0).value();
m_curr_pos = create_and_insert_node(RoguelikeNodeType::Init, INIT_INDEX, 0).value();
}
// ————————————————————————————————————————————————————————————————————————————————
@@ -73,8 +73,8 @@ void asst::RoguelikeMap::reset()
size_t asst::RoguelikeMap::get_column_begin(const size_t& column) const
{
if (column == init_index) {
return init_index;
if (column == INIT_INDEX) {
return INIT_INDEX;
}
if (column >= m_column_indices.size()) {
Log.warn(__FUNCTION__, "| column does not exist");
@@ -232,12 +232,12 @@ void asst::RoguelikeMap::set_node_refresh_times(const size_t& node_index, int re
std::optional<size_t> asst::RoguelikeMap::insert_node(const RoguelikeNodePtr& node, const size_t& column)
{
// 第一个 node 必须为 init node
if (column != init_index && m_nodes.empty()) [[unlikely]] {
if (column != INIT_INDEX && m_nodes.empty()) [[unlikely]] {
Log.error(__FUNCTION__, "| insert node to column", column, "before init node");
return std::nullopt;
}
// 只允许有一个 init node
if (column == init_index && column < m_column_indices.size() && m_column_indices.at(column) > 0) [[unlikely]] {
if (column == INIT_INDEX && column < m_column_indices.size() && m_column_indices.at(column) > 0) [[unlikely]] {
Log.error(__FUNCTION__, "| init node has already exist");
return std::nullopt;
}

View File

@@ -80,25 +80,22 @@ public:
void reset();
// ———————— get map details ———————————————————————————————————————————————————————
size_t size() const { return m_nodes.size(); }
size_t get_num_columns() const { return m_column_indices.size(); }
size_t get_curr_pos() const { return m_curr_pos; }
size_t get_column_begin(const size_t& column) const;
size_t get_column_end(const size_t& column) const;
size_t get_next_node() const;
[[nodiscard]] size_t size() const { return m_nodes.size(); }
[[nodiscard]] size_t get_num_columns() const { return m_column_indices.size(); }
[[nodiscard]] size_t get_curr_pos() const { return m_curr_pos; }
[[nodiscard]] size_t get_column_begin(const size_t& column) const;
[[nodiscard]] size_t get_column_end(const size_t& column) const;
[[nodiscard]] size_t get_next_node() const;
// ———————— get node fields ———————————————————————————————————————————————————————
RoguelikeNodeType get_node_type(const size_t& node_index) const;
size_t get_node_column(const size_t& node_index) const;
int get_node_y(const size_t& node_index) const;
bool get_node_visited(const size_t& node_index) const;
std::vector<size_t> get_node_succs(const size_t& node_index) const;
std::vector<size_t> get_node_preds(const size_t& node_index) const;
int get_node_cost(const size_t& node_index) const;
int get_node_refresh_times(const size_t& node_index) const;
[[nodiscard]] RoguelikeNodeType get_node_type(const size_t& node_index) const;
[[nodiscard]] size_t get_node_column(const size_t& node_index) const;
[[nodiscard]] int get_node_y(const size_t& node_index) const;
[[nodiscard]] bool get_node_visited(const size_t& node_index) const;
[[nodiscard]] std::vector<size_t> get_node_succs(const size_t& node_index) const;
[[nodiscard]] std::vector<size_t> get_node_preds(const size_t& node_index) const;
[[nodiscard]] int get_node_cost(const size_t& node_index) const;
[[nodiscard]] int get_node_refresh_times(const size_t& node_index) const;
// ———————— set node fields ———————————————————————————————————————————————————————
void set_node_type(const size_t& node_index, RoguelikeNodeType type);
@@ -106,7 +103,7 @@ public:
void set_node_refresh_times(const size_t& node_index, int refresh_times);
// ———————— constants and variables ———————————————————————————————————————————————
const size_t init_index = 0; // 常量,既是 init 的 node index 也是它的 column index
static constexpr size_t INIT_INDEX = 0; // 常量,既是 init 的 node index 也是它的 column index
private:
// ———————— update map ————————————————————————————————————————————————————————————
@@ -116,8 +113,6 @@ private:
std::vector<RoguelikeNodePtr> m_nodes;
std::vector<size_t> m_column_indices; // m_column_indices[c] 代表列 c 的 node index 的上限 (exclusive)
size_t m_curr_pos = 0; // 当前位置的 node index
RoguelikeNodeCostFun m_cost_fun = [&]([[maybe_unused]] const RoguelikeNodePtr& node) {
return 0;
};
RoguelikeNodeCostFun m_cost_fun = [&]([[maybe_unused]] const RoguelikeNodePtr& node) { return 0; };
};
}

View File

@@ -124,7 +124,7 @@ void asst::RoguelikeRoutingTaskPlugin::generate_map()
const std::string& theme = m_config->get_theme();
m_map.reset();
size_t curr_col = m_map.init_index + 1;
size_t curr_col = RoguelikeMap::INIT_INDEX + 1;
Rect roi = Task.get<MatchTaskInfo>(theme + "@Roguelike@RoutingNodeAnalyze")->roi;
// 第一列节点
@@ -162,8 +162,6 @@ void asst::RoguelikeRoutingTaskPlugin::generate_map()
}
ProcessTask(*this, { theme + "@Roguelike@RoutingExitThenContinue" }).run(); // 通过退出重进回到初始位置
return;
}
void asst::RoguelikeRoutingTaskPlugin::generate_edges(const size_t& node, const cv::Mat& image, const int& node_x)
@@ -172,13 +170,13 @@ void asst::RoguelikeRoutingTaskPlugin::generate_edges(const size_t& node, const
const size_t node_column = m_map.get_node_column(node);
if (node_column == m_map.init_index) {
if (node_column == RoguelikeMap::INIT_INDEX) {
Log.error(__FUNCTION__, "| cannot generate edges for init node");
return;
}
if (node_column == m_map.init_index + 1) {
m_map.add_edge(m_map.init_index, node); // 第一列节点直接与 init 连接
if (node_column == RoguelikeMap::INIT_INDEX + 1) {
m_map.add_edge(RoguelikeMap::INIT_INDEX, node); // 第一列节点直接与 init 连接
return;
}
@@ -221,14 +219,10 @@ void asst::RoguelikeRoutingTaskPlugin::generate_edges(const size_t& node, const
ranges::minmax(rightmostBrightPoints, /*comp=*/ {}, [](const Point& p) { return p.y; });
const int rightmost_y = (rightmost_y_min_p.y + rightmost_y_max_p.y) / 2;
if (std::abs(prev_y - node_y) < m_direction_threshold &&
std::abs(leftmost_y - rightmost_y) < m_direction_threshold) {
m_map.add_edge(prev, node);
}
else if (prev_y < node_y && leftmost_y < rightmost_y - m_direction_threshold) {
m_map.add_edge(prev, node);
}
else if (prev_y > node_y && leftmost_y > rightmost_y + m_direction_threshold) {
if ((std::abs(prev_y - node_y) < m_direction_threshold &&
std::abs(leftmost_y - rightmost_y) < m_direction_threshold) ||
(prev_y < node_y && leftmost_y < rightmost_y - m_direction_threshold) ||
(prev_y > node_y && leftmost_y > rightmost_y + m_direction_threshold)) {
m_map.add_edge(prev, node);
}
}
@@ -292,7 +286,7 @@ void asst::RoguelikeRoutingTaskPlugin::refresh_following_combat_nodes()
node_analyzer.set_task_info(theme + "@Roguelike@RoutingNodeAnalyze");
node_analyzer.set_roi(next_node_rect);
if (node_analyzer.analyze()) {
Matcher::Result match_results = node_analyzer.get_result();
const Matcher::Result& match_results = node_analyzer.get_result();
m_map.set_node_type(next_node, RoguelikeMapInfo.templ2type(theme, match_results.templ_name));
}
}
@@ -302,14 +296,14 @@ void asst::RoguelikeRoutingTaskPlugin::navigate_route()
{
LogTraceFunction;
const size_t curr_column = m_map.get_node_column(m_map.get_curr_pos());
const size_t curr_col = m_map.get_node_column(m_map.get_curr_pos());
m_map.set_cost_fun([&](const RoguelikeNodePtr& node) {
if (node->visited) {
return 1000;
}
if (node->column == curr_column) {
if (node->column == curr_col) {
return 1000;
}
@@ -354,10 +348,10 @@ void asst::RoguelikeRoutingTaskPlugin::navigate_route()
void asst::RoguelikeRoutingTaskPlugin::update_selected_x()
{
if (m_selected_column == m_map.init_index) {
if (m_selected_column == RoguelikeMap::INIT_INDEX) {
m_selected_x = m_origin_x - m_column_offset;
}
else if (m_selected_column == m_map.init_index + 1) {
else if (m_selected_column == RoguelikeMap::INIT_INDEX + 1) {
m_selected_x = m_origin_x;
}
else if (m_selected_column == m_map.get_num_columns() - 1) [[unlikely]] {

View File

@@ -46,7 +46,7 @@ private:
int m_node_height = 0; // 节点 Rect.height
int m_column_offset = 0; // 两列节点之间的距离
int m_nameplate_offset = 0; // 节点 Rect 下边缘到节点铭牌下边缘的距离
int m_roi_margin = 0; // roi 的 margin offset
int m_roi_margin = 0; // roi 的 margin offset
int m_direction_threshold = 0; // 节点间连线方向判定的阈值
};
}