[Orxonox-commit 4319] r8990 - code/branches/formation/src/orxonox/controllers

jo at orxonox.net jo at orxonox.net
Fri Dec 16 16:25:28 CET 2011


Author: jo
Date: 2011-12-16 16:25:28 +0100 (Fri, 16 Dec 2011)
New Revision: 8990

Modified:
   code/branches/formation/src/orxonox/controllers/AIController.cc
   code/branches/formation/src/orxonox/controllers/AIController.h
   code/branches/formation/src/orxonox/controllers/ArtificialController.cc
   code/branches/formation/src/orxonox/controllers/FormationController.cc
   code/branches/formation/src/orxonox/controllers/FormationController.h
   code/branches/formation/src/orxonox/controllers/HumanController.cc
   code/branches/formation/src/orxonox/controllers/NewHumanController.cc
Log:
Rough adding of both concepts. Further work on the states is needed. (single player bots are inactive at the moment)

Modified: code/branches/formation/src/orxonox/controllers/AIController.cc
===================================================================
--- code/branches/formation/src/orxonox/controllers/AIController.cc	2011-12-14 18:13:09 UTC (rev 8989)
+++ code/branches/formation/src/orxonox/controllers/AIController.cc	2011-12-16 15:25:28 UTC (rev 8990)
@@ -64,8 +64,8 @@
                 // return to Master after being forced free
                 if (this->freedomCount_ == 1)
                 {
-                this->state_ = SLAVE;
-                this->freedomCount_ = 0;
+                    this->state_ = SLAVE;
+                    this->freedomCount_ = 0;
                 }
 
                 random = rnd(maxrand);
@@ -73,49 +73,11 @@
                     this->searchNewMaster();
             }
 
-            // search enemy
-            random = rnd(maxrand);
-            if (random < 15 && (!this->target_))
-                this->searchNewTarget();
+            this->defaultBehaviour(maxrand);
 
-            // forget enemy
-            random = rnd(maxrand);
-            if (random < 5 && (this->target_))
-                this->forgetTarget();
-
-            // next enemy
-            random = rnd(maxrand);
-            if (random < 10 && (this->target_))
-                this->searchNewTarget();
-
-            // fly somewhere
-            random = rnd(maxrand);
-            if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
-                this->searchRandomTargetPosition();
-
-            // stop flying
-            random = rnd(maxrand);
-            if (random < 10 && (this->bHasTargetPosition_ && !this->target_))
-                this->bHasTargetPosition_ = false;
-
-            // fly somewhere else
-            random = rnd(maxrand);
-            if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
-                this->searchRandomTargetPosition();
-
-            // shoot
-            random = rnd(maxrand);
-            if (!(this->passive_) && random < 75 && (this->target_ && !this->bShooting_))
-                this->bShooting_ = true;
-
-            // stop shooting
-            random = rnd(maxrand);
-            if (random < 25 && (this->bShooting_))
-                this->bShooting_ = false;
-
         }
 
-        if (this->state_ == SLAVE && this->mode_==ATTACK)
+        if (this->state_ == SLAVE && this->mode_ == ATTACK) //TODO: add botlevel parameter
         {
             // search enemy
             random = rnd(maxrand);
@@ -141,8 +103,6 @@
 
         if (this->state_ == MASTER)
         {
-
-
             this->commandSlaves();
 
             if  (this->specificMasterAction_ != NONE)
@@ -175,59 +135,85 @@
                 if(this->slaves_.size() < 3 && random < 20)
                     this->searchNewMaster();
 
-                // search enemy
-                random = rnd(maxrand);
-                if (random < 15 && (!this->target_))
-                    this->searchNewTarget();
+                this->defaultBehaviour(maxrand);
 
-                // forget enemy
-                random = rnd(maxrand);
-                if (random < 5 && (this->target_))
-                    this->forgetTarget();
+            }
+        }
 
-                // next enemy
-                random = rnd(maxrand);
-                if (random < 10 && (this->target_))
-                    this->searchNewTarget();
+    }
 
