Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cache = peripheral.wrap("top")
- local saveFile = ".iter.dat"
- if not cache then
- error("No cache.", 0)
- end
- local function save(file, n)
- local h, err = io.open(file, 'w')
- if h then
- h:write(n):close()
- else
- printError("Failed to open file for writing.")
- error(err, 0)
- end
- end
- local function load(file)
- local h, err = io.open(file, 'r')
- if h then
- local dat = h:read("*a")
- h:close()
- return tonumber(dat) + 1
- else
- if type(err) == "string" then
- if err:find("No such file") then
- printError("Save file missing, starting at 1.")
- os.sleep(2)
- return 1
- else
- printError("Failed to open file for reading.")
- error(err, 0)
- end
- else
- error("Unknown error occured while opening file for reading.")
- end
- end
- end
- local function iterateItems(func)
- local a = {}
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- table.insert(a, func(i))
- end
- end
- return a
- end
- local function getCount()
- local tmp = cache.getItem(1)
- if tmp then
- return tmp.getMetadata().count
- else
- return 0
- end
- end
- local function getInvCount()
- return #iterateItems(function()
- return 1
- end)
- end
- local function doDump()
- local cnt = getCount()
- return cnt < 9000
- end
- local function dump()
- iterateItems(function(i)
- turtle.select(i)
- turtle.dropUp()
- end)
- end
- local function doDig()
- local inv = getInvCount()
- return inv >= 15 and not doDump() or true
- end
- local function dig()
- for i = 1, 4 do
- turtle.dig()
- turtle.turnRight()
- end
- end
- local c = load(saveFile)
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- print("Running iteration " .. tostring(c))
- local fl = true
- if doDig() then
- print("Digging.")
- dig()
- print("Done.")
- else
- print("Cannot dig.")
- fl = false
- end
- if doDump() then
- print("Dumping inv")
- dump()
- print("Done.")
- elseif not fl then
- error("Cannot dig and cannot dump. Inventories are full.", 0)
- end
- print("Total items in cache: " .. tostring(getCount()))
- print("Saving.")
- save(saveFile, c)
- print("Done.")
- print("Waiting 60 seconds.")
- os.sleep(60)
- c = c + 1
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement