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