[Orxonox-commit 2779] r7482 - code/trunk/src/modules/objects

dafrick at orxonox.net dafrick at orxonox.net
Thu Sep 23 11:12:22 CEST 2010


Author: dafrick
Date: 2010-09-23 11:12:22 +0200 (Thu, 23 Sep 2010)
New Revision: 7482

Modified:
   code/trunk/src/modules/objects/Script.cc
   code/trunk/src/modules/objects/Script.h
Log:

Fixed Script class. Works now without having to rely on guessing how long it is going to take for the object to be synchronized.


Modified: code/trunk/src/modules/objects/Script.cc
===================================================================
--- code/trunk/src/modules/objects/Script.cc	2010-09-22 21:30:33 UTC (rev 7481)
+++ code/trunk/src/modules/objects/Script.cc	2010-09-23 09:12:22 UTC (rev 7482)
@@ -1,3 +1,4 @@
+
 /*
  *   ORXONOX - the hottest 3D action shooter ever to exist
  *                    > www.orxonox.net <
@@ -46,13 +47,15 @@
 {
     CreateFactory(Script);
 
-    registerMemberNetworkFunction(Script, execute);
+    registerStaticNetworkFunction(Script::executeHelper);
 
     // Initializing constants.
     /*static*/ const std::string Script::NORMAL = "normal";
     /*static*/ const std::string Script::LUA = "lua";
     /*static*/ const int Script::INF = -1;
 
