Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/track/track_manager.cc @ 7917

Last change on this file since 7917 was 7221, checked in by bensch, 19 years ago

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

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