Using Single Autoplace Control for Multiple Resources

Place to get help with not working mods / modding interface.
TitaniumLucas
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sun Jul 14, 2024 8:19 pm
Contact:

Using Single Autoplace Control for Multiple Resources

Post by TitaniumLucas »

For the mod I am working on, I want to use an Autoplace Controller to generate lots of tiny resource patches of different materials (each patch is one material, but different patches are made of different materials). However, I cannot seem to get the behaviour I want.

Using the resource_autoplace.resource_autoplace_settings() method, it seems that it attempts to place the resources on the same spots, which results in only one resource being placed.
My limited experiments with the AutoplaceSpecification struct have returned similar results (I do not understand the parameters enough to know what is or isn't impactful).

Given that I want to add a lot of different minable resources, ideally I would like to avoid having to dedicate a unique slider for each resource. Does anyone have any suggestions as to how I can approach this?
Natha
Fast Inserter
Fast Inserter
Posts: 199
Joined: Sun Mar 15, 2015 1:48 pm
Contact:

Re: Using Single Autoplace Control for Multiple Resources

Post by Natha »

So you just want to add some more resources?

Could you post your code? Just copy-paste the existing resources didn't work?
TitaniumLucas
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sun Jul 14, 2024 8:19 pm
Contact:

Re: Using Single Autoplace Control for Multiple Resources

Post by TitaniumLucas »

Thank you for responding.

I am able to create resources, but I cannot get the world-gen behaviour I want. To clarify, I want to control multiple resources with the same slider, so that if you increase the richness of the slider, resource A deposits and resource B deposits both increase. Technically, this works as far as I can tell.

Instead, the locations where the resources spawn is not what I want. Currently, the behaviour I have is that the game seems to try to spawn the ores on top of each other, meaning that only one of the ores is actually spawned in, when I actually want them to spawn in different locations. I have created example prototypes here:

This creates a single autoplace controller, and 3 resources. When I load up the game, it seems like only the Blue ore spawns in, which I assume is due to the fact that it spawns in first due to order, and the type of ore placement does not allow for existing patches to be overwritten.

Code: Select all

local resource_autoplace = require("resource-autoplace")

-- Create single autoplace control to manage all 3 resources

data:extend({
	{
		type = "autoplace-control",
		name = "test-autoplace-control",
		category = "resource",
		can_be_disabled = true,
		richness = true,
	},
})

-- Create 3 test items, colored Red, Green, and Blue

data:extend({
	{
		type = "item",
		name = "test-resource-R",
		stack_size = 200,
		icons = {
			{
				icon = "__base__/graphics/icons/iron-ore.png",
				icon_size = 64,
				icon_mipmaps = 4,
				tint = { 1, 0, 0 },
			},
		},
	},
	{
		type = "item",
		name = "test-resource-G",
		stack_size = 200,
		icons = {
			{
				icon = "__base__/graphics/icons/iron-ore.png",
				icon_size = 64,
				icon_mipmaps = 4,
				tint = { 0, 1, 0 },
			},
		},
	},
	{
		type = "item",
		name = "test-resource-B",
		stack_size = 200,
		icons = {
			{
				icon = "__base__/graphics/icons/iron-ore.png",
				icon_size = 64,
				icon_mipmaps = 4,
				tint = { 0, 0, 1 },
			},
		},
	},
})

-- Create resource patches for all test resources based on __base__ implementation

data:extend({
	{
		type = "resource",
		name = "test-resource-R",
		icons = {
			{
				icon = "__base__/graphics/icons/iron-ore.png",
				icon_size = 64,
				icon_mipmaps = 4,
				tint = { 1, 0, 0 },
			},
		},
		flags = { "placeable-neutral" },
		tree_removal_probability = 0.8,
		tree_removal_max_distance = 32 * 32,
		minable = {
			mining_time = 1,
			result = "test-resource-R",
		},
		collision_box = { { -0.1, -0.1 }, { 0.1, 0.1 } },
		selection_box = { { -0.5, -0.5 }, { 0.5, 0.5 } },
		autoplace = resource_autoplace.resource_autoplace_settings({
			name = "test-autoplace-control",
			order = "b",
			base_density = 1,
			has_starting_area_placement = true,
			starting_re_factor_multipler = 1,
			regular_rq_factor_multiplier = 1.5,
		}),
		stage_counts = { 15000, 9500, 5500, 2900, 1300, 400, 150, 80 },
		stages = {
			sheets = {
				{
					filename = "__base__/graphics/entity/iron-ore/iron-ore.png",
					priority = "extra-high",
					size = 64,
					frame_count = 8,
					variation_count = 8,
					tint = { 1, 0, 0 },
				},
			},
		},
		map_color = { 1, 0, 0 },
	},
	{
		type = "resource",
		name = "test-resource-G",
		icons = {
			{
				icon = "__base__/graphics/icons/iron-ore.png",
				icon_size = 64,
				icon_mipmaps = 4,
				tint = { 1, 0, 0 },
			},
		},
		flags = { "placeable-neutral" },
		tree_removal_probability = 0.8,
		tree_removal_max_distance = 32 * 32,
		minable = {
			mining_time = 1,
			result = "test-resource-G",
		},
		collision_box = { { -0.1, -0.1 }, { 0.1, 0.1 } },
		selection_box = { { -0.5, -0.5 }, { 0.5, 0.5 } },
		autoplace = resource_autoplace.resource_autoplace_settings({
			name = "test-autoplace-control",
			order = "b",
			base_density = 1,
			has_starting_area_placement = true,
			starting_re_factor_multipler = 1,
			regular_rq_factor_multiplier = 1.5,
		}),
		stage_counts = { 15000, 9500, 5500, 2900, 1300, 400, 150, 80 },
		stages = {
			sheets = {
				{
					filename = "__base__/graphics/entity/iron-ore/iron-ore.png",
					priority = "extra-high",
					size = 64,
					frame_count = 8,
					variation_count = 8,
					tint = { 0, 1, 0 },
				},
			},
		},
		map_color = { 0, 1, 0 },
	},
	{
		type = "resource",
		name = "test-resource-B",
		icons = {
			{
				icon = "__base__/graphics/icons/iron-ore.png",
				icon_size = 64,
				icon_mipmaps = 4,
				tint = { 1, 0, 0 },
			},
		},
		flags = { "placeable-neutral" },
		tree_removal_probability = 0.8,
		tree_removal_max_distance = 32 * 32,
		minable = {
			mining_time = 1,
			result = "test-resource-B",
		},
		collision_box = { { -0.1, -0.1 }, { 0.1, 0.1 } },
		selection_box = { { -0.5, -0.5 }, { 0.5, 0.5 } },
		autoplace = resource_autoplace.resource_autoplace_settings({
			name = "test-autoplace-control",
			order = "b",
			base_density = 1,
			has_starting_area_placement = true,
			starting_re_factor_multipler = 1,
			regular_rq_factor_multiplier = 1.5,
		}),
		stage_counts = { 15000, 9500, 5500, 2900, 1300, 400, 150, 80 },
		stages = {
			sheets = {
				{
					filename = "__base__/graphics/entity/iron-ore/iron-ore.png",
					priority = "extra-high",
					size = 64,
					frame_count = 8,
					variation_count = 8,
					tint = { 0, 0, 1 },
				},
			},
		},
		map_color = { 0, 0, 1 },
	},
})

Natha
Fast Inserter
Fast Inserter
Posts: 199
Joined: Sun Mar 15, 2015 1:48 pm
Contact:

Re: Using Single Autoplace Control for Multiple Resources

Post by Natha »

If you dig into the code of resource-autoplace.lua, you find the following documented for resource_autoplace_settings():

Code: Select all

-- - patch_set_name - name of the patch set; patches sets of the same name and seed1 will overlap; default: name
-- - autoplace_control_name - name of the corresponding autoplace control; default: name
-- - random_probability - probability of placement at any given tile within a patch; default: 1
-- - base_spots_per_km2 - number of patches per square kilometer near the starting area
-- - has_starting_area_placement - true|false|nil - yes, no, and there is no special starting area, respectively
-- - seed1 - random seed to use when generating patch positions; default: 100
So, without testing, it seems for me that you need to pass "patch_set_name" to be the name of the resource (because in your code it's always "test-autoplace-control") and/or different "seed1" per resource.
TitaniumLucas
Manual Inserter
Manual Inserter
Posts: 3
Joined: Sun Jul 14, 2024 8:19 pm
Contact:

Re: Using Single Autoplace Control for Multiple Resources

Post by TitaniumLucas »

Just tested it, and setting a unique "patch_set_name" to each resource worked!

I checked where that documentation was located, and feel a bit silly to see I missed it xD.
Thanks for the help!
Post Reply

Return to “Modding help”