Index: code/branches/turretFS14/src/modules/objects/Turret.cc
===================================================================
--- code/branches/turretFS14/src/modules/objects/Turret.cc	(revision 10049)
+++ code/branches/turretFS14/src/modules/objects/Turret.cc	(revision 10060)
@@ -30,4 +30,7 @@
 #include "core/CoreIncludes.h"
 #include "core/XMLPort.h"
+#include "Scene.h"
+#include <OgreSceneQuery.h>
+
 
 namespace orxonox
@@ -73,20 +76,23 @@
     /**
         @brief
-        Checks, if a (world)position is inside the turret's range.
+        Checks, if a WorldEntity is inside the turret's range.
 
         This function is safe to use on turrets that are attached, rotated, etc.
-        The turret's range is determined with the maxPitch, maxYaw, and the two attackRadius'.
-
-        @param position
-        The position to check
-    */
-    bool Turret::isInRange(const Vector3 &position)
+        The turret's range is determined with the maxPitch, maxYaw, and the two attackRadius.
+
+        @param target
+        The WorldEntity to check
+
+        @return
+        The squared distance to the position. -1, if it's ouside of range
+    */
+    float Turret::isInRange(const WorldEntity* target ) const
     {
         //Check distance
-        Vector3 distance = position - this->getWorldPosition();
+        Vector3 distance = target->getWorldPosition() - this->getWorldPosition();
         float distanceVal = distance.squaredLength();
         if(distanceVal > (this->maxAttackRadius_ * this->maxAttackRadius_) || distanceVal < (this->minAttackRadius_ * this->minAttackRadius_))
         {
-            return false;
+            return -1.f;
         }
 
@@ -100,5 +106,5 @@
         if(angle > this->maxPitch_)
         {
-            return false;
+            return -1.f;
         }
 
@@ -111,7 +117,13 @@
         if(angle > this->maxYaw_)
         {
-            return false;
-        }
-        return true;
+            return -1.f;
+        }
+
+        Ogre::SceneManager* scenemanager = this->getScene()->getSceneManager();
+        Ogre::Ray ray = Ogre::Ray(this->getWorldPosition(), distance);
+        Ogre::DefaultRaySceneQuery rayscenequery = Ogre::DefaultRaySceneQuery(scenemanager);
+        rayscenequery.setRay(ray);
+
+        return distanceVal;
     }
 
@@ -215,4 +227,7 @@
 
         Every tick, the turret gets rotated if it should, and the local axes get updated with the parent's rotation.
+    
+        @param dt
+        Duration of the tick
     */
     void Turret::tick(float dt)
@@ -220,5 +235,5 @@
         SUPER(Turret, tick, dt);
 
