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