2021-05-15 18:22:44 +02:00
|
|
|
/*
|
|
|
|
* AILayerTransitionRule.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 "AILayerTransitionRule.h"
|
2020-05-04 17:58:43 +02:00
|
|
|
#include "../../Engine/Nullkiller.h"
|
2021-05-15 18:22:44 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
2021-05-15 18:22:44 +02:00
|
|
|
namespace AIPathfinding
|
|
|
|
{
|
2020-05-04 17:58:43 +02:00
|
|
|
AILayerTransitionRule::AILayerTransitionRule(CPlayerSpecificInfoCallback * cb, Nullkiller * ai, std::shared_ptr<AINodeStorage> nodeStorage)
|
2021-05-15 18:22:44 +02:00
|
|
|
:cb(cb), ai(ai), nodeStorage(nodeStorage)
|
|
|
|
{
|
|
|
|
setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AILayerTransitionRule::process(
|
|
|
|
const PathNodeInfo & source,
|
|
|
|
CDestinationNodeInfo & destination,
|
|
|
|
const PathfinderConfig * pathfinderConfig,
|
|
|
|
CPathfinderHelper * pathfinderHelper) const
|
|
|
|
{
|
|
|
|
LayerTransitionRule::process(source, destination, pathfinderConfig, pathfinderHelper);
|
|
|
|
|
|
|
|
if(!destination.blocked)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(source.node->layer == EPathfindingLayer::LAND && destination.node->layer == EPathfindingLayer::SAIL)
|
|
|
|
{
|
|
|
|
std::shared_ptr<const VirtualBoatAction> virtualBoat = findVirtualBoat(destination, source);
|
|
|
|
|
|
|
|
if(virtualBoat && tryEmbarkVirtualBoat(destination, source, virtualBoat))
|
|
|
|
{
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_PATHFINDER_TRACE_LEVEL >= 1
|
2021-05-15 18:22:44 +02:00
|
|
|
logAi->trace("Embarking to virtual boat while moving %s -> %s!", source.coord.toString(), destination.coord.toString());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AILayerTransitionRule::setup()
|
|
|
|
{
|
|
|
|
std::vector<const IShipyard *> shipyards;
|
|
|
|
|
|
|
|
for(const CGTownInstance * t : cb->getTownsInfo())
|
|
|
|
{
|
2023-04-22 17:37:27 +02:00
|
|
|
if(t->hasBuilt(BuildingID::SHIPYARD))
|
2021-05-15 18:22:44 +02:00
|
|
|
shipyards.push_back(t);
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:58:43 +02:00
|
|
|
for(const CGObjectInstance * obj : ai->memory->visitableObjs)
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2023-04-22 17:37:27 +02:00
|
|
|
if(obj->ID != Obj::TOWN) //towns were handled in the previous loop
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
|
|
|
if(const IShipyard * shipyard = IShipyard::castFrom(obj))
|
|
|
|
shipyards.push_back(shipyard);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const IShipyard * shipyard : shipyards)
|
|
|
|
{
|
|
|
|
if(shipyard->shipyardStatus() == IShipyard::GOOD)
|
|
|
|
{
|
|
|
|
int3 boatLocation = shipyard->bestLocation();
|
2021-05-16 14:08:56 +02:00
|
|
|
virtualBoats[boatLocation] = std::make_shared<BuildBoatAction>(cb, shipyard);
|
2021-05-15 18:22:44 +02:00
|
|
|
logAi->debug("Virtual boat added at %s", boatLocation.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:22:49 +02:00
|
|
|
for(const CGHeroInstance * hero : nodeStorage->getAllHeroes())
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2021-05-15 18:22:49 +02:00
|
|
|
auto summonBoatSpell = SpellID(SpellID::SUMMON_BOAT).toSpell();
|
|
|
|
|
|
|
|
if(hero->canCastThisSpell(summonBoatSpell)
|
|
|
|
&& hero->getSpellSchoolLevel(summonBoatSpell) >= SecSkillLevel::ADVANCED)
|
|
|
|
{
|
|
|
|
// TODO: For lower school level we might need to check the existance of some boat
|
|
|
|
summonableVirtualBoats[hero] = std::make_shared<SummonBoatAction>();
|
|
|
|
}
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const VirtualBoatAction> AILayerTransitionRule::findVirtualBoat(
|
|
|
|
CDestinationNodeInfo & destination,
|
|
|
|
const PathNodeInfo & source) const
|
|
|
|
{
|
|
|
|
std::shared_ptr<const VirtualBoatAction> virtualBoat;
|
|
|
|
|
|
|
|
if(vstd::contains(virtualBoats, destination.coord))
|
|
|
|
{
|
|
|
|
virtualBoat = virtualBoats.at(destination.coord);
|
|
|
|
}
|
2021-05-15 18:22:49 +02:00
|
|
|
else
|
2021-05-15 18:22:44 +02:00
|
|
|
{
|
2021-05-15 18:22:49 +02:00
|
|
|
const CGHeroInstance * hero = nodeStorage->getHero(source.node);
|
|
|
|
|
|
|
|
if(vstd::contains(summonableVirtualBoats, hero)
|
2021-05-16 13:38:53 +02:00
|
|
|
&& summonableVirtualBoats.at(hero)->canAct(nodeStorage->getAINode(source.node)))
|
2021-05-15 18:22:49 +02:00
|
|
|
{
|
|
|
|
virtualBoat = summonableVirtualBoats.at(hero);
|
|
|
|
}
|
2021-05-15 18:22:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return virtualBoat;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AILayerTransitionRule::tryEmbarkVirtualBoat(
|
|
|
|
CDestinationNodeInfo & destination,
|
|
|
|
const PathNodeInfo & source,
|
|
|
|
std::shared_ptr<const VirtualBoatAction> virtualBoat) const
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
|
|
|
|
{
|
|
|
|
auto boatNodeOptional = nodeStorage->getOrCreateNode(
|
|
|
|
node->coord,
|
|
|
|
node->layer,
|
2021-05-15 18:22:49 +02:00
|
|
|
virtualBoat->getActor(node->actor));
|
2021-05-15 18:22:44 +02:00
|
|
|
|
|
|
|
if(boatNodeOptional)
|
|
|
|
{
|
2023-04-16 19:42:56 +02:00
|
|
|
AIPathNode * boatNode = boatNodeOptional.value();
|
2021-05-15 18:22:44 +02:00
|
|
|
|
|
|
|
if(boatNode->action == CGPathNode::UNKNOWN)
|
|
|
|
{
|
2022-12-26 20:17:39 +02:00
|
|
|
boatNode->addSpecialAction(virtualBoat);
|
2021-05-15 18:22:44 +02:00
|
|
|
destination.blocked = false;
|
|
|
|
destination.action = CGPathNode::ENodeAction::EMBARK;
|
|
|
|
destination.node = boatNode;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_PATHFINDER_TRACE_LEVEL >= 1
|
2021-05-15 18:22:44 +02:00
|
|
|
logAi->trace(
|
|
|
|
"Special transition node already allocated. Blocked moving %s -> %s",
|
|
|
|
source.coord.toString(),
|
|
|
|
destination.coord.toString());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logAi->debug(
|
|
|
|
"Can not allocate special transition node while moving %s -> %s",
|
|
|
|
source.coord.toString(),
|
|
|
|
destination.coord.toString());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 20:01:07 +02:00
|
|
|
|
|
|
|
}
|