1
0
mirror of https://github.com/0O0o0oOoO00/Alas.git synced 2026-05-14 13:39:25 +08:00

fix: use Chapter.findPath to find the nearest enemy

This commit is contained in:
0O0o0oOoO00
2026-01-12 20:02:47 +08:00
parent 3c4728559c
commit ee73e57e15

View File

@@ -1978,7 +1978,46 @@ std::optional<Cracker::Cell> Cracker::NearestEnemy(sol::this_state& L, Lua::Tabl
std::optional<Cracker::Cell> best;
std::optional<int> bestDist;
auto IsValidEnemy = [&](Lua::Table& cell) {
std::optional<Lua::Object> cell_flag_o = cell["flag"];
if (!cell_flag_o.has_value() || !cell_flag_o->is<int>()) {
return false;
}
int cell_flag = cell_flag_o.value().as<int>();
if (cell_flag == m_lua_res.ChapterConst_CellFlagDisabled) {
return false;
}
std::optional<Lua::Object> cell_attachment_o = cell["attachment"];
if (!cell_attachment_o.has_value()) {
return false;
}
bool is_enemy_attach = m_lua_res.ChapterConst_IsEnemyAttach(cell_attachment_o.value());
if (!is_enemy_attach) {
return false;
}
int cell_row = cell["row"];
int cell_column = cell["column"];
std::optional<Lua::Table> cell_data = m_lua_res.Chapter_getChapterCell(L, chap, cell_row, cell_column);
if (cell_data.has_value()) {
std::optional<Lua::Object> cell_data_flag_o = cell_data.value()["flag"];
if (!cell_data_flag_o.has_value() || !cell_data_flag_o->is<int>()) {
return false;
}
int cell_data_flag = cell_data_flag_o.value().as<int>();
if (cell_data_flag == m_lua_res.ChapterConst_CellFlagDisabled) {
return false;
}
}
return true;
};
auto CheckCell = [&] (Lua::Table& cell) {
if (!IsValidEnemy(cell)) {
return;
}
if (farming && hasNoneBoss) {
if (bool is_boss_cell = m_lua_res.ChapterConst_IsBossCell(L, cell)) {
return;
@@ -1993,14 +2032,20 @@ std::optional<Cracker::Cell> Cracker::NearestEnemy(sol::this_state& L, Lua::Tabl
return;
}
int d = std::abs(cell_row - fl_row) + std::abs(cell_column - fl_column);
std::tuple<int, Lua::Table> result = m_lua_res.Chapter_findPath(L, chap, m_lua_res.ChapterConst_SubjectPlayer, fl, cell);
auto length = std::get<0>(result);
auto& path = std::get<1>(result);
if (length == 0) {
return;
}
if (!bestDist.has_value()) {
bestDist.emplace(d);
bestDist = length;
best = Cracker::Cell{cell_row, cell_column};
} else {
if (d < bestDist.value()) {
bestDist.emplace(d);
if (length < bestDist.value()) {
bestDist = length;
best = Cracker::Cell{cell_row, cell_column};
}
}
@@ -2015,17 +2060,7 @@ std::optional<Cracker::Cell> Cracker::NearestEnemy(sol::this_state& L, Lua::Tabl
return;
}
Lua::Table cell = value.as<Lua::Table>();
std::optional<Lua::Object> cell_flag = cell["flag"];
if(cell_flag.has_value()) {
if (!cell_flag->is<int>()) {
return;
}
int cell_attachment = cell["attachment"];
bool is_enemy_attach = m_lua_res.ChapterConst_IsEnemyAttach(L, cell_attachment);
if((cell_flag->as<int>() == m_lua_res.ChapterConst_CellFlagActive) && (is_enemy_attach)) {
CheckCell(cell);
}
}
CheckCell(cell);
});
}
@@ -2037,16 +2072,7 @@ std::optional<Cracker::Cell> Cracker::NearestEnemy(sol::this_state& L, Lua::Tabl
}
Lua::Table cp = value.as<Lua::Table>();
std::optional<Lua::Object> cp_flag = cp["flag"];
if(cp_flag.has_value()) {
if(!cp_flag->is<int>()) {
return;
}
if(cp_flag->as<int>() != m_lua_res.ChapterConst_CellFlagDisabled) {
CheckCell(cp);
}
}
CheckCell(cp);
});
}
@@ -2089,6 +2115,14 @@ void Cracker::my_LevelStageView_TryAutoFight(sol::this_state& L, Lua::VariadicAr
auto enemy = NearestEnemy(L, chap_v, true);
if (!enemy.has_value()) {
Lua::Object auto_fight_timer = m_lua_res.Timer_New(L, [self, chap, this](sol::this_state l, Lua::VariadicArgs) {
bool auto_fight = m_real_func.Chapter_IsAutoFight(l, chap.value());
if (auto_fight) {
Lua::Function TryAutoFight_func = self["TryAutoFight"];
TryAutoFight_func(l, self);
}
}, 1, 1);
m_lua_res.Timer_Start(L, auto_fight_timer);
return;
}
auto& enemy_v = enemy.value();
@@ -2114,8 +2148,16 @@ void Cracker::my_LevelStageView_TryAutoFight(sol::this_state& L, Lua::VariadicAr
std::tuple<Lua::Object, Lua::Table> result = m_lua_res.Chapter_findPath(L, chap_v, m_lua_res.ChapterConst_SubjectPlayer, fleet_line, enemy_cell);
auto& path = std::get<1>(result);
if(path.empty()) {
int path_size = path.size();
if (path_size == 0) {
return;
} else {
int path_row = path[path_size]["row"];
int path_column = path[path_size]["column"];
if (path_row != enemy_v.row || path_column != enemy_v.column) {
path.add(enemy_cell);
}
}
mover_go(L, self, path);