Index: orxonox/trunk/src/track_manager.cc
===================================================================
--- orxonox/trunk/src/track_manager.cc	(revision 3593)
+++ orxonox/trunk/src/track_manager.cc	(revision 3594)
@@ -65,6 +65,10 @@
   if ((!this->isJoined &&this->childCount > 0) || (this->isJoined && this->mainJoin))
     {
-      for (int i=0; i < this->childCount; i++)
-	delete this->children[i];
+      TrackElement* enumElem = children->enumerate();
+      while (enumElem)
+	{
+	  delete enumElem;
+	  enumElem = children->nextElement();
+	}
       delete this->children;
     }
@@ -85,11 +89,16 @@
   // search on.
   if (this->childCount > 0)
-    for (int i=0; i < this->childCount; i++)
-      {
-	TrackElement* tmpElem;
-	if ((tmpElem = this->children[i]->findByID(trackID)))
-	  return tmpElem;
-      }
-  else return NULL;
+    {
+      TrackElement* enumElem = this->children->enumerate();
+      TrackElement* tmpElem;
+      while (enumElem)
+	{
+	  if ((tmpElem = enumElem->findByID(trackID)))
+	    return tmpElem;
+	  enumElem = this->children->nextElement();
+	}
+    }
+  else 
+    return NULL;
 }
 
@@ -106,10 +115,31 @@
   else
     {
-      for (int i = 0; i < this->childCount; i++)
-	if(!this->children[i]->backLoopCheck(trackElem))
-	  return false;
+      TrackElement* enumElem = this->children->enumerate();
+      while (enumElem)
+	{
+	  if(!enumElem->backLoopCheck(trackElem))
+	    return false;
+	  enumElem = this->children->nextElement();
+	}
       
       return true;
     }
+}
+
+/**
+   \param childNumber which child to return
+   \returns the n-the children (starting at 0)
+*/
+TrackElement* TrackElement::getChild(int childCount)
+{
+  if (this->childCount == 0)
+    return NULL;
+  if (childCount > this->childCount)
+    childCount = this->childCount;
+  
+  TrackElement* enumElem = this->children->enumerate();
+  for (int i = 0; i < childCount; i++)
+    enumElem = this->children->nextElement();
+  return enumElem;
 }
 
@@ -155,10 +185,14 @@
     PRINT(0)("   has no child\n");
   else if (this->childCount == 1)
-    PRINT(0)("   has 1 child: =%d=\n", this->children[0]->ID);
+    PRINT(0)("   has 1 child: =%d=\n", this->getChild(0)->ID);
   else if (this->childCount > 1)
     {
       PRINT(0)("   has %d children: ", this->childCount);
-      for(int i = 0; i < this->childCount; i++)
-	PRINT(0)("=%d= ", this->children[i]->ID);
+      TrackElement* enumElem = this->children->enumerate();
+      while (enumElem)
+	{
+	  PRINT(0)("=%d= ", enumElem->ID);
+	  enumElem = this->children->nextElement();
+	}
       PRINT(0)("\n");
     }
@@ -246,16 +280,22 @@
   Vector nodeRelCoord = tmpNode->getRelCoor();
   float minDist = 100000000;
-  int nodeNumber = 0;
-  for (int i = 0; i < this->childCount; i++)
-    {
-      float dist = (nodeRelCoord - this->children[i]->curve->getNode(4)).len();
+  int childNumber = 0;
+  int i = 0;
+
+  TrackElement* enumElem = this->children->enumerate();
+  while (enumElem)
+    {
+      float dist = (nodeRelCoord - enumElem->curve->getNode(4)).len();
       if (dist < minDist)
 	{
 	  minDist = dist;
-	  nodeNumber = i;
-	}
-    }
-  PRINTF(4)("PathDecision with nearest algorithm: %d\n", nodeNumber);
-  return nodeNumber;
+	  childNumber = i;
+	}
+      i++;
+      enumElem = this->children->nextElement();
+    }
+
+  PRINTF(4)("PathDecision with nearest algorithm: %d\n", childNumber);
+  return childNumber;
 }
 
@@ -270,7 +310,7 @@
 TrackManager::TrackManager(void)
 {
-  this->setClassName ("TrackManager");
+  this->setClassName("TrackManager");
   
-  singletonRef = this;
+  TrackManager::singletonRef = this;
 
   PRINTF(3)("Initializing the TrackManager\n");
@@ -281,5 +321,5 @@
   this->maxTime = 0;
   this->trackElemCount = 1;
-  this->bindSlave = this->trackNode = new TrackNode();
+  this->setBindSlave(this->trackNode = new TrackNode());
 }
 
@@ -292,9 +332,9 @@
   PRINTF(3)("Destruct TrackManager\n");
 
-  PRINTF(3)("Deleting all the TrackElements\n");
+  PRINTF(4)("Deleting all the TrackElements\n");
   delete this->firstTrackElem;
 
   // we do not have a TrackManager anymore
