[Orxonox-commit 5402] r10065 - in code/branches/ScriptableController: data/gui/scripts data/levels src/orxonox/controllers

smerkli at orxonox.net smerkli at orxonox.net
Wed May 21 14:32:42 CEST 2014


Author: smerkli
Date: 2014-05-21 14:32:41 +0200 (Wed, 21 May 2014)
New Revision: 10065

Modified:
   code/branches/ScriptableController/data/gui/scripts/testscript.lua
   code/branches/ScriptableController/data/levels/levelTry.oxw
   code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc
   code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc
   code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h
Log:
Improved controller, "move and look" is now scriptable.


Modified: code/branches/ScriptableController/data/gui/scripts/testscript.lua
===================================================================
--- code/branches/ScriptableController/data/gui/scripts/testscript.lua	2014-05-21 09:45:35 UTC (rev 10064)
+++ code/branches/ScriptableController/data/gui/scripts/testscript.lua	2014-05-21 12:32:41 UTC (rev 10065)
@@ -1,20 +1,43 @@
---k = 1.2
+-- Set some test variables
+xm = 1000 
+ym = 0
+zm = 0
 
---orxonox.execute("orxout message test " .. k)
+xl = -1000 
+yl = 3000 
+zl = -1000
 
--- Set some test variables
-x = 1.1
-y = 2.2
-z = 3.3
+d = 5
+Tabs = 0
 
---os.execute("sleep " .. 2)
+pi = 3.1415
 
 -- Get a local pointer to a scriptcontroller
 local ctrl = orxonox.ScriptController:getScriptController()
 
 -- If it worked, call its "movetoposition" function
 if ctrl ~= nil then
-  ctrl:eventScheduler("moveToPosition_beta", x, y, z, 0.01)
+  -- Move to the starting point at (xl+3000,yl,zl) while looking 
+  -- at xl,yl,zl over the time span of 3 seconds
+  ctrl:eventScheduler("mal", xl+3000,yl,zl, xl,yl,zl, 3, 0)
+
+  -- From there, perform a rotation around the harvester placed there
+  -- in 100 steps
+  dt = math.pi/100
+  for t = 0,math.pi,dt do
+    xt = math.cos(t)
+    yt = math.sin(t)
+
+    ctrl:eventScheduler("mal", xl+3000*xt, yl+3000*yt, zl, xl, yl, zl, 3*dt, 3*t+2.9)
+  end
+
+  -- Update absolute time
+  Tabs = 3*math.pi + 2.9
+
+  -- Move away again, still looking at the station
+  ctrl:eventScheduler("mal", 0,0,1000, xl,yl,zl, 3, Tabs+0.4)
+
+
 end
 
 

Modified: code/branches/ScriptableController/data/levels/levelTry.oxw
===================================================================
--- code/branches/ScriptableController/data/levels/levelTry.oxw	2014-05-21 09:45:35 UTC (rev 10064)
+++ code/branches/ScriptableController/data/levels/levelTry.oxw	2014-05-21 12:32:41 UTC (rev 10065)
@@ -46,29 +46,31 @@
     <Light type=directional position="0,0,0" direction="0.253, 0.593, -0.765" diffuse="1.0,1.0,1.0,0.5" specular="1.0, 0.9, 0.9, 1.0"/>
     <SpawnPoint team=0 position="3200,0,0" lookat="2800,0,0" spawnclass=SpaceShip pawndesign=spaceshipescort />
 
+    <!--
     <StaticEntity position  = "2800,0,0" mass=10000 collisionType=static >
       <attached>
         <Model mesh="HydroHarvester.mesh" mass=10 position="0,0,0" scale=50 />
       </attached>
       <collisionShapes>
-        <BoxCollisionShape  position="-560,0,0" halfExtents="115,100,245" /><!-- Three lower boxes -->
+        <BoxCollisionShape  position="-560,0,0" halfExtents="115,100,245" />
         <BoxCollisionShape  position="290,0,-480" halfExtents="115,100,245" yaw=-120 />
         <BoxCollisionShape  position="290,0,480" halfExtents="115,100,245" yaw=-240 />
-        <BoxCollisionShape  position="-280,0,0" halfExtents="163,50,50" /><!-- Three lower connections -->
+        <BoxCollisionShape  position="-280,0,0" halfExtents="163,50,50" />
         <BoxCollisionShape  position="140,0,-240" halfExtents="163,50,50" yaw=-120 />
         <BoxCollisionShape  position="140,0,240" halfExtents="163,50,50" yaw=-240 />
-        <BoxCollisionShape  position="0,530,0" halfExtents="172,52,298" /><!-- Upper Tower -->
+        <BoxCollisionShape  position="0,530,0" halfExtents="172,52,298" />
         <BoxCollisionShape  position="0,530,0" halfExtents="172,52,298" yaw=-120 />
         <BoxCollisionShape  position="0,530,0" halfExtents="172,52,298" yaw=-240 />
-        <BoxCollisionShape  position="0,400,0" halfExtents="43,110,26" yaw=-30 /><!-- Middle one-->
-        <BoxCollisionShape  position="-200,100,0" halfExtents="26,50,43" /><!--Three lower legs -->
+        <BoxCollisionShape  position="0,400,0" halfExtents="43,110,26" yaw=-30 />
+        <BoxCollisionShape  position="-200,100,0" halfExtents="26,50,43" />
         <BoxCollisionShape  position="100,100,-173" halfExtents="43,50,26" yaw=-30 />
         <BoxCollisionShape  position="100,100,-173" halfExtents="43,50,26" yaw=30 />
-        <BoxCollisionShape  position="-100,264,0" halfExtents="26,105,43" roll=-49 /><!--Three upper legs -->
+        <BoxCollisionShape  position="-100,264,0" halfExtents="26,105,43" roll=-49 />
         <BoxCollisionShape  position="50,264,-87" halfExtents="26,105,43" roll=-49 yaw=-120 />
         <BoxCollisionShape  position="50,264,87" halfExtents="26,105,43" roll=-49 yaw=-240 />
       </collisionShapes>
     </StaticEntity>
+    -->
 
 
     <Billboard position="3500,0,0" material="Flares/ringflare2" colour="0.2,0.4,0.8" scale=10 />
@@ -76,25 +78,27 @@
       beaconMode="exclude" targetname="bcnDestroyer" name="takeControl"
     />
 
+    <StaticEntity position  = "0,0,0" mass=10000 collisionType=static >
+      <attached>
+        <Model mesh="HydroHarvester.mesh" mass=10 position="0,0,0" scale=10 />
+      </attached>
+    </StaticEntity>
 
+    <StaticEntity position  = "-1000,3000,-1000" mass=10000 collisionType=static >
+      <attached>
+        <Model mesh="HydroHarvester.mesh" mass=10 position="0,0,0" scale=80 />
+      </attached>
+    </StaticEntity>
+
 <!-- ControllerDirector waits for the event takeControl to attach a new Controller -->
     <ControllerDirector position="0,0,0">
         <events>
             <takeControl>
                 <EventListener event="takeControl" />
             </takeControl>
-
         </events>
-
     </ControllerDirector>
 
-<!-- -->
-    <ScriptController position="0,0,1">
-
-
-    </ScriptController>
-
-
 <!-- FPS Player as destination of the dock -->
 <FpsPlayer template = "fps" radarname = "First Person Player" position = "0,-19900,0" >
       <attached>

Modified: code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc
===================================================================
--- code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc	2014-05-21 09:45:35 UTC (rev 10064)
+++ code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc	2014-05-21 12:32:41 UTC (rev 10065)
@@ -88,7 +88,7 @@
        
        /* Assemble a string to define a controller id variable in lua space */
        std::stringstream tmp;
-       tmp << "newctrlid = " << ctrlid;
+       tmp << "newctrlid = " << ctrlid << endl;
        std::string todo = tmp.str();
        
        /* Run the string using the luastate created earlier */

