[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 | { |
---|
| 153 | // orxout(internal_error) << "attacking" << endl; |
---|
[10927] | 154 | this->bStartedDodging_ = false; |
---|
| 155 | |
---|
[10906] | 156 | this->setTargetPosition(this->positionOfTarget_ - diffUnit * 50.0f); |
---|
| 157 | return; |
---|
| 158 | } |
---|
| 159 | else if (bTargetIsLookingAtThis || diffLength < 700.0f) |
---|
| 160 | { |
---|
| 161 | // orxout(internal_error) << "dodging" << endl; |
---|
| 162 | if (!this->bStartedDodging_) |
---|
| 163 | { |
---|
| 164 | this->bStartedDodging_ = true; |
---|
| 165 | dodge(thisPosition, diffLength, diffUnit); |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | else |
---|
| 169 | { |
---|
| 170 | if (diffLength < 1000) |
---|
| 171 | { |
---|
| 172 | // orxout(internal_error) << "looking" << endl; |
---|
| 173 | this->stopMoving(); |
---|
| 174 | this->startLookingAtTarget(); |
---|
| 175 | |
---|
| 176 | } |
---|
| 177 | else |
---|
| 178 | { |
---|
| 179 | // orxout(internal_error) << "closing up" << endl; |
---|
| 180 | this->setTargetPosition(this->positionOfTarget_ - diffUnit * 300.0f); |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | } |
---|
[10871] | 184 | } |
---|
[10923] | 185 | |
---|
[10906] | 186 | void FightingController::dodge(const Vector3& thisPosition, float diffLength, Vector3& diffUnit) |
---|
[10871] | 187 | { |
---|
[10885] | 188 | //d.x*x + d.y*y + d.z*z == 0 |
---|
| 189 | //z = 1/d.z * (-d.y*y - d.x * x) |
---|
[10906] | 190 | float x = CommonController::randomInRange (300, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1); |
---|
| 191 | float y = CommonController::randomInRange (300, 800) * (CommonController::randomInRange(0,1) <= 0.5 ? 1 : -1); |
---|
[10888] | 192 | float z = diffUnit.z == 0 ? 0 : (1/diffUnit.z) * (-x * diffUnit.x - y * diffUnit.y); |
---|
[10906] | 193 | if (diffLength < 150.0f) |
---|
| 194 | { |
---|
| 195 | this->setTargetPosition(this->positionOfTarget_ + Vector3(z,x,y)); |
---|
| 196 | } |
---|
| 197 | else |
---|
| 198 | { |
---|
| 199 | this->setTargetPosition(thisPosition + Vector3(x,y,z) + (this->deltaHp < 0 ? -diffUnit * 450.0f : |
---|
| 200 | (diffLength < 700.0f ? -diffUnit*700.0f : diffUnit * 50.0f))); |
---|
| 201 | |
---|
| 202 | } |
---|
[10885] | 203 | this->boostControl(); |
---|
[10871] | 204 | |
---|
| 205 | } |
---|
| 206 | bool FightingController::canFire() |
---|
| 207 | { |
---|
| 208 | //no target? why fire? |
---|
[10923] | 209 | if (!this->target_ || !this->getControllableEntity()) |
---|
[10871] | 210 | return false; |
---|
[10885] | 211 | Vector3 newPositionOfTarget = getPredictedPosition(this->getControllableEntity()->getWorldPosition(), |
---|
| 212 | hardcoded_projectile_speed, this->target_->getWorldPosition(), |
---|
| 213 | this->target_->getVelocity()); |
---|
[10925] | 214 | if (!this->target_ || !this->getControllableEntity()) |
---|
| 215 | return false; |
---|
[10891] | 216 | //Vector3.isNaN() is what I used on my machine and it worked... |
---|
| 217 | if (!(std::isnan(newPositionOfTarget.x) || std::isnan(newPositionOfTarget.y) || std::isnan(newPositionOfTarget.z))) |
---|
[10871] | 218 | { |
---|
[10885] | 219 | this->setPositionOfTarget(newPositionOfTarget); |
---|
[10871] | 220 | } |
---|
[10886] | 221 | |
---|
[10953] | 222 | return squaredDistanceToTarget() < this->attackRange_*this->attackRange_ && this->isLookingAtTarget(math::pi / 20.0f); |
---|
[10871] | 223 | } |
---|
| 224 | |
---|
[10885] | 225 | |
---|
[10871] | 226 | float FightingController::squaredDistanceToTarget() const |
---|
| 227 | { |
---|
[10923] | 228 | if (!this || !this->getControllableEntity()) |
---|
[10871] | 229 | return 0; |
---|
[10885] | 230 | if (!this->target_ || !this->getControllableEntity()) |
---|
| 231 | return (this->getControllableEntity()->getPosition().squaredDistance(this->targetPosition_)); |
---|
[10871] | 232 | else |
---|
[10885] | 233 | return (this->getControllableEntity()->getPosition().squaredDistance(this->positionOfTarget_)); |
---|
[10871] | 234 | } |
---|
[10885] | 235 | bool FightingController::isLookingAtTarget( float angle ) const |
---|
[10871] | 236 | { |
---|
| 237 | if ( !this->getControllableEntity() || !this->target_ ) |
---|
| 238 | return false; |
---|
[10877] | 239 | return CommonController::isLooking(this->getControllableEntity(), this->getTarget(), angle); |
---|
[10871] | 240 | } |
---|
[10885] | 241 | void FightingController::setClosestTarget() |
---|
[10871] | 242 | { |
---|
| 243 | this->setTarget (static_cast<ControllableEntity*>( closestTarget() ) ); |
---|
| 244 | } |
---|
| 245 | |
---|
[10885] | 246 | Pawn* FightingController::closestTarget() const |
---|
[10871] | 247 | { |
---|
[10923] | 248 | if (!this || !this->getControllableEntity()) |
---|
[10871] | 249 | return 0; |
---|
| 250 | |
---|
| 251 | Pawn* closestTarget = 0; |
---|
| 252 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
| 253 | Gametype* gt = this->getGametype(); |
---|
| 254 | for (ObjectList<Pawn>::iterator itP = ObjectList<Pawn>::begin(); itP; ++itP) |
---|
| 255 | { |
---|
| 256 | if ( CommonController::sameTeam (this->getControllableEntity(), static_cast<ControllableEntity*>(*itP), gt) ) |
---|
| 257 | continue; |
---|
| 258 | |
---|
| 259 | float distance = CommonController::distance (*itP, this->getControllableEntity()); |
---|
| 260 | if (distance < minDistance) |
---|
| 261 | { |
---|
| 262 | closestTarget = *itP; |
---|
| 263 | minDistance = distance; |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | if (closestTarget) |
---|
| 267 | { |
---|
| 268 | return closestTarget; |
---|
| 269 | } |
---|
| 270 | return 0; |
---|
| 271 | } |
---|
[10885] | 272 | //I checked it out, rockets DO NOT cause any problems! this->getControllableEntity() is always a SpaceShip |
---|
| 273 | void FightingController::doFire() |
---|
| 274 | { |
---|
| 275 | if (!this->bSetupWorked) |
---|
| 276 | { |
---|
| 277 | this->setupWeapons(); |
---|
| 278 | } |
---|
| 279 | if (!this->target_ || !this->getControllableEntity()) |
---|
| 280 | { |
---|
| 281 | return; |
---|
| 282 | } |
---|
[10886] | 283 | |
---|
[10885] | 284 | Pawn* pawn = orxonox_cast<Pawn*> (this->getControllableEntity()); |
---|
| 285 | if (pawn) |
---|
| 286 | pawn->setAimPosition (this->positionOfTarget_); |
---|
[10935] | 287 | |
---|
[10885] | 288 | int firemode; |
---|
| 289 | float distance = CommonController::distance (this->getControllableEntity(), this->target_); |
---|
| 290 | |
---|
[10935] | 291 | |
---|
| 292 | |
---|
[10886] | 293 | if (distance < 1500) |
---|
[10885] | 294 | { |
---|
[10886] | 295 | if (this->rocketsLeft_ > 0 && !this->bFiredRocket_) |
---|
[10885] | 296 | { |
---|
[10886] | 297 | firemode = getFiremode ("RocketFire"); |
---|
[10885] | 298 | } |
---|
| 299 | else |
---|
| 300 | { |
---|
[10886] | 301 | if (distance > 800) |
---|
| 302 | firemode = getFiremode ("HsW01"); |
---|
| 303 | else |
---|
[10885] | 304 | firemode = getFiremode ("LightningGun"); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | } |
---|
[10935] | 308 | |
---|
| 309 | |
---|
[10886] | 310 | else if (distance < 2000) |
---|
[10885] | 311 | { |
---|
[10886] | 312 | firemode = getFiremode ("HsW01"); |
---|
[10885] | 313 | } |
---|
[10886] | 314 | else |
---|
[10885] | 315 | { |
---|
| 316 | firemode = getFiremode ("LightningGun"); |
---|
| 317 | } |
---|
| 318 | if (firemode < 0) |
---|
| 319 | { |
---|
[10886] | 320 | //assuming there is always some weapon with index 0 |
---|
| 321 | firemode = 0; |
---|
[10885] | 322 | } |
---|
| 323 | if (firemode == getFiremode("RocketFire")) |
---|
| 324 | { |
---|
[10934] | 325 | this->timeout_ = 5; |
---|
[10885] | 326 | this->rocketsLeft_--; |
---|
| 327 | this->bFiredRocket_ = true; |
---|
| 328 | } |
---|
| 329 | if (firemode == getFiremode("SimpleRocketFire")) |
---|
| 330 | { |
---|
| 331 | this->rocketsLeft_--; |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | this->getControllableEntity()->fire(firemode); |
---|
| 335 | |
---|
| 336 | } |
---|
| 337 | void FightingController::setupWeapons() //TODO: Make this function generic!! (at the moment is is based on conventions) |
---|
| 338 | { |
---|
| 339 | this->bSetupWorked = false; |
---|
| 340 | if(this->getControllableEntity()) |
---|
| 341 | { |
---|
| 342 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
---|
| 343 | if(pawn && pawn->isA(Class(SpaceShip))) //fix for First Person Mode: check for SpaceShip |
---|
| 344 | { |
---|
| 345 | this->weaponModes_.clear(); // reset previous weapon information |
---|
| 346 | WeaponSlot* wSlot = 0; |
---|
| 347 | for(int l=0; (wSlot = pawn->getWeaponSlot(l)) ; l++) |
---|
| 348 | { |
---|
| 349 | WeaponMode* wMode = 0; |
---|
| 350 | for(int i=0; (wMode = wSlot->getWeapon()->getWeaponmode(i)) ; i++) |
---|
| 351 | { |
---|
| 352 | std::string wName = wMode->getIdentifier()->getName(); |
---|
[10903] | 353 | // SubclassIdentifier<Munition> munition = ClassByString(wName); |
---|
[10885] | 354 | if (wName == "RocketFire") |
---|
| 355 | this->rocketsLeft_ = 10; |
---|
[10903] | 356 | // this->rocketsLeft_ = orxonox_cast<Pawn*>(this->getControllableEntity())->getWeaponSystem()->getMunition(&munition)->getNumMunitionInCurrentMagazine(wMode); |
---|
[10885] | 357 | if(this->getFiremode(wName) == -1) //only add a weapon, if it is "new" |
---|
| 358 | weaponModes_[wName] = wMode->getMode(); |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | if(weaponModes_.size())//at least one weapon detected |
---|
| 362 | this->bSetupWorked = true; |
---|
| 363 | }//pawn->weaponSystem_->getMunition(SubclassIdentifier< Munition > *identifier)->getNumMunition (WeaponMode *user); |
---|
| 364 | } |
---|
[10903] | 365 | |
---|
[10885] | 366 | } |
---|
| 367 | |
---|
| 368 | int FightingController::getFiremode(std::string name) |
---|
| 369 | { |
---|
| 370 | for (std::map< std::string, int >::iterator it = this->weaponModes_.begin(); it != this->weaponModes_.end(); ++it) |
---|
| 371 | { |
---|
| 372 | if (it->first == name) |
---|
| 373 | return it->second; |
---|
| 374 | } |
---|
| 375 | return -1; |
---|
| 376 | } |
---|
[10871] | 377 | } |
---|