Index: /code/branches/turretFS14/data/levels/templates/spaceshipAssff.oxt
===================================================================
--- /code/branches/turretFS14/data/levels/templates/spaceshipAssff.oxt	(revision 10030)
+++ /code/branches/turretFS14/data/levels/templates/spaceshipAssff.oxt	(revision 10031)
@@ -48,4 +48,14 @@
       <BlinkingBillboard position="17,-1.5,0" material="Examples/Flare" colour="1.0, 0.5, 0.3" amplitude=0.1 frequency=0.5 quadratic=1 />
       <BlinkingBillboard position="-17,-1.5,0" material="Examples/Flare" colour="0.5, 1.0, 0.3" amplitude=0.1 frequency=0.5 phase=180 quadratic=1 />
+
+              <Turret position="0,10,0" pitch="90" yaw="0" roll="0" maxPitch=90 maxYaw=90 attackRadius=2000>
+            <templates>
+              <Template link=spaceshipturrettest />
+            </templates>
+            <controller>
+              <TurretController team=10 />
+            </controller>
+        </Turret>
+
     </attached>
     <collisionShapes>
Index: /code/branches/turretFS14/data/levels/templates/spaceshipTurretTest.oxt
===================================================================
--- /code/branches/turretFS14/data/levels/templates/spaceshipTurretTest.oxt	(revision 10030)
+++ /code/branches/turretFS14/data/levels/templates/spaceshipTurretTest.oxt	(revision 10031)
@@ -2,5 +2,5 @@
   <Turret position="0,0,0" collisionType="dynamic" angularDamping=0.999999 mass=100>
     <attached>
-        <Model position="0,0,0" pitch="90" roll="0" mesh="turretHead.mesh" scale3D="10,10,10"/>
+        <Model position="0,0,0" pitch="-90" roll="0" mesh="turretHead.mesh" scale3D="10,10,10"/>
     </attached>
     <collisionShapes>
Index: /code/branches/turretFS14/data/levels/turretTest.oxw
===================================================================
--- /code/branches/turretFS14/data/levels/turretTest.oxw	(revision 10030)
+++ /code/branches/turretFS14/data/levels/turretTest.oxw	(revision 10031)
@@ -34,5 +34,5 @@
 
 
-<StaticEntity position="0,0,0" yaw=0 pitch=0>
+<MovableEntity position="0,0,0" yaw=0 pitch=0>
     <attached>
         <Model position="0,0,0" mesh="turretSocketFront.mesh" scale3D="10,10,10" />
@@ -42,5 +42,5 @@
         <Model position="0,0,0" mesh="turretSocketRight.mesh" scale3D="10,10,10" />
 
-        <Turret position="0,10,0" pitch="-90" yaw="0" roll="0" maxPitch=30 maxYaw=30>
+        <!-- <Turret position="0,10,0" pitch="90" yaw="0" roll="0" maxPitch=90 maxYaw=90 attackRadius=2000>
             <templates>
               <Template link=spaceshipturrettest />
@@ -49,14 +49,14 @@
               <TurretController team=10 />
             </controller>
-        </Turret>
+        </Turret> -->
 
-        <Model mesh="sphere.mesh" position="25,0,0" scale=1 />
+        <Model mesh="sphere.mesh" position="20,0,0" scale=1/>
 
-        <Model mesh="sphere.mesh" position="0,25,0" scale=1 />
+        <Model mesh="sphere.mesh" position="0,25,0" scale=1/>
 
-        <Model mesh="sphere.mesh" position="0,0,25" scale=1 />
+        <Model mesh="sphere.mesh" position="0,0,30" scale=1/>
 
     </attached>
