mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-13 19:54:17 +02:00
NKAI: fix potential concurrency and town treat calculation
This commit is contained in:
committed by
Ivan Savenko
parent
04fe78d31c
commit
1eb58bcc32
@@ -53,6 +53,13 @@ void DangerHitMapAnalyzer::updateHitMap()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto ourTowns = cb->getTownsInfo();
|
||||||
|
|
||||||
|
for(auto town : ourTowns)
|
||||||
|
{
|
||||||
|
townTreats[town->id]; // insert empty list
|
||||||
|
}
|
||||||
|
|
||||||
foreach_tile_pos([&](const int3 & pos){
|
foreach_tile_pos([&](const int3 & pos){
|
||||||
hitMap[pos.x][pos.y][pos.z].reset();
|
hitMap[pos.x][pos.y][pos.z].reset();
|
||||||
});
|
});
|
||||||
@@ -95,33 +102,33 @@ void DangerHitMapAnalyzer::updateHitMap()
|
|||||||
node.fastestDanger = newTreat;
|
node.fastestDanger = newTreat;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(newTreat.turn == 0)
|
auto objects = cb->getVisitableObjs(pos, false);
|
||||||
|
|
||||||
|
for(auto obj : objects)
|
||||||
{
|
{
|
||||||
auto objects = cb->getVisitableObjs(pos, false);
|
if(obj->ID == Obj::TOWN && obj->getOwner() == ai->playerID)
|
||||||
|
|
||||||
for(auto obj : objects)
|
|
||||||
{
|
{
|
||||||
if(cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES)
|
auto & treats = townTreats[obj->id];
|
||||||
enemyHeroAccessibleObjects[path.targetHero].insert(obj);
|
auto treat = std::find_if(treats.begin(), treats.end(), [&](const HitMapInfo & i) -> bool
|
||||||
|
{
|
||||||
|
return i.hero.hid == path.targetHero->id;
|
||||||
|
});
|
||||||
|
|
||||||
if(obj->ID == Obj::TOWN && obj->getOwner() == ai->playerID)
|
if(treat == treats.end())
|
||||||
{
|
{
|
||||||
auto & treats = townTreats[obj->id];
|
treats.emplace_back();
|
||||||
auto treat = std::find_if(treats.begin(), treats.end(), [&](const HitMapInfo & i) -> bool
|
treat = std::prev(treats.end(), 1);
|
||||||
{
|
}
|
||||||
return i.hero.hid == path.targetHero->id;
|
|
||||||
});
|
|
||||||
|
|
||||||
if(treat == treats.end())
|
if(newTreat.value() > treat->value())
|
||||||
{
|
{
|
||||||
treats.emplace_back();
|
*treat = newTreat;
|
||||||
treat = std::prev(treats.end(), 1);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if(newTreat.value() > treat->value())
|
if(newTreat.turn == 0)
|
||||||
{
|
{
|
||||||
*treat = newTreat;
|
if(cb->getPlayerRelations(obj->tempOwner, ai->playerID) != PlayerRelations::ENEMIES)
|
||||||
}
|
enemyHeroAccessibleObjects.emplace_back(path.targetHero, obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -274,16 +281,17 @@ const HitMapNode & DangerHitMapAnalyzer::getTileTreat(const int3 & tile) const
|
|||||||
|
|
||||||
const std::set<const CGObjectInstance *> empty = {};
|
const std::set<const CGObjectInstance *> empty = {};
|
||||||
|
|
||||||
const std::set<const CGObjectInstance *> & DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const
|
std::set<const CGObjectInstance *> DangerHitMapAnalyzer::getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const
|
||||||
{
|
{
|
||||||
auto result = enemyHeroAccessibleObjects.find(enemy);
|
std::set<const CGObjectInstance *> result;
|
||||||
|
|
||||||
if(result == enemyHeroAccessibleObjects.end())
|
for(auto & obj : enemyHeroAccessibleObjects)
|
||||||
{
|
{
|
||||||
return empty;
|
if(obj.hero == enemy)
|
||||||
|
result.insert(obj.obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result->second;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DangerHitMapAnalyzer::reset()
|
void DangerHitMapAnalyzer::reset()
|
||||||
|
@@ -55,11 +55,22 @@ struct HitMapNode
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct EnemyHeroAccessibleObject
|
||||||
|
{
|
||||||
|
const CGHeroInstance * hero;
|
||||||
|
const CGObjectInstance * obj;
|
||||||
|
|
||||||
|
EnemyHeroAccessibleObject(const CGHeroInstance * hero, const CGObjectInstance * obj)
|
||||||
|
:hero(hero), obj(obj)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class DangerHitMapAnalyzer
|
class DangerHitMapAnalyzer
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
boost::multi_array<HitMapNode, 3> hitMap;
|
boost::multi_array<HitMapNode, 3> hitMap;
|
||||||
std::map<const CGHeroInstance *, std::set<const CGObjectInstance *>> enemyHeroAccessibleObjects;
|
tbb::concurrent_vector<EnemyHeroAccessibleObject> enemyHeroAccessibleObjects;
|
||||||
bool hitMapUpToDate = false;
|
bool hitMapUpToDate = false;
|
||||||
bool tileOwnersUpToDate = false;
|
bool tileOwnersUpToDate = false;
|
||||||
const Nullkiller * ai;
|
const Nullkiller * ai;
|
||||||
@@ -73,7 +84,7 @@ public:
|
|||||||
uint64_t enemyCanKillOurHeroesAlongThePath(const AIPath & path) const;
|
uint64_t enemyCanKillOurHeroesAlongThePath(const AIPath & path) const;
|
||||||
const HitMapNode & getObjectTreat(const CGObjectInstance * obj) const;
|
const HitMapNode & getObjectTreat(const CGObjectInstance * obj) const;
|
||||||
const HitMapNode & getTileTreat(const int3 & tile) const;
|
const HitMapNode & getTileTreat(const int3 & tile) const;
|
||||||
const std::set<const CGObjectInstance *> & getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const;
|
std::set<const CGObjectInstance *> getOneTurnAccessibleObjects(const CGHeroInstance * enemy) const;
|
||||||
void reset();
|
void reset();
|
||||||
void resetTileOwners() { tileOwnersUpToDate = false; }
|
void resetTileOwners() { tileOwnersUpToDate = false; }
|
||||||
PlayerColor getTileOwner(const int3 & tile) const;
|
PlayerColor getTileOwner(const int3 & tile) const;
|
||||||
|
Reference in New Issue
Block a user