1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "TorpedoLauncher.h"
#include "WeaponDef.h"
#include "Map/Ground.h"
#include "Sim/Projectiles/WeaponProjectiles/WeaponProjectileFactory.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/Unit.h"
#include "System/SpringMath.h"
CR_BIND_DERIVED(CTorpedoLauncher, CWeapon, )
CR_REG_METADATA(CTorpedoLauncher,(
CR_MEMBER(tracking)
))
CTorpedoLauncher::CTorpedoLauncher(CUnit* owner, const WeaponDef* def): CWeapon(owner, def)
{
// null happens when loading
if (def != nullptr)
tracking = weaponDef->turnrate * def->tracks;
}
bool CTorpedoLauncher::TestTarget(const float3 pos, const SWeaponTarget& trg) const
{
// by default we are a waterweapon, therefore:
// if muzzle is above water, target position is only allowed to be IN water
// if muzzle is below water, target position being valid depends on submissile
//
// NOTE:
// generally a TorpedoLauncher has its muzzle UNDER water and can *only* fire at
// targets IN water but depth-charge weapons break first part of this assumption
// (as do aircraft-based launchers)
//
// this check used to be in the base-class but is really out of place there: any
// "normal" weapon with fireSubmersed = true should be able to fire out of water
// (regardless of submissile which applies only to TorpedoLaunchers) but was not
// able to, see #3951
//
// land- or air-based launchers cannot target anything not in water
if (weaponMuzzlePos.y > 0.0f && !TargetInWater(pos, trg))
return false;
// water-based launchers cannot target anything not in water unless submissile
if (weaponMuzzlePos.y <= 0.0f && !weaponDef->submissile && !TargetInWater(pos, trg))
return false;
return (CWeapon::TestTarget(pos, trg));
}
void CTorpedoLauncher::FireImpl(const bool scriptCall)
{
float3 dir = currentTargetPos - weaponMuzzlePos;
float3 vel;
const float dist = dir.LengthNormalize();
if (weaponDef->fixedLauncher) {
vel = weaponDir * weaponDef->startvelocity;
} else {
dir += (UpVector * std::max(0.0f, weaponDef->trajectoryHeight));
vel = dir.Normalize() * weaponDef->startvelocity;
}
ProjectileParams params = GetProjectileParams();
params.speed = vel;
params.pos = weaponMuzzlePos;
params.end = currentTargetPos;
params.ttl = (weaponDef->flighttime == 0)? math::ceil(std::max(dist, range) / projectileSpeed + 25): weaponDef->flighttime;
params.tracking = tracking;
WeaponProjectileFactory::LoadProjectile(params);
}
|