Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SciptableControllerFS15/src/orxonox/controllers/ScriptController.cc @ 10487

Last change on this file since 10487 was 10448, checked in by rgraczyk, 10 years ago

Various changes, see comments in cpp file

  • Property svn:eol-style set to native
File size: 12.4 KB
Line 
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:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
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
43// TODO: Which library can this be found in?
44#define M_PI 3.14159265358979323846 /* pi */
45
46#include "ScriptController.h"
47#include "infos/PlayerInfo.h"
48#include "core/CoreIncludes.h"
49#include "worldentities/ControllableEntity.h"
50#include "core/LuaState.h"
51#include "core/LuaState.h"
52#include <cmath>
53
54
55namespace orxonox
56{
57    RegisterClass(ScriptController);
58
59    ScriptController::ScriptController(Context* context) : ArtificialController(context)
60    {
61        RegisterObject(ScriptController);
62
63        /* By default, this controller has ID 0, which means it is not assigned
64         * to anything yet.
65         */
66        this->ctrlid_ = 0;
67
68        /* Set default values for all variables */
69        /* - pointers to zero */
70        this->player_ = NULL;
71        this->entity_ = NULL;
72
73        /* - times */
74        this->scTime = 0.0f;
75        this->eventTime = 0.0f;
76
77        /* - Points in space */
78        this->startpos = Vector3(0,0,0);
79        //this->lookAtPosition = Vector3(0,0,0);
80
81        /* - Processing flag */
82        this->processing = false;
83
84        /* - Counters */
85        this->eventno = 0;
86
87        /* - First "previous event" scheduled at t=0 */
88        /* - Needed for automatically updating event times */
89        this->prevEventTime = 0;
90
91        /* hack */
92        this->deltat;
93    }
94
95    void ScriptController::takeControl(int ctrlid)
96    {
97        /* Output some debugging information */
98        orxout(verbose) << "ScriptController: Taking control" << endl;
99        orxout(verbose) << "This-pointer: " << this << endl;
100
101        /* Set the controller ID (the argument here should be nonzero) */
102        this->ctrlid_ = ctrlid;
103
104        /* Store the entity pointer in a private variable */
105        this->entity_ = this->player_->getControllableEntity();
106        assert(this->entity_);
107         
108        /* Add the controller here to this entity. Apparently this still leaves
109         * any preexisting human controllers in place.
110         */
111        this->entity_->setDestroyWhenPlayerLeft(false);
112        this->player_->pauseControl();
113        this->entity_->setController(this);
114        this->setControllableEntity(this->entity_);
115        this->entity_->mouseLook();
116        this->entity_->setVisible(false);
117    }
118
119    const Vector3& ScriptController::getPosition()
120    {
121      return this->entity_->getPosition();
122    }
123
124    ScriptController* ScriptController::getScriptController()
125    {
126      /* Output a message that confirms this function was called */
127      orxout(verbose) << "Great success!" << std::endl;
128
129      /* Debugging: print all the scriptcontroller object pointers */
130      for(ObjectList<ScriptController>::iterator it = 
131        ObjectList<ScriptController>::begin(); 
132        it != ObjectList<ScriptController>::end(); ++it)
133      { orxout(verbose) << "Have object in list: " << *it << endl; }
134
135      /* Find the first one with a nonzero ID */
136      for(ObjectList<ScriptController>::iterator it = 
137        ObjectList<ScriptController>::begin(); 
138        it != ObjectList<ScriptController>::end(); ++it)
139      { 
140        // TODO: do some selection here. Currently just returns the first one
141        if( (*it)->getID() > 0 )
142        { orxout(verbose) << "Controller to return: " << *it << endl;
143          return *it; 
144        }
145     
146      }
147      return NULL;
148    }
149
150    void ScriptController::execute(event ev)
151    {
152        /* Debugging output */
153        //orxout() << "Executing event " << ev.fctName
154          //<< " with parameters:\n "
155          //<< ev.x1 << " " << ev.y1 << " " << ev.z1 << "\n"
156          //<< ev.x2 << " " << ev.y2 << " " << ev.z2 << "\n"
157          //<< ev.duration << endl;
158
159        /* Event is starting, hence set the time to 0 */
160        this->eventTime = 0.0f;
161        this->processing = true;
162
163        /* Copy the event into the currentEvent holder */
164        this->currentEvent = ev;
165
166        /* Store starting position */
167        this->startpos = this->entity_->getPosition();
168    }
169
170
171    void ScriptController::tick(float dt)
172    {
173        /* hack */
174        this->deltat = dt;
175
176        /* Call the tick function of the classes we derive from */
177        SUPER(ScriptController, tick, dt);
178
179        /* If this controller has no entity entry, do nothing */
180        if( !(this->entity_) ) return;
181
182        /* See if time has come for the next event to be run */
183        if(this->eventList.size() > 0 && this->eventList[0].eventTime <= scTime)
184        { /* Execute the next event on the list */
185          this->execute(this->eventList[0]);
186          this->eventList.erase(this->eventList.begin());
187          this->eventno -= 1;
188        }
189
190        /* Update the local timers in this object */
191        scTime += dt; eventTime += dt;
192
193        /* If we've arrived at the target, stop processing */
194        if( eventTime > currentEvent.duration && this->processing == true)
195        { this->processing = false;
196
197          /* If we reached the last event, also reenable the normal movement
198           * and make the model visible again
199           */
200          if( this->eventno == 0 )
201          {
202            this->entity_->mouseLook();
203            this->entity_->setVisible(true);
204          }
205        }
206
207        /* Get a variable that specifies how far along the trajectory
208         * we are currently.
209         */
210        float dl = eventTime / currentEvent.duration; 
211
212        /* Depending on command */
213        /* Do some moving */
214        if( this->processing )
215        {
216          // Abbreviation for "spiral" (rotation + translation)
217          if (this->currentEvent.fctName == "spi") {
218
219            // Need to know a perpendicular basis to the translation vector:
220            // Given (a, b, c) we chose (b, -a, 0)norm and (0, c, -b)norm
221            // Currently we set a fix rotational radius of 400
222            // TODO: Add an option to adjust radius of spiral movement
223            Vector3 direction = this->currentEvent.v1 - startpos;
224
225            Vector3* ortho1 = new Vector3(direction.y, -direction.x, 0);
226            float absOrtho1 = sqrt(direction.y * direction.y + direction.x * direction.x);
227            *ortho1 = 400 * cos(2 * M_PI * dl) * (*ortho1)/absOrtho1;
228
229            Vector3* ortho2 = new Vector3(0, direction.z, -direction.y);
230            float absOrtho2 = sqrt(direction.y * direction.y + direction.z * direction.z);
231            *ortho2 = 400 * sin(2 * M_PI * dl) * (*ortho2)/absOrtho2;
232
233            this->entity_->setPosition( (1-dl)*startpos + dl * this->currentEvent.v1 + *ortho1 + *ortho2);
234
235            delete ortho1;
236            delete ortho2;
237          }
238
239          // Abbreviation for "rotate and look"
240          if (this->currentEvent.fctName == "ral")
241          { 
242            // Specify the axis
243            Vector3* a;
244              switch ((int) currentEvent.d) {
245                case 3:
246                  a = new Vector3(this->currentEvent.v1.x + 3000*cos(2*M_PI*dl),
247                                  this->currentEvent.v1.y + 3000*sin(2*M_PI*dl),
248                                  this->currentEvent.v1.z);
249                break;
250                case 2:
251                  a = new Vector3(this->currentEvent.v1.x + 3000*cos(2*M_PI*dl),
252                                  this->currentEvent.v1.y,
253                                  this->currentEvent.v1.z + 3000*cos(2*M_PI*dl));
254                break;
255                case 1:
256                  a = new Vector3(this->currentEvent.v1.x,
257                                  this->currentEvent.v1.y + 3000*sin(2*M_PI*dl),
258                                  this->currentEvent.v1.z + 3000*cos(2*M_PI*dl));
259                break;
260              }
261
262            this->entity_->setPosition(*a);
263
264            /* Look at the specified position */
265            this->entity_->lookAt(this->currentEvent.v1);
266          }
267          else if( this->currentEvent.fctName == "mal" )
268          {
269            /* Set the position to the correct place in the trajectory */
270            this->entity_->setPosition( (1-dl)*startpos + dl * this->currentEvent.v1);
271
272            /* Look at the specified position */
273            this->entity_->lookAt(this->currentEvent.v2);
274          }
275          else if( this->currentEvent.fctName == "chl" )
276          {
277            /* Sweep the look from v1 to v2 */
278            this->entity_->lookAt( (1-dl)*this->currentEvent.v1 + 
279              dl * this->currentEvent.v2 );
280          }
281
282
283          /* Force mouse look */
284          if( this->entity_->isInMouseLook() == false )
285            this->entity_->mouseLook();
286        }
287    }
288
289    void ScriptController::eventScheduler(std::string instruction, 
290      float x1, float y1, float z1, 
291      float x2, float y2, float z2, 
292      float duration, float executionTime)
293    {
294      /* put data (from LUA) into time-sorted eventList*/ 
295      /* Nimmt den befehl und die argumente aus luascript und ertellt einen
296       * struct pro event, diese structs werden sortiert nach eventTime
297       */
298      struct event tmp;
299
300      /* Fill the structure with all the provided information */
301      tmp.fctName = instruction;
302
303      //tmp.x1 = x1; tmp.y1 = y1; tmp.z1 = z1;
304      //tmp.x2 = x2; tmp.y2 = y2; tmp.z2 = z2;
305      tmp.v1 = Vector3(x1,y1,z1);
306      tmp.v2 = Vector3(x2,y2,z2);
307
308      // the parameters are not required to be vector coordinates!
309      // for convenience they are however stored twice if they have some kind of different meaning
310      tmp.a = x1;
311      tmp.b = y1;
312      tmp.c = z1;
313      tmp.d = x2;
314      tmp.e = y2;
315      tmp.f = z2;
316
317      tmp.duration = duration;
318
319      /* This is kind of a hack. If we hit the function idle all we want to do is
320         advance event execution time, not schedule anything */
321      if (instruction == "idle") {
322        tmp.eventTime = this->prevEventTime;
323        this->prevEventTime += x1;
324        return;
325      } else {
326        tmp.eventTime = this->prevEventTime;
327        this->prevEventTime += duration;
328      }
329
330      /* Add the created event to the event list */
331      if(eventList.size()==0)
332      { /* The list is still empty, just add it */
333        orxout(verbose) << "eventList empty (01)" << endl;
334        eventList.insert(eventList.begin(), tmp);
335        this->eventno += 1;
336        return; /* Nothing more to do, the event was added */
337      }
338
339      /* Event was not added yet since the list was not empty. Walk through
340       * the list of events and add it so that the events are correctly in
341       * order.
342       */
343      for (std::vector<event>::iterator it=eventList.begin(); it<eventList.end(); it++)
344      { if(tmp.eventTime < it->eventTime)
345        { eventList.insert(it,tmp);
346          this->eventno += 1;
347          //orxout()<<"new event added"<<endl;
348          return;
349        }
350      }
351
352      /* If the event was still not added here, it belongs in the end of the list */
353      eventList.insert(eventList.end(), tmp);
354      this->eventno += 1;
355
356    }
357}
Note: See TracBrowser for help on using the repository browser.