Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10411 was 10368, checked in by patrick, 18 years ago

merged the branche playability into the trunk

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