Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/cleanup/src/util/track/track_manager.cc @ 10571

Last change on this file since 10571 was 10571, checked in by bensch, 17 years ago

cleaned out unused defs files, and moved glincl to grafics, alincl.h to sound

File size: 36.1 KB
RevLine 
[4584]1/*
[3311]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
[3591]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_TRACK_MANAGER
[3311]17
18#include "track_manager.h"
[3495]19
[3608]20#include "base_object.h"
[7193]21#include "util/loading/load_param.h"
[3433]22#include "p_node.h"
[3528]23#include "track_node.h"
[5344]24#include "text.h"
[3849]25#include "t_animation.h"
[4496]26
[10368]27#include <string.h>
[4496]28
[10368]29
[5944]30#include "parser/tinyxml/tinyxml.h"
[4220]31#include "substring.h"
[3528]32
[3495]33#include <stdarg.h>
34
[10368]35ObjectListDefinition(TrackElement);
[3311]36
[9406]37
[3331]38/**
[4836]39 *  initializes a TrackElement (sets the default values)
[3331]40*/
[4746]41TrackElement::TrackElement()
[3331]42{
[10368]43  this->registerObject(this, TrackElement::_objectList);
[4597]44
[10368]45
[3332]46  this->isFresh = true;
[3354]47  this->isHotPoint = false;
[3331]48  this->isSavePoint = false;
49  this->isFork = false;
50  this->isJoined = false;
[3356]51  this->mainJoin = false;
[3331]52  this->ID = -1;
[3588]53  this->startingTime = 0;
54  this->duration = TMAN_DEFAULT_DURATION;
[3433]55  this->endTime = 1;
56  this->jumpTime = 0;
[3596]57  this->width = TMAN_DEFAULT_WIDTH;
[3331]58  this->nodeCount = 0;
[3835]59  this->curve = NULL;
[3348]60  this->childCount = 0;
[3835]61  this->children = NULL;
[3527]62
63  this->history = NULL;
64
[3835]65  this->subject = NULL;
[3522]66  this->condFunc = &TrackElement::random;
[3331]67}
[3311]68
[4584]69/**
[4836]70  *  destroys all alocated memory)
71    @todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements
[3331]72*/
[4746]73TrackElement::~TrackElement()
[3331]74{
[3835]75  // deleting the Curve
76  delete this->curve;
77
78  // deleting all the Children of this TrackNode.
[4584]79  if ((!this->isJoined &&this->childCount > 0)
[3835]80      || (this->isJoined && this->mainJoin)) // only if this is the MainJoin.
[3331]81    {
[3661]82      tIterator<TrackElement>* iterator = this->children->getIterator();
[5115]83      TrackElement* enumElem = iterator->firstElement();
[3594]84      while (enumElem)
[4584]85        {
86          delete enumElem;
87          enumElem = iterator->nextElement();
88        }
[3661]89      delete iterator;
[3356]90      delete this->children;
[3331]91    }
92}
93
[3332]94/**
[5336]95 * Searches through all the TrackElements for trackID.
[4836]96 * @param trackID The ID to search for.
97 * @returns The TrackElement if Found, NULL otherwise.
[3332]98*/
99TrackElement* TrackElement::findByID(unsigned int trackID)
100{
101  // return if Found.
102  if (this->ID == trackID)
103    return this;
[3835]104  // search all children
[3332]105  if (this->childCount > 0)
[3594]106    {
[3661]107      tIterator<TrackElement>* iterator = this->children->getIterator();
[5115]108      TrackElement* enumElem = iterator->firstElement();
[5313]109      TrackElement* tmpElem = NULL;
[3594]110      while (enumElem)
[4584]111        {
[5313]112          if ((tmpElem = enumElem->findByID(trackID)) != NULL)
[4584]113            return tmpElem;
114          enumElem = iterator->nextElement();
115        }
[3661]116      delete iterator;
[3594]117    }
[3835]118  // if not found
[3882]119  return NULL;
[3332]120}
[3331]121
122
[3522]123/**
[4836]124 *  Searches through all the TrackElements for a trackName
125 * @param trackName The name to search for.
126 * @returns The TrackElement if Found, NULL otherwise.
[3522]127*/
[7221]128TrackElement* TrackElement::findByName(const std::string& trackName)
[3522]129{
[3835]130  // return if Found.
[10368]131//   if (this->getName() && !strcmp(this->getName(), trackName))
132//     return this;
[3835]133  // search all children
134  if (this->childCount > 0)
[3522]135    {
[3661]136      tIterator<TrackElement>* iterator = this->children->getIterator();
[5115]137      TrackElement* enumElem = iterator->firstElement();
[3835]138      TrackElement* tmpElem;
[3594]139      while (enumElem)
[4584]140        {
141          if ((tmpElem = enumElem->findByName(trackName)))
142            return tmpElem;
143          enumElem = iterator->nextElement();
144        }
[3661]145      delete iterator;
[3522]146    }
[3835]147  // if not found
[3882]148  return NULL;
[3522]149}
[3331]150
[3522]151/**
[4836]152 *  checks if there are any BackLoops in the Track
153 * @returns true if NO loop was found, false Otherwise
[3835]154   You actually have to act on false!!
[4508]155*/
[4746]156bool TrackElement::backLoopCheck() const
[4508]157{
158  tList<const TrackElement>* trackList = new tList<const TrackElement>;
[4584]159
[4508]160  this->backLoopCheckAtomic(trackList);
[4584]161
[4508]162  delete trackList;
163  // only returns if everything worked out
164  return true;
165}
[4489]166
[4508]167/**
[4836]168 *  checks if there are any BackLoops in the Track.
169 * @param trackList A list of stored tracks, to search in.
170 * @returns true if NO loop was found, false Otherwise
[4508]171   You actually have to act on false!!
[3835]172*/
[4508]173bool TrackElement::backLoopCheckAtomic(tList<const TrackElement>* trackList) const
[3835]174{
[4508]175  if (trackList->inList(this))
176    return false;
177
178  trackList->add(this);
179
180  if (this->children)
[3835]181    {
[4508]182      tIterator<TrackElement>* iterator = this->children->getIterator();
[5115]183      TrackElement* enumElem = iterator->firstElement();
[4508]184      while (enumElem)
[4584]185        {
186          if (!enumElem->backLoopCheckAtomic(trackList))
187            return false;
188        }
[4508]189      delete iterator;
[3835]190    }
191  return true;
192}
193
[4508]194
[3835]195/**
[4836]196 * @param childCount which child to return
197 * @returns the n-the children (starting at 0).
[3835]198   Be aware, that when the trackElement has no Children, NULL will be returned
[3594]199*/
[5313]200TrackElement* TrackElement::getChild(unsigned int childCount) const
[3594]201{
[3835]202  // if the the trackElement has no children return NULL.
[3594]203  if (this->childCount == 0)
204    return NULL;
[3835]205  // we cannot return the childCount+m's Child, so we return the last.
[3594]206  if (childCount > this->childCount)
207    childCount = this->childCount;
[4584]208
[3832]209  tIterator<TrackElement>* iterator = this->children->getIterator();
[5115]210  TrackElement* enumElem = iterator->firstElement();
[5313]211  for (unsigned int i = 0; i < childCount; i++)
[3832]212    enumElem = iterator->nextElement();
213  delete iterator;
[3594]214  return enumElem;
215}
216
217/**
[4836]218 *  prints out debug information about this TrackElement
[3593]219*/
[4746]220void TrackElement::debug() const
[3593]221{
[10368]222//   PRINT(0)("--== TrackElement:%i ==--", this->ID);
223//   if(this->getName())
224//     PRINT(0)("--++Name: %s++--", this->getName());
225//   if(this->isFresh)
226//     PRINT(0)("  -- has not jet eddited in any way --\n");
227//   PRINT(0)("\n   TimeTable: startingTime=%f; endTime=%f; duration=%f; jumpTime=%f\n", this->startingTime, this->endTime, this->duration, this->jumpTime);
228//   PRINT(0)("   consists of %d Points\n", this->nodeCount);
229//   if (this->childCount == 0)
230//     PRINT(0)("   has no child\n");
231//   else if (this->childCount == 1)
232//     PRINT(0)("   has 1 child: =%d=\n", this->getChild(0)->ID);
233//   else if (this->childCount > 1)
234//     {
235//       PRINT(0)("   has %d children: ", this->childCount);
236//       //TrackElement* enumElem = this->children->enumerate();
237//       tIterator<TrackElement>* iterator = this->children->getIterator();
238//       TrackElement* enumElem = iterator->firstElement();
239//       while (enumElem)
240//         {
241//           PRINT(0)("=%d= ", enumElem->ID);
242//           enumElem = iterator->nextElement();
243//         }
244//       delete iterator;
245//       PRINT(0)("\n");
246//     }
247//
248//   if(this->isHotPoint)
249//     PRINT(0)("   is a special Point:\n");
250//   if(this->isSavePoint)
251//     PRINT(0)("    is a SavePoint\n");
252//   if(this->isFork)
253//     {
254//       PRINT(0)("    is A Fork with with %d children.\n", this->childCount);
255//     }
256//   if(this->isJoined)
257//     PRINT(0)("   is Joined at the End\n");
258//
259//   if(!this->backLoopCheck()) /* this should not happen */
260//     PRINT(2)(" THERE IS A BACKLOOP TO THIS ELEMENT\n");
[3593]261}
[3588]262
263/**
[4836]264 *  CONDITION that chooses the first child for the decision (static)
265 * @param nothing Nothing in this function
266 * @returns the chosen child
[3522]267*/
[3835]268int TrackElement::lowest(const void* nothing) const
[3522]269{
270  return 0;
271}
[3332]272
[3522]273/**
[4836]274 *  CONDITION that chooses the last child for the decision (static)
275 * @param nothing Nothing in this function
276 * @returns the chosen child
[3522]277*/
[3835]278int TrackElement::highest(const void* nothing) const
[4584]279{
[3522]280  return this->childCount-1;
281}
[3332]282
[3522]283/**
[4836]284 *  CONDITION that chooses a random child for the decision (static)
285 * @param nothing Nothing in this function
286 * @returns the chosen child
[3522]287*/
[3835]288int TrackElement::random(const void* nothing) const
[3522]289{
290  int i = (int)floor ((float)rand()/(float)RAND_MAX * (float)this->childCount);
291  if (i >= this->childCount)
292    return this->childCount-1;
[4584]293  else
[3522]294    return i;
295}
296
297/**
[4836]298 *  CONDITION that chooses child 0, if the node(probably Player)
[3522]299   is left of its parent (z<0)) and 1/right otherwise.
[4836]300 * @param node The node to act upon.
301 * @returns the chosen child
[3522]302*/
[3835]303int TrackElement::leftRight(const void* node) const
[3522]304{
305  PNode* tmpNode = (PNode*)node;
306
[3966]307  if (tmpNode->getRelCoor().z < 0)
[3522]308    return 0;
[4584]309  else
[3522]310    return 1;
311}
312
313
314/**
[4836]315 *  CONDITION that chooses the child, that has the nearest distance to the node (probably player).
316 * @param node The node to act upon.
317 * @returns the chosen child
[3522]318
319   This is rather dangerous, because one must carefully set the points on the curve.
320   The best Way is to set the nodes as wide away of each other as possible,
321   but take into consideration, that if the nodes are to far from a center node, the center will be chosen.
322   (play with this!!).
323*/
[3835]324int TrackElement::nearest(const void* node) const
[3522]325{
326  PNode* tmpNode = (PNode*)node;
327
[3966]328  Vector nodeRelCoord = tmpNode->getRelCoor();
[3522]329  float minDist = 100000000;
[3594]330  int childNumber = 0;
331  int i = 0;
332
[3832]333  //TrackElement* enumElem = this->children->enumerate();
334  tIterator<TrackElement>* iterator = this->children->getIterator();
[5115]335  TrackElement* enumElem = iterator->firstElement();
[3594]336  while (enumElem)
[3522]337    {
[3594]338      float dist = (nodeRelCoord - enumElem->curve->getNode(4)).len();
[3522]339      if (dist < minDist)
[4584]340        {
341          minDist = dist;
342          childNumber = i;
343        }
[3594]344      i++;
[3832]345      enumElem = iterator->nextElement();
[3522]346    }
[3832]347  delete iterator;
[3594]348
349  PRINTF(4)("PathDecision with nearest algorithm: %d\n", childNumber);
350  return childNumber;
[3522]351}
352
353
[3599]354////////////////////////
355///// TRACKMANAGER /////
356////////////////////////
[3311]357/**
[4836]358 *  standard constructor
[3311]359
360*/
[4746]361TrackManager::TrackManager()
[3311]362{
[3836]363  TrackManager::singletonRef = this; // do this because otherwise the TrackNode cannot get The instance of the TrackManager
[3331]364
365  PRINTF(3)("Initializing the TrackManager\n");
[3836]366  // setting up the First TrackElement
[3348]367  this->firstTrackElem = new TrackElement();
368  this->firstTrackElem->ID = 1;
[3842]369  this->firstTrackElem->setName("root");
370
[3331]371  this->currentTrackElem = firstTrackElem;
[3836]372
373  this->curveType = CURVE_BEZIER;
[3331]374  this->localTime = 0;
375  this->maxTime = 0;
[3348]376  this->trackElemCount = 1;
[3845]377
[3836]378  this->trackNode = new TrackNode();
379  this->setBindSlave(this->trackNode);
[3845]380  // initializing the Text
[10320]381  this->trackText = new Text("fonts/final_frontier.ttf", 30);
[4856]382  this->trackText->setAlignment(E2D_ALIGN_SCREEN_CENTER);
[3845]383  // initializing the Animation for the Text.
[3847]384  this->textAnimation = new tAnimation<Text>(this->trackText, &Text::setBlending);
[3872]385  this->textAnimation->addKeyFrame(1.0, 3.0, ANIM_NEG_EXP);
[3846]386  this->textAnimation->addKeyFrame(0.0, .001);
[3845]387  this->textAnimation->setInfinity(ANIM_INF_CONSTANT);
[3311]388}
389
[4017]390
[3311]391/**
[4836]392 *  loads a trackElement from a TiXmlElement
393 * @param root the TiXmlElement to load the Data from
[4017]394*/
[6512]395void TrackManager::loadParams(const TiXmlElement* root)
[4017]396{
[10368]397//   double x, y, z, d;
398//
399//   LOAD_PARAM_START_CYCLE(root, element);
400//   {
401//
402//     LoadParam_CYCLE(element, "WorkOn", this, TrackManager, workOnS)
403//         .describe("Selects a TrackElement (by name) to work on");
404//
405//     LoadParam_CYCLE(element, "Point", this, TrackManager, addPoint)
406//         .describe("Adds a new Point to the currently selected TrackElement");
407//
408//     LoadParam_CYCLE(element, "Duration", this, TrackManager, setDuration)
409//         .describe("Sets the Duration of the currently selected TrackElement");
410//
411//     LoadParam_CYCLE(element, "HotPoint", this, TrackManager, addHotPoint)
412//         .describe("Sets a new Point that acts as a hot point. meaning, the curve will flow through this Point");
413//
414//     LoadParam_CYCLE(element, "SavePoint", this, TrackManager, setSavePointS)
415//         .describe("Sets the current selected Point to a Savepoint, meaning that the curve will be ended and a new one starts, and that one starts again from this point on");
416//
417//     LoadParam_CYCLE(element, "Fork", this, TrackManager, forkS)
418//         .describe("Forks the Path into multiple forked Path names seperated by ','");
419//
420//     LoadParam_CYCLE(element, "Join", this, TrackManager, joinS)
421//         .describe("Joins multiple joining Path names seperated by ','");
[4584]422
[4496]423      /*
[5654]424    if( !strcmp( element->Value(), "Fork"))
425    {
426    container = element->FirstChild();
427    if( container->ToText())
[4584]428        {
429        assert( container->Value() != NULL);
430        PRINTF(4)("Loaded Fork: %s\n", container->Value());
431        forkS(container->Value());
432        }
433        }
[4496]434      */
[4501]435      /*
[4584]436        if( !strcmp( element->Value(), "Join"))
437        {
438        container = element->FirstChild();
439        if( container->ToText())
440        {
441        assert( container->Value() != NULL);
442        PRINTF0("Loaded Join: %s\n", container->Value());
443        joinS(container->Value());
444        }
445        }
446      */
[10368]447
448//   }
449//   LOAD_PARAM_END_CYCLE(element);
[4017]450}
451
452/**
[4836]453 *  standard destructor
[3311]454*/
[4746]455TrackManager::~TrackManager()
[3330]456{
[3331]457  PRINTF(3)("Destruct TrackManager\n");
[3311]458
[3594]459  PRINTF(4)("Deleting all the TrackElements\n");
[3331]460  delete this->firstTrackElem;
[3335]461
[5079]462  delete this->trackText;
[3836]463  // the tracknode should be deleted here, but is deleted by pNode: -> null_parent
464
[3331]465  // we do not have a TrackManager anymore
[3594]466  TrackManager::singletonRef = NULL;
[3330]467}
468
[3543]469//! Singleton Reference to TrackManager
[3331]470TrackManager* TrackManager::singletonRef = NULL;
471
[3836]472// INITIALIZE //
[3331]473/**
[4836]474 *  reserves Space for childCount children
475 * @param childCount The Count of children to make space for.
476 * @param trackElem The TrackElement to appy this to. (if NULL chose this->currentTrackElement)
[3335]477*/
[3836]478void TrackManager::initChildren(unsigned int childCount, TrackElement* trackElem)
[3335]479{
[10368]480//   if (!trackElem)
481//     trackElem = this->currentTrackElem;
482//
483//   trackElem->childCount = childCount;
484//   trackElem->mainJoin = true;  // this tells join, that this one is the Main Join, if it tries to join multiple Tracks
485//   trackElem->children =  new tList<TrackElement>();
486//   for (int i = 0; i < childCount; i++)
487//     {
488//       // create a new Element
489//       TrackElement* newElem = new TrackElement();
490//       // setting up the new ID
491//       newElem->ID = ++trackElemCount;
492//       // setting up the Time
493//       newElem->startingTime = trackElem->endTime + trackElem->jumpTime;
494//       // adds the conection Point
495//       this->addPointV(trackElem->curve->getNode(trackElem->curve->getNodeCount()),
496//                      newElem);
497//       // add the new child to the childList.
498//       trackElem->children->add(newElem);
499//     }
500//
501//   // setting the Name of the new TrackElement to the name of the last one + _childI
502//
503//   if (trackElem->getName())
504//     {
505//       for (int i = 0; i < trackElem->childCount; i++)
506//       {
507//         char* childName = new char[strlen(trackElem->getName())+10];
508//         sprintf(childName, "%s_child%d", trackElem->getName(), i);
509//         trackElem->getChild(i)->setName(childName);
510//       }
511//     }
512//   // select the first Child to work on.
513//   this->currentTrackElem = trackElem->getChild(0);
[3335]514}
515
[3836]516
[3335]517/**
[4836]518 *  Sets the trackID we are working on.
519 * @param trackID the trackID we are working on
[3330]520*/
[3836]521void TrackManager::workOn(unsigned int trackID)
[3330]522{
[3836]523  TrackElement* tmpElem = this->firstTrackElem->findByID(trackID);
524  if (tmpElem)
525    this->currentTrackElem = tmpElem;
526  else
527    PRINTF(2)("TrackElement %d not Found, leaving unchanged\n", trackID);
528  PRINTF(4)("now Working on %d\n", this->currentTrackElem->ID);
[3330]529}
530
531/**
[4836]532 *  Sets the TrackElement to work on
533 * @param trackName the Name of the Track to work on
[3330]534*/
[7221]535void TrackManager::workOnS(const std::string& trackName)
[3330]536{
[3836]537  TrackElement* tmpElem = this->firstTrackElem->findByName(trackName);
[3355]538  if (tmpElem)
539    this->currentTrackElem = tmpElem;
540  else
[3836]541    PRINTF(2)("TrackElement %s not Found, leaving unchanged\n", trackName);
[3594]542  PRINTF(4)("now Working on %d\n", this->currentTrackElem->ID);
[3330]543}
544
545/**
[4836]546 *  Sets the Type of the Curve
547 * @param curveType The Type to set
548 * @param trackElem the TrackElement that should get a new Curve.
[3836]549
[4836]550 *  this will possibly get obsolete during the process.
[3330]551*/
[3433]552void TrackManager::setCurveType(CurveType curveType, TrackElement* trackElem)
[3330]553{
[3433]554  if (!trackElem->isFresh)
[3332]555    {
556      PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n");
557      return;
558    }
[3588]559  this->curveType = curveType;
[3332]560  switch (curveType)
561    {
[3836]562    case CURVE_BEZIER:
[3433]563      trackElem->curve = new BezierCurve();
[3332]564      break;
565    }
[3330]566}
567
568/**
[4836]569 * @param duration the duration of the TrackElement
[4496]570   \see void TrackManager::setDuration(float duration, TrackElement* trackElem)
571*/
572void TrackManager::setDuration(float duration)
573{
574  this->setDuration(duration, NULL);
575}
576
577/**
[4836]578 *  Sets the duration of the current path in seconds.
579 * @param duration The duration in seconds.
580 * @param trackElem The TrackElement to apply this to.
[3330]581*/
[3836]582void TrackManager::setDuration(float duration, TrackElement* trackElem)
[3330]583{
[3836]584  if (!trackElem)
585    trackElem = this->currentTrackElem;
[3330]586
[3836]587  trackElem->duration = duration;
588  trackElem->endTime = trackElem->startingTime + duration;
[3352]589}
590
591/**
[4836]592 *  adds a point to trackElem
593 * @param x x coord
594 * @param y y coord
595 * @param z z coord
596 * @param trackElem The TrackElement to add the Point to
[4496]597*/
598void TrackManager::addPoint(float x, float y, float z)
599{
600  this->addPointV(Vector(x,y,z));
601}
602
603/**
[4836]604 *  adds a point to trackElem
605 * @param newPoint The point to add.
606 * @param trackElem The TrackElement to add the Point to
[3352]607*/
[4496]608void TrackManager::addPointV(Vector newPoint, TrackElement* trackElem)
[3352]609{
[3836]610  if (!trackElem)
611    trackElem = this->currentTrackElem;
612
[3352]613  if (trackElem->isFresh)
[3332]614    {
[3588]615      this->setCurveType(TMAN_DEFAULT_CURVETYPE, trackElem);
[3352]616      trackElem->isFresh = false;
[3332]617    }
[3352]618  trackElem->curve->addNode(newPoint);
619  trackElem->nodeCount++;
[3330]620}
621
622/**
[4836]623 *  adds a new Hot Point
624 * @param x: the x coordinate of the hotpoint
625 * @param y: the y coordinate of the hotpoint
626 * @param z: the z coordinate of the hotpoint
[4509]627   \see int TrackManager::addHotPointV(Vector newPoint, TrackElement* trackElem)
628*/
629void TrackManager::addHotPoint(float x, float y, float z)
630{
631  this->addHotPointV(Vector(x, y, z));
632}
633
634/**
[4836]635 *  adds save/splitpoint.
636 * @param newPoint The point to add.
637 * @param trackElem if supplied it will add a hotpoint on this TrackElement
638 * @returns A Pointer to a newly appended Curve
[3330]639*/
[4509]640int TrackManager::addHotPointV(Vector newPoint, TrackElement* trackElem)
[3330]641{
[3836]642  if (!trackElem)
643    trackElem = this->currentTrackElem;
644
[3594]645  PRINTF(4)("setting up a HotPoint\n");
[3836]646  if (trackElem->isFresh)
[3332]647    {
[3836]648      trackElem->isFresh = false;
[3332]649    }
[3330]650
[4836]651  // @todo HotPoint Handling.
[3836]652  trackElem->curve->addNode(newPoint);
653  trackElem->nodeCount++;
[3837]654  this->initChildren(1, trackElem);
[3330]655}
656
657/**
[4836]658   @todo this must be better
[4496]659*/
[7221]660void TrackManager::setSavePointS(const std::string& nextElementName)
[4496]661{
[10368]662//   this->setSavePoint(NULL);
663//   if (strcmp(nextElementName, ""))
664//     this->firstTrackElem->findByID(this->trackElemCount)->setName(nextElementName);
[4496]665}
666
667/**
[4836]668 *  Sets the last HotPoint into a savePoint.
669 * @param trackElem The TrackElement to appy this to. (if NULL chose this->currentTrackElement)
670 * @returns A Pointer to a newly appended Curve
[3837]671
[3330]672   If no HotPoint was defined the last added Point will be rendered into a savePoint. \n
673   If the HotPoint was defined as a fork the Point will \b not be set into a savePoint.
674*/
[4502]675void TrackManager::setSavePoint(TrackElement* trackElem)
[3330]676{
[3837]677  if (!trackElem)
678    trackElem = this->currentTrackElem;
679
[3594]680  PRINTF(4)("setting up a SavePoint.\n");
[3837]681  if (trackElem->isFork || trackElem->isSavePoint)
[3594]682    {
[3837]683      PRINTF(2)("%d is already finished \n", trackElem->ID);
[4502]684      return;
[3594]685    }
[3837]686  trackElem->isSavePoint = true;
687  trackElem->isHotPoint = true;
[3332]688
[3837]689  this->initChildren(1, trackElem);
[3330]690}
691
692/**
[4836]693 *  adds some interessting non-linear movments through the level.
694 * @param count The Count of children the fork will produce
[3330]695
696   If no HotPoint was defined the last added Point will be rendered into a fork. \n
697   If the HotPoint was defined as a savePoint the Point will \b not be set into a fork.
698*/
[3332]699void TrackManager::fork(unsigned int count, ...)
[3330]700{
[3351]701  int* trackIDs = new int[count];
[4220]702  this->forkV(count, trackIDs, NULL);
[3332]703  va_list ID;
704  va_start (ID, count);
705  for(int i = 0; i < count; i++)
706    {
[3351]707      *va_arg (ID, int*) = trackIDs[i];
[3332]708    }
[4584]709  va_end(ID);
[3332]710  delete []trackIDs;
[3330]711}
712
713/**
[4836]714 * @param count how many children to produce
715 * @param ... the information on the children (these are the Stings of their names
[4220]716   \see TrackManager::fork(unsigned int count, ...)
717
718   does the same as fork, but has an array of strings as an input.
719*/
720void TrackManager::forkS(unsigned int count, ...)
721{
722  int* trackIDs = new int[count];
723  this->forkV(count, trackIDs, NULL);
724  va_list name;
725  va_start (name, count);
726  for(int i = 0; i < count; i++)
727    {
[7221]728      this->firstTrackElem->findByID(trackIDs[i])->setName(va_arg(name, const std::string&));
[4220]729    }
[4584]730  va_end(name);
[4220]731  delete []trackIDs;
732}
733
734/**
735   \see TrackManager::fork(unsigned int count, ...)
736*/
[7221]737void TrackManager::forkS(const std::string& forkString)
[4220]738{
[10368]739//   SubString strings(forkString, ',');
740//
741//   int* trackIDs = new int[strings.getCount()];
742//   this->forkV(strings.getCount(), trackIDs, NULL);
743//
744//   for(int i = 0; i < strings.getCount(); i++)
745//     {
746//       this->firstTrackElem->findByID(trackIDs[i])->setName(strings.getString(i));
747//     }
748//   delete []trackIDs;
[4220]749}
750
751/**
[4836]752 *  adds some interessting non-linear movments through the level.
753 * @param count The Count of childrens the current HotPoint will have.
754 * @param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this).
755 * @param trackNames the names for the tracks as a char-arrey-array
756 * @param trackElem The TrackElement to appy this to. (if NULL choose this->currentTrackElement)
[3838]757   \see TrackManager::fork(unsigned int count, ...)
[3330]758*/
[4220]759void TrackManager::forkV(unsigned int count, int* trackIDs, char** trackNames, TrackElement* trackElem)
[3330]760{
[3837]761  if (!trackElem)
762    trackElem = this->currentTrackElem;
763
[3594]764  PRINTF(4)("Forking with %d children\n", count);
[3837]765  if (trackElem->isSavePoint)
[3332]766    return;
[3837]767  trackElem->isFork = true;
768  trackElem->isHotPoint = true;
[3351]769  for(int i = 0; i < count; i++)
770    trackIDs[i]=this->trackElemCount+1+i;
[3837]771  this->initChildren(count, trackElem);
[3330]772}
773
774/**
[4836]775 *  decides under what condition a certain Path will be chosen.
776 * @param trackID the trackID to apply this to.
777 * @param cond the CONDITION of the decision
778 * @param subject the Subject that will be decided upon with CONDITION cond.
[3522]779*/
[3837]780void TrackManager::condition(unsigned int trackID, CONDITION cond, void* subject)
[3522]781{
[3837]782  this->condition(cond, subject, this->firstTrackElem->findByID(trackID));
[3522]783}
[3837]784
[3522]785/**
[4836]786 *  decides under what condition a certain Path will be chosen.
787 * @param cond the CONDITION of the decision
788 * @param subject the Subject that will be decided upon with CONDITION cond.
789 * @param trackElem The TrackElement to appy this to. (if NULL chose this->currentTrackElement)
[3330]790*/
[3837]791void TrackManager::condition(CONDITION cond, void* subject, TrackElement* trackElem)
[3330]792{
[3837]793  if (!trackElem)
794    trackElem = this->currentTrackElem;
795
796  if (!trackElem->isFork)
[3522]797    {
[3837]798      PRINTF(2)("%d is not a Fork, and no condition can be set in this case\n", trackElem->ID);
[3594]799      return;
[3522]800    }
[3594]801  else
802    {
803      switch (cond)
[4584]804        {
805        case LOWEST:
806          trackElem->condFunc = &TrackElement::lowest;
807          break;
808        case HIGHEST:
809          trackElem->condFunc = &TrackElement::highest;
810          break;
811        case RANDOM:
812          trackElem->condFunc = &TrackElement::random;
813          break;
814        case LEFTRIGHT:
815          trackElem->condFunc = &TrackElement::leftRight;
816          break;
817        case NEAREST:
818          trackElem->condFunc = &TrackElement::nearest;
819          break;
820        case ENEMYKILLED:
821          break;
822        }
[3837]823      trackElem->subject=subject;
[3594]824    }
[3330]825}
826
827/**
[4836]828 *  joins some tracks together again.
829 * @param count The count of Paths to join.
[3330]830
831   Join will set the localTime to the longest time a Path has to get to this Point. \n
[3354]832   Join will join all curves to the first curve, meaning that all the tangents will be matched.
[3330]833*/
[3332]834void TrackManager::join(unsigned int count, ...)
[3330]835{
[3332]836  int* trackIDs = new int [count];
837  va_list ID;
838  va_start (ID, count);
839  for(int i = 0; i < count; i++)
840    {
841      trackIDs[i] = va_arg (ID, int);
842    }
843  va_end(ID);
844  this->joinV(count, trackIDs);
845  delete []trackIDs;
[3330]846}
847
848/**
[4836]849 *  Joins some Tracks together again.
850 * @param count The count of trackElements to join
[4220]851   \see void TrackManager::join(unsigned int count, ...)
[3841]852
853   The difference to void TrackManager::join(unsigned int count, ...) is, that this function takes
854   the Names of the TrackElements as inputs and not their ID
855*/
[4220]856void TrackManager::joinS(unsigned int count, ...)
[3841]857{
858  int* trackIDs = new int [count];
859  va_list NAME;
860  va_start (NAME, count);
861  for(int i = 0; i < count; i++)
862    {
[7221]863      const std::string& name = va_arg (NAME, char*);
[3841]864      TrackElement* tmpElem = this->firstTrackElem->findByName(name);
865      if (tmpElem)
[4584]866        trackIDs[i] = tmpElem->ID;
[3841]867      else
[4584]868        PRINTF(1)("Trying to join a Track, of which the name does not exist: %s\n", name);
[3841]869    }
870  va_end(NAME);
871  this->joinV(count, trackIDs);
872  delete []trackIDs;
873}
874
[4220]875/**
876   \see void TrackManager::join(unsigned int count, ...)
877*/
[7221]878void TrackManager::joinS(const std::string& joinString)
[4220]879{
[10368]880//   SubString strings(joinString, ',');
881//
882//   int* trackIDs = new int[strings.getCount()];
883//   this->joinV(strings.getCount(), trackIDs);
884//
885//   for(unsigned int i = 0; i < strings.getCount(); i++)
886//     {
887//       TrackElement* tmpElem = this->firstTrackElem->findByName(strings.getString(i).c_str());
888//       if (tmpElem != NULL)
889//         trackIDs[i] = tmpElem->ID;
890//       else
891//       {
892//         PRINTF(1)("Trying to join a Track, of which the name does not exist: %s\n", strings.getString(i).c_str());
893//         trackIDs[i] = -1;
894//       }
895//     }
896//   this->joinV(strings.getCount(), trackIDs);
897//   delete []trackIDs;
[4220]898}
899
[3841]900/**
[4836]901 *  joins some tracks together again.
902 * @param count The count of Paths to join.
903 * @param trackIDs an Array with the trackID's to join
[5335]904 *
905 * @see void TrackManager::join(unsigned int count, ...)
[3330]906*/
[3332]907void TrackManager::joinV(unsigned int count, int* trackIDs)
[3330]908{
[3840]909  TrackElement* tmpTrackElem;
[3880]910  TrackElement* tmpJoinElem;
[5313]911  for (unsigned int i = 0; i < count; i++)
[3840]912    if (!this->firstTrackElem->findByID(trackIDs[i]))
913      {
[4584]914        PRINTF(1)("Trying to Connect Paths that do not exist yet: %d\n Not Joining Anything\n", trackIDs[i]);
915        return;
[3840]916      }
917
[4584]918
[3594]919  PRINTF(3)("Joining %d tracks and merging to Track %d\n", count, trackIDs[0]);
[3354]920
[3522]921  // checking if there is a back-loop-connection and ERROR if it is.
[3840]922  tmpTrackElem = this->firstTrackElem->findByID(trackIDs[0]);
[4508]923  if (!tmpTrackElem->backLoopCheck())
[3838]924    {
925      PRINTF(2)("Backloop connection detected at joining trackElements\n -> TRACK WILL NOT BE JOINED\n");
926      return;
927    }
[3522]928
[3838]929  TrackElement* firstJoint =   this->firstTrackElem->findByID(trackIDs[0]);
[3433]930  float tmpLatestTime = firstJoint->endTime;
[3354]931
932  Vector tmpEndPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount());
[3433]933  Vector tmpTangentPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-1);
934  Vector tmpc2Point = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-2);
[3354]935  firstJoint->isJoined = true;
[3433]936  //  firstJoint->mainJoin = true;
[3354]937  if(!firstJoint->isHotPoint)
[3838]938    this->setSavePoint(firstJoint);
[3433]939  // Timing:
[5313]940  for (unsigned int i = 0; i < count; i++)
[3433]941    {
[3880]942      if(tmpJoinElem = this->firstTrackElem->findByID(trackIDs[i]))
[4584]943        {
944          if (tmpJoinElem->childCount == 0
945              && tmpJoinElem->endTime > tmpLatestTime)
946            tmpLatestTime = tmpJoinElem->endTime;
947        }
[3433]948    }
949  // time the main Join.
950  firstJoint->jumpTime = tmpLatestTime - firstJoint->endTime;
[4584]951
[3433]952  // Joining:
[3354]953  for (int i = 1; i < count; i++)
[3352]954    {
[3880]955      if( tmpJoinElem = this->firstTrackElem->findByID(trackIDs[i]))
[4584]956        {
957          if (tmpJoinElem->childCount > 0)
958            printf("!!This Curve has children, and as such will not be joined!!\n You can try joining other childless TrackElements to this one!");
959          else
960            {
961              this->addPointV(tmpc2Point, tmpJoinElem);
962              this->addPointV(tmpTangentPoint, tmpJoinElem);
963              this->addPointV(tmpEndPoint, tmpJoinElem);
964              // time all other Joins
965              tmpJoinElem->jumpTime = tmpLatestTime - tmpJoinElem->endTime;
966
967              //Copying Joint-Info
968              tmpJoinElem->children = firstJoint->children;
969              tmpJoinElem->childCount = firstJoint->childCount;
970              tmpJoinElem->isSavePoint = firstJoint->isSavePoint;
971              tmpJoinElem->isFork = firstJoint->isFork;
972
973              tmpJoinElem->isJoined = true;
974            }
975        }
[3352]976    }
[3838]977  if(firstJoint->children)
[3594]978    {
[3832]979      //TrackElement* enumElem = firstJoint->children->enumerate();
980      tIterator<TrackElement>* iterator = firstJoint->children->getIterator();
[5115]981      TrackElement* enumElem = iterator->firstElement();
[3594]982      while (enumElem)
[4584]983        {
984          PRINTF(5)("Setting startingTime of %d to %f.\n", enumElem->ID, tmpLatestTime);
985          enumElem->startingTime = tmpLatestTime;
986          enumElem->endTime = tmpLatestTime + enumElem->duration;
987
988          enumElem = iterator->nextElement();
989        }
[3832]990      delete iterator;
[3594]991    }
[3330]992}
993
[3433]994/**
[4836]995 *  finalizes the TrackSystem. after this it will not be editable anymore
[3433]996
[4836]997   @todo check for any inconsistencies, output errors
[3433]998*/
[4746]999void TrackManager::finalize()
[3433]1000{
1001  for (int i = 1; i<= trackElemCount ;i++)
1002    {
[3836]1003      TrackElement* tmpElem = this->firstTrackElem->findByID(i);
[3832]1004      if( tmpElem->childCount > 0 && tmpElem->mainJoin)
[4584]1005        {
1006          tIterator<TrackElement>* iterator = tmpElem->children->getIterator();
[5115]1007          TrackElement* enumElem = iterator->firstElement();
[4584]1008          //TrackElement* enumElem = tmpElem->children->enumerate();
1009          while (enumElem)
1010            {
1011
1012              // c1-continuity
1013              enumElem->curve->addNode(enumElem->curve->getNode(0) +
1014                                                   ((enumElem->curve->getNode(0) -
1015                                                    tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1))
1016                                                    ),2);
1017              enumElem->nodeCount++;
1018              // c2-continuity
1019              enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())-
1020                                                    tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 +
1021                                                   tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3);
1022              enumElem->nodeCount++;
1023              PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n                  %d-out: count: %d %f, %f, %f\n",
1024                     tmpElem->ID, tmpElem->nodeCount,
1025                     tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z,
1026                     enumElem->ID, enumElem->nodeCount,
1027                     enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z);
1028
1029              enumElem = iterator->nextElement();
1030            }
1031          delete iterator;
1032        }
[3433]1033    }
[3838]1034  for (int i = 1; i <= trackElemCount;i++)
[3836]1035    if (this->firstTrackElem->findByID(i)->endTime > this->maxTime)
1036      this->maxTime = this->firstTrackElem->findByID(i)->endTime; // very bad implemented :/
[3433]1037}
1038
1039
[3330]1040// RUNTIME //
1041
1042/**
[4836]1043 *  calculates the Position for the localTime of the Track.
1044 * @returns the calculated Position
[3330]1045*/
[3332]1046Vector TrackManager::calcPos() const
[3330]1047{
[3348]1048  return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration);
[3330]1049}
1050
1051/**
[4836]1052 *  calculates the Rotation for the localTime of the Track.
1053 * @returns the calculated Rotation
[3330]1054*/
[3332]1055Vector TrackManager::calcDir() const
[3330]1056{
[3433]1057  return this->currentTrackElem->curve->calcDir((this->localTime - this->currentTrackElem->startingTime)/this->currentTrackElem->duration);
[3330]1058}
1059
1060/**
[4836]1061 * @returns the current Width of the track
[3596]1062*/
[4746]1063float TrackManager::getWidth() const
[3596]1064{
1065  return this->currentTrackElem->width;
1066}
1067
1068/**
[4836]1069 *  Advances the local-time of the Track around dt
1070 * @param dt The time about which to advance.
[3333]1071
1072   This function also checks, if the TrackElement has to be changed.
[3330]1073*/
1074void TrackManager::tick(float dt)
1075{
[10368]1076//   PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt);
1077//   if (this->localTime <= this->firstTrackElem->duration)
1078//     this->jumpTo(this->localTime);
1079//   if (this->localTime <= this->maxTime)
1080//     this->localTime += dt;
1081//   if (this->localTime > this->currentTrackElem->endTime
1082//       && this->currentTrackElem->children)
1083//     {
1084//       if (this->currentTrackElem->jumpTime != 0.0)
1085//         this->jumpTo(this->localTime + this->currentTrackElem->jumpTime);
1086//       // jump to the next TrackElement and also set the history of the new Element to the old one.
1087//       TrackElement* tmpHistoryElem = this->currentTrackElem;
1088//       this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem));
1089//       this->currentTrackElem->history = tmpHistoryElem;
1090//       if (this->currentTrackElem->getName())
1091//         {
1092//           this->trackText->setText(this->currentTrackElem->getName());
1093//           this->textAnimation->replay();
1094//         }
1095//     }
1096//   if (this->bindSlave)
1097//     {
1098//       Vector tmp = this->calcPos();
1099//       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));
1100//
1101//       Vector v(0.0, 1.0, 0.0);
1102//       Quaternion q(-PI/2, v);
1103//       quat = quat * q;
1104//
1105//       this->bindSlave->setAbsCoor(tmp);
1106//       this->bindSlave->setAbsDir(quat);
1107//     }
[3330]1108}
1109
1110/**
[4836]1111 *  Jumps to a certain point on the Track.
1112 * @param time The time on the Track to jump to.
[3331]1113
1114   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.)
1115   Max is trackLengthMax.
1116*/
1117void TrackManager::jumpTo(float time)
1118{
[10368]1119//   if (time == 0)
1120//     {
1121//       this->currentTrackElem = this->firstTrackElem;
1122//       if (this->currentTrackElem->getName())
1123//         {
1124//           this->trackText->setText(this->currentTrackElem->getName());
1125//           this->textAnimation->play();
1126//         }
1127//     }
1128//   this->localTime = time;
[3331]1129}
1130
1131/**
[4836]1132 *  a Function that decides which Path we should follow.
1133 * @param trackElem The Path to choose.
[4584]1134
[3330]1135*/
[3522]1136int TrackManager::choosePath(TrackElement* trackElem)
[3330]1137{
[3522]1138  return (trackElem->*(trackElem->condFunc))(trackElem->subject);
[3330]1139}
1140
[3433]1141/**
[4836]1142 *  Sets the PNode, that should be moved along the Tack
1143 * @param bindSlave the PNode to set
[3433]1144*/
1145void TrackManager::setBindSlave(PNode* bindSlave)
1146{
[3556]1147  this->bindSlave = bindSlave;
[3433]1148}
[3350]1149
[3556]1150/**
[4836]1151 * @returns the main TrackNode
[3556]1152*/
[4746]1153PNode* TrackManager::getTrackNode()
[3556]1154{
1155  return this->trackNode;
1156}
[3350]1157
1158// DEBUG //
1159
1160/**
[4836]1161 *  Imports a model of the Graph into the OpenGL-environment.
1162 * @param dt The Iterator used in seconds for Painting the Graph.
[3350]1163
1164   This is for testing facility only. Do this if you want to see the Path inside the Level.
1165   eventually this will all be packed into a gl-list.
1166*/
1167void TrackManager::drawGraph(float dt) const
1168{
1169  for (int i = 1; i <= trackElemCount; i++)
1170    {
[3352]1171      glBegin(GL_LINE_STRIP);
[3836]1172      TrackElement* tmpElem = this->firstTrackElem->findByID(i);
[3350]1173      if (tmpElem->curve)
[4584]1174        for(float f = 0.0; f < 1.0; f+=dt)
1175          {
1176            //      printf("%f, %f, %f\n",trackManager->calcPos().x, trackManager->calcPos().y, trackManager->calcPos().z);
1177            Vector tmpVector = tmpElem->curve->calcPos(f);
1178            glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z);
1179          }
[3710]1180      glEnd();
[3350]1181    }
1182}
1183
[3433]1184/**
[4836]1185 *  outputs debug information about the trackManager
1186 * @param level how much debug
[3433]1187*/
[3350]1188void TrackManager::debug(unsigned int level) const
1189{
[3522]1190  PRINT(0)("=========================================\n");
1191  PRINT(0)("= CLASS TRACKMANAGER::debug information =\n");
1192  PRINT(0)("=========================================\n");
1193  //  PRINT(0)("Status is: %
1194  PRINT(0)(" Consists of %d elements\n", this->trackElemCount);
1195  PRINT(0)(" localTime is: %f\n", this->localTime);
[3350]1196  if (level >= 2)
1197    {
1198      for (int i = 1; i <= trackElemCount; i++)
[4584]1199        {
1200          TrackElement* tmpElem = this->firstTrackElem->findByID(i);
1201          tmpElem->debug();
1202        }
[3350]1203    }
[3522]1204  PRINT(0)("-----------------------------------------\n");
[3350]1205}
Note: See TracBrowser for help on using the repository browser.