2021-05-15 21:04:26 +02:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2021-05-16 13:55:57 +02:00
|
|
|
#include "../StdInc.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "DangerHitMapAnalyzer.h"
|
|
|
|
|
2020-05-04 17:58:43 +02:00
|
|
|
#include "../Engine/Nullkiller.h"
|
2024-03-25 17:05:24 +02:00
|
|
|
#include "../pforeach.h"
|
2024-01-31 00:17:40 +02:00
|
|
|
#include "../../../lib/CRandomGenerator.h"
|
2024-08-06 21:29:04 +02:00
|
|
|
#include "../../../lib/logging/VisualLogger.h"
|
2021-05-15 20:57:36 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
2024-02-12 13:49:45 +02:00
|
|
|
const HitMapInfo HitMapInfo::NoThreat;
|
2023-02-28 09:07:59 +02:00
|
|
|
|
2023-06-11 18:21:50 +02:00
|
|
|
double HitMapInfo::value() const
|
|
|
|
{
|
|
|
|
return danger / std::sqrt(turn / 3.0f + 1);
|
|
|
|
}
|
|
|
|
|
2024-08-06 21:29:04 +02:00
|
|
|
void logHitmap(PlayerColor playerID, DangerHitMapAnalyzer & data)
|
|
|
|
{
|
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
|
|
|
logVisual->updateWithLock(playerID.toString() + ".danger.max", [&data](IVisualLogBuilder & b)
|
|
|
|
{
|
|
|
|
foreach_tile_pos([&b, &data](const int3 & pos)
|
|
|
|
{
|
|
|
|
auto & treat = data.getTileThreat(pos).maximumDanger;
|
|
|
|
b.addText(pos, std::to_string(treat.danger));
|
|
|
|
|
|
|
|
if(treat.hero.validAndSet())
|
2024-08-10 18:13:09 +02:00
|
|
|
{
|
|
|
|
b.addText(pos, std::to_string(treat.turn));
|
2024-08-06 21:29:04 +02:00
|
|
|
b.addText(pos, treat.hero->getNameTranslated());
|
2024-08-10 18:13:09 +02:00
|
|
|
}
|
2024-08-06 21:29:04 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
logVisual->updateWithLock(playerID.toString() + ".danger.fast", [&data](IVisualLogBuilder & b)
|
|
|
|
{
|
|
|
|
foreach_tile_pos([&b, &data](const int3 & pos)
|
|
|
|
{
|
|
|
|
auto & treat = data.getTileThreat(pos).fastestDanger;
|
|
|
|
b.addText(pos, std::to_string(treat.danger));
|
|
|
|
|
|
|
|
if(treat.hero.validAndSet())
|
2024-08-10 18:13:09 +02:00
|
|
|
{
|
|
|
|
b.addText(pos, std::to_string(treat.turn));
|
2024-08-06 21:29:04 +02:00
|
|
|
b.addText(pos, treat.hero->getNameTranslated());
|
2024-08-10 18:13:09 +02:00
|
|
|
}
|
2024-08-06 21:29:04 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:57:36 +02:00
|
|
|
void DangerHitMapAnalyzer::updateHitMap()
|
|
|
|
{
|
2023-06-04 15:02:02 +02:00
|
|
|
if(hitMapUpToDate)
|
2021-05-16 13:13:35 +02:00
|
|
|
return;
|
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
logAi->trace("Update danger hitmap");
|
|
|
|
|
2023-06-04 15:02:02 +02:00
|
|
|
hitMapUpToDate = true;
|
2021-11-23 08:41:03 +02:00
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
2021-05-16 13:13:35 +02:00
|
|
|
|
2020-05-04 17:58:43 +02:00
|
|
|
auto cb = ai->cb.get();
|
|
|
|
auto mapSize = ai->cb->getMapSize();
|
2023-06-11 18:21:50 +02:00
|
|
|
|
|
|
|
if(hitMap.shape()[0] != mapSize.x || hitMap.shape()[1] != mapSize.y || hitMap.shape()[2] != mapSize.z)
|
|
|
|
hitMap.resize(boost::extents[mapSize.x][mapSize.y][mapSize.z]);
|
|
|
|
|
2021-05-16 13:13:35 +02:00
|
|
|
enemyHeroAccessibleObjects.clear();
|
2023-10-27 21:32:52 +02:00
|
|
|
townThreats.clear();
|
2021-05-15 20:57:36 +02:00
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
std::map<PlayerColor, std::map<const CGHeroInstance *, HeroRole>> heroes;
|
2021-05-15 20:57:36 +02:00
|
|
|
|
2020-05-04 17:58:43 +02:00
|
|
|
for(const CGObjectInstance * obj : ai->memory->visitableObjs)
|
2021-05-15 20:57:36 +02:00
|
|
|
{
|
|
|
|
if(obj->ID == Obj::HERO)
|
|
|
|
{
|
2020-05-04 17:58:43 +02:00
|
|
|
auto hero = dynamic_cast<const CGHeroInstance *>(obj);
|
2021-05-15 20:57:36 +02:00
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
heroes[hero->tempOwner][hero] = HeroRole::MAIN;
|
2021-05-15 20:57:36 +02:00
|
|
|
}
|
2024-08-17 17:17:46 +02:00
|
|
|
|
|
|
|
if(obj->ID == Obj::TOWN)
|
|
|
|
{
|
|
|
|
auto town = dynamic_cast<const CGTownInstance *>(obj);
|
|
|
|
|
|
|
|
if(town->garrisonHero)
|
|
|
|
heroes[town->garrisonHero->tempOwner][town->garrisonHero] = HeroRole::MAIN;
|
|
|
|
}
|
2021-05-15 20:57:36 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 07:57:14 +02:00
|
|
|
auto ourTowns = cb->getTownsInfo();
|
|
|
|
|
|
|
|
for(auto town : ourTowns)
|
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
townThreats[town->id]; // insert empty list
|
2023-08-06 07:57:14 +02:00
|
|
|
}
|
|
|
|
|
2021-05-15 20:57:36 +02:00
|
|
|
foreach_tile_pos([&](const int3 & pos){
|
|
|
|
hitMap[pos.x][pos.y][pos.z].reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
for(auto pair : heroes)
|
|
|
|
{
|
2023-02-28 09:07:59 +02:00
|
|
|
if(!pair.first.isValidPlayer())
|
|
|
|
continue;
|
|
|
|
|
2021-05-16 13:57:36 +02:00
|
|
|
if(ai->cb->getPlayerRelations(ai->playerID, pair.first) != PlayerRelations::ENEMIES)
|
|
|
|
continue;
|
|
|
|
|
2023-09-16 11:33:02 +02:00
|
|
|
PathfinderSettings ps;
|
|
|
|
|
2024-03-03 10:21:17 +02:00
|
|
|
ps.scoutTurnDistanceLimit = ps.mainTurnDistanceLimit = ai->settings->getMainHeroTurnDistanceLimit();
|
2023-09-16 11:33:02 +02:00
|
|
|
ps.useHeroChain = false;
|
|
|
|
|
|
|
|
ai->pathfinder->updatePaths(pair.second, ps);
|
2021-05-15 20:57:36 +02:00
|
|
|
|
2021-05-16 13:57:33 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2024-03-24 09:32:29 +02:00
|
|
|
pforeachTilePaths(mapSize, ai, [&](const int3 & pos, const std::vector<AIPath> & paths)
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
2024-03-24 09:32:29 +02:00
|
|
|
for(const AIPath & path : paths)
|
2021-05-15 20:57:36 +02:00
|
|
|
{
|
2021-05-16 13:15:03 +02:00
|
|
|
if(path.getFirstBlockedAction())
|
|
|
|
continue;
|
|
|
|
|
2021-05-15 20:57:36 +02:00
|
|
|
auto & node = hitMap[pos.x][pos.y][pos.z];
|
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
HitMapInfo newThreat;
|
2023-06-11 18:21:50 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
newThreat.hero = path.targetHero;
|
|
|
|
newThreat.turn = path.turn();
|
|
|
|
newThreat.danger = path.getHeroStrength();
|
2023-06-04 15:02:02 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
if(newThreat.value() > node.maximumDanger.value())
|
2021-05-15 20:57:36 +02:00
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
node.maximumDanger = newThreat;
|
2021-05-15 20:57:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
if(newThreat.turn < node.fastestDanger.turn
|
|
|
|
|| (newThreat.turn == node.fastestDanger.turn && node.fastestDanger.danger < newThreat.danger))
|
2021-05-15 20:57:36 +02:00
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
node.fastestDanger = newThreat;
|
2021-05-15 20:57:36 +02:00
|
|
|
}
|
2021-05-16 13:13:35 +02:00
|
|
|
|
2023-08-06 07:57:14 +02:00
|
|
|
auto objects = cb->getVisitableObjs(pos, false);
|
|
|
|
|
|
|
|
for(auto obj : objects)
|
2021-05-16 13:13:35 +02:00
|
|
|
{
|
2023-08-06 07:57:14 +02:00
|
|
|
if(obj->ID == Obj::TOWN && obj->getOwner() == ai->playerID)
|
2021-05-16 13:13:35 +02:00
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
auto & threats = townThreats[obj->id];
|
|
|
|
auto threat = std::find_if(threats.begin(), threats.end(), [&](const HitMapInfo & i) -> bool
|
2023-08-06 07:57:14 +02:00
|
|
|
{
|
|
|
|
return i.hero.hid == path.targetHero->id;
|
|
|
|
});
|
2023-06-11 18:21:50 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
if(threat == threats.end())
|
2023-06-11 18:21:50 +02:00
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
threats.emplace_back();
|
|
|
|
threat = std::prev(threats.end(), 1);
|
2023-08-06 07:57:14 +02:00
|
|
|
}
|
2023-06-11 18:21:50 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
if(newThreat.value() > threat->value())
|
2023-08-06 07:57:14 +02:00
|
|
|
{
|
2023-10-27 21:32:52 +02:00
|
|
|
*threat = newThreat;
|
2023-08-06 07:57:14 +02:00
|
|
|
}
|
2023-06-11 18:21:50 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
if(newThreat.turn == 0)
|
2023-08-06 07:57:14 +02:00
|
|
|
{
|
|
|
|
if(cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES)
|
|
|
|
enemyHeroAccessibleObjects.emplace_back(path.targetHero, obj);
|
2023-06-11 18:21:50 +02:00
|
|
|
}
|
2021-05-16 13:13:35 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-15 20:57:36 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-05-16 13:56:13 +02:00
|
|
|
|
|
|
|
logAi->trace("Danger hit map updated in %ld", timeElapsed(start));
|
2024-08-06 21:29:04 +02:00
|
|
|
|
|
|
|
logHitmap(ai->playerID, *this);
|
2021-05-15 20:57:36 +02:00
|
|
|
}
|
|
|
|
|
2023-06-04 15:02:02 +02:00
|
|
|
void DangerHitMapAnalyzer::calculateTileOwners()
|
|
|
|
{
|
|
|
|
if(tileOwnersUpToDate) return;
|
|
|
|
|
|
|
|
tileOwnersUpToDate = true;
|
|
|
|
|
|
|
|
auto cb = ai->cb.get();
|
|
|
|
auto mapSize = ai->cb->getMapSize();
|
|
|
|
|
2023-06-11 18:21:50 +02:00
|
|
|
if(hitMap.shape()[0] != mapSize.x || hitMap.shape()[1] != mapSize.y || hitMap.shape()[2] != mapSize.z)
|
|
|
|
hitMap.resize(boost::extents[mapSize.x][mapSize.y][mapSize.z]);
|
2023-06-04 15:02:02 +02:00
|
|
|
|
2024-03-09 16:20:00 +02:00
|
|
|
std::vector<std::unique_ptr<CGHeroInstance>> temporaryHeroes;
|
2023-06-04 15:02:02 +02:00
|
|
|
std::map<const CGHeroInstance *, const CGTownInstance *> heroTownMap;
|
2024-03-09 16:20:00 +02:00
|
|
|
std::map<const CGHeroInstance *, HeroRole> townHeroes;
|
2023-06-04 15:02:02 +02:00
|
|
|
|
|
|
|
auto addTownHero = [&](const CGTownInstance * town)
|
|
|
|
{
|
2024-03-09 16:20:00 +02:00
|
|
|
auto townHero = temporaryHeroes.emplace_back(std::make_unique<CGHeroInstance>(town->cb)).get();
|
2023-06-04 15:02:02 +02:00
|
|
|
CRandomGenerator rng;
|
2023-07-30 17:02:56 +02:00
|
|
|
auto visitablePos = town->visitablePos();
|
2023-06-04 15:02:02 +02:00
|
|
|
|
|
|
|
townHero->setOwner(ai->playerID); // lets avoid having multiple colors
|
|
|
|
townHero->initHero(rng, static_cast<HeroTypeID>(0));
|
2023-07-30 17:02:56 +02:00
|
|
|
townHero->pos = townHero->convertFromVisitablePos(visitablePos);
|
2023-07-31 21:00:22 +02:00
|
|
|
townHero->initObj(rng);
|
2023-06-04 15:02:02 +02:00
|
|
|
|
|
|
|
heroTownMap[townHero] = town;
|
|
|
|
townHeroes[townHero] = HeroRole::MAIN;
|
|
|
|
};
|
|
|
|
|
|
|
|
for(auto obj : ai->memory->visitableObjs)
|
|
|
|
{
|
|
|
|
if(obj && obj->ID == Obj::TOWN)
|
|
|
|
{
|
|
|
|
addTownHero(dynamic_cast<const CGTownInstance *>(obj));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto town : cb->getTownsInfo())
|
|
|
|
{
|
|
|
|
addTownHero(town);
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
PathfinderSettings ps;
|
2024-03-03 10:21:17 +02:00
|
|
|
ps.mainTurnDistanceLimit = ps.scoutTurnDistanceLimit = ai->settings->getMainHeroTurnDistanceLimit();
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
ai->pathfinder->updatePaths(townHeroes, ps);
|
2023-06-04 15:02:02 +02:00
|
|
|
|
2024-03-24 09:32:29 +02:00
|
|
|
pforeachTilePaths(mapSize, ai, [&](const int3 & pos, const std::vector<AIPath> & paths)
|
2023-06-04 15:02:02 +02:00
|
|
|
{
|
|
|
|
float ourDistance = std::numeric_limits<float>::max();
|
|
|
|
float enemyDistance = std::numeric_limits<float>::max();
|
|
|
|
const CGTownInstance * enemyTown = nullptr;
|
2023-06-11 18:21:50 +02:00
|
|
|
const CGTownInstance * ourTown = nullptr;
|
2023-06-04 15:02:02 +02:00
|
|
|
|
2024-03-24 09:32:29 +02:00
|
|
|
for(const AIPath & path : paths)
|
2023-06-04 15:02:02 +02:00
|
|
|
{
|
|
|
|
if(!path.targetHero || path.getFirstBlockedAction())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto town = heroTownMap[path.targetHero];
|
|
|
|
|
|
|
|
if(town->getOwner() == ai->playerID)
|
|
|
|
{
|
2023-06-11 18:21:50 +02:00
|
|
|
if(ourDistance > path.movementCost())
|
|
|
|
{
|
|
|
|
ourDistance = path.movementCost();
|
|
|
|
ourTown = town;
|
|
|
|
}
|
2023-06-04 15:02:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(enemyDistance > path.movementCost())
|
|
|
|
{
|
|
|
|
enemyDistance = path.movementCost();
|
|
|
|
enemyTown = town;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 23:42:05 +02:00
|
|
|
if(vstd::isAlmostEqual(ourDistance, enemyDistance))
|
2023-06-04 15:02:02 +02:00
|
|
|
{
|
2023-06-11 18:21:50 +02:00
|
|
|
hitMap[pos.x][pos.y][pos.z].closestTown = nullptr;
|
2023-06-04 15:02:02 +02:00
|
|
|
}
|
|
|
|
else if(!enemyTown || ourDistance < enemyDistance)
|
|
|
|
{
|
2023-06-11 18:21:50 +02:00
|
|
|
hitMap[pos.x][pos.y][pos.z].closestTown = ourTown;
|
2023-06-04 15:02:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-11 18:21:50 +02:00
|
|
|
hitMap[pos.x][pos.y][pos.z].closestTown = enemyTown;
|
2023-06-04 15:02:02 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
const std::vector<HitMapInfo> & DangerHitMapAnalyzer::getTownThreats(const CGTownInstance * town) const
|
2023-06-11 18:21:50 +02:00
|
|
|
{
|
|
|
|
static const std::vector<HitMapInfo> empty = {};
|
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
auto result = townThreats.find(town->id);
|
2023-06-11 18:21:50 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
return result == townThreats.end() ? empty : result->second;
|
2023-06-11 18:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PlayerColor DangerHitMapAnalyzer::getTileOwner(const int3 & tile) const
|
|
|
|
{
|
|
|
|
auto town = hitMap[tile.x][tile.y][tile.z].closestTown;
|
|
|
|
|
|
|
|
return town ? town->getOwner() : PlayerColor::NEUTRAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGTownInstance * DangerHitMapAnalyzer::getClosestTown(const int3 & tile) const
|
|
|
|
{
|
|
|
|
return hitMap[tile.x][tile.y][tile.z].closestTown;
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:57:36 +02:00
|
|
|
uint64_t DangerHitMapAnalyzer::enemyCanKillOurHeroesAlongThePath(const AIPath & path) const
|
|
|
|
{
|
|
|
|
int3 tile = path.targetTile();
|
|
|
|
int turn = path.turn();
|
|
|
|
|
2023-12-30 12:01:21 +02:00
|
|
|
const auto& info = getTileThreat(tile);
|
2023-12-30 00:12:06 +02:00
|
|
|
|
|
|
|
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));
|
2021-05-15 21:04:26 +02:00
|
|
|
}
|
2021-05-16 12:53:32 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
const HitMapNode & DangerHitMapAnalyzer::getObjectThreat(const CGObjectInstance * obj) const
|
2021-05-16 12:53:32 +02:00
|
|
|
{
|
2021-05-16 13:13:35 +02:00
|
|
|
auto tile = obj->visitablePos();
|
2021-05-16 13:55:33 +02:00
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
return getTileThreat(tile);
|
2021-05-16 13:55:33 +02:00
|
|
|
}
|
|
|
|
|
2023-10-27 21:32:52 +02:00
|
|
|
const HitMapNode & DangerHitMapAnalyzer::getTileThreat(const int3 & tile) const
|
2021-05-16 13:55:33 +02:00
|
|
|
{
|
2023-12-27 23:24:58 +02:00
|
|
|
return hitMap[tile.x][tile.y][tile.z];
|
2021-05-16 12:53:32 +02:00
|
|
|
}
|
2021-05-16 13:13:35 +02:00
|
|
|
|
2023-08-06 07:57:14 +02:00
|
|
|
std::set<const CGObjectInstance *> DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const
|
2021-05-16 13:13:35 +02:00
|
|
|
{
|
2023-08-06 07:57:14 +02:00
|
|
|
std::set<const CGObjectInstance *> result;
|
|
|
|
|
|
|
|
for(auto & obj : enemyHeroAccessibleObjects)
|
2021-05-16 13:13:35 +02:00
|
|
|
{
|
2023-08-06 07:57:14 +02:00
|
|
|
if(obj.hero == enemy)
|
|
|
|
result.insert(obj.obj);
|
2021-05-16 13:13:35 +02:00
|
|
|
}
|
|
|
|
|
2023-08-06 07:57:14 +02:00
|
|
|
return result;
|
2021-05-16 13:13:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DangerHitMapAnalyzer::reset()
|
|
|
|
{
|
2023-06-04 15:02:02 +02:00
|
|
|
hitMapUpToDate = false;
|
2021-05-16 13:13:35 +02:00
|
|
|
}
|
2022-09-26 20:01:07 +02:00
|
|
|
|
|
|
|
}
|