+    /*static*/ LuaState* Script::LUA_STATE = NULL;
+
     /**
     @brief
         Constructor. Registers and initializes the object.
@@ -64,16 +67,11 @@
         RegisterObject(Script);
 
         // Initialize variables.
-        this->luaState_ = NULL;
         this->remainingExecutions_ = Script::INF;
         this->mode_ = ScriptMode::normal;
         this->onLoad_ = true;
         this->times_ = Script::INF;
         this->needsGraphics_ = false;
-
-        this->counter_ = 0.0f;
-
-        this->registerVariables();
     }
 
     /**
@@ -82,8 +80,8 @@
     */
     Script::~Script()
     {
-        if(this->isInitialized() && this->luaState_ != NULL)
-            delete this->luaState_;
+        //if(this->isInitialized() && this->luaState_ != NULL)
+        //    delete this->luaState_;
     }
 
     /**
@@ -107,7 +105,7 @@
 
         XMLPortEventSink(Script, BaseObject, "trigger", trigger, xmlelement, mode);
 
-        if(this->isOnLoad()) // If the object is onLoad the code is executed at once for all clients.
+        if(this->isOnLoad()) // If the object is onLoad the code is executed at once for the server.
             this->execute(0);
     }
 
@@ -128,44 +126,6 @@
 
     /**
     @brief
-        Register the variables that need to be synchronized.
-    */
-    void Script::registerVariables(void)
-    {
-        registerVariable(code_, VariableDirection::ToClient);
-        registerVariable(needsGraphics_, VariableDirection::ToClient);
-        registerVariable(modeStr_, VariableDirection::ToClient, new NetworkCallback<Script>(this, &Script::modeChanged));
-    }
-
-    void Script::modeChanged(void)
-    {
-        this->setMode(this->modeStr_);
-    }
-
-    void Script::tick(float dt)
-    {
-        SUPER(Script, tick, dt);
-
-        if(!this->clientCallbacks_.empty())
-        {
-            if(this->counter_ < 2.0f)
-            {
-                this->counter_ += dt;
-            }
-            else
-            {
-                for(std::vector<unsigned int>::iterator it = this->clientCallbacks_.begin(); it != this->clientCallbacks_.end(); it++)
-                {
-                    this->execute(*it, true);
-                }
-                this->clientCallbacks_.clear();
-                this->counter_ = 0.0f;
-            }
-        }
-    }
-
-    /**
-    @brief
         Is called when an event comes in trough the event port.
     @param triggered
         Whether the event is triggering or un-triggering.
@@ -224,56 +184,84 @@
     */
     void Script::execute(unsigned int clientId, bool fromCallback)
     {
-        if(GameMode::isServer())
+        COUT(0) << "EXECUTE: " << Host::getPlayerID() << " | " << clientId << std::endl; //TODO: Remove.
+        // If this is the server or we're in standalone mode we check whether we still want to execute the code and if so decrease the number of remaining executions.
+        if(GameMode::isServer() || GameMode::isStandalone())
         {
             // If the number of executions have been used up.
             if(this->times_ != Script::INF && this->remainingExecutions_ == 0)
                 return;
 
             // Decrement the number of remaining executions.
+            //TODO: Mode such that this is consistent in any case.
             if(this->times_ != Script::INF)
                 this->remainingExecutions_--;
         }
 
+        // If this is either the standalone mode or we're on the client we want to be.
         if(GameMode::isStandalone() || Host::getPlayerID() == clientId)
         {
-            // If the code needs graphics to be executed but the GameMode doesn't show graphics the code isn't executed.
-            if(this->needsGraphics_ && !GameMode::showsGraphics())
-                return;
-
-            if(this->mode_ == ScriptMode::normal) // If the mode is 'normal'.
-                CommandExecutor::execute(this->code_);
-            else if(this->mode_ == ScriptMode::lua) // If it's 'lua'.
-            {
-                if(this->luaState_ == NULL)
-                    this->luaState_ = new LuaState();
-                this->luaState_->doString(this->code_);
-            }
+           this->executeHelper(this->getCode(), this->getMode(), this->getNeedsGraphics());
         }
+
+        // If this is the server and we're not on the client we want to be.
         if(!GameMode::isStandalone() && GameMode::isServer() && Host::getPlayerID() != clientId)
         {
+            COUT(0) << "1" << std::endl; //TODO: Remove.
+            // If this is not the result of a clientConnected callback and we want to execute the code for all clients.
+            //TODO: In this case does the server get executed as well?
             if(!fromCallback && this->isForAll())
             {
                 const std::map<unsigned int, PlayerInfo*> clients = PlayerManager::getInstance().getClients();
                 for(std::map<unsigned int, PlayerInfo*>::const_iterator it = clients.begin(); it != clients.end(); it++)
                 {
-                    callMemberNetworkFunction(Script, execute, this->getObjectID(), it->first, it->first, false);
+                    callStaticNetworkFunction(Script::executeHelper, it->first, this->getCode(), this->getMode(), this->getNeedsGraphics());
                 }
             }
+            // Else we execute the code just for the specified client.
             else
             {
-                callMemberNetworkFunction(Script, execute, this->getObjectID(), clientId, clientId, false);
+                COUT(0) << "2" << std::endl; //TODO: Remove.
+                callStaticNetworkFunction(Script::executeHelper, clientId, this->getCode(), this->getMode(), this->getNeedsGraphics());
             }
         }
     }
 
+    /**
+    @brief
+        Helper method that is used to reach this Script object on other clients.
+    */
+    /*static*/ void Script::executeHelper(const std::string& code, const std::string& mode, bool needsGraphics)
+    {
+        COUT(0) << "HELPER: " << code << " | " << mode << " | " << needsGraphics << std::endl; //TODO: Remove.
+
+        // If the code needs graphics to be executed but the GameMode doesn't show graphics the code isn't executed.
+        if(needsGraphics && !GameMode::showsGraphics())
+            return;
+
+        if(mode == Script::NORMAL) // If the mode is 'normal'.
+            CommandExecutor::execute(code);
+        else if(mode == Script::LUA) // If it's 'lua'.
+        {
+            if(Script::LUA_STATE == NULL)
+                Script::LUA_STATE = new LuaState();
+            Script::LUA_STATE->doString(code);
+        }
+    }
+
+    /**
+    @brief
+        Callback that is called when a new client has connected.
+    @param clientId
+        The clientId of the new client that has connected.
+    */
     void Script::clientConnected(unsigned int clientId)
     {
+        // If this is the server and the Script is specified as being 'onLoad'.
         if(!GameMode::isStandalone() && GameMode::isServer() && this->isOnLoad())
         {
-            if(clientId != 0)
-                //TODO: Do better. This is only a temporary fix.
-                this->clientCallbacks_.push_back(clientId);
+            if(clientId != 0) //TODO: Remove if not needed.
+                this->execute(clientId, true);
         }
     }
 
