Insert new minable results to range of entities

Place to get help with not working mods / modding interface.
Post Reply
User avatar
DemerNkardaz
Inserter
Inserter
Posts: 26
Joined: Wed Jan 18, 2023 12:32 am
Contact:

Insert new minable results to range of entities

Post by DemerNkardaz »

I want to add some resources to loottable of trees and rocks (or ores too) as extension, i.e. just call the function to insert new resources to existing entity, example - "we have resin → call the function to insert it to any tree harvesting result" for compatibility with any mods that change trees or rocks loot or/and adding new trees and rocks (like Alien Biomes and BZ Zircon).
I wrote some not good code that adds new resource to vanilla rocks (but works only to huge), but I disunderstanding how make it works with modded rocks, i.e call all entities by part of name like "rock-huge-*". I was found only variants like "rock-huge%-", and google can't reply to my question, my google searches bad too lol.

Code: Select all

function PLORD_addresult(t, name, result) 
if data.raw[t][name].minable.results then 
table.insert(data.raw[t][name].minable.results, result) end end
PLORD_addresult("simple-entity", "rock-big", {name="PLORD_resin_ore", amount_min=0, amount_max=1})
PLORD_addresult("simple-entity", "rock-huge", {name="PLORD_resin_ore", amount_min=0, amount_max=5})

Pi-C
Smart Inserter
Smart Inserter
Posts: 1652
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Insert new minable results to range of entities

Post by Pi-C »

DemerNkardaz wrote:
Wed Jan 18, 2023 3:39 pm
I wrote some not good code that adds new resource to vanilla rocks (but works only to huge), but I disunderstanding how make it works with modded rocks, i.e call all entities by part of name like "rock-huge-*". I was found only variants like "rock-huge%-", and google can't reply to my question, my google searches bad too lol.
Something like this?

Code: Select all

function PLORD_addresult(t, name, result) 
	local entity = data.raw[t][name]
	if entity and entity.minable and entity.minable.results then 
		table.insert(entity.minable.results, result) 
	end 
end

local amount_max
for e_name, entity in pairs(data.raw["simple-entitiy"]) do
	if e_name:find("rock") then
		amount_max = (e_name:find("huge") and 10) or
				(e_name:find("big") and 5) or
				(e_name:find("small") and 1) or
				3
		PLORD_addresult("simple-entity", e_name, {name="PLORD_resin_ore", amount_min=0, amount_max=amount_max})
	end
end
Please note that I've added checks to PLORD_addresult to make sure both entity and entity.minable exist before trying to read entity.minable.results. Without these, the function will throw an error if the entity isn't minable (trying to index a nil value).
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
DemerNkardaz
Inserter
Inserter
Posts: 26
Joined: Wed Jan 18, 2023 12:32 am
Contact:

Re: Insert new minable results to range of entities

Post by DemerNkardaz »

Pi-C wrote:
Wed Jan 18, 2023 7:34 pm
Something like this?
Ye, thank you!
Microtest on youtube https://www.youtube.com/watch?v=YWUiUtI5VTM
I see from tries part of stones cannot to use resin (if you spawn infinity of this, it not will contain resin), maybe it needs to convert single "result" with stone to table "results" code I think, but not very required this time.

User avatar
DemerNkardaz
Inserter
Inserter
Posts: 26
Joined: Wed Jan 18, 2023 12:32 am
Contact:

Re: Insert new minable results to range of entities

Post by DemerNkardaz »

Okay, some mini-additions, and now it works with all stones, and some with trees. Very thank for Pi-C!
But we have new trouble - Tral's Robot Tree Farm removes resin from tree loot lol.
Screens with working stones which don't have resin before and trees:
ImageImage

Screenshot of tree after launching Tral's Robot Tree Farm:
Image

Code: Select all

function PLORD_addresult(t, name, result) 
	local entity = data.raw[t][name]
	if entity.minable.result and not entity.minable.results then 
	entity.minable.results = {{entity.minable.result, entity.minable.count}}
	end
		if entity and entity.minable and entity.minable.results then 
		table.insert(entity.minable.results, result) 
	end
