Skip to content

Commit

Permalink
Fix being able to pass through invalid ballistic values (#6398)
Browse files Browse the repository at this point in the history
## Description of the proposed changes

If you pass in an invalid value to `SetBallisticAcceleration` then the
simulation will freeze up. This is a bug in the engine, but we can 'fix'
it in Lua by simply error'ing out if we see such a value.

## Testing done on the proposed changes

Ran a game and tested/confirmed that regular values are untouched and
all variants of invalid values are caught.

## Additional context

Possibly related to:

-
https://forum.faforever.com/topic/8022/replays-of-games-stuck-in-infinite-loop/1

## Checklist

- [x] Changes are annotated, including comments where useful
- [x] Changes are documented in the changelog for the next game version
  • Loading branch information
Garanas authored Aug 9, 2024
1 parent f39f8cc commit 0a3a52e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog/snippets/fix.6398.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- (#6398) Fix a possible cause for a simulation freeze

It was possible to pass invalid numbers (NaN or infinite) as a ballistic acceleration for a projectile. This would cause the engine to freeze up. With these changes we introduce Lua guards to catch the invalid numbers and throw an error instead of passing the invalid number to the engine.
23 changes: 23 additions & 0 deletions lua/sim/Projectile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local ProjectileMethods = moho.projectile_methods
local ProjectileMethodsCreateChildProjectile = ProjectileMethods.CreateChildProjectile
local ProjectileMethodsGetMaxZigZag = ProjectileMethods.GetMaxZigZag
local ProjectileMethodsGetZigZagFrequency = ProjectileMethods.GetZigZagFrequency
local ProjectileMethodsSetBallisticAcceleration = ProjectileMethods.SetBallisticAcceleration

local EntityMethods = _G.moho.entity_methods
local EntityGetBlueprint = EntityMethods.GetBlueprint
Expand Down Expand Up @@ -996,6 +997,28 @@ Projectile = ClassProjectile(ProjectileMethods, DebugProjectileComponent) {
return frequency
end,

--- Set the vertical (gravitational) acceleration of the projectile. Default is -4.9, which is expected by the engine's weapon targeting and firing
---@param acceleration number
SetBallisticAcceleration = function(self, acceleration)

-- Fix an engine bug where the values `1.#INF` or `-1.#IND` passed
-- into this particular engine function can cause the simulation to freeze up.
--
-- Since `math.huge` does not exist (and does not cover the #IND case) I see
-- no other approach than this to try and 'fix' it.
--
-- Related sources:
-- - https://stackoverflow.com/questions/19107302/in-lua-what-is-inf-and-ind

-- guard to prevent invalid numbers (#IND) and infinite numbers (#INF) from reaching the engine function
local stringified = tostring(acceleration)
if stringified:find('#') then
error("Invalid acceleration value: " .. stringified)
end

return ProjectileMethodsSetBallisticAcceleration(self, acceleration)
end,

---------------------------------------------------------------------------
--#region Deprecated functionality

Expand Down

0 comments on commit 0a3a52e

Please sign in to comment.