1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-28 23:06:24 +02:00
vcmi/AI/Nullkiller/Behaviors/GatherArmyBehavior.cpp

392 lines
9.7 KiB
C++
Raw Normal View History

2021-05-16 13:19:07 +02:00
/*
* GatherArmyBehavior.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"
2021-05-16 14:39:38 +02:00
#include "../AIGateway.h"
2021-05-16 13:19:07 +02:00
#include "../Engine/Nullkiller.h"
#include "../Goals/ExecuteHeroChain.h"
#include "../Goals/Composition.h"
2023-06-11 18:21:50 +02:00
#include "../Goals/RecruitHero.h"
#include "../Markers/HeroExchange.h"
#include "../Markers/ArmyUpgrade.h"
2021-05-16 13:19:07 +02:00
#include "GatherArmyBehavior.h"
2023-07-27 14:58:49 +02:00
#include "CaptureObjectsBehavior.h"
2021-05-16 13:19:07 +02:00
#include "../AIUtility.h"
#include "../Goals/ExchangeSwapTownHeroes.h"
2021-05-16 13:19:07 +02:00
2022-09-26 20:01:07 +02:00
namespace NKAI
{
2021-05-16 13:19:07 +02:00
using namespace Goals;
std::string GatherArmyBehavior::toString() const
{
return "Gather army";
}
Goals::TGoalVec GatherArmyBehavior::decompose() const
2021-05-16 13:19:07 +02:00
{
Goals::TGoalVec tasks;
auto heroes = cb->getHeroesInfo();
if(heroes.empty())
{
return tasks;
}
2021-05-16 13:19:07 +02:00
for(const CGHeroInstance * hero : heroes)
{
2023-03-05 15:42:15 +02:00
if(ai->nullkiller->heroManager->getHeroRole(hero) == HeroRole::MAIN)
2021-05-16 13:19:07 +02:00
{
vstd::concatenate(tasks, deliverArmyToHero(hero));
2021-05-16 13:19:07 +02:00
}
}
2021-05-16 13:19:07 +02:00
auto towns = cb->getTownsInfo();
for(const CGTownInstance * town : towns)
{
vstd::concatenate(tasks, upgradeArmy(town));
}
return tasks;
}
Goals::TGoalVec GatherArmyBehavior::deliverArmyToHero(const CGHeroInstance * hero) const
{
Goals::TGoalVec tasks;
const int3 pos = hero->visitablePos();
auto targetHeroScore = ai->nullkiller->heroManager->evaluateHero(hero);
2021-05-16 13:19:07 +02:00
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 1
logAi->trace("Checking ways to gaher army for hero %s, %s", hero->getObjectName(), pos.toString());
2021-05-16 13:19:07 +02:00
#endif
auto paths = ai->nullkiller->pathfinder->getPathInfo(pos);
2021-05-16 13:19:07 +02:00
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 1
2021-05-16 13:56:49 +02:00
logAi->trace("Gather army found %d paths", paths.size());
#endif
for(const AIPath & path : paths)
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Path found %s, %s, %lld", path.toString(), path.targetHero->getObjectName(), path.heroArmy->getArmyStrength());
2021-05-16 13:19:07 +02:00
#endif
if(path.containsHero(hero))
{
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Selfcontaining path. Ignore");
#endif
continue;
}
if(path.turn() > 0 && ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
2021-05-16 13:19:07 +02:00
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Ignore path. Target hero can be killed by enemy. Our power %lld", path.heroArmy->getArmyStrength());
2021-05-16 13:19:07 +02:00
#endif
continue;
}
2021-05-16 13:19:07 +02:00
if(ai->nullkiller->arePathHeroesLocked(path))
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Ignore path because of locked hero");
#endif
continue;
}
2021-05-16 13:19:07 +02:00
HeroExchange heroExchange(hero, path);
uint64_t armyValue = heroExchange.getReinforcementArmyStrength();
float armyRatio = (float)armyValue / hero->getArmyStrength();
// avoid transferring very small amount of army
if((armyRatio < 0.1f && armyValue < 20000) || armyValue < 500)
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Army value is too small.");
#endif
continue;
}
// avoid trying to move bigger army to the weaker one.
bool hasOtherMainInPath = false;
for(auto node : path.nodes)
{
if(!node.targetHero) continue;
2023-03-05 15:42:15 +02:00
auto heroRole = ai->nullkiller->heroManager->getHeroRole(node.targetHero);
2023-03-05 15:42:15 +02:00
if(heroRole == HeroRole::MAIN)
{
auto score = ai->nullkiller->heroManager->evaluateHero(node.targetHero);
2023-03-05 15:42:15 +02:00
if(score >= targetHeroScore)
2023-03-05 15:42:15 +02:00
{
hasOtherMainInPath = true;
break;
}
}
}
2023-03-05 15:42:15 +02:00
if(hasOtherMainInPath)
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Army value is too large.");
#endif
continue;
}
auto danger = path.getTotalDanger();
auto isSafe = isSafeToVisit(hero, path.heroArmy, danger);
2021-05-16 13:19:07 +02:00
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace(
"It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
isSafe ? "safe" : "not safe",
2023-02-28 09:07:59 +02:00
hero->getObjectName(),
path.targetHero->getObjectName(),
path.getHeroStrength(),
danger,
path.getTotalArmyLoss());
#endif
if(isSafe)
{
Composition composition;
ExecuteHeroChain exchangePath(path, hero);
exchangePath.closestWayRatio = 1;
composition.addNext(heroExchange);
2023-08-05 12:49:49 +02:00
if(hero->inTownGarrison && path.turn() == 0)
{
auto lockReason = ai->nullkiller->getHeroLockedReason(hero);
2023-08-05 12:49:49 +02:00
if(path.targetHero->visitedTown == hero->visitedTown)
{
composition.addNextSequence({
sptr(ExchangeSwapTownHeroes(hero->visitedTown, hero, lockReason))});
}
else
{
composition.addNextSequence({
sptr(ExchangeSwapTownHeroes(hero->visitedTown)),
sptr(exchangePath),
sptr(ExchangeSwapTownHeroes(hero->visitedTown, hero, lockReason))});
}
}
else
{
composition.addNext(exchangePath);
}
auto blockedAction = path.getFirstBlockedAction();
if(blockedAction)
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Action is blocked. Considering decomposition.");
#endif
auto subGoal = blockedAction->decompose(path.targetHero);
if(subGoal->invalid())
{
#if NKAI_TRACE_LEVEL >= 1
logAi->trace("Path is invalid. Skipping");
#endif
continue;
}
composition.addNext(subGoal);
}
tasks.push_back(sptr(composition));
}
}
return tasks;
}
2021-05-16 13:22:37 +02:00
Goals::TGoalVec GatherArmyBehavior::upgradeArmy(const CGTownInstance * upgrader) const
{
Goals::TGoalVec tasks;
const int3 pos = upgrader->visitablePos();
2021-05-16 14:39:38 +02:00
TResources availableResources = ai->nullkiller->getFreeResources();
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 1
logAi->trace("Checking ways to upgrade army in town %s, %s", upgrader->getObjectName(), pos.toString());
#endif
auto paths = ai->nullkiller->pathfinder->getPathInfo(pos);
2023-07-27 14:58:49 +02:00
auto goals = CaptureObjectsBehavior::getVisitGoals(paths);
std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 1
logAi->trace("Found %d paths", paths.size());
2021-05-16 13:19:07 +02:00
#endif
2023-06-11 18:21:50 +02:00
bool hasMainAround = false;
for(const AIPath & path : paths)
{
auto heroRole = ai->nullkiller->heroManager->getHeroRole(path.targetHero);
if(heroRole == HeroRole::MAIN && path.turn() < SCOUT_TURN_DISTANCE_LIMIT)
hasMainAround = true;
}
2023-07-27 14:58:49 +02:00
for(int i = 0; i < paths.size(); i++)
{
2023-07-27 14:58:49 +02:00
auto & path = paths[i];
auto visitGoal = goals[i];
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Path found %s, %s, %lld", path.toString(), path.targetHero->getObjectName(), path.heroArmy->getArmyStrength());
2021-05-16 13:19:07 +02:00
#endif
2023-07-27 14:58:49 +02:00
if(visitGoal->invalid())
{
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Ignore path. Not valid way.");
#endif
continue;
}
2023-06-11 18:21:50 +02:00
if(upgrader->visitingHero && (upgrader->visitingHero.get() != path.targetHero || path.exchangeCount == 1))
2021-05-16 13:22:37 +02:00
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
2021-05-16 13:22:37 +02:00
logAi->trace("Ignore path. Town has visiting hero.");
#endif
continue;
}
2021-05-16 13:19:07 +02:00
if(ai->nullkiller->arePathHeroesLocked(path))
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Ignore path because of locked hero");
#endif
continue;
}
if(path.getFirstBlockedAction())
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
// TODO: decomposition?
logAi->trace("Ignore path. Action is blocked.");
2021-05-16 13:19:07 +02:00
#endif
continue;
}
2021-05-16 13:19:07 +02:00
2023-03-05 15:42:15 +02:00
auto heroRole = ai->nullkiller->heroManager->getHeroRole(path.targetHero);
if(heroRole == HeroRole::SCOUT
&& ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
{
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Ignore path. Target hero can be killed by enemy. Our power %lld", path.heroArmy->getArmyStrength());
#endif
continue;
}
2021-05-16 13:19:07 +02:00
auto upgrade = ai->nullkiller->armyManager->calculateCreaturesUpgrade(path.heroArmy, upgrader, availableResources);
2021-05-16 13:19:07 +02:00
2023-06-11 18:21:50 +02:00
if(!upgrader->garrisonHero
&& (
hasMainAround
|| ai->nullkiller->heroManager->getHeroRole(path.targetHero) == HeroRole::MAIN))
2023-02-28 09:07:59 +02:00
{
2023-06-11 18:21:50 +02:00
ArmyUpgradeInfo armyToGetOrBuy;
armyToGetOrBuy.addArmyToGet(
ai->nullkiller->armyManager->getBestArmy(
2023-03-11 13:54:05 +02:00
path.targetHero,
path.heroArmy,
2023-06-11 18:21:50 +02:00
upgrader->getUpperArmy()));
2023-07-02 09:27:30 +02:00
armyToGetOrBuy.upgradeValue -= path.heroArmy->getArmyStrength();
2023-06-11 18:21:50 +02:00
armyToGetOrBuy.addArmyToBuy(
ai->nullkiller->armyManager->toSlotInfo(
ai->nullkiller->armyManager->getArmyAvailableToBuy(
path.heroArmy,
upgrader,
ai->nullkiller->getFreeResources(),
path.turn())));
upgrade.upgradeValue += armyToGetOrBuy.upgradeValue;
upgrade.upgradeCost += armyToGetOrBuy.upgradeCost;
vstd::concatenate(upgrade.resultingArmy, armyToGetOrBuy.resultingArmy);
if(!upgrade.upgradeValue
&& armyToGetOrBuy.upgradeValue > 20000
&& ai->nullkiller->heroManager->canRecruitHero(town)
&& path.turn() < SCOUT_TURN_DISTANCE_LIMIT)
{
for(auto hero : cb->getAvailableHeroes(town))
{
auto scoutReinforcement = ai->nullkiller->armyManager->howManyReinforcementsCanBuy(hero, town)
+ ai->nullkiller->armyManager->howManyReinforcementsCanGet(hero, town);
2023-06-11 18:21:50 +02:00
if(scoutReinforcement >= armyToGetOrBuy.upgradeValue
&& ai->nullkiller->getFreeGold() >20000
&& ai->nullkiller->buildAnalyzer->getGoldPreasure() < MAX_GOLD_PEASURE)
{
Composition recruitHero;
recruitHero.addNext(ArmyUpgrade(path.targetHero, town, armyToGetOrBuy)).addNext(RecruitHero(town, hero));
}
}
}
2023-02-28 09:07:59 +02:00
}
2023-03-05 15:42:15 +02:00
auto armyValue = (float)upgrade.upgradeValue / path.getHeroStrength();
if((armyValue < 0.25f && upgrade.upgradeValue < 40000) || upgrade.upgradeValue < 2000) // avoid small upgrades
2023-03-05 15:42:15 +02:00
{
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Ignore path. Army value is too small (%f)", armyValue);
#endif
continue;
2023-03-05 15:42:15 +02:00
}
2021-05-16 13:19:07 +02:00
auto danger = path.getTotalDanger();
2021-05-16 13:19:07 +02:00
auto isSafe = isSafeToVisit(path.targetHero, path.heroArmy, danger);
2021-05-16 13:19:07 +02:00
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 2
logAi->trace(
"It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
isSafe ? "safe" : "not safe",
upgrader->getObjectName(),
2023-02-28 09:07:59 +02:00
path.targetHero->getObjectName(),
path.getHeroStrength(),
danger,
path.getTotalArmyLoss());
2021-05-16 13:19:07 +02:00
#endif
if(isSafe)
{
2023-07-27 14:58:49 +02:00
tasks.push_back(sptr(Composition().addNext(ArmyUpgrade(path, upgrader, upgrade)).addNext(visitGoal)));
2021-05-16 13:19:07 +02:00
}
}
2021-05-16 13:19:07 +02:00
return tasks;
}
2022-09-26 20:01:07 +02:00
}