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