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