Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10832


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

minor bugfixes and code style improvement

Location:
code/branches/AI_HS15
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/branches/AI_HS15/data/levels/AITest.oxw

    r10826 r10832  
    5757      </templates>
    5858      <controller>
    59         <DivisionController team=1 formationMode="DIAMOND">
     59        <DivisionController team=1 formationMode="wall" action="FIGHT">
    6060        </DivisionController>
    6161      </controller>
     
    9494      </templates>
    9595      <controller>
    96         <DivisionController team=2 formationMode="FINGER4">
     96        <DivisionController team=2 formationMode="wall" action="FIGHT">
    9797        </DivisionController>
    9898      </controller>
  • 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
  • code/branches/AI_HS15/src/orxonox/controllers/CommonController.h

    r10827 r10832  
    7373        public:
    7474            static const float hardcoded_projectile_speed = 750;
    75 
    7675            static const float ACTION_INTERVAL = 1.0f;
    77 
    7876
    7977            CommonController(Context* context);
    8078            virtual ~CommonController();
    8179
     80            //----[XML data]----
     81                virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
     82                //----[Action data]----
     83                    Action::Value getAction ();
     84                    void setAction (Action::Value action);
     85                    void setAction (Action::Value action, ControllableEntity* target);
     86                    void setAction (Action::Value action, const Vector3& target);
     87                    void setAction (Action::Value action, const Vector3& target,  const Quaternion& orient );
     88                    void setActionXML( std::string val);
     89                    std::string getActionXML();
     90                //----[/Action data]----
     91                //----[Formation data]----
     92                    virtual void setFormationModeXML(std::string val);
     93                    virtual std::string getFormationModeXML();
     94                    virtual void setFormationMode(FormationMode::Value val)
     95                        { this->formationMode_ = val; }
     96                    inline FormationMode::Value getFormationMode() const
     97                        { return this->formationMode_; }
     98                //----[/Formation data]----
     99                //----[Rank data]----
     100                    virtual void setRank(Rank::Value val)
     101                        { this->rank_ = val; }
     102                    inline Rank::Value getRank() const
     103                        { return this->rank_; }
     104                //----[/Rank data]----
     105            //----[/XML data]----
    82106
     107            //----[Interaction with other Controllers]----
     108                virtual bool setWingman(CommonController* wingman);
     109                virtual bool hasWingman();       
    83110
     111                void setPositionOfTarget(const Vector3& target);
     112                void setOrientationOfTarget(const Quaternion& orient);
    84113
     114                void setTarget(ControllableEntity* target);
     115                ControllableEntity* getTarget();
     116                bool hasTarget();
    85117
    86             virtual void setFormationMode(FormationMode::Value val)
    87                 { this->formationMode_ = val; }
    88             inline FormationMode::Value getFormationMode() const
    89                 { return this->formationMode_; }
    90             virtual void setFormationModeXML(std::string val);
    91             virtual std::string getFormationModeXML();
     118                void setTargetPosition(const Vector3& target);
     119                void setTargetOrientation(const Quaternion& orient);
     120                void setTargetOrientation(ControllableEntity* target);
     121            //----[/Interaction with other Controllers]----
    92122
    93             virtual void setRank(Rank::Value val)
    94                 { this->rank_ = val; }
    95             inline Rank::Value getRank() const
    96                 { return this->rank_; }
     123            //----[Helper functions]----
     124                float randomInRange(float a, float b);
     125                static float distance(ControllableEntity* entity1, ControllableEntity* entity2);
     126                static bool sameTeam (ControllableEntity* entity1, ControllableEntity* entity2);
     127                static bool isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle ) ;
    97128
    98             virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    99 
    100 
    101 
    102             virtual bool setWingman(CommonController* wingman);
    103             virtual bool hasWingman();
    104             static bool sameTeam (ControllableEntity* entity1, ControllableEntity* entity2);
    105             void setTarget(ControllableEntity* target);
    106             bool hasTarget();
    107             ControllableEntity* getTarget();
    108             void setTargetOrientation(const Quaternion& orient);
    109             void setTargetOrientation(ControllableEntity* target);
    110             void setTargetPosition(const Vector3& target);
    111 
    112             /*void spin();
    113             void turn180();*/
    114             Action::Value getAction ();
    115             void setAction (Action::Value action, ControllableEntity* target);
    116             void setAction (Action::Value action, const Vector3& target);
    117             void setAction (Action::Value action, const Vector3& target,  const Quaternion& orient );
     129                float squaredDistanceToTarget() const;
     130                bool isLookingAtTarget(float angle) const;
     131            //----[/Helper functions]----
    118132
    119133        protected:
    120             void dodge(Vector3& thisPosition, Vector3& diffUnit);
    121             int counter;
    122             void moveToPoint(const Vector3& relativeTargetPosition, float angleRoll);
    123             bool moveAndRoll(float dt);
     134            //----[Flying functionality]----
     135                void stopMoving();
    124136
    125             void moveToPosition(const Vector3& target, float dt);
    126             void moveToTargetPosition(float dt);
    127             //enum Mode {ROCKET, ATTACK, MOVE, HOLD};//TODO; implement DEFENCE, MOVING modes
     137                void moveToPoint(const Vector3& relativeTargetPosition, float angleRoll);
     138                bool moveAndRoll(float dt);
    128139
    129             //Mode mode_;
    130             void copyOrientation(const Quaternion& orient, float dt);
    131             void copyTargetOrientation(float dt);
     140                void moveToPosition(const Vector3& target, float dt);
     141                void moveToTargetPosition(float dt);
    132142
    133             static float distance(ControllableEntity* entity1, ControllableEntity* entity2);
    134             float squaredDistanceToTarget() const;
    135             void doFire();
    136             void aimAtTarget();
    137             bool isLookingAtTarget(float angle) const;
    138             bool isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle )const;
     143                void copyOrientation(const Quaternion& orient, float dt);
     144                void copyTargetOrientation(float dt);
    139145
    140             //checks if spaceship points at enemy and if there are allies inbetween
    141             bool canFire();
    142             std::map<std::string, int> weaponModes_; //<! Links each "weapon" to it's weaponmode - managed by setupWeapons()
    143             //std::vector<int> projectiles_; //<! Displays amount of projectiles of each weapon. - managed by setupWeapons()
    144             float timeout_; //<! Timeout for rocket usage. (If a rocket misses, a bot should stop using it.)
    145             void setupWeapons(); //<! Defines which weapons are available for a bot. Is recalled whenever a bot was killed.
    146             bool bSetupWorked; //<! If false, setupWeapons() is called.
    147             int getFiremode(std::string name);
     146                void lookAtTarget(float dt);
     147                void stopLookingAtTarget();
     148                void startLookingAtTarget();
    148149
    149             float randomInRange(float a, float b);
    150             bool bHasTargetPosition_;
    151             Vector3 targetPosition_;
    152             bool bHasTargetOrientation_;
    153             Quaternion targetOrientation_;
     150                bool bLookAtTarget_;       
     151            //----[/Flying functionality]----
     152           
     153            //----[Fighting functionality]----
     154                void maneuver();
     155                void dodge(Vector3& thisPosition, Vector3& diffUnit);
     156                void aimAtTarget();           
     157                bool canFire();
     158                void doFire();
     159                void setClosestTarget();
    154160
    155             Vector3 destination_;
    156             bool bHasDestination;
     161                bool bShooting_;
     162                int maneuverCounter_;         
     163            //----[/Fighting functionality]----           
     164           
     165            //----[where-to-fly information]----
     166                bool bHasTargetPosition_;
     167                Vector3 targetPosition_;
     168                bool bHasTargetOrientation_;
     169                Quaternion targetOrientation_;
     170                // Vector3 destination_;
     171                // bool bHasDestination;
     172            //----[/where-to-fly information]----
     173           
     174            //----[who-to-kill information]----
     175                WeakPtr<ControllableEntity> target_;
     176                //WeakPtr<ControllableEntity> objectiveTarget_;
    157177
     178                bool bHasPositionOfTarget_;
     179                Vector3 positionOfTarget_;
     180                bool bHasOrientationOfTarget_;
     181                Quaternion orientationOfTarget_;
     182            //----[/who-to-kill information]----
    158183
    159             void stopMoving();
    160             void setPositionOfTarget(const Vector3& target);
    161             void setOrientationOfTarget(const Quaternion& orient);
    162             bool bHasPositionOfTarget_;
    163             Vector3 positionOfTarget_;
    164             bool bHasOrientationOfTarget_;
    165             Quaternion orientationOfTarget_;
    166 
    167 
    168             WeakPtr<ControllableEntity> target_;
    169             //WeakPtr<ControllableEntity> thisEntity_;
    170 
    171            
    172 
    173             Action::Value action_;
    174             bool bEngaging_;
    175             bool bShooting_;
    176             WeakPtr<ControllableEntity> objectiveTarget_;
    177 
    178             void lookAtTarget(float dt);
    179             void stopLookingAtTarget();
    180             void startLookingAtTarget();
    181             bool bLookAtTarget_;
    182             void maneuver();
    183             void chooseManeuverType();
    184             void gunsD();
    185             void attack();
    186             void scissors();
    187             FormationMode::Value formationMode_;
    188             Rank::Value rank_;
    189        
    190             bool executingMoveToPoint_;
    191          
    192         private:
    193            
    194                
     184            //----["Private" variables]----
     185                FormationMode::Value formationMode_;
     186                Rank::Value rank_;
     187                Action::Value action_;
     188            //----[/"Private" variables]----               
    195189    };
    196190}
  • code/branches/AI_HS15/src/orxonox/controllers/DivisionController.cc

    r10826 r10832  
    8484    void DivisionController::action()
    8585    {
    86         //----find a target----
    87         if ( !this->hasTarget() )
    88         {
    89             for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    90             {
    91                 if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) )
    92                     continue;           
    93 
    94                
    95                 if (static_cast<ControllableEntity*>(*itP) != (this)->getControllableEntity()
    96                     && CommonController::distance (*itP, this->getControllableEntity())  < 10000)
    97                 {
    98                     (this)->setAction(Action::FIGHT, *itP);
    99                 }   
    100             }
    101         }
     86
    10287       
    10388        if (this->action_ == Action::FIGHT)
    10489        {
    105             //----choose where to go----
    106             this->maneuver();
    107             //----fire if you can----
    108             this->bShooting_ = this->canFire();
    109 
    110             if (this->target_)
    111             {
    112                 if (this->myWingman_)
    113                 {
    114                     //----wingmans shall support the fire of their leaders----
    115                     this->myWingman_->setAction (Action::FIGHT, this->target_);                   
    116                 }
    117                
     90            if (!this->hasTarget())
     91            {
     92                //----find a target----
     93                this->setClosestTarget();
     94            }
     95            else
     96            {
    11897                //----fly in formation if far enough----
    11998                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
     
    121100                {
    122101                    this->setTargetPositionOfWingman();
    123                     //this->setTargetPositionOfFollower();                   
    124                 }   
    125             }
     102                    this->setTargetPositionOfFollower();                   
     103                }
     104                else
     105                {
     106                    //----wingmans shall support the fire of their leaders----
     107                    if (this->myWingman_)
     108                    {
     109                        this->myWingman_->setAction (Action::FIGHT, this->target_);     
     110                    }
     111                    if (this->myFollower_)
     112                    {
     113                        this->myFollower_->setAction (Action::FIGHT);                                   
     114                    }
     115
     116                }
     117               
     118            }
     119            if (this->hasTarget())
     120            {
     121                //----choose where to go----
     122                this->maneuver();
     123                //----fire if you can----
     124                this->bShooting_ = this->canFire();               
     125            }
     126
    126127        }
    127128        else if (this->action_ == Action::FLY)
     
    152153            case FormationMode::FINGER4:
    153154            {
    154                 targetRelativePositionOfWingman = new Vector3 (400, 0, -200); 
     155                targetRelativePositionOfWingman = new Vector3 (400, 0, 200); 
    155156                break;
    156157            }
     
    158159            case FormationMode::DIAMOND:
    159160            {
    160                 targetRelativePositionOfWingman = new Vector3 (400, 0, -200);                 
     161                targetRelativePositionOfWingman = new Vector3 (400, 0, 200);                 
    161162                break;
    162163            }
     
    185186            case FormationMode::FINGER4:
    186187            {
    187                 targetRelativePositionOfFollower = new Vector3 (-400, 0, -200);   
     188                targetRelativePositionOfFollower = new Vector3 (-400, 0, 200);   
    188189                break;
    189190            }
     
    191192            case FormationMode::DIAMOND:
    192193            {
    193                 targetRelativePositionOfFollower = new Vector3 (-400, 0, -200);                   
     194                targetRelativePositionOfFollower = new Vector3 (-400, 0, 200);                   
    194195                break;
    195196            }
  • code/branches/AI_HS15/src/orxonox/controllers/SectionController.cc

    r10826 r10832  
    8989            this->myDivisionLeader_ = newDivisionLeader;
    9090
    91             if (newDivisionLeader)
    92             {
    93                 //orxout(internal_error) << "new DivisionLeader set" << endl;
    94             }
    95             //----If no leader found, attack someone----
    96             //----TODO: find closest enemy----
    97             else
    98             {
    99                 if ( !this->hasTarget() || this->action_ != Action::FIGHT )
    100                 {
    101                     for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    102                     {
    103                         if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) )
    104                             continue;
    105 
    106                         this->setAction(Action::FIGHT, (*itP));
    107                         break;   
    108                     }   
    109                 }
    110                
    111             }
    112 
    11391        }
    11492        //----If have leader----
    11593        else
    11694        {
    117             this->chooseTarget();
    11895        }
    11996
     
    12198        if (this->action_ == Action::FIGHT)
    12299        {
    123             //----choose where to go----
    124             this->maneuver();
    125             //----fire if you can----
    126             this->bShooting_ = this->canFire();
    127 
    128             if (this->target_)
    129             {
    130                 //----wingmans shall support the fire of their leaders----
    131                 if (this->myWingman_)
    132                 {
    133                     this->myWingman_->setAction (Action::FIGHT, this->target_);                   
    134                 }
     100            if (!this->hasTarget())
     101            {
     102                if (this->myDivisionLeader_)
     103                {
     104                    this->chooseTarget();               
     105                }
     106                else
     107                {
     108                    this->setClosestTarget();
     109                }
     110            }
     111            else
     112            {
     113               
    135114                //----fly in formation if far enough----
    136115                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
     
    139118                    this->setTargetPositionOfWingman();
    140119                }   
     120                else
     121                {
     122                    //----wingmans shall support the fire of their leaders----
     123                    if (this->myWingman_)
     124                    {
     125                        this->myWingman_->setAction (Action::FIGHT, this->target_);                   
     126                    }
     127                }
     128            }
     129            if (this->hasTarget())
     130            {
     131                //----choose where to go----
     132                this->maneuver();
     133                //----fire if you can----
     134                this->bShooting_ = this->canFire();               
    141135            }
    142136        }
     
    156150
    157151    }
    158     //PRE: myDivisionLeader_ != 0
     152    //PRE: myDivisionLeader_ != 0 && myDivisionLeader_->action_ == Action::FIGHT
    159153    //POST: this->target_ is set unless division leader doesn't have one
    160154    void SectionController::chooseTarget()
     
    211205
    212206    //----stay in formation----
     207    //gani-TODO: sum targetAbso... and this->predicted position
    213208    void SectionController::setTargetPositionOfWingman()
    214209    {
     
    224219            case FormationMode::FINGER4:
    225220            {
    226                 targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); 
     221                targetRelativePositionOfWingman = new Vector3 (-400, 0, 200); 
    227222                break;
    228223            }
  • code/branches/AI_HS15/src/orxonox/controllers/WingmanController.cc

    r10826 r10832  
    8585            CommonController* newLeader = findNewLeader();
    8686            this->myLeader_ = newLeader;
    87             if (newLeader)
    88             {
    89                 //orxout(internal_error) << "new Leader set" << endl;
    90             }
    91             //----If no leader found, attack someone----
    92             //----TODO: find closest enemy----
    93             else
    94             {
    95                 if ( !this->hasTarget() || this->action_ != Action::FIGHT )
    96                 {
    97                     for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    98                     {
    99                         if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) )
    100                             continue;   
    101                         this->setAction(Action::FIGHT, (*itP));
    102                         break;
    103                     }
    104                 }
    105             }
     87           
    10688        }
    10789        //----If have leader, he will deal with logic----
    10890        else
    10991        {
    110 
     92            this->action_ = this->myLeader_->getAction();
    11193        }
    11294
     
    11597        if (this->action_ == Action::FIGHT)
    11698        {
    117             //----choose where to go----
    118             this->maneuver();
    119             //----fire if you can----
    120             this->bShooting_ = this->canFire();
     99            //----If no leader found, attack someone----
     100            if (!this->hasTarget() && !this->myLeader_)
     101            {
     102                this->setClosestTarget();
     103            }
     104            if (this->hasTarget())
     105            {
     106                //----choose where to go----
     107                this->maneuver();
     108                //----fire if you can----
     109                this->bShooting_ = this->canFire();               
     110            }
    121111        }
    122112        //----action was set to fly, leader handles the logic----
     
    125115
    126116        }
    127         //----TODO: implement protect----
     117        //----gani-TODO: implement protect----
    128118        else if (this->action_ == Action::PROTECT)
    129119        {
Note: See TracChangeset for help on using the changeset viewer.