Multiply / divide recipe ingredients

Place to get help with not working mods / modding interface.
Post Reply
Kenira
Fast Inserter
Fast Inserter
Posts: 108
Joined: Fri Nov 04, 2016 10:21 am
Contact:

Multiply / divide recipe ingredients

Post by Kenira »

I want to half all ingredients in a number of recipes (bob's power solar panels). I found this:
spoiler
but for my case i get the error for the line with the multiplication: "attempt to perform arithmetic on field '?' (a nil value)".

I found out that it works for some ingredients if i do it like this:
spoiler
Which works for solar panels and the basic ingredients in the recipe, but not for some of the ingredients changed out by bobspower. Bobs exchanges steel to titanium plate for example for certain panels. Even if i do

Code: Select all

 if ingredient[2] ~= nil then
    ingredient[2] = ingredient[2] * 2
end
to get rid of the error, the switched out ingredients don't get multiplied for some reason. It gets better though: Some ingredients, that do get exchanged (like circuit boards) will get multiplied, but not titanium / silver plates etc. Also, if i switch from multiplying by 2 to dividing by 2, i get an error: "conversion of data to type "unsigned short" failed" so even if the first issue gets sorted out it looks like i can't divide anyway? (there are a few ingredients with odd numbers which might be the problem)

I looked at the code from bobslibrary but i'm not really versed in lua so i can't judge if titanium plates etc not getting multiplied is an issue with how it changes out the ingredients. The code is here in any case:
Ingredient switching

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Multiply / divide recipe ingredients

Post by Klonan »

Some recipes define an ingredient like this:

Code: Select all

ingredients =
    {
      {"rocket", 1},
      {"explosives", 2}
    },
while others are like this:

Code: Select all

ingredients =
    {
      {type="fluid", name="water", amount=50},
      {type="fluid", name="crude-oil", amount=100}
    },

Kenira
Fast Inserter
Fast Inserter
Posts: 108
Joined: Fri Nov 04, 2016 10:21 am
Contact:

Re: Multiply / divide recipe ingredients

Post by Kenira »

Ah, that explains why it only affected some. Still not sure how i would go about fixing that since it's basically inside a string and i'd have to multiply just the number...
And then there's the error when dividing too.
Maybe i should just change everything manually.

User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Multiply / divide recipe ingredients

Post by prg »

You just use ingredient.amount instead of ingredient[2] in the second case.

Try math.floor() for the conversion problem.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!

Kenira
Fast Inserter
Fast Inserter
Posts: 108
Joined: Fri Nov 04, 2016 10:21 am
Contact:

Re: Multiply / divide recipe ingredients

Post by Kenira »

Thank you very much, it works now!
I had even tried the amount before but because not every recipe had it i of course got an error, now i just try both with a ~= nil.

daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: Multiply / divide recipe ingredients

Post by daniel34 »

I once wrote this function, it should still work and multiplies every ingredient (item or fluid) of the given recipe with a float value (e.g. 0.5 to divide by 2). Item results are rounded, items that would require < 0.5 are rounded up to 1 instead of being removed.

EDIT: I see you already got it in the time it took me to find it :lol: , but incase someone else needs it:

Code: Select all

function round_ingredient(num)
	if num < 1 then
		return 1;
	else
		return math.floor(num+0.5);
	end
end

function rescale_recipe(recipe, factor)
	for _, ingredient in pairs(recipe.ingredients) do
		if ingredient[2] ~= nil then
			ingredient[2] = round_ingredient(ingredient[2] * factor);
		end
		--log("recipe: "..recipe.name.." type: "..ingredient["type"]);
		if ingredient["amount"] ~= nil then
			if ingredient["type"] == "fluid" then
				ingredient["amount"] = ingredient["amount"] * factor;
			else
				ingredient["amount"] = round_ingredient(ingredient["amount"] * factor);
			end
		end
	end
end
quick links: log file | graphical issues | wiki

Kenira
Fast Inserter
Fast Inserter
Posts: 108
Joined: Fri Nov 04, 2016 10:21 am
Contact:

Re: Multiply / divide recipe ingredients

Post by Kenira »

Still a big thanks to you too!

I was also just discovering that i do need to catch the case where 1 / 2 becomes 0 ^^

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Multiply / divide recipe ingredients

Post by Klonan »

Kenira wrote:Still a big thanks to you too!

I was also just discovering that i do need to catch the case where 1 / 2 becomes 0 ^^
I suggest just doing:

Code: Select all

amount = math.ceil(amount/2)

Post Reply

Return to “Modding help”