Index: code/branches/turretFS14/src/modules/objects/Turret.cc
===================================================================
--- code/branches/turretFS14/src/modules/objects/Turret.cc	(revision 10042)
+++ code/branches/turretFS14/src/modules/objects/Turret.cc	(revision 10044)
@@ -21,5 +21,5 @@
  *
  *   Author:
- *      Marian Runo
+ *      Marian Runo, Martin Mueller
  *   Co-authors:
  *      ...
@@ -30,5 +30,4 @@
 #include "core/CoreIncludes.h"
 #include "core/XMLPort.h"
-#include "BulletDynamics/Dynamics/btRigidBody.h"
 
 namespace orxonox
@@ -45,6 +44,15 @@
         RegisterObject(Turret);
         this->rotationThrust_ = 50;
-
-        this->localAngularAcceleration_.setValue(0, 0, 0);
+        this->startDir_ = Vector3::ZERO;
+        this->localZ_ = Vector3::UNIT_Z;
+        this->localY_ = Vector3::UNIT_Y;
+        this->localX_ = Vector3::UNIT_X;
+        this->attackRadius_ = 200;
+        this->maxPitch_ = 90;
+        this->maxYaw_ = 90;
+        this->once_ = false;
+        this->rotation_ = Quaternion::IDENTITY;
+
+        this->setRadarVisibility(false);
     }
 
@@ -57,4 +65,47 @@
     }
 
+
+    bool Turret::isInRange(const Vector3 &position)
+    {
+        //Check distance
+        Vector3 distance = position - this->getWorldPosition();
+        if(distance.squaredLength() > (this->attackRadius_ * this->attackRadius_))
+        {
+            return false;
+        }
+
+        //Check pitch
+        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;
+        }
+
+        //Check yaw
+        dirProjected = dir;
+        dirProjected.y = 0;
+        startDirProjected = this->startDir_;
+        startDirProjected.y = 0;
+        angle = startDirProjected.angleBetween(dirProjected).valueDegrees();
+        if(angle > this->maxYaw_)
+        {
+            return false;
+        }
+        return true;
+    }
+
+    void Turret::aimAtPosition(const Vector3& position)
+    {
+        Vector3 currDir = this->getWorldOrientation() * WorldEntity::FRONT;
+        Vector3 targetDir = position - this->getWorldPosition();
+
+        this->rotation_ = currDir.getRotationTo(targetDir);
+
+    }
 
     void Turret::rotatePitch(const Vector2& value)
@@ -74,5 +125,4 @@
         }
         */
-        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);
     }
 
@@ -93,10 +143,8 @@
         }
         */
-        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f);
     }
 
     void Turret::rotateRoll(const Vector2& value)
     {
-        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x*0.8f);
     }
 
@@ -104,4 +152,9 @@
     {
         SUPER(Turret, XMLPort, xmlelement, mode);
+        
+        XMLPortParamVariable(Turret, "rotationThrust", rotationThrust_, xmlelement, mode);
+        XMLPortParam(Turret, "attackRadius", setAttackRadius, getAttackRadius, xmlelement, mode);
+        XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode);
+        XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode);
     }
 
@@ -110,15 +163,45 @@
         SUPER(Turret, tick, dt);
 
-        this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
-        this->localAngularAcceleration_ = physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_;
-
-        //physics don't work when attached :(
-        //this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
-
-        pitch(Degree(localAngularAcceleration_.x()*dt/1000), WorldEntity::World);
-        yaw(Degree(localAngularAcceleration_.y()*dt/1000), WorldEntity::World);
-        roll(Degree(localAngularAcceleration_.z()*dt/1000), WorldEntity::World);
-
-        this->localAngularAcceleration_.setValue(0, 0, 0);
+
+        if(!this->once_)
+        {
+
+            Quaternion startOrient = this->getOrientation();
+            this->localXStart_ = startOrient * this->localX_;
+            this->localXStart_.normalise();
+            this->localX_ = this->localXStart_;
+            this->localYStart_ = startOrient * this->localY_;
+            this->localYStart_.normalise();
+            this->localY_ = this->localYStart_;
+            this->localZStart_ = startOrient * this->localZ_;
+            this->localZStart_.normalise();
+            this->localZ_ = this->localZStart_;
+
+            //startDir should always be (0,0,-1)
+            this->startDir_ = getTransformedVector(startOrient * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_);
+
+            this->once_ = true;
+
+        }
+
+        //Adjust local axes to parent's rotation
+        WorldEntity* parent = this->getParent();
+        if(parent)
+        {
+            Quaternion parentrot = parent->getWorldOrientation();
+            this->localX_ = parentrot * this->localXStart_;
+            this->localY_ = parentrot * this->localYStart_;
+            this->localZ_ = parentrot * this->localZStart_;
+        }
+
+        //rotate
+        if(this->rotation_ != Quaternion::IDENTITY)
+        {
+            //Don't make the rotation instantaneous
+            Quaternion drot = Quaternion::Slerp(dt*this->rotationThrust_/20.f, Quaternion::IDENTITY, this->rotation_);
+            this->rotate(drot, WorldEntity::World);
+            this->rotation_ = Quaternion::IDENTITY;
+        }
+
     }
 }
