Index: code/branches/AI_HS15/data/levels/AITest.oxw
===================================================================
--- code/branches/AI_HS15/data/levels/AITest.oxw	(revision 10709)
+++ code/branches/AI_HS15/data/levels/AITest.oxw	(revision 10717)
@@ -78,6 +78,6 @@
       </templates>
       <controller>
-        <WingmanController team=1>
-        </WingmanController>
+        <DivisionController team=1>
+        </DivisionController>
       </controller>
     </SpaceShip>
@@ -91,4 +91,23 @@
       </controller>
     </SpaceShip>
+    <SpaceShip position="1000, 1000, -1500 ?>" lookat="0,0,0">
+      <templates>
+        <Template link=spaceshipassff />
+      </templates>
+      <controller>
+        <LeaderController team=1>
+        </LeaderController>
+      </controller>
+    </SpaceShip>
+    <SpaceShip position="1000, 1000, -1900 ?>" lookat="0,0,0">
+      <templates>
+        <Template link=spaceshipassff />
+      </templates>
+      <controller>
+        <WingmanController team=1>
+        </WingmanController>
+      </controller>
+    </SpaceShip>
+    <!-->    
     <!-->
     <?lua
Index: code/branches/AI_HS15/src/orxonox/controllers/AIController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/AIController.cc	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/AIController.cc	(revision 10717)
@@ -44,4 +44,5 @@
     {
         RegisterObject(AIController);
+
         this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&AIController::action, this)));
     }
@@ -61,8 +62,4 @@
             if (this->formationFlight_)
             {
-                //When this is a master and was destroyed, destructor might complain that there are slaves of this, although this was removed from formation
-                //race conditions?
-                //destructor takes care of slaves anyway, so no need to worry about internal_error
-
 
                 //changed order -> searchNewMaster MUSTN'T be called in SLAVE-state (bugfix for internal-error messages at quit)
@@ -72,32 +69,35 @@
 
                 // return to Master after being forced free
-                if (this->freedomCount_ == ACTION_INTERVAL)
+                if (this->freedomCount_ == 1)
                 {
                     this->state_ = SLAVE;
-                    this->freedomCount_ = 0; // ACTION_INTERVAL is 1 sec, freedomCount is a remaining time of temp. freedom
-                }
-            } else{
-                //form a formation 
-                if (!this->forcedFree())
-                    this->searchNewMaster();
-            }
+                    this->freedomCount_ = 0;
+                }
+            }
+
             this->defaultBehaviour(maxrand);
 
         }
 
-
         if (this->state_ == SLAVE && this->formationMode_ == ATTACK) 
         {
             // search enemy
-            if ((!this->target_))
+            random = rnd(maxrand);
+            if (random < (botlevel_*100) && (!this->target_))
                 this->searchNewTarget();
 
-            
+            // next enemy
+            random = rnd(maxrand);
+            if (random < (botlevel_*30) && (this->target_))
+                this->searchNewTarget();
+
             // shoot
-            if ((this->target_ && !this->bShooting_))
+            random = rnd(maxrand);
+            if (!(this->passive_) && random < (botlevel_*100) && (this->target_ && !this->bShooting_))
                 this->bShooting_ = true;
 
             // stop shooting
-            if (this->bShooting_ && !this->target_)
+            random = rnd(maxrand);
+            if (random < (1-botlevel_)*50 && (this->bShooting_))
                 this->bShooting_ = false;
 
@@ -106,52 +106,5 @@
         if (this->state_ == MASTER)
         {
-            
-            //-------------------------------------------------------
-            //collect data for AI behaviour
-            Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0);
-            Vector3* meanOfAlliesPtr  = new Vector3(0.0,0.0,0.0);
-            Vector3 meanOfAllies = *meanOfAlliesPtr;
-            Vector3 meanOfEnemies = *meanOfEnemiesPtr;
-
-
-            for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it)
-            {
-
-                Gametype* gt=this->getGametype();
-                if (!gt)
-                {
-                    gt=it->getGametype();
-                }
-                if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt))
-                {
-                    enemies_.push_back(*it);
-                } 
-                else {
-                    allies_.push_back(*it);
-                } 
-            }
-            if (enemies_.size() != 0 && allies_.size() != 0){
-                for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it)
-                    meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition();
-
-                meanOfEnemies /= enemies_.size();
-
-                for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it)
-                    meanOfAllies += (*it)->getControllableEntity()->getWorldPosition();
-
-                meanOfAllies /= allies_.size();
-
-                //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl;
-                orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl;
-                orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl;
-                orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl;
-            }
-            //-------------------------------------------------------
-            
-            //Decide which formationMode to choose
-            this->setFormationMode(ATTACK);
-            this->setDesiredPositionOfSlaves();
-
-            //this->commandSlaves();
+            this->commandSlaves();
 
             if  (this->specificMasterAction_ != NONE)
