2021-05-15 20:56:31 +02:00
|
|
|
/*
|
|
|
|
* ExecuteHeroChain.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 "ExecuteHeroChain.h"
|
|
|
|
#include "VisitTile.h"
|
|
|
|
#include "../VCAI.h"
|
|
|
|
#include "../FuzzyHelper.h"
|
|
|
|
#include "../AIhelper.h"
|
2021-05-15 20:57:44 +02:00
|
|
|
#include "../../../lib/mapping/CMap.h" //for victory conditions
|
|
|
|
#include "../../../lib/CPathfinder.h"
|
2021-05-15 20:56:31 +02:00
|
|
|
#include "../Engine/Nullkiller.h"
|
|
|
|
|
|
|
|
extern boost::thread_specific_ptr<CCallback> cb;
|
|
|
|
extern boost::thread_specific_ptr<VCAI> ai;
|
|
|
|
extern FuzzyHelper * fh;
|
|
|
|
|
|
|
|
using namespace Goals;
|
|
|
|
|
|
|
|
ExecuteHeroChain::ExecuteHeroChain(const AIPath & path, const CGObjectInstance * obj)
|
|
|
|
:CGoal(Goals::EXECUTE_HERO_CHAIN), chainPath(path)
|
|
|
|
{
|
2021-05-16 13:13:40 +02:00
|
|
|
evaluationContext.danger = path.getTotalDanger();
|
2021-05-15 20:56:31 +02:00
|
|
|
evaluationContext.movementCost = path.movementCost();
|
2021-05-16 13:13:40 +02:00
|
|
|
evaluationContext.armyLoss = path.getTotalArmyLoss();
|
2021-05-15 20:56:31 +02:00
|
|
|
evaluationContext.heroStrength = path.getHeroStrength();
|
2021-05-16 13:14:07 +02:00
|
|
|
|
2021-05-15 20:56:31 +02:00
|
|
|
hero = path.targetHero;
|
2021-05-15 20:57:27 +02:00
|
|
|
tile = path.targetTile();
|
|
|
|
|
2021-05-16 13:14:07 +02:00
|
|
|
for(auto & node : path.nodes)
|
|
|
|
{
|
|
|
|
auto role = ai->ah->getHeroRole(node.targetHero);
|
|
|
|
|
|
|
|
evaluationContext.movementCostByRole[role] += node.cost;
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
if(obj)
|
|
|
|
{
|
|
|
|
objid = obj->id.getNum();
|
|
|
|
targetName = obj->getObjectName() + tile.toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
targetName = "tile" + tile.toString();
|
|
|
|
}
|
2021-05-15 20:56:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExecuteHeroChain::operator==(const ExecuteHeroChain & other) const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TSubgoal ExecuteHeroChain::whatToDoToAchieve()
|
|
|
|
{
|
|
|
|
return iAmElementar();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExecuteHeroChain::accept(VCAI * ai)
|
|
|
|
{
|
2021-05-15 20:57:27 +02:00
|
|
|
logAi->debug("Executing hero chain towards %s. Path %s", targetName, chainPath.toString());
|
2021-05-15 20:56:31 +02:00
|
|
|
|
|
|
|
std::set<int> blockedIndexes;
|
|
|
|
|
|
|
|
for(int i = chainPath.nodes.size() - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
auto & node = chainPath.nodes[i];
|
|
|
|
|
|
|
|
HeroPtr hero = node.targetHero;
|
|
|
|
|
|
|
|
if(vstd::contains(blockedIndexes, i))
|
|
|
|
{
|
|
|
|
blockedIndexes.insert(node.parentIndex);
|
2021-05-16 13:13:56 +02:00
|
|
|
ai->nullkiller->lockHero(hero.get(), HeroLockedReason::HERO_CHAIN);
|
2021-05-15 20:56:31 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-16 12:51:32 +02:00
|
|
|
logAi->debug("Executing chain node %d. Moving hero %s to %s", i, hero.name, node.coord.toString());
|
2021-05-15 20:56:31 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-05-15 21:02:27 +02:00
|
|
|
if(hero->movement)
|
|
|
|
{
|
2021-05-16 13:07:54 +02:00
|
|
|
ai->nullkiller->setActive(hero.get());
|
2021-05-15 20:56:31 +02:00
|
|
|
|
2021-05-15 21:02:57 +02:00
|
|
|
if(node.specialAction)
|
|
|
|
{
|
2021-05-16 13:07:54 +02:00
|
|
|
if(node.specialAction->canAct(hero.get()))
|
|
|
|
{
|
|
|
|
auto specialGoal = node.specialAction->whatToDo(hero);
|
2021-05-15 21:02:57 +02:00
|
|
|
|
2021-05-16 13:19:07 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
specialGoal->accept(ai);
|
|
|
|
}
|
|
|
|
catch(cannotFulfillGoalException e)
|
|
|
|
{
|
|
|
|
logAi->warn("Can not complete %s because of an exception: %s", specialGoal->name(), e.what());
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2021-05-16 13:07:54 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//TODO: decompose
|
|
|
|
}
|
2021-05-15 21:02:57 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:19:07 +02:00
|
|
|
if(node.turns == 0 && node.coord != hero->visitablePos())
|
2021-05-16 13:09:49 +02:00
|
|
|
{
|
|
|
|
auto targetNode = cb->getPathsInfo(hero.get())->getPathInfo(node.coord);
|
|
|
|
|
|
|
|
if(!targetNode->accessible || targetNode->turns != 0)
|
|
|
|
{
|
|
|
|
logAi->error(
|
2021-05-16 13:13:48 +02:00
|
|
|
"Enable to complete chain. Expected hero %s to arive to %s in 0 turns but he can not do this",
|
2021-05-16 13:09:49 +02:00
|
|
|
hero.name,
|
2021-05-16 13:13:48 +02:00
|
|
|
node.coord.toString());
|
2021-05-16 13:09:49 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:13:48 +02:00
|
|
|
if(hero->movement)
|
2021-05-16 13:13:40 +02:00
|
|
|
{
|
2021-05-16 13:13:48 +02:00
|
|
|
try
|
2021-05-16 13:13:40 +02:00
|
|
|
{
|
2021-05-16 13:13:48 +02:00
|
|
|
Goals::VisitTile(node.coord).sethero(hero).accept(ai);
|
|
|
|
}
|
|
|
|
catch(cannotFulfillGoalException)
|
|
|
|
{
|
|
|
|
if(hero->movement > 0)
|
2021-05-16 13:13:40 +02:00
|
|
|
{
|
2021-05-16 13:13:48 +02:00
|
|
|
CGPath path;
|
|
|
|
bool isOk = cb->getPathsInfo(hero.get())->getPath(path, node.coord);
|
|
|
|
|
|
|
|
if(isOk && path.nodes.back().turns > 0)
|
|
|
|
{
|
|
|
|
logAi->warn("Hero %s has %d mp which is not enough to continue his way towards %s.", hero.name, hero->movement, node.coord.toString());
|
2021-05-16 13:13:40 +02:00
|
|
|
|
2021-05-16 13:13:56 +02:00
|
|
|
ai->nullkiller->lockHero(hero.get(), HeroLockedReason::HERO_CHAIN);
|
2021-05-16 13:13:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-05-16 13:13:40 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:13:48 +02:00
|
|
|
throw;
|
|
|
|
}
|
2021-05-16 13:13:40 +02:00
|
|
|
}
|
2021-05-15 21:02:27 +02:00
|
|
|
}
|
2021-05-15 20:57:27 +02:00
|
|
|
|
2021-05-16 13:19:07 +02:00
|
|
|
if(node.coord == hero->visitablePos())
|
|
|
|
continue;
|
|
|
|
|
2021-05-16 13:09:49 +02:00
|
|
|
if(node.turns == 0)
|
|
|
|
{
|
|
|
|
logAi->error(
|
|
|
|
"Enable to complete chain. Expected hero %s to arive to %s but he is at %s",
|
|
|
|
hero.name,
|
|
|
|
node.coord.toString(),
|
|
|
|
hero->visitablePos().toString());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2021-05-16 13:13:56 +02:00
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
// no exception means we were not able to rich the tile
|
2021-05-16 13:13:56 +02:00
|
|
|
ai->nullkiller->lockHero(hero.get(), HeroLockedReason::HERO_CHAIN);
|
2021-05-15 20:56:31 +02:00
|
|
|
blockedIndexes.insert(node.parentIndex);
|
|
|
|
}
|
|
|
|
catch(goalFulfilledException)
|
|
|
|
{
|
|
|
|
if(!hero)
|
|
|
|
{
|
|
|
|
logAi->debug("Hero %s was killed while attempting to rich %s", hero.name, node.coord.toString());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ExecuteHeroChain::name() const
|
|
|
|
{
|
2021-05-16 13:15:03 +02:00
|
|
|
return "ExecuteHeroChain " + targetName + " by " + chainPath.targetHero->name;
|
2021-05-15 20:56:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ExecuteHeroChain::completeMessage() const
|
|
|
|
{
|
|
|
|
return "Hero chain completed";
|
2021-05-15 21:02:52 +02:00
|
|
|
}
|