Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/heightMap/src/track_manager.cc @ 5030

Last change on this file since 5030 was 4122, checked in by bensch, 20 years ago

orxonox/branches/heightMap: merged the Trunk back into branches/heightMap:
merged with Command
svn merge -r 3918:HEAD trunk branches/heightMap
conflicts resolved in favor of the Trunk

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