Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- quarrySize = 0
- minFuelLevel = 1000
- fuelUsed = 0
- x = 0 --Standing in positive X direction when starting
- y = 0
- z = 0
- direction = 0
- local goHome
- function printPosition()
- --print("X: " .. x .. ", Y: " .. y .. ", Z: " .. z .. ", Direction: " .. direction)
- end
- function checkFullInventory()
- for i = 1, 16, 1 do
- if turtle.getItemCount(i) == 0 then
- return false
- end
- end
- return true
- end
- function dig(careAboutInventory)
- if careAboutInventory == nil then
- careAboutInventory = true
- end
- if careAboutInventory then
- if not checkFullInventory() then
- turtle.dig()
- else
- goHome()
- end
- else
- turtle.dig()
- end
- end
- function digDown(careAboutInventory)
- if careAboutInventory == nil then
- careAboutInventory = true
- end
- if careAboutInventory then
- if not checkFullInventory() then
- turtle.digDown()
- else
- goHome()
- end
- else
- turtle.digDown()
- end
- end
- function digUp(careAboutInventory)
- if careAboutInventory == nil then
- careAboutInventory = true
- end
- if careAboutInventory then
- if not checkFullInventory() then
- turtle.digUp()
- else
- goHome()
- end
- else
- turtle.digUp()
- end
- end
- function forward(steps, careAboutInventory)
- if careAboutInventory == nil then
- careAboutInventory = true
- end
- if steps == nil then
- steps = 1
- end
- for i = 1, steps, 1 do
- while not turtle.forward() do
- turtle.attack()
- dig(careAboutInventory)
- end
- fuelUsed = fuelUsed + 1
- --Update coordinates
- if direction == 0 then
- x = x + 1
- elseif direction == 1 then
- z = z + 1
- elseif direction == 2 then
- x = x - 1
- elseif direction == 3 then
- z = z - 1
- end
- printPosition()
- end
- end
- function up(steps, careAboutInventory)
- if careAboutInventory == nil then
- careAboutInventory = true
- end
- if steps == nil then
- steps = 1
- end
- for i = 1, steps, 1 do
- while not turtle.up() do
- turtle.attackUp()
- digUp(careAboutInventory)
- end
- fuelUsed = fuelUsed + 1
- y = y + 1
- printPosition()
- end
- end
- function down(steps, careAboutInventory)
- if careAboutInventory == nil then
- careAboutInventory = true
- end
- if steps == nil then
- steps = 1
- end
- for i = 1, steps, 1 do
- while not turtle.down() do
- turtle.attackDown()
- digDown(careAboutInventory)
- end
- fuelUsed = fuelUsed + 1
- y = y - 1
- printPosition()
- end
- end
- function updateDirection(amount)
- direction = direction + amount
- direction = direction % 4
- end
- function turnLeft(steps)
- if steps == nil then
- steps = 1
- end
- for i = 1, steps, 1 do
- turtle.turnLeft()
- updateDirection(-1)
- end
- end
- function turnRight(steps)
- if steps == nil then
- steps = 1
- end
- for i = 1, steps, 1 do
- turtle.turnRight()
- updateDirection(1)
- end
- end
- function turnTo(dir)
- turnLeft((direction - dir) % 4 )
- end
- function unloadInventory()
- for i = 1, 16, 1 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- end
- function waitForFuel()
- print("Waiting for fuel..")
- while true do
- fuel = turtle.getFuelLevel()
- print(fuel .. " of " .. minFuelLevel .. " fuel.")
- if fuel >= minFuelLevel then
- return
- end
- sleep(5)
- end
- end
- function goHome()
- print("Going home...")
- resumeAtX = x
- resumeAtY = y
- resumeAtZ = z
- resumeDirection = direction
- --Go to the home position
- up(-y, false)
- turnTo(2) --X-axis
- forward(x, false)
- turnTo(3) --Z-axis
- forward(z, false)
- print(fuelUsed .. " fuel used.")
- --Unload
- turnTo(2) --Face the chest
- unloadInventory()
- turnTo(0) --Face the quarry again
- waitForFuel()
- --Return where it left off
- turnTo(0) --X-axis
- forward(resumeAtX, false)
- turnTo(1) --Z-axis
- forward(resumeAtZ, false)
- down(-resumeAtY, false)
- turnTo(resumeDirection)
- end
- function mine()
- digUp()
- digDown()
- dig()
- end
- function checkArguments(args)
- if #args == 0 then
- print("You have to supply an argument for quarry size.")
- return false
- end
- quarrySize = tonumber(args[1])
- return true
- end
- function nextRow(left)
- if left then
- turnLeft()
- else
- turnRight()
- end
- mine()
- forward()
- if left then
- turnLeft()
- else
- turnRight()
- end
- end
- function mineLayer()
- left = false
- for i=1, quarrySize, 1 do
- --For each row
- for j=2, quarrySize, 1 do
- mine()
- forward()
- end
- if i ~= quarrySize then
- nextRow(left)
- left = not left
- else
- if evenSize then
- turnRight()
- else
- turnLeft(2)
- end
- end
- mine()
- end
- end
- if not checkArguments({...}) then
- return --Exit the program
- end
- done = false
- evenSize = (quarrySize % 2) == 0
- waitForFuel()
- down(2)
- while true do
- mineLayer()
- down(3)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement