From b1ea3c7ccb2472291420aa2886d8c267acce227a Mon Sep 17 00:00:00 2001 From: MistEO Date: Sun, 13 Nov 2022 23:28:32 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=89=93=E5=BC=80=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=88=98=E6=96=97=E7=9A=84minitouch=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=88=98=E6=96=97=E6=97=B6=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E5=B9=B2=E5=91=98=E7=9A=84=E6=BB=91=E5=8A=A8=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resource/tasks.json | 12 ++++++-- src/MeoAssistant/Controller.cpp | 29 ++++++++----------- .../Task/Plugin/RoguelikeBattleTaskPlugin.cpp | 6 ++-- src/MeoAssistant/Task/Sub/AbstractTask.cpp | 10 +++++++ src/MeoAssistant/Task/Sub/AbstractTask.h | 10 +++++++ .../Task/Sub/BattleProcessTask.cpp | 6 ++-- 6 files changed, 49 insertions(+), 24 deletions(-) diff --git a/resource/tasks.json b/resource/tasks.json index e5ef717601..e50127bf28 100644 --- a/resource/tasks.json +++ b/resource/tasks.json @@ -6138,9 +6138,15 @@ }, "BattleSwipeOper": { "algorithm": "JustReturn", - "preDelay": 500, - "postDelay": 300, - "Doc": "pre 是将干员滑动到场上的 duration 系数;rear 是设置干员朝向滑动的 duration" + "preDelay": 200, + "postDelay": 50, + "Doc": "pre 是将干员滑动到场上的 duration 系数;rear 是设置干员朝向滑动的 duration", + "specialParams": [ + 200 + ], + "specialParamsDoc": [ + "设置朝向时的滑动距离" + ] }, "BattleOpersFlag": { "templThreshold": 0.85, diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 4e373ea16d..968d1a595b 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -607,7 +607,6 @@ bool asst::Controller::call_and_hup_minitouch(const std::string& cmd) bool asst::Controller::input_to_minitouch(const std::string& cmd, int delay_ms) { - LogTraceFunction; Log.info("Input to minitouch with delay", delay_ms, Logger::separator::newline, cmd); #ifdef _WIN32 @@ -879,7 +878,7 @@ bool asst::Controller::screencap(bool allow_reconnect) if (m_support_socket && m_server_started && screencap(m_adb.screencap_raw_by_nc, decode_raw, allow_reconnect, true)) { // sock 第一次截图比较长(不知道是不是初始化了什么东西耽误时间,减个额外的的时间) - auto duration = duration_cast(high_resolution_clock::now() - start_time) + 1000ms; + auto duration = duration_cast(high_resolution_clock::now() - start_time) - 100ms; if (duration < min_cost) { m_adb.screencap_method = AdbProperty::ScreencapMethod::RawByNc; make_instance_inited(true); @@ -1111,13 +1110,12 @@ bool asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int const auto& opt = Configer.get_options(); if (m_minitouch_enabled && m_minitouch_avaiable) { constexpr int MoveInterval = 1; - std::string minitouch_cmd = - MinitouchCmd::down(static_cast(x1 * m_minitouch_props.x_scaling), - static_cast(m_minitouch_props.y_scaling * y1), true, MoveInterval); + input_to_minitouch(MinitouchCmd::down(static_cast(x1 * m_minitouch_props.x_scaling), + static_cast(m_minitouch_props.y_scaling * y1), true, MoveInterval)); if (duration == 0) { - duration = 100; + duration = 200; } - auto move_cmd = [&](int _x1, int _y1, int _x2, int _y2, int _duration) -> std::string { + auto minitouch_move = [&](int _x1, int _y1, int _x2, int _y2, int _duration) { _x1 = static_cast(_x1 * m_minitouch_props.x_scaling); _y1 = static_cast(_y1 * m_minitouch_props.y_scaling); _x2 = static_cast(_x2 * m_minitouch_props.x_scaling); @@ -1127,30 +1125,27 @@ bool asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int int x_step = (_x2 - _x1) / move_times; int y_step = (_y2 - _y1) / move_times; // TODO: 加点随机因子,或者改成中间快两头慢 - std::string minitouch_cmd; for (int times = 1; times < move_times; ++times) { int cur_x = _x1 + x_step * times; int cur_y = _y1 + y_step * times; if (cur_x < 0 || cur_x > m_minitouch_props.max_x || cur_y < 0 || cur_y > m_minitouch_props.max_y) { continue; } - minitouch_cmd += MinitouchCmd::move(cur_x, cur_y, true, MoveInterval); + input_to_minitouch(MinitouchCmd::move(cur_x, cur_y, true, MoveInterval)); } if (_x2 >= 0 && _x2 <= m_minitouch_props.max_x && _y2 >= 0 && _y2 <= m_minitouch_props.max_y) { - minitouch_cmd += MinitouchCmd::move(_x2, _y2); + input_to_minitouch(MinitouchCmd::move(_x2, _y2)); } - return minitouch_cmd; }; - minitouch_cmd += move_cmd(x1, y1, x2, y2, duration); + minitouch_move(x1, y1, x2, y2, duration); if (extra_swipe && opt.minitouch_extra_swipe_duration > 0) { - minitouch_cmd += - move_cmd(x2, y2, x2, y2 - opt.minitouch_extra_swipe_dist, opt.minitouch_extra_swipe_duration); + minitouch_move(x2, y2, x2, y2 - opt.minitouch_extra_swipe_dist, opt.minitouch_extra_swipe_duration); duration += opt.minitouch_extra_swipe_duration; } - minitouch_cmd += MinitouchCmd::up(); - - return input_to_minitouch(minitouch_cmd, duration); + constexpr int ExtraWait = 50; + duration += ExtraWait; + return input_to_minitouch(MinitouchCmd::wait(ExtraWait) + MinitouchCmd::up(), duration); } else { std::string cur_cmd = diff --git a/src/MeoAssistant/Task/Plugin/RoguelikeBattleTaskPlugin.cpp b/src/MeoAssistant/Task/Plugin/RoguelikeBattleTaskPlugin.cpp index 5481eccfac..7eb91a553a 100644 --- a/src/MeoAssistant/Task/Plugin/RoguelikeBattleTaskPlugin.cpp +++ b/src/MeoAssistant/Task/Plugin/RoguelikeBattleTaskPlugin.cpp @@ -53,6 +53,8 @@ bool asst::RoguelikeBattleTaskPlugin::_run() { using namespace std::chrono_literals; + MinitouchTempSwitcher switcher(m_ctrler); + bool gotten_info = get_stage_info(); if (!gotten_info) { return true; @@ -718,13 +720,13 @@ bool asst::RoguelikeBattleTaskPlugin::auto_battle() int dist = static_cast(Point::distance( placed_point, { opt_oper.rect.x + opt_oper.rect.width / 2, opt_oper.rect.y + opt_oper.rect.height / 2 })); // 1000 是随便取的一个系数,把整数的 pre_delay 转成小数用的 - int duration = static_cast(swipe_oper_task_ptr->pre_delay / 800.0 * dist * log10(dist)); + int duration = static_cast(dist / 800.0 * swipe_oper_task_ptr->pre_delay); m_ctrler->swipe(opt_oper.rect, placed_rect, duration); sleep(use_oper_task_ptr->post_delay); // 将方向转换为实际的 swipe end 坐标点 if (direction != Point::zero()) { - constexpr int coeff = 500; + static const int coeff = Task.get("BattleSwipeOper")->special_params.at(0); Point end_point = placed_point + (direction * coeff); m_ctrler->swipe(placed_point, end_point, swipe_oper_task_ptr->post_delay); } diff --git a/src/MeoAssistant/Task/Sub/AbstractTask.cpp b/src/MeoAssistant/Task/Sub/AbstractTask.cpp index c8babbb2e7..d8be4271c3 100644 --- a/src/MeoAssistant/Task/Sub/AbstractTask.cpp +++ b/src/MeoAssistant/Task/Sub/AbstractTask.cpp @@ -214,3 +214,13 @@ bool asst::AbstractTask::save_img(const std::string& dirname) Log.trace("Save image", full_path); return asst::imwrite(full_path, image); } + +asst::MinitouchTempSwitcher::MinitouchTempSwitcher(std::shared_ptr ctrler) : m_ctrler(ctrler) +{ + m_ctrler->set_minitouch_enabled(true); +} + +asst::MinitouchTempSwitcher::~MinitouchTempSwitcher() +{ + m_ctrler->set_minitouch_enabled(false); +} diff --git a/src/MeoAssistant/Task/Sub/AbstractTask.h b/src/MeoAssistant/Task/Sub/AbstractTask.h index d09a431faf..467a8ac700 100644 --- a/src/MeoAssistant/Task/Sub/AbstractTask.h +++ b/src/MeoAssistant/Task/Sub/AbstractTask.h @@ -83,4 +83,14 @@ namespace asst std::shared_ptr m_ctrler = nullptr; std::shared_ptr m_status = nullptr; }; + + class MinitouchTempSwitcher + { + public: + MinitouchTempSwitcher(std::shared_ptr ctrler); + ~MinitouchTempSwitcher() noexcept; + + private: + std::shared_ptr m_ctrler; + }; } // namespace asst diff --git a/src/MeoAssistant/Task/Sub/BattleProcessTask.cpp b/src/MeoAssistant/Task/Sub/BattleProcessTask.cpp index e55db54315..50c0147755 100644 --- a/src/MeoAssistant/Task/Sub/BattleProcessTask.cpp +++ b/src/MeoAssistant/Task/Sub/BattleProcessTask.cpp @@ -27,6 +27,8 @@ void asst::BattleProcessTask::set_stage_name(std::string name) bool asst::BattleProcessTask::_run() { + MinitouchTempSwitcher minitoucher(m_ctrler); + bool ret = get_stage_info(); if (!ret) { @@ -519,7 +521,7 @@ bool asst::BattleProcessTask::oper_deploy(const BattleAction& action) int dist = static_cast( Point::distance(placed_point, { oper_rect.x + oper_rect.width / 2, oper_rect.y + oper_rect.height / 2 })); // 1000 是随便取的一个系数,把整数的 pre_delay 转成小数用的 - int duration = static_cast(swipe_oper_task_ptr->pre_delay / 1000.0 * dist * log10(dist)); + int duration = static_cast(dist / 800.0 * swipe_oper_task_ptr->pre_delay); m_ctrler->swipe(oper_rect, placed_rect, duration); sleep(use_oper_task_ptr->post_delay); @@ -536,7 +538,7 @@ bool asst::BattleProcessTask::oper_deploy(const BattleAction& action) Point direction = DirectionMapping.at(action.direction); // 将方向转换为实际的 swipe end 坐标点 - constexpr int coeff = 500; + static const int coeff = Task.get("BattleSwipeOper")->special_params.at(0); Point end_point = placed_point + (direction * coeff); m_ctrler->swipe(placed_point, end_point, swipe_oper_task_ptr->post_delay);