From c7d0f5681abe2d98464066f36863ace63b273fbd Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Sat, 25 May 2024 22:02:27 +0200 Subject: [PATCH] feat(mystical-upgrader): replace first picked item by the correct block --- mystical-upgrader.lua | 46 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/mystical-upgrader.lua b/mystical-upgrader.lua index 754698e..f51a145 100644 --- a/mystical-upgrader.lua +++ b/mystical-upgrader.lua @@ -105,7 +105,7 @@ local inspectBlockName = function() return block and block.name end -local selectBlock = function(itemName) +local selectItem = function(itemName) for i=1, 16, 1 do local details = turtle.getItemDetail(i) if details and details.name == itemName then @@ -124,7 +124,7 @@ local replaceBlockAt = function(y) return false end - local selected = selectBlock(blockName) + local selected = selectItem(blockName) if not selected then return 'error' @@ -136,10 +136,18 @@ local replaceBlockAt = function(y) end local placeDownFillBlock = function() - selectBlock(FILL_BLOCK) + selectItem(FILL_BLOCK) turtle.placeDown() end +local placeDownItem = function(itemName) + if selectItem(itemName) then + turtle.placeDown() + else + placeDownFillBlock() + end +end + -- Main state and stateful methods local function getInitialState() return { @@ -164,6 +172,15 @@ local goUp = function(state) state.y = state.y - 1 end +local function tryToSelectEmptySlot() + if not turtleUtils.selectFirstEmptySlot() then + turtleUtils.compactInventory() + if not turtleUtils.selectFirstEmptySlot() then + error('Fatal error: turtle inventory is full', 0) + end + end +end + -- 1. down procedure local downProcedure = function(state) while state.y < 60 do @@ -183,15 +200,32 @@ local downProcedure = function(state) break end - digAndGoDown(state) + if not state.firstPickedItem then + tryToSelectEmptySlot() + digAndGoDown(state) + + local item = turtle.getItemDetail(turtle.getSelectedSlot()) + -- consider FILL_BLOCK if no block was picked + state.firstPickedItem = (item and item.name) or FILL_BLOCK + else + digAndGoDown(state) + end + end end -- 2. up procedure local upProcedure = function(state) - while state.y ~= 1 do + local firstY = getInitialState().y + + while state.y ~= firstY do goUp(state) - placeDownFillBlock() + + if state.y == firstY + 1 and state.firstPickedItem then + placeDownItem(state.firstPickedItem) + else + placeDownFillBlock() + end end end