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