Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Utility functions --
- function containsValue (tab, val)
- for index, value in ipairs(tab) do
- if value == val then
- return true
- end
- end
- return false
- end
- function sign(x)
- if x > 0 then
- return 1
- elseif x < 0 then
- return -1
- else
- return 1
- end
- end
- -- Safe Movement Methods --
- function forward()
- while t.detect() do t.dig() end
- attemptForward()
- end
- function attemptForward()
- if w == 0 then
- z = z + 1
- elseif w == 1 then
- x = x - 1
- elseif w == 2 then
- z = z - 1
- elseif w == 3 then
- x = x + 1
- end
- while not t.forward() do t.attack() end
- end
- function up()
- while t.detectUp() do t.digUp() end
- y = y + 1
- while not t.up() do t.attackUp() end
- end
- function down()
- while t.detectDown() do t.digDown() end
- y = y - 1
- while not t.down() do t.attackDown() end
- end
- function turnLeft()
- w = (w-1) % 4
- if handedness == 0 then
- while not t.turnLeft() do end
- else
- while not t.turnRight() do end
- end
- end
- function turnRight()
- w = (w+1) % 4
- if handedness == 0 then
- while not t.turnRight() do end
- else
- while not t.turnLeft() do end
- end
- end
- function selfOrient()
- a, c, b = gps.locate()
- forward()
- x, y, z = gps.locate()
- a = x - a
- b = z - b
- print(a,b)
- if b == 1 then
- w = 0
- elseif a == 1 then
- w = 1
- elseif b == -1 then
- w = 2
- else
- w = 3
- end
- t.back()
- x, y, z = gps.locate()
- print(string.format("Location:%s, %s, %s, %s", x, y, z, w))
- end
- function moveToY(set)
- while y < set do
- up()
- end
- while y > set do
- down()
- end
- end
- -- Mining Operation Methods --
- function dir() -- Mine Relative Coordinate System
- return (w - homeDir) % 4
- end
- function verPos() -- Mine Relative Coordinate System
- if homeDir == 0 then
- return z - homeZ
- elseif homeDir == 1 then
- return x - homeX
- elseif homeDir == 2 then
- return homeZ - z
- elseif homeDir == 3 then
- return homeX - x
- end
- end
- function latPos() -- Mine Relative Coordinate System
- if homeDir == 0 then
- return homeX - x
- elseif homeDir == 1 then
- return z - homeZ
- elseif homeDir == 2 then
- return x - homeX
- elseif homeDir == 3 then
- return homeZ - z
- end
- end
- function returnToCorner()
- while dir() ~= 3 do
- turnLeft()
- end
- while latPos() > 0 do
- forward()
- end
- turnLeft()
- while verPos() > 0 do
- forward()
- end
- turnLeft()
- turnLeft()
- end
- function resupply()
- returnToCorner()
- returnY = y
- moveToY(homeY)
- emptyAndRefuel()
- moveToY(returnY)
- end
- function emptyAndRefuel()
- while dir() ~= 2 do
- turnLeft()
- end
- for i = 1,16 do
- t.select(i)
- if t.getItemCount() > 0 then
- if not t.drop() then
- up()
- success, data = t.inspect()
- while data.name ~= "minecraft:chest" do
- success, data = t.inspect()
- end
- t.drop()
- end
- end
- end
- print("Inventory emptied")
- turnRight()
- t.select(1)
- while t.getFuelLevel() < 15000 and (t.suck() or (t.getFuelLevel() < 600)) do
- t.refuel()
- t.drop()
- end
- print("Refueled")
- turnRight()
- end
- function checkInventory()
- if (t.getItemCount(16) > 0) or (t.getFuelLevel() < (homeY + 2*width + 50)) then
- print("Fuel low or Inventory full, returning to base")
- local returnLat = latPos()
- local returnVer = verPos()
- local returnDir = w
- returnToCorner()
- resupply()
- print("Resuming")
- while verPos() < returnVer do
- forward()
- end
- turnRight()
- while latPos() < returnLat do
- forward()
- end
- while dir() ~= returnDir do
- turnRight()
- end
- end
- end
- function step()
- checkInventory()
- forward()
- success, data = t.inspectUp()
- if not containsValue(unwantedBlocks, data.name) or (mineMode == 1) then
- while t.detectUp() do
- t.digUp()
- t.attackUp()
- end
- end
- success, data = t.inspectDown()
- if not containsValue(unwantedBlocks, data.name) or (mineMode == 1) then
- while t.detectDown() do
- t.digDown()
- t.attackDown()
- end
- end
- print(latPos(), verPos(), dir())
- end
- function layer(size)
- print("Digging layer")
- print(latPos())
- print(verPos())
- print(dir())
- -- Find and return to last location in layer --
- while (not t.detect()) and verPos() < size-1 do
- attemptForward()
- end
- if verPos() == size-1 then
- turnRight()
- while (not t.detect()) and latPos() < size-1 do
- attemptForward()
- end
- if latPos() == size-1 then
- returnToCorner()
- return true
- else
- turnRight()
- while verPos() > 0 do
- step()
- end
- end
- else
- while verPos() < size-1 do
- step()
- end
- end
- -- Dig Layer--
- for i = latPos(),size-2 do
- if dir() == 0 then
- turnRight()
- step()
- turnRight()
- else
- turnLeft()
- step()
- turnLeft()
- end
- for j = 1,size-1 do
- step()
- end
- end
- returnToCorner()
- end
- -- Save File Methods --
- function updateSave()
- s = string.format(":%s:%s:%s:%s:%s:%s:%s:%s:%s:%s", homeX, homeY, homeZ, homeDir, startHeight, stopHeight, width, currentLayer, tostring(mineMode), tostring(handedness))
- print(s)
- saveFile.writeLine(s)
- saveFile.flush()
- end
- function recoverSaveFile()
- local f = fs.open("quarry_status", "r")
- local latestLine = f.readLine()
- local lastLine = latestLine
- while latestLine ~= nil do
- lastLine = latestLine
- latestLine = f.readLine()
- end
- f.close()
- local val = {}
- for gmat in string.gmatch(lastLine, ':%d+') do
- i = string.find(gmat, ':')
- table.insert(val, tonumber(string.sub(gmat, i + 1)))
- end
- homeX, homeY, homeZ, homeDir, startHeight, stopHeight, width, currentLayer, mineMode, handedness = val[1], val[2], val[3], val[4], val[5], val[6], val[7], val[8], val[9], val[10]
- selfOrient()
- print("Save recoverd")
- print(string.format("Home:%s, %s, %s, %s", homeX, homeY, homeZ, homeDir))
- print(string.format("Location:%s, %s, %s, %s", x, y, z, w))
- print("Start: " .. startHeight)
- print("End: " .. stopHeight)
- print("Width: " .. width)
- print("Mine Mode: " .. mineMode)
- saveFile = fs.open("quarry_status", "w")
- updateSave()
- end
- -- Main --
- t = turtle
- unwantedBlocks = {"minecraft:dirt", "minecraft:grass" , "minecraft:stone", "minecraft:cobblestone", "minecraft:sand", "minecraft:sandstone", "minecraft:gravel", "minecraft:mossy_cobblestone", "minecraft:bedrock", "minecraft:netherrack", "minecraft:soul_sand"}
- if fs.exists("startup") then
- fs.delete("startup")
- end
- fs.copy(shell.getRunningProgram(), "startup")
- -- Recover from reboot with SaveFile --
- if not fs.exists("quarry_status") then
- print("Starting Height (Recommended: 5): ")
- startHeight = tonumber(io.read())
- currentLayer = startHeight
- print("Stopping Height (Recommended: 30): ")
- stopHeight = tonumber(io.read())
- print("Quarry width:")
- width = tonumber(io.read())
- print("Input t if all blocks should be mined")
- mineMode = (io.read() == "t") and 1 or 0
- --print("Handedness (Default: R): ")
- handedness = 0 --(io.read() == "l") and 1 or 0
- x, y, z, w, homeDir = 0, 0, 0, 0, 0
- emptyAndRefuel()
- selfOrient()
- homeX, homeY, homeZ, homeDir = x, y, z, w
- saveFile = fs.open("quarry_status", "w")
- updateSave()
- print("Starting quarry")
- moveToY(startHeight)
- else
- recoverSaveFile()
- returnToCorner()
- print("Return to layer at: " .. currentLayer)
- moveToY(currentLayer)
- end
- -- Main Loop --
- for i = y, stopHeight, sign(stopHeight-startHeight)*3 do
- currentLayer = i
- moveToY(currentLayer)
- updateSave()
- layer(width)
- saveFile.close()
- saveFile = fs.open("quarry_status", "w")
- end
- returnToCorner()
- moveToY(homeY)
- resupply()
- fs.delete("quarry_status")
- fs.delete("startup")
- print("Quarry completed!")
Add Comment
Please, Sign In to add comment