1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/AI/Nullkiller/Behaviors/StartupBehavior.cpp

236 lines
6.3 KiB
C++
Raw Normal View History

/*
* StartupBehavior.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 "StartupBehavior.h"
2021-05-16 14:39:38 +02:00
#include "../AIGateway.h"
#include "../AIUtility.h"
#include "../Goals/BuildThis.h"
#include "../Goals/RecruitHero.h"
#include "../Goals/ExecuteHeroChain.h"
#include "../Goals/ExchangeSwapTownHeroes.h"
2021-05-16 12:52:30 +02:00
#include "lib/mapObjects/MapObjects.h" //for victory conditions
#include "../Engine/Nullkiller.h"
2022-09-26 20:01:07 +02:00
namespace NKAI
{
using namespace Goals;
std::string StartupBehavior::toString() const
{
return "Startup";
}
const AIPath getShortestPath(const CGTownInstance * town, const std::vector<AIPath> & paths)
{
auto shortestPath = *vstd::minElementByFun(paths, [town](const AIPath & path) -> float
{
if(town->garrisonHero && path.targetHero == town->garrisonHero.get())
return 1;
return path.movementCost();
});
return shortestPath;
}
2024-03-31 17:39:00 +02:00
const CGHeroInstance * getNearestHero(const Nullkiller * ai, const CGTownInstance * town)
{
2024-03-31 17:39:00 +02:00
auto paths = ai->pathfinder->getPathInfo(town->visitablePos());
if(paths.empty())
return nullptr;
auto shortestPath = getShortestPath(town, paths);
if(shortestPath.nodes.size() > 1
|| shortestPath.turn() != 0
|| shortestPath.targetHero->visitablePos().dist2dSQ(town->visitablePos()) > 4
|| (town->garrisonHero && shortestPath.targetHero == town->garrisonHero.get()))
return nullptr;
return shortestPath.targetHero;
}
2024-03-31 17:39:00 +02:00
bool needToRecruitHero(const Nullkiller * ai, const CGTownInstance * startupTown)
{
2024-03-31 17:39:00 +02:00
if(!ai->heroManager->canRecruitHero(startupTown))
return false;
if(!startupTown->garrisonHero && !startupTown->visitingHero)
return true;
2021-05-16 13:45:45 +02:00
int treasureSourcesCount = 0;
2024-03-31 17:39:00 +02:00
for(auto obj : ai->objectClusterizer->getNearbyObjects())
{
auto armed = dynamic_cast<const CArmedInstance *>(obj);
if(armed && armed->getArmyStrength() > 0)
continue;
bool isGoldPile = dynamic_cast<const CGResource *>(obj)
&& dynamic_cast<const CGResource *>(obj)->resourceID() == EGameResID::GOLD;
2024-04-20 14:28:42 +02:00
auto rewardable = dynamic_cast<const Rewardable::Interface *>(obj);
if(rewardable)
{
for(auto & info : rewardable->configuration.info)
if(info.reward.resources[EGameResID::GOLD] > 0)
isGoldPile = true;
}
if(isGoldPile
|| obj->ID == Obj::TREASURE_CHEST
|| obj->ID == Obj::CAMPFIRE
|| obj->ID == Obj::WATER_WHEEL)
{
treasureSourcesCount++;
}
}
2021-05-16 13:45:45 +02:00
auto basicCount = cb->getTownsInfo().size() + 2;
auto boost = std::min(
(int)std::floor(std::pow(1 + (cb->getMapSize().x / 50), 2)),
treasureSourcesCount / 2);
logAi->trace("Treasure sources found %d", treasureSourcesCount);
logAi->trace("Startup allows %d+%d heroes", basicCount, boost);
2021-05-16 13:45:45 +02:00
return cb->getHeroCount(ai->playerID, true) < basicCount + boost;
}
2024-03-31 17:39:00 +02:00
Goals::TGoalVec StartupBehavior::decompose(const Nullkiller * ai) const
{
Goals::TGoalVec tasks;
2024-03-31 17:39:00 +02:00
auto towns = ai->cb->getTownsInfo();
if(!towns.size())
return tasks;
const CGTownInstance * startupTown = towns.front();
if(towns.size() > 1)
{
2024-03-31 17:39:00 +02:00
startupTown = *vstd::maxElementByFun(towns, [ai](const CGTownInstance * town) -> float
{
if(town->garrisonHero)
2024-03-31 17:39:00 +02:00
return ai->heroManager->evaluateHero(town->garrisonHero.get());
2024-03-31 17:39:00 +02:00
auto closestHero = getNearestHero(ai, town);
if(closestHero)
2024-03-31 17:39:00 +02:00
return ai->heroManager->evaluateHero(closestHero);
return 0;
});
}
if(!startupTown->hasBuilt(BuildingID::TAVERN)
2024-03-31 17:39:00 +02:00
&& ai->cb->canBuildStructure(startupTown, BuildingID::TAVERN) == EBuildingState::ALLOWED)
{
tasks.push_back(Goals::sptr(Goals::BuildThis(BuildingID::TAVERN, startupTown).setpriority(100)));
return tasks;
}
2024-03-31 17:39:00 +02:00
bool canRecruitHero = needToRecruitHero(ai, startupTown);
auto closestHero = getNearestHero(ai, startupTown);
if(closestHero)
{
if(!startupTown->visitingHero)
{
2024-03-31 17:39:00 +02:00
if(ai->armyManager->howManyReinforcementsCanGet(startupTown->getUpperArmy(), startupTown->getUpperArmy(), closestHero) > 200)
{
2024-03-31 17:39:00 +02:00
auto paths = ai->pathfinder->getPathInfo(startupTown->visitablePos());
if(paths.size())
{
auto path = getShortestPath(startupTown, paths);
tasks.push_back(Goals::sptr(Goals::ExecuteHeroChain(path, startupTown).setpriority(100)));
}
}
}
else
{
auto visitingHero = startupTown->visitingHero.get();
2024-03-31 17:39:00 +02:00
auto visitingHeroScore = ai->heroManager->evaluateHero(visitingHero);
if(startupTown->garrisonHero)
{
auto garrisonHero = startupTown->garrisonHero.get();
2024-03-31 17:39:00 +02:00
auto garrisonHeroScore = ai->heroManager->evaluateHero(garrisonHero);
if(visitingHeroScore > garrisonHeroScore
2024-03-31 17:39:00 +02:00
|| (ai->heroManager->getHeroRole(garrisonHero) == HeroRole::SCOUT && ai->heroManager->getHeroRole(visitingHero) == HeroRole::MAIN))
{
2024-03-31 17:39:00 +02:00
if(canRecruitHero || ai->armyManager->howManyReinforcementsCanGet(visitingHero, garrisonHero) > 200)
{
tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero, HeroLockedReason::STARTUP).setpriority(100)));
}
}
2024-03-31 17:39:00 +02:00
else if(ai->armyManager->howManyReinforcementsCanGet(garrisonHero, visitingHero) > 200)
{
tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, garrisonHero, HeroLockedReason::STARTUP).setpriority(100)));
}
}
else if(canRecruitHero)
{
2021-05-16 14:00:24 +02:00
auto canPickTownArmy = startupTown->stacksCount() == 0
2024-03-31 17:39:00 +02:00
|| ai->armyManager->howManyReinforcementsCanGet(startupTown->visitingHero, startupTown) > 0;
2021-05-16 14:00:24 +02:00
if(canPickTownArmy)
{
tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(startupTown, visitingHero, HeroLockedReason::STARTUP).setpriority(100)));
}
}
}
}
if(tasks.empty() && canRecruitHero && !startupTown->visitingHero)
{
tasks.push_back(Goals::sptr(Goals::RecruitHero(startupTown)));
}
if(tasks.empty() && !startupTown->visitingHero)
{
for(auto town : towns)
{
2024-03-31 17:39:00 +02:00
if(!town->visitingHero && needToRecruitHero(ai, town))
{
tasks.push_back(Goals::sptr(Goals::RecruitHero(town)));
break;
}
}
}
if(tasks.empty() && towns.size())
{
for(const CGTownInstance * town : towns)
{
if(town->garrisonHero
&& town->garrisonHero->movementPointsRemaining()
&& !town->visitingHero
2024-03-31 17:39:00 +02:00
&& ai->getHeroLockedReason(town->garrisonHero) != HeroLockedReason::DEFENCE)
{
tasks.push_back(Goals::sptr(ExchangeSwapTownHeroes(town, nullptr).setpriority(MIN_PRIORITY)));
}
}
}
return tasks;
}
2022-09-26 20:01:07 +02:00
}