-</StaticEntity>
+</MovableEntity>
 
 
Index: /code/branches/turretFS14/src/modules/objects/Turret.cc
===================================================================
--- /code/branches/turretFS14/src/modules/objects/Turret.cc	(revision 10030)
+++ /code/branches/turretFS14/src/modules/objects/Turret.cc	(revision 10031)
@@ -44,7 +44,12 @@
     {
         RegisterObject(Turret);
-        this->startOrientInv_ = Quaternion::IDENTITY;
+        this->startOrient_ = Quaternion::IDENTITY;
+        this->startDir_ = Vector3::ZERO;
+        this->localZ_ = Vector3::UNIT_Z;
+        this->localY_ = Vector3::UNIT_Y;
+        this->localX_ = Vector3::UNIT_X;
         this->maxPitch_ = 0;
         this->maxYaw_ = 0;
+        this->attackRadius_ = 200;
         this->gotOrient_ = false;
         this->rotationThrust_ = 50;
@@ -63,80 +68,75 @@
 
     void Turret::rotatePitch(const Vector2& value)
-    {
-        if (this->maxPitch_ == 0)
-        {
-            return;
-        }
-        if (this->maxPitch_ >= 180) //no need to check, if the limit too big
-        {
-            this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);
-            return;
-        }
-
-        Quaternion drot = startOrientInv_ * this->getOrientation();
-
-        Ogre::Real val = boundBetween(drot.getPitch(false).valueDegrees(), -180, 180);
-        Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180);
-        Ogre::Real lowerBound = offset - this->maxPitch_;
-        Ogre::Real upperBound = offset + this->maxPitch_;
-        if (lowerBound < -180) //Avoid wrapping around of the boundaries
-        {
-            lowerBound += this->maxPitch_;
-            upperBound += this->maxPitch_;
-            val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here
-        }
-        else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side)
-        {
-            lowerBound -= this->maxPitch_;
-            upperBound -= this->maxPitch_;
-            val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here
-        }
-        if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) 
+    {    
+        //This is a failed attempt at limiting the turret's rotation. It's handled in the controller (for now?)
+        /*
+        Vector3 currentDir = getTransformedVector(this->getOrientation() * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_);
+        Vector3 currentDirProjected = currentDir;
+        currentDirProjected.x = 0;
+        Vector3 startDirProjected = this->startDir_;
+        startDirProjected.x = 0;     
+        Ogre::Real angle = startDirProjected.angleBetween(currentDirProjected).valueDegrees();
+        //orxout() << "Pitch: " << angle << endl;   
+        //if(angle < this->maxPitch_ || (currentDirProjected.y <= 0 && value.x > 0) || (currentDirProjected.y > 0 && value.x < 0) )
         {
             this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);
         }
-        return;
+        */
+        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);
     }
 
     void Turret::rotateYaw(const Vector2& value)
     {
-        if (this->maxPitch_ == 0)
-        {
-            return;
-        }
-        if (this->maxPitch_ >= 180) //no need to check, if the limit too big
+        //This is a failed attempt at limiting the turret's rotation. It's handled in the controller (for now?)
+        /*
+        Vector3 currentDir = getTransformedVector(this->getOrientation() * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_);
+        Vector3 currentDirProjected = currentDir;
+        currentDirProjected.y = 0;
+        Vector3 startDirProjected = this->startDir_;
+        startDirProjected.y = 0;
+        Ogre::Real angle = startDirProjected.angleBetween(currentDirProjected).valueDegrees();
+        orxout() << "Yaw: " << angle << endl;
+        if(angle < this->maxYaw_ || (currentDirProjected.x <= 0 && value.x < 0) || (currentDirProjected.x > 0 && value.x > 0))
         {
             this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f);
-            return;
         }
-
-        Quaternion drot = startOrientInv_ * this->getOrientation();
-
-        Ogre::Real val = boundBetween(drot.getYaw(false).valueDegrees(), -180, 180);
-        Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180);
-        Ogre::Real lowerBound = offset - this->maxPitch_;
-        Ogre::Real upperBound = offset + this->maxPitch_;
-        if (lowerBound < -180) //Avoid wrapping around of the boundaries
-        {
-            lowerBound += this->maxPitch_;
-            upperBound += this->maxPitch_;
-            val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here
-        }
-        else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side)
-        {
-            lowerBound -= this->maxPitch_;
-            upperBound -= this->maxPitch_;
-            val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here
-        }
-        if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) 
-        {
-           this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f);
-        }
-        return;
+        */
+        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f);
     }
 
     void Turret::rotateRoll(const Vector2& value)
     {
-        return; //Standard turrets don't roll
+        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x*0.8f);
+    }
+
+    bool Turret::isInRange(Vector3 position)
+    {
+        Vector3 distance = position - this->getPosition();
+        if(distance.squaredLength() > (this->attackRadius_ * this->attackRadius_))
+        {
+            return false;
+        }
+
+        Vector3 dir = getTransformedVector(distance, this->localX_, this->localY_, this->localZ_);
+        Vector3 dirProjected = dir;
+        dirProjected.x = 0;
+        Vector3 startDirProjected = this->startDir_;
+        startDirProjected.x = 0;
+        Ogre::Real angle = startDirProjected.angleBetween(dirProjected).valueDegrees();
+        if(angle > this->maxPitch_)
+        {
+            return false;
+        }
+
+        dirProjected = dir;
+        dirProjected.y = 0;
+        startDirProjected = this->startDir_;
+        startDirProjected.y = 0;
+        angle = startDirProjected.angleBetween(dirProjected).valueDegrees();
+        if(angle > this->maxYaw_)
+        {
+            return false;
+        }
+        return true;
     }
 
