Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 21, 2014, 2:32:41 PM (10 years ago)
Author:
smerkli
Message:

Improved controller, "move and look" is now scriptable.

Location:
code/branches/ScriptableController/src/orxonox/controllers
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc

    r10059 r10065  
    8989       /* Assemble a string to define a controller id variable in lua space */
    9090       std::stringstream tmp;
    91        tmp << "newctrlid = " << ctrlid;
     91       tmp << "newctrlid = " << ctrlid << endl;
    9292       std::string todo = tmp.str();
    9393       
  • code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc

    r10064 r10065  
    3636namespace orxonox
    3737{
    38     float scTime=0;  /*initialize time, to coordinate eventTime*/
    39 
    40 
    41     std::vector<event> eventList;
    42 
    43    
    44 
    4538    RegisterClass(ScriptController);
    4639
    47     //ScriptController::ScriptController(Context* context, ControllableEntity* CE) : ArtificialController(context)
    4840    ScriptController::ScriptController(Context* context) : ArtificialController(context)
    4941    {
    5042        RegisterObject(ScriptController);
    51         //set_controlled(CE);
     43
     44        /* By default, this controller has ID 0, which means it is not assigned
     45         * to anything yet.
     46         */
    5247        this->ctrlid_ = 0;
     48
     49
     50        /* Set default values for all variables */
     51        /* - pointers to zero */
     52        this->player_ = NULL;
     53        this->entity_ = NULL;
     54
     55        /* - times */
     56        this->scTime = 0.0f;
     57        this->timeToTarget = 0.0f;
     58        this->eventTime = 0.0f;
     59
     60        /* - Points in space */
     61        this->target = Vector3(0,0,0);
     62        this->startpos = Vector3(0,0,0);
     63        this->lookAtPosition = Vector3(0,0,0);
     64
     65        /* - Processing flag */
     66        this->processing = false;
     67
     68        /* - Counters */
     69        this->eventno = 0;
     70
    5371    }
    5472
    5573    void ScriptController::takeControl(int ctrlid)
    5674    {
     75        /* Output some debugging information */
    5776        orxout() << "ScriptController: Taking control" << endl;
    5877        orxout() << "This-pointer: " << this << endl;
     78
     79        /* Set the controller ID (the argument here should be nonzero) */
    5980        this->ctrlid_ = ctrlid;
     81
     82        /* Store the entity pointer in a private variable */
    6083        this->entity_ = this->player_->getControllableEntity();
    6184        assert(this->entity_);
    62 
     85         
     86        /* Add the controller here to this entity. Apparently this still leaves
     87         * any preexisting human controllers in place.
     88         */
    6389        this->entity_->setDestroyWhenPlayerLeft(false);
    6490        this->player_->pauseControl();
    6591        this->entity_->setController(this);
    6692        this->setControllableEntity(this->entity_);
     93        this->entity_->mouseLook();
     94        this->entity_->setVisible(false);
    6795    }
    6896
     
    75103    //}
    76104
    77     void ScriptController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    78     {
    79         //XMLPortParam(ScriptController, BaseObject, "lsrc", set_luasrc, xmlelement, mode);
    80 
    81     }
    82 
    83105    const Vector3& ScriptController::getPosition()
    84106    {
    85         return this->entity_->getPosition();
     107      return this->entity_->getPosition();
    86108    }
    87109
     
    104126        // TODO: do some selection here. Currently just returns the first one
    105127        if( (*it)->getID() > 0 )
     128        { orxout() << "Controller to return: " << *it << endl;
    106129          return *it;
     130        }
    107131     
    108132      }
     
    112136    void ScriptController::execute(event ev)
    113137    {
    114         if(ev.fctName=="moveToPosition_beta")
    115         {
    116 
    117             moveToPosition_beta(ev.xCoord,ev.yCoord,ev.zCoord);
    118         }
     138        orxout() << "Executing event " << ev.fctName
     139          << " with parameters:\n "
     140          << ev.x1 << " " << ev.y1 << " " << ev.z1 << "\n"
     141          << ev.x2 << " " << ev.y2 << " " << ev.z2 << "\n"
     142          << ev.duration << endl;
     143
     144        this->eventTime = 0.0f;
     145        this->startpos = this->entity_->getPosition();
     146        this->processing = true;
     147
     148        if(ev.fctName == "mal")
     149          moveAndLook(ev.x1, ev.y1, ev.z1, ev.x2, ev.y2, ev.z2, ev.duration);
    119150    }
    120151
     
    122153    void ScriptController::tick(float dt)
    123154    {
    124 
    125        
     155        /* Call the tick function of the classes we derive from */
     156        SUPER(ScriptController, tick, dt);
    126157
    127158        /* If this controller has no entity entry, do nothing */
     
    129160          return;
    130161
    131         //orxout() << "Rotating!" << endl;
    132 
    133         //this->entity_->rotateYaw(-1.0f * 100.0f * dt);
    134         //this->entity_->rotatePitch(0.8f * 100.0f);
    135 
    136         if(eventList.size() > 0 && eventList[0].eventTime<=scTime)
     162        //orxout() << "Size 0: " << this->eventList.size() << endl;
     163
     164        /* See if time has come for the next event to be run */
     165        if(this->eventList.size() > 0 && this->eventList[0].eventTime <= scTime)
    137166        {
    138             /*TO DO: execute the function: eventList[0].fctName*/
    139 
    140             execute(eventList[0]);
    141             eventList.erase(eventList.begin());
    142         }
    143 
    144         SUPER(ScriptController, tick, dt);
    145 
    146         scTime=scTime+dt;
    147     }
    148 
    149 
    150 
    151 
    152     void ScriptController::moveToPosition_beta(float x, float y, float z )
    153     {
    154 
    155         orxout()<<"moveToPosition_beta executed"<<endl;
    156         //const Vector3 local = this->getPosition();
    157         const Vector3 target = Vector3(100*x,100*y,100*z);
    158         //Vector3 way = target-local;
    159         orxout() << "Moving This-pointer: " << this << endl;
    160        
    161        
    162         //this->entity_->lookAt(target);
    163         //this->entity_->moveFrontBack(-1000*target.length());     
    164 
    165         if(this->entity_!=NULL)
    166             orxout()<<"not-NULL-entity"<<endl;
    167 
    168         if(this->player_!=NULL)
    169             orxout()<<"not-NULL-player"<<endl;
    170 
    171         orxout() << this->player_->getClientID() << endl; // IMPOSSIBLE TO ACCESS this->player AND this->entity
    172        
    173         //this->entity_ = this->player_->getClientID();//getControllableEntity();
    174 
    175             //if(this->entity_==this->player_->getControllableEntity())
    176             //orxout()<<"same entity"<<endl;
    177 
    178         /* This works fine */
    179         orxout()<<x<<"  "<<y<<"  "<<z<<endl;
    180     }
    181 
    182     void ScriptController::eventScheduler(std::string instruction, float x, float y, float z, float executionTime)
    183     {
    184 
    185 
    186         /*put data (from LUA) into time-sorted eventList*/
    187         /*nimmt den befehl und die argumente aus luascript und ertellt einen struct pro event, diese structs werden sortiert nach eventTime*/
    188         struct event tmp;
    189         tmp.fctName=instruction;
    190         tmp.xCoord=x;
    191         tmp.yCoord=y;
    192         tmp.zCoord=z;
    193         tmp.eventTime=executionTime;
    194 
    195         orxout()<<tmp.fctName<<endl;
    196 
    197         if(eventList.size()==0)
    198         {
    199             orxout()<<"eventList empty (01)"<<endl;
    200             eventList.insert(eventList.begin(), tmp);
    201             orxout()<<"first event added"<<endl;
    202         }
    203 
    204 
    205        for (std::vector<event>::iterator it=eventList.begin(); it<eventList.end(); it++)
    206             {
    207 
    208                 if(tmp.eventTime<it->eventTime)
    209                 {
    210                     eventList.insert(it,tmp);
    211                     orxout()<<"new event added"<<endl;
    212                 }
    213 
    214             }
    215 
    216        
    217        if(eventList.size()==0)
    218             orxout()<<"eventList empty"<<endl;
    219 
    220         else
    221             orxout()<<"eventList is not empty"<<endl;
    222 
    223        
    224     }
    225 
    226 
    227 
    228     /* TODO:    struct event erweitern um mehr funktionen benutzen zu koennen
    229 
    230                 mehr funktionen definieren (und dann in  execute if(...))
    231                 NB: viele noetige funktionen sind schon in artificial- bzw formationcontroller vorhanden */       
    232 
    233 
    234 
     167          /* Execute the next event on the list */
     168          this->execute(this->eventList[0]);
     169          this->eventList.erase(this->eventList.begin());
     170          this->eventno -= 1;
     171          //orxout() << "Size 1: " << this->eventList.size() << endl;
     172          //orxout() << "Eventno is now: " << this->eventno << endl;
     173        }
     174
     175        /* Update the local timers in this object */
     176        scTime += dt;
     177        eventTime += dt;
     178
     179        /* If we've arrived at the target, stop processing */
     180        if( eventTime > timeToTarget && this->processing == true)
     181        { this->processing = false;
     182
     183          //orxout() << "Size 4: " << this->eventList.size() << endl;
     184          //orxout() << "Eventno: " << this->eventno << endl;
     185         
     186          if( this->eventno == 0 )
     187          {
     188            this->entity_->mouseLook();
     189            this->entity_->setVisible(true);
     190          }
     191        }
     192
     193        /* Get a variable that specifies how far along the trajectory
     194         * we are
     195         */
     196        float dl = eventTime / timeToTarget;
     197
     198        /* Do some moving */
     199        if( this->processing )
     200        {
     201          /* Set the position to the correct place in the trajectory */
     202          this->entity_->setPosition( (1-dl)*startpos + dl * target );
     203
     204          /* Look at the specified position */
     205          this->entity_->lookAt(lookAtPosition);
     206
     207          /* Force mouse look */
     208          if( this->entity_->isInMouseLook() == false )
     209            this->entity_->mouseLook();
     210        }
     211    }
     212
     213    void ScriptController::moveAndLook(
     214      float xm, float ym, float zm,
     215      float xl, float yl, float zl,
     216      float t )
     217    {
     218      orxout()<<"moveAndLook executed"<<endl;
     219
     220      /* Set the local variables required for this event */
     221      this->target = Vector3(xm,ym,zm);
     222      this->lookAtPosition = Vector3(xl,yl,zl);
     223      this->timeToTarget = t;
     224
     225
     226      orxout() << "Moving This-pointer: " << this << endl;
     227
     228      if(this->entity_ != NULL)
     229        orxout()<<"not-NULL-entity"<<endl;
     230      else
     231        return;
     232
     233      if(this->player_ != NULL)
     234        orxout()<<"not-NULL-player"<<endl;
     235      else
     236        return;
     237    }
     238
     239    void ScriptController::eventScheduler(std::string instruction,
     240      float x1, float y1, float z1,
     241      float x2, float y2, float z2,
     242      float duration, float executionTime)
     243    {
     244      /* put data (from LUA) into time-sorted eventList*/
     245      /* Nimmt den befehl und die argumente aus luascript und ertellt einen
     246       * struct pro event, diese structs werden sortiert nach eventTime
     247       */
     248      struct event tmp;
     249
     250      /* Fill the structure with all the provided information */
     251      tmp.fctName = instruction;
     252      tmp.x1 = x1; tmp.y1 = y1; tmp.z1 = z1;
     253      tmp.x2 = x2; tmp.y2 = y2; tmp.z2 = z2;
     254      tmp.duration = duration;
     255      tmp.eventTime = executionTime;
     256
     257      orxout() << tmp.fctName << endl;
     258
     259      /* Add the created event to the event list */
     260      if(eventList.size()==0)
     261      { /* The list is still empty, just add it */
     262        orxout() << "eventList empty (01)" << endl;
     263        eventList.insert(eventList.begin(), tmp);
     264        this->eventno += 1;
     265        return; /* Nothing more to do, the event was added */
     266      }
     267
     268      /* Event was not added yet since the list was not empty. Walk through
     269       * the list of events and add it so that the events are correctly in
     270       * order.
     271       */
     272      for (std::vector<event>::iterator it=eventList.begin(); it<eventList.end(); it++)
     273      { if(tmp.eventTime < it->eventTime)
     274        { eventList.insert(it,tmp);
     275          this->eventno += 1;
     276          orxout()<<"new event added"<<endl;
     277          return;
     278        }
     279      }
     280
     281      /* If the event was still not added here, it belongs in the end of the list */
     282      eventList.insert(eventList.end(), tmp);
     283      this->eventno += 1;
     284
     285    }
    235286}
  • code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h

    r10059 r10065  
    4242    {   
    4343        std::string fctName;
    44         float xCoord;
    45         float yCoord;
    46         float zCoord;
     44        float x1;
     45        float y1;
     46        float z1;
     47
     48        float x2;
     49        float y2;
     50        float z2;
     51
     52        float duration;
    4753
    4854        float eventTime;
     
    5460    {  // tolua_export
    5561        public:
    56             //ScriptController(Context* context, ControllableEntity* CE);
    5762            ScriptController(Context* context);
    5863
    5964            virtual ~ScriptController() { }
    6065
    61             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     66            //virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    6267           
    6368            void takeControl(int ctrlid);
    6469            void setPlayer(PlayerInfo* player) { this->player_ = player; }
    6570           
    66            
    67 
    68             //void set_luasrc(std::string);
    69             //void set_controlled(ControllableEntity*);
    70 
    7171            virtual void tick(float dt);
    7272
    7373            // LUA interface
    7474            // tolua_begin
    75             void moveToPosition_beta(float x, float y, float z);
     75            void moveAndLook(float xm, float ym, float zm,
     76              float xl, float yl, float zl, float t);
    7677
    77             void eventScheduler(std::string instruction, float x, float y, float z, float time);
     78            void eventScheduler(std::string instruction,
     79              float x1, float y1, float z1,
     80              float x2, float y2, float z2,
     81              float duration, float executionTime);
    7882
    7983            static ScriptController* getScriptController();
    8084
    8185            int getID() { return ctrlid_; }
    82            
    8386
    8487            // tolua_end
     
    8891
    8992        private:
    90             // name of the LUA-sourcefile that shall be executed->see XMLPort-function
    91             std::string luasrc;         
     93            /* Information about the player that this ScriptController will
     94             * control */
     95            /* - Player pointer */
    9296            PlayerInfo* player_;
     97
     98            /* - Entity pointer, this is for convenience and will be the same as
     99             *   player_->getControllableEntity()
     100             */
    93101            ControllableEntity* entity_;
     102
     103            /* Controller ID, defaults to 0 and is set using takeControl() */
    94104            int ctrlid_;
    95105
     106            /* List of events to walk through */
     107            std::vector<event> eventList;
     108            unsigned int eventno;
     109
     110            /* Time since the creation of this ScriptController object */
     111            float scTime; 
     112
     113            /* Boolean that specifies whether we're processing an event right
     114             * now */
     115            bool processing;
     116
     117            /* Data about the point to go to and what to look at */
     118            /* - Target position */
     119            Vector3 target;
     120
     121            /* - Time it should take to get there */
     122            float timeToTarget;
     123
     124            /* - Time this event has been going on for */
     125            float eventTime;
     126            Vector3 startpos;
     127
     128            /* - Position to look at during that transition */
     129            Vector3 lookAtPosition;
    96130
    97131    };// tolua_export
Note: See TracChangeset for help on using the changeset viewer.