From e8bff714755fc851150e74f37360a827b90b155d Mon Sep 17 00:00:00 2001 From: Dmitry Novikov Date: Tue, 5 Sep 2023 10:53:05 +0700 Subject: [PATCH] Fixed crash sometimes occurring while zbot map analyzing (#844) --- regamedll/dlls/bot/cs_bot.h | 4 --- regamedll/dlls/bot/cs_bot_learn.cpp | 46 ++++++++++++++--------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/regamedll/dlls/bot/cs_bot.h b/regamedll/dlls/bot/cs_bot.h index 27d8bd59e..345e11fbd 100644 --- a/regamedll/dlls/bot/cs_bot.h +++ b/regamedll/dlls/bot/cs_bot.h @@ -42,9 +42,6 @@ enum BOT_PROGGRESS_HIDE, // hide status bar progress }; -extern int _navAreaCount; -extern int _currentIndex; - class CCSBot; class BotChatterInterface; @@ -970,7 +967,6 @@ class CCSBot: public CBot const CNavNode *m_navNodeList; CNavNode *m_currentNode; NavDirType m_generationDir; - NavAreaList::iterator m_analyzeIter; enum ProcessType { diff --git a/regamedll/dlls/bot/cs_bot_learn.cpp b/regamedll/dlls/bot/cs_bot_learn.cpp index bb6df51d2..76edded7a 100644 --- a/regamedll/dlls/bot/cs_bot_learn.cpp +++ b/regamedll/dlls/bot/cs_bot_learn.cpp @@ -30,8 +30,7 @@ const float updateTimesliceDuration = 0.5f; -int _navAreaCount = 0; -int _currentIndex = 0; +unsigned int _generationIndex = 0; // used for iterating nav areas during generation process inline CNavNode *LadderEndSearch(CBaseEntity *pEntity, const Vector *pos, NavDirType mountDir) { @@ -385,11 +384,8 @@ void CCSBot::UpdateLearnProcess() void CCSBot::StartAnalyzeAlphaProcess() { - m_processMode = PROCESS_ANALYZE_ALPHA; - m_analyzeIter = TheNavAreaList.begin(); - - _navAreaCount = TheNavAreaList.size(); - _currentIndex = 0; + m_processMode = PROCESS_ANALYZE_ALPHA; + _generationIndex = 0; ApproachAreaAnalysisPrep(); DestroyHidingSpots(); @@ -400,15 +396,18 @@ void CCSBot::StartAnalyzeAlphaProcess() bool CCSBot::AnalyzeAlphaStep() { - _currentIndex++; - if (m_analyzeIter == TheNavAreaList.end()) + _generationIndex++; + + if (_generationIndex < 0 || _generationIndex >= TheNavAreaList.size()) return false; - CNavArea *area = (*m_analyzeIter); + // TODO: Pretty ugly and very slow way to access element by index + // There is no reason not to use a vector instead of a linked list + const NavAreaList::const_iterator &iter = std::next(TheNavAreaList.begin(), _generationIndex - 1); + + CNavArea *area = (*iter); area->ComputeHidingSpots(); area->ComputeApproachAreas(); - m_analyzeIter++; - return true; } @@ -426,29 +425,30 @@ void CCSBot::UpdateAnalyzeAlphaProcess() } } - float progress = (double(_currentIndex) / double(_navAreaCount)) * 0.5f; + float progress = (double(_generationIndex) / double(TheNavAreaList.size())) * 0.5f; drawProgressMeter(progress, "#CZero_AnalyzingHidingSpots"); } void CCSBot::StartAnalyzeBetaProcess() { - m_processMode = PROCESS_ANALYZE_BETA; - m_analyzeIter = TheNavAreaList.begin(); - - _navAreaCount = TheNavAreaList.size(); - _currentIndex = 0; + m_processMode = PROCESS_ANALYZE_BETA; + _generationIndex = 0; } bool CCSBot::AnalyzeBetaStep() { - _currentIndex++; - if (m_analyzeIter == TheNavAreaList.end()) + _generationIndex++; + + if (_generationIndex < 0 || _generationIndex >= TheNavAreaList.size()) return false; - CNavArea *area = (*m_analyzeIter); + // TODO: Pretty ugly and very slow way to access element by index + // There is no reason not to use a vector instead of a linked list + const NavAreaList::const_iterator &iter = std::next(TheNavAreaList.begin(), _generationIndex - 1); + + CNavArea *area = (*iter); area->ComputeSpotEncounters(); area->ComputeSniperSpots(); - m_analyzeIter++; return true; } @@ -466,7 +466,7 @@ void CCSBot::UpdateAnalyzeBetaProcess() } } - float progress = (double(_currentIndex) / double(_navAreaCount) + 1.0f) * 0.5f; + float progress = (double(_generationIndex) / double(TheNavAreaList.size()) + 1.0f) * 0.5f; drawProgressMeter(progress, "#CZero_AnalyzingApproachPoints"); }