[Orxonox-commit 4092] r8763 - code/branches/ai2/src/orxonox/controllers

jo at orxonox.net jo at orxonox.net
Mon Jul 18 22:59:35 CEST 2011


Author: jo
Date: 2011-07-18 22:59:35 +0200 (Mon, 18 Jul 2011)
New Revision: 8763

Modified:
   code/branches/ai2/src/orxonox/controllers/ArtificialController.cc
   code/branches/ai2/src/orxonox/controllers/ArtificialController.h
Log:
New implementation. New bug. Bots use only yellow energy munition (LightningGun).

Modified: code/branches/ai2/src/orxonox/controllers/ArtificialController.cc
===================================================================
--- code/branches/ai2/src/orxonox/controllers/ArtificialController.cc	2011-07-17 21:43:20 UTC (rev 8762)
+++ code/branches/ai2/src/orxonox/controllers/ArtificialController.cc	2011-07-18 20:59:35 UTC (rev 8763)
@@ -90,7 +90,6 @@
 
         this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this));
         this->bSetupWorked = false;
-        this->numberOfWeapons = 0;
         this->botlevel_ = 0.5f;
         this->mode_ = DEFAULT;////Vector-implementation: mode_.push_back(DEFAULT);
         this->timeout_=0;
@@ -101,7 +100,7 @@
         if (this->isInitialized())
         {//Vector-implementation: mode_.erase(mode_.begin(),mode_.end());
             this->removeFromFormation();
-
+            this->weaponModes_.clear();
             for (ObjectList<ArtificialController>::iterator it = ObjectList<ArtificialController>::begin(); it; ++it)
             {
                 if (*it != this)
@@ -316,7 +315,6 @@
         if (!this->getControllableEntity())
             this->removeFromFormation();
         this->bSetupWorked = false;        // reset weapon information
-        this->numberOfWeapons = 0;
         this->setupWeapons();
     }
 
@@ -1042,20 +1040,19 @@
         if(!this->bSetupWorked)//setup: find out which weapons are active ! hard coded: laser is "0", lens flare is "1", ...
         {
             this->setupWeapons();
-            if(numberOfWeapons > 0)
-                this->bSetupWorked = true;
         }
-        else if(this->getControllableEntity() && (numberOfWeapons>0)&&this->bShooting_ && this->isCloseAtTarget((1 + 2*botlevel_)*1000) && this->isLookingAtTarget(math::pi / 20.0f))
+        else if(this->getControllableEntity() && weaponModes_.size()&&this->bShooting_ && this->isCloseAtTarget((1 + 2*botlevel_)*1000) && this->isLookingAtTarget(math::pi / 20.0f))
         {
+            int firemode;
             float random = rnd(1);//
-            if (this->isCloseAtTarget(130) && (weaponModes[1]>-1) )
+            if (this->isCloseAtTarget(130) && (firemode = getFiremode("LightningGun"))>-1 )
             {//LENSFLARE: short range weapon
-                this->getControllableEntity()->fire(weaponModes[1]); //ai uses lens flare if they're close enough to the target
+                this->getControllableEntity()->fire(firemode); //ai uses lens flare if they're close enough to the target
             }
-            else if( (weaponModes[3]>-1) && this->isCloseAtTarget(400) && (projectiles[3] > 0) && (random < this->botlevel_) )
+            else if( this->isCloseAtTarget(400) && (random < this->botlevel_) && (firemode = getFiremode("RocketFire")>-1))
             {//ROCKET: mid range weapon
                 this->mode_ = ROCKET; //Vector-implementation: mode_.push_back(ROCKET);
-                this->getControllableEntity()->fire(weaponModes[3]); //launch rocket
+                this->getControllableEntity()->fire(firemode); //launch rocket
                 if(this->getControllableEntity() && this->target_) //after fire(3) is called, getControllableEntity() refers to the rocket!
                 {
                     float speed = this->getControllableEntity()->getVelocity().length() - target_->getVelocity().length();
@@ -1065,10 +1062,9 @@
                 }
                 else
                     this->timeout_ = 4.0f; //TODO: find better default value
-                this->projectiles[3] -= 1; //decrease ammo
             }
-            else if (weaponModes[0]>-1) //LASER: default weapon
-                this->getControllableEntity()->fire(weaponModes[0]);
+            else if ((firemode = getFiremode("HsW01")>-1)) //LASER: default weapon
+                this->getControllableEntity()->fire(firemode);
         }
     }
 
@@ -1077,37 +1073,25 @@
     */
     void ArtificialController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions)
     {
+        this->bSetupWorked = false;
         if(this->getControllableEntity())
         {
             Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
             if(pawn)
             {
-                int max = WeaponSystem::MAX_WEAPON_MODES; // assumption: there are only so many weaponmodes possible
-                for(int x=0; x<max ;x++)
-                    weaponModes[x] = -1; // reset previous weapon information
-                this->numberOfWeapons = 0;
-                for(int l=0; l<max ;l++)
+                this->weaponModes_.clear(); // reset previous weapon information
+                WeaponSlot* wSlot = 0;
+                for(int l=0; (wSlot = pawn->getWeaponSlot(l)) ; l++)
                 {
-                    WeaponSlot* wSlot = pawn->getWeaponSlot(l);
-                    if(wSlot==NULL) continue;
-                    for(int i=0; i<max; i++)
+                    WeaponMode* wMode = 0;
+                    for(int i=0; (wMode = wSlot->getWeapon()->getWeaponmode(i)) ; i++)
                     {
-                        WeaponMode* wMode = wSlot->getWeapon()->getWeaponmode(i);
-                        if(wMode == NULL) continue;
-                        this->numberOfWeapons++;
-                        if(wMode->getIdentifier()->getName() == "HsW01") // red laser <-> 0
-                            weaponModes[0] = wMode->getMode();
-                        else if(wMode->getIdentifier()->getName() == "LightningGun") // yellow energy  <-> 1
-                            weaponModes[1] = wMode->getMode();
-                        else if(wMode->getIdentifier()->getName() == "SimpleRocketFire") // target seeking rocktes <-> 2
-                            weaponModes[2] = wMode->getMode(); // not relevant jet, since doFire() doesn't support simple rockets
-                        else if(wMode->getIdentifier()->getName() == "RocketFire") // manual rockets <-> 3
-                            weaponModes[3] = wMode->getMode();
-                        else
-                            COUT(1)<< wMode->getIdentifier()->getName() << " has to be added in ArtificialController.cc as new weapon." << std::endl;
+                        std::string wName = wMode->getIdentifier()->getName();
+                        if(this->getFiremode(wName) == -1) //only add a weapon, if it is "new"
+                            weaponModes_[wName] = wMode->getMode();
                     }
                 }
-                if(numberOfWeapons>0)
+                if(weaponModes_.size())//at least one weapon detected
                     this->bSetupWorked = true;
             }//pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user);
         }
@@ -1145,4 +1129,14 @@
             this->getControllableEntity()->boost(false);
     }
 
+    int ArtificialController::getFiremode(std::string name)
+    {
+        for (std::map< std::string, int >::iterator it = this->weaponModes_.begin(); it != this->weaponModes_.end(); ++it)
+        {
+            if (it->first == name)
+                return it->second;
+        }
+        return -1;
+    }
+
 }

Modified: code/branches/ai2/src/orxonox/controllers/ArtificialController.h
===================================================================
--- code/branches/ai2/src/orxonox/controllers/ArtificialController.h	2011-07-17 21:43:20 UTC (rev 8762)
+++ code/branches/ai2/src/orxonox/controllers/ArtificialController.h	2011-07-18 20:59:35 UTC (rev 8763)
@@ -31,7 +31,7 @@
 
 #include "OrxonoxPrereqs.h"
 
-#include <vector>
+#include <map>
 
 #include "util/Math.h"
 #include "Controller.h"
@@ -148,9 +148,8 @@
             WeakPtr<Pawn> target_;
             bool bShooting_;
 
-            int numberOfWeapons; //<! Used for weapon init function. Displayes number of weapons available for a bot.
-            int weaponModes[WeaponSystem::MAX_WEAPON_MODES]; //<! Links each "weapon" to it's weaponmode- managed by setupWeapons()
-            int projectiles[WeaponSystem::MAX_WEAPON_MODES]; //<! Displays amount of projectiles of each weapon. - managed by setupWeapons()
+            std::map<std::string, int> weaponModes_; //<! Links each "weapon" to it's weaponmode- managed by setupWeapons()
+            //std::vector<int> projectiles_; //<! Displays amount of projectiles of each weapon. - managed by setupWeapons()
             float botlevel_; //<! Makes the level of a bot configurable.
             float timeout_; //<! Timeout for rocket usage. (If a rocket misses, a bot should stop using it.)
 
@@ -159,6 +158,7 @@
             void setPreviousMode();
             void setupWeapons(); //<! Defines which weapons are available for a bot. Is recalled whenever a bot was killed.
             bool bSetupWorked; //<! If false, setupWeapons() is called.
+            int getFiremode(std::string name);
     };
 }
 




More information about the Orxonox-commit mailing list