Index: code/branches/ai/data/levels/teamdeathmatch.oxw
===================================================================
--- code/branches/ai/data/levels/teamdeathmatch.oxw	(revision 6854)
+++ code/branches/ai/data/levels/teamdeathmatch.oxw	(revision 6888)
@@ -35,5 +35,5 @@
 
 <?lua
-for i = 1, 70, 1
+for i = 1, 8, 1
 do ?>
   <MovableEntity position="<?lua print(math.random() * 6000 - 3000)?>, <?lua print(math.random() * 6000 - 3000) ?>, <?lua print(math.random() * 1000 + 3000) ?>" rotationaxis="<?lua print(math.random()) ?>, <?lua print(math.random()) ?>, <?lua print(math.random()) ?>" rotationrate="<?lua print(math.random() * 30 + 5) ?>">
Index: code/branches/ai/src/orxonox/controllers/AIController.cc
===================================================================
--- code/branches/ai/src/orxonox/controllers/AIController.cc	(revision 6854)
+++ code/branches/ai/src/orxonox/controllers/AIController.cc	(revision 6888)
@@ -127,7 +127,7 @@
 
             // lose master status (only if less than 4 slaves in formation)
-            random = rnd(maxrand);
-            if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 5 ) 
-                this->loseMasterState();
+//             random = rnd(maxrand);
+//             if(random < 15/(this->slaves_.size()+1) && this->slaves_.size() < 4 ) 
+//                 this->loseMasterState();
 
             // look out for outher masters if formation is small
@@ -167,5 +167,5 @@
             {
                 this->bShooting_ = true;
-                this->forceFreeSlaves();
+//                 this->forceFreeSlaves();
             }
             // stop shooting
Index: code/branches/ai/src/orxonox/controllers/ArtificialController.cc
===================================================================
--- code/branches/ai/src/orxonox/controllers/ArtificialController.cc	(revision 6854)
+++ code/branches/ai/src/orxonox/controllers/ArtificialController.cc	(revision 6888)
@@ -36,7 +36,16 @@
 #include "gametypes/TeamDeathmatch.h"
 #include "controllers/WaypointPatrolController.h"
