From 526d531ebd3808950a9d18d28fe7935ac997941e Mon Sep 17 00:00:00 2001 From: D4VID Date: Tue, 26 Nov 2024 21:39:20 +0100 Subject: [PATCH] Clear inventory grid before crafting --- craft.lua | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/craft.lua b/craft.lua index 4effa86..a4a733c 100644 --- a/craft.lua +++ b/craft.lua @@ -11,7 +11,7 @@ local inventory = component.inventory_controller RESULT_SLOT = 4 -- robots inventory is 4 wide and crafting uses the top left portion -local slot_map = { +SLOT_MAP = { 1, 2, 3, 5, 6, 7, 9, 10, 11 @@ -177,6 +177,20 @@ local function fetch_item(item, output_slot, count) return false end +---clear the 3x3 grid of any leftover items +local function clear_crafting_space() + for _, slot in ipairs(SLOT_MAP) do + if inventory.getStackInInternalSlot(slot) then + robot.select(slot) + store_item() + end + end + if inventory.getStackInInternalSlot(RESULT_SLOT) then + robot.select(RESULT_SLOT) + store_item() + end +end + ---accepts a recipe table containing result and ingredients ---@param recipe table ---@param count number how many items to craft @@ -188,7 +202,7 @@ local function craft_single_recipe(recipe, count) end for i, ingredient in pairs(recipe.shape) do -- print('Fetching ' .. ingredient) - if not fetch_item(ingredient, slot_map[i], count) then + if not fetch_item(ingredient, SLOT_MAP[i], count) then print('Failed to fetch ' .. ingredient) confirm() end @@ -391,6 +405,8 @@ local function craft_item(search) return false end + clear_crafting_space() + -- proceed to crafting the things for i, rec in ipairs(crafting_order) do print('Step #' .. i .. ' - ' .. rec.result)