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