Don't trigger "on_pre_build" when out of range

Things that we aren't going to implement
Post Reply
PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Don't trigger "on_pre_build" when out of range

Post by PFQNiet »

I found out the hard way:

- Placing an entity that is blocked by collision does not fire the on_pre_build event
- Placing an entity that is out of build range does call the event.

Currently I am working around this by re-implementing the build range check myself:

Code: Select all

local function canReach(player, position, entity)
	-- building reach is based on the entity's north-facing collision box
	local box = entity.collision_box
	local dx = math.max(box.left_top.x + position.x - player.position.x, 0, player.position.x - (box.right_bottom.x + position.x))
	local dy = math.max(box.left_top.y + position.y - player.position.y, 0, player.position.y - (box.right_bottom.y + position.y))
	return math.sqrt(dx*dx + dy*dy) <= player.build_distance
end
I then just discard the event "if not canReach(...)".

So I have a workaround that works (although I haven't checked if that should be a "<" or a "<=" - I don't expect it to matter, it's literally an edge case!) but it would be nice if either the event didn't fire at all (potentially breaks existing mods?) or had the built-in range check exposed in "event.can_reach" (backwards-compatible).

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: Don't trigger "on_pre_build" when out of range

Post by PFQNiet »

Never mind, I just missed LuaPlayer#can_build_entity() in the docs. That solves my problem neatly.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13209
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Don't trigger "on_pre_build" when out of range

Post by Rseding91 »

If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Won't implement”