-                // fly somewhere
-                random = rnd(maxrand);
-                if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
-                    this->searchRandomTargetPosition();
+    void AIController::tick(float dt)
+    {
+        if (!this->isActive())
+            return;
 
+        float random;
+        float maxrand = 100.0f / ACTION_INTERVAL;
+        ControllableEntity* controllable = this->getControllableEntity();
 
-                // fly somewhere else
-                random = rnd(maxrand);
-                if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
-                    this->searchRandomTargetPosition();
-
-                // shoot
-                random = rnd(maxrand);
-                if (!(this->passive_) && random < 9 && (this->target_ && !this->bShooting_))
+        if (controllable && this->mode_ == NORMAL)// bot is ready to move to a target // mode was DEFAULT in original implementation!
+        {
+            if (this->waypoints_.size() > 0 ) //Waypoint functionality.
+            {
+                WorldEntity* wPoint = this->waypoints_[this->waypoints_.size()-1];
+                if(wPoint)
                 {
-                this->bShooting_ = true;
-                this->forceFreeSlaves();
+                    this->moveToPosition(wPoint->getWorldPosition()); //BUG ?? sometime wPoint->getWorldPosition() causes crash
+                    if (wPoint->getWorldPosition().squaredDistance(controllable->getPosition()) <= this->squaredaccuracy_)
+                        this->waypoints_.pop_back(); // if goal is reached, remove it from the list
                 }
+                else
+                    this->waypoints_.pop_back(); // remove invalid waypoints
 
-                // stop shooting
+            }
+            else if(this->defaultWaypoint_ && ((this->defaultWaypoint_->getPosition()-controllable->getPosition()).length()  > 200.0f))
+            {
+                this->moveToPosition(this->defaultWaypoint_->getPosition()); // stay within a certain range of the defaultWaypoint_
                 random = rnd(maxrand);
-                if (random < 25 && (this->bShooting_))
-                    this->bShooting_ = false;
-
             }
         }
 
-    }
+        if (this->mode_ != ROCKET)
+        {
+            if (this->state_ == MASTER)
+            {
+                if (this->specificMasterAction_ ==  NONE)
+                {
+                    if (this->target_)
+                    {
+                        if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
+                            this->forgetTarget();
+                        else
+                        {
+                            this->aimAtTarget();
+                            random = rnd(maxrand);
+                            if(this->botlevel_*70 > random && !this->isCloseAtTarget(100))
+                                this->follow();  //If a bot is shooting a player, it shouldn't let him go away easily.
+                        }
+                    }
 
-    void AIController::tick(float dt)
-    {
-        if (!this->isActive())
-            return;
+                    if (this->bHasTargetPosition_)
+                        this->moveToTargetPosition();
+                    this->doFire();
+                }
 
-        if (this->state_ == MASTER)
-        {
-            if (this->specificMasterAction_ ==  NONE)
+                if (this->specificMasterAction_  == TURN180)
+                    this->turn180();
+
+                if (this->specificMasterAction_ == SPIN)
+                    this->spin();
+                if (this->specificMasterAction_ == FOLLOW)
+                    this->follow();
+            }
+
+            if (this->state_ == SLAVE && this->mode_!=ATTACK)
             {
+                if (this->bHasTargetPosition_)
+                    this->moveToTargetPosition();
+            }
+
+            if (this->state_ == FREE || (this->state_==SLAVE && this->mode_==ATTACK) )
+            {
                 if (this->target_)
                 {
                     if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
@@ -238,44 +224,100 @@
                 if (this->bHasTargetPosition_)
                     this->moveToTargetPosition();
 
-                if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(math::pi / 20.0f))
-                    this->getControllableEntity()->fire(0);
+                    this->doFire();
             }
+        }
+        else if (this->mode_ == ROCKET)//Rockets do not belong to a group of bots -> bot states are not relevant.
+        {   //Vector-implementation: mode_.back() == ROCKET;
+            if(controllable)
+            {//Check wether the bot is controlling the rocket and if the timeout is over.
+                if(controllable->getIdentifier() == ClassByString("Rocket"))
 
-            if (this->specificMasterAction_  == TURN180)
-                    this->turn180();
-
-            if (this->specificMasterAction_ == SPIN)
-                    this->spin();
-            if (this->specificMasterAction_ == FOLLOW)
+                {
                     this->follow();
-        }
+                    this->timeout_ -= dt;
+                    if((timeout_< 0)||(!target_))//Check if the timeout is over or target died.
+                    {
+                       controllable->fire(0);//kill the rocket
+                       this->setPreviousMode();//get out of rocket mode
+                    }
+                }
+                else
+                    this->setPreviousMode();//no rocket entity -> get out of rocket mode
+            }
+            else
+                this->setPreviousMode();//If bot dies -> getControllableEntity == NULL -> get out of ROCKET mode
+        }//END_OF ROCKET MODE
 
-        if (this->state_ == SLAVE && this->mode_!=ATTACK)
-        {
+        SUPER(AIController, tick, dt);
+    }
+//**********************************************NEW
+    void AIController::defaultBehaviour(float maxrand)
+    {       float random;
+            // search enemy
+            random = rnd(maxrand);
+            if (random < (botlevel_* 100) && (!this->target_))
+                this->searchNewTarget();
 
-            if (this->bHasTargetPosition_)
-                this->moveToTargetPosition();
+            // forget enemy
+            random = rnd(maxrand);
+            if (random < ((1-botlevel_)*20) && (this->target_))
+                this->forgetTarget();
 
-        }
+            // next enemy
+            random = rnd(maxrand);
+            if (random < (botlevel_*30) && (this->target_))
+                this->searchNewTarget();
 
-         if (this->state_ == FREE || (this->state_==SLAVE && this->mode_==ATTACK) )
-        {
-            if (this->target_)
+            // fly somewhere
+            random = rnd(maxrand);
+            if (random < 50 && (!this->bHasTargetPosition_ && !this->target_))
+                this->searchRandomTargetPosition();
+
+            // stop flying
+            random = rnd(maxrand);
+            if (random < 10 && (this->bHasTargetPosition_ && !this->target_))
+                this->bHasTargetPosition_ = false;
+
+            // fly somewhere else
+            random = rnd(maxrand);
+            if (random < 30 && (this->bHasTargetPosition_ && !this->target_))
+                this->searchRandomTargetPosition();
+
+            if (this->state_ == MASTER) // master: shoot
             {
-                if (!this->target_->getRadarVisibility()) /* So AI won't shoot invisible Spaceships */
-                    this->forgetTarget();
-                else this->aimAtTarget();
+                random = rnd(maxrand);
+                if (!(this->passive_) && random < (100*botlevel_) && (this->target_ && !this->bShooting_))
+                {
+                    this->bShooting_ = true;
+                    this->forceFreeSlaves();
+                }
             }
+            else
+            {
+                // shoot
+                random = rnd(maxrand);
+                if (!(this->passive_) && random < (botlevel_*100) && (this->target_ && !this->bShooting_))
+                    this->bShooting_ = true;
+            }
 
-            if (this->bHasTargetPosition_)
-                this->moveToTargetPosition();
+            // stop shooting
+            random = rnd(maxrand);
+            if (random < ((1 - botlevel_)*50) && (this->bShooting_))
+                this->bShooting_ = false;
 
-            if (this->getControllableEntity() && this->bShooting_ && this->isCloseAtTarget(1000) && this->isLookingAtTarget(math::pi / 20.0f))
-                this->getControllableEntity()->fire(0);
-        }
+            // boost
+            random = rnd(maxrand);
+            if (random < botlevel_*50 )
+                this->boostControl();
 
-        SUPER(AIController, tick, dt);
+            // update Checkpoints
+            /*random = rnd(maxrand);
+            if (this->defaultWaypoint_ && random > (maxrand-10))
+                this->manageWaypoints();
+            else //if(random > maxrand-10) //CHECK USABILITY!!*/
+            if (this->waypoints_.size() == 0 )
+                this->manageWaypoints();
     }
 
 }

