Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10047


Ignore:
Timestamp:
May 8, 2014, 10:16:23 AM (11 years ago)
Author:
smerkli
Message:

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

Location:
code/branches/ScriptableController
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/ScriptableController/data/gui/scripts/testscript.lua

    r10046 r10047  
    33--orxonox.execute("orxout message test " .. k)
    44
     5-- Set some test variables
    56x = 1.1
    67y = 2.2
    78z = 3.3
    89
     10--os.execute("sleep " .. 2)
    911
     12-- Get a local pointer to a scriptcontroller
    1013local ctrl = orxonox.ScriptController:getScriptController()
     14
     15-- If it worked, call its "movetoposition" function
    1116if ctrl ~= nil then
    1217  ctrl:moveToPosition_beta(x, y, z)
    1318end
    1419
    15 --ctrl.moveToPosition(x,y,z)
    16 --
     20-- Output the newctrlid variable we set from the C++ code
    1721if newctrlid ~= nil then
    1822  orxonox.execute("orxout message test " .. newctrlid)
    19 end
    20 
    21 local docks = orxonox.Dock:getNumberOfActiveDocks()
    22 local docklist = {}
    23 for i = 0, docks-1 do
    24   table.insert(docklist, orxonox.Dock:getActiveDockAtIndex(i))
    25 end
    26 local dock = docklist[1]
    27 if dock ~= nil then
    28     dock:dock()
    2923end
    3024
  • code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc

    r10046 r10047  
    66
    77#include "ControllerDirector.h"
     8#include "ScriptController.h"
    89#include "core/CoreIncludes.h"
    910
     
    3233        this->entity_ = NULL;
    3334        this->pTrigger_ = NULL;
     35        this->context_ = context;
    3436    }
    3537
     
    6466        * triggered our event.
    6567        */
    66        static int ctrlid = 0;
    67        // preparationTo(trigger);
    68        // setNewController(controller);
     68       static int ctrlid = 1;
     69       bool prepok = preparationToTakeControl(trigger);
     70       if( prepok == true)
     71       {
     72         /* Create a scriptcontroller object */
     73         ScriptController *newctrl = new ScriptController(this->context_);
     74
     75         /* Make the player we were given its slave */
     76         newctrl->setPlayer(this->player_);
     77
     78         /* Start controlling that object */
     79         newctrl->takeControl(ctrlid);
     80       }
     81       else
     82         return;
    6983       
    7084       /* Set up a luastate to use for running the scripts */
     
    90104
    91105       
    92     /*bool ControllerDirector::preparationToTakeControl(BaseObject * trigger) {
    93 
    94             this->pTrigger_ = orxonox_cast<PlayerTrigger*>(trigger);
     106    bool ControllerDirector::preparationToTakeControl(BaseObject * trigger)
     107    {
     108        this->pTrigger_ = orxonox_cast<PlayerTrigger*>(trigger);
    95109        this->player_ = NULL;
    96110
    97111        orxout() << "Preparation to take Control!" << endl;
     112
    98113        // Check whether it is a player trigger and extract pawn from it
    99114        if(this->pTrigger_ != NULL)
    100115        {
    101            
    102             player_ = this->pTrigger_->getTriggeringPlayer();  //Get the object which triggered the event.
     116            // Get the object which triggered the event.
     117            this->player_ = this->pTrigger_->getTriggeringPlayer(); 
     118
     119            // Check if there actually was a player returned.
     120            if( this->player_ == NULL) return false;
    103121        }
    104122        else
    105123        {
    106             orxout() << "ControllerDirector::preparationToTakeControl Not a player trigger, can't extract pawn from it.." << endl;
     124            orxout() << "ControllerDirector::preparationToTakeControl "
     125              << "Not a player trigger, can't extract pawn from it.." << endl;
    107126            return false;
    108127        }
    109128
    110        
    111129        this->entity_ = this->player_->getControllableEntity();
    112130        assert(this->entity_);
    113131
    114     return true;
    115 
     132        return true;
    116133    }
    117134
     135    /* // Currently unused
    118136    void ControllerDirector::setNewController(Controller * controller) {
    119137
     
    124142        this->player_->pauseControl();
    125143        this->entity_->setController(controller);
    126         this->setControllableEntity(this->entity_);
    127 
    128 
    129 
     144        this->player_->startControl(this->entity_);
     145        //this->setControllableEntity(this->entity_);
    130146    }
    131 */
     147    */
    132148       
    133149   
  • code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.h

    r10027 r10047  
    4848       
    4949       
    50        
     50            /* Take over control of a given object */
    5151            void takeControl(Controller * controller, BaseObject * trigger);
    5252            bool preparationToTakeControl(BaseObject * trigger);
    53             void setNewController(Controller * controller);
     53
     54            // currently unused
     55            //void setNewController(Controller * controller);
    5456       
    5557
     
    5759        private:
    5860           
    59            PlayerInfo* player_;
    60 
    61        ControllableEntity* entity_;
    62 
    63            PlayerTrigger * pTrigger_;
     61            PlayerInfo* player_;
     62            ControllableEntity* entity_;
     63            PlayerTrigger * pTrigger_;
     64            Context* context_;
    6465       
    6566
  • code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc

    r10046 r10047  
    2828
    2929#include "ScriptController.h"
     30#include "infos/PlayerInfo.h"
    3031#include "core/CoreIncludes.h"
    3132#include "worldentities/ControllableEntity.h"
     
    4243        RegisterObject(ScriptController);
    4344        //set_controlled(CE);
     45        this->ctrlid_ = 0;
    4446    }
    4547
    46     void ScriptController::set_luasrc(std::string lsrc)
     48    void ScriptController::takeControl(int ctrlid)
    4749    {
    48         this->luasrc=lsrc;
     50        orxout() << "ScriptController: Taking control" << endl;
     51        orxout() << "This-pointer: " << this << endl;
     52        this->ctrlid_ = ctrlid;
     53        this->entity_ = this->player_->getControllableEntity();
     54        assert(this->entity_);
     55
     56        this->entity_->setDestroyWhenPlayerLeft(false);
     57        this->player_->pauseControl();
     58        this->entity_->setController(this);
     59        this->setControllableEntity(this->entity_);
    4960    }
    5061
    51     void ScriptController::set_controlled(ControllableEntity* toControl)
    52     {
    53         this->controlled=toControl;
    54     }
    55    
     62    /* Yet to be implemented and tested */
     63    //void ScriptController::yieldControl()
     64    //{
     65        //this->player_->startControl(this->entity_);
     66        //this->setActive(false);
     67        //this->controllableEntity_ = NULL;
     68    //}
     69
    5670    void ScriptController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    5771    {
     
    6276    const Vector3& ScriptController::getPosition()
    6377    {
    64         return this->controlled->getPosition();
     78        return this->entity_->getPosition();
    6579    }
    6680
     
    7084      orxout() << "Great success!" << std::endl;
    7185
    72       /* Loop over all the scriptcontrollers currently present in the game */
     86      /* Debugging: print all the scriptcontroller object pointers */
     87      for(ObjectList<ScriptController>::iterator it =
     88        ObjectList<ScriptController>::begin();
     89        it != ObjectList<ScriptController>::end(); ++it)
     90      { orxout() << "Have object in list: " << *it << endl; }
     91
     92      /* Find the first one with a nonzero ID */
    7393      for(ObjectList<ScriptController>::iterator it =
    7494        ObjectList<ScriptController>::begin();
     
    7696      {
    7797        // TODO: do some selection here. Currently just returns the first one
    78         return *it;
     98        if( (*it)->getID() > 0 )
     99          return *it;
    79100     
    80101      }
     
    82103    }
    83104
     105    void ScriptController::tick(float dt)
     106    {
     107        /* If this controller has no entity entry, do nothing */
     108        if( !(this->entity_) )
     109          return;
     110
     111        //orxout() << "Rotating!" << endl;
     112
     113        //this->entity_->rotateYaw(-1.0f * 100.0f * dt);
     114        //this->entity_->rotatePitch(0.8f * 100.0f);
     115
     116        SUPER(ScriptController, tick, dt);
     117    }
    84118
    85119
    86120    void ScriptController::moveToPosition_beta(float x, float y, float z )
    87121    {
    88         /* The section commented out here below throws segfaults */
    89         //const Vector3 local=getPosition();
    90         //const Vector3 target=Vector3(x,y,z);
    91         //Vector3 way=target-local;
     122        //const Vector3 local = this->getPosition();
     123        const Vector3 target = Vector3(100*x,100*y,100*z);
     124        //Vector3 way = target-local;
     125        orxout() << "Moving This-pointer: " << this << endl;
    92126       
    93127       
    94         //this->controlled->lookAt(target);
    95         //this->controlled->moveFrontBack(way.length());     
     128        this->entity_->lookAt(target);
     129        this->entity_->moveFrontBack(-1000*target.length());     
    96130
    97131 
     
    99133        orxout()<<x<<"  "<<y<<"  "<<z<<endl;
    100134    }
     135
    101136
    102137    /* TODO:    hilfs(zwischen)funktionen um lua eingabe zu ermoeglichen: zb moveToPosition(float...) weil in LUA wohl
  • code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h

    r10046 r10047  
    3838{  // tolua_export
    3939    class _OrxonoxExport ScriptController // tolua_export
    40        : public ArtificialController
     40       : public ArtificialController, public Tickable
    4141    {  // tolua_export
    4242        public:
     
    4848            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    4949           
     50            void takeControl(int ctrlid);
     51            void setPlayer(PlayerInfo* player) { this->player_ = player; }
    5052           
    51             void set_luasrc(std::string);
    52             void set_controlled(ControllableEntity*);
     53            //void set_luasrc(std::string);
     54            //void set_controlled(ControllableEntity*);
    5355
     56            virtual void tick(float dt);
    5457
    5558            // LUA interface
     
    5962           
    6063            static ScriptController* getScriptController();
     64
     65            int getID() { return ctrlid_; }
    6166           
    62               /* virtual void tick(float dt);*/
    6367
    6468            // tolua_end
     
    6670
    6771        private:
    68                 std::string luasrc;             // name of the LUA-sourcefile that shall be executed->see XMLPort-function
    69 
    70             ControllableEntity* controlled; //entity controlled by this SC
     72            // name of the LUA-sourcefile that shall be executed->see XMLPort-function
     73            std::string luasrc;         
     74            PlayerInfo* player_;
     75            ControllableEntity* entity_;
     76            int ctrlid_;
    7177
    7278
Note: See TracChangeset for help on using the changeset viewer.