Index: /orxonox/trunk/src/track_manager.cc
===================================================================
--- /orxonox/trunk/src/track_manager.cc	(revision 3834)
+++ /orxonox/trunk/src/track_manager.cc	(revision 3835)
@@ -48,11 +48,12 @@
   this->width = TMAN_DEFAULT_WIDTH;
   this->nodeCount = 0;
+  this->curve = NULL;
   this->childCount = 0;
+  this->children = NULL;
   this->name = NULL;
-  this->curve = NULL;
-  this->children = NULL;
 
   this->history = NULL;
 
+  this->subject = NULL;
   this->condFunc = &TrackElement::random;
 }
@@ -64,9 +65,12 @@
 TrackElement::~TrackElement(void)
 {
-  if (this->name)
-    delete []name;
-  if (this->curve)
-    delete this->curve;
-  if ((!this->isJoined &&this->childCount > 0) || (this->isJoined && this->mainJoin))
+  // deleting the Name
+  delete []name;
+  // deleting the Curve
+  delete this->curve;
+
+  // deleting all the Children of this TrackNode.
+  if ((!this->isJoined &&this->childCount > 0) 
+      || (this->isJoined && this->mainJoin)) // only if this is the MainJoin.
     {
       tIterator<TrackElement>* iterator = this->children->getIterator();
@@ -86,6 +90,4 @@
    \param trackID The ID to search for.
    \returns The TrackElement if Found, NULL otherwise.
-   
-   \todo make this more modular, to search for different things
 */
 TrackElement* TrackElement::findByID(unsigned int trackID)
@@ -94,5 +96,5 @@
   if (this->ID == trackID)
     return this;
-  // search on.
+  // search all children
   if (this->childCount > 0)
     {
@@ -108,4 +110,5 @@
       delete iterator;
     }
+  // if not found
   else 
     return NULL;
@@ -114,40 +117,77 @@
 
 /**
-   \brief checks if there are any BackLoops in the Track
-   \param trackElem the trackElement to check about
-   it simply does this by looking if the current trackElem is found again somewhere else in the Track
-*/
-bool TrackElement::backLoopCheck(TrackElement* trackElem)
-{
-  if (this->childCount == 0)
-    return true;
-  else
+   \brief Searches through all the TrackElements for a trackName
+   \param trackName The name to search for.
+   \returns The TrackElement if Found, NULL otherwise.
+*/
+TrackElement* TrackElement::findByName(const char* trackName)
+{
+  // return if Found.
+  if (this->name && !strcmp(this->name, trackName))
+    return this;
+  // search all children
+  if (this->childCount > 0)
     {
       tIterator<TrackElement>* iterator = this->children->getIterator();
       TrackElement* enumElem = iterator->nextElement();
+      TrackElement* tmpElem;
       while (enumElem)
 	{
-	  if(!enumElem->backLoopCheck(trackElem))
-	    return false;
+	  if ((tmpElem = enumElem->findByName(trackName)))
+	    return tmpElem;
 	  enumElem = iterator->nextElement();
 	}
       delete iterator;
-      
-      return true;
-    }
+    }
+  // if not found
+  else 
+    return NULL;
+}
+
+/**
+   \brief checks if there are any BackLoops in the Track (Backloops only 
+   \param trackElem the trackElement to check about
+   \returns true if NO loop was found, false Otherwise
+   You actually have to act on false!!
+   it simply does this by looking if the current trackElem is found again somewhere else in the Track
+*/
+bool TrackElement::backLoopCheck(const TrackElement* trackElem, unsigned int depth) const
+{
+  if(depth == 0 || this != trackElem)
+    {
+      if (this->children)
+	{
+	  tIterator<TrackElement>* iterator = this->children->getIterator();
+	  TrackElement* enumElem = iterator->nextElement();
+	  while (enumElem)
+	    {
+	      if(!enumElem->backLoopCheck(trackElem, depth + 1))
+		return false;
+	      enumElem = iterator->nextElement();
+	    }
+	  delete iterator;
+	}
+    }
+  else
+    return false;
+
+  // only returns if everything worked out
+  return true;
 }
 
 /**
    \param childNumber which child to return
-   \returns the n-the children (starting at 0)
-*/
-TrackElement* TrackElement::getChild(int childCount)
-{
+   \returns the n-the children (starting at 0). 
+   Be aware, that when the trackElement has no Children, NULL will be returned
+*/
+TrackElement* TrackElement::getChild(int childCount) const
+{
+  // if the the trackElement has no children return NULL.
   if (this->childCount == 0)
     return NULL;
+  // we cannot return the childCount+m's Child, so we return the last.
   if (childCount > this->childCount)
     childCount = this->childCount;
   
-  //  TrackElement* enumElem = this->children->enumerate();
   tIterator<TrackElement>* iterator = this->children->getIterator();
   TrackElement* enumElem = iterator->nextElement();
@@ -166,5 +206,5 @@
   if (this->name)
     delete []this->name;
-  // if a name was given already.
+  // if a name was given.
   if (name)
     {
@@ -179,5 +219,5 @@
    \returns The name of this TrackElement
 */
-char* TrackElement::getName(void) const
+const char* TrackElement::getName(void) const
 {
   return this->name;
@@ -187,5 +227,5 @@
    \brief prints out debug information about this TrackElement
 */
-void TrackElement::debug(void)
+void TrackElement::debug(void) const
 {
   PRINT(0)("--== TrackElement:%i ==--", this->ID);
@@ -235,5 +275,5 @@
    \returns the chosen child
 */
-int TrackElement::lowest(void* nothing)
+int TrackElement::lowest(const void* nothing) const
 {
   return 0;
@@ -245,5 +285,5 @@
    \returns the chosen child
 */
-int TrackElement::highest(void* nothing)
+int TrackElement::highest(const void* nothing) const
 { 
   return this->childCount-1;
@@ -255,5 +295,5 @@
    \returns the chosen child
 */
-int TrackElement::random(void* nothing)
+int TrackElement::random(const void* nothing) const
 {
   int i = (int)floor ((float)rand()/(float)RAND_MAX * (float)this->childCount);
@@ -270,5 +310,5 @@
    \returns the chosen child
 */
-int TrackElement::leftRight(void* node)
+int TrackElement::leftRight(const void* node) const
 {
   PNode* tmpNode = (PNode*)node;
@@ -291,5 +331,5 @@
    (play with this!!).
 */
-int TrackElement::nearest(void* node)
+int TrackElement::nearest(const void* node) const
 {
   PNode* tmpNode = (PNode*)node;
Index: /orxonox/trunk/src/track_manager.h
===================================================================
--- /orxonox/trunk/src/track_manager.h	(revision 3834)
+++ /orxonox/trunk/src/track_manager.h	(revision 3835)
@@ -37,9 +37,10 @@
 
   TrackElement* findByID(unsigned int trackID);
-  bool backLoopCheck(TrackElement* trackElem);
+  TrackElement* findByName(const char* trackName);
+  bool backLoopCheck(const TrackElement* trackElem, unsigned int depth = 0) const;
 
-  TrackElement* getChild(int childNumber);
+  TrackElement* getChild(int childNumber) const;
   void setName(const char* name);
-  char* getName(void) const;
+  const char* getName(void) const;
 
   // atributes
@@ -65,16 +66,16 @@
   TrackElement* history;     //!< a pointer to the last TrackElement we were on. This is if you want to walk the path backwards again.
 
-  void debug(void);
+  void debug(void) const;
 
   // CONDITION FUNCTIONS and STUFF
   void* subject;             //!< The Subject the Condition should act upon.
-  int (TrackElement::*condFunc)(void*); //!< Pointer to the condition function
+  int (TrackElement::*condFunc)(const void*) const; //!< Pointer to the condition function
 
-  int lowest(void* nothing);
-  int highest(void* nothing);
-  int random(void* nothing);
+  int lowest(const void* nothing) const;
+  int highest(const void* nothing) const;
+  int random(const void* nothing) const;
 
-  int leftRight(void* node);
-  int nearest(void* node);
+  int leftRight(const void* node) const;
+  int nearest(const void* node) const;
   // todo  int enemyKilled(void* entity);
 
