Page 1 of 1

[SOLVED]replacing ingredients in recipes

Posted: Sat May 12, 2018 4:02 pm
by Villfuk02
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.

Re: replacing ingredients in recipes

Posted: Sun May 13, 2018 1:33 am
by eradicator
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}.

Re: replacing ingredients in recipes

Posted: Sun May 13, 2018 8:51 am
by Villfuk02
After lots of trial and erro i created this:

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
And it works!

Re: replacing ingredients in recipes

Posted: Sun May 13, 2018 9:36 am
by darkfrei
Villfuk02 wrote:After lots of trial and erro i created this:
code
And it works!
How about normal and expensive recipes?

Re: [SOLVED]replacing ingredients in recipes

Posted: Sun May 13, 2018 11:06 am
by eradicator
There's a fair bit of redundancy in there which could be shortened by using a lookup table. I.e. something like

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
...further down

Code: Select all

  if n.name then
   n.name = replace(n.name)
  else
   n[1] = replace(n[1])
   end
Recipe difficulty should be hacked in too, something like:

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
     --....
Also your current version replaces all speed modules (i.e. 1,2,3,etc) with the same module, is that on purpose?