Index: code/branches/formation/src/orxonox/controllers/Masterable.cc
===================================================================
--- code/branches/formation/src/orxonox/controllers/Masterable.cc	(revision 8956)
+++ code/branches/formation/src/orxonox/controllers/Masterable.cc	(revision 8957)
@@ -40,4 +40,5 @@
 #include "gametypes/TeamDeathmatch.h"
 #include "gametypes/Dynamicmatch.h"
+#include "gametypes/Gametype.h"
 #include "controllers/WaypointPatrolController.h"
 #include "controllers/NewHumanController.h"
@@ -393,5 +394,10 @@
            
             //same team?
-            if (!Masterable::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
+            Gametype* gt=this->getGametype();
+            if (!gt)
+            {
+                gt=it->getGametype();
+            }
+            if (!Masterable::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it),gt))
                 continue;
 
@@ -898,35 +904,16 @@
     }
 
-  bool Masterable::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
-    {
-        if (entity1 == entity2)
-            return true;
+/*
+    bool Masterable::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
+    {
+        if (!entity1 || !entity2) return false;
+        if (entity1 == entity2) return true;
+
+        if (!gametype)
+            orxout(debug_output) << "gametype NULL"<< endl;
 
         int team1 = -1;
         int team2 = -1;
 
-        Controller* controller = 0;
-        if (entity1->getController())
-            controller = entity1->getController();
-        else
-            controller = entity1->getXMLController();
-        if (controller)
-        {
-            Masterable* ac = orxonox_cast<Masterable*>(controller);
-            if (ac)
-                team1 = ac->getTeam();
-        }
-
-        if (entity2->getController())
-            controller = entity2->getController();
-        else
-            controller = entity2->getXMLController();
-        if (controller)
-        {
-            Masterable* ac = orxonox_cast<Masterable*>(controller);
-            if (ac)
-                team2 = ac->getTeam();
-        }
-
         TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype);
         if (tdm)
@@ -937,7 +924,14 @@
             if (entity2->getPlayer())
                 team2 = tdm->getTeam(entity2->getPlayer());
-            return (team1 == team2 && team1 != -1); //temp solution
-        }
-
+             orxout(debug_output) << "teamdeath okay"<< endl;
+        }
+
+        if (team1!=-1 && team2!=-1)
+        {
+            orxout(debug_output) << "Sameteam: decided in TeamDeathmatch. return "<<(team1 == team2 && team1 != -1)<< endl;
+            return (team1 == team2 && team1 != -1); 
+        }
+        team1=team2=-1;
+        orxout(debug_output) << "Sameteam: not in teamdeathmatch."<< endl;
         TeamBaseMatchBase* base = 0;
         base = orxonox_cast<TeamBaseMatchBase*>(entity1);
@@ -973,4 +967,9 @@
             }
         }
+
+        if (team1!=-1 && team2!=-1)
+            return (team1 == team2 && team1 != -1); 
+        team1=team2=-1;
+
 
         DroneController* droneController = 0;
@@ -1005,4 +1004,114 @@
 
         return (team1 == team2 && team1 != -1);
+
+    }*/
+
+  bool Masterable::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
+    {
+        if (entity1 == entity2)
+            return true;
+
+        int team1 = -1;
+        int team2 = -1;
+
+        Controller* controller = 0;
+        if (entity1->getController())
+            controller = entity1->getController();
+        else
+            controller = entity1->getXMLController();
+        if (controller)
+        {
+            Masterable* ac = orxonox_cast<Masterable*>(controller);
+            if (ac)
+                team1 = ac->getTeam();
+        }
+
+        if (entity2->getController())
+            controller = entity2->getController();
+        else
+            controller = entity2->getXMLController();
+        if (controller)
+        {
+            Masterable* ac = orxonox_cast<Masterable*>(controller);
+            if (ac)
+                team2 = ac->getTeam();
+        }
+
+        TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype);
+        if (tdm)
+        {
+            if (entity1->getPlayer())
+                team1 = tdm->getTeam(entity1->getPlayer());
+
+            if (entity2->getPlayer())
+                team2 = tdm->getTeam(entity2->getPlayer());
+            return (team1 == team2 && team1 != -1); //temp solution
+        }
+
+        TeamBaseMatchBase* base = 0;
+        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
+        if (base)
+        {
+            switch (base->getState())
+            {
+                case BaseState::ControlTeam1:
+                    team1 = 0;
+                    break;
+                case BaseState::ControlTeam2:
+                    team1 = 1;
+                    break;
+                case BaseState::Uncontrolled:
+                default:
+                    team1 = -1;
+            }
+        }
+        base = orxonox_cast<TeamBaseMatchBase*>(entity2);
+        if (base)
+        {
+            switch (base->getState())
+            {
+                case BaseState::ControlTeam1:
+                    team2 = 0;
+                    break;
+                case BaseState::ControlTeam2:
+                    team2 = 1;
+                    break;
+                case BaseState::Uncontrolled:
+                default:
+                    team2 = -1;
+            }
+        }
+
+        DroneController* droneController = 0;
+        droneController = orxonox_cast<DroneController*>(entity1->getController());
+        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2)
+            return true;
+        droneController = orxonox_cast<DroneController*>(entity2->getController());
+        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1)
+            return true;
+        DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController());
+        DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController());
+        if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner())
+            return true;
+
+        Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype);
+        if (dynamic)
+        {
+            if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;}
+
+            if (entity1->getPlayer())
+                team1 = dynamic->getParty(entity1->getPlayer());
+
+            if (entity2->getPlayer())
+                team2 = dynamic->getParty(entity2->getPlayer());
+
+            if (team1 ==-1 ||team2 ==-1 ) {return false;}
+            else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;}
+            else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;}
+            else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;}
+            else return true;
+        }
+
+        return (team1 == team2 && team1 != -1);
     }
 
Index: code/branches/formation/src/orxonox/controllers/Masterable.h
===================================================================
--- code/branches/formation/src/orxonox/controllers/Masterable.h	(revision 8956)
+++ code/branches/formation/src/orxonox/controllers/Masterable.h	(revision 8957)
@@ -77,5 +77,6 @@
 
       inline void setTeam(int team)
-           { this->team_ = team; }
+           { this->team_ = team; 
+             orxout(debug_output) << "Set team to: "<<team<<" in "<<this<< endl;}
       inline int getTeam() const
            { return this->team_; }
