Page 1 of 1

[Done] Adding a Collision Mask

Posted: Thu Jun 13, 2019 8:01 pm
by TheSAguy
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.

Re: Adding a Collision Mask

Posted: Thu Jun 13, 2019 8:04 pm
by Klonan
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

Re: Adding a Collision Mask

Posted: Thu Jun 13, 2019 8:53 pm
by darkfrei
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.

Re: Adding a Collision Mask

Posted: Thu Jun 13, 2019 9:52 pm
by TheSAguy
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

Re: Adding a Collision Mask

Posted: Fri Jun 14, 2019 9:02 am
by Klonan
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

Re: [Done] Adding a Collision Mask

Posted: Fri Jun 14, 2019 9:39 am
by eradicator
@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.

Re: [Done] Adding a Collision Mask

Posted: Fri Jun 14, 2019 9:45 am
by Klonan
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

Re: [Done] Adding a Collision Mask

Posted: Fri Jun 14, 2019 9:58 am
by eradicator
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.