[Orxonox-commit 6104] r10762 - code/branches/AI_HS15/src/orxonox/controllers

gania at orxonox.net gania at orxonox.net
Tue Nov 3 15:56:41 CET 2015


Author: gania
Date: 2015-11-03 15:56:41 +0100 (Tue, 03 Nov 2015)
New Revision: 10762

Modified:
   code/branches/AI_HS15/src/orxonox/controllers/CommonController.cc
   code/branches/AI_HS15/src/orxonox/controllers/CommonController.h
   code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc
   code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc
   code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc
Log:
added a canFire() function

Modified: code/branches/AI_HS15/src/orxonox/controllers/CommonController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/CommonController.cc	2015-11-03 13:11:19 UTC (rev 10761)
+++ code/branches/AI_HS15/src/orxonox/controllers/CommonController.cc	2015-11-03 14:56:41 UTC (rev 10762)
@@ -241,16 +241,6 @@
         }
     }
 
-
-    int CommonController::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;
-    }
     bool CommonController::isCloseAtTarget(float distance) const
     {
         if (!this->getControllableEntity())
@@ -261,75 +251,52 @@
         else
             return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance);
     }
-    void CommonController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions)
+    
+
+    bool CommonController::canFire()
     {
-        this->bSetupWorked = false;
-        if(this->getControllableEntity())
+        //check pointers
+        if (!this->getControllableEntity() || !this->target_ || !this->target_->getControllableEntity())
+            return false;
+        
+        //check if this points in the direction of target_
+
+        Vector3 myPosition = this->getControllableEntity()->getWorldPosition();
+        Vector3 targetPosition = this->target_->getControllableEntity()->getWorldPosition();
+        Vector3 differenceVector = targetPosition - myPosition;
+        float scalarProduct = differenceVector * WorldEntity::FRONT;
+        Vector3 projection = scalarProduct * WorldEntity::FRONT;
+        if ((differenceVector - projection).length() > 50)
+            return false;
+       
+
+        //check if there are allies on the way
+        Vector3 allyPosition, allyDifference, allyProjection;
+        float allyScalarProduct;
+        for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)
         {
-            Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
-            if(pawn && pawn->isA(Class(SpaceShip))) //fix for First Person Mode: check for SpaceShip
+            if (!it->getControllableEntity())
+                continue;
+            if ((this->getControllableEntity()->getTeam() == (it)->getControllableEntity()->getTeam()))
             {
-                this->weaponModes_.clear(); // reset previous weapon information
-                WeaponSlot* wSlot = 0;
-                for(int l=0; (wSlot = pawn->getWeaponSlot(l)) ; l++)
-                {
-                    WeaponMode* wMode = 0;
-                    for(int i=0; (wMode = wSlot->getWeapon()->getWeaponmode(i)) ; i++)
-                    {
-                        std::string wName = wMode->getIdentifier()->getName();
-                        if(this->getFiremode(wName) == -1) //only add a weapon, if it is "new"
-                            weaponModes_[wName] = wMode->getMode();
-                    }
-                }
-                if(weaponModes_.size())//at least one weapon detected
-                    this->bSetupWorked = true;
-            }//pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user);
+                allyPosition = it->getControllableEntity()->getWorldPosition();
+                allyDifference = allyPosition - myPosition;
+                allyScalarProduct = allyDifference * WorldEntity::FRONT;
+                allyProjection = allyScalarProduct * WorldEntity::FRONT;
+                if (allyScalarProduct < 0 || allyScalarProduct > scalarProduct)
+                    continue;
+                if ((allyDifference - allyProjection).length() < 50)
+                    return false;
+            } 
         }
+
+        return true;
+
     }
     void CommonController::doFire()
     {
-          if(!this->bSetupWorked)//setup: find out which weapons are active ! hard coded: laser is "0", lens flare is "1", ...
-        {
-            this->setupWeapons();
-        }
-        else if(this->getControllableEntity() && weaponModes_.size()&&this->bShooting_ && 
-            this->isCloseAtTarget((1 + 2)*1000) && this->isLookingAtTarget(math::pi / 20.0f))
-        {
-            int firemode;
-            float random = rnd(1);//
-            if (this->isCloseAtTarget(130) && (firemode = getFiremode("LightningGun")) > -1 )
-            {//LENSFLARE: short range weapon
-                this->getControllableEntity()->fire(firemode); //ai uses lens flare if they're close enough to the target
-            }
-           
-            else if ((firemode = getFiremode("HsW01")) > -1 ) //LASER: default weapon
-                this->getControllableEntity()->fire(firemode);
-        
-        }
+        this->getControllableEntity()->fire(0);
     }
-    bool CommonController::isLookingAtTarget(float angle) const
-    {
-        if (!this->getControllableEntity())
-            return false;
+   
 
-        return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);
-    }
-
-    void CommonController::aimAtTarget()
-    {
-        if (!this->target_ || !this->getControllableEntity())
-            return;
-
-        static const float hardcoded_projectile_speed = 750;
-
-        Vector3 aimPosition = getPredictedPosition(this->getControllableEntity()->getWorldPosition(), 
-            hardcoded_projectile_speed, this->target_->getWorldPosition(), this->target_->getVelocity());
-
-        Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
-        if (pawn)
-            pawn->setAimPosition(aimPosition);
-    }
-    
- 
-
 }

Modified: code/branches/AI_HS15/src/orxonox/controllers/CommonController.h
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/CommonController.h	2015-11-03 13:11:19 UTC (rev 10761)
+++ code/branches/AI_HS15/src/orxonox/controllers/CommonController.h	2015-11-03 14:56:41 UTC (rev 10762)
@@ -132,7 +132,8 @@
             void aimAtTarget();
             bool isLookingAtTarget(float angle) const;
 
-
+            //checks if spaceship points at enemy and if there are allies inbetween
+            bool canFire();
             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 timeout_; //<! Timeout for rocket usage. (If a rocket misses, a bot should stop using it.)

Modified: code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc	2015-11-03 13:11:19 UTC (rev 10761)
+++ code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc	2015-11-03 14:56:41 UTC (rev 10762)
@@ -94,6 +94,9 @@
             } 
         }
            */
+        
+        if (canFire())
+            doFire();
       
     }
 

Modified: code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc	2015-11-03 13:11:19 UTC (rev 10761)
+++ code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc	2015-11-03 14:56:41 UTC (rev 10762)
@@ -57,12 +57,7 @@
         if (!this->isActive())
             return;
         
-        /*if (this->target_)
-        {
-            this->aimAtTarget();
-            this->doFire();
-            this->bShooting_ = true;
-        }*/
+        
         if (this->bHasTargetPosition_)
         {
             this->moveToTargetPosition();
@@ -90,6 +85,9 @@
         setTargetPositionOfWingman();
         if (this->target_ && this->myWingman_)
             this->myWingman_->setTarget(this->target_);
+
+        if (canFire())
+            doFire();
                 
 
     }

Modified: code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc
===================================================================
--- code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc	2015-11-03 13:11:19 UTC (rev 10761)
+++ code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc	2015-11-03 14:56:41 UTC (rev 10762)
@@ -110,42 +110,25 @@
 
     void WingmanController::tick(float dt)
     {   
-        //-------------------------------------------------------
-       /* if (this->target_)
-        {
-            this->aimAtTarget();
-            this->doFire();
-            this->bShooting_ = true;
-        }*/
+       
         
         if (!this->isActive())
             return;
-        //--------------------------Stay in formation--------------------------
         if (!this->target_)
         {
             //stay in formation
         }
         else
         {
-            
+
         }
         if (this->bHasTargetPosition_)
         {
             this->moveToTargetPosition();
         } 
         
-        //--------------------------Attack same target as the Leader--------------------------
-
-        /*if (this->target_)
-        {
-            this->aimAtTarget();
-            this->doFire();
-        }
-        */
+       
         
-        //orxout(internal_error) << "I am " << this << endl;
-
-        
         SUPER(WingmanController, tick, dt);
     }
     
@@ -168,6 +151,9 @@
         {
 
         }
+
+        if (canFire())
+            doFire();
     }
      
    




More information about the Orxonox-commit mailing list