[3311] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
[3433] | 12 | main-programmer: Benjamin Grauer |
---|
[3311] | 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
[3591] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_TRACK_MANAGER |
---|
[3311] | 17 | |
---|
| 18 | #include "track_manager.h" |
---|
[3495] | 19 | |
---|
[3608] | 20 | #include "base_object.h" |
---|
[3433] | 21 | #include "p_node.h" |
---|
[3528] | 22 | #include "track_node.h" |
---|
[3608] | 23 | #include "stdincl.h" |
---|
| 24 | #include "list.h" |
---|
[3845] | 25 | #include "text_engine.h" |
---|
[3849] | 26 | #include "t_animation.h" |
---|
[4220] | 27 | #include "substring.h" |
---|
[4261] | 28 | #include "tinyxml.h" |
---|
[3528] | 29 | |
---|
[3495] | 30 | #include <stdarg.h> |
---|
| 31 | |
---|
[3311] | 32 | using namespace std; |
---|
| 33 | |
---|
[3331] | 34 | /** |
---|
| 35 | \brief initializes a TrackElement (sets the default values) |
---|
| 36 | */ |
---|
| 37 | TrackElement::TrackElement(void) |
---|
| 38 | { |
---|
[3332] | 39 | this->isFresh = true; |
---|
[3354] | 40 | this->isHotPoint = false; |
---|
[3331] | 41 | this->isSavePoint = false; |
---|
| 42 | this->isFork = false; |
---|
| 43 | this->isJoined = false; |
---|
[3356] | 44 | this->mainJoin = false; |
---|
[3331] | 45 | this->ID = -1; |
---|
[3588] | 46 | this->startingTime = 0; |
---|
| 47 | this->duration = TMAN_DEFAULT_DURATION; |
---|
[3433] | 48 | this->endTime = 1; |
---|
| 49 | this->jumpTime = 0; |
---|
[3596] | 50 | this->width = TMAN_DEFAULT_WIDTH; |
---|
[3331] | 51 | this->nodeCount = 0; |
---|
[3835] | 52 | this->curve = NULL; |
---|
[3348] | 53 | this->childCount = 0; |
---|
[3835] | 54 | this->children = NULL; |
---|
[3332] | 55 | this->name = NULL; |
---|
[3527] | 56 | |
---|
| 57 | this->history = NULL; |
---|
| 58 | |
---|
[3835] | 59 | this->subject = NULL; |
---|
[3522] | 60 | this->condFunc = &TrackElement::random; |
---|
[3331] | 61 | } |
---|
[3311] | 62 | |
---|
[3331] | 63 | /** |
---|
[3332] | 64 | \brief destroys all alocated memory) |
---|
[3331] | 65 | \todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements |
---|
| 66 | */ |
---|
| 67 | TrackElement::~TrackElement(void) |
---|
| 68 | { |
---|
[3835] | 69 | // deleting the Name |
---|
| 70 | delete []name; |
---|
| 71 | // deleting the Curve |
---|
| 72 | delete this->curve; |
---|
| 73 | |
---|
| 74 | // deleting all the Children of this TrackNode. |
---|
| 75 | if ((!this->isJoined &&this->childCount > 0) |
---|
| 76 | || (this->isJoined && this->mainJoin)) // only if this is the MainJoin. |
---|
[3331] | 77 | { |
---|
[3661] | 78 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 79 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 80 | while (enumElem) |
---|
| 81 | { |
---|
| 82 | delete enumElem; |
---|
[3661] | 83 | enumElem = iterator->nextElement(); |
---|
[3594] | 84 | } |
---|
[3661] | 85 | delete iterator; |
---|
[3356] | 86 | delete this->children; |
---|
[3331] | 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
[3332] | 90 | /** |
---|
| 91 | \brief Searches through all the TrackElements for trackID. |
---|
| 92 | \param trackID The ID to search for. |
---|
| 93 | \returns The TrackElement if Found, NULL otherwise. |
---|
| 94 | */ |
---|
| 95 | TrackElement* TrackElement::findByID(unsigned int trackID) |
---|
| 96 | { |
---|
| 97 | // return if Found. |
---|
| 98 | if (this->ID == trackID) |
---|
| 99 | return this; |
---|
[3835] | 100 | // search all children |
---|
[3332] | 101 | if (this->childCount > 0) |
---|
[3594] | 102 | { |
---|
[3661] | 103 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 104 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 105 | TrackElement* tmpElem; |
---|
| 106 | while (enumElem) |
---|
| 107 | { |
---|
| 108 | if ((tmpElem = enumElem->findByID(trackID))) |
---|
| 109 | return tmpElem; |
---|
[3661] | 110 | enumElem = iterator->nextElement(); |
---|
[3594] | 111 | } |
---|
[3661] | 112 | delete iterator; |
---|
[3594] | 113 | } |
---|
[3835] | 114 | // if not found |
---|
[3882] | 115 | return NULL; |
---|
[3332] | 116 | } |
---|
[3331] | 117 | |
---|
| 118 | |
---|
[3522] | 119 | /** |
---|
[3835] | 120 | \brief Searches through all the TrackElements for a trackName |
---|
| 121 | \param trackName The name to search for. |
---|
| 122 | \returns The TrackElement if Found, NULL otherwise. |
---|
[3522] | 123 | */ |
---|
[3835] | 124 | TrackElement* TrackElement::findByName(const char* trackName) |
---|
[3522] | 125 | { |
---|
[3835] | 126 | // return if Found. |
---|
| 127 | if (this->name && !strcmp(this->name, trackName)) |
---|
| 128 | return this; |
---|
| 129 | // search all children |
---|
| 130 | if (this->childCount > 0) |
---|
[3522] | 131 | { |
---|
[3661] | 132 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 133 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3835] | 134 | TrackElement* tmpElem; |
---|
[3594] | 135 | while (enumElem) |
---|
| 136 | { |
---|
[3835] | 137 | if ((tmpElem = enumElem->findByName(trackName))) |
---|
| 138 | return tmpElem; |
---|
[3661] | 139 | enumElem = iterator->nextElement(); |
---|
[3594] | 140 | } |
---|
[3661] | 141 | delete iterator; |
---|
[3522] | 142 | } |
---|
[3835] | 143 | // if not found |
---|
[3882] | 144 | return NULL; |
---|
[3522] | 145 | } |
---|
[3331] | 146 | |
---|
[3522] | 147 | /** |
---|
[3835] | 148 | \brief checks if there are any BackLoops in the Track (Backloops only |
---|
| 149 | \param trackElem the trackElement to check about |
---|
[4489] | 150 | \param depth the depth to search in |
---|
[3835] | 151 | \returns true if NO loop was found, false Otherwise |
---|
| 152 | You actually have to act on false!! |
---|
| 153 | it simply does this by looking if the current trackElem is found again somewhere else in the Track |
---|
[4489] | 154 | |
---|
| 155 | \todo this has to be reimplemented |
---|
[3835] | 156 | */ |
---|
| 157 | bool TrackElement::backLoopCheck(const TrackElement* trackElem, unsigned int depth) const |
---|
| 158 | { |
---|
| 159 | if(depth == 0 || this != trackElem) |
---|
| 160 | { |
---|
| 161 | if (this->children) |
---|
| 162 | { |
---|
| 163 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 164 | TrackElement* enumElem = iterator->nextElement(); |
---|
| 165 | while (enumElem) |
---|
| 166 | { |
---|
| 167 | if(!enumElem->backLoopCheck(trackElem, depth + 1)) |
---|
| 168 | return false; |
---|
| 169 | enumElem = iterator->nextElement(); |
---|
| 170 | } |
---|
| 171 | delete iterator; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | else |
---|
| 175 | return false; |
---|
| 176 | |
---|
| 177 | // only returns if everything worked out |
---|
| 178 | return true; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | /** |
---|
[4489] | 182 | \param childCount which child to return |
---|
[3835] | 183 | \returns the n-the children (starting at 0). |
---|
| 184 | Be aware, that when the trackElement has no Children, NULL will be returned |
---|
[3594] | 185 | */ |
---|
[3835] | 186 | TrackElement* TrackElement::getChild(int childCount) const |
---|
[3594] | 187 | { |
---|
[3835] | 188 | // if the the trackElement has no children return NULL. |
---|
[3594] | 189 | if (this->childCount == 0) |
---|
| 190 | return NULL; |
---|
[3835] | 191 | // we cannot return the childCount+m's Child, so we return the last. |
---|
[3594] | 192 | if (childCount > this->childCount) |
---|
| 193 | childCount = this->childCount; |
---|
| 194 | |
---|
[3832] | 195 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 196 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 197 | for (int i = 0; i < childCount; i++) |
---|
[3832] | 198 | enumElem = iterator->nextElement(); |
---|
| 199 | delete iterator; |
---|
[3594] | 200 | return enumElem; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /** |
---|
[3588] | 204 | \param name the Name to set. |
---|
| 205 | */ |
---|
| 206 | void TrackElement::setName(const char* name) |
---|
| 207 | { |
---|
| 208 | // delete the old name |
---|
| 209 | if (this->name) |
---|
| 210 | delete []this->name; |
---|
[3835] | 211 | // if a name was given. |
---|
[3588] | 212 | if (name) |
---|
| 213 | { |
---|
| 214 | this->name = new char[strlen(name)+1]; |
---|
| 215 | strcpy(this->name, name); |
---|
| 216 | } |
---|
| 217 | else |
---|
| 218 | this->name = NULL; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | /** |
---|
[3593] | 222 | \brief prints out debug information about this TrackElement |
---|
| 223 | */ |
---|
[3835] | 224 | void TrackElement::debug(void) const |
---|
[3593] | 225 | { |
---|
| 226 | PRINT(0)("--== TrackElement:%i ==--", this->ID); |
---|
| 227 | if(this->getName()) |
---|
[3842] | 228 | PRINT(0)("--++Name: %s++--", this->getName()); |
---|
[3593] | 229 | if(this->isFresh) |
---|
| 230 | PRINT(0)(" -- has not jet eddited in any way --\n"); |
---|
| 231 | PRINT(0)("\n TimeTable: startingTime=%f; endTime=%f; duration=%f; jumpTime=%f\n", this->startingTime, this->endTime, this->duration, this->jumpTime); |
---|
| 232 | PRINT(0)(" consists of %d Points\n", this->nodeCount); |
---|
| 233 | if (this->childCount == 0) |
---|
| 234 | PRINT(0)(" has no child\n"); |
---|
| 235 | else if (this->childCount == 1) |
---|
[3594] | 236 | PRINT(0)(" has 1 child: =%d=\n", this->getChild(0)->ID); |
---|
[3593] | 237 | else if (this->childCount > 1) |
---|
| 238 | { |
---|
| 239 | PRINT(0)(" has %d children: ", this->childCount); |
---|
[3832] | 240 | //TrackElement* enumElem = this->children->enumerate(); |
---|
| 241 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 242 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 243 | while (enumElem) |
---|
| 244 | { |
---|
| 245 | PRINT(0)("=%d= ", enumElem->ID); |
---|
[3832] | 246 | enumElem = iterator->nextElement(); |
---|
[3594] | 247 | } |
---|
[3832] | 248 | delete iterator; |
---|
[3593] | 249 | PRINT(0)("\n"); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | if(this->isHotPoint) |
---|
| 253 | PRINT(0)(" is a special Point:\n"); |
---|
| 254 | if(this->isSavePoint) |
---|
| 255 | PRINT(0)(" is a SavePoint\n"); |
---|
| 256 | if(this->isFork) |
---|
| 257 | { |
---|
| 258 | PRINT(0)(" is A Fork with with %d children.\n", this->childCount); |
---|
| 259 | } |
---|
| 260 | if(this->isJoined) |
---|
| 261 | PRINT(0)(" is Joined at the End\n"); |
---|
| 262 | |
---|
| 263 | if(!this->backLoopCheck(this)) /* this should not happen */ |
---|
| 264 | PRINT(2)(" THERE IS A BACKLOOP TO THIS ELEMENT\n"); |
---|
| 265 | } |
---|
[3588] | 266 | |
---|
| 267 | /** |
---|
[3522] | 268 | \brief CONDITION that chooses the first child for the decision (static) |
---|
| 269 | \param nothing Nothing in this function |
---|
| 270 | \returns the chosen child |
---|
| 271 | */ |
---|
[3835] | 272 | int TrackElement::lowest(const void* nothing) const |
---|
[3522] | 273 | { |
---|
| 274 | return 0; |
---|
| 275 | } |
---|
[3332] | 276 | |
---|
[3522] | 277 | /** |
---|
| 278 | \brief CONDITION that chooses the last child for the decision (static) |
---|
| 279 | \param nothing Nothing in this function |
---|
| 280 | \returns the chosen child |
---|
| 281 | */ |
---|
[3835] | 282 | int TrackElement::highest(const void* nothing) const |
---|
[3522] | 283 | { |
---|
| 284 | return this->childCount-1; |
---|
| 285 | } |
---|
[3332] | 286 | |
---|
[3522] | 287 | /** |
---|
| 288 | \brief CONDITION that chooses a random child for the decision (static) |
---|
| 289 | \param nothing Nothing in this function |
---|
| 290 | \returns the chosen child |
---|
| 291 | */ |
---|
[3835] | 292 | int TrackElement::random(const void* nothing) const |
---|
[3522] | 293 | { |
---|
| 294 | int i = (int)floor ((float)rand()/(float)RAND_MAX * (float)this->childCount); |
---|
| 295 | if (i >= this->childCount) |
---|
| 296 | return this->childCount-1; |
---|
| 297 | else |
---|
| 298 | return i; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | /** |
---|
| 302 | \brief CONDITION that chooses child 0, if the node(probably Player) |
---|
| 303 | is left of its parent (z<0)) and 1/right otherwise. |
---|
| 304 | \param node The node to act upon. |
---|
| 305 | \returns the chosen child |
---|
| 306 | */ |
---|
[3835] | 307 | int TrackElement::leftRight(const void* node) const |
---|
[3522] | 308 | { |
---|
| 309 | PNode* tmpNode = (PNode*)node; |
---|
| 310 | |
---|
[3966] | 311 | if (tmpNode->getRelCoor().z < 0) |
---|
[3522] | 312 | return 0; |
---|
| 313 | else |
---|
| 314 | return 1; |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | /** |
---|
| 319 | \brief CONDITION that chooses the child, that has the nearest distance to the node (probably player). |
---|
| 320 | \param node The node to act upon. |
---|
| 321 | \returns the chosen child |
---|
| 322 | |
---|
| 323 | This is rather dangerous, because one must carefully set the points on the curve. |
---|
| 324 | The best Way is to set the nodes as wide away of each other as possible, |
---|
| 325 | but take into consideration, that if the nodes are to far from a center node, the center will be chosen. |
---|
| 326 | (play with this!!). |
---|
| 327 | */ |
---|
[3835] | 328 | int TrackElement::nearest(const void* node) const |
---|
[3522] | 329 | { |
---|
| 330 | PNode* tmpNode = (PNode*)node; |
---|
| 331 | |
---|
[3966] | 332 | Vector nodeRelCoord = tmpNode->getRelCoor(); |
---|
[3522] | 333 | float minDist = 100000000; |
---|
[3594] | 334 | int childNumber = 0; |
---|
| 335 | int i = 0; |
---|
| 336 | |
---|
[3832] | 337 | //TrackElement* enumElem = this->children->enumerate(); |
---|
| 338 | tIterator<TrackElement>* iterator = this->children->getIterator(); |
---|
| 339 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 340 | while (enumElem) |
---|
[3522] | 341 | { |
---|
[3594] | 342 | float dist = (nodeRelCoord - enumElem->curve->getNode(4)).len(); |
---|
[3522] | 343 | if (dist < minDist) |
---|
| 344 | { |
---|
| 345 | minDist = dist; |
---|
[3594] | 346 | childNumber = i; |
---|
[3522] | 347 | } |
---|
[3594] | 348 | i++; |
---|
[3832] | 349 | enumElem = iterator->nextElement(); |
---|
[3522] | 350 | } |
---|
[3832] | 351 | delete iterator; |
---|
[3594] | 352 | |
---|
| 353 | PRINTF(4)("PathDecision with nearest algorithm: %d\n", childNumber); |
---|
| 354 | return childNumber; |
---|
[3522] | 355 | } |
---|
| 356 | |
---|
| 357 | |
---|
[3599] | 358 | //////////////////////// |
---|
| 359 | ///// TRACKMANAGER ///// |
---|
| 360 | //////////////////////// |
---|
[3311] | 361 | /** |
---|
| 362 | \brief standard constructor |
---|
| 363 | |
---|
| 364 | */ |
---|
[3354] | 365 | TrackManager::TrackManager(void) |
---|
[3311] | 366 | { |
---|
[4320] | 367 | this->setClassID(CL_TRACK_MANAGER, "TrackManager"); |
---|
[3528] | 368 | |
---|
[3836] | 369 | TrackManager::singletonRef = this; // do this because otherwise the TrackNode cannot get The instance of the TrackManager |
---|
[3331] | 370 | |
---|
| 371 | PRINTF(3)("Initializing the TrackManager\n"); |
---|
[3836] | 372 | // setting up the First TrackElement |
---|
[3348] | 373 | this->firstTrackElem = new TrackElement(); |
---|
| 374 | this->firstTrackElem->ID = 1; |
---|
[3842] | 375 | this->firstTrackElem->setName("root"); |
---|
| 376 | |
---|
[3331] | 377 | this->currentTrackElem = firstTrackElem; |
---|
[3836] | 378 | |
---|
| 379 | this->curveType = CURVE_BEZIER; |
---|
[3331] | 380 | this->localTime = 0; |
---|
| 381 | this->maxTime = 0; |
---|
[3348] | 382 | this->trackElemCount = 1; |
---|
[3845] | 383 | |
---|
[3836] | 384 | this->trackNode = new TrackNode(); |
---|
| 385 | this->setBindSlave(this->trackNode); |
---|
[3845] | 386 | // initializing the Text |
---|
| 387 | this->trackText = TextEngine::getInstance()->createText("fonts/earth.ttf", 30, TEXT_DYNAMIC, 0, 255, 0); |
---|
| 388 | this->trackText->setAlignment(TEXT_ALIGN_SCREEN_CENTER); |
---|
| 389 | // initializing the Animation for the Text. |
---|
[3847] | 390 | this->textAnimation = new tAnimation<Text>(this->trackText, &Text::setBlending); |
---|
[3872] | 391 | this->textAnimation->addKeyFrame(1.0, 3.0, ANIM_NEG_EXP); |
---|
[3846] | 392 | this->textAnimation->addKeyFrame(0.0, .001); |
---|
[3845] | 393 | this->textAnimation->setInfinity(ANIM_INF_CONSTANT); |
---|
[3311] | 394 | } |
---|
| 395 | |
---|
[4017] | 396 | |
---|
[3311] | 397 | /** |
---|
[4017] | 398 | \brief loads a trackElement from a TiXmlElement |
---|
| 399 | \param root the TiXmlElement to load the Data from |
---|
| 400 | */ |
---|
[4489] | 401 | bool TrackManager::loadParams( TiXmlElement* root) |
---|
[4017] | 402 | { |
---|
[4220] | 403 | TiXmlElement* element; |
---|
| 404 | TiXmlNode* container; |
---|
| 405 | double x, y, z, d; |
---|
| 406 | |
---|
| 407 | element = root->FirstChildElement(); |
---|
| 408 | |
---|
| 409 | while( element != NULL) |
---|
| 410 | { |
---|
| 411 | if( !strcmp( element->Value(), "Point")) |
---|
| 412 | { |
---|
| 413 | container = element->FirstChild(); |
---|
| 414 | if( container->ToText()) |
---|
| 415 | { |
---|
| 416 | assert( container->Value() != NULL); |
---|
| 417 | if( sscanf( container->Value(), "%lf,%lf,%lf", &x, &y, &z) == 3) |
---|
| 418 | { |
---|
[4261] | 419 | PRINTF(5)("Loaded Point: %lf,%lf,%lf (%s)\n", x, y, z, container->Value()); |
---|
[4220] | 420 | addPoint( Vector( x, y, z)); |
---|
| 421 | } |
---|
| 422 | else |
---|
| 423 | { |
---|
| 424 | PRINTF(0)("Invalid Point in Track (skipped)\n"); |
---|
| 425 | } |
---|
| 426 | } |
---|
| 427 | } |
---|
| 428 | else if( !strcmp( element->Value(), "Duration")) |
---|
| 429 | { |
---|
| 430 | container = element->FirstChild(); |
---|
| 431 | if( container->ToText()) |
---|
| 432 | { |
---|
| 433 | assert( container->Value() != NULL); |
---|
| 434 | if( sscanf( container->Value(), "%lf", &d) == 1) |
---|
| 435 | { |
---|
| 436 | PRINTF(5)("Loaded Duration: %lf (%s)\n", d, container->Value()); |
---|
| 437 | setDuration( d); |
---|
| 438 | } |
---|
| 439 | else |
---|
| 440 | { |
---|
| 441 | PRINTF(2)("Invalid Duration in Track (skipped)\n"); |
---|
| 442 | } |
---|
| 443 | } |
---|
| 444 | } |
---|
| 445 | else if( !strcmp( element->Value(), "SavePoint")) |
---|
| 446 | { |
---|
| 447 | PRINTF(5)("Loaded Savepoint\n"); |
---|
| 448 | setSavePoint(); |
---|
| 449 | } |
---|
| 450 | else if( !strcmp( element->Value(), "Fork")) |
---|
| 451 | { |
---|
| 452 | container = element->FirstChild(); |
---|
| 453 | if( container->ToText()) |
---|
| 454 | { |
---|
| 455 | assert( container->Value() != NULL); |
---|
| 456 | PRINTF(4)("Loaded Fork: %s\n", container->Value()); |
---|
| 457 | forkS(container->Value()); |
---|
| 458 | } |
---|
| 459 | } |
---|
| 460 | else if( !strcmp( element->Value(), "Join")) |
---|
| 461 | { |
---|
| 462 | container = element->FirstChild(); |
---|
| 463 | if( container->ToText()) |
---|
| 464 | { |
---|
| 465 | assert( container->Value() != NULL); |
---|
| 466 | PRINTF0("Loaded Join: %s\n", container->Value()); |
---|
| 467 | joinS(container->Value()); |
---|
| 468 | } |
---|
| 469 | } |
---|
| 470 | else if( !strcmp( element->Value(), "WorkOn")) |
---|
| 471 | { |
---|
| 472 | container = element->FirstChild(); |
---|
| 473 | if( container->ToText()) |
---|
| 474 | { |
---|
| 475 | assert( container->Value() != NULL); |
---|
| 476 | PRINTF(4)("Loaded WorkOn: %s\n", container->Value()); |
---|
| 477 | workOn( container->Value()); |
---|
| 478 | } |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | element = element->NextSiblingElement(); |
---|
| 482 | } |
---|
[4017] | 483 | |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | /** |
---|
[3331] | 487 | \brief standard destructor |
---|
[3311] | 488 | */ |
---|
[3354] | 489 | TrackManager::~TrackManager(void) |
---|
[3330] | 490 | { |
---|
[3331] | 491 | PRINTF(3)("Destruct TrackManager\n"); |
---|
[3311] | 492 | |
---|
[3594] | 493 | PRINTF(4)("Deleting all the TrackElements\n"); |
---|
[3331] | 494 | delete this->firstTrackElem; |
---|
[3335] | 495 | |
---|
[3836] | 496 | // the tracknode should be deleted here, but is deleted by pNode: -> null_parent |
---|
| 497 | |
---|
[3331] | 498 | // we do not have a TrackManager anymore |
---|
[3594] | 499 | TrackManager::singletonRef = NULL; |
---|
[3330] | 500 | } |
---|
| 501 | |
---|
[3543] | 502 | //! Singleton Reference to TrackManager |
---|
[3331] | 503 | TrackManager* TrackManager::singletonRef = NULL; |
---|
| 504 | |
---|
[3836] | 505 | // INITIALIZE // |
---|
[3331] | 506 | /** |
---|
[3335] | 507 | \brief reserves Space for childCount children |
---|
| 508 | \param childCount The Count of children to make space for. |
---|
[3836] | 509 | \param trackElem The TrackElement to appy this to. (if NULL chose this->currentTrackElement) |
---|
[3335] | 510 | */ |
---|
[3836] | 511 | void TrackManager::initChildren(unsigned int childCount, TrackElement* trackElem) |
---|
[3335] | 512 | { |
---|
[3836] | 513 | if (!trackElem) |
---|
| 514 | trackElem = this->currentTrackElem; |
---|
| 515 | |
---|
| 516 | trackElem->childCount = childCount; |
---|
| 517 | trackElem->mainJoin = true; // this tells join, that this one is the Main Join, if it tries to join multiple Tracks |
---|
| 518 | trackElem->children = new tList<TrackElement>(); |
---|
[3594] | 519 | for (int i = 0; i < childCount; i++) |
---|
[3348] | 520 | { |
---|
[3836] | 521 | // create a new Element |
---|
[3594] | 522 | TrackElement* newElem = new TrackElement(); |
---|
[3836] | 523 | // setting up the new ID |
---|
[3594] | 524 | newElem->ID = ++trackElemCount; |
---|
[3836] | 525 | // setting up the Time |
---|
| 526 | newElem->startingTime = trackElem->endTime + trackElem->jumpTime; |
---|
| 527 | // adds the conection Point |
---|
| 528 | this->addPoint(trackElem->curve->getNode(trackElem->curve->getNodeCount()), |
---|
| 529 | newElem); |
---|
| 530 | // add the new child to the childList. |
---|
| 531 | trackElem->children->add(newElem); |
---|
[3348] | 532 | } |
---|
[3836] | 533 | |
---|
[3842] | 534 | // setting the Name of the new TrackElement to the name of the last one + _childI |
---|
[3837] | 535 | |
---|
[3842] | 536 | if (trackElem->getName()) |
---|
| 537 | { |
---|
| 538 | for (int i = 0; i < trackElem->childCount; i++) |
---|
| 539 | { |
---|
| 540 | char* childName = new char[strlen(trackElem->getName())+10]; |
---|
| 541 | sprintf(childName, "%s_child%d", trackElem->getName(), i); |
---|
| 542 | trackElem->getChild(i)->setName(childName); |
---|
| 543 | } |
---|
| 544 | } |
---|
[3837] | 545 | // select the first Child to work on. |
---|
| 546 | this->currentTrackElem = trackElem->getChild(0); |
---|
[3335] | 547 | } |
---|
| 548 | |
---|
[3836] | 549 | |
---|
[3335] | 550 | /** |
---|
[3836] | 551 | \brief Sets the trackID we are working on. |
---|
| 552 | \param trackID the trackID we are working on |
---|
[3330] | 553 | */ |
---|
[3836] | 554 | void TrackManager::workOn(unsigned int trackID) |
---|
[3330] | 555 | { |
---|
[3836] | 556 | TrackElement* tmpElem = this->firstTrackElem->findByID(trackID); |
---|
| 557 | if (tmpElem) |
---|
| 558 | this->currentTrackElem = tmpElem; |
---|
| 559 | else |
---|
| 560 | PRINTF(2)("TrackElement %d not Found, leaving unchanged\n", trackID); |
---|
| 561 | PRINTF(4)("now Working on %d\n", this->currentTrackElem->ID); |
---|
[3330] | 562 | } |
---|
| 563 | |
---|
| 564 | /** |
---|
[3836] | 565 | \brief Sets the TrackElement to work on |
---|
| 566 | \param trackName the Name of the Track to work on |
---|
[3330] | 567 | */ |
---|
[3836] | 568 | void TrackManager::workOn(const char* trackName) |
---|
[3330] | 569 | { |
---|
[3836] | 570 | TrackElement* tmpElem = this->firstTrackElem->findByName(trackName); |
---|
[3355] | 571 | if (tmpElem) |
---|
| 572 | this->currentTrackElem = tmpElem; |
---|
| 573 | else |
---|
[3836] | 574 | PRINTF(2)("TrackElement %s not Found, leaving unchanged\n", trackName); |
---|
[3594] | 575 | PRINTF(4)("now Working on %d\n", this->currentTrackElem->ID); |
---|
[3330] | 576 | } |
---|
| 577 | |
---|
| 578 | /** |
---|
| 579 | \brief Sets the Type of the Curve |
---|
[3522] | 580 | \param curveType The Type to set |
---|
| 581 | \param trackElem the TrackElement that should get a new Curve. |
---|
[3836] | 582 | |
---|
| 583 | \brief this will possibly get obsolete during the process. |
---|
[3330] | 584 | */ |
---|
[3433] | 585 | void TrackManager::setCurveType(CurveType curveType, TrackElement* trackElem) |
---|
[3330] | 586 | { |
---|
[3433] | 587 | if (!trackElem->isFresh) |
---|
[3332] | 588 | { |
---|
| 589 | PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n"); |
---|
| 590 | return; |
---|
| 591 | } |
---|
[3588] | 592 | this->curveType = curveType; |
---|
[3332] | 593 | switch (curveType) |
---|
| 594 | { |
---|
[3836] | 595 | case CURVE_BEZIER: |
---|
[3433] | 596 | trackElem->curve = new BezierCurve(); |
---|
[3332] | 597 | break; |
---|
| 598 | } |
---|
[3330] | 599 | } |
---|
| 600 | |
---|
| 601 | /** |
---|
[3333] | 602 | \brief Sets the duration of the current path in seconds. |
---|
[3836] | 603 | \param duration The duration in seconds. |
---|
| 604 | \param trackElem The TrackElement to apply this to. |
---|
[3330] | 605 | */ |
---|
[3836] | 606 | void TrackManager::setDuration(float duration, TrackElement* trackElem) |
---|
[3330] | 607 | { |
---|
[3836] | 608 | if (!trackElem) |
---|
| 609 | trackElem = this->currentTrackElem; |
---|
[3330] | 610 | |
---|
[3836] | 611 | trackElem->duration = duration; |
---|
| 612 | trackElem->endTime = trackElem->startingTime + duration; |
---|
[3352] | 613 | } |
---|
| 614 | |
---|
| 615 | /** |
---|
| 616 | \brief adds a point to trackElem |
---|
| 617 | \param newPoint The point to add. |
---|
| 618 | \param trackElem The TrackElement to add the Point to |
---|
| 619 | */ |
---|
| 620 | bool TrackManager::addPoint(Vector newPoint, TrackElement* trackElem) |
---|
| 621 | { |
---|
[3836] | 622 | if (!trackElem) |
---|
| 623 | trackElem = this->currentTrackElem; |
---|
| 624 | |
---|
[3352] | 625 | if (trackElem->isFresh) |
---|
[3332] | 626 | { |
---|
[3588] | 627 | this->setCurveType(TMAN_DEFAULT_CURVETYPE, trackElem); |
---|
[3352] | 628 | trackElem->isFresh = false; |
---|
[3332] | 629 | } |
---|
[3352] | 630 | trackElem->curve->addNode(newPoint); |
---|
| 631 | trackElem->nodeCount++; |
---|
[3330] | 632 | } |
---|
| 633 | |
---|
| 634 | /** |
---|
| 635 | \brief adds save/splitpoint. |
---|
| 636 | \param newPoint The point to add. |
---|
[4489] | 637 | \param trackElem if supplied it will add a hotpoint on this TrackElement |
---|
[3333] | 638 | \returns A Pointer to a newly appended Curve |
---|
[3330] | 639 | */ |
---|
[3836] | 640 | int TrackManager::addHotPoint(Vector newPoint, TrackElement* trackElem) |
---|
[3330] | 641 | { |
---|
[3836] | 642 | if (!trackElem) |
---|
| 643 | trackElem = this->currentTrackElem; |
---|
| 644 | |
---|
[3594] | 645 | PRINTF(4)("setting up a HotPoint\n"); |
---|
[3836] | 646 | if (trackElem->isFresh) |
---|
[3332] | 647 | { |
---|
[3836] | 648 | trackElem->isFresh = false; |
---|
[3332] | 649 | } |
---|
[3330] | 650 | |
---|
[3332] | 651 | // \todo HotPoint Handling. |
---|
[3836] | 652 | trackElem->curve->addNode(newPoint); |
---|
| 653 | trackElem->nodeCount++; |
---|
[3837] | 654 | this->initChildren(1, trackElem); |
---|
[3330] | 655 | } |
---|
| 656 | |
---|
| 657 | /** |
---|
| 658 | \brief Sets the last HotPoint into a savePoint. |
---|
[3837] | 659 | \param trackElem The TrackElement to appy this to. (if NULL chose this->currentTrackElement) |
---|
[3333] | 660 | \returns A Pointer to a newly appended Curve |
---|
[3837] | 661 | |
---|
[3330] | 662 | If no HotPoint was defined the last added Point will be rendered into a savePoint. \n |
---|
| 663 | If the HotPoint was defined as a fork the Point will \b not be set into a savePoint. |
---|
| 664 | */ |
---|
[3837] | 665 | int TrackManager::setSavePoint(TrackElement* trackElem) |
---|
[3330] | 666 | { |
---|
[3837] | 667 | if (!trackElem) |
---|
| 668 | trackElem = this->currentTrackElem; |
---|
| 669 | |
---|
[3594] | 670 | PRINTF(4)("setting up a SavePoint.\n"); |
---|
[3837] | 671 | if (trackElem->isFork || trackElem->isSavePoint) |
---|
[3594] | 672 | { |
---|
[3837] | 673 | PRINTF(2)("%d is already finished \n", trackElem->ID); |
---|
| 674 | return trackElem->getChild(0)->ID; |
---|
[3594] | 675 | } |
---|
[3837] | 676 | trackElem->isSavePoint = true; |
---|
| 677 | trackElem->isHotPoint = true; |
---|
[3332] | 678 | |
---|
[3837] | 679 | this->initChildren(1, trackElem); |
---|
[3330] | 680 | } |
---|
| 681 | |
---|
| 682 | /** |
---|
| 683 | \brief adds some interessting non-linear movments through the level. |
---|
[4220] | 684 | \param count The Count of children the fork will produce |
---|
[3330] | 685 | |
---|
| 686 | If no HotPoint was defined the last added Point will be rendered into a fork. \n |
---|
| 687 | If the HotPoint was defined as a savePoint the Point will \b not be set into a fork. |
---|
| 688 | */ |
---|
[3332] | 689 | void TrackManager::fork(unsigned int count, ...) |
---|
[3330] | 690 | { |
---|
[3351] | 691 | int* trackIDs = new int[count]; |
---|
[4220] | 692 | this->forkV(count, trackIDs, NULL); |
---|
[3332] | 693 | va_list ID; |
---|
| 694 | va_start (ID, count); |
---|
| 695 | for(int i = 0; i < count; i++) |
---|
| 696 | { |
---|
[3351] | 697 | *va_arg (ID, int*) = trackIDs[i]; |
---|
[3332] | 698 | } |
---|
[3351] | 699 | va_end(ID); |
---|
[3332] | 700 | delete []trackIDs; |
---|
[3330] | 701 | } |
---|
| 702 | |
---|
| 703 | /** |
---|
[4489] | 704 | \param count how many children to produce |
---|
| 705 | \param ... the information on the children (these are the Stings of their names |
---|
[4220] | 706 | \see TrackManager::fork(unsigned int count, ...) |
---|
| 707 | |
---|
| 708 | does the same as fork, but has an array of strings as an input. |
---|
| 709 | */ |
---|
| 710 | void TrackManager::forkS(unsigned int count, ...) |
---|
| 711 | { |
---|
| 712 | int* trackIDs = new int[count]; |
---|
| 713 | this->forkV(count, trackIDs, NULL); |
---|
| 714 | va_list name; |
---|
| 715 | va_start (name, count); |
---|
| 716 | for(int i = 0; i < count; i++) |
---|
| 717 | { |
---|
| 718 | this->firstTrackElem->findByID(trackIDs[i])->setName(va_arg(name, const char*)); |
---|
| 719 | } |
---|
| 720 | va_end(name); |
---|
| 721 | delete []trackIDs; |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | /** |
---|
| 725 | \see TrackManager::fork(unsigned int count, ...) |
---|
| 726 | */ |
---|
| 727 | void TrackManager::forkS(const char* forkString) |
---|
| 728 | { |
---|
| 729 | SubString strings(forkString); |
---|
| 730 | |
---|
| 731 | int* trackIDs = new int[strings.getCount()]; |
---|
| 732 | this->forkV(strings.getCount(), trackIDs, NULL); |
---|
| 733 | |
---|
| 734 | for(int i = 0; i < strings.getCount(); i++) |
---|
| 735 | { |
---|
| 736 | this->firstTrackElem->findByID(trackIDs[i])->setName(strings.getString(i)); |
---|
| 737 | } |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | /** |
---|
[3330] | 741 | \brief adds some interessting non-linear movments through the level. |
---|
| 742 | \param count The Count of childrens the current HotPoint will have. |
---|
| 743 | \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this). |
---|
[4489] | 744 | \param trackNames the names for the tracks as a char-arrey-array |
---|
[4220] | 745 | \param trackElem The TrackElement to appy this to. (if NULL choose this->currentTrackElement) |
---|
[3838] | 746 | \see TrackManager::fork(unsigned int count, ...) |
---|
[3330] | 747 | */ |
---|
[4220] | 748 | void TrackManager::forkV(unsigned int count, int* trackIDs, char** trackNames, TrackElement* trackElem) |
---|
[3330] | 749 | { |
---|
[3837] | 750 | if (!trackElem) |
---|
| 751 | trackElem = this->currentTrackElem; |
---|
| 752 | |
---|
[3594] | 753 | PRINTF(4)("Forking with %d children\n", count); |
---|
[3837] | 754 | if (trackElem->isSavePoint) |
---|
[3332] | 755 | return; |
---|
[3837] | 756 | trackElem->isFork = true; |
---|
| 757 | trackElem->isHotPoint = true; |
---|
[3351] | 758 | for(int i = 0; i < count; i++) |
---|
| 759 | trackIDs[i]=this->trackElemCount+1+i; |
---|
[3837] | 760 | this->initChildren(count, trackElem); |
---|
[3330] | 761 | } |
---|
| 762 | |
---|
| 763 | /** |
---|
| 764 | \brief decides under what condition a certain Path will be chosen. |
---|
[3837] | 765 | \param trackID the trackID to apply this to. |
---|
[3522] | 766 | \param cond the CONDITION of the decision |
---|
| 767 | \param subject the Subject that will be decided upon with CONDITION cond. |
---|
| 768 | */ |
---|
[3837] | 769 | void TrackManager::condition(unsigned int trackID, CONDITION cond, void* subject) |
---|
[3522] | 770 | { |
---|
[3837] | 771 | this->condition(cond, subject, this->firstTrackElem->findByID(trackID)); |
---|
[3522] | 772 | } |
---|
[3837] | 773 | |
---|
[3522] | 774 | /** |
---|
| 775 | \brief decides under what condition a certain Path will be chosen. |
---|
| 776 | \param cond the CONDITION of the decision |
---|
| 777 | \param subject the Subject that will be decided upon with CONDITION cond. |
---|
[3837] | 778 | \param trackElem The TrackElement to appy this to. (if NULL chose this->currentTrackElement) |
---|
[3330] | 779 | */ |
---|
[3837] | 780 | void TrackManager::condition(CONDITION cond, void* subject, TrackElement* trackElem) |
---|
[3330] | 781 | { |
---|
[3837] | 782 | if (!trackElem) |
---|
| 783 | trackElem = this->currentTrackElem; |
---|
| 784 | |
---|
| 785 | if (!trackElem->isFork) |
---|
[3522] | 786 | { |
---|
[3837] | 787 | PRINTF(2)("%d is not a Fork, and no condition can be set in this case\n", trackElem->ID); |
---|
[3594] | 788 | return; |
---|
[3522] | 789 | } |
---|
[3594] | 790 | else |
---|
| 791 | { |
---|
| 792 | switch (cond) |
---|
| 793 | { |
---|
| 794 | case LOWEST: |
---|
[3837] | 795 | trackElem->condFunc = &TrackElement::lowest; |
---|
[3594] | 796 | break; |
---|
| 797 | case HIGHEST: |
---|
[3837] | 798 | trackElem->condFunc = &TrackElement::highest; |
---|
[3594] | 799 | break; |
---|
| 800 | case RANDOM: |
---|
[3837] | 801 | trackElem->condFunc = &TrackElement::random; |
---|
[3594] | 802 | break; |
---|
| 803 | case LEFTRIGHT: |
---|
[3837] | 804 | trackElem->condFunc = &TrackElement::leftRight; |
---|
[3594] | 805 | break; |
---|
| 806 | case NEAREST: |
---|
[3837] | 807 | trackElem->condFunc = &TrackElement::nearest; |
---|
[3594] | 808 | break; |
---|
| 809 | case ENEMYKILLED: |
---|
| 810 | break; |
---|
| 811 | } |
---|
[3837] | 812 | trackElem->subject=subject; |
---|
[3594] | 813 | } |
---|
[3330] | 814 | } |
---|
| 815 | |
---|
| 816 | /** |
---|
| 817 | \brief joins some tracks together again. |
---|
| 818 | \param count The count of Paths to join. |
---|
| 819 | |
---|
| 820 | Join will set the localTime to the longest time a Path has to get to this Point. \n |
---|
[3354] | 821 | Join will join all curves to the first curve, meaning that all the tangents will be matched. |
---|
[3330] | 822 | */ |
---|
[3332] | 823 | void TrackManager::join(unsigned int count, ...) |
---|
[3330] | 824 | { |
---|
[3332] | 825 | int* trackIDs = new int [count]; |
---|
| 826 | va_list ID; |
---|
| 827 | va_start (ID, count); |
---|
| 828 | for(int i = 0; i < count; i++) |
---|
| 829 | { |
---|
| 830 | trackIDs[i] = va_arg (ID, int); |
---|
| 831 | } |
---|
| 832 | va_end(ID); |
---|
| 833 | this->joinV(count, trackIDs); |
---|
| 834 | delete []trackIDs; |
---|
[3330] | 835 | } |
---|
| 836 | |
---|
| 837 | /** |
---|
[3841] | 838 | \brief Joins some Tracks together again. |
---|
| 839 | \param count The count of trackElements to join |
---|
[4220] | 840 | \see void TrackManager::join(unsigned int count, ...) |
---|
[3841] | 841 | |
---|
| 842 | The difference to void TrackManager::join(unsigned int count, ...) is, that this function takes |
---|
| 843 | the Names of the TrackElements as inputs and not their ID |
---|
| 844 | */ |
---|
[4220] | 845 | void TrackManager::joinS(unsigned int count, ...) |
---|
[3841] | 846 | { |
---|
| 847 | int* trackIDs = new int [count]; |
---|
| 848 | va_list NAME; |
---|
| 849 | va_start (NAME, count); |
---|
| 850 | for(int i = 0; i < count; i++) |
---|
| 851 | { |
---|
[4220] | 852 | const char* name = va_arg (NAME, char*); |
---|
[3841] | 853 | TrackElement* tmpElem = this->firstTrackElem->findByName(name); |
---|
| 854 | if (tmpElem) |
---|
| 855 | trackIDs[i] = tmpElem->ID; |
---|
| 856 | else |
---|
| 857 | PRINTF(1)("Trying to join a Track, of which the name does not exist: %s\n", name); |
---|
| 858 | } |
---|
| 859 | va_end(NAME); |
---|
| 860 | this->joinV(count, trackIDs); |
---|
| 861 | delete []trackIDs; |
---|
| 862 | } |
---|
| 863 | |
---|
[4220] | 864 | /** |
---|
| 865 | \see void TrackManager::join(unsigned int count, ...) |
---|
| 866 | */ |
---|
| 867 | void TrackManager::joinS(const char* joinString) |
---|
| 868 | { |
---|
| 869 | SubString strings(joinString); |
---|
[3841] | 870 | |
---|
[4220] | 871 | int* trackIDs = new int[strings.getCount()]; |
---|
| 872 | this->joinV(strings.getCount(), trackIDs); |
---|
| 873 | |
---|
| 874 | for(int i = 0; i < strings.getCount(); i++) |
---|
| 875 | { |
---|
| 876 | TrackElement* tmpElem = this->firstTrackElem->findByName(strings.getString(i)); |
---|
| 877 | if (tmpElem) |
---|
| 878 | trackIDs[i] = tmpElem->ID; |
---|
| 879 | else |
---|
| 880 | PRINTF(1)("Trying to join a Track, of which the name does not exist: %s\n", strings.getString(i)); |
---|
| 881 | } |
---|
| 882 | |
---|
| 883 | this->joinV(strings.getCount(), trackIDs); |
---|
| 884 | delete []trackIDs; |
---|
| 885 | } |
---|
| 886 | |
---|
[3841] | 887 | /** |
---|
[3330] | 888 | \brief joins some tracks together again. |
---|
| 889 | \param count The count of Paths to join. |
---|
| 890 | \param trackIDs an Array with the trackID's to join |
---|
| 891 | |
---|
[3841] | 892 | \see void TrackManager::join(unsigned int count, ...) |
---|
[3330] | 893 | */ |
---|
[3332] | 894 | void TrackManager::joinV(unsigned int count, int* trackIDs) |
---|
[3330] | 895 | { |
---|
[3840] | 896 | TrackElement* tmpTrackElem; |
---|
[3880] | 897 | TrackElement* tmpJoinElem; |
---|
[3840] | 898 | for (int i = 0; i < count; i++) |
---|
| 899 | if (!this->firstTrackElem->findByID(trackIDs[i])) |
---|
| 900 | { |
---|
[4220] | 901 | PRINTF(1)("Trying to Connect Paths that do not exist yet: %d\n Not Joining Anything\n", trackIDs[i]); |
---|
[3840] | 902 | return; |
---|
| 903 | } |
---|
| 904 | |
---|
| 905 | |
---|
[3594] | 906 | PRINTF(3)("Joining %d tracks and merging to Track %d\n", count, trackIDs[0]); |
---|
[3354] | 907 | |
---|
[3522] | 908 | // checking if there is a back-loop-connection and ERROR if it is. |
---|
[3840] | 909 | tmpTrackElem = this->firstTrackElem->findByID(trackIDs[0]); |
---|
[3522] | 910 | if (!tmpTrackElem->backLoopCheck(tmpTrackElem)) |
---|
[3838] | 911 | { |
---|
| 912 | PRINTF(2)("Backloop connection detected at joining trackElements\n -> TRACK WILL NOT BE JOINED\n"); |
---|
| 913 | return; |
---|
| 914 | } |
---|
[3522] | 915 | |
---|
[3838] | 916 | TrackElement* firstJoint = this->firstTrackElem->findByID(trackIDs[0]); |
---|
[3433] | 917 | float tmpLatestTime = firstJoint->endTime; |
---|
[3354] | 918 | |
---|
| 919 | Vector tmpEndPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()); |
---|
[3433] | 920 | Vector tmpTangentPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-1); |
---|
| 921 | Vector tmpc2Point = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-2); |
---|
[3354] | 922 | firstJoint->isJoined = true; |
---|
[3433] | 923 | // firstJoint->mainJoin = true; |
---|
[3354] | 924 | if(!firstJoint->isHotPoint) |
---|
[3838] | 925 | this->setSavePoint(firstJoint); |
---|
[3433] | 926 | // Timing: |
---|
| 927 | for (int i = 0; i < count; i++) |
---|
| 928 | { |
---|
[3880] | 929 | if(tmpJoinElem = this->firstTrackElem->findByID(trackIDs[i])) |
---|
| 930 | { |
---|
| 931 | if (tmpJoinElem->childCount == 0 |
---|
| 932 | && tmpJoinElem->endTime > tmpLatestTime) |
---|
| 933 | tmpLatestTime = tmpJoinElem->endTime; |
---|
| 934 | } |
---|
[3433] | 935 | } |
---|
| 936 | // time the main Join. |
---|
| 937 | firstJoint->jumpTime = tmpLatestTime - firstJoint->endTime; |
---|
| 938 | |
---|
| 939 | // Joining: |
---|
[3354] | 940 | for (int i = 1; i < count; i++) |
---|
[3352] | 941 | { |
---|
[3880] | 942 | if( tmpJoinElem = this->firstTrackElem->findByID(trackIDs[i])) |
---|
[3354] | 943 | { |
---|
[3880] | 944 | if (tmpJoinElem->childCount > 0) |
---|
| 945 | printf("!!This Curve has children, and as such will not be joined!!\n You can try joining other childless TrackElements to this one!"); |
---|
| 946 | else |
---|
| 947 | { |
---|
| 948 | this->addPoint(tmpc2Point, tmpJoinElem); |
---|
| 949 | this->addPoint(tmpTangentPoint, tmpJoinElem); |
---|
| 950 | this->addPoint(tmpEndPoint, tmpJoinElem); |
---|
| 951 | // time all other Joins |
---|
| 952 | tmpJoinElem->jumpTime = tmpLatestTime - tmpJoinElem->endTime; |
---|
| 953 | |
---|
| 954 | //Copying Joint-Info |
---|
| 955 | tmpJoinElem->children = firstJoint->children; |
---|
| 956 | tmpJoinElem->childCount = firstJoint->childCount; |
---|
| 957 | tmpJoinElem->isSavePoint = firstJoint->isSavePoint; |
---|
| 958 | tmpJoinElem->isFork = firstJoint->isFork; |
---|
| 959 | |
---|
| 960 | tmpJoinElem->isJoined = true; |
---|
| 961 | } |
---|
[3354] | 962 | } |
---|
[3352] | 963 | } |
---|
[3838] | 964 | if(firstJoint->children) |
---|
[3594] | 965 | { |
---|
[3832] | 966 | //TrackElement* enumElem = firstJoint->children->enumerate(); |
---|
| 967 | tIterator<TrackElement>* iterator = firstJoint->children->getIterator(); |
---|
| 968 | TrackElement* enumElem = iterator->nextElement(); |
---|
[3594] | 969 | while (enumElem) |
---|
| 970 | { |
---|
[3838] | 971 | PRINTF(5)("Setting startingTime of %d to %f.\n", enumElem->ID, tmpLatestTime); |
---|
[3594] | 972 | enumElem->startingTime = tmpLatestTime; |
---|
| 973 | enumElem->endTime = tmpLatestTime + enumElem->duration; |
---|
| 974 | |
---|
[3832] | 975 | enumElem = iterator->nextElement(); |
---|
[3594] | 976 | } |
---|
[3832] | 977 | delete iterator; |
---|
[3594] | 978 | } |
---|
[3330] | 979 | } |
---|
| 980 | |
---|
[3433] | 981 | /** |
---|
| 982 | \brief finalizes the TrackSystem. after this it will not be editable anymore |
---|
| 983 | |
---|
| 984 | \todo check for any inconsistencies, output errors |
---|
| 985 | */ |
---|
| 986 | void TrackManager::finalize(void) |
---|
| 987 | { |
---|
| 988 | for (int i = 1; i<= trackElemCount ;i++) |
---|
| 989 | { |
---|
[3836] | 990 | TrackElement* tmpElem = this->firstTrackElem->findByID(i); |
---|
[3832] | 991 | if( tmpElem->childCount > 0 && tmpElem->mainJoin) |
---|
[3433] | 992 | { |
---|
[3832] | 993 | tIterator<TrackElement>* iterator = tmpElem->children->getIterator(); |
---|
| 994 | TrackElement* enumElem = iterator->nextElement(); |
---|
| 995 | //TrackElement* enumElem = tmpElem->children->enumerate(); |
---|
[3594] | 996 | while (enumElem) |
---|
[3433] | 997 | { |
---|
| 998 | |
---|
| 999 | // c1-continuity |
---|
[3594] | 1000 | enumElem->curve->addNode(enumElem->curve->getNode(0) + |
---|
| 1001 | ((enumElem->curve->getNode(0) - |
---|
[3433] | 1002 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) |
---|
| 1003 | ),2); |
---|
[3594] | 1004 | enumElem->nodeCount++; |
---|
[3433] | 1005 | // c2-continuity |
---|
[3594] | 1006 | enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())- |
---|
[3433] | 1007 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 + |
---|
| 1008 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3); |
---|
[3594] | 1009 | enumElem->nodeCount++; |
---|
| 1010 | PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n %d-out: count: %d %f, %f, %f\n", |
---|
[3433] | 1011 | tmpElem->ID, tmpElem->nodeCount, |
---|
| 1012 | tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z, |
---|
[3594] | 1013 | enumElem->ID, enumElem->nodeCount, |
---|
| 1014 | enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z); |
---|
| 1015 | |
---|
[3832] | 1016 | enumElem = iterator->nextElement(); |
---|
[3433] | 1017 | } |
---|
[3832] | 1018 | delete iterator; |
---|
[3433] | 1019 | } |
---|
| 1020 | } |
---|
[3838] | 1021 | for (int i = 1; i <= trackElemCount;i++) |
---|
[3836] | 1022 | if (this->firstTrackElem->findByID(i)->endTime > this->maxTime) |
---|
| 1023 | this->maxTime = this->firstTrackElem->findByID(i)->endTime; // very bad implemented :/ |
---|
[3433] | 1024 | } |
---|
| 1025 | |
---|
| 1026 | |
---|
[3330] | 1027 | // RUNTIME // |
---|
| 1028 | |
---|
| 1029 | /** |
---|
| 1030 | \brief calculates the Position for the localTime of the Track. |
---|
| 1031 | \returns the calculated Position |
---|
| 1032 | */ |
---|
[3332] | 1033 | Vector TrackManager::calcPos() const |
---|
[3330] | 1034 | { |
---|
[3348] | 1035 | return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
[3330] | 1036 | } |
---|
| 1037 | |
---|
| 1038 | /** |
---|
| 1039 | \brief calculates the Rotation for the localTime of the Track. |
---|
| 1040 | \returns the calculated Rotation |
---|
| 1041 | */ |
---|
[3332] | 1042 | Vector TrackManager::calcDir() const |
---|
[3330] | 1043 | { |
---|
[3433] | 1044 | return this->currentTrackElem->curve->calcDir((this->localTime - this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
[3330] | 1045 | } |
---|
| 1046 | |
---|
| 1047 | /** |
---|
[3596] | 1048 | \returns the current Width of the track |
---|
| 1049 | */ |
---|
| 1050 | float TrackManager::getWidth(void) const |
---|
| 1051 | { |
---|
| 1052 | return this->currentTrackElem->width; |
---|
| 1053 | } |
---|
| 1054 | |
---|
| 1055 | /** |
---|
[3330] | 1056 | \brief Advances the local-time of the Track around dt |
---|
| 1057 | \param dt The time about which to advance. |
---|
[3333] | 1058 | |
---|
| 1059 | This function also checks, if the TrackElement has to be changed. |
---|
[3330] | 1060 | */ |
---|
| 1061 | void TrackManager::tick(float dt) |
---|
| 1062 | { |
---|
[3433] | 1063 | dt /= 1000; |
---|
[3591] | 1064 | PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt); |
---|
[3348] | 1065 | if (this->localTime <= this->firstTrackElem->duration) |
---|
| 1066 | this->jumpTo(this->localTime); |
---|
[3601] | 1067 | if (this->localTime <= this->maxTime) |
---|
| 1068 | this->localTime += dt; |
---|
[3433] | 1069 | if (this->localTime > this->currentTrackElem->endTime |
---|
| 1070 | && this->currentTrackElem->children) |
---|
| 1071 | { |
---|
[3527] | 1072 | if (this->currentTrackElem->jumpTime != 0.0) |
---|
[3433] | 1073 | this->jumpTo(this->localTime + this->currentTrackElem->jumpTime); |
---|
[3527] | 1074 | // jump to the next TrackElement and also set the history of the new Element to the old one. |
---|
| 1075 | TrackElement* tmpHistoryElem = this->currentTrackElem; |
---|
[3594] | 1076 | this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem)); |
---|
[3527] | 1077 | this->currentTrackElem->history = tmpHistoryElem; |
---|
[3845] | 1078 | if (this->currentTrackElem->getName()) |
---|
| 1079 | { |
---|
| 1080 | this->trackText->setText(this->currentTrackElem->getName()); |
---|
[3846] | 1081 | this->textAnimation->replay(); |
---|
[3845] | 1082 | } |
---|
[3433] | 1083 | } |
---|
| 1084 | if (this->bindSlave) |
---|
| 1085 | { |
---|
| 1086 | Vector tmp = this->calcPos(); |
---|
| 1087 | Quaternion quat = Quaternion(this->calcDir(), Vector(this->currentTrackElem->curve->calcAcc((localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration).x,1,this->currentTrackElem->curve->calcAcc((localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration).z)); |
---|
[3539] | 1088 | |
---|
| 1089 | Vector v(0.0, 1.0, 0.0); |
---|
[3543] | 1090 | Quaternion q(-PI/2, v); |
---|
[3539] | 1091 | quat = quat * q; |
---|
| 1092 | |
---|
[3809] | 1093 | this->bindSlave->setAbsCoor(tmp); |
---|
| 1094 | this->bindSlave->setAbsDir(quat); |
---|
[3433] | 1095 | } |
---|
[3330] | 1096 | } |
---|
| 1097 | |
---|
| 1098 | /** |
---|
[3331] | 1099 | \brief Jumps to a certain point on the Track. |
---|
| 1100 | \param time The time on the Track to jump to. |
---|
| 1101 | |
---|
| 1102 | This should be used to Jump backwards on a Track, because moving forward means to change between the Path. (it then tries to choose the default.) |
---|
| 1103 | Max is trackLengthMax. |
---|
| 1104 | */ |
---|
| 1105 | void TrackManager::jumpTo(float time) |
---|
| 1106 | { |
---|
[3348] | 1107 | if (time == 0) |
---|
[3845] | 1108 | { |
---|
| 1109 | this->currentTrackElem = this->firstTrackElem; |
---|
| 1110 | if (this->currentTrackElem->getName()) |
---|
| 1111 | { |
---|
| 1112 | this->trackText->setText(this->currentTrackElem->getName()); |
---|
| 1113 | this->textAnimation->play(); |
---|
| 1114 | } |
---|
| 1115 | } |
---|
[3348] | 1116 | this->localTime = time; |
---|
[3331] | 1117 | } |
---|
| 1118 | |
---|
| 1119 | /** |
---|
[3330] | 1120 | \brief a Function that decides which Path we should follow. |
---|
[3522] | 1121 | \param trackElem The Path to choose. |
---|
[3330] | 1122 | |
---|
| 1123 | */ |
---|
[3522] | 1124 | int TrackManager::choosePath(TrackElement* trackElem) |
---|
[3330] | 1125 | { |
---|
[3522] | 1126 | return (trackElem->*(trackElem->condFunc))(trackElem->subject); |
---|
[3330] | 1127 | } |
---|
| 1128 | |
---|
[3433] | 1129 | /** |
---|
| 1130 | \brief Sets the PNode, that should be moved along the Tack |
---|
| 1131 | \param bindSlave the PNode to set |
---|
| 1132 | */ |
---|
| 1133 | void TrackManager::setBindSlave(PNode* bindSlave) |
---|
| 1134 | { |
---|
[3556] | 1135 | this->bindSlave = bindSlave; |
---|
[3433] | 1136 | } |
---|
[3350] | 1137 | |
---|
[3556] | 1138 | /** |
---|
| 1139 | \returns the main TrackNode |
---|
| 1140 | */ |
---|
| 1141 | PNode* TrackManager::getTrackNode(void) |
---|
| 1142 | { |
---|
| 1143 | return this->trackNode; |
---|
| 1144 | } |
---|
[3350] | 1145 | |
---|
| 1146 | // DEBUG // |
---|
| 1147 | |
---|
| 1148 | /** |
---|
| 1149 | \brief Imports a model of the Graph into the OpenGL-environment. |
---|
| 1150 | \param dt The Iterator used in seconds for Painting the Graph. |
---|
| 1151 | |
---|
| 1152 | This is for testing facility only. Do this if you want to see the Path inside the Level. |
---|
| 1153 | eventually this will all be packed into a gl-list. |
---|
| 1154 | */ |
---|
| 1155 | void TrackManager::drawGraph(float dt) const |
---|
| 1156 | { |
---|
| 1157 | for (int i = 1; i <= trackElemCount; i++) |
---|
| 1158 | { |
---|
[3352] | 1159 | glBegin(GL_LINE_STRIP); |
---|
[3836] | 1160 | TrackElement* tmpElem = this->firstTrackElem->findByID(i); |
---|
[3350] | 1161 | if (tmpElem->curve) |
---|
| 1162 | for(float f = 0.0; f < 1.0; f+=dt) |
---|
| 1163 | { |
---|
| 1164 | // printf("%f, %f, %f\n",trackManager->calcPos().x, trackManager->calcPos().y, trackManager->calcPos().z); |
---|
| 1165 | Vector tmpVector = tmpElem->curve->calcPos(f); |
---|
| 1166 | glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z); |
---|
| 1167 | } |
---|
[3710] | 1168 | glEnd(); |
---|
[3350] | 1169 | } |
---|
| 1170 | } |
---|
| 1171 | |
---|
[3433] | 1172 | /** |
---|
| 1173 | \brief outputs debug information about the trackManager |
---|
| 1174 | \param level how much debug |
---|
| 1175 | */ |
---|
[3350] | 1176 | void TrackManager::debug(unsigned int level) const |
---|
| 1177 | { |
---|
[3522] | 1178 | PRINT(0)("=========================================\n"); |
---|
| 1179 | PRINT(0)("= CLASS TRACKMANAGER::debug information =\n"); |
---|
| 1180 | PRINT(0)("=========================================\n"); |
---|
| 1181 | // PRINT(0)("Status is: % |
---|
| 1182 | PRINT(0)(" Consists of %d elements\n", this->trackElemCount); |
---|
| 1183 | PRINT(0)(" localTime is: %f\n", this->localTime); |
---|
[3350] | 1184 | if (level >= 2) |
---|
| 1185 | { |
---|
| 1186 | for (int i = 1; i <= trackElemCount; i++) |
---|
| 1187 | { |
---|
[3836] | 1188 | TrackElement* tmpElem = this->firstTrackElem->findByID(i); |
---|
[3593] | 1189 | tmpElem->debug(); |
---|
[3350] | 1190 | } |
---|
| 1191 | } |
---|
[3522] | 1192 | PRINT(0)("-----------------------------------------\n"); |
---|
[3350] | 1193 | } |
---|