Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Shaft Mining Program
- --Made by Andrew Lalis
- local args = {...}
- local segmentLength = 0
- local sideLength = 0
- local sideInterval = 0
- local torchInterval = 0
- --Parses command line arguments and gets input.
- function parseArgs(args)
- if (args[1] ~= nil) then
- segmentLength = tonumber(args[1])
- else
- print("How long should this mine be?")
- segmentLength = tonumber(read())
- end
- if (args[2] ~= nil) then
- sideLength = tonumber(args[2])
- else
- print("How long should side shafts be?")
- sideLength = tonumber(read())
- end
- if (args[3] ~= nil) then
- sideInterval = tonumber(args[3])
- else
- print("How often should side shafts be dug?")
- sideInterval = tonumber(read())
- end
- if (args[4] ~= nil) then
- torchInterval = tonumber(args[4])
- else
- print("How often should torches be placed?")
- torchInterval = tonumber(read())
- end
- end
- --Rounds a number for display.
- function round(num, places)
- return (string.format("%." .. (places or 0) .. "f", num))
- end
- --Gets the number of items in the turtle's inventory.
- function getItemCount(itemName)
- local count = 0
- for i=1,16 do
- local data = turtle.getItemDetail(i)
- if (data ~= nil and data.name == itemName) then
- count = count + data.count
- end
- end
- return count
- end
- --Selects an item in the inventory, or the first slot if it can't be selected. Returns success.
- function selectItem(itemName)
- for i=1,16 do
- local data = turtle.getItemDetail(i)
- if (data ~= nil and data.name == itemName) then
- turtle.select(i)
- return true
- end
- end
- turtle.select(0)
- return false
- end
- --What to do when the turtle's inventory is too full.
- function onFullInventory()
- print("Inventory is full, please remove the items.")
- local clean = false
- while (not clean) do
- local event = os.pullEvent("turtle_inventory")
- for i=1,16 do
- local data = turtle.getItemDetail(i)
- if (data ~= nil and data.name ~= "minecraft:torch") then
- clean = false
- break
- end
- end
- end
- print("Inventory clean. Continuing.")
- end
- --Interface for loading fuel until the turtle is full.
- function fuelLoader(amount)
- while (turtle.getFuelLevel() < amount) do
- print("Need "..(amount - turtle.getFuelLevel()).." more fuel units.")
- local event = os.pullEvent("turtle_inventory")
- for i=1,16 do
- turtle.select(i)
- turtle.refuel()
- end
- end
- end
- --Ensures the turtle will have enough fuel for a given shaft mine.
- function ensureFuel(segLength, sideLength, sideInterval)
- local fuelUsage = (segLength / sideInterval) * sideLength + segLength
- if (turtle.getFuelLevel() < fuelUsage) then
- print("Not enough fuel to begin.")
- fuelLoader(fuelUsage)
- print("Fuel loaded!")
- end
- end
- --Ensures that the turtle will have enough torches for the shaft.
- function ensureTorches(segmentLength, torchInterval)
- local torchesNeeded = (segmentLength / torchInterval)
- while (getItemCount("minecraft:torch") < torchesNeeded) do
- print("Need "..(torchesNeeded - getItemCount("minecraft:torch")).." more torches.")
- local event = os.pullEvent("turtle_inventory")
- end
- print("Torches loaded!")
- end
- --Moves back by some amount.
- function goBack(length)
- for i=1,length do
- local success = turtle.back()
- while (not success) do
- success = turtle.back()
- end
- end
- end
- --Digs forward for some amount.
- function digForwardSegment(length)
- for i=1,length do
- turtle.dig()
- local success = turtle.forward()
- while (not success) do
- success = turtle.forward()
- end
- turtle.digDown()
- end
- end
- --Digs side shafts on either side of the main shaft.
- function digSideShafts(length)
- turtle.turnLeft()
- digForwardSegment(length)
- goBack(length)
- turtle.turnRight()
- turtle.turnRight()
- digForwardSegment(length)
- goBack(length)
- turtle.turnLeft()
- end
- --Places a torch on the ground.
- function placeTorch()
- local success = selectItem("minecraft:torch")
- if (not success) then
- print("Unable to find torch to place.")
- return
- end
- turtle.placeDown()
- end
- --Checks if the inventory is available for more items.
- function checkInventory()
- local freeSlots = 0
- for i=1,16 do
- local data = turtle.getItemDetail(i)
- if (data == nil) then
- freeSlots = freeSlots + 1
- end
- end
- if (freeSlots < 2) then
- onFullInventory()
- end
- end
- --Displays current statistics.
- function updateStatistics(currentLength, segmentLength)
- print(round((currentLength / segmentLength) * 100, 2) .."% complete.")
- end
- --Main mining program.
- function mineShaft(segmentLength, sideLength, sideInterval, torchInterval)
- for i=1,segmentLength do
- checkInventory()
- digForwardSegment(1)
- if ((i % sideInterval == 0) and (sideInterval > 0)) then
- digSideShafts(sideLength)
- end
- if ((i % torchInterval == 0) and (torchInterval > 0)) then
- placeTorch()
- end
- updateStatistics(i, segmentLength)
- end
- end
- --Main program.
- parseArgs(args)
- ensureFuel(segmentLength, sideLength, sideInterval)
- ensureTorches(segmentLength, torchInterval)
- mineShaft(segmentLength, sideLength, sideInterval, torchInterval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement