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

200 lines
4.9 KiB
C++
Raw Normal View History

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"
2021-05-16 14:39:38 +02:00
#include "../AIGateway.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;
2021-05-16 14:39:38 +02:00
extern boost::thread_specific_ptr<AIGateway> ai;
2021-05-15 20:56:31 +02:00
using namespace Goals;
ExecuteHeroChain::ExecuteHeroChain(const AIPath & path, const CGObjectInstance * obj)
:ElementarGoal(Goals::EXECUTE_HERO_CHAIN), chainPath(path), closestWayRatio(1)
2021-05-15 20:56:31 +02:00
{
hero = path.targetHero;
tile = path.targetTile();
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
{
2021-05-16 13:38:53 +02:00
return tile == other.tile
&& chainPath.targetHero == other.chainPath.targetHero
&& chainPath.nodes.size() == other.chainPath.nodes.size()
&& chainPath.chainMask == other.chainPath.chainMask;
2021-05-15 20:56:31 +02:00
}
2021-05-16 14:39:38 +02:00
void ExecuteHeroChain::accept(AIGateway * ai)
2021-05-15 20:56:31 +02:00
{
logAi->debug("Executing hero chain towards %s. Path %s", targetName, chainPath.toString());
2021-05-15 20:56:31 +02:00
ai->nullkiller->setActive(chainPath.targetHero, tile);
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];
const CGHeroInstance * hero = node.targetHero;
HeroPtr heroPtr = hero;
2021-05-15 20:56:31 +02:00
2021-05-16 14:39:38 +02:00
if(node.parentIndex >= i)
{
logAi->error("Invalid parentIndex while executing node " + node.coord.toString());
}
2021-05-15 20:56:31 +02:00
if(vstd::contains(blockedIndexes, i))
{
blockedIndexes.insert(node.parentIndex);
ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
2021-05-15 20:56:31 +02:00
continue;
}
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
{
if(hero->movement)
{
ai->nullkiller->setActive(hero, node.coord);
2021-05-15 20:56:31 +02:00
2021-05-15 21:02:57 +02:00
if(node.specialAction)
{
2021-05-16 13:38:53 +02:00
if(node.actionIsBlocked)
2021-05-16 13:07:54 +02:00
{
throw cannotFulfillGoalException("Path is nondeterministic.");
2021-05-16 13:07:54 +02:00
}
2021-05-16 13:38:53 +02:00
node.specialAction->execute(hero);
if(!heroPtr.validAndSet())
{
logAi->error("Hero %s was lost trying to execute special action. Exit hero chain.", heroPtr.name);
return;
}
2021-05-15 21:02:57 +02:00
}
if(node.turns == 0 && node.coord != hero->visitablePos())
2021-05-16 13:09:49 +02:00
{
auto targetNode = cb->getPathsInfo(hero)->getPathInfo(node.coord);
2021-05-16 13:09:49 +02:00
if(targetNode->accessible == CGPathNode::EAccessibility::NOT_SET
|| targetNode->accessible == CGPathNode::EAccessibility::BLOCKED
|| targetNode->accessible == CGPathNode::EAccessibility::FLYABLE
|| targetNode->turns != 0)
2021-05-16 13:09:49 +02:00
{
logAi->error(
"Enable to complete chain. Expected hero %s to arive to %s in 0 turns but he can not do this",
hero->name,
node.coord.toString());
2021-05-16 13:09:49 +02:00
return;
}
}
if(hero->movement)
{
try
{
if(moveHeroToTile(hero, node.coord))
{
continue;
}
}
catch(cannotFulfillGoalException)
{
if(!heroPtr.validAndSet())
{
logAi->error("Hero %s was lost. Exit hero chain.", heroPtr.name);
return;
}
if(hero->movement > 0)
{
CGPath path;
bool isOk = cb->getPathsInfo(hero)->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());
ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
return;
}
}
throw;
}
}
}
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,
2021-05-16 13:09:49 +02:00
node.coord.toString(),
hero->visitablePos().toString());
return;
}
// no exception means we were not able to rich the tile
ai->nullkiller->lockHero(hero, HeroLockedReason::HERO_CHAIN);
2021-05-15 20:56:31 +02:00
blockedIndexes.insert(node.parentIndex);
}
catch(goalFulfilledException)
{
if(!heroPtr.validAndSet())
2021-05-15 20:56:31 +02:00
{
logAi->debug("Hero %s was killed while attempting to rich %s", heroPtr.name, node.coord.toString());
2021-05-15 20:56:31 +02:00
return;
}
}
}
}
std::string ExecuteHeroChain::toString() const
2021-05-15 20:56:31 +02:00
{
return "ExecuteHeroChain " + targetName + " by " + chainPath.targetHero->name;
}
bool ExecuteHeroChain::moveHeroToTile(const CGHeroInstance * hero, const int3 & tile)
{
2021-05-16 13:38:53 +02:00
if(tile == hero->visitablePos() && cb->getVisitableObjs(hero->visitablePos()).size() < 2)
{
2021-05-16 13:38:53 +02:00
logAi->warn("Why do I want to move hero %s to tile %s? Already standing on that tile! ", hero->name, tile.toString());
return true;
}
return ai->moveHeroToTile(tile, hero);
2021-05-15 21:02:52 +02:00
}