[Orxonox-commit 1923] r6640 - in code/branches/ai/src/orxonox: controllers worldentities/pawns

solex at orxonox.net solex at orxonox.net
Mon Mar 29 16:23:08 CEST 2010


Author: solex
Date: 2010-03-29 16:23:08 +0200 (Mon, 29 Mar 2010)
New Revision: 6640

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
   code/branches/ai/src/orxonox/worldentities/pawns/Pawn.h
Log:
first bot master/slave behavior

Modified: code/branches/ai/src/orxonox/controllers/AIController.cc
===================================================================
--- code/branches/ai/src/orxonox/controllers/AIController.cc	2010-03-29 14:01:57 UTC (rev 6639)
+++ code/branches/ai/src/orxonox/controllers/AIController.cc	2010-03-29 14:23:08 UTC (rev 6640)
@@ -55,45 +55,65 @@
         float random;
         float maxrand = 100.0f / ACTION_INTERVAL;
 
-        // search enemy
-        random = rnd(maxrand);
-        if (random < 15 && (!this->target_))
-            this->searchNewTarget();
+        if (this->state_==0)
+        {
 
-        // forget enemy
-        random = rnd(maxrand);
-        if (random < 5 && (this->target_))
-            this->forgetTarget();
+            // search master
+            random = rnd(maxrand);
+            if (random < 50 && (!this->target_))
+                this->searchNewMaster();
 
-        // next enemy
-        random = rnd(maxrand);
-        if (random < 10 && (this->target_))
-            this->searchNewTarget();
+            //this->state_=1;
 
-        // fly somewhere
-        random = rnd(maxrand);
-        if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
-            this->searchRandomTargetPosition();
+        }
 
-        // stop flying
-        random = rnd(maxrand);
-        if (random < 10 && (this->bHasTargetPosition_ && !this->target_))
-            this->bHasTargetPosition_ = false;
+        if (this->state_==-1)
+        {
+            
+        }
 
-        // fly somewhere else
-        random = rnd(maxrand);
-        if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
-            this->searchRandomTargetPosition();
+        if (this->state_==1)
+        {
+            // search enemy
+            random = rnd(maxrand);
+            if (random < 15 && (!this->target_))
+                this->searchNewTarget();
 
-        // shoot
-        random = rnd(maxrand);
-        if (random < 75 && (this->target_ && !this->bShooting_))
-            this->bShooting_ = true;
+            // forget enemy
+            random = rnd(maxrand);
+            if (random < 5 && (this->target_))
+                this->forgetTarget();
 
-        // stop shooting
-        random = rnd(maxrand);
-        if (random < 25 && (this->bShooting_))
-            this->bShooting_ = false;
+            // next enemy
+            random = rnd(maxrand);
+            if (random < 10 && (this->target_))
+                this->searchNewTarget();
+
+            // fly somewhere
+            random = rnd(maxrand);
+            if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
+                this->searchRandomTargetPosition();
+
+            // stop flying
+            random = rnd(maxrand);
+            if (random < 10 && (this->bHasTargetPosition_ && !this->target_))
+                this->bHasTargetPosition_ = false;
+
+            // fly somewhere else
+            random = rnd(maxrand);
+            if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
+                this->searchRandomTargetPosition();
+
+            // shoot
+            random = rnd(maxrand);
+            if (random < 75 && (this->target_ && !this->bShooting_))
+                this->bShooting_ = true;
+
+            // stop shooting
+            random = rnd(maxrand);
+            if (random < 25 && (this->bShooting_))
+                this->bShooting_ = false;
+        }
     }
 
     void AIController::tick(float dt)
@@ -101,15 +121,25 @@
         if (!this->isActive())
             return;
 
-        if (this->target_)
-            this->aimAtTarget();
+        if (this->state_==1)
+        {
+            if (this->target_)
+                this->aimAtTarget();
 
-        if (this->bHasTargetPosition_)
-            this->moveToTargetPosition();
+            if (this->bHasTargetPosition_)
+                this->moveToTargetPosition();
 
-        if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f))
-            this->getControllableEntity()->fire(0);
+            if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(Ogre::Math::PI / 20.0f))
+                this->getControllableEntity()->fire(0);
+        }
 
+        if (this->state_==-1)
+        {
+            //this->state_=1;
+
+
+        }
+
         SUPER(AIController, tick, dt);
     }
 

Modified: code/branches/ai/src/orxonox/controllers/ArtificialController.cc
===================================================================
--- code/branches/ai/src/orxonox/controllers/ArtificialController.cc	2010-03-29 14:01:57 UTC (rev 6639)
+++ code/branches/ai/src/orxonox/controllers/ArtificialController.cc	2010-03-29 14:23:08 UTC (rev 6640)
@@ -43,8 +43,8 @@
         RegisterObject(ArtificialController);
 
         this->target_ = 0;
-	this->team_ = 0;//new
-	this->isMaster_ = false;//new
+	this->team_ = -1;//new
+	this->state_ = 0;//new
         this->bShooting_ = false;
         this->bHasTargetPosition_ = false;
         this->targetPosition_ = Vector3::ZERO;
@@ -89,6 +89,46 @@
         this->moveToPosition(this->targetPosition_);
     }
 
+    int ArtificialController::getState()
+    {
+        return this->state_;
+    }
+
+    void ArtificialController::searchNewMaster()
+    {
+        if (!this->getControllableEntity())
+            return;
+
+        this->targetPosition_ = this->getControllableEntity()->getPosition();
+        this->forgetTarget();
+
+        //go through all pawns
+        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
+        {
+            //same team? no: continue
+            if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
+                continue;
+
+            //has it an ArtificialController and is it a master? no: continue
+
+            ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
+            if (controller && controller->getState()!=1)
+                continue;
+
+            //is pawn oneself? && is pawn in range?
+            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity() /*&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000 */)
+            {
+                //this->target_ = (*it);
+                //this->targetPosition_ = it->getPosition();
+                this->state_ = -1;
+
+            }
+        }//for
+
+        //hasn't encountered any masters in range? -> become a master
+        if (state_!=-1) state_=1; // keep in mind: what happens when two masters encounter eache other? -> has to be evaluated in the for loop of within master mode in AIcontroller...
+    }
+
     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-03-29 14:01:57 UTC (rev 6639)
+++ code/branches/ai/src/orxonox/controllers/ArtificialController.h	2010-03-29 14:23:08 UTC (rev 6640)
@@ -58,6 +58,9 @@
             void moveToPosition(const Vector3& target);
             void moveToTargetPosition();
 
+            int getState();
+            void searchNewMaster();
+
             void setTargetPosition(const Vector3& target);
             void searchRandomTargetPosition();
 
@@ -76,7 +79,7 @@
             WeakPtr<Pawn> target_;
             bool bShooting_;
 
-            bool isMaster_;//new
+            int state_;//new master: 1 slave: -1  free: 0 
             int team_;//new
 
         private:

Modified: code/branches/ai/src/orxonox/worldentities/pawns/Pawn.h
===================================================================
--- code/branches/ai/src/orxonox/worldentities/pawns/Pawn.h	2010-03-29 14:01:57 UTC (rev 6639)
+++ code/branches/ai/src/orxonox/worldentities/pawns/Pawn.h	2010-03-29 14:23:08 UTC (rev 6640)
@@ -114,7 +114,7 @@
                 { this->aimPosition_ = position; }
             Vector3 getAimPosition()
                 { return this->aimPosition_; }
-                
+
             virtual const Vector3& getCarrierPosition(void)
                 { return this->getWorldPosition(); };
 




More information about the Orxonox-commit mailing list