LuaPlayer::can_place_entity

Post Reply
Bilka
Factorio Staff
Factorio Staff
Posts: 3118
Joined: Sat Aug 13, 2016 9:20 am
Contact:

LuaPlayer::can_place_entity

Post by Bilka »

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.

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

Re: LuaPlayer::can_place_entity

Post by Klonan »

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.
You can just check the distance is within the players build_distance:
http://lua-api.factorio.com/latest/LuaC ... d_distance

Bilka
Factorio Staff
Factorio Staff
Posts: 3118
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: LuaPlayer::can_place_entity

Post by Bilka »

Klonan wrote:You can just check the distance is within the players build_distance:
http://lua-api.factorio.com/latest/LuaC ... d_distance
We are currently doing that (36 is the square of the default build distance)

Code: Select all

function inrange(position, myplayer)
  return ((position[1]-myplayer.position.x)^2+(position[2]-myplayer.position.y)^2) < 36
end
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.

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.

blueblue
Long Handed Inserter
Long Handed Inserter
Posts: 75
Joined: Tue Apr 11, 2017 7:39 pm
Contact:

Re: LuaPlayer::can_place_entity

Post by blueblue »

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

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

Re: LuaPlayer::can_place_entity

Post by Rseding91 »

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.

galibert
Inserter
Inserter
Posts: 42
Joined: Fri Sep 15, 2017 7:42 am
Contact:

Re: LuaPlayer::can_place_entity

Post by galibert »

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.
I don't see what's difficult about it...

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

Bilka
Factorio Staff
Factorio Staff
Posts: 3118
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: LuaPlayer::can_place_entity

Post by Bilka »

galibert wrote:I don't see what's difficult about it...
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 :/
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Post Reply

Return to “Implemented mod requests”