Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 23, 2015, 11:17:22 AM (8 years ago)
Author:
gania
Message:

minor bugfixes and code style improvement

File:
1 edited

Legend:

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

    r10826 r10832  
    5151    CommonController::CommonController( Context* context ): Controller( context )
    5252    {
    53         this->bSetupWorked = false;
    5453
    5554       
    56         this->executingMoveToPoint_ = false;
    5755        this->action_ = Action::FLY;
    5856        this->stopLookingAtTarget();
     
    7169        SUPER( CommonController, XMLPort, xmlelement, mode );
    7270        XMLPortParam( CommonController, "formationMode", setFormationModeXML, getFormationModeXML,  xmlelement, mode );
    73 
     71        XMLPortParam( CommonController, "action", setActionXML, getActionXML,  xmlelement, mode );
     72
     73    }
     74    void CommonController::setActionXML( std::string val)
     75    {
     76        const std::string valUpper = getUppercase( val );
     77        Action::Value value;
     78       
     79        if ( valUpper == "FIGHT" )
     80            value = Action::FIGHT;
     81        else if ( valUpper == "FLY" )
     82            value = Action::FLY;
     83        else if ( valUpper == "PROTECT" )
     84            value = Action::PROTECT;
     85        else
     86            ThrowException( ParseError, std::string( "Attempting to set an unknown Action: '" )+ val + "'." );
     87        this->setAction( value );
     88    }
     89    std::string CommonController::getActionXML()
     90    {
     91        switch ( this->action_ )
     92        {
     93            case Action::FIGHT:
     94            {
     95                return "FIGHT";
     96                break;
     97            }
     98            case Action::FLY:
     99            {
     100                return "FLY";
     101                break;
     102            }
     103            case Action::PROTECT:
     104            {
     105                return "PROTECT";
     106                break;
     107            }
     108            default:
     109                return "FIGHT";
     110                break;
     111        }
    74112    }
    75113    void CommonController::setFormationModeXML( std::string val )
     
    118156        return this->action_;
    119157    }
     158    void CommonController::setAction (Action::Value action)
     159    {
     160        this->action_ = action;
     161    }
    120162
    121163    void CommonController::setAction (Action::Value action, ControllableEntity* target)
     
    156198        }
    157199    }
     200    void CommonController::setClosestTarget()
     201    {
     202        if (!this->getControllableEntity())
     203            return;
     204
     205        Pawn* closestTarget = 0;
     206        float minDistance =  std::numeric_limits<float>::infinity();
     207       
     208        for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
     209        {
     210            if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) )
     211                continue;
     212
     213            float distance = CommonController::distance (*itP, this->getControllableEntity());
     214            if (distance < minDistance)
     215            {
     216                closestTarget = *itP;
     217                minDistance = distance;
     218            }
     219        }
     220        if (closestTarget)
     221        {
     222           (this)->setTarget(static_cast<ControllableEntity*>(closestTarget));
     223        }   
     224    }
    158225    void CommonController::maneuver()
    159226    {
    160         counter++;
    161 
    162         if (counter > 5)
    163             counter = 0;
     227        maneuverCounter_++;
     228
     229        if (maneuverCounter_ > 5)
     230            maneuverCounter_ = 0;
    164231        if ( this->target_ && this->getControllableEntity())
    165232        {
     
    183250
    184251            //bool bThisIsLookingAtTarget = this->isLooking ( getControllableEntity(), this->target_, math::pi/4 );
    185             bool bTargetIsLookingAtThis = this->isLooking ( this->target_, getControllableEntity(), math::pi/5.0f );
     252            bool bTargetIsLookingAtThis = this->isLooking ( this->target_, getControllableEntity(), math::pi/10.0f );
    186253           
    187254
     
    217284            {
    218285                this->setTargetPosition( this->positionOfTarget_ );
    219               /*  if (counter == 0)
     286              /*  if (maneuverCounter_ == 0)
    220287                {
    221288                    this->setTargetPosition( this->positionOfTarget_ );   
     
    231298            {   
    232299           
    233                 if (counter == 0)
     300                if (maneuverCounter_ == 0)
    234301                {
    235302                    this->setTargetPosition( this->positionOfTarget_ );   
     
    562629            this->getControllableEntity() ->getOrientation()  * WorldEntity::FRONT, this->positionOfTarget_ ) < angle );
    563630    }
    564     bool CommonController::isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle )const
     631    bool CommonController::isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle )
    565632    {
    566633        if ( !entityThatLooks || !entityBeingLookedAt )
     
    587654        float squaredDistance = squaredDistanceToTarget();
    588655
    589         if ( squaredDistance < 9000000.0f && this->isLookingAtTarget( math::pi / 10.0f)) {
     656        if ( squaredDistance < 9000000.0f && this->isLookingAtTarget( math::pi / 20.0f)) {
    590657            return true;
    591658        }
     
    615682        }
    616683     
    617 
    618        
    619684        Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() );
    620685
Note: See TracChangeset for help on using the changeset viewer.