[Orxonox-commit 1978] r6695 - code/branches/ai/src/orxonox/controllers

solex at orxonox.net solex at orxonox.net
Mon Apr 12 16:18:12 CEST 2010


Author: solex
Date: 2010-04-12 16:18:12 +0200 (Mon, 12 Apr 2010)
New Revision: 6695

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:
master now lists slaves

Modified: code/branches/ai/src/orxonox/controllers/AIController.cc
===================================================================
--- code/branches/ai/src/orxonox/controllers/AIController.cc	2010-04-12 14:08:15 UTC (rev 6694)
+++ code/branches/ai/src/orxonox/controllers/AIController.cc	2010-04-12 14:18:12 UTC (rev 6695)
@@ -49,6 +49,8 @@
     AIController::~AIController()
     {
         if (this->state_ == MASTER) freeAllSlaves();
+        if (this->state_ == SLAVE) unregisterSlave();
+        this->slaves.clear();
     }
 
     void AIController::action()
@@ -59,13 +61,12 @@
         if (this->state_ == FREE)//FREE
         {
 
+            //this->state_ = MASTER;
             // search master
-            random = rnd(maxrand);
-            if (random < 50 && (!this->target_))
+            //random = rnd(maxrand);
+            //if (random < 101 && (!this->target_))
                 this->searchNewMaster();
 
-
-
         }
 
         if (this->state_ == SLAVE)//SLAVE
@@ -77,6 +78,7 @@
         {
             // command slaves
             this->commandSlaves();
+
             // search enemy
             random = rnd(maxrand);
             if (random < 15 && (!this->target_))
@@ -129,14 +131,14 @@
             if (this->target_)
                 this->aimAtTarget();
 
-            /*if (this->bHasTargetPosition_)
+            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->state_==SLAVE)
+        if (this->state_ == SLAVE)
         {
 
             if (this->bHasTargetPosition_)

Modified: code/branches/ai/src/orxonox/controllers/ArtificialController.cc
===================================================================
--- code/branches/ai/src/orxonox/controllers/ArtificialController.cc	2010-04-12 14:08:15 UTC (rev 6694)
+++ code/branches/ai/src/orxonox/controllers/ArtificialController.cc	2010-04-12 14:18:12 UTC (rev 6695)
@@ -43,6 +43,8 @@
         RegisterObject(ArtificialController);
 
         this->target_ = 0;
+        this->myMaster_ = 0;
+        //this->slaveNumber_ = -1;
 	this->team_ = -1;//new
 	this->state_ = FREE;//new
         this->bShooting_ = false;
@@ -94,8 +96,16 @@
         return this->state_;
     }
 
+    void ArtificialController::unregisterSlave() {
+        if(myMaster_)
+        {
+            myMaster_->slaves.remove(this);
+        }
+    }
+
     void ArtificialController::searchNewMaster()
     {
+
         if (!this->getControllableEntity())
             return;
 
@@ -105,49 +115,64 @@
         //go through all pawns
         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
         {
-            //same team? no: continue
+
+            //same team?
             if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
                 continue;
 
-            //has it an ArtificialController and is it a master? no: continue
+            //has it an ArtificialController and is it a master?
+            if (!it->getController())
+                continue;
 
             ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
-            if (controller && controller->getState() != MASTER)
+            if (!controller || controller->getState() != MASTER)
                 continue;
 
             //is pawn oneself? && is pawn in range?
-            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity() /*&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000 */)
+            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) //&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000 
             {
-                //this->target_ = (*it);
-                //this->targetPosition_ = it->getPosition();
                 this->state_ = SLAVE;
-
+                this->myMaster_ = controller;
+                controller->slaves.push_back(this);
+                break;
             }
         }//for
 
         //hasn't encountered any masters in range? -> become a master
-        if (state_!=SLAVE) state_=MASTER; // 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...
+        if (state_!=SLAVE) state_ = MASTER; // keep in mind: what happens when two masters encounter eache other? -> has to be evaluated in the for loop within master mode in AIcontroller...
+
     }
 
     void ArtificialController::commandSlaves() {
 
+        for(std::list<ArtificialController*>::iterator it = slaves.begin(); it != slaves.end(); it++)
+        {
+            (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
+        }
+/*
         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
         {
+
+            if (!it->getController())
+                continue;
+
             ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
-            if (controller && controller->getState() != MASTER)
+            if (!controller || controller->getState() != SLAVE)
                 continue;
-
+            //controller->setTargetPosition(this->getControllableEntity()->getPosition());
             controller->targetPosition_ = this->getControllableEntity()->getPosition();
+            controller->bHasTargetPosition_ = true;
         }
-
+*/
     }
 
     void ArtificialController::freeAllSlaves()
     {
+
         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
         {
             ArtificialController *controller = static_cast<ArtificialController*>(it->getController());
-            if (controller && controller->getState() != MASTER)
+            if (controller && controller->getState() != SLAVE)
                 continue;
 
             controller->state_ = FREE;

Modified: code/branches/ai/src/orxonox/controllers/ArtificialController.h
===================================================================
--- code/branches/ai/src/orxonox/controllers/ArtificialController.h	2010-04-12 14:08:15 UTC (rev 6694)
+++ code/branches/ai/src/orxonox/controllers/ArtificialController.h	2010-04-12 14:18:12 UTC (rev 6695)
@@ -60,10 +60,14 @@
 
             enum State {SLAVE, MASTER, FREE};
             int getState();
+            std::list<ArtificialController*> slaves;
+            void unregisterSlave();
             void searchNewMaster();
             void commandSlaves();
             void freeAllSlaves();
 
+            ArtificialController *myMaster_;
+
             void setTargetPosition(const Vector3& target);
             void searchRandomTargetPosition();
 




More information about the Orxonox-commit mailing list