Index: /code/trunk/data/levels/maxim.oxw
===================================================================
--- /code/trunk/data/levels/maxim.oxw	(revision 9625)
+++ /code/trunk/data/levels/maxim.oxw	(revision 9625)
@@ -0,0 +1,126 @@
+<LevelInfo
+ name = "Maxim's Test"
+ description = "Intelligent Formation"
+ tags = "test"
+/>
+
+<?lua
+  include("stats.oxo")
+  include("HUDTemplates3.oxo")
+  include("templates/lodInformation.oxt")
+?>
+
+<?lua
+  include("templates/spaceshipAssff.oxt")
+  include("templates/spaceshipPirate.oxt")
+  include("templates/spaceshipSwallow.oxt")
+  include("templates/spaceshipTransporter.oxt")
+?>
+
+<Level
+ gametype = "Mission"
+>                  
+  <templates>
+    <Template link=lodtemplate_default />
+  </templates>
+  <?lua include("includes/notifications.oxi") ?>
+
+  <NotificationQueueCEGUI
+    name="narrative"
+    targets="simpleNotification"
+    size=3
+    displayTime=3.9
+    position="0.2, 0, 0.1, 0"
+    fontSize="23"
+    fontColor="0.3, 1, 0.2, 0.8"
+    alignment="HorzCentred"
+    displaySize="0.6, 0, 0, 0"
+    />
+
+  <Scene
+    ambientlight = "0.8, 0.8, 0.8"
+    skybox       = "Orxonox/skyBoxMoreNebula"
+  >
+
+  <Light type=directional position="0,0,0" direction="0.253, 0.593, -0.765" diffuse="1.0, 0.9, 0.9, 1.0" specular="1.0, 0.9, 0.9, 1.0"/>
+
+  <TeamSpawnPoint team=0 position="800,700,600" direction="-1,0,0" spawnclass=SpaceShip pawndesign=spaceshipswallow/>
+
+  <!-- TRIGGERS -->
+    <DistanceTrigger name="FormationInit" position="800,700,600"  target="Pawn" distance=60 stayActive="true" delay=0.1 />
+    <DistanceTrigger name="LetsGo"        position="200,-200,1000" target="Pawn" distance=180 stayActive="true" delay=0.1 />
+    <BlinkingBillboard colour="1,1,0.05" position="200,-200,1000" frequency=0.6 amplitude=3 material="Flares/lensflare" />
+	<DistanceTrigger name="Fight"        position="-600,3000,-2000" target="Pawn" distance=180 stayActive="true" delay=0.1 />
+    <BlinkingBillboard colour="0,0,1" position="-600,3000,-2000" frequency=0.6 amplitude=3 material="Flares/lensflare" />
+
+
+
+    <Script code="formationcontroller formationflight true" onLoad="false">
+        <events>
+            <trigger>
+                <EventListener event=FormationInit />
+            </trigger>
+        </events>
+    </Script> <!-- !!! activates formation behaviour !!! -->
+
+    <Script code="artificialcontroller setbotlevel 5" onLoad="false">
+        <events>
+            <trigger>
+                <EventListener event=FormationInit />
+            </trigger>
+        </events>
+    </Script>
+
+    <SimpleNotification message="Let's go!">
+        <events>
+            <trigger>
+                <EventListener event="LetsGo" />
+            </trigger>
+        </events>
+    </SimpleNotification>
+
+
+
+
+<!-- ADDING FORMATION: 2 Swallows -->
+<?lua for i=0,1,1 do
+?>
+    <SpaceShip position="<?lua print(400+i*200) ?>,-200,1000" lookat="-1300,-600,900" visible="true">
+        <templates>
+            <Template link=spaceshipswallow /> <!--spaceshipTransporter spaceshippirate-->
+        </templates>
+        <controller>
+            <AIController team=0>
+                <events>
+                    <activity>
+                        <EventListener event="LetsGo" />
+                     </activity>
+                </events>
+            </AIController>
+        </controller>
+    </SpaceShip>
+<?lua end ?>
+
+<?lua for i=0,2,1 do
+?>
+
+
+<!-- ENEMY SWALLOWS -->
+    <SpaceShip position="<?lua print(-400+i*200) ?>,3000,-2000" lookat="-1300,-600,900" visible="true">
+        <templates>
+            <Template link=spaceshipswallow />
+        </templates>
+        <controller>
+            <AIController team=1>
+                <events>
+                    <activity>
+                        <EventListener event="Fight" />
+                     </activity>
+                </events>
+            </AIController>
+        </controller>
+    </SpaceShip>
+<?lua end ?>
+
+  </Scene>
+</Level>
Index: /code/trunk/src/orxonox/controllers/Controller.h
===================================================================
--- /code/trunk/src/orxonox/controllers/Controller.h	(revision 9624)
+++ /code/trunk/src/orxonox/controllers/Controller.h	(revision 9625)
@@ -65,5 +65,4 @@
             virtual void changedControllableEntity() {}
 
