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