[Orxonox-commit 4091] r8762 - code/branches/ai2/src/orxonox/controllers

jo at orxonox.net jo at orxonox.net
Sun Jul 17 23:43:20 CEST 2011


Author: jo
Date: 2011-07-17 23:43:20 +0200 (Sun, 17 Jul 2011)
New Revision: 8762

Modified:
   code/branches/ai2/src/orxonox/controllers/ArtificialController.cc
   code/branches/ai2/src/orxonox/controllers/ArtificialController.h
Log:
Weaponsetup now generic. Munition management has not jet been implemented.

Modified: code/branches/ai2/src/orxonox/controllers/ArtificialController.cc
===================================================================
--- code/branches/ai2/src/orxonox/controllers/ArtificialController.cc	2011-07-15 08:49:17 UTC (rev 8761)
+++ code/branches/ai2/src/orxonox/controllers/ArtificialController.cc	2011-07-17 21:43:20 UTC (rev 8762)
@@ -317,7 +317,7 @@
             this->removeFromFormation();
         this->bSetupWorked = false;        // reset weapon information
         this->numberOfWeapons = 0;
-        COUT(0)<<"ArtificialController::changedControllableEntity()"<<endl; //why is this function called more than once ??
+        this->setupWeapons();
     }
 
     void ArtificialController::removeFromFormation()
@@ -1039,23 +1039,23 @@
     */
     void ArtificialController::doFire()
     {
-        if(!bSetupWorked)//setup: find out which weapons are active ! hard coded: laser is "0", lens flare is "1", ...
+        if(!this->bSetupWorked)//setup: find out which weapons are active ! hard coded: laser is "0", lens flare is "1", ...
         {
             this->setupWeapons();
             if(numberOfWeapons > 0)
-                bSetupWorked = true;
+                this->bSetupWorked = true;
         }
         else if(this->getControllableEntity() && (numberOfWeapons>0)&&this->bShooting_ && this->isCloseAtTarget((1 + 2*botlevel_)*1000) && this->isLookingAtTarget(math::pi / 20.0f))
         {
             float random = rnd(1);//
-            if (this->isCloseAtTarget(130) && weapons[1] )
+            if (this->isCloseAtTarget(130) && (weaponModes[1]>-1) )
             {//LENSFLARE: short range weapon
-                this->getControllableEntity()->fire(1); //ai uses lens flare if they're close enough to the target
+                this->getControllableEntity()->fire(weaponModes[1]); //ai uses lens flare if they're close enough to the target
             }
-            else if(weapons[3] && this->isCloseAtTarget(400) && (projectiles[3] > 0) && (random < this->botlevel_) )
+            else if( (weaponModes[3]>-1) && this->isCloseAtTarget(400) && (projectiles[3] > 0) && (random < this->botlevel_) )
             {//ROCKET: mid range weapon
                 this->mode_ = ROCKET; //Vector-implementation: mode_.push_back(ROCKET);
-                this->getControllableEntity()->fire(3); //launch rocket
+                this->getControllableEntity()->fire(weaponModes[3]); //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();
@@ -1067,8 +1067,8 @@
                     this->timeout_ = 4.0f; //TODO: find better default value
                 this->projectiles[3] -= 1; //decrease ammo
             }
-            else if (weapons[0])//LASER: default weapon
-                this->getControllableEntity()->fire(0);
+            else if (weaponModes[0]>-1) //LASER: default weapon
+                this->getControllableEntity()->fire(weaponModes[0]);
         }
     }
 
@@ -1082,75 +1082,38 @@
             Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
             if(pawn)
             {
-                this->analyseWeapons(pawn);
-                for(unsigned int i=0; i < WeaponSystem::MAX_WEAPON_MODES; i++)
+                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++)
                 {
-                    //const std::string wpn = getWeaponname(i, 0, pawn); COUT(0)<<wpn<< std::endl;//Temporary debug info.
-                    /*if(wpn=="") //future, more generic implementation; until now, only LaserMunition works. Is this a bug??
-                        weapons[i]=false;
-                    else if(wpn=="LaserMunition")//other munitiontypes are not defined yet :-(
-                        weapons[0]=true;
-                    else if(wpn=="FusionMunition")
-                        weapons[1]=true;
-                    else if(wpn=="TargetSeeking Rockets")
-                        weapons[2]=true;
-                    else if(wpn=="RocketMunition")
-                        weapons[3]=true;
-                    else
-                        COUT(1)<< wpn << + << " has to be added in ArtificialController.cc as new weapon." << std::endl;
-                    */
-                    if(pawn->getWeaponSet(i)) //main part: find which weapons a pawn can use; hard coded at the moment!
+                    WeaponSlot* wSlot = pawn->getWeaponSlot(l);
+                    if(wSlot==NULL) continue;
+                    for(int i=0; i<max; i++)
                     {
-                        weapons[i] = true;
-                        projectiles[i] = 10; //TODO: how to get data?? getWeaponmode(i)->getMunition()->getNumMunition(WeaponMode* user)
-                          numberOfWeapons++;
+                        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;
                     }
-                    else
-                        weapons[i] = false;
                 }
-                 //pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user);
-            }
+                if(numberOfWeapons>0)
+                    this->bSetupWorked = true;
+            }//pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user);
         }
     }
 
-    const std::string ArtificialController::getWeaponname(int i, int u, Pawn* pawn)//Search through all wPacks,
-    {//is there a way to minimize this long if-return structure, without triggering nullpointer exceptions?
-        if(!pawn) return "a";
-        WeaponPack* wPack = pawn->getWeaponPack(u);
-        if(!wPack) return "b";
-        Weapon* wpn = wPack->getWeapon(i);
-        if(!wpn && u<10 && i>0)
-        {
-            return this->getWeaponname(i, u+1, pawn);
-        }
-        else if(!wpn)
-            return "c";
-        //return wpn->getName();
-        WeaponMode* wMode = wpn->getWeaponmode(0);
-        if(!wMode) return "d";
-        return wMode->getMunitionName();//getName();
-    }//pawn->getWeaponpack(i)->getWeapon(i)->getWeaponmode(i)->getMunitionName()
-    /**
-        @brief Display how a spaceship is equiped with weapons. TODO: why are only 3 out of 8 weapons displayed??
-    */
-    void ArtificialController::analyseWeapons(Pawn* pawn)
-    {
-        int max = 10;
-        if(!pawn) return;
-        for(int l=0; l<max ;l++)
-        {
-            WeaponSlot* wSlot = pawn->getWeaponSlot(l);
-            if(wSlot==NULL) continue;//{COUT(0)<<"WEAPONSLOT "<<l<< " failed"<<endl; continue;}
-            for(int i=0; i<max; i++)
-            {
-                 WeaponMode* wMode = wSlot->getWeapon()->getWeaponmode(i);
-                 if(wMode==NULL) continue;//{COUT(0)<<"WEAPONMODE "<<i<< " failed"<<endl;}
-                 COUT(0)<<wMode->getIdentifier()->getName()<< " using mode:"<<i<<endl;
-            }
-        }
-    }
 
-
     void ArtificialController::setBotLevel(float level)
     {
         if (level < 0.0f)
@@ -1176,9 +1139,9 @@
     {
         SpaceShip* ship = orxonox_cast<SpaceShip*>(this->getControllableEntity());
         if(ship == NULL) return;
-        if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower() )//upper limit ->boost
+        if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower() ) //upper limit ->boost
             this->getControllableEntity()->boost(true);
-        else if(ship->getBoostPower()*4.0f < ship->getInitialBoostPower())//lower limit ->do not boost
+        else if(ship->getBoostPower()*4.0f < ship->getInitialBoostPower()) //lower limit ->do not boost
             this->getControllableEntity()->boost(false);
     }
 

Modified: code/branches/ai2/src/orxonox/controllers/ArtificialController.h
===================================================================
--- code/branches/ai2/src/orxonox/controllers/ArtificialController.h	2011-07-15 08:49:17 UTC (rev 8761)
+++ code/branches/ai2/src/orxonox/controllers/ArtificialController.h	2011-07-17 21:43:20 UTC (rev 8762)
@@ -149,7 +149,7 @@
             bool bShooting_;
 
             int numberOfWeapons; //<! Used for weapon init function. Displayes number of weapons available for a bot.
-            bool weapons[WeaponSystem::MAX_WEAPON_MODES]; //<! Displays if a weapon is available - managed by setupWeapons()
+            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()
             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.)
@@ -158,8 +158,6 @@
             Mode mode_; //TODO: replace single value with stack-like implementation: std::vector<Mode> mode_;
             void setPreviousMode();
             void setupWeapons(); //<! Defines which weapons are available for a bot. Is recalled whenever a bot was killed.
-            const std::string getWeaponname(int i, int u, Pawn* pawn); //<! Function that links a weapon's firemode to its name.
-            void analyseWeapons(Pawn* pawn);
             bool bSetupWorked; //<! If false, setupWeapons() is called.
     };
 }




More information about the Orxonox-commit mailing list