djgaven588

Turtle Control

Mar 10th, 2024 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.80 KB | None | 0 0
  1. local controlId = 5
  2.  
  3. local builtMessages = {}
  4. local builtIndex = 1
  5. local function send(msg, type)
  6.     local response = {
  7.         type = type,
  8.         message = msg
  9.     }
  10.  
  11.     builtMessages[builtIndex] = response
  12.  
  13.     builtIndex = builtIndex + 1;
  14. end
  15.  
  16. local function finalizeMessage()
  17.     rednet.send(controlId, builtMessages)
  18. end
  19.  
  20. local function sendInventory()
  21.     local response = {
  22.         slots = {},
  23.         current = turtle.getSelectedSlot(),
  24.         fuel = turtle.getFuelLevel()
  25.     }
  26.     for i=1,16,1 do
  27.         response.slots[i] = turtle.getItemDetail(i)
  28.     end
  29.     send(response, "inventory")
  30. end
  31.  
  32. local function refuel()
  33.     if turtle.getFuelLevel() > 5 then
  34.         return true
  35.     end
  36.     send("Refueling")
  37.     local currentSlot = turtle.getSelectedSlot()
  38.     turtle.select(1)
  39.     if turtle.refuel(1) == false then
  40.         turtle.select(currentSlot)
  41.         send("Failed to refuel!", "log")
  42.         return false
  43.     end
  44.     turtle.select(currentSlot)
  45.     sendInventory()
  46.     return true
  47. end
  48.  
  49. local function sendInspect()
  50. end
  51. local function sendInspect_DISABLE()
  52.     local response = {
  53.         top = {},
  54.         forward = {},
  55.         bottom = {}
  56.     }
  57.     local hasBlock, data = turtle.inspectUp()
  58.     if hasBlock then
  59.         response.top = data
  60.     else
  61.         response.top = nil
  62.     end
  63.     hasBlock, data = turtle.inspect()
  64.     if hasBlock then
  65.         response.forward = data
  66.     else
  67.         response.forward = nil
  68.     end
  69.     hasBlock, data = turtle.inspectDown()
  70.     if hasBlock then
  71.         response.bottom = data
  72.     else
  73.         response.bottom = nil
  74.     end
  75.     send(response, "inspect")
  76. end
  77.  
  78. local function handleMessage(msg)
  79.     if msg == "left" then
  80.         turtle.turnLeft()
  81.         sendInspect()
  82.         return
  83.     end
  84.     if msg == "right" then
  85.         turtle.turnRight()
  86.         sendInspect()
  87.         return
  88.     end
  89.     if msg == "forward" then
  90.         refuel();
  91.         turtle.forward()
  92.         sendInspect()
  93.         sendInventory()
  94.         return
  95.     end
  96.     if msg == "back" then
  97.         refuel();
  98.         turtle.back()
  99.         sendInspect()
  100.         sendInventory()
  101.         return
  102.     end
  103.     if msg == "up" then
  104.         refuel();
  105.         turtle.up()
  106.         sendInspect()
  107.         sendInventory()
  108.         return
  109.     end
  110.     if msg == "down" then
  111.         refuel();
  112.         turtle.down()
  113.         sendInspect()
  114.         sendInventory()
  115.         return
  116.     end
  117.     if msg == "refuel" then
  118.         refuel()
  119.         return
  120.     end
  121.     if msg == "dig" then
  122.         turtle.dig()
  123.         sendInventory()
  124.         sendInspect()
  125.         return
  126.     end
  127.     if msg == "digUp" then
  128.         turtle.digUp()
  129.         sendInventory()
  130.         sendInspect()
  131.         return
  132.     end
  133.     if msg == "digDown" then
  134.         turtle.digDown()
  135.         sendInventory()
  136.         sendInspect()
  137.         return
  138.     end
  139.     if msg == "place" then
  140.         turtle.place()
  141.         sendInventory()
  142.         sendInspect()
  143.         return
  144.     end
  145.     if msg == "placeUp" then
  146.         turtle.placeUp()
  147.         sendInventory()
  148.         sendInspect()
  149.         return
  150.     end
  151.     if msg == "placeDown" then
  152.         turtle.placeDown()
  153.         sendInventory()
  154.         sendInspect()
  155.         return
  156.     end
  157.     if msg == "inventory" then
  158.         sendInventory()
  159.         return
  160.     end
  161.     for i=1,16,1 do
  162.         if msg == ("select " .. i) then
  163.             send("Current slot: " .. i, "log")
  164.             turtle.select(i)
  165.             sendInventory()
  166.             return
  167.         end
  168.     end
  169.     send("Unexpected message: " .. msg, "log")
  170. end
  171.  
  172. rednet.close("left")
  173. rednet.open("left")
  174.  
  175. while true do
  176.     local id, message = rednet.receive()
  177.     if id == controlId then
  178.         handleMessage(message)
  179.         finalizeMessage()
  180.     end
  181. end
  182.  
Add Comment
Please, Sign In to add comment