1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-18 17:40:48 +02:00
vcmi/AI/Nullkiller/Goals/RecruitHero.cpp

82 lines
1.7 KiB
C++
Raw Normal View History

2021-05-15 18:22:44 +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"
2021-05-16 14:39:38 +02:00
#include "../AIGateway.h"
2021-05-15 18:22:44 +02:00
#include "../AIUtility.h"
#include "../../../lib/mapping/CMap.h" //for victory conditions
#include "../../../lib/CPathfinder.h"
#include "../../../lib/StringConstants.h"
2022-09-26 20:01:07 +02:00
namespace NKAI
{
2021-05-15 18:22:44 +02:00
extern boost::thread_specific_ptr<CCallback> cb;
2021-05-16 14:39:38 +02:00
extern boost::thread_specific_ptr<AIGateway> ai;
2021-05-15 18:22:44 +02:00
using namespace Goals;
std::string RecruitHero::toString() const
{
return "Recruit hero at " + town->getNameTranslated();
2021-05-16 13:38:53 +02:00
}
2021-05-16 14:39:38 +02:00
void RecruitHero::accept(AIGateway * ai)
2021-05-16 13:38:53 +02:00
{
auto t = town;
if(!t) t = ai->findTownWithTavern();
2022-10-14 10:24:43 +02:00
if(!t)
2021-05-16 13:38:53 +02:00
{
2022-10-14 10:24:43 +02:00
throw cannotFulfillGoalException("No town to recruit hero!");
2021-05-16 13:38:53 +02:00
}
2022-10-14 10:24:43 +02:00
logAi->debug("Trying to recruit a hero in %s at %s", t->getNameTranslated(), t->visitablePos().toString());
2022-10-14 10:24:43 +02:00
auto heroes = cb->getAvailableHeroes(t);
if(!heroes.size())
2021-05-16 13:38:53 +02:00
{
2022-10-14 10:24:43 +02:00
throw cannotFulfillGoalException("No available heroes in tavern in " + t->nodeName());
2021-05-16 13:38:53 +02:00
}
2022-09-26 20:01:07 +02:00
2022-10-14 10:24:43 +02:00
auto heroToHire = heroes[0];
for(auto hero : heroes)
{
if(objid == hero->id.getNum())
{
heroToHire = hero;
break;
}
if(hero->getTotalStrength() > heroToHire->getTotalStrength())
heroToHire = hero;
}
if(t->visitingHero)
{
cb->swapGarrisonHero(t);
}
2022-10-15 14:05:20 +02:00
if(t->visitingHero)
throw cannotFulfillGoalException("Town " + t->nodeName() + " is occupied. Cannot recruit hero!");
2022-10-14 10:24:43 +02:00
cb->recruitHero(t, heroToHire);
ai->nullkiller->heroManager->update();
if(t->visitingHero)
ai->moveHeroToTile(t->visitablePos(), t->visitingHero.get());
2022-09-26 20:01:07 +02:00
}
2022-10-14 10:24:43 +02:00
}