Advertisement
fatboychummy

spindig

Aug 10th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.25 KB | None | 0 0
  1. local cache = peripheral.wrap("top")
  2. local saveFile = ".iter.dat"
  3.  
  4. if not cache then
  5.   error("No cache.", 0)
  6. end
  7.  
  8. local function save(file, n)
  9.   local h, err = io.open(file, 'w')
  10.   if h then
  11.     h:write(n):close()
  12.   else
  13.     printError("Failed to open file for writing.")
  14.     error(err, 0)
  15.   end
  16. end
  17.  
  18. local function load(file)
  19.   local h, err = io.open(file, 'r')
  20.   if h then
  21.     local dat = h:read("*a")
  22.     h:close()
  23.     return tonumber(dat) + 1
  24.   else
  25.     if type(err) == "string" then
  26.       if err:find("No such file") then
  27.         printError("Save file missing, starting at 1.")
  28.         os.sleep(2)
  29.         return 1
  30.       else
  31.         printError("Failed to open file for reading.")
  32.         error(err, 0)
  33.       end
  34.     else
  35.       error("Unknown error occured while opening file for reading.")
  36.     end
  37.   end
  38. end
  39.  
  40. local function iterateItems(func)
  41.   local a = {}
  42.   for i = 1, 16 do
  43.     if turtle.getItemCount(i) > 0 then
  44.       table.insert(a, func(i))
  45.     end
  46.   end
  47.   return a
  48. end
  49.  
  50. local function getCount()
  51.   local tmp = cache.getItem(1)
  52.   if tmp then
  53.     return tmp.getMetadata().count
  54.   else
  55.     return 0
  56.   end
  57. end
  58.  
  59. local function getInvCount()
  60.   return #iterateItems(function()
  61.     return 1
  62.   end)
  63. end
  64.  
  65. local function doDump()
  66.   local cnt = getCount()
  67.   return cnt < 9000
  68. end
  69.  
  70. local function dump()
  71.   iterateItems(function(i)
  72.     turtle.select(i)
  73.     turtle.dropUp()
  74.   end)
  75. end
  76.  
  77. local function doDig()
  78.   local inv = getInvCount()
  79.   return inv >= 15 and not doDump() or true
  80. end
  81.  
  82. local function dig()
  83.   for i = 1, 4 do
  84.     turtle.dig()
  85.     turtle.turnRight()
  86.   end
  87. end
  88.  
  89. local c = load(saveFile)
  90. while true do
  91.   term.clear()
  92.   term.setCursorPos(1, 1)
  93.   print("Running iteration " .. tostring(c))
  94.  
  95.   local fl = true
  96.   if doDig() then
  97.     print("Digging.")
  98.     dig()
  99.     print("Done.")
  100.   else
  101.     print("Cannot dig.")
  102.     fl = false
  103.   end
  104.  
  105.   if doDump() then
  106.     print("Dumping inv")
  107.     dump()
  108.     print("Done.")
  109.   elseif not fl then
  110.     error("Cannot dig and cannot dump.  Inventories are full.", 0)
  111.   end
  112.  
  113.   print("Total items in cache: " .. tostring(getCount()))
  114.   print("Saving.")
  115.   save(saveFile, c)
  116.   print("Done.")
  117.   print("Waiting 60 seconds.")
  118.   os.sleep(60)
  119.   c = c + 1
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement