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