Skip to content

fix(can_place_entity): check the requested direction - #437

Open
hashbender wants to merge 1 commit into
JackHopkins:mainfrom
hashbender:fix/can-place-entity-direction
Open

hashbender wants to merge 1 commit into
JackHopkins:mainfrom
hashbender:fix/can-place-entity-direction

Conversation

@hashbender

Copy link
Copy Markdown

Problem

can_place_entity ignores the direction it is given, for every entity.

The client sends the direction shifted by one:

response, elapsed = self.execute(
    self.player_index, name, direction.value + 1, x, y
)

and can_place_entity/server.lua maps it with its own global helper:

if prototype and prototype.type == "inserter" then
    return cardinals[(direction % 4) + 1]
else
    return cardinals[direction % 4]
end

With Factorio 2.0 directions (north 0, east 4, south 8, west 12) the value that arrives is 1, 5, 9 or 13. % 4 is always 1, so every non-inserter is checked facing north and every inserter facing east, whatever was asked for. The final check, storage.utils.avoid_entity, maps the same shifted value through storage.utils.get_entity_direction (the mapping place_entity uses), which doesn't match any of its cases and falls through to a default. The collision area is also always the north-facing footprint.

So can_place_entity(Prototype.SteamEngine, Direction.RIGHT, pos) answers for an engine facing UP. It can say yes where place_entity then refuses that direction, and no where place_entity would build. shift_entity uses can_place_entity, so it inherits the same error.

Fix

  • can_place_entity/client.py: send DirectionInternal.to_factorio_direction(direction), the same encoding place_entity sends.
  • can_place_entity/server.lua: map the direction with storage.utils.get_entity_direction, as place_entity does, so the answer is for the orientation place_entity builds. Swap the collision area's width and height for east/west. Remove the file's global get_entity_direction; nothing else uses it.
  • can_place_entity/agent.md: one line saying the check is for the given direction.

Testing

  • New tests/unit/test_can_place_entity_direction.py (server-free): runs the action in Lua (lupa, already a dependency) against a stub surface, for all four directions, and checks the direction that reaches surface.can_place_entity and the collision area's footprint (3x5 for UP/DOWN, 5x3 for RIGHT/LEFT). It also checks that the client sends 0, 4, 8, 12. On main, 3 of these 5 tests fail; with the fix all 5 pass.
  • New test_can_place_depends_on_direction in tests/actions/test_can_place.py: puts a pipe inside a steam engine's vertical footprint only. Then it checks that UP is refused and RIGHT is accepted. It needs a server and is left to the factorio-test workflow; I haven't run it.
  • The changed Lua compiles under lupa. ruff 0.11.13 is clean, and the server-free unit tests pass.

🤖 Generated with Claude Code

The client sent `direction.value + 1` and the action mapped it with its
own get_entity_direction: `cardinals[direction % 4]`. With Factorio 2.0
directions (0, 4, 8, 12) that is 1, 5, 9 or 13, so every non-inserter was
checked facing north and every inserter facing east, whatever direction
was asked for. The final check (avoid_entity) mapped the same shifted
value through storage.utils.get_entity_direction and got yet another
direction. So can_place_entity could say yes for a position place_entity
then refused in that direction, and no for one it would build.

Send the Factorio direction, as place_entity does, and map it with
storage.utils.get_entity_direction, the mapping place_entity uses, so the
answer is for the orientation place_entity builds. Swap the collision
area's width and height for east/west. Remove the file's global
get_entity_direction, which nothing else used.

Tests: a server-free run of the action in Lua (lupa) for all four
directions plus the client's encoding, and tests/actions: a steam engine
blocked only when facing UP.

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