[Orxonox-commit 6375] r11032 - code/branches/presentationHS15/src/orxonox/gametypes

landauf at orxonox.net landauf at orxonox.net
Mon Jan 4 00:07:42 CET 2016


Author: landauf
Date: 2016-01-04 00:07:42 +0100 (Mon, 04 Jan 2016)
New Revision: 11032

Modified:
   code/branches/presentationHS15/src/orxonox/gametypes/Dynamicmatch.cc
   code/branches/presentationHS15/src/orxonox/gametypes/Dynamicmatch.h
Log:
fixed another situation where a timer may keep existing after the level was unloaded.
there is, however, yet another bug in this piece of code (marked with a TODO): a spaceship can be destroyed before the timer is executed which leads to a crash.

Modified: code/branches/presentationHS15/src/orxonox/gametypes/Dynamicmatch.cc
===================================================================
--- code/branches/presentationHS15/src/orxonox/gametypes/Dynamicmatch.cc	2016-01-03 20:46:11 UTC (rev 11031)
+++ code/branches/presentationHS15/src/orxonox/gametypes/Dynamicmatch.cc	2016-01-03 23:07:42 UTC (rev 11032)
@@ -85,6 +85,12 @@
         this->setHUDTemplate("DynamicmatchHUD");
     }
 
+    Dynamicmatch::~Dynamicmatch()
+    {
+        for (std::set<Timer*>::iterator it = this->piggyTimers_.begin(); it != this->piggyTimers_.end(); ++it)
+            delete (*it);
+    }
+
     void Dynamicmatch::setConfigValues()
     {
         SetConfigValue(gameTime_, 180);
@@ -311,8 +317,10 @@
         {
             spaceship->addSpeedFactor(5);
             ExecutorPtr executor = createExecutor(createFunctor(&Dynamicmatch::resetSpeedFactor, this));
-            executor->setDefaultValue(0, spaceship);
-            new Timer(10, false, executor, true);
+            Timer* timer = new Timer(10, false, executor);
+            executor->setDefaultValue(0, spaceship); // TODO: use WeakPtr because spaceship can be destroyed in the meantime
+            executor->setDefaultValue(1, timer);
+            this->piggyTimers_.insert(timer);
         }
     }
 
@@ -589,12 +597,15 @@
         return this->playerParty_[player];
     }
 
-    void Dynamicmatch::resetSpeedFactor(SpaceShip* spaceship)// helper function
+    void Dynamicmatch::resetSpeedFactor(SpaceShip* spaceship, Timer* timer)// helper function
     {
         if (spaceship)
         {
             spaceship->addSpeedFactor(1.0f/5.0f);
         }
+
+        this->piggyTimers_.erase(timer);
+        delete timer;
     }
 
     bool Dynamicmatch::playerChangedName(PlayerInfo* player) //standardfunction

Modified: code/branches/presentationHS15/src/orxonox/gametypes/Dynamicmatch.h
===================================================================
--- code/branches/presentationHS15/src/orxonox/gametypes/Dynamicmatch.h	2016-01-03 20:46:11 UTC (rev 11031)
+++ code/branches/presentationHS15/src/orxonox/gametypes/Dynamicmatch.h	2016-01-03 23:07:42 UTC (rev 11032)
@@ -32,6 +32,7 @@
 #include "OrxonoxPrereqs.h"
 
 #include <map>
+#include <set>
 #include <vector>
 
 #include "tools/Timer.h"
@@ -44,7 +45,7 @@
     {
         public:
             Dynamicmatch(Context* context);
-            virtual ~Dynamicmatch() {}
+            virtual ~Dynamicmatch();
 
             bool notEnoughPigs;
             bool notEnoughKillers;
@@ -76,7 +77,7 @@
             virtual void furtherInstructions();*/
             virtual void rewardPig();
             void grantPigBoost(SpaceShip* spaceship); // Grant the piggy a boost.
-            void resetSpeedFactor(SpaceShip* spaceship);
+            void resetSpeedFactor(SpaceShip* spaceship, Timer* timer);
             void tick (float dt);// used to end the game
             SpawnPoint* getBestSpawnPoint(PlayerInfo* player) const;
 
@@ -87,6 +88,7 @@
 
             std::map< PlayerInfo*, int > playerParty_; //player's parties are recorded here
             std::vector<ColourValue> partyColours_; //aus TeamDeathmatch
+            std::set<Timer*> piggyTimers_;
             unsigned int numberOf[3]; //array to count number of chasers, pigs, killers
             float pointsPerTime;
             float gameTime_;   // from UnderAttack




More information about the Orxonox-commit mailing list