1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-16 10:19:47 +02:00
vcmi/AI/Nullkiller/Analyzers/DangerHitMapAnalyzer.cpp

354 lines
8.8 KiB
C++
Raw Normal View History

/*
* 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"
#include "../Engine/Nullkiller.h"
2024-03-25 17:05:24 +02:00
#include "../pforeach.h"
#include "../../../lib/CRandomGenerator.h"
2024-08-06 21:29:04 +02:00
#include "../../../lib/logging/VisualLogger.h"
2022-09-26 20:01:07 +02:00
namespace NKAI
{
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
}
void DangerHitMapAnalyzer::updateHitMap()
{
if(hitMapUpToDate)
return;
logAi->trace("Update danger hitmap");
hitMapUpToDate = true;
2021-11-23 08:41:03 +02:00
auto start = std::chrono::high_resolution_clock::now();
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]);
enemyHeroAccessibleObjects.clear();
townThreats.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;
}
if(obj->ID == Obj::TOWN)
{
auto town = dynamic_cast<const CGTownInstance *>(obj);
if(town->garrisonHero)
heroes[town->garrisonHero->tempOwner][town->garrisonHero] = HeroRole::MAIN;
}
}
auto ourTowns = cb->getTownsInfo();
for(auto town : ourTowns)
{
townThreats[town->id]; // insert empty list
}
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;
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-16 13:57:33 +02:00
boost::this_thread::interruption_point();
pforeachTilePaths(mapSize, ai, [&](const int3 & pos, const std::vector<AIPath> & paths)
2021-05-16 12:53:32 +02:00
{
for(const AIPath & path : paths)
{
if(path.getFirstBlockedAction())
continue;
auto & node = hitMap[pos.x][pos.y][pos.z];
HitMapInfo newThreat;
2023-06-11 18:21:50 +02:00
newThreat.hero = path.targetHero;
newThreat.turn = path.turn();
newThreat.danger = path.getHeroStrength();
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 = cb->getVisitableObjs(pos, false);
for(auto obj : objects)
{
if(obj->ID == Obj::TOWN && obj->getOwner() == ai->playerID)
{
auto & threats = townThreats[obj->id];
auto threat = std::find_if(threats.begin(), threats.end(), [&](const HitMapInfo & i) -> bool
{
return i.hero.hid == path.targetHero->id;
});
2023-06-11 18:21:50 +02:00
if(threat == threats.end())
2023-06-11 18:21:50 +02:00
{
threats.emplace_back();
threat = std::prev(threats.end(), 1);
}
2023-06-11 18:21:50 +02:00
if(newThreat.value() > threat->value())
{
*threat = newThreat;
}
2023-06-11 18:21:50 +02:00
if(newThreat.turn == 0)
{
if(cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES)
enemyHeroAccessibleObjects.emplace_back(path.targetHero, obj);
2023-06-11 18:21:50 +02:00
}
}
}
}
});
}
logAi->trace("Danger hit map updated in %ld", timeElapsed(start));
2024-08-06 21:29:04 +02:00
logHitmap(ai->playerID, *this);
}
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]);
2024-03-09 16:20:00 +02:00
std::vector<std::unique_ptr<CGHeroInstance>> temporaryHeroes;
std::map<const CGHeroInstance *, const CGTownInstance *> heroTownMap;
2024-03-09 16:20:00 +02:00
std::map<const CGHeroInstance *, HeroRole> townHeroes;
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();
CRandomGenerator rng;
auto visitablePos = town->visitablePos();
townHero->setOwner(ai->playerID); // lets avoid having multiple colors
townHero->initHero(rng, static_cast<HeroTypeID>(0));
townHero->pos = townHero->convertFromVisitablePos(visitablePos);
2023-07-31 21:00:22 +02:00
townHero->initObj(rng);
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);
}
PathfinderSettings ps;
ps.mainTurnDistanceLimit = ps.scoutTurnDistanceLimit = ai->settings->getMainHeroTurnDistanceLimit();
ai->pathfinder->updatePaths(townHeroes, ps);
pforeachTilePaths(mapSize, ai, [&](const int3 & pos, const std::vector<AIPath> & paths)
{
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;
for(const AIPath & path : paths)
{
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;
}
}
else
{
if(enemyDistance > path.movementCost())
{
enemyDistance = path.movementCost();
enemyTown = town;
}
}
}
if(vstd::isAlmostEqual(ourDistance, enemyDistance))
{
2023-06-11 18:21:50 +02:00
hitMap[pos.x][pos.y][pos.z].closestTown = nullptr;
}
else if(!enemyTown || ourDistance < enemyDistance)
{
2023-06-11 18:21:50 +02:00
hitMap[pos.x][pos.y][pos.z].closestTown = ourTown;
}
else
{
2023-06-11 18:21:50 +02:00
hitMap[pos.x][pos.y][pos.z].closestTown = enemyTown;
}
});
}
const std::vector<HitMapInfo> & DangerHitMapAnalyzer::getTownThreats(const CGTownInstance * town) const
2023-06-11 18:21:50 +02:00
{
static const std::vector<HitMapInfo> empty = {};
auto result = townThreats.find(town->id);
2023-06-11 18:21:50 +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;
}
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))
|| (info.maximumDanger.turn <= turn && !isSafeToVisit(path.targetHero, path.heroArmy, info.maximumDanger.danger));
}
2021-05-16 12:53:32 +02:00
const HitMapNode & DangerHitMapAnalyzer::getObjectThreat(const CGObjectInstance * obj) const
2021-05-16 12:53:32 +02:00
{
auto tile = obj->visitablePos();
return getTileThreat(tile);
}
const HitMapNode & DangerHitMapAnalyzer::getTileThreat(const int3 & tile) const
{
return hitMap[tile.x][tile.y][tile.z];
2021-05-16 12:53:32 +02:00
}
std::set<const CGObjectInstance *> DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const
{
std::set<const CGObjectInstance *> result;
for(auto & obj : enemyHeroAccessibleObjects)
{
if(obj.hero == enemy)
result.insert(obj.obj);
}
return result;
}
void DangerHitMapAnalyzer::reset()
{
hitMapUpToDate = false;
}
2022-09-26 20:01:07 +02:00
}