Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- arg 1 = y start
- -- arg 2 =
- -- arg 3 = "manual" / boolean for ender chest eller normal chest
- term.clear()
- term.setCursorPos(1,1)
- local area = 16
- local turtleId = os.getComputerLabel()
- local INVENTORY_SIZE = 16
- local isRunning
- local isModem
- local isEnderchest
- local args = {...}
- local bot = {}
- bot.yStart = 0
- bot.currY = 0
- bot.currX = 1
- bot.currZ = 1
- bot.direction = 1
- bot.currLayer = 0
- bot.valuable = 0
- bot.resumeY = 0
- bot.resumeX = 0
- bot.resumeZ = 0
- bot.resumeDirection = 1
- bot.isRunning = false
- bot.isModem = false
- bot.isEnderchest = false
- bot.tilesToMine = 0
- bot.tilesMined = 0
- local mine_length
- local mine_width
- -------------------------------
- if peripheral.isPresent("right") then
- bot.isModem = true
- end
- if #arg == 3 then
- bot.yStart = tonumber(arg[1])
- bot.currY = tonumber(arg[1])
- mine_length = tonumber(arg[2])
- mine_width = tonumber(arg[3])
- if mine_width % 2 ~= 0 or mine_length % 2 ~= 0 then
- print("Both the height and width arguments must be an even number")
- return
- elseif mine_width == 1 or mine_length == 1 then
- print("Both the height and width arguments must be greater and 1")
- return
- end
- else
- print("Please enter the correct arguments when executing this script. The turtle's current Y height, width and length of mine are required (e.g. mining 64 16 16)")
- return
- end
- -------------------------------
- local function textOutput(output_message, x_screen_pos, z_screen_pos, clear_area)
- term.setCursorPos(x_screen_pos,z_screen_pos)
- if clear_area == 0 then
- clear_area = string.len(output_message)
- end
- write(output_message..string.rep(" ", (clear_area - string.len(output_message))))
- end
- local function textInput()
- end
- local function printPos()
- term.clear()
- term.setCursorPos(1,1)
- print("------------")
- print("x"..bot.currX)
- print("z"..bot.currZ)
- print("y"..bot.currY)
- print("direction "..bot.direction)
- print("layer "..bot.currLayer)
- print("------------")
- end
- -- ## WRITE FILE ##
- local function savePos()
- handle = fs.open("enderMine/savedFile", "w")
- handle.write(textutils.serialize(bot))
- handle.close()
- end
- local function loadPos()
- handle = fs.open("enderMine/savedFile", "r")
- readFile = handle.readLine()
- if readFile ~= nil then
- bot = textutils.unserialize(readFile)
- end
- handle.close()
- end
- local function sendBroadcast(message)
- if bot.isModem == true then
- rednet.open("right")
- rednet.broadcast("["..turtleId.."] "..message)
- end
- end
- function refuel(slot_number)
- print("[TURTLE] Refueling... ")
- turtle.select(slot_number)
- turtle.refuel()
- print("[TURTLE] Nom. Nom. Nom.")
- end
- local function checkFuel()
- layers = math.floor(bot.yStart / 4)
- -- mass = area * area * layers
- mass = mine_length * mine_width * layers
- fuel = turtle.getFuelLevel()
- if fuel < mass then
- term.clear()
- term.setCursorPos(1,1)
- print("Not enought fuel")
- print("Fuel needed: "..mass)
- print("Current fuel: "..fuel)
- sleep(1)
- print("")
- print("Place fuel in inventory and press a key to continue")
- event = os.pullEvent("key")
- for i = 1, INVENTORY_SIZE do
- local currentItem = turtle.getItemDetail(i)
- if currentItem ~= nil then
- refuel(i)
- end
- end
- end
- end
- local function inventorySort()
- for i = 1, INVENTORY_SIZE do
- local currentItem = turtle.getItemDetail(i)
- if currentItem ~= nil then
- turtle.select(i)
- for j = i, INVENTORY_SIZE do
- if turtle.compareTo(j) then
- turtle.select(j)
- turtle.transferTo(i)
- turtle.select(i)
- end
- end
- end
- end
- end
- local function moveForward()
- while not turtle.forward() do
- turtle.dig()
- end
- savePos()
- printPos()
- end
- local function moveUp()
- while not turtle.up() do
- turtle.digUp()
- end
- bot.currY = bot.currY+1
- savePos()
- printPos()
- end
- local function moveDown()
- while not turtle.down() do
- turtle.digDown()
- end
- bot.currY = bot.currY-1
- savePos()
- printPos()
- end
- local function turnRight()
- turtle.turnRight()
- bot.direction = bot.direction +1
- if bot.direction > 4 then
- bot.direction = 1
- end
- savePos()
- printPos()
- end
- local function turnLeft()
- turtle.turnLeft()
- bot.direction = bot.direction -1
- if bot.direction < 1 then
- bot.direction = 4
- end
- savePos()
- printPos()
- end
- local function gotoShaft() -- finder punktet hvor den gik ned "skakten"
- while bot.direction ~= 3 do
- turnRight()
- end
- while bot.currX > 1 do
- moveForward()
- bot.currX = bot.currX -1
- savePos()
- end
- while bot.direction ~= 4 do
- turnRight()
- end
- while bot.currZ > 1 do
- moveForward()
- bot.currZ = bot.currZ -1
- savePos()
- end
- while bot.direction ~= 1 do
- turnRight()
- end
- end
- --#### COMPARE UP ####
- local function compareUp()
- for i = 1, 3 do
- turtle.select(i)
- turtle.compareUp()
- if turtle.compareUp() == true then
- notinuse = 1
- else
- turtle.digUp()
- bot.valuable = bot.valuable +1
- end
- end
- end
- --#### COMPARE DOWN ####
- local function compareDown()
- for i = 1, 3 do
- turtle.select(i)
- turtle.compareDown()
- if turtle.compareDown() == true then
- notinuse = 1
- else
- turtle.digDown()
- bot.valuable = bot.valuable +1
- end
- end
- end
- local function dropOff() -- vender tilbage til overfladen og smider alt af
- stone = turtle.getItemCount(4)
- turtle.select(4)
- turtle.drop(stone-1)
- gotoShaft()
- bot.resumeY = tonumber(bot.currY)
- toSurface()
- turnRight()
- turnRight()
- for i = 5, 15 do
- turtle.select(i)
- turtle.drop()
- end
- turnRight()
- turnRight()
- while bot.currY > bot.resumeY do
- moveDown()
- printPos()
- savePos()
- end
- end
- local function dropCobble() -- dropper alt cobble stone i slot 4
- local cobble = turtle.getItemCount(4)
- if cobble ~= 0 then
- turtle.select(4)
- turtle.drop(cobble-1)
- end
- end
- local function mineRowXPlus() -- miner en række frem
- while bot.currX < mine_length do
- moveForward()
- bot.currX = bot.currX +1
- bot.tilesMined = bot.tilesMined +1
- savePos()
- printPos()
- compareUp()
- compareDown()
- end
- turnRight()
- moveForward()
- bot.currZ = bot.currZ +1
- savePos()
- compareUp()
- compareDown()
- turnRight()
- end
- local function mineRowXMinus() -- mine en række tilbage
- while bot.currX > 1 do
- moveForward()
- bot.currX = bot.currX -1
- bot.tilesMined = bot.tilesMined +1
- savePos()
- printPos()
- compareUp()
- compareDown()
- end
- turnLeft()
- moveForward()
- bot.currZ = bot.currZ +1
- savePos()
- compareUp()
- compareDown()
- turnLeft()
- end
- local function mineLayer() -- miner et lag og dropper det af
- while bot.currZ < mine_width do
- mineRowXPlus()
- mineRowXMinus()
- dropCobble()
- inventorySort()
- end
- gotoShaft()
- dropOff()
- end
- local function newLayer()
- for i = 1, 3 do
- moveUp()
- printPos()
- end
- bot.currLayer = bot.currLayer + 1
- savePos()
- sendBroadcast("Starting layer number: "..bot.currLayer..", Y:"..bot.currY)
- sendBroadcast("current valuables: "..bot.valuable)
- end
- local function toBedrock()
- local mineStart = bot.yStart - 6
- for i = 1, mineStart do
- moveDown()
- end
- bot.currLayer = 1
- savePos()
- sendBroadcast("Bottom reached")
- printPos()
- end
- function toSurface()
- gotoShaft()
- sendBroadcast("Going up")
- --for i = 1, bot.yStart - bot.currY do
- while tonumber(bot.currY) < tonumber(bot.yStart) do
- moveUp()
- end
- --if tonumber(bot.currY) > tonumber(bot.yStart) then
- while tonumber(bot.currY) > tonumber(bot.yStart) do
- turtle.down()
- bot.currY = bot.currY -1
- savePos()
- printPos()
- end
- bot.status = "off"
- bot.isRunning = false
- end
- local function resume()
- gotoShaft()
- sendBroadcast("Resuming, Y:"..bot.currY)
- turtle.select(16)
- turtle.dig()
- layersToGo = math.floor((bot.yStart - bot.currY)/4)
- for i = 1, layersToGo do
- mineLayer()
- newLayer()
- if bot.currY > 50 then
- break
- end
- end
- toSurface()
- end
- --------------- LOOP
- local function startMining()
- if bot.isRunning == true then
- loadPos()
- gotoShaft()
- resume()
- bot.status = "off"
- os.reboot()
- else
- checkFuel()
- bot.isRunning = true
- if turtle.getItemCount(15) ~= 0 then -- ender chest
- isEnderchest = true
- else -- stationær chest
- isEnderchest = false
- end
- savePos()
- bot.status = "on"
- layers = math.floor(bot.yStart / 4)
- bot.currY = bot.yStart
- toBedrock()
- for i = 1, layers do
- mineLayer()
- newLayer()
- if bot.currY > 50 then
- break
- end
- end
- bot.status = "off"
- toSurface()
- end
- end
- startMining()
Add Comment
Please, Sign In to add comment