diff --git a/changelog/snippets/fix.6398.md b/changelog/snippets/fix.6398.md new file mode 100644 index 0000000000..d9f65d1536 --- /dev/null +++ b/changelog/snippets/fix.6398.md @@ -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. diff --git a/lua/sim/Projectile.lua b/lua/sim/Projectile.lua index 71df80d9cd..c481e8aca9 100644 --- a/lua/sim/Projectile.lua +++ b/lua/sim/Projectile.lua @@ -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 @@ -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