Index: code/branches/campaignHS15/src/orxonox/controllers/CommonController.cc
===================================================================
--- code/branches/campaignHS15/src/orxonox/controllers/CommonController.cc	(revision 10851)
+++ code/branches/campaignHS15/src/orxonox/controllers/CommonController.cc	(revision 10854)
@@ -65,5 +65,5 @@
         this->squaredaccuracy_ = 10000;
         this->bFirstTick_ = true;
-        this->tolerance_ = 65;
+        this->tolerance_ = 50;
         this->action_ = Action::NONE;
         this->stopLookingAtTarget();
@@ -73,11 +73,5 @@
     CommonController::~CommonController() 
     {
-        for (size_t i = 0; i < this->actionpoints_.size(); ++i)
-        {
-            if(this->actionpoints_[i])
-                this->actionpoints_[i]->destroy();
-        }
-        this->parsedActionpoints_.clear();
-        this->actionpoints_.clear();
+       
     }
     void CommonController::tick(float dt)
@@ -492,5 +486,5 @@
                 }
             }
-            if (distance > 200 || (rotateX > -0.1 && rotateX < 0.1 && rotateY > -0.1 && rotateY < 0.1))
+            if (distance > this->tolerance_*1.5f || (rotateX > -0.01 && rotateX < 0.01 && rotateY > -0.01 && rotateY < 0.01))
                 this->getControllableEntity() ->moveFrontBack( SPEED * dt );
         }
@@ -746,5 +740,5 @@
         return 0;  
     }
-    bool CommonController::startAttackingEnemiesThatAreClose()
+    void CommonController::startAttackingEnemiesThatAreClose()
     {
         if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL)
@@ -761,9 +755,7 @@
                     this->parsedActionpoints_.push_back(p);
                     this->executeActionpoint();
-                    return true;
-                }
-            }
-        }
-        return false;
+                }
+            }
+        }
     }
 
@@ -783,5 +775,17 @@
             {
                 case Action::FIGHT:
-                {                
+                {
+                    std::string targetName = this->parsedActionpoints_.back().name;
+                    if (targetName == "")
+                    {
+                        break;
+                    }
+                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
+                    {
+                        if (CommonController::getName(*itP) == targetName)
+                        {
+                            this->setTarget (static_cast<ControllableEntity*>(*itP));
+                        }
+                    }          
                     break;
                 }
@@ -851,4 +855,12 @@
         }
     }
+    void CommonController::stayNearProtect()
+    {
+        Vector3* targetRelativePosition;
+        targetRelativePosition = new Vector3 (0, 300, 300);  
+        Vector3 targetAbsolutePosition = ((this->getProtect()->getWorldPosition()) + 
+            (this->getProtect()->getWorldOrientation()* (*targetRelativePosition)));
+        this->setTargetPosition(targetAbsolutePosition);
+    }
     void CommonController::nextActionpoint()
     {
@@ -859,3 +871,106 @@
         this->setAction(Action::NONE);
     }
+    void CommonController::action()
+    {
+        this->startAttackingEnemiesThatAreClose();
+        //No action -> pop one from stack
+        if (this->action_ == Action::NONE)
+        {
+            this->executeActionpoint();
+        }
+        //Action fightall -> fight till nobody alive
+        if (this->action_ == Action::FIGHTALL)
+        {
+            if (!this->hasTarget())
+            {
+                //----find a target----
+                ControllableEntity* newTarget = this->closestTarget();    
+                if (newTarget)
+                {
+                    this->setAction (Action::FIGHTALL, newTarget);
+                }
+                else
+                {
+                    this->nextActionpoint();
+                    return;
+                }
+            }
+            else if (this->hasTarget())
+            {
+
+            }
+        }
+        //Action fight -> fight as long as enemies in range
+        else if (this->action_ == Action::FIGHT)
+        {
+            if (!this->hasTarget())
+            {
+                //----find a target----
+                ControllableEntity* newTarget = this->closestTarget();    
+                if (newTarget && 
+                        CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_)
+                {
+                    this->setAction (Action::FIGHT, newTarget);
+                }
+                else
+                {
+                    this->nextActionpoint();
+                    return;
+                }
+            }
+            else if (this->hasTarget())
+            {
+                //----fly in formation if far enough----
+                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
+                    
+                if (diffVector.length() > this->attackRange_)
+                {
+                    ControllableEntity* newTarget = this->closestTarget();
+                    
+                    if (newTarget && 
+                        CommonController::distance (this->getControllableEntity(), newTarget) < this->attackRange_)
+                    {
+                        this->setAction (Action::FIGHT, newTarget);
+                    }
+                    else
+                    {
+                        this->nextActionpoint();
+                        return;
+                    }
+                }
+            }
+        }
+        else if (this->action_ == Action::FLY)
+        {
+            if (this->squaredDistanceToTarget() <= this->squaredaccuracy_)
+            {
+                this->nextActionpoint();   
+                return;
+            }
+        }
+        else if (this->action_ == Action::PROTECT)
+        {
+            if (!this->getProtect())
+            {
+                this->nextActionpoint(); 
+                return;
+            }
+            this->stayNearProtect();
+        }
+        else if (this->action_ == Action::ATTACK)
+        {   
+            if (!this->hasTarget())
+            {
+                this->nextActionpoint();
+                return;
+            }
+        }
+        if (this->hasTarget())
+        {
+            //----choose where to go----
+            this->maneuver();
+            //----fire if you can----
+            this->bShooting_ = this->canFire();                
+        }
+    }
 }
