1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00

Cache visible tiles within SectorMap for quicker access.

This commit is contained in:
DjWarmonger 2015-08-31 07:39:03 +02:00
parent 904a66764c
commit a3ce1521e5
5 changed files with 45 additions and 6 deletions

View File

@ -2906,7 +2906,7 @@ SectorMap::SectorMap(HeroPtr h)
makeParentBFS(h->visitablePos());
}
bool markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t)
bool SectorMap::markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t)
{
if(t->blocked && !t->visitable)
{
@ -2917,13 +2917,15 @@ bool markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t)
return false;
}
bool markIfBlocked(ui8 &sec, crint3 pos)
bool SectorMap::markIfBlocked(ui8 &sec, crint3 pos)
{
return markIfBlocked(sec, pos, cb->getTile(pos));
return markIfBlocked(sec, pos, getTile(pos));
}
void SectorMap::update()
{
visibleTiles = std::move(cb->getAllVisibleTiles());
clear();
int curSector = 3; //0 is invisible, 1 is not explored
@ -2949,7 +2951,7 @@ void SectorMap::exploreNewSector(crint3 pos, int num, CCallback * cbp)
{
Sector &s = infoOnSectors[num];
s.id = num;
s.water = cbp->getTile(pos)->isWater();
s.water = getTile(pos)->isWater();
std::queue<int3> toVisit;
toVisit.push(pos);
@ -2960,7 +2962,7 @@ void SectorMap::exploreNewSector(crint3 pos, int num, CCallback * cbp)
ui8 &sec = retreiveTile(curPos);
if(sec == NOT_CHECKED)
{
const TerrainTile *t = cbp->getTile(curPos);
const TerrainTile *t = getTile(curPos);
if(!markIfBlocked(sec, curPos, t))
{
if(t->isWater() == s.water) //sector is only-water or only-land
@ -2974,7 +2976,7 @@ void SectorMap::exploreNewSector(crint3 pos, int num, CCallback * cbp)
toVisit.push(neighPos);
//parent[neighPos] = curPos;
}
const TerrainTile *nt = cbp->getTile(neighPos, false);
const TerrainTile *nt = getTile(neighPos);
if(nt && nt->isWater() != s.water && canBeEmbarkmentPoint(nt, s.water))
{
s.embarkmentPoints.push_back(neighPos);
@ -3406,3 +3408,9 @@ unsigned char & SectorMap::retreiveTile(crint3 pos)
return retreiveTileN(sector, pos);
}
TerrainTile* SectorMap::getTile(crint3 pos) const
{
//out of bounds access should be handled by boost::multi_array
//still we cached this array to avoid any checks
return visibleTiles->operator[](pos.x)[pos.y][pos.z];
}

View File

@ -95,6 +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;
SectorMap();
SectorMap(HeroPtr h);
@ -103,7 +104,10 @@ struct SectorMap
void exploreNewSector(crint3 pos, int num, CCallback * cbp);
void write(crstring fname);
bool markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t);
bool markIfBlocked(ui8 &sec, crint3 pos);
unsigned char &retreiveTile(crint3 pos);
TerrainTile* getTile(crint3 pos) const;
void makeParentBFS(crint3 source);

View File

@ -157,6 +157,7 @@ static_assert(sizeof(bool) == 1, "Bool needs to be 1 byte in size.");
#include <boost/thread.hpp>
#include <boost/variant.hpp>
#include <boost/math/special_functions/round.hpp>
#include <boost/multi_array.hpp>
#ifndef M_PI
# define M_PI 3.14159265358979323846

View File

@ -464,6 +464,31 @@ const TerrainTile * CGameInfoCallback::getTile( int3 tile, bool verbose) const
return &gs->map->getTile(tile);
}
//TODO: typedef?
unique_ptr<boost::multi_array<TerrainTile*, 3>> CGameInfoCallback::getAllVisibleTiles() const
{
assert(player.is_initialized());
auto team = getPlayerTeam(player.get());
size_t width = gs->map->width;
size_t height = gs->map->height;
size_t levels = (gs->map->twoLevel ? 2 : 1);
boost::multi_array<TerrainTile*, 3> tileArray(boost::extents[width][height][levels]);
for (size_t x = 0; x < width; x++)
for (size_t y = 0; y < height; y++)
for (size_t z = 0; z < levels; z++)
{
if (team->fogOfWarMap[x][y][z])
tileArray[x][y][z] = &gs->map->getTile(int3(x, y, z));
else
tileArray[x][y][z] = nullptr;
}
return make_unique<boost::multi_array<TerrainTile*, 3>>(tileArray);
}
EBuildingState::EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID )
{
ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED);

View File

@ -91,6 +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;
bool isInTheMap(const int3 &pos) const;
//town