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