1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00
Files
vcmi/AI/Nullkiller2/Analyzers/DangerHitMapAnalyzer.cpp

362 lines
9.6 KiB
C++
Raw Normal View History

2025-08-13 14:41:15 +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
*
*/
#include "../StdInc.h"
#include "DangerHitMapAnalyzer.h"
#include "../Engine/Nullkiller.h"
2025-08-13 14:41:15 +02:00
#include "../pforeach.h"
#include "../../../lib/callback/GameRandomizer.h"
#include "../../../lib/logging/VisualLogger.h"
2025-08-13 17:16:27 +02:00
namespace NK2AI
2025-08-13 14:41:15 +02:00
{
const HitMapInfo HitMapInfo::NoThreat;
void logHitmap(PlayerColor playerID, DangerHitMapAnalyzer & data, const CCallback & cc)
2025-08-13 14:41:15 +02:00
{
2025-08-24 21:15:34 +02:00
#if NK2AI_TRACE_LEVEL >= 1
logVisual->updateWithLock(
playerID.toString() + ".danger.max",
[&data, &cc](IVisualLogBuilder & b)
2025-08-13 14:41:15 +02:00
{
foreach_tile_pos(
cc,
[&b, &data](const int3 & pos)
2025-08-13 14:41:15 +02:00
{
auto & threat = data.getTileThreat(pos).maximumDanger;
b.addText(pos, std::to_string(threat.danger));
2025-08-13 14:41:15 +02:00
if(threat.heroPtr.isVerified())
2025-08-13 14:41:15 +02:00
{
b.addText(pos, std::to_string(threat.turn));
b.addText(pos, threat.heroPtr->getNameTranslated());
2025-08-13 14:41:15 +02:00
}
}
);
}
);
2025-08-13 14:41:15 +02:00
logVisual->updateWithLock(
playerID.toString() + ".danger.fast",
[&data, &cc](IVisualLogBuilder & b)
2025-08-13 14:41:15 +02:00
{
foreach_tile_pos(
cc,
[&b, &data](const int3 & pos)
2025-08-13 14:41:15 +02:00
{
auto & threat = data.getTileThreat(pos).fastestDanger;
b.addText(pos, std::to_string(threat.danger));
2025-08-13 14:41:15 +02:00
if(threat.heroPtr.isVerified())
2025-08-13 14:41:15 +02:00
{
b.addText(pos, std::to_string(threat.turn));
b.addText(pos, threat.heroPtr->getNameTranslated());
2025-08-13 14:41:15 +02:00
}
}
);
}
);
2025-08-13 14:41:15 +02:00
#endif
}
void DangerHitMapAnalyzer::updateHitMap()
{
if(hitMapUpToDate)
return;
logAi->trace("Update danger hitmap");
hitMapUpToDate = true;
auto start = std::chrono::high_resolution_clock::now();
auto cc = aiNk->cc.get();
auto mapSize = aiNk->cc->getMapSize();
2025-08-13 14:41:15 +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]);
enemyHeroAccessibleObjects.clear();
townThreats.clear();
std::map<PlayerColor, std::map<const CGHeroInstance *, HeroRole>> heroes;
for(const CGObjectInstance * obj : aiNk->memory->visitableObjs)
2025-08-13 14:41:15 +02:00
{
if(obj->ID == Obj::HERO)
{
auto hero = dynamic_cast<const CGHeroInstance *>(obj);
heroes[hero->tempOwner][hero] = HeroRole::MAIN;
}
if(obj->ID == Obj::TOWN)
{
auto town = dynamic_cast<const CGTownInstance *>(obj);
if(town->getGarrisonHero())
heroes[town->getGarrisonHero()->tempOwner][town->getGarrisonHero()] = HeroRole::MAIN;
}
}
auto ourTowns = cc->getTownsInfo();
2025-08-13 14:41:15 +02:00
for(auto town : ourTowns)
{
townThreats[town->id]; // insert empty list
}
foreach_tile_pos(*aiNk->cc, [&](const int3 & pos){
2025-08-13 14:41:15 +02:00
hitMap[pos.x][pos.y][pos.z].reset();
});
for(auto pair : heroes)
{
if(!pair.first.isValidPlayer())
continue;
if(aiNk->cc->getPlayerRelations(aiNk->playerID, pair.first) != PlayerRelations::ENEMIES)
2025-08-13 14:41:15 +02:00
continue;
PathfinderSettings ps;
ps.scoutTurnDistanceLimit = ps.mainTurnDistanceLimit = aiNk->settings->getThreatTurnDistanceLimit();
2025-08-13 14:41:15 +02:00
ps.useHeroChain = false;
aiNk->pathfinder->updatePaths(pair.second, ps);
2025-08-13 14:41:15 +02:00
aiNk->makingTurnInterruption.interruptionPoint();
2025-08-13 14:41:15 +02:00
pforeachTilePaths(mapSize, aiNk, [&](const int3 & pos, const std::vector<AIPath> & paths)
2025-08-13 14:41:15 +02:00
{
for(const AIPath & path : paths)
{
if(path.getFirstBlockedAction())
continue;
auto & node = hitMap[pos.x][pos.y][pos.z];
HitMapInfo newThreat;
newThreat.heroPtr = HeroPtr(path.targetHero, aiNk->cc.get());
2025-08-13 14:41:15 +02:00
newThreat.turn = path.turn();
newThreat.threat = path.getHeroStrength() * (1 - path.movementCost() / 2.0);
// TODO: Mircea: Why is this danger calculated so differently than FuzzyHelper::evaluateDanger?
2025-08-13 14:41:15 +02:00
// shouldn't it use the path danger instead of hero strength?
newThreat.danger = path.getHeroStrength();
auto danger2 = aiNk->dangerEvaluator->evaluateDanger(pos, path.targetHero);
2025-08-13 14:41:15 +02:00
logAi->trace("Danger comparison for (%d %d %d) %s is %f vs %f",
pos.x, pos.y, pos.z, path.targetHero->getNameTranslated(), newThreat.danger, danger2);
if(newThreat.value() > node.maximumDanger.value())
{
node.maximumDanger = newThreat;
}
if(newThreat.turn < node.fastestDanger.turn
|| (newThreat.turn == node.fastestDanger.turn && node.fastestDanger.danger < newThreat.danger))
{
node.fastestDanger = newThreat;
}
auto objects = cc->getVisitableObjs(pos, false);
2025-08-13 14:41:15 +02:00
for(auto obj : objects)
{
if(obj->ID == Obj::TOWN && obj->getOwner() == aiNk->playerID)
2025-08-13 14:41:15 +02:00
{
auto & threats = townThreats[obj->id];
auto threat = std::find_if(threats.begin(), threats.end(), [&](const HitMapInfo & i) -> bool
{
return i.heroPtr.idOrNone() == path.targetHero->id;
2025-08-13 14:41:15 +02:00
});
if(threat == threats.end())
{
threats.emplace_back();
threat = std::prev(threats.end(), 1);
}
if(newThreat.value() > threat->value())
{
*threat = newThreat;
}
if(newThreat.turn == 0)
{
if(cc->getPlayerRelations(obj->tempOwner, aiNk->playerID) != PlayerRelations::ENEMIES)
2025-08-13 14:41:15 +02:00
enemyHeroAccessibleObjects.emplace_back(path.targetHero, obj);
}
}
}
}
});
}
logAi->trace("Danger hit map updated in %ld", timeElapsed(start));
logHitmap(aiNk->playerID, *this, *aiNk->cc);
2025-08-13 14:41:15 +02:00
}
void DangerHitMapAnalyzer::calculateTileOwners()
{
if(tileOwnersUpToDate) return;
tileOwnersUpToDate = true;
auto * cc = aiNk->cc.get();
auto mapSize = aiNk->cc->getMapSize();
2025-08-13 14:41:15 +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]);
std::vector<std::unique_ptr<CGHeroInstance>> temporaryHeroes;
std::map<const CGHeroInstance *, const CGTownInstance *> heroTownMap;
std::map<const CGHeroInstance *, HeroRole> townHeroes;
auto addTownHero = [&](const CGTownInstance * town)
{
auto *townHero = temporaryHeroes.emplace_back(std::make_unique<CGHeroInstance>(town->cb)).get();
2025-08-13 14:41:15 +02:00
GameRandomizer randomizer(*town->cb);
auto visitablePos = town->visitablePos();
townHero->id = town->id;
townHero->setOwner(aiNk->playerID); // lets avoid having multiple colors
2025-08-13 14:41:15 +02:00
townHero->initHero(randomizer, static_cast<HeroTypeID>(0));
townHero->pos = townHero->convertFromVisitablePos(visitablePos);
townHero->initObj(randomizer);
heroTownMap[townHero] = town;
townHeroes[townHero] = HeroRole::MAIN;
};
for(const auto *obj : aiNk->memory->visitableObjs)
2025-08-13 14:41:15 +02:00
{
if(obj && obj->ID == Obj::TOWN)
{
addTownHero(dynamic_cast<const CGTownInstance *>(obj));
}
}
for(const auto *town : cc->getTownsInfo())
2025-08-13 14:41:15 +02:00
{
addTownHero(town);
}
PathfinderSettings ps;
ps.mainTurnDistanceLimit = ps.scoutTurnDistanceLimit = aiNk->settings->getMainHeroTurnDistanceLimit();
2025-08-13 14:41:15 +02:00
aiNk->pathfinder->updatePaths(townHeroes, ps);
2025-08-13 14:41:15 +02:00
pforeachTilePaths(mapSize, aiNk, [&](const int3 & pos, const std::vector<AIPath> & paths)
2025-08-13 14:41:15 +02:00
{
float ourDistance = std::numeric_limits<float>::max();
float enemyDistance = std::numeric_limits<float>::max();
const CGTownInstance * enemyTown = nullptr;
const CGTownInstance * ourTown = nullptr;
for(const AIPath & path : paths)
{
if(!path.targetHero || path.getFirstBlockedAction())
continue;
const auto *town = heroTownMap[path.targetHero];
2025-08-13 14:41:15 +02:00
if(town->getOwner() == aiNk->playerID)
2025-08-13 14:41:15 +02:00
{
if(ourDistance > path.movementCost())
{
ourDistance = path.movementCost();
ourTown = town;
}
}
else
{
if(enemyDistance > path.movementCost())
{
enemyDistance = path.movementCost();
enemyTown = town;
}
}
}
if(vstd::isAlmostEqual(ourDistance, enemyDistance))
{
hitMap[pos.x][pos.y][pos.z].closestTown = nullptr;
}
else if(!enemyTown || ourDistance < enemyDistance)
{
hitMap[pos.x][pos.y][pos.z].closestTown = ourTown;
}
else
{
hitMap[pos.x][pos.y][pos.z].closestTown = enemyTown;
}
});
}
const std::vector<HitMapInfo> & DangerHitMapAnalyzer::getTownThreats(const CGTownInstance * town) const
{
static const std::vector<HitMapInfo> empty = {};
auto result = townThreats.find(town->id);
return result == townThreats.end() ? empty : result->second;
}
PlayerColor DangerHitMapAnalyzer::getTileOwner(const int3 & tile) const
{
const auto *town = hitMap[tile.x][tile.y][tile.z].closestTown;
2025-08-13 14:41:15 +02:00
return town ? town->getOwner() : PlayerColor::NEUTRAL;
}
const CGTownInstance * DangerHitMapAnalyzer::getClosestTown(const int3 & tile) const
{
return hitMap[tile.x][tile.y][tile.z].closestTown;
}
uint64_t DangerHitMapAnalyzer::enemyCanKillOurHeroesAlongThePath(const AIPath & path) const
{
int3 tile = path.targetTile();
int turn = path.turn();
const auto& info = getTileThreat(tile);
return (info.fastestDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.fastestDanger.danger, aiNk->settings->getSafeAttackRatio()))
|| (info.maximumDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.maximumDanger.danger, aiNk->settings->getSafeAttackRatio()));
2025-08-13 14:41:15 +02:00
}
const HitMapNode & DangerHitMapAnalyzer::getObjectThreat(const CGObjectInstance * obj) const
{
auto tile = obj->visitablePos();
return getTileThreat(tile);
}
const HitMapNode & DangerHitMapAnalyzer::getTileThreat(const int3 & tile) const
{
return hitMap[tile.x][tile.y][tile.z];
}
std::set<const CGObjectInstance *> DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const
{
std::set<const CGObjectInstance *> result;
for(const auto & obj : enemyHeroAccessibleObjects)
2025-08-13 14:41:15 +02:00
{
if(obj.hero == enemy)
result.insert(obj.obj);
}
return result;
}
}