Add more options for placing chests

This commit is contained in:
Gabriel Tofvesson 2024-12-03 22:58:38 +01:00
parent 147dc40365
commit d80ff4e679

View File

@ -57,9 +57,29 @@ local function isFull(minEmpty)
return true
end
local function placeChest()
if not turtle.select(CHEST_SLOT) then
Logger:error("Cannot select chest slot", CHEST_SLOT)
return false, nil, nil, nil
end
if turtle.placeUp() or (turtle.digUp() and turtle.placeUp()) then
return true, turtle.dropUp, turtle.digUp, function() end
end
if turtle.turnLeft() and turtle.turnLeft() and (turtle.place() or (turtle.dig() and turtle.place())) then
return true, turtle.drop, turtle.dig, function()
turtle.turnRight()
turtle.turnRight()
end
end
return false, nil, nil, nil
end
local function handleFullInv(minEmpty)
local didPlace = false
local result, drop, dig, onComplete
-- Empty inventory
while isFull(minEmpty) do
if not didPlace then
@ -72,7 +92,8 @@ local function handleFullInv(minEmpty)
-- Try: place, check block above is empty or dig it, place
-- If all fails, print error, wait and repeat
if not (turtle.select(CHEST_SLOT) and (turtle.placeUp() or ((not turtle.inspectUp() or turtle.digUp()) and turtle.placeUp()))) then
result, drop, dig, onComplete = placeChest()
if not result then
Logger:error("Can't place chest :(")
os.sleep(5)
goto continue
@ -80,11 +101,12 @@ local function handleFullInv(minEmpty)
didPlace = true
end
assert(drop ~= nil, "Placed chest, but drop operation is nil")
for i=1,16 do
if i == CHEST_SLOT then
goto continue_SLOT
end
if turtle.getItemCount(i) > 0 and not (turtle.select(i) and turtle.dropUp()) then
if turtle.getItemCount(i) > 0 and not (turtle.select(i) and drop()) then
Logger:error("Couldn't drop items into chest!")
goto continue
end
@ -95,10 +117,14 @@ local function handleFullInv(minEmpty)
::continue::
end
assert(dig ~= nil, "Placed chest, but dig operation is nil")
if didPlace and CHEST_PICKUP then
turtle.select(CHEST_SLOT)
turtle.digUp()
dig()
end
assert(onComplete ~= nil, "Placed chest, but onComplete operation is nil")
onComplete()
end
local function dig(checkRefuel)
@ -106,12 +132,13 @@ local function dig(checkRefuel)
turtle.dig()
checkRefuel()
end
turtle.digUp()
turtle.digDown()
end
local function line(length, turn, checkRefuel)
turtle.digDown()
for i=2,length do
for _=2,length do
dig(checkRefuel)
end
turn()
@ -120,7 +147,7 @@ end
local function panel(width, length, leftFirst, checkRefuel, checkFullInv)
Logger:trace("Panel:", width, length)
local turn, otherTurn = leftFirst and turtle.turnLeft or turtle.turnRight, leftFirst and turtle.turnRight or turtle.turnLeft
for i=2,width do
for _=2,width do
checkFullInv()
line(length, turn, checkRefuel)
dig(checkRefuel)