1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00
vcmi/AI/Nullkiller/Goals/RecruitHero.cpp

78 lines
1.6 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/constants/StringConstants.h"
2021-05-15 18:22:44 +02:00
2022-09-26 20:01:07 +02:00
namespace NKAI
{
2021-05-15 18:22:44 +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();
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;
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
auto heroToHire = heroToBuy;
2022-10-14 10:24:43 +02:00
if(!heroToHire)
2022-10-14 10:24:43 +02:00
{
for(auto hero : heroes)
2022-10-14 10:24:43 +02:00
{
if(!heroToHire || hero->getTotalStrength() > heroToHire->getTotalStrength())
heroToHire = hero;
2022-10-14 10:24:43 +02:00
}
}
if(!heroToHire)
throw cannotFulfillGoalException("No hero to hire!");
2022-10-14 10:24:43 +02:00
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
}