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