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
19 changes: 12 additions & 7 deletions fle/env/tools/agent/place_entity_next_to/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ storage.actions.place_entity_next_to = function(player_index, entity, ref_x, ref
error("Invalid direction " .. direction .. " provided. Please use 0 (north), 4 (east), 8 (south), or 12 (west).")
end

-- The entity is built from the character's inventory, so it must hold one
if player.get_item_count(entity) < 1 then
local inv_contents = storage.utils.format_inventory_for_error(player)
error("\"No " .. entity .. " in inventory. Current inventory: " .. inv_contents .. "\"")
end

-- Convert from Factorio 2.0 direction (0,4,8,12) to internal direction (0,1,2,3) for calculations
local internal_direction = direction / 4

Expand Down Expand Up @@ -543,13 +549,12 @@ storage.actions.place_entity_next_to = function(player_index, entity, ref_x, ref
end

local placement_info = storage.utils.serialize_entity(new_entity)

local item_stack = {name = entity, count = 1}
if player.get_main_inventory().can_insert(item_stack) then
player.get_main_inventory().remove(item_stack)
return placement_info
else

-- Consume the item; if it can't be removed, undo the placement
if player.remove_item{name = entity, count = 1} < 1 then
new_entity.destroy()
local inv_contents = storage.utils.format_inventory_for_error(player)
error("Not enough " .. entity .. " in inventory. Current inventory: " .. inv_contents)
error("\"No " .. entity .. " in inventory. Current inventory: " .. inv_contents .. "\"")
end
return placement_info
end
19 changes: 19 additions & 0 deletions tests/actions/test_place_entity_next_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,3 +1055,22 @@ def test_belt_smart_collision_avoidance(game):

assert belt, "Belt should be placed with smart collision avoidance"
print(f"✓ Belt placed at {belt.position} avoiding obstacles")


def test_place_next_to_consumes_item(game):
chest = game.place_entity(Prototype.WoodenChest, position=Position(x=0, y=0))
before = game.inspect_inventory()[Prototype.WoodenChest]
game.place_entity_next_to(
Prototype.WoodenChest, chest.position, direction=Direction.RIGHT, spacing=0
)
assert game.inspect_inventory()[Prototype.WoodenChest] == before - 1


def test_place_next_to_without_item_places_nothing(game):
chest = game.place_entity(Prototype.WoodenChest, position=Position(x=0, y=0))
assert game.inspect_inventory().get(Prototype.IronChest, 0) == 0
with pytest.raises(Exception, match="No iron-chest in inventory"):
game.place_entity_next_to(
Prototype.IronChest, chest.position, direction=Direction.RIGHT, spacing=0
)
assert not game.get_entities({Prototype.IronChest})
1 change: 1 addition & 0 deletions tests/actions/test_rotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def game(configure_game):
return configure_game(
inventory={
"iron-chest": 1,
"boiler": 1,
"pipe": 10,
"assembling-machine-2": 2,
"transport-belt": 10,
Expand Down