Advertisement
HandieAndy

Shaft Mine

Dec 11th, 2017
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.60 KB | None | 0 0
  1. --Shaft Mining Program
  2. --Made by Andrew Lalis
  3.  
  4. local args = {...}
  5.  
  6. local segmentLength = 0
  7. local sideLength = 0
  8. local sideInterval = 0
  9. local torchInterval = 0
  10.  
  11. --Parses command line arguments and gets input.
  12. function parseArgs(args)
  13.     if (args[1] ~= nil) then
  14.         segmentLength = tonumber(args[1])
  15.     else
  16.         print("How long should this mine be?")
  17.         segmentLength = tonumber(read())
  18.     end
  19.     if (args[2] ~= nil) then
  20.         sideLength = tonumber(args[2])
  21.     else
  22.         print("How long should side shafts be?")
  23.         sideLength = tonumber(read())
  24.     end
  25.     if (args[3] ~= nil) then
  26.         sideInterval = tonumber(args[3])
  27.     else
  28.         print("How often should side shafts be dug?")
  29.         sideInterval = tonumber(read())
  30.     end
  31.     if (args[4] ~= nil) then
  32.         torchInterval = tonumber(args[4])
  33.     else
  34.         print("How often should torches be placed?")
  35.         torchInterval = tonumber(read())
  36.     end
  37. end
  38.  
  39. --Rounds a number for display.
  40. function round(num, places)
  41.     return (string.format("%." .. (places or 0) .. "f", num))
  42. end
  43.  
  44. --Gets the number of items in the turtle's inventory.
  45. function getItemCount(itemName)
  46.     local count = 0
  47.     for i=1,16 do
  48.         local data = turtle.getItemDetail(i)
  49.         if (data ~= nil and data.name == itemName) then
  50.             count = count + data.count
  51.         end
  52.     end
  53.     return count
  54. end
  55.  
  56. --Selects an item in the inventory, or the first slot if it can't be selected. Returns success.
  57. function selectItem(itemName)
  58.     for i=1,16 do
  59.         local data = turtle.getItemDetail(i)
  60.         if (data ~= nil and data.name == itemName) then
  61.             turtle.select(i)
  62.             return true
  63.         end
  64.     end
  65.     turtle.select(0)
  66.     return false
  67. end
  68.  
  69. --What to do when the turtle's inventory is too full.
  70. function onFullInventory()
  71.     print("Inventory is full, please remove the items.")
  72.     local clean = false
  73.     while (not clean) do
  74.         local event = os.pullEvent("turtle_inventory")
  75.         for i=1,16 do
  76.             local data = turtle.getItemDetail(i)
  77.             if (data ~= nil and data.name ~= "minecraft:torch") then
  78.                 clean = false
  79.                 break
  80.             end
  81.         end
  82.     end
  83.     print("Inventory clean. Continuing.")
  84. end
  85.  
  86. --Interface for loading fuel until the turtle is full.
  87. function fuelLoader(amount)
  88.     while (turtle.getFuelLevel() < amount) do
  89.         print("Need "..(amount - turtle.getFuelLevel()).." more fuel units.")
  90.         local event = os.pullEvent("turtle_inventory")
  91.         for i=1,16 do
  92.             turtle.select(i)
  93.             turtle.refuel()
  94.         end
  95.     end
  96. end
  97.  
  98. --Ensures the turtle will have enough fuel for a given shaft mine.
  99. function ensureFuel(segLength, sideLength, sideInterval)
  100.     local fuelUsage = (segLength / sideInterval) * sideLength + segLength
  101.     if (turtle.getFuelLevel() < fuelUsage) then
  102.         print("Not enough fuel to begin.")
  103.         fuelLoader(fuelUsage)
  104.         print("Fuel loaded!")
  105.     end
  106. end
  107.  
  108. --Ensures that the turtle will have enough torches for the shaft.
  109. function ensureTorches(segmentLength, torchInterval)
  110.     local torchesNeeded = (segmentLength / torchInterval)
  111.     while (getItemCount("minecraft:torch") < torchesNeeded) do
  112.         print("Need "..(torchesNeeded - getItemCount("minecraft:torch")).." more torches.")
  113.         local event = os.pullEvent("turtle_inventory")
  114.     end
  115.     print("Torches loaded!")
  116. end
  117.  
  118. --Moves back by some amount.
  119. function goBack(length)
  120.     for i=1,length do
  121.         local success = turtle.back()
  122.         while (not success) do
  123.             success = turtle.back()
  124.         end
  125.     end
  126. end
  127.  
  128. --Digs forward for some amount.
  129. function digForwardSegment(length)
  130.     for i=1,length do
  131.         turtle.dig()
  132.         local success = turtle.forward()
  133.         while (not success) do
  134.             success = turtle.forward()
  135.         end
  136.         turtle.digDown()
  137.     end
  138. end
  139.  
  140. --Digs side shafts on either side of the main shaft.
  141. function digSideShafts(length)
  142.     turtle.turnLeft()
  143.     digForwardSegment(length)
  144.     goBack(length)
  145.     turtle.turnRight()
  146.     turtle.turnRight()
  147.     digForwardSegment(length)
  148.     goBack(length)
  149.     turtle.turnLeft()
  150. end
  151.  
  152. --Places a torch on the ground.
  153. function placeTorch()
  154.     local success = selectItem("minecraft:torch")
  155.     if (not success) then
  156.         print("Unable to find torch to place.")
  157.         return
  158.     end
  159.     turtle.placeDown()
  160. end
  161.  
  162. --Checks if the inventory is available for more items.
  163. function checkInventory()
  164.     local freeSlots = 0
  165.     for i=1,16 do
  166.         local data = turtle.getItemDetail(i)
  167.         if (data == nil) then
  168.             freeSlots = freeSlots + 1
  169.         end
  170.     end
  171.     if (freeSlots < 2) then
  172.         onFullInventory()
  173.     end
  174. end
  175.  
  176. --Displays current statistics.
  177. function updateStatistics(currentLength, segmentLength)
  178.     print(round((currentLength / segmentLength) * 100, 2) .."% complete.")
  179. end
  180.  
  181. --Main mining program.
  182. function mineShaft(segmentLength, sideLength, sideInterval, torchInterval)
  183.     for i=1,segmentLength do
  184.         checkInventory()
  185.         digForwardSegment(1)
  186.         if ((i % sideInterval == 0) and (sideInterval > 0)) then
  187.             digSideShafts(sideLength)
  188.         end
  189.         if ((i % torchInterval == 0) and (torchInterval > 0)) then
  190.             placeTorch()
  191.         end
  192.         updateStatistics(i, segmentLength)
  193.     end
  194. end
  195.  
  196. --Main program.
  197. parseArgs(args)
  198. ensureFuel(segmentLength, sideLength, sideInterval)
  199. ensureTorches(segmentLength, torchInterval)
  200. mineShaft(segmentLength, sideLength, sideInterval, torchInterval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement