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"
|
|
|
|
#include "../VCAI.h"
|
|
|
|
#include "../Engine/Nullkiller.h"
|
|
|
|
#include "../AIhelper.h"
|
|
|
|
#include "../Goals/ExecuteHeroChain.h"
|
|
|
|
#include "GatherArmyBehavior.h"
|
|
|
|
#include "../AIUtility.h"
|
|
|
|
#include "lib/mapping/CMap.h" //for victory conditions
|
|
|
|
#include "lib/CPathfinder.h"
|
|
|
|
|
|
|
|
extern boost::thread_specific_ptr<CCallback> cb;
|
|
|
|
extern boost::thread_specific_ptr<VCAI> ai;
|
|
|
|
extern FuzzyHelper * fh;
|
|
|
|
|
|
|
|
using namespace Goals;
|
|
|
|
|
|
|
|
#define AI_TRACE_LEVEL 2
|
|
|
|
|
|
|
|
std::string GatherArmyBehavior::toString() const
|
|
|
|
{
|
|
|
|
return "Gather army";
|
|
|
|
}
|
|
|
|
|
|
|
|
Goals::TGoalVec GatherArmyBehavior::getTasks()
|
|
|
|
{
|
|
|
|
Goals::TGoalVec tasks;
|
|
|
|
|
|
|
|
auto heroes = cb->getHeroesInfo();
|
|
|
|
|
|
|
|
if(heroes.empty())
|
|
|
|
{
|
|
|
|
return tasks;
|
|
|
|
}
|
2021-05-16 13:19:27 +02:00
|
|
|
|
2021-05-16 13:19:07 +02:00
|
|
|
for(const CGHeroInstance * hero : heroes)
|
|
|
|
{
|
2021-05-16 13:19:27 +02:00
|
|
|
if(ai->ah->getHeroRole(hero) == HeroRole::MAIN
|
|
|
|
&& hero->getArmyStrength() >= 300)
|
2021-05-16 13:19:07 +02:00
|
|
|
{
|
2021-05-16 13:19:27 +02:00
|
|
|
vstd::concatenate(tasks, deliverArmyToHero(hero));
|
2021-05-16 13:19:07 +02:00
|
|
|
}
|
2021-05-16 13:19:27 +02:00
|
|
|
}
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
auto towns = cb->getTownsInfo();
|
|
|
|
|
|
|
|
for(const CGTownInstance * town : towns)
|
|
|
|
{
|
|
|
|
vstd::concatenate(tasks, upgradeArmy(town));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Goals::TGoalVec GatherArmyBehavior::deliverArmyToHero(const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
Goals::TGoalVec tasks;
|
|
|
|
const int3 pos = hero->visitablePos();
|
2021-05-16 13:19:07 +02:00
|
|
|
|
|
|
|
#ifdef AI_TRACE_LEVEL >= 1
|
2021-05-16 13:19:27 +02:00
|
|
|
logAi->trace("Checking ways to gaher army for hero %s, %s", hero->getObjectName(), pos.toString());
|
2021-05-16 13:19:07 +02:00
|
|
|
#endif
|
2021-05-16 13:19:27 +02:00
|
|
|
if(ai->nullkiller->isHeroLocked(hero))
|
|
|
|
{
|
2021-05-16 13:19:07 +02:00
|
|
|
#ifdef AI_TRACE_LEVEL >= 1
|
2021-05-16 13:19:27 +02:00
|
|
|
logAi->trace("Skipping locked hero %s, %s", hero->getObjectName(), pos.toString());
|
2021-05-16 13:19:07 +02:00
|
|
|
#endif
|
2021-05-16 13:19:27 +02:00
|
|
|
return tasks;
|
|
|
|
}
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
auto paths = ai->ah->getPathsToTile(pos);
|
|
|
|
std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
|
2021-05-16 13:19:07 +02:00
|
|
|
|
|
|
|
#ifdef AI_TRACE_LEVEL >= 1
|
2021-05-16 13:19:27 +02:00
|
|
|
logAi->trace("Found %d paths", paths.size());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for(const AIPath & path : paths)
|
|
|
|
{
|
|
|
|
#ifdef AI_TRACE_LEVEL >= 2
|
|
|
|
logAi->trace("Path found %s", path.toString());
|
2021-05-16 13:19:07 +02:00
|
|
|
#endif
|
2021-05-16 13:19:27 +02:00
|
|
|
|
|
|
|
if(path.containsHero(hero)) continue;
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
if(path.getFirstBlockedAction())
|
2021-05-16 13:19:07 +02:00
|
|
|
{
|
|
|
|
#ifdef AI_TRACE_LEVEL >= 2
|
2021-05-16 13:19:27 +02:00
|
|
|
// TODO: decomposition?
|
|
|
|
logAi->trace("Ignore path. Action is blocked.");
|
2021-05-16 13:19:07 +02:00
|
|
|
#endif
|
2021-05-16 13:19:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
if(ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
|
|
|
|
{
|
|
|
|
#ifdef AI_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
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
float armyValue = (float)ai->ah->howManyReinforcementsCanGet(hero, path.heroArmy) / hero->getArmyStrength();
|
|
|
|
|
|
|
|
// avoid transferring very small amount of army
|
|
|
|
if(armyValue < 0.1f)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// avoid trying to move bigger army to the weaker one.
|
|
|
|
if(armyValue > 0.5f)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto danger = path.getTotalDanger();
|
|
|
|
|
|
|
|
auto isSafe = isSafeToVisit(hero, path.heroArmy, danger);
|
2021-05-16 13:19:07 +02:00
|
|
|
|
|
|
|
#ifdef AI_TRACE_LEVEL >= 2
|
2021-05-16 13:19:27 +02:00
|
|
|
logAi->trace(
|
|
|
|
"It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
|
|
|
|
isSafe ? "safe" : "not safe",
|
|
|
|
hero->name,
|
|
|
|
path.targetHero->name,
|
|
|
|
path.getHeroStrength(),
|
|
|
|
danger,
|
|
|
|
path.getTotalArmyLoss());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(isSafe)
|
|
|
|
{
|
|
|
|
auto newWay = std::make_shared<ExecuteHeroChain>(path, hero);
|
|
|
|
|
|
|
|
newWay->evaluationContext.strategicalValue = armyValue;
|
|
|
|
waysToVisitObj.push_back(newWay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(waysToVisitObj.empty())
|
|
|
|
return tasks;
|
|
|
|
|
|
|
|
for(auto way : waysToVisitObj)
|
|
|
|
{
|
|
|
|
if(ai->nullkiller->arePathHeroesLocked(way->getPath()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
way->evaluationContext.closestWayRatio = 1;
|
|
|
|
|
|
|
|
tasks.push_back(sptr(*way));
|
|
|
|
}
|
|
|
|
|
|
|
|
return tasks;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:22:37 +02:00
|
|
|
Goals::TGoalVec GatherArmyBehavior::upgradeArmy(const CGTownInstance * upgrader) const
|
2021-05-16 13:19:27 +02:00
|
|
|
{
|
|
|
|
Goals::TGoalVec tasks;
|
|
|
|
const int3 pos = upgrader->visitablePos();
|
|
|
|
TResources availableResources = cb->getResourceAmount();
|
|
|
|
|
|
|
|
#ifdef AI_TRACE_LEVEL >= 1
|
|
|
|
logAi->trace("Checking ways to upgrade army in town %s, %s", upgrader->getObjectName(), pos.toString());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
auto paths = ai->ah->getPathsToTile(pos);
|
|
|
|
std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
|
|
|
|
|
|
|
|
#ifdef AI_TRACE_LEVEL >= 1
|
|
|
|
logAi->trace("Found %d paths", paths.size());
|
2021-05-16 13:19:07 +02:00
|
|
|
#endif
|
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
for(const AIPath & path : paths)
|
|
|
|
{
|
2021-05-16 13:19:07 +02:00
|
|
|
#ifdef AI_TRACE_LEVEL >= 2
|
2021-05-16 13:19:27 +02:00
|
|
|
logAi->trace("Path found %s", path.toString());
|
2021-05-16 13:19:07 +02:00
|
|
|
#endif
|
2021-05-16 13:22:37 +02:00
|
|
|
if(upgrader->visitingHero != path.targetHero)
|
|
|
|
{
|
|
|
|
#ifdef AI_TRACE_LEVEL >= 2
|
|
|
|
logAi->trace("Ignore path. Town has visiting hero.");
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
if(path.getFirstBlockedAction())
|
|
|
|
{
|
2021-05-16 13:19:07 +02:00
|
|
|
#ifdef AI_TRACE_LEVEL >= 2
|
2021-05-16 13:19:27 +02:00
|
|
|
// TODO: decomposition?
|
|
|
|
logAi->trace("Ignore path. Action is blocked.");
|
2021-05-16 13:19:07 +02:00
|
|
|
#endif
|
2021-05-16 13:19:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
if(ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
|
|
|
|
{
|
|
|
|
#ifdef AI_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
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
auto upgrade = ai->ah->calculateCreateresUpgrade(path.heroArmy, upgrader, availableResources);
|
|
|
|
auto armyValue = (float)upgrade.upgradeValue / path.getHeroStrength();
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
if(armyValue < 0.1f || upgrade.upgradeValue < 300) // avoid small upgrades
|
|
|
|
continue;
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
auto danger = path.getTotalDanger();
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
auto isSafe = isSafeToVisit(path.targetHero, path.heroArmy, danger);
|
2021-05-16 13:19:07 +02:00
|
|
|
|
|
|
|
#ifdef AI_TRACE_LEVEL >= 2
|
2021-05-16 13:19:27 +02:00
|
|
|
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(),
|
|
|
|
path.targetHero->name,
|
|
|
|
path.getHeroStrength(),
|
|
|
|
danger,
|
|
|
|
path.getTotalArmyLoss());
|
2021-05-16 13:19:07 +02:00
|
|
|
#endif
|
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
if(isSafe)
|
|
|
|
{
|
|
|
|
auto newWay = std::make_shared<ExecuteHeroChain>(path, upgrader);
|
|
|
|
|
|
|
|
newWay->evaluationContext.strategicalValue = armyValue;
|
|
|
|
newWay->evaluationContext.goldCost = upgrade.upgradeCost[Res::GOLD];
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
waysToVisitObj.push_back(newWay);
|
2021-05-16 13:19:07 +02:00
|
|
|
}
|
2021-05-16 13:19:27 +02:00
|
|
|
}
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
if(waysToVisitObj.empty())
|
|
|
|
return tasks;
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
for(auto way : waysToVisitObj)
|
|
|
|
{
|
|
|
|
if(ai->nullkiller->arePathHeroesLocked(way->getPath()))
|
|
|
|
continue;
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
way->evaluationContext.closestWayRatio = 1;
|
2021-05-16 13:19:07 +02:00
|
|
|
|
2021-05-16 13:19:27 +02:00
|
|
|
tasks.push_back(sptr(*way));
|
2021-05-16 13:19:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return tasks;
|
|
|
|
}
|