diff --git a/blcrack/cracker/cracker.cpp b/blcrack/cracker/cracker.cpp index 84630fe20..e59fdaec7 100644 --- a/blcrack/cracker/cracker.cpp +++ b/blcrack/cracker/cracker.cpp @@ -1978,7 +1978,46 @@ std::optional Cracker::NearestEnemy(sol::this_state& L, Lua::Tabl std::optional best; std::optional bestDist; + auto IsValidEnemy = [&](Lua::Table& cell) { + std::optional cell_flag_o = cell["flag"]; + if (!cell_flag_o.has_value() || !cell_flag_o->is()) { + return false; + } + int cell_flag = cell_flag_o.value().as(); + if (cell_flag == m_lua_res.ChapterConst_CellFlagDisabled) { + return false; + } + + std::optional 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 cell_data = m_lua_res.Chapter_getChapterCell(L, chap, cell_row, cell_column); + if (cell_data.has_value()) { + std::optional cell_data_flag_o = cell_data.value()["flag"]; + if (!cell_data_flag_o.has_value() || !cell_data_flag_o->is()) { + return false; + } + int cell_data_flag = cell_data_flag_o.value().as(); + 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::NearestEnemy(sol::this_state& L, Lua::Tabl return; } - int d = std::abs(cell_row - fl_row) + std::abs(cell_column - fl_column); + std::tuple 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::NearestEnemy(sol::this_state& L, Lua::Tabl return; } Lua::Table cell = value.as(); - std::optional cell_flag = cell["flag"]; - if(cell_flag.has_value()) { - if (!cell_flag->is()) { - return; - } - int cell_attachment = cell["attachment"]; - bool is_enemy_attach = m_lua_res.ChapterConst_IsEnemyAttach(L, cell_attachment); - if((cell_flag->as() == m_lua_res.ChapterConst_CellFlagActive) && (is_enemy_attach)) { - CheckCell(cell); - } - } + CheckCell(cell); }); } @@ -2037,16 +2072,7 @@ std::optional Cracker::NearestEnemy(sol::this_state& L, Lua::Tabl } Lua::Table cp = value.as(); - std::optional cp_flag = cp["flag"]; - - if(cp_flag.has_value()) { - if(!cp_flag->is()) { - return; - } - if(cp_flag->as() != 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 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);