-
+        //Stuff isn't properly initialized in the c'tor, so we have to do it like this
         if(!this->once_)
         {
@@ -256,4 +271,6 @@
         {
             //Don't make the rotation instantaneous. Use an arbitrary interpolation, not that great...
+            //TODO: make the rotation better (constant velocity etc.). At the moment, the turret rotates
+            //      slower the closer it is to the destination
             Quaternion drot = Quaternion::nlerp(dt*this->rotationThrust_/20.f, Quaternion::IDENTITY, this->rotation_);
             this->rotate(drot, WorldEntity::World);
Index: code/branches/turretFS14/src/modules/objects/Turret.h
===================================================================
--- code/branches/turretFS14/src/modules/objects/Turret.h	(revision 10049)
+++ code/branches/turretFS14/src/modules/objects/Turret.h	(revision 10060)
@@ -46,4 +46,9 @@
         it's behaviour.
 
+        This class also contains a custom local coordinate system, which gets initially rotated through xml, and
+        afterwards is updated with the parent's rotation (if there is one). This allows for almost trivialal calculation
+        of pitch, yaw and roll through coordinate transformation. (TODO: Ogre should do something like this already, investigate...)
+        
+
     @note
         The rotation isn't limited "physically". You have to call isInRange to find out if the turret is allowed to shoot at a target.
@@ -58,5 +63,5 @@
             virtual void rotateYaw(const Vector2& value);
             virtual void rotateRoll(const Vector2& value);
-            virtual bool isInRange(const Vector3 &position);
+            virtual float isInRange(const WorldEntity* target) const;
             virtual void aimAtPosition(const Vector3 &position);
 
Index: code/branches/turretFS14/src/modules/objects/controllers/TeamTargetProxy.cc
===================================================================
--- code/branches/turretFS14/src/modules/objects/controllers/TeamTargetProxy.cc	(revision 10049)
+++ code/branches/turretFS14/src/modules/objects/controllers/TeamTargetProxy.cc	(revision 10060)
@@ -28,11 +28,19 @@
 
 #include "TeamTargetProxy.h"
+#include "worldentities/ControllableEntity.h"
 #include "worldentities/pawns/Pawn.h"
 
- namespace orxonox
- {
- 	RegisterClass(TeamTargetProxy);
+namespace orxonox
+{
+	RegisterClass(TeamTargetProxy);
 
- 	TeamTargetProxy::TeamTargetProxy(Context* context) : FormationController(context)
+   /**
+       @brief 
+       Sets default values for all variables.
+
+       @param context
+       The context
+    */
+	TeamTargetProxy::TeamTargetProxy(Context* context) : FormationController(context)
  	{
  		RegisterObject(TeamTargetProxy);
@@ -41,8 +49,18 @@
  	}
 
+    /**
+        @brief 
+        Destructor. Nothing to see here.
+     */
  	TeamTargetProxy::~TeamTargetProxy()
  	{
  	}
 
+    /**
+        @brief
+        Copies the team and the target from the parent.
+
+        That's all there is.
+    */
  	void TeamTargetProxy::tick(float dt)
  	{
@@ -51,29 +69,30 @@
 
         ControllableEntity* parent = orxonox_cast<ControllableEntity*> (this->getControllableEntity()->getParent());
- 		
 
+        if(this->getTeam() != -1 && !this->once_ && parent)
+        {
+            orxout(internal_warning) << "TeamTargetProxy: Team already set, may result in undesired behaviour. Will get overridden by the parent's team." << endl;
+        }
+
+        if(!this->once_)
+            this->once_ = true;
+
+        //Teams aren't set immediately, after creation, so we have to check every tick...
+        if(parent)
+        {
+            Controller* parentcontroller = parent->getController();
+            if(parentcontroller)
+            {
+                this->setTeam(parentcontroller->getTeam());
+            }
+            else
+            {
+                this->setTeam(parent->getTeam());
+            }
+            this->getControllableEntity()->setTeam(parent->getTeam());
+        }
 
         if(parent)
         {
-
-            if(!this->once_)
-            {
-                //Set the same team
-                if(parent)
-                {
-                    Controller* parentcontroller = parent->getController();
-                    if(parentcontroller)
-                    {
-                        this->setTeam(parentcontroller->getTeam());
-                    }
-                    else
-                    {
-                        this->setTeam(parent->getTeam());
-                    }
-                    this->getControllableEntity()->setTeam(parent->getTeam());
-                }
-                this->once_ = true;
-            }
-
             Pawn* parenttarget = orxonox_cast<Pawn*>(parent->getTarget());
             if(parenttarget)
@@ -83,5 +102,4 @@
             }
         }
-
  	}
- }
+}
Index: code/branches/turretFS14/src/modules/objects/controllers/TeamTargetProxy.h
===================================================================
--- code/branches/turretFS14/src/modules/objects/controllers/TeamTargetProxy.h	(revision 10049)
+++ code/branches/turretFS14/src/modules/objects/controllers/TeamTargetProxy.h	(revision 10060)
@@ -35,4 +35,14 @@
  namespace orxonox
  {
+ 	/**
+ 		@brief
+ 		A controller, that just copies the team and the target of a parent for itself and it's controllable entity.
+
+ 		Useful for following (and similar) situations: (-> means attached to)
+			turret (rotates) -> some kind of turret base (looks nice) -> spaceship (flies around)
+			The turret has a controller that wants to copy the spaceship's target and team. In this case it doesn't work though,
+			because the turret isn't directly attached to the spaceship. Here's where this controller comes in. Drawback: the base
+			has to be controllable and ticks every second (performance?)
+ 	*/
  	class _OrxonoxExport TeamTargetProxy : public FormationController, public Tickable
  	{
@@ -44,5 +54,5 @@
 
  		private:
- 			bool once_;
+ 			bool once_; //!< Flag for executing code in the tick function only once.
  	};
  }
Index: code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc
===================================================================
--- code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc	(revision 10049)
+++ code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc	(revision 10060)
@@ -35,4 +35,11 @@
  	RegisterClass(TurretController);
 
+    /**
+        @brief 
+        Sets default values for all variables.
+
+        @param context
+        The context
+    */
  	TurretController::TurretController(Context* context) : ArtificialController(context)
  	{
@@ -43,4 +50,8 @@
  	}
 
+    /**
+        @brief 
+        Destructor. Nothing to see here.
+    */
  	TurretController::~TurretController()
  	{
@@ -48,8 +59,20 @@
  	}
 
+    /**
+        @brief 
+        Searches a valid target for the turret to aim at.
+
+        Loops through all pawns and tests, if it is in range. Scores every pawn and chooses the best one (the one with the lowest score).
+        If the turret has a parent, try to aim at the same target the parent has, if there is one.
+
+        @see targetScore
+        The function that scores the pawns.
+    */
  	void TurretController::searchTarget()
  	{
         Turret* turret = orxonox_cast<Turret*>(this->getControllableEntity());
-        if(target_ && turret->isInRange(target_->getWorldPosition()))
+
+        //The controller might find a target before teams are set, so we need to check again here.
+        if(this->target_ && turret->isInRange(target_) != -1.f && !FormationController::sameTeam(turret, this->target_, this->getGametype()))
         {
         	return;
@@ -66,5 +89,5 @@
         {
         	Pawn* parenttarget = orxonox_cast<Pawn*>(parent->getTarget());
-        	if(parenttarget && turret->isInRange(parenttarget->getWorldPosition()))
+        	if(parenttarget && turret->isInRange(parenttarget))
         	{
         		this->setTarget(parenttarget);
@@ -74,19 +97,36 @@
         }
 
-  		for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
+        float maxScore = 0;
+        float tempScore;
+        Pawn* maxScorePawn = 0;
+
+  		for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)     
         {
         	Pawn* entity = orxonox_cast<Pawn*>(*it);
-            if (this->FormationController::sameTeam(this->getControllableEntity(), entity, this->getGametype()))
+            if (!entity || FormationController::sameTeam(this->getControllableEntity(), entity, this->getGametype()))
             	continue;
-
-            if(turret->isInRange(entity->getWorldPosition()))
+            tempScore = turret->isInRange(entity);
+            if(tempScore != -1.f)
             {
-            	this->setTarget(entity);
-            	turret->setTarget(entity);
-            	break;
+            	if(tempScore > maxScore)
+            	{
+            		maxScore = tempScore;
+            		maxScorePawn = entity;
+            	}
             }
-    	}		
- 	}
-
+    	}
+        this->setTarget(maxScorePawn);
+        turret->setTarget(maxScorePawn);
+ 	}
+
+    /**
+        @brief 
+        Tests, if the turret is looking at the target, with a specified tolerance
+
+		This uses the world position as opposed to the local position in the old version.
+
+		@param angle
+		The tolerance, in radians
+    */
     bool TurretController::isLookingAtTargetNew(float angle) const
     {
@@ -94,4 +134,28 @@
     }
 
