Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/nico/src/track_manager.cc @ 3542

Last change on this file since 3542 was 3399, checked in by bensch, 20 years ago

orxonox/branches/nico: merged trunk into branches/nico
merged with command:
svn merge ../trunk/ nico/ -r 3238:HEAD
resolved conflict in favor of branches/nico.

File size: 15.2 KB
RevLine 
[3311]1
2
3/*
4   orxonox - the future of 3D-vertical-scrollers
5
6   Copyright (C) 2004 orx
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   ### File Specific:
14   main-programmer: Patrick Boenzli
15   co-programmer: ...
16*/
17
18
19#include "track_manager.h"
[3332]20#include <stdarg.h>
[3311]21
22
23using namespace std;
24
[3331]25/**
26   \brief initializes a TrackElement (sets the default values)
27*/
28TrackElement::TrackElement(void)
29{
[3332]30  this->isFresh = true;
[3354]31  this->isHotPoint = false;
[3331]32  this->isSavePoint = false;
33  this->isFork = false;
34  this->isJoined = false;
[3356]35  this->mainJoin = false;
[3331]36  this->cond; //!< todo think!!
37  this->ID = -1;
[3333]38  this->startingTime = 0; //!< \todo eventually set this to the max time of TrackManager.
[3348]39  this->duration = 1;
[3331]40  this->curveType = BEZIERCURVE;
41  this->nodeCount = 0;
[3348]42  this->childCount = 0;
[3332]43  this->name = NULL;
[3349]44  this->startPoint = Vector(0,0,0);
45  this->startTangentPoint = Vector(0,0,0);
[3331]46  this->curve = NULL;
47  this->children = NULL;
48}
[3311]49
[3331]50/**
[3332]51    \brief destroys all alocated memory)
[3331]52    \todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements
53*/
54TrackElement::~TrackElement(void)
55{
[3332]56  if (this->name)
57    delete []name;
[3331]58  if (this->curve)
59    delete this->curve;
[3356]60  if ((!this->isJoined &&this->childCount > 0) || (this->isJoined && this->mainJoin))
[3331]61    {
62      for (int i=0; i < this->childCount; i++)
63        delete this->children[i];
[3356]64      delete this->children;
[3331]65    }
66}
67
[3332]68/**
69   \brief Searches through all the TrackElements for trackID.
70   \param trackID The ID to search for.
71   \returns The TrackElement if Found, NULL otherwise.
72   
73   \todo make this more modular, to search for different things
74*/
75TrackElement* TrackElement::findByID(unsigned int trackID)
76{
77  // return if Found.
78  if (this->ID == trackID)
79    return this;
80  // search on.
81  if (this->childCount > 0)
82    for (int i=0; i < this->childCount; i++)
83      {
84        TrackElement* tmpElem;
85        if ((tmpElem = this->children[i]->findByID(trackID)))
86          return tmpElem;
87      }
88  else return NULL;
89}
[3331]90
91
92
[3332]93
94
95/////////////////////////////////////
96///// TRACKMANAGER //////////////////
97/////////////////////////////////////
[3311]98/**
99   \brief standard constructor
100
101*/
[3354]102TrackManager::TrackManager(void)
[3311]103{
[3331]104  this->setClassName ("TrackManager");
105
106  PRINTF(3)("Initializing the TrackManager\n");
[3348]107  this->firstTrackElem = new TrackElement();
108  this->firstTrackElem->ID = 1;
[3331]109  this->currentTrackElem = firstTrackElem;
110  this->localTime = 0;
111  this->maxTime = 0;
[3348]112  this->trackElemCount = 1;
[3311]113}
114
115
116/**
[3331]117   \brief standard destructor
[3311]118
119*/
[3354]120TrackManager::~TrackManager(void)
[3330]121{
[3331]122  PRINTF(3)("Destruct TrackManager\n");
[3311]123
[3331]124  PRINTF(3)("Deleting all the TrackElements\n");
125  delete this->firstTrackElem;
[3335]126
[3331]127  // we do not have a TrackManager anymore
128  singletonRef = NULL;
[3330]129}
130
[3331]131TrackManager* TrackManager::singletonRef = NULL;
132
[3330]133/**
[3331]134   \returns The reference on the TrackManager.
135
136   If the TrackManager does not exist, it will be created.
137*/
138TrackManager* TrackManager::getInstance(void) 
139{
140  if (singletonRef)
141    return singletonRef;
142  else
143    return singletonRef = new TrackManager();
144}
145
146/**
[3335]147   \brief reserves Space for childCount children
148   \param childCount The Count of children to make space for.
149*/
150void TrackManager::initChildren(unsigned int childCount)
151{
152  this->currentTrackElem->childCount = childCount;
153  this->currentTrackElem->children = new TrackElement*[childCount];
154  for (int i=0; i<childCount; i++)
[3348]155    {
156      this->currentTrackElem->children[i] = new TrackElement();
157      this->currentTrackElem->children[i]->ID = ++trackElemCount;
158      this->currentTrackElem->children[i]->startingTime = this->currentTrackElem->startingTime+this->currentTrackElem->duration;
[3349]159      this->currentTrackElem->children[i]->startPoint = this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount());
160      this->currentTrackElem->children[i]->startTangentPoint = (this->currentTrackElem->children[i]->startPoint *2) - this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount()-1);
[3348]161    }
[3335]162}
163
164/**
[3330]165   \brief Searches for a given trackID.
166   \param trackID the trackID to search for.
[3332]167   \returns The TrackElement #trackID if found, NULL otherwise.
[3330]168*/
[3332]169TrackElement* TrackManager::findTrackElementByID(unsigned int trackID) const
[3330]170{
[3332]171  return firstTrackElem->findByID(trackID);
[3330]172}
173
174// INITIALIZE //
175
176/**
177   \brief Sets the trackID we are working on.
178   \param trackID the trackID we are working on
179*/
[3332]180void TrackManager::workOn(unsigned int trackID)
[3330]181{
[3355]182  TrackElement* tmpElem = findTrackElementByID(trackID);
183  if (tmpElem)
184    this->currentTrackElem = tmpElem;
185  else
186    printf("TrackElement not Found, leaving unchanged\n");
187  printf("now Working on %d\n", this->currentTrackElem->ID);
188
[3330]189}
190
191/**
192   \brief Sets the Type of the Curve
193   \brief curveType The Type to set
194*/
[3332]195void TrackManager::setCurveType(CurveType curveType)
[3330]196{
[3332]197  if (!this->currentTrackElem->isFresh)
198    {
199      PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n");
200      return;
201    }
202  this->currentTrackElem->curveType = curveType;
203  switch (curveType)
204    {
205    case BEZIERCURVE:
206      this->currentTrackElem->curve = new BezierCurve();
207      break;
208    case UPOINTCURVE:
209      this->currentTrackElem->curve = new UPointCurve();
210      break;
211    }
[3330]212}
213
214/**
[3333]215   \brief Sets the duration of the current path in seconds.
216   \param time The duration in seconds.
[3330]217*/
218
[3333]219void TrackManager::setDuration(float time)
[3330]220{
[3333]221  this->currentTrackElem->duration = time;
[3330]222}
223
224/**
225   \brief adds a point to the current TrackElement
226   \param newPoint The point to add.
227*/
[3333]228bool TrackManager::addPoint(Vector newPoint)
[3330]229{
[3352]230  return this->addPoint(newPoint, this->currentTrackElem);
231}
232
233/**
234   \brief adds a point to trackElem
235   \param newPoint The point to add.
236   \param trackElem The TrackElement to add the Point to
237*/
238bool TrackManager::addPoint(Vector newPoint, TrackElement* trackElem)
239{
240  if (trackElem->isFresh)
[3332]241    {
242      this->setCurveType(BEZIERCURVE);
[3352]243      trackElem->isFresh = false;
244      if(trackElem != this->firstTrackElem)
[3350]245        {
[3352]246          this->addPoint(trackElem->startPoint);
247          this->addPoint(trackElem->startTangentPoint);
[3350]248        }
[3332]249    }
[3352]250  trackElem->curve->addNode(newPoint);
251  trackElem->nodeCount++;
[3330]252}
253
254/**
255   \brief adds save/splitpoint.
256   \param newPoint The point to add.
[3333]257   \returns A Pointer to a newly appended Curve
[3330]258*/
[3333]259int TrackManager::addHotPoint(Vector newPoint)
[3330]260{
[3354]261  printf("setting up a HotPoint\n");
[3332]262  if (this->currentTrackElem->isFresh)
263    {
264      this->setCurveType(BEZIERCURVE);
265      this->currentTrackElem->isFresh = false;
266    }
[3330]267
[3332]268  // \todo HotPoint Handling.
269  this->currentTrackElem->curve->addNode(newPoint);
[3351]270  this->currentTrackElem->nodeCount++;
[3354]271  this->initChildren(1);
272  this->currentTrackElem = this->currentTrackElem->children[0];
[3330]273}
274
275/**
276   \brief Sets the last HotPoint into a savePoint.
[3333]277   \returns A Pointer to a newly appended Curve
[3330]278   
279   If no HotPoint was defined the last added Point will be rendered into a savePoint. \n
280   If the HotPoint was defined as a fork the Point will \b not be set into a savePoint.
281*/
[3333]282int TrackManager::setSavePoint(void)
[3330]283{
[3354]284  printf("setting up a SavePoint.\n");
[3332]285  if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint)
[3333]286    return this->currentTrackElem->children[1]->ID;
[3332]287  this->currentTrackElem->isSavePoint = true;
[3354]288  this->currentTrackElem->isHotPoint = true;
[3332]289
[3335]290  this->initChildren(1);
[3348]291  this->currentTrackElem = this->currentTrackElem->children[0];
[3330]292}
293
294/**
295   \brief adds some interessting non-linear movments through the level.
296   \param count The Count of childrens the current HotPoint will have.
297
298   If no HotPoint was defined the last added Point will be rendered into a fork. \n
299   If the HotPoint was defined as a savePoint the Point will \b not be set into a fork.
300*/
[3332]301void TrackManager::fork(unsigned int count, ...)
[3330]302{
[3351]303  int* trackIDs = new int[count];
304  this->forkV(count, trackIDs);
[3332]305  va_list ID;
306  va_start (ID, count);
307  for(int i = 0; i < count; i++)
308    {
[3351]309      *va_arg (ID, int*) = trackIDs[i];
[3332]310    }
[3351]311  va_end(ID); 
[3332]312  delete []trackIDs;
[3330]313}
314
315/**
316   \brief adds some interessting non-linear movments through the level.
317   \param count The Count of childrens the current HotPoint will have.
318   \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this).
319
320   \see void TrackManager::fork(int count, ...)
[3332]321
322   \todo initialisation is wrong!! also in setSavePoint.
[3330]323*/
[3332]324void TrackManager::forkV(unsigned int count, int* trackIDs)
[3330]325{
[3354]326  printf("Forking with %d children\n", count);
[3332]327  if (this->currentTrackElem->isSavePoint)
328    return;
329  this->currentTrackElem->isFork = true;
[3354]330  this->currentTrackElem->isHotPoint = true;
[3351]331  for(int i = 0; i < count; i++)
332    trackIDs[i]=this->trackElemCount+1+i;
[3335]333  this->initChildren(count);
[3330]334}
335
336/**
337   \brief decides under what condition a certain Path will be chosen.
338   \param groupID the ID on which to choose the preceding move
339   \param cond \todo think about this
340*/
[3332]341void TrackManager::condition(unsigned int groupID, PathCondition cond)
[3330]342{
[3332]343 
[3330]344}
345
346/**
347   \brief joins some tracks together again.
348   \param count The count of Paths to join.
349
350   Join will set the localTime to the longest time a Path has to get to this Point. \n
[3354]351   Join will join all curves to the first curve, meaning that all the tangents will be matched.
[3330]352*/
[3332]353void TrackManager::join(unsigned int count, ...)
[3330]354{
[3332]355  int* trackIDs = new int [count];
356  va_list ID;
357  va_start (ID, count);
358  for(int i = 0; i < count; i++)
359    {
360      trackIDs[i] = va_arg (ID, int);
361    }
362  va_end(ID);
363  this->joinV(count, trackIDs);
364  delete []trackIDs;
[3330]365}
366
367/**
368   \brief joins some tracks together again.
369   \param count The count of Paths to join.
370   \param trackIDs an Array with the trackID's to join
371
372   \see void TrackManager::join(int count, ...)
373*/
[3332]374void TrackManager::joinV(unsigned int count, int* trackIDs)
[3330]375{
[3354]376  printf("Joining %d tracks to Track %d\n", count, trackIDs[0]);
377
378  // chanching work-on to temporary value. going back at the end.
379  int tmpCurrentWorkingID = this->currentTrackElem->ID;
380  this->workOn(trackIDs[0]);
381
382  TrackElement* firstJoint = this->currentTrackElem;
383  Vector tmpEndPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount());
384  Vector tmpTangentPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-1); 
385  firstJoint->isJoined = true;
[3356]386  firstJoint->mainJoin = true;
[3354]387  if(!firstJoint->isHotPoint)
388    this->setSavePoint();
389
390  for (int i = 1; i < count; i++)
[3352]391    {
392      TrackElement* tmpJoinElem = this->findTrackElementByID(trackIDs[i]);
[3354]393      if (tmpJoinElem->childCount > 0)
394        printf("!!This Curve has children, and as such will not be joined!!\n You can try joining other childless TrackElements to this one!");
395      else
396        {
397          this->addPoint(tmpTangentPoint, tmpJoinElem);
398          this->addPoint(tmpEndPoint, tmpJoinElem);
399         
400          //Copying Joint-Info
401          tmpJoinElem->children = firstJoint->children;
402          tmpJoinElem->childCount = firstJoint->childCount;
403          tmpJoinElem->isSavePoint = firstJoint->isSavePoint;
404          tmpJoinElem->isFork = firstJoint->isFork;
405         
406          tmpJoinElem->isJoined = true;
407        }
[3352]408    }
[3354]409  // returning to the TrackElement we were working on.
410  this->workOn(tmpCurrentWorkingID);
[3330]411}
412
413// RUNTIME //
414
415/**
416   \brief calculates the Position for the localTime of the Track.
417   \returns the calculated Position
418*/
[3332]419Vector TrackManager::calcPos() const
[3330]420{
[3348]421  //  PRINTF(0)("TrackElement:%d, localTime: %f\n",this->currentTrackElem->ID, this->localTime);
422  return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration);
[3330]423}
424
425/**
426   \brief calculates the Rotation for the localTime of the Track.
427   \returns the calculated Rotation
428*/
[3332]429Vector TrackManager::calcDir() const
[3330]430{
[3348]431  return this->currentTrackElem->curve->calcDir((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration);
[3330]432}
433
434/**
435   \brief Advances the local-time of the Track around dt
436   \param dt The time about which to advance.
[3333]437
438   This function also checks, if the TrackElement has to be changed.
[3330]439*/
440void TrackManager::tick(float dt)
441{
[3348]442  if (this->localTime <= this->firstTrackElem->duration)
443    this->jumpTo(this->localTime);
[3332]444  this->localTime += dt;
[3348]445  if (this->localTime > this->currentTrackElem->startingTime + this->currentTrackElem->duration && this->currentTrackElem->childCount > 0)
446    this->currentTrackElem = this->currentTrackElem->children[0];
[3330]447}
448
449/**
[3331]450   \brief Jumps to a certain point on the Track.
451   \param time The time on the Track to jump to.
452
453   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.)
454   Max is trackLengthMax.
455*/
456void TrackManager::jumpTo(float time)
457{
[3348]458  if (time == 0)
459    this->currentTrackElem = this->firstTrackElem;
460  this->localTime = time;
[3331]461}
462
463/**
[3330]464   \brief a Function that decides which Path we should follow.
465   \param graphID The Path to choose.
466   
467*/
468void TrackManager::choosePath(int graphID)
469{
470
471}
472
[3350]473
474
475// DEBUG //
476
477/**
478   \brief Imports a model of the Graph into the OpenGL-environment.
479   \param dt The Iterator used in seconds for Painting the Graph.
480
481   This is for testing facility only. Do this if you want to see the Path inside the Level.
482   eventually this will all be packed into a gl-list.
483*/
484void TrackManager::drawGraph(float dt) const
485{
[3352]486
[3350]487  for (int i = 1; i <= trackElemCount; i++)
488    {
[3352]489      glBegin(GL_LINE_STRIP);
[3350]490      TrackElement* tmpElem = this->findTrackElementByID(i);
491      if (tmpElem->curve)
492        for(float f = 0.0; f < 1.0; f+=dt)
493          {
494            //      printf("%f, %f, %f\n",trackManager->calcPos().x, trackManager->calcPos().y, trackManager->calcPos().z);
495            Vector tmpVector = tmpElem->curve->calcPos(f);
496            glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z);
497          }
[3352]498  glEnd();
[3350]499    }
500}
501
502void TrackManager::debug(unsigned int level) const
503{
504  printf("::CLASS TRACKMANAGER::debug information::\n");
505  //  printf("Status is: %
506  printf(" Consists of %d elements\n", this->trackElemCount);
507  printf(" localTime is: %f\n", this->localTime);
508  if (level >= 2)
509    {
510      for (int i = 1; i <= trackElemCount; i++)
511        {
512          TrackElement* tmpElem = this->findTrackElementByID(i);
513          printf("  ::TrackElement:%i::", tmpElem->ID);
514          if(tmpElem->name)
515            printf("name:%s::", tmpElem->name);
[3354]516          if(tmpElem->isFresh)
517            printf("   has not jet eddited in any way\n");
[3350]518          printf("\n   TimeTable: starting at = %f; ends at = %f; duration = %f\n", tmpElem->startingTime, tmpElem->startingTime+tmpElem->duration, tmpElem->duration);
519          printf("   consists of %d Points\n", tmpElem->nodeCount);
[3354]520          if (tmpElem->childCount == 0)
521            printf("   has no child\n");           
522          else if (tmpElem->childCount == 1)
523            printf("   has 1 child: ==%d==\n", tmpElem->children[0]->ID);
524          else if (tmpElem->childCount > 1)
[3351]525            {
[3354]526              printf("   has %d children: ", tmpElem->childCount);
[3351]527              for(int i = 0; i < tmpElem->childCount; i++)
528                printf("=%d= ", tmpElem->children[i]->ID);
529              printf("\n");
530            }
[3354]531
532          if(tmpElem->isHotPoint)
533            printf("   is a special Point:\n");
534          if(tmpElem->isSavePoint)
535            printf("    is a SavePoint\n");
536          if(tmpElem->isFork)
537            {
538              printf("    is A Fork with with %d children.", tmpElem->childCount);
539            }
[3350]540          if(tmpElem->isJoined)
541            printf("   is Joined at the End\n");
542        }
543    }
544}
Note: See TracBrowser for help on using the repository browser.