Modified: code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc
===================================================================
--- code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc	2014-05-21 09:45:35 UTC (rev 10064)
+++ code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc	2014-05-21 12:32:41 UTC (rev 10065)
@@ -35,35 +35,63 @@
 
 namespace orxonox
 {
-    float scTime=0;  /*initialize time, to coordinate eventTime*/
-
-
-    std::vector<event> eventList;
-
-    
-
     RegisterClass(ScriptController);
 
-    //ScriptController::ScriptController(Context* context, ControllableEntity* CE) : ArtificialController(context)
     ScriptController::ScriptController(Context* context) : ArtificialController(context)
     {
         RegisterObject(ScriptController);
-        //set_controlled(CE);
+
+        /* By default, this controller has ID 0, which means it is not assigned
+         * to anything yet. 
+         */
         this->ctrlid_ = 0;
+
+
+        /* Set default values for all variables */
+        /* - pointers to zero */
+        this->player_ = NULL;
+        this->entity_ = NULL;
+
+        /* - times */
+        this->scTime = 0.0f;
+        this->timeToTarget = 0.0f;
+        this->eventTime = 0.0f;
+
+        /* - Points in space */
+        this->target = Vector3(0,0,0);
+        this->startpos = Vector3(0,0,0);
+        this->lookAtPosition = Vector3(0,0,0);
+
+        /* - Processing flag */
+        this->processing = false;
+
+        /* - Counters */
+        this->eventno = 0;
+
     }
 
     void ScriptController::takeControl(int ctrlid)
     {
+        /* Output some debugging information */
         orxout() << "ScriptController: Taking control" << endl;
         orxout() << "This-pointer: " << this << endl;
+
+        /* Set the controller ID (the argument here should be nonzero) */
         this->ctrlid_ = ctrlid;
+
+        /* Store the entity pointer in a private variable */
         this->entity_ = this->player_->getControllableEntity();
         assert(this->entity_);
-
+         
+        /* Add the controller here to this entity. Apparently this still leaves
+         * any preexisting human controllers in place. 
+         */
         this->entity_->setDestroyWhenPlayerLeft(false);
         this->player_->pauseControl();
         this->entity_->setController(this);
         this->setControllableEntity(this->entity_);
+        this->entity_->mouseLook();
+        this->entity_->setVisible(false);
     }
 
     /* Yet to be implemented and tested */
@@ -74,15 +102,9 @@
         //this->controllableEntity_ = NULL;
     //}
 
-    void ScriptController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
-    {
-    	//XMLPortParam(ScriptController, BaseObject, "lsrc", set_luasrc, xmlelement, mode);
-
-    }
-
     const Vector3& ScriptController::getPosition()
     {
-        return this->entity_->getPosition();
+      return this->entity_->getPosition();
     }
 
     ScriptController* ScriptController::getScriptController()
@@ -103,7 +125,9 @@
       { 
         // TODO: do some selection here. Currently just returns the first one
         if( (*it)->getID() > 0 )
+        { orxout() << "Controller to return: " << *it << endl;
           return *it; 
+        }
       
       }
       return NULL;
@@ -111,125 +135,152 @@
 
     void ScriptController::execute(event ev)
     {
-        if(ev.fctName=="moveToPosition_beta")
-        {
+        orxout() << "Executing event " << ev.fctName 
+          << " with parameters:\n " 
+          << ev.x1 << " " << ev.y1 << " " << ev.z1 << "\n"
+          << ev.x2 << " " << ev.y2 << " " << ev.z2 << "\n"
+          << ev.duration << endl;
 
-            moveToPosition_beta(ev.xCoord,ev.yCoord,ev.zCoord);
-        }
+        this->eventTime = 0.0f;
+        this->startpos = this->entity_->getPosition();
+        this->processing = true;
+
+        if(ev.fctName == "mal")
+          moveAndLook(ev.x1, ev.y1, ev.z1, ev.x2, ev.y2, ev.z2, ev.duration);
     }
 
 
     void ScriptController::tick(float dt)
     {
+        /* Call the tick function of the classes we derive from */
+        SUPER(ScriptController, tick, dt);
 
-        
-
         /* If this controller has no entity entry, do nothing */
         if( !(this->entity_) )
           return;
 
-        //orxout() << "Rotating!" << endl;
+        //orxout() << "Size 0: " << this->eventList.size() << endl;
 
-        //this->entity_->rotateYaw(-1.0f * 100.0f * dt);
-        //this->entity_->rotatePitch(0.8f * 100.0f);
-
-        if(eventList.size() > 0 && eventList[0].eventTime<=scTime)
+        /* See if time has come for the next event to be run */
+        if(this->eventList.size() > 0 && this->eventList[0].eventTime <= scTime)
         {
-            /*TO DO: execute the function: eventList[0].fctName*/
+          /* Execute the next event on the list */
+          this->execute(this->eventList[0]);
+          this->eventList.erase(this->eventList.begin());
+          this->eventno -= 1;
+          //orxout() << "Size 1: " << this->eventList.size() << endl;
+          //orxout() << "Eventno is now: " << this->eventno << endl;
+        }
 
-            execute(eventList[0]);
-            eventList.erase(eventList.begin());
+        /* Update the local timers in this object */
+        scTime += dt;
+        eventTime += dt;
+
+        /* If we've arrived at the target, stop processing */
+        if( eventTime > timeToTarget && this->processing == true)
+        { this->processing = false;
+
+          //orxout() << "Size 4: " << this->eventList.size() << endl;
+          //orxout() << "Eventno: " << this->eventno << endl;
+          
+          if( this->eventno == 0 )
+          {
+            this->entity_->mouseLook();
+            this->entity_->setVisible(true);
+          }
         }
 
-        SUPER(ScriptController, tick, dt);
+        /* Get a variable that specifies how far along the trajectory
+         * we are 
+         */
+        float dl = eventTime / timeToTarget; 
 
-        scTime=scTime+dt;
-    }
+        /* Do some moving */
+        if( this->processing )
+        { 
+          /* Set the position to the correct place in the trajectory */
+          this->entity_->setPosition( (1-dl)*startpos + dl * target );
 
+          /* Look at the specified position */
+          this->entity_->lookAt(lookAtPosition);
 
+          /* Force mouse look */
+          if( this->entity_->isInMouseLook() == false )
+            this->entity_->mouseLook();
+        }
+    }
 
