123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- /*
- * AINodeStorage.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 "AINodeStorage.h"
- #include "Actions/TownPortalAction.h"
- #include "../Goals/Goals.h"
- #include "../../../CCallback.h"
- #include "../../../lib/mapping/CMap.h"
- #include "../../../lib/mapObjects/MapObjects.h"
- #include "../../../lib/PathfinderUtil.h"
- #include "../../../lib/CPlayerState.h"
- AINodeStorage::AINodeStorage(const int3 & Sizes)
- : sizes(Sizes)
- {
- nodes.resize(boost::extents[sizes.x][sizes.y][sizes.z][EPathfindingLayer::NUM_LAYERS][NUM_CHAINS]);
- dangerEvaluator.reset(new FuzzyHelper());
- }
- AINodeStorage::~AINodeStorage() = default;
- void AINodeStorage::initialize(const PathfinderOptions & options, const CGameState * gs)
- {
- if(heroChainPass)
- return;
- //TODO: fix this code duplication with NodeStorage::initialize, problem is to keep `resetTile` inline
- int3 pos;
- const PlayerColor player = ai->playerID;
- const int3 sizes = gs->getMapSize();
- const auto & fow = static_cast<const CGameInfoCallback *>(gs)->getPlayerTeam(player)->fogOfWarMap;
- //make 200% sure that these are loop invariants (also a bit shorter code), let compiler do the rest(loop unswitching)
- const bool useFlying = options.useFlying;
- const bool useWaterWalking = options.useWaterWalking;
- for(pos.x=0; pos.x < sizes.x; ++pos.x)
- {
- for(pos.y=0; pos.y < sizes.y; ++pos.y)
- {
- for(pos.z=0; pos.z < sizes.z; ++pos.z)
- {
- const TerrainTile * tile = &gs->map->getTile(pos);
- switch(tile->terType)
- {
- case ETerrainType::ROCK:
- break;
- case ETerrainType::WATER:
- resetTile(pos, ELayer::SAIL, PathfinderUtil::evaluateAccessibility<ELayer::SAIL>(pos, tile, fow, player, gs));
- if(useFlying)
- resetTile(pos, ELayer::AIR, PathfinderUtil::evaluateAccessibility<ELayer::AIR>(pos, tile, fow, player, gs));
- if(useWaterWalking)
- resetTile(pos, ELayer::WATER, PathfinderUtil::evaluateAccessibility<ELayer::WATER>(pos, tile, fow, player, gs));
- break;
- default:
- resetTile(pos, ELayer::LAND, PathfinderUtil::evaluateAccessibility<ELayer::LAND>(pos, tile, fow, player, gs));
- if(useFlying)
- resetTile(pos, ELayer::AIR, PathfinderUtil::evaluateAccessibility<ELayer::AIR>(pos, tile, fow, player, gs));
- break;
- }
- }
- }
- }
- }
- void AINodeStorage::clear()
- {
- actors.clear();
- heroChainPass = false;
- }
- const AIPathNode * AINodeStorage::getAINode(const CGPathNode * node) const
- {
- return static_cast<const AIPathNode *>(node);
- }
- void AINodeStorage::updateAINode(CGPathNode * node, std::function<void(AIPathNode *)> updater)
- {
- auto aiNode = static_cast<AIPathNode *>(node);
- updater(aiNode);
- }
- boost::optional<AIPathNode *> AINodeStorage::getOrCreateNode(
- const int3 & pos,
- const EPathfindingLayer layer,
- const ChainActor * actor)
- {
- auto chains = nodes[pos.x][pos.y][pos.z][layer];
- for(AIPathNode & node : chains)
- {
- if(node.actor == actor)
- {
- return &node;
- }
- if(!node.actor)
- {
- node.actor = actor;
- return &node;
- }
- }
- return boost::none;
- }
- std::vector<CGPathNode *> AINodeStorage::getInitialNodes()
- {
- if(heroChainPass)
- return heroChain;
- std::vector<CGPathNode *> initialNodes;
- for(auto actorPtr : actors)
- {
- ChainActor * actor = actorPtr.get();
- AIPathNode * initialNode =
- getOrCreateNode(actor->initialPosition, actor->layer, actor)
- .get();
- initialNode->turns = actor->initialTurn;
- initialNode->moveRemains = actor->initialMovement;
- initialNode->danger = 0;
- initialNode->cost = 0.0;
- if(actor->isMovable)
- {
- initialNodes.push_back(initialNode);
- }
- else
- {
- initialNode->locked = true;
- }
- }
- return initialNodes;
- }
- void AINodeStorage::resetTile(const int3 & coord, EPathfindingLayer layer, CGPathNode::EAccessibility accessibility)
- {
- for(int i = 0; i < NUM_CHAINS; i++)
- {
- AIPathNode & heroNode = nodes[coord.x][coord.y][coord.z][layer][i];
- heroNode.actor = nullptr;
- heroNode.danger = 0;
- heroNode.manaCost = 0;
- heroNode.specialAction.reset();
- heroNode.armyLoss = 0;
- heroNode.chainOther = nullptr;
- heroNode.update(coord, layer, accessibility);
- }
- }
- void AINodeStorage::commit(CDestinationNodeInfo & destination, const PathNodeInfo & source)
- {
- const AIPathNode * srcNode = getAINode(source.node);
- updateAINode(destination.node, [&](AIPathNode * dstNode)
- {
- commit(dstNode, srcNode, destination.action, destination.turn, destination.movementLeft, destination.cost);
- if(dstNode->specialAction && dstNode->actor)
- {
- dstNode->specialAction->applyOnDestination(dstNode->actor->hero, destination, source, dstNode, srcNode);
- }
- });
- }
- void AINodeStorage::commit(
- AIPathNode * destination,
- const AIPathNode * source,
- CGPathNode::ENodeAction action,
- int turn,
- int movementLeft,
- float cost) const
- {
- destination->action = action;
- destination->cost = cost;
- destination->moveRemains = movementLeft;
- destination->turns = turn;
- destination->armyLoss = source->armyLoss;
- destination->manaCost = source->manaCost;
- destination->danger = source->danger;
- destination->theNodeBefore = source->theNodeBefore;
- }
- std::vector<CGPathNode *> AINodeStorage::calculateNeighbours(
- const PathNodeInfo & source,
- const PathfinderConfig * pathfinderConfig,
- const CPathfinderHelper * pathfinderHelper)
- {
- std::vector<CGPathNode *> neighbours;
- neighbours.reserve(16);
- const AIPathNode * srcNode = getAINode(source.node);
- auto accessibleNeighbourTiles = pathfinderHelper->getNeighbourTiles(source);
- for(auto & neighbour : accessibleNeighbourTiles)
- {
- for(EPathfindingLayer i = EPathfindingLayer::LAND; i <= EPathfindingLayer::AIR; i.advance(1))
- {
- auto nextNode = getOrCreateNode(neighbour, i, srcNode->actor);
- if(!nextNode || nextNode.get()->accessible == CGPathNode::NOT_SET)
- continue;
- neighbours.push_back(nextNode.get());
- }
- }
-
- return neighbours;
- }
- bool AINodeStorage::calculateHeroChain()
- {
- heroChainPass = true;
- heroChain.resize(0);
- foreach_tile_pos([&](const int3 & pos) {
- auto layer = EPathfindingLayer::LAND;
- auto chains = nodes[pos.x][pos.y][pos.z][layer];
- for(AIPathNode & node : chains)
- {
- if(node.locked && node.turns < 1)
- addHeroChain(&node);
- }
- });
- return heroChain.size();
- }
- void AINodeStorage::addHeroChain(AIPathNode * srcNode)
- {
- auto chains = nodes[srcNode->coord.x][srcNode->coord.y][srcNode->coord.z][srcNode->layer];
- for(AIPathNode & node : chains)
- {
- if(!node.locked || !node.actor || node.action == CGPathNode::ENodeAction::UNKNOWN && node.actor->hero)
- {
- continue;
- }
- addHeroChain(srcNode, &node);
- addHeroChain(&node, srcNode);
- }
- }
- void AINodeStorage::addHeroChain(AIPathNode * carrier, AIPathNode * other)
- {
- if(carrier->actor->canExchange(other->actor))
- {
- bool hasLessMp = carrier->turns > other->turns || carrier->moveRemains < other->moveRemains;
- bool hasLessExperience = carrier->actor->hero->exp < other->actor->hero->exp;
- #ifdef VCMI_TRACE_PATHFINDER
- logAi->trace("Check hero exhange at %s, %s -> %s", carrier->coord.toString(), other->actor->hero->name, carrier->actor->hero->name);
- #endif
- if(hasLessMp && hasLessExperience)
- return;
- auto newActor = carrier->actor->exchange(other->actor);
- auto chainNodeOptional = getOrCreateNode(carrier->coord, carrier->layer, newActor);
- if(!chainNodeOptional)
- return;
- auto chainNode = chainNodeOptional.get();
- if(chainNode->locked)
- return;
- #ifdef VCMI_TRACE_PATHFINDER
- logAi->trace("Hero exhange at %s, %s -> %s", carrier->coord.toString(), other->actor->hero->name, carrier->actor->hero->name);
- #endif
-
- commitExchange(chainNode, carrier, other);
- heroChain.push_back(chainNode);
- }
- }
- void AINodeStorage::commitExchange(
- AIPathNode * exchangeNode,
- AIPathNode * carrierParentNode,
- AIPathNode * otherParentNode) const
- {
- auto carrierActor = carrierParentNode->actor;
- auto exchangeActor = exchangeNode->actor;
- auto otherActor = otherParentNode->actor;
- auto armyLoss = carrierParentNode->armyLoss + otherParentNode->armyLoss;
- auto turns = carrierParentNode->turns;
- auto cost = carrierParentNode->cost;
- auto movementLeft = carrierParentNode->moveRemains;
- if(carrierParentNode->turns < otherParentNode->turns)
- {
- int moveRemains = exchangeActor->hero->maxMovePoints(exchangeNode->layer);
- float waitingCost = otherParentNode->turns - carrierParentNode->turns - 1
- + carrierParentNode->moveRemains / (float)moveRemains;
- turns = otherParentNode->turns;
- cost = waitingCost;
- movementLeft = moveRemains;
- }
-
- if(exchangeNode->turns != 0xFF && exchangeNode->cost < cost)
- return;
- #ifdef VCMI_TRACE_PATHFINDER
- logAi->trace(
- "Accepted hero exhange at %s, carrier %s, mp cost %f",
- exchangeNode->coord.toString(),
- carrierActor->hero->name,
- cost);
- #endif
-
- commit(exchangeNode, carrierParentNode, carrierParentNode->action, turns, movementLeft, cost);
- exchangeNode->theNodeBefore = carrierParentNode;
- exchangeNode->chainOther = otherParentNode;
- exchangeNode->armyLoss = armyLoss;
- exchangeNode->manaCost = carrierParentNode->manaCost;
- }
- const CGHeroInstance * AINodeStorage::getHero(const CGPathNode * node) const
- {
- auto aiNode = getAINode(node);
- return aiNode->actor->hero;
- }
- const std::set<const CGHeroInstance *> AINodeStorage::getAllHeroes() const
- {
- std::set<const CGHeroInstance *> heroes;
- for(auto actor : actors)
- {
- if(actor->hero)
- heroes.insert(actor->hero);
- }
- return heroes;
- }
- void AINodeStorage::setHeroes(std::vector<HeroPtr> heroes, const VCAI * _ai)
- {
- cb = _ai->myCb.get();
- ai = _ai;
- for(auto & hero : heroes)
- {
- uint64_t mask = 1 << actors.size();
- actors.push_back(std::make_shared<HeroActor>(hero.get(), mask));
- }
- }
- std::vector<CGPathNode *> AINodeStorage::calculateTeleportations(
- const PathNodeInfo & source,
- const PathfinderConfig * pathfinderConfig,
- const CPathfinderHelper * pathfinderHelper)
- {
- std::vector<CGPathNode *> neighbours;
- if(source.isNodeObjectVisitable())
- {
- auto accessibleExits = pathfinderHelper->getTeleportExits(source);
- auto srcNode = getAINode(source.node);
- for(auto & neighbour : accessibleExits)
- {
- auto node = getOrCreateNode(neighbour, source.node->layer, srcNode->actor);
- if(!node)
- continue;
- neighbours.push_back(node.get());
- }
- }
- if(source.isInitialPosition)
- {
- calculateTownPortalTeleportations(source, neighbours);
- }
- return neighbours;
- }
- void AINodeStorage::calculateTownPortalTeleportations(
- const PathNodeInfo & source,
- std::vector<CGPathNode *> & neighbours)
- {
- SpellID spellID = SpellID::TOWN_PORTAL;
- const CSpell * townPortal = spellID.toSpell();
- auto srcNode = getAINode(source.node);
- auto hero = srcNode->actor->hero;
- if(hero->canCastThisSpell(townPortal) && hero->mana >= hero->getSpellCost(townPortal))
- {
- auto towns = cb->getTownsInfo(false);
- vstd::erase_if(towns, [&](const CGTownInstance * t) -> bool
- {
- return cb->getPlayerRelations(hero->tempOwner, t->tempOwner) == PlayerRelations::ENEMIES;
- });
- if(!towns.size())
- {
- return;
- }
- // TODO: Copy/Paste from TownPortalMechanics
- auto skillLevel = hero->getSpellSchoolLevel(townPortal);
- auto movementCost = GameConstants::BASE_MOVEMENT_COST * (skillLevel >= 3 ? 2 : 3);
- if(hero->movement < movementCost)
- {
- return;
- }
- if(skillLevel < SecSkillLevel::ADVANCED)
- {
- const CGTownInstance * nearestTown = *vstd::minElementByFun(towns, [&](const CGTownInstance * t) -> int
- {
- return hero->visitablePos().dist2dSQ(t->visitablePos());
- });
- towns = std::vector<const CGTownInstance *>{ nearestTown };
- }
- for(const CGTownInstance * targetTown : towns)
- {
- if(targetTown->visitingHero)
- continue;
- auto nodeOptional = getOrCreateNode(targetTown->visitablePos(), EPathfindingLayer::LAND, srcNode->actor->castActor);
- if(nodeOptional)
- {
- #ifdef VCMI_TRACE_PATHFINDER
- logAi->trace("Adding town portal node at %s", targetTown->name);
- #endif
- AIPathNode * node = nodeOptional.get();
- node->theNodeBefore = source.node;
- node->specialAction.reset(new AIPathfinding::TownPortalAction(targetTown));
- node->moveRemains = source.node->moveRemains;
-
- neighbours.push_back(node);
- }
- }
- }
- }
- bool AINodeStorage::hasBetterChain(const PathNodeInfo & source, CDestinationNodeInfo & destination) const
- {
- auto pos = destination.coord;
- auto chains = nodes[pos.x][pos.y][pos.z][EPathfindingLayer::LAND];
- auto destinationNode = getAINode(destination.node);
- for(const AIPathNode & node : chains)
- {
- auto sameNode = node.actor == destinationNode->actor;
- if(sameNode || node.action == CGPathNode::ENodeAction::UNKNOWN)
- {
- continue;
- }
- if(node.danger <= destinationNode->danger && destinationNode->actor == node.actor->battleActor)
- {
- if(node.cost < destinationNode->cost)
- {
- #ifdef VCMI_TRACE_PATHFINDER
- logAi->trace(
- "Block ineficient move %s:->%s, mask=%i, mp diff: %i",
- source.coord.toString(),
- destination.coord.toString(),
- destinationNode->actor->chainMask,
- node.moveRemains - destinationNode->moveRemains);
- #endif
- return true;
- }
- }
- }
- return false;
- }
- bool AINodeStorage::isTileAccessible(const HeroPtr & hero, const int3 & pos, const EPathfindingLayer layer) const
- {
- auto chains = nodes[pos.x][pos.y][pos.z][layer];
- for(const AIPathNode & node : chains)
- {
- if(node.action != CGPathNode::ENodeAction::UNKNOWN
- && node.actor && node.actor->hero == hero.h)
- {
- return true;
- }
- }
- return false;
- }
- std::vector<AIPath> AINodeStorage::getChainInfo(const int3 & pos, bool isOnLand) const
- {
- std::vector<AIPath> paths;
- paths.reserve(NUM_CHAINS / 4);
- auto chains = nodes[pos.x][pos.y][pos.z][isOnLand ? EPathfindingLayer::LAND : EPathfindingLayer::SAIL];
- for(const AIPathNode & node : chains)
- {
- if(node.action == CGPathNode::ENodeAction::UNKNOWN || !node.actor || !node.actor->hero)
- {
- continue;
- }
- AIPath path;
- path.targetHero = node.actor->hero;
- path.heroArmy = node.actor->creatureSet;
- path.armyLoss = node.armyLoss;
- path.targetObjectDanger = evaluateDanger(pos, path.targetHero);
-
- fillChainInfo(&node, path);
- paths.push_back(path);
- }
- return paths;
- }
- void AINodeStorage::fillChainInfo(const AIPathNode * node, AIPath & path) const
- {
- while(node != nullptr)
- {
- if(!node->actor->hero || node->coord == node->actor->hero->visitablePos())
- return;
- AIPathNodeInfo pathNode;
- pathNode.cost = node->cost;
- pathNode.targetHero = node->actor->hero;
- pathNode.turns = node->turns;
- pathNode.danger = node->danger;
- pathNode.coord = node->coord;
- path.nodes.push_back(pathNode);
- path.specialAction = node->specialAction;
- if(node->chainOther)
- fillChainInfo(node->chainOther, path);
- node = getAINode(node->theNodeBefore);
- }
- }
- AIPath::AIPath()
- : nodes({})
- {
- }
- int3 AIPath::firstTileToGet() const
- {
- if(nodes.size())
- {
- return nodes.back().coord;
- }
- return int3(-1, -1, -1);
- }
- uint64_t AIPath::getPathDanger() const
- {
- if(nodes.size())
- {
- return nodes.front().danger;
- }
- return 0;
- }
- float AIPath::movementCost() const
- {
- if(nodes.size())
- {
- return nodes.front().cost;
- }
- // TODO: boost:optional?
- return 0.0;
- }
- uint64_t AIPath::getHeroStrength() const
- {
- return targetHero->getFightingStrength() * heroArmy->getArmyStrength();
- }
- uint64_t AIPath::getTotalDanger(HeroPtr hero) const
- {
- uint64_t pathDanger = getPathDanger();
- uint64_t danger = pathDanger > targetObjectDanger ? pathDanger : targetObjectDanger;
- return danger;
- }
|