Page 1 of 1

[0.16.51] Crash when hovering mouse above a recipe

Posted: Sun Jan 13, 2019 12:17 pm
by pieppiep
I'm running the linux steam version 0.16.51 and made a little mod with just a data-final-fixes.lua

Code: Select all

local resource_list = {}

for _, resource in pairs(data.raw.resource) do
    if resource.minable.results ~= nil then
        for _, results in pairs(resource.minable.results) do
            if results.type == "item" then
                resource_list[#resource_list + 1] = { results.name, 1 }
            end
        end
    elseif resource.minable.result ~= nil then
        resource_list[#resource_list + 1] = { resource.minable.result, 1 }
    end
end

for _, resource in ipairs(resource_list) do
    data:extend({
        {
            type = "recipe",
            name = "transmute-" .. resource[1],
            subgroup = "raw-resource",
            ingredients = resource_list,
            results = {{ resource[1], #resource_list }}
        }
    })
end
What it does is make a list of all minable ore types and make for each a new recipe to create it with ingredients 1 of each ore type and result the same amount of types but all of the choosen ore type.
With just the basegame and this mod everything works as intended.
But when I add a full angels, bobs, madclown I have 30+ ore types and the game crashes (one core full cpu load) when I hover above the recipe with my mouse.

Re: [0.16.51] Crash when hovering mouse above a recipe

Posted: Sun Jan 13, 2019 12:39 pm
by Loewchen
Post the log please.

Re: [0.16.51] Crash when hovering mouse above a recipe

Posted: Sun Jan 13, 2019 12:50 pm
by pieppiep
Here is the log, but it doesn't contain anything usefull for as far I can see.
The last line in the log is saving finished and after saving I've crashed the game.
So I've added my little testing mod and a savegame with it.
If you load this savegame and don't synchronize the mods the game won't crash, if you let the game load and activate the mods it will crash when your hover the recipe.

Re: [0.16.51] Crash when hovering mouse above a recipe

Posted: Sun Jan 13, 2019 1:17 pm
by Loewchen
So the game does not actually crash, it is just busy until you kill the process?

Re: [0.16.51] Crash when hovering mouse above a recipe

Posted: Sun Jan 13, 2019 1:25 pm
by pieppiep
Yeah, I call a hang a crash, there's no exception or something like it.
I can still alt-tab away and back, but alt-f4 or right click and close doesn't do anything.

/edit
pkill factorio doesn't kill the process, pkill -9 factorio does kill it.

Re: [0.16.51] Crash when hovering mouse above a recipe

Posted: Wed Jan 30, 2019 9:55 am
by Rseding91
Thanks for the report. It's technically not crashed - it's just taking that long to calculate the total raw ingredients for the recipe. The reason being: every one of the plates has a recipe to make it from the ore and each ore has a recipe to make it from every other ore.

So in your example there are 39 different ores.

When the tooltip goes to make the total-raw for that recipe it checks "what recipe makes the thing I need" and sees: there's a recipe that takes 39 different ores and produces my required item.

For each of those 39 different ingredients in the recipe it checks: is there a recipe that makes this item (excluding any recipes I've already used to make the item I'm checking).

So: Magic-Plate takes (Ore 1 ... 39) -> Ore 1 takes (transmute 1 ... 38) -> transmute 1 takes (Ore 2 ... 39) -> Ore 2 takes (transmute 1 ... 39) -> transmute 1 can't be used because it's already in this lookup chain (transmute 2 ... 39) -> transmute 2 takes (Ore 1 ... 39) -> Ore 1 and 2 can't be used because it's already in this lookup chain

In the end it means to calculate the total raw items using the recipe chain you've setup it needs to do O(N!) lookups where your N is 39. 39! is: 20'397'882'081'197'443'358'640'281'739'902'897'356'800'000'000. Either that or it's O(N^N) which is even worse. I always have trouble conceptualizing big-O time in code.

So about some time after the heat-death of the universe it will finish calculating and show the results.

Re: [0.16.51] Crash when hovering mouse above a recipe

Posted: Wed Jan 30, 2019 9:59 am
by Rseding91
You can work around the issue by marking your recipes as: don't use this when calculating total raw by setting "allow_decomposition = false" on your recipes.

Re: [0.16.51] Crash when hovering mouse above a recipe

Posted: Wed Jan 30, 2019 11:03 am
by pieppiep
So if I understand it correctly the game tries all the 39 recipies in all possible orders, that's O(N!)
That's good to know, because that means I need a factor of 39^39/39! = 5.5*10^15 less cpu cores compared to O(N^N) :D

Thanks for the tip to use allow_decomposition = false
For now I've just changed it to 2*38 recipes. A recipe for each ore type to stone and a recipe for each ore type from stone.

As a software developer myself I'm thinking about how this could be fixed.
The only thing I can come up with is somehow setting a timeout on calculating the raw ingredients.
This can be done by a timeout like x seconds or a timeout by following at most x recipies.

Re: [0.16.51] Crash when hovering mouse above a recipe

Posted: Wed Jan 30, 2019 11:01 pm
by Rseding91
pieppiep wrote:
Wed Jan 30, 2019 11:03 am
As a software developer myself I'm thinking about how this could be fixed.
The only thing I can come up with is somehow setting a timeout on calculating the raw ingredients.
This can be done by a timeout like x seconds or a timeout by following at most x recipies.
That was the only idea I thought of as well.