- attempted some speed improvements for unit lookup as well as exploreCells

This commit is contained in:
Mark Vejvoda 2010-07-14 06:59:55 +00:00
parent 5f16b486d7
commit 206170bc76
3 changed files with 65 additions and 5 deletions

View File

@ -60,7 +60,6 @@ void UnitUpdater::init(Game *game){
//skill dependent actions
void UnitUpdater::updateUnit(Unit *unit){
SoundRenderer &soundRenderer= SoundRenderer::getInstance();
//play skill sound
@ -121,8 +120,7 @@ void UnitUpdater::updateUnit(Unit *unit){
//VERY IMPORTANT: compute next state depending on the first order of the list
void UnitUpdater::updateUnitCommand(Unit *unit){
//if unis has command process it
//if unit has command process it
if(unit->anyCommand()) {
unit->getCurrCommand()->getCommandType()->update(this, unit);
}
@ -132,7 +130,7 @@ void UnitUpdater::updateUnitCommand(Unit *unit){
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
unit->setCurrSkill(scStop);
if(unit->getType()->hasCommandClass(ccStop)){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
unit->giveCommand(new Command(unit->getType()->getFirstCtOfClass(ccStop)));
}
}

View File

@ -247,6 +247,9 @@ void World::update(){
//tick
if(frameCount%GameConstants::updateFps==0){
computeFow();
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
tick();
}
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
@ -296,7 +299,11 @@ void World::tick(){
Unit* World::findUnitById(int id){
for(int i= 0; i<getFactionCount(); ++i){
Faction* faction= getFaction(i);
Unit* unit = faction->findUnit(id);
if(unit != NULL) {
return unit;
}
/*
for(int j= 0; j<faction->getUnitCount(); ++j){
Unit* unit= faction->getUnit(j);
@ -304,6 +311,7 @@ Unit* World::findUnitById(int id){
return unit;
}
}
*/
}
return NULL;
}
@ -727,6 +735,28 @@ void World::initMap(){
// ==================== exploration ====================
void World::exploreCells(const Vec2i &newPos, int sightRange, int teamIndex){
// Experimental cache lookup of previously calculated cells + sight range
for(int idx = 0; idx < ExploredCellsLookupItemCache.size(); ++idx) {
ExploredCellsLookupItem &item = ExploredCellsLookupItemCache[idx];
if(item.pos == newPos && item.sightRange == sightRange && item.teamIndex == teamIndex) {
std::vector<SurfaceCell *> &cellList = item.exploredCellList;
for(int idx2 = 0; idx2 < cellList.size(); ++idx2) {
SurfaceCell *sc = cellList[idx2];
sc->setExplored(teamIndex, true);
}
cellList = item.visibleCellList;
for(int idx2 = 0; idx2 < cellList.size(); ++idx2) {
SurfaceCell *sc = cellList[idx2];
sc->setVisible(teamIndex, true);
}
break;
}
}
ExploredCellsLookupItem item;
item.pos = newPos;
item.sightRange = sightRange;
item.teamIndex = teamIndex;
Vec2i newSurfPos= Map::toSurfCoords(newPos);
int surfSightRange= sightRange/Map::cellScale+1;
@ -743,23 +773,34 @@ void World::exploreCells(const Vec2i &newPos, int sightRange, int teamIndex){
//explore
if(Vec2i(0).dist(currRelPos) < surfSightRange+indirectSightRange+1){
sc->setExplored(teamIndex, true);
item.exploredCellList.push_back(sc);
}
//visible
if(Vec2i(0).dist(currRelPos) < surfSightRange){
sc->setVisible(teamIndex, true);
item.visibleCellList.push_back(sc);
}
}
}
}
if(item.exploredCellList.size() > 0 || item.visibleCellList.size() > 0) {
ExploredCellsLookupItemCache.push_back(item);
}
}
//computes the fog of war texture, contained in the minimap
void World::computeFow(){
Chrono chrono;
chrono.start();
//reset texture
minimap.resetFowTex();
if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
//reset cells
for(int i=0; i<map.getSurfaceW(); ++i){
for(int j=0; j<map.getSurfaceH(); ++j){
@ -771,6 +812,8 @@ 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());
//compute cells
for(int i=0; i<getFactionCount(); ++i){
for(int j=0; j<getFaction(i)->getUnitCount(); ++j){
@ -783,6 +826,8 @@ 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());
//fire
for(int i=0; i<getFactionCount(); ++i){
for(int j=0; j<getFaction(i)->getUnitCount(); ++j){
@ -796,6 +841,8 @@ 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());
//compute texture
for(int i=0; i<getFactionCount(); ++i){
Faction *faction= getFaction(i);
@ -839,6 +886,8 @@ 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());
}
// WARNING! This id is critical! MAke sure it fits inside the network packet

View File

@ -52,10 +52,23 @@ class RoutePlanner;
/// The game world: Map + Tileset + TechTree
// =====================================================
class ExploredCellsLookupItem {
public:
Vec2i pos;
int sightRange;
int teamIndex;
std::vector<SurfaceCell *> exploredCellList;
std::vector<SurfaceCell *> visibleCellList;
};
class World{
private:
typedef vector<Faction> Factions;
std::vector<ExploredCellsLookupItem> ExploredCellsLookupItemCache;
public:
static const int generationArea= 100;
static const float airHeight;