[2.0.23] Productivity bonus incorrect when set by mods
Posted: Fri Dec 13, 2024 4:21 pm
My mod (https://mods.factorio.com/mod/progressive-productivity) sets productivity to different amounts in increments of minimally 1%, default 5% when the player gets to a breakpoint in production. When they hit a breakpoint, it notifies them with a game log, and currently, when they hit 105%, it notifies them repeatedly. The issue seems to be that 1.05 is stored internally as 1.04.
In my code I'm doing:
And then using it:
I've added a save which shows the issue, if you hover over the furnace it shows the percentage as 104%. I added a couple of logs to debug, and I got:
Additionally, I tried going into a game with no mods at all whatsoever and ran:
which also produced a 104% bonus on the furnace with iron ore in it.
It seems the reason is that the productivity_bonus is floored at 2 decimal places, rather than rounded to 2 decimal places (1.050001 works properly.)
In my code I'm doing:
Code: Select all
local function are_doubles_equal(a, b, epsilon)
epsilon = epsilon or 1e-4 -- Default epsilon value
return math.abs(a - b) < epsilon
end
Code: Select all
for recipe_name, prod_bonus in pairs(processed_recipes) do
if not are_doubles_equal(force.recipes[recipe_name].productivity_bonus, prod_bonus) then
local display_item_name = {"?", {"item-name."..recipe_name}, {"fluid-name."..recipe_name}, {"entity-name."..recipe_name}, recipe_name}
game.print({"", {"mod-message.progressive-productivity-progressed", display_item_name, (prod_bonus * 100)}})
force.recipes[recipe_name].productivity_bonus = prod_bonus
end
end
Code: Select all
538.765 Script @__progressive-productivity__/utility/product_cache.lua:87: Productivity not equal!
538.765 Script @__progressive-productivity__/utility/product_cache.lua:88: 1.039999961853
538.765 Script @__progressive-productivity__/utility/product_cache.lua:89: 1.05
Code: Select all
/c game.player.force.recipes["iron-plate"].productivity_bonus=1.05
It seems the reason is that the productivity_bonus is floored at 2 decimal places, rather than rounded to 2 decimal places (1.050001 works properly.)