feat(mystical-upgrader): replace first picked item by the correct block

This commit is contained in:
Guillaume ARM 2024-05-25 22:02:27 +02:00
parent 1da3d12cdc
commit c7d0f5681a

View File

@ -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