Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fuelSlot = 1
- local torchSlot = 2
- local firstItem,lastItem = 3,16
- local currentY = 1
- local currentX = 1
- local currentZ = 2
- local startY = 1
- local startX = 1
- local startZ = 2
- local setting = {}
- setting["tunnelLength"] = 0
- setting["lastX"] = 1
- setting["lastY"] = 2
- setting["lastZ"] = 1
- setting["currentCaveMiddle"] = 2
- setting["lastTorching"] = 0
- setting["currentTunnel"] = 0
- setting["WallEntryWidth"] = 1
- setting["WallScore"] = 0
- local fuelBarrel = nil
- local torchBarrel = nil
- local cobbleBarrel = nil
- local CONFIG_FILE = "miner.json"
- local loadedConfig = nil
- local NORTH = 1
- local EAST = 2
- local SOUTH = 3
- local WEST = 4
- local currentFace = NORTH
- local currentStage = 1
- function placeAll(all)
- local start = firstItem
- if(all) then
- start = 1
- end
- local i = firstItem
- while i <= lastItem do
- turtle.select(i)
- turtle.drop()
- if not turtle.getItemDetail() then
- i = i + 1
- else
- print("Chest is full, stopping")
- exit()
- end
- end
- end
- function checkAndReset()
- local lookingAt = nil
- local itemData = nil
- lookingAt, itemData = turtle.inspect()
- if lookingAt then
- if string.match(itemData.name, "chest") then
- print("Found Chest")
- placeAll(true)
- else
- print("Missing loot Chest")
- return false
- end
- else
- print("Missing loot Chest")
- return false
- end
- setDirection(WEST)
- turtle.forward()
- setDirection(NORTH)
- lookingAt, itemData = turtle.inspect()
- if lookingAt then
- if string.match(itemData.name, "barrel") then
- print("found barrel")
- turtle.select(1)
- turtle.suck()
- item = turtle.getItemDetail()
- turtle.drop()
- if item then
- if string.match(item.name, "coal") then
- fuelBarrel = 1
- elseif string.match(item.name, "torch") then
- torchBarrel = 1
- elseif string.match(item.name, "stone") then
- cobbleBarrel = 1
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- turtle.up()
- lookingAt, itemData = turtle.inspect()
- if lookingAt then
- if string.match(itemData.name, "barrel") then
- print("found barrel")
- turtle.select(1)
- turtle.suck()
- item = turtle.getItemDetail()
- turtle.drop()
- if item then
- if string.match(item.name, "coal") then
- fuelBarrel = 2
- elseif string.match(item.name, "torch") then
- torchBarrel = 2
- elseif string.match(item.name, "stone") then
- cobbleBarrel = 2
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- turtle.up()
- lookingAt, itemData = turtle.inspect()
- if lookingAt then
- if string.match(itemData.name, "barrel") then
- print("found barrel")
- turtle.select(1)
- turtle.suck()
- item = turtle.getItemDetail()
- turtle.drop()
- if item then
- if string.match(item.name, "coal") then
- fuelBarrel = 3
- elseif string.match(item.name, "torch") then
- torchBarrel = 3
- elseif string.match(item.name, "stone") then
- cobbleBarrel = 3
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- else
- print("Missing coal, torch or stone in barrel")
- return false
- end
- turtle.down()
- turtle.down()
- setDirection(EAST)
- turtle.forward()
- setDirection(NORTH)
- return true
- end
- function setDirection(direction)
- local turnAmount = currentFace - direction
- if turnAmount == -1 or turnAmount == 3 then
- turtle.turnRight()
- end
- if turnAmount == 2 or turnAmount == -2 then
- turtle.turnRight()
- turtle.turnRight()
- end
- if turnAmount == 1 or turnAmount == -3 then
- turtle.turnLeft()
- end
- currentFace = direction
- end
- function walkToStart()
- setting.lastY = currentY
- setting.lastX = currentX
- setting.lastZ = currentZ
- WalkToPoint(startY, startX, startZ)
- setDirection(NORTH)
- end
- function startNewCave()
- WalkToPoint(2, startX, setting.currentCaveMiddle)
- setDirection(WEST)
- WalkDirectionForce(WEST)
- WalkDirectionForce(WEST)
- turtle.digUp()
- turtle.digDown()
- WalkDirectionForce(WEST)
- turtle.digUp()
- turtle.digDown()
- WalkDirectionForce(WEST)
- turtle.digUp()
- turtle.digDown()
- WalkDirectionForce(WEST)
- turtle.digUp()
- turtle.digDown()
- WalkDirectionForce(EAST)
- setting.currentCaveMiddle = currentZ
- print(setting.currentCaveMiddle)
- walkToStart()
- setting.lastY = 2
- setting.lastX = startX
- setting.lastZ = setting.currentCaveMiddle
- setting.tunnelLength = 0
- setting.lastTorching = 0
- setting.WallEntryWidth = 3
- end
- function WalkToPoint(height, length, width)
- print("walking to:", height, length, width)
- print("walking from:", currentY, currentX, currentZ)
- if currentX > length then
- while currentX ~= length do
- print("solving lenght")
- if currentX < length then
- setDirection(SOUTH)
- turtle.forward()
- currentX = currentX + 1
- else
- setDirection(NORTH)
- turtle.forward()
- currentX = currentX - 1
- end
- end
- while currentZ ~= width do
- print("solving width")
- if currentZ < width then
- setDirection(WEST)
- turtle.forward()
- currentZ = currentZ + 1
- else
- setDirection(EAST)
- turtle.forward()
- currentZ = currentZ - 1
- end
- end
- while currentY ~= height do
- print("solving height")
- if currentY < height then
- turtle.up()
- currentY = currentY + 1
- else
- turtle.down()
- currentY = currentY - 1
- end
- end
- else
- while currentZ ~= width do
- print("solving width")
- if currentZ < width then
- setDirection(WEST)
- turtle.forward()
- currentZ = currentZ + 1
- else
- setDirection(EAST)
- turtle.forward()
- currentZ = currentZ - 1
- end
- end
- while currentY ~= height do
- print("solving height")
- if currentY < height then
- turtle.up()
- currentY = currentY + 1
- else
- turtle.down()
- currentY = currentY - 1
- end
- end
- while currentX ~= length do
- print("solving lenght")
- if currentX < length then
- setDirection(SOUTH)
- turtle.forward()
- currentX = currentX + 1
- else
- setDirection(NORTH)
- turtle.forward()
- currentX = currentX - 1
- end
- end
- end
- end
- function setupInventory()
- WalkToPoint(torchBarrel, startX, 3)
- setDirection(NORTH)
- turtle.select(1)
- turtle.suck()
- turtle.select(firstItem)
- WalkToPoint(cobbleBarrel, startX, 3)
- turtle.select(2)
- turtle.suck()
- turtle.select(firstItem)
- WalkToPoint(startZ, startX, startZ)
- setDirection(NORTH)
- end
- function IsBlockInFront()
- return turtle.detect()
- end
- function IsBlockOver()
- return turtle.detectUp()
- end
- function IsBlockUnder()
- return turtle.detectDown()
- end
- function isInventoryFull()
- turtle.select(lastItem)
- if turtle.getItemCount() ~= 0 then
- turtle.select(firstItem)
- return true
- end
- turtle.select(firstItem)
- return false
- end
- function WalkDirectionForce(direction)
- setDirection(direction)
- while IsBlockInFront() do
- turtle.dig()
- end
- turtle.forward()
- if direction == NORTH then
- currentX = currentX - 1
- end
- if direction == SOUTH then
- currentX = currentX + 1
- end
- if direction == EAST then
- currentZ = currentZ - 1
- end
- if direction == WEST then
- currentZ = currentZ + 1
- end
- end
- function mine()
- if setting.tunnelLength > 6 then
- walkToStart()
- currentStage = 5
- end
- if isInventoryFull() then
- walkToStart()
- currentStage = 4
- end
- print("WallScore:", setting.WallScore)
- local walkTo = 1
- if setting.WallEntryWidth == 3 then
- local walkTo = 1
- else
- walkTo = 3
- end
- if setting.WallScore >= 7 then
- if IsBlockOver() then
- turtle.digUp()
- end
- setting.WallScore = setting.WallScore + 1
- if IsBlockUnder() then
- turtle.digDown()
- end
- setting.WallScore = setting.WallScore + 1
- WalkDirectionForce(SOUTH)
- setting.WallScore = 1
- setting.tunnelLength = setting.tunnelLength + 1
- setting.WallEntryWidth = walkTo
- return
- end
- if IsBlockOver() then
- turtle.digUp()
- end
- setting.WallScore = setting.WallScore + 1
- if IsBlockUnder() then
- turtle.digDown()
- end
- setting.WallScore = setting.WallScore + 1
- if currentZ - (setting.currentCaveMiddle - 2) == 2 and setting.lastTorching + 5 < setting.tunnelLength then
- turtle.down()
- if not turtle.detectDown() then
- select(2)
- turtle.placeDown()
- end
- turtle.up()
- turtle.select(1)
- turtle.placeDown()
- setting.lastTorching = setting.tunnelLength
- turtle.select(firstItem)
- end
- if(walkTo == 1) then
- WalkDirectionForce(EAST)
- else
- WalkDirectionForce(WEST)
- end
- setting.WallScore = setting.WallScore + 1
- end
- function refuel()
- walkToStart()
- WalkToPoint(fuelBarrel, startX, 3)
- turtle.select(1)
- setDirection(NORTH)
- local hasNotRefueled = true
- while hasNotRefueled do
- turtle.suck()
- if turtle.getItemDetail() then
- turtle.refuel()
- hasNotRefueled = false
- else
- if not turtle.getItemDetail() then
- print("No fuel found")
- sleep(20)
- end
- end
- end
- currentStage = 4
- end
- function updateConfig()
- --loadedConfig = fs.open(CONFIG_FILE, "w")
- --loadedConfig.write(textutils.serialize(setting))
- --loadedConfig.close()
- end
- function Main()
- if fs.exists(CONFIG_FILE) then
- --loadedConfig = fs.open(CONFIG_FILE, "r")
- --setting = textutils.unserialize(loadedConfig.readAll())
- --loadedConfig.close()
- else
- updateConfig()
- end
- print(textutils.serialize(setting))
- if checkAndReset() then
- print("All good")
- else
- print("Fix the setup and restart")
- exit()
- end
- while true do
- if turtle.getFuelLevel() <= currentX * 2 then
- refuel()
- end
- updateConfig()
- if currentStage == 1 then
- setupInventory()
- currentStage = currentStage + 1
- end
- if currentStage == 2 then
- WalkToPoint(setting.lastY, setting.lastX, setting.lastZ)
- currentStage = currentStage + 1
- end
- if currentStage == 3 then -- MINING
- mine()
- end
- if currentStage == 4 then -- BANKING
- placeAll()
- WalkToPoint(setting.lastY, setting.lastX, setting.lastZ)
- currentStage = 3
- end
- if currentStage == 5 then -- Create new tunnel
- startNewCave()
- currentStage = 4
- end
- end
- end
- Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement