Created
June 30, 2022 06:40
-
-
Save dacioromero/8a23ea8ef76290c5d40af1e37e6fd1ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local WARP_MIN = 64 * 4 | |
local posPath = fs.combine(shell.dir(), "transport.pos") | |
local endAutomata = peripheral.find("endAutomata") | |
function getTotalItemCount () | |
local count = 0 | |
-- Slot 1 is for fuel | |
for slot=2,16 do | |
count = count + turtle.getItemCount(slot) | |
end | |
return count | |
end | |
function debug (text) | |
term.clear() | |
term.setCursorPos(1, 1) | |
term.write(text) | |
end | |
function writePos (state) | |
local file = fs.open(posPath, "w") | |
file.write(state) | |
file.close() | |
end | |
function readPos () | |
local file = fs.open(posPath, "r") | |
if file ~= nil then | |
local state = file.readLine() | |
file.close() | |
return state | |
else | |
return nil | |
end | |
end | |
function chargeWaitAndWarp (point) | |
local warpCost = endAutomata.estimateWarpCost(point) | |
debug("Charging") | |
endAutomata.chargeTurtle() | |
-- Keep trying to charge until we can warp | |
while turtle.getFuelLevel() < warpCost do | |
os.sleep(1) | |
endAutomata.chargeTurtle() | |
end | |
-- Cooldown is returned in ms | |
local cooldown = endAutomata.getOperationCooldown("warp") / 1000 | |
debug("Waiting " .. tostring(cooldown)) | |
os.sleep(cooldown) | |
debug("Warping to " .. point) | |
endAutomata.warpToPoint(point) | |
-- Store pos in case of restart | |
writePos(point) | |
end | |
function dump () | |
for slot=2,16 do | |
turtle.select(slot) | |
turtle.drop() | |
end | |
turtle.select(1) | |
end | |
function farm () | |
-- Wait to warp for power efficency | |
while getTotalItemCount() < WARP_MIN do | |
debug("Waiting for harvester") | |
while not turtle.suckUp() do | |
os.sleep(0.05) | |
end | |
debug("Sucking items") | |
while turtle.suckUp() do end | |
end | |
chargeWaitAndWarp("bread") | |
bread() | |
end | |
function bread () | |
debug("Emptying") | |
dump() | |
-- Wait to dump full inventory into chest | |
while getTotalItemCount() > 0 do | |
os.sleep(1) | |
dump() | |
end | |
chargeWaitAndWarp("farm") | |
farm() | |
end | |
-- Continue from before restart | |
if readPos() == "bread" then | |
bread() | |
else | |
farm() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment