[Orxonox-commit 5384] r10047 - in code/branches/ScriptableController: data/gui/scripts src/orxonox/controllers

smerkli at orxonox.net smerkli at orxonox.net
Thu May 8 10:16:24 CEST 2014


Author: smerkli
Date: 2014-05-08 10:16:23 +0200 (Thu, 08 May 2014)
New Revision: 10047

Modified:
   code/branches/ScriptableController/data/gui/scripts/testscript.lua
   code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc
   code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.h
   code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc
   code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h
Log:
Controller switching works now, however lua script
execution is blocking, which means we can only schedule
stuff in them, not leave them running in realtime


Modified: code/branches/ScriptableController/data/gui/scripts/testscript.lua
===================================================================
--- code/branches/ScriptableController/data/gui/scripts/testscript.lua	2014-05-07 12:03:18 UTC (rev 10046)
+++ code/branches/ScriptableController/data/gui/scripts/testscript.lua	2014-05-08 08:16:23 UTC (rev 10047)
@@ -2,32 +2,26 @@
 
 --orxonox.execute("orxout message test " .. k)
 
+-- Set some test variables
 x = 1.1
 y = 2.2
 z = 3.3
 
+--os.execute("sleep " .. 2)
 
+-- Get a local pointer to a scriptcontroller
 local ctrl = orxonox.ScriptController:getScriptController()
+
+-- If it worked, call its "movetoposition" function
 if ctrl ~= nil then
   ctrl:moveToPosition_beta(x, y, z)
 end
 
---ctrl.moveToPosition(x,y,z)
---
+-- Output the newctrlid variable we set from the C++ code
 if newctrlid ~= nil then
   orxonox.execute("orxout message test " .. newctrlid)
 end
 
-local docks = orxonox.Dock:getNumberOfActiveDocks()
-local docklist = {}
-for i = 0, docks-1 do
-  table.insert(docklist, orxonox.Dock:getActiveDockAtIndex(i))
-end
-local dock = docklist[1]
-if dock ~= nil then
-    dock:dock()
-end
-
 --orxonox.execute("setPause 1")
 
 

Modified: code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc
===================================================================
--- code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc	2014-05-07 12:03:18 UTC (rev 10046)
+++ code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc	2014-05-08 08:16:23 UTC (rev 10047)
@@ -5,6 +5,7 @@
  */
 
 #include "ControllerDirector.h"
+#include "ScriptController.h"
 #include "core/CoreIncludes.h"
 
 //#include "network/NetworkFunction.h"
@@ -31,6 +32,7 @@
         this->player_ = NULL;
         this->entity_ = NULL;
         this->pTrigger_ = NULL;
+        this->context_ = context;
     }
 
     void ControllerDirector::XMLPort(Element& xmlelement, XMLPort::Mode mode)
@@ -63,9 +65,21 @@
        /* First, we set up a new controller to attach to the unit that
         * triggered our event. 
         */
-       static int ctrlid = 0;
-       // preparationTo(trigger);
-       // setNewController(controller);
+       static int ctrlid = 1;
+       bool prepok = preparationToTakeControl(trigger);
+       if( prepok == true) 
+       {
+         /* Create a scriptcontroller object */
+         ScriptController *newctrl = new ScriptController(this->context_);
+
+         /* Make the player we were given its slave */
+         newctrl->setPlayer(this->player_);
+
+         /* Start controlling that object */
+         newctrl->takeControl(ctrlid);
+       }
+       else
+         return;
        
        /* Set up a luastate to use for running the scripts */
        LuaState * ls = new LuaState();
@@ -89,32 +103,36 @@
     } 
 
 	
-    /*bool ControllerDirector::preparationToTakeControl(BaseObject * trigger) {
-
-	    this->pTrigger_ = orxonox_cast<PlayerTrigger*>(trigger);
+    bool ControllerDirector::preparationToTakeControl(BaseObject * trigger) 
+    {
+        this->pTrigger_ = orxonox_cast<PlayerTrigger*>(trigger);
         this->player_ = NULL;
 
         orxout() << "Preparation to take Control!" << endl; 
+
         // Check whether it is a player trigger and extract pawn from it
         if(this->pTrigger_ != NULL)
         {
-            
-            player_ = this->pTrigger_->getTriggeringPlayer();  //Get the object which triggered the event.
+            // Get the object which triggered the event.
+            this->player_ = this->pTrigger_->getTriggeringPlayer();  
+
+            // Check if there actually was a player returned.
+            if( this->player_ == NULL) return false;
         }
         else
         {
-            orxout() << "ControllerDirector::preparationToTakeControl Not a player trigger, can't extract pawn from it.." << endl;
+            orxout() << "ControllerDirector::preparationToTakeControl " 
+              << "Not a player trigger, can't extract pawn from it.." << endl;
             return false;
         }
 
-	
 	this->entity_ = this->player_->getControllableEntity();
 	assert(this->entity_);
 
-    return true;
-
+        return true;
     }
 
+    /* // Currently unused
     void ControllerDirector::setNewController(Controller * controller) {
 
 
@@ -123,12 +141,10 @@
         this->entity_->setDestroyWhenPlayerLeft(false);
         this->player_->pauseControl();
         this->entity_->setController(controller);
-        this->setControllableEntity(this->entity_);
-
-
-
+        this->player_->startControl(this->entity_);
+        //this->setControllableEntity(this->entity_);
     }
-*/
+    */
        
     
 

Modified: code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.h
===================================================================
--- code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.h	2014-05-07 12:03:18 UTC (rev 10046)
+++ code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.h	2014-05-08 08:16:23 UTC (rev 10047)
@@ -47,20 +47,21 @@
 	    virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
 	
 	
-	
+            /* Take over control of a given object */
 	    void takeControl(Controller * controller, BaseObject * trigger);
 	    bool preparationToTakeControl(BaseObject * trigger);
-	    void setNewController(Controller * controller);
+
+            // currently unused
+	    //void setNewController(Controller * controller);
 	
 
 
         private:
 	   
-	   PlayerInfo* player_;
-
-       ControllableEntity* entity_; 
-
-	   PlayerTrigger * pTrigger_;
+            PlayerInfo* player_;
+            ControllableEntity* entity_; 
+            PlayerTrigger * pTrigger_;
+            Context* context_;
 	
 
     };

Modified: code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc
===================================================================
--- code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc	2014-05-07 12:03:18 UTC (rev 10046)
+++ code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc	2014-05-08 08:16:23 UTC (rev 10047)
@@ -27,6 +27,7 @@
  */
 
 #include "ScriptController.h"
+#include "infos/PlayerInfo.h"
 #include "core/CoreIncludes.h"
 #include "worldentities/ControllableEntity.h"
 #include "core/LuaState.h"
@@ -41,18 +42,31 @@
     {
         RegisterObject(ScriptController);
         //set_controlled(CE);
+        this->ctrlid_ = 0;
     }
 
-    void ScriptController::set_luasrc(std::string lsrc)
+    void ScriptController::takeControl(int ctrlid)
     {
-        this->luasrc=lsrc;
+        orxout() << "ScriptController: Taking control" << endl;
+        orxout() << "This-pointer: " << this << endl;
+        this->ctrlid_ = ctrlid;
+        this->entity_ = this->player_->getControllableEntity();
+        assert(this->entity_);
+
+        this->entity_->setDestroyWhenPlayerLeft(false);
+        this->player_->pauseControl();
+        this->entity_->setController(this);
+        this->setControllableEntity(this->entity_);
     }
 
