HappySunChild

Remote Receiver

Sep 12th, 2021 (edited)
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.12 KB | None | 0 0
  1. peripheral.find("modem", rednet.open)
  2.  
  3. local turtleNeighbor = peripheral.find("turtle")
  4.  
  5. term.clear()
  6. term.setCursorPos(1, 1)
  7.  
  8. if turtleNeighbor then
  9.     print("Turning on neighbor turtle")
  10.     turtleNeighbor.turnOn()
  11. end
  12.  
  13. print("Running Turtle Control Reciever\n")
  14.  
  15. os.setComputerLabel(tostring(os.getComputerID()))
  16.  
  17. local usedPastes = {
  18.     { "YYizkd25", "tunnel.lua" },
  19.     { "dHn1pbtb", "miner.lua" },
  20.     { "aPfiecwi", "bridge.lua" },
  21.     { "AmKU7gZV", "lavafuel.lua" },
  22.     { "Rvu21mX6", "drain.lua" },
  23.     { "wjrXLHvw", "startup.lua" }
  24. }
  25.  
  26. local function Split(inputstr, sep)
  27.     if sep == nil then
  28.         sep = "%s"
  29.     end
  30.  
  31.     local t = {}
  32.     for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
  33.         table.insert(t, str)
  34.     end
  35.     return t
  36. end
  37.  
  38. local function DownloadPastebins(update)
  39.     local downloaded = {}
  40.  
  41.     for i, paste in pairs(usedPastes) do
  42.         local pid, ppath = paste[1], paste[2]
  43.  
  44.         if update then
  45.             if fs.exists(ppath) then
  46.                 fs.delete(ppath)
  47.             end
  48.         end
  49.  
  50.         if not fs.exists(ppath) then
  51.             table.insert(downloaded, ppath)
  52.  
  53.             shell.run("pastebin", "get", pid, ppath)
  54.             term.clear()
  55.             term.setCursorPos(1, 1)
  56.  
  57.             for i, p in pairs(downloaded) do
  58.                 print("Downloading " .. p)
  59.             end
  60.         end
  61.     end
  62.  
  63.     if #downloaded > 0 then
  64.         print("Done downloading!")
  65.     end
  66.  
  67.     for i, pname in pairs(downloaded) do
  68.         if pname == "startup.lua" then
  69.             print("Restarting new startup.lua")
  70.             shell.run("startup.lua")
  71.             break
  72.         end
  73.     end
  74. end
  75.  
  76. local function RunProgram(path, args)
  77.     if not fs.exists(path) then
  78.         print("missing program!")
  79.         print("reinstalling program...")
  80.         DownloadPastebins()
  81.         print("done installing!")
  82.     end
  83.  
  84.     table.remove(args, 1)
  85.  
  86.     shell.run(path, table.unpack(args))
  87. end
  88.  
  89. DownloadPastebins(false)
  90.  
  91. while true do
  92.     local id, message, protocol = rednet.receive()
  93.  
  94.     local args = Split(message)
  95.  
  96.     if args[1] == "go" then
  97.         local moveFunc = nil
  98.  
  99.         if args[2] == "up" then
  100.             moveFunc = turtle.up
  101.         elseif args[2] == "forward" then
  102.             moveFunc = function()
  103.                 local attempts = 0
  104.  
  105.                 local hasBlock, data = turtle.inspect()
  106.  
  107.                 if hasBlock then
  108.                     if data.name == "computercraft:turtle_normal" then
  109.                         repeat
  110.                             local success = turtle.forward()
  111.                             attempts = attempts + 1
  112.                         until success or attempts > 50
  113.                     end
  114.                 else
  115.                     turtle.forward()
  116.                 end
  117.             end
  118.         elseif args[2] == "back" then
  119.             moveFunc = function()
  120.                 local attempts = 0
  121.  
  122.                 repeat
  123.                     local success = turtle.back()
  124.                     attempts = attempts + 1
  125.                 until success or attempts > 100
  126.             end
  127.         elseif args[2] == "left" then
  128.             moveFunc = turtle.turnLeft
  129.         elseif args[2] == "right" then
  130.             moveFunc = turtle.turnRight
  131.         elseif args[2] == "down" then
  132.             moveFunc = turtle.down
  133.         end
  134.  
  135.         for i = 1, tonumber(args[3]) or 1 do
  136.             pcall(moveFunc)
  137.         end
  138.     elseif args[1] == "dig" then
  139.         if args[2] == "up" then
  140.             turtle.digUp()
  141.         elseif args[2] == "down" then
  142.             turtle.digDown()
  143.         else
  144.             turtle.dig()
  145.         end
  146.     elseif args[1] == "place" then
  147.         if args[2] == "up" then
  148.             turtle.placeUp()
  149.         elseif args[2] == "down" then
  150.             turtle.placeDown()
  151.         else
  152.             turtle.place()
  153.         end
  154.     elseif args[1] == "drop" then
  155.         for i = 1, 16 do
  156.             if turtle.getItemCount(i) > 0 then
  157.                 turtle.select(i)
  158.                 local data = turtle.getItemDetail()
  159.                 if args[2] == nil or data.name == args[2] then
  160.                     turtle.drop(64)
  161.                 end
  162.             end
  163.         end
  164.  
  165.         turtle.select(1)
  166.     elseif args[1] == "miner" then
  167.         RunProgram("miner.lua", args)
  168.     elseif args[1] == "tunnel" then
  169.         RunProgram("tunnel.lua", args)
  170.     elseif args[1] == "bridge" then
  171.         RunProgram("bridge.lua", args)
  172.     elseif args[1] == "lavafuel" then
  173.         RunProgram("lavafuel.lua", args)
  174.     elseif args[1] == "drain" then
  175.         RunProgram("drain.lua", args)
  176.     elseif args[1] == "update" then
  177.         DownloadPastebins(true)
  178.     elseif args[1] == "refuel" then
  179.         shell.run("refuel", "all")
  180.     elseif args[1] == "reboot" then
  181.         shell.run("reboot")
  182.     elseif args[1] == "shutdown" then
  183.         peripheral.find("modem", rednet.close)
  184.         shell.run("shutdown")
  185.     elseif args[1] == "select" then
  186.         turtle.select(tonumber(args[2] or turtle.getSelectedSlot()))
  187.     elseif args[1] == "dance" then
  188.         shell.run("dance")
  189.     end
  190. end
  191.  
Add Comment
Please, Sign In to add comment