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