[Done] Adding a Collision Mask

Place to get help with not working mods / modding interface.
Post Reply
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1401
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Done] Adding a Collision Mask

Post by TheSAguy » Thu Jun 13, 2019 8:01 pm

Hi,

I'd like it that certain types of buildings can't be placed on sand/desert tiles.
So I need to add a collision mask to the tiles and to the entities.

I got the tiles working. (I think). I'm placing it on all non minable tiles currently.
But am having difficulties with the entities.
The idea is that you can't place these building on non stone or concrete floors.

Here is my code:

Code: Select all

-- List of Entities Types that can't be placed on sand
local Blocked_Buildings = 
{
["furnace"] = true,
["boiler"] = true,
["generator"] = true,
["radar"] = true,
["assembling-machine"] = true,
["solar-panel"] = true,
["lab"] = true,
["rocket-silo"] = true,
["roboport"] = true,
["storage-tank"] = true,
["pump"] = true,
["market"] = true,
["accumulator"] = true,
["beacon"] = true,
["electric-turret"] = true,
["ammo-turret"] = true,
["reactor"] = true	
}

--- Collision Layer
local terrain_collision_layer = "layer-15"

--add collision mask to entities 
local function add_Collision_Layer(entity,layer)
	if not entity  then return end
	if entity.collision_mask then
		table.insert(entity.collision_mask, layer);
	else
		entity.collision_mask = {layer,"water-tile","player-layer"} 
	end
end


--Add collision to mask for non mineable tiles
for _,tile in pairs(data.raw["tile"]) do
	if not tile.minable then
		add_Collision_Layer(tile,terrain_collision_layer);
	end
end

--Add Collision layer to the Entity List.
for i = 1, #Blocked_Buildings do
	local entity = data.raw.Blocked_Buildings[i]
	add_Collision_Layer(entity, terrain_collision_layer)

end
I tried "for _,entity in pairs(data.raw[Blocked_Buildings]) do", but that did not work.

Thanks.
Last edited by TheSAguy on Thu Jun 13, 2019 11:15 pm, edited 1 time in total.

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

Re: Adding a Collision Mask

Post by Klonan » Thu Jun 13, 2019 8:04 pm

The layera are hardcoded, and you cannot add any more:
https://wiki.factorio.com/Types/CollisionMask

But you can use one of the layers, such as `layer-11`, which is unused by the base game and most mods

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2040
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Adding a Collision Mask

Post by darkfrei » Thu Jun 13, 2019 8:53 pm

Klonan wrote:
Thu Jun 13, 2019 8:04 pm
But you can use one of the layers, such as `layer-11`, which is unused by the base game and most mods
Why we cannot add new layers, for example up to 64 layers? Most of mods that use collision layers use "layer-11", but not another.

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1401
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: Adding a Collision Mask

Post by TheSAguy » Thu Jun 13, 2019 9:52 pm

I was using Layer-15. That’s the last one on the list.

Got my code fixed.

Code: Select all

--Add Collision layer to the Entity List.
for i = 1, #Blocked_Buildings do
	local entity = Blocked_Buildings[i]
	for _, entity in pairs(data.raw[entity]) do
		add_Collision_Layer(entity, terrain_collision_layer)
	end
end

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

Re: Adding a Collision Mask

Post by Klonan » Fri Jun 14, 2019 9:02 am

darkfrei wrote:
Thu Jun 13, 2019 8:53 pm
Klonan wrote:
Thu Jun 13, 2019 8:04 pm
But you can use one of the layers, such as `layer-11`, which is unused by the base game and most mods
Why we cannot add new layers, for example up to 64 layers? Most of mods that use collision layers use "layer-11", but not another.
Because the collision mask is currently a uint16_t, and uses bitmask logic, so the max number of unique masks is 16.

It is technically possible for the number to be extended sometime in the future, but now, in the modding interface, it is not possible to add any more masks

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 3175
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Done] Adding a Collision Mask

Post by eradicator » Fri Jun 14, 2019 9:39 am

@Klonan:

TL;DR: How difficult would adding can_be_placed_only_on_tiles = {} to entity prototypes be?

The problem with the "layer-x" masks is that they basically only work if only one mod uses them, or the authors collaborate. In all other cases it's almost certain that some intended behavior will inhibited. Case in point: @OP wants to add the layer to almost every tile on the entire map. As there is no good way to know which mod uses which layers this is one of the most unreliable hacks in the whole modding space :D.

I've made a post about more flexible placing rules for all entities a long time ago. Which also included "Place only on {list,of,tile,prototypes}" for entity prototypes. But nobody ever answered.
Author of: Hand Crank Generator, Screenshot Hotkey 2.0
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

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

Re: [Done] Adding a Collision Mask

Post by Klonan » Fri Jun 14, 2019 9:45 am

eradicator wrote:
Fri Jun 14, 2019 9:39 am
@Klonan:

TL;DR: How difficult would adding can_be_placed_only_on_tiles = {} to entity prototypes be?

The problem with the "layer-x" masks is that they basically only work if only one mod uses them, or the authors collaborate. In all other cases it's almost certain that some intended behavior will inhibited. Case in point: @OP wants to add the layer to almost every tile on the entire map. As there is no good way to know which mod uses which layers this is one of the most unreliable hacks in the whole modding space :D.

I've made a post about more flexible placing rules for all entities a long time ago. Which also included "Place only on {list,of,tile,prototypes}" for entity prototypes. But nobody ever answered.
Not gonna happen at least before 1.0

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 3175
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Done] Adding a Collision Mask

Post by eradicator » Fri Jun 14, 2019 9:58 am

Klonan wrote:
Fri Jun 14, 2019 9:45 am
Not gonna happen at least before 1.0
I see. Thanks for the answer. I'll put it on the list then :p.
Author of: Hand Crank Generator, Screenshot Hotkey 2.0
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding help”

Who is online

Users browsing this forum: No registered users