Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --constants
- ---CONFIG_constants
- local CONFIG_targetBlock = "minecraft:dirt"
- ---ARGUMENT_CONSTANTs
- local tArgs = { ... }
- local ZONE_DEPTH
- -- local ACCEPTABLE_FUELS
- if #tArgs < 2 then
- local myname = shell.getRunningProgram()
- local b = string.find(myname, "/")
- while b do
- local startindex, endindex = b
- myname = string.sub(endindex)
- end
- print( "Usage:"..myname.." <diameter> <quarry depth>" )
- return
- elseif #tArgs >= 2 then
- ZONE_DEPTH = tonumber ( tArgs[2] )
- end
- local DIAMETER = tonumber( tArgs[1] )
- -- if #tArgs > 2 then
- -- for n=3,#tArgs do
- -- ACCEPTABLE_FUELS = tArgs[2]
- -- else
- -- ACCEPTABLE_FUELS
- -- end
- --variables
- local acceptableFuels = {["minecraft:coal"] = true}
- local nextTurnDirection = right
- local depth = 0 --this keeps track of how far we had descended from our starting position
- local xPos,zPos = 0,0 --these store the turtle's location within its own relativistic world grid
- local xDir,zDir = 0,1 --these are a direction vector that assumes we face positive Z initially
- --(but whether this is true is irrelevant since we don't rely on GPS, etc.
- --We just track our location and direction internally)
- --functions
- local function turnLeft() --this just updates the direction vector
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir --this is pretty smart vector handling, TBH. I might have to remember this...
- end
- local function turnRight() --this just updates the direction vector
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- end
- function refuel( amount ) -- checks if we have enough fuel, and if not, search the inventory for valid fuels
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true -- if fuel requirements are disabled for any reason, this function always returns true
- end
- local needed = amount or (xPos + zPos + depth + 2) -- if amount isn't specified, estimate how much we need to get "home"
- if turtle.getFuelLevel() < needed then
- local fueled = false
- for n=1,16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- local function membershipTest(x, mySet)
- for n=1,#mySet do
- if x == mySet[n] then
- return true
- end
- end
- return false
- end
- if acceptableFuels[turtle.getItemDetail().name] then --I only want to accept certain fuels
- while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
- turtle.refuel(1)
- end
- if turtle.getFuelLevel() >= needed then
- turtle.select(1) -- reset the inventory cursor
- return true
- end
- end
- end
- end
- turtle.select(1)
- return false -- if the fuel requirements could not be met, refuel failed
- end
- return true
- end
- local function collect() --checks if inventory has an open slot
- local bFull = true --assume backpack is full until disproven
- for n=1,16 do
- local nCount = turtle.getItemCount(n)
- if nCount == 0 then --good, we have an empty slot
- bFull = false
- end
- end
- if bFull then
- print( "No empty slots left." )
- return false
- end
- return true
- end
- local function chestIO( _bKeepOneFuelStack ) --adjusted to account for not dropping targetBlock
- print( "Unloading items..." )
- local targetBlockCount = 0
- for n=1,16 do
- local nCount = turtle.getItemCount(n)
- if nCount > 0 then
- turtle.select(n)
- local bDrop = true
- if _bKeepOneFuelStack and turtle.refuel(0) then
- bDrop = false
- _bKeepOneFuelStack = false
- elseif turtle.getItemDetail().name == CONFIG_targetBlock then
- bDrop = false
- targetBlockCount = targetBlockCount + nCount
- end
- if bDrop then
- turtle.drop()
- end
- end
- end
- local function countTargetBlock(threshold)
- local myCount = 0
- for n=1,16 do
- local nCount = turtle.getItemCount(n)
- if nCount > 0 then
- turtle.select(n)
- if turtle.getItemDetail().name == CONFIG_targetBlock then
- myCount = myCount + nCount
- if myCount > threshold then
- return true
- end
- end
- end
- end
- return false
- end
- if targetBlockCount < 512 then
- print ( "Waiting for"..CONFIG_targetBlock.."...")
- while not countTargetBlock(512) do
- os.pullEvent( "turtle_inventory" )
- end
- end
- turtle.select(1)
- end
- function goTo( x, y, z, xd, zd ) --given a location and a current direction vector, go to location
- --while we are too low, forcibly go up (y axis translation part 1)
- while depth > y do
- if turtle.up() then
- depth = depth - 1
- elseif turtle.digUp() or turtle.attackUp() then --if something was in the way[...]
- collect() --update the inventory manager. Isn't functional programming fun?
- else
- sleep( 0.5 )
- end
- end
- --while we are too high, forcibly go down(y axis translation part 2)
- while depth < y do
- if turtle.down() then
- depth = depth + 1
- elseif turtle.digDown() or turtle.attackDown() then --if something was in the way[...]
- --ensure there's still empty space
- collect()
- else
- sleep( 0.5 )
- end
- end
- --x axis translation
- if xPos > x then --if we need to go toward negative X
- while xDir ~= -1 do --... but we aren't facing negative X
- turnLeft() --turn and update the vector
- end
- while xPos > x do --start moving left just like we did with up
- if turtle.forward() then
- xPos = xPos - 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- elseif xPos < x then --same as above, but reversed
- while xDir ~= 1 do
- turnLeft()
- end
- while xPos < x do
- if turtle.forward() then
- xPos = xPos + 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- end
- --z axis translation
- if zPos > z then --same as above, but the other axis (negative Z)
- while zDir ~= -1 do
- turnLeft()
- end
- while zPos > z do
- if turtle.forward() then
- zPos = zPos - 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- elseif zPos < z then --same as above, but reverse (positive Z)
- while zDir ~= 1 do
- turnLeft()
- end
- while zPos < z do
- if turtle.forward() then
- zPos = zPos + 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- end
- --Rotation to match aim vector
- while zDir ~= zd or xDir ~= xd do
- turnLeft()
- end
- end
- local function returnSupplies()
- local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
- print( "Returning to surface..." )
- goTo( 0,0,0,0,-1 )
- chestIO( true )
- local fuelNeeded = 2*(x+y+z) + 1
- if not refuel( fuelNeeded ) then
- print( "Waiting for fuel" )
- while not refuel( fuelNeeded ) do
- os.pullEvent( "turtle_inventory" ) --waits until something modifies the inventory to add enough fuel
- end
- end
- print( "Resuming mining..." )
- goTo( x,y,z,xd,zd )
- end
- local function seekItem(itemID)
- for n=1,16 do
- local nCount = turtle.getItemCount(n)
- if nCount > 0 then
- turtle.select(n)
- if turtle.getItemDetail().name == itemID then
- return n
- end
- end
- end
- --return false --I could just let it return nil but I'd rather not
- end
- local function placeBlock(itemSlot)
- turtle.select(itemSlot)
- while not turtle.placeDown() do --I have the block, place it!
- if not turtle.digDown() then --but smash things if necessary
- turtle.attackDown()
- end
- end
- return true
- end
- local function seekAndPlaceBlock()
- local targetSlot = seekItem(CONFIG_targetBlock)
- if not targetSlot then
- print("out of target block"..targetSlot)
- returnSupplies()
- targetSlot = seekItem(CONFIG_targetBlock)
- end
- if not targetSlot then
- return false
- end
- placeBlock(targetSlot)
- return true
- end
- local function forwardOp()
- if not refuel() then -- only move if we can get back from this location
- print( "Not enough Fuel" )
- returnSupplies()
- end
- --keep attempting to go forward; if unsuccessful, break & attack
- while not turtle.forward() do -- keep trying to move forward (digging or attacking if necessary) until successful
- if turtle.detect() then -- is there a block in front of us?
- if turtle.dig() then
- if not collect() then
- returnSupplies()
- end
- else
- print ("forwardOp aborting!")
- return false -- The block in front of us couldn't be broken
- end
- elseif turtle.attack() then
- if not collect() then --no empty slots
- returnSupplies()
- end
- else
- sleep( 0.5 )
- end
- end
- --update location
- xPos = xPos + xDir -- adjust the turtle's memory of its location by the direction vector
- zPos = zPos + zDir -- see above
- --Place targetBlock below
- if not seekAndPlaceBlock() then
- print("could not place ".. CONFIG_targetBlock..". Is something in the way or did I break?")
- end
- turtle.select(1)
- return true
- end
- local function layerChangeOp()
- if not refuel() then -- only move if we can get back from this location
- print( "Not enough Fuel" )
- returnSupplies()
- end
- --keep attempting to go up; if unsuccessful, break & attack
- while not turtle.up() do -- keep trying to move up (digging or attacking if necessary) until successful
- if turtle.detectUp() then -- is there a block above of us?
- if turtle.digUp() then
- if not collect() then
- returnSupplies()
- end
- else
- print ("layerChangeOp aborting!")
- return false -- The block above us couldn't be broken
- end
- elseif turtle.attackUp() then
- if not collect() then --no empty slots
- returnSupplies()
- end
- else
- sleep( 0.5 )
- end
- end
- depth = depth - 1
- turtle.select(1)
- return true
- end
- local function turnAround(turnDirection)
- if turnDirection == 0 then
- turnLeft()
- if not forwardOp() then
- print ("turnAround aborting!")
- return false
- end
- turnLeft()
- elseif turnDirection == 1 then
- turnRight()
- if not forwardOp() then
- print ("turnAround aborting!")
- return false
- end
- turnRight()
- else
- turnRight()
- if not forwardOp() then
- print ("turnAround aborting!")
- return false
- end
- turnRight()
- end
- return true
- end
- local function doRow(length)
- for m=1,length-1 do --first row gets done by turnaround
- if not forwardOp() then
- print ("doRow aborting!")
- return false
- end
- end
- return true
- end
- local function doLayer(width)--handles doRow and turnAround calls. Three-dimensional pathing calls are handled by doCuboid
- --don't forget to place a block on the first spot, not just after each forwardOp
- if not seekAndPlaceBlock() then
- print("could not place ".. CONFIG_targetBlock..". Is something in the way or did I break?")
- end
- turtle.select(1)
- local directionBit
- for n=1,width-1 do
- directionBit = math.fmod(n,2)
- if not doRow(DIAMETER) then
- print ("doLayer aborting!")
- return false
- end
- if not turnAround(directionBit) then
- print ("doLayer aborting!")
- return false
- end
- end
- if not doRow(DIAMETER) then
- print ("doLayer aborting!")
- return false
- end
- if directionBit == 0 then
- turnRight()
- turnRight()
- else
- turnRight()
- end
- return true
- end
- local function doCuboid(height)
- for p=1,height-1 do
- if not doLayer(DIAMETER) then
- print ("doLayer returned false. doCuboid aborting!")
- return false
- end
- if not layerChangeOp() then
- print ("layerChangeOp returned false. doCuboid aborting!")
- return false
- end
- end
- if not doLayer(DIAMETER) then
- print ("doLayer returned false. doCuboid aborting!")
- return false
- end
- end
- goTo(0, ZONE_DEPTH-1, 0, 0, 1)
- doCuboid(ZONE_DEPTH)
- goTo(0, 0, 0, 0, 1)
Add Comment
Please, Sign In to add comment