[2362] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
[7163] | 25 | * Dominik Solenicki |
---|
[2362] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "ArtificialController.h" |
---|
| 30 | |
---|
[7163] | 31 | #include <vector> |
---|
| 32 | #include <climits> |
---|
| 33 | |
---|
| 34 | #include "util/Math.h" |
---|
[2362] | 35 | #include "core/CoreIncludes.h" |
---|
[7163] | 36 | #include "core/XMLPort.h" |
---|
[7284] | 37 | #include "core/command/ConsoleCommand.h" |
---|
[5735] | 38 | #include "worldentities/ControllableEntity.h" |
---|
| 39 | #include "worldentities/pawns/Pawn.h" |
---|
| 40 | #include "worldentities/pawns/TeamBaseMatchBase.h" |
---|
[8736] | 41 | #include "worldentities/pawns/SpaceShip.h" |
---|
[5735] | 42 | #include "gametypes/TeamDeathmatch.h" |
---|
[7163] | 43 | #include "gametypes/Dynamicmatch.h" |
---|
[5735] | 44 | #include "controllers/WaypointPatrolController.h" |
---|
[7163] | 45 | #include "controllers/NewHumanController.h" |
---|
| 46 | #include "controllers/DroneController.h" |
---|
[8723] | 47 | #include "weaponsystem/WeaponMode.h" |
---|
| 48 | #include "weaponsystem/WeaponPack.h" |
---|
| 49 | #include "weaponsystem/Weapon.h" |
---|
[8761] | 50 | #include "weaponsystem/WeaponSlot.h" |
---|
[3049] | 51 | |
---|
[2362] | 52 | namespace orxonox |
---|
| 53 | { |
---|
[7284] | 54 | SetConsoleCommand("ArtificialController", "formationflight", &ArtificialController::formationflight); |
---|
| 55 | SetConsoleCommand("ArtificialController", "masteraction", &ArtificialController::masteraction); |
---|
| 56 | SetConsoleCommand("ArtificialController", "followme", &ArtificialController::followme); |
---|
| 57 | SetConsoleCommand("ArtificialController", "passivebehaviour", &ArtificialController::passivebehaviour); |
---|
| 58 | SetConsoleCommand("ArtificialController", "formationsize", &ArtificialController::formationsize); |
---|
[8723] | 59 | SetConsoleCommand("ArtificialController", "setbotlevel", &ArtificialController::setAllBotLevel); |
---|
[7163] | 60 | |
---|
| 61 | static const unsigned int STANDARD_MAX_FORMATION_SIZE = 7; |
---|
| 62 | static const int RADIUS_TO_SEARCH_FOR_MASTERS = 5000; |
---|
| 63 | static const int FORMATION_LENGTH = 130; |
---|
| 64 | static const int 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 | |
---|
[2362] | 72 | ArtificialController::ArtificialController(BaseObject* creator) : Controller(creator) |
---|
| 73 | { |
---|
| 74 | RegisterObject(ArtificialController); |
---|
| 75 | |
---|
| 76 | this->target_ = 0; |
---|
[7801] | 77 | this->formationFlight_ = false; |
---|
[7163] | 78 | this->passive_ = false; |
---|
| 79 | this->maxFormationSize_ = STANDARD_MAX_FORMATION_SIZE; |
---|
| 80 | this->myMaster_ = 0; |
---|
| 81 | this->freedomCount_ = 0; |
---|
| 82 | this->team_ = -1; |
---|
| 83 | this->state_ = FREE; |
---|
| 84 | this->specificMasterAction_ = NONE; |
---|
| 85 | this->specificMasterActionHoldCount_ = 0; |
---|
[2362] | 86 | this->bShooting_ = false; |
---|
| 87 | this->bHasTargetPosition_ = false; |
---|
[7163] | 88 | this->speedCounter_ = 0.2f; |
---|
[2362] | 89 | this->targetPosition_ = Vector3::ZERO; |
---|
[6417] | 90 | |
---|
[5929] | 91 | this->target_.setCallback(createFunctor(&ArtificialController::targetDied, this)); |
---|
[8723] | 92 | this->bSetupWorked = false; |
---|
| 93 | this->numberOfWeapons = 0; |
---|
[8735] | 94 | this->botlevel_ = 0.5f; |
---|
[8723] | 95 | this->mode_ = DEFAULT;////Vector-implementation: mode_.push_back(DEFAULT); |
---|
| 96 | this->timeout_=0; |
---|
[2362] | 97 | } |
---|
| 98 | |
---|
| 99 | ArtificialController::~ArtificialController() |
---|
| 100 | { |
---|
[7163] | 101 | if (this->isInitialized()) |
---|
[8723] | 102 | {//Vector-implementation: mode_.erase(mode_.begin(),mode_.end()); |
---|
[7163] | 103 | this->removeFromFormation(); |
---|
| 104 | |
---|
| 105 | for (ObjectList<ArtificialController>::iterator it = ObjectList<ArtificialController>::begin(); it; ++it) |
---|
| 106 | { |
---|
| 107 | if (*it != this) |
---|
| 108 | { |
---|
| 109 | if (it->myMaster_ == this) |
---|
| 110 | { |
---|
| 111 | COUT(1) << "error: " << this << " is still master in " << (*it) << std::endl; |
---|
| 112 | it->myMaster_ = 0; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | while (true) |
---|
| 116 | { |
---|
| 117 | std::vector<ArtificialController*>::iterator it2 = std::find(it->slaves_.begin(), it->slaves_.end(), this); |
---|
| 118 | if (it2 != it->slaves_.end()) |
---|
| 119 | { |
---|
| 120 | COUT(1) << "error: " << this << " is still slave in " << (*it) << std::endl; |
---|
| 121 | it->slaves_.erase(it2); |
---|
| 122 | } |
---|
| 123 | else |
---|
| 124 | break; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | } |
---|
[2362] | 129 | } |
---|
| 130 | |
---|
[7163] | 131 | void ArtificialController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 132 | { |
---|
| 133 | SUPER(ArtificialController, XMLPort, xmlelement, mode); |
---|
| 134 | |
---|
| 135 | XMLPortParam(ArtificialController, "team", setTeam, getTeam, xmlelement, mode).defaultValues(-1); |
---|
| 136 | XMLPortParam(ArtificialController, "formationFlight", setFormationFlight, getFormationFlight, xmlelement, mode).defaultValues(false); |
---|
| 137 | XMLPortParam(ArtificialController, "formationSize", setFormationSize, getFormationSize, xmlelement, mode).defaultValues(STANDARD_MAX_FORMATION_SIZE); |
---|
| 138 | XMLPortParam(ArtificialController, "passive", setPassive, getPassive, xmlelement, mode).defaultValues(false); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | // Documentation only here to get a faster overview for creating a useful documentation... |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | @brief Activates / deactivates formationflight behaviour |
---|
| 145 | @param form activate formflight if form is true |
---|
| 146 | */ |
---|
| 147 | void ArtificialController::formationflight(const bool form) |
---|
| 148 | { |
---|
| 149 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 150 | { |
---|
| 151 | Controller* controller = 0; |
---|
| 152 | |
---|
| 153 | if (it->getController()) |
---|
| 154 | controller = it->getController(); |
---|
| 155 | else if (it->getXMLController()) |
---|
| 156 | controller = it->getXMLController(); |
---|
| 157 | |
---|
| 158 | if (!controller) |
---|
| 159 | continue; |
---|
| 160 | |
---|
| 161 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(controller); |
---|
| 162 | |
---|
| 163 | if (aiController) |
---|
| 164 | { |
---|
| 165 | aiController->formationFlight_ = form; |
---|
| 166 | if (!form) |
---|
| 167 | { |
---|
| 168 | aiController->removeFromFormation(); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | @brief Get all masters to do a "specific master action" |
---|
| 176 | @param action which action to perform (integer, so it can be called with a console command (tmp solution)) |
---|
| 177 | */ |
---|
| 178 | void ArtificialController::masteraction(const int action) |
---|
| 179 | { |
---|
| 180 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 181 | { |
---|
| 182 | Controller* controller = 0; |
---|
| 183 | |
---|
| 184 | if (it->getController()) |
---|
| 185 | controller = it->getController(); |
---|
| 186 | else if (it->getXMLController()) |
---|
| 187 | controller = it->getXMLController(); |
---|
| 188 | |
---|
| 189 | if (!controller) |
---|
| 190 | continue; |
---|
| 191 | |
---|
| 192 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(controller); |
---|
| 193 | |
---|
| 194 | if(aiController && aiController->state_ == MASTER) |
---|
| 195 | { |
---|
| 196 | if (action == 1) |
---|
| 197 | aiController->spinInit(); |
---|
| 198 | if (action == 2) |
---|
| 199 | aiController->turn180Init(); |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | @brief A human player gets followed by its nearest master. Initiated by console command, so far intended for demonstration puproses (possible future pickup). |
---|
| 206 | */ |
---|
| 207 | void ArtificialController::followme() |
---|
| 208 | { |
---|
| 209 | |
---|
| 210 | Pawn *humanPawn = NULL; |
---|
| 211 | NewHumanController *currentHumanController = NULL; |
---|
| 212 | std::vector<ArtificialController*> allMasters; |
---|
| 213 | |
---|
| 214 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 215 | { |
---|
| 216 | Controller* controller = 0; |
---|
| 217 | |
---|
| 218 | if (it->getController()) |
---|
| 219 | controller = it->getController(); |
---|
| 220 | else if (it->getXMLController()) |
---|
| 221 | controller = it->getXMLController(); |
---|
| 222 | |
---|
| 223 | if (!controller) |
---|
| 224 | continue; |
---|
| 225 | |
---|
| 226 | currentHumanController = orxonox_cast<NewHumanController*>(controller); |
---|
| 227 | |
---|
| 228 | if(currentHumanController) humanPawn = *it; |
---|
| 229 | |
---|
| 230 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(controller); |
---|
| 231 | |
---|
| 232 | if(aiController && aiController->state_ == MASTER) |
---|
| 233 | allMasters.push_back(aiController); |
---|
| 234 | |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | if((humanPawn != NULL) && (allMasters.size() != 0)) |
---|
| 238 | { |
---|
| 239 | float posHuman = humanPawn->getPosition().length(); |
---|
| 240 | float distance = 0.0f; |
---|
| 241 | float minDistance = FLT_MAX; |
---|
| 242 | int index = 0; |
---|
| 243 | int i = 0; |
---|
| 244 | |
---|
| 245 | for(std::vector<ArtificialController*>::iterator it = allMasters.begin(); it != allMasters.end(); it++, i++) |
---|
| 246 | { |
---|
| 247 | if (!ArtificialController::sameTeam((*it)->getControllableEntity(), humanPawn, (*it)->getGametype())) continue; |
---|
| 248 | distance = posHuman - (*it)->getControllableEntity()->getPosition().length(); |
---|
| 249 | if(distance < minDistance) index = i; |
---|
| 250 | } |
---|
| 251 | allMasters[index]->followInit(humanPawn); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | /** |
---|
| 257 | @brief Sets shooting behaviour of pawns. |
---|
| 258 | @param passive if true, bots won't shoot. |
---|
| 259 | */ |
---|
| 260 | void ArtificialController::passivebehaviour(const bool passive) |
---|
| 261 | { |
---|
| 262 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 263 | { |
---|
| 264 | Controller* controller = 0; |
---|
| 265 | |
---|
| 266 | if (it->getController()) |
---|
| 267 | controller = it->getController(); |
---|
| 268 | else if (it->getXMLController()) |
---|
| 269 | controller = it->getXMLController(); |
---|
| 270 | |
---|
| 271 | if (!controller) |
---|
| 272 | continue; |
---|
| 273 | |
---|
| 274 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(controller); |
---|
| 275 | |
---|
| 276 | if(aiController) |
---|
| 277 | { |
---|
| 278 | aiController->passive_ = passive; |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | /** |
---|
| 285 | @brief Sets maximal formation size |
---|
| 286 | @param size maximal formation size. |
---|
| 287 | */ |
---|
| 288 | void ArtificialController::formationsize(const int size) |
---|
| 289 | { |
---|
| 290 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 291 | { |
---|
| 292 | Controller* controller = 0; |
---|
| 293 | |
---|
| 294 | if (it->getController()) |
---|
| 295 | controller = it->getController(); |
---|
| 296 | else if (it->getXMLController()) |
---|
| 297 | controller = it->getXMLController(); |
---|
| 298 | |
---|
| 299 | if (!controller) |
---|
| 300 | continue; |
---|
| 301 | |
---|
| 302 | ArtificialController *aiController = orxonox_cast<ArtificialController*>(controller); |
---|
| 303 | |
---|
| 304 | if(aiController) |
---|
| 305 | { |
---|
| 306 | aiController->maxFormationSize_ = size; |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | /** |
---|
| 312 | @brief Gets called when ControllableEntity is being changed. Resets the bot when it dies. |
---|
| 313 | */ |
---|
| 314 | void ArtificialController::changedControllableEntity() |
---|
| 315 | { |
---|
| 316 | if (!this->getControllableEntity()) |
---|
| 317 | this->removeFromFormation(); |
---|
[8758] | 318 | this->bSetupWorked = false; // reset weapon information |
---|
| 319 | this->numberOfWeapons = 0; |
---|
[8762] | 320 | this->setupWeapons(); |
---|
[7163] | 321 | } |
---|
| 322 | |
---|
| 323 | void ArtificialController::removeFromFormation() |
---|
| 324 | { |
---|
| 325 | if (this->state_ == SLAVE || this->myMaster_) // slaves can also be temporary free, so check if myMaster_ is set |
---|
| 326 | this->unregisterSlave(); |
---|
| 327 | else if (this->state_ == MASTER) |
---|
| 328 | this->setNewMasterWithinFormation(); |
---|
| 329 | } |
---|
| 330 | |
---|
[3049] | 331 | void ArtificialController::moveToPosition(const Vector3& target) |
---|
[2362] | 332 | { |
---|
| 333 | if (!this->getControllableEntity()) |
---|
| 334 | return; |
---|
| 335 | |
---|
[7163] | 336 | // Slave uses special movement if its master is in FOLLOW mode |
---|
| 337 | if(this->state_ == SLAVE && this->myMaster_ && this->myMaster_->specificMasterAction_ == FOLLOW) |
---|
| 338 | { |
---|
| 339 | // this->followForSlaves(target); |
---|
| 340 | // return; |
---|
| 341 | } |
---|
| 342 | |
---|
[3049] | 343 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
| 344 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
[2362] | 345 | |
---|
[7163] | 346 | |
---|
| 347 | if(this->state_ == FREE) |
---|
[2362] | 348 | { |
---|
[7163] | 349 | if (this->target_ || distance > 10) |
---|
| 350 | { |
---|
| 351 | // Multiply with ROTATEFACTOR_FREE to make them a bit slower |
---|
| 352 | this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_FREE * sgn(coord.x) * coord.x*coord.x); |
---|
| 353 | this->getControllableEntity()->rotatePitch(ROTATEFACTOR_FREE * sgn(coord.y) * coord.y*coord.y); |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
| 357 | { |
---|
| 358 | this->getControllableEntity()->moveFrontBack(-0.05f); // They don't brake with full power to give the player a chance |
---|
| 359 | } else this->getControllableEntity()->moveFrontBack(SPEED_FREE); |
---|
[2362] | 360 | } |
---|
| 361 | |
---|
[7163] | 362 | |
---|
| 363 | |
---|
| 364 | if(this->state_ == MASTER) |
---|
| 365 | { |
---|
| 366 | if (this->target_ || distance > 10) |
---|
| 367 | { |
---|
| 368 | this->getControllableEntity()->rotateYaw(-1.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x); |
---|
| 369 | this->getControllableEntity()->rotatePitch(ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y); |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | if (this->target_ && distance < 200 && this->getControllableEntity()->getVelocity().squaredLength() > this->target_->getVelocity().squaredLength()) |
---|
| 373 | { |
---|
| 374 | this->getControllableEntity()->moveFrontBack(-0.05f); |
---|
| 375 | } else this->getControllableEntity()->moveFrontBack(SPEED_MASTER); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | |
---|
| 379 | |
---|
| 380 | if(this->state_ == SLAVE) |
---|
| 381 | { |
---|
| 382 | |
---|
| 383 | this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR_MASTER * sgn(coord.x) * coord.x*coord.x); |
---|
| 384 | this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR_MASTER * sgn(coord.y) * coord.y*coord.y); |
---|
| 385 | |
---|
| 386 | if (distance < 300) |
---|
| 387 | { |
---|
| 388 | if (distance < 40) |
---|
| 389 | { |
---|
| 390 | this->getControllableEntity()->moveFrontBack(0.8f*SPEED_MASTER); |
---|
| 391 | } else this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER); |
---|
| 392 | |
---|
| 393 | } else { |
---|
| 394 | this->getControllableEntity()->moveFrontBack(1.2f*SPEED_MASTER + distance/300.0f); |
---|
| 395 | } |
---|
| 396 | } |
---|
[8706] | 397 | |
---|
| 398 | if (distance < 10) |
---|
| 399 | { |
---|
| 400 | this->positionReached(); |
---|
| 401 | } |
---|
[2362] | 402 | } |
---|
| 403 | |
---|
[3049] | 404 | void ArtificialController::moveToTargetPosition() |
---|
| 405 | { |
---|
| 406 | this->moveToPosition(this->targetPosition_); |
---|
| 407 | } |
---|
| 408 | |
---|
[7163] | 409 | /** |
---|
| 410 | @brief Unregisters a slave from its master. Initiated by a slave. |
---|
| 411 | */ |
---|
| 412 | void ArtificialController::unregisterSlave() |
---|
| 413 | { |
---|
| 414 | if (this->myMaster_) |
---|
| 415 | { |
---|
| 416 | std::vector<ArtificialController*>::iterator it = std::find(this->myMaster_->slaves_.begin(), this->myMaster_->slaves_.end(), this); |
---|
| 417 | if (it != this->myMaster_->slaves_.end()) |
---|
| 418 | this->myMaster_->slaves_.erase(it); |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | this->myMaster_ = 0; |
---|
| 422 | this->state_ = FREE; |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | void ArtificialController::searchNewMaster() |
---|
| 426 | { |
---|
| 427 | |
---|
| 428 | if (!this->getControllableEntity()) |
---|
| 429 | return; |
---|
| 430 | |
---|
| 431 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
| 432 | this->forgetTarget(); |
---|
| 433 | int teamSize = 0; |
---|
| 434 | //go through all pawns |
---|
| 435 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 436 | { |
---|
| 437 | //same team? |
---|
| 438 | if (!ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
| 439 | continue; |
---|
| 440 | |
---|
| 441 | //has it an ArtificialController? |
---|
| 442 | Controller* controller = 0; |
---|
| 443 | |
---|
| 444 | if (it->getController()) |
---|
| 445 | controller = it->getController(); |
---|
| 446 | else if (it->getXMLController()) |
---|
| 447 | controller = it->getXMLController(); |
---|
| 448 | |
---|
| 449 | if (!controller) |
---|
| 450 | continue; |
---|
| 451 | |
---|
| 452 | //is pawn oneself? |
---|
| 453 | if (orxonox_cast<ControllableEntity*>(*it) == this->getControllableEntity()) |
---|
| 454 | continue; |
---|
| 455 | |
---|
| 456 | teamSize++; |
---|
| 457 | |
---|
| 458 | ArtificialController *newMaster = orxonox_cast<ArtificialController*>(controller); |
---|
| 459 | |
---|
| 460 | //is it a master? |
---|
| 461 | if (!newMaster || newMaster->state_ != MASTER) |
---|
| 462 | continue; |
---|
| 463 | |
---|
| 464 | float distance = (it->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
| 465 | |
---|
| 466 | // is pawn in range? |
---|
| 467 | if (distance < RADIUS_TO_SEARCH_FOR_MASTERS) |
---|
| 468 | { |
---|
| 469 | if(newMaster->slaves_.size() > this->maxFormationSize_) continue; |
---|
| 470 | |
---|
| 471 | for(std::vector<ArtificialController*>::iterator itSlave = this->slaves_.begin(); itSlave != this->slaves_.end(); itSlave++) |
---|
| 472 | { |
---|
| 473 | (*itSlave)->myMaster_ = newMaster; |
---|
| 474 | newMaster->slaves_.push_back(*itSlave); |
---|
| 475 | } |
---|
| 476 | this->slaves_.clear(); |
---|
| 477 | this->state_ = SLAVE; |
---|
| 478 | |
---|
| 479 | this->myMaster_ = newMaster; |
---|
| 480 | newMaster->slaves_.push_back(this); |
---|
| 481 | |
---|
| 482 | break; |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | if (this->state_ != SLAVE && teamSize != 0) |
---|
| 487 | { |
---|
| 488 | this->state_ = MASTER; |
---|
| 489 | this->myMaster_ = 0; |
---|
| 490 | } |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | /** |
---|
| 494 | @brief Commands the slaves of a master into a formation. Sufficiently fast not to be called within tick. Initiated by a master. |
---|
| 495 | */ |
---|
| 496 | void ArtificialController::commandSlaves() |
---|
| 497 | { |
---|
| 498 | if(this->state_ != MASTER) return; |
---|
| 499 | |
---|
| 500 | Quaternion orient = this->getControllableEntity()->getOrientation(); |
---|
| 501 | Vector3 dest = this->getControllableEntity()->getPosition(); |
---|
| 502 | |
---|
| 503 | // 1 slave: follow |
---|
| 504 | if (this->slaves_.size() == 1) |
---|
| 505 | { |
---|
| 506 | dest += 4*orient*WorldEntity::BACK; |
---|
| 507 | this->slaves_.front()->setTargetPosition(dest); |
---|
| 508 | } |
---|
| 509 | else |
---|
| 510 | { |
---|
| 511 | dest += 1.0f*orient*WorldEntity::BACK; |
---|
| 512 | Vector3 pos = Vector3::ZERO; |
---|
| 513 | int i = 1; |
---|
| 514 | |
---|
| 515 | for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
| 516 | { |
---|
| 517 | pos = Vector3::ZERO; |
---|
[7183] | 518 | if (i <= 1) pos += dest + (float)FORMATION_WIDTH*(orient*WorldEntity::LEFT); |
---|
| 519 | if (i == 2) pos += dest + (float)FORMATION_WIDTH*(orient*WorldEntity::RIGHT); |
---|
| 520 | if (i == 3) pos += dest + (float)FORMATION_WIDTH*(orient*WorldEntity::UP); |
---|
[7163] | 521 | if (i >= 4) |
---|
| 522 | { |
---|
[7183] | 523 | pos += dest + (float)FORMATION_WIDTH*(orient*WorldEntity::DOWN); |
---|
[7163] | 524 | i = 1; |
---|
[7183] | 525 | dest += (float)FORMATION_LENGTH*(orient*WorldEntity::BACK); |
---|
[7163] | 526 | (*it)->setTargetPosition(pos); |
---|
| 527 | continue; |
---|
| 528 | } |
---|
| 529 | i++; |
---|
| 530 | (*it)->setTargetPosition(pos); |
---|
| 531 | } |
---|
| 532 | } |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | /** |
---|
| 536 | @brief Sets a new master within the formation. Called by a master. |
---|
| 537 | */ |
---|
| 538 | void ArtificialController::setNewMasterWithinFormation() |
---|
| 539 | { |
---|
| 540 | if(this->state_ != MASTER) return; |
---|
| 541 | |
---|
| 542 | if (!this->slaves_.empty()) |
---|
| 543 | { |
---|
| 544 | ArtificialController *newMaster = this->slaves_.back(); |
---|
| 545 | this->slaves_.pop_back(); |
---|
| 546 | |
---|
| 547 | newMaster->state_ = MASTER; |
---|
| 548 | newMaster->slaves_ = this->slaves_; |
---|
| 549 | newMaster->myMaster_ = 0; |
---|
| 550 | |
---|
| 551 | for(std::vector<ArtificialController*>::iterator it = newMaster->slaves_.begin(); it != newMaster->slaves_.end(); it++) |
---|
| 552 | { |
---|
| 553 | (*it)->myMaster_ = newMaster; |
---|
| 554 | } |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | this->slaves_.clear(); |
---|
| 558 | this->specificMasterAction_ = NONE; |
---|
| 559 | this->state_ = FREE; |
---|
| 560 | } |
---|
| 561 | |
---|
| 562 | /** |
---|
| 563 | @brief Frees all slaves form a master. Initiated by a master. |
---|
| 564 | */ |
---|
| 565 | void ArtificialController::freeSlaves() |
---|
| 566 | { |
---|
| 567 | if(this->state_ != MASTER) return; |
---|
| 568 | |
---|
| 569 | for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
| 570 | { |
---|
| 571 | (*it)->state_ = FREE; |
---|
| 572 | (*it)->myMaster_ = 0; |
---|
| 573 | } |
---|
| 574 | this->slaves_.clear(); |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | /** |
---|
[7401] | 578 | @brief Master sets its slaves free for @ref FREEDOM_COUNT seconds. |
---|
[7163] | 579 | */ |
---|
| 580 | void ArtificialController::forceFreeSlaves() |
---|
| 581 | { |
---|
| 582 | if(this->state_ != MASTER) return; |
---|
| 583 | |
---|
| 584 | for(std::vector<ArtificialController*>::iterator it = slaves_.begin(); it != slaves_.end(); it++) |
---|
| 585 | { |
---|
| 586 | (*it)->state_ = FREE; |
---|
| 587 | (*it)->forceFreedom(); |
---|
| 588 | (*it)->targetPosition_ = this->targetPosition_; |
---|
| 589 | (*it)->bShooting_ = true; |
---|
| 590 | // (*it)->getControllableEntity()->fire(0);// fire once for fun |
---|
| 591 | } |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | void ArtificialController::loseMasterState() |
---|
| 595 | { |
---|
| 596 | this->freeSlaves(); |
---|
| 597 | this->state_ = FREE; |
---|
| 598 | } |
---|
| 599 | |
---|
| 600 | |
---|
| 601 | void ArtificialController::forceFreedom() |
---|
| 602 | { |
---|
| 603 | this->freedomCount_ = FREEDOM_COUNT; |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | /** |
---|
| 607 | @brief Checks wether caller has been forced free, decrements time to stay forced free. |
---|
| 608 | @return true if forced free. |
---|
| 609 | */ |
---|
| 610 | bool ArtificialController::forcedFree() |
---|
| 611 | { |
---|
| 612 | if(this->freedomCount_ > 0) |
---|
| 613 | { |
---|
| 614 | this->freedomCount_--; |
---|
| 615 | return true; |
---|
| 616 | } else return false; |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | /** |
---|
| 620 | @brief Used to continue a "specific master action" for a certain time and resuming normal behaviour after. |
---|
| 621 | */ |
---|
| 622 | void ArtificialController::specificMasterActionHold() |
---|
| 623 | { |
---|
| 624 | if(this->state_ != MASTER) return; |
---|
| 625 | |
---|
| 626 | if (specificMasterActionHoldCount_ == 0) |
---|
| 627 | { |
---|
| 628 | this->specificMasterAction_ = NONE; |
---|
| 629 | this->searchNewTarget(); |
---|
| 630 | } |
---|
| 631 | else specificMasterActionHoldCount_--; |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | /** |
---|
| 635 | @brief Master initializes a 180 degree turn. Leads to a "specific master action". |
---|
| 636 | */ |
---|
| 637 | void ArtificialController::turn180Init() |
---|
| 638 | { |
---|
| 639 | if(this->state_ != MASTER) return; |
---|
| 640 | |
---|
| 641 | Quaternion orient = this->getControllableEntity()->getOrientation(); |
---|
| 642 | |
---|
| 643 | this->setTargetPosition(this->getControllableEntity()->getPosition() + 1000.0f*orient*WorldEntity::BACK); |
---|
| 644 | |
---|
| 645 | this->specificMasterActionHoldCount_ = 4; |
---|
| 646 | |
---|
| 647 | this->specificMasterAction_ = TURN180; |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | /** |
---|
| 651 | @brief Execute the 180 degree turn. Called within tick. |
---|
| 652 | */ |
---|
| 653 | void ArtificialController::turn180() |
---|
| 654 | { |
---|
| 655 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_); |
---|
| 656 | |
---|
| 657 | this->getControllableEntity()->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x); |
---|
| 658 | this->getControllableEntity()->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y); |
---|
| 659 | |
---|
| 660 | this->getControllableEntity()->moveFrontBack(SPEED_MASTER); |
---|
| 661 | } |
---|
| 662 | |
---|
| 663 | /** |
---|
| 664 | @brief Master initializes a spin around its looking direction axis. Leads to a "specific master action". |
---|
| 665 | */ |
---|
| 666 | void ArtificialController::spinInit() |
---|
| 667 | { |
---|
| 668 | if(this->state_ != MASTER) return; |
---|
| 669 | this->specificMasterAction_ = SPIN; |
---|
| 670 | this->specificMasterActionHoldCount_ = 10; |
---|
| 671 | } |
---|
| 672 | |
---|
| 673 | /** |
---|
| 674 | @brief Execute the spin. Called within tick. |
---|
| 675 | */ |
---|
| 676 | void ArtificialController::spin() |
---|
| 677 | { |
---|
| 678 | this->moveToTargetPosition(); |
---|
| 679 | this->getControllableEntity()->rotateRoll(0.8f); |
---|
| 680 | } |
---|
| 681 | |
---|
| 682 | /** |
---|
| 683 | @brief Master begins to follow a pawn. Is a "specific master action". |
---|
| 684 | @param pawn pawn to follow. |
---|
[7401] | 685 | @param always follows pawn forever if true (false if omitted). |
---|
[7163] | 686 | @param secondsToFollow seconds to follow the pawn if always is false. Will follow pawn 100 seconds if omitted (set in header). |
---|
| 687 | */ |
---|
| 688 | void ArtificialController::followInit(Pawn* pawn, const bool always, const int secondsToFollow) |
---|
| 689 | { |
---|
| 690 | if (pawn == NULL || this->state_ != MASTER) |
---|
| 691 | return; |
---|
| 692 | this->specificMasterAction_ = FOLLOW; |
---|
| 693 | |
---|
| 694 | this->setTarget(pawn); |
---|
| 695 | if (!always) |
---|
| 696 | this->specificMasterActionHoldCount_ = secondsToFollow; |
---|
| 697 | else |
---|
| 698 | this->specificMasterActionHoldCount_ = INT_MAX; //for now... |
---|
| 699 | |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | |
---|
| 703 | /** |
---|
| 704 | @brief Master begins to follow a randomly chosen human player of the same team. Is a "specific master action". |
---|
| 705 | */ |
---|
| 706 | void ArtificialController::followRandomHumanInit() |
---|
| 707 | { |
---|
| 708 | |
---|
| 709 | Pawn *humanPawn = NULL; |
---|
| 710 | NewHumanController *currentHumanController = NULL; |
---|
| 711 | |
---|
| 712 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 713 | { |
---|
| 714 | if (!it->getController()) |
---|
| 715 | continue; |
---|
| 716 | |
---|
| 717 | currentHumanController = orxonox_cast<NewHumanController*>(it->getController()); |
---|
| 718 | if(currentHumanController) |
---|
| 719 | { |
---|
| 720 | if (!ArtificialController::sameTeam(this->getControllableEntity(), *it, this->getGametype())) continue; |
---|
| 721 | humanPawn = *it; |
---|
| 722 | break; |
---|
| 723 | } |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | if((humanPawn != NULL)) |
---|
| 727 | this->followInit(humanPawn); |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | /** |
---|
| 731 | @brief Master follows target with adjusted speed. Called within tick. |
---|
| 732 | */ |
---|
| 733 | void ArtificialController::follow() |
---|
| 734 | { |
---|
| 735 | if (this->target_) |
---|
| 736 | this->moveToPosition(this->target_->getPosition()); |
---|
| 737 | else |
---|
| 738 | this->specificMasterActionHoldCount_ = 0; |
---|
| 739 | /* |
---|
| 740 | if (!this->getControllableEntity()) |
---|
| 741 | return; |
---|
| 742 | |
---|
| 743 | float distance = (this->target_->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
| 744 | |
---|
| 745 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->target_->getPosition()); |
---|
| 746 | |
---|
| 747 | |
---|
| 748 | this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x); |
---|
| 749 | this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y); |
---|
| 750 | |
---|
| 751 | float speedDiv = this->getControllableEntity()->getVelocity().squaredLength() - this->target_->getVelocity().squaredLength(); |
---|
| 752 | |
---|
| 753 | COUT(0) << "~follow distance: " << distance << "SpeedCounter: " << this->speedCounter_ << "~speedDiv: " << speedDiv << std::endl; |
---|
| 754 | if (distance < 800) |
---|
| 755 | { |
---|
| 756 | if (distance < 200) |
---|
| 757 | { |
---|
| 758 | this->speedCounter_ -= 0.5f; |
---|
| 759 | if(this->speedCounter_ < 0) this->speedCounter_ = 0.0f; |
---|
| 760 | this->getControllableEntity()->moveFrontBack(speedCounter_); |
---|
| 761 | } else { |
---|
| 762 | if(speedDiv < 0) |
---|
| 763 | this->speedCounter_ += 0.01f; |
---|
| 764 | else |
---|
| 765 | this->speedCounter_ -= 0.05f; |
---|
| 766 | this->getControllableEntity()->moveFrontBack(speedCounter_); |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | } else { |
---|
| 770 | this->speedCounter_ += 0.05f; |
---|
| 771 | this->getControllableEntity()->moveFrontBack(speedCounter_ + distance/300.0f); |
---|
| 772 | } |
---|
| 773 | // if (this->getControllableEntity()->getVelocity().squaredLength() > 50.0f) this->speedCounter_ = 0; |
---|
| 774 | |
---|
| 775 | */ |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | |
---|
| 779 | /** |
---|
| 780 | @brief Slave moving behaviour when master is following a pawn, gets redirected from moveToPosition(const Vector3& target)). Called within tick. |
---|
| 781 | */ |
---|
| 782 | void ArtificialController::followForSlaves(const Vector3& target) |
---|
| 783 | { |
---|
| 784 | |
---|
| 785 | /* |
---|
| 786 | if (!this->getControllableEntity() && !this->myMaster_ && this->myMaster_->state_ != FOLLOW && !this->myMaster_->target_) |
---|
| 787 | return; |
---|
| 788 | |
---|
| 789 | float distance = (target - this->getControllableEntity()->getPosition()).length(); |
---|
| 790 | |
---|
| 791 | Vector2 coord = get2DViewdirection(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, target); |
---|
| 792 | |
---|
| 793 | |
---|
| 794 | this->getControllableEntity()->rotateYaw(-0.8f * sgn(coord.x) * coord.x*coord.x); |
---|
| 795 | this->getControllableEntity()->rotatePitch(0.8f * sgn(coord.y) * coord.y*coord.y); |
---|
| 796 | |
---|
| 797 | |
---|
| 798 | float speedDiv = this->getControllableEntity()->getVelocity().squaredLength() - this->myMaster_->target_->getVelocity().squaredLength(); |
---|
| 799 | |
---|
| 800 | |
---|
| 801 | if (distance < 800) |
---|
| 802 | { |
---|
| 803 | if (distance < 200) |
---|
| 804 | { |
---|
| 805 | this->speedCounter_ -= 5.0f; |
---|
| 806 | if(this->speedCounter_ < 0) this->speedCounter_ = 0.0f; |
---|
| 807 | this->getControllableEntity()->moveFrontBack(speedCounter_); |
---|
| 808 | } else { |
---|
| 809 | if(speedDiv < 0) |
---|
| 810 | this->speedCounter_ += 0.01f; |
---|
| 811 | else |
---|
| 812 | this->speedCounter_ -= 0.05f; |
---|
| 813 | this->getControllableEntity()->moveFrontBack(speedCounter_); |
---|
| 814 | } |
---|
| 815 | |
---|
| 816 | } else { |
---|
| 817 | this->speedCounter_ += 0.05f; |
---|
| 818 | this->getControllableEntity()->moveFrontBack(speedCounter_ + distance/300.0f); |
---|
| 819 | } |
---|
| 820 | // if (this->getControllableEntity()->getVelocity().squaredLength() > 50.0f) this->speedCounter_ = 0; |
---|
| 821 | */ |
---|
| 822 | } |
---|
| 823 | |
---|
| 824 | |
---|
[3049] | 825 | void ArtificialController::setTargetPosition(const Vector3& target) |
---|
| 826 | { |
---|
| 827 | this->targetPosition_ = target; |
---|
| 828 | this->bHasTargetPosition_ = true; |
---|
| 829 | } |
---|
| 830 | |
---|
[2362] | 831 | void ArtificialController::searchRandomTargetPosition() |
---|
| 832 | { |
---|
[2493] | 833 | this->targetPosition_ = Vector3(rnd(-2000,2000), rnd(-2000,2000), rnd(-2000,2000)); |
---|
[2362] | 834 | this->bHasTargetPosition_ = true; |
---|
| 835 | } |
---|
| 836 | |
---|
[3049] | 837 | void ArtificialController::setTarget(Pawn* target) |
---|
| 838 | { |
---|
| 839 | this->target_ = target; |
---|
| 840 | |
---|
| 841 | if (target) |
---|
| 842 | this->targetPosition_ = target->getPosition(); |
---|
| 843 | } |
---|
| 844 | |
---|
[2362] | 845 | void ArtificialController::searchNewTarget() |
---|
| 846 | { |
---|
| 847 | if (!this->getControllableEntity()) |
---|
| 848 | return; |
---|
| 849 | |
---|
| 850 | this->targetPosition_ = this->getControllableEntity()->getPosition(); |
---|
| 851 | this->forgetTarget(); |
---|
| 852 | |
---|
| 853 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it; ++it) |
---|
| 854 | { |
---|
[3049] | 855 | if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype())) |
---|
| 856 | continue; |
---|
| 857 | |
---|
[7163] | 858 | /* So AI won't choose invisible Spaceships as target */ |
---|
| 859 | if (!it->getRadarVisibility()) |
---|
| 860 | continue; |
---|
| 861 | |
---|
[2506] | 862 | if (static_cast<ControllableEntity*>(*it) != this->getControllableEntity()) |
---|
[2362] | 863 | { |
---|
| 864 | float speed = this->getControllableEntity()->getVelocity().length(); |
---|
| 865 | Vector3 distanceCurrent = this->targetPosition_ - this->getControllableEntity()->getPosition(); |
---|
| 866 | Vector3 distanceNew = it->getPosition() - this->getControllableEntity()->getPosition(); |
---|
[8351] | 867 | if (!this->target_ || it->getPosition().squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceNew) / speed / distanceNew.length()) / math::twoPi) |
---|
| 868 | < this->targetPosition_.squaredDistance(this->getControllableEntity()->getPosition()) * (1.5f + acos((this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).dotProduct(distanceCurrent) / speed / distanceCurrent.length()) / math::twoPi) + rnd(-250, 250)) |
---|
[2362] | 869 | { |
---|
| 870 | this->target_ = (*it); |
---|
| 871 | this->targetPosition_ = it->getPosition(); |
---|
| 872 | } |
---|
| 873 | } |
---|
| 874 | } |
---|
| 875 | } |
---|
| 876 | |
---|
| 877 | void ArtificialController::forgetTarget() |
---|
| 878 | { |
---|
| 879 | this->target_ = 0; |
---|
| 880 | this->bShooting_ = false; |
---|
| 881 | } |
---|
| 882 | |
---|
| 883 | void ArtificialController::aimAtTarget() |
---|
| 884 | { |
---|
| 885 | if (!this->target_ || !this->getControllableEntity()) |
---|
| 886 | return; |
---|
| 887 | |
---|
[2493] | 888 | static const float hardcoded_projectile_speed = 1250; |
---|
[2362] | 889 | |
---|
[2493] | 890 | this->targetPosition_ = getPredictedPosition(this->getControllableEntity()->getPosition(), hardcoded_projectile_speed, this->target_->getPosition(), this->target_->getVelocity()); |
---|
[2362] | 891 | this->bHasTargetPosition_ = (this->targetPosition_ != Vector3::ZERO); |
---|
[6417] | 892 | |
---|
[7163] | 893 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
---|
[6417] | 894 | if (pawn) |
---|
| 895 | pawn->setAimPosition(this->targetPosition_); |
---|
[2362] | 896 | } |
---|
| 897 | |
---|
| 898 | bool ArtificialController::isCloseAtTarget(float distance) const |
---|
| 899 | { |
---|
| 900 | if (!this->getControllableEntity()) |
---|
| 901 | return false; |
---|
| 902 | |
---|
| 903 | if (!this->target_) |
---|
| 904 | return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_) < distance*distance); |
---|
| 905 | else |
---|
| 906 | return (this->getControllableEntity()->getPosition().squaredDistance(this->target_->getPosition()) < distance*distance); |
---|
| 907 | } |
---|
| 908 | |
---|
| 909 | bool ArtificialController::isLookingAtTarget(float angle) const |
---|
| 910 | { |
---|
| 911 | if (!this->getControllableEntity()) |
---|
| 912 | return false; |
---|
| 913 | |
---|
| 914 | return (getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle); |
---|
| 915 | } |
---|
| 916 | |
---|
[5929] | 917 | void ArtificialController::abandonTarget(Pawn* target) |
---|
[2362] | 918 | { |
---|
[5929] | 919 | if (target == this->target_) |
---|
| 920 | this->targetDied(); |
---|
[2362] | 921 | } |
---|
[3049] | 922 | |
---|
[5929] | 923 | void ArtificialController::targetDied() |
---|
| 924 | { |
---|
| 925 | this->forgetTarget(); |
---|
| 926 | this->searchRandomTargetPosition(); |
---|
| 927 | } |
---|
| 928 | |
---|
[3049] | 929 | bool ArtificialController::sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
---|
| 930 | { |
---|
| 931 | if (entity1 == entity2) |
---|
| 932 | return true; |
---|
| 933 | |
---|
| 934 | int team1 = -1; |
---|
| 935 | int team2 = -1; |
---|
| 936 | |
---|
[7163] | 937 | Controller* controller = 0; |
---|
| 938 | if (entity1->getController()) |
---|
| 939 | controller = entity1->getController(); |
---|
| 940 | else |
---|
| 941 | controller = entity1->getXMLController(); |
---|
| 942 | if (controller) |
---|
[3049] | 943 | { |
---|
[7163] | 944 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
| 945 | if (ac) |
---|
| 946 | team1 = ac->getTeam(); |
---|
[3049] | 947 | } |
---|
[7163] | 948 | |
---|
| 949 | if (entity2->getController()) |
---|
| 950 | controller = entity2->getController(); |
---|
| 951 | else |
---|
| 952 | controller = entity2->getXMLController(); |
---|
| 953 | if (controller) |
---|
[3049] | 954 | { |
---|
[7163] | 955 | ArtificialController* ac = orxonox_cast<ArtificialController*>(controller); |
---|
| 956 | if (ac) |
---|
| 957 | team2 = ac->getTeam(); |
---|
[3049] | 958 | } |
---|
| 959 | |
---|
[3325] | 960 | TeamDeathmatch* tdm = orxonox_cast<TeamDeathmatch*>(gametype); |
---|
[3049] | 961 | if (tdm) |
---|
| 962 | { |
---|
| 963 | if (entity1->getPlayer()) |
---|
| 964 | team1 = tdm->getTeam(entity1->getPlayer()); |
---|
| 965 | |
---|
| 966 | if (entity2->getPlayer()) |
---|
| 967 | team2 = tdm->getTeam(entity2->getPlayer()); |
---|
| 968 | } |
---|
| 969 | |
---|
[3086] | 970 | TeamBaseMatchBase* base = 0; |
---|
[3325] | 971 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
---|
[3086] | 972 | if (base) |
---|
| 973 | { |
---|
| 974 | switch (base->getState()) |
---|
| 975 | { |
---|
[3280] | 976 | case BaseState::ControlTeam1: |
---|
[3086] | 977 | team1 = 0; |
---|
| 978 | break; |
---|
[3280] | 979 | case BaseState::ControlTeam2: |
---|
[3086] | 980 | team1 = 1; |
---|
| 981 | break; |
---|
[3280] | 982 | case BaseState::Uncontrolled: |
---|
[3086] | 983 | default: |
---|
| 984 | team1 = -1; |
---|
| 985 | } |
---|
| 986 | } |
---|
[3325] | 987 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
---|
[3086] | 988 | if (base) |
---|
| 989 | { |
---|
| 990 | switch (base->getState()) |
---|
| 991 | { |
---|
[3280] | 992 | case BaseState::ControlTeam1: |
---|
[3086] | 993 | team2 = 0; |
---|
| 994 | break; |
---|
[3280] | 995 | case BaseState::ControlTeam2: |
---|
[3086] | 996 | team2 = 1; |
---|
| 997 | break; |
---|
[3280] | 998 | case BaseState::Uncontrolled: |
---|
[3086] | 999 | default: |
---|
| 1000 | team2 = -1; |
---|
| 1001 | } |
---|
| 1002 | } |
---|
| 1003 | |
---|
[7163] | 1004 | DroneController* droneController = 0; |
---|
| 1005 | droneController = orxonox_cast<DroneController*>(entity1->getController()); |
---|
| 1006 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2) |
---|
| 1007 | return true; |
---|
| 1008 | droneController = orxonox_cast<DroneController*>(entity2->getController()); |
---|
| 1009 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1) |
---|
| 1010 | return true; |
---|
| 1011 | DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController()); |
---|
| 1012 | DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController()); |
---|
| 1013 | if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner()) |
---|
| 1014 | return true; |
---|
| 1015 | |
---|
| 1016 | Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype); |
---|
| 1017 | if (dynamic) |
---|
| 1018 | { |
---|
| 1019 | if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;} |
---|
| 1020 | |
---|
| 1021 | if (entity1->getPlayer()) |
---|
| 1022 | team1 = dynamic->getParty(entity1->getPlayer()); |
---|
| 1023 | |
---|
| 1024 | if (entity2->getPlayer()) |
---|
| 1025 | team2 = dynamic->getParty(entity2->getPlayer()); |
---|
| 1026 | |
---|
| 1027 | if (team1 ==-1 ||team2 ==-1 ) {return false;} |
---|
| 1028 | else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;} |
---|
| 1029 | else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;} |
---|
| 1030 | else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;} |
---|
| 1031 | else return true; |
---|
| 1032 | } |
---|
| 1033 | |
---|
[3049] | 1034 | return (team1 == team2 && team1 != -1); |
---|
| 1035 | } |
---|
[8723] | 1036 | |
---|
| 1037 | /** |
---|
| 1038 | @brief DoFire is called when a bot should shoot and decides which weapon is used and whether the bot shoots at all. |
---|
| 1039 | */ |
---|
| 1040 | void ArtificialController::doFire() |
---|
| 1041 | { |
---|
[8762] | 1042 | if(!this->bSetupWorked)//setup: find out which weapons are active ! hard coded: laser is "0", lens flare is "1", ... |
---|
[8723] | 1043 | { |
---|
| 1044 | this->setupWeapons(); |
---|
[8736] | 1045 | if(numberOfWeapons > 0) |
---|
[8762] | 1046 | this->bSetupWorked = true; |
---|
[8723] | 1047 | } |
---|
[8736] | 1048 | else if(this->getControllableEntity() && (numberOfWeapons>0)&&this->bShooting_ && this->isCloseAtTarget((1 + 2*botlevel_)*1000) && this->isLookingAtTarget(math::pi / 20.0f)) |
---|
[8723] | 1049 | { |
---|
[8736] | 1050 | float random = rnd(1);// |
---|
[8762] | 1051 | if (this->isCloseAtTarget(130) && (weaponModes[1]>-1) ) |
---|
[8757] | 1052 | {//LENSFLARE: short range weapon |
---|
[8762] | 1053 | this->getControllableEntity()->fire(weaponModes[1]); //ai uses lens flare if they're close enough to the target |
---|
[8723] | 1054 | } |
---|
[8762] | 1055 | else if( (weaponModes[3]>-1) && this->isCloseAtTarget(400) && (projectiles[3] > 0) && (random < this->botlevel_) ) |
---|
[8723] | 1056 | {//ROCKET: mid range weapon |
---|
[8736] | 1057 | this->mode_ = ROCKET; //Vector-implementation: mode_.push_back(ROCKET); |
---|
[8762] | 1058 | this->getControllableEntity()->fire(weaponModes[3]); //launch rocket |
---|
[8736] | 1059 | if(this->getControllableEntity() && this->target_) //after fire(3) is called, getControllableEntity() refers to the rocket! |
---|
[8723] | 1060 | { |
---|
| 1061 | float speed = this->getControllableEntity()->getVelocity().length() - target_->getVelocity().length(); |
---|
| 1062 | if(!speed) speed = 0.1f; |
---|
| 1063 | float distance = target_->getPosition().length() - this->getControllableEntity()->getPosition().length(); |
---|
[8736] | 1064 | this->timeout_= distance/speed*sgn(speed*distance) + 1.8f; //predicted time of target hit (+ tolerance) |
---|
[8723] | 1065 | } |
---|
| 1066 | else |
---|
[8736] | 1067 | this->timeout_ = 4.0f; //TODO: find better default value |
---|
| 1068 | this->projectiles[3] -= 1; //decrease ammo |
---|
[8723] | 1069 | } |
---|
[8762] | 1070 | else if (weaponModes[0]>-1) //LASER: default weapon |
---|
| 1071 | this->getControllableEntity()->fire(weaponModes[0]); |
---|
[8723] | 1072 | } |
---|
| 1073 | } |
---|
| 1074 | |
---|
| 1075 | /** |
---|
| 1076 | @brief Information gathering: Which weapons are ready to use? |
---|
| 1077 | */ |
---|
| 1078 | void ArtificialController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions) |
---|
| 1079 | { |
---|
| 1080 | if(this->getControllableEntity()) |
---|
| 1081 | { |
---|
| 1082 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
---|
| 1083 | if(pawn) |
---|
| 1084 | { |
---|
[8762] | 1085 | int max = WeaponSystem::MAX_WEAPON_MODES; // assumption: there are only so many weaponmodes possible |
---|
| 1086 | for(int x=0; x<max ;x++) |
---|
| 1087 | weaponModes[x] = -1; // reset previous weapon information |
---|
| 1088 | this->numberOfWeapons = 0; |
---|
| 1089 | for(int l=0; l<max ;l++) |
---|
[8723] | 1090 | { |
---|
[8762] | 1091 | WeaponSlot* wSlot = pawn->getWeaponSlot(l); |
---|
| 1092 | if(wSlot==NULL) continue; |
---|
| 1093 | for(int i=0; i<max; i++) |
---|
[8723] | 1094 | { |
---|
[8762] | 1095 | WeaponMode* wMode = wSlot->getWeapon()->getWeaponmode(i); |
---|
| 1096 | if(wMode == NULL) continue; |
---|
| 1097 | this->numberOfWeapons++; |
---|
| 1098 | if(wMode->getIdentifier()->getName() == "HsW01") // red laser <-> 0 |
---|
| 1099 | weaponModes[0] = wMode->getMode(); |
---|
| 1100 | else if(wMode->getIdentifier()->getName() == "LightningGun") // yellow energy <-> 1 |
---|
| 1101 | weaponModes[1] = wMode->getMode(); |
---|
| 1102 | else if(wMode->getIdentifier()->getName() == "SimpleRocketFire") // target seeking rocktes <-> 2 |
---|
| 1103 | weaponModes[2] = wMode->getMode(); // not relevant jet, since doFire() doesn't support simple rockets |
---|
| 1104 | else if(wMode->getIdentifier()->getName() == "RocketFire") // manual rockets <-> 3 |
---|
| 1105 | weaponModes[3] = wMode->getMode(); |
---|
| 1106 | else |
---|
| 1107 | COUT(1)<< wMode->getIdentifier()->getName() << " has to be added in ArtificialController.cc as new weapon." << std::endl; |
---|
[8723] | 1108 | } |
---|
| 1109 | } |
---|
[8762] | 1110 | if(numberOfWeapons>0) |
---|
| 1111 | this->bSetupWorked = true; |
---|
| 1112 | }//pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user); |
---|
[8723] | 1113 | } |
---|
| 1114 | } |
---|
| 1115 | |
---|
| 1116 | |
---|
| 1117 | void ArtificialController::setBotLevel(float level) |
---|
| 1118 | { |
---|
| 1119 | if (level < 0.0f) |
---|
[8757] | 1120 | this->botlevel_ = 0.0f; |
---|
[8723] | 1121 | else if (level > 1.0f) |
---|
| 1122 | this->botlevel_ = 1.0f; |
---|
| 1123 | else |
---|
| 1124 | this->botlevel_ = level; |
---|
| 1125 | } |
---|
[8757] | 1126 | |
---|
[8723] | 1127 | void ArtificialController::setAllBotLevel(float level) |
---|
| 1128 | { |
---|
| 1129 | for (ObjectList<ArtificialController>::iterator it = ObjectList<ArtificialController>::begin(); it != ObjectList<ArtificialController>::end(); ++it) |
---|
| 1130 | it->setBotLevel(level); |
---|
| 1131 | } |
---|
| 1132 | |
---|
| 1133 | void ArtificialController::setPreviousMode() |
---|
| 1134 | { |
---|
| 1135 | this->mode_ = DEFAULT; //Vector-implementation: mode_.pop_back(); |
---|
| 1136 | } |
---|
[8757] | 1137 | |
---|
[8736] | 1138 | void ArtificialController::boostControl() |
---|
| 1139 | { |
---|
| 1140 | SpaceShip* ship = orxonox_cast<SpaceShip*>(this->getControllableEntity()); |
---|
| 1141 | if(ship == NULL) return; |
---|
[8762] | 1142 | if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower() ) //upper limit ->boost |
---|
[8736] | 1143 | this->getControllableEntity()->boost(true); |
---|
[8762] | 1144 | else if(ship->getBoostPower()*4.0f < ship->getInitialBoostPower()) //lower limit ->do not boost |
---|
[8736] | 1145 | this->getControllableEntity()->boost(false); |
---|
| 1146 | } |
---|
| 1147 | |
---|
[2362] | 1148 | } |
---|