Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding TTL to CWeapon #1893

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions rts/Lua/LuaSyncedCtrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2243,6 +2243,10 @@ static bool SetSingleUnitWeaponState(lua_State* L, CWeapon* weapon, int index)
weapon->collisionFlags = lua_toint(L, index + 1);
} break;

case hashString("ttl"): {
weapon->ttl = (int) (lua_toint(L, index + 1) * GAME_SPEED);
} break;

default: {
return false;
} break;
Expand Down
6 changes: 3 additions & 3 deletions rts/Sim/Projectiles/WeaponProjectiles/StarburstProjectile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ CStarburstProjectile::CStarburstProjectile(const ProjectileParams& params): CWea

if (weaponDef != nullptr) {
maxSpeed = weaponDef->projectilespeed;
ttl = weaponDef->flighttime;
ttl = params.ttl;

// Default uptime is -1. Positive values override the weapondef.
// Default uptime is -1. Positive values override the projectile params.
if (uptime < 0)
uptime = weaponDef->uptime * GAME_SPEED;

if (weaponDef->flighttime == 0)
if (ttl == 0)
ttl = std::min(3000.0f, uptime + myrange / maxSpeed + 100);
}

Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Weapons/BombDropper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void CBombDropper::FireImpl(const bool scriptCall)
params.pos = weaponMuzzlePos;
params.end = currentTargetPos;
params.speed = launchSpeed;
params.ttl = (weaponDef->flighttime == 0)? ((range / projectileSpeed) + 15 + predict): weaponDef->flighttime;
params.ttl = (ttl == 0)? ((range / projectileSpeed) + 15 + predict): ttl;
params.tracking = tracking;

assert(weaponDef->projectileType == WEAPON_TORPEDO_PROJECTILE);
Expand Down
14 changes: 7 additions & 7 deletions rts/Sim/Weapons/Cannon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,27 @@ void CCannon::FireImpl(const bool scriptCall)
launchDir += (gsRNG.NextVector() * SprayAngleExperience() + SalvoErrorExperience());
launchDir.SafeNormalize();

int ttl = 0;
int thisTtl = 0;
Copy link
Collaborator

@saurtron saurtron Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't like Ttl form personally, but not the only place that writes it (Ttl) like that (vs TTL). Also maybe consider myTTL or myTtl instead of thisTtl/thisTTL, I think it's more in line with style around these files.

Anyways not really an issue since it's more of a personal preference thing, you can choose any you want from thisTtl/thisTTL/myTtl/myTTL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm yeah I think in camelcase abbreviations should be treated as a single word, rather than in all caps

const float sqSpeed2D = launchDir.SqLength2D() * projectileSpeed * projectileSpeed;
const int predict = math::ceil((sqSpeed2D == 0.0f) ?
(-2.0f * projectileSpeed * launchDir.y / gravity):
math::sqrt(targetVec.SqLength2D() / sqSpeed2D));

if (weaponDef->flighttime > 0) {
ttl = weaponDef->flighttime;
if (ttl > 0) {
thisTtl = ttl;
} else if (weaponDef->selfExplode) {
ttl = (predict + gsRNG.NextFloat() * 2.5f - 0.5f);
thisTtl = (predict + gsRNG.NextFloat() * 2.5f - 0.5f);
} else if ((weaponDef->groundBounce || weaponDef->waterBounce) && weaponDef->numBounce > 0) {
ttl = (predict * (1 + weaponDef->numBounce * weaponDef->bounceRebound));
thisTtl = (predict * (1 + weaponDef->numBounce * weaponDef->bounceRebound));
} else {
ttl = predict * 2;
thisTtl = predict * 2;
}

ProjectileParams params = GetProjectileParams();
params.pos = weaponMuzzlePos;
params.end = currentTargetPos;
params.speed = launchDir * projectileSpeed;
params.ttl = ttl;
params.ttl = thisTtl;
params.gravity = gravity;

WeaponProjectileFactory::LoadProjectile(params);
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Weapons/EmgCannon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void CEmgCannon::FireImpl(const bool scriptCall)
ProjectileParams params = GetProjectileParams();
params.pos = weaponMuzzlePos;
params.speed = dir * projectileSpeed;
params.ttl = weaponDef->flighttime > 0 ? weaponDef->flighttime : math::ceil(std::max(dist, range) / projectileSpeed);
params.ttl = ttl > 0 ? ttl : math::ceil(std::max(dist, range) / projectileSpeed);

WeaponProjectileFactory::LoadProjectile(params);
}
2 changes: 1 addition & 1 deletion rts/Sim/Weapons/MissileLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void CMissileLauncher::FireImpl(const bool scriptCall)
params.pos = weaponMuzzlePos;
params.end = currentTargetPos;
params.speed = startSpeed;
params.ttl = weaponDef->flighttime == 0? math::ceil(std::max(targetDist, range) / projectileSpeed + 25 * weaponDef->selfExplode): weaponDef->flighttime;
params.ttl = ttl == 0? math::ceil(std::max(targetDist, range) / projectileSpeed + 25 * weaponDef->selfExplode): ttl;
params.gravity = -weaponDef->myGravity;

