From 21277ad5c877d90680b757b058a3759e8f8b5559 Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Sun, 7 Aug 2011 19:07:15 +0300
Subject: Add auto adjust perfomance ability.

---
 src/client.cpp          |   1 +
 src/configuration.cpp   |   5 ++
 src/configuration.h     |   8 +++
 src/defaults.cpp        |   1 +
 src/game.cpp            | 131 +++++++++++++++++++++++++++++++++++++++++++++++-
 src/game.h              |  13 +++++
 src/gui/setup.cpp       |   3 ++
 src/gui/setup_video.cpp |  44 ++++++++++------
 src/gui/setup_video.h   |   2 +
 src/localplayer.h       |   6 +++
 src/map.cpp             |   6 +++
 11 files changed, 202 insertions(+), 18 deletions(-)

(limited to 'src')

diff --git a/src/client.cpp b/src/client.cpp
index 68f31bd27..01543d8be 100644
--- a/src/client.cpp
+++ b/src/client.cpp
@@ -849,6 +849,7 @@ int Client::exec()
             {
                 delete game;
                 game = 0;
+                Game::clearInstance();
                 ResourceManager *resman = ResourceManager::getInstance();
                 if (resman)
                     resman->cleanOrphans();
diff --git a/src/configuration.cpp b/src/configuration.cpp
index 5dca06b56..7d1f41fe5 100644
--- a/src/configuration.cpp
+++ b/src/configuration.cpp
@@ -66,6 +66,11 @@ void Configuration::setValue(const std::string &key, const std::string &value)
     }
 }
 
+void Configuration::setSilent(const std::string &key, const std::string &value)
+{
+    ConfigurationObject::setValue(key, value);
+}
+
 std::string ConfigurationObject::getValue(const std::string &key,
                                           const std::string &deflt) const
 {
diff --git a/src/configuration.h b/src/configuration.h
index 4b0238e9a..2e694002d 100644
--- a/src/configuration.h
+++ b/src/configuration.h
@@ -246,9 +246,14 @@ class Configuration : public ConfigurationObject
 
         void setValue(const std::string &key, const std::string &value);
 
+        void setSilent(const std::string &key, const std::string &value);
+
         inline void setValue(const std::string &key, const char *value)
         { setValue(key, std::string(value)); }
 
+        inline void setSilent(const std::string &key, const char *value)
+        { setSilent(key, std::string(value)); }
+
         inline void setValue(const std::string &key, float value)
         { setValue(key, toString(value)); }
 
@@ -267,6 +272,9 @@ class Configuration : public ConfigurationObject
         inline void setValue(const std::string &key, bool value)
         { setValue(key, value ? "1" : "0"); }
 
+        inline void setSilent(const std::string &key, bool value)
+        { setSilent(key, value ? "1" : "0"); }
+
         int resetIntValue(const std::string &key);
 
         bool resetBoolValue(const std::string &key);
diff --git a/src/defaults.cpp b/src/defaults.cpp
index fcb32bf06..7b5f5255a 100644
--- a/src/defaults.cpp
+++ b/src/defaults.cpp
@@ -209,6 +209,7 @@ DefaultsData* getConfigDefaults()
     AddDEF(configData, "enableMapReduce", true);
     AddDEF(configData, "showPlayersStatus", true);
     AddDEF(configData, "beingopacity", false);
+    AddDEF(configData, "adjustPerfomance", true);
     return configData;
 }
 
diff --git a/src/game.cpp b/src/game.cpp
index 494d35692..6abc15277 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -162,6 +162,8 @@ ChatTab *debugChatTab = NULL;
 TradeTab *tradeChatTab = NULL;
 BattleTab *battleChatTab = NULL;
 
+const unsigned adjustDelay = 10;
+
 /**
  * Initialize every game sub-engines in the right order
  */
@@ -331,8 +333,12 @@ Game *Game::mInstance = 0;
 
 Game::Game():
     mLastTarget(ActorSprite::UNKNOWN),
-    mCurrentMap(0), mMapName(""),
-    mValidSpeed(true), mLastAction(0)
+    mCurrentMap(0),
+    mMapName(""),
+    mValidSpeed(true),
+    mLastAction(0),
+    mNextAdjustTime(cur_time + adjustDelay),
+    mAdjustLevel(0)
 {
     spellManager = new SpellManager;
     spellShortcut = new SpellShortcut;
@@ -342,6 +348,8 @@ Game::Game():
 
     disconnectedDialog = NULL;
 
+    mAdjustPerfomance = config.getBoolValue("adjustPerfomance");
+
     // Create the viewport
     viewport = new Viewport;
     viewport->setDimension(gcn::Rectangle(0, 0, graphics->mWidth,
@@ -391,6 +399,9 @@ Game::~Game()
 {
     config.write();
     serverConfig.write();
+
+    resetAdjustLevel();
+
 //    delete mWindowMenu;
 //    mWindowMenu = 0;
 
@@ -504,6 +515,7 @@ void Game::logic()
     // Handle network stuff
     if (!Net::getGameHandler()->isConnected())
     {
+
         if (Client::getState() == STATE_CHANGE_MAP)
             return; // Not a problem here
 
@@ -530,6 +542,8 @@ void Game::logic()
     }
     else
     {
+        if (mAdjustPerfomance)
+            adjustPerfomance();
         if (disconnectedDialog)
         {
             disconnectedDialog->scheduleDelete();
@@ -538,6 +552,117 @@ void Game::logic()
     }
 }
 
+void Game::adjustPerfomance()
+{
+    if (mNextAdjustTime <= adjustDelay)
+    {
+        mNextAdjustTime = cur_time + adjustDelay;
+    }
+    else if (mNextAdjustTime < cur_time)
+    {
+        mNextAdjustTime = cur_time + adjustDelay;
+
+        if (mAdjustLevel > 3 || !player_node || player_node->getHalfAway()
+            || player_node->getAway())
+        {
+            return;
+        }
+
+        int maxFps = config.getIntValue("fpslimit");
+        if (!maxFps)
+            maxFps = 30;
+        else if (maxFps < 6)
+            maxFps = 6;
+
+        if (fps < maxFps - 5)
+        {
+            mAdjustLevel ++;
+            switch (mAdjustLevel)
+            {
+                case 1:
+                {
+                    if (config.getBoolValue("beingopacity"))
+                    {
+                        config.setValue("beingopacity", false);
+                        config.setSilent("beingopacity", true);
+                        if (localChatTab)
+                        {
+                            localChatTab->chatLog("Auto disable Show "
+                                "beings transparency", BY_SERVER);
+                        }
+                    }
+                    else
+                    {
+                        mNextAdjustTime = cur_time + 1;
+                    }
+                    break;
+                }
+                case 2:
+                    if (Particle::emitterSkip < 4)
+                    {
+                        Particle::emitterSkip = 4;
+//                        config.setValue("particleEmitterSkip", 3);
+                        if (localChatTab)
+                        {
+                            localChatTab->chatLog("Auto lower Particle "
+                                "effects", BY_SERVER);
+                        }
+                    }
+                    else
+                    {
+                        mNextAdjustTime = cur_time + 1;
+                    }
+                    break;
+                case 3:
+                    if (!config.getBoolValue("alphaCache"))
+                    {
+                        config.setValue("alphaCache", true);
+                        config.setSilent("alphaCache", false);
+                        if (localChatTab)
+                        {
+                            localChatTab->chatLog("Auto enable opacity cache",
+                                BY_SERVER);
+                        }
+                    }
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+}
+
+void Game::resetAdjustLevel()
+{
+    if (!mAdjustPerfomance)
+        return;
+
+    mNextAdjustTime = cur_time + adjustDelay;
+    switch (mAdjustLevel)
+    {
+        case 1:
+            config.setValue("beingopacity",
+                config.getBoolValue("beingopacity"));
+            break;
+        case 2:
+            config.setValue("beingopacity",
+                config.getBoolValue("beingopacity"));
+            Particle::emitterSkip = config.getIntValue(
+                "particleEmitterSkip") + 1;
+            break;
+        default:
+        case 3:
+            config.setValue("beingopacity",
+                config.getBoolValue("beingopacity"));
+            Particle::emitterSkip = config.getIntValue(
+                "particleEmitterSkip") + 1;
+            config.setValue("alphaCache",
+                config.getBoolValue("alphaCache"));
+            break;
+    }
+    mAdjustLevel = 0;
+}
+
 /**
  * The huge input handling method.
  */
@@ -1414,6 +1539,8 @@ void Game::handleInput()
  */
 void Game::changeMap(const std::string &mapPath)
 {
+    resetAdjustLevel();
+
     // Clean up floor items, beings and particles
     actorSpriteManager->clear();
 
diff --git a/src/game.h b/src/game.h
index 03c22587d..88dc9166b 100644
--- a/src/game.h
+++ b/src/game.h
@@ -65,6 +65,9 @@ class Game
         static Game *instance()
         { return mInstance; }
 
+        static void clearInstance()
+        { mInstance = 0; }
+
         /**
          * This method takes the game a small step further. It is called 100
          * times per second.
@@ -86,6 +89,13 @@ class Game
 
         void setValidSpeed();
 
+        void adjustPerfomance();
+
+        void resetAdjustLevel();
+
+        void setAdjustLevel(int n)
+        { mAdjustLevel = n; }
+
     private:
 
         void updateHistory(SDL_Event &event);
@@ -104,6 +114,9 @@ class Game
         bool mValidSpeed;
         int mLastAction;
         LastKey mLastKeys[MAX_LASTKEYS];
+        unsigned mNextAdjustTime;
+        int mAdjustLevel;
+        bool mAdjustPerfomance;
 
         static Game *mInstance;
 };
diff --git a/src/gui/setup.cpp b/src/gui/setup.cpp
index 76588a860..33ff6ddb7 100644
--- a/src/gui/setup.cpp
+++ b/src/gui/setup.cpp
@@ -23,6 +23,7 @@
 #include "gui/setup.h"
 
 #include "configuration.h"
+#include "game.h"
 #include "main.h"
 
 #include "gui/setup_audio.h"
@@ -129,6 +130,8 @@ Setup::~Setup()
 
 void Setup::action(const gcn::ActionEvent &event)
 {
+    if (Game::instance())
+        Game::instance()->resetAdjustLevel();
     if (event.getId() == "Apply")
     {
         setVisible(false);
diff --git a/src/gui/setup_video.cpp b/src/gui/setup_video.cpp
index 72c2e151b..3e5c56f64 100644
--- a/src/gui/setup_video.cpp
+++ b/src/gui/setup_video.cpp
@@ -284,6 +284,7 @@ Setup_Video::Setup_Video():
     mAltFps(config.getIntValue("altfpslimit")),
     mAlphaCache(config.getBoolValue("alphaCache")),
     mEnableMapReduce(config.getBoolValue("enableMapReduce")),
+    mAdjustPerfomance(config.getBoolValue("adjustPerfomance")),
     mBeingOpacity(config.getBoolValue("beingopacity")),
     mSpeechMode(static_cast<Being::Speech>(
         config.getIntValue("speech"))),
@@ -304,6 +305,8 @@ Setup_Video::Setup_Video():
     mAlphaCacheCheckBox(new CheckBox(_("Enable opacity cache"), mAlphaCache)),
     mEnableMapReduceCheckBox(new CheckBox(_("Enable map reduce"),
         mEnableMapReduce)),
+    mAdjustPerfomanceCheckBox(new CheckBox(_("Auto adjust perfomance"),
+        mAdjustPerfomance)),
     mBeingOpacityCheckBox(new CheckBox(_("Show beings transparency"),
                           mBeingOpacity)),
     mSpeechSlider(new Slider(0, 3)),
@@ -380,6 +383,7 @@ Setup_Video::Setup_Video():
     mParticleDetailField->setActionEventId("particledetailfield");
     mAlphaCacheCheckBox->setActionEventId("alphaCache");
     mEnableMapReduceCheckBox->setActionEventId("enableMapReduce");
+    mAdjustPerfomanceCheckBox->setActionEventId("adjustPerfomance");
     mOpenGLDropDown->setActionEventId("opengl");
 
     mModeList->addActionListener(this);
@@ -400,6 +404,7 @@ Setup_Video::Setup_Video():
 
     mAlphaCacheCheckBox->addKeyListener(this);
     mEnableMapReduceCheckBox->addKeyListener(this);
+    mAdjustPerfomanceCheckBox->addKeyListener(this);
 
     mSpeechLabel->setCaption(speechModeToString(mSpeechMode));
     mSpeechSlider->setValue(mSpeechMode);
@@ -433,27 +438,29 @@ Setup_Video::Setup_Video():
     place(1, 5, mPickupChatCheckBox, 1);
     place(2, 5, mPickupParticleCheckBox, 2);
 
-    place(0, 9, mAlphaSlider);
-    place(1, 9, alphaLabel, 3);
+    place(0, 9, mAdjustPerfomanceCheckBox, 6);
 
-    place(0, 10, mFpsSlider);
-    place(1, 10, mFpsCheckBox).setPadding(3);
-    place(2, 10, mFpsLabel).setPadding(1);
+    place(0, 10, mAlphaSlider);
+    place(1, 10, alphaLabel, 3);
 
-    place(0, 11, mAltFpsSlider);
-    place(1, 11, mAltFpsLabel).setPadding(3);
+    place(0, 11, mFpsSlider);
+    place(1, 11, mFpsCheckBox).setPadding(3);
+    place(2, 11, mFpsLabel).setPadding(1);
 
-    place(0, 12, mSpeechSlider);
-    place(1, 12, speechLabel);
-    place(2, 12, mSpeechLabel, 3).setPadding(2);
+    place(0, 12, mAltFpsSlider);
+    place(1, 12, mAltFpsLabel).setPadding(3);
 
-    place(0, 13, mOverlayDetailSlider);
-    place(1, 13, overlayDetailLabel);
-    place(2, 13, mOverlayDetailField, 3).setPadding(2);
+    place(0, 13, mSpeechSlider);
+    place(1, 13, speechLabel);
+    place(2, 13, mSpeechLabel, 3).setPadding(2);
 
-    place(0, 14, mParticleDetailSlider);
-    place(1, 14, particleDetailLabel);
-    place(2, 14, mParticleDetailField, 3).setPadding(2);
+    place(0, 14, mOverlayDetailSlider);
+    place(1, 14, overlayDetailLabel);
+    place(2, 14, mOverlayDetailField, 3).setPadding(2);
+
+    place(0, 15, mParticleDetailSlider);
+    place(1, 15, particleDetailLabel);
+    place(2, 15, mParticleDetailField, 3).setPadding(2);
 
     int width = 600;
 
@@ -552,6 +559,8 @@ void Setup_Video::apply()
 
     config.setValue("alphaCache", mAlphaCacheCheckBox->isSelected());
     config.setValue("enableMapReduce", mEnableMapReduceCheckBox->isSelected());
+    config.setValue("adjustPerfomance",
+        mAdjustPerfomanceCheckBox->isSelected());
     config.setValue("beingopacity", mBeingOpacityCheckBox->isSelected());
 
     // We sync old and new values at apply time
@@ -560,6 +569,7 @@ void Setup_Video::apply()
     mParticleEffectsEnabled = config.getBoolValue("particleeffects");
     mAlphaCache = config.getBoolValue("alphaCache");
     mEnableMapReduce = config.getBoolValue("enableMapReduce");
+    mAdjustPerfomance = config.getBoolValue("adjustPerfomance");
     mBeingOpacity = config.getBoolValue("beingopacity");
 
     mSpeechMode = static_cast<Being::Speech>(
@@ -587,6 +597,7 @@ void Setup_Video::cancel()
     mSpeechSlider->setValue(mSpeechMode);
     mAlphaCacheCheckBox->setSelected(mAlphaCache);
     mEnableMapReduceCheckBox->setSelected(mEnableMapReduce);
+    mAdjustPerfomanceCheckBox->setSelected(mAdjustPerfomance);
     mBeingOpacityCheckBox->setSelected(mBeingOpacity);
     mAlphaSlider->setValue(mOpacity);
     mOverlayDetailSlider->setValue(mOverlayDetail);
@@ -609,6 +620,7 @@ void Setup_Video::cancel()
     config.setValue("speech", static_cast<int>(mSpeechMode));
     config.setValue("alphaCache", mAlphaCache);
     config.setValue("enableMapReduce", mEnableMapReduce);
+    config.setValue("adjustPerfomance", mAdjustPerfomance);
     config.setValue("beingopacity", mBeingOpacity);
     config.setValue("guialpha", mOpacity);
     Image::setEnableAlpha(mOpacity != 1.0f);
diff --git a/src/gui/setup_video.h b/src/gui/setup_video.h
index f5d3208c8..9533877fb 100644
--- a/src/gui/setup_video.h
+++ b/src/gui/setup_video.h
@@ -65,6 +65,7 @@ class Setup_Video : public SetupTab, public gcn::KeyListener
         int mAltFps;
         bool mAlphaCache;
         bool mEnableMapReduce;
+        bool mAdjustPerfomance;
         bool mBeingOpacity;
         Being::Speech mSpeechMode;
 
@@ -92,6 +93,7 @@ class Setup_Video : public SetupTab, public gcn::KeyListener
 
         gcn::CheckBox *mAlphaCacheCheckBox;
         gcn::CheckBox *mEnableMapReduceCheckBox;
+        gcn::CheckBox *mAdjustPerfomanceCheckBox;
         gcn::CheckBox *mBeingOpacityCheckBox;
         gcn::Slider *mSpeechSlider;
         gcn::Label *mSpeechLabel;
diff --git a/src/localplayer.h b/src/localplayer.h
index df2a61b03..ebc347ce0 100644
--- a/src/localplayer.h
+++ b/src/localplayer.h
@@ -331,9 +331,15 @@ class LocalPlayer : public Being, public ActorSpriteListener,
 
         void setAway(const std::string &message);
 
+        bool getAway()
+        { return mAwayMode; }
+
         void setHalfAway(bool n)
         { mInactive = n; }
 
+        bool getHalfAway()
+        { return mInactive; }
+
         void afkRespond(ChatTab *tab, const std::string &nick);
 
         bool navigateTo(int x, int y);
diff --git a/src/map.cpp b/src/map.cpp
index 0903ef0e2..5723d5ec8 100644
--- a/src/map.cpp
+++ b/src/map.cpp
@@ -642,6 +642,12 @@ Map::Map(int width, int height, int tileWidth, int tileHeight):
     config.addListener("guialpha", this);
     config.addListener("beingopacity", this);
 
+    mOpacity = config.getFloatValue("guialpha");
+    if (mOpacity != 1.0f)
+        mBeingOpacity = config.getBoolValue("beingopacity");
+    else
+        mBeingOpacity = false;
+
 #ifdef USE_OPENGL
     mOpenGL = config.getIntValue("opengl");
 #else
-- 
cgit v1.2.3-70-g09d2