1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-19 19:10:20 +02:00
vcmi/AI/Nullkiller/Goals/RecruitHero.cpp

81 lines
1.6 KiB
C++
Raw Normal View History

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