| 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 | * Gani Aliguzhinov |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * Fabian 'x3n' Landau, Dominik Solenicki |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | #include "controllers/FightingController.h" |
|---|
| 29 | #include "util/Math.h" |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | #include "worldentities/pawns/SpaceShip.h" |
|---|
| 33 | |
|---|
| 34 | #include "weaponsystem/WeaponMode.h" |
|---|
| 35 | #include "weaponsystem/WeaponPack.h" |
|---|
| 36 | #include "weaponsystem/Weapon.h" |
|---|
| 37 | #include "weaponsystem/WeaponSlot.h" |
|---|
| 38 | #include "weaponsystem/WeaponSystem.h" |
|---|
| 39 | #include "weaponsystem/Munition.h" |
|---|
| 40 | |
|---|
| 41 | namespace orxonox |
|---|
| 42 | { |
|---|
| 43 | |
|---|
| 44 | RegisterClass (FightingController); |
|---|
| 45 | |
|---|
| 46 | FightingController::FightingController( Context* context ): FlyingController( context ) |
|---|
| 47 | { |
|---|
| 48 | this->attackRange_ = 2500; |
|---|
| 49 | this->stopLookingAtTarget(); |
|---|
| 50 | this->bSetupWorked = false; |
|---|
| 51 | this->timeout_ = 0; |
|---|
| 52 | RegisterObject( FightingController ); |
|---|
| 53 | } |
|---|
| 54 | FightingController::~FightingController() |
|---|
| 55 | { |
|---|
| 56 | |
|---|
| 57 | } |
|---|
| 58 | void FightingController::lookAtTarget(float dt) |
|---|
| 59 | { |
|---|
| 60 | if (!this || !this->getControllableEntity()) |
|---|
| 61 | return; |
|---|
| 62 | ControllableEntity* entity = this->getControllableEntity(); |
|---|
| 63 | if ( !entity ) |
|---|
| 64 | return; |
|---|
| 65 | Vector2 coord = get2DViewCoordinates |
|---|
| 66 | ( entity->getPosition() , |
|---|
| 67 | entity->getOrientation() * WorldEntity::FRONT, |
|---|
| 68 | entity->getOrientation() * WorldEntity::UP, |
|---|
| 69 | positionOfTarget_ ); |
|---|
| 70 | |
|---|
| 71 | //rotates should be in range [-1,+1], clamp cuts off all that is not |
|---|
| 72 | float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f ); |
|---|
| 73 | float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); |
|---|
| 74 | |
|---|
| 75 | //Yaw and Pitch are enough to start facing the target |
|---|
| 76 | this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt ); |
|---|
| 77 | this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt ); |
|---|
| 78 | } |
|---|
| 79 | void FightingController::stopLookingAtTarget() |
|---|
| 80 | { |
|---|
| 81 | this->bLookAtTarget_ = false; |
|---|
| 82 | } |
|---|
| 83 | void FightingController::startLookingAtTarget() |
|---|
| 84 | { |
|---|
| 85 | this->bLookAtTarget_ = true; |
|---|
| 86 | } |
|---|
| 87 | bool FightingController::hasTarget() const |
|---|
| 88 | { |
|---|
| 89 | if ( this->target_ ) |
|---|
| 90 | return true; |
|---|
| 91 | return false; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void FightingController::setTarget( ControllableEntity* target ) |
|---|
| 95 | { |
|---|
| 96 | this->target_ = target; |
|---|
| 97 | if ( this->target_ ) |
|---|
| 98 | { |
|---|
| 99 | this->setPositionOfTarget( target_->getWorldPosition() ); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | void FightingController::setPositionOfTarget( const Vector3& target ) |
|---|
| 103 | { |
|---|
| 104 | this->positionOfTarget_ = target; |
|---|
| 105 | this->bHasPositionOfTarget_ = true; |
|---|
| 106 | } |
|---|
| 107 | void FightingController::setOrientationOfTarget( const Quaternion& orient ) |
|---|
| 108 | { |
|---|
| 109 | this->orientationOfTarget_=orient; |
|---|
| 110 | this->bHasOrientationOfTarget_=true; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void FightingController::maneuver() |
|---|
| 114 | { |
|---|
| 115 | if ( !this->target_ || !this->getControllableEntity()) |
|---|
| 116 | return; |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | Vector3 thisPosition = this->getControllableEntity()->getWorldPosition(); |
|---|
| 120 | this->setPositionOfTarget(this->target_->getWorldPosition()); |
|---|
| 121 | //this->setOrientationOfTarget(this->target_->getOrientation()); |
|---|
| 122 | Vector3 diffVector = this->positionOfTarget_ - thisPosition; |
|---|
| 123 | float diffLength = diffVector.length(); |
|---|
| 124 | Vector3 diffUnit = diffVector/diffLength; |
|---|
| 125 | |
|---|
| 126 | if (!this || !this->getControllableEntity()) |
|---|
| 127 | return; |
|---|
| 128 | |
|---|
| 129 | //too far? well, come closer then |
|---|
| 130 | if (diffLength > this->attackRange_) |
|---|
| 131 | { |
|---|
| 132 | this->spread_ = 400; |
|---|
| 133 | this->formationMode_ = FormationMode::DIAMOND; |
|---|
| 134 | this->bKeepFormation_ = true; |
|---|
| 135 | |
|---|
| 136 | this->setTargetPosition(this->positionOfTarget_ - diffUnit * 100.0f); |
|---|
| 137 | } |
|---|
| 138 | else |
|---|
| 139 | { |
|---|
| 140 | bool bTargetIsLookingAtThis = CommonController::isLooking (this->target_, this->getControllableEntity(), math::pi/20.0f) |
|---|
| 141 | || this->deltaHp < 0; |
|---|
| 142 | this->bKeepFormation_ = false; |
|---|
| 143 | |
|---|
| 144 | if (!this || !this->getControllableEntity()) |
|---|
| 145 | return; |
|---|
| 146 | if (!this->bDodge_) |
|---|
| 147 | { |
|---|
| 148 | this->bStartedDodging_ = false; |
|---|
| 149 | |
|---|
| 150 | this->setTargetPosition(this->positionOfTarget_ - diffUnit * 50.0f); |
|---|
| 151 | return; |
|---|
| 152 | } |
|---|
| 153 | else if (bTargetIsLookingAtThis || diffLength < 700.0f) |
|---|
| 154 | { |
|---|
| 155 | if (!this->bStartedDodging_) |
|---|
| 156 | { |
|---|
| 157 | this->bStartedDodging_ = true; |
|---|
| 158 | dodge(thisPosition, diffLength, diffUnit); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | else |
|---|
| 162 | { |
|---|
| 163 | if (diffLength < 1000) |
|---|
| 164 | { |
|---|
| 165 | this->stopMoving(); |
|---|
| 166 | this->startLookingAtTarget(); |
|---|
| 167 | |
|---|
| 168 | } |
|---|
| 169 | else |
|---|
| 170 | { |
|---|
| 171 | this->setTargetPosition(this->positionOfTarget_ - diffUnit * 300.0f); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | void FightingController::dodge(const Vector3& thisPosition, float diffLength, Vector3& diffUnit) |
|---|
| 178 | { |
|---|
| 179 | //d.x*x + d.y*y + d.z*z == 0 |
|---|
| 180 | //z = 1/d.z * (-d.y*y - d.x * x) |
|---|
| 181 | float x = CommonController::randomInRange (300, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1); |
|---|
| 182 | float y = CommonController::randomInRange (300, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1); |
|---|
| 183 | float z = diffUnit.z == 0 ? 0 : (1/diffUnit.z) * (-x * diffUnit.x - y * diffUnit.y); |
|---|
| 184 | if (diffLength < 150.0f) |
|---|
| 185 | { |
|---|
| 186 | this->setTargetPosition(this->positionOfTarget_ + Vector3(z,x,y)); |
|---|
| 187 | } |
|---|
| 188 | else |
|---|
| 189 | { |
|---|
| 190 | this->setTargetPosition(thisPosition + Vector3(x,y,z) + (this->deltaHp < 0 ? -diffUnit * 450.0f : |
|---|
| 191 | (diffLength < 700.0f ? -diffUnit*700.0f : diffUnit * 50.0f))); |
|---|
| 192 | |
|---|
| 193 | } |
|---|
| 194 | this->boostControl(); |
|---|
| 195 | |
|---|
| 196 | } |
|---|
| 197 | bool FightingController::canFire() |
|---|
| 198 | { |
|---|
| 199 | //no target? why fire? |
|---|
| 200 | if (!this->target_ || !this->getControllableEntity()) |
|---|
| 201 | return false; |
|---|
| 202 | Vector3 newPositionOfTarget = getPredictedPosition(this->getControllableEntity()->getWorldPosition(), |
|---|
| 203 | HARDCODED_PROJECTILE_SPEED, this->target_->getWorldPosition(), |
|---|
| 204 | this->target_->getVelocity()); |
|---|
| 205 | if (!this->target_ || !this->getControllableEntity()) |
|---|
| 206 | return false; |
|---|
| 207 | //Vector3.isNaN() is what I used on my machine and it worked... |
|---|
| 208 | if (!(std::isnan(newPositionOfTarget.x) || std::isnan(newPositionOfTarget.y) || std::isnan(newPositionOfTarget.z))) |
|---|
| 209 | { |
|---|
| 210 | this->setPositionOfTarget(newPositionOfTarget); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | return squaredDistanceToTarget() < this->attackRange_*this->attackRange_ && this->isLookingAtTarget(math::pi / 20.0f); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | float FightingController::squaredDistanceToTarget() const |
|---|
| 218 | { |
|---|
| 219 | if (!this || !this->getControllableEntity()) |
|---|
| 220 | return 0; |
|---|
| 221 | if (!this->target_ || !this->getControllableEntity()) |
|---|
| 222 | return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_)); |
|---|
| 223 | else |
|---|
| 224 | return (this->getControllableEntity()->getPosition().squaredDistance(this->positionOfTarget_)); |
|---|
| 225 | } |
|---|
| 226 | bool FightingController::isLookingAtTarget( float angle ) const |
|---|
| 227 | { |
|---|
| 228 | if ( !this->getControllableEntity() || !this->target_ ) |
|---|
| 229 | return false; |
|---|
| 230 | return CommonController::isLooking(this->getControllableEntity(), this->getTarget(), angle); |
|---|
| 231 | } |
|---|
| 232 | void FightingController::setClosestTarget() |
|---|
| 233 | { |
|---|
| 234 | this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) ); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | Pawn* FightingController::closestTarget() const |
|---|
| 238 | { |
|---|
| 239 | if (!this || !this->getControllableEntity()) |
|---|
| 240 | return nullptr; |
|---|
| 241 | |
|---|
| 242 | Pawn* closestTarget = nullptr; |
|---|
| 243 | float minDistance = std::numeric_limits<float>::infinity(); |
|---|
| 244 | Gametype* gt = this->getGametype(); |
|---|
| 245 | for (Pawn* pawn : ObjectList<Pawn>()) |
|---|
| 246 | { |
|---|
| 247 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(pawn), gt) ) |
|---|
| 248 | continue; |
|---|
| 249 | |
|---|
| 250 | float distance = CommonController::distance (pawn, this->getControllableEntity()); |
|---|
| 251 | if (distance < minDistance) |
|---|
| 252 | { |
|---|
| 253 | closestTarget = pawn; |
|---|
| 254 | minDistance = distance; |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | if (closestTarget) |
|---|
| 258 | { |
|---|
| 259 | return closestTarget; |
|---|
| 260 | } |
|---|
| 261 | return nullptr; |
|---|
| 262 | } |
|---|
| 263 | //I checked it out, rockets DO NOT cause any problems! this->getControllableEntity() is always a SpaceShip |
|---|
| 264 | void FightingController::doFire() |
|---|
| 265 | { |
|---|
| 266 | if (!this->bSetupWorked) |
|---|
| 267 | { |
|---|
| 268 | this->setupWeapons(); |
|---|
| 269 | } |
|---|
| 270 | if (!this->target_ || !this->getControllableEntity()) |
|---|
| 271 | { |
|---|
| 272 | return; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | Pawn* pawn = orxonox_cast<Pawn*> (this->getControllableEntity()); |
|---|
| 276 | if (pawn) |
|---|
| 277 | pawn->setAimPosition (this->positionOfTarget_); |
|---|
| 278 | |
|---|
| 279 | int firemode; |
|---|
| 280 | float distance = CommonController::distance (this->getControllableEntity(), this->target_); |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | if (distance < 1500) |
|---|
| 285 | { |
|---|
| 286 | if (this->rocketsLeft_ > 0 && !this->bFiredRocket_) |
|---|
| 287 | { |
|---|
| 288 | firemode = getFiremode ("RocketFire"); |
|---|
| 289 | } |
|---|
| 290 | else |
|---|
| 291 | { |
|---|
| 292 | if (distance > 800) |
|---|
| 293 | firemode = getFiremode ("HsW01"); |
|---|
| 294 | else |
|---|
| 295 | firemode = getFiremode ("LightningGun"); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | else if (distance < 2000) |
|---|
| 302 | { |
|---|
| 303 | firemode = getFiremode ("HsW01"); |
|---|
| 304 | } |
|---|
| 305 | else |
|---|
| 306 | { |
|---|
| 307 | firemode = getFiremode ("LightningGun"); |
|---|
| 308 | } |
|---|
| 309 | if (firemode < 0) |
|---|
| 310 | { |
|---|
| 311 | //assuming there is always some weapon with index 0 |
|---|
| 312 | firemode = 0; |
|---|
| 313 | } |
|---|
| 314 | if (firemode == getFiremode("RocketFire")) |
|---|
| 315 | { |
|---|
| 316 | this->timeout_ = 5; |
|---|
| 317 | this->rocketsLeft_--; |
|---|
| 318 | this->bFiredRocket_ = true; |
|---|
| 319 | } |
|---|
| 320 | if (firemode == getFiremode("SimpleRocketFire")) |
|---|
| 321 | { |
|---|
| 322 | this->rocketsLeft_--; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | this->getControllableEntity()->fire(firemode); |
|---|
| 326 | |
|---|
| 327 | } |
|---|
| 328 | void FightingController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions) |
|---|
| 329 | { |
|---|
| 330 | this->bSetupWorked = false; |
|---|
| 331 | if(this->getControllableEntity()) |
|---|
| 332 | { |
|---|
| 333 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
|---|
| 334 | if(pawn && pawn->isA(Class(SpaceShip))) //fix for First Person Mode: check for SpaceShip |
|---|
| 335 | { |
|---|
| 336 | this->weaponModes_.clear(); // reset previous weapon information |
|---|
| 337 | WeaponSlot* wSlot = nullptr; |
|---|
| 338 | for(int l=0; (wSlot = pawn->getWeaponSlot(l)) ; l++) |
|---|
| 339 | { |
|---|
| 340 | WeaponMode* wMode = nullptr; |
|---|
| 341 | for(int i=0; (wMode = wSlot->getWeapon()->getWeaponmode(i)) ; i++) |
|---|
| 342 | { |
|---|
| 343 | std::string wName = wMode->getIdentifier()->getName(); |
|---|
| 344 | // SubclassIdentifier<Munition> munition = ClassByString(wName); |
|---|
| 345 | if (wName == "RocketFire") |
|---|
| 346 | this->rocketsLeft_ = 10; |
|---|
| 347 | if(this->getFiremode(wName) == -1) //only add a weapon, if it is "new" |
|---|
| 348 | weaponModes_[wName] = wMode->getMode(); |
|---|
| 349 | } |
|---|
| 350 | } |
|---|
| 351 | if(weaponModes_.size())//at least one weapon detected |
|---|
| 352 | this->bSetupWorked = true; |
|---|
| 353 | } |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | int FightingController::getFiremode(const std::string& name) |
|---|
| 359 | { |
|---|
| 360 | for (const auto& mapEntry : this->weaponModes_) |
|---|
| 361 | { |
|---|
| 362 | if (mapEntry.first == name) |
|---|
| 363 | return mapEntry.second; |
|---|
| 364 | } |
|---|
| 365 | return -1; |
|---|
| 366 | } |
|---|
| 367 | } |
|---|