mirror of
https://github.com/vcmi/vcmi.git
synced 2025-11-27 22:49:25 +02:00
Cache visible tiles within SectorMap for quicker access.
This commit is contained in:
@@ -2906,7 +2906,7 @@ SectorMap::SectorMap(HeroPtr h)
|
|||||||
makeParentBFS(h->visitablePos());
|
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)
|
if(t->blocked && !t->visitable)
|
||||||
{
|
{
|
||||||
@@ -2917,13 +2917,15 @@ bool markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t)
|
|||||||
return false;
|
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()
|
void SectorMap::update()
|
||||||
{
|
{
|
||||||
|
visibleTiles = std::move(cb->getAllVisibleTiles());
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
int curSector = 3; //0 is invisible, 1 is not explored
|
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];
|
Sector &s = infoOnSectors[num];
|
||||||
s.id = num;
|
s.id = num;
|
||||||
s.water = cbp->getTile(pos)->isWater();
|
s.water = getTile(pos)->isWater();
|
||||||
|
|
||||||
std::queue<int3> toVisit;
|
std::queue<int3> toVisit;
|
||||||
toVisit.push(pos);
|
toVisit.push(pos);
|
||||||
@@ -2960,7 +2962,7 @@ void SectorMap::exploreNewSector(crint3 pos, int num, CCallback * cbp)
|
|||||||
ui8 &sec = retreiveTile(curPos);
|
ui8 &sec = retreiveTile(curPos);
|
||||||
if(sec == NOT_CHECKED)
|
if(sec == NOT_CHECKED)
|
||||||
{
|
{
|
||||||
const TerrainTile *t = cbp->getTile(curPos);
|
const TerrainTile *t = getTile(curPos);
|
||||||
if(!markIfBlocked(sec, curPos, t))
|
if(!markIfBlocked(sec, curPos, t))
|
||||||
{
|
{
|
||||||
if(t->isWater() == s.water) //sector is only-water or only-land
|
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);
|
toVisit.push(neighPos);
|
||||||
//parent[neighPos] = curPos;
|
//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))
|
if(nt && nt->isWater() != s.water && canBeEmbarkmentPoint(nt, s.water))
|
||||||
{
|
{
|
||||||
s.embarkmentPoints.push_back(neighPos);
|
s.embarkmentPoints.push_back(neighPos);
|
||||||
@@ -3406,3 +3408,9 @@ unsigned char & SectorMap::retreiveTile(crint3 pos)
|
|||||||
return retreiveTileN(sector, 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];
|
||||||
|
}
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ struct SectorMap
|
|||||||
//std::vector<std::vector<std::vector<unsigned char>>> pathfinderSector;
|
//std::vector<std::vector<std::vector<unsigned char>>> pathfinderSector;
|
||||||
|
|
||||||
std::map<int, Sector> infoOnSectors;
|
std::map<int, Sector> infoOnSectors;
|
||||||
|
unique_ptr<boost::multi_array<TerrainTile*, 3>> visibleTiles;
|
||||||
|
|
||||||
SectorMap();
|
SectorMap();
|
||||||
SectorMap(HeroPtr h);
|
SectorMap(HeroPtr h);
|
||||||
@@ -103,7 +104,10 @@ struct SectorMap
|
|||||||
void exploreNewSector(crint3 pos, int num, CCallback * cbp);
|
void exploreNewSector(crint3 pos, int num, CCallback * cbp);
|
||||||
void write(crstring fname);
|
void write(crstring fname);
|
||||||
|
|
||||||
|
bool markIfBlocked(ui8 &sec, crint3 pos, const TerrainTile *t);
|
||||||
|
bool markIfBlocked(ui8 &sec, crint3 pos);
|
||||||
unsigned char &retreiveTile(crint3 pos);
|
unsigned char &retreiveTile(crint3 pos);
|
||||||
|
TerrainTile* getTile(crint3 pos) const;
|
||||||
|
|
||||||
void makeParentBFS(crint3 source);
|
void makeParentBFS(crint3 source);
|
||||||
|
|
||||||
|
|||||||
1
Global.h
1
Global.h
@@ -157,6 +157,7 @@ static_assert(sizeof(bool) == 1, "Bool needs to be 1 byte in size.");
|
|||||||
#include <boost/thread.hpp>
|
#include <boost/thread.hpp>
|
||||||
#include <boost/variant.hpp>
|
#include <boost/variant.hpp>
|
||||||
#include <boost/math/special_functions/round.hpp>
|
#include <boost/math/special_functions/round.hpp>
|
||||||
|
#include <boost/multi_array.hpp>
|
||||||
|
|
||||||
#ifndef M_PI
|
#ifndef M_PI
|
||||||
# define M_PI 3.14159265358979323846
|
# define M_PI 3.14159265358979323846
|
||||||
|
|||||||
@@ -464,6 +464,31 @@ const TerrainTile * CGameInfoCallback::getTile( int3 tile, bool verbose) const
|
|||||||
return &gs->map->getTile(tile);
|
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 )
|
EBuildingState::EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID )
|
||||||
{
|
{
|
||||||
ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED);
|
ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED);
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ public:
|
|||||||
const CMapHeader * getMapHeader()const;
|
const CMapHeader * getMapHeader()const;
|
||||||
int3 getMapSize() const; //returns size of map - z is 1 for one - level map and 2 for two level map
|
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;
|
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;
|
bool isInTheMap(const int3 &pos) const;
|
||||||
|
|
||||||
//town
|
//town
|
||||||
|
|||||||
Reference in New Issue
Block a user