Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 5, 2015, 10:47:51 PM (8 years ago)
Author:
landauf
Message:

use range-based for-loop where it makes sense (e.g. ObjectList)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/cpp11_v2/src/orxonox/controllers/FormationController.cc

    r10916 r10919  
    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_ = nullptr;
     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)
     142        for (Pawn* pawn : ObjectList<Pawn>())
    143143        {
    144144            Controller* controller = nullptr;
    145145
    146             if (it->getController())
    147                 controller = it->getController();
    148             else if (it->getXMLController())
    149                 controller = it->getXMLController();
     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)
     173        for (Pawn* pawn : ObjectList<Pawn>())
    174174        {
    175175            Controller* controller = nullptr;
    176176
    177             if (it->getController())
    178                 controller = it->getController();
    179             else if (it->getXMLController())
    180                 controller = it->getXMLController();
     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)
     203        for (Pawn* pawn : ObjectList<Pawn>())
    204204        {
    205205            Controller* controller = nullptr;
    206206
    207             if (it->getController())
    208                 controller = it->getController();
    209             else if (it->getXMLController())
    210                 controller = it->getXMLController();
     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)
     230        for (Pawn* pawn : ObjectList<Pawn>())
    231231        {
    232232            Controller* controller = nullptr;
    233233
    234             if (it->getController())
    235                 controller = it->getController();
    236             else if (it->getXMLController())
    237                 controller = it->getXMLController();
     234            if (pawn->getController())
     235                controller = pawn->getController();
     236            else if (pawn->getXMLController())
     237                controller = pawn->getXMLController();
    238238
    239239            if (!controller)
     
    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
     
    413413            Controller* controller = nullptr;
    414414
    415             if (it->getController())
    416                 controller = it->getController();
    417             else if (it->getXMLController())
    418                 controller = it->getXMLController();
     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?
     
    520520            newMaster->myMaster_ = nullptr;
    521521
    522             for(std::vector<FormationController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
    523             {
    524                 (*it)->myMaster_ = newMaster;
     522            for(FormationController* slave : newMaster->slaves_)
     523            {
     524                slave->myMaster_ = newMaster;
    525525            }
    526526        }
     
    549549                newMaster->myMaster_ = nullptr;
    550550
    551                 for(std::vector<FormationController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++)
     551                for(FormationController* slave : newMaster->slaves_)
    552552                {
    553                     (*it)->myMaster_ = newMaster;
     553                    slave->myMaster_ = newMaster;
    554554                }
    555555            }
     
    777777        std::vector<FormationController*> allMasters;
    778778
    779         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
     779        for (Pawn* pawn : ObjectList<Pawn>())
    780780        {
    781781            Controller* controller = nullptr;
    782782
    783             if (it->getController())
    784                 controller = it->getController();
    785             else if (it->getXMLController())
    786                 controller = it->getXMLController();
     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);
     
    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);
     
    847848        NewHumanController *currentHumanController = nullptr;
    848849
    849         for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)
    850         {
    851             if (!it->getController())
    852                 continue;
    853 
    854             currentHumanController = orxonox_cast<NewHumanController*>(it->getController());
     850        for (Pawn* pawn : ObjectList<Pawn>())
     851        {
     852            if (!pawn->getController())
     853                continue;
     854
     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            }
     
    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                 continue;
    928 
    929             if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity())
     927            if (!pawn->getRadarVisibility())
     928                continue;
     929
     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            }
Note: See TracChangeset for help on using the changeset viewer.