Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 22, 2015, 5:06:38 PM (8 years ago)
Author:
gania
Message:

some comments added

File:
1 edited

Legend:

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

    r10805 r10826  
    3434    RegisterClass(SectionController);
    3535
     36    //Leaders share the fact that they have Wingmans
    3637    SectionController::SectionController(Context* context) : LeaderController(context)
    3738    {
     
    4445        this->rank_ = Rank::SECTIONLEADER;
    4546
    46         orxout(internal_error) << this << "Was created" << endl;
     47        //orxout(internal_error) << this << "Was created" << endl;
    4748
    4849    }
     
    5253       
    5354    }
    54 
     55    void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
     56    {
     57        SUPER(SectionController, XMLPort, xmlelement, mode);
     58
     59        //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
     60    }
     61
     62    //----in tick, move (or look) and shoot----
    5563    void SectionController::tick(float dt)
    5664    {
     
    7583    void SectionController::action()
    7684    {
    77         //this->target_ = this->sectionTarget_;       
     85        //----If no leader, find one---- 
    7886        if (!myDivisionLeader_)
    7987        {
    8088            LeaderController* newDivisionLeader = findNewDivisionLeader();
    8189            this->myDivisionLeader_ = newDivisionLeader;
     90
    8291            if (newDivisionLeader)
    83                 orxout(internal_error) << "new DivisionLeader set" << endl;
     92            {
     93                //orxout(internal_error) << "new DivisionLeader set" << endl;
     94            }
     95            //----If no leader found, attack someone----
     96            //----TODO: find closest enemy----
    8497            else
    8598            {
    86                 for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    87                 {
    88                     if (this->getControllableEntity()->getTeam() == static_cast<ControllableEntity*>(*itP)->getTeam())
    89                         continue;           
    90 
    91                     if (!this->myDivisionLeader_)
     99                if ( !this->hasTarget() || this->action_ != Action::FIGHT )
     100                {
     101                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    92102                    {
     103                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) )
     104                            continue;
     105
    93106                        this->setAction(Action::FIGHT, (*itP));
    94                         break;
     107                        break;   
     108                    }   
     109                }
     110               
     111            }
     112
     113        }
     114        //----If have leader----
     115        else
     116        {
     117            this->chooseTarget();
     118        }
     119
     120        //----action was set to fight----
     121        if (this->action_ == Action::FIGHT)
     122        {
     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                }
     135                //----fly in formation if far enough----
     136                Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
     137                if (diffVector.length() > 3000)
     138                {
     139                    this->setTargetPositionOfWingman();
     140                }   
     141            }
     142        }
     143
     144        //----action was set to fly----
     145        else if (this->action_ == Action::FLY)
     146        {
     147            this->setTargetPositionOfWingman();
     148        }
     149
     150        //----action was set to protect----
     151        else if (this->action_ == Action::PROTECT)
     152        {
     153
     154        }
     155               
     156
     157    }
     158    //PRE: myDivisionLeader_ != 0
     159    //POST: this->target_ is set unless division leader doesn't have one
     160    void SectionController::chooseTarget()
     161    {
     162        //----If division leader fights, cover him by fighting emenies close to his target----
     163        if (this->myDivisionLeader_->getAction() == Action::FIGHT)
     164        {
     165            //----if he has a target----
     166            if (this->myDivisionLeader_->hasTarget())
     167            {
     168                //----try to find a new target if division leader has wingman (doing fine) and no good target already set----
     169                if ( this->myDivisionLeader_->hasWingman() &&
     170                    !( this->hasTarget() && this->getTarget() != this->myDivisionLeader_->getTarget() ) )
     171                {
     172
     173                    bool foundTarget = false;
     174                    //----new target should be close to division's target----
     175                    Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition();
     176                   
     177                    for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
     178                    {
     179                        //----is enemy?----
     180                        if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP)) )
     181                            continue;           
     182                        //----in range?----
     183                        if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 &&
     184                            (*itP) != this->myDivisionLeader_->getTarget())
     185                        {
     186                            foundTarget = true;
     187                            this->setAction(Action::FIGHT, (*itP));
     188                            //orxout(internal_error) << "Found target" << endl;
     189                            break;
     190                        }
    95191                    }
    96                    
    97                 }
    98             }
    99 
    100         }
    101         else
    102         {
    103             if (this->myDivisionLeader_->getAction() == Action::FIGHT)
    104             {
    105                 if (this->myDivisionLeader_->hasTarget())
    106                 {
    107                     if (this->myDivisionLeader_->hasWingman() && (!this->hasTarget() || this->getTarget() == this->myDivisionLeader_->getTarget()))
     192                    //----no target? then attack same target as division leader----
     193                    if (!foundTarget)
    108194                    {
    109                         bool foundTarget = false;
    110                         Vector3 divisionTargetPosition = this->myDivisionLeader_->getTarget()->getWorldPosition();
    111                         for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP)
    112                         {
    113                             if (this->getControllableEntity()->getTeam() == static_cast<ControllableEntity*>(*itP)->getTeam())
    114                                 continue;           
    115 
    116                             if (((*itP)->getWorldPosition() - divisionTargetPosition).length() < 3000 &&
    117                                 ((*itP)->getWorldPosition() - divisionTargetPosition).length() > 1000 &&
    118                                 (*itP) != this->myDivisionLeader_->getTarget())
    119                             {
    120                                 foundTarget = true;
    121                                 this->setAction(Action::FIGHT, (*itP));
    122                                 orxout(internal_error) << "Found target" << endl;
    123                                 break;
    124                             }
    125                         }
    126                         if (!foundTarget)
    127                         {
    128                             this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget());
    129                         }
    130 
     195                        this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget());
    131196                    }
    132197                }
    133             }   
    134         }
    135        
    136         if (this->action_ == Action::FIGHT)
    137         {
    138             this->maneuver();
    139             this->bShooting_ = this->canFire();
    140             if (this->target_)
    141             {
    142                 if (this->myWingman_)
    143                 {
    144                     this->myWingman_->setAction (Action::FIGHT, this->target_);                   
     198                //----if division leader doesn't have a wingman, support his fire----
     199                else
     200                {
     201                    this->setAction(Action::FIGHT, this->myDivisionLeader_->getTarget());
    145202                }
    146                 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition();         
    147                 if (diffVector.length() > 3000)
    148                 {
    149                     this->setTargetPositionOfWingman();
    150                 }   
    151             }
    152         }
    153         else if (this->action_ == Action::FLY)
    154         {
    155             this->setTargetPositionOfWingman();
    156         }
    157         else if (this->action_ == Action::PROTECT)
    158         {
    159 
    160         }
    161                
    162 
    163     }
    164    
     203            }
     204            //----If he fights but doesn't have a target, wait for him to get one----
     205            else
     206            {
     207
     208            }
     209        }
     210    }
     211
     212    //----stay in formation----
    165213    void SectionController::setTargetPositionOfWingman()
    166214    {
     
    177225            {
    178226                targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); 
    179                 break;
    180             }
    181             case FormationMode::VEE:
    182             {
    183227                break;
    184228            }
     
    197241       
    198242    }
     243
    199244    LeaderController* SectionController::findNewDivisionLeader()
    200245    {
     
    219264                continue;
    220265
    221 
    222             float distance = ((it)->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length();
     266            float distance = CommonController::distance (it->getControllableEntity(), this->getControllableEntity());
    223267           
    224268            if (distance < minDistance && !(it->hasFollower()))
     
    260304    }
    261305
    262     void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    263     {
    264         SUPER(SectionController, XMLPort, xmlelement, mode);
    265 
    266         //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f);
    267     }
    268 
     306   
    269307   
    270308   
Note: See TracChangeset for help on using the changeset viewer.