[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 | |
---|
[3433] | 20 | #include "p_node.h" |
---|
[3311] | 21 | |
---|
[3528] | 22 | #include "track_node.h" |
---|
| 23 | |
---|
[3495] | 24 | #include <stdarg.h> |
---|
| 25 | |
---|
[3311] | 26 | using namespace std; |
---|
| 27 | |
---|
[3331] | 28 | /** |
---|
| 29 | \brief initializes a TrackElement (sets the default values) |
---|
| 30 | */ |
---|
| 31 | TrackElement::TrackElement(void) |
---|
| 32 | { |
---|
[3332] | 33 | this->isFresh = true; |
---|
[3354] | 34 | this->isHotPoint = false; |
---|
[3331] | 35 | this->isSavePoint = false; |
---|
| 36 | this->isFork = false; |
---|
| 37 | this->isJoined = false; |
---|
[3356] | 38 | this->mainJoin = false; |
---|
[3331] | 39 | this->ID = -1; |
---|
[3588] | 40 | this->startingTime = 0; |
---|
| 41 | this->duration = TMAN_DEFAULT_DURATION; |
---|
[3433] | 42 | this->endTime = 1; |
---|
| 43 | this->jumpTime = 0; |
---|
[3331] | 44 | this->nodeCount = 0; |
---|
[3348] | 45 | this->childCount = 0; |
---|
[3332] | 46 | this->name = NULL; |
---|
[3331] | 47 | this->curve = NULL; |
---|
| 48 | this->children = NULL; |
---|
[3527] | 49 | |
---|
| 50 | this->history = NULL; |
---|
| 51 | |
---|
[3522] | 52 | this->condFunc = &TrackElement::random; |
---|
[3331] | 53 | } |
---|
[3311] | 54 | |
---|
[3331] | 55 | /** |
---|
[3332] | 56 | \brief destroys all alocated memory) |
---|
[3331] | 57 | \todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements |
---|
| 58 | */ |
---|
| 59 | TrackElement::~TrackElement(void) |
---|
| 60 | { |
---|
[3332] | 61 | if (this->name) |
---|
| 62 | delete []name; |
---|
[3331] | 63 | if (this->curve) |
---|
| 64 | delete this->curve; |
---|
[3356] | 65 | if ((!this->isJoined &&this->childCount > 0) || (this->isJoined && this->mainJoin)) |
---|
[3331] | 66 | { |
---|
[3594] | 67 | TrackElement* enumElem = children->enumerate(); |
---|
| 68 | while (enumElem) |
---|
| 69 | { |
---|
| 70 | delete enumElem; |
---|
| 71 | enumElem = children->nextElement(); |
---|
| 72 | } |
---|
[3356] | 73 | delete this->children; |
---|
[3331] | 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
[3332] | 77 | /** |
---|
| 78 | \brief Searches through all the TrackElements for trackID. |
---|
| 79 | \param trackID The ID to search for. |
---|
| 80 | \returns The TrackElement if Found, NULL otherwise. |
---|
| 81 | |
---|
| 82 | \todo make this more modular, to search for different things |
---|
| 83 | */ |
---|
| 84 | TrackElement* TrackElement::findByID(unsigned int trackID) |
---|
| 85 | { |
---|
| 86 | // return if Found. |
---|
| 87 | if (this->ID == trackID) |
---|
| 88 | return this; |
---|
| 89 | // search on. |
---|
| 90 | if (this->childCount > 0) |
---|
[3594] | 91 | { |
---|
| 92 | TrackElement* enumElem = this->children->enumerate(); |
---|
| 93 | TrackElement* tmpElem; |
---|
| 94 | while (enumElem) |
---|
| 95 | { |
---|
| 96 | if ((tmpElem = enumElem->findByID(trackID))) |
---|
| 97 | return tmpElem; |
---|
| 98 | enumElem = this->children->nextElement(); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | return NULL; |
---|
[3332] | 103 | } |
---|
[3331] | 104 | |
---|
| 105 | |
---|
[3522] | 106 | /** |
---|
| 107 | \brief checks if there are any BackLoops in the Track |
---|
| 108 | \param trackElem the trackElement to check about |
---|
| 109 | it simply does this by looking if the current trackElem is found again somewhere else in the Track |
---|
| 110 | */ |
---|
| 111 | bool TrackElement::backLoopCheck(TrackElement* trackElem) |
---|
| 112 | { |
---|
| 113 | if (this->childCount == 0) |
---|
| 114 | return true; |
---|
| 115 | else |
---|
| 116 | { |
---|
[3594] | 117 | TrackElement* enumElem = this->children->enumerate(); |
---|
| 118 | while (enumElem) |
---|
| 119 | { |
---|
| 120 | if(!enumElem->backLoopCheck(trackElem)) |
---|
| 121 | return false; |
---|
| 122 | enumElem = this->children->nextElement(); |
---|
| 123 | } |
---|
[3522] | 124 | |
---|
| 125 | return true; |
---|
| 126 | } |
---|
| 127 | } |
---|
[3331] | 128 | |
---|
[3522] | 129 | /** |
---|
[3594] | 130 | \param childNumber which child to return |
---|
| 131 | \returns the n-the children (starting at 0) |
---|
| 132 | */ |
---|
| 133 | TrackElement* TrackElement::getChild(int childCount) |
---|
| 134 | { |
---|
| 135 | if (this->childCount == 0) |
---|
| 136 | return NULL; |
---|
| 137 | if (childCount > this->childCount) |
---|
| 138 | childCount = this->childCount; |
---|
| 139 | |
---|
| 140 | TrackElement* enumElem = this->children->enumerate(); |
---|
| 141 | for (int i = 0; i < childCount; i++) |
---|
| 142 | enumElem = this->children->nextElement(); |
---|
| 143 | return enumElem; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /** |
---|
[3588] | 147 | \param name the Name to set. |
---|
| 148 | */ |
---|
| 149 | void TrackElement::setName(const char* name) |
---|
| 150 | { |
---|
| 151 | // delete the old name |
---|
| 152 | if (this->name) |
---|
| 153 | delete []this->name; |
---|
| 154 | // if a name was given already. |
---|
| 155 | if (name) |
---|
| 156 | { |
---|
| 157 | this->name = new char[strlen(name)+1]; |
---|
| 158 | strcpy(this->name, name); |
---|
| 159 | } |
---|
| 160 | else |
---|
| 161 | this->name = NULL; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | \returns The name of this TrackElement |
---|
| 166 | */ |
---|
| 167 | char* TrackElement::getName(void) const |
---|
| 168 | { |
---|
| 169 | return this->name; |
---|
| 170 | } |
---|
| 171 | |
---|
[3593] | 172 | /** |
---|
| 173 | \brief prints out debug information about this TrackElement |
---|
| 174 | */ |
---|
| 175 | void TrackElement::debug(void) |
---|
| 176 | { |
---|
| 177 | PRINT(0)("--== TrackElement:%i ==--", this->ID); |
---|
| 178 | if(this->getName()) |
---|
| 179 | PRINT(0)("Name: %s::", this->getName()); |
---|
| 180 | if(this->isFresh) |
---|
| 181 | PRINT(0)(" -- has not jet eddited in any way --\n"); |
---|
| 182 | PRINT(0)("\n TimeTable: startingTime=%f; endTime=%f; duration=%f; jumpTime=%f\n", this->startingTime, this->endTime, this->duration, this->jumpTime); |
---|
| 183 | PRINT(0)(" consists of %d Points\n", this->nodeCount); |
---|
| 184 | if (this->childCount == 0) |
---|
| 185 | PRINT(0)(" has no child\n"); |
---|
| 186 | else if (this->childCount == 1) |
---|
[3594] | 187 | PRINT(0)(" has 1 child: =%d=\n", this->getChild(0)->ID); |
---|
[3593] | 188 | else if (this->childCount > 1) |
---|
| 189 | { |
---|
| 190 | PRINT(0)(" has %d children: ", this->childCount); |
---|
[3594] | 191 | TrackElement* enumElem = this->children->enumerate(); |
---|
| 192 | while (enumElem) |
---|
| 193 | { |
---|
| 194 | PRINT(0)("=%d= ", enumElem->ID); |
---|
| 195 | enumElem = this->children->nextElement(); |
---|
| 196 | } |
---|
[3593] | 197 | PRINT(0)("\n"); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | if(this->isHotPoint) |
---|
| 201 | PRINT(0)(" is a special Point:\n"); |
---|
| 202 | if(this->isSavePoint) |
---|
| 203 | PRINT(0)(" is a SavePoint\n"); |
---|
| 204 | if(this->isFork) |
---|
| 205 | { |
---|
| 206 | PRINT(0)(" is A Fork with with %d children.\n", this->childCount); |
---|
| 207 | } |
---|
| 208 | if(this->isJoined) |
---|
| 209 | PRINT(0)(" is Joined at the End\n"); |
---|
| 210 | |
---|
| 211 | if(!this->backLoopCheck(this)) /* this should not happen */ |
---|
| 212 | PRINT(2)(" THERE IS A BACKLOOP TO THIS ELEMENT\n"); |
---|
| 213 | } |
---|
[3588] | 214 | |
---|
| 215 | /** |
---|
[3522] | 216 | \brief CONDITION that chooses the first child for the decision (static) |
---|
| 217 | \param nothing Nothing in this function |
---|
| 218 | \returns the chosen child |
---|
| 219 | */ |
---|
| 220 | int TrackElement::lowest(void* nothing) |
---|
| 221 | { |
---|
| 222 | return 0; |
---|
| 223 | } |
---|
[3332] | 224 | |
---|
[3522] | 225 | /** |
---|
| 226 | \brief CONDITION that chooses the last child for the decision (static) |
---|
| 227 | \param nothing Nothing in this function |
---|
| 228 | \returns the chosen child |
---|
| 229 | */ |
---|
| 230 | int TrackElement::highest(void* nothing) |
---|
| 231 | { |
---|
| 232 | return this->childCount-1; |
---|
| 233 | } |
---|
[3332] | 234 | |
---|
[3522] | 235 | /** |
---|
| 236 | \brief CONDITION that chooses a random child for the decision (static) |
---|
| 237 | \param nothing Nothing in this function |
---|
| 238 | \returns the chosen child |
---|
| 239 | */ |
---|
| 240 | int TrackElement::random(void* nothing) |
---|
| 241 | { |
---|
| 242 | int i = (int)floor ((float)rand()/(float)RAND_MAX * (float)this->childCount); |
---|
| 243 | if (i >= this->childCount) |
---|
| 244 | return this->childCount-1; |
---|
| 245 | else |
---|
| 246 | return i; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | /** |
---|
| 250 | \brief CONDITION that chooses child 0, if the node(probably Player) |
---|
| 251 | is left of its parent (z<0)) and 1/right otherwise. |
---|
| 252 | \param node The node to act upon. |
---|
| 253 | \returns the chosen child |
---|
| 254 | */ |
---|
| 255 | int TrackElement::leftRight(void* node) |
---|
| 256 | { |
---|
| 257 | PNode* tmpNode = (PNode*)node; |
---|
| 258 | |
---|
| 259 | if (tmpNode->getRelCoor().z < 0) |
---|
| 260 | return 0; |
---|
| 261 | else |
---|
| 262 | return 1; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | |
---|
| 266 | /** |
---|
| 267 | \brief CONDITION that chooses the child, that has the nearest distance to the node (probably player). |
---|
| 268 | \param node The node to act upon. |
---|
| 269 | \returns the chosen child |
---|
| 270 | |
---|
| 271 | This is rather dangerous, because one must carefully set the points on the curve. |
---|
| 272 | The best Way is to set the nodes as wide away of each other as possible, |
---|
| 273 | but take into consideration, that if the nodes are to far from a center node, the center will be chosen. |
---|
| 274 | (play with this!!). |
---|
| 275 | */ |
---|
| 276 | int TrackElement::nearest(void* node) |
---|
| 277 | { |
---|
| 278 | PNode* tmpNode = (PNode*)node; |
---|
| 279 | |
---|
| 280 | Vector nodeRelCoord = tmpNode->getRelCoor(); |
---|
| 281 | float minDist = 100000000; |
---|
[3594] | 282 | int childNumber = 0; |
---|
| 283 | int i = 0; |
---|
| 284 | |
---|
| 285 | TrackElement* enumElem = this->children->enumerate(); |
---|
| 286 | while (enumElem) |
---|
[3522] | 287 | { |
---|
[3594] | 288 | float dist = (nodeRelCoord - enumElem->curve->getNode(4)).len(); |
---|
[3522] | 289 | if (dist < minDist) |
---|
| 290 | { |
---|
| 291 | minDist = dist; |
---|
[3594] | 292 | childNumber = i; |
---|
[3522] | 293 | } |
---|
[3594] | 294 | i++; |
---|
| 295 | enumElem = this->children->nextElement(); |
---|
[3522] | 296 | } |
---|
[3594] | 297 | |
---|
| 298 | PRINTF(4)("PathDecision with nearest algorithm: %d\n", childNumber); |
---|
| 299 | return childNumber; |
---|
[3522] | 300 | } |
---|
| 301 | |
---|
| 302 | |
---|
[3332] | 303 | ///////////////////////////////////// |
---|
| 304 | ///// TRACKMANAGER ////////////////// |
---|
| 305 | ///////////////////////////////////// |
---|
[3311] | 306 | /** |
---|
| 307 | \brief standard constructor |
---|
| 308 | |
---|
| 309 | */ |
---|
[3354] | 310 | TrackManager::TrackManager(void) |
---|
[3311] | 311 | { |
---|
[3594] | 312 | this->setClassName("TrackManager"); |
---|
[3528] | 313 | |
---|
[3594] | 314 | TrackManager::singletonRef = this; |
---|
[3331] | 315 | |
---|
| 316 | PRINTF(3)("Initializing the TrackManager\n"); |
---|
[3348] | 317 | this->firstTrackElem = new TrackElement(); |
---|
| 318 | this->firstTrackElem->ID = 1; |
---|
[3331] | 319 | this->currentTrackElem = firstTrackElem; |
---|
| 320 | this->localTime = 0; |
---|
| 321 | this->maxTime = 0; |
---|
[3348] | 322 | this->trackElemCount = 1; |
---|
[3594] | 323 | this->setBindSlave(this->trackNode = new TrackNode()); |
---|
[3311] | 324 | } |
---|
| 325 | |
---|
| 326 | |
---|
| 327 | /** |
---|
[3331] | 328 | \brief standard destructor |
---|
[3311] | 329 | */ |
---|
[3354] | 330 | TrackManager::~TrackManager(void) |
---|
[3330] | 331 | { |
---|
[3331] | 332 | PRINTF(3)("Destruct TrackManager\n"); |
---|
[3311] | 333 | |
---|
[3594] | 334 | PRINTF(4)("Deleting all the TrackElements\n"); |
---|
[3331] | 335 | delete this->firstTrackElem; |
---|
[3335] | 336 | |
---|
[3331] | 337 | // we do not have a TrackManager anymore |
---|
[3594] | 338 | TrackManager::singletonRef = NULL; |
---|
[3330] | 339 | } |
---|
| 340 | |
---|
[3543] | 341 | //! Singleton Reference to TrackManager |
---|
[3331] | 342 | TrackManager* TrackManager::singletonRef = NULL; |
---|
| 343 | |
---|
[3330] | 344 | /** |
---|
[3331] | 345 | \returns The reference on the TrackManager. |
---|
| 346 | |
---|
| 347 | If the TrackManager does not exist, it will be created. |
---|
| 348 | */ |
---|
| 349 | TrackManager* TrackManager::getInstance(void) |
---|
| 350 | { |
---|
[3594] | 351 | if (!TrackManager::singletonRef) |
---|
| 352 | TrackManager::singletonRef = new TrackManager(); |
---|
| 353 | return TrackManager::singletonRef; |
---|
[3331] | 354 | } |
---|
| 355 | |
---|
| 356 | /** |
---|
[3335] | 357 | \brief reserves Space for childCount children |
---|
| 358 | \param childCount The Count of children to make space for. |
---|
| 359 | */ |
---|
| 360 | void TrackManager::initChildren(unsigned int childCount) |
---|
| 361 | { |
---|
| 362 | this->currentTrackElem->childCount = childCount; |
---|
[3433] | 363 | this->currentTrackElem->mainJoin = true; |
---|
[3594] | 364 | this->currentTrackElem->children = new tList<TrackElement>(); |
---|
| 365 | for (int i = 0; i < childCount; i++) |
---|
[3348] | 366 | { |
---|
[3594] | 367 | TrackElement* newElem = new TrackElement(); |
---|
| 368 | this->currentTrackElem->children->add(newElem); |
---|
| 369 | newElem->ID = ++trackElemCount; |
---|
| 370 | newElem->startingTime = this->currentTrackElem->endTime + this->currentTrackElem->jumpTime; |
---|
| 371 | this->addPoint(this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount()), this->currentTrackElem->getChild(i)); |
---|
[3348] | 372 | } |
---|
[3588] | 373 | if (childCount == 1) |
---|
[3594] | 374 | this->currentTrackElem->getChild(0)->setName(this->currentTrackElem->getName()); |
---|
[3335] | 375 | } |
---|
| 376 | |
---|
| 377 | /** |
---|
[3330] | 378 | \brief Searches for a given trackID. |
---|
| 379 | \param trackID the trackID to search for. |
---|
[3332] | 380 | \returns The TrackElement #trackID if found, NULL otherwise. |
---|
[3330] | 381 | */ |
---|
[3332] | 382 | TrackElement* TrackManager::findTrackElementByID(unsigned int trackID) const |
---|
[3330] | 383 | { |
---|
[3332] | 384 | return firstTrackElem->findByID(trackID); |
---|
[3330] | 385 | } |
---|
| 386 | |
---|
| 387 | // INITIALIZE // |
---|
| 388 | |
---|
| 389 | /** |
---|
| 390 | \brief Sets the trackID we are working on. |
---|
| 391 | \param trackID the trackID we are working on |
---|
| 392 | */ |
---|
[3332] | 393 | void TrackManager::workOn(unsigned int trackID) |
---|
[3330] | 394 | { |
---|
[3355] | 395 | TrackElement* tmpElem = findTrackElementByID(trackID); |
---|
| 396 | if (tmpElem) |
---|
| 397 | this->currentTrackElem = tmpElem; |
---|
| 398 | else |
---|
[3594] | 399 | PRINTF(2)("TrackElement not Found, leaving unchanged\n"); |
---|
| 400 | PRINTF(4)("now Working on %d\n", this->currentTrackElem->ID); |
---|
[3355] | 401 | |
---|
[3330] | 402 | } |
---|
| 403 | |
---|
| 404 | /** |
---|
| 405 | \brief Sets the Type of the Curve |
---|
[3522] | 406 | \param curveType The Type to set |
---|
| 407 | \param trackElem the TrackElement that should get a new Curve. |
---|
[3330] | 408 | */ |
---|
[3433] | 409 | void TrackManager::setCurveType(CurveType curveType, TrackElement* trackElem) |
---|
[3330] | 410 | { |
---|
[3433] | 411 | if (!trackElem->isFresh) |
---|
[3332] | 412 | { |
---|
| 413 | PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n"); |
---|
| 414 | return; |
---|
| 415 | } |
---|
[3588] | 416 | this->curveType = curveType; |
---|
[3332] | 417 | switch (curveType) |
---|
| 418 | { |
---|
| 419 | case BEZIERCURVE: |
---|
[3433] | 420 | trackElem->curve = new BezierCurve(); |
---|
[3332] | 421 | break; |
---|
| 422 | case UPOINTCURVE: |
---|
[3433] | 423 | trackElem->curve = new UPointCurve(); |
---|
[3332] | 424 | break; |
---|
| 425 | } |
---|
[3330] | 426 | } |
---|
| 427 | |
---|
| 428 | /** |
---|
[3333] | 429 | \brief Sets the duration of the current path in seconds. |
---|
| 430 | \param time The duration in seconds. |
---|
[3330] | 431 | */ |
---|
| 432 | |
---|
[3333] | 433 | void TrackManager::setDuration(float time) |
---|
[3330] | 434 | { |
---|
[3333] | 435 | this->currentTrackElem->duration = time; |
---|
[3433] | 436 | this->currentTrackElem->endTime = this->currentTrackElem->startingTime + time; |
---|
[3330] | 437 | } |
---|
| 438 | |
---|
| 439 | /** |
---|
| 440 | \brief adds a point to the current TrackElement |
---|
| 441 | \param newPoint The point to add. |
---|
| 442 | */ |
---|
[3333] | 443 | bool TrackManager::addPoint(Vector newPoint) |
---|
[3330] | 444 | { |
---|
[3352] | 445 | return this->addPoint(newPoint, this->currentTrackElem); |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | /** |
---|
| 449 | \brief adds a point to trackElem |
---|
| 450 | \param newPoint The point to add. |
---|
| 451 | \param trackElem The TrackElement to add the Point to |
---|
| 452 | */ |
---|
| 453 | bool TrackManager::addPoint(Vector newPoint, TrackElement* trackElem) |
---|
| 454 | { |
---|
| 455 | if (trackElem->isFresh) |
---|
[3332] | 456 | { |
---|
[3588] | 457 | this->setCurveType(TMAN_DEFAULT_CURVETYPE, trackElem); |
---|
[3352] | 458 | trackElem->isFresh = false; |
---|
[3332] | 459 | } |
---|
[3352] | 460 | trackElem->curve->addNode(newPoint); |
---|
| 461 | trackElem->nodeCount++; |
---|
[3330] | 462 | } |
---|
| 463 | |
---|
| 464 | /** |
---|
| 465 | \brief adds save/splitpoint. |
---|
| 466 | \param newPoint The point to add. |
---|
[3333] | 467 | \returns A Pointer to a newly appended Curve |
---|
[3330] | 468 | */ |
---|
[3333] | 469 | int TrackManager::addHotPoint(Vector newPoint) |
---|
[3330] | 470 | { |
---|
[3594] | 471 | PRINTF(4)("setting up a HotPoint\n"); |
---|
[3332] | 472 | if (this->currentTrackElem->isFresh) |
---|
| 473 | { |
---|
| 474 | this->setCurveType(BEZIERCURVE); |
---|
| 475 | this->currentTrackElem->isFresh = false; |
---|
| 476 | } |
---|
[3330] | 477 | |
---|
[3332] | 478 | // \todo HotPoint Handling. |
---|
| 479 | this->currentTrackElem->curve->addNode(newPoint); |
---|
[3351] | 480 | this->currentTrackElem->nodeCount++; |
---|
[3354] | 481 | this->initChildren(1); |
---|
[3594] | 482 | this->currentTrackElem = this->currentTrackElem->getChild(0); |
---|
[3330] | 483 | } |
---|
| 484 | |
---|
| 485 | /** |
---|
| 486 | \brief Sets the last HotPoint into a savePoint. |
---|
[3333] | 487 | \returns A Pointer to a newly appended Curve |
---|
[3330] | 488 | |
---|
| 489 | If no HotPoint was defined the last added Point will be rendered into a savePoint. \n |
---|
| 490 | If the HotPoint was defined as a fork the Point will \b not be set into a savePoint. |
---|
| 491 | */ |
---|
[3333] | 492 | int TrackManager::setSavePoint(void) |
---|
[3330] | 493 | { |
---|
[3594] | 494 | PRINTF(4)("setting up a SavePoint.\n"); |
---|
[3332] | 495 | if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint) |
---|
[3594] | 496 | { |
---|
| 497 | PRINTF(2)("%d is already finished \n", currentTrackElem->ID); |
---|
| 498 | return this->currentTrackElem->getChild(0)->ID; |
---|
| 499 | } |
---|
[3332] | 500 | this->currentTrackElem->isSavePoint = true; |
---|
[3354] | 501 | this->currentTrackElem->isHotPoint = true; |
---|
[3332] | 502 | |
---|
[3335] | 503 | this->initChildren(1); |
---|
[3594] | 504 | this->currentTrackElem = this->currentTrackElem->getChild(0); |
---|
[3330] | 505 | } |
---|
| 506 | |
---|
| 507 | /** |
---|
| 508 | \brief adds some interessting non-linear movments through the level. |
---|
| 509 | \param count The Count of childrens the current HotPoint will have. |
---|
| 510 | |
---|
| 511 | If no HotPoint was defined the last added Point will be rendered into a fork. \n |
---|
| 512 | If the HotPoint was defined as a savePoint the Point will \b not be set into a fork. |
---|
| 513 | */ |
---|
[3332] | 514 | void TrackManager::fork(unsigned int count, ...) |
---|
[3330] | 515 | { |
---|
[3351] | 516 | int* trackIDs = new int[count]; |
---|
| 517 | this->forkV(count, trackIDs); |
---|
[3332] | 518 | va_list ID; |
---|
| 519 | va_start (ID, count); |
---|
| 520 | for(int i = 0; i < count; i++) |
---|
| 521 | { |
---|
[3351] | 522 | *va_arg (ID, int*) = trackIDs[i]; |
---|
[3332] | 523 | } |
---|
[3351] | 524 | va_end(ID); |
---|
[3332] | 525 | delete []trackIDs; |
---|
[3330] | 526 | } |
---|
| 527 | |
---|
| 528 | /** |
---|
| 529 | \brief adds some interessting non-linear movments through the level. |
---|
| 530 | \param count The Count of childrens the current HotPoint will have. |
---|
| 531 | \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this). |
---|
| 532 | |
---|
| 533 | \see void TrackManager::fork(int count, ...) |
---|
[3332] | 534 | |
---|
| 535 | \todo initialisation is wrong!! also in setSavePoint. |
---|
[3330] | 536 | */ |
---|
[3332] | 537 | void TrackManager::forkV(unsigned int count, int* trackIDs) |
---|
[3330] | 538 | { |
---|
[3594] | 539 | PRINTF(4)("Forking with %d children\n", count); |
---|
[3332] | 540 | if (this->currentTrackElem->isSavePoint) |
---|
| 541 | return; |
---|
| 542 | this->currentTrackElem->isFork = true; |
---|
[3354] | 543 | this->currentTrackElem->isHotPoint = true; |
---|
[3351] | 544 | for(int i = 0; i < count; i++) |
---|
| 545 | trackIDs[i]=this->trackElemCount+1+i; |
---|
[3335] | 546 | this->initChildren(count); |
---|
[3330] | 547 | } |
---|
| 548 | |
---|
| 549 | /** |
---|
| 550 | \brief decides under what condition a certain Path will be chosen. |
---|
[3522] | 551 | \param cond the CONDITION of the decision |
---|
| 552 | \param subject the Subject that will be decided upon with CONDITION cond. |
---|
| 553 | */ |
---|
| 554 | void TrackManager::condition(CONDITION cond, void* subject) |
---|
| 555 | { |
---|
| 556 | this->condition(this->currentTrackElem->ID, cond, subject); |
---|
| 557 | } |
---|
| 558 | /** |
---|
| 559 | \brief decides under what condition a certain Path will be chosen. |
---|
[3330] | 560 | \param groupID the ID on which to choose the preceding move |
---|
[3522] | 561 | \param cond the CONDITION of the decision |
---|
| 562 | \param subject the Subject that will be decided upon with CONDITION cond. |
---|
[3330] | 563 | */ |
---|
[3522] | 564 | void TrackManager::condition(unsigned int groupID, CONDITION cond, void* subject) |
---|
[3330] | 565 | { |
---|
[3522] | 566 | TrackElement* tmpElem = this->findTrackElementByID(groupID); |
---|
[3594] | 567 | if (!tmpElem->isFork) |
---|
[3522] | 568 | { |
---|
[3594] | 569 | PRINTF(2)("%d is not a Fork, and no condition can be set in this case\n", tmpElem->ID); |
---|
| 570 | return; |
---|
[3522] | 571 | } |
---|
[3594] | 572 | else |
---|
| 573 | { |
---|
| 574 | switch (cond) |
---|
| 575 | { |
---|
| 576 | case LOWEST: |
---|
| 577 | tmpElem->condFunc = &TrackElement::lowest; |
---|
| 578 | break; |
---|
| 579 | case HIGHEST: |
---|
| 580 | tmpElem->condFunc = &TrackElement::highest; |
---|
| 581 | break; |
---|
| 582 | case RANDOM: |
---|
| 583 | tmpElem->condFunc = &TrackElement::random; |
---|
| 584 | break; |
---|
| 585 | case LEFTRIGHT: |
---|
| 586 | tmpElem->condFunc = &TrackElement::leftRight; |
---|
| 587 | break; |
---|
| 588 | case NEAREST: |
---|
| 589 | tmpElem->condFunc = &TrackElement::nearest; |
---|
| 590 | break; |
---|
| 591 | case ENEMYKILLED: |
---|
| 592 | break; |
---|
| 593 | } |
---|
| 594 | tmpElem->subject=subject; |
---|
| 595 | } |
---|
[3330] | 596 | } |
---|
| 597 | |
---|
[3522] | 598 | |
---|
[3330] | 599 | /** |
---|
| 600 | \brief joins some tracks together again. |
---|
| 601 | \param count The count of Paths to join. |
---|
| 602 | |
---|
| 603 | Join will set the localTime to the longest time a Path has to get to this Point. \n |
---|
[3354] | 604 | Join will join all curves to the first curve, meaning that all the tangents will be matched. |
---|
[3330] | 605 | */ |
---|
[3332] | 606 | void TrackManager::join(unsigned int count, ...) |
---|
[3330] | 607 | { |
---|
[3332] | 608 | int* trackIDs = new int [count]; |
---|
| 609 | va_list ID; |
---|
| 610 | va_start (ID, count); |
---|
| 611 | for(int i = 0; i < count; i++) |
---|
| 612 | { |
---|
| 613 | trackIDs[i] = va_arg (ID, int); |
---|
| 614 | } |
---|
| 615 | va_end(ID); |
---|
| 616 | this->joinV(count, trackIDs); |
---|
| 617 | delete []trackIDs; |
---|
[3330] | 618 | } |
---|
| 619 | |
---|
| 620 | /** |
---|
| 621 | \brief joins some tracks together again. |
---|
| 622 | \param count The count of Paths to join. |
---|
| 623 | \param trackIDs an Array with the trackID's to join |
---|
| 624 | |
---|
| 625 | \see void TrackManager::join(int count, ...) |
---|
| 626 | */ |
---|
[3332] | 627 | void TrackManager::joinV(unsigned int count, int* trackIDs) |
---|
[3330] | 628 | { |
---|
[3594] | 629 | PRINTF(3)("Joining %d tracks and merging to Track %d\n", count, trackIDs[0]); |
---|
[3354] | 630 | |
---|
[3522] | 631 | // checking if there is a back-loop-connection and ERROR if it is. |
---|
| 632 | TrackElement* tmpTrackElem = this->findTrackElementByID(trackIDs[0]); |
---|
| 633 | if (!tmpTrackElem->backLoopCheck(tmpTrackElem)) |
---|
[3594] | 634 | PRINTF(2)("Backloop connection detected at joining trackElements\n"); |
---|
[3522] | 635 | |
---|
[3354] | 636 | // chanching work-on to temporary value. going back at the end. |
---|
| 637 | int tmpCurrentWorkingID = this->currentTrackElem->ID; |
---|
| 638 | this->workOn(trackIDs[0]); |
---|
[3433] | 639 | TrackElement* firstJoint = this->currentTrackElem; |
---|
| 640 | float tmpLatestTime = firstJoint->endTime; |
---|
[3354] | 641 | |
---|
| 642 | Vector tmpEndPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()); |
---|
[3433] | 643 | Vector tmpTangentPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-1); |
---|
| 644 | Vector tmpc2Point = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-2); |
---|
[3354] | 645 | firstJoint->isJoined = true; |
---|
[3433] | 646 | // firstJoint->mainJoin = true; |
---|
[3354] | 647 | if(!firstJoint->isHotPoint) |
---|
| 648 | this->setSavePoint(); |
---|
[3433] | 649 | // Timing: |
---|
| 650 | for (int i = 0; i < count; i++) |
---|
| 651 | { |
---|
| 652 | TrackElement* tmpJoinElem = this->findTrackElementByID(trackIDs[i]); |
---|
| 653 | if (tmpJoinElem->childCount == 0 |
---|
| 654 | && tmpJoinElem->endTime > tmpLatestTime) |
---|
| 655 | tmpLatestTime = tmpJoinElem->endTime; |
---|
| 656 | } |
---|
| 657 | // time the main Join. |
---|
| 658 | firstJoint->jumpTime = tmpLatestTime - firstJoint->endTime; |
---|
| 659 | |
---|
| 660 | // Joining: |
---|
[3354] | 661 | for (int i = 1; i < count; i++) |
---|
[3352] | 662 | { |
---|
| 663 | TrackElement* tmpJoinElem = this->findTrackElementByID(trackIDs[i]); |
---|
[3354] | 664 | if (tmpJoinElem->childCount > 0) |
---|
| 665 | printf("!!This Curve has children, and as such will not be joined!!\n You can try joining other childless TrackElements to this one!"); |
---|
| 666 | else |
---|
| 667 | { |
---|
[3433] | 668 | this->addPoint(tmpc2Point, tmpJoinElem); |
---|
[3354] | 669 | this->addPoint(tmpTangentPoint, tmpJoinElem); |
---|
| 670 | this->addPoint(tmpEndPoint, tmpJoinElem); |
---|
[3433] | 671 | // time all other Joins |
---|
| 672 | tmpJoinElem->jumpTime = tmpLatestTime - tmpJoinElem->endTime; |
---|
[3354] | 673 | |
---|
| 674 | //Copying Joint-Info |
---|
| 675 | tmpJoinElem->children = firstJoint->children; |
---|
| 676 | tmpJoinElem->childCount = firstJoint->childCount; |
---|
| 677 | tmpJoinElem->isSavePoint = firstJoint->isSavePoint; |
---|
| 678 | tmpJoinElem->isFork = firstJoint->isFork; |
---|
[3433] | 679 | |
---|
[3354] | 680 | tmpJoinElem->isJoined = true; |
---|
| 681 | } |
---|
[3352] | 682 | } |
---|
[3433] | 683 | if(firstJoint->childCount > 0) |
---|
[3594] | 684 | { |
---|
| 685 | TrackElement* enumElem = firstJoint->children->enumerate(); |
---|
| 686 | while (enumElem) |
---|
| 687 | { |
---|
| 688 | PRINTF(4)("Setting startingTime of %d to %f.\n", enumElem->ID, tmpLatestTime); |
---|
| 689 | enumElem->startingTime = tmpLatestTime; |
---|
| 690 | enumElem->endTime = tmpLatestTime + enumElem->duration; |
---|
| 691 | |
---|
| 692 | enumElem = firstJoint->children->nextElement(); |
---|
| 693 | } |
---|
| 694 | } |
---|
[3354] | 695 | // returning to the TrackElement we were working on. |
---|
| 696 | this->workOn(tmpCurrentWorkingID); |
---|
[3330] | 697 | } |
---|
| 698 | |
---|
[3433] | 699 | /** |
---|
| 700 | \brief finalizes the TrackSystem. after this it will not be editable anymore |
---|
| 701 | |
---|
| 702 | \todo check for any inconsistencies, output errors |
---|
| 703 | */ |
---|
| 704 | void TrackManager::finalize(void) |
---|
| 705 | { |
---|
| 706 | for (int i = 1; i<= trackElemCount ;i++) |
---|
| 707 | { |
---|
| 708 | TrackElement* tmpElem = findTrackElementByID(i); |
---|
[3594] | 709 | if (tmpElem->childCount > 0 && tmpElem->mainJoin) |
---|
[3433] | 710 | { |
---|
[3594] | 711 | |
---|
| 712 | TrackElement* enumElem = tmpElem->children->enumerate(); |
---|
| 713 | while (enumElem) |
---|
[3433] | 714 | { |
---|
| 715 | |
---|
| 716 | // c1-continuity |
---|
[3594] | 717 | enumElem->curve->addNode(enumElem->curve->getNode(0) + |
---|
| 718 | ((enumElem->curve->getNode(0) - |
---|
[3433] | 719 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) |
---|
| 720 | ),2); |
---|
[3594] | 721 | enumElem->nodeCount++; |
---|
[3433] | 722 | // c2-continuity |
---|
[3594] | 723 | enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())- |
---|
[3433] | 724 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 + |
---|
| 725 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3); |
---|
[3594] | 726 | enumElem->nodeCount++; |
---|
| 727 | PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n %d-out: count: %d %f, %f, %f\n", |
---|
[3433] | 728 | tmpElem->ID, tmpElem->nodeCount, |
---|
| 729 | tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z, |
---|
[3594] | 730 | enumElem->ID, enumElem->nodeCount, |
---|
| 731 | enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z); |
---|
| 732 | |
---|
| 733 | enumElem = tmpElem->children->nextElement(); |
---|
[3433] | 734 | } |
---|
[3594] | 735 | |
---|
| 736 | |
---|
[3433] | 737 | } |
---|
| 738 | } |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | |
---|
[3330] | 742 | // RUNTIME // |
---|
| 743 | |
---|
| 744 | /** |
---|
| 745 | \brief calculates the Position for the localTime of the Track. |
---|
| 746 | \returns the calculated Position |
---|
| 747 | */ |
---|
[3332] | 748 | Vector TrackManager::calcPos() const |
---|
[3330] | 749 | { |
---|
[3348] | 750 | // PRINTF(0)("TrackElement:%d, localTime: %f\n",this->currentTrackElem->ID, this->localTime); |
---|
| 751 | return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
[3330] | 752 | } |
---|
| 753 | |
---|
| 754 | /** |
---|
| 755 | \brief calculates the Rotation for the localTime of the Track. |
---|
| 756 | \returns the calculated Rotation |
---|
| 757 | */ |
---|
[3332] | 758 | Vector TrackManager::calcDir() const |
---|
[3330] | 759 | { |
---|
[3433] | 760 | return this->currentTrackElem->curve->calcDir((this->localTime - this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
[3330] | 761 | } |
---|
| 762 | |
---|
| 763 | /** |
---|
| 764 | \brief Advances the local-time of the Track around dt |
---|
| 765 | \param dt The time about which to advance. |
---|
[3333] | 766 | |
---|
| 767 | This function also checks, if the TrackElement has to be changed. |
---|
[3330] | 768 | */ |
---|
| 769 | void TrackManager::tick(float dt) |
---|
| 770 | { |
---|
[3433] | 771 | dt /= 1000; |
---|
[3591] | 772 | PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt); |
---|
[3348] | 773 | if (this->localTime <= this->firstTrackElem->duration) |
---|
| 774 | this->jumpTo(this->localTime); |
---|
[3332] | 775 | this->localTime += dt; |
---|
[3433] | 776 | if (this->localTime > this->currentTrackElem->endTime |
---|
| 777 | && this->currentTrackElem->children) |
---|
| 778 | { |
---|
[3527] | 779 | if (this->currentTrackElem->jumpTime != 0.0) |
---|
[3433] | 780 | this->jumpTo(this->localTime + this->currentTrackElem->jumpTime); |
---|
[3527] | 781 | // jump to the next TrackElement and also set the history of the new Element to the old one. |
---|
| 782 | TrackElement* tmpHistoryElem = this->currentTrackElem; |
---|
[3594] | 783 | this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem)); |
---|
[3527] | 784 | this->currentTrackElem->history = tmpHistoryElem; |
---|
[3433] | 785 | } |
---|
| 786 | if (this->bindSlave) |
---|
| 787 | { |
---|
| 788 | Vector tmp = this->calcPos(); |
---|
| 789 | 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] | 790 | |
---|
| 791 | Vector v(0.0, 1.0, 0.0); |
---|
[3543] | 792 | Quaternion q(-PI/2, v); |
---|
[3539] | 793 | quat = quat * q; |
---|
| 794 | |
---|
[3433] | 795 | this->bindSlave->setAbsCoor(&tmp); |
---|
| 796 | this->bindSlave->setAbsDir(&quat); |
---|
| 797 | } |
---|
[3330] | 798 | } |
---|
| 799 | |
---|
| 800 | /** |
---|
[3331] | 801 | \brief Jumps to a certain point on the Track. |
---|
| 802 | \param time The time on the Track to jump to. |
---|
| 803 | |
---|
| 804 | 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.) |
---|
| 805 | Max is trackLengthMax. |
---|
| 806 | */ |
---|
| 807 | void TrackManager::jumpTo(float time) |
---|
| 808 | { |
---|
[3348] | 809 | if (time == 0) |
---|
| 810 | this->currentTrackElem = this->firstTrackElem; |
---|
| 811 | this->localTime = time; |
---|
[3331] | 812 | } |
---|
| 813 | |
---|
| 814 | /** |
---|
[3330] | 815 | \brief a Function that decides which Path we should follow. |
---|
[3522] | 816 | \param trackElem The Path to choose. |
---|
[3330] | 817 | |
---|
| 818 | */ |
---|
[3522] | 819 | int TrackManager::choosePath(TrackElement* trackElem) |
---|
[3330] | 820 | { |
---|
[3522] | 821 | return (trackElem->*(trackElem->condFunc))(trackElem->subject); |
---|
[3330] | 822 | } |
---|
| 823 | |
---|
[3433] | 824 | /** |
---|
| 825 | \brief Sets the PNode, that should be moved along the Tack |
---|
| 826 | \param bindSlave the PNode to set |
---|
| 827 | */ |
---|
| 828 | void TrackManager::setBindSlave(PNode* bindSlave) |
---|
| 829 | { |
---|
[3556] | 830 | this->bindSlave = bindSlave; |
---|
[3433] | 831 | } |
---|
[3350] | 832 | |
---|
[3556] | 833 | /** |
---|
| 834 | \returns the main TrackNode |
---|
| 835 | */ |
---|
| 836 | PNode* TrackManager::getTrackNode(void) |
---|
| 837 | { |
---|
| 838 | return this->trackNode; |
---|
| 839 | } |
---|
[3350] | 840 | |
---|
| 841 | // DEBUG // |
---|
| 842 | |
---|
| 843 | /** |
---|
| 844 | \brief Imports a model of the Graph into the OpenGL-environment. |
---|
| 845 | \param dt The Iterator used in seconds for Painting the Graph. |
---|
| 846 | |
---|
| 847 | This is for testing facility only. Do this if you want to see the Path inside the Level. |
---|
| 848 | eventually this will all be packed into a gl-list. |
---|
| 849 | */ |
---|
| 850 | void TrackManager::drawGraph(float dt) const |
---|
| 851 | { |
---|
[3352] | 852 | |
---|
[3350] | 853 | for (int i = 1; i <= trackElemCount; i++) |
---|
| 854 | { |
---|
[3352] | 855 | glBegin(GL_LINE_STRIP); |
---|
[3350] | 856 | TrackElement* tmpElem = this->findTrackElementByID(i); |
---|
| 857 | if (tmpElem->curve) |
---|
| 858 | for(float f = 0.0; f < 1.0; f+=dt) |
---|
| 859 | { |
---|
| 860 | // printf("%f, %f, %f\n",trackManager->calcPos().x, trackManager->calcPos().y, trackManager->calcPos().z); |
---|
| 861 | Vector tmpVector = tmpElem->curve->calcPos(f); |
---|
| 862 | glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z); |
---|
| 863 | } |
---|
[3352] | 864 | glEnd(); |
---|
[3350] | 865 | } |
---|
| 866 | } |
---|
| 867 | |
---|
[3433] | 868 | /** |
---|
| 869 | \brief outputs debug information about the trackManager |
---|
| 870 | \param level how much debug |
---|
| 871 | */ |
---|
[3350] | 872 | void TrackManager::debug(unsigned int level) const |
---|
| 873 | { |
---|
[3522] | 874 | PRINT(0)("=========================================\n"); |
---|
| 875 | PRINT(0)("= CLASS TRACKMANAGER::debug information =\n"); |
---|
| 876 | PRINT(0)("=========================================\n"); |
---|
| 877 | // PRINT(0)("Status is: % |
---|
| 878 | PRINT(0)(" Consists of %d elements\n", this->trackElemCount); |
---|
| 879 | PRINT(0)(" localTime is: %f\n", this->localTime); |
---|
[3350] | 880 | if (level >= 2) |
---|
| 881 | { |
---|
| 882 | for (int i = 1; i <= trackElemCount; i++) |
---|
| 883 | { |
---|
| 884 | TrackElement* tmpElem = this->findTrackElementByID(i); |
---|
[3593] | 885 | tmpElem->debug(); |
---|
[3350] | 886 | } |
---|
| 887 | } |
---|
[3522] | 888 | PRINT(0)("-----------------------------------------\n"); |
---|
[3350] | 889 | } |
---|