- Timestamp:
- Jun 3, 2012, 6:05:24 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation2012merge/src/orxonox/controllers/FormationController.cc
r9271 r9272 50 50 { 51 51 52 SetConsoleCommand("FormationController", "formationflight", &FormationController::formationflight);53 SetConsoleCommand("FormationController", "masteraction", &FormationController::masteraction);54 SetConsoleCommand("FormationController", "followme", &FormationController::followme);55 SetConsoleCommand("FormationController", "passivebehaviour", &FormationController::passivebehaviour);56 SetConsoleCommand("FormationController", "formationsize", &FormationController::formationsize);57 58 59 60 61 static const unsigned int STANDARD_MAX_FORMATION_SIZE = 9;62 static const int RADIUS_TO_SEARCH_FOR_MASTERS = 5000;63 static const float FORMATION_LENGTH = 110;64 static const float FORMATION_WIDTH = 110;65 static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy66 static const float SPEED_MASTER = 0.6f;67 static const float ROTATEFACTOR_MASTER = 0.2f;68 static const float SPEED_FREE = 0.8f;69 static const float ROTATEFACTOR_FREE = 0.8f;70 71 FormationController::FormationController(BaseObject* creator) : Controller(creator)72 {52 SetConsoleCommand("FormationController", "formationflight", &FormationController::formationflight); 53 SetConsoleCommand("FormationController", "masteraction", &FormationController::masteraction); 54 SetConsoleCommand("FormationController", "followme", &FormationController::followme); 55 SetConsoleCommand("FormationController", "passivebehaviour", &FormationController::passivebehaviour); 56 SetConsoleCommand("FormationController", "formationsize", &FormationController::formationsize); 57 58 59 60 61 static const unsigned int STANDARD_MAX_FORMATION_SIZE = 9; 62 static const int RADIUS_TO_SEARCH_FOR_MASTERS = 5000; 63 static const float FORMATION_LENGTH = 110; 64 static const float FORMATION_WIDTH = 110; 65 static const int FREEDOM_COUNT = 4; //seconds the slaves in a formation will be set free when master attacks an enemy 66 static const float SPEED_MASTER = 0.6f; 67 static const float ROTATEFACTOR_MASTER = 0.2f; 68 static const float SPEED_FREE = 0.8f; 69 static const float ROTATEFACTOR_FREE = 0.8f; 70 71 FormationController::FormationController(BaseObject* creator) : Controller(creator) 72 { 73 73 RegisterObject(FormationController); 74 74 … … 91 91 this->team_=-1; 92 92 this->target_.setCallback(createFunctor(&FormationController::targetDied, this)); 93 }94 95 FormationController::~FormationController()96 {97 if (this->isInitialized())93 } 94 95 FormationController::~FormationController() 96 { 97 if (this->isInitialized()) 98 98 { 99 99 this->removeFromFormation(); … … 123 123 } 124 124 } 125 }126 127 void FormationController::XMLPort(Element& xmlelement, XMLPort::Mode mode)125 } 126 127 void FormationController::XMLPort(Element& xmlelement, XMLPort::Mode mode) 128 128 { 129 129 SUPER(FormationController, XMLPort, xmlelement, mode); … … 137 137 138 138 139 /**139 /** 140 140 @brief Activates / deactivates formationflight behaviour 141 141 @param form activate formflight if form is true 142 142 */ 143 void FormationController::formationflight(const bool form)143 void FormationController::formationflight(const bool form) 144 144 { 145 145 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) … … 168 168 } 169 169 170 /**170 /** 171 171 @brief Get all masters to do a "specific master action" 172 172 @param action which action to perform (integer, so it can be called with a console command (tmp solution)) … … 198 198 } 199 199 200 /**200 /** 201 201 @brief Sets shooting behaviour of pawns. 202 202 @param passive if true, bots won't shoot. … … 225 225 } 226 226 227 /**227 /** 228 228 @brief Sets maximal formation size 229 229 @param size maximal formation size. … … 282 282 Vector2 coord = get2DViewcoordinates(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); 283 283 float distance = (target - this->getControllableEntity()->getPosition()).length(); 284 285 286 287 288 289 290 284 if(coord.x < 0.0001 && coord.y < 0.0001) 285 { 286 // if the ship reaches a direction very close to the direct one, set it to the direct one 287 Vector3 v_temp = this->getControllableEntity()->getPosition(); 288 Quaternion quat = v_temp.getRotationTo(target); 289 this->getControllableEntity()->rotate(quat); 290 } 291 291 292 292 if(this->state_ == FREE) … … 324 324 325 325 if(this->state_ == SLAVE) 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 } else this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER); 342 343 } else { 344 this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f); 345 } 346 326 { 327 328 this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR_MASTER * coord.x * 2); 329 this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR_MASTER * coord.y * 2); 330 331 if (distance < 300) 332 { 333 if (bHasTargetOrientation_) 334 { 335 copyTargetOrientation(); 336 } 337 if (distance < 100) 338 { //linear speed reduction 339 this->getControllableEntity()->moveFrontBack(distance/100.0f*0.4f*SPEED_MASTER); 340 } 341 else 342 this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER); 343 } 344 else 345 this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f); 346 } 347 347 348 348 if (distance < 10) 349 349 { 350 350 this->positionReached(); 351 352 } 353 } 354 355 356 357 void FormationController::moveToTargetPosition()351 bHasTargetOrientation_=false; 352 } 353 } 354 355 356 357 void FormationController::moveToTargetPosition() 358 358 { 359 359 this->moveToPosition(this->targetPosition_); 360 360 } 361 361 362 //copy the Roll orientation of given Quaternion.363 void FormationController::copyOrientation(const Quaternion& orient)362 //copy the Roll orientation of given Quaternion. 363 void FormationController::copyOrientation(const Quaternion& orient) 364 364 { 365 365 //roll angle difference in radian … … 379 379 380 380 381 /**381 /** 382 382 @brief Unregisters a slave from its master. Initiated by a slave. 383 383 */ … … 469 469 } 470 470 } 471 /** 471 472 /** 472 473 @brief Commands the slaves of a master into a formation. Sufficiently fast not to be called within tick. Initiated by a master. 473 474 */ 474 475 void FormationController::commandSlaves() 475 void FormationController::commandSlaves() 476 476 { 477 477 if(this->state_ != MASTER) return; … … 487 487 } 488 488 else 489 489 // formation: 490 490 { 491 491 dest += 1.0f*orient*WorldEntity::BACK; 492 492 Vector3 pos = Vector3::ZERO; 493 493 bool left=true; 494 494 int i = 1; 495 495 … … 689 689 690 690 if (specificMasterActionHoldCount_ == 0) 691 691 { 692 692 this->specificMasterAction_ = NONE; 693 693 this->searchNewTarget(); 694 } 695 else specificMasterActionHoldCount_--; 694 } 695 else 696 specificMasterActionHoldCount_--; 696 697 } 697 698 … … 717 718 void FormationController::turn180() 718 719 { 719 720 721 722 723 724 720 Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_); 721 722 this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x); 723 this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y); 724 725 this->getControllableEntity()->moveFrontBack(SPEED_MASTER); 725 726 } 726 727 … … 740 741 void FormationController::spin() 741 742 { 742 743 743 this->moveToTargetPosition(); 744 this->getControllableEntity()->rotateRoll(0.8f); 744 745 } 745 746 … … 779 780 if((humanPawn != NULL) && (allMasters.size() != 0)) 780 781 { 781 float posHuman = humanPawn->getPosition().length(); 782 float distance = 0.0f; 783 float minDistance = FLT_MAX; 784 int index = 0; 785 int i = 0; 786 787 for(std::vector<FormationController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++, i++) 788 { 789 if (!FormationController::sameTeam((*it)->getControllableEntity(), humanPawn, (*it)->getGametype())) continue; 790 distance = posHuman - (*it)->getControllableEntity()->getPosition().length(); 791 if(distance < minDistance) index = i; 792 } 793 allMasters[index]->followInit(humanPawn); 794 } 795 796 } 797 798 799 800 782 float posHuman = humanPawn->getPosition().length(); 783 float distance = 0.0f; 784 float minDistance = FLT_MAX; 785 int index = 0; 786 int i = 0; 787 788 for(std::vector<FormationController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++, i++) 789 { 790 if (!FormationController::sameTeam((*it)->getControllableEntity(), humanPawn, (*it)->getGametype())) continue; 791 distance = posHuman - (*it)->getControllableEntity()->getPosition().length(); 792 if(distance < minDistance) index = i; 793 } 794 allMasters[index]->followInit(humanPawn); 795 } 796 } 801 797 802 798 /** … … 820 816 } 821 817 822 /**818 /** 823 819 @brief Master begins to follow a randomly chosen human player of the same team. Is a "specific master action". 824 820 */ … … 848 844 849 845 850 /**846 /** 851 847 @brief Master follows target with adjusted speed. Called within tick. 852 848 */ … … 860 856 861 857 862 void FormationController::setTargetPosition(const Vector3& target)858 void FormationController::setTargetPosition(const Vector3& target) 863 859 { 864 860 this->targetPosition_ = target; … … 924 920 } 925 921 926 void FormationController::forgetTarget()922 void FormationController::forgetTarget() 927 923 { 928 924 this->target_ = 0; … … 930 926 } 931 927 932 void FormationController::targetDied()928 void FormationController::targetDied() 933 929 { 934 930 this->forgetTarget(); … … 936 932 } 937 933 938 bool FormationController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)934 bool FormationController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) 939 935 { 940 936 if (entity1 == entity2)
Note: See TracChangeset
for help on using the changeset viewer.