@@ -161,5 +114,4 @@
 
                  // make 180 degree turn - a specific Master Action
-                /*
                 random = rnd(1000.0f);
                 if (random < 5)
@@ -171,5 +123,4 @@
                    this->spinInit();
 
-                */
                 /*// follow a randomly chosen human - a specific Master Action
                 random = rnd(1000.0f);
@@ -177,11 +128,9 @@
                    this->followRandomHumanInit();
 */
-               /*
                  // lose master status (only if less than 4 slaves in formation)
                 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
                 random = rnd(maxrand);
@@ -193,30 +142,15 @@
             }
         }
-        allies_.clear();
-        enemies_.clear();
+
     }
 
     void AIController::tick(float dt)
     {
-
         if (!this->isActive())
             return;
+
         float random;
         float maxrand = 100.0f / ACTION_INTERVAL;
         ControllableEntity* controllable = this->getControllableEntity();
-        if (this->state_ == SLAVE && controllable && this->mode_ == DEFAULT)
-        {
-            
-            if (this->myMaster_ && this->myMaster_->getControllableEntity() && desiredRelativePosition_){
-                Vector3 desiredAbsolutePosition = this->myMaster_->getControllableEntity()->getWorldPosition() + this->myMaster_->getControllableEntity()->getWorldOrientation()* (*desiredRelativePosition_);
-                this->moveToPosition (desiredAbsolutePosition);
-                //WorldEntity* waypoint = new WorldEntity(this->getContext());
-                //waypoint->setPosition(desiredAbsolutePosition);
-            
-                //this->addWaypoint(waypoint);
-            
-            }
-          
-        }
         //DOES: Either move to the waypoint or search for a Point of interest
         if (controllable && this->mode_ == DEFAULT)// bot is ready to move to a target
@@ -255,5 +189,7 @@
                         {
                             this->aimAtTarget();
-                            this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
+                            random = rnd(maxrand);
+                            if(this->botlevel_*70 > random && !this->isCloseAtTarget(100))
+                                this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
                         }
                     }
@@ -316,15 +252,73 @@
         }//END_OF ROCKET MODE
 
-
         SUPER(AIController, tick, dt);
     }
 //**********************************************NEW
     void AIController::defaultBehaviour(float maxrand)
-    {  
-        if (!this->target_)
-            this->searchNewTarget();
-        if (!(this->passive_) && (this->target_ && !this->bShooting_))
-            this->bShooting_ = true;
-            
+    {       float random;
+            // search enemy
+            random = rnd(maxrand);
+            if (random < (botlevel_* 100) && (!this->target_))
+                this->searchNewTarget();
+
+            // forget enemy
+            random = rnd(maxrand);
+            if (random < ((1-botlevel_)*20) && (this->target_))
+                this->forgetTarget();
+
+            // next enemy
+            random = rnd(maxrand);
+            if (random < (botlevel_*30) && (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();
+
+            if (this->state_ == MASTER) // master: shoot
+            {
+                random = rnd(maxrand);
+                if (!(this->passive_) && random < (100*botlevel_) && (this->target_ && !this->bShooting_))
+                {
+                    this->bShooting_ = true;
+                    this->forceFreeSlaves();
+                }
+            }
+            else
+            {
+                // shoot
+                random = rnd(maxrand);
+                if (!(this->passive_) && random < (botlevel_*100) && (this->target_ && !this->bShooting_))
+                    this->bShooting_ = true;
+            }
+
+            // stop shooting
+            random = rnd(maxrand);
+            if (random < ((1 - botlevel_)*50) && (this->bShooting_))
+                this->bShooting_ = false;
+
+            // boost
+            random = rnd(maxrand);
+            if (random < botlevel_*50 )
+                this->boostControl();
+
+            // update Checkpoints
+            /*random = rnd(maxrand);
+            if (this->defaultWaypoint_ && random > (maxrand-10))
+                this->manageWaypoints();
+            else //if(random > maxrand-10) //CHECK USABILITY!!*/
+            if (this->waypoints_.size() == 0 )
+                this->manageWaypoints();
     }
 
Index: code/branches/AI_HS15/src/orxonox/controllers/AIController.h
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/AIController.h	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/AIController.h	(revision 10717)
@@ -30,11 +30,9 @@
 #define _AIController_H__
 
-#include "OrxonoxPrereqs.h" 
+#include "OrxonoxPrereqs.h"
 
-#include "util/Math.h"
 #include "tools/Timer.h"
 #include "tools/interfaces/Tickable.h"
 #include "ArtificialController.h"
-
 
 namespace orxonox
@@ -56,6 +54,4 @@
 
             Timer actionTimer_; //<! Regularly calls action().
-            std::vector<WeakPtr<AIController> > enemies_, allies_;
-           
     };
 }
Index: code/branches/AI_HS15/src/orxonox/controllers/ArtificialController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/ArtificialController.cc	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/ArtificialController.cc	(revision 10717)
@@ -82,6 +82,4 @@
     void ArtificialController::changedControllableEntity()
     {
-        FormationController::changedControllableEntity(); // super
-
         if (!this->getControllableEntity())
             this->removeFromFormation();
Index: code/branches/AI_HS15/src/orxonox/controllers/ArtificialController.h
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/ArtificialController.h	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/ArtificialController.h	(revision 10717)
@@ -53,5 +53,5 @@
                 { return this->botlevel_; }
             static void setAllBotLevel(float level);
-            //WAYPOINT FUNCTIONS`
+            //WAYPOINT FUNCTIONS
             void addWaypoint(WorldEntity* waypoint);
             WorldEntity* getWaypoint(unsigned int index) const;
@@ -74,5 +74,4 @@
 
             float botlevel_; //<! Makes the level of a bot configurable.
-            //botLevel_ is never used in XML (so far), is it redundant?
             enum Mode {DEFAULT, ROCKET, DEFENCE, MOVING};//TODO; implement DEFENCE, MOVING modes
             Mode mode_; //TODO: replace single value with stack-like implementation: std::vector<Mode> mode_;
Index: code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc	(revision 10717)
@@ -38,4 +38,5 @@
     {
         RegisterObject(DivisionController);
+        bIsDivisionLeader_ = true;
     }
 
@@ -47,7 +48,15 @@
         }
     }
-    void DivisionController::setLeader(LeaderController* leader)
+    bool DivisionController::setFollower(LeaderController* myFollower)
     {
-        this->leader_ = leader;
+         if (!this->myFollower_)
+        {
+            this->myFollower_ = myFollower;
+            return true;
+        }
+        else
+        {
+            return false;
+        }
     }
     /*void DivisionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
Index: code/branches/AI_HS15/src/orxonox/controllers/DivisionController.h
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/DivisionController.h	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/DivisionController.h	(revision 10717)
@@ -33,4 +33,7 @@
 
 
+#include "util/Math.h"
+#include "tools/Timer.h"
+#include "tools/interfaces/Tickable.h"
 
 namespace orxonox
@@ -49,5 +52,5 @@
      		inline FormationMode getFormationMode() const
            		{ return this->formationMode_; }
-			virtual void setLeader(LeaderController* leader);
+			virtual bool setFollower(LeaderController* myFollower);
 
 			
@@ -63,45 +66,6 @@
 			FormationMode formationMode_;
 			
-			/*void setTargetPosition(const Vector3& target);
-			void searchRandomTargetPosition();
-
-			void setTargetOrientation(const Quaternion& orient);
-			void setTargetOrientation(Pawn* target);
-
-			virtual void positionReached() {}
-
-
-
-			void setTarget(Pawn* target);
-
-			void searchNewTarget();
-		    void forgetTarget();
-
-			void targetDied();
-			bool bShooting_;
-			void aimAtTarget();
-
-            bool isCloseAtTarget(float distance) const;
-            bool isLookingAtTarget(float angle) const;
-
-	        //Has nothing to do with desiredRelativePosition_,
-	        //is set by fleet controller.
-	        Vector3* desiredAbsolutePosition_;
-
-    		enum Maneuver {NONE, SPIN, TURN180};
-    		Maneuver maneuver_;
-
-    		void moveToPosition(const Vector3& target);
-		    void moveToTargetPosition();
-		    void absoluteMoveToPosition(const Vector3& target);
-		    void copyOrientation(const Quaternion& orient);
-		    void copyTargetOrientation();
-
-			void turn180Init();
-		    void spinInit();
-		    void spin();
-      		void turn180();*/
-
-      		LeaderController* leader_;
+			
+      		LeaderController* myFollower_;
 	        
 
Index: code/branches/AI_HS15/src/orxonox/controllers/FleetController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/FleetController.cc	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/FleetController.cc	(revision 10717)
@@ -63,5 +63,5 @@
     {
 
-         if (nTicks_ == 30)
+         /*if (nTicks_ == 30)
          {
             std::vector<WeakPtr<WingmanController> > wingmen;
@@ -143,5 +143,5 @@
             nTicks_ += 1;
 
-         }
+         }*/
         SUPER(FleetController, tick, dt);
 
Index: code/branches/AI_HS15/src/orxonox/controllers/FormationController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/FormationController.cc	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/FormationController.cc	(revision 10717)
@@ -105,5 +105,4 @@
                         orxout(internal_error) << this << " is still master in " << (*it) << endl;
                         it->myMaster_ = 0;
-                        it->state_ = FREE;
                     }
 
@@ -278,5 +277,5 @@
         }
 
-        Vector2 coord = get2DViewCoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
+        Vector2 coord = get2DViewcoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
         float distance = (target - this->getControllableEntity()->getPosition()).length();
         float rotateX = clamp(coord.x * 10, -1.0f, 1.0f);
@@ -473,5 +472,5 @@
         Vector3 dest = this->getControllableEntity()->getPosition();
 
-        // only 1 slave: follow
+        // 1 slave: follow
         if (this->slaves_.size() == 1)
         {
@@ -703,33 +702,4 @@
         }
     }