WeaponProjectileFactory::LoadProjectile(params);
Expand Down
6 changes: 4 additions & 2 deletions rts/Sim/Weapons/StarburstLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ void CStarburstLauncher::FireImpl(const bool scriptCall)
params.end = currentTargetPos;
params.speed = speed;
params.error = aimError;
params.ttl = 200; //???
// Projectile TTL (params.ttl) is ignored by the Starburst Projectile and it only uses the weapondef.
// I tried overriding the projectile TTL to 1 and it caused the starburst rocket to fly off into space
Copy link
Collaborator

@saurtron saurtron Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it goes up until uptime runs out, and only then is ttl evaluated, so you need to set a ttl a bit higher than uptime in that case to see an effect. Could make the logic of how ttl and uptime interact a bit more intuitive maybe, but not in scope of this pr.

params.ttl = (ttl > 0) ? ttl: weaponDef->flighttime;
params.tracking = tracking;
params.maxRange = (weaponDef->flighttime > 0 || weaponDef->fixedLauncher)? MAX_PROJECTILE_RANGE: range;
params.maxRange = (ttl > 0 || weaponDef->fixedLauncher)? MAX_PROJECTILE_RANGE: range;

WeaponProjectileFactory::LoadProjectile(params);
}
Expand Down
2 changes: 1 addition & 1 deletion rts/Sim/Weapons/TorpedoLauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void CTorpedoLauncher::FireImpl(const bool scriptCall)
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.ttl = (ttl == 0)? math::ceil(std::max(dist, range) / projectileSpeed + 25): ttl;
params.tracking = tracking;

WeaponProjectileFactory::LoadProjectile(params);
Expand Down
2 changes: 2 additions & 0 deletions rts/Sim/Weapons/Weapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ CR_REG_METADATA(CWeapon, (
CR_MEMBER(nextSalvo),
CR_MEMBER(salvoLeft),
CR_MEMBER(salvoWindup),
CR_MEMBER(ttl),

CR_MEMBER(range),
CR_MEMBER(projectileSpeed),
Expand Down Expand Up @@ -139,6 +140,7 @@ CWeapon::CWeapon(CUnit* owner, const WeaponDef* def):
nextSalvo(0),
salvoLeft(0),
salvoWindup(0),
ttl(1),

range(1.0f),
projectileSpeed(1.0f),
Expand Down
1 change: 1 addition & 0 deletions rts/Sim/Weapons/Weapon.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class CWeapon : public CObject
int nextSalvo; // when the next shot in the current salvo will fire
int salvoLeft; // number of shots left in current salvo
int salvoWindup; // delay before first shot (in frames)
int ttl; // flight time for most projectile type weapons except for Starburst

float range;
float projectileSpeed;
Expand Down
2 changes: 2 additions & 0 deletions rts/Sim/Weapons/WeaponLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,7 @@ void CWeaponLoader::InitWeapon(CUnit* owner, CWeapon* weapon, const UnitDefWeapo
weapon->fastAutoRetargeting = defWeapon->fastAutoRetargeting;
weapon->fastQueryPointUpdate = defWeapon->fastQueryPointUpdate;
weapon->burstControlWhenOutOfArc = defWeapon->burstControlWhenOutOfArc;

weapon->ttl = weaponDef->flighttime;
}