[Orxonox-commit 6253] r10910 - code/branches/campaignHS15/src/orxonox/controllers

gania at orxonox.net gania at orxonox.net
Tue Dec 1 16:33:46 CET 2015


Author: gania
Date: 2015-12-01 16:33:46 +0100 (Tue, 01 Dec 2015)
New Revision: 10910

Modified:
   code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.cc
   code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.h
Log:
didn'change any behaviour yet, but implemented some methods that I don't use yet

Modified: code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.cc
===================================================================
--- code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.cc	2015-12-01 09:03:10 UTC (rev 10909)
+++ code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.cc	2015-12-01 15:33:46 UTC (rev 10910)
@@ -142,6 +142,229 @@
             this->bManeuverCalled_ = false;
         SUPER(ActionpointController, tick, dt);
     }
+    std::pair <ControllableEntity*, ControllableEntity*> ActionpointController::closestTargets()
+    {
+        WeakPtr<ControllableEntity> firstTarget, secondTarget, tempTarget;
+        float firstDistance = std::numeric_limits<float>::infinity(), secondDistance = std::numeric_limits<float>::infinity(), tempDistance = 0;
+        Gametype* gt = this->getGametype();
+        for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
+        {
+            tempTarget = static_cast<ControllableEntity*>(*itP);
+            if (CommonController::sameTeam (this->getControllableEntity(), tempTarget, gt))
+                continue;
+
+            tempDistance = CommonController::distance (*itP, this->getControllableEntity());
+            if (tempDistance < firstDistance)
+            {
+                secondDistance = firstDistance;
+                secondTarget = firstTarget;
+
+                firstDistance = tempDistance;
+                firstTarget = tempTarget;
+            }
+            else if (tempDistance < secondDistance)
+            {
+                secondDistance = tempDistance;
+                secondTarget = tempTarget;
+            }
+        }
+        return std::make_pair (firstTarget, secondTarget);  
+    }
+
+    //patrol can only be called by divisionController or others if they don't have a leader
+    //searches for closest enemy and sets target to it. If wasn't fighting, pushes Action::FIGHT on a stack.
+    //patrol gets called constantly either if this is fighting or if this->bDefaultPatrol_ is set to true via XML
+    bool ActionpointController::patrol()
+    {
+        std::pair <ControllableEntity*, ControllableEntity*> newTargets = this->closestTargets();
+        if (!newTargets.first)
+            return false;
+        ControllableEntity* newTarget = newTargets.first;
+
+        float distance = CommonController::distance (this->getControllableEntity(), newTarget);
+        if (distance < this->attackRange_ && distance < CommonController::distance(this->getControllableEntity(), this->target_))
+        {
+            this->setTarget (newTarget);
+
+            if (this->getIdentifier()->getName() == "DivisionController")
+            {
+                if (!newTargets.second)
+                {
+                    if (this->myFollower_)
+                        this->myFollower_->setTarget(newTargets.first);
+                }
+                else
+                {
+                    if (this->myFollower_ && CommonController::distance(this->getControllableEntity(), newTargets.second) < this->attackRange_ + 600.0f)
+                        this->myFollower_->setTarget(newTargets.second);
+                    else if (this->myFollower_)
+                        this->myFollower_->setTarget(newTargets.first);
+                }
+            }
+            if (this->action_ != Action::FIGHT && this->action_ != Action::FIGHTALL)
+            {
+                this->bPatrolling_ = true;
+                if (this->bLoop_)
+                {
+                    Point p = { Action::FIGHT, "", Vector3::ZERO, true };
+                    this->loopActionpoints_.push_back(p);
+                    this->executeActionpoint();
+                }
+                else
+                {
+                    Point p = { Action::FIGHT, "", Vector3::ZERO, false };
+                    this->parsedActionpoints_.push_back(p);
+                    this->executeActionpoint();   
+                }                    
+            }
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+    //checks if state is still to be executed and makes a transition otherwise.
+    //if this->bDefaultPatrol_ == true, patrols area for enemies and, if not fighting, pushes a state with action = FIGHT
+    void ActionpointController::stateMachine()
+    {
+        //Check if calculations needed
+        if (!this->getControllableEntity() || !orxonox_cast<Pawn*> (this->getControllableEntity()) || !this->isActive())
+            return;
+
+        //state NONE means that either previous state finished executing and next state is to be fetched or no states are left to execute
+        //NONE is never on a stack -> it is only a transition state saved in a variable
+        if (this->action_ == Action::NONE || this->bTakenOver_)
+        {
+            //no actionpoints a.k.a. states left to execute
+            if (this->parsedActionpoints_.empty() && this->loopActionpoints_.empty())
+            {
+                //default action is fighting
+                if (this->bDefaultFightAll_)
+                {
+                    //make state
+                    Point p = { Action::FIGHTALL, "", Vector3::ZERO, false };
+                    //push it on the stack
+                    this->parsedActionpoints_.push_back (p);
+                }
+                //default action is nothing
+                else
+                {
+                    this->bActive_ = false;
+                    return;
+                }     
+            }
+            //switch to the new state
+            this->executeActionpoint(); 
+            this->bTakenOver_ = false; 
+        }
+        if (this->action_ == Action::FIGHTALL)
+        {
+            if (!this->hasTarget())
+            {
+                ControllableEntity* newTarget = this->closestTarget();    
+                if (newTarget)
+                {
+                    this->setTarget (newTarget);
+                }
+                else
+                {
+                    this->nextActionpoint();
+                    this->executeActionpoint();
+                }
+            }
+            else
+            {
+                bool b = this->patrol();
+            }
+
+        }
+        if (this->action_ == Action::FIGHT)
+        {
+            if (!this->hasTarget() )
+            {
+                if (!this->patrol())
+                {
+                    if (this->bPatrolling_)
+                    {
+                        if (this->bLoop_)
+                        {
+                            if (!this->loopActionpoints_.empty())
+                            {
+                                this->loopActionpoints_.pop_back();
+                            }
+                        }
+                        else
+                        {
+                            if (!this->parsedActionpoints_.empty())
+                            {
+                                this->parsedActionpoints_.pop_back();
+                            }            
+                        }
+                        this->setAction(Action::NONE);
+                        this->bHasTargetPosition_ = false;
+                        this->bPatrolling_ = false;
+                    }
+                    else
+                    {
+                        this->nextActionpoint();
+                    }
+                    this->executeActionpoint();
+                }
+            }
+            else if (this->hasTarget())
+            {
+                if (CommonController::distance (this->getControllableEntity(), this->target_) > this->attackRange_)
+                {
+                    this->setTarget(0);
+                    this->stateMachine();
+                }
+            }
+        }
+        if (this->action_ == Action::FLY)
+        {
+            if (this->squaredDistanceToTarget() <= this->squaredaccuracy_)
+            {
+                this->nextActionpoint();   
+                this->executeActionpoint();
+            }
+            else if (this->bDefaultPatrol_)
+            {
+                bool b = this->patrol();
+            }
+        }
+        if (this->action_ == Action::PROTECT)
+        {
+            if (!this->getProtect())
+            {
+                this->nextActionpoint();
+                this->executeActionpoint(); 
+            }
+            else
+            {
+                this->stayNearProtect();
+            }
+            if (this->bDefaultPatrol_)
+            {
+                bool b = this->patrol();
+            }
+        }
+        if (this->action_ == Action::ATTACK)
+        {   
+            if (!this->hasTarget())
+            {
+                this->nextActionpoint();
+                this->executeActionpoint();
+            }
+            if (this->bDefaultPatrol_)
+            {
+                bool b = this->patrol();
+            }
+        }
+    }
+
+
+
     void ActionpointController::action()
     {
         if (!this || !this->getControllableEntity())

Modified: code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.h
===================================================================
--- code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.h	2015-12-01 09:03:10 UTC (rev 10909)
+++ code/branches/campaignHS15/src/orxonox/controllers/ActionpointController.h	2015-12-01 15:33:46 UTC (rev 10910)
@@ -179,6 +179,9 @@
                 { return false; }
             virtual bool hasFollower()
                 { return true; }
+            void stateMachine();
+            bool patrol();
+            std::pair <ControllableEntity*, ControllableEntity*> closestTargets();
 
         protected:
                 void startAttackingEnemiesThatAreClose();
@@ -238,6 +241,10 @@
                 float timeOffset_;
                 bool bActionCalled_;
                 bool bManeuverCalled_;
+                bool bDefaultFightAll_;
+                bool bActive_;
+                bool bPatrolling_;
+                bool bDefaultPatrol_;
 
         private:
             




More information about the Orxonox-commit mailing list