Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local clr, cp = term.clear, term.setCursorPos
- local function ensure(func, ...) -- Thanks Fatboychummy
- local args = table.pack(...)
- while not func() do
- for i = 1, args.n do
- args[i]()
- end
- end
- end
- local function find(item)
- local slot = 1
- while slot ~= 17 do
- local data = turtle.getItemDetail(slot)
- if data then
- if data.name == item then return slot end
- end
- slot = slot + 1
- end
- return false
- end
- local function refuel()
- if turtle.getFuelLevel() == 0 then
- print("Refueling...")
- turtle.select(find("minecraft:coal_block") or find("minecraft:coal") or 1)
- turtle.refuel(1)
- if turtle.getFuelLevel() == 0 then
- print("Refuel Needed.")
- while turtle.getFuelLevel() == 0 do
- sleep(.5) turtle.select(find("minecraft:coal") or 1) turtle.refuel(1)
- end
- end
- end
- end
- local function forward() refuel() write("0") ensure(turtle.forward) end
- local function reverse() refuel() write("1") ensure(turtle.back) end
- local function turn(d) write("2") if d == 1 then ensure(turtle.turnRight) else ensure(turtle.turnLeft) end end
- --[[
- Only when idle: <<= create the config only when it is time to move
- - config file does not exist
- Only while moving forward: <<= only edit config when starting to turn
- - config file exists with:
- - turned = false
- Only while turning: <<== edit on every stage of turn
- - config file exists with:
- - turned = true
- - stage = x
- --]]
- local wait = 1800
- local tbl = {}
- local function reset()
- local d = tbl.data or {farmed = 0}
- tbl = {waittimes = {wait, 0}, running = false, staging = false, stage = 0, ended = 0, data=d}
- end reset()
- local function save()
- write("*")
- local f = fs.open("/ft_config.lua", "w") f.write(textutils.serialize(tbl)) f.close()
- end
- if fs.exists("/ft_config.lua") then
- local f = fs.open("/ft_config.lua", "r")
- if not f then return printError("Failed to open ft_config.lua") end
- tbl = textutils.unserialize(f.readAll()) f.close()
- if type(tbl) ~= "table" then return printError("Failed to read ft_config.lua") end
- end
- clr()
- print("Rebooted:")
- print(textutils.serialize(tbl)) sleep(3) clr()
- local stages = {
- turtle.turnRight,
- forward,
- turtle.turnRight,
- turtle.turnLeft,
- forward,
- turtle.turnLeft,
- }
- --[[
- tbl.stage = tbl.stage + 1 save()
- if tbl.stage > #stages then
- tbl.stage = 0
- elseif tbl.stage == 2 or tbl.stage == 5 then
- if turtle.detect() then
- print("Journey ended.")
- tbl.staging = false
- tbl.ended = 1 -- has started it's 1st phase of returning
- end
- end
- --]]
- local function doStage()
- tbl.stage = tbl.stage + 1
- if turtle.detect() then
- if tbl.stage == 2 or tbl.stage == 5 then tbl.staging, tbl.ended = false, 1 return end
- end
- if tbl.stage == 6 then tbl.staging = false end
- if tbl.stage > 6 then tbl.stage = 1 end
- local tmp = tbl.stage
- if tbl.stage == 3 then tbl.staging = false end save()
- if tbl.ended ~= 1 then write("4["..tbl.stage.."]") stages[tmp]() end
- end
- while true do
- if tbl.running then
- while tbl.staging do
- doStage() if tbl.ended == 1 then turn(1) end
- end
- while not tbl.staging do
- if not turtle.detect() then
- local a,data = turtle.inspectDown()
- if a then
- if data.metadata == 7 then
- turtle.digDown() tbl.data.farmed = tbl.data.farmed + 1 save()
- local a = find("minecraft:carrot")
- if a ~= false then
- turtle.select(a)
- turtle.placeDown()
- end
- end
- else
- local a = find("minecraft:carrot")
- if a ~= false then turtle.select(a) turtle.placeDown() end
- end
- forward()
- else
- write("5")
- if tbl.ended == 1 then
- tbl.ended = 2 -- has started it's last phase of returning
- turn(1)
- elseif tbl.ended == 2 then
- turn() -- ENDED
- for i=1, 16 do
- local a = turtle.getItemDetail(i)
- if a then if a.name ~= "minecraft:coal_block" and a.name ~= "minecraft:coal" then turtle.select(i) turtle.drop(64) end end
- end turn() turn()
- reset() save() clr() break
- else
- tbl.staging = true
- end
- end
- end
- else
- cp(1,1) print("Time left: "..tbl.waittimes[1]-tbl.waittimes[2].." ")
- print("Starts in "..math.ceil((tbl.waittimes[1]-tbl.waittimes[2])/60).." minutes ") print("Farmed: "..tbl.data.farmed)
- sleep(1) tbl.waittimes[2] = tbl.waittimes[2] + 1 if tbl.waittimes[2] >= tbl.waittimes[1] then tbl.waittimes[2] = 0 tbl.running = true end save()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment