Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ENDER_CHEST_SLOT = 2
- local DIG_SLOT = 3
- --save movments
- function move(move, detect, dig, attack)
- if needsPitstop() then
- doPitstop()
- end
- local first = true
- while not move() do
- if detect() then
- turtle.select(DIG_SLOT)
- dig()
- else
- attack()
- end
- if not first then
- sleep(0.5)
- end
- first = false
- end
- end
- function moveForward()
- move(turtle.forward, turtle.detect, turtle.dig, turtle.attack)
- end
- function moveBack()
- turn180()
- moveForward()
- turn180()
- end
- function moveUp()
- move(turtle.up, turtle.detectUp, turtle.digUp, turtle.attackUp)
- end
- function moveDown()
- move(turtle.down, turtle.detectDown, turtle.digDown, turtle.attackDown)
- end
- function turn180()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- --pitstop
- function doPitstop()
- if turtle.detectUp() then
- turtle.select(DIG_SLOT)
- turtle.digUp()
- end
- turtle.select(ENDER_CHEST_SLOT)
- turtle.placeUp()
- local enderChest = peripherals.wrap()
- if not hasSpace() then
- for i = 3, 16, 1 do
- while turtle.getItemCount(i) > 0 do
- local moved = false
- for j = 2, enderChest.getInventorySize(), 1 do
- if enderChest.pullItem("down", i, 64, j) ~= 0 then
- moved = true
- end
- end
- if not moved then
- sleep(2)
- end
- end
- end
- end
- --TODO: add support for normal fuel
- while turtle.getFuelLevel() < 5 do
- local lavaBucket = enderChest.getStackInSlot(1)
- if lavaBucket ~= nil and lavaBucket["id"] == "minecraft:lava_bucket" then
- enderChest.pushItem("down", 1, 64, 1)
- turtle.select(1)
- turtle.refuel()
- enderChest.pullItem("down", 1, 64, 1)
- end
- if turtle.getFuelLevel() < 5 then
- sleep(1)
- end
- end
- turtle.select(ENDER_CHEST_SLOT)
- turtle.digUp()
- end
- function needsPitstop()
- return (not hasSpace()) or turtle.getFuelLevel() < 5
- end
- function hasSpace()
- local count = 0
- for i = 3, 16, 1 do
- if turtle.getItemCount(i) == 0 then
- count = count + 1
- end
- end
- return count > 1
- end
- function readNumber()
- local num = -1
- repeat
- if num ~= -1 then
- print("Text is not a number.")
- end
- num = tonumber(read())
- until num ~= nil
- return num
- end
- --ore stuff
- function isOre()
- local success, data = turtle.inspect()
- return checkOre(success, data)
- end
- function isOreUp()
- local success, data = turtle.inspectUp()
- return checkOre(success, data)
- end
- function isOreDown()
- local success, data = turtle.inspectDown()
- return checkOre(success, data)
- end
- function checkOre(success, data)
- if success then
- local name = data["name"]
- if name ~= nil then
- --Alternative name bypass
- if name == "Forestry:resources" or name == "TConstruct:SearedBrick"
- or name == "denseores:block0" or name == "rftools:dimensionalShardBlock"
- or name == "harvestcraft:salt" then
- return true
- end
- local index = string.find(name, ":")
- if index ~= nil then
- local part = string.lower(string.sub(name, index))
- local isOre = string.find(part, "ore")
- return isOre ~= nil
- end
- end
- end
- return false
- end
- --vein stuff
- function mineVein()
- while true do
- if isOre() then
- veinMoveForward()
- elseif isOreUp() then
- veinMoveUp()
- elseif isOreDown() then
- veinMoveDown()
- else
- local success = false
- for i = 1, 3, 1 do
- veinMoveLeft()
- if isOre() then
- veinMoveForward()
- success = true
- break
- end
- end
- if not success then
- if not popVeinMovment() then
- return
- end
- end
- end
- sleep(0.5)
- end
- end
- function veinMoveForward()
- moveForward()
- pushToVeinStack(1)
- end
- function veinMoveUp()
- moveUp()
- pushToVeinStack(2)
- end
- function veinMoveDown()
- moveDown()
- pushToVeinStack(3)
- end
- function veinMoveLeft()
- turtle.turnLeft()
- pushToVeinStack(4)
- end
- function veinMoveRight()
- turtle.turnRight()
- pushToVeinStack(5)
- end
- function veinMoveBack()
- moveBack()
- pushToVeinStack(6)
- end
- function popVeinMovment()
- local direction = getFromVeinStack()
- if direction == nil then
- return false
- end
- if direction == 1 then
- moveBack()
- elseif direction == 2 then
- moveDown()
- elseif direction == 3 then
- moveUp()
- elseif direction == 4 then
- turtle.turnRight()
- removeLastVeinStack()
- return popVeinMovment()
- elseif direction == 5 then
- turtle.turnLeft()
- removeLastVeinStack()
- return popVeinMovment()
- elseif direction == 6 then
- moveForward()
- elseif direction == 0 then
- removeLastVeinStack()
- return popVeinMovment()
- end
- removeLastVeinStack()
- return true;
- end
- function pushToVeinStack(direction)
- local handle = fs.open("VeinStack.dat", "a")
- handle.writeLine(tostring(direction))
- handle.close()
- end
- function getFromVeinStack()
- local data = getVeinStack()
- if data ~= nil then
- return tonumber(data[#data])
- end
- return nil
- end
- function getVeinStack()
- --optimize vein stack
- local handle = fs.open("VeinStack.dat", "r")
- if handle ~= nil then
- local data = {}
- local index = 0
- repeat
- index = index + 1
- data[index] = handle.readLine()
- until data[index] == nil
- handle.close()
- if #data > 0 then
- data = optimizeVeinStack(data)
- if #data > 0 then
- return data
- end
- end
- end
- return nil
- end
- function optimizeVeinStack(data)
- local lastAction = 0
- local actionCount = 0
- for i = 1, #data, 1 do
- --turn right and then left is somewhat useless
- if data[i] == 4 and lastAction == 5 or
- data[i] == 5 and lastAction == 4 then
- data = moveArrayContent(data, i + 2, 2)
- if #data < 1 then
- break
- end
- end
- if data[i] ~= lastAction then
- lastAction = 0
- actionCount = 0
- end
- if data[i] == 4 then
- lastAction = 4
- actionCount = actionCount + 1
- elseif data[i] == 5 then
- lastAction = 5
- actionCount = actionCount + 1
- end
- if actionCount == 3 then
- local newAction = 4
- if lastAction == 4 then
- newAction = 5
- end
- data = moveArrayContent(data, i + 1, 2)
- data[i - 2] = newAction
- i = i - 1
- end
- end
- return data
- end
- function moveArrayContent(array, startIndex, amount)
- local size = #array
- for i = startIndex, size, 1 do
- array[i - amount] = array[i]
- array[i] = nil
- end
- return array
- end
- function removeLastVeinStack()
- local data = getVeinStack()
- if data ~= nil then
- handle = fs.open("VeinStack.dat", "w")
- for i = 1, #data - 1, 1 do
- handle.writeLine(data[i])
- end
- handle.close()
- end
- end
- mineVein()
- --print("Enter the max depth of the mine.")
- --local depth = readNumber()
- --print("Enter the max width of the mine.")
- --local width = readNumber()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement