Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc @ 10064

Last change on this file since 10064 was 10064, checked in by smerkli, 10 years ago

Test commit to see if committing still works

File size: 6.7 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#include "ScriptController.h"
30#include "infos/PlayerInfo.h"
31#include "core/CoreIncludes.h"
32#include "worldentities/ControllableEntity.h"
33#include "core/LuaState.h"
34#include <cmath>
35
36namespace orxonox
37{
38    float scTime=0;  /*initialize time, to coordinate eventTime*/
39
40
41    std::vector<event> eventList;
42
43   
44
45    RegisterClass(ScriptController);
46
47    //ScriptController::ScriptController(Context* context, ControllableEntity* CE) : ArtificialController(context)
48    ScriptController::ScriptController(Context* context) : ArtificialController(context)
49    {
50        RegisterObject(ScriptController);
51        //set_controlled(CE);
52        this->ctrlid_ = 0;
53    }
54
55    void ScriptController::takeControl(int ctrlid)
56    {
57        orxout() << "ScriptController: Taking control" << endl;
58        orxout() << "This-pointer: " << this << endl;
59        this->ctrlid_ = ctrlid;
60        this->entity_ = this->player_->getControllableEntity();
61        assert(this->entity_);
62
63        this->entity_->setDestroyWhenPlayerLeft(false);
64        this->player_->pauseControl();
65        this->entity_->setController(this);
66        this->setControllableEntity(this->entity_);
67    }
68
69    /* Yet to be implemented and tested */
70    //void ScriptController::yieldControl()
71    //{
72        //this->player_->startControl(this->entity_);
73        //this->setActive(false);
74        //this->controllableEntity_ = NULL;
75    //}
76
77    void ScriptController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
78    {
79        //XMLPortParam(ScriptController, BaseObject, "lsrc", set_luasrc, xmlelement, mode);
80
81    }
82
83    const Vector3& ScriptController::getPosition()
84    {
85        return this->entity_->getPosition();
86    }
87
88    ScriptController* ScriptController::getScriptController()
89    {
90      /* Output a message that confirms this function was called */
91      orxout() << "Great success!" << std::endl;
92
93      /* Debugging: print all the scriptcontroller object pointers */
94      for(ObjectList<ScriptController>::iterator it = 
95        ObjectList<ScriptController>::begin(); 
96        it != ObjectList<ScriptController>::end(); ++it)
97      { orxout() << "Have object in list: " << *it << endl; }
98
99      /* Find the first one with a nonzero ID */
100      for(ObjectList<ScriptController>::iterator it = 
101        ObjectList<ScriptController>::begin(); 
102        it != ObjectList<ScriptController>::end(); ++it)
103      { 
104        // TODO: do some selection here. Currently just returns the first one
105        if( (*it)->getID() > 0 )
106          return *it; 
107     
108      }
109      return NULL;
110    }
111
112    void ScriptController::execute(event ev)
113    {
114        if(ev.fctName=="moveToPosition_beta")
115        {
116
117            moveToPosition_beta(ev.xCoord,ev.yCoord,ev.zCoord);
118        }
119    }
120
121
122    void ScriptController::tick(float dt)
123    {
124
125       
126
127        /* If this controller has no entity entry, do nothing */
128        if( !(this->entity_) )
129          return;
130
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)
137        {
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
235}
Note: See TracBrowser for help on using the repository browser.