Modified: code/branches/formation/src/orxonox/controllers/AIController.h
===================================================================
--- code/branches/formation/src/orxonox/controllers/AIController.h	2011-12-14 18:13:09 UTC (rev 8989)
+++ code/branches/formation/src/orxonox/controllers/AIController.h	2011-12-16 15:25:28 UTC (rev 8990)
@@ -43,15 +43,16 @@
             AIController(BaseObject* creator);
             virtual ~AIController();
 
-            virtual void tick(float dt);
+            virtual void tick(float dt); //<! Carrying out the targets set in action().
 
         protected:
-            virtual void action();
+            virtual void action(); //<! action() is called in regular intervals managing the bot's behaviour ~ setting targets.
+            void defaultBehaviour(float maxrand); //<! Helper function for code reusage. Some concrete commands for a bot.
 
         private:
             static const float ACTION_INTERVAL;
 
-            Timer actionTimer_;
+            Timer actionTimer_; //<! Regularly calls action().
     };
 }
 

Modified: code/branches/formation/src/orxonox/controllers/ArtificialController.cc
===================================================================
--- code/branches/formation/src/orxonox/controllers/ArtificialController.cc	2011-12-14 18:13:09 UTC (rev 8989)
+++ code/branches/formation/src/orxonox/controllers/ArtificialController.cc	2011-12-16 15:25:28 UTC (rev 8990)
@@ -29,19 +29,35 @@
 #include "ArtificialController.h"
 #include "core/CoreIncludes.h"
 #include "worldentities/pawns/Pawn.h"
+#include "worldentities/pawns/SpaceShip.h"
 
+#include "weaponsystem/WeaponMode.h"
+#include "weaponsystem/WeaponPack.h"
+#include "weaponsystem/Weapon.h"
+#include "weaponsystem/WeaponSlot.h"
+#include "weaponsystem/WeaponSlot.h"
 
+
 namespace orxonox
 {
 
     ArtificialController::ArtificialController(BaseObject* creator) : FormationController(creator)
     {
-          
+        this->bSetupWorked = false;
+        this->botlevel_ = 0.5f;
+        this->timeout_ = 0;
+        this->currentWaypoint_ = 0;
+        this->setAccuracy(5);
+        this->defaultWaypoint_ = NULL;
     }
 
     ArtificialController::~ArtificialController()
     {
-        
+        if (this->isInitialized())
+        {//Vector-implementation: mode_.erase(mode_.begin(),mode_.end());
+            this->waypoints_.clear();
+            this->weaponModes_.clear();
+        }
     }
 
 
@@ -180,7 +196,7 @@
 
     void ArtificialController::setPreviousMode()
     {
-        this->mode_ = DEFAULT; //Vector-implementation: mode_.pop_back();
+        this->mode_ = NORMAL; //Vector-implementation: mode_.pop_back();
     }
 
     /**
@@ -248,7 +264,7 @@
     }
 
     /**
-        @brief Adds point of interest depending on context. Further Possibilites: "ForceField", "PortalEndPoint", "MovableEntity", "Dock"
+        @brief Adds point of interest depending on context.  TODO: Further Possibilites: "ForceField", "PortalEndPoint", "MovableEntity", "Dock"
     */
     void ArtificialController::manageWaypoints()
     {

Modified: code/branches/formation/src/orxonox/controllers/FormationController.cc
===================================================================
--- code/branches/formation/src/orxonox/controllers/FormationController.cc	2011-12-14 18:13:09 UTC (rev 8989)
+++ code/branches/formation/src/orxonox/controllers/FormationController.cc	2011-12-16 15:25:28 UTC (rev 8990)
@@ -39,6 +39,7 @@
 #include "worldentities/pawns/TeamBaseMatchBase.h"
 #include "gametypes/TeamDeathmatch.h"
 #include "gametypes/Dynamicmatch.h"
+//#include "gametypes/Mission.h" TODO: include mission after merging
 #include "gametypes/Gametype.h"
 #include "controllers/WaypointPatrolController.h"
 #include "controllers/NewHumanController.h"
@@ -960,7 +961,7 @@
                 team2 = tdm->getTeam(entity2->getPlayer());
         }
 
-        Mission* miss = orxonox_cast<Mission*>(gametype); //NEW
+        /*Mission* miss = orxonox_cast<Mission*>(gametype); //NEW
         if (miss)
         {
             if (entity1->getPlayer())
@@ -968,7 +969,7 @@
 
             if (entity2->getPlayer())
                 team2 = miss->getTeam(entity2->getPlayer());
-        }
+        }*/
 
         TeamBaseMatchBase* base = 0;
         base = orxonox_cast<TeamBaseMatchBase*>(entity1);

Modified: code/branches/formation/src/orxonox/controllers/FormationController.h
===================================================================
--- code/branches/formation/src/orxonox/controllers/FormationController.h	2011-12-14 18:13:09 UTC (rev 8989)
+++ code/branches/formation/src/orxonox/controllers/FormationController.h	2011-12-16 15:25:28 UTC (rev 8990)
@@ -87,7 +87,7 @@
                Defend-just defend the master
                Attack-leave formation, attack every target
       */ 
-      enum Mode {NORMAL,DEFEND,ATTACK};
+      enum Mode {NORMAL,DEFEND,ATTACK,ROCKET};
       
       /**
         @brief Sets the new mode. If master, set it for all slaves.

Modified: code/branches/formation/src/orxonox/controllers/HumanController.cc
===================================================================
--- code/branches/formation/src/orxonox/controllers/HumanController.cc	2011-12-14 18:13:09 UTC (rev 8989)
+++ code/branches/formation/src/orxonox/controllers/HumanController.cc	2011-12-16 15:25:28 UTC (rev 8990)
@@ -317,18 +317,20 @@
         if (HumanController::localController_s && HumanController::localController_s->state_==MASTER)
         {
             switch (HumanController::localController_s->getMode()) {
-              case NORMAL:
-                HumanController::localController_s->setMode(DEFEND);
-                orxout(message) <<"Mode: DEFEND "<< endl;
-                break;
-              case DEFEND:
-                HumanController::localController_s->setMode(ATTACK);
-                orxout(message) <<"Mode: ATTACK "<< endl;
-                break;
-              case ATTACK:
-                HumanController::localController_s->setMode(NORMAL);
-                orxout(message) <<"Mode: NORMAL "<< endl;
-                break;
+                case NORMAL:
+                    HumanController::localController_s->setMode(DEFEND);
+                    orxout(message) <<"Mode: DEFEND "<< endl;
+                    break;
+                case DEFEND:
+                    HumanController::localController_s->setMode(ATTACK);
+                    orxout(message) <<"Mode: ATTACK "<< endl;
+                    break;
+                case ATTACK:
+                    HumanController::localController_s->setMode(NORMAL);
+                    orxout(message) <<"Mode: NORMAL "<< endl;
+                    break;
+                default: //catch all non-formation related states
+                    break;
             }
         }
     }

Modified: code/branches/formation/src/orxonox/controllers/NewHumanController.cc
===================================================================
--- code/branches/formation/src/orxonox/controllers/NewHumanController.cc	2011-12-14 18:13:09 UTC (rev 8989)
+++ code/branches/formation/src/orxonox/controllers/NewHumanController.cc	2011-12-16 15:25:28 UTC (rev 8990)
@@ -276,7 +276,7 @@
         HumanController::tick(dt);
     }
 
-    void NewHumanController::doFire(unsigned int firemode)
+    void NewHumanController::doFire(unsigned int firemode)//TODO??
     {
         if (!this->controllableEntity_)
             return;




More information about the Orxonox-commit mailing list