Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions fle/env/tools/agent/can_place_entity/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ else:

- Offshore pumps require water tiles
- Some entities have specific placement requirements

4. Direction

- The check is for the entity in the given direction, the same orientation `place_entity` builds for that direction
5 changes: 4 additions & 1 deletion fle/env/tools/agent/can_place_entity/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from fle.env import DirectionInternal
from fle.env import entities as ent
from fle.env.game_types import Prototype
from fle.env.tools import Tool
Expand Down Expand Up @@ -33,8 +34,10 @@ def __call__(
x, y = self.get_position(position)
name, metaclass = entity.value

# Same direction encoding as place_entity (Factorio 2.0: 0, 4, 8, 12)
factorio_direction = DirectionInternal.to_factorio_direction(direction)
response, elapsed = self.execute(
self.player_index, name, direction.value + 1, x, y
self.player_index, name, factorio_direction, x, y
)

if not isinstance(response, dict):
Expand Down
22 changes: 7 additions & 15 deletions fle/env/tools/agent/can_place_entity/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ storage.actions.can_place_entity = function(player_index, entity, direction, x,
error("No " .. name .. " in inventory. Current inventory: " .. inv_contents)
end

-- Get entity direction and collision box
local direction_enum = get_entity_direction(entity, direction)
-- Get entity direction and collision box. `direction` is a Factorio 2.0
-- direction (0, 4, 8, 12); map it the same way place_entity does, so the
-- answer is for the orientation place_entity would build.
local direction_enum = storage.utils.get_entity_direction(entity, direction)
local prototype = prototypes.entity[entity]
local collision_box = prototype.collision_box
local width = math.abs(collision_box.right_bottom.x - collision_box.left_top.x)
local height = math.abs(collision_box.right_bottom.y - collision_box.left_top.y)
if direction_enum == defines.direction.east or direction_enum == defines.direction.west then
width, height = height, width
end

-- Define the area where the entity will be placed
local target_area = {
Expand Down Expand Up @@ -66,16 +71,3 @@ storage.actions.can_place_entity = function(player_index, entity, direction, x,
end
return true
end

function get_entity_direction(entity, direction)
if direction == nil then
return defines.direction.north
end
local prototype = prototypes.entity[entity]
local cardinals = {defines.direction.north, defines.direction.east, defines.direction.south, defines.direction.west}
if prototype and prototype.type == "inserter" then
return cardinals[(direction % 4) + 1]
else
return cardinals[direction % 4]
end
end
20 changes: 20 additions & 0 deletions tests/actions/test_can_place.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,23 @@ def test_can_place_over_player_large(game):
game.place_entity(
Prototype.SteamEngine, position=Position(x=0, y=0), direction=Direction.UP
)


def test_can_place_depends_on_direction(game):
# A steam engine is 3x5 facing UP and 5x3 facing RIGHT
game.move_to(Position(x=0, y=6))
engine = game.place_entity(
Prototype.SteamEngine, position=Position(x=0, y=0), direction=Direction.UP
)
position = engine.position
assert game.pickup_entity(engine)

# A pipe inside the vertical footprint only
game.place_entity(Prototype.Pipe, position=Position(x=position.x, y=position.y + 2))

assert not game.can_place_entity(
Prototype.SteamEngine, direction=Direction.UP, position=position
)
assert game.can_place_entity(
Prototype.SteamEngine, direction=Direction.RIGHT, position=position
)
98 changes: 98 additions & 0 deletions tests/unit/test_can_place_entity_direction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""can_place_entity checks the direction it is asked about. Server-free.

The client used to send ``direction.value + 1`` and the action mapped every
non-inserter to north (and every inserter to east), so the answer ignored
the direction. The action is run here in Lua (lupa) against a stub surface.
"""

from pathlib import Path

import pytest

from fle.env.entities import Direction, Position
from fle.env.game_types import Prototype
from fle.env.tools.agent.can_place_entity.client import CanPlaceEntity

lupa = pytest.importorskip("lupa")

SERVER_LUA = (
Path(__file__).resolve().parents[2]
/ "fle/env/tools/agent/can_place_entity/server.lua"
)

# A 3x5 entity (steam engine) at the origin; the stub surface records each call
STUB_LUA = """
defines = {direction = {north = 0, east = 4, south = 8, west = 12}}
prototypes = {entity = {["steam-engine"] = {
type = "generator",
collision_box = {left_top = {x = -1.5, y = -2.5}, right_bottom = {x = 1.5, y = 2.5}}}}}
calls = {placed = {}, areas = {}}
local surface = {
find_entities_filtered = function(filter)
table.insert(calls.areas, filter.area)
return {}
end,
can_place_entity = function(params)
table.insert(calls.placed, params.direction)
return true
end,
}
local player = {
position = {x = 0, y = 0}, reach_distance = 10, surface = surface, force = "player",
get_item_count = function() return 1 end,
teleport = function() end,
}
storage = {actions = {}, agent_characters = {player}, utils = {
-- place_entity's mapping; identity is enough to see which direction arrives
get_entity_direction = function(entity, direction) return direction end,
format_inventory_for_error = function() return "" end,
}}
storage.utils.avoid_entity = function(player_index, entity, position, direction)
return surface.can_place_entity{name = entity, position = position,
direction = storage.utils.get_entity_direction(entity, direction)}
end
"""


def _run(direction):
lua = lupa.LuaRuntime()
lua.execute(STUB_LUA)
lua.execute(SERVER_LUA.read_text())
assert lua.eval("storage.actions.can_place_entity")(
1, "steam-engine", direction, 0, 0
)
calls = lua.eval("calls")
placed = [calls.placed[i] for i in range(1, len(calls.placed) + 1)]
area = calls.areas[1]
size = (area[2].x - area[1].x, area[2].y - area[1].y)
return placed, size


@pytest.mark.parametrize(
"direction, footprint",
[
(Direction.UP, (3, 5)),
(Direction.RIGHT, (5, 3)),
(Direction.DOWN, (3, 5)),
(Direction.LEFT, (5, 3)),
],
)
def test_action_checks_requested_direction(direction, footprint):
placed, size = _run(direction.value)
assert placed == [direction.value]
assert size == footprint


def test_client_sends_factorio_direction():
tool = CanPlaceEntity.__new__(CanPlaceEntity)
tool.player_index = 1
sent = []

def execute(*args):
sent.append(args)
return True, 0

tool.execute = execute
for direction in (Direction.UP, Direction.RIGHT, Direction.DOWN, Direction.LEFT):
assert tool(Prototype.SteamEngine, direction, Position(x=0.5, y=0.5))
assert [args[2] for args in sent] == [0, 4, 8, 12]