-
-    void ScriptController::moveToPosition_beta(float x, float y, float z )
+    void ScriptController::moveAndLook(
+      float xm, float ym, float zm,
+      float xl, float yl, float zl,
+      float t )
     {
+      orxout()<<"moveAndLook executed"<<endl;
 
-        orxout()<<"moveToPosition_beta executed"<<endl;
-        //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->entity_->lookAt(target);
-        //this->entity_->moveFrontBack(-1000*target.length());      
+      /* Set the local variables required for this event */
+      this->target = Vector3(xm,ym,zm);
+      this->lookAtPosition = Vector3(xl,yl,zl);
+      this->timeToTarget = t;
 
-        if(this->entity_!=NULL)
-            orxout()<<"not-NULL-entity"<<endl;
 
-        if(this->player_!=NULL)
-            orxout()<<"not-NULL-player"<<endl;
+      orxout() << "Moving This-pointer: " << this << endl;
 
-        orxout() << this->player_->getClientID() << endl; // IMPOSSIBLE TO ACCESS this->player AND this->entity
-        
-        //this->entity_ = this->player_->getClientID();//getControllableEntity();
+      if(this->entity_ != NULL)
+        orxout()<<"not-NULL-entity"<<endl;
+      else 
+        return;
 
-            //if(this->entity_==this->player_->getControllableEntity())
-            //orxout()<<"same entity"<<endl;
-
-        /* This works fine */
-        orxout()<<x<<"  "<<y<<"  "<<z<<endl;
+      if(this->player_ != NULL)
+        orxout()<<"not-NULL-player"<<endl;
+      else 
+        return;
     }
 
-    void ScriptController::eventScheduler(std::string instruction, float x, float y, float z, float executionTime)
+    void ScriptController::eventScheduler(std::string instruction, 
+      float x1, float y1, float z1, 
+      float x2, float y2, float z2, 
+      float duration, float executionTime)
     {
+      /* put data (from LUA) into time-sorted eventList*/ 
+      /* Nimmt den befehl und die argumente aus luascript und ertellt einen
+       * struct pro event, diese structs werden sortiert nach eventTime
+       */
+      struct event tmp;
 
+      /* Fill the structure with all the provided information */
+      tmp.fctName = instruction;
+      tmp.x1 = x1; tmp.y1 = y1; tmp.z1 = z1;
+      tmp.x2 = x2; tmp.y2 = y2; tmp.z2 = z2;
+      tmp.duration = duration;
+      tmp.eventTime = executionTime;
 
-        /*put data (from LUA) into time-sorted eventList*/ 
-        /*nimmt den befehl und die argumente aus luascript und ertellt einen struct pro event, diese structs werden sortiert nach eventTime*/
-        struct event tmp;
-        tmp.fctName=instruction;
-        tmp.xCoord=x;
-        tmp.yCoord=y;
-        tmp.zCoord=z;
-        tmp.eventTime=executionTime;
+      orxout() << tmp.fctName << endl;
 
-        orxout()<<tmp.fctName<<endl;
+      /* Add the created event to the event list */
+      if(eventList.size()==0)
+      { /* The list is still empty, just add it */
+        orxout() << "eventList empty (01)" << endl;
+        eventList.insert(eventList.begin(), tmp);
+        this->eventno += 1;
+        return; /* Nothing more to do, the event was added */
+      }
 
-        if(eventList.size()==0)
-        {
-            orxout()<<"eventList empty (01)"<<endl;
-            eventList.insert(eventList.begin(), tmp);
-            orxout()<<"first event added"<<endl;
+      /* Event was not added yet since the list was not empty. Walk through
+       * the list of events and add it so that the events are correctly in
+       * order.
+       */
+      for (std::vector<event>::iterator it=eventList.begin(); it<eventList.end(); it++)
+      { if(tmp.eventTime < it->eventTime)
+        { eventList.insert(it,tmp);
+          this->eventno += 1;
+          orxout()<<"new event added"<<endl;
+          return;
         }
+      }
 
+      /* If the event was still not added here, it belongs in the end of the list */
+      eventList.insert(eventList.end(), tmp);
+      this->eventno += 1;
 
-       for (std::vector<event>::iterator it=eventList.begin(); it<eventList.end(); it++)
-            {
-
-                if(tmp.eventTime<it->eventTime)
-                {
-                    eventList.insert(it,tmp);
-                    orxout()<<"new event added"<<endl;
-                }
-
-            }
-
-       
-       if(eventList.size()==0)
-            orxout()<<"eventList empty"<<endl;
-
-        else
-            orxout()<<"eventList is not empty"<<endl;
-
-        
     }
-
-
-
-    /* TODO:    struct event erweitern um mehr funktionen benutzen zu koennen
-
-                mehr funktionen definieren (und dann in  execute if(...))
-                NB: viele noetige funktionen sind schon in artificial- bzw formationcontroller vorhanden */        
-
-
-
 }

Modified: code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h
===================================================================
--- code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h	2014-05-21 09:45:35 UTC (rev 10064)
+++ code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h	2014-05-21 12:32:41 UTC (rev 10065)
@@ -41,10 +41,16 @@
     struct event 
     {   
         std::string fctName;
-        float xCoord;
-        float yCoord;
-        float zCoord;
+        float x1;
+        float y1;
+        float z1;
 
+        float x2;
+        float y2;
+        float z2;
+
+        float duration;
+
         float eventTime;
 
     }; 
@@ -53,33 +59,30 @@
        : public ArtificialController, public Tickable
     {  // tolua_export
         public:
-            //ScriptController(Context* context, ControllableEntity* CE);
             ScriptController(Context* context);
 
             virtual ~ScriptController() { }
 
-            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
+            //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*);
-
             virtual void tick(float dt);
 
             // LUA interface
             // tolua_begin 
-            void moveToPosition_beta(float x, float y, float z);
+            void moveAndLook(float xm, float ym, float zm,
+              float xl, float yl, float zl, float t);
 
-            void eventScheduler(std::string instruction, float x, float y, float z, float time);
+            void eventScheduler(std::string instruction, 
+              float x1, float y1, float z1, 
+              float x2, float y2, float z2, 
+              float duration, float executionTime);
 
             static ScriptController* getScriptController();
 
             int getID() { return ctrlid_; }
-            
 
             // tolua_end
             const Vector3& getPosition();
@@ -87,13 +90,44 @@
             void execute(event ev);
 
         private:
-            // name of the LUA-sourcefile that shall be executed->see XMLPort-function
-            std::string luasrc;		
+            /* Information about the player that this ScriptController will
+             * control */
+            /* - Player pointer */
             PlayerInfo* player_;
+
+            /* - Entity pointer, this is for convenience and will be the same as 
+             *   player_->getControllableEntity() 
+             */
             ControllableEntity* entity_;
+
+            /* Controller ID, defaults to 0 and is set using takeControl() */
             int ctrlid_;
 
+            /* List of events to walk through */
+            std::vector<event> eventList;
+            unsigned int eventno;
 
+            /* Time since the creation of this ScriptController object */
+            float scTime;  
+
+            /* Boolean that specifies whether we're processing an event right
+             * now */
+            bool processing;
+
+            /* Data about the point to go to and what to look at */
+            /* - Target position */
+            Vector3 target;
+
+            /* - Time it should take to get there */
+            float timeToTarget;
+
+            /* - Time this event has been going on for */
+            float eventTime;
+            Vector3 startpos;
+
+            /* - Position to look at during that transition */
+            Vector3 lookAtPosition;
+
     };// tolua_export
 } // tolua_export
 




More information about the Orxonox-commit mailing list