Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

added a canFire() function

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.