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