Index: code/branches/turretFS14/src/modules/objects/Turret.h
===================================================================
--- code/branches/turretFS14/src/modules/objects/Turret.h	(revision 10042)
+++ code/branches/turretFS14/src/modules/objects/Turret.h	(revision 10044)
@@ -50,13 +50,46 @@
             virtual void rotateYaw(const Vector2& value);
             virtual void rotateRoll(const Vector2& value);
+            virtual bool isInRange(const Vector3 &position);
+            virtual void aimAtPosition(const Vector3 &position);
 
             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
             virtual void tick(float dt);
 
+            inline void setAttackRadius(float radius)
+                { this->attackRadius_ = radius; }
+
+            inline void setMaxPitch(float pitch)
+                { this->maxPitch_ = pitch; }
+
+            inline void setMaxYaw(float yaw)
+                { this->maxYaw_ = yaw; }
+
+            inline float getAttackRadius() const
+                { return this->attackRadius_; }                
+
+            inline float getMaxPitch() const
+                { return this->maxPitch_; }
+
+            inline float getMaxYaw() const
+                { return this->maxYaw_; }
+
+        protected:
+            Vector3 startDir_;
+            Vector3 localZ_;
+            Vector3 localZStart_;
+            Vector3 localY_;
+            Vector3 localYStart_;
+            Vector3 localX_;
+            Vector3 localXStart_;           
 
         private:
+            bool once_;
+
+            float attackRadius_;
+            Ogre::Real maxPitch_;
+            Ogre::Real maxYaw_;
             float rotationThrust_;
 
-            btVector3 localAngularAcceleration_;
+            Quaternion rotation_;
     };
 }
Index: code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc
===================================================================
--- code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc	(revision 10042)
+++ code/branches/turretFS14/src/modules/objects/controllers/TurretController.cc	(revision 10044)
@@ -38,13 +38,7 @@
  	{
  		RegisterObject(TurretController);
- 		this->startOrient_ = Quaternion::IDENTITY;
-        this->startDir_ = Vector3::ZERO;
-        this->localZ_ = Vector3::UNIT_Z;
-        this->localY_ = Vector3::UNIT_Y;
-        this->localX_ = Vector3::UNIT_X;
-        this->attackRadius_ = 200;
-        this->maxPitch_ = 90;
-        this->maxYaw_ = 90;
-        this->gotOrient_ = false;
+
+ 		this->once_ = false;
+
  	}
 
