[Orxonox-commit 4763] r9432 - code/branches/Racingbot/src/modules/gametypes

purgham at orxonox.net purgham at orxonox.net
Mon Nov 5 15:58:03 CET 2012


Author: purgham
Date: 2012-11-05 15:58:03 +0100 (Mon, 05 Nov 2012)
New Revision: 9432

Added:
   code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.cc
   code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.h
Modified:
   code/branches/Racingbot/src/modules/gametypes/CMakeLists.txt
   code/branches/Racingbot/src/modules/gametypes/GametypesPrereqs.h
   code/branches/Racingbot/src/modules/gametypes/RaceCheckPoint.cc
   code/branches/Racingbot/src/modules/gametypes/SpaceRace.cc
   code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc
   code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h
   code/branches/Racingbot/src/modules/gametypes/SpaceRaceManager.cc
Log:
first working Version

Modified: code/branches/Racingbot/src/modules/gametypes/CMakeLists.txt
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/CMakeLists.txt	2012-11-05 14:55:40 UTC (rev 9431)
+++ code/branches/Racingbot/src/modules/gametypes/CMakeLists.txt	2012-11-05 14:58:03 UTC (rev 9432)
@@ -4,6 +4,7 @@
   SpaceRaceManager.cc
   OldSpaceRace.cc
   OldRaceCheckPoint.cc
+  SpaceRaceBot.cc
   SpaceRaceController.cc
 )
 

Modified: code/branches/Racingbot/src/modules/gametypes/GametypesPrereqs.h
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/GametypesPrereqs.h	2012-11-05 14:55:40 UTC (rev 9431)
+++ code/branches/Racingbot/src/modules/gametypes/GametypesPrereqs.h	2012-11-05 14:58:03 UTC (rev 9432)
@@ -66,6 +66,8 @@
 {
     class SpaceRace;
     class OldSpaceRace;
+    class SpaceRaceManager;
+    class SpaceRaceBot;
     class SpaceRaceController;
 }
 

Modified: code/branches/Racingbot/src/modules/gametypes/RaceCheckPoint.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/RaceCheckPoint.cc	2012-11-05 14:55:40 UTC (rev 9431)
+++ code/branches/Racingbot/src/modules/gametypes/RaceCheckPoint.cc	2012-11-05 14:58:03 UTC (rev 9432)
@@ -107,7 +107,7 @@
         this->nextCheckpoints_.clear();
 
         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));
         if (checkpoints.z > -1)

Modified: code/branches/Racingbot/src/modules/gametypes/SpaceRace.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRace.cc	2012-11-05 14:55:40 UTC (rev 9431)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRace.cc	2012-11-05 14:58:03 UTC (rev 9432)
@@ -34,7 +34,7 @@
 #include "chat/ChatManager.h"
 #include "util/Convert.h"
 #include "util/Math.h"
-
+#include "SpaceRaceBot.h"
 #include "items/Engine.h"
 
 namespace orxonox
@@ -45,6 +45,7 @@
     {
         RegisterObject(SpaceRace);
 
+        this->botclass_ = Class(SpaceRaceBot);//ClassByString("")
         this->cantMove_ = false;
         this->bTimeIsUp_ = false;
         this->numberOfBots_ = 0; // quick fix: don't allow default-bots to enter the race

Added: code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.cc	                        (rev 0)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.cc	2012-11-05 14:58:03 UTC (rev 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();
+    }
+}

Added: code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.h
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.h	                        (rev 0)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.h	2012-11-05 14:58:03 UTC (rev 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_ */

Modified: code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc	2012-11-05 14:55:40 UTC (rev 9431)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc	2012-11-05 14:58:03 UTC (rev 9432)
@@ -28,69 +28,191 @@
 #include "core/XMLPort.h"
 #include "gametypes/SpaceRaceManager.h"
 
-
 // Von SpaceRaceManager points einlesen
 // Berechnungsklasse nextPoint zur verfuegung stellen
 // ^- aufrufen ueber tick (if ob noetig)
 namespace orxonox
 {
+    CreateFactory(SpaceRaceController);
 
     /*
      * Idea: Find static Point (checkpoints the spaceship has to reach)
      */
-SpaceRaceController::SpaceRaceController(BaseObject* creator): ArtificialController(creator)
-{
+    SpaceRaceController::SpaceRaceController(BaseObject* creator) :
+        ArtificialController(creator)
+    {
+        RegisterObject(SpaceRaceController);
 
-    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");
+        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);
 
-}
-/*
-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));
+    int SpaceRaceController::distanceSpaceshipToCheckPoint(RaceCheckPoint* CheckPoint)
+    {
+        if (this->getControllableEntity() != NULL)
+        {
+            return (CheckPoint->getPosition()- this->getControllableEntity()->getPosition()).length();
+        }
+        return -1;
     }
-    rekSimulationCheckpointsReached(zaehler->begin()->first, & allCheckpoints, zaehler);
-    //Werte auslesen und statische Checkpoints bestimmen
-    delete zaehler;
 
-}
+    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]
+            }
+        }
+    }
 
-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);
+    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;
+        }
+    }
 
-SpaceRaceController::~SpaceRaceController()
-{
-    // TODO Auto-generated destructor stub
-}
+    /*
+     * 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);
 
-void SpaceRaceController::XMLPort(Element& xmlelement, XMLPort::Mode mode){
-            SUPER(SpaceRaceController, XMLPort, xmlelement, mode);
+        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;
+    }
 
-            XMLPortParam(ArtificialController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(100.0f);
-            XMLPortObject(ArtificialController, WorldEntity, "waypoints", addWaypoint, getWaypoint,  xmlelement, mode);
+    /*
+     *
+     * 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
+        }
+    }
 
-}
-void SpaceRaceController::tick(float dt){
+    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());
+    }
 
-
 }

Modified: code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h	2012-11-05 14:55:40 UTC (rev 9431)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h	2012-11-05 14:58:03 UTC (rev 9432)
@@ -37,13 +37,21 @@
 {
     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);
           virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);

Modified: code/branches/Racingbot/src/modules/gametypes/SpaceRaceManager.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceManager.cc	2012-11-05 14:55:40 UTC (rev 9431)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceManager.cc	2012-11-05 14:58:03 UTC (rev 9432)
@@ -44,12 +44,11 @@
     SpaceRaceManager::SpaceRaceManager(BaseObject* creator) :
         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();
     }
 
     SpaceRaceManager::~SpaceRaceManager()
@@ -69,6 +68,8 @@
     {
         SUPER(SpaceRaceManager,tick,dt);
 
+        this->players_ = this->race_->getPlayers();
+
         if (this->checkpoints_[0] != NULL && !this->firstcheckpointvisible_)
         {
             this->checkpoints_[0]->setRadarVisibility(true);
@@ -77,10 +78,12 @@
 
         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()*/);
+                }
             }
         }
 
@@ -141,7 +144,10 @@
         return;
 
         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))
         {
             // the player reached a valid checkpoint




More information about the Orxonox-commit mailing list