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