Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jan 17, 2016, 10:29:21 PM (8 years ago)
Author:
landauf
Message:

merged branch cpp11_v3 back to trunk

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/orxonox/controllers/FormationController.cc

    r11052 r11071  
    5858    RegisterClass(FormationController);
    5959
    60     static const unsigned int STANDARD_MAX_FORMATION_SIZE = 9;
    61     static const int RADIUS_TO_SEARCH_FOR_MASTERS = 5000;
    62     static const float FORMATION_LENGTH =  110;
    63     static const float FORMATION_WIDTH =  110;
    64     static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy
    65     static const float SPEED_MASTER = 0.6f;
    66     static const float ROTATEFACTOR_MASTER = 0.2f;
    67     static const float SPEED_FREE = 0.8f;
    68     static const float ROTATEFACTOR_FREE = 0.8f;
     60    static constexpr unsigned int STANDARD_MAX_FORMATION_SIZE = 9;
     61    static constexpr int RADIUS_TO_SEARCH_FOR_MASTERS = 5000;
     62    static constexpr float FORMATION_LENGTH =  110;
     63    static constexpr float FORMATION_WIDTH =  110;
     64    static constexpr int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy
     65    static constexpr float SPEED_MASTER = 0.6f;
     66    static constexpr float ROTATEFACTOR_MASTER = 0.2f;
     67    static constexpr float SPEED_FREE = 0.8f;
     68    static constexpr float ROTATEFACTOR_FREE = 0.8f;
    6969
    7070    FormationController::FormationController(Context* context) : Controller(context)
     
    7272        RegisterObject(FormationController);
    7373
    74         this->target_ = 0;
     74        this->target_ = nullptr;
    7575        this->formationFlight_ = false;
    7676        this->passive_ = false;
    7777        this->maxFormationSize_ = STANDARD_MAX_FORMATION_SIZE;
    78         this->myMaster_ = 0;
     78        this->myMaster_ = nullptr;
    7979        this->freedomCount_ = 0;
    8080
     
    9797            this->removeFromFormation();
    9898
    99             for (ObjectList<FormationController>::iterator it = ObjectList<FormationController>::begin(); it; ++it)
    100             {
    101                 if (*it != this)
     99            for (FormationController* controller : ObjectList<FormationController>())
     100            {
     101                if (controller != this)
    102102                {
    103                     if (it->myMaster_ == this)
     103                    if (controller->myMaster_ == this)
    104104                    {
    105                         orxout(internal_error) << this << " is still master in " << (*it) << endl;
    106                         it->myMaster_ = 0;
     105                        orxout(internal_error) << this << " is still master in " << controller << endl;
     106                        controller->myMaster_ = nullptr;
    107107                    }
    108108
    109109                    while (true)
    110110                    {
    111                         std::vector<FormationController*>::iterator it2 = std::find(it->slaves_.begin(), it->slaves_.end(), this);
    112                         if (it2 != it->slaves_.end())
     111                        std::vector<FormationController*>::iterator it2 = std::find(controller->slaves_.begin(), controller->slaves_.end(), this);
     112                        if (it2 != controller->slaves_.end())
    113113                        {
    114                             orxout(internal_error) << this << " is still slave in " << (*it) << endl;
    115                             it->slaves_.erase(it2);
     114                            orxout(internal_error) << this << " is still slave in " << controller << endl;
     115                            controller->slaves_.erase(it2);
    116116                        }
    117117                        else
     
    140140    void FormationController::formationflight(const bool form)
    141141    {
    142         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
    143         {
    144             Controller* controller = 0;
    145 
    146             if (it->getController())
    147                 controller = it->getController();
    148             else if (it->getXMLController())
    149                 controller = it->getXMLController();
     142        for (Pawn* pawn : ObjectList<Pawn>())
     143        {
     144            Controller* controller = nullptr;
     145
     146            if (pawn->getController())
     147                controller = pawn->getController();
     148            else if (pawn->getXMLController())
     149                controller = pawn->getXMLController();
    150150
    151151            if (!controller)
     
    171171    void FormationController::masteraction(const int action)
    172172    {
    173         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
    174         {
    175             Controller* controller = 0;
    176 
    177             if (it->getController())
    178                 controller = it->getController();
    179             else if (it->getXMLController())
    180                 controller = it->getXMLController();
     173        for (Pawn* pawn : ObjectList<Pawn>())
     174        {
     175            Controller* controller = nullptr;
     176
     177            if (pawn->getController())
     178                controller = pawn->getController();
     179            else if (pawn->getXMLController())
     180                controller = pawn->getXMLController();
    181181
    182182            if (!controller)
     
    201201    void FormationController::passivebehaviour(const bool passive)
    202202    {
    203         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
    204         {
    205             Controller* controller = 0;
    206 
    207             if (it->getController())
    208                 controller = it->getController();
    209             else if (it->getXMLController())
    210                 controller = it->getXMLController();
     203        for (Pawn* pawn : ObjectList<Pawn>())
     204        {
     205            Controller* controller = nullptr;
     206
     207            if (pawn->getController())
     208                controller = pawn->getController();
     209            else if (pawn->getXMLController())
     210                controller = pawn->getXMLController();
    211211
    212212            if (!controller)
     
    228228    void FormationController::formationsize(const int size)
    229229    {
    230         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
    231         {
    232             Controller* controller = 0;
    233 
    234             if (it->getController())
    235                 controller = it->getController();
    236             else if (it->getXMLController())
    237                 controller = it->getXMLController();
     230        for (Pawn* pawn : ObjectList<Pawn>())
     231        {
     232            Controller* controller = nullptr;
     233
     234            if (pawn->getController())
     235                controller = pawn->getController();
     236            else if (pawn->getXMLController())
     237                controller = pawn->getXMLController();
    238238
    239239            if (!controller)
     
    383383        }
    384384
    385         this->myMaster_ = 0;
     385        this->myMaster_ = nullptr;
    386386        this->state_ = FREE;
    387387    }
     
    398398        int teamSize = 0;
    399399        //go through all pawns
    400         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     400        for (Pawn* pawn : ObjectList<Pawn>())
    401401        {
    402402
     
    405405            if (!gt)
    406406            {
    407                 gt=it->getGametype();
    408             }
    409             if (!FormationController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it),gt))
     407                gt=pawn->getGametype();
     408            }
     409            if (!FormationController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(pawn),gt))
    410410                continue;
    411411
    412412            //has it an FormationController?
    413             Controller* controller = 0;
    414 
    415             if (it->getController())
    416                 controller = it->getController();
    417             else if (it->getXMLController())
    418                 controller = it->getXMLController();
     413            Controller* controller = nullptr;
     414
     415            if (pawn->getController())
     416                controller = pawn->getController();
     417            else if (pawn->getXMLController())
     418                controller = pawn->getXMLController();
    419419
    420420            if (!controller)
     
    422422
    423423            //is pawn oneself?
    424             if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity())
     424            if (orxonox_cast<ControllableEntity*>(pawn) == this->getControllableEntity())
    425425                continue;
    426426
     
    433433                continue;
    434434
    435             float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length();
     435            float distance = (pawn->getPosition() - this->getControllableEntity()->getPosition()).length();
    436436
    437437            // is pawn in range?
     
    440440                if(newMaster->slaves_.size() > this->maxFormationSize_) continue;
    441441
    442                 for(std::vector<FormationController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++)
     442                for(FormationController* slave : this->slaves_)
    443443                {
    444                     (*itSlave)->myMaster_ = newMaster;
    445                     newMaster->slaves_.push_back(*itSlave);
     444                    slave->myMaster_ = newMaster;
     445                    newMaster->slaves_.push_back(slave);
    446446                }
    447447                this->slaves_.clear();
     
    458458        {
    459459            this->state_ = MASTER;
    460             this->myMaster_ = 0;
     460            this->myMaster_ = nullptr;
    461461        }
    462462    }
     
    486486            int i = 1;
    487487
    488             for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
     488            for(FormationController* slave : slaves_)
    489489            {
    490490                pos = Vector3::ZERO;
     
    497497                    dest+=FORMATION_LENGTH*(orient*WorldEntity::BACK);
    498498                }
    499                 (*it)->setTargetOrientation(orient);
    500                 (*it)->setTargetPosition(pos);
     499                slave->setTargetOrientation(orient);
     500                slave->setTargetPosition(pos);
    501501                left=!left;
    502502            }
     
    518518            newMaster->state_ = MASTER;
    519519            newMaster->slaves_ = this->slaves_;
    520             newMaster->myMaster_ = 0;
    521 
    522             for(std::vector<FormationController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
    523             {
    524                 (*it)->myMaster_ = newMaster;
     520            newMaster->myMaster_ = nullptr;
     521
     522            for(FormationController* slave : newMaster->slaves_)
     523            {
     524                slave->myMaster_ = newMaster;
    525525            }
    526526        }
     
    547547                newMaster->state_ = MASTER;
    548548                newMaster->slaves_ = this->slaves_;
    549                 newMaster->myMaster_ = 0;
    550 
    551                 for(std::vector<FormationController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
     549                newMaster->myMaster_ = nullptr;
     550
     551                for(FormationController* slave : newMaster->slaves_)
    552552                {
    553                     (*it)->myMaster_ = newMaster;
     553                    slave->myMaster_ = newMaster;
    554554                }
    555555            }
     
    569569        if(this->state_ != MASTER) return;
    570570
    571         for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
    572         {
    573             (*it)->state_ = FREE;
    574             (*it)->myMaster_ = 0;
     571        for(FormationController* slave : slaves_)
     572        {
     573            slave->state_ = FREE;
     574            slave->myMaster_ = nullptr;
    575575        }
    576576        this->slaves_.clear();
     
    584584        if(this->state_ != MASTER) return;
    585585
    586         for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
    587         {
    588             (*it)->state_ = FREE;
    589             (*it)->forceFreedom();
    590             (*it)->targetPosition_ = this->targetPosition_;
    591             (*it)->bShooting_ = true;
     586        for(FormationController* slave : slaves_)
     587        {
     588            slave->state_ = FREE;
     589            slave->forceFreedom();
     590            slave->targetPosition_ = this->targetPosition_;
     591            slave->bShooting_ = true;
    592592//             (*it)->getControllableEntity()->fire(0);// fire once for fun
    593593        }
     
    629629
    630630        //search new Master, then take lead
    631         if (this->state_==FREE && this->myMaster_==0)
     631        if (this->state_==FREE && this->myMaster_==nullptr)
    632632        {
    633633          searchNewMaster();
     
    650650            this->slaves_.push_back(this->myMaster_);
    651651            //set this as new master
    652             for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
    653             {
    654                  (*it)->myMaster_=this;
    655             }
    656             this->myMaster_=0;
     652            for(FormationController* slave : slaves_)
     653            {
     654                 slave->myMaster_=this;
     655            }
     656            this->myMaster_=nullptr;
    657657            this->state_=MASTER;
    658658        }
     
    694694        if (this->state_ == MASTER)
    695695        {
    696             for(std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)
    697             {
    698                  (*it)->formationMode_ = val;
     696            for(FormationController* slave : slaves_)
     697            {
     698                 slave->formationMode_ = val;
    699699                 if (val == ATTACK)
    700                      (*it)->forgetTarget();
     700                     slave->forgetTarget();
    701701            }
    702702        }
     
    773773    {
    774774
    775         Pawn *humanPawn = NULL;
    776         NewHumanController *currentHumanController = NULL;
     775        Pawn *humanPawn = nullptr;
     776        NewHumanController *currentHumanController = nullptr;
    777777        std::vector<FormationController*> allMasters;
    778778
    779         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
    780         {
    781             Controller* controller = 0;
    782 
    783             if (it->getController())
    784                 controller = it->getController();
    785             else if (it->getXMLController())
    786                 controller = it->getXMLController();
     779        for (Pawn* pawn : ObjectList<Pawn>())
     780        {
     781            Controller* controller = nullptr;
     782
     783            if (pawn->getController())
     784                controller = pawn->getController();
     785            else if (pawn->getXMLController())
     786                controller = pawn->getXMLController();
    787787
    788788            if (!controller)
     
    791791            currentHumanController = orxonox_cast<NewHumanController*>(controller);
    792792
    793             if(currentHumanController) humanPawn = *it;
     793            if(currentHumanController) humanPawn = pawn;
    794794
    795795            FormationController *aiController = orxonox_cast<FormationController*>(controller);
     
    800800        }
    801801
    802         if((humanPawn != NULL) && (allMasters.size() != 0))
     802        if((humanPawn != nullptr) && (allMasters.size() != 0))
    803803        {
    804804            float posHuman = humanPawn->getPosition().length();
     
    808808            int i = 0;
    809809
    810             for(std::vector<FormationController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++, i++)
    811             {
    812                 if (!FormationController::sameTeam((*it)->getControllableEntity(), humanPawn, (*it)->getGametype())) continue;
    813                 distance = posHuman - (*it)->getControllableEntity()->getPosition().length();
     810            for(FormationController* master : allMasters)
     811            {
     812                if (!FormationController::sameTeam(master->getControllableEntity(), humanPawn, master->getGametype())) continue;
     813                distance = posHuman - master->getControllableEntity()->getPosition().length();
    814814                if(distance < minDistance) index = i;
     815                i++;
    815816            }
    816817            allMasters[index]->followInit(humanPawn);
     
    826827    void FormationController::followInit(Pawn* pawn, const bool always, const int secondsToFollow)
    827828    {
    828         if (pawn == NULL || this->state_ != MASTER)
     829        if (pawn == nullptr || this->state_ != MASTER)
    829830            return;
    830831        this->specificMasterAction_  =  FOLLOW;
     
    844845    {
    845846
    846         Pawn *humanPawn = NULL;
    847         NewHumanController *currentHumanController = NULL;
    848 
    849         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
    850         {
    851             if (!it->getController())
     847        Pawn *humanPawn = nullptr;
     848        NewHumanController *currentHumanController = nullptr;
     849
     850        for (Pawn* pawn : ObjectList<Pawn>())
     851        {
     852            if (!pawn->getController())
    852853                continue;
    853854
    854             currentHumanController = orxonox_cast<NewHumanController*>(it->getController());
     855            currentHumanController = orxonox_cast<NewHumanController*>(pawn->getController());
    855856            if(currentHumanController)
    856857            {
    857                 if (!FormationController::sameTeam(this->getControllableEntity(), *it, this->getGametype())) continue;
    858                 humanPawn = *it;
     858                if (!FormationController::sameTeam(this->getControllableEntity(), pawn, this->getGametype())) continue;
     859                humanPawn = pawn;
    859860                break;
    860861            }
    861862        }
    862863
    863         if((humanPawn != NULL))
     864        if((humanPawn != nullptr))
    864865                this->followInit(humanPawn);
    865866    }
     
    918919        this->forgetTarget();
    919920
    920         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
    921         {
    922             if (FormationController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
     921        for (Pawn* pawn : ObjectList<Pawn>())
     922        {
     923            if (FormationController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(pawn), this->getGametype()))
    923924                continue;
    924925
    925926            /* So AI won't choose invisible Spaceships as target */
    926             if (!it->getRadarVisibility())
     927            if (!pawn->getRadarVisibility())
    927928                continue;
    928929
    929             if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
     930            if (static_cast<ControllableEntity*>(pawn) != this->getControllableEntity())
    930931            {
    931932                float speed = this->getControllableEntity()->getVelocity().length();
    932933                Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition();
    933                 Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition();
    934                 if (!this->target_ || it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / math::twoPi)
     934                Vector3 distanceNew = pawn->getPosition() - this->getControllableEntity()->getPosition();
     935                if (!this->target_ || pawn->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / math::twoPi)
    935936                        < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / math::twoPi) + rnd(-250, 250))
    936937                {
    937                     this->setTarget(*it);
     938                    this->setTarget(pawn);
    938939                }
    939940            }
     
    943944    void FormationController::forgetTarget()
    944945    {
    945         this->target_ = 0;
     946        this->target_ = nullptr;
    946947        this->bShooting_ = false;
    947948    }
     
    963964        int team2 = entity2->getTeam();
    964965
    965         Controller* controller = 0;
     966        Controller* controller = nullptr;
    966967        if (entity1->getController())
    967968            controller = entity1->getController();
     
    10001001        }
    10011002
    1002         TeamBaseMatchBase* base = 0;
     1003        TeamBaseMatchBase* base = nullptr;
    10031004        base = orxonox_cast<TeamBaseMatchBase*>(entity1);
    10041005        if (base)
     
    10341035        }
    10351036
    1036         DroneController* droneController = 0;
     1037        DroneController* droneController = nullptr;
    10371038        droneController = orxonox_cast<DroneController*>(entity1->getController());
    10381039        if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2)
Note: See TracChangeset for help on using the changeset viewer.