From 87ddcdf8e668c7b3bb8da245f89e98a023c37c12 Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 7 Nov 2025 09:33:53 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=9B=B4=E5=A5=BD=E7=9A=84=E9=9A=8F?= =?UTF-8?q?=E6=9C=BA=E7=82=B9=E5=88=86=E5=B8=83=20(#14652)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/MaaCore/Controller/AdbController.h | 2 - src/MaaCore/Controller/ControlScaleProxy.cpp | 47 ++++++++++---------- src/MaaCore/Controller/ControlScaleProxy.h | 2 +- src/MaaCore/Controller/Controller.cpp | 3 +- src/MaaCore/Controller/Controller.h | 2 - 5 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/MaaCore/Controller/AdbController.h b/src/MaaCore/Controller/AdbController.h index df3f40ee71..4e3285da30 100644 --- a/src/MaaCore/Controller/AdbController.h +++ b/src/MaaCore/Controller/AdbController.h @@ -103,8 +103,6 @@ protected: AsstCallback m_callback; - std::minstd_rand m_rand_engine; - std::mutex m_callcmd_mutex; std::shared_ptr m_platform_io = nullptr; diff --git a/src/MaaCore/Controller/ControlScaleProxy.cpp b/src/MaaCore/Controller/ControlScaleProxy.cpp index a99d08de32..ba80be8358 100644 --- a/src/MaaCore/Controller/ControlScaleProxy.cpp +++ b/src/MaaCore/Controller/ControlScaleProxy.cpp @@ -165,34 +165,35 @@ std::pair asst::ControlScaleProxy::get_scale_size() const noexcept return m_scale_size; } -asst::Point asst::ControlScaleProxy::rand_point_in_rect(const Rect& rect) +asst::Point asst::ControlScaleProxy::rand_point_in_rect(const Rect& r) { - int x = 0, y = 0; - if (rect.width == 0) { - Log.warn("click rect width is 0"); - x = rect.x; - } - else { - int x_rand; - do { - x_rand = std::poisson_distribution(rect.width / 2.)(m_rand_engine); - } while (x_rand < 0 || x_rand >= rect.width); - x = x_rand + rect.x; + // 过小矩形直接返回中心点,避免死循环 + if (r.width <= 2 || r.height <= 2) { + return { r.x + r.width / 2, r.y + r.height / 2 }; } - if (rect.height == 0) { - Log.warn("click rect height is 0"); - y = rect.y; - } - else { - int y_rand; - do { - y_rand = std::poisson_distribution(rect.height / 2.)(m_rand_engine); - } while (y_rand < 0 || y_rand >= rect.height); - y = y_rand + rect.y; + constexpr double kStdDevFactor = 3.0; + + const double std_dev_x = r.width / kStdDevFactor; + const double std_dev_y = r.height / kStdDevFactor; + + std::normal_distribution dist_x(r.x + r.width / 2.0, std_dev_x); + std::normal_distribution dist_y(r.y + r.height / 2.0, std_dev_y); + + // 优先进行有限次拒绝采样 + constexpr int kMaxAttempts = 8; + for (int i = 0; i < kMaxAttempts; ++i) { + const int x = static_cast(std::round(dist_x(m_rand_engine))); + const int y = static_cast(std::round(dist_y(m_rand_engine))); + if (x < r.x || x >= r.x + r.width || y < r.y || y >= r.y + r.height) { + continue; + } + + return { x, y }; } - return { x, y }; + // 返回中心点 + return { r.x + r.width / 2, r.y + r.height / 2 }; } void asst::ControlScaleProxy::callback(const json::object& details) diff --git a/src/MaaCore/Controller/ControlScaleProxy.h b/src/MaaCore/Controller/ControlScaleProxy.h index 219963e1f6..c184505b4d 100644 --- a/src/MaaCore/Controller/ControlScaleProxy.h +++ b/src/MaaCore/Controller/ControlScaleProxy.h @@ -61,7 +61,7 @@ private: ControllerType m_controller_type = ControllerType::Minitouch; ProxyCallback m_callback = nullptr; - std::minstd_rand m_rand_engine; + std::mt19937 m_rand_engine; std::pair m_scale_size = { WindowWidthDefault, WindowHeightDefault }; double m_control_scale = 1.0; diff --git a/src/MaaCore/Controller/Controller.cpp b/src/MaaCore/Controller/Controller.cpp index e7600cad7d..5014048fbe 100644 --- a/src/MaaCore/Controller/Controller.cpp +++ b/src/MaaCore/Controller/Controller.cpp @@ -29,8 +29,7 @@ asst::Controller::Controller(const AsstCallback& callback, Assistant* inst) : InstHelper(inst), - m_callback(callback), - m_rand_engine(std::random_device {}()) + m_callback(callback) { LogTraceFunction; } diff --git a/src/MaaCore/Controller/Controller.h b/src/MaaCore/Controller/Controller.h index 0ba886cab7..75a9d4568f 100644 --- a/src/MaaCore/Controller/Controller.h +++ b/src/MaaCore/Controller/Controller.h @@ -109,8 +109,6 @@ private: AsstCallback m_callback = nullptr; - std::minstd_rand m_rand_engine; - PlatformType m_platform_type = PlatformType::Native; ControllerType m_controller_type = ControllerType::Minitouch;