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