1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/AI/Nullkiller/Behaviors/RecruitHeroBehavior.cpp

99 lines
2.3 KiB
C++
Raw Normal View History

/*
* RecruitHeroBehavior.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 "RecruitHeroBehavior.h"
2021-05-16 14:39:38 +02:00
#include "../AIGateway.h"
#include "../AIUtility.h"
#include "../Goals/RecruitHero.h"
2021-05-15 21:02:52 +02:00
#include "../Goals/ExecuteHeroChain.h"
#include "../lib/CHeroHandler.h"
2022-09-26 20:01:07 +02:00
namespace NKAI
{
using namespace Goals;
std::string RecruitHeroBehavior::toString() const
{
return "Recruit hero";
}
2024-03-31 17:39:00 +02:00
Goals::TGoalVec RecruitHeroBehavior::decompose(const Nullkiller * ai) const
{
Goals::TGoalVec tasks;
2024-03-31 17:39:00 +02:00
auto towns = ai->cb->getTownsInfo();
2024-03-31 17:39:00 +02:00
auto ourHeroes = ai->heroManager->getHeroRoles();
2022-10-14 10:24:43 +02:00
auto minScoreToHireMain = std::numeric_limits<float>::max();
for(auto hero : ourHeroes)
{
if(hero.second != HeroRole::MAIN)
continue;
2024-03-31 17:39:00 +02:00
auto newScore = ai->heroManager->evaluateHero(hero.first.get());
2022-10-14 10:24:43 +02:00
if(minScoreToHireMain > newScore)
{
// weakest main hero score
minScoreToHireMain = newScore;
}
}
// If we don't have any heros we might want to lower our expectations.
if (ourHeroes.empty())
minScoreToHireMain = 0;
2022-10-14 10:24:43 +02:00
const CGHeroInstance* bestHeroToHire = nullptr;
const CGTownInstance* bestTownToHireFrom = nullptr;
float bestScore = 0;
bool haveCapitol = false;
2021-05-16 13:09:49 +02:00
for(auto town : towns)
{
2024-03-31 17:39:00 +02:00
if(ai->heroManager->canRecruitHero(town))
{
2024-03-31 17:39:00 +02:00
auto availableHeroes = ai->cb->getAvailableHeroes(town);
2022-10-14 10:24:43 +02:00
for(auto hero : availableHeroes)
{
2024-03-31 17:39:00 +02:00
auto score = ai->heroManager->evaluateHero(hero);
if(score > minScoreToHireMain)
2022-10-14 10:24:43 +02:00
{
score *= score / minScoreToHireMain;
}
score *= hero->getArmyCost();
if (hero->type->heroClass->faction == town->getFaction())
score *= 1.5;
score *= town->getTownLevel();
if (score > bestScore)
{
bestScore = score;
bestHeroToHire = hero;
bestTownToHireFrom = town;
2022-10-14 10:24:43 +02:00
}
}
}
if (town->hasCapitol())
haveCapitol = true;
}
if (bestHeroToHire && bestTownToHireFrom)
{
if (ai->cb->getHeroesInfo().size() < ai->cb->getTownsInfo().size() + 1
|| (ai->getFreeResources()[EGameResID::GOLD] > 10000 && !ai->buildAnalyzer->isGoldPressureHigh() && haveCapitol))
{
tasks.push_back(Goals::sptr(Goals::RecruitHero(bestTownToHireFrom, bestHeroToHire).setpriority((float)3 / ourHeroes.size())));
}
}
2021-05-15 21:02:52 +02:00
return tasks;
}
2022-09-26 20:01:07 +02:00
}