Skip to content

fix(get_entity): do not return an entity from a neighbouring tile - #429

Open
hashbender wants to merge 1 commit into
JackHopkins:mainfrom
hashbender:fix/get-entity-only-at-position
Open

hashbender wants to merge 1 commit into
JackHopkins:mainfrom
hashbender:fix/get-entity-only-at-position

Conversation

@hashbender

Copy link
Copy Markdown

Problem

get_entity on an empty tile returns the entity on the tile next to it instead of None:

chest = place_entity(Prototype.IronChest, position=Position(x=0, y=0))
get_entity(Prototype.IronChest, Position(x=chest.position.x + 1, y=chest.position.y))
# -> the chest at (0.5, 0.5); expected None

Agents use get_entity(...) is None to find gaps in pipe or belt lines, so a one-tile gap stays hidden.

Root cause

  • get_entity/server.lua:13-24 searches {{x - 1, y - 1}, {x + 1, y + 1}} (width and height hard-coded to 1; the collision-box code is commented out).
  • :29-38 then takes the closest entity of that name, and any entity on a neighbouring tile is inside that area.

Fix

  • Only consider entities whose tile footprint (bounding_box rounded out to whole tiles) contains the position.
  • With none, the existing "not found" error makes the client return None, as before.
  • Multi-tile entities are still found from any tile they cover.

Testing

Run against a Factorio 2.0.73 headless server:

  • New test_get_entity_on_empty_neighbouring_tile_returns_none fails on main (returns the chest) and passes with the fix. All 10 tests in tests/actions/test_get_entity.py pass with the fix.
  • Extra check: for a 3x3 assembling machine, every tile it covers still returns it with the fix. One tile past its edge, main still returns it and the fix returns None.
  • ruff 0.11.13 format/check clean.

🤖 Generated with Claude Code

get_entity searches an area one tile wider than the position on every
side and returns the closest entity of the requested type in it. For an
empty tile next to an entity of that type (e.g. a gap in a pipe line) it
returned the neighbour, so a caller checking "is there a pipe here?" got
a pipe from the next tile.

Only consider entities whose tile footprint contains the position; when
there is none, the existing "not found" path returns None as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant