feat(mystical-upgrader): replace first picked item by the correct block
This commit is contained in:
parent
1da3d12cdc
commit
c7d0f5681a
@ -105,7 +105,7 @@ local inspectBlockName = function()
|
|||||||
return block and block.name
|
return block and block.name
|
||||||
end
|
end
|
||||||
|
|
||||||
local selectBlock = function(itemName)
|
local selectItem = function(itemName)
|
||||||
for i=1, 16, 1 do
|
for i=1, 16, 1 do
|
||||||
local details = turtle.getItemDetail(i)
|
local details = turtle.getItemDetail(i)
|
||||||
if details and details.name == itemName then
|
if details and details.name == itemName then
|
||||||
@ -124,7 +124,7 @@ local replaceBlockAt = function(y)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local selected = selectBlock(blockName)
|
local selected = selectItem(blockName)
|
||||||
|
|
||||||
if not selected then
|
if not selected then
|
||||||
return 'error'
|
return 'error'
|
||||||
@ -136,10 +136,18 @@ local replaceBlockAt = function(y)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local placeDownFillBlock = function()
|
local placeDownFillBlock = function()
|
||||||
selectBlock(FILL_BLOCK)
|
selectItem(FILL_BLOCK)
|
||||||
turtle.placeDown()
|
turtle.placeDown()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local placeDownItem = function(itemName)
|
||||||
|
if selectItem(itemName) then
|
||||||
|
turtle.placeDown()
|
||||||
|
else
|
||||||
|
placeDownFillBlock()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- Main state and stateful methods
|
-- Main state and stateful methods
|
||||||
local function getInitialState()
|
local function getInitialState()
|
||||||
return {
|
return {
|
||||||
@ -164,6 +172,15 @@ local goUp = function(state)
|
|||||||
state.y = state.y - 1
|
state.y = state.y - 1
|
||||||
end
|
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
|
-- 1. down procedure
|
||||||
local downProcedure = function(state)
|
local downProcedure = function(state)
|
||||||
while state.y < 60 do
|
while state.y < 60 do
|
||||||
@ -183,16 +200,33 @@ local downProcedure = function(state)
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not state.firstPickedItem then
|
||||||
|
tryToSelectEmptySlot()
|
||||||
digAndGoDown(state)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 2. up procedure
|
-- 2. up procedure
|
||||||
local upProcedure = function(state)
|
local upProcedure = function(state)
|
||||||
while state.y ~= 1 do
|
local firstY = getInitialState().y
|
||||||
|
|
||||||
|
while state.y ~= firstY do
|
||||||
goUp(state)
|
goUp(state)
|
||||||
|
|
||||||
|
if state.y == firstY + 1 and state.firstPickedItem then
|
||||||
|
placeDownItem(state.firstPickedItem)
|
||||||
|
else
|
||||||
placeDownFillBlock()
|
placeDownFillBlock()
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 3. print report
|
-- 3. print report
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user