[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: |
---|
| 26 | * ... |
---|
| 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; |
---|
[11175] | 65 | this->chargeable_ = false; |
---|
[11185] | 66 | this->charges_ = 0; |
---|
| 67 | this->maxCharges_ = 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 | { |
---|
[11175] | 125 | orxout() << "--- " << endl; |
---|
[2918] | 126 | (*reloadTime) = this->reloadTime_; |
---|
[11052] | 127 | // Fireing is only possible if this weapon mode is not reloading and there is enough munition |
---|
[2918] | 128 | if (!this->bReloading_ && this->munition_ && this->munition_->takeMunition(this->munitionPerShot_, this)) |
---|
| 129 | { |
---|
[10650] | 130 | float tempReloadtime = this->reloadTime_; |
---|
[2918] | 131 | |
---|
| 132 | if (this->bAutoReload_ && this->munition_->needReload(this)) |
---|
| 133 | { |
---|
| 134 | if (this->munition_->reload(this)) |
---|
| 135 | { |
---|
[11052] | 136 | // If true, the weapon reloads in parallel to the magazine reloading |
---|
[7847] | 137 | if (this->bParallelReload_) |
---|
[11052] | 138 | { |
---|
| 139 | // The time needed to reload is the maximum of the reload time of the weapon mode and the magazine. |
---|
[10650] | 140 | tempReloadtime = std::max(this->reloadTime_, this->munition_->getReloadTime()); |
---|
[11052] | 141 | } |
---|
[7847] | 142 | else |
---|
[11052] | 143 | { |
---|
| 144 | // The time needed to reload is the sum of the reload time of the weapon mode and the magazine. |
---|
[10650] | 145 | tempReloadtime = this->reloadTime_ + this->munition_->getReloadTime(); |
---|
[11108] | 146 | } |
---|
| 147 | playReloadSound(); |
---|
[2918] | 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
[11108] | 151 | // For stacked munition, a reload sound is played after every fired projectile |
---|
| 152 | if (this->munition_->getMunitionDeployment() == MunitionDeployment::Stack) |
---|
| 153 | { |
---|
| 154 | playReloadSound(); |
---|
| 155 | } |
---|
| 156 | |
---|
[11052] | 157 | // Mark this weapon mode as reloading and start the reload timer |
---|
[2918] | 158 | this->bReloading_ = true; |
---|
[10650] | 159 | this->reloadTimer_.setInterval(tempReloadtime); |
---|
[2918] | 160 | this->reloadTimer_.startTimer(); |
---|
| 161 | |
---|
[11108] | 162 | // Play the fire sound and fire the weapon mode |
---|
| 163 | this->playFireSound(); |
---|
[2918] | 164 | this->fire(); |
---|
| 165 | |
---|
| 166 | return true; |
---|
| 167 | } |
---|
| 168 | else |
---|
| 169 | { |
---|
| 170 | return false; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
[11174] | 174 | bool WeaponMode::push(float* reloadTime) |
---|
| 175 | { |
---|
[11185] | 176 | |
---|
[11174] | 177 | if( this->chargeable_) |
---|
| 178 | { |
---|
[11185] | 179 | if(this->charges_ < this->maxCharges_){ |
---|
| 180 | this->charges_ += 1; |
---|
| 181 | } |
---|
[11175] | 182 | return false; |
---|
[11174] | 183 | } else { |
---|
| 184 | return fire(reloadTime); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
[11166] | 188 | bool WeaponMode::release(float* reloadTime) |
---|
| 189 | { |
---|
[11174] | 190 | if( this->chargeable_) |
---|
[11175] | 191 | { |
---|
[11174] | 192 | return fire(reloadTime); |
---|
[11175] | 193 | }else{ |
---|
| 194 | return false; |
---|
[11174] | 195 | } |
---|
[11166] | 196 | } |
---|
[11153] | 197 | |
---|
[2918] | 198 | bool WeaponMode::reload() |
---|
| 199 | { |
---|
| 200 | if (this->munition_ && this->munition_->reload(this)) |
---|
| 201 | { |
---|
| 202 | if (!this->bParallelReload_) |
---|
| 203 | { |
---|
| 204 | this->bReloading_ = true; |
---|
| 205 | this->reloadTimer_.setInterval(this->reloadTime_ + this->munition_->getReloadTime()); |
---|
| 206 | this->reloadTimer_.startTimer(); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | return true; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | return false; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | void WeaponMode::setMunitionType(Identifier* identifier) |
---|
| 216 | { |
---|
| 217 | this->munitionname_ = identifier->getName(); |
---|
| 218 | this->munitiontype_ = identifier; |
---|
| 219 | this->updateMunition(); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | void WeaponMode::setMunitionName(const std::string& munitionname) |
---|
| 223 | { |
---|
| 224 | this->munitionname_ = munitionname; |
---|
[6417] | 225 | Identifier* identifier = ClassByString(this->munitionname_); |
---|
| 226 | if (identifier) |
---|
| 227 | this->munitiontype_ = identifier; |
---|
| 228 | else |
---|
[8858] | 229 | orxout(internal_warning) << "No munition class defined in WeaponMode " << this->getName() << endl; |
---|
[2918] | 230 | this->updateMunition(); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | void WeaponMode::updateMunition() |
---|
| 234 | { |
---|
| 235 | if (this->munitiontype_ && this->weapon_ && this->weapon_->getWeaponPack() && this->weapon_->getWeaponPack()->getWeaponSystem()) |
---|
| 236 | { |
---|
| 237 | this->munition_ = this->weapon_->getWeaponPack()->getWeaponSystem()->getMunition(&this->munitiontype_); |
---|
| 238 | |
---|
| 239 | if (this->munition_) |
---|
| 240 | { |
---|
| 241 | // Add the initial magazines |
---|
| 242 | this->munition_->addMagazines(this->initialMagazines_); |
---|
| 243 | |
---|
| 244 | // Maybe we have to reload (if this munition is used the first time or if there weren't any magazines available before) |
---|
| 245 | if (this->munition_->needReload(this)) |
---|
| 246 | this->munition_->reload(this, false); |
---|
| 247 | |
---|
| 248 | // Add the initial munition |
---|
| 249 | if (this->initialMunition_ > 0 && this->munition_->getNumMunitionInCurrentMagazine(this) == this->munition_->getMaxMunitionPerMagazine()) |
---|
| 250 | { |
---|
| 251 | // The current magazine is still full, so let's just add another magazine to |
---|
| 252 | // the stack and reduce the current magazine to the given amount of munition |
---|
| 253 | |
---|
| 254 | unsigned int initialmunition = this->initialMunition_; |
---|
| 255 | if (initialmunition > this->munition_->getMaxMunitionPerMagazine()) |
---|
| 256 | initialmunition = this->munition_->getMaxMunitionPerMagazine(); |
---|
| 257 | |
---|
| 258 | this->munition_->takeMunition(this->munition_->getMaxMunitionPerMagazine() - initialmunition, this); |
---|
| 259 | this->munition_->addMagazines(1); |
---|
| 260 | } |
---|
| 261 | else |
---|
| 262 | { |
---|
| 263 | // The current magazine isn't full, add the munition directly |
---|
| 264 | |
---|
| 265 | this->munition_->addMunition(this->initialMunition_); |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | else |
---|
[11052] | 270 | { |
---|
[11071] | 271 | this->munition_ = nullptr; |
---|
[11052] | 272 | } |
---|
[2918] | 273 | } |
---|
| 274 | |
---|
| 275 | void WeaponMode::reloaded() |
---|
| 276 | { |
---|
| 277 | this->bReloading_ = false; |
---|
| 278 | } |
---|
| 279 | |
---|
[6417] | 280 | void WeaponMode::computeMuzzleParameters(const Vector3& target) |
---|
[2918] | 281 | { |
---|
| 282 | if (this->weapon_) |
---|
[6417] | 283 | { |
---|
| 284 | this->muzzlePosition_ = this->weapon_->getWorldPosition() + this->weapon_->getWorldOrientation() * this->muzzleOffset_; |
---|
| 285 | |
---|
| 286 | Vector3 muzzleDirection; |
---|
| 287 | muzzleDirection = target - this->muzzlePosition_; |
---|
| 288 | this->muzzleOrientation_ = (this->weapon_->getWorldOrientation() * WorldEntity::FRONT).getRotationTo(muzzleDirection) * this->weapon_->getWorldOrientation(); |
---|
| 289 | } |
---|
[2918] | 290 | else |
---|
[6417] | 291 | { |
---|
| 292 | this->muzzlePosition_ = this->muzzleOffset_; |
---|
| 293 | this->muzzleOrientation_ = Quaternion::IDENTITY; |
---|
| 294 | } |
---|
[2918] | 295 | } |
---|
| 296 | |
---|
[6417] | 297 | Vector3 WeaponMode::getMuzzleDirection() const |
---|
[2918] | 298 | { |
---|
| 299 | if (this->weapon_) |
---|
[6417] | 300 | return (this->getMuzzleOrientation() * WorldEntity::FRONT); |
---|
[2918] | 301 | else |
---|
[6417] | 302 | return WorldEntity::FRONT; |
---|
[2918] | 303 | } |
---|
| 304 | |
---|
[11108] | 305 | void WeaponMode::setFireSound(const std::string& soundPath, const float soundVolume) |
---|
[2918] | 306 | { |
---|
[11108] | 307 | fireSoundPath_ = soundPath; |
---|
| 308 | fireSoundVolume_ = soundVolume; |
---|
[6417] | 309 | } |
---|
| 310 | |
---|
[11108] | 311 | const std::string& WeaponMode::getFireSound() |
---|
[6417] | 312 | { |
---|
[11108] | 313 | return fireSoundPath_; |
---|
[2918] | 314 | } |
---|
[7163] | 315 | |
---|
[11108] | 316 | void WeaponMode::setReloadSound(const std::string& soundPath, const float soundVolume) |
---|
[11052] | 317 | { |
---|
[11108] | 318 | reloadSoundPath_ = soundPath; |
---|
| 319 | reloadSoundVolume_ = soundVolume; |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | const std::string& WeaponMode::getReloadSound() |
---|
| 323 | { |
---|
| 324 | return reloadSoundPath_; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | void WeaponMode::playFireSound() |
---|
| 328 | { |
---|
| 329 | WorldSound* unusedSound = nullptr; |
---|
| 330 | |
---|
| 331 | if (!GameMode::isMaster()) |
---|
[11052] | 332 | { |
---|
[11108] | 333 | return; |
---|
[7163] | 334 | } |
---|
[11108] | 335 | |
---|
| 336 | // If no sound path or no weapon was specified, then no sound is played. |
---|
| 337 | if (fireSoundPath_ == BLANKSTRING || !this->getWeapon()) |
---|
| 338 | { |
---|
| 339 | return; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | // 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) |
---|
| 343 | for (auto sound : fireSounds_) |
---|
| 344 | { |
---|
| 345 | if( sound && !(sound->isPlaying())) |
---|
| 346 | { |
---|
| 347 | // Unused sound found |
---|
| 348 | unusedSound = sound; |
---|
| 349 | break; |
---|
| 350 | } |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | // If no unused sound was found, create a new one and add it to the list |
---|
| 354 | if (!unusedSound) |
---|
| 355 | { |
---|
| 356 | unusedSound = new WorldSound(this->getContext()); |
---|
| 357 | fireSounds_.push_back(unusedSound); |
---|
| 358 | unusedSound->setLooping(false); |
---|
| 359 | unusedSound->setSource(fireSoundPath_); |
---|
| 360 | unusedSound->setVolume(fireSoundVolume_); |
---|
| 361 | this->getWeapon()->attach(unusedSound); |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | // Play the fire sound |
---|
| 365 | unusedSound->play(); |
---|
[7163] | 366 | } |
---|
| 367 | |
---|
[11108] | 368 | void WeaponMode::playReloadSound() |
---|
| 369 | { |
---|
| 370 | if (!GameMode::isMaster()) |
---|
| 371 | { |
---|
| 372 | return; |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | // If no sound path or no weapon was specified, then no sound is played. |
---|
| 376 | if (reloadSoundPath_ == BLANKSTRING || !this->getWeapon()) |
---|
| 377 | { |
---|
| 378 | return; |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | // Create a reload WorldSound if not done yet |
---|
| 382 | if (!reloadSound_) |
---|
| 383 | { |
---|
| 384 | reloadSound_ = new WorldSound(this->getContext()); |
---|
| 385 | reloadSound_->setSource(reloadSoundPath_); |
---|
| 386 | reloadSound_->setVolume(reloadSoundVolume_); |
---|
| 387 | this->getWeapon()->attach(reloadSound_); |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | // Play the reload sound |
---|
| 391 | reloadSound_->play(); |
---|
| 392 | } |
---|
[2918] | 393 | } |
---|