Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10059 was 10059, checked in by samuezu, 11 years ago

works more or less, but big trouble accessing the scriptableController-controlled entity

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
42    std::vector<event> eventList;
43
44   
45
46
47
48    RegisterClass(ScriptController);
49
50    //ScriptController::ScriptController(Context* context, ControllableEntity* CE) : ArtificialController(context)
51    ScriptController::ScriptController(Context* context) : ArtificialController(context)
52    {
53        RegisterObject(ScriptController);
54        //set_controlled(CE);
55        this->ctrlid_ = 0;
56    }
57
58    void ScriptController::takeControl(int ctrlid)
59    {
60        orxout() << "ScriptController: Taking control" << endl;
61        orxout() << "This-pointer: " << this << endl;
62        this->ctrlid_ = ctrlid;
63        this->entity_ = this->player_->getControllableEntity();
64        assert(this->entity_);
65
66        this->entity_->setDestroyWhenPlayerLeft(false);
67        this->player_->pauseControl();
68        this->entity_->setController(this);
69        this->setControllableEntity(this->entity_);
70    }
71
72    /* Yet to be implemented and tested */
73    //void ScriptController::yieldControl()
74    //{
75        //this->player_->startControl(this->entity_);
76        //this->setActive(false);
77        //this->controllableEntity_ = NULL;
78    //}
79
80    void ScriptController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
81    {
82        //XMLPortParam(ScriptController, BaseObject, "lsrc", set_luasrc, xmlelement, mode);
83
84    }
85
86    const Vector3& ScriptController::getPosition()
87    {
88        return this->entity_->getPosition();
89    }
90
91    ScriptController* ScriptController::getScriptController()
92    {
93      /* Output a message that confirms this function was called */
94      orxout() << "Great success!" << std::endl;
95
96      /* Debugging: print all the scriptcontroller object pointers */
97      for(ObjectList<ScriptController>::iterator it = 
98        ObjectList<ScriptController>::begin(); 
99        it != ObjectList<ScriptController>::end(); ++it)
100      { orxout() << "Have object in list: " << *it << endl; }
101
102      /* Find the first one with a nonzero ID */
103      for(ObjectList<ScriptController>::iterator it = 
104        ObjectList<ScriptController>::begin(); 
105        it != ObjectList<ScriptController>::end(); ++it)
106      { 
107        // TODO: do some selection here. Currently just returns the first one
108        if( (*it)->getID() > 0 )
109          return *it; 
110     
111      }
112      return NULL;
113    }
114
115    void ScriptController::execute(event ev)
116    {
117        if(ev.fctName=="moveToPosition_beta")
118        {
119
120            moveToPosition_beta(ev.xCoord,ev.yCoord,ev.zCoord);
121        }
122    }
123
124
125    void ScriptController::tick(float dt)
126    {
127
128       
129
130        /* If this controller has no entity entry, do nothing */
131        if( !(this->entity_) )
132          return;
133
134        //orxout() << "Rotating!" << endl;
135
136        //this->entity_->rotateYaw(-1.0f * 100.0f * dt);
137        //this->entity_->rotatePitch(0.8f * 100.0f);
138
139        if(eventList.size() > 0 && eventList[0].eventTime<=scTime)
140        {
141            /*TO DO: execute the function: eventList[0].fctName*/
142
143            execute(eventList[0]);
144            eventList.erase(eventList.begin());
145        }
146
147        SUPER(ScriptController, tick, dt);
148
149        scTime=scTime+dt;
150    }
151
152
153
154
155    void ScriptController::moveToPosition_beta(float x, float y, float z )
156    {
157
158        orxout()<<"moveToPosition_beta executed"<<endl;
159        //const Vector3 local = this->getPosition();
160        const Vector3 target = Vector3(100*x,100*y,100*z);
161        //Vector3 way = target-local;
162        orxout() << "Moving This-pointer: " << this << endl;
163       
164       
165        //this->entity_->lookAt(target);
166        //this->entity_->moveFrontBack(-1000*target.length());     
167
168        if(this->entity_!=NULL)
169            orxout()<<"not-NULL-entity"<<endl;
170
171        if(this->player_!=NULL)
172            orxout()<<"not-NULL-player"<<endl;
173
174        orxout() << this->player_->getClientID() << endl; // IMPOSSIBLE TO ACCESS this->player AND this->entity
175       
176        //this->entity_ = this->player_->getClientID();//getControllableEntity();
177
178            //if(this->entity_==this->player_->getControllableEntity())
179            //orxout()<<"same entity"<<endl;
180
181        /* This works fine */
182        orxout()<<x<<"  "<<y<<"  "<<z<<endl;
183    }
184
185    void ScriptController::eventScheduler(std::string instruction, float x, float y, float z, float executionTime)
186    {
187
188
189        /*put data (from LUA) into time-sorted eventList*/ 
190        /*nimmt den befehl und die argumente aus luascript und ertellt einen struct pro event, diese structs werden sortiert nach eventTime*/
191        struct event tmp;
192        tmp.fctName=instruction;
193        tmp.xCoord=x;
194        tmp.yCoord=y;
195        tmp.zCoord=z;
196        tmp.eventTime=executionTime;
197
198        orxout()<<tmp.fctName<<endl;
199
200        if(eventList.size()==0)
201        {
202            orxout()<<"eventList empty (01)"<<endl;
203            eventList.insert(eventList.begin(), tmp);
204            orxout()<<"first event added"<<endl;
205        }
206
207
208       for (std::vector<event>::iterator it=eventList.begin(); it<eventList.end(); it++)
209            {
210
211                if(tmp.eventTime<it->eventTime)
212                {
213                    eventList.insert(it,tmp);
214                    orxout()<<"new event added"<<endl;
215                }
216
217            }
218
219       
220       if(eventList.size()==0)
221            orxout()<<"eventList empty"<<endl;
222
223        else
224            orxout()<<"eventList is not empty"<<endl;
225
226       
227    }
228
229
230
231    /* TODO:    struct event erweitern um mehr funktionen benutzen zu koennen
232
233                mehr funktionen definieren (und dann in  execute if(...))
234                NB: viele noetige funktionen sind schon in artificial- bzw formationcontroller vorhanden */       
235
236
237
238}
Note: See TracBrowser for help on using the repository browser.