|
@@ -47,151 +47,171 @@ namespace AIPathfinding
|
|
|
{
|
|
|
if(nodeStorage->isMovementIneficient(source, destination))
|
|
|
{
|
|
|
+ destination.node->locked = true;
|
|
|
destination.blocked = true;
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
auto blocker = getBlockingReason(source, destination, pathfinderConfig, pathfinderHelper);
|
|
|
-
|
|
|
if(blocker == BlockingReason::NONE)
|
|
|
return;
|
|
|
|
|
|
- if(blocker == BlockingReason::DESTINATION_BLOCKVIS && destination.nodeObject)
|
|
|
+ auto destGuardians = cb->getGuardingCreatures(destination.coord);
|
|
|
+ bool allowBypass = true;
|
|
|
+
|
|
|
+ switch(blocker)
|
|
|
{
|
|
|
- auto enemyHero = destination.nodeHero && destination.heroRelations == PlayerRelations::ENEMIES;
|
|
|
+ case BlockingReason::DESTINATION_GUARDED:
|
|
|
+ allowBypass = bypassDestinationGuards(destGuardians, source, destination, pathfinderConfig, pathfinderHelper);
|
|
|
|
|
|
- if(!enemyHero && !isObjectRemovable(destination.nodeObject))
|
|
|
- {
|
|
|
- if(nodeStorage->getHero(destination.node) == destination.nodeHero)
|
|
|
- return;
|
|
|
+ break;
|
|
|
|
|
|
- destination.blocked = true;
|
|
|
- }
|
|
|
+ case BlockingReason::DESTINATION_BLOCKVIS:
|
|
|
+ allowBypass = destination.nodeObject && bypassRemovableObject(source, destination, pathfinderConfig, pathfinderHelper);
|
|
|
+
|
|
|
+ if(allowBypass && destGuardians.size())
|
|
|
+ allowBypass = bypassDestinationGuards(destGuardians, source, destination, pathfinderConfig, pathfinderHelper);
|
|
|
|
|
|
- if(destination.nodeObject->ID == Obj::QUEST_GUARD || destination.nodeObject->ID == Obj::BORDERGUARD)
|
|
|
- {
|
|
|
- auto questObj = dynamic_cast<const IQuestObject *>(destination.nodeObject);
|
|
|
- auto nodeHero = pathfinderHelper->hero;
|
|
|
-
|
|
|
- if(!destination.nodeObject->wasVisited(nodeHero->tempOwner)
|
|
|
- || !questObj->checkQuest(nodeHero))
|
|
|
- {
|
|
|
- nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
|
|
|
- {
|
|
|
- auto questInfo = QuestInfo(questObj->quest, destination.nodeObject, destination.coord);
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
- node->specialAction.reset(new QuestAction(questInfo));
|
|
|
- });
|
|
|
- }
|
|
|
- }
|
|
|
+ destination.blocked = !allowBypass || nodeStorage->isDistanceLimitReached(source, destination);
|
|
|
+ destination.node->locked = !allowBypass;
|
|
|
+ }
|
|
|
|
|
|
- return;
|
|
|
- }
|
|
|
+ bool AIMovementAfterDestinationRule::bypassRemovableObject(
|
|
|
+ const PathNodeInfo & source,
|
|
|
+ CDestinationNodeInfo & destination,
|
|
|
+ const PathfinderConfig * pathfinderConfig,
|
|
|
+ CPathfinderHelper * pathfinderHelper) const
|
|
|
+ {
|
|
|
+ auto enemyHero = destination.nodeHero && destination.heroRelations == PlayerRelations::ENEMIES;
|
|
|
|
|
|
- if(blocker == BlockingReason::DESTINATION_VISIT)
|
|
|
+ if(!enemyHero && !isObjectRemovable(destination.nodeObject))
|
|
|
{
|
|
|
- return;
|
|
|
+ if(nodeStorage->getHero(destination.node) == destination.nodeHero)
|
|
|
+ return true;
|
|
|
+
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
- if(blocker == BlockingReason::DESTINATION_GUARDED)
|
|
|
+ if(destination.nodeObject->ID == Obj::QUEST_GUARD || destination.nodeObject->ID == Obj::BORDERGUARD)
|
|
|
{
|
|
|
- auto srcGuardians = cb->getGuardingCreatures(source.coord);
|
|
|
- auto destGuardians = cb->getGuardingCreatures(destination.coord);
|
|
|
+ auto questObj = dynamic_cast<const IQuestObject *>(destination.nodeObject);
|
|
|
+ auto nodeHero = pathfinderHelper->hero;
|
|
|
|
|
|
- if(destGuardians.empty())
|
|
|
+ if(!destination.nodeObject->wasVisited(nodeHero->tempOwner)
|
|
|
+ || !questObj->checkQuest(nodeHero))
|
|
|
{
|
|
|
- destination.blocked = true;
|
|
|
+ nodeStorage->updateAINode(destination.node, [&](AIPathNode * node)
|
|
|
+ {
|
|
|
+ auto questInfo = QuestInfo(questObj->quest, destination.nodeObject, destination.coord);
|
|
|
|
|
|
- return;
|
|
|
+ node->specialAction.reset(new QuestAction(questInfo));
|
|
|
+ });
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- vstd::erase_if(destGuardians, [&](const CGObjectInstance * destGuard) -> bool
|
|
|
- {
|
|
|
- return vstd::contains(srcGuardians, destGuard);
|
|
|
- });
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
- auto guardsAlreadyBypassed = destGuardians.empty() && srcGuardians.size();
|
|
|
- auto srcNode = nodeStorage->getAINode(source.node);
|
|
|
- if(guardsAlreadyBypassed && srcNode->actor->allowBattle)
|
|
|
- {
|
|
|
-#ifdef VCMI_TRACE_PATHFINDER
|
|
|
- logAi->trace(
|
|
|
- "Bypass guard at destination while moving %s -> %s",
|
|
|
- source.coord.toString(),
|
|
|
- destination.coord.toString());
|
|
|
-#endif
|
|
|
+ bool AIMovementAfterDestinationRule::bypassDestinationGuards(
|
|
|
+ std::vector<const CGObjectInstance *> destGuardians,
|
|
|
+ const PathNodeInfo & source,
|
|
|
+ CDestinationNodeInfo & destination,
|
|
|
+ const PathfinderConfig * pathfinderConfig,
|
|
|
+ CPathfinderHelper * pathfinderHelper) const
|
|
|
+ {
|
|
|
+ auto srcGuardians = cb->getGuardingCreatures(source.coord);
|
|
|
|
|
|
- return;
|
|
|
- }
|
|
|
+ if(destGuardians.empty())
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- const AIPathNode * destNode = nodeStorage->getAINode(destination.node);
|
|
|
- auto battleNodeOptional = nodeStorage->getOrCreateNode(
|
|
|
- destination.coord,
|
|
|
- destination.node->layer,
|
|
|
- destNode->actor->battleActor);
|
|
|
+ auto srcNode = nodeStorage->getAINode(source.node);
|
|
|
|
|
|
- if(!battleNodeOptional)
|
|
|
- {
|
|
|
+ vstd::erase_if(destGuardians, [&](const CGObjectInstance * destGuard) -> bool
|
|
|
+ {
|
|
|
+ return vstd::contains(srcGuardians, destGuard);
|
|
|
+ });
|
|
|
+
|
|
|
+ auto guardsAlreadyBypassed = destGuardians.empty() && srcGuardians.size();
|
|
|
+
|
|
|
+ if(guardsAlreadyBypassed && srcNode->actor->allowBattle)
|
|
|
+ {
|
|
|
#ifdef VCMI_TRACE_PATHFINDER
|
|
|
- logAi->trace(
|
|
|
- "Can not allocate battle node while moving %s -> %s",
|
|
|
- source.coord.toString(),
|
|
|
- destination.coord.toString());
|
|
|
+ logAi->trace(
|
|
|
+ "Bypass guard at destination while moving %s -> %s",
|
|
|
+ source.coord.toString(),
|
|
|
+ destination.coord.toString());
|
|
|
#endif
|
|
|
|
|
|
- destination.blocked = true;
|
|
|
-
|
|
|
- return;
|
|
|
- }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
- AIPathNode * battleNode = battleNodeOptional.get();
|
|
|
+ const AIPathNode * destNode = nodeStorage->getAINode(destination.node);
|
|
|
+ auto battleNodeOptional = nodeStorage->getOrCreateNode(
|
|
|
+ destination.coord,
|
|
|
+ destination.node->layer,
|
|
|
+ destNode->actor->battleActor);
|
|
|
|
|
|
- if(battleNode->locked)
|
|
|
- {
|
|
|
+ if(!battleNodeOptional)
|
|
|
+ {
|
|
|
#ifdef VCMI_TRACE_PATHFINDER
|
|
|
- logAi->trace(
|
|
|
- "Block bypass guard at destination while moving %s -> %s",
|
|
|
- source.coord.toString(),
|
|
|
- destination.coord.toString());
|
|
|
+ logAi->trace(
|
|
|
+ "Can not allocate battle node while moving %s -> %s",
|
|
|
+ source.coord.toString(),
|
|
|
+ destination.coord.toString());
|
|
|
#endif
|
|
|
- destination.blocked = true;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- return;
|
|
|
- }
|
|
|
+ AIPathNode * battleNode = battleNodeOptional.get();
|
|
|
|
|
|
- auto hero = nodeStorage->getHero(source.node);
|
|
|
- auto danger = nodeStorage->evaluateDanger(destination.coord, hero);
|
|
|
- double actualArmyValue = srcNode->actor->armyValue - srcNode->armyLoss;
|
|
|
- double loss = nodeStorage->evaluateArmyLoss(hero, actualArmyValue, danger);
|
|
|
+ if(battleNode->locked)
|
|
|
+ {
|
|
|
+#ifdef VCMI_TRACE_PATHFINDER
|
|
|
+ logAi->trace(
|
|
|
+ "Block bypass guard at destination while moving %s -> %s",
|
|
|
+ source.coord.toString(),
|
|
|
+ destination.coord.toString());
|
|
|
+#endif
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- if(loss < actualArmyValue)
|
|
|
- {
|
|
|
- destination.node = battleNode;
|
|
|
- nodeStorage->commit(destination, source);
|
|
|
+ auto hero = nodeStorage->getHero(source.node);
|
|
|
+ auto danger = nodeStorage->evaluateDanger(destination.coord, hero, true);
|
|
|
+ double actualArmyValue = srcNode->actor->armyValue - srcNode->armyLoss;
|
|
|
+ double loss = nodeStorage->evaluateArmyLoss(hero, actualArmyValue, danger);
|
|
|
|
|
|
- battleNode->armyLoss += loss;
|
|
|
+ if(loss < actualArmyValue)
|
|
|
+ {
|
|
|
+ destination.node = battleNode;
|
|
|
+ nodeStorage->commit(destination, source);
|
|
|
|
|
|
- vstd::amax(battleNode->danger, danger);
|
|
|
+ battleNode->armyLoss += loss;
|
|
|
|
|
|
- battleNode->specialAction = std::make_shared<BattleAction>(destination.coord);
|
|
|
+ vstd::amax(battleNode->danger, danger);
|
|
|
|
|
|
- if(source.nodeObject && isObjectRemovable(source.nodeObject))
|
|
|
- {
|
|
|
- battleNode->theNodeBefore = source.node;
|
|
|
- }
|
|
|
+ battleNode->specialAction = std::make_shared<BattleAction>(destination.coord);
|
|
|
+
|
|
|
+ if(source.nodeObject && isObjectRemovable(source.nodeObject))
|
|
|
+ {
|
|
|
+ battleNode->theNodeBefore = source.node;
|
|
|
+ }
|
|
|
|
|
|
#ifdef VCMI_TRACE_PATHFINDER
|
|
|
- logAi->trace(
|
|
|
- "Begin bypass guard at destination with danger %s while moving %s -> %s",
|
|
|
- std::to_string(danger),
|
|
|
- source.coord.toString(),
|
|
|
- destination.coord.toString());
|
|
|
+ logAi->trace(
|
|
|
+ "Begin bypass guard at destination with danger %s while moving %s -> %s",
|
|
|
+ std::to_string(danger),
|
|
|
+ source.coord.toString(),
|
|
|
+ destination.coord.toString());
|
|
|
#endif
|
|
|
- return;
|
|
|
- }
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
- destination.blocked = true;
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|