1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-28 23:06:24 +02:00

NKAI: mutex for internal AI state

This commit is contained in:
Andrii Danylchenko 2023-12-17 10:11:05 +02:00
parent cc7fc4a2c6
commit 36bc9ddcec
3 changed files with 20 additions and 7 deletions

View File

@ -588,6 +588,8 @@ void AIGateway::heroGotLevel(const CGHeroInstance * hero, PrimarySkill pskill, s
{
if(hPtr.validAndSet())
{
std::unique_lock<std::mutex> lockGuard(nullkiller->aiStateMutex);
nullkiller->heroManager->update();
answerQuery(queryID, nullkiller->heroManager->selectBestSkill(hPtr, skills));
}
@ -661,14 +663,18 @@ void AIGateway::showBlockingDialog(const std::string & text, const std::vector<C
if(selection) //select from multiple components -> take the last one (they're indexed [1-size])
sel = components.size();
// TODO: Find better way to understand it is Chest of Treasures
if(hero.validAndSet()
&& components.size() == 2
&& components.front().type == ComponentType::RESOURCE
&& (nullkiller->heroManager->getHeroRole(hero) != HeroRole::MAIN
|| nullkiller->buildAnalyzer->getGoldPreasure() > MAX_GOLD_PEASURE))
{
sel = 1; // for now lets pick gold from a chest.
std::unique_lock<std::mutex>(nullkiller->aiStateMutex);
// TODO: Find better way to understand it is Chest of Treasures
if(hero.validAndSet()
&& components.size() == 2
&& components.front().type == ComponentType::RESOURCE
&& (nullkiller->heroManager->getHeroRole(hero) != HeroRole::MAIN
|| nullkiller->buildAnalyzer->getGoldPreasure() > MAX_GOLD_PEASURE))
{
sel = 1; // for now lets pick gold from a chest.
}
}
answerQuery(askID, sel);
@ -859,6 +865,8 @@ void AIGateway::performObjectInteraction(const CGObjectInstance * obj, HeroPtr h
{
makePossibleUpgrades(h.get());
std::unique_lock<std::mutex> lockGuard(nullkiller->aiStateMutex);
if(!h->visitedTown->garrisonHero || !nullkiller->isHeroLocked(h->visitedTown->garrisonHero))
moveCreaturesToHero(h->visitedTown);

View File

@ -115,6 +115,8 @@ Goals::TTask Nullkiller::choseBestTask(Goals::TSubgoal behavior, int decompositi
void Nullkiller::resetAiState()
{
std::unique_lock<std::mutex> lockGuard(aiStateMutex);
lockedResources = TResources();
scanDepth = ScanDepth::MAIN_FULL;
playerID = ai->playerID;
@ -127,6 +129,8 @@ void Nullkiller::updateAiState(int pass, bool fast)
{
boost::this_thread::interruption_point();
std::unique_lock<std::mutex> lockGuard(aiStateMutex);
auto start = std::chrono::high_resolution_clock::now();
activeHero = nullptr;

View File

@ -73,6 +73,7 @@ public:
std::unique_ptr<ArmyFormation> armyFormation;
PlayerColor playerID;
std::shared_ptr<CCallback> cb;
std::mutex aiStateMutex;
Nullkiller();
void init(std::shared_ptr<CCallback> cb, PlayerColor playerID);