[2918] | 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 | * Martin Polak |
---|
| 24 | * Fabian 'x3n' Landau |
---|
| 25 | * Co-authors: |
---|
[11189] | 26 | * Johannes Sager |
---|
[2918] | 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include "WeaponMode.h" |
---|
| 31 | |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "core/XMLPort.h" |
---|
[6417] | 34 | #include "controllers/Controller.h" |
---|
| 35 | #include "worldentities/pawns/Pawn.h" |
---|
[2918] | 36 | |
---|
| 37 | #include "Munition.h" |
---|
| 38 | #include "Weapon.h" |
---|
| 39 | #include "WeaponPack.h" |
---|
| 40 | #include "WeaponSystem.h" |
---|
[6417] | 41 | #include "WeaponSlot.h" |
---|
[2918] | 42 | |
---|
[6417] | 43 | #include "sound/WorldSound.h" |
---|
| 44 | |
---|
[2918] | 45 | namespace orxonox |
---|
| 46 | { |
---|
[10624] | 47 | RegisterAbstractClass(WeaponMode).inheritsFrom<BaseObject>(); |
---|
[9667] | 48 | |
---|
| 49 | WeaponMode::WeaponMode(Context* context) : BaseObject(context) |
---|
[2918] | 50 | { |
---|
| 51 | RegisterObject(WeaponMode); |
---|
| 52 | |
---|
[11071] | 53 | this->weapon_ = nullptr; |
---|
[2918] | 54 | this->mode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; |
---|
| 55 | |
---|
[11071] | 56 | this->munition_ = nullptr; |
---|
[2918] | 57 | this->initialMunition_ = 0; |
---|
| 58 | this->initialMagazines_ = 0; |
---|
| 59 | this->munitionPerShot_ = 1; |
---|
| 60 | |
---|
| 61 | this->reloadTime_ = 0.25; |
---|
| 62 | this->bReloading_ = false; |
---|
| 63 | this->bAutoReload_ = true; |
---|
| 64 | this->bParallelReload_ = true; |
---|
[11189] | 65 | this->chargeable_ = false; // most weapons are not chargeable |
---|
| 66 | this->charges_ = 0; // always start with no charges |
---|
| 67 | this->maxCharges_ = 100; // default maximum charges one can have are 100 |
---|
[2918] | 68 | |
---|
[5929] | 69 | this->reloadTimer_.setTimer(0.0f, false, createExecutor(createFunctor(&WeaponMode::reloaded, this))); |
---|
[2918] | 70 | this->reloadTimer_.stopTimer(); |
---|
| 71 | |
---|
| 72 | this->damage_ = 0; |
---|
[8706] | 73 | this->healthdamage_ = 0; |
---|
| 74 | this->shielddamage_ = 0; |
---|
[6417] | 75 | |
---|
[2918] | 76 | this->muzzleOffset_ = Vector3::ZERO; |
---|
[6417] | 77 | this->muzzlePosition_ = Vector3::ZERO; |
---|
| 78 | this->muzzleOrientation_ = Quaternion::IDENTITY; |
---|
| 79 | |
---|
[11052] | 80 | hudImageString_ = "Orxonox/WSHUD_WM_Unknown"; |
---|
| 81 | |
---|
[11108] | 82 | this->fireSoundPath_ = BLANKSTRING; |
---|
| 83 | this->fireSoundVolume_ = 1.0; |
---|
| 84 | this->fireSounds_.clear(); |
---|
| 85 | |
---|
| 86 | this->reloadSoundPath_ = BLANKSTRING; |
---|
| 87 | this->reloadSoundVolume_ = 1.0; |
---|
| 88 | this->reloadSound_ = nullptr; |
---|
[2918] | 89 | } |
---|
| 90 | |
---|
| 91 | WeaponMode::~WeaponMode() |
---|
| 92 | { |
---|
[6417] | 93 | if (this->isInitialized()) |
---|
| 94 | { |
---|
[11108] | 95 | for (auto sound : fireSounds_) |
---|
| 96 | { |
---|
| 97 | sound->destroy(); |
---|
| 98 | } |
---|
[6417] | 99 | } |
---|
[2918] | 100 | } |
---|
| 101 | |
---|
| 102 | void WeaponMode::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 103 | { |
---|
| 104 | SUPER(WeaponMode, XMLPort, xmlelement, mode); |
---|
| 105 | |
---|
| 106 | XMLPortParam(WeaponMode, "mode", setMode, getMode, xmlelement, mode); |
---|
| 107 | |
---|
| 108 | XMLPortParam(WeaponMode, "munitiontype", setMunitionName, getMunitionName, xmlelement, mode); |
---|
| 109 | XMLPortParam(WeaponMode, "initialmunition", setInitialMunition, getInitialMunition, xmlelement, mode); |
---|
| 110 | XMLPortParam(WeaponMode, "initialmagazines", setInitialMagazines, getInitialMagazines, xmlelement, mode); |
---|
| 111 | XMLPortParam(WeaponMode, "munitionpershot", setMunitionPerShot, getMunitionPerShot, xmlelement, mode); |
---|
| 112 | |
---|
| 113 | XMLPortParam(WeaponMode, "reloadtime", setReloadTime, getReloadTime, xmlelement, mode); |
---|
| 114 | XMLPortParam(WeaponMode, "autoreload", setAutoReload, getAutoReload, xmlelement, mode).description("If true, the weapon reloads the magazine automatically"); |
---|
| 115 | XMLPortParam(WeaponMode, "parallelreload", setParallelReload, getParallelReload, xmlelement, mode).description("If true, the weapon reloads in parallel to the magazine reloading"); |
---|
| 116 | |
---|
| 117 | XMLPortParam(WeaponMode, "damage", setDamage, getDamage, xmlelement, mode); |
---|
[8706] | 118 | XMLPortParam(WeaponMode, "healthdamage", setHealthDamage, getHealthDamage, xmlelement, mode); |
---|
| 119 | XMLPortParam(WeaponMode, "shielddamage", setShieldDamage, getShieldDamage, xmlelement, mode); |
---|
[2918] | 120 | XMLPortParam(WeaponMode, "muzzleoffset", setMuzzleOffset, getMuzzleOffset, xmlelement, mode); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | bool WeaponMode::fire(float* reloadTime) |
---|
| 124 | { |
---|
| 125 | (*reloadTime) = this->reloadTime_; |
---|
[11052] | 126 | // Fireing is only possible if this weapon mode is not reloading and there is enough munition |
---|
[2918] | 127 | if (!this->bReloading_ && this->munition_ && this->munition_->takeMunition(this->munitionPerShot_, this)) |
---|
| 128 | { |
---|
[10650] | 129 | float tempReloadtime = this->reloadTime_; |
---|
[2918] | 130 | |
---|
| 131 | if (this->bAutoReload_ && this->munition_->needReload(this)) |
---|
| 132 | { |
---|
| 133 | if (this->munition_->reload(this)) |
---|
| 134 | { |
---|
[11052] | 135 | // If true, the weapon reloads in parallel to the magazine reloading |
---|
[7847] | 136 | if (this->bParallelReload_) |
---|
[11052] | 137 | { |
---|
| 138 | // The time needed to reload is the maximum of the reload time of the weapon mode and the magazine. |
---|
[10650] | 139 | tempReloadtime = std::max(this->reloadTime_, this->munition_->getReloadTime()); |
---|
[11052] | 140 | } |
---|
[7847] | 141 | else |
---|
[11052] | 142 | { |
---|
| 143 | // The time needed to reload is the sum of the reload time of the weapon mode and the magazine. |
---|
[10650] | 144 | tempReloadtime = this->reloadTime_ + this->munition_->getReloadTime(); |
---|
[11108] | 145 | } |
---|
| 146 | playReloadSound(); |
---|
[2918] | 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
[11108] | 150 | // For stacked munition, a reload sound is played after every fired projectile |
---|
| 151 | if (this->munition_->getMunitionDeployment() == MunitionDeployment::Stack) |
---|
| 152 | { |
---|
| 153 | playReloadSound(); |
---|
| 154 | } |
---|
| 155 | |
---|
[11052] | 156 | // Mark this weapon mode as reloading and start the reload timer |
---|
[2918] | 157 | this->bReloading_ = true; |
---|
[10650] | 158 | this->reloadTimer_.setInterval(tempReloadtime); |
---|
[2918] | 159 | this->reloadTimer_.startTimer(); |
---|
| 160 | |
---|
[11108] | 161 | // Play the fire sound and fire the weapon mode |
---|
| 162 | this->playFireSound(); |
---|
[2918] | 163 | this->fire(); |
---|
| 164 | |
---|
| 165 | return true; |
---|
| 166 | } |
---|
| 167 | else |
---|
| 168 | { |
---|
| 169 | return false; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
[11174] | 173 | bool WeaponMode::push(float* reloadTime) |
---|
| 174 | { |
---|
[11185] | 175 | |
---|
[11189] | 176 | if( this->chargeable_) // chargeable weapons are supposed to charge on push |
---|
[11174] | 177 | { |
---|
[11189] | 178 | this->munition_ = this->weapon_->getWeaponPack()->getWeaponSystem()->getMunition(&this->munitiontype_); // updates the pointer to the munition(which we use in the next step) |
---|
| 179 | if(this->charges_ < this->maxCharges_ && this->bReloading_ == false && this->munition_->canTakeMunition(1, this)) // charges up unless: |
---|
| 180 | { // - we are fully charged |
---|
| 181 | this->charges_ += 1; // - we are reloading |
---|
| 182 | } // - we have no munition |
---|
[11175] | 183 | return false; |
---|
[11189] | 184 | } |
---|
| 185 | else // normal (not chargeable) weapons are supposed to fire on push |
---|
| 186 | { |
---|
[11174] | 187 | return fire(reloadTime); |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
[11189] | 191 | bool WeaponMode::release(float* reloadTime) |
---|
[11166] | 192 | { |
---|
[11189] | 193 | if( this->chargeable_) // chargeable weapons are supposed to fire on release |
---|
[11175] | 194 | { |
---|
[11174] | 195 | return fire(reloadTime); |
---|
[11189] | 196 | } |
---|
| 197 | else // normal (not chargeable) weapons should do nothing on release |
---|
| 198 | { |
---|
[11175] | 199 | return false; |
---|
[11174] | 200 | } |
---|
[11166] | 201 | } |
---|
[11153] | 202 | |
---|
[2918] | 203 | bool WeaponMode::reload() |
---|
| 204 | { |
---|
| 205 | if (this->munition_ && this->munition_->reload(this)) |
---|
| 206 | { |
---|
| 207 | if (!this->bParallelReload_) |
---|
| 208 | { |
---|
| 209 | this->bReloading_ = true; |
---|
| 210 | this->reloadTimer_.setInterval(this->reloadTime_ + this->munition_->getReloadTime()); |
---|
| 211 | this->reloadTimer_.startTimer(); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | return true; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | return false; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | void WeaponMode::setMunitionType(Identifier* identifier) |
---|
| 221 | { |
---|
| 222 | this->munitionname_ = identifier->getName(); |
---|
| 223 | this->munitiontype_ = identifier; |
---|
| 224 | this->updateMunition(); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | void WeaponMode::setMunitionName(const std::string& munitionname) |
---|
| 228 | { |
---|
| 229 | this->munitionname_ = munitionname; |
---|
[6417] | 230 | Identifier* identifier = ClassByString(this->munitionname_); |
---|
| 231 | if (identifier) |
---|
| 232 | this->munitiontype_ = identifier; |
---|
| 233 | else |
---|
[8858] | 234 | orxout(internal_warning) << "No munition class defined in WeaponMode " << this->getName() << endl; |
---|
[2918] | 235 | this->updateMunition(); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | void WeaponMode::updateMunition() |
---|
| 239 | { |
---|
| 240 | if (this->munitiontype_ && this->weapon_ && this->weapon_->getWeaponPack() && this->weapon_->getWeaponPack()->getWeaponSystem()) |
---|
| 241 | { |
---|
| 242 | this->munition_ = this->weapon_->getWeaponPack()->getWeaponSystem()->getMunition(&this->munitiontype_); |
---|
| 243 | |
---|
| 244 | if (this->munition_) |
---|
| 245 | { |
---|
| 246 | // Add the initial magazines |
---|
| 247 | this->munition_->addMagazines(this->initialMagazines_); |
---|
| 248 | |
---|
| 249 | // Maybe we have to reload (if this munition is used the first time or if there weren't any magazines available before) |
---|
| 250 | if (this->munition_->needReload(this)) |
---|
| 251 | this->munition_->reload(this, false); |
---|
| 252 | |
---|
| 253 | // Add the initial munition |
---|
| 254 | if (this->initialMunition_ > 0 && this->munition_->getNumMunitionInCurrentMagazine(this) == this->munition_->getMaxMunitionPerMagazine()) |
---|
| 255 | { |
---|
| 256 | // The current magazine is still full, so let's just add another magazine to |
---|
| 257 | // the stack and reduce the current magazine to the given amount of munition |
---|
| 258 | |
---|
| 259 | unsigned int initialmunition = this->initialMunition_; |
---|
| 260 | if (initialmunition > this->munition_->getMaxMunitionPerMagazine()) |
---|
| 261 | initialmunition = this->munition_->getMaxMunitionPerMagazine(); |
---|
| 262 | |
---|
| 263 | this->munition_->takeMunition(this->munition_->getMaxMunitionPerMagazine() - initialmunition, this); |
---|
| 264 | this->munition_->addMagazines(1); |
---|
| 265 | } |
---|
| 266 | else |
---|
| 267 | { |
---|
| 268 | // The current magazine isn't full, add the munition directly |
---|
| 269 | |
---|
| 270 | this->munition_->addMunition(this->initialMunition_); |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | else |
---|
[11052] | 275 | { |
---|
[11071] | 276 | this->munition_ = nullptr; |
---|
[11052] | 277 | } |
---|
[2918] | 278 | } |
---|
| 279 | |
---|
| 280 | void WeaponMode::reloaded() |
---|
| 281 | { |
---|
| 282 | this->bReloading_ = false; |
---|
| 283 | } |
---|
| 284 | |
---|
[6417] | 285 | void WeaponMode::computeMuzzleParameters(const Vector3& target) |
---|
[2918] | 286 | { |
---|
| 287 | if (this->weapon_) |
---|
[6417] | 288 | { |
---|
| 289 | this->muzzlePosition_ = this->weapon_->getWorldPosition() + this->weapon_->getWorldOrientation() * this->muzzleOffset_; |
---|
| 290 | |
---|
| 291 | Vector3 muzzleDirection; |
---|
| 292 | muzzleDirection = target - this->muzzlePosition_; |
---|
| 293 | this->muzzleOrientation_ = (this->weapon_->getWorldOrientation() * WorldEntity::FRONT).getRotationTo(muzzleDirection) * this->weapon_->getWorldOrientation(); |
---|
| 294 | } |
---|
[2918] | 295 | else |
---|
[6417] | 296 | { |
---|
| 297 | this->muzzlePosition_ = this->muzzleOffset_; |
---|
| 298 | this->muzzleOrientation_ = Quaternion::IDENTITY; |
---|
| 299 | } |
---|
[2918] | 300 | } |
---|
| 301 | |
---|
[6417] | 302 | Vector3 WeaponMode::getMuzzleDirection() const |
---|
[2918] | 303 | { |
---|
| 304 | if (this->weapon_) |
---|
[6417] | 305 | return (this->getMuzzleOrientation() * WorldEntity::FRONT); |
---|
[2918] | 306 | else |
---|
[6417] | 307 | return WorldEntity::FRONT; |
---|
[2918] | 308 | } |
---|
| 309 | |
---|
[11108] | 310 | void WeaponMode::setFireSound(const std::string& soundPath, const float soundVolume) |
---|
[2918] | 311 | { |
---|
[11108] | 312 | fireSoundPath_ = soundPath; |
---|
| 313 | fireSoundVolume_ = soundVolume; |
---|
[6417] | 314 | } |
---|
| 315 | |
---|
[11108] | 316 | const std::string& WeaponMode::getFireSound() |
---|
[6417] | 317 | { |
---|
[11108] | 318 | return fireSoundPath_; |
---|
[2918] | 319 | } |
---|
[7163] | 320 | |
---|
[11108] | 321 | void WeaponMode::setReloadSound(const std::string& soundPath, const float soundVolume) |
---|
[11052] | 322 | { |
---|
[11108] | 323 | reloadSoundPath_ = soundPath; |
---|
| 324 | reloadSoundVolume_ = soundVolume; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | const std::string& WeaponMode::getReloadSound() |
---|
| 328 | { |
---|
| 329 | return reloadSoundPath_; |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | void WeaponMode::playFireSound() |
---|
| 333 | { |
---|
| 334 | WorldSound* unusedSound = nullptr; |
---|
| 335 | |
---|
| 336 | if (!GameMode::isMaster()) |
---|
[11052] | 337 | { |
---|
[11108] | 338 | return; |
---|
[7163] | 339 | } |
---|
[11108] | 340 | |
---|
| 341 | // If no sound path or no weapon was specified, then no sound is played. |
---|
| 342 | if (fireSoundPath_ == BLANKSTRING || !this->getWeapon()) |
---|
| 343 | { |
---|
| 344 | return; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | // Search in the sound list for a WorldSound that may be used. It must be an idle WorldSound instance (i.e. it is not playing a sound now) |
---|
| 348 | for (auto sound : fireSounds_) |
---|
| 349 | { |
---|
| 350 | if( sound && !(sound->isPlaying())) |
---|
| 351 | { |
---|
| 352 | // Unused sound found |
---|
| 353 | unusedSound = sound; |
---|
| 354 | break; |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | // If no unused sound was found, create a new one and add it to the list |
---|
| 359 | if (!unusedSound) |
---|
| 360 | { |
---|
| 361 | unusedSound = new WorldSound(this->getContext()); |
---|
| 362 | fireSounds_.push_back(unusedSound); |
---|
| 363 | unusedSound->setLooping(false); |
---|
| 364 | unusedSound->setSource(fireSoundPath_); |
---|
| 365 | unusedSound->setVolume(fireSoundVolume_); |
---|
| 366 | this->getWeapon()->attach(unusedSound); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | // Play the fire sound |
---|
| 370 | unusedSound->play(); |
---|
[7163] | 371 | } |
---|
| 372 | |
---|
[11108] | 373 | void WeaponMode::playReloadSound() |
---|
| 374 | { |
---|
| 375 | if (!GameMode::isMaster()) |
---|
| 376 | { |
---|
| 377 | return; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | // If no sound path or no weapon was specified, then no sound is played. |
---|
| 381 | if (reloadSoundPath_ == BLANKSTRING || !this->getWeapon()) |
---|
| 382 | { |
---|
| 383 | return; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | // Create a reload WorldSound if not done yet |
---|
| 387 | if (!reloadSound_) |
---|
| 388 | { |
---|
| 389 | reloadSound_ = new WorldSound(this->getContext()); |
---|
| 390 | reloadSound_->setSource(reloadSoundPath_); |
---|
| 391 | reloadSound_->setVolume(reloadSoundVolume_); |
---|
| 392 | this->getWeapon()->attach(reloadSound_); |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | // Play the reload sound |
---|
| 396 | reloadSound_->play(); |
---|
| 397 | } |
---|
[2918] | 398 | } |
---|