@@ -294,9 +282,6 @@
         {
             this->setMode(ScriptMode::lua);
             this->modeStr_ = Script::LUA;
-            // Creates a new LuaState.
-            if(this->luaState_ == NULL)
-                this->luaState_ = new LuaState();
         }
         else
         {
@@ -308,6 +293,16 @@
 
     /**
     @brief
+        Sets the mode to the mode specified in this->modeStr_.
+        This is used internally for networking purposes.
+    */
+    void Script::modeChanged(void)
+    {
+        this->setMode(this->modeStr_);
+    }
+
+    /**
+    @brief
         Get the mode of the Script.
     @return
         Returns the mode as a string.

Modified: code/trunk/src/modules/objects/Script.h
===================================================================
--- code/trunk/src/modules/objects/Script.h	2010-09-22 21:30:33 UTC (rev 7481)
+++ code/trunk/src/modules/objects/Script.h	2010-09-23 09:12:22 UTC (rev 7482)
@@ -1,3 +1,4 @@
+
 /*
  *   ORXONOX - the hottest 3D action shooter ever to exist
  *                    > www.orxonox.net <
@@ -35,7 +36,6 @@
 #include <vector>
 
 #include "core/BaseObject.h"
-#include "tools/interfaces/Tickable.h"
 #include "network/synchronisable/Synchronisable.h"
 #include "network/ClientConnectionListener.h"
 
@@ -82,7 +82,7 @@
         Benjamin Knecht
         Damian 'Mozork' Frick
     */
-    class _ObjectsExport Script : public BaseObject, public Synchronisable, public ClientConnectionListener, public Tickable
+    class _ObjectsExport Script : public BaseObject, public Synchronisable, public ClientConnectionListener
     {
         public:
             Script(BaseObject* creator);
@@ -91,10 +91,9 @@
             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); //!< Method for creating a Script object through XML.
             virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode); //!< Creates a port that can be used to channel events and react to them.
 
-            virtual void tick(float dt);
-
             bool trigger(bool triggered, BaseObject* trigger); //!< Is called when an event comes in trough the event port.
             void execute(unsigned int clientId, bool fromCallback = false); //!< Executes the Scripts code for the input client, depending on the mode.
+            static void executeHelper(const std::string& code, const std::string& mode, bool needsGraphics); //!< Helper method that is used to reach this Script object on other clients.
 
             /**
             @brief Sets the code that is executed by this Script.
@@ -159,7 +158,7 @@
             bool isForAll(void)
                 { return this->forAll_; }
 
-            virtual void clientConnected(unsigned int clientId);
+            virtual void clientConnected(unsigned int clientId); //!< Callback that is called when a new client has connected.
             virtual void clientDisconnected(unsigned int clientid) {}
 
         private:
@@ -170,21 +169,16 @@
 
             std::string code_; //!< The code that is executed by this Script.
             ScriptMode::Value mode_; //!< The mode the Script is in. Determines whether the code is executed the normal way or in lua.
+            std::string modeStr_; //!< The mode the Script is in, as a string. Is used for networking purposes.
             bool onLoad_; //!< Whether the Scripts code is executed upon loading (creation) of this Script.
             int times_; //!< The number of times the Scripts code is executed at the most. -1 denotes infinity.
             bool needsGraphics_; //!< Whether the code to be executed needs graphics.
             bool forAll_; //!< Whether the code is executed for all players (in a multiplayer setup) or just for the one triggering the Script.
 
-            std::string modeStr_;
-
-            std::vector<unsigned int> clientCallbacks_;
-            float counter_;
-
-            LuaState* luaState_; //!< The LuaState to execute the code in lua.
+            static LuaState* LUA_STATE; //!< The LuaState to execute the code in lua.
             int remainingExecutions_; //!< The number of remainign executions. -1 denotes infinity.
 
-            void registerVariables(void);
-            void modeChanged();
+            void modeChanged(); //!< Sets the mode to the mode specified in this->modeStr_.
 
             /**
             @brief Sets the mode of the Script.




More information about the Orxonox-commit mailing list