From 187ee9e577c711bba0bae93406cc8a52b5caabe9 Mon Sep 17 00:00:00 2001 From: Horror Proton <107091537+horror-proton@users.noreply.github.com> Date: Wed, 11 Jan 2023 11:33:58 +0800 Subject: [PATCH] feat: update `BattleHelper::move_camera` --- .../include/Arknights-Tile-Pos/TileCalc.hpp | 20 +++++----- src/MaaCore/Config/Miscellaneous/TilePack.cpp | 11 +++--- src/MaaCore/Config/Miscellaneous/TilePack.h | 6 ++- src/MaaCore/Task/BattleHelper.cpp | 38 ++----------------- src/MaaCore/Task/BattleHelper.h | 2 +- 5 files changed, 25 insertions(+), 52 deletions(-) diff --git a/3rdparty/include/Arknights-Tile-Pos/TileCalc.hpp b/3rdparty/include/Arknights-Tile-Pos/TileCalc.hpp index 8c6fd9bbe6..78356b61b5 100644 --- a/3rdparty/include/Arknights-Tile-Pos/TileCalc.hpp +++ b/3rdparty/include/Arknights-Tile-Pos/TileCalc.hpp @@ -45,13 +45,13 @@ namespace Map bool contains(const LevelKey& key); bool run(const std::string& any_key, bool side, std::vector>& out_pos, - std::vector>& out_tiles) const; + std::vector>& out_tiles, double shift_x = 0, double shift_y = 0) const; bool run(const LevelKey& key, bool side, std::vector>& out_pos, - std::vector>& out_tiles) const; + std::vector>& out_tiles, double shift_x = 0, double shift_y = 0) const; private: bool run(const Level& level, bool side, std::vector>& out_pos, - std::vector>& out_tiles) const; + std::vector>& out_tiles, double shift_x = 0, double shift_y = 0) const; bool adapter(double& x, double& y) const; int width = 0; @@ -154,29 +154,29 @@ namespace Map } inline bool TileCalc::run(const std::string& any_key, bool side, std::vector>& out_pos, - std::vector>& out_tiles) const + std::vector>& out_tiles, double shift_x, double shift_y) const { auto iter = std::find_if(levels.cbegin(), levels.cend(), [&any_key](const Level& level) -> bool { return level.key == any_key; }); if (iter == levels.cend()) { return false; } - return run(*iter, side, out_pos, out_tiles); + return run(*iter, side, out_pos, out_tiles, shift_x, shift_y); } inline bool TileCalc::run(const LevelKey& key, bool side, std::vector>& out_pos, - std::vector>& out_tiles) const + std::vector>& out_tiles, double shift_x, double shift_y) const { auto iter = std::find_if(levels.cbegin(), levels.cend(), [&key](const Level& level) -> bool { return level.key == key; }); if (iter == levels.cend()) { return false; } - return run(*iter, side, out_pos, out_tiles); + return run(*iter, side, out_pos, out_tiles, shift_x, shift_y); } inline bool TileCalc::run(const Level& level, bool side, std::vector>& out_pos, - std::vector>& out_tiles) const + std::vector>& out_tiles, double shift_x, double shift_y) const { auto [x, y, z] = level.view[side ? 1 : 0]; double adapter_y = 0, adapter_z = 0; @@ -202,8 +202,8 @@ namespace Map for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { tmp_tiles[j] = level.get_item(i, j); - map_point.at(0, 0) = j - (w - 1) / 2.0; - map_point.at(1, 0) = (h - 1) / 2.0 - i; + map_point.at(0, 0) = j - (w - 1) / 2.0 + shift_x; + map_point.at(1, 0) = (h - 1) / 2.0 - i + shift_y; map_point.at(2, 0) = tmp_tiles[j].heightType * -0.4; cv::Mat view_point = Finall_Matrix * map_point; view_point = view_point / view_point.at(3, 0); diff --git a/src/MaaCore/Config/Miscellaneous/TilePack.cpp b/src/MaaCore/Config/Miscellaneous/TilePack.cpp index a519b4273f..9ff9b942d3 100644 --- a/src/MaaCore/Config/Miscellaneous/TilePack.cpp +++ b/src/MaaCore/Config/Miscellaneous/TilePack.cpp @@ -105,15 +105,15 @@ std::unordered_map proc_data(const std::v return dst; } -std::unordered_map asst::TilePack::calc(const std::string& any_key, - bool side) const +std::unordered_map asst::TilePack::calc(const std::string& any_key, bool side, + double shift_x, double shift_y) const { LogTraceFunction; std::vector> pos; std::vector> tiles; - bool ret = m_tile_calculator->run(any_key, side, pos, tiles); + bool ret = m_tile_calculator->run(any_key, side, pos, tiles, shift_x, shift_y); if (!ret) { Log.info("Tiles calc error!"); @@ -123,14 +123,15 @@ std::unordered_map asst::TilePack::calc(c return proc_data(pos, tiles); } -std::unordered_map asst::TilePack::calc(const LevelKey& key, bool side) const +std::unordered_map asst::TilePack::calc(const LevelKey& key, bool side, + double shift_x, double shift_y) const { LogTraceFunction; std::vector> pos; std::vector> tiles; - bool ret = m_tile_calculator->run(key, side, pos, tiles); + bool ret = m_tile_calculator->run(key, side, pos, tiles, shift_x, shift_y); if (!ret) { Log.info("Tiles calc error!"); diff --git a/src/MaaCore/Config/Miscellaneous/TilePack.h b/src/MaaCore/Config/Miscellaneous/TilePack.h index 68cfac49a9..3270a6b3bd 100644 --- a/src/MaaCore/Config/Miscellaneous/TilePack.h +++ b/src/MaaCore/Config/Miscellaneous/TilePack.h @@ -64,8 +64,10 @@ namespace asst bool contains(const std::string& any_key) const; bool contains(const LevelKey& key) const; - std::unordered_map calc(const std::string& any_key, bool side) const; - std::unordered_map calc(const LevelKey& key, bool side) const; + std::unordered_map calc(const std::string& any_key, bool side, double shift_x = 0, + double shift_y = 0) const; + std::unordered_map calc(const LevelKey& key, bool side, double shift_x = 0, + double shift_y = 0) const; private: std::shared_ptr m_tile_calculator = nullptr; diff --git a/src/MaaCore/Task/BattleHelper.cpp b/src/MaaCore/Task/BattleHelper.cpp index 6cf21ac45c..6e2aadb84e 100644 --- a/src/MaaCore/Task/BattleHelper.cpp +++ b/src/MaaCore/Task/BattleHelper.cpp @@ -45,7 +45,7 @@ void asst::BattleHelper::clear() m_used_tiles.clear(); } -bool asst::BattleHelper::calc_tiles_info(const std::string& stage_name) +bool asst::BattleHelper::calc_tiles_info(const std::string& stage_name, double shift_x, double shift_y) { LogTraceFunction; @@ -53,8 +53,8 @@ bool asst::BattleHelper::calc_tiles_info(const std::string& stage_name) return false; } - m_normal_tile_info = Tile.calc(stage_name, false); - m_side_tile_info = Tile.calc(stage_name, true); + m_normal_tile_info = Tile.calc(stage_name, false, shift_x, shift_y); + m_side_tile_info = Tile.calc(stage_name, true, shift_x, shift_y); return true; } @@ -619,37 +619,7 @@ bool asst::BattleHelper::move_camera(const std::pair& move_loc, LogTraceFunction; Log.info("move", move_loc.first, move_loc.second); - auto move_tiles = [&](std::unordered_map& tile_info) { - auto raw_tile_info = tile_info; - for (auto& [loc, tile] : tile_info) { - int view_loc_x = loc.x - static_cast(move_loc.first); - int view_loc_y = loc.y - static_cast(move_loc.second); - auto iter = raw_tile_info.find(Point(view_loc_x, view_loc_y)); - if (iter == raw_tile_info.end()) { - continue; - } - tile.pos.x = iter->second.pos.x; - // y 的移动会导致高台和平地的坐标不对,先不管了 - // tile.pos.y = iter->second.pos.y; - - if (double view_loc_x_dec = std::fabs(loc.x - move_loc.first - view_loc_x); view_loc_x_dec > DoubleDiff) { - auto left_iter = raw_tile_info.find(Point(view_loc_x - 1, view_loc_y)); - if (left_iter != raw_tile_info.end()) { - tile.pos.x -= static_cast((tile.pos.x - left_iter->second.pos.x) * view_loc_x_dec); - } - } - // if (double view_loc_y_dec = std::fabs(loc.y - move_loc.second - view_loc_y); view_loc_y_dec > DoubleDiff) - // { - // auto top_iter = raw_tile_info.find(Point(view_loc_x, view_loc_y - 1)); - // if (top_iter != raw_tile_info.end()) { - // tile.pos.y -= static_cast((tile.pos.y - top_iter->second.pos.y) * view_loc_y_dec); - // } - // } - } - }; - - move_tiles(m_side_tile_info); - move_tiles(m_normal_tile_info); + calc_tiles_info(m_stage_name, -move_loc.first, move_loc.second); if (clear_kills) { m_kills = 0; diff --git a/src/MaaCore/Task/BattleHelper.h b/src/MaaCore/Task/BattleHelper.h index 97bf37bedc..bcbb2ca078 100644 --- a/src/MaaCore/Task/BattleHelper.h +++ b/src/MaaCore/Task/BattleHelper.h @@ -31,7 +31,7 @@ namespace asst virtual const std::string oper_name_ocr_task_name() const noexcept { return "BattleOperName"; } virtual bool do_strategic_action(const cv::Mat& reusable = cv::Mat()); - bool calc_tiles_info(const std::string& stage_name); + bool calc_tiles_info(const std::string& stage_name, double shift_x = 0, double shift_y = 0); bool load_avatar_cache(const std::string& name, bool with_token = false); void save_avatar_cache(const std::string& name, const cv::Mat& avatar);