LuaPlayer::can_place_entity
LuaPlayer::can_place_entity
surface.can_place_entity does not account for the player build range. It would be nice if there was player.can_place_entity which would account both for buildability and player range (including whether the player is on the right surface). If that is not possible, it would be nice to be able to find out if the player is in range to place an entity without creating the entity first.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Re: LuaPlayer::can_place_entity
You can just check the distance is within the players build_distance:Bilka wrote:surface.can_place_entity does not account for the player build range. It would be nice if there was player.can_place_entity which would account both for buildability and player range (including whether the player is on the right surface). If that is not possible, it would be nice to be able to find out if the player is in range to place an entity without creating the entity first.
http://lua-api.factorio.com/latest/LuaC ... d_distance
Re: LuaPlayer::can_place_entity
We are currently doing that (36 is the square of the default build distance)Klonan wrote:You can just check the distance is within the players build_distance:
http://lua-api.factorio.com/latest/LuaC ... d_distance
Code: Select all
function inrange(position, myplayer)
return ((position[1]-myplayer.position.x)^2+(position[2]-myplayer.position.y)^2) < 36
end
I'm basically looking for LuaControl::can_reach_entity BUT with an entity prototype and a position parameter, instead of a LuaEntity parameter.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Re: LuaPlayer::can_place_entity
Additionally the surface.can_place does not quite work to determine wether a player can place a building, for example it returns false if an item-entity is within the collision box. The LuaPlayer::can_place_entity would solve my request too.
unique_2 on discord and mod portal
Re: LuaPlayer::can_place_entity
I've added 3 new functions to LuaPlayer for 0.16: can_place_entity(), can_build_from_cursor(), and build_from_cursor().
If you want to get ahold of me I'm almost always on Discord.
Re: LuaPlayer::can_place_entity
I don't see what's difficult about it...Bilka wrote:This calculates the distance to the entity position, not to the entity bounding box which is a problem because the game uses the bounding box to calculate whether the player is in range to build something. While it is possible to make a function that does calculate the distance to the bounding box, it's rather performance intensive (getting the prototype and then calculating the distance between a point and a rectangle (that's fun...)) compared to using the check that the game itself uses.
Code: Select all
-- Find the minimal distance on one axis
local function min_distance(ppos, epos1, epos2)
local d1 = epos1 - ppos
local d2 = epos2 - ppos
-- If the signs are different, the minimum distance is zero
-- Lua-wise, the multiply is more efficient
if d1*d2 <= 0 then
return 0
end
-- Return the minimum of the absolute values
if d1 < 0 then
d1 = -d1
end
if d2 < 0 then
d2 = -d2
end
if d1 < d2 then
return d1
else
return d2
end
end
-- Test whether an entity is within build distance of the player
local function can_build(player, entity)
local dx = min_distance(player.position.x, entity.bounding_box.left_top.x, entity.bounding_box.right_bottom.x)
local dy = min_distance(player.position.y, entity.bounding_box.left_top.y, entity.bounding_box.right_bottom.y)
local dist = math.sqrt(dx*dx + dy*dy)
return dist <= player.build_distance
end
Re: LuaPlayer::can_place_entity
Never said it was. I like your function more than the one we've been using for around 2 months, so we might switch. Yours doesn't work for rotated things though :/galibert wrote:I don't see what's difficult about it...
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.