diff --git a/source/glest_game/ai/path_finder.cpp b/source/glest_game/ai/path_finder.cpp index aec61c56..2a7ca499 100644 --- a/source/glest_game/ai/path_finder.cpp +++ b/source/glest_game/ai/path_finder.cpp @@ -223,7 +223,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos){ Node *lastNode= node; - //if consumed all nodes find best node (to avoid strage behaviour) + //if consumed all nodes find best node (to avoid strange behaviour) if(nodeLimitReached){ for(Nodes::iterator it= closedNodes.begin(); it!=closedNodes.end(); ++it){ if((*it)->heuristic < lastNode->heuristic){ diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 16dcf74b..bcbfa2aa 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -614,60 +614,34 @@ void Game::updateCamera(){ //render void Game::render() { - //SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d\n",__FILE__,__FUNCTION__,__LINE__,renderFps); - // Ensure the camera starts in the right position if(isFirstRender == true) { isFirstRender = false; -/* - Map *map= world.getMap(); - const Vec2i &v= map->getStartLocation(world.getThisFaction()->getStartLocationIndex()); - gameCamera.init(map->getW(), map->getH()); - gameCamera.setPos(Vec2f(v.x, v.y)); - - //const Vec2i &mapPos = Map::toSurfCoords(v); - Renderer &renderer= Renderer::getInstance(); - Vec3f screenPos = renderer.computeScreenPosition(gameCamera.getPos()); - SDL_WarpMouse(v.x, v.y); -*/ - gameCamera.resetPosition(); this->restoreToStartXY(); } renderFps++; renderWorker(); - //SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d\n",__FILE__,__FUNCTION__,__LINE__,renderFps); } void Game::renderWorker() { - //Chrono chrono; - //chrono.start(); + Chrono chrono; + chrono.start(); - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - //program->getWindow()->makeCurrentGl(); - - //renderFps++; - //if(renderFps >= MIN_RENDER_FPS_ALLOWED) { render3d(); - //} - //if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %d\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %d [render3d]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) chrono.start(); - //chrono.start(); render2d(); - //if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %d\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %d [render2d]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) chrono.start(); - //chrono.start(); Renderer::getInstance().swapBuffers(); - //if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %d\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); - - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %d [swap buffers]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); } // ==================== tick ==================== @@ -1204,30 +1178,28 @@ void Game::render3d(){ Renderer &renderer= Renderer::getInstance(); //init - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.reset3d(); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [reset3d]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.computeVisibleQuad(); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [computeVisibleQuad]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.loadGameCameraMatrix(); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [loadGameCameraMatrix]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.setupLighting(); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [setupLighting]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); //shadow map //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.renderShadowsToTexture(avgRenderFps); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderShadowsToTexture]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); //clear buffers @@ -1239,44 +1211,44 @@ void Game::render3d(){ //surface //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.renderSurface(avgRenderFps,world.getFrameCount()); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderSurface]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); //selection circles //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.renderSelectionEffects(); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderSelectionEffects]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); //units //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.renderUnits(avgRenderFps,world.getFrameCount()); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderUnits]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); //objects //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.renderObjects(avgRenderFps,world.getFrameCount()); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); //water //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.renderWater(); renderer.renderWaterEffects(); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderWater]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); //particles //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.renderParticleManager(rsGame); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderParticleManager]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); if(chrono.getMillis() > 0) chrono.start(); //mouse 3d //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); renderer.renderMouse3d(); - if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderMouse3d]\n",__FILE__,__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); } void Game::render2d(){ diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index af3e53b5..e5795730 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -34,7 +34,7 @@ using namespace Shared::Util; namespace Glest{ namespace Game{ -const int UnitPathBasic::maxBlockCount= 10; +const int UnitPathBasic::maxBlockCount= GameConstants::updateFps / 2; UnitPathBasic::UnitPathBasic() { this->blockCount = 0; diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index 31f0373c..93c68a82 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -53,6 +53,7 @@ UnitUpdater::UnitUpdater() { this->scriptManager= NULL; this->routePlanner = NULL; this->pathFinder = NULL; + UnitRangeCellsLookupItemCacheTimerCount = 0; } void UnitUpdater::init(Game *game){ @@ -66,6 +67,7 @@ void UnitUpdater::init(Game *game){ this->scriptManager= game->getScriptManager(); this->routePlanner = NULL; this->pathFinder = NULL; + UnitRangeCellsLookupItemCacheTimerCount = 0; switch(this->game->getGameSettings()->getPathFinderType()) { case pfBasic: @@ -88,7 +90,10 @@ UnitUpdater::~UnitUpdater() { // ==================== progress skills ==================== //skill dependent actions -void UnitUpdater::updateUnit(Unit *unit){ +void UnitUpdater::updateUnit(Unit *unit) { + Chrono chrono; + chrono.start(); + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); //play skill sound @@ -102,6 +107,9 @@ void UnitUpdater::updateUnit(Unit *unit){ } } + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld [play skill sound]\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(chrono.getMillis() > 0) chrono.start(); + //start attack particle system if(unit->getCurrSkill()->getClass()==scAttack){ const AttackSkillType *ast= static_cast(unit->getCurrSkill()); @@ -111,22 +119,37 @@ void UnitUpdater::updateUnit(Unit *unit){ } } + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld [start attack particles]\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(chrono.getMillis() > 0) chrono.start(); + //update unit if(unit->update()){ //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld [update unit check]\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(chrono.getMillis() > 0) chrono.start(); + updateUnitCommand(unit); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld [update unit command]\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(chrono.getMillis() > 0) chrono.start(); + //if unit is out of EP, it stops if(unit->computeEp()){ unit->setCurrSkill(scStop); unit->cancelCommand(); } + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld [compute ep]\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(chrono.getMillis() > 0) chrono.start(); + //move unit in cells if(unit->getCurrSkill()->getClass()==scMove){ world->moveUnitCells(unit); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld [move unit cells]\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(chrono.getMillis() > 0) chrono.start(); + //play water sound if(map->getCell(unit->getPos())->getHeight()getWaterLevel() && unit->getCurrField()==fLand){ soundRenderer.playFx( @@ -134,6 +157,9 @@ void UnitUpdater::updateUnit(Unit *unit){ unit->getCurrVector(), gameCamera->getPos() ); + + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld [play water sound]\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(chrono.getMillis() > 0) chrono.start(); } } } @@ -141,6 +167,8 @@ void UnitUpdater::updateUnit(Unit *unit){ //unit death if(unit->isDead() && unit->getCurrSkill()->getClass()!=scDie){ unit->kill(); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld [kill unit]\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(chrono.getMillis() > 0) chrono.start(); } } @@ -149,6 +177,9 @@ void UnitUpdater::updateUnit(Unit *unit){ //VERY IMPORTANT: compute next state depending on the first order of the list void UnitUpdater::updateUnitCommand(Unit *unit){ + Chrono chrono; + chrono.start(); + //if unit has command process it if(unit->anyCommand()) { unit->getCurrCommand()->getCommandType()->update(this, unit); @@ -163,11 +194,16 @@ void UnitUpdater::updateUnitCommand(Unit *unit){ unit->giveCommand(new Command(unit->getType()->getFirstCtOfClass(ccStop))); } } + + 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) chrono.start(); } // ==================== updateStop ==================== void UnitUpdater::updateStop(Unit *unit){ + Chrono chrono; + chrono.start(); Command *command= unit->getCurrCommand(); const StopCommandType *sct = static_cast(command->getCommandType()); @@ -208,11 +244,16 @@ void UnitUpdater::updateStop(Unit *unit){ unit->giveCommand(new Command(unit->getType()->getFirstCtOfClass(ccMove), escapePos)); } } + + 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) chrono.start(); } // ==================== updateMove ==================== void UnitUpdater::updateMove(Unit *unit){ + Chrono chrono; + chrono.start(); Command *command= unit->getCurrCommand(); const MoveCommandType *mct= static_cast(command->getCommandType()); @@ -246,12 +287,17 @@ void UnitUpdater::updateMove(Unit *unit){ default: unit->finishCommand(); } + + 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) chrono.start(); } // ==================== updateAttack ==================== void UnitUpdater::updateAttack(Unit *unit){ + Chrono chrono; + chrono.start(); Command *command= unit->getCurrCommand(); const AttackCommandType *act= static_cast(command->getCommandType()); @@ -306,12 +352,17 @@ void UnitUpdater::updateAttack(Unit *unit){ unit->finishCommand(); } } + + 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) chrono.start(); } // ==================== updateAttackStopped ==================== void UnitUpdater::updateAttackStopped(Unit *unit){ + Chrono chrono; + chrono.start(); Command *command= unit->getCurrCommand(); const AttackStoppedCommandType *asct= static_cast(command->getCommandType()); @@ -324,12 +375,17 @@ void UnitUpdater::updateAttackStopped(Unit *unit){ else{ unit->setCurrSkill(asct->getStopSkillType()); } + + 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) chrono.start(); } // ==================== updateBuild ==================== void UnitUpdater::updateBuild(Unit *unit){ + Chrono chrono; + chrono.start(); Command *command= unit->getCurrCommand(); const BuildCommandType *bct= static_cast(command->getCommandType()); @@ -466,12 +522,17 @@ void UnitUpdater::updateBuild(Unit *unit){ } } } + + 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) chrono.start(); } // ==================== updateHarvest ==================== void UnitUpdater::updateHarvest(Unit *unit){ + Chrono chrono; + chrono.start(); Command *command= unit->getCurrCommand(); const HarvestCommandType *hct= static_cast(command->getCommandType()); @@ -609,37 +670,37 @@ void UnitUpdater::updateHarvest(Unit *unit){ // hct has changed to a different harvest command. unit->setCurrSkill(hct->getStopLoadedSkillType()); // this is actually the wrong animation unit->getPath()->clear(); - return; } + else { + // if there is a resource, continue working, until loaded + unit->update2(); + if (unit->getProgress2() >= hct->getHitsPerUnit()) { + if (unit->getLoadCount() < hct->getMaxLoad()) { + unit->setProgress2(0); + unit->setLoadCount(unit->getLoadCount() + 1); - // if there is a resource, continue working, until loaded - unit->update2(); - if (unit->getProgress2() >= hct->getHitsPerUnit()) { - if (unit->getLoadCount() < hct->getMaxLoad()) { - unit->setProgress2(0); - unit->setLoadCount(unit->getLoadCount() + 1); + //if resource exausted, then delete it and stop + if (r->decAmount(1)) { + const ResourceType *rt = r->getType(); + sc->deleteResource(); - //if resource exausted, then delete it and stop - if (r->decAmount(1)) { - const ResourceType *rt = r->getType(); - sc->deleteResource(); + switch(this->game->getGameSettings()->getPathFinderType()) { + case pfBasic: + break; + case pfRoutePlanner: + world->getCartographer()->onResourceDepleted(Map::toSurfCoords(unit->getTargetPos()), rt); + break; + default: + throw runtime_error("detected unsupported pathfinder type!"); + } - switch(this->game->getGameSettings()->getPathFinderType()) { - case pfBasic: - break; - case pfRoutePlanner: - world->getCartographer()->onResourceDepleted(Map::toSurfCoords(unit->getTargetPos()), rt); - break; - default: - throw runtime_error("detected unsupported pathfinder type!"); - } - - unit->setCurrSkill(hct->getStopLoadedSkillType()); + unit->setCurrSkill(hct->getStopLoadedSkillType()); + } + } + if (unit->getLoadCount() >= hct->getMaxLoad()) { + unit->setCurrSkill(hct->getStopLoadedSkillType()); + unit->getPath()->clear(); } - } - if (unit->getLoadCount() >= hct->getMaxLoad()) { - unit->setCurrSkill(hct->getStopLoadedSkillType()); - unit->getPath()->clear(); } } } @@ -649,12 +710,17 @@ void UnitUpdater::updateHarvest(Unit *unit){ } } + + 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) chrono.start(); } // ==================== updateRepair ==================== void UnitUpdater::updateRepair(Unit *unit){ + Chrono chrono; + chrono.start(); Command *command= unit->getCurrCommand(); const RepairCommandType *rct= static_cast(command->getCommandType()); @@ -723,12 +789,17 @@ void UnitUpdater::updateRepair(Unit *unit){ } } } + + 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) chrono.start(); } // ==================== updateProduce ==================== void UnitUpdater::updateProduce(Unit *unit){ + Chrono chrono; + chrono.start(); Command *command= unit->getCurrCommand(); const ProduceCommandType *pct= static_cast(command->getCommandType()); @@ -779,12 +850,17 @@ void UnitUpdater::updateProduce(Unit *unit){ } } } + + 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) chrono.start(); } // ==================== updateUpgrade ==================== void UnitUpdater::updateUpgrade(Unit *unit){ + Chrono chrono; + chrono.start(); Command *command= unit->getCurrCommand(); const UpgradeCommandType *uct= static_cast(command->getCommandType()); @@ -802,11 +878,16 @@ void UnitUpdater::updateUpgrade(Unit *unit){ unit->getFaction()->finishUpgrade(uct->getProducedUpgrade()); } } + + 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) chrono.start(); } // ==================== updateMorph ==================== void UnitUpdater::updateMorph(Unit *unit){ + Chrono chrono; + chrono.start(); Command *command= unit->getCurrCommand(); const MorphCommandType *mct= static_cast(command->getCommandType()); @@ -871,6 +952,9 @@ void UnitUpdater::updateMorph(Unit *unit){ } } + + 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) chrono.start(); } // ==================== PRIVATE ==================== @@ -882,6 +966,8 @@ void UnitUpdater::hit(Unit *attacker){ } void UnitUpdater::hit(Unit *attacker, const AttackSkillType* ast, const Vec2i &targetPos, Field targetField){ + Chrono chrono; + chrono.start(); //hit attack positions if(ast->getSplash()){ @@ -904,9 +990,13 @@ void UnitUpdater::hit(Unit *attacker, const AttackSkillType* ast, const Vec2i &t } } + 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) chrono.start(); } void UnitUpdater::damage(Unit *attacker, const AttackSkillType* ast, Unit *attacked, float distance){ + Chrono chrono; + chrono.start(); //get vars float damage= ast->getTotalAttackStrength(attacker->getTotalUpgrade()); @@ -941,6 +1031,9 @@ void UnitUpdater::damage(Unit *attacker, const AttackSkillType* ast, Unit *attac } scriptManager->onUnitDied(attacked); } + + 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) chrono.start(); } void UnitUpdater::startAttackParticleSystem(Unit *unit){ @@ -992,6 +1085,8 @@ void UnitUpdater::startAttackParticleSystem(Unit *unit){ //looks for a resource of type rt, if rt==NULL looks for any //resource the unit can harvest bool UnitUpdater::searchForResource(Unit *unit, const HarvestCommandType *hct){ + Chrono chrono; + chrono.start(); Vec2i pos= unit->getCurrCommand()->getPos(); @@ -1003,6 +1098,10 @@ bool UnitUpdater::searchForResource(Unit *unit, const HarvestCommandType *hct){ if(r!=NULL){ if(hct->canHarvest(r->getType())){ unit->getCurrCommand()->setPos(Vec2i(i, j)); + + 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) chrono.start(); + return true; } } @@ -1010,6 +1109,10 @@ bool UnitUpdater::searchForResource(Unit *unit, const HarvestCommandType *hct){ } } } + + 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) chrono.start(); + return false; } @@ -1028,59 +1131,118 @@ bool UnitUpdater::attackableOnRange(const Unit *unit, Unit **rangedPtr, const At return unitOnRange(unit, range, rangedPtr, ast); } +bool UnitUpdater::findCachedCellsEnemies(Vec2i center, Vec2f floatCenter, + int range, int size, vector &enemies, + const AttackSkillType *ast, const Unit *unit, + const Unit *commandTarget) { + bool result = false; + + std::map > > >::iterator iterFind = UnitRangeCellsLookupItemCache.find(center); + if(iterFind != UnitRangeCellsLookupItemCache.end()) { + std::map > >::iterator iterFind2 = iterFind->second.find(floatCenter); + if(iterFind2 != iterFind->second.end()) { + std::map >::iterator iterFind3 = iterFind2->second.find(range); + if(iterFind3 != iterFind2->second.end()) { + std::map::iterator iterFind4 = iterFind3->second.find(size); + if(iterFind4 != iterFind3->second.end()) { + result = true; + + std::vector &cellList = iterFind4->second.rangeCellList; + for(int idx = 0; idx < cellList.size(); ++idx) { + Cell *cell = cellList[idx]; + + findEnemiesForCell(ast,cell,unit,commandTarget,enemies); + } + } + } + } + } + + return result; +} + +void UnitUpdater::findEnemiesForCell(const AttackSkillType *ast, Cell *cell, const Unit *unit, + const Unit *commandTarget,vector &enemies) { + //all fields + for(int k=0; k(k); + + //check field + if((ast == NULL || ast->getAttackField(f))) { + Unit *possibleEnemy = cell->getUnit(f); + + //check enemy + if(possibleEnemy != NULL && possibleEnemy->isAlive()) { + if((!unit->isAlly(possibleEnemy) && commandTarget == NULL) || + commandTarget == possibleEnemy) { + enemies.push_back(possibleEnemy); + } + } + } + } +} + //if the unit has any enemy on range -bool UnitUpdater::unitOnRange(const Unit *unit, int range, Unit **rangedPtr, const AttackSkillType *ast){ +bool UnitUpdater::unitOnRange(const Unit *unit, int range, Unit **rangedPtr, + const AttackSkillType *ast){ + Chrono chrono; + chrono.start(); vector enemies; //we check command target - const Unit *commandTarget= NULL; - if(unit->anyCommand()){ - commandTarget= static_cast(unit->getCurrCommand()->getUnit()); + const Unit *commandTarget = NULL; + if(unit->anyCommand()) { + commandTarget = static_cast(unit->getCurrCommand()->getUnit()); } - if(commandTarget!=NULL && commandTarget->isDead()){ - commandTarget= NULL; + if(commandTarget != NULL && commandTarget->isDead()) { + commandTarget = NULL; } //aux vars - int size= unit->getType()->getSize(); - Vec2i center= unit->getPos(); - Vec2f floatCenter= unit->getFloatCenteredPos(); + int size = unit->getType()->getSize(); + Vec2i center = unit->getPos(); + Vec2f floatCenter = unit->getFloatCenteredPos(); - //nearby cells - for(int i=center.x-range; iisInside(i, j) && streflop::floor(floatCenter.dist(Vec2f((float)i, (float)j))) <= (range+1)){ + if(map->isInside(i, j) && streflop::floor(floatCenter.dist(Vec2f((float)i, (float)j))) <= (range+1)){ #else - if(map->isInside(i, j) && floor(floatCenter.dist(Vec2f((float)i, (float)j))) <= (range+1)){ + if(map->isInside(i, j) && floor(floatCenter.dist(Vec2f((float)i, (float)j))) <= (range+1)){ #endif - //all fields - for(int k=0; k(k); + Cell *cell = map->getCell(i,j); + findEnemiesForCell(ast,cell,unit,commandTarget,enemies); - //check field - if((ast==NULL || ast->getAttackField(f))){ - Unit *possibleEnemy= map->getCell(i, j)->getUnit(f); - - //check enemy - if(possibleEnemy!=NULL && possibleEnemy->isAlive()){ - if((!unit->isAlly(possibleEnemy) && commandTarget==NULL) || commandTarget==possibleEnemy){ - enemies.push_back(possibleEnemy); - } - } - } + cacheItem.rangeCellList.push_back(cell); } } } + + // Ok update our caches with the latest info + if(cacheItem.rangeCellList.size() > 0) { + cacheItem.UnitRangeCellsLookupItemCacheTimerCountIndex = UnitRangeCellsLookupItemCacheTimerCount++; + UnitRangeCellsLookupItemCache[center][floatCenter][range][size] = cacheItem; + } } //attack enemies that can attack first for(int i=0; igetType()->hasSkillClass(scAttack)){ *rangedPtr= enemies[i]; + + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld, unit %s, range = %d, size = %d, foundInCache = %d, enemies.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis(),unit->getFullName().c_str(),range,size,foundInCache,enemies.size()); + if(chrono.getMillis() > 0) chrono.start(); + return true; } } @@ -1088,9 +1250,16 @@ bool UnitUpdater::unitOnRange(const Unit *unit, int range, Unit **rangedPtr, con //any enemy if(enemies.size()>0){ *rangedPtr= enemies.front(); + + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld, unit %s, range = %d, size = %d, foundInCache = %d, enemies.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis(),unit->getFullName().c_str(),range,size,foundInCache,enemies.size()); + if(chrono.getMillis() > 0) chrono.start(); + return true; } + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld, unit %s, range = %d, size = %d, foundInCache = %d, enemies.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis(),unit->getFullName().c_str(),range,size,foundInCache,enemies.size()); + if(chrono.getMillis() > 0) chrono.start(); + return false; } diff --git a/source/glest_game/world/unit_updater.h b/source/glest_game/world/unit_updater.h index 400c1c14..c3daabbf 100644 --- a/source/glest_game/world/unit_updater.h +++ b/source/glest_game/world/unit_updater.h @@ -36,8 +36,18 @@ class RoutePlanner; // ===================================================== class ParticleDamager; +class Cell; -class UnitUpdater{ +class UnitRangeCellsLookupItem { +public: + + int UnitRangeCellsLookupItemCacheTimerCountIndex; + std::vector rangeCellList; + + static time_t lastDebug; +}; + +class UnitUpdater { private: friend class ParticleDamager; @@ -59,6 +69,17 @@ private: Game *game; RandomGen random; + std::map > > > UnitRangeCellsLookupItemCache; + //std::map ExploredCellsLookupItemCacheTimer; + int UnitRangeCellsLookupItemCacheTimerCount; + + bool findCachedCellsEnemies(Vec2i center, Vec2f floatCenter, int range, + int size, vector &enemies, + const AttackSkillType *ast, const Unit *unit, + const Unit *commandTarget); + void findEnemiesForCell(const AttackSkillType *ast, Cell *cell, const Unit *unit, + const Unit *commandTarget,vector &enemies); + public: UnitUpdater(); void init(Game *game); diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index 68076ee6..24939007 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -355,10 +355,9 @@ void World::update(){ bool needToTick = canTickWorld(); if(needToTick == true) { //if(frameCount % (GameConstants::updateFps / GameConstants::maxPlayers) == 0) { - 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()); + if(chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld [world tick]\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); } bool World::canTickWorld() const {