Blazuno

TurtleSwarmHOME

Oct 31st, 2021 (edited)
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 KB | None | 0 0
  1. ------Variables and tables that will be needed later
  2. local arg1, arg2, arg3 = ...
  3. local PCID = os.getComputerID()
  4. local turtleAmount = turtle.getItemCount(1)
  5. local p
  6. local k
  7. local tableTurtlePCIDs = {}
  8. local deploymentCoords = {}
  9.    
  10. -------Just opening up the rednet
  11. rednet.open("right")
  12.  
  13. --------Checks if a number is odd or not  
  14. function odd(number)
  15.     if (number % 2) == 0 then
  16.         return false
  17.     else
  18.         return true
  19.     end
  20. end
  21.  
  22. ------Checks if a number is prime or not
  23. function prime(n)
  24.     for i = 2, n^(1/2) do
  25.         if (n % i) == 0 then
  26.             return false
  27.         end
  28.         return true
  29.     end
  30. end
  31.  
  32. ------This function uses math to divide the location up by sectors to count how many turtles will be needed for the whole quarry.
  33. function getSectors (x, y, turtleAmount)
  34.     turtleAmount = tonumber (turtleAmount)
  35.     turtleAmount2 = tonumber (turtleAmount)
  36.     turtleAmount3 = tonumber (turtleAmount)
  37.     x = tonumber (x)
  38.     y = tonumber (y)
  39.     print ("Arguments: " .. x .. ", " .. y .. ", " .. turtleAmount)
  40.     totalArea = x * y
  41.     print ("Total Sector Area: " .. totalArea)
  42.     local l = true
  43.     local w = true
  44.     local j = 2
  45.     local m = 2
  46.     local g = false
  47.     if  prime (x) == true and prime (y) == true then
  48.         sectorCount = 1
  49.         length = length
  50.         width = width
  51.     else
  52.         repeat
  53.             if (x % turtleAmount3) == 0 then
  54.                 l = false
  55.             else
  56.                 turtleAmount3 = turtleAmount3 - 1
  57.                 l = true
  58.             end
  59.             sleep (0.5)
  60.         until l == false
  61.         turtlesPerX = turtleAmount3
  62.         repeat
  63.             if (y % turtleAmount2) == 0 then
  64.                 w = false
  65.             else
  66.                 turtleAmount2 = turtleAmount2 - 1
  67.                 w = true
  68.             end
  69.             sleep (0.5)
  70.         until w == false
  71.         turtlesPerY = turtleAmount2
  72.         print ("Debug: x = " .. turtlesPerX)
  73.         print ("Debug: y = " .. turtlesPerY)
  74.         length = x / turtlesPerX
  75.         width = y / turtlesPerY
  76.         areaPerSector = width * length
  77.         sectorCount = totalArea / areaPerSector
  78.         if sectorCount > turtleAmount then
  79.             print ("Too many sectors, attempting to lower sector amount (will use less turtles)")
  80.             repeat
  81.                 repeat
  82.                     length = length+1
  83.                 until x % length == 0
  84.                 repeat
  85.                     width=width+1
  86.                 until y % width == 0
  87.                 areaPerSector = width * length
  88.                 sectorCount = totalArea / areaPerSector
  89.                 print ("getSectors sectorCount Debug: " .. sectorCount)
  90.                 turtlesPerX = x / length
  91.                 print ("getSectors sectorsPerX Debug: " .. turtlesPerX)
  92.                 turtlesPerY = y / width
  93.                 print ("getSectors sectorsPerY Debug: " .. turtlesPerY)
  94.                 sleep (2)
  95.                 print ("getSectors length Debug: " .. length)
  96.                 print ("getSectors width Debug: " .. width)
  97.             until sectorCount <= turtleAmount
  98.         end
  99.     end
  100.     return length, width, sectorCount, turtlesPerX, turtlesPerY
  101. end
  102.  
  103.  
  104. ------Uses basic math to find the coordinates that each turtle will be deployed to
  105. function getDeploymentCoords (sectorsPerRow, sectorsPerColumn, x, y, length, width, turtleCountNeeded)
  106.     local deploymentTable = {}
  107.     local xCoords = {}
  108.     local sectorsPerRow = tonumber (sectorsPerRow)
  109.     local sectorsPerColumn = tonumber (sectorsPerColumn)
  110.     length2 = length
  111.     local x = x
  112.     local y = y
  113.     local j = 1
  114.     local k = 1
  115.     local l = 1
  116.     local yCoords = {}
  117.     print ("getDeploymentCoords sectorsPerRow Debug: " .. sectorsPerRow)
  118.     print ("getDeploymentCoords sectorsPerColumn Debug: " .. sectorsPerColumn)
  119.     for i=1, tonumber (sectorsPerRow) do
  120.         for i=1, sectorsPerColumn do
  121.             print ("getDeploymentCoords x Debug: " .. length2)
  122.             table.insert (xCoords, length2)
  123.             length2 = length2 + x
  124.             sleep (2)
  125.         end
  126.         table.insert (yCoords, width)
  127.         width = width + y
  128.         print ("getDeploymentCoords y Debug: " .. width)
  129.         length2 = length
  130.         sleep (2)
  131.     end
  132.     for i=1, sectorsPerRow do
  133.         for i=1, sectorsPerColumn do
  134.             table.insert (deploymentTable, xCoords[j])
  135.             sleep (0.1)
  136.             table.insert (deploymentTable, yCoords[k])
  137.             print ("getDeploymentCoords xCoords Debug :" .. xCoords[j])
  138.             print ("getDeploymentCoords yCoords Debug: " .. yCoords[k])
  139.  
  140.             j = j + 1
  141.         end
  142.         k = k + 1
  143.     end
  144.  
  145.     return deploymentTable
  146. end
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.        
  156.            
  157. -------Function to place turtles
  158. function placeTurtles()
  159.     local i
  160.     local turtleID = 1
  161.     for i = 1, turtleCountNeeded do
  162.         local q = true
  163.         repeat  
  164.             if turtle.place() == true then
  165.                 sleep(0.5)
  166.                 peripheral.call("front", "turnOn")
  167.                 local packet2 = {turtleID, turtleCountNeeded, plrPos, miningDimensions, rowsPerTurtle, deploymentCoords, length, width}
  168.                 sleep(2)
  169.                 rednet.broadcast(packet2)
  170.                 q = false
  171.             else
  172.                 q = true
  173.             end  
  174.             sleep(2)
  175.         until q == false
  176.         turtleID = turtleID+1
  177.     end
  178. end
  179.  
  180.  
  181.  
  182. k, packet, p = rednet.receive()
  183. PhonePCID = packet[1]
  184. plrPos = {packet[2], packet[3], packet[4]}
  185. miningDimensions = {packet[5], packet[6], packet[7], packet[8]}
  186. length, width, turtleCountNeeded, sectorsPerColumn, sectorsPerRow = getSectors(miningDimensions[1], miningDimensions[3], turtleAmount)
  187. deploymentCoords = getDeploymentCoords(sectorsPerRow, sectorsPerColumn, length, width, plrPos[1], plrPos[3], turtleCountNeeded)
  188. print(length)
  189. print(width)
  190. print(turtleCountNeeded)
  191. placeTurtles()
  192.  
  193.  
  194.  
  195.  
  196.  
Add Comment
Please, Sign In to add comment