2021-05-15 20:57:31 +02:00
|
|
|
/*
|
2021-05-15 21:04:26 +02:00
|
|
|
* BuyArmyBehavior.cpp, part of VCMI engine
|
2021-05-15 20:57:31 +02:00
|
|
|
*
|
|
|
|
* 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 "BuyArmyBehavior.h"
|
2021-05-16 14:39:38 +02:00
|
|
|
#include "../AIGateway.h"
|
2021-05-15 20:57:31 +02:00
|
|
|
#include "../AIUtility.h"
|
|
|
|
#include "../Goals/BuyArmy.h"
|
2021-05-16 13:19:00 +02:00
|
|
|
#include "../Engine/Nullkiller.h"
|
2021-05-15 20:57:31 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
2021-05-15 20:57:31 +02:00
|
|
|
using namespace Goals;
|
|
|
|
|
|
|
|
std::string BuyArmyBehavior::toString() const
|
|
|
|
{
|
|
|
|
return "Buy army";
|
|
|
|
}
|
|
|
|
|
2024-03-31 17:39:00 +02:00
|
|
|
Goals::TGoalVec BuyArmyBehavior::decompose(const Nullkiller * ai) const
|
2021-05-15 20:57:31 +02:00
|
|
|
{
|
|
|
|
Goals::TGoalVec tasks;
|
|
|
|
|
|
|
|
auto heroes = cb->getHeroesInfo();
|
|
|
|
|
2021-05-16 14:39:38 +02:00
|
|
|
if(heroes.empty())
|
2021-05-15 20:57:31 +02:00
|
|
|
{
|
2021-05-16 14:39:38 +02:00
|
|
|
return tasks;
|
|
|
|
}
|
|
|
|
|
2024-08-18 09:49:02 +02:00
|
|
|
ai->dangerHitMap->updateHitMap();
|
|
|
|
|
2021-05-16 14:39:38 +02:00
|
|
|
for(auto town : cb->getTownsInfo())
|
|
|
|
{
|
2024-07-12 15:41:55 +02:00
|
|
|
//If we can recruit a hero that comes with more army than he costs, we are better off spending our gold on them
|
|
|
|
if (ai->heroManager->canRecruitHero(town))
|
|
|
|
{
|
|
|
|
auto availableHeroes = ai->cb->getAvailableHeroes(town);
|
|
|
|
for (auto hero : availableHeroes)
|
|
|
|
{
|
|
|
|
if (hero->getArmyCost() > GameConstants::HERO_GOLD_COST)
|
|
|
|
return tasks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-18 09:49:02 +02:00
|
|
|
uint8_t closestThreat = UINT8_MAX;
|
|
|
|
for (auto threat : ai->dangerHitMap->getTownThreats(town))
|
|
|
|
{
|
|
|
|
closestThreat = std::min(closestThreat, threat.turn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closestThreat >=2 && ai->buildAnalyzer->isGoldPressureHigh() && !town->hasBuilt(BuildingID::CITY_HALL) && cb->canBuildStructure(town, BuildingID::CITY_HALL) != EBuildingState::FORBIDDEN)
|
2024-07-12 15:41:55 +02:00
|
|
|
{
|
|
|
|
return tasks;
|
|
|
|
}
|
|
|
|
|
2024-03-31 17:39:00 +02:00
|
|
|
auto townArmyAvailableToBuy = ai->armyManager->getArmyAvailableToBuyAsCCreatureSet(
|
2021-05-16 14:39:38 +02:00
|
|
|
town,
|
2024-03-31 17:39:00 +02:00
|
|
|
ai->getFreeResources());
|
2021-05-15 20:57:31 +02:00
|
|
|
|
2021-05-16 14:39:38 +02:00
|
|
|
for(const CGHeroInstance * targetHero : heroes)
|
2021-05-15 20:57:31 +02:00
|
|
|
{
|
2024-03-31 17:39:00 +02:00
|
|
|
if(ai->heroManager->getHeroRole(targetHero) == HeroRole::MAIN)
|
2021-05-15 20:57:31 +02:00
|
|
|
{
|
2024-03-31 17:39:00 +02:00
|
|
|
auto reinforcement = ai->armyManager->howManyReinforcementsCanGet(
|
2021-05-16 14:39:38 +02:00
|
|
|
targetHero,
|
|
|
|
targetHero,
|
|
|
|
&*townArmyAvailableToBuy);
|
2021-05-15 20:57:31 +02:00
|
|
|
|
2021-05-16 14:39:38 +02:00
|
|
|
if(reinforcement)
|
2024-03-31 17:39:00 +02:00
|
|
|
vstd::amin(reinforcement, ai->armyManager->howManyReinforcementsCanBuy(town->getUpperArmy(), town));
|
2021-05-15 20:57:31 +02:00
|
|
|
|
2021-05-16 14:39:38 +02:00
|
|
|
if(reinforcement)
|
|
|
|
{
|
|
|
|
tasks.push_back(Goals::sptr(Goals::BuyArmy(town, reinforcement).setpriority(5)));
|
2021-05-15 20:57:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tasks;
|
2021-05-15 21:04:26 +02:00
|
|
|
}
|
2022-09-26 20:01:07 +02:00
|
|
|
|
|
|
|
}
|