+#include "util/Math.h"
 
 namespace orxonox
 {
+
+    static const unsigned int MAX_FORMATION_SIZE = 6;
+    static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy
+    static const float SPEED_MASTER = 0.6f;
+    static const float ROTATEFACTOR_MASTER = 0.2f;
+    static const float SPEED_FREE = 0.8f;
+    static const float ROTATEFACTOR_FREE = 0.8f;
+
     ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator)
     {
@@ -69,16 +78,11 @@
     void ArtificialController::changedControllableEntity()
     {
-COUT(0) << "~changedControllableEntity 0" << std::endl;
         if(!getControllableEntity()) 
         {
-COUT(0) << "~changedControllableEntity 1" << std::endl;
         if (this->state_ == SLAVE) unregisterSlave();
-COUT(0) << "~changedControllableEntity 2" << std::endl;
          if (this->state_ == MASTER) setNewMasterWithinFormation();
-COUT(0) << "~changedControllableEntity 3" << std::endl;
         this->slaves_.clear();
-COUT(0) << "~changedControllableEntity 4" << std::endl;
         this->state_ = FREE;
-COUT(0) << "~changedControllableEntity 5" << std::endl;
+
         }
     }
@@ -92,27 +96,64 @@
         float distance = (target - this->getControllableEntity()->getPosition()).length();
 
-        if (this->target_ || distance > 10)
-        {
-            // Multiply with rotateFactor to make them a bit slower
-            float rotateFactor;
-            if(this->state_ == SLAVE) rotateFactor = 1.0f;
-            if(this->state_ == MASTER) rotateFactor = 0.4f;
-            else rotateFactor = 0.8f;
-
-            this->getControllableEntity()->rotateYaw(-1.0f * rotateFactor * sgn(coord.x) * coord.x*coord.x);
-            this->getControllableEntity()->rotatePitch(rotateFactor * sgn(coord.y) * coord.y*coord.y);
-
-
-
-        }
-
-        if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
-        {
-            if(this->state_ == SLAVE) this->getControllableEntity()->moveFrontBack(-0.8f);
-            else this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance
-        } else {
-            if(this->state_ == SLAVE) this->getControllableEntity()->moveFrontBack(2.5f);
-            if(this->state_ == MASTER) this->getControllableEntity()->moveFrontBack(0.4f);
-            else this->getControllableEntity()->moveFrontBack(0.8f);
+
+        if(this->state_ == FREE)
+        {
+            if (this->target_ || distance > 10)
+            {
+                // Multiply with 0.8 to make them a bit slower
+                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x*coord.x);
+                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y*coord.y);
+            }
+
+            if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
+            {
+              this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance
+            } else this->getControllableEntity()->moveFrontBack(SPEED_FREE);
+        }
+
+
+
+        if(this->state_ == MASTER)
+        {
+            if (this->target_ || distance > 10)
+            {
+                this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x);
+                this->getControllableEntity()->rotatePitch(ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y);
+
+            }
+
+            if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
+            {
+                this->getControllableEntity()->moveFrontBack(-0.05f);
+            } else this->getControllableEntity()->moveFrontBack(SPEED_MASTER);
+        }
+
+
+
+        if(this->state_ == SLAVE)
+        {
+//             if (this->target_ || distance > 10)
+//             {
+                float rotateFactor;
+                if(this->state_ == SLAVE) rotateFactor = 1.0f;
+
+
+                this->getControllableEntity()->rotateYaw(-1.0f * rotateFactor * sgn(coord.x) * coord.x*coord.x);
+                this->getControllableEntity()->rotatePitch(rotateFactor * sgn(coord.y) * coord.y*coord.y);
+
+
+
+//             }
+
+            if (this->target_ && distance < 500 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength())
+            {
+                if (this->target_ && distance < 60)
+                {
+                    this->getControllableEntity()->setVelocity(0.8f*this->target_->getVelocity());
+                } else this->getControllableEntity()->moveFrontBack(-1.0f*exp(distance/100.0));
+
+            } else {
+                this->getControllableEntity()->moveFrontBack(1.0f + distance/500.0f);
+            }
         }
     }
@@ -131,6 +172,8 @@
         if(myMaster_)
         {
-            myMaster_->slaves_.remove(this);
-COUT(0) << "~unregister slave" << std::endl;
+            std::vector<ArtificialController*>::iterator it = std::find(myMaster_->slaves_.begin(), myMaster_->slaves_.end(), this);
+            if( it != myMaster_->slaves_.end() )
+                myMaster_->slaves_.erase(it);
+//COUT(0) << "~unregister slave" << std::endl;
         }
     }
@@ -162,12 +205,12 @@
                 continue;
 
+            float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
+
             //is pawn oneself? && is pawn in range?
-            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) //&& it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) < 1000 
-            {
-                if(newMaster->slaves_.size() > 6) continue;
-
-                //this->freeSlaves();
-
-                for(std::list<ArtificialController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++)
+            if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity() && distance < 5000) 
+            {
+                if(newMaster->slaves_.size() > MAX_FORMATION_SIZE) continue;
+
+                for(std::vector<ArtificialController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++)
                 {
                     (*itSlave)->myMaster_ = newMaster;
@@ -185,13 +228,33 @@
 
         //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 within master mode in AIcontroller...
-COUT(0) << "~searcheNewMaster" << std::endl;
+        if (state_!=SLAVE) state_ = MASTER;//master encounters master? ->done
     }
 
     void ArtificialController::commandSlaves() {
 
-        for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
-        {
-            (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
+        Quaternion orient = this->getControllableEntity()->getOrientation();
+        Vector3 dest = this->getControllableEntity()->getPosition();
+
+        // 1 slave: follow
+        if (this->slaves_.size() == 1)
+        {
+            dest += 4*orient*WorldEntity::BACK;
+            this->slaves_.front()->setTargetPosition(dest);
+        }
+
+        // 2 slaves: triangle
+         if (this->slaves_.size() == 2) 
+        {
+            dest += 10*orient*WorldEntity::BACK;
+            this->slaves_[0]->setTargetPosition(dest + 10*orient*WorldEntity::LEFT);
+            this->slaves_[1]->setTargetPosition(dest + 10*orient*WorldEntity::RIGHT);
+        }
+
+        if (this->slaves_.size() > MAX_FORMATION_SIZE)
+        {
+            for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
+            {
+                (*it)->setTargetPosition(this->getControllableEntity()->getPosition());
+            }
         }
     }
@@ -200,29 +263,23 @@
     void ArtificialController::setNewMasterWithinFormation()
     {
-COUT(0) << "~setNewMasterWithinFormation 1" << std::endl;
+
         if (this->slaves_.empty())
             return;
-COUT(0) << "~setNewMasterWithinFormation 1b" << std::endl;
 
         ArtificialController *newMaster = this->slaves_.back();
-COUT(0) << "~setNewMasterWithinFormation 2" << std::endl;
         this->slaves_.pop_back();
-COUT(0) << "~setNewMasterWithinFormation 3" << std::endl;
+
         if(!newMaster) return;
-COUT(0) << "~setNewMasterWithinFormation 4" << std::endl;
         newMaster->state_ = MASTER;
         newMaster->slaves_ = this->slaves_;
+
         this->slaves_.clear();
-
         this->state_ = SLAVE;
         this->myMaster_ = newMaster;
 
-COUT(0) << "~setNewMasterWithinFormation 5" << std::endl;
-        for(std::list<ArtificialController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
-        {
-COUT(0) << "~setNewMasterWithinFormation 6" << std::endl;
+        for(std::vector<ArtificialController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
+        {
             (*it)->myMaster_ = newMaster;
         }
-COUT(0) << "~setNewMasterWithinFormation 7" << std::endl;
 
     }
@@ -230,5 +287,5 @@
     void ArtificialController::freeSlaves()
     {
-        for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
+        for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
         {
             (*it)->state_ = FREE;
@@ -239,5 +296,5 @@
     void ArtificialController::forceFreeSlaves()
     {
-        for(std::list<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
+        for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
         {
             (*it)->state_ = FREE;
@@ -245,5 +302,5 @@
             (*it)->targetPosition_ = this->targetPosition_;
             (*it)->bShooting_ = true;
-            (*it)->getControllableEntity()->fire(0);
+            (*it)->getControllableEntity()->fire(0);// fire once for fun
         }
     }
@@ -257,5 +314,5 @@
     void ArtificialController::forceFreedom()
     {
-        this->freedomCount_ = 5;
+        this->freedomCount_ = FREEDOM_COUNT;
     }
 
Index: code/branches/ai/src/orxonox/controllers/ArtificialController.h
===================================================================
--- code/branches/ai/src/orxonox/controllers/ArtificialController.h	(revision 6854)
+++ code/branches/ai/src/orxonox/controllers/ArtificialController.h	(revision 6888)
@@ -32,4 +32,6 @@
 #include "OrxonoxPrereqs.h"
 
+#include <vector>
+
 #include "util/Math.h"
 #include "Controller.h"
@@ -43,5 +45,5 @@
             virtual ~ArtificialController();
 
-            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);//new
+            virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
 
             void abandonTarget(Pawn* target);
@@ -60,10 +62,9 @@
             enum State {SLAVE, MASTER, FREE};
             State state_;
-            std::list<ArtificialController*> slaves_;
+            std::vector<ArtificialController*> slaves_;
 
             void targetDied();
 
             void moveToPosition(const Vector3& target);
-            //void speedToTargetPosition(const Vector3& target);
             void moveToTargetPosition();
 
