MtnMCG

Turtle slave program

Jul 21st, 2024 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. -- wireless_turtle.lua
  2.  
  3. local modem = peripheral.find("modem")
  4. if not modem then
  5.     print("No modem found")
  6.     return
  7. end
  8.  
  9. local basePort = 2469
  10. modem.open(basePort)
  11.  
  12. local currentTask = nil
  13. local shouldStop = false
  14. local turtleId = tostring(math.random(1000, 9999))  -- Generate a random 4-digit ID
  15.  
  16. local function advertise()
  17.     while true do
  18.         modem.transmit(basePort, basePort, {command = "pong", id = turtleId})
  19.         sleep(2)  -- Advertise every 2 seconds
  20.     end
  21. end
  22.  
  23. local function checkStop()
  24.     if shouldStop then
  25.         error("Stopped")
  26.     end
  27. end
  28.  
  29. local function smartmine(length, width)
  30.     currentTask = "Mining"
  31.     shouldStop = false
  32.     for w = 1, width do
  33.         for l = 1, length do
  34.             checkStop()
  35.             turtle.dig()
  36.             turtle.forward()
  37.             turtle.digUp()
  38.         end
  39.         if w < width then
  40.             turtle.turnLeft()
  41.             turtle.dig()
  42.             turtle.forward()
  43.             turtle.turnLeft()
  44.             for l = 1, length do
  45.                 checkStop()
  46.                 turtle.dig()
  47.                 turtle.forward()
  48.                 turtle.digUp()
  49.             end
  50.             turtle.turnRight()
  51.             turtle.turnRight()
  52.         end
  53.     end
  54.     currentTask = nil
  55.     modem.transmit(basePort, basePort, {command = "miningComplete", id = turtleId})
  56. end
  57.  
  58. local function handleCommands()
  59.     while true do
  60.         local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  61.         if channel == basePort and type(message) == "table" then
  62.             if message.id == turtleId or message == "ping" then
  63.                 if message.command == "smartmine" then
  64.                     parallel.waitForAny(
  65.                         function() smartmine(message.length, message.width) end,
  66.                         handleCommands
  67.                     )
  68.                 elseif message.command == "stop" then
  69.                     shouldStop = true
  70.                     currentTask = "Stopped"
  71.                     modem.transmit(basePort, basePort, {command = "stopped", id = turtleId})
  72.                 elseif message.command == "stats" then
  73.                     modem.transmit(basePort, basePort, {command = "stats", id = turtleId, fuel = turtle.getFuelLevel(), task = currentTask or "Idle"})
  74.                 elseif message == "ping" then
  75.                     modem.transmit(basePort, basePort, {command = "pong", id = turtleId})
  76.                 end
  77.             end
  78.         end
  79.     end
  80. end
  81.  
  82. print("Turtle ID: " .. turtleId)
  83.  
  84. parallel.waitForAll(advertise, handleCommands)
Advertisement
Add Comment
Please, Sign In to add comment