MtnMCG

Turtle slave program

Jul 21st, 2024 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.76 KB | None | 0 0
  1. -- wireless_turtle.lua
  2.  
  3.  
  4.  
  5. local modem = peripheral.find("modem")
  6.  
  7. if not modem then
  8.  
  9.     print("No modem found")
  10.  
  11.     return
  12.  
  13. end
  14.  
  15.  
  16.  
  17. local basePort = 2469
  18.  
  19. modem.open(basePort)
  20.  
  21.  
  22.  
  23. local currentTask = nil
  24.  
  25. local shouldStop = false
  26.  
  27. local turtleId = tostring(math.random(1000, 9999))  -- Generate a random 4-digit ID
  28.  
  29.  
  30.  
  31. local function advertise()
  32.  
  33.     while true do
  34.  
  35.         modem.transmit(basePort, basePort, {command = "pong", id = turtleId})
  36.  
  37.         sleep(2)  -- Advertise every 2 seconds
  38.  
  39.     end
  40.  
  41. end
  42.  
  43.  
  44.  
  45. local function checkStop()
  46.  
  47.     if shouldStop then
  48.  
  49.         error("Stopped")
  50.  
  51.     end
  52.  
  53. end
  54.  
  55.  
  56.  
  57. local function smartmine(length, width)
  58.  
  59.     currentTask = "Mining"
  60.  
  61.     shouldStop = false
  62.  
  63.     for w = 1, width do
  64.  
  65.         for l = 1, length do
  66.  
  67.             checkStop()
  68.  
  69.             turtle.dig()
  70.  
  71.             turtle.forward()
  72.  
  73.             turtle.digUp()
  74.  
  75.         end
  76.  
  77.         if w < width then
  78.  
  79.             turtle.turnLeft()
  80.  
  81.             turtle.dig()
  82.  
  83.             turtle.forward()
  84.  
  85.             turtle.turnLeft()
  86.  
  87.             for l = 1, length do
  88.  
  89.                 checkStop()
  90.  
  91.                 turtle.dig()
  92.  
  93.                 turtle.forward()
  94.  
  95.                 turtle.digUp()
  96.  
  97.             end
  98.  
  99.             turtle.turnRight()
  100.  
  101.             turtle.turnRight()
  102.  
  103.         end
  104.  
  105.     end
  106.  
  107.     currentTask = nil
  108.  
  109.     modem.transmit(basePort, basePort, {command = "miningComplete", id = turtleId})
  110.  
  111. end
  112.  
  113.  
  114.  
  115. local function handleCommands()
  116.  
  117.     while true do
  118.  
  119.         local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  120.  
  121.         if channel == basePort and type(message) == "table" then
  122.  
  123.             if message.id == turtleId or message == "ping" then
  124.  
  125.                 if message.command == "smartmine" then
  126.  
  127.                     parallel.waitForAny(
  128.  
  129.                         function() smartmine(message.length, message.width) end,
  130.  
  131.                         handleCommands
  132.  
  133.                     )
  134.  
  135.                 elseif message.command == "stop" then
  136.  
  137.                     shouldStop = true
  138.  
  139.                     currentTask = "Stopped"
  140.  
  141.                     modem.transmit(basePort, basePort, {command = "stopped", id = turtleId})
  142.  
  143.                 elseif message.command == "stats" then
  144.  
  145.                     modem.transmit(basePort, basePort, {command = "stats", id = turtleId, fuel = turtle.getFuelLevel(), task = currentTask or "Idle"})
  146.  
  147.                 elseif message == "ping" then
  148.  
  149.                     modem.transmit(basePort, basePort, {command = "pong", id = turtleId})
  150.  
  151.                 end
  152.  
  153.             end
  154.  
  155.         end
  156.  
  157.     end
  158.  
  159. end
  160.  
  161.  
  162.  
  163. print("Turtle ID: " .. turtleId)
  164.  
  165.  
  166.  
  167. parallel.waitForAll(advertise, handleCommands)
Advertisement
Add Comment
Please, Sign In to add comment