2021-05-16 12:53:32 +02:00
|
|
|
/*
|
|
|
|
* DefenceBehavior.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 "DefenceBehavior.h"
|
2021-05-16 14:39:38 +02:00
|
|
|
#include "../AIGateway.h"
|
2021-05-16 12:53:32 +02:00
|
|
|
#include "../Engine/Nullkiller.h"
|
|
|
|
#include "../AIUtility.h"
|
|
|
|
#include "../Goals/BuyArmy.h"
|
|
|
|
#include "../Goals/ExecuteHeroChain.h"
|
2021-05-16 14:00:24 +02:00
|
|
|
#include "../Behaviors/CaptureObjectsBehavior.h"
|
2020-05-04 17:58:43 +02:00
|
|
|
#include "../Goals/RecruitHero.h"
|
2021-05-16 13:11:35 +02:00
|
|
|
#include "../Goals/DismissHero.h"
|
2021-05-16 14:00:24 +02:00
|
|
|
#include "../Goals/Composition.h"
|
2023-02-28 09:07:59 +02:00
|
|
|
#include "../Goals/CaptureObject.h"
|
2021-05-16 14:00:24 +02:00
|
|
|
#include "../Markers/DefendTown.h"
|
2021-05-16 12:53:32 +02:00
|
|
|
#include "../Goals/ExchangeSwapTownHeroes.h"
|
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
const float THREAT_IGNORE_RATIO = 2;
|
2023-02-28 09:07:59 +02:00
|
|
|
|
2021-05-16 12:53:32 +02:00
|
|
|
using namespace Goals;
|
|
|
|
|
|
|
|
std::string DefenceBehavior::toString() const
|
|
|
|
{
|
|
|
|
return "Defend towns";
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:38:26 +02:00
|
|
|
Goals::TGoalVec DefenceBehavior::decompose() const
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
|
|
|
Goals::TGoalVec tasks;
|
|
|
|
|
2021-05-16 13:39:47 +02:00
|
|
|
for(auto town : cb->getTownsInfo())
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
2021-05-16 13:39:47 +02:00
|
|
|
evaluateDefence(tasks, town);
|
2021-05-16 12:53:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return tasks;
|
|
|
|
}
|
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
bool isThreatUnderControl(const CGTownInstance * town, const HitMapInfo & threat, const std::vector<AIPath> & paths)
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
2021-05-16 13:09:49 +02:00
|
|
|
int dayOfWeek = cb->getDate(Date::DAY_OF_WEEK);
|
|
|
|
|
2023-07-27 14:58:49 +02:00
|
|
|
for(const AIPath & path : paths)
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
bool threatIsWeak = path.getHeroStrength() / (float)threat.danger > THREAT_IGNORE_RATIO;
|
|
|
|
bool needToSaveGrowth = threat.turn == 0 && dayOfWeek == 7;
|
2023-07-27 14:58:49 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
if(threatIsWeak && !needToSaveGrowth)
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
if((path.exchangeCount == 1 && path.turn() < threat.turn)
|
|
|
|
|| path.turn() < threat.turn - 1
|
|
|
|
|| (path.turn() < threat.turn && threat.turn >= 2))
|
2023-07-27 14:58:49 +02:00
|
|
|
{
|
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
|
|
|
logAi->trace(
|
|
|
|
"Hero %s can eliminate danger for town %s using path %s.",
|
|
|
|
path.targetHero->getObjectName(),
|
|
|
|
town->getObjectName(),
|
|
|
|
path.toString());
|
|
|
|
#endif
|
2021-05-16 13:22:37 +02:00
|
|
|
|
2023-07-27 14:58:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-05-16 12:53:32 +02:00
|
|
|
}
|
2023-07-27 14:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleCounterAttack(
|
|
|
|
const CGTownInstance * town,
|
2023-10-27 21:32:52 +02:00
|
|
|
const HitMapInfo & threat,
|
2023-07-27 14:58:49 +02:00
|
|
|
const HitMapInfo & maximumDanger,
|
|
|
|
Goals::TGoalVec & tasks)
|
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
if(threat.hero.validAndSet()
|
|
|
|
&& threat.turn <= 1
|
|
|
|
&& (threat.danger == maximumDanger.danger || threat.turn < maximumDanger.turn))
|
2023-07-27 14:58:49 +02:00
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
auto heroCapturingPaths = ai->nullkiller->pathfinder->getPathInfo(threat.hero->visitablePos());
|
|
|
|
auto goals = CaptureObjectsBehavior::getVisitGoals(heroCapturingPaths, threat.hero.get());
|
2021-05-16 12:53:32 +02:00
|
|
|
|
2023-07-27 14:58:49 +02:00
|
|
|
for(int i = 0; i < heroCapturingPaths.size(); i++)
|
2023-06-04 15:02:02 +02:00
|
|
|
{
|
2023-07-27 14:58:49 +02:00
|
|
|
AIPath & path = heroCapturingPaths[i];
|
|
|
|
TSubgoal goal = goals[i];
|
|
|
|
|
|
|
|
if(!goal || goal->invalid() || !goal->isElementar()) continue;
|
|
|
|
|
|
|
|
Composition composition;
|
2021-05-16 12:53:32 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
composition.addNext(DefendTown(town, threat, path, true)).addNext(goal);
|
2023-06-04 15:02:02 +02:00
|
|
|
|
2023-07-27 14:58:49 +02:00
|
|
|
tasks.push_back(Goals::sptr(composition));
|
2023-06-04 15:02:02 +02:00
|
|
|
}
|
2021-05-16 12:53:32 +02:00
|
|
|
}
|
2023-07-27 14:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool handleGarrisonHeroFromPreviousTurn(const CGTownInstance * town, Goals::TGoalVec & tasks)
|
|
|
|
{
|
|
|
|
if(ai->nullkiller->isHeroLocked(town->garrisonHero.get()))
|
|
|
|
{
|
|
|
|
logAi->trace(
|
2023-10-17 22:06:08 +02:00
|
|
|
"Hero %s in garrison of town %s is supposed to defend the town",
|
2023-07-27 14:58:49 +02:00
|
|
|
town->garrisonHero->getNameTranslated(),
|
|
|
|
town->getNameTranslated());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-28 13:17:01 +02:00
|
|
|
if(!town->visitingHero)
|
2023-07-27 14:58:49 +02:00
|
|
|
{
|
2023-07-28 13:17:01 +02:00
|
|
|
if(cb->getHeroCount(ai->playerID, false) < GameConstants::MAX_HEROES_PER_PLAYER)
|
|
|
|
{
|
|
|
|
logAi->trace(
|
|
|
|
"Extracting hero %s from garrison of town %s",
|
|
|
|
town->garrisonHero->getNameTranslated(),
|
|
|
|
town->getNameTranslated());
|
2023-07-27 14:58:49 +02:00
|
|
|
|
2023-07-28 13:17:01 +02:00
|
|
|
tasks.push_back(Goals::sptr(Goals::ExchangeSwapTownHeroes(town, nullptr).setpriority(5)));
|
2023-07-27 14:58:49 +02:00
|
|
|
|
2023-07-28 13:17:01 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if(ai->nullkiller->heroManager->getHeroRole(town->garrisonHero.get()) == HeroRole::MAIN)
|
|
|
|
{
|
|
|
|
auto armyDismissLimit = 1000;
|
|
|
|
auto heroToDismiss = ai->nullkiller->heroManager->findWeakHeroToDismiss(armyDismissLimit);
|
|
|
|
|
|
|
|
if(heroToDismiss)
|
|
|
|
{
|
|
|
|
tasks.push_back(Goals::sptr(Goals::DismissHero(heroToDismiss).setpriority(5)));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2023-07-27 14:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefenceBehavior::evaluateDefence(Goals::TGoalVec & tasks, const CGTownInstance * town) const
|
|
|
|
{
|
|
|
|
logAi->trace("Evaluating defence for %s", town->getNameTranslated());
|
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
auto threatNode = ai->nullkiller->dangerHitMap->getObjectThreat(town);
|
|
|
|
std::vector<HitMapInfo> threats = ai->nullkiller->dangerHitMap->getTownThreats(town);
|
2023-07-27 14:58:49 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
threats.push_back(threatNode.fastestDanger); // no guarantee that fastest danger will be there
|
2023-07-27 14:58:49 +02:00
|
|
|
|
|
|
|
if(town->garrisonHero && handleGarrisonHeroFromPreviousTurn(town, tasks))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-07-28 13:17:01 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
if(!threatNode.fastestDanger.hero)
|
2023-07-28 13:17:01 +02:00
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
logAi->trace("No threat found for town %s", town->getNameTranslated());
|
2023-07-28 13:17:01 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2021-05-16 13:09:49 +02:00
|
|
|
|
2020-05-04 17:58:43 +02:00
|
|
|
uint64_t reinforcement = ai->nullkiller->armyManager->howManyReinforcementsCanBuy(town->getUpperArmy(), town);
|
2021-05-16 12:53:32 +02:00
|
|
|
|
|
|
|
if(reinforcement)
|
|
|
|
{
|
2023-01-04 15:17:50 +02:00
|
|
|
logAi->trace("Town %s can buy defence army %lld", town->getNameTranslated(), reinforcement);
|
2021-05-16 12:53:32 +02:00
|
|
|
tasks.push_back(Goals::sptr(Goals::BuyArmy(town, reinforcement).setpriority(0.5f)));
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:58:43 +02:00
|
|
|
auto paths = ai->nullkiller->pathfinder->getPathInfo(town->visitablePos());
|
2021-05-16 12:53:32 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
for(auto & threat : threats)
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
2022-09-06 20:14:22 +02:00
|
|
|
logAi->trace(
|
2023-10-27 21:32:52 +02:00
|
|
|
"Town %s has threat %lld in %s turns, hero: %s",
|
2023-01-04 15:17:50 +02:00
|
|
|
town->getNameTranslated(),
|
2023-10-27 21:32:52 +02:00
|
|
|
threat.danger,
|
|
|
|
std::to_string(threat.turn),
|
|
|
|
threat.hero ? threat.hero->getNameTranslated() : std::string("<no hero>"));
|
2021-05-16 12:53:32 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
handleCounterAttack(town, threat, threatNode.maximumDanger, tasks);
|
2021-05-16 13:09:49 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
if(isThreatUnderControl(town, threat, paths))
|
2021-05-16 13:09:49 +02:00
|
|
|
{
|
|
|
|
continue;
|
2023-07-27 14:58:49 +02:00
|
|
|
}
|
2021-05-16 13:09:49 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
evaluateRecruitingHero(tasks, threat, town);
|
2021-05-16 13:09:49 +02:00
|
|
|
|
2021-05-16 13:11:35 +02:00
|
|
|
if(paths.empty())
|
|
|
|
{
|
2023-01-04 15:17:50 +02:00
|
|
|
logAi->trace("No ways to defend town %s", town->getNameTranslated());
|
2021-05-16 13:11:35 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-16 14:00:24 +02:00
|
|
|
std::vector<int> pathsToDefend;
|
|
|
|
std::map<const CGHeroInstance *, std::vector<int>> defferedPaths;
|
2021-05-16 13:55:33 +02:00
|
|
|
|
2021-05-16 14:00:24 +02:00
|
|
|
for(int i = 0; i < paths.size(); i++)
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
2021-05-16 14:00:24 +02:00
|
|
|
auto & path = paths[i];
|
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2021-05-16 12:53:32 +02:00
|
|
|
logAi->trace(
|
2021-05-16 13:09:49 +02:00
|
|
|
"Hero %s can defend town with force %lld in %s turns, cost: %f, path: %s",
|
2023-02-28 09:07:59 +02:00
|
|
|
path.targetHero->getObjectName(),
|
2021-05-16 12:53:32 +02:00
|
|
|
path.getHeroStrength(),
|
|
|
|
std::to_string(path.turn()),
|
2021-05-16 13:09:49 +02:00
|
|
|
path.movementCost(),
|
2021-05-16 12:53:32 +02:00
|
|
|
path.toString());
|
|
|
|
#endif
|
2023-07-27 14:58:49 +02:00
|
|
|
|
|
|
|
auto townDefenseStrength = town->garrisonHero
|
|
|
|
? town->garrisonHero->getTotalStrength()
|
|
|
|
: (town->visitingHero ? town->visitingHero->getTotalStrength() : town->getUpperArmy()->getArmyStrength());
|
|
|
|
|
|
|
|
if(town->visitingHero && path.targetHero == town->visitingHero.get())
|
|
|
|
{
|
|
|
|
if(path.getHeroStrength() < townDefenseStrength)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(town->garrisonHero && path.targetHero == town->garrisonHero.get())
|
|
|
|
{
|
|
|
|
if(path.getHeroStrength() < townDefenseStrength)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
if(path.turn() <= threat.turn - 2)
|
2021-05-16 13:13:40 +02:00
|
|
|
{
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2023-03-06 13:19:49 +02:00
|
|
|
logAi->trace("Defer defence of %s by %s because he has enough time to reach the town next trun",
|
2023-02-28 09:07:59 +02:00
|
|
|
town->getObjectName(),
|
|
|
|
path.targetHero->getObjectName());
|
2022-09-06 20:14:22 +02:00
|
|
|
#endif
|
2021-05-16 13:13:40 +02:00
|
|
|
|
2021-05-16 14:00:24 +02:00
|
|
|
defferedPaths[path.targetHero].push_back(i);
|
2021-05-16 13:55:33 +02:00
|
|
|
|
2021-05-16 13:13:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-16 12:53:32 +02:00
|
|
|
|
2023-03-19 19:04:12 +02:00
|
|
|
if(path.targetHero == town->visitingHero.get() && path.exchangeCount == 1)
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2021-05-16 14:00:24 +02:00
|
|
|
logAi->trace("Put %s to garrison of town %s",
|
2023-02-28 09:07:59 +02:00
|
|
|
path.targetHero->getObjectName(),
|
|
|
|
town->getObjectName());
|
2021-05-16 12:53:32 +02:00
|
|
|
#endif
|
|
|
|
|
2021-05-16 14:00:24 +02:00
|
|
|
// dismiss creatures we are not able to pick to be able to hide in garrison
|
|
|
|
if(town->garrisonHero
|
|
|
|
|| town->getUpperArmy()->stacksCount() == 0
|
2022-09-22 10:22:14 +02:00
|
|
|
|| (town->getUpperArmy()->getArmyStrength() < 500 && town->fortLevel() >= CGTownInstance::CITADEL))
|
2021-05-16 14:00:24 +02:00
|
|
|
{
|
|
|
|
tasks.push_back(
|
|
|
|
Goals::sptr(Composition()
|
2023-10-27 21:32:52 +02:00
|
|
|
.addNext(DefendTown(town, threat, path.targetHero))
|
2021-05-16 14:00:24 +02:00
|
|
|
.addNext(ExchangeSwapTownHeroes(town, town->visitingHero.get(), HeroLockedReason::DEFENCE))));
|
|
|
|
}
|
2021-05-16 13:07:54 +02:00
|
|
|
|
|
|
|
continue;
|
2021-05-16 12:53:32 +02:00
|
|
|
}
|
2023-03-19 19:04:12 +02:00
|
|
|
|
|
|
|
// main without army and visiting scout with army, very specific case
|
|
|
|
if(town->visitingHero && town->getUpperArmy()->stacksCount() == 0
|
|
|
|
&& path.targetHero != town->visitingHero.get() && path.exchangeCount == 1 && path.turn() == 0
|
|
|
|
&& ai->nullkiller->heroManager->evaluateHero(path.targetHero) > ai->nullkiller->heroManager->evaluateHero(town->visitingHero.get())
|
|
|
|
&& 10 * path.targetHero->getTotalStrength() < town->visitingHero->getTotalStrength())
|
|
|
|
{
|
|
|
|
path.heroArmy = town->visitingHero.get();
|
|
|
|
|
|
|
|
tasks.push_back(
|
|
|
|
Goals::sptr(Composition()
|
2023-10-27 21:32:52 +02:00
|
|
|
.addNext(DefendTown(town, threat, path))
|
2023-06-04 15:02:02 +02:00
|
|
|
.addNextSequence({
|
|
|
|
sptr(ExchangeSwapTownHeroes(town, town->visitingHero.get())),
|
|
|
|
sptr(ExecuteHeroChain(path, town)),
|
|
|
|
sptr(ExchangeSwapTownHeroes(town, path.targetHero, HeroLockedReason::DEFENCE))
|
|
|
|
})));
|
2023-03-19 19:04:12 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-16 13:07:54 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
if(threat.turn == 0 || (path.turn() <= threat.turn && path.getHeroStrength() * SAFE_ATTACK_CONSTANT >= threat.danger))
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
2021-05-16 13:13:56 +02:00
|
|
|
if(ai->nullkiller->arePathHeroesLocked(path))
|
|
|
|
{
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2021-05-16 14:00:24 +02:00
|
|
|
logAi->trace("Can not move %s to defend town %s. Path is locked.",
|
2023-02-28 09:07:59 +02:00
|
|
|
path.targetHero->getObjectName(),
|
|
|
|
town->getObjectName());
|
2021-05-16 13:13:56 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-16 14:00:24 +02:00
|
|
|
pathsToDefend.push_back(i);
|
2021-05-16 13:55:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 14:00:24 +02:00
|
|
|
for(int i : pathsToDefend)
|
2021-05-16 13:55:33 +02:00
|
|
|
{
|
2021-05-16 14:00:24 +02:00
|
|
|
AIPath & path = paths[i];
|
2021-05-16 13:55:33 +02:00
|
|
|
|
2021-05-16 14:00:24 +02:00
|
|
|
for(int j : defferedPaths[path.targetHero])
|
2021-05-16 13:55:33 +02:00
|
|
|
{
|
2021-05-16 14:00:24 +02:00
|
|
|
AIPath & defferedPath = paths[j];
|
|
|
|
|
2021-05-16 13:55:33 +02:00
|
|
|
if(defferedPath.getHeroStrength() >= path.getHeroStrength()
|
|
|
|
&& defferedPath.turn() <= path.turn())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-06-04 15:02:02 +02:00
|
|
|
Composition composition;
|
2021-05-16 13:55:33 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
composition.addNext(DefendTown(town, threat, path));
|
2023-06-04 15:02:02 +02:00
|
|
|
TGoalVec sequence;
|
|
|
|
|
2023-07-27 14:58:49 +02:00
|
|
|
if(town->garrisonHero && path.targetHero == town->garrisonHero.get() && path.exchangeCount == 1)
|
|
|
|
{
|
|
|
|
composition.addNext(ExchangeSwapTownHeroes(town, town->garrisonHero.get(), HeroLockedReason::DEFENCE));
|
|
|
|
tasks.push_back(Goals::sptr(composition));
|
|
|
|
|
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
|
|
|
logAi->trace("Locking hero %s in garrison of %s",
|
|
|
|
town->garrisonHero.get()->getObjectName(),
|
|
|
|
town->getObjectName());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(town->visitingHero && path.targetHero != town->visitingHero && !path.containsHero(town->visitingHero))
|
2023-06-04 15:02:02 +02:00
|
|
|
{
|
|
|
|
if(town->garrisonHero)
|
|
|
|
{
|
|
|
|
if(ai->nullkiller->heroManager->getHeroRole(town->visitingHero.get()) == HeroRole::SCOUT
|
|
|
|
&& town->visitingHero->getArmyStrength() < path.heroArmy->getArmyStrength() / 20)
|
|
|
|
{
|
|
|
|
if(path.turn() == 0)
|
|
|
|
sequence.push_back(sptr(DismissHero(town->visitingHero.get())));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2023-06-04 15:02:02 +02:00
|
|
|
logAi->trace("Cancel moving %s to defend town %s as the town has garrison hero",
|
|
|
|
path.targetHero->getObjectName(),
|
|
|
|
town->getObjectName());
|
2021-05-16 14:00:24 +02:00
|
|
|
#endif
|
2023-06-04 15:02:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(path.turn() == 0)
|
|
|
|
{
|
|
|
|
sequence.push_back(sptr(ExchangeSwapTownHeroes(town, town->visitingHero.get())));
|
|
|
|
}
|
|
|
|
}
|
2021-05-16 14:00:24 +02:00
|
|
|
|
2023-06-04 15:02:02 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
|
|
|
logAi->trace("Move %s to defend town %s",
|
|
|
|
path.targetHero->getObjectName(),
|
|
|
|
town->getObjectName());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sequence.push_back(sptr(ExecuteHeroChain(path, town)));
|
|
|
|
composition.addNextSequence(sequence);
|
2021-05-16 14:00:24 +02:00
|
|
|
|
|
|
|
auto firstBlockedAction = path.getFirstBlockedAction();
|
|
|
|
if(firstBlockedAction)
|
|
|
|
{
|
|
|
|
auto subGoal = firstBlockedAction->decompose(path.targetHero);
|
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 2
|
2021-05-16 14:00:24 +02:00
|
|
|
logAi->trace("Decomposing special action %s returns %s", firstBlockedAction->toString(), subGoal->toString());
|
2021-05-16 12:53:32 +02:00
|
|
|
#endif
|
|
|
|
|
2021-05-16 14:00:24 +02:00
|
|
|
if(subGoal->invalid())
|
|
|
|
{
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2021-05-16 14:00:24 +02:00
|
|
|
logAi->trace("Path is invalid. Skipping");
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
composition.addNext(subGoal);
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks.push_back(Goals::sptr(composition));
|
2021-05-16 12:53:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logAi->debug("Found %d tasks", tasks.size());
|
2022-09-22 10:22:14 +02:00
|
|
|
}
|
2022-09-26 20:01:07 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
void DefenceBehavior::evaluateRecruitingHero(Goals::TGoalVec & tasks, const HitMapInfo & threat, const CGTownInstance * town) const
|
2023-06-04 15:02:02 +02:00
|
|
|
{
|
|
|
|
if(town->hasBuilt(BuildingID::TAVERN)
|
|
|
|
&& cb->getResourceAmount(EGameResID::GOLD) > GameConstants::HERO_GOLD_COST)
|
|
|
|
{
|
|
|
|
auto heroesInTavern = cb->getAvailableHeroes(town);
|
|
|
|
|
|
|
|
for(auto hero : heroesInTavern)
|
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
if(hero->getTotalStrength() < threat.danger)
|
2023-06-04 15:02:02 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
auto myHeroes = cb->getHeroesInfo();
|
|
|
|
|
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
|
|
|
logAi->trace("Hero %s can be recruited to defend %s", hero->getObjectName(), town->getObjectName());
|
|
|
|
#endif
|
|
|
|
bool needSwap = false;
|
|
|
|
const CGHeroInstance * heroToDismiss = nullptr;
|
|
|
|
|
|
|
|
if(town->visitingHero)
|
|
|
|
{
|
|
|
|
if(!town->garrisonHero)
|
|
|
|
needSwap = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(town->visitingHero->getArmyStrength() < town->garrisonHero->getArmyStrength())
|
|
|
|
{
|
|
|
|
if(town->visitingHero->getArmyStrength() >= hero->getArmyStrength())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
heroToDismiss = town->visitingHero.get();
|
|
|
|
}
|
|
|
|
else if(town->garrisonHero->getArmyStrength() >= hero->getArmyStrength())
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
needSwap = true;
|
|
|
|
heroToDismiss = town->garrisonHero.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(ai->nullkiller->heroManager->heroCapReached())
|
|
|
|
{
|
2023-07-28 13:17:01 +02:00
|
|
|
heroToDismiss = ai->nullkiller->heroManager->findWeakHeroToDismiss(hero->getArmyStrength());
|
2023-06-04 15:02:02 +02:00
|
|
|
|
2023-07-28 13:17:01 +02:00
|
|
|
if(!heroToDismiss)
|
2023-06-04 15:02:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TGoalVec sequence;
|
|
|
|
Goals::Composition recruitHeroComposition;
|
|
|
|
|
|
|
|
if(needSwap)
|
|
|
|
sequence.push_back(sptr(ExchangeSwapTownHeroes(town, town->visitingHero.get())));
|
|
|
|
|
|
|
|
if(heroToDismiss)
|
|
|
|
sequence.push_back(sptr(DismissHero(heroToDismiss)));
|
|
|
|
|
|
|
|
sequence.push_back(sptr(Goals::RecruitHero(town, hero)));
|
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
tasks.push_back(sptr(Goals::Composition().addNext(DefendTown(town, threat, hero)).addNextSequence(sequence)));
|
2023-06-04 15:02:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
}
|