sparr wrote:The problem here, per Bilka on Discord, is that .find_entities_filtered applies to collision boxes, and the collision box for a diagonal straight rail with .position {1,1} does not actually include {1,1}.
I believe your understanding of tile coordinates is incorrect. Bilka is correct with the reason, but the underlying issue isn't in the game.
The center of map tiles is always at a 0.5 offset, and placement logic when placed at integer coordinates is to align to the grid, then find the center of the tile. (edit: unless the offgrid flag is applied, thanks Nexela)
As an example, creating a stone-wall at {0,0} and then searching {0,0} for that entity will not find it, because the entity was actually placed centered at {0.5, 0.5}. The point {0,0} is outside the bounds of the collision rect, because the width and height of the collision rect is strictly < 1.0.
Similarly, for the rail, the rail is aligned to the grid first, which makes the placement position when specified as {1,1} be translated to {0,0}, and then adjusted to the center based on direction, so it is placed centered at {0.5, 0.5} in your example which, of course, makes the point {1,1} outside of the collision rect. Turning on the tile grid and collision box debug info makes this obvious. See picture:
Grid lines are whole integer coordinates. Printed position is player coordinate X, which is as close to the center to the rail as I could make it by hand.
The way the alignment to the grid is performed is by truncation, so creating a rail like that at any position from 0 <= x(or y) < 2 will result in being placed centered at 0.5. Then, from 2 <= x(or y) < 4 it will be centered at 2.5, and so on.
Note, that using different placement direction(ie: 3), the rail gets created centered around {1.5, 1.5} instead, and so on for other directions that can cause centering at {0.5, 1.5} and {1.5, 0.5}.
Only vertical and horizontal rails get centered literally at {1, 1}. I'm sure there is a relation somewhere that neatly maps the offset based on the direction.
So if you want to find all the rail in the 2x2 grid, you can either check one of 5 coordinates depending on the directions you want to search for specifically, or you can check an area that includes all the possible points.
Code: Select all
game.player.print(#game.player.surface.find_entities_filtered{name="straight-rail", area={{0.5, 0.5}, {1.5, 1.5}}})
will find all 6 possible straight rails.
Code: Select all
game.print(#game.surfaces.nauvis.find_entities_filtered{name="straight-rail",position={x=0.5,y=0.5}})
will find the top left, northeast facing rail.
Code: Select all
game.print(#game.surfaces.nauvis.find_entities_filtered{name="straight-rail",position={x=1.5,y=1.5}})
will find the bottom right, northeast facing rail.
and so on.