Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial6/src/orxonox/controllers/ScriptController.cc @ 11479

Last change on this file since 11479 was 11083, checked in by muemart, 9 years ago

Fix some clang-tidy warnings.
Also, Serialise.h was doing some C-style casts that ended up being const casts. I moved those const casts as close to the source as possible and changed the loadAndIncrease functions to not do that.

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