1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
* DangerHitMapAnalyzer.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 "lib/mapping/CMap.h" //for victory conditions
#include "../Engine/Nullkiller.h"
namespace NKAI
{
void DangerHitMapAnalyzer::updateHitMap()
{
if(upToDate)
return;
logAi->trace("Update danger hitmap");
upToDate = true;
auto start = std::chrono::high_resolution_clock::now();
auto cb = ai->cb.get();
auto mapSize = ai->cb->getMapSize();
hitMap.resize(boost::extents[mapSize.x][mapSize.y][mapSize.z]);
enemyHeroAccessibleObjects.clear();
std::map<PlayerColor, std::map<const CGHeroInstance *, HeroRole>> heroes;
for(const CGObjectInstance * obj : ai->memory->visitableObjs)
{
if(obj->ID == Obj::HERO)
{
auto hero = dynamic_cast<const CGHeroInstance *>(obj);
heroes[hero->tempOwner][hero] = HeroRole::MAIN;
}
}
foreach_tile_pos([&](const int3 & pos){
hitMap[pos.x][pos.y][pos.z].reset();
});
for(auto pair : heroes)
{
if(ai->cb->getPlayerRelations(ai->playerID, pair.first) != PlayerRelations::ENEMIES)
continue;
ai->pathfinder->updatePaths(pair.second, PathfinderSettings());
boost::this_thread::interruption_point();
pforeachTilePos(mapSize, [&](const int3 & pos)
{
for(AIPath & path : ai->pathfinder->getPathInfo(pos))
{
if(path.getFirstBlockedAction())
continue;
auto tileDanger = path.getHeroStrength();
auto turn = path.turn();
auto & node = hitMap[pos.x][pos.y][pos.z];
if(tileDanger > node.maximumDanger.danger
|| (tileDanger == node.maximumDanger.danger && node.maximumDanger.turn > turn))
{
node.maximumDanger.danger = tileDanger;
node.maximumDanger.turn = turn;
node.maximumDanger.hero = path.targetHero;
}
if(turn < node.fastestDanger.turn
|| (turn == node.fastestDanger.turn && node.fastestDanger.danger < tileDanger))
{
node.fastestDanger.danger = tileDanger;
node.fastestDanger.turn = turn;
node.fastestDanger.hero = path.targetHero;
}
if(turn == 0)
{
auto objects = cb->getVisitableObjs(pos, false);
for(auto obj : objects)
{
if(cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES)
enemyHeroAccessibleObjects[path.targetHero].insert(obj);
}
}
}
});
}
logAi->trace("Danger hit map updated in %ld", timeElapsed(start));
}
uint64_t DangerHitMapAnalyzer::enemyCanKillOurHeroesAlongThePath(const AIPath & path) const
{
int3 tile = path.targetTile();
int turn = path.turn();
const HitMapNode & info = hitMap[tile.x][tile.y][tile.z];
return (info.fastestDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.fastestDanger.danger))
|| (info.maximumDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.maximumDanger.danger));
}
const HitMapNode & DangerHitMapAnalyzer::getObjectTreat(const CGObjectInstance * obj) const
{
auto tile = obj->visitablePos();
return getTileTreat(tile);
}
const HitMapNode & DangerHitMapAnalyzer::getTileTreat(const int3 & tile) const
{
const HitMapNode & info = hitMap[tile.x][tile.y][tile.z];
return info;
}
const std::set<const CGObjectInstance *> empty = {};
const std::set<const CGObjectInstance *> & DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const
{
auto result = enemyHeroAccessibleObjects.find(enemy);
if(result == enemyHeroAccessibleObjects.end())
{
return empty;
}
return result->second;
}
void DangerHitMapAnalyzer::reset()
{
upToDate = false;
}
}
|