| 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: |
|---|
| 25 | * Dominik Solenicki |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | //bug or feature? Press 4 control keys from {Q,W,E,A,S,D,C} at the same time or 3 keys from {Q,E,A,D}, spaceship goes in free fly mode |
|---|
| 29 | #include "controllers/CommonController.h" |
|---|
| 30 | #include "core/XMLPort.h" |
|---|
| 31 | |
|---|
| 32 | #include "weaponsystem/WeaponMode.h" |
|---|
| 33 | #include "weaponsystem/WeaponPack.h" |
|---|
| 34 | #include "weaponsystem/Weapon.h" |
|---|
| 35 | #include "weaponsystem/WeaponSlot.h" |
|---|
| 36 | #include "weaponsystem/WeaponSlot.h" |
|---|
| 37 | #include "worldentities/pawns/SpaceShip.h" |
|---|
| 38 | |
|---|
| 39 | #include "Scene.h" |
|---|
| 40 | |
|---|
| 41 | #include "worldentities/pawns/TeamBaseMatchBase.h" |
|---|
| 42 | #include "gametypes/TeamDeathmatch.h" |
|---|
| 43 | #include "gametypes/Dynamicmatch.h" |
|---|
| 44 | #include "gametypes/Mission.h" |
|---|
| 45 | #include "gametypes/Gametype.h" |
|---|
| 46 | #include "controllers/WaypointPatrolController.h" |
|---|
| 47 | #include "controllers/NewHumanController.h" |
|---|
| 48 | #include "controllers/DroneController.h" |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | namespace orxonox |
|---|
| 52 | { |
|---|
| 53 | |
|---|
| 54 | RegisterClass( CommonController ); |
|---|
| 55 | const float SPEED = 0.9f/0.02f; |
|---|
| 56 | const float ROTATEFACTOR = 1.0f/0.02f; |
|---|
| 57 | |
|---|
| 58 | CommonController::CommonController( Context* context ): Controller( context ) |
|---|
| 59 | { |
|---|
| 60 | this->action_ = Action::FLY; |
|---|
| 61 | this->stopLookingAtTarget(); |
|---|
| 62 | |
|---|
| 63 | RegisterObject( CommonController ); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | CommonController::~CommonController() |
|---|
| 68 | { |
|---|
| 69 | //orxout(internal_error) << "I died, my Rank is " << rank_ << endl; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void CommonController::XMLPort( Element& xmlelement, XMLPort::Mode mode ) |
|---|
| 73 | { |
|---|
| 74 | SUPER( CommonController, XMLPort, xmlelement, mode ); |
|---|
| 75 | XMLPortParam( CommonController, "formationMode", setFormationModeXML, getFormationModeXML, xmlelement, mode ); |
|---|
| 76 | XMLPortParam( CommonController, "action", setActionXML, getActionXML, xmlelement, mode ); |
|---|
| 77 | XMLPortParam ( CommonController, "protect", setProtectXML, getProtectXML, xmlelement, mode ); |
|---|
| 78 | //XMLPortParam ( CommonController, "enemy", setEnemyXML, getEnemyXML, xmlelement, mode ); |
|---|
| 79 | } |
|---|
| 80 | void CommonController::setProtectXML( std::string val ) |
|---|
| 81 | { |
|---|
| 82 | this->protectName_ = val; |
|---|
| 83 | |
|---|
| 84 | } |
|---|
| 85 | void CommonController::tick(float dt) |
|---|
| 86 | { |
|---|
| 87 | |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | std::string CommonController::getProtectXML () |
|---|
| 91 | { |
|---|
| 92 | if (!this->getProtect()) |
|---|
| 93 | return "noProtectWasSet"; |
|---|
| 94 | return this->getProtect()->getName(); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void CommonController::setProtect (ControllableEntity* protect) |
|---|
| 98 | { |
|---|
| 99 | this->protect_ = protect; |
|---|
| 100 | } |
|---|
| 101 | ControllableEntity* CommonController::getProtect () |
|---|
| 102 | { |
|---|
| 103 | return this->protect_; |
|---|
| 104 | } |
|---|
| 105 | void CommonController::setActionXML( std::string val) |
|---|
| 106 | { |
|---|
| 107 | const std::string valUpper = getUppercase( val ); |
|---|
| 108 | Action::Value value; |
|---|
| 109 | |
|---|
| 110 | if ( valUpper == "FIGHT" ) |
|---|
| 111 | value = Action::FIGHT; |
|---|
| 112 | else if ( valUpper == "FLY" ) |
|---|
| 113 | value = Action::FLY; |
|---|
| 114 | else if ( valUpper == "PROTECT" ) |
|---|
| 115 | value = Action::PROTECT; |
|---|
| 116 | else |
|---|
| 117 | ThrowException( ParseError, std::string( "Attempting to set an unknown Action: '" )+ val + "'." ); |
|---|
| 118 | this->setAction( value ); |
|---|
| 119 | } |
|---|
| 120 | std::string CommonController::getActionXML() |
|---|
| 121 | { |
|---|
| 122 | switch ( this->action_ ) |
|---|
| 123 | { |
|---|
| 124 | case Action::FIGHT: |
|---|
| 125 | { |
|---|
| 126 | return "FIGHT"; |
|---|
| 127 | break; |
|---|
| 128 | } |
|---|
| 129 | case Action::FLY: |
|---|
| 130 | { |
|---|
| 131 | return "FLY"; |
|---|
| 132 | break; |
|---|
| 133 | } |
|---|
| 134 | case Action::PROTECT: |
|---|
| 135 | { |
|---|
| 136 | return "PROTECT"; |
|---|
| 137 | break; |
|---|
| 138 | } |
|---|
| 139 | default: |
|---|
| 140 | return "FIGHT"; |
|---|
| 141 | break; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | void CommonController::setFormationModeXML( std::string val ) |
|---|
| 145 | { |
|---|
| 146 | const std::string valUpper = getUppercase( val ); |
|---|
| 147 | FormationMode::Value value; |
|---|
| 148 | |
|---|
| 149 | if ( valUpper == "WALL" ) |
|---|
| 150 | value = FormationMode::WALL; |
|---|
| 151 | else if ( valUpper == "FINGER4" ) |
|---|
| 152 | value = FormationMode::FINGER4; |
|---|
| 153 | else if ( valUpper == "DIAMOND" ) |
|---|
| 154 | value = FormationMode::DIAMOND; |
|---|
| 155 | else |
|---|
| 156 | ThrowException( ParseError, std::string( "Attempting to set an unknown FormationMode: '" )+ val + "'." ); |
|---|
| 157 | this->setFormationMode( value ); |
|---|
| 158 | |
|---|
| 159 | } |
|---|
| 160 | std::string CommonController::getFormationModeXML() |
|---|
| 161 | { |
|---|
| 162 | switch ( this->formationMode_ ) |
|---|
| 163 | { |
|---|
| 164 | case FormationMode::WALL: |
|---|
| 165 | { |
|---|
| 166 | return "WALL"; |
|---|
| 167 | break; |
|---|
| 168 | } |
|---|
| 169 | case FormationMode::FINGER4: |
|---|
| 170 | { |
|---|
| 171 | return "FINGER4"; |
|---|
| 172 | break; |
|---|
| 173 | } |
|---|
| 174 | case FormationMode::DIAMOND: |
|---|
| 175 | { |
|---|
| 176 | return "DIAMOND"; |
|---|
| 177 | break; |
|---|
| 178 | } |
|---|
| 179 | default: |
|---|
| 180 | return "DIAMOND"; |
|---|
| 181 | break; |
|---|
| 182 | |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | Action::Value CommonController::getAction () |
|---|
| 186 | { |
|---|
| 187 | return this->action_; |
|---|
| 188 | } |
|---|
| 189 | void CommonController::setAction (Action::Value action) |
|---|
| 190 | { |
|---|
| 191 | this->action_ = action; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | void CommonController::setAction (Action::Value action, ControllableEntity* target) |
|---|
| 195 | { |
|---|
| 196 | this->action_ = action; |
|---|
| 197 | if (action == Action::FIGHT) |
|---|
| 198 | { |
|---|
| 199 | if (target) |
|---|
| 200 | this->setTarget (target); |
|---|
| 201 | } |
|---|
| 202 | else if (action == Action::PROTECT) |
|---|
| 203 | { |
|---|
| 204 | if (target) |
|---|
| 205 | this->setProtect (target); |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | void CommonController::setAction (Action::Value action, const Vector3& target) |
|---|
| 209 | { |
|---|
| 210 | this->action_ = action; |
|---|
| 211 | if (action == Action::FLY) |
|---|
| 212 | { |
|---|
| 213 | this->setTargetPosition (target); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | } |
|---|
| 217 | void CommonController::setAction (Action::Value action, const Vector3& target, const Quaternion& orient ) |
|---|
| 218 | { |
|---|
| 219 | this->action_ = action; |
|---|
| 220 | if (action == Action::FLY) |
|---|
| 221 | { |
|---|
| 222 | this->setTargetPosition (target); |
|---|
| 223 | this->setTargetOrientation (orient); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | } |
|---|
| 227 | void CommonController::setClosestTarget() |
|---|
| 228 | { |
|---|
| 229 | if (!this->getControllableEntity()) |
|---|
| 230 | return; |
|---|
| 231 | |
|---|
| 232 | Pawn* closestTarget = 0; |
|---|
| 233 | float minDistance = std::numeric_limits<float>::infinity(); |
|---|
| 234 | Gametype* gt = this->getGametype(); |
|---|
| 235 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
|---|
| 236 | { |
|---|
| 237 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) ) |
|---|
| 238 | continue; |
|---|
| 239 | |
|---|
| 240 | float distance = CommonController::distance (*itP, this->getControllableEntity()); |
|---|
| 241 | if (distance < minDistance) |
|---|
| 242 | { |
|---|
| 243 | closestTarget = *itP; |
|---|
| 244 | minDistance = distance; |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | if (closestTarget) |
|---|
| 248 | { |
|---|
| 249 | (this)->setTarget(static_cast<ControllableEntity*>(closestTarget)); |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | void CommonController::maneuver() |
|---|
| 253 | { |
|---|
| 254 | maneuverCounter_++; |
|---|
| 255 | |
|---|
| 256 | if (maneuverCounter_ > 5) |
|---|
| 257 | maneuverCounter_ = 0; |
|---|
| 258 | if ( this->target_ && this->getControllableEntity()) |
|---|
| 259 | { |
|---|
| 260 | Vector3 thisPosition = this->getControllableEntity()->getWorldPosition(); |
|---|
| 261 | //Quaternion thisOrientation = this->getControllableEntity()->getOrientation(); |
|---|
| 262 | |
|---|
| 263 | this->setPositionOfTarget( getPredictedPosition( |
|---|
| 264 | thisPosition, |
|---|
| 265 | hardcoded_projectile_speed, |
|---|
| 266 | this->target_->getWorldPosition() , |
|---|
| 267 | this->target_->getVelocity() |
|---|
| 268 | ) ); |
|---|
| 269 | this->setOrientationOfTarget( this->target_->getOrientation() ); |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | Vector3 diffVector = this->positionOfTarget_ - thisPosition; |
|---|
| 273 | float diffLength = diffVector.length(); |
|---|
| 274 | Vector3 diffUnit = diffVector/diffLength; |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | //bool bThisIsLookingAtTarget = this->isLooking ( getControllableEntity(), this->target_, math::pi/4 ); |
|---|
| 279 | bool bTargetIsLookingAtThis = this->isLooking ( this->target_, getControllableEntity(), math::pi/10.0f ); |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | //too far? well, come closer then |
|---|
| 284 | if ( diffLength > 3000 ) |
|---|
| 285 | { |
|---|
| 286 | if (diffLength < 6000) |
|---|
| 287 | { |
|---|
| 288 | |
|---|
| 289 | } |
|---|
| 290 | else |
|---|
| 291 | { |
|---|
| 292 | } |
|---|
| 293 | this->setTargetPosition( this->positionOfTarget_ ); |
|---|
| 294 | } |
|---|
| 295 | //too close? How do u expect to dodge anything? Just attack! |
|---|
| 296 | else if ( diffLength < 500 ) |
|---|
| 297 | { |
|---|
| 298 | //at this point, just look and shoot |
|---|
| 299 | if ( diffLength < 250 ) |
|---|
| 300 | { |
|---|
| 301 | this->stopMoving(); |
|---|
| 302 | this->startLookingAtTarget(); |
|---|
| 303 | } |
|---|
| 304 | else |
|---|
| 305 | { |
|---|
| 306 | this->setTargetPosition( this->positionOfTarget_ ); |
|---|
| 307 | } |
|---|
| 308 | } |
|---|
| 309 | //Good distance? Check if target looks at us. It doesn't? Go hunt! |
|---|
| 310 | else if ( !bTargetIsLookingAtThis ) |
|---|
| 311 | { |
|---|
| 312 | this->setTargetPosition( this->positionOfTarget_ ); |
|---|
| 313 | /* if (maneuverCounter_ == 0) |
|---|
| 314 | { |
|---|
| 315 | this->setTargetPosition( this->positionOfTarget_ ); |
|---|
| 316 | return; |
|---|
| 317 | } |
|---|
| 318 | else |
|---|
| 319 | { |
|---|
| 320 | dodge( thisPosition, diffUnit ); |
|---|
| 321 | }*/ |
|---|
| 322 | } |
|---|
| 323 | //That's unfortunate, he is looking and probably shooting... try to dodge what we can... |
|---|
| 324 | else |
|---|
| 325 | { |
|---|
| 326 | |
|---|
| 327 | if (maneuverCounter_ == 0) |
|---|
| 328 | { |
|---|
| 329 | this->setTargetPosition( this->positionOfTarget_ ); |
|---|
| 330 | return; |
|---|
| 331 | } |
|---|
| 332 | dodge( thisPosition, diffUnit ); |
|---|
| 333 | |
|---|
| 334 | } |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | //orxout ( internal_error ) << "ManeuverType = " << this->maneuverType_ << endl; |
|---|
| 338 | } |
|---|
| 339 | ControllableEntity* CommonController::getTarget() |
|---|
| 340 | { |
|---|
| 341 | return this->target_; |
|---|
| 342 | } |
|---|
| 343 | void CommonController::dodge(Vector3& thisPosition, Vector3& diffUnit) |
|---|
| 344 | { |
|---|
| 345 | float factorX = 0, factorY = 0, factorZ = 0; |
|---|
| 346 | float rand = randomInRange (0, 1); |
|---|
| 347 | if (rand <= 0.5) |
|---|
| 348 | { |
|---|
| 349 | factorX = 1; |
|---|
| 350 | } |
|---|
| 351 | else |
|---|
| 352 | { |
|---|
| 353 | factorX = -1; |
|---|
| 354 | } |
|---|
| 355 | rand = randomInRange (0, 1); |
|---|
| 356 | if (rand <= 0.5) |
|---|
| 357 | { |
|---|
| 358 | factorY = 1; |
|---|
| 359 | } |
|---|
| 360 | else |
|---|
| 361 | { |
|---|
| 362 | factorY = -1; |
|---|
| 363 | } |
|---|
| 364 | rand = randomInRange (0, 1); |
|---|
| 365 | if (rand <= 0.5) |
|---|
| 366 | { |
|---|
| 367 | factorZ = 1; |
|---|
| 368 | } |
|---|
| 369 | else |
|---|
| 370 | { |
|---|
| 371 | factorZ = -1; |
|---|
| 372 | } |
|---|
| 373 | Vector3 target = ( diffUnit )* 8000.0f; |
|---|
| 374 | Vector3* randVector = new Vector3( |
|---|
| 375 | factorX * randomInRange( 10000, 40000 ), |
|---|
| 376 | factorY * randomInRange( 10000, 40000 ), |
|---|
| 377 | factorZ * randomInRange( 10000, 40000 ) |
|---|
| 378 | ); |
|---|
| 379 | Vector3 projection = randVector->dotProduct( diffUnit )* diffUnit; |
|---|
| 380 | *randVector -= projection; |
|---|
| 381 | target += *randVector; |
|---|
| 382 | this->setTargetPosition( thisPosition + target ); |
|---|
| 383 | } |
|---|
| 384 | void CommonController::stopMoving() |
|---|
| 385 | { |
|---|
| 386 | this->bHasTargetPosition_ = false; |
|---|
| 387 | } |
|---|
| 388 | void CommonController::startLookingAtTarget() |
|---|
| 389 | { |
|---|
| 390 | this->bLookAtTarget_ = true; |
|---|
| 391 | } |
|---|
| 392 | void CommonController::stopLookingAtTarget() |
|---|
| 393 | { |
|---|
| 394 | this->bLookAtTarget_ = false; |
|---|
| 395 | } |
|---|
| 396 | void CommonController::lookAtTarget(float dt) |
|---|
| 397 | { |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | ControllableEntity* entity = this->getControllableEntity(); |
|---|
| 401 | if ( !entity ) |
|---|
| 402 | return; |
|---|
| 403 | Vector2 coord = get2DViewCoordinates |
|---|
| 404 | ( entity->getPosition() , |
|---|
| 405 | entity->getOrientation() * WorldEntity::FRONT, |
|---|
| 406 | entity->getOrientation() * WorldEntity::UP, |
|---|
| 407 | positionOfTarget_ ); |
|---|
| 408 | |
|---|
| 409 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
|---|
| 410 | float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); |
|---|
| 411 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | //Yaw and Pitch are enough to start facing the target |
|---|
| 416 | this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt ); |
|---|
| 417 | this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt ); |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | bool CommonController::setWingman ( CommonController* wingman ) |
|---|
| 423 | { |
|---|
| 424 | return false; |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | bool CommonController::hasWingman() |
|---|
| 428 | { |
|---|
| 429 | return true; |
|---|
| 430 | } |
|---|
| 431 | void CommonController::setTarget( ControllableEntity* target ) |
|---|
| 432 | { |
|---|
| 433 | this->target_ = target; |
|---|
| 434 | //orxout ( internal_error ) << " TARGET SET " << endl; |
|---|
| 435 | |
|---|
| 436 | if ( this->target_ ) |
|---|
| 437 | { |
|---|
| 438 | this->setPositionOfTarget( target_->getWorldPosition() ); |
|---|
| 439 | |
|---|
| 440 | } |
|---|
| 441 | } |
|---|
| 442 | bool CommonController::hasTarget() |
|---|
| 443 | { |
|---|
| 444 | if ( this->target_ ) |
|---|
| 445 | return true; |
|---|
| 446 | return false; |
|---|
| 447 | } |
|---|
| 448 | void CommonController::setPositionOfTarget( const Vector3& target ) |
|---|
| 449 | { |
|---|
| 450 | this->positionOfTarget_ = target; |
|---|
| 451 | this->bHasPositionOfTarget_ = true; |
|---|
| 452 | } |
|---|
| 453 | void CommonController::setOrientationOfTarget( const Quaternion& orient ) |
|---|
| 454 | { |
|---|
| 455 | this->orientationOfTarget_=orient; |
|---|
| 456 | this->bHasOrientationOfTarget_=true; |
|---|
| 457 | } |
|---|
| 458 | |
|---|
| 459 | void CommonController::setTargetPosition( const Vector3& target ) |
|---|
| 460 | { |
|---|
| 461 | this->targetPosition_ = target; |
|---|
| 462 | this->bHasTargetPosition_ = true; |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | void CommonController::setTargetOrientation( const Quaternion& orient ) |
|---|
| 466 | { |
|---|
| 467 | this->targetOrientation_=orient; |
|---|
| 468 | this->bHasTargetOrientation_=true; |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | void CommonController::setTargetOrientation( ControllableEntity* target ) |
|---|
| 472 | { |
|---|
| 473 | if ( target ) |
|---|
| 474 | setTargetOrientation( target->getOrientation() ); |
|---|
| 475 | } |
|---|
| 476 | |
|---|
| 477 | |
|---|
| 478 | |
|---|
| 479 | //copy the Roll orientation of given Quaternion. |
|---|
| 480 | void CommonController::copyOrientation( const Quaternion& orient, float dt ) |
|---|
| 481 | { |
|---|
| 482 | //roll angle difference in radian |
|---|
| 483 | float diff=orient.getRoll( false ).valueRadians() -( this->getControllableEntity() ->getOrientation() .getRoll( false ).valueRadians() ); |
|---|
| 484 | while( diff>math::twoPi )diff-=math::twoPi; |
|---|
| 485 | while( diff<-math::twoPi )diff+=math::twoPi; |
|---|
| 486 | this->getControllableEntity() ->rotateRoll( diff*ROTATEFACTOR * dt ); |
|---|
| 487 | } |
|---|
| 488 | void CommonController::copyTargetOrientation( float dt ) |
|---|
| 489 | { |
|---|
| 490 | if ( bHasTargetOrientation_ ) |
|---|
| 491 | { |
|---|
| 492 | copyOrientation( targetOrientation_, dt ); |
|---|
| 493 | } |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | |
|---|
| 498 | |
|---|
| 499 | void CommonController::moveToTargetPosition( float dt ) |
|---|
| 500 | { |
|---|
| 501 | this->moveToPosition( this->targetPosition_, dt ); |
|---|
| 502 | } |
|---|
| 503 | void CommonController::moveToPosition( const Vector3& target, float dt ) |
|---|
| 504 | { |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | //100 is ( so far )the smallest tolerance ( empirically found )that can be reached, |
|---|
| 508 | //with smaller distance spaceships can't reach position and go circles around it instead |
|---|
| 509 | int tolerance = 65; |
|---|
| 510 | |
|---|
| 511 | ControllableEntity* entity = this->getControllableEntity(); |
|---|
| 512 | Vector2 coord = get2DViewCoordinates |
|---|
| 513 | ( entity->getPosition() , |
|---|
| 514 | entity->getOrientation() * WorldEntity::FRONT, |
|---|
| 515 | entity->getOrientation() * WorldEntity::UP, |
|---|
| 516 | target ); |
|---|
| 517 | |
|---|
| 518 | float distance = ( target - this->getControllableEntity() ->getPosition() ).length(); |
|---|
| 519 | |
|---|
| 520 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
|---|
| 521 | float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); |
|---|
| 522 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | if ( distance > tolerance ) |
|---|
| 526 | { |
|---|
| 527 | //Yaw and Pitch are enough to start facing the target |
|---|
| 528 | this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt ); |
|---|
| 529 | this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt ); |
|---|
| 530 | |
|---|
| 531 | //300 works, maybe less is better |
|---|
| 532 | if ( distance < 400 ) |
|---|
| 533 | { |
|---|
| 534 | //Change roll when close. When Spaceship faces target, roll doesn't affect it's trajectory |
|---|
| 535 | //It's important that roll is not changed in the process of changing yaw and pitch |
|---|
| 536 | //Wingmen won't face same direction as Leaders, but when Leaders start moving |
|---|
| 537 | //Yaw and Pitch will adapt. |
|---|
| 538 | if ( bHasTargetOrientation_ ) |
|---|
| 539 | { |
|---|
| 540 | copyTargetOrientation( dt ); |
|---|
| 541 | } |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | this->getControllableEntity() ->moveFrontBack( SPEED * dt ); |
|---|
| 545 | } |
|---|
| 546 | else |
|---|
| 547 | { |
|---|
| 548 | bHasTargetPosition_ = false; |
|---|
| 549 | bHasTargetOrientation_ = false; |
|---|
| 550 | } |
|---|
| 551 | } |
|---|
| 552 | float CommonController::randomInRange( float a, float b ) |
|---|
| 553 | { |
|---|
| 554 | float random = rnd( 1.0f ); |
|---|
| 555 | float diff = b - a; |
|---|
| 556 | float r = random * diff; |
|---|
| 557 | return a + r; |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | //to be called in action |
|---|
| 562 | //PRE: relativeTargetPosition is desired position relative to the spaceship, |
|---|
| 563 | //angleRoll is the angle in degrees of Roll that should be applied by the end of the movement |
|---|
| 564 | //POST: target orientation and position are set, so that it can be used by MoveAndRoll() |
|---|
| 565 | void CommonController::moveToPoint( const Vector3& relativeTargetPosition, float angleRoll ) |
|---|
| 566 | { |
|---|
| 567 | ControllableEntity* entity = this->getControllableEntity(); |
|---|
| 568 | if ( !entity ) |
|---|
| 569 | return; |
|---|
| 570 | Quaternion orient = entity->getWorldOrientation(); |
|---|
| 571 | Quaternion rotation = Quaternion( Degree( angleRoll ), Vector3::UNIT_Z ); |
|---|
| 572 | |
|---|
| 573 | Vector3 target = orient * relativeTargetPosition + entity->getWorldPosition(); |
|---|
| 574 | setTargetPosition( target ); |
|---|
| 575 | orient = orient * rotation; |
|---|
| 576 | this->setTargetOrientation( orient ); |
|---|
| 577 | |
|---|
| 578 | } |
|---|
| 579 | //to be called in tick |
|---|
| 580 | //PRE: MoveToPoint was called |
|---|
| 581 | //POST: spaceship changes its yaw and pitch to point towards targetPosition_, |
|---|
| 582 | //moves towards targetPosition_ by amount depending on dt and its speed, |
|---|
| 583 | //rolls by amount depending on the difference between angleRoll_ and angleRolled_, dt, and |
|---|
| 584 | //angular speed |
|---|
| 585 | //if position reached with a certain tolerance, and angleRolled_ = angleRoll_, returns false, |
|---|
| 586 | //otherwise returns true |
|---|
| 587 | //dt is normally around 0.02f, which makes it 1/0.02 = 50 frames/sec |
|---|
| 588 | bool CommonController::moveAndRoll( float dt ) |
|---|
| 589 | { |
|---|
| 590 | float factor = 1; |
|---|
| 591 | if ( !this->getControllableEntity() ) |
|---|
| 592 | return false; |
|---|
| 593 | if ( this->rank_ == Rank::DIVISIONLEADER ) |
|---|
| 594 | factor = 0.8; |
|---|
| 595 | if ( this->rank_ == Rank::SECTIONLEADER ) |
|---|
| 596 | factor = 0.9; |
|---|
| 597 | int tolerance = 60; |
|---|
| 598 | |
|---|
| 599 | ControllableEntity* entity = this->getControllableEntity(); |
|---|
| 600 | if ( !entity ) |
|---|
| 601 | return true; |
|---|
| 602 | Vector2 coord = get2DViewCoordinates |
|---|
| 603 | ( entity->getPosition() , |
|---|
| 604 | entity->getOrientation() * WorldEntity::FRONT, |
|---|
| 605 | entity->getOrientation() * WorldEntity::UP, |
|---|
| 606 | targetPosition_ ); |
|---|
| 607 | |
|---|
| 608 | float distance = ( targetPosition_ - this->getControllableEntity() ->getPosition() ).length(); |
|---|
| 609 | |
|---|
| 610 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
|---|
| 611 | float rotateX = clamp( coord.x * 10, -1.0f, 1.0f ); |
|---|
| 612 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
|---|
| 613 | |
|---|
| 614 | |
|---|
| 615 | if ( distance > tolerance ) |
|---|
| 616 | { |
|---|
| 617 | //Yaw and Pitch are enough to start facing the target |
|---|
| 618 | this->getControllableEntity() ->rotateYaw( -2.0f * ROTATEFACTOR * rotateX * dt ); |
|---|
| 619 | this->getControllableEntity() ->rotatePitch( 2.0f * ROTATEFACTOR * rotateY * dt ); |
|---|
| 620 | |
|---|
| 621 | //Roll |
|---|
| 622 | if ( bHasTargetOrientation_ ) |
|---|
| 623 | { |
|---|
| 624 | copyTargetOrientation( dt ); |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | //Move |
|---|
| 628 | this->getControllableEntity() ->moveFrontBack( 1.2f * SPEED * factor * dt ); |
|---|
| 629 | //if still moving, return false |
|---|
| 630 | return false; |
|---|
| 631 | } |
|---|
| 632 | else |
|---|
| 633 | { |
|---|
| 634 | |
|---|
| 635 | //if finished, return true; |
|---|
| 636 | return true; |
|---|
| 637 | } |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | float CommonController::squaredDistanceToTarget() const |
|---|
| 641 | { |
|---|
| 642 | if ( !this->getControllableEntity() ) |
|---|
| 643 | return 0; |
|---|
| 644 | if ( !this->target_ || !this->getControllableEntity() ) |
|---|
| 645 | return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->targetPosition_ ) ); |
|---|
| 646 | else |
|---|
| 647 | return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->positionOfTarget_ ) ); |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | bool CommonController::isLookingAtTarget( float angle )const |
|---|
| 651 | { |
|---|
| 652 | if ( !this->getControllableEntity() || !this->target_ ) |
|---|
| 653 | return false; |
|---|
| 654 | |
|---|
| 655 | return ( getAngle( this->getControllableEntity() ->getPosition() , |
|---|
| 656 | this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT, this->positionOfTarget_ ) < angle ); |
|---|
| 657 | } |
|---|
| 658 | bool CommonController::isLooking( ControllableEntity* entityThatLooks, ControllableEntity* entityBeingLookedAt, float angle ) |
|---|
| 659 | { |
|---|
| 660 | if ( !entityThatLooks || !entityBeingLookedAt ) |
|---|
| 661 | return false; |
|---|
| 662 | return ( getAngle( entityThatLooks ->getPosition() , |
|---|
| 663 | entityThatLooks->getOrientation() * WorldEntity::FRONT, |
|---|
| 664 | entityBeingLookedAt->getWorldPosition() ) < angle ); |
|---|
| 665 | } |
|---|
| 666 | |
|---|
| 667 | bool CommonController::canFire() |
|---|
| 668 | { |
|---|
| 669 | |
|---|
| 670 | //no target? why fire? |
|---|
| 671 | if ( !this->target_ ) |
|---|
| 672 | return false; |
|---|
| 673 | |
|---|
| 674 | Vector3 newPositionOfTarget = getPredictedPosition( this->getControllableEntity() ->getWorldPosition() , |
|---|
| 675 | hardcoded_projectile_speed, this->target_->getWorldPosition() , this->target_->getVelocity() ); |
|---|
| 676 | if ( newPositionOfTarget != Vector3::ZERO ) |
|---|
| 677 | { |
|---|
| 678 | this->setPositionOfTarget( newPositionOfTarget ); |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | float squaredDistance = squaredDistanceToTarget(); |
|---|
| 682 | |
|---|
| 683 | if ( squaredDistance < 9000000.0f && this->isLookingAtTarget( math::pi / 20.0f)) { |
|---|
| 684 | return true; |
|---|
| 685 | } |
|---|
| 686 | else |
|---|
| 687 | { |
|---|
| 688 | return false; |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | } |
|---|
| 692 | float CommonController::distance (ControllableEntity* entity1, ControllableEntity* entity2) |
|---|
| 693 | { |
|---|
| 694 | if (!entity1 || !entity2) |
|---|
| 695 | return std::numeric_limits<float>::infinity(); |
|---|
| 696 | return ( entity1->getPosition() - entity2->getPosition() ).length(); |
|---|
| 697 | } |
|---|
| 698 | bool CommonController::sameTeam (ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype) |
|---|
| 699 | { |
|---|
| 700 | /*if (!entity1 || !entity2) |
|---|
| 701 | return false; |
|---|
| 702 | return entity1->getTeam() == entity2->getTeam();*/ |
|---|
| 703 | if (entity1 == entity2) |
|---|
| 704 | return true; |
|---|
| 705 | |
|---|
| 706 | int team1 = entity1->getTeam(); |
|---|
| 707 | int team2 = entity2->getTeam(); |
|---|
| 708 | |
|---|
| 709 | Controller* controller = 0; |
|---|
| 710 | if (entity1->getController()) |
|---|
| 711 | controller = entity1->getController(); |
|---|
| 712 | else |
|---|
| 713 | controller = entity1->getXMLController(); |
|---|
| 714 | if (controller) |
|---|
| 715 | { |
|---|
| 716 | CommonController* ac = orxonox_cast<CommonController*>(controller); |
|---|
| 717 | if (ac) |
|---|
| 718 | team1 = ac->getTeam(); |
|---|
| 719 | } |
|---|
| 720 | |
|---|
| 721 | if (entity2->getController()) |
|---|
| 722 | controller = entity2->getController(); |
|---|
| 723 | else |
|---|
| 724 | controller = entity2->getXMLController(); |
|---|
| 725 | if (controller) |
|---|
| 726 | { |
|---|
| 727 | CommonController* ac = orxonox_cast<CommonController*>(controller); |
|---|
| 728 | if (ac) |
|---|
| 729 | team2 = ac->getTeam(); |
|---|
| 730 | } |
|---|
| 731 | |
|---|
| 732 | TeamGametype* tdm = orxonox_cast<TeamGametype*>(gametype); |
|---|
| 733 | if (tdm) |
|---|
| 734 | { |
|---|
| 735 | if (entity1->getPlayer()) |
|---|
| 736 | team1 = tdm->getTeam(entity1->getPlayer()); |
|---|
| 737 | |
|---|
| 738 | if (entity2->getPlayer()) |
|---|
| 739 | team2 = tdm->getTeam(entity2->getPlayer()); |
|---|
| 740 | } |
|---|
| 741 | |
|---|
| 742 | TeamBaseMatchBase* base = 0; |
|---|
| 743 | base = orxonox_cast<TeamBaseMatchBase*>(entity1); |
|---|
| 744 | if (base) |
|---|
| 745 | { |
|---|
| 746 | switch (base->getState()) |
|---|
| 747 | { |
|---|
| 748 | case BaseState::ControlTeam1: |
|---|
| 749 | team1 = 0; |
|---|
| 750 | break; |
|---|
| 751 | case BaseState::ControlTeam2: |
|---|
| 752 | team1 = 1; |
|---|
| 753 | break; |
|---|
| 754 | case BaseState::Uncontrolled: |
|---|
| 755 | default: |
|---|
| 756 | team1 = -1; |
|---|
| 757 | } |
|---|
| 758 | } |
|---|
| 759 | base = orxonox_cast<TeamBaseMatchBase*>(entity2); |
|---|
| 760 | if (base) |
|---|
| 761 | { |
|---|
| 762 | switch (base->getState()) |
|---|
| 763 | { |
|---|
| 764 | case BaseState::ControlTeam1: |
|---|
| 765 | team2 = 0; |
|---|
| 766 | break; |
|---|
| 767 | case BaseState::ControlTeam2: |
|---|
| 768 | team2 = 1; |
|---|
| 769 | break; |
|---|
| 770 | case BaseState::Uncontrolled: |
|---|
| 771 | default: |
|---|
| 772 | team2 = -1; |
|---|
| 773 | } |
|---|
| 774 | } |
|---|
| 775 | |
|---|
| 776 | DroneController* droneController = 0; |
|---|
| 777 | droneController = orxonox_cast<DroneController*>(entity1->getController()); |
|---|
| 778 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity2) |
|---|
| 779 | return true; |
|---|
| 780 | droneController = orxonox_cast<DroneController*>(entity2->getController()); |
|---|
| 781 | if (droneController && static_cast<ControllableEntity*>(droneController->getOwner()) == entity1) |
|---|
| 782 | return true; |
|---|
| 783 | DroneController* droneController1 = orxonox_cast<DroneController*>(entity1->getController()); |
|---|
| 784 | DroneController* droneController2 = orxonox_cast<DroneController*>(entity2->getController()); |
|---|
| 785 | if (droneController1 && droneController2 && droneController1->getOwner() == droneController2->getOwner()) |
|---|
| 786 | return true; |
|---|
| 787 | |
|---|
| 788 | Dynamicmatch* dynamic = orxonox_cast<Dynamicmatch*>(gametype); |
|---|
| 789 | if (dynamic) |
|---|
| 790 | { |
|---|
| 791 | if (dynamic->notEnoughPigs||dynamic->notEnoughKillers||dynamic->notEnoughChasers) {return false;} |
|---|
| 792 | |
|---|
| 793 | if (entity1->getPlayer()) |
|---|
| 794 | team1 = dynamic->getParty(entity1->getPlayer()); |
|---|
| 795 | |
|---|
| 796 | if (entity2->getPlayer()) |
|---|
| 797 | team2 = dynamic->getParty(entity2->getPlayer()); |
|---|
| 798 | |
|---|
| 799 | if (team1 ==-1 ||team2 ==-1 ) {return false;} |
|---|
| 800 | else if (team1 == dynamic->chaser && team2 != dynamic->chaser) {return false;} |
|---|
| 801 | else if (team1 == dynamic->piggy && team2 == dynamic->chaser) {return false;} |
|---|
| 802 | else if (team1 == dynamic->killer && team2 == dynamic->chaser) {return false;} |
|---|
| 803 | else return true; |
|---|
| 804 | } |
|---|
| 805 | |
|---|
| 806 | return (team1 == team2 && team1 != -1); |
|---|
| 807 | } |
|---|
| 808 | void CommonController::doFire() |
|---|
| 809 | { |
|---|
| 810 | if ( !this->target_ || !this->getControllableEntity() ) |
|---|
| 811 | { |
|---|
| 812 | return; |
|---|
| 813 | } |
|---|
| 814 | |
|---|
| 815 | Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() ); |
|---|
| 816 | |
|---|
| 817 | if ( pawn ) |
|---|
| 818 | //pawn->setAimPosition( this->getControllableEntity() ->getWorldPosition() + 4000*( this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT )); |
|---|
| 819 | pawn->setAimPosition( this->positionOfTarget_ ); |
|---|
| 820 | |
|---|
| 821 | this->getControllableEntity() ->fire( 0 ); |
|---|
| 822 | } |
|---|
| 823 | |
|---|
| 824 | |
|---|
| 825 | } |
|---|