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