Changeset 7066
- Timestamp:
- May 31, 2010, 6:18:50 PM (14 years ago)
- Location:
- code/branches/presentation3/src/orxonox/controllers
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation3/src/orxonox/controllers/AIController.cc
r7034 r7066 127 127 128 128 if (this->specificMasterAction_ != NONE) 129 {130 129 this->specificMasterActionHold(); 131 132 // if (this->specificMasterAction_ == TURN180)133 // this->turn180Init();134 135 // if (this->specificMasterAction_ == SPIN)136 // this->spinInit();137 138 // if (this->specificMasterAction_ == FOLLOWHUMAN)139 // this->followHuman(this->HumanToFollow_, false);140 }141 130 142 131 else { … … 151 140 if (random < 5) 152 141 this->spinInit(); 142 143 // follow a randomly chosen human - a specific Master Action 144 random = rnd(1000.0f); 145 if (random < 1) 146 this->followRandomHumanInit(); 153 147 154 148 // lose master status (only if less than 4 slaves in formation) … … 230 224 if (this->specificMasterAction_ == SPIN) 231 225 this->spin(); 232 if (this->specificMasterAction_ == FOLLOW HUMAN)226 if (this->specificMasterAction_ == FOLLOW) 233 227 this->follow(); 234 228 } -
code/branches/presentation3/src/orxonox/controllers/ArtificialController.cc
r7064 r7066 54 54 55 55 static const unsigned int STANDARD_MAX_FORMATION_SIZE = 7; 56 static const int RADIUS_TO_SEARCH_FOR_MASTERS = 5000; 56 57 static const int FORMATION_LENGTH = 130; 57 58 static const int FORMATION_WIDTH = 110; … … 61 62 static const float SPEED_FREE = 0.8f; 62 63 static const float ROTATEFACTOR_FREE = 0.8f; 63 static const int SECONDS_TO_FOLLOW_HUMAN = 100; 64 64 65 65 66 ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator) … … 79 80 this->bShooting_ = false; 80 81 this->bHasTargetPosition_ = false; 82 this->speedCounter_ = 0.2f; 81 83 this->targetPosition_ = Vector3::ZERO; 82 this->humanToFollow_ = NULL;83 84 84 85 this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this)); … … 94 95 95 96 XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(-1); 96 XMLPortParam(ArtificialController, "formation flight", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(true);97 XMLPortParam(ArtificialController, "formation _size", setFormationSize, getFormationSize, xmlelement, mode).defaultValues(STANDARD_MAX_FORMATION_SIZE);97 XMLPortParam(ArtificialController, "formationFlight", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(true); 98 XMLPortParam(ArtificialController, "formationSize", setFormationSize, getFormationSize, xmlelement, mode).defaultValues(STANDARD_MAX_FORMATION_SIZE); 98 99 } 99 100 … … 104 105 @param form activate formflight if form is true 105 106 */ 106 void ArtificialController::formationflight( bool form)107 void ArtificialController::formationflight(const bool form) 107 108 { 108 109 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) … … 129 130 @param action which action to perform (integer, so it can be called with a console command (tmp solution)) 130 131 */ 131 void ArtificialController::masteraction( int action)132 void ArtificialController::masteraction(const int action) 132 133 { 133 134 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) … … 149 150 150 151 /** 151 @brief A human player gets followed by its nearest master. Initiated by console command, intended for demonstration puproses. Does not work at the moment.152 @brief A human player gets followed by its nearest master. Initiated by console command, so far intended for demonstration puproses (possible future pickup). 152 153 */ 153 154 void ArtificialController::followme() … … 169 170 ArtificialController *aiController = orxonox_cast<ArtificialController*>(it->getController()); 170 171 171 if(aiController ||aiController->state_ == MASTER)172 if(aiController && aiController->state_ == MASTER) 172 173 allMasters.push_back(aiController); 173 174 … … 182 183 int i = 0; 183 184 184 for(std::vector<ArtificialController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++ )185 for(std::vector<ArtificialController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++, i++) 185 186 { 187 if (!ArtificialController::sameTeam((*it)->getControllableEntity(), humanPawn, (*it)->getGametype())) continue; 186 188 distance = posHuman - (*it)->getControllableEntity()->getPosition().length(); 187 189 if(distance < minDistance) index = i; 188 190 } 189 allMasters[index]->humanToFollow_ = humanPawn; 190 // allMasters[index]->followHuman(humanPawn, false); 191 allMasters[index]->followInit(humanPawn); 191 192 } 192 193 … … 197 198 @param passive if true, bots won't shoot. 198 199 */ 199 void ArtificialController::passivebehaviour( bool passive)200 void ArtificialController::passivebehaviour(const bool passive) 200 201 { 201 202 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) … … 218 219 @param size maximal formation size. 219 220 */ 220 void ArtificialController::formationsize( int size)221 void ArtificialController::formationsize(const int size) 221 222 { 222 223 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) … … 242 243 { 243 244 if (this->state_ == SLAVE) unregisterSlave(); 244 245 if (this->state_ == MASTER) setNewMasterWithinFormation(); 245 246 this->slaves_.clear(); 246 247 this->state_ = FREE; 248 this->specificMasterAction_ = NONE; 247 249 248 250 } … … 255 257 return; 256 258 259 // Slave uses special movement if its master is in FOLLOW mode 260 if(this->state_ == SLAVE && this->myMaster_ && this->myMaster_->specificMasterAction_ == FOLLOW) 261 { 262 // this->followForSlaves(target); 263 // return; 264 } 265 257 266 Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); 258 267 float distance = (target - this->getControllableEntity()->getPosition()).length(); … … 263 272 if (this->target_ || distance > 10) 264 273 { 265 // Multiply with 0.8to make them a bit slower274 // Multiply with ROTATEFACTOR_FREE to make them a bit slower 266 275 this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x*coord.x); 267 276 this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y*coord.y); … … 316 325 } 317 326 318 int ArtificialController::getState() 319 { 320 return this->state_; 321 } 322 323 /** 324 @brief Unregisters a slave from its master. Called by a slave. 327 328 /** 329 @brief Unregisters a slave from its master. Initiated by a slave. 325 330 */ 326 331 void ArtificialController::unregisterSlave() { … … 363 368 364 369 //is it a master? 365 if (!newMaster || newMaster-> getState()!= MASTER)370 if (!newMaster || newMaster->state_ != MASTER) 366 371 continue; 367 372 … … 369 374 370 375 // is pawn in range? 371 if (distance < 5000)376 if (distance < RADIUS_TO_SEARCH_FOR_MASTERS) 372 377 { 373 378 if(newMaster->slaves_.size() > this->maxFormationSize_) continue; … … 393 398 394 399 /** 395 @brief Commands the slaves of a master into a formation. Called by a master.400 @brief Commands the slaves of a master into a formation. Sufficiently fast not to be called within tick. Initiated by a master. 396 401 */ 397 402 void ArtificialController::commandSlaves() … … 452 457 453 458 this->slaves_.clear(); 459 this->specificMasterAction_ = NONE; 454 460 this->state_ = SLAVE; 455 461 this->myMaster_ = newMaster; … … 463 469 464 470 /** 465 @brief Frees all slaves form a master. Called by a master.471 @brief Frees all slaves form a master. Initiated by a master. 466 472 */ 467 473 void ArtificialController::freeSlaves() … … 538 544 void ArtificialController::turn180Init() 539 545 { 540 COUT(0) << "~turnInit" << std::endl;541 546 if(this->state_ != MASTER) return; 542 547 … … 564 569 565 570 /** 566 @brief Master initializes a spin around its looking direction axis. Leads to a "specific master action". Not yet implemented.571 @brief Master initializes a spin around its looking direction axis. Leads to a "specific master action". 567 572 */ 568 573 void ArtificialController::spinInit() 569 574 { 570 COUT(0) << "~spinInit" << std::endl;571 575 if(this->state_ != MASTER) return; 572 576 this->specificMasterAction_ = SPIN; … … 584 588 585 589 /** 586 @brief Master begins to follow a human player. Is a "specific master action".587 @param humanController human to follow.588 @param alaways follows human forever if true, else it follows it for @var SECONDS_TO_FOLLOW_HUMAN seconds.589 */590 void ArtificialController::followHumanInit(Pawn* human, bool always)591 {592 COUT(0) << "~followInit" << std::endl;593 if ( human == NULL || this->state_ != MASTER)590 @brief Master begins to follow a pawn. Is a "specific master action". 591 @param pawn pawn to follow. 592 @param alaways follows pawn forever if true (false if omitted). 593 @param secondsToFollow seconds to follow the pawn if always is false. Will follow pawn 100 seconds if omitted (set in header). 594 */ 595 void ArtificialController::followInit(Pawn* pawn, const bool always, const int secondsToFollow) 596 { 597 if (pawn == NULL || this->state_ != MASTER) 594 598 return; 595 596 this->specificMasterAction_ = FOLLOWHUMAN; 597 598 this->setTarget(human); 599 this->specificMasterAction_ = FOLLOW; 600 601 this->setTarget(pawn); 599 602 if (!always) 600 this->specificMasterActionHoldCount_ = SECONDS_TO_FOLLOW_HUMAN;603 this->specificMasterActionHoldCount_ = secondsToFollow; 601 604 else 602 605 this->specificMasterActionHoldCount_ = INT_MAX; //for now... … … 604 607 } 605 608 606 /** 607 @brief Follows target with adjusted speed. Called within tick. 609 610 /** 611 @brief Master begins to follow a randomly chosen human player of the same team. Is a "specific master action". 612 */ 613 void ArtificialController::followRandomHumanInit() 614 { 615 616 Pawn *humanPawn = NULL; 617 NewHumanController *currentHumanController = NULL; 618 619 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) 620 { 621 if (!it->getController()) 622 continue; 623 624 currentHumanController = orxonox_cast<NewHumanController*>(it->getController()); 625 if(currentHumanController) 626 { 627 if (!ArtificialController::sameTeam(this->getControllableEntity(), *it, this->getGametype())) continue; 628 humanPawn = *it; 629 break; 630 } 631 } 632 633 if((humanPawn != NULL)) 634 this->followInit(humanPawn); 635 } 636 637 /** 638 @brief Master follows target with adjusted speed. Called within tick. 608 639 */ 609 640 void ArtificialController::follow() 610 641 { 611 this->moveToTargetPosition(); //standard position apprach for now. 612 } 613 642 this->moveToPosition(this->target_->getPosition()); 643 /* 644 if (!this->getControllableEntity()) 645 return; 646 647 float distance = (this->target_->getPosition() - this->getControllableEntity()->getPosition()).length(); 648 649 Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->target_->getPosition()); 650 651 652 this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x); 653 this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y); 654 655 float speedDiv = this->getControllableEntity()->getVelocity().squaredLength() - this->target_->getVelocity().squaredLength(); 656 657 COUT(0) << "~follow distance: " << distance << "SpeedCounter: " << this->speedCounter_ << "~speedDiv: " << speedDiv << std::endl; 658 if (distance < 800) 659 { 660 if (distance < 200) 661 { 662 this->speedCounter_ -= 0.5f; 663 if(this->speedCounter_ < 0) this->speedCounter_ = 0.0f; 664 this->getControllableEntity()->moveFrontBack(speedCounter_); 665 } else { 666 if(speedDiv < 0) 667 this->speedCounter_ += 0.01f; 668 else 669 this->speedCounter_ -= 0.05f; 670 this->getControllableEntity()->moveFrontBack(speedCounter_); 671 } 672 673 } else { 674 this->speedCounter_ += 0.05f; 675 this->getControllableEntity()->moveFrontBack(speedCounter_ + distance/300.0f); 676 } 677 // if (this->getControllableEntity()->getVelocity().squaredLength() > 50.0f) this->speedCounter_ = 0; 678 679 */ 680 } 681 682 683 /** 684 @brief Slave moving behaviour when master is following a pawn, gets redirected from moveToPosition(const Vector3& target)). Called within tick. 685 */ 686 void ArtificialController::followForSlaves(const Vector3& target) 687 { 688 689 /* 690 if (!this->getControllableEntity() && !this->myMaster_ && this->myMaster_->state_ != FOLLOW && !this->myMaster_->target_) 691 return; 692 693 float distance = (target - this->getControllableEntity()->getPosition()).length(); 694 695 Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); 696 697 698 this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x); 699 this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y); 700 701 702 float speedDiv = this->getControllableEntity()->getVelocity().squaredLength() - this->myMaster_->target_->getVelocity().squaredLength(); 703 704 705 if (distance < 800) 706 { 707 if (distance < 200) 708 { 709 this->speedCounter_ -= 5.0f; 710 if(this->speedCounter_ < 0) this->speedCounter_ = 0.0f; 711 this->getControllableEntity()->moveFrontBack(speedCounter_); 712 } else { 713 if(speedDiv < 0) 714 this->speedCounter_ += 0.01f; 715 else 716 this->speedCounter_ -= 0.05f; 717 this->getControllableEntity()->moveFrontBack(speedCounter_); 718 } 719 720 } else { 721 this->speedCounter_ += 0.05f; 722 this->getControllableEntity()->moveFrontBack(speedCounter_ + distance/300.0f); 723 } 724 // if (this->getControllableEntity()->getVelocity().squaredLength() > 50.0f) this->speedCounter_ = 0; 725 */ 726 } 614 727 615 728 -
code/branches/presentation3/src/orxonox/controllers/ArtificialController.h
r7034 r7066 54 54 inline int getTeam() const 55 55 { return this->team_; } 56 56 57 inline void setFormationFlight(bool formation) 57 58 { this->formationFlight_ = formation; } 58 59 inline bool getFormationFlight() const 59 60 { return this->formationFlight_; } 61 60 62 inline void setFormationSize(int size) 61 63 { this->maxFormationSize_ = size; } 62 64 inline int getFormationSize() const 63 65 { return this->maxFormationSize_; } 66 64 67 virtual void changedControllableEntity(); 65 68 66 static void formationflight( bool form);67 static void masteraction( int action);69 static void formationflight(const bool form); 70 static void masteraction(const int action); 68 71 static void followme(); 69 static void passivebehaviour( bool passive);70 static void formationsize( int size);72 static void passivebehaviour(const bool passive); 73 static void formationsize(const int size); 71 74 72 75 protected: … … 81 84 std::vector<ArtificialController*> slaves_; 82 85 ArtificialController *myMaster_; 83 enum SpecificMasterAction {NONE, HOLD, SPIN, TURN180, FOLLOW HUMAN};86 enum SpecificMasterAction {NONE, HOLD, SPIN, TURN180, FOLLOW}; 84 87 SpecificMasterAction specificMasterAction_; 85 88 int specificMasterActionHoldCount_; 86 Pawn* humanToFollow_; 87 88 void targetDied(); 89 float speedCounter_; //for speed adjustment when following 89 90 90 91 void moveToPosition(const Vector3& target); 91 92 void moveToTargetPosition(); 92 93 int getState();94 93 95 94 void unregisterSlave(); … … 109 108 void spinInit(); 110 109 void spin(); 111 void followHumanInit(Pawn* human, bool always); 110 void followInit(Pawn* pawn, const bool always = false, const int secondsToFollow = 100); 111 void followRandomHumanInit(); 112 112 void follow(); 113 void followForSlaves(const Vector3& target); 113 114 114 115 void setTargetPosition(const Vector3& target); … … 122 123 bool isCloseAtTarget(float distance) const; 123 124 bool isLookingAtTarget(float angle) const; 125 126 void targetDied(); 124 127 125 128 static bool sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype); // hack
Note: See TracChangeset
for help on using the changeset viewer.