Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/levelloader/src/track_manager.cc @ 3701

Last change on this file since 3701 was 3605, checked in by bensch, 20 years ago

orxonox/trunk: merged trunk back to levelloader
merged with command:
svn merge -r 3499:HEAD trunk branches/levelloader

Conflicts in
C track_manager.h
C world_entities/player.cc
C world_entities/player.h
C world_entities/environment.h
C lib/coord/p_node.cc
C defs/debug.h
C track_manager.cc
C story_entities/campaign.h

solved in merge-favouring. It was quite easy because Chris only worked on the headers, and he didi it quite clean. Thats the spirit :)

Conflits in world.cc are a MESS: fix it

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