-        protected:
             // don't use this directly, use getPlayer()->startControl(entity) (unless you know exactly what you do)
             inline void setControllableEntity(ControllableEntity* entity)
Index: /code/trunk/src/orxonox/controllers/FormationController.cc
===================================================================
--- /code/trunk/src/orxonox/controllers/FormationController.cc	(revision 9624)
+++ /code/trunk/src/orxonox/controllers/FormationController.cc	(revision 9625)
@@ -535,6 +535,36 @@
 
 
+    // Sets newMaster as the new master within the formation. Called by the master.
+    void FormationController::setNewMasterWithinFormation(FormationController* newMaster)
+        {
+            if(this->state_ != MASTER || newMaster->myMaster_ != this) return;
+
+            if (!this->slaves_.empty())
+            {
+				std::vector<FormationController*>::iterator it2 = std::find(this->slaves_.begin(), this->slaves_.end(), newMaster);
+				if (it2 != this->slaves_.end())
+				{
+					 this->slaves_.erase(it2);
+				}
+
+                newMaster->state_ = MASTER;
+                newMaster->slaves_ = this->slaves_;
+                newMaster->myMaster_ = 0;
+
+                for(std::vector<FormationController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
+                {
+                    (*it)->myMaster_ = newMaster;
+                }
+            }
+
+            this->slaves_.clear();
+            this->specificMasterAction_ = NONE;
+            this->state_ = FREE;
+        }
+
+
+
   /**
-        @brief Frees all slaves form a master. Initiated by a master.
+        @brief Frees all slaves from a master. Initiated by a master.
     */
     void FormationController::freeSlaves()
Index: /code/trunk/src/orxonox/controllers/FormationController.h
===================================================================
--- /code/trunk/src/orxonox/controllers/FormationController.h	(revision 9624)
+++ /code/trunk/src/orxonox/controllers/FormationController.h	(revision 9625)
@@ -59,4 +59,5 @@
       static void passivebehaviour(const bool passive);
       static void formationsize(const int size);
+      void setNewMasterWithinFormation(FormationController* newMaster);
 
       inline void setFormationFlight(bool formation)
@@ -98,4 +99,8 @@
       virtual void hit(Pawn* originator, btManifoldPoint& contactpoint, float damage);
 
+      FormationController* getMaster( void ) { return myMaster_; }
+      FormationController* getController( void ) { return this; }
+      FormationController* getSlave( void ) { return this->slaves_.back(); }
+
   protected:
       bool formationFlight_;
@@ -127,13 +132,13 @@
       void searchNewMaster();
       void commandSlaves();
+      void takeLeadOfFormation();
+      void loseMasterState();
       void setNewMasterWithinFormation();
 
       void freeSlaves();
       void forceFreeSlaves();
-      void loseMasterState();
       void forceFreedom();
       bool forcedFree();
 
-      void takeLeadOfFormation();
       void masterAttacked(Pawn* originator);
 
@@ -159,4 +164,5 @@
 
       void setTarget(Pawn* target);
+
       void searchNewTarget();
       void forgetTarget();
Index: /code/trunk/src/orxonox/controllers/HumanController.cc
===================================================================
--- /code/trunk/src/orxonox/controllers/HumanController.cc	(revision 9624)
+++ /code/trunk/src/orxonox/controllers/HumanController.cc	(revision 9625)
@@ -102,5 +102,5 @@
 
         // commandslaves when Master of a formation
-        if (HumanController::localController_s && HumanController::localController_s->state_==MASTER)
+        if (HumanController::localController_s && HumanController::localController_s->state_==MASTER && FormationController::slaves_.size() > 0)
         {
             if (HumanController::localController_s->formationMode_ != ATTACK)
Index: /code/trunk/src/orxonox/worldentities/pawns/Pawn.cc
===================================================================
--- /code/trunk/src/orxonox/worldentities/pawns/Pawn.cc	(revision 9624)
+++ /code/trunk/src/orxonox/worldentities/pawns/Pawn.cc	(revision 9625)
@@ -47,4 +47,6 @@
 #include "weaponsystem/WeaponSet.h"
 
+#include "controllers/FormationController.h"
+
 namespace orxonox
 {
@@ -306,4 +308,5 @@
     }
 
+
     void Pawn::death()
     {
@@ -320,6 +323,34 @@
 
             if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
-                this->getPlayer()->stopControl();
-
+            {
+            	// Start to control a new entity if you're the master of a formation
+            	if(this->hasSlaves())
+            	{
+            		Controller* slave = this->getSlave();
+            		ControllableEntity* entity = slave->getControllableEntity();
+
+
+            		if(!entity->hasHumanController())
+            		{
+            			// delete the AIController
+            			slave->setControllableEntity(0);
+
+						// set a new master within the formation
+						orxonox_cast<FormationController*>(this->getController())->setNewMasterWithinFormation(orxonox_cast<FormationController*>(slave));
+
+						// start to control a slave
+						this->getPlayer()->startControl(entity);
+            		}
+            		else
+                	{
+                		this->getPlayer()->stopControl();
+                	}
+
+            	}
+            	else
+            	{
+            		this->getPlayer()->stopControl();
+            	}
+            }
             if (GameMode::isMaster())
             {
@@ -468,3 +499,32 @@
     }
 
+
+    // A function to check if this pawn's controller is the master of any formationcontroller
+    bool Pawn::hasSlaves()
+    {
+    	for (ObjectList<FormationController>::iterator it =
+			 ObjectList<FormationController>::begin();
+			 it != ObjectList<FormationController>::end(); ++it )
+		{
+			// checks if the pawn's controller has a slave
+			if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
+				return true;
+		}
+		return false;
+    }
+
+    // A function that returns a slave of the pawn's controller
+    Controller* Pawn::getSlave(){
+    	for (ObjectList<FormationController>::iterator it =
+    			ObjectList<FormationController>::begin();
+    			it != ObjectList<FormationController>::end(); ++it )
+    	{
+    		if (this->hasHumanController() && it->getMaster() == this->getPlayer()->getController())
+    			return it->getController();
+    	}
+    	return 0;
+    }
+
+
+
 }
Index: /code/trunk/src/orxonox/worldentities/pawns/Pawn.h
===================================================================
--- /code/trunk/src/orxonox/worldentities/pawns/Pawn.h	(revision 9624)
+++ /code/trunk/src/orxonox/worldentities/pawns/Pawn.h	(revision 9625)
@@ -185,4 +185,6 @@
 
             virtual void death();
+            virtual bool hasSlaves();
+            virtual Controller* getSlave();
             virtual void goWithStyle();
             virtual void deatheffect();