+    /**
+        @brief 
+        Scores a pawn as a target, based on distance and health.
+
+        The more health and distance a pawn has, the higher the score. This means lower equals better target.
+
+		@param pawn
+		The pawn to score
+
+		@param distance
+		The distance. Can be squared or normed, doesn't matter as long as all are treated the same.
+    */   
+ 	float TurretController::targetScore(Pawn* pawn, float distance) const
+ 	{
+ 		return pawn->getHealth()/pawn->getMaxHealth() + distance;
+ 	}
+
+    /**
+    	@brief
+    	Does all the controlling of the turret.
+
+    	If the turret has a parent, copies the team from there, if it's not already set.
+    	Other actions are: Search a target. If a target has been found, aim and shoot at it.
+    */
  	void TurretController::tick(float dt)
  	{
@@ -100,33 +164,30 @@
 
 
- 		if(!this->once_)
- 		{
- 			if(this->getTeam() != -1)
- 			{
- 				orxout(internal_warning) << "Turret: Team already set, may result in undesired behaviour" << endl;
- 			}
- 			else
- 			{
-	            //Make sure the turret is in the same team as the parent
-	            ControllableEntity* parent = orxonox_cast<ControllableEntity*> (this->getControllableEntity()->getParent());
-	            if(parent)
-	            {
-	                Controller* parentcontroller = parent->getController();
-	                if(parentcontroller)
-	                {
-	                    this->setTeam(parentcontroller->getTeam());
-	                }
-	                else
-	                {
-	                    this->setTeam(parent->getTeam());
-	                }
-	                this->getControllableEntity()->setTeam(parent->getTeam());
-	            }
-        	}
-            this->once_ = true;
+        ControllableEntity* parent = orxonox_cast<ControllableEntity*> (this->getControllableEntity()->getParent());
+        if(this->getTeam() != -1 && !this->once_ && parent)
+        {
+            orxout(internal_warning) << "TurretController: Team already set, may result in undesired behaviour. Will get overridden by the parent's team." << endl;
+        }
+
+        if(!this->once_)
+        	this->once_ = true;
+     
+        //Teams aren't set immediately, after creation, so we have to check every tick...
+        if(parent)
+        {
+            Controller* parentcontroller = parent->getController();
+            if(parentcontroller)
+            {
+                this->setTeam(parentcontroller->getTeam());
+            }
+            else
+            {
+                this->setTeam(parent->getTeam());
+            }
+            this->getControllableEntity()->setTeam(parent->getTeam());
         }
 
  		this->searchTarget();
-		if(target_)
+		if(this->target_)
  		{
  			Turret* turret = orxonox_cast<Turret*> (this->getControllableEntity());
Index: code/branches/turretFS14/src/modules/objects/controllers/TurretController.h
===================================================================
--- code/branches/turretFS14/src/modules/objects/controllers/TurretController.h	(revision 10049)
+++ code/branches/turretFS14/src/modules/objects/controllers/TurretController.h	(revision 10060)
@@ -57,6 +57,7 @@
  			void searchTarget();
  			bool isLookingAtTargetNew(float angle) const;
+ 			float targetScore(Pawn* pawn, float distance) const;
 
- 			bool once_;
+ 			bool once_; //!< Flag for executing code in the tick function only once.
  	};
  }
Index: code/branches/turretFS14/src/orxonox/worldentities/WorldEntity.cc
===================================================================
--- code/branches/turretFS14/src/orxonox/worldentities/WorldEntity.cc	(revision 10049)
+++ code/branches/turretFS14/src/orxonox/worldentities/WorldEntity.cc	(revision 10060)
@@ -414,5 +414,5 @@
             else if (this->isDynamic())
             {
-                //hacky hack, really shouldn't do that
+                //***HACKY HACK (to allow turrets to be attached)***
                 //orxout(internal_warning) << "Cannot attach a dynamic object to a WorldEntity." << endl;
                 //return false;
