awesome8digger

ComputerCraftSmartQuarry

Feb 21st, 2021 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.51 KB | None | 0 0
  1. quarryWidth = 0
  2. quarryLength = 0
  3. quarryFloorHeight = 0
  4. quarryStartHeight = 0
  5.  
  6. function forward(dist)
  7.   for i = 1, dist do
  8.     while turtle.forward() == false do
  9.       turtle.dig()
  10.     end
  11.   end
  12. end
  13.  
  14. function up(dist)
  15.   for i = 1, dist do
  16.     while turtle.up() == false do
  17.       turtle.digUp()
  18.     end
  19.   end
  20. end
  21.  
  22. function down(dist)
  23.   for i = 1, dist do
  24.     while turtle.down() == false do
  25.       turtle.digDown()
  26.     end
  27.   end
  28. end
  29.  
  30. function mineLayer(width, length)
  31.   for i = 1, width do
  32.     forward(length - 1)
  33.     if (i % 2) == 1 then
  34.       if i == width then
  35.         break
  36.       end
  37.       turtle.turnRight()
  38.       forward(1)
  39.       turtle.turnRight()
  40.     else
  41.       if i == width then
  42.         break
  43.       end
  44.       turtle.turnLeft()
  45.       forward(1)
  46.       turtle.turnLeft()
  47.     end
  48.   end
  49.  
  50.   if (width % 2) == 1 then
  51.     turtle.turnLeft()
  52.     turtle.turnLeft()
  53.     forward(length - 1)
  54.   end
  55.   turtle.turnRight()
  56.   forward(width - 1)
  57.   turtle.turnLeft()
  58. end
  59.  
  60. function depositLoot()
  61.   for i = 2, 16 do
  62.     turtle.select(i)
  63.     while (turtle.drop() == false) and (turtle.getItemCount(i) ~= 0) do
  64.       os.sleep(5)
  65.     end
  66.   end
  67. end
  68.  
  69. function refuelFromChest()
  70.   local fuelNeeded = (quarryLength * quarryWidth) + (2 * quarryStartHeight)
  71.   turtle.select(1)
  72.   while refuelToLevel(fuelNeeded) == false do
  73.     while turtle.suckUp() == false do
  74.       os.sleep(5)
  75.     end
  76.   end
  77. end
  78.  
  79. function main()
  80.   drawHeader()
  81.  
  82.   -- if disk drive exists with disk below turtle get quarry info and start mining
  83.   -- if no disk drive exists prompt the user to build the quarry
  84.   if disk.hasData('bottom') then
  85.     if disk.getLabel('bottom') == "QuarryData" then
  86.       if readQuarryData() == false then
  87.         return
  88.       end
  89.     else
  90.       print("No data disk in drive!")
  91.       return
  92.     end
  93.   else
  94.     if promptBuildQuarry() == false then
  95.     return
  96.     end
  97.   end
  98.  
  99.   if promptStartMining() == false then
  100.     return
  101.   end
  102.  
  103. end
  104.  
  105. function promptBuildQuarry()
  106.   drawHeader()
  107.   print("No disk drive or disk below turtle do you want to create a new quarry (y/n)?")
  108.   local input = read()
  109.   if input ~= "y" then
  110.     return false
  111.   end
  112.   return buildQuarry()
  113. end
  114.  
  115. function buildQuarry()
  116.   drawHeader()
  117.   print("Please Ensure these items are at the correct slots in the turtle:")
  118.   print("Slot 1: Any Fuel")
  119.   print("Slot 2: 1 Disk Drive")
  120.   print("Slot 3: 1 Floppy Disk")
  121.   print("Slot 4: 3 Chests")
  122.   print("Slot 5: 1 Lever")
  123.   print()
  124.   print("Are these items present, in their correct quantities, and in their correct locations (y/n)?")
  125.   local input = read()
  126.   if input ~= "y" then
  127.     return false
  128.   end
  129.   print("Starting Setup...")
  130.   os.sleep(3)
  131.  
  132.   drawHeader()
  133.   -- ensure disk drive is in slot 2
  134.   if (turtle.getItemCount(2) < 1) or not (turtle.getItemDetail(2).name == "computercraft:peripheral" and turtle.getItemDetail(2).damage == 0) then
  135.     print("No Disk Drive in slot 2!")
  136.     return false
  137.   end
  138.  
  139.   -- ensure floppy disk in slot 3
  140.   if (turtle.getItemCount(3) < 1) or (turtle.getItemDetail(3).name ~= "computercraft:disk_expanded") then
  141.     print("No Floppy Disk in slot 3!")
  142.     return false
  143.   end
  144.  
  145.   -- ensure 3 chests are in slot 4
  146.   if (turtle.getItemCount(4) < 3) or (turtle.getItemDetail(4).name ~= "minecraft:chest") then
  147.     print("No Chests in slot 4!")
  148.     return false
  149.   end
  150.  
  151.   -- ensure 3 chests are in slot 4
  152.   if (turtle.getItemCount(5) < 1) or (turtle.getItemDetail(5).name ~= "minecraft:lever") then
  153.     print("No Lever in slot 5!")
  154.     return false
  155.   end
  156.  
  157.   -- prompt user for width of quarry
  158.   print("Enter the width of the quarry (Max 64).")
  159.   local width = math.floor(tonumber(read()))
  160.   quarryWidth = width
  161.   print()
  162.  
  163.   -- prompt user for length of quarry
  164.   print("Enter the length of the quarry (Max 64).")
  165.   local length = math.floor(tonumber(read()))
  166.   quarryLength = length
  167.   print()
  168.  
  169.   -- prompt user for y level
  170.   print("Enter the Y-Level of the turtle.")
  171.   local height = math.floor(tonumber(read()))
  172.   quarryStartHeight = height
  173.   quarryFloorHeight = height
  174.   print()
  175.  
  176.   -- refuel turtle until the fuel level is 10
  177.   if refuelToLevel(10) == false then
  178.     print("Not enough fuel in slot 1!")
  179.     return false
  180.   end
  181.  
  182.   print("Placing Disk Drive...")
  183.   -- break the block below the turtle then select the disk drive and place it below the turtle
  184.   turtle.digDown()
  185.   turtle.select(2)
  186.   turtle.placeDown()
  187.  
  188.   print("Inserting Floppy Disk...")
  189.   -- select the floppy disk and insert it into the disk drive
  190.   turtle.select(3)
  191.   turtle.dropDown(1)
  192.  
  193.   print("Placing Storage Chests...")
  194.   -- turn 180 then break the block in front of the turtle then select the chest and place it in front of the turtle
  195.   turtle.turnRight()
  196.   turtle.turnRight()
  197.   turtle.dig()
  198.   turtle.select(4)
  199.   turtle.place()
  200.  
  201.   -- move the turtle left one block breaking the block in the way
  202.   turtle.turnLeft()
  203.   forward(1)
  204.   turtle.turnRight()
  205.  
  206.   -- break the block in front of the turtle then place a chest in front of the turtle
  207.   turtle.dig()
  208.   turtle.place()
  209.  
  210.   -- move the turtle right one block
  211.   turtle.turnRight()
  212.   forward(1)
  213.   turtle.turnRight()
  214.  
  215.   print("Placing Fuel Chest...")
  216.   -- turn right then place fuel chest breaking the block in the way
  217.   turtle.digUp()
  218.   turtle.placeUp()
  219.  
  220.   print("Placing Lever...")
  221.   -- turn left then place lever breaking block in the way
  222.   turtle.select(5)
  223.   turtle.turnLeft()
  224.   turtle.dig()
  225.   turtle.place()
  226.  
  227.   print("Writing Data To Disk...")
  228.   -- setup the flopy disk to hold info about this quarry
  229.   -- change the name of the disk to QuarryData
  230.   disk.setLabel('bottom', "QuarryData")
  231.  
  232.   -- write data to the disk
  233.   writeQuarryData()
  234.  
  235.   print("Depositing Extra Items...")
  236.   -- deposit extra items in the turtles inventory
  237.   turtle.turnLeft()
  238.   depositLoot()
  239.   turtle.turnLeft()
  240.   turtle.turnLeft()
  241.  
  242.   -- Quarry Setup Finished
  243.   print("Setup Complete!")
  244.   print()
  245.  
  246.   return true
  247. end
  248.  
  249. function writeQuarryData()
  250.   -- save the path that the disk files can be accessed at
  251.   local diskPath = disk.getMountPath('bottom')
  252.  
  253.   -- if the data directory doesn't exist on the disk then create one
  254.   if not fs.exists(diskPath .. '/data') then
  255.     fs.makeDir(diskPath .. '/data')
  256.   end
  257.  
  258.   -- write the width and length of the quarry to a file called QuarryData on the disk
  259.   local size = {}
  260.   size[1] = quarryWidth
  261.   size[2] = quarryLength
  262.   size[3] = quarryStartHeight
  263.   size[4] = quarryFloorHeight
  264.  
  265.   local file = fs.open(diskPath .. '/data/QuarryData', 'w')
  266.   file.flush()
  267.   file.write(textutils.serialize(size))
  268.   file.close()
  269. end
  270.  
  271. function readQuarryData()
  272.   local mountPath = disk.getMountPath('bottom')
  273.   if fs.exists(mountPath .. '/data/QuarryData') then
  274.     local file = fs.open(mountPath .. '/data/QuarryData', 'r')
  275.     local size = textutils.unserialize(file.readAll())
  276.     quarryWidth = size[1]
  277.     quarryLength = size[2]
  278.     quarryStartHeight = size[3]
  279.     quarryFloorHeight = size[4]
  280.     file.close()
  281.   else
  282.     return false
  283.   end
  284. end
  285.  
  286. function promptStartMining()
  287.   print("Quarry Data received do you want to start mining now (y/n)?")
  288.   local input = read()
  289.   if input ~= "y" then
  290.     return false
  291.   end
  292.   return miningLoop()
  293. end
  294.  
  295. function miningLoop()
  296.   while quarryFloorHeight > 5 do
  297.     drawHeader()
  298.     while redstone.getInput('left') == false do
  299.       drawHeader()
  300.       print("Awaiting redstone signal...")
  301.       os.sleep(5)
  302.     end
  303.     print(quarryStartHeight)
  304.     print(quarryFloorHeight)
  305.     print("Refueling...")
  306.     refuelFromChest()
  307.  
  308.     print("Entering Quarry...")
  309.     forward(1)
  310.     down(quarryStartHeight - quarryFloorHeight + 1)
  311.  
  312.     print("Mining Layer...")
  313.     mineLayer(quarryWidth, quarryLength)
  314.     quarryFloorHeight = quarryFloorHeight - 1
  315.  
  316.     print("Returning To Surface...")
  317.     up(quarryStartHeight - quarryFloorHeight)
  318.     forward(1)
  319.  
  320.     print("Updating Quarry Data...")
  321.     writeQuarryData()
  322.     print("Depositing Resources...")
  323.     depositLoot()
  324.     turtle.turnLeft()
  325.     turtle.turnLeft()
  326.   end
  327.  
  328.   drawHeader()
  329.   print("Quarry Complete!")
  330.   return true
  331. end
  332.  
  333. function drawHeader()
  334.   term.clear()
  335.   print("|-------------------------------------|")
  336.   print("|          Smart Quarry v0.0          |")
  337.   print("|-------------------------------------|")
  338.   print()
  339. end
  340.  
  341. function refuelToLevel(level)
  342.   turtle.select(1)
  343.   while turtle.getFuelLevel() < level do
  344.     if turtle.refuel(1) == false then
  345.       return false
  346.     end
  347.   end
  348.   return true
  349. end
  350.  
  351. main()
  352.  
Add Comment
Please, Sign In to add comment