-  singletonRef = NULL;
+  TrackManager::singletonRef = NULL;
 }
 
@@ -309,7 +349,7 @@
 TrackManager* TrackManager::getInstance(void) 
 {
-  if (!singletonRef)
-    singletonRef = new TrackManager();
-  return singletonRef;
+  if (!TrackManager::singletonRef)
+    TrackManager::singletonRef = new TrackManager();
+  return TrackManager::singletonRef;
 }
 
@@ -322,14 +362,15 @@
   this->currentTrackElem->childCount = childCount;
   this->currentTrackElem->mainJoin = true;
-  this->currentTrackElem->children = new TrackElement*[childCount];
-  for (int i=0; i<childCount; i++)
-    {
-      this->currentTrackElem->children[i] = new TrackElement();
-      this->currentTrackElem->children[i]->ID = ++trackElemCount;
-      this->currentTrackElem->children[i]->startingTime = this->currentTrackElem->endTime + this->currentTrackElem->jumpTime;
-      this->addPoint(this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount()), this->currentTrackElem->children[i]);
+  this->currentTrackElem->children =  new tList<TrackElement>();
+  for (int i = 0; i < childCount; i++)
+    {
+      TrackElement* newElem = new TrackElement();
+      this->currentTrackElem->children->add(newElem);
+      newElem->ID = ++trackElemCount;
+      newElem->startingTime = this->currentTrackElem->endTime + this->currentTrackElem->jumpTime;
+      this->addPoint(this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount()), this->currentTrackElem->getChild(i));
     }
   if (childCount == 1)
-    this->currentTrackElem->children[0]->setName(this->currentTrackElem->getName());
+    this->currentTrackElem->getChild(0)->setName(this->currentTrackElem->getName());
 }
 
@@ -356,6 +397,6 @@
     this->currentTrackElem = tmpElem;
   else
-    printf("TrackElement not Found, leaving unchanged\n");
-  printf("now Working on %d\n", this->currentTrackElem->ID);
+    PRINTF(2)("TrackElement not Found, leaving unchanged\n");
+  PRINTF(4)("now Working on %d\n", this->currentTrackElem->ID);
 
 }
@@ -428,5 +469,5 @@
 int TrackManager::addHotPoint(Vector newPoint)
 {
-  printf("setting up a HotPoint\n");
+  PRINTF(4)("setting up a HotPoint\n");
   if (this->currentTrackElem->isFresh)
     {
@@ -439,5 +480,5 @@
   this->currentTrackElem->nodeCount++;
   this->initChildren(1);
-  this->currentTrackElem = this->currentTrackElem->children[0];
+  this->currentTrackElem = this->currentTrackElem->getChild(0);
 }
 
@@ -451,12 +492,15 @@
 int TrackManager::setSavePoint(void)
 {
-  printf("setting up a SavePoint.\n");
+  PRINTF(4)("setting up a SavePoint.\n");
   if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint)
-    return this->currentTrackElem->children[1]->ID;
+    {
+      PRINTF(2)("%d is already finished \n", currentTrackElem->ID);
+      return this->currentTrackElem->getChild(0)->ID;
+    }
   this->currentTrackElem->isSavePoint = true;
   this->currentTrackElem->isHotPoint = true;
 
   this->initChildren(1);
-  this->currentTrackElem = this->currentTrackElem->children[0];
+  this->currentTrackElem = this->currentTrackElem->getChild(0);
 }
 
@@ -493,5 +537,5 @@
 void TrackManager::forkV(unsigned int count, int* trackIDs)
 {
-  printf("Forking with %d children\n", count);
+  PRINTF(4)("Forking with %d children\n", count);
   if (this->currentTrackElem->isSavePoint)
     return;
@@ -521,26 +565,33 @@
 {
   TrackElement* tmpElem = this->findTrackElementByID(groupID);
-  
-  switch (cond)
-    {
-    case LOWEST:
-      tmpElem->condFunc = &TrackElement::lowest;
-      break;
-    case HIGHEST:
-      tmpElem->condFunc = &TrackElement::highest;
-      break;
-    case RANDOM: 
-      tmpElem->condFunc = &TrackElement::random;
-      break;
-    case LEFTRIGHT:
-      tmpElem->condFunc = &TrackElement::leftRight;
-      break;
-    case NEAREST:
-      tmpElem->condFunc = &TrackElement::nearest;
-      break;
-    case ENEMYKILLED:
-      break;
-    }
-  tmpElem->subject=subject;
+  if (!tmpElem->isFork)
+    {
+      PRINTF(2)("%d is not a Fork, and no condition can be set in this case\n", tmpElem->ID);
+      return;
+    }
+  else
+    {
+      switch (cond)
+	{
+	case LOWEST:
+	  tmpElem->condFunc = &TrackElement::lowest;
+	  break;
+	case HIGHEST:
+	  tmpElem->condFunc = &TrackElement::highest;
+	  break;
+	case RANDOM: 
+	  tmpElem->condFunc = &TrackElement::random;
+	  break;
+	case LEFTRIGHT:
+	  tmpElem->condFunc = &TrackElement::leftRight;
+	  break;
+	case NEAREST:
+	  tmpElem->condFunc = &TrackElement::nearest;
+	  break;
+	case ENEMYKILLED:
+	  break;
+	}
+      tmpElem->subject=subject;
+    }
 }
 
@@ -576,10 +627,10 @@
 void TrackManager::joinV(unsigned int count, int* trackIDs)
 {
-  printf("Joining %d tracks and merging to Track %d\n", count, trackIDs[0]);
+  PRINTF(3)("Joining %d tracks and merging to Track %d\n", count, trackIDs[0]);
 
   // checking if there is a back-loop-connection and ERROR if it is.
   TrackElement* tmpTrackElem = this->findTrackElementByID(trackIDs[0]);
   if (!tmpTrackElem->backLoopCheck(tmpTrackElem))
-    PRINTF(1)("Backloop connection detected at joining trackElements\n");
+    PRINTF(2)("Backloop connection detected at joining trackElements\n");
 
   // chanching work-on to temporary value. going back at the end.
@@ -631,11 +682,15 @@
     }
   if(firstJoint->childCount > 0)
-    for(int i = 0; i < firstJoint->childCount; i++)
-      {
-	printf("Setting startingTime of %d to %f.\n", firstJoint->children[i]->ID, tmpLatestTime);
-	firstJoint->children[i]->startingTime = tmpLatestTime;
-	firstJoint->children[i]->endTime = tmpLatestTime+firstJoint->children[i]->duration;
-      }	
-
+    {
+      TrackElement* enumElem = firstJoint->children->enumerate();
+      while (enumElem)
+	{
+	  PRINTF(4)("Setting startingTime of %d to %f.\n", enumElem->ID, tmpLatestTime);
+	  enumElem->startingTime = tmpLatestTime;
+	  enumElem->endTime = tmpLatestTime + enumElem->duration;
+	  
+	  enumElem = firstJoint->children->nextElement();
+	}
+    }
   // returning to the TrackElement we were working on.
   this->workOn(tmpCurrentWorkingID);
@@ -652,26 +707,32 @@
     {
       TrackElement* tmpElem = findTrackElementByID(i);
-      if (tmpElem->childCount>0 && tmpElem->mainJoin)
-	{
-	  for (int j = 0; j < tmpElem->childCount; j++)
+      if (tmpElem->childCount > 0 && tmpElem->mainJoin)
+	{
+
+	  TrackElement* enumElem = tmpElem->children->enumerate();
+	  while (enumElem)
 	    {
 	      
 	      // c1-continuity
-	      tmpElem->children[j]->curve->addNode(tmpElem->children[j]->curve->getNode(0) +
-						   ((tmpElem->children[j]->curve->getNode(0) - 
+	      enumElem->curve->addNode(enumElem->curve->getNode(0) +
+						   ((enumElem->curve->getNode(0) - 
 						    tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1))
 						    ),2);
-	      tmpElem->children[j]->nodeCount++;
+	      enumElem->nodeCount++;
 	      // c2-continuity
-	      tmpElem->children[j]->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())-
+	      enumElem->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())-
 						    tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 +
 						   tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3);
-	      tmpElem->children[j]->nodeCount++;						    
-	      printf("accelerations: %d-in: count: %d, %f, %f, %f\n                  %d-out: count: %d %f, %f, %f\n",
+	      enumElem->nodeCount++;						    
+	      PRINTF(5)("accelerations: %d-in: count: %d, %f, %f, %f\n                  %d-out: count: %d %f, %f, %f\n",
 		     tmpElem->ID, tmpElem->nodeCount,
 		     tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z,
-		     tmpElem->children[j]->ID, tmpElem->children[j]->nodeCount,
-		     tmpElem->children[j]->curve->calcAcc(0).x, tmpElem->children[j]->curve->calcAcc(0).y, tmpElem->children[j]->curve->calcAcc(0).z);
+		     enumElem->ID, enumElem->nodeCount,
+		     enumElem->curve->calcAcc(0).x, enumElem->curve->calcAcc(0).y, enumElem->curve->calcAcc(0).z);
+	      
+	      enumElem = tmpElem->children->nextElement();
 	    }
+	  
+	  
 	}
     }
@@ -720,5 +781,5 @@
       // jump to the next TrackElement and also set the history of the new Element to the old one.
       TrackElement* tmpHistoryElem = this->currentTrackElem;
-      this->currentTrackElem = this->currentTrackElem->children[this->choosePath(this->currentTrackElem)];
+      this->currentTrackElem = this->currentTrackElem->getChild(this->choosePath(this->currentTrackElem));
       this->currentTrackElem->history = tmpHistoryElem;
     }
