JustDoesGames

STP v1

Oct 28th, 2020 (edited)
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.99 KB | None | 0 0
  1. update = false
  2. console = false
  3. --[[
  4.  
  5. Simplistic t Program (STP)
  6.  
  7. --]]
  8.  
  9. local patchnotes = {
  10.     "Removed logo for simplisity",
  11.     "Console on boot off by default",
  12.     "Fully reworked update system. (now goes through github & pastebin)",
  13.     "Updating is now 2x faster and now uses the 'http' api instead of shell",
  14.     "Label will no longer show 'Booting STP...'",
  15.     "Turtle is not longer required to be fueled to start in menu",
  16.     "Added patch notes",
  17.     "I am fully aware of current issues and in the process of fixing them.",
  18. }
  19.  
  20. local w,h = term.getSize()
  21. clr, cp, sb, st = term.clear, term.setCursorPos, term.setBackgroundColor, term.setTextColor
  22.  
  23. local function drawLine() st(colors.gray) for i=1, w do write(string.char(127)) end st(colors.white)  end
  24. local function drawLines() clr() cp(1,1) drawLine() cp(1,h) drawLine() cp(1,2) end
  25.  
  26. drawLines()
  27. cp(w/2-string.len("STP")/2,2) print("STP")
  28.  
  29. if fs.exists("/disk") and not fs.exists("/disk/startup.lua") and shell.getRunningProgram() ~= "/disk/startup.lua" then
  30.     print("Type 'install' to install disk installer to '/disk/startup.lua'")
  31.     write(": ")
  32.     if string.lower(read()) == "install" then
  33.         if fs.exists("/disk") then
  34.             if fs.exists("/disk/startup.lua") then fs.delete("/disk/startup.lua") end
  35.             fs.copy(shell.getRunningProgram(), "/disk/startup.lua")
  36.             print("Installed to 'disk/startup.lua'. Rebooting...")
  37.             sleep(1) os.reboot()
  38.         end
  39.     end
  40. end
  41.  
  42. local c = function(t) if console then print("[STP] "..t) sleep(.02) end end
  43. local setLabel = function(name) return os.setComputerLabel(tostring(name)) end -- did it this way to convert to string because fuck lua
  44. local prevLabel = os.getComputerLabel()
  45.  
  46. local status, color = "Idle", "e"
  47.  
  48. if not http then
  49.     c("Http is disabled. Skipping Update...") sleep(1)
  50. elseif not update then
  51.     c("Updates disabled. Skipping...") sleep(1)
  52. else
  53.     write("[STP] Checking for updates")
  54.     local h = http.get("https://raw.githubusercontent.com/JustDoesGames/STP/main/startup.lua")
  55.     local update = false
  56.     if h then update = h.readAll() h.close() end textutils.slowPrint("...")
  57.     --local f = fs.open("out.lua", "w") f.write(update) f.close() -- For debug reasons.
  58.     if update then
  59.         local t = fs.open(shell.getRunningProgram(), "r") local current = t.readAll() t.close()
  60.         if update ~= current then
  61.             c("Update Found!")
  62.             c("Update Process (0/2)")
  63.             local f = fs.open(shell.getRunningProgram(), "w") f.write(update) f.close()
  64.             c("Update Process (1/2)")
  65.             if fs.exists("/disk/startup.lua") then
  66.                 print("Update disk? ('disk/startup.lua')")
  67.                 print("y - YES")
  68.                 print("n - NO")
  69.                 while true do
  70.                     _,a = os.pullEvent("key")
  71.                     if a == keys.y then
  72.                         if fs.exists("/disk/startup.lua") then fs.delete("/disk/startup.lua") end
  73.                         fs.copy(shell.getRunningProgram(), "/disk/startup.lua")
  74.                         c("Disk updated.") break
  75.                     elseif a == keys.n then break end
  76.                 end
  77.             end
  78.             c("Update Process (2/2)")
  79.             c("Update Complete. Rebooting...") sleep(1) os.reboot()
  80.         else
  81.             c("You are Up-To-Date.")
  82.         end
  83.     else
  84.         c("Failed to obtain update. Skipping Update...")
  85.     end
  86. end
  87. c("Cleaning up files...")
  88. if fs.exists("STP_tmp.lua") then fs.delete("STP_tmp.lua") end -- cleanup update files
  89.  
  90. if not turtle then
  91.     setLabel(prevLabel)
  92.     clr() cp(1,1)
  93.     c("Unit is not a t.") return
  94. end
  95. local t = turtle
  96.  
  97. local getLevel = t.getFuelLevel
  98. local blocks_total, gDistance = 0, 0
  99.  
  100. if not fs.exists("/startup.lua") and t then
  101.     print("Install Simple t Program (STP)?")
  102.     print("Type 'install' to install.")
  103.     write(": ")
  104.     if string.lower(read()) == "install" then
  105.         print("Installing...")
  106.         if fs.exists("/startup.lua") then fs.delete("/startup.lua") end
  107.         fs.copy(shell.getRunningProgram(), "/startup.lua")
  108.         print("Installed to 'startup.lua'. Rebooting...")
  109.         sleep(1) os.reboot()
  110.     end
  111. end
  112.  
  113. if shell.getRunningProgram() == "disk/startup.lua" then
  114.     clr() cp(1,1)
  115.     c("t plugged in. Unplug to use.") return
  116. end
  117. c("Loading Functions...")
  118.  
  119. local flashcolors = {"c", "8"}
  120. local function doRefuel()
  121.     t.select(1) -- ensure first slot is selected
  122.     if getLevel() == 0 then -- checks the first time
  123.         t.refuel(1) -- attempts to refuel
  124.         if getLevel() == 0 then -- if fails
  125.             local flashtrack = 1
  126.             c("Need Fuel to proceed...") -- needs fuel
  127.             while getLevel() == 0 do setLabel("§"..flashcolors[flashtrack].."REFUEL") flashtrack=flashtrack+1 if flashtrack > #flashcolors then flashtrack=1 end t.refuel(1) sleep(.5) end -- searches for fuel
  128.         end
  129.     end
  130. end
  131.  
  132. local function setInfo(stat, col)
  133.     status, color = stat or status, col or color
  134.     setLabel("§f"..math.min(getLevel(), 9999).." | §"..color..status)
  135. end
  136.  
  137. local function drawInfo()
  138.     drawLines()
  139.     print("Fuel: "..math.min(getLevel(), 9999).."  ")
  140.     print("Distance: "..gDistance.."  ")
  141.     print("Blocks Broken: "..blocks_total.."  ") setInfo()
  142. end
  143.  
  144. local function digForward() while t.dig() do t.dig() blocks_total = blocks_total + 1 sleep(.25) end end
  145. local function digUp() while t.digUp() do t.digUp() blocks_total = blocks_total + 1 sleep(.25) end end
  146. local function digDown() while t.digDown() do t.digDown() blocks_total = blocks_total + 1 sleep(.25) end end
  147.  
  148. local function goUp() doRefuel() while not t.up() do t.digUp() sleep(.25) end end
  149. local function goDown() doRefuel() while not t.down() do t.digDown() sleep(.25) end end
  150. local function goForward() doRefuel() while not t.forward() do t.dig() sleep(.25) end gDistance = gDistance + 1 end
  151.  
  152. turnRight, turnLeft = t.turnRight, t.turnLeft
  153.  
  154. local function requestDistance(estMoves)
  155.    
  156.     local distance = nil
  157.     local function doDisCalc(scale)
  158.         return math.ceil((distance*estMoves)/scale)
  159.     end
  160.     clr() cp(1,1) drawLine() cp(1,h) drawLine() cp(1,2)
  161.     write("Distance: ") distance = tonumber(read())
  162.     if distance == nil or distance < 1 then c("Invalid input.") sleep(1) return false end
  163.  
  164.     drawLine()
  165.     print("Current Fuel Level: "..getLevel())
  166.     print("Estimated Fuel Usage: "..distance*estMoves)
  167.     drawLine()
  168.     print("(80) Coal/Charcoal - "..doDisCalc(80))
  169.     print("(120) Blaze Rods - "..doDisCalc(120))
  170.     print("(800) Coal/Charcoal Blocks - "..doDisCalc(800))
  171.     drawLine()
  172.     c("Press 'enter' to confirm...")
  173.     _,b = os.pullEvent("key")
  174.     if b ~= keys.enter then return false end
  175.     return distance
  176. end
  177.  
  178. local function doPatchNotes()
  179.     clr() cp(1,1) print("Press 'enter' to continue") print("Patch Notes: ")
  180.     for i=1, #patchnotes do
  181.         sleep(.1) os.pullEvent("key") print(patchnotes[i])
  182.     end
  183.     drawLine()
  184.     print("press any key to exit.") sleep(.5) os.pullEvent("key")
  185. end
  186.  
  187. local function basicTunnel(distance)
  188.     if not distance then return false end
  189.     drawInfo()
  190.     c("Tunneling for "..distance.." blocks...")
  191.     setInfo(distance-1, "a")
  192.     for i=1, distance do
  193.         t.turnLeft()
  194.         drawInfo() digForward()
  195.         goUp()
  196.         drawInfo() digForward()
  197.         t.turnRight()
  198.         t.turnRight()
  199.         drawInfo() digForward()
  200.         drawInfo() goDown()
  201.         drawInfo() digForward()
  202.         t.turnLeft()
  203.         if i ~= distance then while t.detect() do drawInfo() t.dig() sleep(.5) end goForward() setInfo(distance-i-1, "a") end
  204.     end
  205.     setInfo("Idle", "e")
  206. end
  207.  
  208. local function doTunnel()
  209.     local dis = requestDistance(3)
  210.     if dis then basicTunnel(dis) turnRight() turnRight() for i=1, dis-1 do goForward() end turnRight() turnRight() end
  211. end
  212.  
  213. local function doAdvTunnel()
  214.     local dis = requestDistance(6)
  215.     if dis then
  216.         basicTunnel(dis) sleep(.5)
  217.         t.turnLeft() t.turnLeft()
  218.         goUp() goUp()
  219.         basicTunnel(dis) sleep(.5)
  220.         t.turnLeft() t.turnLeft()
  221.         goDown() goDown()
  222.     end
  223. end
  224.  
  225. local function doStripTunnel()
  226.     local dis = requestDistance(2)
  227.     if dis then
  228.         for i=1, dis do
  229.             drawInfo() goForward() setInfo(dis-i, "a") digUp() if math.floor(i/5) == math.ceil(i/5) then t.turnRight() digForward() t.turnLeft() t.turnLeft() digForward() t.turnRight() end
  230.         end
  231.         goUp() t.turnRight() digForward() t.turnLeft() t.turnLeft() digForward() goDown() t.turnLeft()
  232.         for i=1, dis do drawInfo() setInfo(dis-i, "a") goForward() end t.turnLeft() t.turnLeft() setInfo("Idle", "e")
  233.     end
  234. end
  235.  
  236. local function doExtendedStripTunnel()
  237.     local dis = requestDistance(2)
  238.     if dis then
  239.         for i=1, dis do
  240.             drawInfo() goForward() setInfo(dis-i, "a") digUp() digDown() if math.floor(i/5) == math.ceil(i/5) then t.turnRight() digForward() t.turnLeft() t.turnLeft() digForward() t.turnRight() end
  241.         end
  242.         t.turnRight() digForward() t.turnLeft() t.turnLeft() digForward()  t.turnRight() goDown()
  243.         t.turnRight() digForward() t.turnLeft() t.turnLeft() digForward() goUp() t.turnLeft()
  244.         for i=1, dis do drawInfo() setInfo(dis-i, "a") goForward() end t.turnLeft() t.turnLeft()
  245.     end
  246. end
  247.  
  248. local function doDoubleStripTunnel()
  249.     local dis = requestDistance(0)
  250.     if dis then
  251.         for i=1, dis do
  252.              drawInfo() goForward() setInfo(dis-i, "a") digUp() digDown() if math.floor(i/5) == math.ceil(i/5) then t.turnRight() digForward() t.turnLeft() t.turnLeft() digForward() t.turnRight() end
  253.         end
  254.         t.turnLeft()  goForward() digUp()  goForward() t.turnRight()
  255.         for i=1, dis do
  256.              drawInfo() goForward() setInfo(dis-i, "a") digUp() digDown() if math.floor(i/5) == math.ceil(i/5) then t.turnRight() digForward() t.turnLeft() t.turnLeft() digForward() t.turnRight() end
  257.         end
  258.         t.turnRight() t.turnRight()
  259.     end
  260. end
  261.  
  262. local function doBroken()
  263.     print("BROKEN! PLEASE CONSULT OWNER.")
  264.     sleep(3)
  265. end
  266.  
  267. local function do3x1()
  268.     local dis = requestDistance(2)
  269.     if dis then
  270.         for i=1, dis do
  271.             digForward()  drawInfo() goForward() setInfo(dis-i, "a") t.turnLeft() digForward() t.turnRight() t.turnRight() digForward() t.turnLeft()
  272.         end t.turnRight() t.turnRight()
  273.         for i=1, dis do goForward() end
  274.     end
  275. end
  276.  
  277. local function doManualControl()
  278.     drawLines()
  279.     print("w/s - Go forward / back")
  280.     print("up/down - Go up / down")
  281.     print("a/d - Turn left / right")
  282.     print("e - Dig")
  283.     print("r/f - Dig up / down")
  284.     print("q - exit")
  285.     while true do
  286.         cp(1,h-1) write("Fuel Level: "..getLevel())
  287.         a,b = os.pullEvent("key")
  288.         if b == keys.w then
  289.             goForward()
  290.         elseif b == keys.s then
  291.             t.back()
  292.         elseif b == keys.d then
  293.             turnRight()
  294.         elseif b == keys.a then
  295.             turnLeft()
  296.         elseif b == keys.up then
  297.             goUp()
  298.         elseif b == keys.down then
  299.             goDown()
  300.         elseif b == keys.e then
  301.             digForward()
  302.         elseif b == keys.r then
  303.             digUp()
  304.         elseif b == keys.f then
  305.             digDown()
  306.         elseif b == keys.q then
  307.             break
  308.         end
  309.     end
  310. end
  311.  
  312. local function doInfMine()
  313.     drawLines()
  314.     print("Infinite Mine")
  315.     print("Press 'q' to exit.")
  316.     local function mine()
  317.         while true do
  318.             digForward() sleep(.01)
  319.         end
  320.     end
  321.     local function getKey()
  322.         local b while b ~= keys.q do
  323.             _,b = os.pullEvent("key")
  324.         end
  325.     end
  326.     parallel.waitForAny(mine, getKey)
  327. end
  328.  
  329. local function doInfMineDown()
  330.     drawLines()
  331.     print("Infinite Mine - Dropping Down")
  332.     print("Press 'q' to exit.")
  333.     local function mine()
  334.         while true do
  335.             digForward() t.dropDown() sleep(.01)
  336.         end
  337.     end
  338.     local function getKey()
  339.         local b while b ~= keys.q do
  340.             _,b = os.pullEvent("key")
  341.         end
  342.     end
  343.     parallel.waitForAny(mine, getKey)
  344. end
  345.  
  346. local function doInfRotateMine()
  347.     drawLines()
  348.     print("Infinite Rotate Mine")
  349.     print("Press 'q' to exit.")
  350.     local function mine()
  351.         while true do
  352.             digForward() sleep(.01) turnRight()
  353.         end
  354.     end
  355.     local function getKey()
  356.         local b while b ~= keys.q do
  357.             _,b = os.pullEvent("key")
  358.         end
  359.     end
  360.     parallel.waitForAny(mine, getKey)
  361. end
  362.  
  363. local function doInfRotateMineDown()
  364.     drawLines()
  365.     print("Infinite Rotate Mine - Dropping Down")
  366.     print("Press 'q' to exit.")
  367.     local function mine()
  368.         while true do
  369.             digForward() t.dropDown() sleep(.01) turnRight()
  370.         end
  371.     end
  372.     local function getKey()
  373.         local b while b ~= keys.q do
  374.             _,b = os.pullEvent("key")
  375.         end
  376.     end
  377.     parallel.waitForAny(mine, getKey)
  378. end
  379.  
  380. local function doInfAttack()
  381.     drawLines()
  382.     print("Infinite Attack")
  383.     print("Press 'q' to exit.")
  384.     local function mine()
  385.         while true do
  386.             t.attack() sleep(.01)
  387.         end
  388.     end
  389.     local function getKey()
  390.         local b while b ~= keys.q do
  391.             _,b = os.pullEvent("key")
  392.         end
  393.     end
  394.     parallel.waitForAny(mine, getKey)
  395. end
  396.  
  397. local function doInfAttackDown()
  398.     drawLines()
  399.     print("Infinite Attack - Dropping Down")
  400.     print("Press 'q' to exit.")
  401.     local function mine()
  402.         while true do
  403.             t.attack() t.dropDown() sleep(.01)
  404.         end
  405.     end
  406.     local function getKey()
  407.         local b while b ~= keys.q do
  408.             _,b = os.pullEvent("key")
  409.         end
  410.     end
  411.     parallel.waitForAny(mine, getKey)
  412. end
  413.  
  414. local function doInfRotateAttack()
  415.     drawLines()
  416.     print("Infinite Rotate Attack")
  417.     print("Press 'q' to exit.")
  418.     local function mine()
  419.         while true do
  420.             t.attack() sleep(.01) turnRight()
  421.         end
  422.     end
  423.     local function getKey()
  424.         local b while b ~= keys.q do
  425.             _,b = os.pullEvent("key")
  426.         end
  427.     end
  428.     parallel.waitForAny(mine, getKey)
  429. end
  430.  
  431. local function doInfRotateAttackDown()
  432.     drawLines()
  433.     print("Infinite Rotate Attack - Dropping Down")
  434.     print("Press 'q' to exit.")
  435.     local function mine()
  436.         while true do
  437.             t.attack() t.dropDown() sleep(.01) turnRight()
  438.         end
  439.     end
  440.     local function getKey()
  441.         local b while b ~= keys.q do
  442.             _,b = os.pullEvent("key")
  443.         end
  444.     end
  445.     parallel.waitForAny(mine, getKey)
  446. end
  447.  
  448. local function doSingleStaircaseUp()
  449.     local dis = requestDistance(4)
  450.     if dis then
  451.         for i=1, dis do
  452.             goForward()
  453.             goUp() digUp()
  454.         end turnRight() turnRight()
  455.         for i=1, dis do
  456.             goForward() goDown()
  457.         end turnRight() turnRight()
  458.     end
  459. end
  460.  
  461. local function doTripleStaircaseUp()
  462.     local dis = requestDistance(8)
  463.     if dis then
  464.         for i=1, dis do
  465.             goForward()
  466.             turnRight() digForward() turnLeft() turnLeft() digForward() turnRight()
  467.             for i=1, 2 do
  468.                 goUp()
  469.                 turnRight() digForward() turnLeft() turnLeft() digForward() turnRight()
  470.             end goDown()
  471.         end turnRight() turnRight()
  472.         for i=1, dis do
  473.             goForward() goDown()
  474.         end turnRight() turnRight()
  475.     end
  476. end
  477.  
  478. local function doRSC(cmd)
  479.     st(colors.gray)
  480.     clr() cp(1,1) print("Running Command: "..cmd) st(colors.white)
  481.     shell.run(cmd)
  482.     print("Press any key to continue...") os.pullEvent("key")
  483. end
  484.  
  485. local run, sel = true, 1
  486. local prevMenu = {}
  487.  
  488. local rawMenu = {
  489.     {"Tunnel", {
  490.         {"3x2 Tunnel - Single", doTunnel},
  491.         {"3x4 Tunnel - Double", doAdvTunnel},
  492.         {"3x1 Tunnel - Flat", do3x1},
  493.     }},
  494.     {"Strip Mine", {
  495.         {"1x2 Strip Mine", doStripTunnel},
  496.         {"1x3 Strip Mine", doExtendedStripTunnel},
  497.         --{"Double Strip Mine", doBroken},
  498.     }},
  499.     {"Ininite", {
  500.         {"Inf. Mine", doInfMine},
  501.         {"Inf. Mine - Drop Down", doInfMineDown},
  502.         {"Inf. Rotate Mine", doInfRotateMine},
  503.         {"Inf. Rotate Mine - Drop Down", doInfRotateMineDown},
  504.         {"Inf. Attack", doInfAttack},
  505.         {"Inf. Attack - Drop Down", doInfAttackDown},
  506.         {"Inf. Rotate Attack", doInfRotateAttack},
  507.         {"Inf. Rotate Attack - Drop Down", doInfRotateAttackDown},
  508.     }},
  509.     {"Staircase", {
  510.         {"Single Staircase - Up", doSingleStaircaseUp},
  511.         {"Triple Staircase - Up", doTripleStaircaseUp},
  512.     }},
  513.     {"Turtle", {
  514.         {"Unequip", {
  515.             {"Left", function() doRSC("unequip left") end},
  516.             {"Right", function() doRSC("unequip right") end},
  517.         }},
  518.         {"Equip", {
  519.             {"Left", function() doRSC("equip 1 left") end},
  520.             {"Right", function() doRSC("equip 1 right") end},
  521.         }},
  522.     }},
  523.     {"Misc.", {
  524.         {"Manual Control", doManualControl},
  525.     }},
  526.     {"Patch Notes", doPatchNotes},
  527.     {"Exit"}
  528. }
  529.  
  530. local function runMenu(menu)
  531.     local function resetScreen() clr() cp(1,1) drawLine() cp(1,h) drawLine() cp(w/2-1,2) write("STP") sb(colors.black) end resetScreen()
  532.     while true do
  533.        
  534.         blocks_total = 0
  535.         resetScreen() cp(1,9)
  536.         for i=1, math.min(h-5, #menu) do
  537.             if menu[i+(sel-1)] then
  538.                 if i+(sel-1) == sel then
  539.                     paintutils.drawLine(1,i+3,w,i+3,colors.gray)
  540.                     st(colors.lightGray) cp(1,4) write(sel) st(colors.white)
  541.                     cp(w/2-string.len(menu[i+(sel-1)][1])/2, i+3) write(menu[i+(sel-1)][1]) sb(colors.black)
  542.                 else
  543.                     st(colors.gray) cp(w/2-string.len(menu[i+(sel-1)][1])/2, i+3)
  544.                     write(menu[i+(sel-1)][1])
  545.                 end
  546.             end
  547.         end st(colors.white)
  548.  
  549.         _,b = os.pullEvent("key")
  550.         if b == keys.w or b == keys.up then
  551.             if sel == 1 then sel = #menu else sel = sel - 1 end
  552.         elseif b == keys.s or b == keys.down then
  553.             if sel == #menu then sel = 1 else sel = sel + 1 end
  554.         elseif b == keys.enter or b == keys.e then
  555.             break
  556.         end
  557.     end
  558.     st(colors.white)
  559.     for i=1, 2 do
  560.         resetScreen()
  561.         paintutils.drawLine(1,4,w,4,colors.gray)
  562.         cp(w/2-string.len(menu[sel][1])/2, 4)
  563.         write(menu[sel][1]) sb(colors.black) sleep(.05)
  564.         resetScreen() sleep(.05)
  565.     end
  566.     return sel
  567. end
  568.  
  569. local function execute()
  570.     local menu = rawMenu
  571.     while run do
  572.        
  573.         if menu[#menu][1] ~= "Back" and #prevMenu ~= 0 then menu[#menu+1] = {"Back"} end
  574.         runMenu(menu)
  575.        
  576.         if type(menu[sel][2]) == "table" then -- possible menu
  577.             prevMenu[#prevMenu+1] = {menu = menu, sel = sel}
  578.             menu = menu[sel][2] sel = 1
  579.         elseif not menu[sel][2] then -- possible prev/exit
  580.             if #prevMenu == 0 then run = false else menu, sel = prevMenu[#prevMenu].menu, prevMenu[#prevMenu].sel prevMenu[#prevMenu] = nil end
  581.         elseif type(menu[sel][2]) == "function" then -- possible program
  582.             blocks_total,gDistance=0,0 menu[sel][2](dis) setInfo("Idle", "e")
  583.         end
  584.     end
  585. end
  586.  
  587. c("Executing STP...")
  588. setInfo("Idle", "e")
  589.  
  590. execute()
  591. clr() cp(1,1) print("STP Closed.") sleep(0.2)
Add Comment
Please, Sign In to add comment