[Orxonox-commit 4790] r9459 - code/branches/Racingbot/src/modules/gametypes

purgham at orxonox.net purgham at orxonox.net
Mon Nov 19 16:19:28 CET 2012


Author: purgham
Date: 2012-11-19 16:19:27 +0100 (Mon, 19 Nov 2012)
New Revision: 9459

Modified:
   code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.cc
   code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.h
   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:
working Version 19.11.2012 - Bots can create virtual Checkpoints but not used jet

Modified: code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.cc	2012-11-19 15:12:58 UTC (rev 9458)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.cc	2012-11-19 15:19:27 UTC (rev 9459)
@@ -1,7 +1,25 @@
 /*
- * SpaceRAceBot.cc
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
  *
- *  Created on: 05.11.2012
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *  Created on: Oct 8, 2012
  *      Author: purgham
  */
 

Modified: code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.h
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.h	2012-11-19 15:12:58 UTC (rev 9458)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceBot.h	2012-11-19 15:19:27 UTC (rev 9459)
@@ -1,7 +1,25 @@
 /*
- * SpaceRaceBot.h
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
  *
- *  Created on: 05.11.2012
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *  Created on: Oct 8, 2012
  *      Author: purgham
  */
 

Modified: code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc	2012-11-19 15:12:58 UTC (rev 9458)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.cc	2012-11-19 15:19:27 UTC (rev 9459)
@@ -22,35 +22,41 @@
  *  Created on: Oct 8, 2012
  *      Author: purgham
  */
- /**
-  * Conventions:
-  * -first Checkpoint has index 0
-  *
-  */
+
+/**
+ * Conventions:
+ * -first Checkpoint has index 0
+ * -staticCheckPoint= static Point (see def over = constructor)
+ */
+
+
+/*TODO:
+ * tICK KORRIGIEREN
+ *
+ *
+ */
 #include <gametypes/SpaceRaceController.h>
 #include "core/CoreIncludes.h"
 #include "core/XMLPort.h"
 #include "gametypes/SpaceRaceManager.h"
 
-
-
-
 namespace orxonox
 {
     CreateFactory(SpaceRaceController);
 
+    const int AdjustDistance = 500;
     /*
      * Idea: Find static Point (checkpoints the spaceship has to reach)
      */
     SpaceRaceController::SpaceRaceController(BaseObject* creator) :
         ArtificialController(creator)
     {
-        RegisterObject(SpaceRaceController);
-        std::vector<RaceCheckPoint*> checkpoints;
+        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);
+            nextRaceCheckpoint_=it->findCheckpoint(0);
         }
 
         OrxAssert(!checkpoints.empty(), "No Checkpoints in Level");
@@ -58,16 +64,89 @@
         staticRacePoints_ = findStaticCheckpoints(checkpoints);
         // initialisation of currentRaceCheckpoint_
         currentRaceCheckpoint_ = NULL;
-        // find first Checkpoint
-        for (int i=0; true; i++){
-            if(checkpoints_[i]->getCheckpointIndex()==0){
-                nextRaceCheckpoint_=checkpoints_[i];
-                break;
+        /*
+         // find first Checkpoint
+         for (int i=0; true; i++){
+         if(checkpoints_[i]->getCheckpointIndex()==0){
+         nextRaceCheckpoint_=checkpoints_[i];
+         break;
+         }
+         }*/
+
+        virtualCheckPointIndex=-1;
+    }
+
+
+    //------------------------------
+    // functions for initialisation
+
+    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);
+
+    }
+
+    /*
+     * called from constructor 'SpaceRaceController'
+     * 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;
+    }
 
+    /*
+     * called from 'findStaticCheckpoints'
+     * return how many ways go from the given Checkpoint to the last Checkpoint (of the Game)
+     */
+    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 =lastPositionSpaceship=this->getControllableEntity()->getPosition();
+                    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
+        }
     }
 
+
+
+    //-------------------------------------
+    // functions for dynamic Way-search
+
     int SpaceRaceController::distanceSpaceshipToCheckPoint(RaceCheckPoint* CheckPoint)
     {
         if (this->getControllableEntity() != NULL)
@@ -77,170 +156,158 @@
         return -1;
     }
 
+    /*
+     * called by: 'tick' or  'adjustNextPoint'
+     * returns the next Checkpoint which the shortest way contains
+     */
     RaceCheckPoint* SpaceRaceController::nextPointFind(RaceCheckPoint* raceCheckpoint)
     {
-        int distances[] ={   -1, -1, -1};
+        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());
+            distances[temp_i] = recCalculateDistance(findCheckpoint(*it), 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]
+                return findCheckpoint(*raceCheckpoint->getNextCheckpoints().end()); // return checkpoint with ID of raceCheckpoint->getNextCheckpoints() [2]
             }
             else
             {
                 std::set<int>::iterator temp = raceCheckpoint->getNextCheckpoints().begin();
-                return checkpoints_[*(++temp)];
+                return findCheckpoint(*(++temp)); // return [1]
             }
         }
         else
         {
             if (distances[2] < distances[0] && distances[2] != -1)
             {
-                return checkpoints_[*raceCheckpoint->getNextCheckpoints().end()]; // return [2]
+                return findCheckpoint(*raceCheckpoint->getNextCheckpoints().end()); // return [2]
             }
             else
             {
-                return checkpoints_[*raceCheckpoint->getNextCheckpoints().begin()]; // return [2]
+                return findCheckpoint(*raceCheckpoint->getNextCheckpoints().begin()); // return [0]
             }
         }
     }
 
-    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_);
-    }
-
+    /*
+     * called from 'nextPointFind'
+     * returns the distance between "currentPosition" and the next static checkpoint that can be reached from "currentCheckPoint"
+     */
     int SpaceRaceController::recCalculateDistance(RaceCheckPoint* currentCheckPoint, Vector3 currentPosition)
     {
-        orxout()<< "rec Aufruf" << endl;
-        // if ( staticCheckPoint was reached)
-        if (std::find(staticRacePoints_.begin(), staticRacePoints_.end(),currentCheckPoint) != staticRacePoints_.end())
+        // find: looks if the currentCheckPoint is a staticCheckPoint (staticCheckPoint is the same as: static Point)
+        if (std::find(staticRacePoints_.begin(), staticRacePoints_.end(), currentCheckPoint) != staticRacePoints_.end())
         {
             return (currentCheckPoint->getPosition() - currentPosition).length();
         }
         else
         {
             int minimum = std::numeric_limits<int>::max();
-            int temp=0;
             for (std::set<int>::iterator it = currentCheckPoint->getNextCheckpoints().begin(); it!= currentCheckPoint->getNextCheckpoints().end(); ++it)
-            { //temp++;
-                WorldEntity* ttt= dynamic_cast<WorldEntity*> (currentCheckPoint);
-                OrxAssert(!(ttt==NULL), "WorldEntity null");
-                OrxAssert(!(ttt->getNode()==NULL), "Node of WorldEntity is null");
+            {
+                int dist_currentCheckPoint_currentPosition = static_cast<int> ((currentPosition- currentCheckPoint->getPosition()).length());
 
-                //orxout()<< temp <<endl;
-                //if(temp==1){orxout()<<currentCheckPoint << " == null?  => "<<(currentCheckPoint==NULL)<<currentPosition<<endl;}
-                Vector3 t=(currentPosition- ttt->getPosition()); //TODO: Find Crash Reason. Why can't currentCheck access node.
-                int tt=static_cast<int>(t.length());
-                //OrxAssert(!currentCheckPoint.empty(), "currentCheckPoint == null");
-                //OrxAssert(!(it == currentCheckPoint->getNextCheckpoints().end()), "it is null");
-                minimum= std::min(minimum, tt+ recCalculateDistance(checkpoints_[(*it)], currentCheckPoint->getPosition()));
+                minimum= std::min(minimum, dist_currentCheckPoint_currentPosition + recCalculateDistance(findCheckpoint(*it), currentCheckPoint->getPosition()));
                 // minimum of distanz from 'currentPosition' to the next static Checkpoint
-            }//Error tritt manchmal auf
+            }
             return minimum;
         }
     }
 
-    /*
-     * returns a vector of static Point (checkpoints the spaceship has to reach)
+    /*called by 'tick'
+     *adjust chosen way of the Spaceship every "AdjustDistance" because spaceship could be displaced through an other one
      */
-    std::vector<RaceCheckPoint*> SpaceRaceController::findStaticCheckpoints(
-            std::vector<RaceCheckPoint*> allCheckpoints)
+    RaceCheckPoint* SpaceRaceController::adjustNextPoint()
     {
-        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++)
+        if (currentRaceCheckpoint_ == NULL) // no Adjust possible
+
         {
-            zaehler->insert(std::pair<RaceCheckPoint*, int>(allCheckpoints[i],0));
+            return nextRaceCheckpoint_;
         }
-        int maxWays = rekSimulationCheckpointsReached(zaehler->begin()->first,&allCheckpoints, zaehler);
+        if ((currentRaceCheckpoint_->getNextCheckpoints()).size() == 1) // no Adjust possible
 
-        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);
-            }
+            return nextRaceCheckpoint_;
         }
-        delete zaehler;
-        return returnVec;
+
+        //Adjust possible
+
+        return nextPointFind(currentRaceCheckpoint_);
     }
 
-    /*
-     *
-     * 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
+    RaceCheckPoint* SpaceRaceController::findCheckpoint(int index) const
+       {
+           for (size_t i = 0; i < this->checkpoints_.size(); ++i)
+           if (this->checkpoints_[i]->getCheckpointIndex() == index)
+           return this->checkpoints_[i];
+           return NULL;
+       }
+
+    bool SpaceRaceController::addVirtualCheckPoint(int positionInNextCheckPoint, RaceCheckPoint* previousCheckpoint, int indexFollowingCheckPoint , Vector3 virtualCheckPointPosition ){
+
+        RaceCheckPoint* newTempRaceCheckPoint = new RaceCheckPoint(this);
+        newTempRaceCheckPoint->setPosition(virtualCheckPointPosition);
+        newTempRaceCheckPoint->setCheckpointIndex(virtualCheckPointIndex);
+        newTempRaceCheckPoint->setLast(false);
+        newTempRaceCheckPoint->setNextCheckpointsAsVector3(Vector3(indexFollowingCheckPoint,-1,-1));
+
+        Vector3 temp = previousCheckpoint->getNextCheckpointsAsVector3();
+        checkpoints_.insert(checkpoints_.end(), newTempRaceCheckPoint);
+        switch(positionInNextCheckPoint){
+            case 0: temp.x=virtualCheckPointIndex; break;
+            case 1: temp.y=virtualCheckPointIndex; break;
+            case 2: temp.z=virtualCheckPointIndex; break;
         }
-        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
-        }
+        virtualCheckPointIndex--;
     }
 
+
+
     SpaceRaceController::~SpaceRaceController()
     {
-        // TODO Auto-generated destructor stub
+        for (int i =-1; i>virtualCheckPointIndex ; i--){
+            delete findCheckpoint(i);
+        }
     }
 
-    void SpaceRaceController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
+    void SpaceRaceController::tick(float dt)
     {
-        SUPER(SpaceRaceController, XMLPort, xmlelement, mode);
-        XMLPortParam(ArtificialController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode).defaultValues(
-                100.0f);
-        XMLPortObject(ArtificialController, WorldEntity, "waypoints", addWaypoint, getWaypoint, xmlelement, mode)
-        ;
+        if (this->getControllableEntity() == NULL || this->getControllableEntity()->getPlayer() == NULL )
+        {   orxout()<<this->getControllableEntity()<< " in tick"<<endl; return;}
+        //FOR virtual Checkpoints
+        if(nextRaceCheckpoint_->getCheckpointIndex() < 0){
+            if( distanceSpaceshipToCheckPoint(nextRaceCheckpoint_) < 30){
+                currentRaceCheckpoint_=nextRaceCheckpoint_;
+                nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_);
+                lastPositionSpaceship=this->getControllableEntity()->getPosition();
+            }
+        }
 
-    }
-    void SpaceRaceController::tick(float dt)
-    {
-        if (this->getControllableEntity() ==  NULL || this->getControllableEntity()->getPlayer() == NULL ){orxout()<<this->getControllableEntity()<< " in tick"<<endl; return;}
         if (nextRaceCheckpoint_->playerWasHere(this->getControllableEntity()->getPlayer()))
         {//Checkpoint erreicht
             currentRaceCheckpoint_=nextRaceCheckpoint_;
             OrxAssert(nextRaceCheckpoint_, "next race checkpoint undefined");
             nextRaceCheckpoint_ = nextPointFind(nextRaceCheckpoint_);
+            lastPositionSpaceship=this->getControllableEntity()->getPosition();
         }
-        else if (std::abs(lastDistance - distanceSpaceshipToCheckPoint(nextRaceCheckpoint_)) < 500)
+        else if ((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()> AdjustDistance)
         {
             nextRaceCheckpoint_ = adjustNextPoint();
+            lastPositionSpaceship=this->getControllableEntity()->getPosition();
         }
+        //korrigieren!
+        else if((lastPositionSpaceship-this->getControllableEntity()->getPosition()).length()<5){\
+            this->moveToPosition(Vector3(rnd()*100,rnd()*100,rnd()*100));
+            this->spin();
+        }
         this->moveToPosition(nextRaceCheckpoint_->getPosition());
+
+
     }
 
 }

Modified: code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h	2012-11-19 15:12:58 UTC (rev 9458)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceController.h	2012-11-19 15:19:27 UTC (rev 9459)
@@ -42,7 +42,8 @@
             RaceCheckPoint* nextRaceCheckpoint_;    // checkpoint that should be reached
             RaceCheckPoint* currentRaceCheckpoint_; // last checkPoint (already reached)
             std::vector<RaceCheckPoint*> checkpoints_;
-            int lastDistance;
+            Vector3 lastPositionSpaceship;
+            int virtualCheckPointIndex;
 
             int recCalculateDistance(RaceCheckPoint*, Vector3 currentPosition);
             int distanceSpaceshipToCheckPoint(RaceCheckPoint*);
@@ -51,6 +52,9 @@
             std::vector<RaceCheckPoint*> findStaticCheckpoints(std::vector<RaceCheckPoint*>);
             std::vector<RaceCheckPoint*> staticCheckpoints();
             int rekSimulationCheckpointsReached(RaceCheckPoint* , std::vector<RaceCheckPoint*>* checkpoints, std::map< RaceCheckPoint*, int>*);
+            // same as SpaceRaceManager, but needed to add virtuell Checkpoints ( Checkpoints which don't exist but needed to avoid collisions with big Objects)
+            RaceCheckPoint* findCheckpoint(int index) const;
+            bool addVirtualCheckPoint(int , RaceCheckPoint* , int , Vector3 );
 
         public:
           SpaceRaceController(BaseObject* creator);

Modified: code/branches/Racingbot/src/modules/gametypes/SpaceRaceManager.cc
===================================================================
--- code/branches/Racingbot/src/modules/gametypes/SpaceRaceManager.cc	2012-11-19 15:12:58 UTC (rev 9458)
+++ code/branches/Racingbot/src/modules/gametypes/SpaceRaceManager.cc	2012-11-19 15:19:27 UTC (rev 9459)
@@ -165,7 +165,8 @@
             {
                 // adjust the radarvisibility
                 gametype->newCheckpointReached(newCheckpoint, player);
-                this->updateRadarVisibility(oldCheckpoint, newCheckpoint);
+                if(player->isHumanPlayer())
+                    this->updateRadarVisibility(oldCheckpoint, newCheckpoint);
             }
         }
 




More information about the Orxonox-commit mailing list