Advertisement
Parg60

miningMaster

Dec 18th, 2020 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.79 KB | None | 0 0
  1. modem = peripheral.wrap("left")
  2. modem.open(5555)
  3.  
  4. function setup()
  5.     modem.open(403)
  6.     for i = 1, 10, 1 do
  7.         modem.open(100 + i)
  8.     end
  9.  
  10.     accepted = false
  11.     fullSlotCounter = 0
  12.     temp = 0
  13.     awaitVar = true
  14. end
  15.  
  16.  
  17. function await(x)
  18.     awaitVar = true
  19.     while awaitVar == true do
  20.         event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  21.         if message == x then
  22.             awaitVar = false
  23.         end
  24.     end
  25. end
  26.  
  27.  
  28. function getValues()  
  29.     event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  30.     width = tonumber(message)
  31.  
  32.     event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  33.     height = tonumber(message)
  34.  
  35.     event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  36.     depth = tonumber(message)
  37. end
  38.  
  39.  
  40. function calculateWidth()
  41.     print("calculating")
  42.  
  43.     if width % 10 == 0 then
  44.         slaveWidth = width / 10
  45.         lastSlaveWidth = slaveWidth
  46.         slaveAmount = 10
  47.     elseif width < 11 then
  48.         slaveWidth = 1
  49.         lastSlaveWidth = slaveWidth
  50.         slaveAmount = width
  51.     else
  52.  
  53.         for i = 1, 10, 1 do
  54.             if i == 1 then
  55.                 slaveWidth = (width - (width % i)) / i
  56.                 lastSlaveWidth = slaveWidth + width % i
  57.                 if slaveWidth > lastSlaveWidth then
  58.                     temp = slaveWidth
  59.                 end
  60.            
  61.             else
  62.            
  63.                 if width - (width % i) / i < temp then
  64.                     slaveWidth = (width - (width % i)) / i
  65.                     lastSlaveWidth = slaveWidth + width % i
  66.                     if slaveWidth > lastSlaveWidth then
  67.                         temp = slaveWidth
  68.                     else
  69.                         temp = lastSlaveWidth
  70.                     end
  71.                 end
  72.             end
  73.  
  74.         end
  75.  
  76.     end
  77.  
  78.     print(slaveAmount)
  79.     print(slaveWidth)
  80.     print(lastSlaveWidth)
  81. end
  82.  
  83.  
  84. function approximitTime()
  85.     approximitTime = (((width * height * depth) * 0.75) / 10) + (10 + width * 1)
  86.     modem.transmit(5555, 5555, "Mining started, approximitly "..approximitTime.." seconds left...")
  87. end
  88.  
  89.  
  90. function checkFuel()
  91.     if turtle.getFuelLevel() < 801 then
  92.         turtle.select(14)
  93.         turtle.refuel(16)
  94.         turtle.select(1)
  95.     end
  96. end
  97.  
  98.  
  99. function checkInv()
  100.     if turtle.getItemCount(14) < 8 then
  101.         enderChests()
  102.     elseif turtle.getItemCount(8) > 0 then
  103.         enderChests()
  104.     end
  105.        
  106.        
  107.     fullSlotCounter = 0
  108.     checkFuel()
  109. end
  110.  
  111.  
  112.  
  113. function enderChests()
  114.     -- Unload slot 1 - 10
  115.     turtle.dig()
  116.     turtle.select(15)
  117.     turtle.place()
  118.     for i = 1, 10 , 1 do
  119.         turtle.select(i)
  120.         turtle.drop()
  121.     end
  122.     turtle.select(15)
  123.     turtle.dig()
  124.  
  125.     -- Load Slot 14 with coal
  126.     turtle.select(16)
  127.     turtle.place()
  128.    
  129.     if turtle.getItemCount(14) < 64 then
  130.         turtle.select(14)
  131.         turtle.suck(64 - turtle.getItemCount(14))
  132.     end
  133.    
  134.     turtle.select(16)
  135.     turtle.dig()
  136. end
  137.  
  138.  
  139. function buildSpwan()
  140.     print("building spawn...")
  141.     turtle.select(1)
  142.     turtle.turnLeft()
  143.     turtle.dig()
  144.     turtle.forward()
  145.     turtle.turnRight()
  146.     turtle.turnRight()
  147.     turtle.digUp()
  148.     turtle.digDown()
  149.     turtle.select(12)
  150.     turtle.placeDown()
  151.     turtle.select(11)
  152.     turtle.dropDown()
  153.     turtle.up()
  154. end
  155.  
  156.  
  157. function summonSlaves()
  158.     print("summoning slaves...")
  159.     print(slaveAmount)
  160.  
  161.     for i = 1, slaveAmount, 1 do
  162.         turtle.select(13)
  163.         turtle.placeDown()
  164.  
  165.         print("slave summoned")
  166.  
  167.         peripheral.call('bottom','turnOn')
  168.  
  169.         sleep(1.5)
  170.         turtle.select(15)
  171.         turtle.dropDown(1)
  172.         sleep(.5)
  173.         turtle.select(16)
  174.         turtle.dropDown(1)
  175.         sleep(.5)
  176.         turtle.select(14)
  177.         turtle.dropDown(4)
  178.        
  179.  
  180.         await("listening-port")
  181.         print("accept")
  182.         modem.transmit(403, 403, 100 + i)
  183.  
  184.         if i == 1 then
  185.             distanceToGo = width - lastSlaveWidth
  186.         else
  187.             distanceToGo = width - lastSlaveWidth - (slaveWidth * (i - 1))
  188.         end
  189.  
  190.         print(distanceToGo)
  191.  
  192.         await("listening-position")
  193.         modem.transmit(100 + i, 100 + i, distanceToGo)
  194.        
  195.         sleep(width + 3)
  196.     end
  197.  
  198.     sleep(width + 3)
  199. end
  200.  
  201.  
  202. function destroySpawn()
  203.     print("destroying spawn...")
  204.     turtle.down()
  205.     turtle.select(11)
  206.     turtle.suckDown()
  207.     turtle.select(12)
  208.     turtle.digDown()
  209.     checkInv()
  210. end
  211.  
  212.  
  213. function giveOrder()
  214.     print("sending parameters")
  215.     for i = 1, slaveAmount, 1 do
  216.         modem.transmit(100 + i, 100 + i, "values-incoming")
  217.        
  218.         if i == 1 then
  219.             modem.transmit(100 + i, 100 + i, lastSlaveWidth)
  220.             modem.transmit(100 + i, 100 + i, height)
  221.             modem.transmit(100 + i, 100 + i, depth)
  222.         else
  223.             modem.transmit(100 + i, 100 + i, slaveWidth)
  224.             modem.transmit(100 + i, 100 + i, height)
  225.             modem.transmit(100 + i, 100 + i, depth)
  226.         end
  227.     end
  228. end
  229.  
  230. function eatSlaves()
  231.     for i = 1, slaveAmount, 1 do
  232.         await("arrived-home")
  233.     end
  234.    
  235.     for i = 1, width, 1 do
  236.         turtle.forward()
  237.         turtle.dig()
  238.     end
  239.     turtle.turnRight()
  240.     turtle.turnRight()
  241.     for i = 1, width, 1 do
  242.         turtle.forward()
  243.         turtle.dig()
  244.     end
  245.     turtle.turnRight()
  246. end
  247.  
  248.  
  249.  
  250. -- Actual program
  251.  
  252. await("start-mining")
  253.  
  254. getValues()
  255. calculateWidth()
  256. approximitTime()
  257.  
  258. checkInv()
  259. setup()
  260.  
  261. buildSpwan()
  262. summonSlaves()
  263.    
  264. giveOrder()
  265. sleep(15)
  266. destroySpawn()
  267.  
  268.  
  269. eatSlaves()
  270. checkInv()
  271. sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement