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
26 changes: 19 additions & 7 deletions fle/env/tools/agent/pickup_entity/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ storage.actions.pickup_entity = function(player_index, x, y, entity)
-- end
--end

-- Add chest contents if applicable
if ent.get_inventory(defines.inventory.chest) then
local chest_contents = storage.utils.get_contents_compat(ent.get_inventory(defines.inventory.chest))
for name, count in pairs(chest_contents) do
table.insert(items_to_insert, {name=name, count=count})
-- Add the contents of every inventory. Factorio 2.0 keeps fuel, input,
-- output, modules and lab packs in separate inventories (index 1 is only
-- the chest / fuel inventory), and destroying the entity loses the rest.
for i = 1, ent.get_max_inventory_index() do
local inventory = ent.get_inventory(i)
if inventory then
local contents = storage.utils.get_contents_compat(inventory)
for name, count in pairs(contents) do
table.insert(items_to_insert, {name=name, count=count})
end
end
end

Expand All @@ -63,8 +68,15 @@ storage.actions.pickup_entity = function(player_index, x, y, entity)
end
end

-- Add the entity itself
table.insert(items_to_insert, {name=ent.name, count=1})
-- Add the item that places the entity (its name can differ from the
-- entity's, e.g. the rail item places a straight-rail)
local item_name, item_count = ent.name, 1
local items_to_place = ent.prototype.items_to_place_this
if items_to_place and items_to_place[1] then
item_name = items_to_place[1].name
item_count = items_to_place[1].count or 1
end
table.insert(items_to_insert, {name=item_name, count=item_count})

-- Check if player can receive all items
if not can_receive_items(items_to_insert) then
Expand Down
15 changes: 15 additions & 0 deletions tests/actions/test_pickup_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,18 @@ def test_pickup_belts_that_dont_exist(game):
game.pickup_entity(nbelts)
except Exception:
assert True, "Should not be able to pick up a non-existent belt"


def test_pickup_furnace_returns_its_input(configure_game):
game = configure_game(
inventory={"stone-furnace": 1, "iron-ore": 10}, reset_position=True
)
furnace = game.place_entity(Prototype.StoneFurnace, position=Position(x=0, y=0))
# No fuel, so nothing smelts; the ore sits in the furnace's source inventory
game.insert_item(Prototype.IronOre, furnace, quantity=10)
assert game.inspect_inventory()[Prototype.IronOre] == 0

assert game.pickup_entity(furnace)
inventory = game.inspect_inventory()
assert inventory[Prototype.IronOre] == 10
assert inventory[Prototype.StoneFurnace] == 1