- some performance updates to fog of war computation

This commit is contained in:
Mark Vejvoda
2010-08-23 14:48:33 +00:00
parent db47a14940
commit 284371f206
2 changed files with 79 additions and 62 deletions

View File

@@ -317,18 +317,19 @@ void World::update(){
} }
bool World::canTickFaction(int factionIdx) { bool World::canTickFaction(int factionIdx) {
int expectedFactionIdx = (frameCount / (GameConstants::updateFps / GameConstants::maxPlayers)); int factionUpdateInterval = (GameConstants::updateFps / GameConstants::maxPlayers);
int expectedFactionIdx = (frameCount / factionUpdateInterval) -1;
if(expectedFactionIdx >= GameConstants::maxPlayers) { if(expectedFactionIdx >= GameConstants::maxPlayers) {
expectedFactionIdx = expectedFactionIdx % GameConstants::maxPlayers; expectedFactionIdx = expectedFactionIdx % GameConstants::maxPlayers;
} }
bool result = (expectedFactionIdx == factionIdx); bool result = (expectedFactionIdx == factionIdx);
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] factionIdx = %d, frameCount = %d, GameConstants::updateFps = %d, result = %d\n",__FILE__,__FUNCTION__,__LINE__,factionIdx,frameCount,GameConstants::updateFps,result); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] factionIdx = %d, frameCount = %d, GameConstants::updateFps = %d, expectedFactionIdx = %d, factionUpdateInterval = %d, result = %d\n",__FILE__,__FUNCTION__,__LINE__,factionIdx,frameCount,GameConstants::updateFps,expectedFactionIdx,factionUpdateInterval,result);
return result; return result;
} }
void World::tick() { int World::tickFactionIndex() {
int factionIdxToTick = -1; int factionIdxToTick = -1;
for(int i=0; i<getFactionCount(); ++i) { for(int i=0; i<getFactionCount(); ++i) {
if(canTickFaction(i) == true) { if(canTickFaction(i) == true) {
@@ -336,14 +337,24 @@ void World::tick() {
break; break;
} }
} }
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] factionIdxToTick = %d\n",__FILE__,__FUNCTION__,__LINE__,factionIdxToTick);
return factionIdxToTick;
}
void World::tick() {
int factionIdxToTick = tickFactionIndex();
if(factionIdxToTick < 0) { if(factionIdxToTick < 0) {
return; return;
} }
computeFow(); computeFow(factionIdxToTick);
if(fogOfWarSmoothing == false) { if(factionIdxToTick == 0) {
minimap.updateFowTex(1.f); if(fogOfWarSmoothing == false) {
minimap.updateFowTex(1.f);
}
} }
//increase hp //increase hp
@@ -973,21 +984,27 @@ void World::exploreCells(const Vec2i &newPos, int sightRange, int teamIndex){
} }
//computes the fog of war texture, contained in the minimap //computes the fog of war texture, contained in the minimap
void World::computeFow(){ void World::computeFow(int factionIdxToTick) {
Chrono chrono; Chrono chrono;
chrono.start(); chrono.start();
//reset texture //reset texture
minimap.resetFowTex(); //if(factionIdxToTick == -1 || factionIdxToTick == 0) {
//if(factionIdxToTick == -1 || factionIdxToTick == this->thisFactionIndex) {
//if(frameCount % (GameConstants::updateFps) == 0) {
minimap.resetFowTex();
//}
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
//reset cells //reset cells
for(int i=0; i<map.getSurfaceW(); ++i){ if(factionIdxToTick == -1 || factionIdxToTick == this->thisFactionIndex) {
for(int j=0; j<map.getSurfaceH(); ++j){ for(int i=0; i<map.getSurfaceW(); ++i) {
for(int k=0; k<GameConstants::maxPlayers; ++k){ for(int j=0; j<map.getSurfaceH(); ++j) {
if(fogOfWar || k!=thisTeamIndex){ for(int k=0; k<GameConstants::maxPlayers; ++k) {
map.getSurfaceCell(i, j)->setVisible(k, false); if(fogOfWar || k != thisTeamIndex){
map.getSurfaceCell(i, j)->setVisible(k, false);
}
} }
} }
} }
@@ -996,13 +1013,15 @@ void World::computeFow(){
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
//compute cells //compute cells
for(int i=0; i<getFactionCount(); ++i){ for(int i=0; i<getFactionCount(); ++i) {
for(int j=0; j<getFaction(i)->getUnitCount(); ++j){ if(factionIdxToTick == -1 || factionIdxToTick == this->thisFactionIndex) {
Unit *unit= getFaction(i)->getUnit(j); for(int j=0; j<getFaction(i)->getUnitCount(); ++j) {
Unit *unit= getFaction(i)->getUnit(j);
//exploration //exploration
if(unit->isOperative()){ if(unit->isOperative()) {
exploreCells(unit->getCenteredPos(), unit->getType()->getSight(), unit->getTeam()); exploreCells(unit->getCenteredPos(), unit->getType()->getSight(), unit->getTeam());
}
} }
} }
} }
@@ -1010,14 +1029,16 @@ void World::computeFow(){
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
//fire //fire
for(int i=0; i<getFactionCount(); ++i){ for(int i=0; i<getFactionCount(); ++i) {
for(int j=0; j<getFaction(i)->getUnitCount(); ++j){ if(factionIdxToTick == -1 || factionIdxToTick == this->thisFactionIndex) {
Unit *unit= getFaction(i)->getUnit(j); for(int j=0; j<getFaction(i)->getUnitCount(); ++j){
Unit *unit= getFaction(i)->getUnit(j);
//fire //fire
ParticleSystem *fire= unit->getFire(); ParticleSystem *fire= unit->getFire();
if(fire!=NULL){ if(fire!=NULL){
fire->setActive(map.getSurfaceCell(Map::toSurfCoords(unit->getPos()))->isVisible(thisTeamIndex)); fire->setActive(map.getSurfaceCell(Map::toSurfCoords(unit->getPos()))->isVisible(thisTeamIndex));
}
} }
} }
} }
@@ -1025,53 +1046,48 @@ void World::computeFow(){
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
//compute texture //compute texture
for(int i=0; i<getFactionCount(); ++i){ for(int i=0; i<getFactionCount(); ++i) {
Faction *faction= getFaction(i); //if(factionIdxToTick == -1 || factionIdxToTick == this->thisFactionIndex) {
if(faction->getTeam()==thisTeamIndex){ Faction *faction= getFaction(i);
for(int j=0; j<faction->getUnitCount(); ++j){ if(faction->getTeam() == thisTeamIndex){
const Unit *unit= faction->getUnit(j); for(int j=0; j<faction->getUnitCount(); ++j){
if(unit->isOperative()){ const Unit *unit= faction->getUnit(j);
int sightRange= unit->getType()->getSight(); if(unit->isOperative()){
int sightRange= unit->getType()->getSight();
//iterate through all cells //iterate through all cells
PosCircularIterator pci(&map, unit->getPos(), sightRange+indirectSightRange); PosCircularIterator pci(&map, unit->getPos(), sightRange+indirectSightRange);
while(pci.next()){ while(pci.next()){
Vec2i pos= pci.getPos(); const Vec2i pos= pci.getPos();
Vec2i surfPos= Map::toSurfCoords(pos); Vec2i surfPos= Map::toSurfCoords(pos);
//compute max alpha
float maxAlpha= 0.0f;
if(surfPos.x>1 && surfPos.y>1 && surfPos.x<map.getSurfaceW()-2 && surfPos.y<map.getSurfaceH()-2){
maxAlpha= 1.f;
}
else if(surfPos.x>0 && surfPos.y>0 && surfPos.x<map.getSurfaceW()-1 && surfPos.y<map.getSurfaceH()-1){
maxAlpha= 0.3f;
}
//compute max alpha //compute alpha
float maxAlpha; float alpha=maxAlpha;
if(surfPos.x>1 && surfPos.y>1 && surfPos.x<map.getSurfaceW()-2 && surfPos.y<map.getSurfaceH()-2){ float dist= unit->getPos().dist(pos);
maxAlpha= 1.f; if(dist>sightRange){
alpha= clamp(1.f-(dist-sightRange)/(indirectSightRange), 0.f, maxAlpha);
}
minimap.incFowTextureAlphaSurface(surfPos, alpha);
} }
else if(surfPos.x>0 && surfPos.y>0 && surfPos.x<map.getSurfaceW()-1 && surfPos.y<map.getSurfaceH()-1){
maxAlpha= 0.3f;
}
else{
maxAlpha= 0.0f;
}
//compute alpha
float alpha;
float dist= unit->getPos().dist(pos);
if(dist>sightRange){
alpha= clamp(1.f-(dist-sightRange)/(indirectSightRange), 0.f, maxAlpha);
}
else{
alpha= maxAlpha;
}
minimap.incFowTextureAlphaSurface(surfPos, alpha);
} }
} }
} }
} //}
} }
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
} }
// WARNING! This id is critical! MAke sure it fits inside the network packet // WARNING! This id is critical! Make sure it fits inside the network packet
// (currently cannot be larger than 2,147,483,647) // (currently cannot be larger than 2,147,483,647)
// Make sure each faction has their own unique section of id #'s for proper // Make sure each faction has their own unique section of id #'s for proper
// multi-platform play // multi-platform play

View File

@@ -201,7 +201,8 @@ private:
//misc //misc
void tick(); void tick();
bool canTickFaction(int factionIdx); bool canTickFaction(int factionIdx);
void computeFow(); int tickFactionIndex();
void computeFow(int factionIdxToTick=-1);
void exploreCells(const Vec2i &newPos, int sightRange, int teamIndex); void exploreCells(const Vec2i &newPos, int sightRange, int teamIndex);
void updateAllFactionUnits(); void updateAllFactionUnits();