Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- -- Config to determine what the turtle should mine
- -- Function calls declared early. We need this as they are inter dependant
- local digVeinUp = nil
- local digVein = nil
- local digVeinDown = nil
- local mineVein = nil
- local throwaways = {
- "stone",
- "dirt",
- "gravel"
- }
- local config = {
- ["diamond"] = true,
- ["iron ore"] = true,
- ["misc"] = false,
- }
- local oppositeInstructions = {
- ["f"] = "b",
- ["b"] = "f",
- ["l"] = "r",
- ["r"] = "l",
- ["u"] = "d",
- ["d"] = "u"
- }
- local minFuelLevel = 200
- local torchSlot = 1
- local fuelSlot = 2
- local chestSlot = 3
- local logSteps = false
- local backtrack = {}
- local function initialize()
- print("Starting turtle...")
- print("Torches set to slot "..torchSlot)
- print("Fuel set to slot "..fuelSlot)
- end
- local function shouldDrop(name)
- for k,v in pairs(throwaways) do
- if string.find(v, name) then
- return true
- end
- end
- return false
- end
- local function dropShit()
- for i = 1, 16 do
- turtle.select(i)
- local slotData = turtle.getItemDetail(i)
- if slotData and shouldDrop(slotData.name) then
- turtle.drop()
- end
- end
- end
- local function log(instruction)
- if not logSteps then
- return
- end
- local instructionCount = #backtrack
- local lastInstruction = backtrack[instructionCount]
- if (instruction == "f" and lastInstruction == "b") or
- (instruction == "b" and lastInstruction == "f") or
- (instruction == "l" and lastInstruction == "r") or
- (instruction == "r" and lastInstruction == "l") or
- (instruction == "u" and lastInstruction == "d") or
- (instruction == "d" and lastInstruction == "u") then
- table.remove(lastInstruction, instructionCount)
- else
- table.insert(backtrack, instruction)
- end
- end
- local function isOre(name)
- return string.find(name, "_ore")
- end
- local function digUp()
- while turtle.detectUp() do
- turtle.digUp()
- end
- end
- local function digDown()
- while turtle.detectDown() do
- turtle.digDown()
- end
- end
- local function dig()
- while turtle.detect() do
- turtle.dig()
- end
- end
- local function forward()
- while not turtle.forward() do
- dig()
- end
- log("f")
- end
- local function up()
- while not turtle.up() do
- digUp()
- end
- log("u")
- end
- local function down()
- while not turtle.down() do
- digDown()
- end
- log("d")
- end
- local function back()
- turtle.back()
- log("b")
- end
- local function executeInstruction(instruction)
- if instruction == "f" then forward()
- elseif instruction == "b" then back()
- elseif instruction == "u" then up()
- elseif instruction == "d" then down()
- elseif instruction == "l" then turnLeft()
- elseif instruction == "r" then turnRight()
- end
- end
- local function doBacktrack()
- local isLogging = logSteps
- logSteps = false
- local currentInstruction = table.remove(backtrack)
- while currentInstruction do
- executeInstruction(oppositeInstructions[currentInstruction])
- currentInstruction = table.remove(backtrack)
- end
- logSteps = isLogging
- end
- local function mineVein()
- local backtrackCache = {}
- for k,v in pairs(backtrack) do
- table.insert(backtrackCache, v)
- end
- backtrack = {}
- -- Store the number of things in the backtrack list so
- -- we can come back to the turtle's position later
- local backtrackListLength = #backtrack
- for i = 1, 4 do
- turtle.turnRight()
- local success, infront = turtle.inspect()
- if success and isOre(infront.name) then
- digVein()
- doBacktrack()
- end
- end
- local success, up = turtle.inspectUp()
- if success and isOre(up.name) then
- digVeinUp()
- doBacktrack()
- end
- local success, down = turtle.inspectDown()
- if success and isOre(down.name) then
- digVeinDown()
- doBacktrack()
- end
- backtrack = backtrackCache
- end
- digVeinUp = function(oreOnly)
- oreOnly = oreOnly or false
- local isRoot = not logSteps
- local success, inspect = turtle.inspectUp()
- if not success then
- return
- end
- -- If it's not an ore then just mine it anyway and continue
- if not isOre(inspect.name) then
- if not oreOnly then
- digUp()
- end
- return
- end
- logSteps = true
- up()
- mineVein()
- if isRoot then
- logSteps = false
- down()
- backtrack = {}
- end
- end
- digVein = function(oreOnly)
- oreOnly = oreOnly or false
- local isRoot = not logSteps
- local success, inspect = turtle.inspect()
- if not success then
- return
- end
- if not isOre(inspect.name) then
- if not oreOnly then
- dig()
- end
- return
- end
- logSteps = true
- forward()
- mineVein()
- if isRoot then
- logSteps = false
- back()
- backtrack = {}
- end
- end
- digVeinDown = function(oreOnly)
- oreOnly = oreOnly or false
- local isRoot = not logSteps
- local success, inspect = turtle.inspectDown()
- if not success then
- return
- end
- if not isOre(inspect.name) then
- if not oreOnly then
- digDown()
- end
- return
- end
- logSteps = true
- down()
- mineVein()
- if isRoot then
- logSteps = false
- up()
- backtrack = {}
- end
- end
- local function aboutFace()
- turtle.turnLeft()
- turtle.turnLeft()
- log("l")
- log("l")
- end
- local function left()
- turtle.turnLeft()
- log("l")
- end
- local function right()
- turtle.turnRight()
- log("r")
- end
- local function checkFuelLevel()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel < minFuelLevel then
- turtle.select(fuelSlot)
- turtle.refuel(1)
- print("Refuelling... fuel level is now "..turtle.getFuelLevel())
- end
- end
- local function placeChest()
- -- Place a chest behind the turtle
- turtle.select(chestSlot)
- up()
- digVein()
- down()
- digVein()
- while not turtle.place() do turtle.dig() end
- for i = 4, 16 do
- turtle.select(i)
- turtle.drop()
- end
- end
- local function mineAboutMine()
- digVein()
- left()
- left()
- digVein()
- end
- local function doStripMine()
- local stripLength = 80
- checkFuelLevel()
- ------------------------------------
- -- STARTING FROM MIDDLE, GO UP THEN MINE DOWN
- ------------------------------------
- up()
- local mf = 0
- for i=1, stripLength / 2 do
- if i % 2 == 0 then
- dropShit()
- end
- checkFuelLevel()
- -- Mine top layer, finish looking right
- turtle.turnLeft()
- digVein()
- aboutFace()
- digVein()
- -- Scan above for ore
- digVeinUp(true)
- if (i % 6) == 0 then
- turtle.select(torchSlot)
- turtle.place()
- end
- -- Mine middle layer, finish looking left
- down()
- digVein()
- aboutFace()
- dig()
- -- Mine bottom layer, finish looking right
- down()
- digVein()
- aboutFace()
- digVein()
- -- Scan below for ore
- digVeinDown(true)
- -- Go forwards to start mining the next layer
- turtle.turnLeft()
- forward()
- mf = mf + 1
- ------------------------------------
- -- STARTING FROM BOTTOM, MINE UP TO THE TOP
- ------------------------------------
- -- Mine bottom layer, finish looking right
- turtle.turnLeft()
- digVein()
- aboutFace()
- digVein()
- -- Scan below for ore
- digVeinDown(true)
- -- Mine middle layer, finish looking left
- up()
- digVein()
- aboutFace()
- digVein()
- -- Mine top layer, finish looking forward
- up()
- digVein()
- aboutFace()
- digVein()
- turtle.turnLeft()
- -- Scan above for ore
- digVeinUp(true)
- forward()
- mf = mf + 1
- end
- down()
- turtle.turnLeft()
- turtle.turnLeft()
- for i=1, mf do
- forward()
- end
- placeChest()
- end
- for i=1, 10 do
- doStripMine()
- turtle.turnLeft()
- for j=1, 5 do
- forward()
- digUp()
- digDown()
- end
- turtle.turnLeft()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement