Advertisement
MavricMC

Miner main turtle

May 30th, 2021 (edited)
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | None | 0 0
  1. local SERVER_PORT = 0
  2. local CLIENT_PORT = 10
  3.  
  4. term.clear()
  5. term.setCursorPos(1, 1)
  6.  
  7. local segmentation = 5
  8. if (#arg == 1) then
  9.     segmentation = tonumber(arg[1])
  10.     print("Segmentation size set to", segmentation)
  11. elseif (#arg == 0) then
  12.     print("No segmentation size selected, defaulting to", segmentation)
  13. else
  14.     error('Too many args given...')
  15. end
  16.  
  17. local endX, endY, endZ = gps.locate()
  18. endX = math.floor(endX)
  19. endY = math.floor(endY + 2)
  20. endZ = math.floor(endZ)
  21.  
  22. local modem = peripheral.wrap("right")
  23. modem.open(SERVER_PORT)
  24.  
  25. function getItemIndex(itemName)
  26.     for slot = 1, 16 do
  27.         local item = turtle.getItemDetail(slot)
  28.         if item ~= nil then
  29.             if item.name == itemName then
  30.                 return slot
  31.             end
  32.         end
  33.     end
  34. end
  35.  
  36. function deploy(cords)
  37.    
  38.     numm = getItemIndex("computercraft:turtle_advanced")
  39.     if numm == nil then
  40.         turtle.suckDown(1)
  41.         turtle.select(getItemIndex("computercraft:turtle_advanced"))
  42.     else
  43.         turtle.select(numm)
  44.     end
  45.        
  46.     while (turtle.detect()) do
  47.         sleep(0.3)
  48.     end
  49.    
  50.     turtle.place()
  51.     peripheral.call("front", "turnOn")
  52.    
  53.     event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  54.     if msg ~= "CLIENT_DEPLOYED" then
  55.         error("No client deploy message, exitting...")
  56.     end
  57.    
  58.    
  59.     --drop thing for the turtle to have on them--
  60.    
  61.     modem.transmit(CLIENT_PORT, SERVER_PORT, cords)
  62.     print("Deploying bot to", cords[1], cords[2], cords[3])
  63. end
  64.  
  65. function round(number, segment)
  66.     --check if remander = 0--
  67.     if number % segment ~= 0 then
  68.         --if not then get the remander--
  69.         remander = number % segment
  70.        
  71.         --work out deveden--
  72.         --example 11 % 5 = 5, remander 1, because 5 times 2 plus 1 = 11
  73.        
  74.         --remove remander from number to round down to the closest mulitpul of the segment
  75.         down = number - remander
  76.        
  77.         --devide rounded down number buy segmnet number to get diveden
  78.         div = down / segment
  79.         times = div
  80.         --rounded = times * segment
  81.         return times, remander
  82.     else
  83.         times = number / segment
  84.         times = times - 1
  85.         return times, 5
  86.     end
  87. end
  88.  
  89. function sendBots(xStart, zStart, xSize, zSize, cords, seg)
  90.     xCount, xRound = round(xSize, seg)  
  91.     zCount, zRound = round(zSize, seg)
  92.     xNum = xCount + 1
  93.     zNum = zCount + 1
  94.     num = xNum * zNum
  95.    
  96.     if num > 1 then
  97.         printError("Deploying", num, "bots...")
  98.     else
  99.         printError("Deploying", num, "bot...")
  100.     end
  101.     number = cords[1]
  102.     zOffset = cords[3]
  103.     for z = 1, xCount do
  104.         xOffset = number
  105.         for x = 1, zCount do
  106.             cords[1] = xOffset
  107.             cords[3] = zOffset
  108.             cords[4] = seg
  109.             cords[6] = seg
  110.             deploy(cords)
  111.             xOffset = xOffset - seg
  112.         end
  113.         cords[1] = xOffset
  114.         cords[3] = zOffset
  115.         cords[4] = xRound
  116.         cords[6] = seg
  117.         deploy(cords)
  118.         zOffset = zOffset - seg
  119.     end
  120.     xOffset = number
  121.     for x = 1, xCount do
  122.         cords[1] = xOffset
  123.         cords[3] = zOffset
  124.         cords[4] = seg
  125.         cords[6] = zRound
  126.         deploy(cords)
  127.         xOffset = xOffset - seg
  128.     end
  129.     cords[1] = xOffset
  130.     cords[3] = zOffset
  131.     cords[4] = xRound
  132.     cords[6] = zRound
  133.     deploy(cords)
  134. end
  135.  
  136.  
  137.  
  138. while true do
  139.     printError("Waiting for target signal...")
  140.     event, side, senderChannel, replyChannel, cord, distance = os.pullEvent("modem_message")
  141.     print("RECEIVED QUARY REQUEST AT:", cord[1], cord[2], cord[3])
  142.     cord[7] = endX
  143.     cord[8] = endY
  144.     cord[9] = endZ
  145.    
  146.     sendBots(cord[1], cord[3], cord[4], cord[6], cord, segmentation)
  147.    
  148.     event, side, senderChannel, replyChannel, msg, distance = os.pullEvent("modem_message")
  149.     turtle.digUp()
  150.     term.clear()
  151.     term.setCursorPos(1, 1)
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement