- streamlining fog of war unit cache

This commit is contained in:
Mark Vejvoda 2013-11-11 22:11:13 +00:00
parent 6705a346ef
commit feedde5f61
6 changed files with 22 additions and 48 deletions

View File

@ -212,6 +212,7 @@ private:
bool quitGameCalled;
bool disableSpeedChange;
std::map<int,FowAlphaCellsLookupItem> teamFowAlphaCellsLookupItem;
std::map<string,int64> gamePerformanceCounts;
public:

View File

@ -54,10 +54,7 @@ class GameSettings;
class FowAlphaCellsLookupItem {
public:
std::vector<Vec2i> surfPosList;
std::vector<float> alphaList;
static time_t lastDebug;
std::map<Vec2i,float> surfPosAlphaList;
};
// =====================================================

View File

@ -1278,18 +1278,15 @@ FowAlphaCellsLookupItem Unit::getFogOfWarRadius(bool useCache) const {
if(dist > sightRange) {
alpha= clamp(1.f-(dist - sightRange) / (World::indirectSightRange), 0.f, maxAlpha);
}
result.surfPosList.push_back(surfPos);
result.alphaList.push_back(alpha);
result.surfPosAlphaList[surfPos] = alpha;
}
return result;
}
void Unit::calculateFogOfWarRadius() {
if(game->getWorld()->getFogOfWar() == true) {
//if(Config::getInstance().getBool("EnableFowCache","true") == true && this->pos != this->cachedFowPos) {
if(this->pos != this->cachedFowPos) {
cachedFow = getFogOfWarRadius(false);
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
MutexSafeWrapper safeMutex(mutexCommands,mutexOwnerId);
this->cachedFowPos = this->pos;
@ -4160,8 +4157,7 @@ Vec2i Unit::getPos() {
}
void Unit::clearCaches() {
cachedFow.alphaList.clear();
cachedFow.surfPosList.clear();
cachedFow.surfPosAlphaList.clear();
cachedFowPos = Vec2i(0,0);
if(unitPath != NULL) {

View File

@ -137,14 +137,14 @@ Minimap::~Minimap() {
void Minimap::incFowTextureAlphaSurface(const Vec2i &sPos, float alpha,
bool isIncrementalUpdate) {
if(fowPixmap1) {
assert(sPos.x<fowPixmap1->getW() && sPos.y<fowPixmap1->getH());
assert(sPos.x < fowPixmap1->getW() && sPos.y < fowPixmap1->getH());
if(fowPixmap1->getPixelf(sPos.x, sPos.y)<alpha){
if(fowPixmap1->getPixelf(sPos.x, sPos.y) < alpha){
fowPixmap1->setPixel(sPos.x, sPos.y, alpha);
}
if(fowPixmap1Copy != NULL && isIncrementalUpdate == true) {
if(fowPixmap1Copy->getPixelf(sPos.x, sPos.y)<alpha){
if(fowPixmap1Copy->getPixelf(sPos.x, sPos.y) < alpha){
fowPixmap1Copy->setPixel(sPos.x, sPos.y, alpha);
}
}

View File

@ -54,9 +54,6 @@ World::World() {
ExploredCellsLookupItemCache.clear();
ExploredCellsLookupItemCacheTimer.clear();
ExploredCellsLookupItemCacheTimerCount = 0;
// Disable this cache as it takes too much RAM (not sure if its worth the performance gain)
//enableFowAlphaCellsLookupItemCache = config.getBool("EnableFowCache","true");
enableFowAlphaCellsLookupItemCache = true;
nextCommandGroupId = 0;
techTree = NULL;
@ -2566,23 +2563,13 @@ void World::computeFow() {
faction->getTeam() == thisTeamIndex &&
unit->isOperative() == true) {
if(enableFowAlphaCellsLookupItemCache == true) {
const FowAlphaCellsLookupItem &cellList = unit->getCachedFow();
for(int cellIndex = 0; cellIndex < cellList.surfPosList.size(); ++cellIndex) {
const Vec2i &surfPos = cellList.surfPosList[cellIndex];
const float &alpha = cellList.alphaList[cellIndex];
const FowAlphaCellsLookupItem &cellList = unit->getCachedFow();
for(std::map<Vec2i,float>::const_iterator iterMap = cellList.surfPosAlphaList.begin();
iterMap != cellList.surfPosAlphaList.end(); ++iterMap) {
const Vec2i &surfPos = iterMap->first;
const float &alpha = iterMap->second;
minimap.incFowTextureAlphaSurface(surfPos, alpha, true);
}
}
else {
const FowAlphaCellsLookupItem cellList = unit->getFogOfWarRadius(false);
for(int cellIndex = 0; cellIndex < cellList.surfPosList.size(); ++cellIndex) {
const Vec2i &surfPos = cellList.surfPosList[cellIndex];
const float &alpha = cellList.alphaList[cellIndex];
minimap.incFowTextureAlphaSurface(surfPos, alpha, true);
}
minimap.incFowTextureAlphaSurface(surfPos, alpha, true);
}
}
}
@ -2687,28 +2674,26 @@ string World::getExploredCellsLookupItemCacheStats() {
string World::getFowAlphaCellsLookupItemCacheStats() {
string result = "";
int surfPosCount = 0;
int alphaListCount = 0;
int surfPosAlphaCount = 0;
for(int i=0; i<getFactionCount(); ++i) {
Faction *faction= getFaction(i);
for(int factionIndex = 0; factionIndex < getFactionCount(); ++factionIndex) {
Faction *faction= getFaction(factionIndex);
if(faction->getTeam() == thisTeamIndex) {
for(int j=0; j<faction->getUnitCount(); ++j) {
const Unit *unit= faction->getUnit(j);
for(int unitIndex = 0; unitIndex < faction->getUnitCount(); ++unitIndex) {
const Unit *unit= faction->getUnit(unitIndex);
const FowAlphaCellsLookupItem &cache = unit->getCachedFow();
surfPosCount += (int)cache.surfPosList.size();
alphaListCount += (int)cache.alphaList.size();
surfPosAlphaCount += (int)cache.surfPosAlphaList.size();
}
}
}
uint64 totalBytes = surfPosCount * sizeof(Vec2i);
totalBytes += alphaListCount * sizeof(float);
uint64 totalBytes = surfPosAlphaCount * sizeof(Vec2i);
totalBytes += surfPosAlphaCount * sizeof(float);
totalBytes /= 1000;
char szBuf[8096]="";
snprintf(szBuf,8096,"surface count [%d] alpha count [%d] total KB: %s",surfPosCount,alphaListCount,formatNumber(totalBytes).c_str());
snprintf(szBuf,8096,"surface count [%d] total KB: %s",surfPosAlphaCount,formatNumber(totalBytes).c_str());
result = szBuf;
return result;
}

View File

@ -84,9 +84,6 @@ private:
std::map<int,ExploredCellsLookupKey> ExploredCellsLookupItemCacheTimer;
int ExploredCellsLookupItemCacheTimerCount;
bool enableFowAlphaCellsLookupItemCache;
//std::map<Vec2i, std::map<int, FowAlphaCellsLookupItem > > FowAlphaCellsLookupItemCache;
public:
static const int generationArea= 100;
static const int indirectSightRange= 5;
@ -95,7 +92,6 @@ private:
Map map;
Tileset tileset;
//TechTree techTree;
TechTree *techTree;
TimeFlow timeFlow;
Scenario scenario;
@ -115,7 +111,6 @@ private:
int thisFactionIndex;
int thisTeamIndex;
int frameCount;
//int nextUnitId;
Mutex mutexFactionNextUnitId;
std::map<int,int> mapFactionNextUnitId;