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

Conversation

seedship
Copy link
Contributor

@seedship seedship commented Jan 12, 2025

Added option to set ttl in SetUnitWeaponState, and added a ttl field into CWeapon. This overrides the original weaponDef->flighttime so games can set weapon ttl per weapon.

Addresses #1683

Tested with the following gadget in Zero-K:

if gadgetHandler:IsSyncedCode() then
    function gadget:AllowCommand(unitID, unitDefID, teamID)
        local ttl = 1
        print("Calling Spring Set Unit Weapon State")
        Spring.SetUnitWeaponState(unitID, 1, "ttl", ttl)
        return true
    end
end

Verified:

  • Nimbus Emg shells explode immediately
  • Emissary cannon shell explodes immediately
  • Likho bomb explodes immediately
  • Ronin rocket looks like it explodes immediately
  • Rogue rocket has no fuel and does an arc and cannot hit the rogue's max range
  • Starburst weapons (racketeer and impaler) seem to be unaffected.
  • Urchin torpedos just fall straight down and explode and cannot hit their target.

@seedship
Copy link
Contributor Author

It seems Starburst weapons/projectiles are complicated and do their own thing. I will leave them alone in this PR.

@@ -39,9 +39,10 @@ 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.

@@ -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

@@ -2206,6 +2206,10 @@ static bool SetSingleUnitWeaponState(lua_State* L, CWeapon* weapon, int index)
weapon->collisionFlags = lua_toint(L, index + 1);
} break;

case hashString("ttl"): {
weapon->ttl = lua_toint(L, index + 1);
Copy link
Collaborator

@sprunk sprunk Jan 12, 2025

Choose a reason for hiding this comment

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

Use seconds (see e.g. line 2176)

Suggested change
weapon->ttl = lua_toint(L, index + 1);
weapon->ttl = (int) (lua_tofloat(L, index + 1) * GAME_SPEED);

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.

This can make it a bit awkward if a specific number of frames is desired.

If changing it to seconds, then SetSingleUnitWeaponState key should maybe be called flightTime since ttl is in frames for projectiles Get/SetProjectileTimeToLive.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can control frames perfectly by multiplying by frames per second. Calling it flightTime sounds fine.

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.

Actually... weapondef flightTime is also in frames, probably better to have all in frames for consistency.

Copy link
Collaborator

@sprunk sprunk Jan 12, 2025

Choose a reason for hiding this comment

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

No it's not.

WEAPONDUMMYTAG(float, flighttime).defaultValue(0).scaleValue(GAME_SPEED).description("Lifetime of the projectile, in seconds. Missile/Torpedo/Starburst projectiles 'lose fuel' and fall down; Cannons explode; others fade away"); // needs to be written as int and read as float

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.

Right, perfect then, lets call it flightTime and make it in seconds too.

@seedship
Copy link
Contributor Author

Going on vacation for a week, will address the comments when I get back.

@seedship
Copy link
Contributor Author

Addressed comments:

  • Made starburst take in ttl from projectile params
  • Made TTL in seconds

Verified the changes by using the gadget in the description and verified missiles visually run out of fuel after 1 second

Copy link
Collaborator

@sprunk sprunk left a comment

Choose a reason for hiding this comment

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

Haven't tested yet but looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants