1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Cache SectorMaps to evaluate multiple goals -> huge performance boost

This commit is contained in:
DjWarmonger 2015-08-31 08:25:33 +02:00
parent a3ce1521e5
commit 1c14dbf7b2
6 changed files with 27 additions and 7 deletions

View File

@ -302,6 +302,8 @@ Goals::TSubgoal FuzzyHelper::chooseSolution (Goals::TGoalVec vec)
if (vec.empty()) //no possibilities found
return sptr(Goals::Invalid());
cachedSectorMaps.clear();
//a trick to switch between heroes less often - calculatePaths is costly
auto sortByHeroes = [](const Goals::TSubgoal & lhs, const Goals::TSubgoal & rhs) -> bool
{
@ -480,8 +482,7 @@ float FuzzyHelper::evaluate (Goals::ClearWayTo & g)
if (!g.hero.h)
throw cannotFulfillGoalException("ClearWayTo called without hero!");
SectorMap sm(g.hero);
int3 t = sm.firstTileToGet(g.hero, g.tile);
int3 t = getCachedSectorMap(g.hero).firstTileToGet(g.hero, g.tile);
if (t.valid())
{
@ -530,3 +531,16 @@ void FuzzyHelper::setPriority (Goals::TSubgoal & g)
{
g->setpriority(g->accept(this)); //this enforces returned value is set
}
SectorMap& FuzzyHelper::getCachedSectorMap(HeroPtr h)
{
auto it = cachedSectorMaps.find(h);
if (it != cachedSectorMaps.end())
return it->second;
else
{
cachedSectorMaps[h] = SectorMap(h);
}
return cachedSectorMaps[h];
}

View File

@ -15,6 +15,7 @@
class VCAI;
class CArmedInstance;
class CBank;
class SectorMap;
class engineBase
{
@ -54,6 +55,8 @@ class FuzzyHelper
~EvalVisitTile();
} vt;
std::map <HeroPtr, SectorMap> cachedSectorMaps;
public:
enum RuleBlocks {BANK_DANGER, TACTICAL_ADVANTAGE, VISIT_TILE};
//blocks should be initialized in this order, which may be confusing :/
@ -81,4 +84,7 @@ public:
Goals::TSubgoal chooseSolution (Goals::TGoalVec vec);
//shared_ptr<AbstractGoal> chooseSolution (std::vector<shared_ptr<AbstractGoal>> & vec);
//optimization - use one SM for every hero call
SectorMap& getCachedSectorMap (HeroPtr h);
};

View File

@ -2924,7 +2924,7 @@ bool SectorMap::markIfBlocked(ui8 &sec, crint3 pos)
void SectorMap::update()
{
visibleTiles = std::move(cb->getAllVisibleTiles());
visibleTiles = cb->getAllVisibleTiles();
clear();
int curSector = 3; //0 is invisible, 1 is not explored

View File

@ -95,7 +95,7 @@ struct SectorMap
//std::vector<std::vector<std::vector<unsigned char>>> pathfinderSector;
std::map<int, Sector> infoOnSectors;
unique_ptr<boost::multi_array<TerrainTile*, 3>> visibleTiles;
shared_ptr<boost::multi_array<TerrainTile*, 3>> visibleTiles;
SectorMap();
SectorMap(HeroPtr h);

View File

@ -465,7 +465,7 @@ const TerrainTile * CGameInfoCallback::getTile( int3 tile, bool verbose) const
}
//TODO: typedef?
unique_ptr<boost::multi_array<TerrainTile*, 3>> CGameInfoCallback::getAllVisibleTiles() const
shared_ptr<boost::multi_array<TerrainTile*, 3>> CGameInfoCallback::getAllVisibleTiles() const
{
assert(player.is_initialized());
auto team = getPlayerTeam(player.get());
@ -486,7 +486,7 @@ unique_ptr<boost::multi_array<TerrainTile*, 3>> CGameInfoCallback::getAllVisible
else
tileArray[x][y][z] = nullptr;
}
return make_unique<boost::multi_array<TerrainTile*, 3>>(tileArray);
return make_shared<boost::multi_array<TerrainTile*, 3>>(tileArray);
}
EBuildingState::EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID )

View File

@ -91,7 +91,7 @@ public:
const CMapHeader * getMapHeader()const;
int3 getMapSize() const; //returns size of map - z is 1 for one - level map and 2 for two level map
const TerrainTile * getTile(int3 tile, bool verbose = true) const;
unique_ptr<boost::multi_array<TerrainTile*, 3>> CGameInfoCallback::getAllVisibleTiles() const;
shared_ptr<boost::multi_array<TerrainTile*, 3>> CGameInfoCallback::getAllVisibleTiles() const;
bool isInTheMap(const int3 &pos) const;
//town