Skip to content

Commit

Permalink
feat: 调整自动战斗等待条件的逻辑,增加一个cd中干员数量的条件
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Aug 31, 2022
1 parent 36fb612 commit 69b2996
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 32 deletions.
6 changes: 5 additions & 1 deletion docs/战斗流程协议.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@
// "打印" 界面不显示这条步骤,仅用于输出 doc 里的内容(用来做字幕之类的)
// "摆完挂机" 仅使用 "好了就用" 的技能,其他什么都不做,直到战斗结束

// 目前下面三个条件是且的关系,即 &&
"kills": 0, // 击杀数条件,如果没达到就一直等待。可选,默认为 0,直接执行

"cost_changes": 5, // 费用变化量,如果没达到就一直等待。可选,默认为 0,直接执行
"cost_changes": 5, // 费用变化量条件,如果没达到就一直等待。可选,默认为 0,直接执行
// 注意是从开始执行本 actions 开始计算的(即前一个 action 结束时的费用作为基准)
// 另外仅在费用是两位数的时候识别的比较准,三位数的费用可能会识别错,不推荐使用

"cooling": 2, // CD 中干员数量条件,如果没达到就一直等待。可选,默认 -1,不识别

// TODO: 其他条件
// TODO: "condition_type": 0, // 执行条件间的关系,可选,默认 0
// // 0 - 且; 1 - 或
Expand Down
1 change: 1 addition & 0 deletions src/MeoAssistant/AsstBattleDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace asst
{
int kills = 0;
int cost_changes = 0;
int cooling = 0;
BattleActionType type = BattleActionType::Deploy;
std::string group_name; // 目标名,若 type >= SwitchSpeed, group_name 为空
Point location;
Expand Down
98 changes: 67 additions & 31 deletions src/MeoAssistant/BattleProcessTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <future>
#include <thread>

#include "AsstRanges.hpp"
#include "NoWarningCV.h"

#include "Controller.h"
Expand Down Expand Up @@ -387,39 +388,33 @@ bool asst::BattleProcessTask::do_action(const BattleAction& action)

bool asst::BattleProcessTask::wait_condition(const BattleAction& action)
{
cv::Mat image;
cv::Mat image = m_ctrler->get_image();

// 因为要算基准cost,所以这个要放在kills前面
if (action.cost_changes != 0) {
int cost_base = -1;

while (true) {
image = m_ctrler->get_image();
BattleImageAnalyzer analyzer(image);
analyzer.set_target(BattleImageAnalyzer::Target::Cost);
if (analyzer.analyze()) {
int cost = analyzer.get_cost();
if (cost_base == -1) {
cost_base = cost;
continue;
}
if (cost >= cost_base + action.cost_changes) {
break;
}
}
// 计算初始状态
int cost_base = -1;
// int cooling_base = -1;

try_possible_skill(image);
std::this_thread::yield();
if (action.cost_changes != 0) {
BattleImageAnalyzer analyzer(image);
analyzer.set_target(BattleImageAnalyzer::Target::Cost);
if (analyzer.analyze()) {
cost_base = analyzer.get_cost();
}
}
// if (action.cooling != 0) {
// BattleImageAnalyzer analyzer(image);
// analyzer.set_target(BattleImageAnalyzer::Target::Oper);
// if (analyzer.analyze()) {
// cooling_base =
// ranges::count_if(analyzer.get_opers(), [](const auto& oper) -> bool { return oper.cooling; });
// }
// }

// 计算击杀数
while (m_kills < action.kills) {
if (need_exit()) {
return false;
}
if (image.empty()) {
image = m_ctrler->get_image();
}
BattleImageAnalyzer analyzer(image);
if (m_total_kills) {
analyzer.set_pre_total_kills(m_total_kills);
Expand All @@ -438,16 +433,61 @@ bool asst::BattleProcessTask::wait_condition(const BattleAction& action)
image = m_ctrler->get_image();
}

// 计算费用变化量
if (action.cost_changes) {
while (true) {
if (need_exit()) {
return false;
}
BattleImageAnalyzer analyzer(image);
analyzer.set_target(BattleImageAnalyzer::Target::Cost);
if (analyzer.analyze()) {
int cost = analyzer.get_cost();
if (cost_base == -1) {
cost_base = cost;
image = m_ctrler->get_image();
continue;
}
if (cost >= cost_base + action.cost_changes) {
break;
}
}

try_possible_skill(image);
std::this_thread::yield();
image = m_ctrler->get_image();
}
}

// 计算有几个干员在cd
if (action.cooling >= 0) {
while (true) {
if (need_exit()) {
return false;
}
BattleImageAnalyzer analyzer(image);
analyzer.set_target(BattleImageAnalyzer::Target::Oper);
if (analyzer.analyze()) {
int cooling_count =
ranges::count_if(analyzer.get_opers(), [](const auto& oper) -> bool { return oper.cooling; });
if (cooling_count == action.cooling) {
break;
}
}

try_possible_skill(image);
std::this_thread::yield();
image = m_ctrler->get_image();
}
}

// 部署干员还有额外等待费用够或 CD 转好
if (action.type == BattleActionType::Deploy) {
const std::string& name = m_group_to_oper_mapping[action.group_name].name;
while (true) {
if (need_exit()) {
return false;
}
if (image.empty()) {
image = m_ctrler->get_image();
}
update_opers_info(image);

if (auto iter = m_cur_opers_info.find(name); iter != m_cur_opers_info.cend() && iter->second.available) {
Expand All @@ -459,10 +499,6 @@ bool asst::BattleProcessTask::wait_condition(const BattleAction& action)
image = m_ctrler->get_image();
}
}
if (image.empty()) {
image = m_ctrler->get_image();
try_possible_skill(image);
}

return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/MeoAssistant/CopilotConfiger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ bool asst::CopilotConfiger::parse(const json::value& json)
}
action.kills = action_info.get("kills", 0);
action.cost_changes = action_info.get("cost_changes", 0);
action.cooling = action_info.get("cooling", -1);
action.group_name = action_info.get("name", std::string());

action.location.x = action_info.get("location", 0, 0);
Expand Down

0 comments on commit 69b2996

Please sign in to comment.