-    void ScriptController::set_controlled(ControllableEntity* toControl)
-    {
-        this->controlled=toControl;
-    }
-    
+    /* Yet to be implemented and tested */
+    //void ScriptController::yieldControl()
+    //{
+        //this->player_->startControl(this->entity_);
+        //this->setActive(false);
+        //this->controllableEntity_ = NULL;
+    //}
+
     void ScriptController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     {
     	//XMLPortParam(ScriptController, BaseObject, "lsrc", set_luasrc, xmlelement, mode);
@@ -61,7 +75,7 @@
 
     const Vector3& ScriptController::getPosition()
     {
-        return this->controlled->getPosition();
+        return this->entity_->getPosition();
     }
 
     ScriptController* ScriptController::getScriptController()
@@ -69,36 +83,57 @@
       /* Output a message that confirms this function was called */
       orxout() << "Great success!" << std::endl;
 
-      /* Loop over all the scriptcontrollers currently present in the game */
+      /* Debugging: print all the scriptcontroller object pointers */
       for(ObjectList<ScriptController>::iterator it = 
         ObjectList<ScriptController>::begin(); 
         it != ObjectList<ScriptController>::end(); ++it)
+      { orxout() << "Have object in list: " << *it << endl; }
+
+      /* Find the first one with a nonzero ID */
+      for(ObjectList<ScriptController>::iterator it = 
+        ObjectList<ScriptController>::begin(); 
+        it != ObjectList<ScriptController>::end(); ++it)
       { 
         // TODO: do some selection here. Currently just returns the first one
-        return *it; 
+        if( (*it)->getID() > 0 )
+          return *it; 
       
       }
       return NULL;
     }
 
+    void ScriptController::tick(float dt)
+    {
+        /* If this controller has no entity entry, do nothing */
+        if( !(this->entity_) )
+          return;
 
+        //orxout() << "Rotating!" << endl;
 
+        //this->entity_->rotateYaw(-1.0f * 100.0f * dt);
+        //this->entity_->rotatePitch(0.8f * 100.0f);
+
+        SUPER(ScriptController, tick, dt);
+    }
+
+
     void ScriptController::moveToPosition_beta(float x, float y, float z )
     {
-        /* The section commented out here below throws segfaults */
-        //const Vector3 local=getPosition();
-        //const Vector3 target=Vector3(x,y,z);
-        //Vector3 way=target-local;
+        //const Vector3 local = this->getPosition();
+        const Vector3 target = Vector3(100*x,100*y,100*z);
+        //Vector3 way = target-local;
+        orxout() << "Moving This-pointer: " << this << endl;
        
         
-        //this->controlled->lookAt(target);
-        //this->controlled->moveFrontBack(way.length());      
+        this->entity_->lookAt(target);
+        this->entity_->moveFrontBack(-1000*target.length());      
 
   
         /* This works fine */
         orxout()<<x<<"  "<<y<<"  "<<z<<endl;
     }
 
+
     /* TODO:    hilfs(zwischen)funktionen um lua eingabe zu ermoeglichen: zb moveToPosition(float...) weil in LUA wohl 
                 kein vektor3 definierbar ist
 

Modified: code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h
===================================================================
--- code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h	2014-05-07 12:03:18 UTC (rev 10046)
+++ code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h	2014-05-08 08:16:23 UTC (rev 10047)
@@ -37,7 +37,7 @@
 namespace orxonox  // tolua_export
 {  // tolua_export
     class _OrxonoxExport ScriptController // tolua_export 
-       : public ArtificialController
+       : public ArtificialController, public Tickable
     {  // tolua_export
         public:
             //ScriptController(Context* context, ControllableEntity* CE);
@@ -47,10 +47,13 @@
 
             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
             
+            void takeControl(int ctrlid);
+            void setPlayer(PlayerInfo* player) { this->player_ = player; }
            
-            void set_luasrc(std::string);
-            void set_controlled(ControllableEntity*);
+            //void set_luasrc(std::string);
+            //void set_controlled(ControllableEntity*);
 
+            virtual void tick(float dt);
 
             // LUA interface
             // tolua_begin 
@@ -58,18 +61,21 @@
 
            
             static ScriptController* getScriptController();
+
+            int getID() { return ctrlid_; }
             
-              /* virtual void tick(float dt);*/ 
 
             // tolua_end
             const Vector3& getPosition();
 
         private:
-        	std::string luasrc;		// name of the LUA-sourcefile that shall be executed->see XMLPort-function
+            // name of the LUA-sourcefile that shall be executed->see XMLPort-function
+            std::string luasrc;		
+            PlayerInfo* player_;
+            ControllableEntity* entity_;
+            int ctrlid_;
 
-            ControllableEntity* controlled; //entity controlled by this SC
 
-
     };// tolua_export
 } // tolua_export
 




More information about the Orxonox-commit mailing list