Advertisement
omr__

Diggy turtle STARTUP

Sep 11th, 2019
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.98 KB | None | 0 0
  1. K_VERSION = 0.093
  2.  
  3. function _print(message)
  4.     wrap = function(str) return "| " .. str .. string.rep(" ", 35 - string.len(str)) .. " |" end
  5.     split = function() print(string.rep("- ", 20)) end
  6.     _read = function() print("»", message, "«") return read() end
  7.     write = function() print(wrap(message)) end
  8.     sub = function() print(wrap(" » " .. message)) end
  9.     raw = function() print(message) end
  10.     return { split = split, write = write, read = _read, sub = sub, raw = raw }
  11. end
  12.  
  13. function message()
  14.     function printTitle()
  15.         _print("        Diggy Turtle v" .. getVersion(getRemoteData("Pu8jbGue")) .. " by omr").raw()
  16.         _print().split()
  17.     end
  18.  
  19.     function info()
  20.         printTitle()
  21.         _print("Features:").write()
  22.         _print("Auto-update").sub()
  23.         _print("Auto-resume").sub()
  24.         _print("Usage:").write()
  25.         _print("Start position: Bottom Right").sub()
  26.         _print("Chest position: Above turtle").sub()
  27.         _print("Fuel slot: Last").sub()
  28.         _print("Run: " .. fs.getName(shell.getRunningProgram()) .. " <Length> <Width> <Height>").sub()
  29.         _print().split()
  30.     end
  31.  
  32.     function status(params)
  33.         resetScreen()
  34.         printTitle()
  35.         _print("Details:").write()
  36.         _print("Size (x:y:z): " .. params[1] .. ":" .. params[2] .. ":" .. params[3]).sub()
  37.         _print("Fuel: " .. turtle.getFuelLevel()).sub()
  38.         _print().split()
  39.         if (params[4]) then
  40.             _print("Resuming...").raw()
  41.         end
  42.     end
  43.  
  44.     return { info = info, status = status }
  45. end
  46.  
  47. function resetScreen()
  48.     term.clear()
  49.     term.setCursorPos(1, 1)
  50. end
  51.  
  52. function getRemoteData(id)
  53.     if (not http) then return end
  54.     local httpResponce = http.get("http://pastebin.com/raw.php?i=" .. id)
  55.     local data = httpResponce.readAll()
  56.     httpResponce.close()
  57.     return data
  58. end
  59.  
  60. function getLocalData(name)
  61.     local file = fs.open(name, "r")
  62.     local data = file.readAll()
  63.     file.close()
  64.     return data
  65. end
  66.  
  67. function getVersion(data)
  68.     local _, versionIndex = string.find(data, 'K_VERSION *= *')
  69.    
  70.     if (versionIndex) then return tonumber(string.match(data, '%d+[%.?%d*]*', versionIndex + 1))
  71.     else return 0 end
  72. end
  73.  
  74. function loadLib(id, name, isAPI)
  75.     if (not fs.exists(name)) then
  76.         shell.run("pastebin get " .. id .. " " .. name)
  77.         assert(fs.exists(name), "Connection issue, try again later")
  78.     end
  79.  
  80.     if (isAPI) then
  81.         os.loadAPI(name)
  82.     else
  83.         local remoteVersion = getVersion(getRemoteData(id))
  84.         local localVersion = getVersion(getLocalData(name))
  85.         if (localVersion < remoteVersion) then
  86.             shell.run("rm " .. name)
  87.             shell.run("pastebin get " .. id .. " " .. name)
  88.             os.reboot()
  89.             --shell.run(shell.getRunningProgram())
  90.         end
  91.     end
  92. end
  93.  
  94. function saveParams(table)
  95.     local file = fs.open("resumeParams", "w")
  96.     file.write(textutils.serialize(table))
  97.     file.close()
  98.     return true
  99. end
  100.  
  101. function loadParams()
  102.     if (fs.exists("resumeParams")) then
  103.         local file = fs.open("resumeParams", "r")
  104.         local data = file.readAll()
  105.         file.close()
  106.         local params = textutils.unserialize(data)
  107.         table.insert(params, "true")
  108.         return params
  109.     else
  110.         local params
  111.         if (#args ~= 3) then
  112.             local z = tonumber(_print("Enter length").read())
  113.             assert(type(z) == "number" and z > 0, "Value must be a number and greater than 0")
  114.             local x = tonumber(_print("Enter width").read())
  115.             assert(type(x) == "number" and x > 0, "Value must be a number and greater than 0")
  116.             local y = tonumber(_print("Enter height").read())
  117.             assert(type(y) == "number" and y > 0, "Value must be a number and greater than 0")
  118.             params = {x, y, z}
  119.         else
  120.             params = args
  121.         end
  122.         saveParams(params)
  123.         return params;
  124.     end
  125. end
  126.  
  127. function run()
  128.     message().info()
  129.     local params = loadParams()
  130.     message().status(params)
  131.  
  132.     if (shell.run("diggyturtle", unpack(params))) then
  133.         _print("Mining finished!").raw()
  134.         fs.delete("resumeParams")
  135.         fs.delete("/.lama/")
  136.     else
  137.         _print("Mining aborted!").raw()
  138.     end
  139. end
  140.  
  141. function main()
  142.     resetScreen()
  143.     loadLib("qDaGHBU3", "lama", true)  
  144.     loadLib("Pu8jbGue", "diggyturtle")
  145.     loadLib("85G2W3Hz", shell.getRunningProgram())
  146.     run()
  147. end
  148.  
  149. args = { ... }
  150. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement