- Timestamp:
- May 27, 2010, 6:39:59 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/ai/src/orxonox/controllers/ArtificialController.cc
r6958 r6978 23 23 * Fabian 'x3n' Landau 24 24 * Co-authors: 25 * ...25 * Dominik Solenicki 26 26 * 27 27 */ … … 29 29 #include "ArtificialController.h" 30 30 31 #include <vector> 31 32 #include "core/CoreIncludes.h" 32 33 #include "core/XMLPort.h" … … 36 37 #include "gametypes/TeamDeathmatch.h" 37 38 #include "controllers/WaypointPatrolController.h" 39 #include "controllers/NewHumanController.h" 38 40 #include "controllers/DroneController.h" 39 41 #include "util/Math.h" … … 42 44 namespace orxonox 43 45 { 44 SetConsoleCommand(ArtificialController, formationflight, true); //.defaultValues(0, true);46 SetConsoleCommand(ArtificialController, formationflight, true); 45 47 SetConsoleCommand(ArtificialController, masteraction, true); 48 SetConsoleCommand(ArtificialController, followme, true); 49 SetConsoleCommand(ArtificialController, passivbehaviour, true); 46 50 47 51 static const unsigned int MAX_FORMATION_SIZE = 7; … … 53 57 static const float SPEED_FREE = 0.8f; 54 58 static const float ROTATEFACTOR_FREE = 0.8f; 59 static const int SECONDS_TO_FOLLOW_HUMAN = 10; 55 60 56 61 ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator) … … 60 65 this->target_ = 0; 61 66 this->formationFlight_ = true; 67 this->passive_ = false; 62 68 this->myMaster_ = 0; 63 69 this->freedomCount_ = 0; … … 69 75 this->bHasTargetPosition_ = false; 70 76 this->targetPosition_ = Vector3::ZERO; 77 this->humanToFollow_ = NULL; 71 78 72 79 this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this)); … … 85 92 } 86 93 87 //activate/deactivate formationflight 94 // Documentation only here to get a faster overview for creating a useful documentation, what you're reading here not intended for an actual documentation... 95 96 /** 97 @brief Activates / deactivates formationflight behaviour 98 @param form activate formflight if form is true 99 */ 88 100 void ArtificialController::formationflight(bool form) 89 101 { … … 101 113 } 102 114 } 103 //get all masters to do this action 115 116 /** 117 @brief Get all masters to do a specific action 118 @param action which action to perform (integer, so it can be called with a console command (tmp solution)) 119 */ 104 120 void ArtificialController::masteraction(int action) 105 121 { … … 118 134 } 119 135 120 // gets called when Bot dies 136 /** 137 @brief A human player gets followed by its nearest master. Initiated by console command, only for demonstration puproses. Does not work at the moment. 138 */ 139 void ArtificialController::followme() 140 { 141 142 Pawn *humanPawn = NULL; 143 NewHumanController *currentHumanController = NULL; 144 std::vector<ArtificialController*> allMasters; 145 146 for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) 147 { 148 if (!it->getController()) 149 continue; 150 151 currentHumanController = static_cast<NewHumanController*>(it->getController()); 152 153 if(currentHumanController) humanPawn = *it; 154 155 ArtificialController *aiController = static_cast<ArtificialController*>(it->getController()); 156 157 if(aiController || aiController->state_ == MASTER) 158 allMasters.push_back(aiController); 159 160 } 161 /*if((humanPawn != NULL) && (allMasters.size != 0)) 162 { 163 164 float posHuman = humanPawn->getPosition().length(); 165 float distance = 0.0f; 166 float minDistance = posHuman - allMasters.back()->getControllableEntity()->getPosition().length(); 167 int index = 0; 168 int i = 0; 169 170 for(std::vector<ArtificialController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++) 171 { 172 distance = posHuman - (*it)->getControllableEntity()->getPosition().length(); 173 if(distance < minDistance) index = i; 174 } 175 allMasters[index].humanToFollow_ = humanPawn; 176 allMasters[index].followHuman(humanPawn, false); 177 }*/ 178 179 } 180 181 /** 182 @brief Sets shootingbehaviour of pawns. 183 @param passive if true, pawns won't shoot. 184 */ 185 void ArtificialController::passivebehaviour(bool passive) 186 { 187 this->passive_ = passive; 188 } 189 190 /** 191 @brief Gets called if ControllableEntity is changed. Resets the bot when it dies. 192 */ 121 193 void ArtificialController::changedControllableEntity() 122 194 { … … 130 202 } 131 203 } 204 132 205 133 206 void ArtificialController::moveToPosition(const Vector3& target) … … 210 283 } 211 284 285 /** 286 @brief Unregisters a slave from its master. Called by a slave. 287 */ 212 288 void ArtificialController::unregisterSlave() { 213 289 if(myMaster_) … … 272 348 break; 273 349 } 274 }//for 275 //hasn't encountered any masters in range? -> become a master 276 if (state_ != SLAVE && teamSize != 0) state_ = MASTER;//master encounters master? ->done 277 278 } 279 280 void ArtificialController::commandSlaves() { 350 } 351 352 if (state_ != SLAVE && teamSize != 0) state_ = MASTER; 353 354 } 355 356 /** 357 @brief Commands the slaves of a master into a formation. Called by a master. 358 */ 359 void ArtificialController::commandSlaves() 360 { 361 if(this->state_ != MASTER) return; 281 362 282 363 Quaternion orient = this->getControllableEntity()->getOrientation(); … … 315 396 } 316 397 317 // binds slaves to new Master within formation 398 /** 399 @brief Sets a new master within the formation. Called by a master. 400 */ 318 401 void ArtificialController::setNewMasterWithinFormation() 319 402 { 403 if(this->state_ != MASTER) return; 320 404 321 405 if (this->slaves_.empty()) … … 340 424 } 341 425 426 /** 427 @brief Frees all slaves form a master. Called by a master. 428 */ 342 429 void ArtificialController::freeSlaves() 343 430 { 431 if(this->state_ != MASTER) return; 432 344 433 for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) 345 434 { … … 349 438 } 350 439 440 /** 441 @brief Master sets its slaves free for \var FREEDOM_COUNT seconds. 442 */ 351 443 void ArtificialController::forceFreeSlaves() 352 444 { 445 if(this->state_ != MASTER) return; 446 353 447 for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) 354 448 { … … 367 461 } 368 462 463 369 464 void ArtificialController::forceFreedom() 370 465 { … … 372 467 } 373 468 469 /** 470 @brief Checks wether caller has been forced free, decrements time to stay forced free. 471 @return true if forced free. 472 */ 374 473 bool ArtificialController::forcedFree() 375 474 { … … 381 480 } 382 481 482 /** 483 @brief Used to continue a "specific master action" for a certain time. 484 */ 383 485 void ArtificialController::specificMasterActionHold() 384 486 { 487 if(this->state_ != MASTER) return; 488 385 489 if (specificMasterActionHoldCount_ == 0) 386 490 { 387 491 this->specificMasterAction_ = NONE; 388 492 this->searchNewTarget(); 389 COUT(0) << "~action end" << std::endl;390 493 } 391 494 else specificMasterActionHoldCount_--; 392 495 } 393 496 497 /** 498 @brief Master engages a 180 degree turn. Is a "specific master action". 499 */ 394 500 void ArtificialController::turn180() 395 501 { 502 if(this->state_ != MASTER) return; 503 396 504 COUT(0) << "~turn" << std::endl; 397 505 … … 403 511 } 404 512 513 /** 514 @brief Master spins around looking directions axis. Is a "specific master action". 515 */ 405 516 void ArtificialController::spin() 406 517 { 518 if(this->state_ != MASTER) return; 519 407 520 this->specificMasterAction_ = NONE; 521 } 522 523 /** 524 @brief Master begins to follow a human player. Is a "specific master action". 525 @param humanController human to follow. 526 @param alaways follows human forever if true - yet only inplemented for false. 527 */ 528 void ArtificialController::followHuman(Pawn* human, bool always) 529 { 530 if (human == NULL) 531 { 532 this->specificMasterAction_ = NONE; 533 return; 534 } 535 if (!always) 536 { 537 this->setTarget(human); 538 this->specificMasterActionHoldCount_ = SECONDS_TO_FOLLOW_HUMAN; 539 this->specificMasterAction_ = HOLD; 540 } 541 408 542 } 409 543
Note: See TracChangeset
for help on using the changeset viewer.