@@ -57,5 +51,5 @@
  	{
         Turret* turret = orxonox_cast<Turret*>(this->getControllableEntity());
-        if(target_ && this->isInRange(target_->getWorldPosition()))
+        if(target_ && turret->isInRange(target_->getWorldPosition()))
         {
         	return;
@@ -64,4 +58,5 @@
         {
         	this->forgetTarget();
+        	turret->setTarget(0);
         }
 
@@ -71,5 +66,5 @@
         {
         	Pawn* parenttarget = orxonox_cast<Pawn*>(parent->getTarget());
-        	if(parenttarget && this->isInRange(parenttarget->getWorldPosition()))
+        	if(parenttarget && turret->isInRange(parenttarget->getWorldPosition()))
         	{
         		this->setTarget(parenttarget);
@@ -82,8 +77,8 @@
         {
         	Pawn* entity = orxonox_cast<Pawn*>(*it);
-            if (ArtificialController::sameTeam(this->getControllableEntity(), entity, this->getGametype()))
+            if (this->FormationController::sameTeam(this->getControllableEntity(), entity, this->getGametype()))
             	continue;
 
-            if(this->isInRange(entity->getWorldPosition()))
+            if(turret->isInRange(entity->getWorldPosition()))
             {
             	this->setTarget(entity);
@@ -94,58 +89,4 @@
  	}
 
- 	bool TurretController::isInRange(const Vector3 &position)
-    {
-    	//Check distance
-        Vector3 distance = position - this->getControllableEntity()->getWorldPosition();
-        if(distance.squaredLength() > (this->attackRadius_ * this->attackRadius_))
-        {
-            return false;
-        }
-
-        //Check pitch
-        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;
-        }
-
-        //Check yaw
-        dirProjected = dir;
-        dirProjected.y = 0;
-        startDirProjected = this->startDir_;
-        startDirProjected.y = 0;
-        angle = startDirProjected.angleBetween(dirProjected).valueDegrees();
-        if(angle > this->maxYaw_)
-        {
-            return false;
-        }
-        return true;
-    }
-
-    void TurretController::aimAtPositionRot(const Vector3 &position)
-    {
-
-        Vector3 currDir = this->getControllableEntity()->getWorldOrientation() * WorldEntity::FRONT;
-        Vector3 targetDir = position - this->getControllableEntity()->getWorldPosition();
-
-        Quaternion rot = currDir.getRotationTo(targetDir);
-
-        //Don't make the rotation instantaneous
-        rot = Quaternion::Slerp(0.1, Quaternion::IDENTITY, rot);
-
-        this->getControllableEntity()->rotate(rot, WorldEntity::World);
-    }
-    
-
-    void TurretController::aimAtTargetRot()
-    {
-    	this->aimAtPositionRot(this->target_->getWorldPosition());
-    }
-
     bool TurretController::isLookingAtTargetNew(float angle) const
     {
@@ -155,50 +96,47 @@
  	void TurretController::tick(float dt)
  	{
-			if(!gotOrient_)
-	        {
-	            this->startOrient_ = this->getControllableEntity()->getOrientation();
-	            this->localXStart_ = this->startOrient_ * this->localX_;
-	            this->localXStart_.normalise();
-	            this->localX_ = this->localXStart_;
-	            this->localYStart_ = this->startOrient_ * this->localY_;
-	            this->localYStart_.normalise();
-	            this->localY_ = this->localYStart_;
-	            this->localZStart_ = this->startOrient_ * this->localZ_;
-	            this->localZStart_.normalise();
-	            this->localZ_ = this->localZStart_;
-
-	            //startDir should always be (0,0,-1)
-	            this->startDir_ = getTransformedVector(this->startOrient_ * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_);
-
-	            this->gotOrient_ = true;
-
-	        }
-
-	        WorldEntity* parent = this->getControllableEntity()->getParent();
-	        if(parent)
-	        {
-	            Quaternion parentrot = parent->getOrientation();
-	            this->localX_ = parentrot * this->localXStart_;
-	            this->localY_ = parentrot * this->localYStart_;
-	            this->localZ_ = parentrot * this->localZStart_;
-	        }
+	    if (!this->isActive() || !this->getControllableEntity())
+	        return;
 
 
+ 		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;
+        }
 
-	        if (!this->isActive() || !this->getControllableEntity())
-	            return;
-
-	 		this->searchTarget();
-	 		if(target_)
-	 		{
-	 			this->aimAtTarget();
-	 			//this->getControllableEntity()->lookAt(this->targetPosition_);
-	 			//It says move, but really it only turns
-	 			this->aimAtTargetRot();
-	 			if(this->isLookingAtTargetNew(Degree(5).valueRadians()))
-	 			{
-	 				orxout() << 42 << endl;
-	 			}
-	 		}
+ 		this->searchTarget();
+		if(target_)
+ 		{
+ 			Turret* turret = orxonox_cast<Turret*> (this->getControllableEntity());
+ 			this->aimAtTarget();
+ 			turret->aimAtPosition(target_->getWorldPosition());
+ 			if(this->isLookingAtTargetNew(Degree(5).valueRadians()))
+ 			{
+ 				this->getControllableEntity()->fire(0);
+ 				orxout() << 42 << endl;
+ 			}
+ 		}
  	}
  }
Index: code/branches/turretFS14/src/modules/objects/controllers/TurretController.h
===================================================================
--- code/branches/turretFS14/src/modules/objects/controllers/TurretController.h	(revision 10042)
+++ code/branches/turretFS14/src/modules/objects/controllers/TurretController.h	(revision 10044)
@@ -44,22 +44,9 @@
 
  		private:
-            bool gotOrient_;
-            float attackRadius_;
-            Ogre::Real maxPitch_;
-            Ogre::Real maxYaw_;
-            Quaternion startOrient_;
-            Vector3 startDir_;
-            Vector3 localZ_;
-            Vector3 localZStart_;
-            Vector3 localY_;
-            Vector3 localYStart_;
-            Vector3 localX_;
-            Vector3 localXStart_;
 
-            void aimAtPositionRot(const Vector3 &position);
-            void aimAtTargetRot();
  			void searchTarget();
- 			bool isInRange(const Vector3 &position);
  			bool isLookingAtTargetNew(float angle) const;
+
+ 			bool once_;
  	};
  }
