Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- This program contains mining functions.
- version 1.1
- --]]
- -- Enumeration to store the the different types of message that can be written
- MessageLevel = { DEBUG=0, INFO=1, WARNING=2, ERROR=3, FATAL=4 }
- local messageOutputLevel = MessageLevel.INFO
- -- Strata dimensions (X is on the RIGHT side; Z is in front)
- local STRATA_Z_FRONT = 10
- local STRATA_X_RIGHT = 20
- -- Slots (needs to be initialized by each command)
- local Slots
- -- Fuel needed for mining a full strata
- local FUEL_NEEDED_STRATA = 4000
- -- Fuel required at fuel check
- local FUEL_NEEDED_AT = 4000
- -- MAX Nb Tries to move
- local MNB_FAILED_MOVE = 35
- -- LAST SLOT (must remain empty)
- local SLOT_LAST = 16
- -- allows disabling inventory check during
- -- making the shaft and transport runs
- local doInventoryCheck = true
- local FILENAME_MESSAGES = "messages.txt"
- local FILENAME_POSITION = "position.txt"
- -- File run at startup
- local FILENAME_STARTUP = "startup"
- -- File identifying the bot was rebooted prematurely
- -- (chunk unloaded, etc.)
- local FILENAME_REBOOT = "reboot_token"
- -- File identifying the strata mined
- local FILENAME_STRATA = "strata.txt"
- -- HOME position
- local posHOME = { x = 0, y = 0, z = 0}
- -- Variables to store the current _relative_ position of the turtle
- local posCurr = { x = 0, y = 0, z = 0 }
- -- movement vectors
- -- We use a relative x,y,z coordinate system but the axes
- -- do NOT align with absolute minecraft axes and positions.
- -- These vectors are based on the starting position
- -- and orientation of the turtle.
- local vecMOVEMENT = {
- FRONT1 = { x=0, y=0, z = -1},
- FRONT2 = { x=0, y=0, z = -2},
- FRONT3 = { x=0, y=0, z = -3},
- BACK1 = { x=0, y=0, z = 1},
- BACK2 = { x=0, y=0, z = 2},
- BACK3 = { x=0, y=0, z = 3},
- RIGHT1 = { x = 1, y=0, z=0},
- RIGHT2 = { x = 2, y=0, z=0},
- RIGHT3 = { x = 3, y=0, z=0},
- LEFT1 = { x = -1, y=0, z=0},
- LEFT2 = { x = -2, y=0, z=0},
- LEFT3 = { x = -3, y=0, z=0},
- UP1 = { x=0, y = 1, z=0},
- UP2 = { x=0, y = 2, z=0},
- UP3 = { x=0, y = 3, z=0},
- DOWN1 = { x=0, y = -1, z=0},
- }
- -- Orientation enum (clockwise)
- local Orientation = { FRONT=0, RIGHT=1, BACK=2, LEFT=3 }
- local NB_ORIENTATIONS = 4
- -- Variables to store the current cardinal orientation of the turtle
- local orientationCurr = Orientation.FRONT
- local vecForwardMovement1 = getMovementVector1(orientationCurr)
- -- ********************************************************************************** --
- -- Returns the movement vector1 of the given Orientation
- -- ********************************************************************************** --
- function getMovementVector1(orientation)
- if (orientation == Orientation.FRONT) then
- return vecMOVEMENT.FRONT1
- elseif (orientation == Orientation.BACK) then
- return vecMOVEMENT.BACK1
- elseif (orientation == Orientation.RIGHT) then
- return vecMOVEMENT.RIGHT1
- elseif (orientation == Orientation.LEFT) then
- return vecMOVEMENT.LEFT1
- else
- writeMessage("FATAL: getMovementVector1(): orientation invalid: " .. tostring(orientation), MessageLevel.FATAL)
- emergencyExit()
- end
- end
- -- ********************************************************************************** --
- -- Mark the mining program startup with some files to take
- -- corrective actions when a premature reboot happens.
- -- ********************************************************************************** --
- function openAppendOrCreate(filename)
- local file = io.open(filename, "a")
- if file == nil then
- file = io.open(filename, "w")
- end
- return file
- end
- -- ********************************************************************************** --
- -- Mark the mining program startup with some files to take
- -- corrective actions when a premature reboot happens.
- -- ********************************************************************************** --
- function startup()
- -- Write a new startup file to resume the turtle
- file = io.open(FILENAME_STARTUP, "w")
- file:write("\nshell.run(\"")
- file:write(shell.getRunningProgram())
- file:write("\")\n")
- file:close()
- -- touch file to detect reboots
- file = openAppendOrCreate(FILENAME_REBOOT)
- file:close()
- end
- -- ********************************************************************************** --
- -- Mark the mining program startup with some file to take
- -- corrective actions when a premature reboot happens.
- -- ********************************************************************************** --
- function finish()
- -- delete reboot file
- fs.delete(FILENAME_REBOOT)
- -- delete startup file
- fs.delete(FILENAME_STARTUP)
- end
- -- ********************************************************************************** --
- -- Write the current location to a file
- -- ********************************************************************************** --
- function savePositionAndOrientation()
- local file = io.open(FILENAME_POSITION , "w")
- file:write(textutils.serialize(posCurr) .. "\n")
- file:write(orientationCurr)
- file:write("\n")
- file:close()
- end
- -- ********************************************************************************** --
- -- Read our location from file
- -- ********************************************************************************** --
- function loadPositionAndOrientation()
- writeMessage("Attempting to load position", MessageLevel.FATAL)
- if not fs.exists(FILENAME_POSITION) then
- writeMessage("File not found: " .. FILENAME_POSITION, MessageLevel.FATAL)
- return false
- end
- local file = io.open(FILENAME_POSITION , "r")
- posCurr = textutils.unserialize(file:read())
- writeMessage("posCurr=" .. posToString(posCurr), MessageLevel.FATAL)
- orientationCurr = tonumber(file:read())
- writeMessage("orientationCurr=" .. toString(orientationCurr), MessageLevel.FATAL)
- file:close()
- vecForwardMovement1 = getMovementVector1(orientationCurr)
- return true
- end
- -- ********************************************************************************** --
- -- Appends a message to a file
- -- ********************************************************************************** --
- function appendFile(filename, msg)
- local file = openAppendOrCreate(filename)
- file:write(msg .. "\n")
- file:close()
- end
- -- ********************************************************************************** --
- -- Writes an output message
- -- ********************************************************************************** --
- function writeMessage(message, msgLevel)
- local level = tonumber(msgLevel)
- if not level then
- level = 999
- end
- if (level >= messageOutputLevel) then
- print(message)
- if FILENAME_MESSAGES ~= nil then
- -- Open file, write message and close file (flush doesn't seem to work!)
- local file = openAppendOrCreate(FILENAME_MESSAGES)
- coords = "(" .. posToString(posCurr) .. ") "
- file:write(coords .. message)
- file:write("\n")
- file:close()
- end
- end
- end
- -- ********************************************************************************** --
- -- Writes a position to string (x, y, z)
- -- ********************************************************************************** --
- function posToString(pos)
- return tostring(posCurr.x) .. "," .. tostring(posCurr.y) .. "," .. tostring(posCurr.z)
- end
- -- ********************************************************************************** --
- -- Selects the same item as specified slot.
- -- ********************************************************************************** --
- function selectSameAs(slot)
- -- Try to get same item but elsewhere
- for i = 1, 16 do
- if i ~= slot then
- turtle.select(i)
- if turtle.compareTo(slot) then
- writeMessage("Found item " .. slot .. " also in slot " .. i, MessageLevel.DEBUG)
- return true, slot
- end
- end
- end
- -- didn't find anything use slot if possible
- if turtle.getItemCount(slot) > 1 then
- writeMessage("Using item " .. slot .. ": not other slot found", MessageLevel.DEBUG)
- turtle.select(slot)
- return true, slot
- else
- writeMessage("NOT consuming item " .. slot .. ": only 1 left", MessageLevel.DEBUG)
- return false, 0
- end
- end
- -- ********************************************************************************** --
- -- Place same item as specified slot.
- -- ********************************************************************************** --
- function placeSameAs(slot, fnPlace, fnDetect)
- if fnDetect() then
- return true
- end
- if fnPlace == nil then
- fnPlace = turtle.place
- end
- local success
- local slotChosen
- success, slotChosen = selectSameAs(slot)
- if not success then
- turtle.select(slot)
- end
- return fnPlace()
- end
- -- ********************************************************************************** --
- -- Function to turn the Turtle in the given orientation.
- -- ********************************************************************************** --
- function changeOrientation(newOrient)
- if (orientationCurr == newOrient) then
- return
- end
- -- turn left?
- if (newOrient - orientationCurr + NB_ORIENTATIONS) % NB_ORIENTATIONS == NB_ORIENTATIONS-1 then
- turtle.turnLeft()
- orientationCurr = newOrient
- end
- while orientationCurr ~= newOrient do
- turtle.turnRight()
- orientationCurr = (orientationCurr + 1) % NB_ORIENTATIONS
- end
- vecForwardMovement1 = getMovementVector1(orientationCurr)
- savePositionAndOrientation()
- end
- -- ********************************************************************************** --
- -- Functions to move the Turtle forward n spaces in the current orientation
- -- (or 1 if no n is specified)
- -- pushing through any gravel or other
- -- things such as mobs that might get in the way.
- -- ********************************************************************************** --
- function digAndMoveForward(n)
- preMoveChecks()
- return digAndMoveForwardNoCheck(n)
- end
- function digAndMoveForwardNoCheck(n)
- n = tonumber(n)
- if n == nil then
- n = 1
- end
- for i = 1,n do
- local success = digG(turtle.detect, turtle.dig, turtle.forward, turtle.attack, vecForwardMovement1)
- if not success then
- writeMessage("digAndMoveForwardNoCheck: could not move", MessageLevel.DEBUG)
- return false
- end
- end
- return true
- end
- -- ********************************************************************************** --
- -- Functions to move the Turtle up n spaces (or 1 if no n is specified)
- -- pushing through any gravel or other
- -- things such as mobs that might get in the way.
- -- ********************************************************************************** --
- function digAndMoveUp(n)
- preMoveChecks()
- return digAndMoveUpNoCheck(n)
- end
- function digAndMoveUpNoCheck(n)
- n = tonumber(n)
- if n == nil then
- n = 1
- end
- for i = 1,n do
- local success = digG(turtle.detectUp, turtle.digUp, turtle.up, turtle.attackUp, vecMOVEMENT.UP1)
- if not success then
- return false
- end
- end
- return true
- end
- -- ********************************************************************************** --
- -- Functions to move the Turtle down n spaces (or 1 if no n is specified)
- -- Pushing through any gravel or other
- -- things such as mobs that might get in the way.
- -- ********************************************************************************** --
- function digAndMoveDown(n)
- preMoveChecks()
- return digAndMoveDownNoCheck(n)
- end
- function digAndMoveDownNoCheck(n)
- n = tonumber(n)
- if n == nil then
- n = 1
- end
- for i = 1,n do
- local success = digG(turtle.detectDown, turtle.digDown, turtle.down, turtle.attackDown, vecMOVEMENT.DOWN1)
- if not success then
- return false
- end
- end
- return true
- end
- -- ********************************************************************************** --
- -- Generic function to dig 1 space
- -- pushing through any gravel or other
- -- things such as mobs that might get in the way.
- -- ********************************************************************************** --
- function digG(fnDetect, fnDig, fnMove, fnAttack, vecMovement1)
- local nbTries = 0
- local success = false
- local successMove = false
- -- we do not try to be clever with gravel, we just retry
- while not successMove and nbTries < MNB_FAILED_MOVE do
- nbTries = nbTries + 1
- successMove = fnMove()
- if not successMove then
- success = false
- if fnDetect() then
- success = fnDig()
- end
- if not success then
- if fnAttack() then
- writeMessage("Turtle hit mob!", MessageLevel.INFO)
- end
- end
- end
- end
- if successMove then
- -- update current position
- posCurr = posAdd(posCurr, vecMovement1)
- savePositionAndOrientation()
- else
- writeMessage("Turtle unable to move for " .. MNB_FAILED_MOVE .. " tries", MessageLevel.DEBUG)
- end
- return successMove
- end
- -- ********************************************************************************** --
- -- Function to move to a position (no checks)
- -- ********************************************************************************** --
- function digToNoCheck(pos)
- while posCurr.y < pos.y do
- digAndMoveUpNoCheck()
- end
- while posCurr.y > pos.y do
- digAndMoveDownNoCheck()
- end
- changeOrientation(Orientation.RIGHT)
- while posCurr.x < pos.x do
- digAndMoveForwardNoCheck()
- end
- changeOrientation(Orientation.BACK)
- while posCurr.z < pos.z do
- digAndMoveForwardNoCheck()
- end
- changeOrientation(Orientation.LEFT)
- while posCurr.x > pos.x do
- digAndMoveForwardNoCheck()
- end
- changeOrientation(Orientation.FRONT)
- while posCurr.z > pos.z do
- digAndMoveForwardNoCheck()
- end
- end
- -- ********************************************************************************** --
- -- Function to move to a position
- -- ********************************************************************************** --
- function digTo(pos)
- while posCurr.y < pos.y do
- digAndMoveUp()
- end
- while posCurr.y > pos.y do
- digAndMoveDown()
- end
- changeOrientation(Orientation.RIGHT)
- while posCurr.x < pos.x do
- digAndMoveForward()
- end
- changeOrientation(Orientation.LEFT)
- while posCurr.x > pos.x do
- digAndMoveForward()
- end
- changeOrientation(Orientation.FRONT)
- while posCurr.z > pos.z do
- digAndMoveForward()
- end
- changeOrientation(Orientation.BACK)
- while posCurr.z < pos.z do
- digAndMoveForward()
- end
- end
- -- ********************************************************************************** --
- -- Function to perform an emergency return home
- -- ********************************************************************************** --
- function emergencyExit()
- writeMessage("FATAL: Emergency exit", MessageLevel.FATAL)
- local success = loadPositionAndOrientation()
- if not success then
- writeMessage("FATAL: Cannot load return coordinates", MessageLevel.FATAL)
- return
- end
- digToNoCheck(posHOME)
- changeOrientation(Orientation.FRONT)
- finish()
- error()
- end
- -- ********************************************************************************** --
- -- Performs pre-move checks
- -- ********************************************************************************** --
- function preMoveChecks()
- if not checkFuel() then
- writeMessage("Not enough fuel to continue: " .. turtle.getFuelLevel(), MessageLevel.FATAL)
- emergencyExit()
- end
- ensureInventorySpace()
- end
- -- ********************************************************************************** --
- -- Check fuel gauge and return whether we have enough.
- -- ********************************************************************************** --
- function checkFuel()
- -- Determine whether a refuel is required
- local fuelLevel = turtle.getFuelLevel()
- if (fuelLevel == "unlimited") then
- return true
- end
- if (fuelLevel > FUEL_NEEDED_AT) then
- return true
- end
- return false
- end
- -- ********************************************************************************** --
- -- Checks that the turtle has inventory space by checking for spare slots.
- -- ********************************************************************************** --
- function ensureInventorySpace()
- if doInventoryCheck then
- local count = turtle.getItemCount(SLOT_LAST)
- if count > 0 then
- writeMessage("Turtle must empty inventory: last slot count = " .. count, MessageLevel.INFO)
- doInventoryCheck = false
- success = emptyInventoryAndReturn()
- doInventoryCheck = true
- return success
- end
- end
- end
- -- ********************************************************************************** --
- -- Empty the inventory at the starting point then return
- -- ********************************************************************************** --
- function emptyInventoryAndReturn()
- -- Return to the starting point and empty the inventory
- -- then go back to mining
- local posOld = posCurr
- local orientationOld = orientationCurr
- -- empty turtle
- digTo(posHOME)
- emptyInventory()
- -- get back to initial position
- digTo(posOld)
- changeOrientation(orientationOld)
- end
- -- ********************************************************************************** --
- -- Empty the turtle's inventory
- -- Expects a chest at the top pf the starting position
- -- ********************************************************************************** --
- function emptyInventory()
- -- drop unwanted first
- dumpUnwantedBlocks()
- for i = Slots.FIRST_INVENTORY, SLOT_LAST do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.dropUp()
- end
- end
- -- very important to reselect first empty slot
- -- otherwise the next dig will fill the last slot
- turtle.select(Slots.FIRST_INVENTORY)
- end
- -- ********************************************************************************** --
- -- Check all is ok before launching and set required states
- -- ********************************************************************************** --
- function preLaunch()
- -- perform internal validation
- if Slots == nil then
- writeMessage("Slots not defined", MessageLevel.FATAL)
- error()
- else
- if not Slots.FIRST_INVENTORY then
- writeMessage("Slots.FIRST_INVENTORY is undefined", MessageLevel.FATAL)
- error()
- end
- end
- local fuel = turtle.getFuelLevel()
- print ("Fuel level: " .. fuel)
- if not checkFuel() then
- writeMessage("Not enough fuel", MessageLevel.FATAL)
- error()
- end
- -- delete prior files
- fs.delete(FILENAME_MESSAGES)
- fs.delete(FILENAME_POSITION)
- fs.delete(FILENAME_STARTUP)
- fs.delete(FILENAME_REBOOT)
- fs.delete(FILENAME_STRATA)
- -- reset position and orientation
- posCurr = posHOME
- orientationCurr = Orientation.FRONT
- savePositionAndOrientation()
- -- make sure we fill inventory from the start
- turtle.select(Slots.FIRST_INVENTORY)
- -- await final confirmation
- print "Press enter (Ctrl-T to abort)"
- io.read()
- end
- -- ********************************************************************************** --
- -- Returns true if the positions are equal (orientation is not compared)
- -- ********************************************************************************** --
- function posEquals(pos1, pos2)
- return pos1.x == pos2.x and pos1.y == pos2.y and pos1.z == pos2.z
- end
- -- ********************************************************************************** --
- -- Copy a position vector.
- -- ********************************************************************************** --
- function posCopy(pos)
- local vec = { x = pos.x, y = pos.y, z = pos.z }
- return vec
- end
- -- ********************************************************************************** --
- -- Adds two position vectors.
- -- ********************************************************************************** --
- function posAdd(pos1, pos2)
- local vec = { x = pos1.x + pos2.x, y = pos1.y + pos2.y, z = pos1.z + pos2.z }
- return vec
- end
- -- ********************************************************************************** --
- -- Subtract two position vectors.
- -- ********************************************************************************** --
- function posDiff(pos1, pos2)
- local vec = { x = pos1.x - pos2.x, y = pos1.y-pos2.y, z = pos1.z-pos2.z }
- return vec
- end
- -- ********************************************************************************** --
- -- Compute the distance between two positions
- -- ********************************************************************************** --
- function posDist(pos1, pos2)
- local vec = posDiff(pos1, pos2)
- local dist = math.sqrt((vec.x*vec.x) + (vec.y*vec.y) + (vec.z*vec.z))
- return dist
- end
- -- ********************************************************************************** --
- -- Compute the distance between two positions considering only the x and z coords
- -- ********************************************************************************** --
- function posDistPlane(pos1, pos2)
- local vec = posDiff(pos1, pos2)
- local dist = math.sqrt((vec.x*vec.x) + (vec.z*vec.z))
- return dist
- end
- -- ********************************************************************************** --
- -- Dump unwanted blocks (if Slots.UNWANTED_BLOCKS is defined)
- -- ********************************************************************************** --
- function dumpUnwantedBlocks()
- if Slots.UNWANTED_BLOCKS then
- -- Dump unwanted blocks
- for item = Slots.FIRST_INVENTORY, SLOT_LAST do
- if turtle.getItemCount(item) > 0 then
- turtle.select(item)
- for unwanted = Slots.UNWANTED_BLOCKS, Slots.FIRST_INVENTORY-1 do
- if turtle.compareTo(unwanted) then
- writeMessage("dumpUnwantedBlocks: UNWANTED FOUND!", MessageLevel.DEBUG)
- turtle.dropDown()
- end
- end
- end
- end
- turtle.select(1)
- end
- end
- -- ********************************************************************************** --
- -- Drill to bedrock
- -- ********************************************************************************** --
- function mineDrill()
- Slots = { FIRST_INVENTORY = 1 }
- print "*****************************"
- print "*****************************"
- print "*****************************"
- preLaunch()
- -- mine down
- local moveSuccess = true
- while moveSuccess do
- -- dig in front
- turtle.dig()
- -- go down
- moveSuccess = digAndMoveDown()
- end
- digTo(posHOME)
- changeOrientation(Orientation.FRONT)
- end
- -- ********************************************************************************** --
- -- Mine corridor x meters deep
- -- ********************************************************************************** --
- function mineCorridor(x)
- Slots = { FIRST_INVENTORY = 1 }
- preLaunch()
- for i = 1, x do
- changeOrientation(Orientation.FRONT)
- digAndMoveForward()
- turtle.digUp()
- turtle.digDown()
- end
- end
- -- ********************************************************************************** --
- -- Mine tunnel 3 by 3 by x
- -- ********************************************************************************** --
- function mineTunnel(x)
- Slots = { COBBLESTONE = 1,
- FIRST_INVENTORY = 2 }
- print "*****************************"
- print "*****************************"
- print "*****************************"
- print "Arrange slots as follows:"
- print "Slot 1: cobblestone"
- preLaunch()
- -- spiral
- for i = 1, x do
- -- mid center
- changeOrientation(Orientation.FRONT)
- digAndMoveForward()
- changeOrientation(Orientation.RIGHT)
- -- bottom center
- digAndMoveDown()
- placeSameAs(Slots.COBBLESTONE, turtle.placeDown, turtle.detectDown)
- -- bottom right
- digAndMoveForward()
- placeSameAs(Slots.COBBLESTONE, turtle.placeDown, turtle.detectDown)
- placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
- -- mid right
- digAndMoveUp()
- placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
- -- top right
- digAndMoveUp()
- placeSameAs(Slots.COBBLESTONE, turtle.placeUp, turtle.detectUp)
- placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
- changeOrientation(Orientation.LEFT)
- -- top center
- digAndMoveForward()
- placeSameAs(Slots.COBBLESTONE, turtle.placeUp, turtle.detectUp)
- -- top left
- digAndMoveForward()
- placeSameAs(Slots.COBBLESTONE, turtle.placeUp, turtle.detectUp)
- placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
- -- mid left
- digAndMoveDown()
- placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
- -- bottom left
- digAndMoveDown()
- placeSameAs(Slots.COBBLESTONE, turtle.place, turtle.detect)
- placeSameAs(Slots.COBBLESTONE, turtle.placeDown, turtle.detectDown)
- -- go back to mid center
- digAndMoveUp()
- changeOrientation(Orientation.RIGHT)
- digAndMoveForward()
- changeOrientation(Orientation.FRONT)
- end
- end
- -- ********************************************************************************** --
- -- Pose blocks in a track
- -- ********************************************************************************** --
- function mineTrack(len)
- Slots = { ITEM = 1,
- FIRST_INVENTORY = 2 }
- print "*****************************"
- print "*****************************"
- print "*****************************"
- print "Arrange slots as follows:"
- print "Slot 1: item to place"
- preLaunch()
- for i = 1, len do
- digAndMoveForward()
- turtle.digDown()
- placeSameAs(Slots.ITEM, turtle.placeDown, turtle.detectDown)
- end
- end
- -- ********************************************************************************** --
- -- Pose stairs going down
- -- ********************************************************************************** --
- function mineStairsDown(len)
- Slots = { ITEM = 1,
- FIRST_INVENTORY = 2 }
- print "*****************************"
- print "*****************************"
- print "*****************************"
- print "Arrange slots as follows:"
- print "Slot 1: material to place as stairs"
- preLaunch()
- for i = 1, len do
- digAndMoveForward()
- -- make head room
- digAndMoveUp(2)
- turtle.digUp()
- digAndMoveDown(2)
- -- pose/dig stairs proper
- digAndMoveDown(2)
- changeOrientation(Orientation.BACK)
- placeSameAs(Slots.ITEM, turtle.place, turtle.detect)
- digAndMoveUp()
- placeSameAs(Slots.ITEM, turtle.placeDown, turtle.detectDown)
- changeOrientation(Orientation.FRONT)
- end
- end
- -- ********************************************************************************** --
- -- Pose stairs going up
- -- ********************************************************************************** --
- function mineStairsUp(len)
- Slots = { ITEM = 1,
- FIRST_INVENTORY = 2 }
- print "*****************************"
- print "*****************************"
- print "*****************************"
- print "Arrange slots as follows:"
- print "Slot 1: material to place as stairs"
- preLaunch()
- for i = 1, len do
- digAndMoveForward()
- placeSameAs(Slots.ITEM, turtle.placeDown, turtle.detectDown)
- digAndMoveUp()
- placeSameAs(Slots.ITEM, turtle.placeDown, turtle.detectDown)
- -- make head room
- digAndMoveUp(3)
- turtle.digUp()
- digAndMoveDown(3)
- end
- end
- -- ********************************************************************************** --
- -- Mine all stratas down
- -- ********************************************************************************** --
- function mineAll()
- Slots = { BUCKET = 1,
- UNWANTED_BLOCKS = 2,
- FIRST_INVENTORY = 3 }
- print "*****************************"
- print "*****************************"
- print "*****************************"
- print "ASSUMES there is a CHEST ABOVE the turtle"
- print "Turtle will mine going RIGHT than FORWARD"
- print "Arrange slots as follows:"
- print "Slot 1: empty bucket"
- print "Slot 2: cobblestone/netherrack"
- preLaunch()
- digTo(posHOME)
- emptyInventory()
- -- will have to decrement counter
- -- going FRONT, z becomes smaller
- fromZ = posHOME.z
- toZ = posHOME.z - STRATA_Z_FRONT
- fromX = posHOME.x
- toX = posHOME.x + STRATA_X_RIGHT
- fromY = posHOME.y
- local msg = "Mining all"
- writeMessage(msg, MessageLevel.INFO)
- appendFile(FILENAME_STRATA, msg)
- -- go to starting position
- posStarting = { x = fromX, y = fromY, z = fromZ }
- digTo(posStarting)
- -- Moving to the FRONT (step -1)
- for iz = fromZ, toZ, -1 do
- -- LEFT -> RIGHT (step 4)
- for ix = fromX, toX, 4 do
- writeMessage("Fuel level: " .. turtle.getFuelLevel(), MessageLevel.INFO)
- dumpUnwantedBlocks()
- -- get in position
- pos = { x = ix, y = fromY, z = iz }
- digTo(pos)
- changeOrientation(Orientation.RIGHT)
- local moveSuccess = true
- while moveSuccess do
- -- dig RIGHT
- turtle.dig()
- -- collect lava/water
- turtle.select(Slots.BUCKET)
- local placeSuccess = turtle.placeDown()
- if (placeSuccess) then
- turtle.refuel()
- -- in case of water, must empty
- turtle.digDown() -- break block
- turtle.placeDown() -- quickly empty
- end
- -- make sure all items are stacked
- turtle.select(1)
- -- go down
- moveSuccess = digAndMoveDown()
- end
- dumpUnwantedBlocks()
- -- prep ascension
- -- move up in case bedrock is uneven
- digAndMoveUp()
- digAndMoveForward()
- digAndMoveForward()
- -- go down to get the missed blocks
- moveSuccess = digAndMoveDown()
- turtle.dig()
- -- mine up
- while posCurr.y < fromY do
- -- dig RIGHT
- turtle.dig()
- -- collect lava
- turtle.select(Slots.BUCKET)
- local placeSuccess = turtle.placeUp()
- if (placeSuccess) then
- turtle.refuel()
- -- in case of water, must empty
- turtle.digDown() -- break block
- turtle.placeDown() -- quickly empty
- end
- -- make sure all items are stacked
- turtle.select(1)
- -- go up
- moveSuccess = digAndMoveUp()
- end
- turtle.dig()
- msg = "Finished carrot: " .. iz .. ", " .. ix
- writeMessage(msg)
- appendFile(FILENAME_STRATA, msg)
- end
- end
- digTo(posHOME)
- dumpUnwantedBlocks()
- emptyInventory()
- changeOrientation(Orientation.FRONT)
- end
- -- ********************************************************************************** --
- -- Print commands
- -- ********************************************************************************** --
- function printCommands()
- print("Commands:")
- print(" emergency Turtle returns HOME")
- print(" all Mine all stratas")
- print(" drill Drill to bedrock")
- print(" tunnel x Mine x meters ahead")
- print(" corridor x Mine x meters ahead")
- print(" track x Lays item for x meters")
- print("... press enter ...")
- io.read()
- print(" stairsUp x Stairs up x steps")
- print(" stairsDn x Stairs down x steps")
- end
- -- ********************************************************************************** --
- -- Main program
- -- ********************************************************************************** --
- function main(tArgs)
- if #tArgs < 1 then
- printCommands()
- return
- end
- local command = tArgs[1]
- writeMessage("Orientation = " .. orientationCurr, MessageLevel.DEBUG)
- writeMessage("Current position = " .. textutils.serialize(posCurr), MessageLevel.DEBUG)
- if command == "emergency" then
- emergencyExit()
- return
- elseif command == "all" then
- mineAll()
- elseif command == "drill" then
- mineDrill()
- elseif command == "tunnel" then
- local x = tonumber(tArgs[2])
- mineTunnel(x)
- elseif command == "corridor" then
- local x = tonumber(tArgs[2])
- mineCorridor(x)
- elseif command == "track" then
- local x = tonumber(tArgs[2])
- mineTrack(x)
- elseif command == "stairsUp" then
- local x = tonumber(tArgs[2])
- mineStairsUp(x)
- elseif command == "stairsDn" then
- local x = tonumber(tArgs[2])
- mineStairsDown(x)
- else
- printCommands()
- end
- end
- -- ********************************************************************************** --
- -- startup script
- -- ********************************************************************************** --
- -- check if we have been rebooted
- if fs.exists(FILENAME_REBOOT) then
- writeMessage("Turtle rebooted prematurely", MessageLevel.FATAL)
- writeMessage("Engaging emergency procedures in 60 seconds", MessageLevel.FATAL)
- writeMessage("(ctrl-T to abort and delete reboot_token)", MessageLevel.FATAL)
- sleep(60)
- emergencyExit()
- error()
- end
- startup()
- local args = { ... }
- -- wrap call to main so we can catch errors and cleanup
- local status, msg = pcall(main, args)
- if not status then
- if msg ~= nil then
- writeMessage(tostring(msg), MessageLevel.FATAL)
- else
- writeMessage("ERROR in main() but no error message was provided", MessageLevel.FATAL)
- end
- emergencyExit()
- end
- -- cleanup
- finish()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement