JustDoesGames

STP v1 (unreleased version)

Dec 21st, 2020 (edited)
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.66 KB | None | 0 0
  1. update = false
  2. --[[
  3.  
  4. Simplistic Turtle Program (STP)
  5.  
  6. --]]
  7.  
  8. if fs.exists("/disk") and not fs.exists("/disk/startup.lua") and shell.getRunningProgram() ~= "/disk/startup.lua" then
  9.     print("Type 'install' to install disk installer")
  10.     write(": ")
  11.     if string.lower(read()) == "install" then
  12.         if fs.exists("/disk") then
  13.             if fs.exists("/disk/startup.lua") then fs.delete("/disk/startup.lua") end
  14.             fs.copy(shell.getRunningProgram(), "/disk/startup.lua")
  15.             print("Installed to 'disk/startup.lua'. Rebooting...")
  16.             sleep(1) os.reboot()
  17.         end
  18.     end
  19. end
  20.  
  21. local w,h = term.getSize()
  22. clr, cp, sb, st = term.clear, term.setCursorPos, term.setBackgroundColor, term.setTextColor
  23. local loadingIcon, lisel = {"  \n  ", ". \n  ", "..\n  ", "..\n. ", "..\n.."," .\n..","  \n..","  \n ."},1
  24. local load = function() while math.random(1,2) ~= 1 do clr() cp(1,1) print("STL") write(loadingIcon[lisel]) lisel = lisel+1 if lisel > #loadingIcon then lisel = 1 end sleep(.2) end end
  25. local c = function(t) return print("[STP] "..t) end
  26. local setLabel = function(name) return os.setComputerLabel(tostring(name)) end -- did it this way to convert to string because fuck lua
  27. local prevLabel = os.getComputerLabel()
  28. setLabel("§aBooting STL...")
  29.  
  30. local status, color = "Idle", "e"
  31.  
  32. load()
  33.  
  34. if not http then
  35.     --c("Http is disabled. Skipping Update...") sleep(.5)
  36.     load() sleep(.5)
  37. elseif not update then
  38.     --c("Updates disabled. Skipping...") sleep(.2)
  39.     load() sleep(.2)
  40. else
  41.     --c("Checking for updates")
  42.     load()
  43.     load() st(colors.black)
  44.     if fs.exists("STP_tmp.lua") then fs.delete("STP_tmp.lua") end
  45.     local tmpx, tmpy = term.getCursorPos()
  46.     shell.run("pastebin get VJAVD7UY STP_tmp.lua") st(colors.white) cp(tmpx, tmpy)
  47.     if fs.exists("STP_tmp.lua") then
  48.         local t = fs.open("STP_tmp.lua", "r")
  49.         local u = t.readAll() t.close()
  50.         local t = fs.open(shell.getRunningProgram(), "r")
  51.         local current = t.readAll() t.close()
  52.         if u ~= current then
  53.             load()
  54.             fs.delete(shell.getRunningProgram())
  55.             fs.copy("STP_tmp.lua", shell.getRunningProgram())
  56.             if fs.exists("/disk/startup.lua") then
  57.                 clr() cp(1,1)
  58.                 print("Update disk? ('disk/startup.lua')")
  59.                 print("y - yes")
  60.                 print("n - no")
  61.                 while true do
  62.                     _,a = os.pullEvent("key")
  63.                     if a == keys.y then
  64.                         if fs.exists("/disk/startup.lua") then fs.delete("/disk/startup.lua") end
  65.                         fs.copy("STP_tmp.lua", "/disk/startup.lua") break
  66.                     elseif a == keys.n then break end
  67.                 end
  68.             end
  69.             c("Update Complete. Rebooting...") sleep(1) os.reboot()
  70.         else
  71.             --c("You are Up-To-Date.") sleep(.2)
  72.             load() sleep(.2)
  73.         end
  74.     else
  75.         --c("Failed to obtain update. Skipping Update...") sleep(.5)
  76.         load() sleep(.5)
  77.     end
  78. end
  79. --c("Cleaning up files...")
  80. load()
  81. if fs.exists("STP_tmp.lua") then fs.delete("STP_tmp.lua") end -- cleanup update files
  82.  
  83. if not turtle then
  84.     setLabel(prevLabel)
  85.     clr() cp(1,1)
  86.     c("Unit is not a turtle.") return
  87. end
  88.  
  89. local getLevel = turtle.getFuelLevel
  90. local blocks_total = 0
  91.  
  92. if not fs.exists("/startup.lua") and turtle then
  93.     clr() cp(1,1)
  94.     print("Install Simple Turtle Program (STP)?")
  95.     print("Type 'install' to install.")
  96.     write(": ")
  97.     if string.lower(read()) == "install" then
  98.         print("Installing...")
  99.         if fs.exists("/startup.lua") then fs.delete("/startup.lua") end
  100.         fs.copy(shell.getRunningProgram(), "/startup.lua")
  101.         print("Installed to 'startup.lua'. Rebooting...")
  102.         sleep(1) os.reboot()
  103.     end
  104. end
  105.  
  106. if shell.getRunningProgram() == "disk/startup.lua" then
  107.     clr() cp(1,1)
  108.     setLabel("§eBooting STL...")
  109.     c("Turtle plugged in. Unplug to use.") return
  110. end
  111. --c("Loading Functions...")
  112. load()
  113.  
  114. local function doRefuel()
  115.     turtle.select(1) -- ensure first slot is selected
  116.     if getLevel() == 0 then -- checks the first time
  117.         turtle.refuel(1) -- attempts to refuel
  118.         if getLevel() == 0 then -- if fails
  119.             local tmp = os.getComputerLabel()
  120.             setLabel("§cREFUEL")
  121.             c("Need Fuel to proceed...") -- needs fuel
  122.             while getLevel() == 0 do turtle.refuel(1) sleep(.5) end -- searches for fuel
  123.             setLabel("§f"..tmp)
  124.         end
  125.     end
  126. end
  127.  
  128. local function setInfo(stat, col)
  129.     status, color = stat or status, col or color
  130.     setLabel("§f"..math.min(getLevel(), 9999).." | §"..color..status)
  131. end
  132.  
  133. local function drawInfo()
  134.     setInfo()
  135.     local res,block_info = turtle.inspect()
  136.     clr() cp(1,1)
  137.     print("Fuel: "..math.min(getLevel(), 9999))
  138.     for i=1, w do write("=") end
  139.     if res then print("Block Info: "..block_info.name) end
  140.     print("Blocks Total: "..blocks_total)
  141. end
  142.  
  143. local function goUp() while not turtle.up() do blocks_total = blocks_total + 1 turtle.digUp() sleep(.25) end end
  144. local function goDown() while not turtle.down() do blocks_total = blocks_total + 1 turtle.digDown() sleep(.25) end end
  145. local function goForward() while not turtle.forward() do blocks_total = blocks_total + 1 turtle.dig() sleep(.25) end end
  146. local function goBack() while not turtle.back() do blocks_total = blocks_total + 1 turtle.turnRight() turtle.turnRight() turtle.dig() turtle.turnRight() turtle.turnRight() sleep(.25) end end
  147.  
  148. local function digForward() while turtle.dig() do blocks_total = blocks_total + 1 turtle.dig() sleep(.25) end end
  149. local function digUp() while turtle.digUp() do blocks_total = blocks_total + 1 turtle.digUp() sleep(.25) end end
  150.  
  151. local function doTunnel(distance)
  152.     drawInfo()
  153.     c("Tunneling for "..distance.." blocks...")
  154.     setInfo(distance-1, "a")
  155.     for i=1, distance do
  156.         turtle.turnLeft()
  157.         drawInfo() digForward()
  158.         doRefuel() goUp()
  159.         drawInfo() digForward()
  160.         turtle.turnRight()
  161.         turtle.turnRight()
  162.         drawInfo() digForward()
  163.         drawInfo() doRefuel() goDown()
  164.         drawInfo() digForward()
  165.         turtle.turnLeft()
  166.         if i ~= distance then while turtle.detect() do drawInfo() turtle.dig() sleep(.5) end doRefuel() turtle.forward() setInfo(distance-i-1, "a") end
  167.     end
  168.     setInfo("Idle", "e")
  169. end
  170.  
  171. local function doAdvTunnel(distance)
  172.     doTunnel(distance) sleep(.5)
  173.     turtle.turnLeft()
  174.     turtle.turnLeft()
  175.     doRefuel() goUp()
  176.     doRefuel() goUp()
  177.     doTunnel(distance) sleep(.5)
  178.     turtle.turnLeft()
  179.     turtle.turnLeft()
  180.     doRefuel() goDown()
  181.     doRefuel() goDown()
  182. end
  183.  
  184. local function doStripTunnel(distance)
  185.     for i=1, distance do
  186.         doRefuel() drawInfo() setInfo(distance-i, "a") goForward() digUp()
  187.     end
  188.     for i=1, distance do doRefuel() drawInfo() setInfo(distance-i, "a") goBack() end setInfo("Idle", "e")
  189. end
  190.  
  191. local function requestDistance(efu)
  192.     clr() cp(1,1)
  193.     write("Distance: ") distance = tonumber(read())
  194.     if distance == nil then c("Invalid input.") sleep(1) return false end
  195.     print("Estimated Fuel Usage: "..math.ceil((distance*efu)/80))
  196.     print("Current Fuel Level: "..getLevel())
  197.     c("Press enter to confirm...")
  198.     _,b = os.pullEvent("key")
  199.     if b ~= keys.enter then return false end
  200.     return distance
  201. end
  202.  
  203. local run, sel = true, 1
  204. local menu = {{"3x4 Tunnel (Big)", 4, doAdvTunnel}, {"3x2 Tunnel (Small)", 3, doTunnel}, {"1x2 Strip Mine (Tiny)", 2, doStripTunnel}, {"Exit", 0}}
  205.  
  206. --c("Executing STP...") sleep(.5)
  207. load() sleep(.5)
  208. doRefuel() setInfo("Idle", "e")
  209.  
  210. while run do
  211.     blocks_total = 0
  212.     clr() cp(1,1)
  213.     for i=1, #menu do
  214.         if i == sel then
  215.             print("> "..menu[i][1].." <")
  216.         else
  217.             print("  "..menu[i][1].."  ")
  218.         end
  219.     end
  220.     _,b = os.pullEvent("key")
  221.     if b == keys.w or b == keys.up then
  222.         if sel == 1 then sel = #menu else sel = sel - 1 end
  223.     elseif b == keys.s or b == keys.down then
  224.         if sel == #menu then sel = 1 else sel = sel + 1 end
  225.     elseif b == keys.enter or b == keys.e then
  226.         sleep(0.2)
  227.         if sel == #menu then break end
  228.         local dis = requestDistance(menu[sel][2])
  229.         if dis then
  230.             menu[sel][3](dis)
  231.             for i=1, 8 do turtle.turnLeft() sleep(.25) end
  232.         end
  233.     elseif b == keys.q then
  234.         run = false
  235.     end
  236. end
  237.  
  238. clr() cp(1,1) print("STP Closed.") sleep(0.2)
Add Comment
Please, Sign In to add comment