Index: code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.cc
===================================================================
--- code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.cc	(revision 10915)
+++ code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.cc	(revision 10923)
@@ -40,7 +40,7 @@
     ActionpointController::ActionpointController(Context* context) : FightingController(context)
     {
+        this->ticks_ = 0;
         this->bPatrolling_ = false;
-
-        this->actionCounter_ = 0;
+        this->maneuverCounter_ = 0;
         this->bInLoop_ = false;
         this->bLoop_ = false;
@@ -49,7 +49,5 @@
         parsedActionpoints_.clear();
         actionpoints_.clear();
-        healthSpawners_.clear();
-        damageSpawners_.clear();
-        speedSpawners_.clear();
+
         this->bTakenOver_ = false;
         this->action_ = Action::NONE;
@@ -96,30 +94,64 @@
         parsedActionpoints_.clear();
         actionpoints_.clear();
-        healthSpawners_.clear();
-        damageSpawners_.clear();
-        speedSpawners_.clear();
+        ActionpointController::nextActionpointControllerId_--;
+        if (ActionpointController::nextActionpointControllerId_ < 0)
+            ActionpointController::nextActionpointControllerId_ = 0;
     }
     void ActionpointController::tick(float dt)
     {
-        if (!this->getControllableEntity())
-            return;
+        if (!this || !this->getControllableEntity())
+            return;
+        ++this->ticks_;
+        // orxout (internal_error) << "this id = " << this->actionpointControllerId_ << endl;
+        if (ActionpointController::sTicks_ < this->ticks_)
+        {
+            // orxout (internal_error) << "total id's = " << ActionpointController::nextActionpointControllerId_ << endl;
+            ++ActionpointController::sTicks_;
+        }
+
+        if (this->ticks_ == 1)
+        {
+
+            this->actionpointControllerId_ = ActionpointController::nextActionpointControllerId_++;
+            std::reverse(parsedActionpoints_.begin(), parsedActionpoints_.end());
+            std::reverse(actionpoints_.begin(), actionpoints_.end());
+            if (this->parsedActionpoints_.empty())
+            {
+                this->action_ = Action::FIGHTALL;
+            }
+        }
+        if (this->ticks_ == 1)
+        {    
+            this->timeOffset_ = 0.0f;
+            this->bActionCalled_ = false;
+            
+            this->bFirstTick_ = false;
+        }
+/*        if (ActionpointController::sTicks_ % (ActionpointController::nextActionpointControllerId_+1) != this->actionpointControllerId_)
+        {
+            return;
+        }*/
+        if (!this || !this->getControllableEntity())
+            return;
+        
+        if (this->bHasTargetPosition_)
+        {
+            this->moveToTargetPosition(dt);
+        }
+        else if (this->bLookAtTarget_)
+        {
+            this->lookAtTarget(dt);
+        }
+        if (this->bShooting_)
+        {
+            this->doFire();
+        }
+
+        if (!this || !this->getControllableEntity())
+            return;
+        
         if (stop_)
             return;
-        if (this->timeout_ > 0 && this->bFiredRocket_)
-        {
-            this->timeout_ -= dt;
-        }
-        if (this->bStartedDodging_)
-        {
-            this->timeDodged_ += dt;
-            if (this->timeDodged_ > 4.0f)
-            {
-                this->bStartedDodging_ = false;
-                this->timeDodged_ = 0;
-            }
-        }
-        maneuverCounter_ += dt;
-        if (maneuverCounter_ > 6.0f)
-            maneuverCounter_ = 0;
+        
         this->timeOffset_ += dt;
         if (this->timeOffset_ >= 2.4f)
@@ -127,57 +159,50 @@
         if (timeout_ <= 0)
             this->bFiredRocket_ = false;
-
-        if (this->bHasTargetPosition_)
-        {
-            this->moveToTargetPosition(dt);
-        }
-        else if (this->bLookAtTarget_)
-        {
-            this->lookAtTarget(dt);
-        }
-
-        if (!this->getControllableEntity())
-            return;
-        if (this->bShooting_)
-        {
-            this->doFire();
-        }
-        if (this->bFirstTick_)
-        {
-            std::reverse(parsedActionpoints_.begin(), parsedActionpoints_.end());
-            std::reverse(actionpoints_.begin(), actionpoints_.end());
-            if (this->parsedActionpoints_.empty())
-            {
-                this->action_ = Action::FIGHTALL;
-            }
-        }
-        if (this->bFirstTick_)
-        {    
-            this->timeOffset_ = 0.0f;
-            this->bActionCalled_ = false;
-            setSpawners();
-            // orxout(internal_error) << "health spawners size = " << this->healthSpawners_.size() <<
-            // ", damage spawners size = " << this->damageSpawners_.size() <<
-            // ", speed spawners size = " << this->speedSpawners_.size() << endl;        
-            this->bFirstTick_ = false;
-        }
-        if (!this->getControllableEntity())
-            return;
+        if (!this || !this->getControllableEntity())
+            return;
+
+        if (!this || !this->getControllableEntity())
+            return;
+        if (this->timeout_ > 0 && this->bFiredRocket_)
+            {
+                this->timeout_ -= dt;
+            }
+            if (this->bStartedDodging_)
+            {
+                this->timeDodged_ += dt;
+            }
+            maneuverCounter_ += dt;
+
         //maneuver every 0.25 sec -> 
-        if (this->hasTarget() && 
-            ((this->timeOffset_ >= 0.4f && this->timeOffset_ < 0.8f) || 
-             (this->timeOffset_ >= 1.2f && this->timeOffset_ < 1.6f) ||
-             (this->timeOffset_ >= 1.8f && this->timeOffset_ < 2.0f) ||
-             (this->timeOffset_ >= 2.2f && this->timeOffset_ < 2.4f)) && !this->bManeuverCalled_)
-        {
-            this->bManeuverCalled_ = true;
+        int step =  5;
+        if (this->hasTarget() && ActionpointController::sTicks_ % 100 == step * (this->actionpointControllerId_) + 3)
+        {
+            if (maneuverCounter_ > 6.0f)
+                maneuverCounter_ = 0;
+            if (this->bStartedDodging_ &&  this->timeDodged_ > 4.0f)
+            {
+                this->bStartedDodging_ = false;
+                this->timeDodged_ = 0;
+            }
             this->maneuver();
+        }
+        if (ActionpointController::sTicks_ % 100 == step * this->actionpointControllerId_ + 1)
+        {
+            this->action();
+        }
+        if (!this || !this->getControllableEntity())
+            return;
+
+        if (!this || !this->getControllableEntity())
+            return;
+        if (this->hasTarget() &&  ActionpointController::sTicks_ % 100 == step * (this->actionpointControllerId_))
+        {
             this->bShooting_ = this->canFire();
-        }
-        if ((this->timeOffset_ >= 0.0f && this->timeOffset_ < 0.4f) || 
-             (this->timeOffset_ >= 0.8f && this->timeOffset_ < 1.2f) ||
-             (this->timeOffset_ >= 1.6f && this->timeOffset_ < 1.8f) ||
-             (this->timeOffset_ >= 2.0f && this->timeOffset_ < 2.2f))
-            this->bManeuverCalled_ = false;
+            
+        }
+        if (!this || !this->getControllableEntity())
+            return;
+        
+
         SUPER(ActionpointController, tick, dt);
     }
