- Timestamp:
- Jan 17, 2016, 10:29:21 PM (9 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/orxonox/controllers/FormationController.cc
r11052 r11071 58 58 RegisterClass(FormationController); 59 59 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 enemy65 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; 69 69 70 70 FormationController::FormationController(Context* context) : Controller(context) … … 72 72 RegisterObject(FormationController); 73 73 74 this->target_ = 0;74 this->target_ = nullptr; 75 75 this->formationFlight_ = false; 76 76 this->passive_ = false; 77 77 this->maxFormationSize_ = STANDARD_MAX_FORMATION_SIZE; 78 this->myMaster_ = 0;78 this->myMaster_ = nullptr; 79 79 this->freedomCount_ = 0; 80 80 … … 97 97 this->removeFromFormation(); 98 98 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) 102 102 { 103 if ( it->myMaster_ == this)103 if (controller->myMaster_ == this) 104 104 { 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; 107 107 } 108 108 109 109 while (true) 110 110 { 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()) 113 113 { 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); 116 116 } 117 117 else … … 140 140 void FormationController::formationflight(const bool form) 141 141 { 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(); 150 150 151 151 if (!controller) … … 171 171 void FormationController::masteraction(const int action) 172 172 { 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(); 181 181 182 182 if (!controller) … … 201 201 void FormationController::passivebehaviour(const bool passive) 202 202 { 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(); 211 211 212 212 if (!controller) … … 228 228 void FormationController::formationsize(const int size) 229 229 { 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(); 238 238 239 239 if (!controller) … … 383 383 } 384 384 385 this->myMaster_ = 0;385 this->myMaster_ = nullptr; 386 386 this->state_ = FREE; 387 387 } … … 398 398 int teamSize = 0; 399 399 //go through all pawns 400 for ( ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it)400 for (Pawn* pawn : ObjectList<Pawn>()) 401 401 { 402 402 … … 405 405 if (!gt) 406 406 { 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)) 410 410 continue; 411 411 412 412 //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(); 419 419 420 420 if (!controller) … … 422 422 423 423 //is pawn oneself? 424 if (orxonox_cast<ControllableEntity*>( *it) == this->getControllableEntity())424 if (orxonox_cast<ControllableEntity*>(pawn) == this->getControllableEntity()) 425 425 continue; 426 426 … … 433 433 continue; 434 434 435 float distance = ( it->getPosition() - this->getControllableEntity()->getPosition()).length();435 float distance = (pawn->getPosition() - this->getControllableEntity()->getPosition()).length(); 436 436 437 437 // is pawn in range? … … 440 440 if(newMaster->slaves_.size() > this->maxFormationSize_) continue; 441 441 442 for( std::vector<FormationController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++)442 for(FormationController* slave : this->slaves_) 443 443 { 444 (*itSlave)->myMaster_ = newMaster;445 newMaster->slaves_.push_back( *itSlave);444 slave->myMaster_ = newMaster; 445 newMaster->slaves_.push_back(slave); 446 446 } 447 447 this->slaves_.clear(); … … 458 458 { 459 459 this->state_ = MASTER; 460 this->myMaster_ = 0;460 this->myMaster_ = nullptr; 461 461 } 462 462 } … … 486 486 int i = 1; 487 487 488 for( std::vector<FormationController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++)488 for(FormationController* slave : slaves_) 489 489 { 490 490 pos = Vector3::ZERO; … … 497 497 dest+=FORMATION_LENGTH*(orient*WorldEntity::BACK); 498 498 } 499 (*it)->setTargetOrientation(orient);500 (*it)->setTargetPosition(pos);499 slave->setTargetOrientation(orient); 500 slave->setTargetPosition(pos); 501 501 left=!left; 502 502 } … … 518 518 newMaster->state_ = MASTER; 519 519 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; 525 525 } 526 526 } … … 547 547 newMaster->state_ = MASTER; 548 548 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_) 552 552 { 553 (*it)->myMaster_ = newMaster;553 slave->myMaster_ = newMaster; 554 554 } 555 555 } … … 569 569 if(this->state_ != MASTER) return; 570 570 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; 575 575 } 576 576 this->slaves_.clear(); … … 584 584 if(this->state_ != MASTER) return; 585 585 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; 592 592 // (*it)->getControllableEntity()->fire(0);// fire once for fun 593 593 } … … 629 629 630 630 //search new Master, then take lead 631 if (this->state_==FREE && this->myMaster_== 0)631 if (this->state_==FREE && this->myMaster_==nullptr) 632 632 { 633 633 searchNewMaster(); … … 650 650 this->slaves_.push_back(this->myMaster_); 651 651 //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; 657 657 this->state_=MASTER; 658 658 } … … 694 694 if (this->state_ == MASTER) 695 695 { 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; 699 699 if (val == ATTACK) 700 (*it)->forgetTarget();700 slave->forgetTarget(); 701 701 } 702 702 } … … 773 773 { 774 774 775 Pawn *humanPawn = NULL;776 NewHumanController *currentHumanController = NULL;775 Pawn *humanPawn = nullptr; 776 NewHumanController *currentHumanController = nullptr; 777 777 std::vector<FormationController*> allMasters; 778 778 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(); 787 787 788 788 if (!controller) … … 791 791 currentHumanController = orxonox_cast<NewHumanController*>(controller); 792 792 793 if(currentHumanController) humanPawn = *it;793 if(currentHumanController) humanPawn = pawn; 794 794 795 795 FormationController *aiController = orxonox_cast<FormationController*>(controller); … … 800 800 } 801 801 802 if((humanPawn != NULL) && (allMasters.size() != 0))802 if((humanPawn != nullptr) && (allMasters.size() != 0)) 803 803 { 804 804 float posHuman = humanPawn->getPosition().length(); … … 808 808 int i = 0; 809 809 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(); 814 814 if(distance < minDistance) index = i; 815 i++; 815 816 } 816 817 allMasters[index]->followInit(humanPawn); … … 826 827 void FormationController::followInit(Pawn* pawn, const bool always, const int secondsToFollow) 827 828 { 828 if (pawn == NULL|| this->state_ != MASTER)829 if (pawn == nullptr || this->state_ != MASTER) 829 830 return; 830 831 this->specificMasterAction_ = FOLLOW; … … 844 845 { 845 846 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()) 852 853 continue; 853 854 854 currentHumanController = orxonox_cast<NewHumanController*>( it->getController());855 currentHumanController = orxonox_cast<NewHumanController*>(pawn->getController()); 855 856 if(currentHumanController) 856 857 { 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; 859 860 break; 860 861 } 861 862 } 862 863 863 if((humanPawn != NULL))864 if((humanPawn != nullptr)) 864 865 this->followInit(humanPawn); 865 866 } … … 918 919 this->forgetTarget(); 919 920 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())) 923 924 continue; 924 925 925 926 /* So AI won't choose invisible Spaceships as target */ 926 if (! it->getRadarVisibility())927 if (!pawn->getRadarVisibility()) 927 928 continue; 928 929 929 if (static_cast<ControllableEntity*>( *it) != this->getControllableEntity())930 if (static_cast<ControllableEntity*>(pawn) != this->getControllableEntity()) 930 931 { 931 932 float speed = this->getControllableEntity()->getVelocity().length(); 932 933 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) 935 936 < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / math::twoPi) + rnd(-250, 250)) 936 937 { 937 this->setTarget( *it);938 this->setTarget(pawn); 938 939 } 939 940 } … … 943 944 void FormationController::forgetTarget() 944 945 { 945 this->target_ = 0;946 this->target_ = nullptr; 946 947 this->bShooting_ = false; 947 948 } … … 963 964 int team2 = entity2->getTeam(); 964 965 965 Controller* controller = 0;966 Controller* controller = nullptr; 966 967 if (entity1->getController()) 967 968 controller = entity1->getController(); … … 1000 1001 } 1001 1002 1002 TeamBaseMatchBase* base = 0;1003 TeamBaseMatchBase* base = nullptr; 1003 1004 base = orxonox_cast<TeamBaseMatchBase*>(entity1); 1004 1005 if (base) … … 1034 1035 } 1035 1036 1036 DroneController* droneController = 0;1037 DroneController* droneController = nullptr; 1037 1038 droneController = orxonox_cast<DroneController*>(entity1->getController()); 1038 1039 if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2)
Note: See TracChangeset
for help on using the changeset viewer.