Compare commits

...

2 Commits

Author SHA1 Message Date
Gabriel Tofvesson
408fed1ad2 Dig down twice after a panel 2024-12-03 23:00:39 +01:00
Gabriel Tofvesson
d80ff4e679 Add more options for placing chests 2024-12-03 22:58:38 +01:00

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)
@ -141,11 +168,13 @@ local function rectPrism(depth, width, length, leftFirst)
Logger:debug("Handling full inventory with target:", invEmptyTarget, " handled:", handleFullInv(invEmptyTarget))
end
Logger:trace("RectPrism:", depth, width, length)
for i=1,depth do
for _=1,depth do
panel(width, length, leftFirst, checkRefuel, checkFullInv)
while not turtle.down() do
turtle.digDown()
checkRefuel()
for __=1,2 do
while not turtle.down() do
turtle.digDown()
checkRefuel()
end
end
leftFirst = (not not leftFirst) ~= (width % 2 == 0)
end