MtnMCG

smartminenetwork

Jul 22nd, 2024 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 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 digHeight(height)
  30.     if height > 0 then
  31.         for i = 1, height - 1 do
  32.             turtle.digUp()
  33.             if i < height - 1 then
  34.                 turtle.up()
  35.             end
  36.         end
  37.         for i = 1, height - 2 do
  38.             turtle.down()
  39.         end
  40.     elseif height < 0 then
  41.         for i = 1, -height do
  42.             turtle.digDown()
  43.             if i < -height then
  44.                 turtle.down()
  45.             end
  46.         end
  47.         for i = 1, -height - 1 do
  48.             turtle.up()
  49.         end
  50.     end
  51.     turtle.dig()  -- Always dig forward
  52. end
  53.  
  54. local function smartmine(length, width, height)
  55.     currentTask = "Mining"
  56.     shouldStop = false
  57.     local direction = 1 -- 1 for forward, -1 for backward
  58.  
  59.     for w = 1, width do
  60.         for l = 1, length do
  61.             checkStop()
  62.             digHeight(height)
  63.             if l < length then
  64.                 turtle.forward()
  65.             end
  66.         end
  67.  
  68.         if w < width then
  69.             if direction == 1 then
  70.                 turtle.turnRight()
  71.             else
  72.                 turtle.turnLeft()
  73.             end
  74.             digHeight(height)
  75.             turtle.forward()
  76.             if direction == 1 then
  77.                 turtle.turnRight()
  78.             else
  79.                 turtle.turnLeft()
  80.             end
  81.             direction = -direction
  82.         end
  83.     end
  84.  
  85.     currentTask = nil
  86.     modem.transmit(basePort, basePort, {command = "miningComplete", id = turtleId})
  87. end
  88.  
  89. local function handleCommands()
  90.     while true do
  91.         local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  92.         if channel == basePort and type(message) == "table" then
  93.             if message.id == turtleId or message == "ping" then
  94.                 if message.command == "smartmine" then
  95.                     parallel.waitForAny(
  96.                         function() smartmine(message.length, message.width, message.height) end,
  97.                         handleCommands
  98.                     )
  99.                 elseif message.command == "stop" then
  100.                     shouldStop = true
  101.                     currentTask = "Stopped"
  102.                     modem.transmit(basePort, basePort, {command = "stopped", id = turtleId})
  103.                 elseif message.command == "stats" then
  104.                     modem.transmit(basePort, basePort, {command = "stats", id = turtleId, fuel = turtle.getFuelLevel(), task = currentTask or "Idle"})
  105.                 elseif message == "ping" then
  106.                     modem.transmit(basePort, basePort, {command = "pong", id = turtleId})
  107.                 end
  108.             end
  109.         end
  110.     end
  111. end
  112.  
  113. print("Turtle ID: " .. turtleId)
  114.  
  115. parallel.waitForAll(advertise, handleCommands)
Advertisement
Add Comment
Please, Sign In to add comment