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