Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Local
- local bedrockLayer = 5
- local startingPos
- local startingD = 0
- local currentD = 0
- local currentPos
- local squareSize
- local done = true
- local currentSlot
- local data
- local succes
- local horizontalState = 0
- local i
- local t = peripheral.wrap("left")
- dir = {NORTH=3, EAST=4, SOUTH=1, WEST=2}
- -- Movement functions
- function printAllData()
- print("startingX" .. startingX)
- print("startingY" .. startingY)
- print("startingZ" .. startingZ)
- print("startingD" .. startingD)
- print("currentX" .. currentX)
- print("currentY" .. currentY)
- print("currentZ" .. currentZ)
- print("currentD" .. currentD)
- print("squareSize" .. squareSize)
- end
- function goUp()
- currentY = currentY + 1
- t.digUp()
- turtle.up()
- checkFuelLevel()
- end
- function goDown()
- currentY = currentY - 1
- t.digDown()
- turtle.down()
- checkFuelLevel()
- end
- function goNorth()
- if(currentD == dir.WEST) then
- turtle.turnRight()
- end
- if(currentD == dir.SOUTH) then
- turtle.turnRight()
- turtle.turnRight()
- end
- if(currentD == dir.EAST) then
- turtle.turnLeft()
- end
- currentD = dir.NORTH
- t.dig()
- turtle.forward()
- checkFuelLevel()
- end
- function goSouth()
- if(currentD == dir.EAST) then
- turtle.turnRight()
- end
- if(currentD == dir.NORTH) then
- turtle.turnRight()
- turtle.turnRight()
- end
- if(currentD == dir.WEST) then
- turtle.turnLeft()
- end
- currentD = dir.SOUTH
- t.dig()
- turtle.forward()
- checkFuelLevel()
- end
- function goEast()
- if(currentD == dir.NORTH) then
- turtle.turnRight()
- end
- if(currentD == dir.WEST) then
- turtle.turnRight()
- turtle.turnRight()
- end
- if(currentD == dir.SOUTH) then
- turtle.turnLeft()
- end
- currentD = dir.EAST
- t.dig()
- turtle.forward()
- checkFuelLevel()
- end
- function goWest()
- if(currentD == dir.SOUTH) then
- turtle.turnRight()
- end
- if(currentD == dir.EAST) then
- turtle.turnRight()
- turtle.turnRight()
- end
- if(currentD == dir.North) then
- turtle.turnLeft()
- end
- currentD = dir.WEST
- t.dig()
- turtle.forward()
- checkFuelLevel()
- end
- function goToMiningStart()
- print("goToMiningStart")
- getPosition()
- for i = 0, currentPos.Y - bedrockLayer -1 :
- goDown()
- end
- end
- function goToZero()
- print("goToZero")
- getPosition()
- distanceFromStartingX = currentPos.x - startingPos.x
- distanceFromStartingZ = currentPos.z - startingPos.z
- if(distanceFromStartingX < 0) then
- distanceFromStartingX = math.abs(distanceFromStartingX)
- for i=1,distanceFromStartingX do
- goNorth()
- end
- elseif(distanceFromStartingX > 0) then
- for i=1,distanceFromStartingX do
- goSouth()
- end
- end
- if(distanceFromStartingZ < 0) then
- distanceFromStartingZ = math.abs(distanceFromStartingZ)
- for i=1,distanceFromStartingZ do
- goEast()
- end
- elseif(distanceFromStartingZ > 0) then
- for i=1,distanceFromStartingZ do
- goWest()
- end
- end
- end
- function goToNextSlice()
- print("goToNextSlice")
- goToZero()
- faceNorth()
- goUp()
- goUp()
- end
- function returnToStartPoint()
- print("returnToStartPoint")
- goToZero()
- distanceFromStartingY = currentPos.y - startingPos.y
- if(distanceFromStartingY < 0) then
- for i = 0, math.abs(distanceFromStartingY) do
- goUp()
- end
- else
- for i = 0, math.abs(distanceFromStartingY) do
- goDown()
- end
- end
- end
- function faceNorth()
- if(currentD == dir.WEST) then
- turtle.turnRight()
- end
- if(currentD == dir.SOUTH) then
- turtle.turnRight()
- turtle.turnRight()
- end
- if(currentD == dir.EAST) then
- turtle.turnLeft()
- end
- currentD = dir.NORTH
- end
- function getPosition()
- currentPos = vector.new(gps.locate(2, false))
- end
- function getOrientation()
- loc1 = vector.new(gps.locate(2, false))
- t.dig()
- turtle.forward()
- loc2 = vector.new(gps.locate(2, false))
- heading = loc2 - loc1
- turtle.backward()
- return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))
- end
- -- Check functions
- function checkFuelLevel()
- if(turtle.getFuelLevel() < 80) then
- refuelFromEnderChest()
- end
- end
- function refuelFromEnderChest()
- print("refuelFromEnderChest")
- dumpIntoEnderChest()
- turtle.select(16)
- turtle.place()
- turtle.select(1)
- turtle.suck(32)
- turtle.refuel(32)
- if(turtle.getItemCount(1) ~= 0) then
- turtle.drop()
- end
- t.dig()
- turtle.transferTo(16)
- end
- function isBlockUseful(data)
- if(data.name == "minecraft:stone" or data.name == "minecraft:dirt") then
- return false
- else
- return true
- end
- end
- function checkAvailableInventory()
- for i = 1, 14 do
- if(turtle.getItemCount(i) == 0) then
- return i
- end
- end
- return -1
- end
- function dumpIntoEnderChest()
- print("dumpIntoEnderChest")
- t.dig()
- turtle.select(15)
- turtle.place()
- for i = 1,14 do
- turtle.select(i)
- if(turtle.getItemCount(i) ~= 0) then
- done = turtle.drop()
- end
- while(done == false) do
- print("enter while")
- sleep(10)
- done = turtle.drop()
- end
- done = true
- end
- turtle.select(1)
- t.dig()
- turtle.transferTo(15)
- end
- function changeHorizontalState()
- if(horizontalState == 0) then
- horizontalState = 1
- else
- horizontalState = 0
- end
- end
- -- Mining function
- -- Direction code : Front = 0, Under = 1, Above = 2
- function mineDirection(direc)
- if(direc == 0 ) then
- succes, data = turtle.inspect()
- if(succes == true and isBlockUseful(data) == true) then
- if(checkAvailableInventory() == -1) then
- dumpIntoEnderChest()
- end
- t.dig()
- end
- end
- if(direc == 1 ) then
- succes, data = turtle.inspectDown()
- if(succes == true and isBlockUseful(data) == true) then
- if(checkAvailableInventory() == -1) then
- dumpIntoEnderChest()
- end
- t.digDown()
- end
- end
- if(direc == 2 ) then
- succes, data = turtle.inspectUp()
- if(succes == true and isBlockUseful(data) == true) then
- if(checkAvailableInventory() == -1) then
- dumpIntoEnderChest()
- end
- t.digUp()
- end
- end
- end
- function mine()
- turtle.turnLeft()
- mineDirection(0)
- turtle.turnRight()
- mineDirection(0)
- turtle.turnRight()
- mineDirection(0)
- turtle.turnLeft()
- mineDirection(2)
- end
- function doVerticalLine()
- if(currentD == 0) then
- print("toto")
- while(currentX < squareSize) do
- mine()
- goNorth()
- end
- mine()
- goEast()
- end
- if (currentD == 2) then
- while(currentX > 0) do
- mine()
- goSouth()
- end
- mine()
- goEast()
- end
- end
- function doHorizontalLine()
- if (currentD == 1) then
- for i = 0, 1 do
- mine()
- goEast()
- end
- mine()
- if(horizontalState == 0 ) then
- goSouth()
- changeHorizontalState()
- else
- goNorth()
- changeHorizontalState()
- end
- end
- end
- function doSlice()
- print("doSlice")
- printAllData()
- while(currentX <= squareSize and currentZ <= squareSize) do
- doVerticalLine()
- doHorizontalLine()
- end
- end
- function mineAll()
- goToMiningStart()
- doSlice()
- print("before while")
- while(currentY < 30) do
- print("entered while")
- goToNextSlice()
- doSlice()
- end
- dumpIntoEnderChest()
- returnToStartPoint()
- end
- -- Main
- local args = { ... }
- local canRun = false
- print("Please input two argument, square size and current turtle height ")
- if (#args <= 1 or #args > 2) then
- print("Please input two argument, square size, current turtle height and turtle facing direction")
- elseif(#args == 2) then
- print("Detected 2 arguments")
- squareSize = tonumber(args[1])
- startingY = tonumber(args[2])
- currentY = startingY
- canRun = true
- end
- if (canRun == true) then
- print("Mining started")
- mineAll()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement