Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentationFS15merge/src/orxonox/controllers/ScriptController.cc @ 10614

Last change on this file since 10614 was 10614, checked in by landauf, 9 years ago

merged branch SciptableControllerFS15

  • Property svn:eol-style set to native
File size: 12.3 KB
RevLine 
[10014]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
[10614]23 *      ...
[10014]24 *   Co-authors:
25 *      ...
26 *
27 */
28
[10614]29 /*
30  * Currently available lua commands:
31  *
32  * IMPORTANT: ALL COMMANDS DO REQUIRE 7 PARAMETERS TO BE PROVIDED. FILL UP WITH ZEROES IN UNIMPORTANT PLACES.
33  *
34  * Command             | Abbreviation | Parameter 1          | '' 2     | '' 3    | '' 4                 | '' 5     | '' 6     | '' 7
35  *
36  * "Move And Look"     | mal          | GoTo X Coordinate    | '' Y ''  | '' Z '' | LookAt X Coordinate  |  '' Y '' |  '' Y '' | Duration
37  * "Rotate And Look"   | ral          | GoTo X Coordinate    | '' Y ''  | '' Z '' | Axis (1=x, 2=z, 3=z) |     -    |     -    | Duration
38  * "Spiral"            | spi          | GoTo X Coordinate    | '' Y ''  | '' Z '' |          -           |     -    |     -    | Duration
39  * "Transition Look"   | chl          | From X Coordinate    | '' Y ''  | '' Z '' | To X Coordinate      |  '' Y '' |  '' Y '' | Duration
40  * "Idle (Do nothing)" | idle         | Duration
41  */
42
[10014]43#include "ScriptController.h"
[10047]44#include "infos/PlayerInfo.h"
[10014]45#include "core/CoreIncludes.h"
[10028]46#include "worldentities/ControllableEntity.h"
[10045]47#include "core/LuaState.h"
[10614]48#include "core/LuaState.h"
[10034]49#include <cmath>
[10014]50
[10614]51
[10014]52namespace orxonox
53{
54    RegisterClass(ScriptController);
55
[10028]56    ScriptController::ScriptController(Context* context) : ArtificialController(context)
[10014]57    {
58        RegisterObject(ScriptController);
[10065]59
60        /* By default, this controller has ID 0, which means it is not assigned
61         * to anything yet.
62         */
[10047]63        this->ctrlid_ = 0;
[10065]64
65        /* Set default values for all variables */
66        /* - pointers to zero */
67        this->player_ = NULL;
68        this->entity_ = NULL;
69
70        /* - times */
71        this->scTime = 0.0f;
72        this->eventTime = 0.0f;
73
74        /* - Points in space */
75        this->startpos = Vector3(0,0,0);
[10066]76        //this->lookAtPosition = Vector3(0,0,0);
[10065]77
78        /* - Processing flag */
79        this->processing = false;
80
81        /* - Counters */
82        this->eventno = 0;
83
[10614]84        /* - First "previous event" scheduled at t=0 */
85        /* - Needed for automatically updating event times */
86        this->prevEventTime = 0;
[10014]87    }
88
[10047]89    void ScriptController::takeControl(int ctrlid)
[10014]90    {
[10065]91        /* Output some debugging information */
[10077]92        orxout(verbose) << "ScriptController: Taking control" << endl;
93        orxout(verbose) << "This-pointer: " << this << endl;
[10065]94
95        /* Set the controller ID (the argument here should be nonzero) */
[10047]96        this->ctrlid_ = ctrlid;
[10065]97
98        /* Store the entity pointer in a private variable */
[10047]99        this->entity_ = this->player_->getControllableEntity();
100        assert(this->entity_);
[10065]101         
102        /* Add the controller here to this entity. Apparently this still leaves
103         * any preexisting human controllers in place.
104         */
[10047]105        this->entity_->setDestroyWhenPlayerLeft(false);
106        this->player_->pauseControl();
107        this->entity_->setController(this);
108        this->setControllableEntity(this->entity_);
[10065]109        this->entity_->mouseLook();
110        this->entity_->setVisible(false);
[10014]111    }
112
[10034]113    const Vector3& ScriptController::getPosition()
[10020]114    {
[10065]115      return this->entity_->getPosition();
[10028]116    }
[10014]117
[10045]118    ScriptController* ScriptController::getScriptController()
119    {
[10046]120      /* Output a message that confirms this function was called */
[10077]121      orxout(verbose) << "Great success!" << std::endl;
[10046]122
[10047]123      /* Debugging: print all the scriptcontroller object pointers */
[10045]124      for(ObjectList<ScriptController>::iterator it = 
125        ObjectList<ScriptController>::begin(); 
126        it != ObjectList<ScriptController>::end(); ++it)
[10077]127      { orxout(verbose) << "Have object in list: " << *it << endl; }
[10047]128
129      /* Find the first one with a nonzero ID */
130      for(ObjectList<ScriptController>::iterator it = 
131        ObjectList<ScriptController>::begin(); 
132        it != ObjectList<ScriptController>::end(); ++it)
[10045]133      { 
134        // TODO: do some selection here. Currently just returns the first one
[10047]135        if( (*it)->getID() > 0 )
[10077]136        { orxout(verbose) << "Controller to return: " << *it << endl;
[10047]137          return *it; 
[10065]138        }
[10045]139     
140      }
141      return NULL;
142    }
143
[10048]144    void ScriptController::execute(event ev)
145    {
[10066]146        /* Debugging output */
147        //orxout() << "Executing event " << ev.fctName
148          //<< " with parameters:\n "
149          //<< ev.x1 << " " << ev.y1 << " " << ev.z1 << "\n"
150          //<< ev.x2 << " " << ev.y2 << " " << ev.z2 << "\n"
151          //<< ev.duration << endl;
[10057]152
[10066]153        /* Event is starting, hence set the time to 0 */
[10065]154        this->eventTime = 0.0f;
155        this->processing = true;
156
[10066]157        /* Copy the event into the currentEvent holder */
158        this->currentEvent = ev;
159
160        /* Store starting position */
161        this->startpos = this->entity_->getPosition();
[10048]162    }
163
164
[10047]165    void ScriptController::tick(float dt)
166    {
[10065]167        /* Call the tick function of the classes we derive from */
168        SUPER(ScriptController, tick, dt);
[10059]169
[10047]170        /* If this controller has no entity entry, do nothing */
[10066]171        if( !(this->entity_) ) return;
[10045]172
[10065]173        /* See if time has come for the next event to be run */
174        if(this->eventList.size() > 0 && this->eventList[0].eventTime <= scTime)
[10066]175        { /* Execute the next event on the list */
[10065]176          this->execute(this->eventList[0]);
177          this->eventList.erase(this->eventList.begin());
178          this->eventno -= 1;
179        }
[10048]180
[10065]181        /* Update the local timers in this object */
[10066]182        scTime += dt; eventTime += dt;
[10065]183
184        /* If we've arrived at the target, stop processing */
[10066]185        if( eventTime > currentEvent.duration && this->processing == true)
[10065]186        { this->processing = false;
187
[10066]188          /* If we reached the last event, also reenable the normal movement
189           * and make the model visible again
190           */
[10065]191          if( this->eventno == 0 )
192          {
193            this->entity_->mouseLook();
194            this->entity_->setVisible(true);
195          }
[10048]196        }
197
[10065]198        /* Get a variable that specifies how far along the trajectory
[10614]199         * we are currently.
[10065]200         */
[10066]201        float dl = eventTime / currentEvent.duration; 
[10048]202
[10614]203        /* Depending on command */
[10065]204        /* Do some moving */
205        if( this->processing )
[10614]206        {
207          // Abbreviation for "spiral" (rotation + translation)
208          if (this->currentEvent.fctName == "spi") {
209
210            // Need to know a perpendicular basis to the translation vector:
211            // Given (a, b, c) we chose (b, -a, 0)norm and (0, c, -b)norm
212            // Currently we set a fix rotational radius of 400
213            // TODO: Add an option to adjust radius of spiral movement
214            Vector3 direction = this->currentEvent.v1 - startpos;
215
216            Vector3* ortho1 = new Vector3(direction.y, -direction.x, 0);
217            float absOrtho1 = sqrt(direction.y * direction.y + direction.x * direction.x);
218            *ortho1 = 400 * cos(2 * M_PI * dl) * (*ortho1)/absOrtho1;
219
220            Vector3* ortho2 = new Vector3(0, direction.z, -direction.y);
221            float absOrtho2 = sqrt(direction.y * direction.y + direction.z * direction.z);
222            *ortho2 = 400 * sin(2 * M_PI * dl) * (*ortho2)/absOrtho2;
223
224            this->entity_->setPosition( (1-dl)*startpos + dl * this->currentEvent.v1 + *ortho1 + *ortho2);
225
226            delete ortho1;
227            delete ortho2;
228          }
229
230          // Abbreviation for "rotate and look"
231          if (this->currentEvent.fctName == "ral")
232          { 
233            // Specify the axis
234            Vector3* a;
235              switch ((int) currentEvent.d) {
236                case 3:
237                  a = new Vector3(this->currentEvent.v1.x + this->currentEvent.e*cos(2*M_PI*dl),
238                                  this->currentEvent.v1.y + this->currentEvent.e*sin(2*M_PI*dl),
239                                  this->currentEvent.v1.z);
240                break;
241                case 2:
242                  a = new Vector3(this->currentEvent.v1.x + this->currentEvent.e*sin(2*M_PI*dl),
243                                  this->currentEvent.v1.y,
244                                  this->currentEvent.v1.z + this->currentEvent.e*cos(2*M_PI*dl));
245                break;
246                case 1:
247                  a = new Vector3(this->currentEvent.v1.x,
248                                  this->currentEvent.v1.y + this->currentEvent.e*sin(2*M_PI*dl),
249                                  this->currentEvent.v1.z + this->currentEvent.e*cos(2*M_PI*dl));
250                break;
251              }
252
253            this->entity_->setPosition(*a);
254
255            /* Look at the specified position */
256            this->entity_->lookAt(this->currentEvent.v1);
257          }
258          else if( this->currentEvent.fctName == "mal" )
[10066]259          {
260            /* Set the position to the correct place in the trajectory */
261            this->entity_->setPosition( (1-dl)*startpos + dl * this->currentEvent.v1);
[10047]262
[10066]263            /* Look at the specified position */
264            this->entity_->lookAt(this->currentEvent.v2);
265          }
266          else if( this->currentEvent.fctName == "chl" )
267          {
268            /* Sweep the look from v1 to v2 */
269            this->entity_->lookAt( (1-dl)*this->currentEvent.v1 + 
270              dl * this->currentEvent.v2 );
271          }
272
273
[10065]274          /* Force mouse look */
275          if( this->entity_->isInMouseLook() == false )
276            this->entity_->mouseLook();
277        }
278    }
[10048]279
[10065]280    void ScriptController::eventScheduler(std::string instruction, 
281      float x1, float y1, float z1, 
282      float x2, float y2, float z2, 
283      float duration, float executionTime)
[10048]284    {
[10065]285      /* put data (from LUA) into time-sorted eventList*/ 
286      /* Nimmt den befehl und die argumente aus luascript und ertellt einen
287       * struct pro event, diese structs werden sortiert nach eventTime
288       */
289      struct event tmp;
[10057]290
[10065]291      /* Fill the structure with all the provided information */
292      tmp.fctName = instruction;
[10614]293
[10066]294      //tmp.x1 = x1; tmp.y1 = y1; tmp.z1 = z1;
295      //tmp.x2 = x2; tmp.y2 = y2; tmp.z2 = z2;
296      tmp.v1 = Vector3(x1,y1,z1);
297      tmp.v2 = Vector3(x2,y2,z2);
[10614]298
299      // the parameters are not required to be vector coordinates!
300      // for convenience they are however stored twice if they have some kind of different meaning
301      tmp.a = x1;
302      tmp.b = y1;
303      tmp.c = z1;
304      tmp.d = x2;
305      tmp.e = y2;
306      tmp.f = z2;
307
[10065]308      tmp.duration = duration;
[10059]309
[10614]310      /* This is kind of a hack. If we hit the function idle all we want to do is
311         advance event execution time, not schedule anything */
312      if (instruction == "idle") {
313        tmp.eventTime = this->prevEventTime;
314        this->prevEventTime += x1;
315        return;
316      } else {
317        tmp.eventTime = this->prevEventTime;
318        this->prevEventTime += duration;
319      }
[10047]320
[10065]321      /* Add the created event to the event list */
322      if(eventList.size()==0)
323      { /* The list is still empty, just add it */
[10077]324        orxout(verbose) << "eventList empty (01)" << endl;
[10065]325        eventList.insert(eventList.begin(), tmp);
326        this->eventno += 1;
327        return; /* Nothing more to do, the event was added */
328      }
[10059]329
[10065]330      /* Event was not added yet since the list was not empty. Walk through
331       * the list of events and add it so that the events are correctly in
332       * order.
333       */
334      for (std::vector<event>::iterator it=eventList.begin(); it<eventList.end(); it++)
335      { if(tmp.eventTime < it->eventTime)
336        { eventList.insert(it,tmp);
337          this->eventno += 1;
[10076]338          //orxout()<<"new event added"<<endl;
[10065]339          return;
[10059]340        }
[10065]341      }
[10034]342
[10065]343      /* If the event was still not added here, it belongs in the end of the list */
344      eventList.insert(eventList.end(), tmp);
345      this->eventno += 1;
[10059]346
[10048]347    }
[10014]348}
Note: See TracBrowser for help on using the repository browser.