@@ -145,4 +145,5 @@
         XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode);
         XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode);
+        XMLPortParam(Turret, "attackRadius", setAttackRadius, getAttackRadius, xmlelement, mode);
         SUPER(Turret, XMLPort, xmlelement, mode);
     }
@@ -154,10 +155,18 @@
         if(!gotOrient_)
         {
-            startOrientInv_ = this->getOrientation().Inverse();
-            gotOrient_ = true;
+            this->startOrient_ = this->getOrientation();
+            this->localX_ = this->startOrient_ * this->localX_;
+            this->localX_.normalise();
+            this->localY_ = this->startOrient_ * this->localY_;
+            this->localY_.normalise();
+            this->localZ_ = this->startOrient_ * this->localZ_;
+            this->localZ_.normalise();
+
+            //startDir should always be (0,0,-1)
+            this->startDir_ = getTransformedVector(this->startOrient_ * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_);
+
+            this->gotOrient_ = true;
         }
-        Quaternion drot = startOrientInv_ * this->getOrientation();
-        orxout() << "Pitch: " << drot.getPitch(false).valueDegrees() << "\tYaw: " << drot.getYaw(false).valueDegrees() << "\tRoll: " << drot.getRoll(false).valueDegrees() << endl;
-        
+
         this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
         this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
@@ -165,13 +174,3 @@
     }
 
-
-    Ogre::Real Turret::boundBetween(Ogre::Real val, Ogre::Real lowerBound, Ogre::Real upperBound)
-    {
-        if (lowerBound > upperBound){ std::swap(lowerBound, upperBound); }
-        val -= lowerBound; //adjust to 0
-        Ogre::Real rangeSize = upperBound - lowerBound;
-        if (rangeSize == 0){ return upperBound; } //avoid dividing by 0
-        return val - (rangeSize * std::floor(val / rangeSize)) + lowerBound;
-    }
-
 }
Index: /code/branches/turretFS14/src/modules/objects/Turret.h
===================================================================
--- /code/branches/turretFS14/src/modules/objects/Turret.h	(revision 10030)
+++ /code/branches/turretFS14/src/modules/objects/Turret.h	(revision 10031)
@@ -37,7 +37,5 @@
 
 #include "objects/ObjectsPrereqs.h"
-#include "OgreQuaternion.h"
-
-#include "worldentities/pawns/SpaceShip.h"
+#include "worldentities/pawns/Pawn.h"
 
 namespace orxonox
@@ -56,4 +54,6 @@
             virtual void tick(float dt);
 
+            virtual bool isInRange(Vector3 position);
+
             inline void setMaxPitch(Ogre::Real pitch)
                 {this->maxPitch_ = pitch;}
@@ -68,14 +68,24 @@
                 {return this->maxYaw_;}
 
+            inline void setAttackRadius(float radius)
+                {this->attackRadius_ = radius;}
+
+            inline float getAttackRadius()
+                {return this->attackRadius_;}
+
+
         private:
             bool gotOrient_;
             Ogre::Real maxPitch_;
             Ogre::Real maxYaw_;
-            Quaternion startOrientInv_;
+            float attackRadius_;
+            Quaternion startOrient_;
+            Vector3 startDir_;
+            Vector3 localZ_;
+            Vector3 localY_;
+            Vector3 localX_;
             float rotationThrust_;
 
             btVector3 localAngularAcceleration_;
-
-            Ogre::Real boundBetween(float val, float lowerBound, float upperBound);
     };
 }
Index: /code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc
===================================================================
--- /code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc	(revision 10030)
+++ /code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc	(revision 10031)
@@ -27,5 +27,7 @@
  */
 
- #include "TurretController.h"
+#include "TurretController.h"
+#include "worldentities/pawns/Pawn.h"
+#include "Turret.h"
 
  namespace orxonox
@@ -36,6 +38,4 @@
  	{
  		RegisterObject(TurretController);
- 		counter = 0;
- 		flag = false;
  	}
 
@@ -45,23 +45,60 @@
  	}
 
+ 	void TurretController::searchTarget()
+ 	{
+        Turret* turret = orxonox_cast<Turret*>(this->getControllableEntity());
+        if(target_ && turret->isInRange(target_->getPosition()))
+        {
+        	return;
+        }
+        else
+        {
+        	this->forgetTarget();
+        }
+
+
+        ControllableEntity* parent = orxonox_cast<ControllableEntity*>(turret->getParent());
+        if(parent)
+        {
+        	Pawn* parenttarget = orxonox_cast<Pawn*>(parent->getTarget());
+        	if(parenttarget && turret->isInRange(parenttarget->getPosition()))
+        	{
+        		this->setTarget(parenttarget);
+        		turret->setTarget(parenttarget);
+        		return;
+        	}
+        }
+
+  		for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
+        {
+        	Pawn* entity = orxonox_cast<Pawn*>(*it);
+            if (ArtificialController::sameTeam(this->getControllableEntity(), entity, this->getGametype()))
+            	continue;
+
+            if(turret->isInRange(entity->getPosition()))
+            {
+            	this->setTarget(entity);
+            	turret->setTarget(entity);
+            	break;
+            }
+    	}		
+ 	}
+
  	void TurretController::tick(float dt)
  	{
- 		counter += dt;
- 		if(counter >= 10)
+        if (!this->isActive() || !this->getControllableEntity())
+            return;
+ 		this->searchTarget();
+ 		this->getControllableEntity()->rotatePitch(0.2);
+ 		/*if(target_)
  		{
- 			counter = 0;
- 			flag = !flag;
- 			orxout() << "Direction change" << endl;
- 		}
- 		if(flag)
- 		{
- 			this->getControllableEntity()->rotatePitch(10*dt);
- 			//this->getControllableEntity()->rotateYaw(10*dt);
- 		}
- 		else
- 		{
- 			this->getControllableEntity()->rotatePitch(-10*dt);
- 			//this->getControllableEntity()->rotateYaw(-10*dt);
- 		}
+ 			this->aimAtTarget();
+ 			//It says move, but really it only turns
+ 			this->moveToTargetPosition();
+ 			if(this->isLookingAtTarget(Degree(5).valueRadians()))
+ 			{
+ 				orxout() << 42 << endl;
+ 			}
+ 		}*/
  	}
  }
Index: /code/branches/turretFS14/src/modules/objects/controllers/TurretController.h
===================================================================
--- /code/branches/turretFS14/src/modules/objects/controllers/TurretController.h	(revision 10030)
+++ /code/branches/turretFS14/src/modules/objects/controllers/TurretController.h	(revision 10031)
@@ -44,6 +44,5 @@
 
  		private:
- 			float counter;
- 			bool flag;
+ 			void searchTarget();
  	};
  }
Index: /code/branches/turretFS14/src/orxonox/worldentities/WorldEntity.cc
===================================================================
--- /code/branches/turretFS14/src/orxonox/worldentities/WorldEntity.cc	(revision 10030)
+++ /code/branches/turretFS14/src/orxonox/worldentities/WorldEntity.cc	(revision 10031)
@@ -414,6 +414,6 @@
             else if (this->isDynamic())
             {
-                orxout(internal_warning) << "Cannot attach a dynamic object to a WorldEntity." << endl;
-                return false;
+                //orxout(internal_warning) << "Cannot attach a dynamic object to a WorldEntity." << endl;
+                //return false;
             }
             else if (this->isKinematic() && newParent->isDynamic())
