Index: code/branches/Racingbot/src/modules/gametypes/CMakeLists.txt
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/CMakeLists.txt	(revision 9429)
+++ code/branches/Racingbot/src/modules/gametypes/CMakeLists.txt	(revision 9432)
@@ -5,4 +5,5 @@
   OldSpaceRace.cc
   OldRaceCheckPoint.cc
+  SpaceRaceBot.cc
   SpaceRaceController.cc
 )
Index: code/branches/Racingbot/src/modules/gametypes/GametypesPrereqs.h
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/GametypesPrereqs.h	(revision 9429)
+++ code/branches/Racingbot/src/modules/gametypes/GametypesPrereqs.h	(revision 9432)
@@ -67,4 +67,6 @@
     class SpaceRace;
     class OldSpaceRace;
+    class SpaceRaceManager;
+    class SpaceRaceBot;
     class SpaceRaceController;
 }
Index: code/branches/Racingbot/src/modules/gametypes/RaceCheckPoint.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/RaceCheckPoint.cc	(revision 9429)
+++ code/branches/Racingbot/src/modules/gametypes/RaceCheckPoint.cc	(revision 9432)
@@ -108,5 +108,5 @@
 
         if (checkpoints.x > -1)
-        this->nextCheckpoints_.insert(static_cast<int>(checkpoints.x + 0.5));
+        this->nextCheckpoints_.insert(static_cast<int>(checkpoints.x + 0.5)); // the red number has the type double and for the cast (to int) is added 0.5 so that the cast works correctly
         if (checkpoints.y > -1)
         this->nextCheckpoints_.insert(static_cast<int>(checkpoints.y + 0.5));
Index: code/branches/Racingbot/src/modules/gametypes/SpaceRace.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRace.cc	(revision 9429)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRace.cc	(revision 9432)
@@ -35,5 +35,5 @@
 #include "util/Convert.h"
 #include "util/Math.h"
-
+#include "SpaceRaceBot.h"
 #include "items/Engine.h"
 
@@ -46,4 +46,5 @@
         RegisterObject(SpaceRace);
 
+        this->botclass_ = Class(SpaceRaceBot);//ClassByString("")
         this->cantMove_ = false;
         this->bTimeIsUp_ = false;
Index: code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.cc	(revision 9432)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.cc	(revision 9432)
@@ -0,0 +1,20 @@
+/*
+ * SpaceRAceBot.cc
+ *
+ *  Created on: 05.11.2012
+ *      Author: purgham
+ */
+
+#include "SpaceRaceBot.h"
+#include "core/CoreIncludes.h"
+
+namespace orxonox
+{
+    CreateFactory(SpaceRaceBot);
+
+    SpaceRaceBot::SpaceRaceBot(BaseObject* creator) : Bot(creator){
+        RegisterObject(SpaceRaceBot);
+        this->defaultController_ = Class(SpaceRaceController);// ClassByString("")
+        this->createController();
+    }
+}
Index: code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.h
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.h	(revision 9432)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.h	(revision 9432)
@@ -0,0 +1,31 @@
+/*
+ * SpaceRaceBot.h
+ *
+ *  Created on: 05.11.2012
+ *      Author: purgham
+ */
+
+#ifndef SPACERACEBOT_H_
+#define SPACERACEBOT_H_
+
+#include "infos/Bot.h"
+#include "gametypes/GametypesPrereqs.h"
+#include "gametypes/SpaceRaceController.h"
+
+
+namespace orxonox
+{
+
+    /**
+
+    */
+    class _GametypesExport SpaceRaceBot: public Bot
+    {
+        public:
+        SpaceRaceBot(BaseObject* creator);
+            virtual ~SpaceRaceBot() {}
+    };
+}
+
+
+#endif /* SPACERACEBOT_H_ */
Index: code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc	(revision 9429)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc	(revision 9432)
@@ -28,5 +28,4 @@
 #include "core/XMLPort.h"
 #include "gametypes/SpaceRaceManager.h"
-
 
 // Von SpaceRaceManager points einlesen
@@ -35,62 +34,185 @@
 namespace orxonox
 {
+    CreateFactory(SpaceRaceController);
 
     /*
      * Idea: Find static Point (checkpoints the spaceship has to reach)
      */
-SpaceRaceController::SpaceRaceController(BaseObject* creator): ArtificialController(creator)
-{
-
-    std::vector<RaceCheckPoint*> checkpoints;
-    // TODO Auto-generated constructor stub
-    for (ObjectList<SpaceRaceManager >::iterator it = ObjectList<SpaceRaceManager>::begin(); it != ObjectList<SpaceRaceManager>::end(); ++it)
-        checkpoints=it->getAllCheckpoints();
-    //OrxAssert(checkpoints, "No Checkpoints in Level");
-
+    SpaceRaceController::SpaceRaceController(BaseObject* creator) :
+        ArtificialController(creator)
+    {
+        RegisterObject(SpaceRaceController);
+
+        std::vector<RaceCheckPoint*> checkpoints;
+        for (ObjectList<SpaceRaceManager>::iterator it = ObjectList<SpaceRaceManager>::begin(); it!= ObjectList<SpaceRaceManager>::end(); ++it)
+        {
+            checkpoints = it->getAllCheckpoints();
+            nextRaceCheckpoint_=it->findCheckpoint(1);
+        }
+
+        OrxAssert(!checkpoints.empty(), "No Checkpoints in Level");
+        checkpoints_=checkpoints;
+        staticRacePoints_ = findStaticCheckpoints(checkpoints);
+
+    }
+
+    int SpaceRaceController::distanceSpaceshipToCheckPoint(RaceCheckPoint* CheckPoint)
+    {
+        if (this->getControllableEntity() != NULL)
+        {
+            return (CheckPoint->getPosition()- this->getControllableEntity()->getPosition()).length();
+        }
+        return -1;
+    }
+
+    RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint)
+    {
+        int distances[] = { -1, -1, -1 };
+        int temp_i = 0;
+        for (std::set<int>::iterator it =raceCheckpoint->getNextCheckpoints().begin(); it!= raceCheckpoint->getNextCheckpoints().end(); ++it)
+        {
+            distances[temp_i] = recCalculateDistance(raceCheckpoint,this->getControllableEntity()->getPosition());
+            temp_i++;
+        }
+        if (distances[0] > distances[1] && distances[1] != -1)
+        {
+            if (distances[2] < distances[1] && distances[2] != -1)
+            {
+                return checkpoints_[*raceCheckpoint->getNextCheckpoints().end()]; // return [2]
+            }
+            else
+            {
+                std::set<int>::iterator temp = raceCheckpoint->getNextCheckpoints().begin();
+                return checkpoints_[*(++temp)];
+            }
+        }
+        else
+        {
+            if (distances[2] < distances[0] && distances[2] != -1)
+            {
+                return checkpoints_[*raceCheckpoint->getNextCheckpoints().end()]; // return [2]
+            }
+            else
+            {
+                return checkpoints_[*raceCheckpoint->getNextCheckpoints().begin()]; // return [2]
+            }
+        }
+    }
+
+    RaceCheckPoint* SpaceRaceController::adjustNextPoint()
+    {
+        if (currentRaceCheckpoint_ == NULL) // no Adjust possible
+        {
+            return nextRaceCheckpoint_;
+        }
+        if ((currentRaceCheckpoint_->getNextCheckpoints()).size() == 1) // no Adjust possible
+        {
+            return nextRaceCheckpoint_;
+        }
+
+        //Adjust possible
+
+        return nextPointFind(currentRaceCheckpoint_);
+    }
+
+    int SpaceRaceController::recCalculateDistance(RaceCheckPoint* currentCheckPoint, Vector3 currentPosition)
+    {
+        // if ( staticCheckPoint was reached)
+        if (std::find(staticRacePoints_.begin(), staticRacePoints_.end(),currentCheckPoint) != staticRacePoints_.end())
+        {
+            return (currentCheckPoint->getPosition() - currentPosition).length();
+        }
+        else
+        {
+            int minimum = std::numeric_limits<int>::max();
+            for (std::set<int>::iterator it = currentCheckPoint->getNextCheckpoints().begin(); it!= currentCheckPoint->getNextCheckpoints().end(); ++it)
+            {
+                minimum= std::min(minimum,(int) (currentPosition- currentCheckPoint->getPosition()).length() + recCalculateDistance(checkpoints_[(*it)], currentCheckPoint->getPosition()));
+            }//TODO: fix cast
+            return minimum;
+        }
+    }
+
+    /*
+     * returns a vector of static Point (checkpoints the spaceship has to reach)
+     */
+    std::vector<RaceCheckPoint*> SpaceRaceController::findStaticCheckpoints(
+            std::vector<RaceCheckPoint*> allCheckpoints)
+    {
+        std::map<RaceCheckPoint*, int> * zaehler = new std::map<
+                RaceCheckPoint*, int>(); // counts how many times the checkpoit was reached (for simulation)
+        for (unsigned int i = 0; i < allCheckpoints.size(); i++)
+        {
+            zaehler->insert(std::pair<RaceCheckPoint*, int>(allCheckpoints[i],0));
+        }
+        int maxWays = rekSimulationCheckpointsReached(zaehler->begin()->first,&allCheckpoints, zaehler);
+
+        std::vector<RaceCheckPoint*> returnVec;
+        returnVec.clear();
+        for (std::map<RaceCheckPoint*, int>::iterator iter = zaehler->begin(); iter!= zaehler->end(); iter++)
+        {
+            if (iter->second == maxWays)
+            {
+                //returnVec.insert(allCheckpoints[1]);
+                returnVec.insert(returnVec.end(), iter->first);
+            }
+        }
+        delete zaehler;
+        return returnVec;
+    }
+
+    /*
+     *
+     * return how many ways go from the given checkpoint to the last one
+     */
+    int SpaceRaceController::rekSimulationCheckpointsReached(RaceCheckPoint* currentCheckpoint, std::vector<RaceCheckPoint*>* checkpoints, std::map<RaceCheckPoint*, int>* zaehler)
+    {
+        if (currentCheckpoint->isLast())
+        {// last point reached
+            (*zaehler)[currentCheckpoint] += 1;
+            return 1; // 1 Way form the last point to this one
+        }
+        else
+        {
+            int numberOfWays = 0; // counts number of ways from this Point to the last point
+            for (std::set<int>::iterator it =
+                    currentCheckpoint->getNextCheckpoints().begin(); it
+                    != currentCheckpoint->getNextCheckpoints().end(); ++it)
+            {
+                numberOfWays += rekSimulationCheckpointsReached((*checkpoints)[(*it)], checkpoints, zaehler);
+            }
+            (*zaehler)[currentCheckpoint] += numberOfWays;
+            return numberOfWays; // returns the number of ways from this point to the last one
+        }
+    }
+
+    SpaceRaceController::~SpaceRaceController()
+    {
+        // TODO Auto-generated destructor stub
+    }
+
+    void SpaceRaceController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
+    {
+        SUPER(SpaceRaceController, XMLPort, xmlelement, mode);
+        XMLPortParam(ArtificialController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(
+                100.0f);
+        XMLPortObject(ArtificialController, WorldEntity, "waypoints", addWaypoint, getWaypoint, xmlelement, mode)
+        ;
+
+    }
+    void SpaceRaceController::tick(float dt)
+    {
+        if (nextRaceCheckpoint_->playerWasHere(this->getControllableEntity()->getPlayer()))
+        {//Checkpoint erreicht
+            currentRaceCheckpoint_=nextRaceCheckpoint_;
+            OrxAssert(nextRaceCheckpoint_, "next race checkpoint undefined");
+            nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_);
+        }
+        else if (std::abs(lastDistance - distanceSpaceshipToCheckPoint(nextRaceCheckpoint_)) < 500)
+        {
+            nextRaceCheckpoint_ = adjustNextPoint();
+        }
+        this->moveToPosition(nextRaceCheckpoint_->getPosition());
+    }
 
 }
-/*
-RaceCheckPoint* SpaceRaceController::nextPoint(){
-    return NULL;
-}*/
-
-std::vector<RaceCheckPoint*> SpaceRaceController::findStaticCheckpoints(std::vector<RaceCheckPoint*> allCheckpoints){
-    std::map< RaceCheckPoint*, int> * zaehler= new std::map< RaceCheckPoint*, int>(); // counts how many times the checkpoit was reached for simulation
-    for (unsigned int i=0; i<allCheckpoints.size() ; i++){
-        zaehler->insert ( std::pair<RaceCheckPoint*,int>(allCheckpoints[i], 0));
-    }
-    rekSimulationCheckpointsReached(zaehler->begin()->first, & allCheckpoints, zaehler);
-    //Werte auslesen und statische Checkpoints bestimmen
-    delete zaehler;
-
-}
-
-void SpaceRaceController::rekSimulationCheckpointsReached(RaceCheckPoint* currentCheckpoint, std::vector<RaceCheckPoint*>* checkpoints, std::map< RaceCheckPoint*, int>* zaehler){
-    zaehler->at(currentCheckpoint)++;
-    if(!currentCheckpoint->isLast()){
-        for( std::set<int>::iterator it=currentCheckpoint->getNextCheckpoints().begin(); it != currentCheckpoint->getNextCheckpoints().end(); ++it){
-            rekSimulationCheckpointsReached( (*checkpoints)[(*it)], checkpoints, zaehler);
-        }
-    }
-}
-
-
-SpaceRaceController::~SpaceRaceController()
-{
-    // TODO Auto-generated destructor stub
-}
-
-void SpaceRaceController::XMLPort(Element& xmlelement, XMLPort::Mode mode){
-            SUPER(SpaceRaceController, XMLPort, xmlelement, mode);
-
-            XMLPortParam(ArtificialController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(100.0f);
-            XMLPortObject(ArtificialController, WorldEntity, "waypoints", addWaypoint, getWaypoint,  xmlelement, mode);
-
-}
-void SpaceRaceController::tick(float dt){
-
-}
-
-
-
-}
Index: code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h	(revision 9429)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h	(revision 9432)
@@ -38,11 +38,19 @@
     class _GametypesExport SpaceRaceController : public ArtificialController, public Tickable
     {
-        private:/*
-            vector<RaceCheckPoint*> lastRaceCheckpoint;
-            vector<RaceCheckPoint*> nextRaceCheckpoint;*/
-            //RaceCheckPoint* nextPoint();
+        private:
+            std::vector<RaceCheckPoint*> staticRacePoints_;
+            RaceCheckPoint* nextRaceCheckpoint_;
+            RaceCheckPoint* currentRaceCheckpoint_;
+            std::vector<RaceCheckPoint*> checkpoints_;
+            int lastDistance;
+
+            int recCalculateDistance(RaceCheckPoint*, Vector3 currentPosition);
+            int distanceSpaceshipToCheckPoint(RaceCheckPoint*);
+            RaceCheckPoint* nextPointFind(RaceCheckPoint*);
+            RaceCheckPoint* adjustNextPoint();
             std::vector<RaceCheckPoint*> findStaticCheckpoints(std::vector<RaceCheckPoint*>);
             std::vector<RaceCheckPoint*> staticCheckpoints();
-            void rekSimulationCheckpointsReached(RaceCheckPoint* , std::vector<RaceCheckPoint*>* checkpoints, std::map< RaceCheckPoint*, int>*);
+            int rekSimulationCheckpointsReached(RaceCheckPoint* , std::vector<RaceCheckPoint*>* checkpoints, std::map< RaceCheckPoint*, int>*);
+
         public:
           SpaceRaceController(BaseObject* creator);
Index: code/branches/Racingbot/src/modules/gametypes/SpaceRaceManager.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceManager.cc	(revision 9429)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceManager.cc	(revision 9432)
@@ -45,10 +45,9 @@
         BaseObject(creator)
     {
-        RegisterObject(SpaceRaceManager)
-;        this->race_ = orxonox_cast<SpaceRace*>(this->getGametype().get());
+        RegisterObject(SpaceRaceManager);
+        this->race_ = orxonox_cast<SpaceRace*>(this->getGametype().get());
         assert(race_);
         //amountOfPlayers=(race_->getPlayers()).size();
         this->firstcheckpointvisible_ = false;
-        this->players_ = this->race_->getPlayers();
     }
 
@@ -70,4 +69,6 @@
         SUPER(SpaceRaceManager,tick,dt);
 
+        this->players_ = this->race_->getPlayers();
+
         if (this->checkpoints_[0] != NULL && !this->firstcheckpointvisible_)
         {
@@ -78,8 +79,10 @@
         for ( std::map< PlayerInfo*, Player>::iterator it = players_.begin(); it != players_.end(); ++it)
         {
+
             for (size_t i = 0; i < this->checkpoints_.size(); ++i)
             {
-                if (this->checkpoints_[i]->playerWasHere(it->first))
+                if (this->checkpoints_[i]->playerWasHere(it->first)){
                 this->checkpointReached(this->checkpoints_[i], it->first /*this->checkpoints_[i]->getPlayer()*/);
+                }
             }
         }
@@ -142,5 +145,8 @@
 
         RaceCheckPoint* oldCheckpoint = gametype->getCheckpointReached(player); // returns the last from player reached checkpoint
-
+        // % fixing
+        orxout() << "SpaceRaceManager.checkpointReached( "<<newCheckpoint->getCheckpointIndex()
+                 <<", "<< player->isHumanPlayer() << endl;
+        // % end fixing
         if (this->reachedValidCheckpoint(oldCheckpoint, newCheckpoint, player))
         {
