1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-27 22:49:25 +02:00
Files
vcmi/AI/Nullkiller2/Goals/RecruitHero.cpp

79 lines
1.7 KiB
C++
Raw Normal View History

2025-08-13 14:41:15 +02:00
/*
* RecruitHero.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "Goals.h"
#include "../AIGateway.h"
#include "../AIUtility.h"
#include "../../../lib/constants/StringConstants.h"
2025-08-13 17:16:27 +02:00
namespace NK2AI
2025-08-13 14:41:15 +02:00
{
using namespace Goals;
std::string RecruitHero::toString() const
{
if(heroToBuy)
return "Recruit " + heroToBuy->getNameTranslated() + " at " + town->getNameTranslated();
else
return "Recruit hero at " + town->getNameTranslated();
}
void RecruitHero::accept(AIGateway * aiGw)
2025-08-13 14:41:15 +02:00
{
auto t = town;
if(!t)
{
throw cannotFulfillGoalException("No town to recruit hero!");
}
logAi->debug("Trying to recruit a hero in %s at %s", t->getNameTranslated(), t->visitablePos().toString());
const auto heroes = aiGw->cc->getAvailableHeroes(t);
2025-08-13 14:41:15 +02:00
if(!heroes.size())
{
throw cannotFulfillGoalException("No available heroes in tavern in " + t->nodeName());
}
auto heroToHire = heroToBuy;
if(!heroToHire)
{
for(auto hero : heroes)
{
if(!heroToHire || hero->getTotalStrength() > heroToHire->getTotalStrength())
heroToHire = hero;
}
}
if(!heroToHire)
throw cannotFulfillGoalException("No hero to hire!");
if(t->getVisitingHero())
{
aiGw->cc->swapGarrisonHero(t);
2025-08-13 14:41:15 +02:00
}
if(t->getVisitingHero())
throw cannotFulfillGoalException("Town " + t->nodeName() + " is occupied. Cannot recruit hero!");
aiGw->cc->recruitHero(t, heroToHire);
2025-08-13 14:41:15 +02:00
{
// TODO: Mircea: Consider same behavior when a hero is lost? Relevant?
std::unique_lock lockGuard(aiGw->nullkiller->aiStateMutex);
aiGw->nullkiller->heroManager->update();
aiGw->nullkiller->objectClusterizer->reset();
2025-08-13 14:41:15 +02:00
}
}
}