-     /**
-      @brief If master, set the desired relative position of slaves (Vector3) in respect to a master for all the slaves.
-    */
-    void FormationController::setDesiredPositionOfSlaves()
-    {
-        if (this->state_ != MASTER)
-            return;
-        switch (this->formationMode_){
-            case ATTACK:
-            {
-                float i = 0;
-                for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
-                {
-                    (*it)->desiredRelativePosition_ = new Vector3 ((i-slaves_.size()/2)*200, 0, 0);
-                    i++;
-                }
-                break;
-            }
-            case NORMAL: 
-            {
-                break;
-            }
-            case DEFEND: 
-            {
-                break;
-            }
-        }
-       
-    }
 
     /**
@@ -1099,5 +1069,5 @@
             return;
 
-        Vector2 coord = get2DViewCoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
+        Vector2 coord = get2DViewcoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target);
         float distance = (target - this->getControllableEntity()->getPosition()).length();
 
@@ -1117,11 +1087,3 @@
     }
 
-    void FormationController::changedControllableEntity()
-    {
-        Controller::changedControllableEntity(); // super
-
-        // when changing the controllable entity, ensure that the controller does not use the new entity as target
-        if (this->target_ && this->getControllableEntity() == static_cast<ControllableEntity*>(this->target_))
-            this->forgetTarget();
-    }
 }
Index: code/branches/AI_HS15/src/orxonox/controllers/FormationController.h
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/FormationController.h	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/FormationController.h	(revision 10717)
@@ -70,8 +70,4 @@
            { return this->maxFormationSize_; }
 
-      /**
-      @brief If master, set the desired position of slaves (Vector2) for all the slaves.
-      */
-      void setDesiredPositionOfSlaves();
 
       inline void setPassive(bool passive)
@@ -96,5 +92,5 @@
       inline FormationMode getFormationMode() const
            { return this->formationMode_; }
-      
+
       virtual void hit(Pawn* originator, btManifoldPoint& contactpoint, float damage);
 
@@ -103,8 +99,5 @@
       FormationController* getSlave( void ) { return this->slaves_.back(); }
 
-      virtual void changedControllableEntity();
-
   protected:
-      Vector3* desiredRelativePosition_;
       bool formationFlight_;
       bool passive_;
@@ -185,2 +178,3 @@
 #endif /* _FormationController_h__ */
 
+
Index: code/branches/AI_HS15/src/orxonox/controllers/LeaderController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/LeaderController.cc	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/LeaderController.cc	(revision 10717)
@@ -30,5 +30,4 @@
 
 
-
 namespace orxonox
 {
@@ -36,8 +35,12 @@
     RegisterClass(LeaderController);
 
-    LeaderController::LeaderController(Context* context) : WingmanController(context)
+    static const int RADIUS_TO_SEARCH_FOR_LEADER = 3000;
+
+    LeaderController::LeaderController(Context* context) : CommonController(context)
     {
 
         RegisterObject(LeaderController);
+        bIsDivisionLeader_ = false;
+
         //this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&LeaderController::action, this)));
     }
@@ -48,7 +51,63 @@
     }
 
+    LeaderController* LeaderController::findNewDivisionLeader()
+    {
+
+        if (!this->getControllableEntity())
+            return NULL;
+
+        
+        //go through all pawns
+        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
+        {
+
+            //same team?
+            if (!(this->getControllableEntity()->getTeam() != static_cast<ControllableEntity*>(*it)->getTeam()))
+                continue;
+
+            //Does it have a Controller?
+            Controller* controller = 0;
+
+            if (it->getController())
+                controller = it->getController();
+            else if (it->getXMLController())
+                controller = it->getXMLController();
+
+            if (!controller)
+                continue;
+
+            //is equal to this?
+            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
+                continue;
+
+
+            LeaderController* newLeader = orxonox_cast<LeaderController*>(controller);
+
+            //nullptr or not DivisionController?
+            if (!newLeader || !newLeader->bIsDivisionLeader_)
+                continue;
+
+            float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
+
+            // is pawn in range?
+            if (distance < RADIUS_TO_SEARCH_FOR_LEADER)
+            {
+
+                if (newLeader->setFollower(this))
+                    return newLeader;
+            }
+        }
+                return NULL;
+
+    }
     void LeaderController::action()
     {
         //this->target_ = this->sectionTarget_;        
+        if (!myDivisionLeader_)
+        {
+            LeaderController* newDivisionLeader = findNewDivisionLeader();
+            myDivisionLeader_ = newDivisionLeader;
+            orxout(internal_error) << "new DivisionLeader set" << endl;
+        }
     }
     /*
@@ -94,11 +153,23 @@
         }*/
 
