From e162ddd4af989fc40d683521cf0ecd415bd005de Mon Sep 17 00:00:00 2001 From: Horror Proton <107091537+horror-proton@users.noreply.github.com> Date: Thu, 21 Jul 2022 13:44:24 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E7=AE=80=E5=8C=96=E8=82=89=E9=B8=BD?= =?UTF-8?q?=E5=B9=B2=E5=91=98=E6=9C=9D=E5=90=91=E8=AF=84=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RoguelikeBattleTaskPlugin.cpp | 123 +++++++----------- 1 file changed, 44 insertions(+), 79 deletions(-) diff --git a/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp b/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp index 1d8e2f4722..f3a52a4dc4 100644 --- a/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp +++ b/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp @@ -709,110 +709,75 @@ std::pair asst::RoguelikeBattleTaskPlugin::calc_best_direction Point(0, 0), Point(1, 0) }; break; + default: + break; } - // 对余下三个方向分别计算攻击范围 - std::vector down_attack_range; - std::vector left_attack_range; - std::vector up_attack_range; - - const size_t size = right_attack_range.size(); - down_attack_range.reserve(size); - left_attack_range.reserve(size); - up_attack_range.reserve(size); - - for (const auto& [x, y] : right_attack_range) { - down_attack_range.emplace_back(-y, x); - left_attack_range.emplace_back(-x, -y); - up_attack_range.emplace_back(y, -x); - } - - // It's ordered. - const std::vector>> DirectionAttackRangeMap{ - { Point::up(), std::move(up_attack_range) }, - { Point::right(), std::move(right_attack_range) }, - { Point::left(), std::move(left_attack_range) }, - { Point::down(), std::move(down_attack_range) }, - }; - int max_score = 0; Point opt_direction; - // 计算每个方向上的得分 - for (const auto& [direction, direction_attack_range] : DirectionAttackRangeMap) { + for (const Point& direction : {Point::right(), Point::up(), Point::left(), Point::down()}) { int score = 0; - for (const Point& cur_attack : direction_attack_range) { - Point cur_point = loc; - cur_point.x += cur_attack.x; - cur_point.y += cur_attack.y; + for (const Point& relative_pos : right_attack_range) { + Point absolute_pos = loc + relative_pos; using TileKey = TilePack::TileKey; // 战斗干员朝向的权重 static const std::unordered_map TileKeyFightWeights = { - { TileKey::Invalid, 0 }, - { TileKey::Forbidden, 0 }, - { TileKey::Wall, 500 }, - { TileKey::Road, 1000 }, - { TileKey::Home, 500 }, - { TileKey::EnemyHome, 900 }, - { TileKey::Floor, 1000 }, - { TileKey::Hole, 0 }, - { TileKey::Telin, 700 }, - { TileKey::Telout, 800 }, - { TileKey::Volcano, 1000 }, - { TileKey::Healing, 1000 }, + { TileKey::Invalid, 0 }, + { TileKey::Forbidden, 0 }, + { TileKey::Wall, 500 }, + { TileKey::Road, 1000 }, + { TileKey::Home, 500 }, + { TileKey::EnemyHome, 900 }, + { TileKey::Floor, 1000 }, + { TileKey::Hole, 0 }, + { TileKey::Telin, 700 }, + { TileKey::Telout, 800 }, + { TileKey::Volcano, 1000 }, + { TileKey::Healing, 1000 }, }; // 治疗干员朝向的权重 static const std::unordered_map TileKeyMedicWeights = { - { TileKey::Invalid, 0 }, - { TileKey::Forbidden, 0 }, - { TileKey::Wall, 1000 }, - { TileKey::Road, 1000 }, - { TileKey::Home, 0 }, - { TileKey::EnemyHome, 0 }, - { TileKey::Floor, 0 }, - { TileKey::Hole, 0 }, - { TileKey::Telin, 0 }, - { TileKey::Telout, 0 }, - { TileKey::Volcano, 1000 }, - { TileKey::Healing, 1000 }, + { TileKey::Invalid, 0 }, + { TileKey::Forbidden, 0 }, + { TileKey::Wall, 1000 }, + { TileKey::Road, 1000 }, + { TileKey::Home, 0 }, + { TileKey::EnemyHome, 0 }, + { TileKey::Floor, 0 }, + { TileKey::Hole, 0 }, + { TileKey::Telin, 0 }, + { TileKey::Telout, 0 }, + { TileKey::Volcano, 1000 }, + { TileKey::Healing, 1000 }, }; switch (role) { - case BattleRole::Medic: - // 医疗干员根据哪个方向上人多决定朝向哪 - if (m_used_tiles.find(cur_point) != m_used_tiles.cend()) { - score += 10000; - } - // 再额外加上没人的格子的权重 - if (auto iter = m_side_tile_info.find(cur_point); - iter == m_side_tile_info.cend()) { - continue; - } - else { - score += TileKeyMedicWeights.at(iter->second.key); - } - break; - default: - // 其他干员(战斗干员)根据哪个方向上权重高决定朝向哪 - if (auto iter = m_side_tile_info.find(cur_point); - iter == m_side_tile_info.cend()) { - continue; - } - else { - score += TileKeyFightWeights.at(iter->second.key); - } + case BattleRole::Medic: + if (m_used_tiles.find(absolute_pos) != m_used_tiles.end()) // 根据哪个方向上人多决定朝向哪 + score += 10000; + if (auto iter = m_side_tile_info.find(absolute_pos); iter != m_side_tile_info.end()) + score += TileKeyMedicWeights.at(iter->second.key); + break; + default: + if (auto iter = m_side_tile_info.find(absolute_pos); iter != m_side_tile_info.end()) + score += TileKeyFightWeights.at(iter->second.key); + break; } } - if (direction == base_direction) { + if (direction == base_direction) score += 50; - } if (score > max_score) { max_score = score; opt_direction = direction; } + + // rotate relative attack range counterclockwise + for (Point& point : right_attack_range) + point = {point.y, -point.x}; } return std::make_pair(opt_direction, max_score);