end

Code: Select all

local amount_max
for e_name, entity in pairs(data.raw["simple-entity"]) do
	if e_name:find("rock") then
		amount_max = (e_name:find("huge") and 10) or
				(e_name:find("big") and 5) or
				(e_name:find("small") and 1) or
				2
		PLORD_addresult("simple-entity", e_name, {name="PLORD_resin_ore", amount_min=0, amount_max=amount_max})
	end
end
for e_name, entity in pairs(data.raw["tree"]) do
	if e_name:find("tree") then
		amount_max = (e_name:find("dry") and 2) or
				(e_name:find("tree") and 1) or
				1
		PLORD_addresult("tree", e_name, {name="PLORD_resin_ore", amount_min=0, amount_max=amount_max})
	end
end

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2540
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: Insert new minable results to range of entities

Post by FuryoftheStars »

DemerNkardaz wrote:
Wed Jan 18, 2023 8:53 pm
But we have new trouble - Tral's Robot Tree Farm removes resin from tree loot lol.
That sounds like the other mod is destructively setting its results table. They shouldn't do that and should be reported to them.

You can get around this in the mean time via 2 methods that I can think of off the top of my head:
  • Figure out in which data stage file they are doing this and move your code to the one after (assuming they're doing it in data.lua or data-updates.lua)
  • Set your mod to have an optional dependency on their mod and then run your code in the same data stage as theirs, thus forcing their mod to load first and then your changes are added after.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

User avatar
DemerNkardaz
Inserter
Inserter
Posts: 26
Joined: Wed Jan 18, 2023 12:32 am
Contact:

Re: Insert new minable results to range of entities

Post by DemerNkardaz »

FuryoftheStars wrote:
Wed Jan 18, 2023 9:01 pm
That sounds like the other mod is destructively setting its results table. They shouldn't do that and should be reported to them.

You can get around this in the mean time via 2 methods that I can think of off the top of my head:
  • Figure out in which data stage file they are doing this and move your code to the one after (assuming they're doing it in data.lua or data-updates.lua)
  • Set your mod to have an optional dependency on their mod and then run your code in the same data stage as theirs, thus forcing their mod to load first and then your changes are added after.
Tral's uses data-final-fixes. I was made it optional dependency on start making own mod, some starting setups for later settings ("recommended use with my mod" like).
Thank you, I place code to my data-final-fixes, and now it works both.
Image

Pi-C
Smart Inserter
Smart Inserter
Posts: 1652
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Insert new minable results to range of entities

Post by Pi-C »

DemerNkardaz wrote:
Wed Jan 18, 2023 8:53 pm
Okay, some mini-additions, and now it works with all stones, and some with trees. Very thank for Pi-C!
You're welcome!
But we have new trouble - Tral's Robot Tree Farm removes resin from tree loot lol.
Add the resin after Tral's Robot Tree Farm has removed it from trees! It only runs in data-final-fixes, so you must add an optional dependency on it (info.json) to force it to load before your mod:

Code: Select all

"dependencies" = [ "?  Tral_robot_tree_farm"]
Some more safeguards for your function

Code: Select all

function PLORD_addresult(t, name, result) 
	local entity = data.raw[t][name]
	
	-- Make sure entity exists and is minable!
	if entity and entity.minable then
		if entity.minable.result and not entity.minable.results then 
			-- minable.count may be nil. If not set, the game will fall add the default value (1) 
			-- after data-final-fixes
			entity.minable.results = {{entity.minable.result, entity.minable.count or 1}}
		end
	
		-- You already have checked that entity.minable exists!
		--if entity and entity.minable and entity.minable.results then
		if entity.minable.results then
			table.insert(entity.minable.results, result) 
		end
	end
end

Code: Select all

for e_name, entity in pairs(data.raw["tree"]) do
	if e_name:find("tree") then
		 amount_max = (e_name:find("dry") and 2) or
		-- You already checked that e_name contains "tree", so this test is redundant
		--		(e_name:find("tree") and 1) or
				1
		PLORD_addresult("tree", e_name, {name="PLORD_resin_ore", amount_min=0, amount_max=amount_max})
	end
end
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
DemerNkardaz
Inserter
Inserter
Posts: 26
Joined: Wed Jan 18, 2023 12:32 am
Contact:

Re: Insert new minable results to range of entities

Post by DemerNkardaz »

Pi-C wrote:
Wed Jan 18, 2023 9:32 pm
DemerNkardaz wrote:
Wed Jan 18, 2023 8:53 pm
Okay, some mini-additions, and now it works with all stones, and some with trees. Very thank for Pi-C!
You're welcome!
But we have new trouble - Tral's Robot Tree Farm removes resin from tree loot lol.
Add the resin after Tral's Robot Tree Farm has removed it from trees! It only runs in data-final-fixes, so you must add an optional dependency on it (info.json) to force it to load before your mod:

Code: Select all

"dependencies" = [ "?  Tral_robot_tree_farm"]
Some more safeguards for your function
Thanks!

User avatar
DemerNkardaz
Inserter
Inserter
Posts: 26
Joined: Wed Jan 18, 2023 12:32 am
Contact:

Re: Insert new minable results to range of entities

Post by DemerNkardaz »

Now I try to use this code as base for ingredient additions and have and worked and don't worked result.
I add new ingredient to craft for sulfur and for plastic bars, I don't see critical difference in vanilla recipes - both have "ingredients", but works it only on sulfur, and other structure similar.

ImageImage

Edited code from higher posts for recipes, very short by I thought it doesn't need other checks like minable.result (and maybe this my mistake):

Code: Select all

function PLORD_addingredient(t, name, ingredient) 
	local recipe = data.raw[t][name]
		if recipe.ingredients then
			table.insert(recipe.ingredients, ingredient) 
		end
end
No working and working, this identical but different target recipe name:

Code: Select all

for e_name, recipe in pairs(data.raw["recipe"]) do
	if e_name:find("plastic-bar") then PLORD_addingredient("recipe", e_name, {type="item", name="PLORD_rosin", amount=10})
	end
end
for e_name, recipe in pairs(data.raw["recipe"]) do
	if e_name:find("sulfur") then PLORD_addingredient("recipe", e_name, {type="item", name="PLORD_rosin", amount=10})
	end
end
Maybe this code real need more checks, or what some I don't know.

User avatar
DemerNkardaz
Inserter
Inserter
Posts: 26
Joined: Wed Jan 18, 2023 12:32 am
Contact:

Re: Insert new minable results to range of entities

Post by DemerNkardaz »

DemerNkardaz wrote:
Fri Jan 20, 2023 8:47 pm
Now I try to use this code as base for ingredient additions and have and worked and don't worked result.
I add new ingredient to craft for sulfur and for plastic bars, I don't see critical difference in vanilla recipes - both have "ingredients", but works it only on sulfur, and other structure similar.
Tested some method, it works, shorter and better I think.

Code: Select all

function PLORD_add_ing(t, name, ingredient)
	table.insert(data.raw.recipe[name].ingredients, ingredient)
end
Add to recipe example with Storage Tank MK2

Code: Select all

PLORD_add_ing("recipe", "storage-tank2", {type="item", name="assembling-machine-2", amount=1})
Image

Not worked before and now worked plastic bar recipe adding example

Code: Select all

PLORD_add_ing("recipe", "plastic-bar", {type="item", name="PLORD_rosin", amount=4})
Image

User avatar
DemerNkardaz
Inserter
Inserter
Posts: 26
Joined: Wed Jan 18, 2023 12:32 am
Contact:

Re: Insert new minable results to range of entities

Post by DemerNkardaz »

Pi-C wrote:
Wed Jan 18, 2023 9:32 pm
Some more safeguards for your function
Got some question - this can use random number range? Like how it works with ores - any deposit have different count of resource.
And usable the float? If just write float like (e_name:find("big") and 1.2) this launches error

Post Reply

Return to “Modding help”