-        orxout(internal_error) << "my Wingman is " << this->wingman_ << endl;
+        orxout(internal_error) << "my Wingman is " << this->myWingman_ << endl;
         
         SUPER(LeaderController, tick, dt);
     }
-    void LeaderController::setWingman(WingmanController* wingman)
+    bool LeaderController::setWingman(WingmanController* wingman)
     {
-        this->wingman_ = wingman;
+        if (!this->myWingman_)
+        {
+            this->myWingman_ = wingman;
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+    bool LeaderController::isLeader()
+    {
+        return true;
     }
 //**********************************************NEW
Index: code/branches/AI_HS15/src/orxonox/controllers/LeaderController.h
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/LeaderController.h	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/LeaderController.h	(revision 10717)
@@ -30,12 +30,15 @@
 #define _LeaderController_H__
 
+#include "controllers/CommonController.h"
 #include "controllers/WingmanController.h"
 
 
-
+#include "util/Math.h"
+#include "tools/Timer.h"
+#include "tools/interfaces/Tickable.h"
 
 namespace orxonox
 {
-    class _OrxonoxExport LeaderController : public WingmanController
+    class _OrxonoxExport LeaderController : public CommonController, public Tickable
     {
         public:
@@ -43,10 +46,14 @@
             LeaderController(Context* context);
             virtual ~LeaderController();
-            virtual void setWingman(WingmanController* wingman);
+            virtual bool isLeader();
+
+            virtual bool setWingman(WingmanController* wingman);
             virtual void tick(float dt); //<! Carrying out the targets set in action().
 
         protected:
            
-            
+            LeaderController* findNewDivisionLeader();
+
+            bool bIsDivisionLeader_;
 
             virtual void action(); //<! action() is called in regular intervals managing the bot's behaviour ~ setting targets.
@@ -57,6 +64,6 @@
             WeakPtr<Pawn> target_;
             
-            WingmanController* wingman_;
-
+            WingmanController* myWingman_;
+            LeaderController* myDivisionLeader_;
             //Timer actionTimer_; //<! Regularly calls action().
                
Index: code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc	(revision 10709)
+++ 	(revision )
@@ -1,59 +1,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   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.
- *
- *   Author:
- *      Fabian 'x3n' Landau
- *   Co-authors:
- *      Dominik Solenicki
- *
- */
-
-#include "SectionController.h"
-
-namespace orxonox
-{
-
-    RegisterClass(SectionController);
-
-    SectionController::SectionController(Context* context) : DivisionController(context)
-    {
-        RegisterObject(SectionController);
-    }
-
-    SectionController::~SectionController()
-    {
-        if (this->isInitialized())
-        {
-           
-        }
-    }
-
-    /*void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
-    {
-        SUPER(SectionController, XMLPort, xmlelement, mode);
-
-        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
-    }*/
-
-    
-   
-
-}
Index: code/branches/AI_HS15/src/orxonox/controllers/SectionController.h
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/SectionController.h	(revision 10709)
+++ 	(revision )
@@ -1,65 +1,0 @@
-/*
- *   ORXONOX - the hottest 3D action shooter ever to exist
- *                    > www.orxonox.net <
- *
- *
- *   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.
- *
- *   Author:
- *      Fabian 'x3n' Landau
- *   Co-authors:
- *      Dominik Solenicki
- *
- */
-
-#ifndef _SectionController_H__
-#define _SectionController_H__
-
-#include "controllers/DivisionController.h"
-
-
-namespace orxonox
-{
-    class _OrxonoxExport SectionController : public DivisionController
-    {
-        public:
-            SectionController(Context* context);
-            virtual ~SectionController();
-
-            //virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
-
-
-
-        protected:
-      
-            //A division is the biggest block of spaceships.
-            //In division one section is leading, the other one always stays on the same position
-            //relative to the Leader of the leading section.
-            //In section a Wingman always stays on same position relative to the Leader.
-            //That position is desiredRelativePosition_
-            Vector3* desiredRelativePosition_;
-            
-
-
-            LeaderController* leader_;
-            WingmanController* wingman_;
-        
-        private:
-    };
-}
-
-#endif /* _SectionController_H__ */
Index: code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc	(revision 10717)
@@ -29,4 +29,11 @@
 #include "WingmanController.h"
 
+#include "core/CoreIncludes.h"
+
+#include "core/XMLPort.h"
+#include "core/command/ConsoleCommandIncludes.h"
+
+#include "worldentities/ControllableEntity.h"
+#include "worldentities/pawns/Pawn.h"
 
 namespace orxonox
@@ -34,6 +41,7 @@
 
     RegisterClass(WingmanController);
-
-    WingmanController::WingmanController(Context* context) : Controller(context)
+    static const int RADIUS_TO_SEARCH_FOR_LEADER = 3000;
+
+    WingmanController::WingmanController(Context* context) : CommonController(context)
     {
         RegisterObject(WingmanController);
@@ -52,11 +60,111 @@
         XMLPortObject(WingmanController, WorldEntity, "waypoints", addWaypoint, getWaypoint,  xmlelement, mode);
     }*/
+    CommonController* WingmanController::findNewLeader()
+    {
+
+        if (!this->getControllableEntity())
+            return NULL;
+
+        
+        //go through all pawns
+        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
+        {
+
+            //same team?
+            if (!(this->getControllableEntity()->getTeam() != static_cast<ControllableEntity*>(*it)->getTeam()))
+                continue;
+
+            //Does it have a Controller?
+            Controller* controller = 0;
+
+            if (it->getController())
+                controller = it->getController();
+            else if (it->getXMLController())
+                controller = it->getXMLController();
+
+            if (!controller)
+                continue;
+
+            //is equal to this?
+            if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
+                continue;
+
+
+            CommonController* newLeader = orxonox_cast<CommonController*>(controller);
+
+            //nullptr?
+            if (!newLeader || !newLeader->isLeader())
+                continue;
+
+            float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
+
+            // is pawn in range?
+            if (distance < RADIUS_TO_SEARCH_FOR_LEADER)
+            {
+
+                if (newLeader->setWingman(this))
+                    return newLeader;
+            }
+        }
+        return NULL;
+    }
+    bool WingmanController::isLeader()
+    {
+        return false;
+    }
     void WingmanController::action()
     {
         //this->target_ = this->sectionTarget_;
+        if (!myLeader_)
+        {
+            CommonController* newLeader = findNewLeader();
+            myLeader_ = newLeader;
+            orxout(internal_error) << "new Leader set" << endl;
+        }
     }
 
     void WingmanController::tick(float dt)
-    {/*
+    {   
+        //-------------------------------------------------------
+            /*//collect data for AI behaviour
+            Vector3* meanOfEnemiesPtr = new Vector3(0.0,0.0,0.0);
+            Vector3* meanOfAlliesPtr  = new Vector3(0.0,0.0,0.0);
+            Vector3 meanOfAllies = *meanOfAlliesPtr;
+            Vector3 meanOfEnemies = *meanOfEnemiesPtr;
+
+
+            for (ObjectList<AIController>::iterator it = ObjectList<AIController>::begin(); it; ++it)
+            {
+
+                Gametype* gt=this->getGametype();
+                if (!gt)
+                {
+                    gt=it->getGametype();
+                }
+                if (!FormationController::sameTeam(this->getControllableEntity(), it->getControllableEntity(),gt))
+                {
+                    enemies_.push_back(*it);
+                } 
+                else {
+                    allies_.push_back(*it);
+                } 
+            }
+            if (enemies_.size() != 0 && allies_.size() != 0){
+                for (std::vector<WeakPtr<AIController> >::iterator it = enemies_.begin() ; it != enemies_.end(); ++it)
+                    meanOfEnemies += (*it)->getControllableEntity()->getWorldPosition();
+
+                meanOfEnemies /= enemies_.size();
+
+                for (std::vector<WeakPtr<AIController> >::iterator it = allies_.begin() ; it != allies_.end(); ++it)
+                    meanOfAllies += (*it)->getControllableEntity()->getWorldPosition();
+
+                meanOfAllies /= allies_.size();
+
+                //orxout(internal_error) << "There are " << enemies_Counter << " enemies_, mean position is " << meanOfEnemies << endl;
+                orxout(internal_error) << "Distance is " << (meanOfEnemies-meanOfAllies).length() << endl;
+                orxout(internal_error) << "mean of allies_ is " << meanOfAllies << ", with a size " << allies_.size() << endl;
+                orxout(internal_error) << "mean of enemies_ is " << meanOfEnemies << ", with a size " << enemies_.size() << endl;
+            }*/
+    /*
         if (!this->isActive())
             return;
@@ -83,4 +191,31 @@
         }*/
          //orxout(internal_error) << "I am " << this << endl;
+
+       /* void FormationController::setDesiredPositionOfSlaves()
+    {
+        if (this->state_ != MASTER)
+            return;
+        switch (this->formationMode_){
+            case ATTACK:
+            {
+                float i = 0;
+                for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
+                {
+                    (*it)->desiredRelativePosition_ = new Vector3 ((i-slaves_.size()/2)*200, 0, 0);
+                    i++;
+                }
+                break;
+            }
+            case NORMAL: 
+            {
+                break;
+            }
+            case DEFEND: 
+            {
+                break;
+            }
+        }
+       
+    }*/
         
         SUPER(WingmanController, tick, dt);
Index: code/branches/AI_HS15/src/orxonox/controllers/WingmanController.h
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/WingmanController.h	(revision 10709)
+++ code/branches/AI_HS15/src/orxonox/controllers/WingmanController.h	(revision 10717)
@@ -31,44 +31,26 @@
 
 
-#include "controllers/Controller.h"
+#include "controllers/CommonController.h"
 
 
-#include "OrxonoxPrereqs.h"
-#include "core/class/Super.h"
-#include "core/CoreIncludes.h"
-#include "core/XMLPort.h"
-#include "core/command/ConsoleCommandIncludes.h"
-#include "core/command/Executor.h"
-
-#include <vector>
 #include "util/Math.h"
-#include <climits>
-
 #include "tools/Timer.h"
 #include "tools/interfaces/Tickable.h"
-
-#include "worldentities/ControllableEntity.h"
-#include "worldentities/pawns/SpaceShip.h"
-#include "worldentities/pawns/Pawn.h"
-#include "worldentities/pawns/TeamBaseMatchBase.h"
-
-#include "gametypes/TeamDeathmatch.h"
-#include "gametypes/Dynamicmatch.h"
-#include "gametypes/Mission.h"
-#include "gametypes/Gametype.h"
-
 
 
 namespace orxonox
 {
-    class _OrxonoxExport WingmanController : public Controller, public Tickable
+    class _OrxonoxExport WingmanController : public CommonController, public Tickable
     {
         public:
             WingmanController(Context* context);
             virtual ~WingmanController();
-            //virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
+            virtual bool isLeader();
+          //virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
             virtual void tick(float dt); //<! Carrying out the targets set in action().
 
         protected:
+            CommonController* findNewLeader();
+
             virtual void action(); //<! action() is called in regular intervals managing the bot's behaviour ~ setting targets.
             //void defaultBehaviour(float maxrand); //<! Helper function for code reusage. Some concrete commands for a bot.
@@ -80,5 +62,5 @@
             
             WeakPtr<Pawn> target_;
-            
+            CommonController* myLeader_;
             //LeaderController* leader_;
 
