Changeset 10065 for code/branches
- Timestamp:
- May 21, 2014, 2:32:41 PM (11 years ago)
- Location:
- code/branches/ScriptableController
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/ScriptableController/data/gui/scripts/testscript.lua
r10057 r10065 1 --k = 1.2 1 -- Set some test variables 2 xm = 1000 3 ym = 0 4 zm = 0 2 5 3 --orxonox.execute("orxout message test " .. k) 6 xl = -1000 7 yl = 3000 8 zl = -1000 4 9 5 -- Set some test variables 6 x = 1.1 7 y = 2.2 8 z = 3.3 10 d = 5 11 Tabs = 0 9 12 10 --os.execute("sleep " .. 2) 13 pi = 3.1415 11 14 12 15 -- Get a local pointer to a scriptcontroller … … 15 18 -- If it worked, call its "movetoposition" function 16 19 if ctrl ~= nil then 17 ctrl:eventScheduler("moveToPosition_beta", x, y, z, 0.01) 20 -- Move to the starting point at (xl+3000,yl,zl) while looking 21 -- at xl,yl,zl over the time span of 3 seconds 22 ctrl:eventScheduler("mal", xl+3000,yl,zl, xl,yl,zl, 3, 0) 23 24 -- From there, perform a rotation around the harvester placed there 25 -- in 100 steps 26 dt = math.pi/100 27 for t = 0,math.pi,dt do 28 xt = math.cos(t) 29 yt = math.sin(t) 30 31 ctrl:eventScheduler("mal", xl+3000*xt, yl+3000*yt, zl, xl, yl, zl, 3*dt, 3*t+2.9) 32 end 33 34 -- Update absolute time 35 Tabs = 3*math.pi + 2.9 36 37 -- Move away again, still looking at the station 38 ctrl:eventScheduler("mal", 0,0,1000, xl,yl,zl, 3, Tabs+0.4) 39 40 18 41 end 19 42 -
code/branches/ScriptableController/data/levels/levelTry.oxw
r10059 r10065 47 47 <SpawnPoint team=0 position="3200,0,0" lookat="2800,0,0" spawnclass=SpaceShip pawndesign=spaceshipescort /> 48 48 49 <!-- 49 50 <StaticEntity position = "2800,0,0" mass=10000 collisionType=static > 50 51 <attached> … … 52 53 </attached> 53 54 <collisionShapes> 54 <BoxCollisionShape position="-560,0,0" halfExtents="115,100,245" /> <!-- Three lower boxes -->55 <BoxCollisionShape position="-560,0,0" halfExtents="115,100,245" /> 55 56 <BoxCollisionShape position="290,0,-480" halfExtents="115,100,245" yaw=-120 /> 56 57 <BoxCollisionShape position="290,0,480" halfExtents="115,100,245" yaw=-240 /> 57 <BoxCollisionShape position="-280,0,0" halfExtents="163,50,50" /> <!-- Three lower connections -->58 <BoxCollisionShape position="-280,0,0" halfExtents="163,50,50" /> 58 59 <BoxCollisionShape position="140,0,-240" halfExtents="163,50,50" yaw=-120 /> 59 60 <BoxCollisionShape position="140,0,240" halfExtents="163,50,50" yaw=-240 /> 60 <BoxCollisionShape position="0,530,0" halfExtents="172,52,298" /> <!-- Upper Tower -->61 <BoxCollisionShape position="0,530,0" halfExtents="172,52,298" /> 61 62 <BoxCollisionShape position="0,530,0" halfExtents="172,52,298" yaw=-120 /> 62 63 <BoxCollisionShape position="0,530,0" halfExtents="172,52,298" yaw=-240 /> 63 <BoxCollisionShape position="0,400,0" halfExtents="43,110,26" yaw=-30 /> <!-- Middle one-->64 <BoxCollisionShape position="-200,100,0" halfExtents="26,50,43" /> <!--Three lower legs -->64 <BoxCollisionShape position="0,400,0" halfExtents="43,110,26" yaw=-30 /> 65 <BoxCollisionShape position="-200,100,0" halfExtents="26,50,43" /> 65 66 <BoxCollisionShape position="100,100,-173" halfExtents="43,50,26" yaw=-30 /> 66 67 <BoxCollisionShape position="100,100,-173" halfExtents="43,50,26" yaw=30 /> 67 <BoxCollisionShape position="-100,264,0" halfExtents="26,105,43" roll=-49 /> <!--Three upper legs -->68 <BoxCollisionShape position="-100,264,0" halfExtents="26,105,43" roll=-49 /> 68 69 <BoxCollisionShape position="50,264,-87" halfExtents="26,105,43" roll=-49 yaw=-120 /> 69 70 <BoxCollisionShape position="50,264,87" halfExtents="26,105,43" roll=-49 yaw=-240 /> 70 71 </collisionShapes> 71 72 </StaticEntity> 73 --> 72 74 73 75 … … 77 79 /> 78 80 81 <StaticEntity position = "0,0,0" mass=10000 collisionType=static > 82 <attached> 83 <Model mesh="HydroHarvester.mesh" mass=10 position="0,0,0" scale=10 /> 84 </attached> 85 </StaticEntity> 86 87 <StaticEntity position = "-1000,3000,-1000" mass=10000 collisionType=static > 88 <attached> 89 <Model mesh="HydroHarvester.mesh" mass=10 position="0,0,0" scale=80 /> 90 </attached> 91 </StaticEntity> 79 92 80 93 <!-- ControllerDirector waits for the event takeControl to attach a new Controller --> … … 84 97 <EventListener event="takeControl" /> 85 98 </takeControl> 86 87 99 </events> 88 89 100 </ControllerDirector> 90 91 <!-- -->92 <ScriptController position="0,0,1">93 94 95 </ScriptController>96 97 101 98 102 <!-- FPS Player as destination of the dock --> -
code/branches/ScriptableController/src/orxonox/controllers/ControllerDirector.cc
r10059 r10065 89 89 /* Assemble a string to define a controller id variable in lua space */ 90 90 std::stringstream tmp; 91 tmp << "newctrlid = " << ctrlid ;91 tmp << "newctrlid = " << ctrlid << endl; 92 92 std::string todo = tmp.str(); 93 93 -
code/branches/ScriptableController/src/orxonox/controllers/ScriptController.cc
r10064 r10065 36 36 namespace orxonox 37 37 { 38 float scTime=0; /*initialize time, to coordinate eventTime*/39 40 41 std::vector<event> eventList;42 43 44 45 38 RegisterClass(ScriptController); 46 39 47 //ScriptController::ScriptController(Context* context, ControllableEntity* CE) : ArtificialController(context)48 40 ScriptController::ScriptController(Context* context) : ArtificialController(context) 49 41 { 50 42 RegisterObject(ScriptController); 51 //set_controlled(CE); 43 44 /* By default, this controller has ID 0, which means it is not assigned 45 * to anything yet. 46 */ 52 47 this->ctrlid_ = 0; 48 49 50 /* Set default values for all variables */ 51 /* - pointers to zero */ 52 this->player_ = NULL; 53 this->entity_ = NULL; 54 55 /* - times */ 56 this->scTime = 0.0f; 57 this->timeToTarget = 0.0f; 58 this->eventTime = 0.0f; 59 60 /* - Points in space */ 61 this->target = Vector3(0,0,0); 62 this->startpos = Vector3(0,0,0); 63 this->lookAtPosition = Vector3(0,0,0); 64 65 /* - Processing flag */ 66 this->processing = false; 67 68 /* - Counters */ 69 this->eventno = 0; 70 53 71 } 54 72 55 73 void ScriptController::takeControl(int ctrlid) 56 74 { 75 /* Output some debugging information */ 57 76 orxout() << "ScriptController: Taking control" << endl; 58 77 orxout() << "This-pointer: " << this << endl; 78 79 /* Set the controller ID (the argument here should be nonzero) */ 59 80 this->ctrlid_ = ctrlid; 81 82 /* Store the entity pointer in a private variable */ 60 83 this->entity_ = this->player_->getControllableEntity(); 61 84 assert(this->entity_); 62 85 86 /* Add the controller here to this entity. Apparently this still leaves 87 * any preexisting human controllers in place. 88 */ 63 89 this->entity_->setDestroyWhenPlayerLeft(false); 64 90 this->player_->pauseControl(); 65 91 this->entity_->setController(this); 66 92 this->setControllableEntity(this->entity_); 93 this->entity_->mouseLook(); 94 this->entity_->setVisible(false); 67 95 } 68 96 … … 75 103 //} 76 104 77 void ScriptController::XMLPort(Element& xmlelement, XMLPort::Mode mode)78 {79 //XMLPortParam(ScriptController, BaseObject, "lsrc", set_luasrc, xmlelement, mode);80 81 }82 83 105 const Vector3& ScriptController::getPosition() 84 106 { 85 107 return this->entity_->getPosition(); 86 108 } 87 109 … … 104 126 // TODO: do some selection here. Currently just returns the first one 105 127 if( (*it)->getID() > 0 ) 128 { orxout() << "Controller to return: " << *it << endl; 106 129 return *it; 130 } 107 131 108 132 } … … 112 136 void ScriptController::execute(event ev) 113 137 { 114 if(ev.fctName=="moveToPosition_beta") 115 { 116 117 moveToPosition_beta(ev.xCoord,ev.yCoord,ev.zCoord); 118 } 138 orxout() << "Executing event " << ev.fctName 139 << " with parameters:\n " 140 << ev.x1 << " " << ev.y1 << " " << ev.z1 << "\n" 141 << ev.x2 << " " << ev.y2 << " " << ev.z2 << "\n" 142 << ev.duration << endl; 143 144 this->eventTime = 0.0f; 145 this->startpos = this->entity_->getPosition(); 146 this->processing = true; 147 148 if(ev.fctName == "mal") 149 moveAndLook(ev.x1, ev.y1, ev.z1, ev.x2, ev.y2, ev.z2, ev.duration); 119 150 } 120 151 … … 122 153 void ScriptController::tick(float dt) 123 154 { 124 125 155 /* Call the tick function of the classes we derive from */ 156 SUPER(ScriptController, tick, dt); 126 157 127 158 /* If this controller has no entity entry, do nothing */ … … 129 160 return; 130 161 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) 162 //orxout() << "Size 0: " << this->eventList.size() << endl; 163 164 /* See if time has come for the next event to be run */ 165 if(this->eventList.size() > 0 && this->eventList[0].eventTime <= scTime) 137 166 { 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 167 /* Execute the next event on the list */ 168 this->execute(this->eventList[0]); 169 this->eventList.erase(this->eventList.begin()); 170 this->eventno -= 1; 171 //orxout() << "Size 1: " << this->eventList.size() << endl; 172 //orxout() << "Eventno is now: " << this->eventno << endl; 173 } 174 175 /* Update the local timers in this object */ 176 scTime += dt; 177 eventTime += dt; 178 179 /* If we've arrived at the target, stop processing */ 180 if( eventTime > timeToTarget && this->processing == true) 181 { this->processing = false; 182 183 //orxout() << "Size 4: " << this->eventList.size() << endl; 184 //orxout() << "Eventno: " << this->eventno << endl; 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 195 */ 196 float dl = eventTime / timeToTarget; 197 198 /* Do some moving */ 199 if( this->processing ) 200 { 201 /* Set the position to the correct place in the trajectory */ 202 this->entity_->setPosition( (1-dl)*startpos + dl * target ); 203 204 /* Look at the specified position */ 205 this->entity_->lookAt(lookAtPosition); 206 207 /* Force mouse look */ 208 if( this->entity_->isInMouseLook() == false ) 209 this->entity_->mouseLook(); 210 } 211 } 212 213 void ScriptController::moveAndLook( 214 float xm, float ym, float zm, 215 float xl, float yl, float zl, 216 float t ) 217 { 218 orxout()<<"moveAndLook executed"<<endl; 219 220 /* Set the local variables required for this event */ 221 this->target = Vector3(xm,ym,zm); 222 this->lookAtPosition = Vector3(xl,yl,zl); 223 this->timeToTarget = t; 224 225 226 orxout() << "Moving This-pointer: " << this << endl; 227 228 if(this->entity_ != NULL) 229 orxout()<<"not-NULL-entity"<<endl; 230 else 231 return; 232 233 if(this->player_ != NULL) 234 orxout()<<"not-NULL-player"<<endl; 235 else 236 return; 237 } 238 239 void ScriptController::eventScheduler(std::string instruction, 240 float x1, float y1, float z1, 241 float x2, float y2, float z2, 242 float duration, float executionTime) 243 { 244 /* put data (from LUA) into time-sorted eventList*/ 245 /* Nimmt den befehl und die argumente aus luascript und ertellt einen 246 * struct pro event, diese structs werden sortiert nach eventTime 247 */ 248 struct event tmp; 249 250 /* Fill the structure with all the provided information */ 251 tmp.fctName = instruction; 252 tmp.x1 = x1; tmp.y1 = y1; tmp.z1 = z1; 253 tmp.x2 = x2; tmp.y2 = y2; tmp.z2 = z2; 254 tmp.duration = duration; 255 tmp.eventTime = executionTime; 256 257 orxout() << tmp.fctName << endl; 258 259 /* Add the created event to the event list */ 260 if(eventList.size()==0) 261 { /* The list is still empty, just add it */ 262 orxout() << "eventList empty (01)" << endl; 263 eventList.insert(eventList.begin(), tmp); 264 this->eventno += 1; 265 return; /* Nothing more to do, the event was added */ 266 } 267 268 /* Event was not added yet since the list was not empty. Walk through 269 * the list of events and add it so that the events are correctly in 270 * order. 271 */ 272 for (std::vector<event>::iterator it=eventList.begin(); it<eventList.end(); it++) 273 { if(tmp.eventTime < it->eventTime) 274 { eventList.insert(it,tmp); 275 this->eventno += 1; 276 orxout()<<"new event added"<<endl; 277 return; 278 } 279 } 280 281 /* If the event was still not added here, it belongs in the end of the list */ 282 eventList.insert(eventList.end(), tmp); 283 this->eventno += 1; 284 285 } 235 286 } -
code/branches/ScriptableController/src/orxonox/controllers/ScriptController.h
r10059 r10065 42 42 { 43 43 std::string fctName; 44 float xCoord; 45 float yCoord; 46 float zCoord; 44 float x1; 45 float y1; 46 float z1; 47 48 float x2; 49 float y2; 50 float z2; 51 52 float duration; 47 53 48 54 float eventTime; … … 54 60 { // tolua_export 55 61 public: 56 //ScriptController(Context* context, ControllableEntity* CE);57 62 ScriptController(Context* context); 58 63 59 64 virtual ~ScriptController() { } 60 65 61 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);66 //virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 62 67 63 68 void takeControl(int ctrlid); 64 69 void setPlayer(PlayerInfo* player) { this->player_ = player; } 65 70 66 67 68 //void set_luasrc(std::string);69 //void set_controlled(ControllableEntity*);70 71 71 virtual void tick(float dt); 72 72 73 73 // LUA interface 74 74 // tolua_begin 75 void moveToPosition_beta(float x, float y, float z); 75 void moveAndLook(float xm, float ym, float zm, 76 float xl, float yl, float zl, float t); 76 77 77 void eventScheduler(std::string instruction, float x, float y, float z, float time); 78 void eventScheduler(std::string instruction, 79 float x1, float y1, float z1, 80 float x2, float y2, float z2, 81 float duration, float executionTime); 78 82 79 83 static ScriptController* getScriptController(); 80 84 81 85 int getID() { return ctrlid_; } 82 83 86 84 87 // tolua_end … … 88 91 89 92 private: 90 // name of the LUA-sourcefile that shall be executed->see XMLPort-function 91 std::string luasrc; 93 /* Information about the player that this ScriptController will 94 * control */ 95 /* - Player pointer */ 92 96 PlayerInfo* player_; 97 98 /* - Entity pointer, this is for convenience and will be the same as 99 * player_->getControllableEntity() 100 */ 93 101 ControllableEntity* entity_; 102 103 /* Controller ID, defaults to 0 and is set using takeControl() */ 94 104 int ctrlid_; 95 105 106 /* List of events to walk through */ 107 std::vector<event> eventList; 108 unsigned int eventno; 109 110 /* Time since the creation of this ScriptController object */ 111 float scTime; 112 113 /* Boolean that specifies whether we're processing an event right 114 * now */ 115 bool processing; 116 117 /* Data about the point to go to and what to look at */ 118 /* - Target position */ 119 Vector3 target; 120 121 /* - Time it should take to get there */ 122 float timeToTarget; 123 124 /* - Time this event has been going on for */ 125 float eventTime; 126 Vector3 startpos; 127 128 /* - Position to look at during that transition */ 129 Vector3 lookAtPosition; 96 130 97 131 };// tolua_export
Note: See TracChangeset
for help on using the changeset viewer.