[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: |
---|
[3499] | 12 | main-programmer: Benjamin Grauer |
---|
[3523] | 13 | co-programmer: Christian Meyer |
---|
[3311] | 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | #include "track_manager.h" |
---|
[3499] | 18 | |
---|
| 19 | #include "p_node.h" |
---|
[3523] | 20 | #include "substring.h" |
---|
[3499] | 21 | |
---|
[3332] | 22 | #include <stdarg.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->cond; //!< todo think!! |
---|
| 38 | this->ID = -1; |
---|
[3333] | 39 | this->startingTime = 0; //!< \todo eventually set this to the max time of TrackManager. |
---|
[3348] | 40 | this->duration = 1; |
---|
[3499] | 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; |
---|
| 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 | |
---|
[3525] | 92 | /** |
---|
| 93 | \brief Sets the name of this TrackElement |
---|
| 94 | \param trackName The new name for this element |
---|
| 95 | */ |
---|
| 96 | void TrackElement::setName(const char* trackName) |
---|
| 97 | { |
---|
| 98 | if( this->name != NULL) delete this->name; |
---|
| 99 | |
---|
| 100 | this->name = new char[strlen(trackName) + 1]; |
---|
| 101 | strcpy( this->name, trackName); |
---|
| 102 | } |
---|
[3331] | 103 | |
---|
[3525] | 104 | /** |
---|
| 105 | \brief Searches through all the TrackElements for trackName. |
---|
| 106 | \param trackName The name to search for. |
---|
| 107 | \returns The TrackElement if Found, NULL otherwise. |
---|
| 108 | |
---|
| 109 | \todo make this more modular, to search for different things |
---|
| 110 | */ |
---|
| 111 | TrackElement* TrackElement::findByName(const char* trackName) |
---|
| 112 | { |
---|
| 113 | // return if Found. |
---|
[3530] | 114 | if ( this->name != NULL && !strcmp( this->name, trackName)) |
---|
[3525] | 115 | return this; |
---|
| 116 | // search on. |
---|
| 117 | if (this->childCount > 0) |
---|
| 118 | for (int i=0; i < this->childCount; i++) |
---|
| 119 | { |
---|
| 120 | TrackElement* tmpElem; |
---|
| 121 | if ((tmpElem = this->children[i]->findByName(trackName))) |
---|
| 122 | return tmpElem; |
---|
| 123 | } |
---|
| 124 | else return NULL; |
---|
| 125 | } |
---|
[3331] | 126 | |
---|
[3332] | 127 | |
---|
| 128 | |
---|
[3525] | 129 | |
---|
[3332] | 130 | ///////////////////////////////////// |
---|
| 131 | ///// TRACKMANAGER ////////////////// |
---|
| 132 | ///////////////////////////////////// |
---|
[3311] | 133 | /** |
---|
| 134 | \brief standard constructor |
---|
| 135 | |
---|
| 136 | */ |
---|
[3354] | 137 | TrackManager::TrackManager(void) |
---|
[3311] | 138 | { |
---|
[3331] | 139 | this->setClassName ("TrackManager"); |
---|
| 140 | |
---|
| 141 | PRINTF(3)("Initializing the TrackManager\n"); |
---|
[3348] | 142 | this->firstTrackElem = new TrackElement(); |
---|
| 143 | this->firstTrackElem->ID = 1; |
---|
[3331] | 144 | this->currentTrackElem = firstTrackElem; |
---|
| 145 | this->localTime = 0; |
---|
| 146 | this->maxTime = 0; |
---|
[3348] | 147 | this->trackElemCount = 1; |
---|
[3499] | 148 | this->bindSlave = NULL; |
---|
[3311] | 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | /** |
---|
[3331] | 153 | \brief standard destructor |
---|
[3311] | 154 | |
---|
| 155 | */ |
---|
[3354] | 156 | TrackManager::~TrackManager(void) |
---|
[3330] | 157 | { |
---|
[3331] | 158 | PRINTF(3)("Destruct TrackManager\n"); |
---|
[3311] | 159 | |
---|
[3331] | 160 | PRINTF(3)("Deleting all the TrackElements\n"); |
---|
| 161 | delete this->firstTrackElem; |
---|
[3530] | 162 | // delete this->name; |
---|
[3335] | 163 | |
---|
[3331] | 164 | // we do not have a TrackManager anymore |
---|
| 165 | singletonRef = NULL; |
---|
[3330] | 166 | } |
---|
| 167 | |
---|
[3331] | 168 | TrackManager* TrackManager::singletonRef = NULL; |
---|
| 169 | |
---|
[3330] | 170 | /** |
---|
[3331] | 171 | \returns The reference on the TrackManager. |
---|
| 172 | |
---|
| 173 | If the TrackManager does not exist, it will be created. |
---|
| 174 | */ |
---|
| 175 | TrackManager* TrackManager::getInstance(void) |
---|
| 176 | { |
---|
| 177 | if (singletonRef) |
---|
| 178 | return singletonRef; |
---|
| 179 | else |
---|
| 180 | return singletonRef = new TrackManager(); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
[3335] | 184 | \brief reserves Space for childCount children |
---|
| 185 | \param childCount The Count of children to make space for. |
---|
| 186 | */ |
---|
[3525] | 187 | void TrackManager::initChildren(unsigned int childCount, SubString* names) |
---|
[3335] | 188 | { |
---|
| 189 | this->currentTrackElem->childCount = childCount; |
---|
[3499] | 190 | this->currentTrackElem->mainJoin = true; |
---|
[3335] | 191 | this->currentTrackElem->children = new TrackElement*[childCount]; |
---|
| 192 | for (int i=0; i<childCount; i++) |
---|
[3348] | 193 | { |
---|
[3525] | 194 | if( names != NULL) |
---|
| 195 | { |
---|
| 196 | if( this->findTrackElementByName( names->getString(i))) |
---|
| 197 | { |
---|
[3530] | 198 | PRINTF(1)("Track name '%s' already taken", names->getString(i)); |
---|
[3525] | 199 | } |
---|
| 200 | } |
---|
| 201 | |
---|
[3348] | 202 | this->currentTrackElem->children[i] = new TrackElement(); |
---|
| 203 | this->currentTrackElem->children[i]->ID = ++trackElemCount; |
---|
[3499] | 204 | this->currentTrackElem->children[i]->startingTime = this->currentTrackElem->endTime + this->currentTrackElem->jumpTime; |
---|
| 205 | this->addPoint(this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount()), this->currentTrackElem->children[i]); |
---|
[3348] | 206 | } |
---|
[3335] | 207 | } |
---|
| 208 | |
---|
| 209 | /** |
---|
[3330] | 210 | \brief Searches for a given trackID. |
---|
| 211 | \param trackID the trackID to search for. |
---|
[3332] | 212 | \returns The TrackElement #trackID if found, NULL otherwise. |
---|
[3330] | 213 | */ |
---|
[3332] | 214 | TrackElement* TrackManager::findTrackElementByID(unsigned int trackID) const |
---|
[3330] | 215 | { |
---|
[3332] | 216 | return firstTrackElem->findByID(trackID); |
---|
[3330] | 217 | } |
---|
| 218 | |
---|
[3525] | 219 | /** |
---|
| 220 | \brief Searches for a given trackname. |
---|
| 221 | \param trackName the trackName to search for. |
---|
| 222 | \returns The TrackElement if found, NULL otherwise. |
---|
| 223 | */ |
---|
| 224 | TrackElement* TrackManager::findTrackElementByName(const char* trackName) const |
---|
| 225 | { |
---|
| 226 | return firstTrackElem->findByName(trackName); |
---|
| 227 | } |
---|
[3330] | 228 | // INITIALIZE // |
---|
| 229 | |
---|
| 230 | /** |
---|
| 231 | \brief Sets the trackID we are working on. |
---|
| 232 | \param trackID the trackID we are working on |
---|
| 233 | */ |
---|
[3332] | 234 | void TrackManager::workOn(unsigned int trackID) |
---|
[3330] | 235 | { |
---|
[3355] | 236 | TrackElement* tmpElem = findTrackElementByID(trackID); |
---|
| 237 | if (tmpElem) |
---|
| 238 | this->currentTrackElem = tmpElem; |
---|
| 239 | else |
---|
| 240 | printf("TrackElement not Found, leaving unchanged\n"); |
---|
| 241 | printf("now Working on %d\n", this->currentTrackElem->ID); |
---|
| 242 | |
---|
[3330] | 243 | } |
---|
| 244 | |
---|
| 245 | /** |
---|
| 246 | \brief Sets the Type of the Curve |
---|
| 247 | \brief curveType The Type to set |
---|
| 248 | */ |
---|
[3499] | 249 | void TrackManager::setCurveType(CurveType curveType, TrackElement* trackElem) |
---|
[3330] | 250 | { |
---|
[3499] | 251 | if (!trackElem->isFresh) |
---|
[3332] | 252 | { |
---|
| 253 | PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n"); |
---|
| 254 | return; |
---|
| 255 | } |
---|
[3499] | 256 | trackElem->curveType = curveType; |
---|
[3332] | 257 | switch (curveType) |
---|
| 258 | { |
---|
| 259 | case BEZIERCURVE: |
---|
[3499] | 260 | trackElem->curve = new BezierCurve(); |
---|
[3332] | 261 | break; |
---|
| 262 | case UPOINTCURVE: |
---|
[3499] | 263 | trackElem->curve = new UPointCurve(); |
---|
[3332] | 264 | break; |
---|
| 265 | } |
---|
[3330] | 266 | } |
---|
| 267 | |
---|
| 268 | /** |
---|
[3333] | 269 | \brief Sets the duration of the current path in seconds. |
---|
| 270 | \param time The duration in seconds. |
---|
[3330] | 271 | */ |
---|
| 272 | |
---|
[3333] | 273 | void TrackManager::setDuration(float time) |
---|
[3330] | 274 | { |
---|
[3333] | 275 | this->currentTrackElem->duration = time; |
---|
[3499] | 276 | this->currentTrackElem->endTime = this->currentTrackElem->startingTime + time; |
---|
[3330] | 277 | } |
---|
| 278 | |
---|
| 279 | /** |
---|
| 280 | \brief adds a point to the current TrackElement |
---|
| 281 | \param newPoint The point to add. |
---|
| 282 | */ |
---|
[3333] | 283 | bool TrackManager::addPoint(Vector newPoint) |
---|
[3330] | 284 | { |
---|
[3352] | 285 | return this->addPoint(newPoint, this->currentTrackElem); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | /** |
---|
| 289 | \brief adds a point to trackElem |
---|
| 290 | \param newPoint The point to add. |
---|
| 291 | \param trackElem The TrackElement to add the Point to |
---|
| 292 | */ |
---|
| 293 | bool TrackManager::addPoint(Vector newPoint, TrackElement* trackElem) |
---|
| 294 | { |
---|
| 295 | if (trackElem->isFresh) |
---|
[3332] | 296 | { |
---|
[3499] | 297 | this->setCurveType(BEZIERCURVE, trackElem); |
---|
[3352] | 298 | trackElem->isFresh = false; |
---|
[3332] | 299 | } |
---|
[3352] | 300 | trackElem->curve->addNode(newPoint); |
---|
| 301 | trackElem->nodeCount++; |
---|
[3330] | 302 | } |
---|
| 303 | |
---|
| 304 | /** |
---|
| 305 | \brief adds save/splitpoint. |
---|
| 306 | \param newPoint The point to add. |
---|
[3333] | 307 | \returns A Pointer to a newly appended Curve |
---|
[3330] | 308 | */ |
---|
[3333] | 309 | int TrackManager::addHotPoint(Vector newPoint) |
---|
[3330] | 310 | { |
---|
[3354] | 311 | printf("setting up a HotPoint\n"); |
---|
[3332] | 312 | if (this->currentTrackElem->isFresh) |
---|
| 313 | { |
---|
| 314 | this->setCurveType(BEZIERCURVE); |
---|
| 315 | this->currentTrackElem->isFresh = false; |
---|
| 316 | } |
---|
[3330] | 317 | |
---|
[3332] | 318 | // \todo HotPoint Handling. |
---|
| 319 | this->currentTrackElem->curve->addNode(newPoint); |
---|
[3351] | 320 | this->currentTrackElem->nodeCount++; |
---|
[3354] | 321 | this->initChildren(1); |
---|
| 322 | this->currentTrackElem = this->currentTrackElem->children[0]; |
---|
[3330] | 323 | } |
---|
| 324 | |
---|
| 325 | /** |
---|
| 326 | \brief Sets the last HotPoint into a savePoint. |
---|
[3333] | 327 | \returns A Pointer to a newly appended Curve |
---|
[3330] | 328 | |
---|
| 329 | If no HotPoint was defined the last added Point will be rendered into a savePoint. \n |
---|
| 330 | If the HotPoint was defined as a fork the Point will \b not be set into a savePoint. |
---|
| 331 | */ |
---|
[3333] | 332 | int TrackManager::setSavePoint(void) |
---|
[3330] | 333 | { |
---|
[3354] | 334 | printf("setting up a SavePoint.\n"); |
---|
[3332] | 335 | if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint) |
---|
[3333] | 336 | return this->currentTrackElem->children[1]->ID; |
---|
[3332] | 337 | this->currentTrackElem->isSavePoint = true; |
---|
[3354] | 338 | this->currentTrackElem->isHotPoint = true; |
---|
[3332] | 339 | |
---|
[3335] | 340 | this->initChildren(1); |
---|
[3348] | 341 | this->currentTrackElem = this->currentTrackElem->children[0]; |
---|
[3330] | 342 | } |
---|
| 343 | |
---|
| 344 | /** |
---|
| 345 | \brief adds some interessting non-linear movments through the level. |
---|
| 346 | \param count The Count of childrens the current HotPoint will have. |
---|
| 347 | |
---|
| 348 | If no HotPoint was defined the last added Point will be rendered into a fork. \n |
---|
| 349 | If the HotPoint was defined as a savePoint the Point will \b not be set into a fork. |
---|
| 350 | */ |
---|
[3332] | 351 | void TrackManager::fork(unsigned int count, ...) |
---|
[3330] | 352 | { |
---|
[3351] | 353 | int* trackIDs = new int[count]; |
---|
| 354 | this->forkV(count, trackIDs); |
---|
[3332] | 355 | va_list ID; |
---|
| 356 | va_start (ID, count); |
---|
| 357 | for(int i = 0; i < count; i++) |
---|
| 358 | { |
---|
[3351] | 359 | *va_arg (ID, int*) = trackIDs[i]; |
---|
[3332] | 360 | } |
---|
[3351] | 361 | va_end(ID); |
---|
[3332] | 362 | delete []trackIDs; |
---|
[3330] | 363 | } |
---|
| 364 | |
---|
| 365 | /** |
---|
| 366 | \brief adds some interessting non-linear movments through the level. |
---|
| 367 | \param count The Count of childrens the current HotPoint will have. |
---|
| 368 | \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this). |
---|
[3525] | 369 | \param names A SubString containing the trackNames for the new tracks, if NULL, no names will be assigned |
---|
[3330] | 370 | |
---|
| 371 | \see void TrackManager::fork(int count, ...) |
---|
[3332] | 372 | |
---|
| 373 | \todo initialisation is wrong!! also in setSavePoint. |
---|
[3330] | 374 | */ |
---|
[3525] | 375 | void TrackManager::forkV(unsigned int count, int* trackIDs, SubString* names) |
---|
[3330] | 376 | { |
---|
[3354] | 377 | printf("Forking with %d children\n", count); |
---|
[3332] | 378 | if (this->currentTrackElem->isSavePoint) |
---|
| 379 | return; |
---|
| 380 | this->currentTrackElem->isFork = true; |
---|
[3354] | 381 | this->currentTrackElem->isHotPoint = true; |
---|
[3351] | 382 | for(int i = 0; i < count; i++) |
---|
| 383 | trackIDs[i]=this->trackElemCount+1+i; |
---|
[3525] | 384 | this->initChildren(count, names); |
---|
[3330] | 385 | } |
---|
| 386 | |
---|
| 387 | /** |
---|
| 388 | \brief decides under what condition a certain Path will be chosen. |
---|
| 389 | \param groupID the ID on which to choose the preceding move |
---|
| 390 | \param cond \todo think about this |
---|
| 391 | */ |
---|
[3332] | 392 | void TrackManager::condition(unsigned int groupID, PathCondition cond) |
---|
[3330] | 393 | { |
---|
[3332] | 394 | |
---|
[3330] | 395 | } |
---|
| 396 | |
---|
| 397 | /** |
---|
| 398 | \brief joins some tracks together again. |
---|
| 399 | \param count The count of Paths to join. |
---|
| 400 | |
---|
| 401 | Join will set the localTime to the longest time a Path has to get to this Point. \n |
---|
[3354] | 402 | Join will join all curves to the first curve, meaning that all the tangents will be matched. |
---|
[3330] | 403 | */ |
---|
[3332] | 404 | void TrackManager::join(unsigned int count, ...) |
---|
[3330] | 405 | { |
---|
[3332] | 406 | int* trackIDs = new int [count]; |
---|
| 407 | va_list ID; |
---|
| 408 | va_start (ID, count); |
---|
| 409 | for(int i = 0; i < count; i++) |
---|
| 410 | { |
---|
| 411 | trackIDs[i] = va_arg (ID, int); |
---|
| 412 | } |
---|
| 413 | va_end(ID); |
---|
| 414 | this->joinV(count, trackIDs); |
---|
| 415 | delete []trackIDs; |
---|
[3330] | 416 | } |
---|
| 417 | |
---|
| 418 | /** |
---|
| 419 | \brief joins some tracks together again. |
---|
| 420 | \param count The count of Paths to join. |
---|
| 421 | \param trackIDs an Array with the trackID's to join |
---|
| 422 | |
---|
| 423 | \see void TrackManager::join(int count, ...) |
---|
| 424 | */ |
---|
[3332] | 425 | void TrackManager::joinV(unsigned int count, int* trackIDs) |
---|
[3330] | 426 | { |
---|
[3499] | 427 | printf("Joining %d tracks and merging to Track %d\n", count, trackIDs[0]); |
---|
[3354] | 428 | |
---|
| 429 | // chanching work-on to temporary value. going back at the end. |
---|
| 430 | int tmpCurrentWorkingID = this->currentTrackElem->ID; |
---|
| 431 | this->workOn(trackIDs[0]); |
---|
[3499] | 432 | TrackElement* firstJoint = this->currentTrackElem; |
---|
| 433 | float tmpLatestTime = firstJoint->endTime; |
---|
[3354] | 434 | |
---|
| 435 | Vector tmpEndPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()); |
---|
[3499] | 436 | Vector tmpTangentPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-1); |
---|
| 437 | Vector tmpc2Point = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-2); |
---|
[3354] | 438 | firstJoint->isJoined = true; |
---|
[3499] | 439 | // firstJoint->mainJoin = true; |
---|
[3354] | 440 | if(!firstJoint->isHotPoint) |
---|
| 441 | this->setSavePoint(); |
---|
[3499] | 442 | // Timing: |
---|
| 443 | for (int i = 0; i < count; i++) |
---|
| 444 | { |
---|
| 445 | TrackElement* tmpJoinElem = this->findTrackElementByID(trackIDs[i]); |
---|
| 446 | if (tmpJoinElem->childCount == 0 |
---|
| 447 | && tmpJoinElem->endTime > tmpLatestTime) |
---|
| 448 | tmpLatestTime = tmpJoinElem->endTime; |
---|
| 449 | } |
---|
| 450 | // time the main Join. |
---|
| 451 | firstJoint->jumpTime = tmpLatestTime - firstJoint->endTime; |
---|
| 452 | |
---|
| 453 | // Joining: |
---|
[3354] | 454 | for (int i = 1; i < count; i++) |
---|
[3352] | 455 | { |
---|
| 456 | TrackElement* tmpJoinElem = this->findTrackElementByID(trackIDs[i]); |
---|
[3354] | 457 | if (tmpJoinElem->childCount > 0) |
---|
| 458 | printf("!!This Curve has children, and as such will not be joined!!\n You can try joining other childless TrackElements to this one!"); |
---|
| 459 | else |
---|
| 460 | { |
---|
[3499] | 461 | this->addPoint(tmpc2Point, tmpJoinElem); |
---|
[3354] | 462 | this->addPoint(tmpTangentPoint, tmpJoinElem); |
---|
| 463 | this->addPoint(tmpEndPoint, tmpJoinElem); |
---|
[3499] | 464 | // time all other Joins |
---|
| 465 | tmpJoinElem->jumpTime = tmpLatestTime - tmpJoinElem->endTime; |
---|
[3354] | 466 | |
---|
| 467 | //Copying Joint-Info |
---|
| 468 | tmpJoinElem->children = firstJoint->children; |
---|
| 469 | tmpJoinElem->childCount = firstJoint->childCount; |
---|
| 470 | tmpJoinElem->isSavePoint = firstJoint->isSavePoint; |
---|
| 471 | tmpJoinElem->isFork = firstJoint->isFork; |
---|
[3499] | 472 | |
---|
[3354] | 473 | tmpJoinElem->isJoined = true; |
---|
| 474 | } |
---|
[3352] | 475 | } |
---|
[3499] | 476 | if(firstJoint->childCount > 0) |
---|
| 477 | for(int i = 0; i < firstJoint->childCount; i++) |
---|
| 478 | { |
---|
| 479 | printf("Setting startingTime of %d to %f.\n", firstJoint->children[i]->ID, tmpLatestTime); |
---|
| 480 | firstJoint->children[i]->startingTime = tmpLatestTime; |
---|
| 481 | firstJoint->children[i]->endTime = tmpLatestTime+firstJoint->children[i]->duration; |
---|
| 482 | } |
---|
| 483 | |
---|
[3354] | 484 | // returning to the TrackElement we were working on. |
---|
| 485 | this->workOn(tmpCurrentWorkingID); |
---|
[3330] | 486 | } |
---|
| 487 | |
---|
[3499] | 488 | /** |
---|
| 489 | \brief finalizes the TrackSystem. after this it will not be editable anymore |
---|
| 490 | |
---|
| 491 | \todo check for any inconsistencies, output errors |
---|
| 492 | */ |
---|
| 493 | void TrackManager::finalize(void) |
---|
| 494 | { |
---|
| 495 | for (int i = 1; i<= trackElemCount ;i++) |
---|
| 496 | { |
---|
| 497 | TrackElement* tmpElem = findTrackElementByID(i); |
---|
| 498 | if (tmpElem->childCount>0 && tmpElem->mainJoin) |
---|
| 499 | { |
---|
| 500 | for (int j = 0; j < tmpElem->childCount; j++) |
---|
| 501 | { |
---|
| 502 | |
---|
| 503 | // c1-continuity |
---|
| 504 | tmpElem->children[j]->curve->addNode(tmpElem->children[j]->curve->getNode(0) + |
---|
| 505 | ((tmpElem->children[j]->curve->getNode(0) - |
---|
| 506 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) |
---|
| 507 | ),2); |
---|
| 508 | tmpElem->children[j]->nodeCount++; |
---|
| 509 | // c2-continuity |
---|
| 510 | tmpElem->children[j]->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())- |
---|
| 511 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 + |
---|
| 512 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3); |
---|
| 513 | tmpElem->children[j]->nodeCount++; |
---|
| 514 | printf("accelerations: %d-in: count: %d, %f, %f, %f\n %d-out: count: %d %f, %f, %f\n", |
---|
| 515 | tmpElem->ID, tmpElem->nodeCount, |
---|
| 516 | tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z, |
---|
| 517 | tmpElem->children[j]->ID, tmpElem->children[j]->nodeCount, |
---|
| 518 | tmpElem->children[j]->curve->calcAcc(0).x, tmpElem->children[j]->curve->calcAcc(0).y, tmpElem->children[j]->curve->calcAcc(0).z); |
---|
| 519 | } |
---|
| 520 | } |
---|
| 521 | } |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | |
---|
[3330] | 525 | // RUNTIME // |
---|
| 526 | |
---|
| 527 | /** |
---|
| 528 | \brief calculates the Position for the localTime of the Track. |
---|
| 529 | \returns the calculated Position |
---|
| 530 | */ |
---|
[3332] | 531 | Vector TrackManager::calcPos() const |
---|
[3330] | 532 | { |
---|
[3348] | 533 | // PRINTF(0)("TrackElement:%d, localTime: %f\n",this->currentTrackElem->ID, this->localTime); |
---|
| 534 | return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
[3330] | 535 | } |
---|
| 536 | |
---|
| 537 | /** |
---|
| 538 | \brief calculates the Rotation for the localTime of the Track. |
---|
| 539 | \returns the calculated Rotation |
---|
| 540 | */ |
---|
[3332] | 541 | Vector TrackManager::calcDir() const |
---|
[3330] | 542 | { |
---|
[3499] | 543 | return this->currentTrackElem->curve->calcDir((this->localTime - this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
[3330] | 544 | } |
---|
| 545 | |
---|
| 546 | /** |
---|
| 547 | \brief Advances the local-time of the Track around dt |
---|
| 548 | \param dt The time about which to advance. |
---|
[3333] | 549 | |
---|
| 550 | This function also checks, if the TrackElement has to be changed. |
---|
[3330] | 551 | */ |
---|
| 552 | void TrackManager::tick(float dt) |
---|
| 553 | { |
---|
[3499] | 554 | dt /= 1000; |
---|
| 555 | printf("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt); |
---|
[3348] | 556 | if (this->localTime <= this->firstTrackElem->duration) |
---|
| 557 | this->jumpTo(this->localTime); |
---|
[3332] | 558 | this->localTime += dt; |
---|
[3499] | 559 | if (this->localTime > this->currentTrackElem->endTime |
---|
| 560 | && this->currentTrackElem->children) |
---|
| 561 | { |
---|
| 562 | if (this->currentTrackElem->jumpTime > 0) |
---|
| 563 | this->jumpTo(this->localTime + this->currentTrackElem->jumpTime); |
---|
| 564 | this->currentTrackElem = this->currentTrackElem->children[0]; |
---|
| 565 | } |
---|
| 566 | if (this->bindSlave) |
---|
| 567 | { |
---|
| 568 | Vector tmp = this->calcPos(); |
---|
| 569 | 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)); |
---|
| 570 | this->bindSlave->setAbsCoor(&tmp); |
---|
| 571 | this->bindSlave->setAbsDir(&quat); |
---|
| 572 | } |
---|
[3330] | 573 | } |
---|
| 574 | |
---|
| 575 | /** |
---|
[3331] | 576 | \brief Jumps to a certain point on the Track. |
---|
| 577 | \param time The time on the Track to jump to. |
---|
| 578 | |
---|
| 579 | 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.) |
---|
| 580 | Max is trackLengthMax. |
---|
| 581 | */ |
---|
| 582 | void TrackManager::jumpTo(float time) |
---|
| 583 | { |
---|
[3348] | 584 | if (time == 0) |
---|
| 585 | this->currentTrackElem = this->firstTrackElem; |
---|
| 586 | this->localTime = time; |
---|
[3331] | 587 | } |
---|
| 588 | |
---|
| 589 | /** |
---|
[3330] | 590 | \brief a Function that decides which Path we should follow. |
---|
| 591 | \param graphID The Path to choose. |
---|
| 592 | |
---|
| 593 | */ |
---|
| 594 | void TrackManager::choosePath(int graphID) |
---|
| 595 | { |
---|
| 596 | |
---|
| 597 | } |
---|
| 598 | |
---|
[3499] | 599 | /** |
---|
| 600 | \brief Sets the PNode, that should be moved along the Tack |
---|
| 601 | \param bindSlave the PNode to set |
---|
| 602 | */ |
---|
| 603 | void TrackManager::setBindSlave(PNode* bindSlave) |
---|
| 604 | { |
---|
| 605 | if (!this->bindSlave) |
---|
| 606 | this->bindSlave = bindSlave; |
---|
| 607 | } |
---|
[3350] | 608 | |
---|
| 609 | |
---|
| 610 | // DEBUG // |
---|
| 611 | |
---|
| 612 | /** |
---|
| 613 | \brief Imports a model of the Graph into the OpenGL-environment. |
---|
| 614 | \param dt The Iterator used in seconds for Painting the Graph. |
---|
| 615 | |
---|
| 616 | This is for testing facility only. Do this if you want to see the Path inside the Level. |
---|
| 617 | eventually this will all be packed into a gl-list. |
---|
| 618 | */ |
---|
| 619 | void TrackManager::drawGraph(float dt) const |
---|
| 620 | { |
---|
[3352] | 621 | |
---|
[3350] | 622 | for (int i = 1; i <= trackElemCount; i++) |
---|
| 623 | { |
---|
[3352] | 624 | glBegin(GL_LINE_STRIP); |
---|
[3350] | 625 | TrackElement* tmpElem = this->findTrackElementByID(i); |
---|
| 626 | if (tmpElem->curve) |
---|
| 627 | for(float f = 0.0; f < 1.0; f+=dt) |
---|
| 628 | { |
---|
| 629 | // printf("%f, %f, %f\n",trackManager->calcPos().x, trackManager->calcPos().y, trackManager->calcPos().z); |
---|
| 630 | Vector tmpVector = tmpElem->curve->calcPos(f); |
---|
| 631 | glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z); |
---|
| 632 | } |
---|
[3352] | 633 | glEnd(); |
---|
[3350] | 634 | } |
---|
| 635 | } |
---|
| 636 | |
---|
[3499] | 637 | /** |
---|
| 638 | \brief outputs debug information about the trackManager |
---|
| 639 | \param level how much debug |
---|
| 640 | */ |
---|
[3350] | 641 | void TrackManager::debug(unsigned int level) const |
---|
| 642 | { |
---|
| 643 | printf("::CLASS TRACKMANAGER::debug information::\n"); |
---|
| 644 | // printf("Status is: % |
---|
| 645 | printf(" Consists of %d elements\n", this->trackElemCount); |
---|
| 646 | printf(" localTime is: %f\n", this->localTime); |
---|
| 647 | if (level >= 2) |
---|
| 648 | { |
---|
| 649 | for (int i = 1; i <= trackElemCount; i++) |
---|
| 650 | { |
---|
| 651 | TrackElement* tmpElem = this->findTrackElementByID(i); |
---|
| 652 | printf(" ::TrackElement:%i::", tmpElem->ID); |
---|
| 653 | if(tmpElem->name) |
---|
| 654 | printf("name:%s::", tmpElem->name); |
---|
[3354] | 655 | if(tmpElem->isFresh) |
---|
| 656 | printf(" has not jet eddited in any way\n"); |
---|
[3499] | 657 | printf("\n TimeTable: startingTime=%f; endTime=%f; duration=%f; jumpTime=%f\n", tmpElem->startingTime, tmpElem->endTime, tmpElem->duration, tmpElem->jumpTime); |
---|
[3350] | 658 | printf(" consists of %d Points\n", tmpElem->nodeCount); |
---|
[3354] | 659 | if (tmpElem->childCount == 0) |
---|
| 660 | printf(" has no child\n"); |
---|
| 661 | else if (tmpElem->childCount == 1) |
---|
| 662 | printf(" has 1 child: ==%d==\n", tmpElem->children[0]->ID); |
---|
| 663 | else if (tmpElem->childCount > 1) |
---|
[3351] | 664 | { |
---|
[3354] | 665 | printf(" has %d children: ", tmpElem->childCount); |
---|
[3351] | 666 | for(int i = 0; i < tmpElem->childCount; i++) |
---|
| 667 | printf("=%d= ", tmpElem->children[i]->ID); |
---|
| 668 | printf("\n"); |
---|
| 669 | } |
---|
[3354] | 670 | |
---|
| 671 | if(tmpElem->isHotPoint) |
---|
| 672 | printf(" is a special Point:\n"); |
---|
| 673 | if(tmpElem->isSavePoint) |
---|
| 674 | printf(" is a SavePoint\n"); |
---|
| 675 | if(tmpElem->isFork) |
---|
| 676 | { |
---|
[3499] | 677 | printf(" is A Fork with with %d children.\n", tmpElem->childCount); |
---|
[3354] | 678 | } |
---|
[3350] | 679 | if(tmpElem->isJoined) |
---|
| 680 | printf(" is Joined at the End\n"); |
---|
| 681 | } |
---|
| 682 | } |
---|
| 683 | } |
---|
[3523] | 684 | |
---|
| 685 | /** |
---|
| 686 | \brief loads track data from a XML Element |
---|
| 687 | \param root the element containing all track data |
---|
| 688 | */ |
---|
[3525] | 689 | void TrackManager::loadTrack( TiXmlElement* root) |
---|
[3523] | 690 | { |
---|
| 691 | assert( root != NULL); |
---|
| 692 | |
---|
[3525] | 693 | TiXmlElement* element; |
---|
| 694 | TiXmlNode* container; |
---|
[3523] | 695 | double x, y, z, d; |
---|
| 696 | |
---|
| 697 | element = root->FirstChildElement(); |
---|
| 698 | |
---|
| 699 | while( element != NULL) |
---|
| 700 | { |
---|
[3530] | 701 | if( !strcmp( element->Value(), "Point")) |
---|
[3523] | 702 | { |
---|
| 703 | container = element->FirstChild(); |
---|
[3530] | 704 | if( container->ToText()) |
---|
[3523] | 705 | { |
---|
| 706 | assert( container->Value() != NULL); |
---|
| 707 | if( sscanf( container->Value(), "%f,%f,%f", &x, &y, &z) == 3) |
---|
| 708 | addPoint( Vector( x, y, z)); |
---|
| 709 | else |
---|
| 710 | { |
---|
[3530] | 711 | PRINTF(1)("Invalid Point in Track (skipped)\n"); |
---|
[3523] | 712 | } |
---|
| 713 | } |
---|
| 714 | } |
---|
[3530] | 715 | else if( !strcmp( element->Value(), "Duration")) |
---|
[3523] | 716 | { |
---|
| 717 | container = element->FirstChild(); |
---|
[3530] | 718 | if( container->ToText()) |
---|
[3523] | 719 | { |
---|
| 720 | assert( container->Value() != NULL); |
---|
| 721 | if( sscanf( container->Value(), "%f", &d) == 1) |
---|
| 722 | setDuration( d); |
---|
| 723 | else |
---|
| 724 | { |
---|
[3530] | 725 | PRINTF(1)("Invalid Duration in Track (skipped)\n"); |
---|
[3523] | 726 | } |
---|
| 727 | } |
---|
| 728 | } |
---|
[3530] | 729 | else if( !strcmp( element->Value(), "SavePoint")) |
---|
[3523] | 730 | { |
---|
| 731 | setSavePoint(); |
---|
| 732 | } |
---|
[3530] | 733 | else if( !strcmp( element->Value(), "Fork")) |
---|
[3523] | 734 | { |
---|
| 735 | container = element->FirstChild(); |
---|
[3530] | 736 | if( container->ToText()) |
---|
[3523] | 737 | { |
---|
| 738 | assert( container->Value() != NULL); |
---|
| 739 | forkS( container->Value()); |
---|
| 740 | } |
---|
| 741 | } |
---|
[3530] | 742 | else if( !strcmp( element->Value(), "Join")) |
---|
[3523] | 743 | { |
---|
| 744 | container = element->FirstChild(); |
---|
[3530] | 745 | if( container->ToText()) |
---|
[3523] | 746 | { |
---|
| 747 | assert( container->Value() != NULL); |
---|
| 748 | joinS( container->Value()); |
---|
| 749 | } |
---|
| 750 | } |
---|
[3530] | 751 | else if( !strcmp( element->Value(), "WorkOn")) |
---|
[3523] | 752 | { |
---|
| 753 | container = element->FirstChild(); |
---|
[3530] | 754 | if( container->ToText()) |
---|
[3523] | 755 | { |
---|
| 756 | assert( container->Value() != NULL); |
---|
| 757 | workOnS( container->Value()); |
---|
| 758 | } |
---|
| 759 | } |
---|
| 760 | |
---|
| 761 | element = element->NextSiblingElement(); |
---|
| 762 | } |
---|
| 763 | } |
---|
| 764 | |
---|
| 765 | /** |
---|
| 766 | \brief forks the current track and names the resulting tracks |
---|
| 767 | \param string the names of the new tracks, separated by commas |
---|
| 768 | |
---|
| 769 | The names used have to be unique within a particular track system. |
---|
| 770 | */ |
---|
[3530] | 771 | void TrackManager::forkS( const char* string) |
---|
[3523] | 772 | { |
---|
[3525] | 773 | // get the names for the tracks and therefore the amount of new tracks |
---|
[3530] | 774 | SubString* parts = new SubString( string); |
---|
[3523] | 775 | int *IDs; |
---|
| 776 | int n = parts->getN(); |
---|
| 777 | |
---|
| 778 | assert( n != 0); |
---|
| 779 | |
---|
| 780 | IDs = new int[n]; |
---|
| 781 | |
---|
[3530] | 782 | forkV( n, IDs, parts); |
---|
[3523] | 783 | |
---|
[3525] | 784 | // IDs are irrelevant when assigning string names to tracks |
---|
[3523] | 785 | delete IDs; |
---|
[3530] | 786 | delete parts; |
---|
[3523] | 787 | } |
---|
| 788 | |
---|
| 789 | /** |
---|
| 790 | \brief joins the named strings |
---|
| 791 | \param string the names of the track to be joined, separated by commas |
---|
| 792 | */ |
---|
[3530] | 793 | void TrackManager::joinS( const char* string) |
---|
[3523] | 794 | { |
---|
| 795 | SubString* parts = new SubString( string); |
---|
| 796 | int *IDs; |
---|
| 797 | int n = parts->getN(); |
---|
| 798 | |
---|
| 799 | assert( n != 0); |
---|
| 800 | |
---|
| 801 | IDs = new int[n]; |
---|
| 802 | |
---|
| 803 | int d, t; |
---|
| 804 | d = 0; |
---|
| 805 | t = n; |
---|
| 806 | |
---|
[3530] | 807 | PRINTF(4)("Joining tracks:"); |
---|
[3523] | 808 | |
---|
| 809 | for( int i = 0; i < n; i++) |
---|
| 810 | { |
---|
[3525] | 811 | TrackElement* element = this->findTrackElementByName(parts->getString( i)); |
---|
| 812 | if( element == NULL) |
---|
[3523] | 813 | { |
---|
[3530] | 814 | PRINTF(1)("Track name '%s' unknown, could not join\n", parts->getString( i)); |
---|
[3523] | 815 | t--; |
---|
| 816 | } |
---|
| 817 | else |
---|
| 818 | { |
---|
[3530] | 819 | PRINTF(4)(" '%s'(%d)", parts->getString( i), element->ID); |
---|
[3525] | 820 | IDs[d] = element->ID; |
---|
[3523] | 821 | d++; |
---|
| 822 | } |
---|
| 823 | } |
---|
| 824 | |
---|
[3530] | 825 | PRINTF(4)("\n"); |
---|
[3523] | 826 | |
---|
| 827 | joinV( t, IDs); |
---|
| 828 | |
---|
| 829 | delete IDs; |
---|
| 830 | delete parts; |
---|
| 831 | } |
---|
| 832 | |
---|
| 833 | /** |
---|
| 834 | \brief set the piece of track to work on |
---|
| 835 | \param string the name of the track to work on (must have been previosly set by forkS) |
---|
| 836 | */ |
---|
[3530] | 837 | void TrackManager::workOnS( const char* string) |
---|
[3523] | 838 | { |
---|
[3525] | 839 | TrackElement* tmpElem = findTrackElementByName( string); |
---|
| 840 | if (tmpElem) |
---|
| 841 | this->currentTrackElem = tmpElem; |
---|
| 842 | else |
---|
[3530] | 843 | PRINTF(1)("TrackElement not Found, leaving unchanged\n"); |
---|
| 844 | PRINTF(4)("now Working on %d\n", this->currentTrackElem->ID); |
---|
[3523] | 845 | } |
---|