t8rt0t

RCturtle.lua

May 21st, 2024 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. --Remote Control Turtle
  2. local args = {...}
  3.  
  4. local inputTable = {
  5.     [keys.w] = "forward",
  6.     [keys.a] = "turnLeft",
  7.     [keys.d] = "turnRight",
  8.     [keys.s] = "back",
  9.     [keys.q] = "exit",
  10.     [keys.z] = "digDown",
  11.     [keys.x] = "dig",
  12.     [keys.c] = "digUp",
  13.     [keys.space] = "up",
  14.     [keys.leftShift] = "down",
  15.     [keys.rightShift] = "down",
  16.     [keys.r] = "refuel",
  17.     [keys.y] = "dropDown",
  18.     [keys.u] = "drop",
  19.     [keys.o] = "dropUp",
  20.     [keys.f] = "placeDown",
  21.     [keys.g] = "place",
  22.     [keys.h] = "placeUp",
  23.     [keys.j] = "suckDown",
  24.     [keys.k] = "suck",
  25.     [keys.l] = "suckUp",
  26.     [keys.left] = "selectLeft",
  27.     [keys.right] = "selectRight",
  28.     [keys.t] = "redstoneOn",
  29.     [keys.y] = "redstoneOff"
  30. }
  31.  
  32. local function createTurtleFunctions()
  33.     local i = 1
  34.     return function() i = (i-1)%16 turtle.select(i) end, function() i = (i+1)%16 turtle.select(i) end, function() shell.exit() end,function() for k,v in ipairs(redstone.getSides()) do redstone.setOutput(v,true) end end,function() for k,v in ipairs(redstone.getSides()) do redstone.setOutput(v,false) end end
  35. end
  36. if turtle then
  37.     turtle.selectLeft, turtle.selectRight,turtle.exit,turtle.redstoneOn,turtle.redstoneOff = createTurtleFunctions()
  38. end
  39.  
  40. local function getActionFromFile()
  41.     if not fs.exists("action.lua") then return false end
  42.     local file = fs.open("action.lua","r")
  43.     local action = file.readAll()
  44.     file.close()
  45.     fs.delete("action.lua")
  46.     return action
  47.     -- local s,action = pcall(function() require("action") end)
  48.     -- print(s,action)
  49.     -- if not s then return false end
  50.     -- fs.delete("action.lua")
  51.     -- return action
  52. end
  53.  
  54. local function writeActionToFile(key)
  55.     if not key or not inputTable[key] then return false, "invalid input" end
  56.     if fs.exists("action.lua") then return false, "action already exists" end
  57.     local file = fs.open("action.lua","w+")
  58.     file.write(inputTable[key])
  59.     -- file.write('return "' .. inputTable[key] .. '"')
  60.     file.close()
  61.     return true,inputTable[key]
  62. end
  63.  
  64. local function formatData()
  65.     local str = "fuel:"
  66.     str = "f:" .. turtle.getFuelLevel()
  67.     str = str .. "s:" .. turtle.getSelectedSlot()
  68.     if turtle.getItemCount() > 0 then
  69.         str = str .. "i:" .. textutils.serialise(turtle.getItemDetail(),{compact=true})
  70.     end
  71.     os.setComputerLabel(str)
  72. end
  73.  
  74. local function turtleLoop()
  75.     turtle.select(1)
  76.     while true do
  77.         formatData()
  78.         local action = getActionFromFile()
  79.         if action then
  80.             print(action)
  81.             pcall(function() turtle[action]() end)
  82.         else
  83.             sleep(0.1)
  84.         end
  85.     end
  86. end
  87.  
  88. local function pocketLoop()
  89.     term.clear()
  90.     term.setCursorPos(1,1)
  91.     for k,action in pairs(inputTable) do
  92.         print(keys.getName(k),action)
  93.     end
  94.     while true do
  95.         local e,k = os.pullEvent("key")
  96.         writeActionToFile(k)
  97.         if e == keys.q then shell.exit() end
  98.     end
  99. end
  100.  
  101. local function rednetHostLoop()
  102.     peripheral.find("modem",rednet.open)
  103.     if not rednet.isOpen() then
  104.         print("Please place a modem")
  105.         shell.exit()
  106.     end
  107.     rednet.host("stalker",tostring(os.getComputerID()))
  108.     while true do
  109.         local e,msg,prtcl = rednet.receive()
  110.         if prtcl == "key" then
  111.             print(writeActionToFile(msg))
  112.             if msg == "keys.q" then shell.exit() end
  113.         end
  114.     end
  115. end
  116.  
  117. local function pocketRednetLoop()
  118.     peripheral.find("modem",rednet.open)
  119.     if not rednet.isOpen() then
  120.         print("Please place a modem")
  121.         shell.exit()
  122.     end
  123.     local host = rednet.lookup("stalker")
  124.     term.clear()
  125.     term.setCursorPos(1,1)
  126.     local str = ""
  127.     term.setTextColor(colors.green)
  128.     for k,action in pairs(inputTable) do
  129.         str = str .. keys.getName(k) .. ":" .. action .. " "
  130.         --print(keys.getName(k),action)
  131.     end
  132.     print(str)
  133.     while true do
  134.         if host then
  135.             local e,k = os.pullEvent("key")
  136.             rednet.send(host,k,"key")
  137.             if e == keys.q then shell.exit() end
  138.         else
  139.             print("Enter Host>")
  140.             host = tonumber(read())
  141.             sleep(1)
  142.             term.clear()
  143.             term.setCursorPos(1,1)
  144.             print(str)
  145.             --host = rednet.lookup("stalker")
  146.         end
  147.     end
  148. end
  149.    
  150. if turtle then turtleLoop()
  151. --elseif args[1] ~= "rednet" and pocket then pocketLoop()
  152. elseif pocket then pocketRednetLoop()
  153. else rednetHostLoop() end
Advertisement
Add Comment
Please, Sign In to add comment