Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local SLOT_COUNT = 16
- local args = {...}
- if #args == 0 then
- print("Usage: mine <x> [z] [depth]")
- return
- end --if
- local xmax = tonumber(args[1]) -- width
- local zmax = tonumber(args[2]) -- depth
- local ymax = tonumber(args[3]) -- height
- if xmax == nil or xmax < 0 or zmax == nil or zmax < 0 or ymax == nil or ymax < 0 then
- print("Invalid dimensions")
- return
- end
- local xDir, zDir = 0,1 -- xDir = 0 to start in z axis direction
- --local direction = 0
- local xPos, zPos, yPos = 0,0,0
- DROPPED_ITEMS = {
- "minecraft:stone",
- "minecraft:cobblestone",
- "minecraft:grass",
- "minecraft:dirt",
- "minecraft:gravel",
- "minecraft:flint",
- "minecraft:sand",
- "minecraft:redstone",
- "minecraft:dye"
- }
- local function condense()
- print("Condensing inventory...")
- local x, y, slot
- local condensed_items = 0
- slot = turtle.getSelectedSlot()
- for x = 2, SLOT_COUNT, 1 do
- turtle.select(x)
- for y = 1, x-1, 1 do
- if turtle.getItemCount(x) == 0 then
- break
- end --if
- turtle.transferTo(y)
- end --for
- end --for
- print("Condensing - finished.")
- turtle.select(slot)
- end --function
- --condense()
- local function dropItems()
- condense()
- print("Dropping items...")
- for slot = 1, SLOT_COUNT, 1 do -- numeric exp, generic exp, increment exp
- local item = turtle.getItemDetail(slot)
- if item ~= nil then
- for filterIndex = 1, #DROPPED_ITEMS, 1 do
- if item["name"] == DROPPED_ITEMS[filterIndex] then
- --print("Dropping " .. item["name"])
- turtle.select(slot)
- turtle.drop()
- end --if/else
- end --for
- end --if
- end --for
- end --function
- --dropItems()
- local function getItemIndex(itemName)
- for slot = 1, SLOT_COUNT, 1 do
- local item = turtle.getItemDetail(slot)
- if item ~= nil then
- if item.name == itemName then
- return slot
- end --if
- end --if
- end --for
- end --function
- local function storeItems()
- turtle.select(1)
- turtle.digUp()
- turtle.placeUp()
- -- Chest has been deployed
- for slot = 1, SLOT_COUNT, 1 do
- local item = turtle.getItemDetail(slot)
- if item ~= nil then
- if item.name ~= "minecraft:coal_block" and item.name ~= "minecraft:coal" and item.name ~= "enderstorage:ender_storage" then
- turtle.select(slot)
- turtle.dropUp()
- end --if
- end --if
- end --for
- -- Items have been stored
- turtle.digUp()
- print("Items stored -- condensing...")
- condense()
- end --function
- --storeItems()
- local function checkInventory()
- local invFull = true
- local nTotalItems = 0
- for n = 1, SLOT_COUNT, 1 do
- local nCount = turtle.getItemCount(n)
- if nCount == 0 then
- invFull = false
- end --if
- end --for
- if invFull then
- print("No empty slots left.")
- return false
- else
- print("There is still room in the inventory.")
- end --if/else
- return true
- end --function
- --checkInventory()
- local function refuel()
- while turtle.getFuelLevel() == 0 do
- local bucketIndex
- turtle.select(2)
- turtle.digUp()
- turtle.placeUp()
- -- fuel chest is deployed
- turtle.suckUp()
- bucketIndex = getItemIndex("minecraft:lava_bucket")
- if bucketIndex ~= nil then
- turtle.select(bucketIndex)
- turtle.refuel()
- storeItems()
- break
- end
- end
- turtle.select(1)
- end --function
- refuel()
- --[[
- ========== MOVEMENT FUNCTIONS ==========
- ]]
- local stuck = false
- local function isStuck()
- return stuck
- end --function
- local function forward()
- refuel()
- stuck = false
- while not turtle.forward() do
- if turtle.detect() then
- if not turtle.dig() then
- stuck = true
- return
- end --if
- end --if
- end --while
- xPos = xPos + xDir
- zPos = zPos + zDir
- print("xPos: "..xPos)
- print("zPos: "..zPos)
- end --function
- local function up()
- refuel()
- stuck = false
- while not turtle.up() do
- if turtle.detectUp() then
- if not turtle.digUp() then
- stuck = true
- return
- end --if
- end --if
- end --while
- print("Going up...")
- yPos = yPos + 1
- end --function
- local function down()
- refuel()
- stuck = false
- while not turtle.down() do
- if turtle.detectDown() then
- if not turtle.digDown() then
- stuck = true
- return
- end --if
- end --if
- end --while
- print("Going down...")
- yPos = yPos - 1
- end --function
- local function left(n)
- local x
- for x = 1, n or 1, 1 do
- print("Turning left...")
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- end --for
- end --function
- local function right(n)
- local x
- for x = 1, n or 1, 1 do
- print("Turning right...")
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- end --for
- end --function
- local function turnAround()
- if zDir == 1 then
- right(2)
- else
- left(2)
- end --if/else
- end --function
- --[[
- ========== GOTO FUNCTIONS ==========
- ]]
- local function goTo(x,y,z,xd,zd)
- print("Going...")
- while yPos > y do down() end
- if xPos > x then -- if to the right of x
- while xDir ~= -1 do left() end -- if xDir == 1 (facing right) then left(2)
- while xPos > x do forward() end -- forward until xPos becomes x
- elseif xPos < x then -- if to the left of x
- while xDir ~= 1 do left() end -- while xDir == -1 (facing left) then left(2)
- while xPos < x do forward() end
- end --if/else
- if zPos > z then -- if past z
- while zDir ~= -1 do left() end -- while zDir == 1 (facing forwards) then left(2)
- while zPos > z do forward() end -- forward until zPos becomes z
- elseif zPos < z then -- if before z
- while zDir ~= 1 do left() end -- while zDir == -1 (facing backwards) then left(2)
- while zPos < z do forward() end
- end --if/else
- while yPos < y do up() end
- while zDir ~= zd or xDir ~= xd do left() end
- end --function
- local function returnToOrigin()
- local x, y, z, xd, zd = xPos, yPos, zPos, xDir, zDir
- print("Returning to surface...")
- goTo(0,0,0,0,-1)
- local fuelNeeded = 2*(x + y + z) + 1
- local currentFuelLevel = turtle.getFuelLevel()
- if currentFuelLevel < fuelNeeded then
- refuel()
- storeItems()
- else
- storeItems()
- end --if/else
- print("Resuming mining...")
- goTo(x,y,z,xd,zd)
- end --function
- --returnToOrigin()
- --[[
- ========== MAIN ==========
- ]]
- print("Mining process - initiated.")
- local currFuel
- local done = false
- while not done and not isStuck() do
- currFuel = turtle.getFuelLevel()
- if currFuel == 0 then
- refuel()
- end --if
- turtle.select(1)
- print("Z Direction: "..zDir)
- print("X Direction: "..xDir)
- if zDir == 1 then
- while zPos < zmax - 1 do
- forward()
- if isStuck() then
- done = true
- break
- end --if
- end --while
- elseif zDir == -1 then
- while zPos > 0 do
- forward()
- if isStuck() then
- done = true
- break
- end --if
- end --while
- end --if/else
- -- reached end of z axis
- if done then break end
- if xPos == 0 and zDir == -1 then
- down()
- turnAround()
- elseif xPos == xmax - 1 and zDir == 1 then
- down()
- turnAround()
- end --if/else
- if zPos == zmax - 1 and zDir == 1 then
- right()
- forward()
- right()
- elseif zPos == 0 and zDir == -1 then
- left()
- forward()
- left()
- end --if/else
- if turtle.getItemCount(15) > 0 then
- storeItems()
- end --if
- end --while/MAIN
- goTo(0,0,0,0,-1)
- storeItems()
- goTo(0,0,0,0,1)
- turtle.select(1)
- print("Mining process - completed.")
Add Comment
Please, Sign In to add comment