Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10762


Ignore:
Timestamp:
Nov 3, 2015, 3:56:41 PM (9 years ago)
Author:
gania
Message:

added a canFire() function

Location:
code/branches/AI_HS15/src/orxonox/controllers
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/AI_HS15/src/orxonox/controllers/CommonController.cc

    r10759 r10762  
    242242    }
    243243
    244 
    245     int CommonController::getFiremode(std::string name)
    246     {
    247         for (std::map< std::string, int >::iterator it = this->weaponModes_.begin(); it != this->weaponModes_.end(); ++it)
    248         {
    249             if (it->first == name)
    250                 return it->second;
    251         }
    252         return -1;
    253     }
    254244    bool CommonController::isCloseAtTarget(float distance) const
    255245    {
     
    262252            return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance);
    263253    }
    264     void CommonController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions)
    265     {
    266         this->bSetupWorked = false;
    267         if(this->getControllableEntity())
     254   
     255
     256    bool CommonController::canFire()
     257    {
     258        //check pointers
     259        if (!this->getControllableEntity() || !this->target_ || !this->target_->getControllableEntity())
     260            return false;
     261       
     262        //check if this points in the direction of target_
     263
     264        Vector3 myPosition = this->getControllableEntity()->getWorldPosition();
     265        Vector3 targetPosition = this->target_->getControllableEntity()->getWorldPosition();
     266        Vector3 differenceVector = targetPosition - myPosition;
     267        float scalarProduct = differenceVector * WorldEntity::FRONT;
     268        Vector3 projection = scalarProduct * WorldEntity::FRONT;
     269        if ((differenceVector - projection).length() > 50)
     270            return false;
     271       
     272
     273        //check if there are allies on the way
     274        Vector3 allyPosition, allyDifference, allyProjection;
     275        float allyScalarProduct;
     276        for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it)
    268277        {
    269             Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
    270             if(pawn && pawn->isA(Class(SpaceShip))) //fix for First Person Mode: check for SpaceShip
    271             {
    272                 this->weaponModes_.clear(); // reset previous weapon information
    273                 WeaponSlot* wSlot = 0;
    274                 for(int l=0; (wSlot = pawn->getWeaponSlot(l)) ; l++)
    275                 {
    276                     WeaponMode* wMode = 0;
    277                     for(int i=0; (wMode = wSlot->getWeapon()->getWeaponmode(i)) ; i++)
    278                     {
    279                         std::string wName = wMode->getIdentifier()->getName();
    280                         if(this->getFiremode(wName) == -1) //only add a weapon, if it is "new"
    281                             weaponModes_[wName] = wMode->getMode();
    282                     }
    283                 }
    284                 if(weaponModes_.size())//at least one weapon detected
    285                     this->bSetupWorked = true;
    286             }//pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user);
    287         }
     278            if (!it->getControllableEntity())
     279                continue;
     280            if ((this->getControllableEntity()->getTeam() == (it)->getControllableEntity()->getTeam()))
     281            {
     282                allyPosition = it->getControllableEntity()->getWorldPosition();
     283                allyDifference = allyPosition - myPosition;
     284                allyScalarProduct = allyDifference * WorldEntity::FRONT;
     285                allyProjection = allyScalarProduct * WorldEntity::FRONT;
     286                if (allyScalarProduct < 0 || allyScalarProduct > scalarProduct)
     287                    continue;
     288                if ((allyDifference - allyProjection).length() < 50)
     289                    return false;
     290            }
     291        }
     292
     293        return true;
     294
    288295    }
    289296    void CommonController::doFire()
    290297    {
    291           if(!this->bSetupWorked)//setup: find out which weapons are active ! hard coded: laser is "0", lens flare is "1", ...
    292         {
    293             this->setupWeapons();
    294         }
    295         else if(this->getControllableEntity() && weaponModes_.size()&&this->bShooting_ &&
    296             this->isCloseAtTarget((1 + 2)*1000) && this->isLookingAtTarget(math::pi / 20.0f))
    297         {
    298             int firemode;
    299             float random = rnd(1);//
    300             if (this->isCloseAtTarget(130) && (firemode = getFiremode("LightningGun")) > -1 )
    301             {//LENSFLARE: short range weapon
    302                 this->getControllableEntity()->fire(firemode); //ai uses lens flare if they're close enough to the target
    303             }
    304            
    305             else if ((firemode = getFiremode("HsW01")) > -1 ) //LASER: default weapon
    306                 this->getControllableEntity()->fire(firemode);
    307        
    308         }
    309     }
    310     bool CommonController::isLookingAtTarget(float angle) const
    311     {
    312         if (!this->getControllableEntity())
    313             return false;
    314 
    315         return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);
    316     }
    317 
    318     void CommonController::aimAtTarget()
    319     {
    320         if (!this->target_ || !this->getControllableEntity())
    321             return;
    322 
    323         static const float hardcoded_projectile_speed = 750;
    324 
    325         Vector3 aimPosition = getPredictedPosition(this->getControllableEntity()->getWorldPosition(),
    326             hardcoded_projectile_speed, this->target_->getWorldPosition(), this->target_->getVelocity());
    327 
    328         Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
    329         if (pawn)
    330             pawn->setAimPosition(aimPosition);
    331     }
    332    
    333  
     298        this->getControllableEntity()->fire(0);
     299    }
     300   
    334301
    335302}
  • code/branches/AI_HS15/src/orxonox/controllers/CommonController.h

    r10761 r10762  
    133133            bool isLookingAtTarget(float angle) const;
    134134
    135 
     135            //checks if spaceship points at enemy and if there are allies inbetween
     136            bool canFire();
    136137            std::map<std::string, int> weaponModes_; //<! Links each "weapon" to it's weaponmode - managed by setupWeapons()
    137138            //std::vector<int> projectiles_; //<! Displays amount of projectiles of each weapon. - managed by setupWeapons()
  • code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc

    r10759 r10762  
    9595        }
    9696           */
     97       
     98        if (canFire())
     99            doFire();
    97100     
    98101    }
  • code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc

    r10759 r10762  
    5858            return;
    5959       
    60         /*if (this->target_)
    61         {
    62             this->aimAtTarget();
    63             this->doFire();
    64             this->bShooting_ = true;
    65         }*/
     60       
    6661        if (this->bHasTargetPosition_)
    6762        {
     
    9186        if (this->target_ && this->myWingman_)
    9287            this->myWingman_->setTarget(this->target_);
     88
     89        if (canFire())
     90            doFire();
    9391               
    9492
  • code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc

    r10761 r10762  
    111111    void WingmanController::tick(float dt)
    112112    {   
    113         //-------------------------------------------------------
    114        /* if (this->target_)
    115         {
    116             this->aimAtTarget();
    117             this->doFire();
    118             this->bShooting_ = true;
    119         }*/
     113       
    120114       
    121115        if (!this->isActive())
    122116            return;
    123         //--------------------------Stay in formation--------------------------
    124117        if (!this->target_)
    125118        {
     
    128121        else
    129122        {
    130            
     123
    131124        }
    132125        if (this->bHasTargetPosition_)
     
    135128        }
    136129       
    137         //--------------------------Attack same target as the Leader--------------------------
    138 
    139         /*if (this->target_)
    140         {
    141             this->aimAtTarget();
    142             this->doFire();
    143         }
    144         */
    145        
    146         //orxout(internal_error) << "I am " << this << endl;
    147 
     130       
    148131       
    149132        SUPER(WingmanController, tick, dt);
     
    169152
    170153        }
     154
     155        if (canFire())
     156            doFire();
    171157    }
    172158     
Note: See TracChangeset for help on using the changeset viewer.