Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Written by shankofmeat
- local MAX_LENGTH = 128
- local function convertSide(num) --just converts number to side (for the for loops)
- local conversionKey = {"front", "up", "right", "down", "left"}
- return conversionKey[num]
- end
- local function analyze(name, entries) --returns true if something to mine, false if not
- if name and name ~= "nil" then
- for i, v in ipairs(entries) do
- if v == name then
- return true
- end
- end
- return false
- end
- end
- local function check() --checks the sides of the turtle, and returns a data buffer with the name of the block on each side of the turtle
- local statusBuf = {}
- --check front
- local t, data = turtle.inspect()
- if t then statusBuf[#statusBuf+1] = data.name else statusBuf[#statusBuf+1] = "nil" end
- --check up
- t, data = turtle.inspectUp()
- if t then statusBuf[#statusBuf+1] = data.name else statusBuf[#statusBuf+1] = "nil" end
- --check right
- turtle.turnRight()
- t, data = turtle.inspect()
- if t then statusBuf[#statusBuf+1] = data.name else statusBuf[#statusBuf+1] = "nil" end
- --check down
- t, data = turtle.inspectDown()
- if t then statusBuf[#statusBuf+1] = data.name else statusBuf[#statusBuf+1] = "nil" end
- --check left
- turtle.turnRight()
- turtle.turnRight()
- t, data = turtle.inspect()
- if t then statusBuf[#statusBuf+1] = data.name else statusBuf[#statusBuf+1] = "nil" end
- turtle.turnRight()
- return statusBuf
- end
- local function moveByCmd(cmd) --moves the turtle based on a simplistic command name I.E. "left"
- if cmd == "front" then
- turtle.forward()
- elseif cmd == "right" then
- turtle.turnRight()
- elseif cmd == "left" then
- turtle.turnLeft()
- elseif cmd == "up" then
- turtle.up()
- elseif cmd == "down" then
- turtle.down()
- elseif cmd == "back" then
- turtle.turnRight()
- turtle.turnRight()
- turtle.forward()
- turtle.turnRight()
- turtle.turnRight()
- end
- end
- local function removeStone()
- for i = 1, 16 do
- local slotItem = turtle.getItemDetail(i, false)
- if slotItem ~= nil and slotItem.name == "minecraft:cobblestone" then
- turtle.select(i)
- turtle.drop(slotItem.count)
- end
- end
- turtle.select(1)
- end
- local function initalize()
- local mineData = fs.open("minedata.txt", "r")
- local parsedData = {}
- while true do
- local line = mineData.readLine()
- if not line then break end
- parsedData[#parsedData+1] = line
- end
- return parsedData
- end
- local function inversion(movement) --used to invert the latest movement inside of the movement buffer
- local invertedMovements = {["front"] = "back", ["left"] = "right", ["right"] = "left", ["up"] = "down", ["down"] = "up", ["back"] = "front"}
- return invertedMovements[movement]
- end
- local function veinMine(initSide, entries)
- local moveBuf = {}
- if initSide == "right" or initSide == "left" then
- moveByCmd(initSide)
- moveBuf[#moveBuf+1] = initSide
- else
- if initSide == "up" then
- turtle.digUp()
- elseif initSide == "down" then
- turtle.digDown()
- elseif initSide == "front" then
- turtle.dig()
- end
- moveByCmd(initSide)
- moveBuf[#moveBuf+1] = initSide
- end
- local foundBlock = true
- while foundBlock do
- foundBlock = false
- local statusBuf = check()
- for i, v in ipairs(statusBuf) do
- local sideName = convertSide(i)
- if analyze(v, entries) then
- foundBlock = true
- print(sideName)
- if sideName == "up" then
- turtle.digUp()
- elseif sideName == "down" then
- turtle.digDown()
- elseif sideName == "front" then
- turtle.dig()
- end
- moveByCmd(sideName)
- moveBuf[#moveBuf+1] = sideName
- end
- end
- print(textutils.serialise(moveBuf))
- end
- -- Backtracking
- for i = #moveBuf, 1, -1 do
- local statusBuf = check()
- for j = 2, 5 do
- if (analyze(statusBuf[j], entries)) then
- veinMine(convertSide(j), entries)
- end
- end
- moveByCmd(inversion(moveBuf[i]))
- print(inversion(moveBuf[i]) .. " -backtrack")
- end
- end
- local function main() --the entry point of the program
- local currentPos = 1
- local mineData = initalize()
- turtle.dig()
- turtle.forward()
- while (currentPos <= MAX_LENGTH) do
- local statusBuf = check()
- print(textutils.serialise(statusBuf))
- for i = 2, 5 do
- if (analyze(statusBuf[i], mineData)) then
- veinMine(convertSide(i), mineData)
- end
- end
- turtle.dig()
- turtle.forward()
- currentPos = currentPos + 1
- if currentPos % 16 == 0 then removeStone() end
- print("---------------------")
- end
- turtle.turnRight()
- turtle.turnRight()
- for i = 1, MAX_LENGTH do
- turtle.forward()
- end
- turtle.turnRight()
- turtle.turnRight()
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement