[Orxonox-commit 2262] r6978 - code/branches/ai/src/orxonox/controllers

solex at orxonox.net solex at orxonox.net
Thu May 27 18:40:00 CEST 2010


Author: solex
Date: 2010-05-27 18:39:59 +0200 (Thu, 27 May 2010)
New Revision: 6978

Modified:
   code/branches/ai/src/orxonox/controllers/AIController.cc
   code/branches/ai/src/orxonox/controllers/ArtificialController.cc
   code/branches/ai/src/orxonox/controllers/ArtificialController.h
Log:
consle commands


Modified: code/branches/ai/src/orxonox/controllers/AIController.cc
===================================================================
--- code/branches/ai/src/orxonox/controllers/AIController.cc	2010-05-27 16:18:27 UTC (rev 6977)
+++ code/branches/ai/src/orxonox/controllers/AIController.cc	2010-05-27 16:39:59 UTC (rev 6978)
@@ -22,7 +22,7 @@
  *   Author:
  *      Fabian 'x3n' Landau
  *   Co-authors:
- *      ...
+ *      Dominik Solenicki
  *
  */
 
@@ -60,7 +60,7 @@
 
             if (this->formationFlight_)
             {
-            // return to Master after being forced free
+                // return to Master after being forced free
                 if (this->freedomCount_ == 1)
                 {
                 this->state_ = SLAVE;
@@ -102,15 +102,15 @@
             if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
                 this->searchRandomTargetPosition();
 
-/*
+            // shoot
             random = rnd(maxrand);
-            if (random < 75 && (this->target_ && !this->bShooting_))
+            if (!(this->passive_) && random < 75 && (this->target_ && !this->bShooting_))
                 this->bShooting_ = true;
 
             // stop shooting
             random = rnd(maxrand);
             if (random < 25 && (this->bShooting_))
-                this->bShooting_ = false; */
+                this->bShooting_ = false; 
 
         }
 
@@ -119,7 +119,7 @@
 
         }
 
-        if (this->state_ == MASTER)//MASTER
+        if (this->state_ == MASTER)
         {
 
 
@@ -135,6 +135,9 @@
 
                 if (this->specificMasterAction_ == SPIN)
                     this->spin();
+
+//                 if (this->specificMasterAction_ == FOLLOWHUMAN)
+//                     this->followHuman(this->HumanToFollow_, false);
             }
 
             else {
@@ -187,7 +190,7 @@
 
                 // shoot
                 random = rnd(maxrand);
-                if (random < 5 && (this->target_ && !this->bShooting_))
+                if (!(this->passive_) && random < 15 && (this->target_ && !this->bShooting_))
                 {
                 this->bShooting_ = true;
                 this->forceFreeSlaves();

Modified: code/branches/ai/src/orxonox/controllers/ArtificialController.cc
===================================================================
--- code/branches/ai/src/orxonox/controllers/ArtificialController.cc	2010-05-27 16:18:27 UTC (rev 6977)
+++ code/branches/ai/src/orxonox/controllers/ArtificialController.cc	2010-05-27 16:39:59 UTC (rev 6978)
@@ -22,12 +22,13 @@
  *   Author:
  *      Fabian 'x3n' Landau
  *   Co-authors:
- *      ...
+ *      Dominik Solenicki
  *
  */
 
 #include "ArtificialController.h"
 
+#include <vector>
 #include "core/CoreIncludes.h"
 #include "core/XMLPort.h"
 #include "worldentities/ControllableEntity.h"
@@ -35,14 +36,17 @@
 #include "worldentities/pawns/TeamBaseMatchBase.h"
 #include "gametypes/TeamDeathmatch.h"
 #include "controllers/WaypointPatrolController.h"
+#include "controllers/NewHumanController.h"
 #include "controllers/DroneController.h"
 #include "util/Math.h"
 #include "core/ConsoleCommand.h"
 
 namespace orxonox
 {
-    SetConsoleCommand(ArtificialController, formationflight, true);//.defaultValues(0, true);
+    SetConsoleCommand(ArtificialController, formationflight, true);
     SetConsoleCommand(ArtificialController, masteraction, true);
+    SetConsoleCommand(ArtificialController, followme, true);
+    SetConsoleCommand(ArtificialController, passivbehaviour, true);
 
     static const unsigned int MAX_FORMATION_SIZE = 7;
     static const int FORMATION_LENGTH =  10;
@@ -52,6 +56,7 @@
     static const float ROTATEFACTOR_MASTER = 0.2f;
     static const float SPEED_FREE = 0.8f;
     static const float ROTATEFACTOR_FREE = 0.8f;
+    static const int SECONDS_TO_FOLLOW_HUMAN = 10;
 
     ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator)
     {
@@ -59,6 +64,7 @@
 
         this->target_ = 0;
         this->formationFlight_ = true;
+        this->passive_ = false;
         this->myMaster_ = 0;
         this->freedomCount_ = 0;
 	this->team_ = -1;
@@ -68,6 +74,7 @@
         this->bShooting_ = false;
         this->bHasTargetPosition_ = false;
         this->targetPosition_ = Vector3::ZERO;
+        this->humanToFollow_ = NULL;
 
         this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
     }
@@ -84,7 +91,12 @@
         XMLPortParam(ArtificialController, "formation", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(true);
     }
 
-    //activate/deactivate formationflight
+// Documentation only here to get a faster overview for creating a useful documentation, what you're reading here not intended for an actual documentation...
+
+    /**
+        @brief Activates / deactivates formationflight behaviour
+        @param form activate formflight if form is true
+    */
     void ArtificialController::formationflight(bool form)
     {
         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
@@ -100,7 +112,11 @@
             }
         }
     }
-    //get all masters to do this action
+
+    /**
+        @brief Get all masters to do a specific action 
+        @param action which action to perform (integer, so it can be called with a console command (tmp solution))
+    */
     void ArtificialController::masteraction(int action) 
     {
         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
@@ -117,7 +133,63 @@
         }
     }
 
-// gets called when Bot dies
+    /**
+        @brief A human player gets followed by its nearest master. Initiated by console command, only for demonstration puproses. Does not work at the moment.
+    */
+    void ArtificialController::followme()
+    {
+
+        Pawn *humanPawn = NULL;
+        NewHumanController *currentHumanController = NULL;
+        std::vector<ArtificialController*> allMasters;
+
+        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
+        {
+            if (!it->getController())
+                continue;
+
+            currentHumanController = static_cast<NewHumanController*>(it->getController());
+
+            if(currentHumanController) humanPawn = *it;
+
+            ArtificialController *aiController = static_cast<ArtificialController*>(it->getController());
+
+            if(aiController || aiController->state_ == MASTER)
+                allMasters.push_back(aiController);
+
+        }
+        /*if((humanPawn != NULL) && (allMasters.size != 0))
+        {
+
+                float posHuman = humanPawn->getPosition().length();
+                float distance = 0.0f;
+                float minDistance = posHuman - allMasters.back()->getControllableEntity()->getPosition().length();
+                int index = 0;
+                int i = 0;
+
+                for(std::vector<ArtificialController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++)
+                    {
+                        distance = posHuman - (*it)->getControllableEntity()->getPosition().length();
+                        if(distance < minDistance) index = i;
+                    }
+                allMasters[index].humanToFollow_ = humanPawn;
+                allMasters[index].followHuman(humanPawn, false);
+            }*/
+
+    }
+
+    /**
+        @brief Sets shootingbehaviour of pawns.
+        @param passive if true, pawns won't shoot.
+    */
+    void ArtificialController::passivebehaviour(bool passive)
+    {
+        this->passive_ = passive;
+    }
+
+    /**
+        @brief Gets called if ControllableEntity is changed. Resets the bot when it dies.
+    */
     void ArtificialController::changedControllableEntity()
     {
         if(!getControllableEntity()) 
@@ -130,6 +202,7 @@
         }
     }
 
+
     void ArtificialController::moveToPosition(const Vector3& target)
     {
         if (!this->getControllableEntity())
@@ -209,6 +282,9 @@
         return this->state_;
     }
 
+    /**
+        @brief Unregisters a slave from its master. Called by a slave.
+    */
     void ArtificialController::unregisterSlave() {
         if(myMaster_)
         {
@@ -271,13 +347,18 @@
 
                 break;
             }
-        }//for
-        //hasn't encountered any masters in range? -> become a master
-        if (state_ != SLAVE  && teamSize != 0) state_ = MASTER;//master encounters master? ->done
+        }
 
+        if (state_ != SLAVE  && teamSize != 0) state_ = MASTER;
+
     }
 
-    void ArtificialController::commandSlaves() {
+    /**
+        @brief Commands the slaves of a master into a formation. Called by a master.
+    */
+    void ArtificialController::commandSlaves() 
+    {
+        if(this->state_ != MASTER) return;
 
         Quaternion orient = this->getControllableEntity()->getOrientation();
         Vector3 dest = this->getControllableEntity()->getPosition();
@@ -314,9 +395,12 @@
         }
     }
 
-    // binds slaves to new Master within formation
+    /**
+        @brief Sets a new master within the formation. Called by a master.
+    */
     void ArtificialController::setNewMasterWithinFormation()
     {
+        if(this->state_ != MASTER) return;
 
         if (this->slaves_.empty())
             return;
@@ -339,8 +423,13 @@
 
     }
 
+    /**
+        @brief Frees all slaves form a master. Called by a master.
+    */
     void ArtificialController::freeSlaves()
     {
+        if(this->state_ != MASTER) return;
+
         for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
         {
             (*it)->state_ = FREE;
@@ -348,8 +437,13 @@
         this->slaves_.clear();
     }
 
+    /**
+        @brief Master sets its slaves free for \var FREEDOM_COUNT seconds.
+    */
     void ArtificialController::forceFreeSlaves()
     {
+        if(this->state_ != MASTER) return;
+
         for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
         {
             (*it)->state_ = FREE;
@@ -366,11 +460,16 @@
         this->state_ = FREE;
     }
 
+
     void ArtificialController::forceFreedom()
     {
         this->freedomCount_ = FREEDOM_COUNT;
     }
 
+    /**
+        @brief Checks wether caller has been forced free, decrements time to stay forced free.
+        @return true if forced free.
+    */
     bool ArtificialController::forcedFree()
     {
         if(this->freedomCount_ > 0) 
@@ -380,19 +479,28 @@
         } else return false;
     }
 
+    /**
+        @brief Used to continue a "specific master action" for a certain time.
+    */
     void ArtificialController::specificMasterActionHold()
     {
+        if(this->state_ != MASTER) return;
+
         if (specificMasterActionHoldCount_ == 0) 
          {
             this->specificMasterAction_ = NONE;
             this->searchNewTarget();
-            COUT(0) << "~action end" << std::endl;
          }
         else specificMasterActionHoldCount_--;
     }
 
+    /**
+        @brief Master engages a 180 degree turn. Is a "specific master action".
+    */
     void ArtificialController::turn180()
     {
+        if(this->state_ != MASTER) return;
+
         COUT(0) << "~turn" << std::endl;
 
         Quaternion orient = this->getControllableEntity()->getOrientation();
@@ -402,12 +510,38 @@
         this->specificMasterAction_  =  HOLD;
     }
 
+    /**
+        @brief Master spins around looking directions axis. Is a "specific master action".
+    */
     void ArtificialController::spin()
     {
+        if(this->state_ != MASTER) return;
+
         this->specificMasterAction_  =  NONE;
     }
 
+    /**
+        @brief Master begins to follow a human player. Is a "specific master action".
+        @param humanController human to follow.
+        @param alaways follows human forever if true - yet only inplemented for false.
+    */
+    void ArtificialController::followHuman(Pawn* human, bool always)
+    {
+        if (human == NULL)
+        {
+            this->specificMasterAction_ = NONE;
+            return;
+        }
+        if (!always)
+        {
+            this->setTarget(human);
+            this->specificMasterActionHoldCount_ = SECONDS_TO_FOLLOW_HUMAN;
+            this->specificMasterAction_  =  HOLD;
+        }
 
+    }
+
+
     void ArtificialController::setTargetPosition(const Vector3& target)
     {
         this->targetPosition_ = target;

Modified: code/branches/ai/src/orxonox/controllers/ArtificialController.h
===================================================================
--- code/branches/ai/src/orxonox/controllers/ArtificialController.h	2010-05-27 16:18:27 UTC (rev 6977)
+++ code/branches/ai/src/orxonox/controllers/ArtificialController.h	2010-05-27 16:39:59 UTC (rev 6978)
@@ -35,6 +35,7 @@
 
 #include "util/Math.h"
 #include "Controller.h"
+#include "controllers/NewHumanController.h"
 
 namespace orxonox
 {
@@ -60,6 +61,7 @@
 
             static void formationflight(bool form);
             static void masteraction(int action);
+            static void followme();
 
         protected:
 
@@ -70,9 +72,10 @@
             State state_;
             std::vector<ArtificialController*> slaves_;
             ArtificialController *myMaster_;
-            enum SpecificMasterAction {NONE, HOLD, SPIN, TURN180};
+            enum SpecificMasterAction {NONE, HOLD, SPIN, TURN180, FOLLOWHUMAN};
             SpecificMasterAction specificMasterAction_;
             int specificMasterActionHoldCount_;
+            Pawn* humanToFollow_;
 
             void targetDied();
 
@@ -85,14 +88,17 @@
             void searchNewMaster();
             void commandSlaves();
             void setNewMasterWithinFormation();
+
             void freeSlaves();
             void forceFreeSlaves();
             void loseMasterState();
             void forceFreedom();
             bool forcedFree();
+
             void specificMasterActionHold();
             void turn180();
             void spin();
+            void followHuman(Pawn* humanController, bool always);
 
             void setTargetPosition(const Vector3& target);
             void searchRandomTargetPosition();




More information about the Orxonox-commit mailing list