@@ -195,6 +220,11 @@
             this->startAttackingEnemiesThatAreClose();
         }
+        if (!this || !this->getControllableEntity())
+            return;
+
         this->deltaHp = orxonox_cast<Pawn*> (this->getControllableEntity())->getHealth() - this->previousHp;
         this->previousHp = orxonox_cast<Pawn*> (this->getControllableEntity())->getHealth();
+        if (!this || !this->getControllableEntity())
+            return;
 
         // if (this->actionCounter_ % 2 == 0)
@@ -202,11 +232,4 @@
         if (this->action_ == Action::NONE || this->bTakenOver_)
         {
-            // Point point = closestPickup(PickupType::HEALTH);
-            // if (point.action != Action::NONE)
-            // {
-            //     orxout(internal_error) << "found health" << endl;
-            //     this->parsedActionpoints_.push_back(point);
-            // }
-
             if (this->parsedActionpoints_.empty() && this->loopActionpoints_.empty() && this->bDefaultFightAll_)
             {
@@ -218,4 +241,7 @@
             // this->action();
         }
+        if (!this || !this->getControllableEntity())
+            return;
+
         //Action fightall -> fight till nobody alive
         if (this->action_ == Action::FIGHTALL)
@@ -307,18 +333,4 @@
         }
 
-            // Vector3 healthPosition = bestHealthPickup((this->target_->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length());
-            // if ((this->getControllableEntity()->getWorldPosition() - healthPosition).length() < this->tolerance_)
-            // {
-            //     //----choose where to go----
-            //     this->maneuver();
-            // }
-            // else
-            // {
-            //     this->dodgeTowards(healthPosition);
-            // }
-            // //----fire if you can----
-            // this->bShooting_ = this->canFire();  
-         
-        this->actionCounter_ += this->actionCounter_ < 100000 ? 1 : -this->actionCounter_ ;
 
     }
@@ -402,15 +414,15 @@
         {
             case Action::FIGHT:
-            { return "FIGHT"; break; }
+            { return "FIGHT"; }
             case Action::FLY:
-            { return "FLY"; break; }
+            { return "FLY"; }
             case Action::PROTECT:
-            { return "PROTECT"; break; }
+            { return "PROTECT"; }
             case Action::NONE:
-            { return "NONE"; break; }
+            { return "NONE"; }
             case Action::FIGHTALL:
-            { return "FIGHTALL"; break; }
+            { return "FIGHTALL"; }
             case Action::ATTACK:
-            { return "ATTACK"; break; }
+            { return "ATTACK"; }
             default:
                 return "NONE";
@@ -462,4 +474,7 @@
     void ActionpointController::executeActionpoint()
     {
+        if (!this || !this->getControllableEntity())
+            return;
+
         Point p;
         if (this->bLoop_ && !loopActionpoints_.empty())
@@ -478,4 +493,7 @@
         else
         {
+            if (!this || !this->getControllableEntity())
+                return;
+
             this->setTarget(0);
             this->setTargetPosition(this->getControllableEntity()->getWorldPosition());
@@ -520,5 +538,7 @@
             case Action::PROTECT:
             {
-                
+                if (!this || !this->getControllableEntity())
+                    return;
+
                 std::string protectName = p.name;
                 if (protectName == "reservedKeyword:human")
@@ -583,4 +603,7 @@
     void ActionpointController::stayNearProtect()
     {
+        if (!this || !this->getControllableEntity())
+            return;
+
         Vector3 targetRelativePosition(0, 300, 300);
         if (!this->getProtect())
@@ -665,5 +688,5 @@
     Pawn* ActionpointController::closestTarget()
     {
-        if (!this->getControllableEntity())
+        if (!this || !this->getControllableEntity())
             return 0;
 
@@ -691,4 +714,7 @@
     void ActionpointController::startAttackingEnemiesThatAreClose()
     {
+        if (!this || !this->getControllableEntity())
+            return;
+
         //if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL)
         {
@@ -700,4 +726,6 @@
                         <= this->attackRange_ )
                 {
+                    if (!this || !this->getControllableEntity())
+                        return;
                     this->setTarget(newTarget);
                     if (this->bLoop_ && !this->bPatrolling_)
@@ -718,124 +746,5 @@
         }
     }
-    Vector3 ActionpointController::bestHealthPickup(float searchRadius)
-    {
-
-        //1000000/distance^2 * 1/index+1
-        float maxUse = 0;
-        float tempUse = -1;
-        int index = 0;
-        float distance = 0;
-        Vector3 bestPosition = this->getControllableEntity()->getWorldPosition();
-
-        for (std::multimap<int, std::pair<PickupSpawner*, bool> >::iterator it=healthSpawners_.begin(); it!=healthSpawners_.end(); ++it)
-        {
-            distance = (this->getControllableEntity()->getWorldPosition() - (*it).second.first->getWorldPosition()).length();
-            if (distance < this->tolerance_ || !(*it).second.second)
-            {
-                (*it).second.second = false;
-                continue;
-            }
-            if (distance > searchRadius)
-                continue;
-            index = (*it).first;
-            tempUse = 1000000*1/(1+distance*distance) * 1/((index/2.0f)+1);
-            if (tempUse > maxUse)
-            {
-                bestPosition = (*it).second.first->getWorldPosition();
-                maxUse = tempUse;
-            }
-        }
-        //std::multimap<int, std::pair<PickupSpawner*, bool> >::iterator it = this->healthSpawners_.find(index);
-        //P.S. check if it == ... .end()
-        //orxout (internal_error) << "best position = " << bestPosition << endl;
-        return  bestPosition;
-    }
-    void ActionpointController::setSpawners()
-    {
-        std::vector<std::string> damagePickups;
-        std::vector<std::string> healthPickups;
-        std::vector<std::string> speedPickups;       
-        int index = 0;
-
-
-        damagePickups.push_back("largedamageboostpickup");
-        damagePickups.push_back("dronepickup");            
-        damagePickups.push_back("mediumdamageboostpickup");
-        damagePickups.push_back("smalldamageboostpickup");
-
-        healthPickups.push_back("triplehealthspeedinvisibilitypickup");            
-        healthPickups.push_back("crazyhealthpickup");
-        healthPickups.push_back("hugehealthpickup");
-        healthPickups.push_back("hugeshieldpickup");
-        healthPickups.push_back("hugeinvisiblepickup");
-        healthPickups.push_back("hugeshrinkpickup");
-        healthPickups.push_back("mediumhealthpickup");
-        healthPickups.push_back("mediumshieldpickup");
-        healthPickups.push_back("mediuminvisiblepickup");
-        healthPickups.push_back("mediumshrinkpickup");  
-        healthPickups.push_back("smallhealthpickup");                   
-        healthPickups.push_back("smallshieldpickup");
-        healthPickups.push_back("smallinvisiblepickup");
-        healthPickups.push_back("smallshrinkpickup");
-
-        speedPickups.push_back("triplehealthspeedinvisibilitypickup");
-        speedPickups.push_back("hugespeedpickup");
-        speedPickups.push_back("smalljumppickup");
-        speedPickups.push_back("mediumspeedpickup");
-        speedPickups.push_back("smallspeedpickup");
-        
-
-
-        for (ObjectList<WorldEntity>::iterator it = ObjectList<WorldEntity>::begin(); it; ++it)
-        {
-            if ((*it)->getIdentifier()->getName() != "PickupSpawner")
-                continue;
-            PickupSpawner* spawner = static_cast<PickupSpawner*>(*it);
-            std::string name = spawner->getPickupTemplateName();
-            //float distance = ((*it)->getWorldPosition() - this->getControllableEntity()->getWorldPosition()).length(); 
-            // if (distance < 50.0f)
-            //     continue;           
-            for (unsigned int i = 0; i < healthPickups.size(); ++i)
-            {
-                if (name == healthPickups.at(i))
-                {
-                    index = i;
-                    this->healthSpawners_.insert(std::make_pair(index, std::make_pair(spawner, true)));
-                    break;
-                }
-            }
-            for (unsigned int i = 0; i < damagePickups.size(); ++i)
-            {
-                if (name == damagePickups.at(i))
-                {
-                    //tempUse = 1000000*1/(1+distance*distance) * 1/((i/2.0f)+1);
-                    index = i;
-                    this->damageSpawners_.insert(std::make_pair(index, std::make_pair(spawner, true)));
-                    break;
-                }
-            }
-            for (unsigned int i = 0; i < speedPickups.size(); ++i)
-            {
-                if (name == speedPickups.at(i))
-                {
-                    //tempUse = 1000000*1/(1+distance*distance) * 1/((i/2.0f)+1);
-                    index = i;
-                    this->speedSpawners_.insert(std::make_pair(index, std::make_pair(spawner, true)));
-                    break;
-                }
-            }
-            // if (tempUse > maxUse)
-            // {
-            //     Point p = { Action::FLY, "", (*it)->getWorldPosition(), false };
-            //     maxUse = tempUse;
-            //     maxPoint = p;
-            // }
-            // else
-            // {
-            //     tempUse = -1;
-            //     continue;
-            // }
-            // orxout(internal_error) << "resp time  = " << static_cast<PickupSpawner*>(*it)->getRespawnTime() << endl;
-        }  
-    }
-}
+    int ActionpointController::nextActionpointControllerId_ = 0;
+    int ActionpointController::sTicks_ = 0;
+}   
