[SOLVED]replacing ingredients in recipes
[SOLVED]replacing ingredients in recipes
I'm making my own modules to replace the vanilla ones, but because of compatibility i just removed the crafting recipes for the normal modules. Now i want to replace all "speed-module" in ingredients of all recipes with "module-B0" but I can't seem to find a way to do it.
Last edited by Villfuk02 on Sun May 13, 2018 8:52 am, edited 1 time in total.
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: replacing ingredients in recipes
Imho for compatibility it's best to just edit the original module properties/recipes. Recipe disabling is non-trivial especially if your mod is added/removed in an ongoing game (i.e. not newly started). But if you have some good reason to not want to you'll have to:
Loop through data.recipe
Loop through default/normal/expensive variants of ingredients subtable (not all of those are guaranteed to exist)
Loop through subtable to see if it has the old module and replace with new (string compare), be aware that ingredients table may have one of two different formats, either {type='item',name='thing',amount=1} or just {'thing',1}.
Loop through data.recipe
Loop through default/normal/expensive variants of ingredients subtable (not all of those are guaranteed to exist)
Loop through subtable to see if it has the old module and replace with new (string compare), be aware that ingredients table may have one of two different formats, either {type='item',name='thing',amount=1} or just {'thing',1}.
Re: replacing ingredients in recipes
After lots of trial and erro i created this:
And it works!
Code: Select all
for i, r in pairs(data.raw.recipe) do
if r.ingredients then
for j, n in pairs(r.ingredients) do
if n.name and string.find(n.name, "speed-module") then
n.name = "module-B0"
end
if n[1] and string.find(n[1], "speed-module") then
n[1] = "module-B0"
end
if n.name and string.find(n.name, "effectivity-module") then
n.name = "module-G0"
end
if n[1] and string.find(n[1], "effectivity-module") then
n[1] = "module-G0"
end
if n.name and string.find(n.name, "productivity-module") then
n.name = "module-R0"
end
if n[1] and string.find(n[1], "productivity-module") then
n[1] = "module-R0"
end
end
end
end
Re: replacing ingredients in recipes
How about normal and expensive recipes?Villfuk02 wrote:After lots of trial and erro i created this:codeAnd it works!
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: [SOLVED]replacing ingredients in recipes
There's a fair bit of redundancy in there which could be shortened by using a lookup table. I.e. something like
...further down
Recipe difficulty should be hacked in too, something like:
Also your current version replaces all speed modules (i.e. 1,2,3,etc) with the same module, is that on purpose?
Code: Select all
--replacement definition and function
local replacement = {
['speed-module-1'] = "module-B1"
['speed-module-2'] = "module-B2"
}
local function replace(module_name)
--keeps the old name if no replacement was found
return replacement[module_name] or module_name
end
Code: Select all
if n.name then
n.name = replace(n.name)
else
n[1] = replace(n[1])
end
Code: Select all
--for i, r in pairs(data.raw.recipe) do
-- if r.ingredients then
for _, x in pairs(data.raw.recipe) do
for _, r in pairs{x, x.normal, x.expensive} do
if r.ingredients then
--....