djgaven588

Remote Turtle Control

Mar 10th, 2024 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.28 KB | None | 0 0
  1. local controlId = 6
  2.  
  3. os.loadAPI("buttonLib.lua")
  4.  
  5. local inspectData
  6. local inventoryData
  7. local lastResponse = ""
  8. local buttons
  9. local buildSide = "forward"
  10. local markDraw = false
  11.  
  12. local function draw()
  13.     term.setBackgroundColor(colours.black)
  14.     term.clear()
  15.     term.setBackgroundColor(colours.green)
  16.     term.setTextColor(colours.black)
  17.     for i, object in ipairs(buttons) do
  18.         buttonLib.draw(object)
  19.     end
  20.    
  21.     term.setBackgroundColor(colors.white);
  22.     term.setTextColor(colors.black);
  23.  
  24.     if buildSide == "forward" then
  25.         term.setCursorPos(18, 13)
  26.         term.write("---")
  27.     elseif buildSide == "up" then
  28.         term.setCursorPos(18, 12)
  29.         term.write("^^^")
  30.     elseif buildSide == "down" then
  31.         term.setCursorPos(18, 14)
  32.         term.write("vvv")
  33.     end
  34.  
  35.     term.setBackgroundColor(colours.black)
  36.     term.setTextColor(colors.white)
  37.  
  38.     if inspectData then
  39.         if inspectData.top then
  40.             term.setCursorPos(26-string.len(inspectData.top.name),1)
  41.             term.write(inspectData.top.name)
  42.         end
  43.         if inspectData.forward then
  44.             term.setCursorPos(26-string.len(inspectData.forward.name),5)
  45.             term.write(inspectData.forward.name)
  46.         end
  47.         if inspectData.bottom then
  48.             term.setCursorPos(26-string.len(inspectData.bottom.name),9)
  49.             term.write(inspectData.bottom.name)
  50.         end
  51.     end
  52.     if inventoryData then
  53.         if inventoryData.slots[inventoryData.current] then
  54.             term.setCursorPos(3,18)
  55.             term.write(inventoryData.slots[inventoryData.current].name)
  56.         end
  57.         term.setCursorPos(1, 1)
  58.         term.write("Fuel: " .. inventoryData.fuel)
  59.         local count = inventoryData.slots.length
  60.         local yOffset
  61.         for i=1,16,1 do
  62.             local xOffset = ((i-1) % 4) * 3
  63.             if (i-1) % 4 == 0 then
  64.                 yOffset = ((i-1) / 4) * 2
  65.             end                
  66.             term.setCursorPos(3 + xOffset, 11 + yOffset)
  67.             if i == inventoryData.current then
  68.                 term.setBackgroundColor(colors.white)
  69.                 term.setTextColor(colors.black)
  70.             end
  71.             if inventoryData.slots[i] then
  72.                 term.write(inventoryData.slots[i].count)
  73.             else
  74.                 term.write(0)
  75.             end
  76.             if i == inventoryData.current then
  77.                 term.setBackgroundColor(colors.black)
  78.                 term.setTextColor(colors.white)
  79.             end
  80.         end
  81.     end
  82.     term.setCursorPos(1, 19)
  83.     term.setTextColor(colors.white)
  84.     print("> " .. tostring(lastResponse))
  85. end
  86.  
  87. local function click(x,y,n)
  88.     if not n == 1 then
  89.         return
  90.     end
  91.     for i, object in ipairs(buttons) do
  92.         if buttonLib.inBounds(object,x, y) then
  93.             if object.action then
  94.                 object.action()
  95.             end
  96.         end
  97.     end
  98. end
  99.  
  100. local function send(msg)
  101.     rednet.send(controlId, msg)
  102. end
  103.  
  104. local function key(keyCode)
  105.     if keyCode == keys.a then
  106.         send("left")
  107.     elseif keyCode == keys.d then
  108.         send("right")
  109.     elseif keyCode == keys.w then
  110.         send("forward")
  111.     elseif keyCode == keys.s then
  112.         send("back")
  113.     elseif keyCode == keys.space then
  114.         send("up")
  115.     elseif keyCode == keys.leftShift then
  116.         send("down")
  117.     elseif keyCode == keys.one then
  118.         buildSide = "down"
  119.         lastResponse = "Build Dir: Down"
  120.         markDraw = true
  121.     elseif keyCode == keys.two then
  122.         buildSide = "forward"
  123.         lastResponse = "Build Dir: Forward"
  124.         markDraw = true
  125.     elseif keyCode == keys.three then
  126.         buildSide = "up"
  127.         lastResponse = "Build Dir: Up"
  128.         markDraw = true
  129.     elseif keyCode == keys.down then
  130.         if buildSide == "forward" then
  131.             buildSide = "down"
  132.         elseif buildSide == "up" then
  133.             buildSide = "forward"
  134.         end
  135.         lastResponse = "Build Dir: " .. buildSide
  136.         markDraw = true
  137.     elseif keyCode == keys.up then
  138.         if buildSide == "forward" then
  139.             buildSide = "up"
  140.         elseif buildSide == "down" then
  141.             buildSide = "forward"
  142.         end
  143.         lastResponse = "Build Dir: " .. buildSide
  144.         markDraw = true
  145.     elseif keyCode == keys.left then
  146.         if buildSide == "forward" then
  147.             send("dig")
  148.         elseif buildSide == "up" then
  149.             send("digUp")
  150.         elseif buildSide == "down" then
  151.             send("digDown")
  152.         end
  153.     elseif keyCode == keys.right then
  154.         if buildSide == "forward" then
  155.             send("place")
  156.         elseif buildSide == "up" then
  157.             send("placeUp")
  158.         elseif buildSide == "down" then
  159.             send("placeDown")
  160.         end
  161.     elseif keyCode == keys.q then
  162.         if inventoryData then
  163.             if inventoryData.current > 1 then
  164.                 send("select " .. inventoryData.current - 1)
  165.             end
  166.         else
  167.             send("select 1")
  168.         end
  169.     elseif keyCode == keys.e then
  170.         if inventoryData then
  171.             if inventoryData.current < 16 then
  172.                 send("select " .. inventoryData.current + 1)
  173.             end
  174.         else
  175.             send("select 1")
  176.         end
  177.     end
  178. end
  179.  
  180. local function net(id, msgBundle)
  181.     if not id == controlId then
  182.         return
  183.     end
  184.  
  185.     for i, object in ipairs(msgBundle) do
  186.         local message = object;
  187.         local type = message.type
  188.         local data = message.message
  189.  
  190.         if type == "inventory" then
  191.             inventoryData = data
  192.             markDraw = true
  193.         elseif type == "inspect" then
  194.             inspectData = data
  195.             markDraw = true
  196.         else
  197.             lastResponse = data
  198.             markDraw = true
  199.         end
  200.     end
  201. end
  202.  
  203. buttons = {
  204.     buttonLib.new(2, 2, 4, 3, "Up",
  205.         function()
  206.             send("up")
  207.         end
  208.     ),
  209.     buttonLib.new(7, 2, 4, 3, "FW",
  210.         function()
  211.             send("forward")
  212.         end
  213.     ),
  214.     buttonLib.new(12, 2, 4, 3, "DW",
  215.         function()
  216.             send("down")
  217.         end
  218.     ),
  219.     buttonLib.new(2, 6, 4, 3, "LF",
  220.         function()
  221.             send("left")
  222.         end
  223.     ),
  224.     buttonLib.new(7, 6, 4, 3, "BK",
  225.         function()
  226.             send("back")
  227.         end
  228.     ),
  229.     buttonLib.new(12, 6, 4, 3, "RT",
  230.         function()
  231.             send("right")
  232.         end
  233.     ),
  234.     buttonLib.new(17, 2, 4, 3, "DU",
  235.         function()
  236.             send("digUp")
  237.         end
  238.     ),
  239.     buttonLib.new(17, 6, 4, 3, "DG",
  240.         function()
  241.             send("dig")
  242.         end
  243.     ),
  244.     buttonLib.new(17, 10, 4, 3, "DD",
  245.         function()
  246.             send("digDown")
  247.         end
  248.     ),
  249.     buttonLib.new(22, 2, 4, 3, "PU",
  250.         function()
  251.             send("placeUp")
  252.         end
  253.     ),
  254.     buttonLib.new(22, 6, 4, 3, "PL",
  255.         function()
  256.             send("place")
  257.         end
  258.     ),
  259.     buttonLib.new(22, 10, 4, 3, "PD",
  260.         function()
  261.             send("placeDown")
  262.         end
  263.     ),
  264.     buttonLib.new(3, 10, 2, 1, "01",
  265.         function()
  266.             send("select 1")
  267.         end
  268.     ),
  269.     buttonLib.new(6, 10, 2, 1, "02",
  270.         function()
  271.             send("select 2")
  272.         end
  273.     ),
  274.     buttonLib.new(9, 10, 2, 1, "03",
  275.         function()
  276.             send("select 3")
  277.         end
  278.     ),
  279.     buttonLib.new(12, 10, 2, 1, "04",
  280.         function()
  281.             send("select 4")
  282.         end
  283.     ),
  284.     buttonLib.new(3, 12, 2, 1, "05",
  285.         function()
  286.             send("select 5")
  287.         end
  288.     ),
  289.     buttonLib.new(6, 12, 2, 1, "06",
  290.         function()
  291.             send("select 6")
  292.         end
  293.     ),
  294.     buttonLib.new(9, 12, 2, 1, "07",
  295.         function()
  296.             send("select 7")
  297.         end
  298.     ),
  299.     buttonLib.new(12, 12, 2, 1, "08",
  300.         function()
  301.             send("select 8")
  302.         end
  303.     ),
  304.     buttonLib.new(3, 14, 2, 1, "09",
  305.         function()
  306.             send("select 9")
  307.         end
  308.     ),
  309.     buttonLib.new(6, 14, 2, 1, "10",
  310.         function()
  311.             send("select 10")
  312.         end
  313.     ),
  314.     buttonLib.new(9, 14, 2, 1, "11",
  315.         function()
  316.             send("select 11")
  317.         end
  318.     ),
  319.     buttonLib.new(12, 14, 2, 1, "12",
  320.         function()
  321.             send("select 12")
  322.         end
  323.     ),
  324.     buttonLib.new(3, 16, 2, 1, "13",
  325.         function()
  326.             send("select 13")
  327.         end
  328.     ),
  329.     buttonLib.new(6, 16, 2, 1, "14",
  330.         function()
  331.             send("select 14")
  332.         end
  333.     ),
  334.     buttonLib.new(9, 16, 2, 1, "15",
  335.         function()
  336.             send("select 15")
  337.         end
  338.     ),
  339.     buttonLib.new(12, 16, 2, 1, "16",
  340.         function()
  341.             send("select 16")
  342.         end
  343.     ),
  344.     buttonLib.new(18, 12, 3, 1, "^",
  345.         function()
  346.             buildSide = "up"
  347.         end
  348.     ),
  349.     buttonLib.new(18, 13, 3, 1, "-",
  350.         function()
  351.             buildSide = "forward"
  352.         end
  353.     ),
  354.     buttonLib.new(18, 14, 3, 1, "v",
  355.         function()
  356.             buildSide = "down"
  357.         end
  358.     ),
  359. }
  360.  
  361. rednet.close("back")
  362. rednet.open("back")
  363.  
  364. markDraw = true
  365. send("inventory")
  366.  
  367. while true do
  368.     local event, n, x, y = os.pullEvent()
  369.  
  370.     if event == "mouse_click" then
  371.         click(x,y,n)
  372.     elseif event == "key" then
  373.         key(n)
  374.     elseif event == "rednet_message" then
  375.         net(n,x)
  376.     end
  377.  
  378.     if markDraw then
  379.         markDraw = false
  380.         draw()
  381.     end
  382. end
  383.  
Add Comment
Please, Sign In to add comment