Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --fuel enderchest slot 15
- --item enderchest slot 16
- --global variables for recovery file
- --initialise all to 0
- data = {}
- --if program should recover from file
- data["shouldRecover"] = 0
- --length of the quarry area
- data["length"] = 0
- --width of the quarry area
- data["width"] = 0
- --remaining distance forward
- --until current move is complete
- data["moveDistLeft"] = 0
- --current column being mined in rect
- data["currCol"] = 0
- --current direction of mining
- -- -1 == left; 1 = right
- data["currDir"] = 0
- --current depth remaining of quarry
- data["currDepth"] = 0
- --state (column, corner or layerChange)
- data["state"] = 0
- --number of turns required before
- --we can dig straight again
- data["numTurnsReq"] = 0
- --direction last enderchest was placed
- data["lastEChestDir"] = 0
- data["currLayer"] = 0
- --writes a file with the recover data
- --file name is recoverFile
- function updateFile()
- --open the recovery file
- --in 'overwrite' mode
- file = fs.open("recoverFile/","w")
- --write all recovery info to file
- file.writeLine(data["shouldRecover"])
- file.writeLine(data["length"])
- file.writeLine(data["width"])
- file.writeLine(data["moveDistLeft"])
- file.writeLine(data["currCol"])
- file.writeLine(data["currDir"])
- file.writeLine(data["currDepth"])
- file.writeLine(data["state"])
- file.writeLine(data["numTurnsReq"])
- file.writeLine(data["lastEChestDir"])
- file.writeLine(data["currLayer"])
- --close the file now we're done with it
- file.close()
- end
- function loadFile()
- --open the file in read mode
- file = fs.open("recoverFile/","r")
- --load data into temporary table
- local fileData = {}
- local line = file.readLine()
- repeat
- table.insert(fileData,line)
- line = file.readLine()
- until line == nil
- --close the file now we're done with it
- file.close()
- --put the data in the proper table
- --after converting it to numbers
- data["shouldRecover"] = tonumber(fileData[1])
- data["length"] = tonumber(fileData[2])
- data["width"] = tonumber(fileData[3])
- data["moveDistLeft"] = tonumber(fileData[4])
- data["currCol"] = tonumber(fileData[5])
- data["currDir"] = tonumber(fileData[6])
- data["currDepth"] = tonumber(fileData[7])
- data["state"] = tonumber(fileData[8])
- data["numTurnsReq"] = tonumber(fileData[9])
- data["lastEChestDir"] = tonumber(fileData[10])
- data["currLayer"] = tonumber(fileData[11])
- end
- --determines if EChest in 'slot' is out
- function isUsingEChest(slot)
- --if there is nothing in EChest slot
- if turtle.getItemCount(slot) == 0 then
- --the chest is out
- return true
- else --something is in EChest slot
- --chest is not out
- return false
- end
- end
- --returns true if inventory is full
- function checkInventory()
- if turtle.getItemCount(14) == 0 then
- return false
- else
- return true
- end
- end
- --returns true if
- --there is a block in 'direction'
- function detectDirection(direction)
- --down
- if direction == -1 then
- return turtle.detectDown()
- --fwd
- elseif direction == 0 then
- return turtle.detect()
- --up
- else --if direction == 1 then
- return turtle.detectUp()
- end
- end
- --performs turtle.suck() in direction
- --and returns whether the
- --operation was successful
- function suckDirection(direction)
- --down
- if direction == -1 then
- return turtle.suckDown()
- --fwd
- elseif direction == 0 then
- return turtle.suck()
- --up
- else --direction == 1
- return turtle.suckUp()
- end
- end
- --digs once in 'direction'
- --does not make any checks
- function digDirection(direction)
- if direction == -1 then
- return turtle.digDown()
- elseif direction == 0 then
- return turtle.dig()
- else --direction == 1 then
- return turtle.digUp()
- end
- end
- --turns the turtle in 'direction'
- function turnDirection(direction)
- if direction < 0 then
- return turtle.turnLeft()
- else
- return turtle.turnRight()
- end
- end
- --returns name of block
- --in direction 'direction'
- -- -1 = Down
- -- 0 = Fwd
- -- 1 = Up
- function getBlockName(direction)
- --get data from relevant direction
- local success, data
- --if block below
- if direction < 0 then
- success, data = turtle.inspectDown()
- --if block in front
- elseif direction == 0 then
- success, data = turtle.inspect()
- --else: block above
- else
- success, data = turtle.inspectUp()
- end
- --if there is a block above the turtle
- if success then
- --*debug code*
- -- print("Block Name: ", data.name)
- -- print("Block Data: ", data.metadata)
- --return the block's name
- return data.name
- --if there is no block above the turtle
- else
- --return 'air' above the turtle
- return "air"
- end
- end
- --returns a value based on
- --whether the turtle
- --should mine it or not
- --used to test for enderchest placement
- --0 = no problem to mine
- --1 = preferred not to mine
- --2 = dangerous to mine
- --but will move given time
- --3 = impossible to mine
- --or should never be mined
- function getMineability(blockName)
- --if the block is not computercraft
- --may need to handle warded blocks
- if string.find(blockName, "ComputerCraft") == nil then
- --*debug code*
- -- print("Not a computercraft block")
- --*/debug code*
- --create a mineability table
- local mLevel = {}
- mLevel["minecraft:bedrock"] = 3
- mLevel["minecraft:chest"] = 1
- mLevel["EnderStorage:enderChest"] = 3
- mLevel["Thaumcraft:blockWarded"] = 3
- --get mining level from table
- local level = mLevel[blockName]
- --if the block isnt in the table
- if level == nil then
- --*debug code*
- -- print("Block not in table")
- -- print("Mining level = 0")
- --*/debug code*
- --return a mining level of 0
- return 0
- --if the block is in the table
- else
- --*debug code*
- -- print("Block name found in table")
- -- print("Mining level = ", level)
- --*/debug code
- --return mining level from table
- return level
- end
- --if the block is computercraft
- else
- return 2
- end
- end
- --returns the mining level in direction dir
- function getMLevel(dir)
- local level = getMineability(getBlockName(dir))
- return level
- end
- --returns the mining level
- --of all directions specified
- function getMLevels(down, fwd, up)
- local upLevel
- local fwdLevel
- local downLevel
- --repeat the detection process until
- --Enderchest location is found
- repeat
- --calculate mining level of downward block
- --if down is an option
- if down then
- --downLevel = mining level(down)
- downLevel = getMineability(getBlockName(-1))
- --if down is not an option
- else
- --downlevel = impossible
- downLevel = 3
- end
- --calculate mining level of fwd block
- if fwd then
- fwdLevel = getMineability(getBlockName(0))
- else
- fwdLevel = 3
- end
- if up then
- upLevel = getMineability(getBlockName(1))
- else
- upLevel = 3
- end
- --if nowhere to place enderchest
- if (9 == downLevel + fwdLevel + upLevel) then
- print("Unable to place Enderchest")
- print("Waiting 30s for assistance")
- print("Before retrying placement")
- sleep(30)
- end
- until (9 > downLevel + fwdLevel + upLevel)
- --return all calculated mining levels
- return downLevel, fwdLevel, upLevel
- end
- --returns the direction an enderchest
- --should be placed
- --and the mining level of that direction
- function getEChestDir(down, fwd, up)
- --get the levels of all specified directions
- local downL, fwdL, upL = getMLevels(down, fwd, up)
- --find the lowest level
- local minLevel = math.min(downL, fwdL, upL)
- --cycle through to return the lowest
- if upL == minLevel then
- return 1, minLevel
- elseif downL == minLevel then
- return -1, minLevel
- else --fwdL = minLevel
- return 0, minLevel
- end
- end
- --places the correct enderchest
- --in the specified direction
- --handles waiting for computercraft
- --blocks to move out of the way
- --does not handle
- --being surrounded by unbreakable blocks
- function placeEChest(slot, direction, mLevel)
- --if the best direction is
- --a computercraft block
- if mLevel == 2 then
- --wait for it to move
- while 2 == getMLevel(direction) do
- print("Computercraft block in way of chest")
- print("Waiting 10s for it to move")
- sleep(10)
- end
- end
- --clear a space for the chest
- if direction == -1 then
- while turtle.digDown() do
- sleep(1)
- end
- elseif direction == 0 then
- while turtle.dig() do
- sleep(1)
- end
- else --direction == 1
- while turtle.digUp() do
- sleep(1)
- end
- end
- --place the chest
- turtle.select(slot)
- if direction == -1 then
- while not turtle.placeDown() do
- print("Failed to place enderchest")
- print("Trying again in 1s")
- sleep(1)
- end
- --update the file
- data["lastEChestDir"] = -1
- elseif direction == 0 then
- while not turtle.place() do
- print("Failed to place enderchest")
- print("Trying again in 1s")
- sleep(1)
- end
- --update the file
- data["lastEChestDir"] = 0
- else --direction == 1 then
- while not turtle.placeUp() do
- print("Failed to place enderchest")
- print("Trying again in 1s")
- sleep(1)
- end
- --update the data
- data["lastEChestDir"] = 1
- --and update the file
- updateFile()
- end
- --reselect slot 1
- turtle.select(1)
- end
- function retrieveEChest(slot, direction)
- turtle.select(slot)
- --if chest is below
- if direction == -1 then
- --retrieve it
- turtle.digDown()
- elseif direction == 0 then
- turtle.dig()
- else --direction == 1 then
- turtle.digUp()
- end
- --reselect slot 1
- turtle.select(1)
- end
- --dumps every item in the inventory
- --into the enderchest in slot 16
- function unload()
- --find where to place the enderchest
- local direction, level = getEChestDir(true, true, true)
- --place the enderchest in direction
- placeEChest(16, direction, level)
- --unload everything into the enderchest
- --for each slot in the turtle
- for i = 1, 14 do
- --select the slot
- turtle.select(i)
- --while there's items in the slot
- while turtle.getItemCount() > 0 do
- --if enderchest is down
- if direction == -1 then
- --try to drop the items into it
- --if that fails
- if not turtle.dropDown() then
- print("Item drop failed")
- print("Waiting 5s for chest to clear")
- sleep(5)
- end
- elseif direction == 0 then
- if not turtle.drop() then
- print("Item drop failed")
- print("Waiting 5s for chest to clear")
- sleep(5)
- end
- else --if direction == 1 then
- if not turtle.dropUp() then
- print("Item drop failed")
- print("Waiting 5s for chest to clear")
- sleep(5)
- end
- end
- end
- end
- --select the enderchest's slot
- turtle.select(16)
- --grab the enderchest
- digDirection(direction)
- --reselect slot 1
- turtle.select(1)
- end
- --returns the amount of fuel which
- --would neccessitate turtle refuelling
- function getMinFuel()
- --if for some reason the configs are
- --set to a limit below 5
- --min fuel = fuel limit/2
- return math.min(5, (turtle.getFuelLimit()/2))
- end
- --returns amount of fuel needed
- --before turtle can stop refuelling
- function getMaxFuel()
- --only refuels up to 1,000 if limit
- --if limit is above that
- --if limit is below, fuels to limit
- return math.min(1000, turtle.getFuelLimit())
- end
- --returns true if turtle needs to refuel
- function shouldRefuel()
- if turtle.getFuelLevel() < getMinFuel() then
- return true
- else
- return false
- end
- end
- function continueRefuel()
- if turtle.getFuelLevel() < getMaxFuel() then
- return true
- else --at or above max fuel
- return false
- end
- end
- --*NOT CODED YET*
- --returns true if an item is a valid fuel
- function isValidFuel()
- return true
- end
- --uses whatever is in the inventory
- --to refuel the turtle
- function refuelFromInventory()
- --for each inventory slot
- for i = 1, 14 do
- --select the slot
- turtle.select(i)
- --*debug code*
- --print("refuelling from slot ", i)
- --*/debug code*
- --if the slot contains valid fuel
- if turtle.refuel(0) then
- --while there are items in the slot
- if isValidFuel(i) then
- while turtle.getItemCount() > 0 do
- --if the turtle is below max fuel then
- if continueRefuel() then
- turtle.refuel(1)
- --*debug code*
- --print("Fuel level: ", turtle.getFuelLevel())
- --*/debug code*
- --if we have enough fuel
- else
- --*debug code*
- print("Full of fuel")
- --*/debug code*
- --select slot 1
- turtle.select(1)
- --stop refuelling
- return true
- end
- end
- end
- end
- end
- --reselect slot 1
- turtle.select(1)
- end
- --refuels the turtle from an enderchest
- --in direction 'direction'
- --requires a clear inventory
- function refuelFromEChest(direction)
- local success
- turtle.select(1)
- --while we still need to refuel
- while continueRefuel() do
- --suck one fuel item
- --from direction of enderchest
- if direction == -1 then
- success = turtle.suckDown(1)
- elseif direction == 0 then
- success = turtle.suck(1)
- else --direction == 1 then
- success = turtle.suckUp(1)
- end
- --if item was sucked from chest
- if success then
- --refuel with it
- turtle.refuel()
- else --failed to suck fuel item
- print("No Fuel in enderchest")
- print("Waiting 30s for chest to be refilled")
- --wait for it to be refilled
- sleep(30)
- end
- end
- end
- function refuel()
- --if the turtle should refuel
- if shouldRefuel() then
- refuelFromInventory()
- --if still need to refuel
- if continueRefuel() then
- --unload
- unload()
- --find where to place enderchest
- local direction, level = getEChestDir(true, true, true)
- --place enderchest
- placeEChest(15, direction, level)
- --refuel from enderchest
- refuelFromEChest(direction)
- --select slot enderchestshould be in
- turtle.select(15)
- --grab the enderchest
- digDirection(direction)
- --reselect slot 1
- turtle.select(1)
- end
- end
- end
- --empties a chest in 'direction'
- function emptyChest(direction)
- --while chest exists in direction
- while getMLevel(direction) == 1 do
- --if inventory full
- if checkInventory() then
- --empty inventory
- unload()
- else --inventory empty
- --if chest empty
- --or removed by unloading
- if suckDirection(direction) == false then
- digDirection(direction)
- return true
- end
- end
- end
- end
- --either guarantees a dig in 'direction'
- --or returns false if
- --asked to dig an unmineable block
- --NEED TO DEAL WITH LIQUIDS
- function safeDig(direction)
- --while there is something
- --(while not air in direction)
- --in the specified direction
- local level
- while detectDirection(direction) do
- --if the inventory is full
- if checkInventory() then
- --empty the inventory
- unload()
- end
- --get mining level of block
- level = getMLevel(direction)
- --if block is unbreakable
- if level == 3 then
- print("Error: Block is unbreakable")
- --return false to indicate this
- return false
- end
- --if block is from computercraft
- while level == 2 do
- print("Computercraft block detected")
- print("Waiting 5s for it to move")
- --wait for it to move
- sleep(5)
- --retest
- level = getMLevel(direction)
- end
- --if a chest is detected
- if level == 1 then
- emptyChest(direction)
- end
- if level == 0 then
- digDirection(direction)
- end
- --wait for blocks with gravity
- sleep(1)
- end
- return true
- end
- --a single function to 'safe dig'
- --in any and all directions
- function safeDigMulti(down, fwd, up)
- local d = false
- local f = false
- local u = false
- --if digging down is desired
- if down then
- --safeDig down
- --set d to result of safeDig
- d = safeDig(-1)
- end
- if fwd then
- f = safeDig(0)
- end
- if up then
- u = safeDig(1)
- end
- return d, f, u
- end
- --attempts to move in 'direction'
- --returns result of the move function
- function moveDirection(direction)
- if direction == -1 then
- return turtle.down()
- elseif direction == 0 then
- return turtle.forward()
- else --if direction == 1 then
- return turtle.up()
- end
- end
- --guarantees that the turtle will
- --move in the specified direction
- --simply waits if unable to
- function safeMove(direction)
- --check fuel
- refuel() --this has built in check
- --if turtle doesnt move in direction
- while not moveDirection(direction) do
- --attempt to dig in that direction
- --if it fails
- while not safeDig(direction) do
- --try again later
- print("Error: Unable to break block")
- print("and therefore cannot move.")
- print("Waiting 30s for assistance")
- sleep(30)
- end
- end
- --turtle has moved now update variable
- data["moveDistLeft"] = data["moveDistLeft"] - 1
- --and update the file.
- updateFile()
- return true
- end
- --digs a column
- --of length length
- --in directions stated
- --may dig fwd anyway for turtle moves
- function digColumn(length, down, fwd, up)
- --update the state variable to 'column'
- data["state"] = 0
- --update the remaining movement variable
- data["moveDistLeft"] = length
- --and update the file
- updateFile()
- --for all but the end of the column
- while length > 1 do
- --dig in all specified directions
- safeDigMulti(down, fwd, up)
- --then move forward 1
- safeMove(0)
- --update the 'length dug' variable
- length = length - 1
- --update the movement variable
- data["moveDistLeft"] = length
- --and update the file
- updateFile()
- end
- --now dig the last bit of the column
- --ie. every direction specified
- --except forwards
- safeDigMulti(down, false, up)
- --update the file to say
- --we are at the end of the column
- data["moveDistLeft"] = 0
- updateFile()
- end
- --digs a corner, for linking two columns
- --left = -1
- --right = 1
- function digCorner(direction)
- --update state variable to 'corner'
- data["state"] = 1
- --update orientation var to '1 turn'
- data["numTurnsReq"] = 1
- --update dist remain var to '1'
- data["moveDistLeft"] = 1
- --update the file
- updateFile()
- --turn in direction specified
- if direction < 0 then
- turtle.turnLeft()
- data["numTurnsReq"] = 0
- updateFile()
- else
- turtle.turnRight()
- data["numTurnsReq"] = 0
- updateFile()
- end
- --safeMove will dig whatever's needed
- --move forwards 1
- safeMove(0) --this updates file automatically
- --indicate we need to turn again
- data["numTurnsReq"] = 1
- updateFile()
- --turn in specified direction
- if direction < 0 then
- turtle.turnLeft()
- data["numTurnsReq"] = 0
- updateFile()
- else
- turtle.turnRight()
- data["numTurnsReq"] = 0
- updateFile()
- end
- end
- --digs a rectangle
- --of specified length
- --of specified width
- --in direction dir (l/r = -1, 1)
- --with specified layers
- --does not turn around at the end!
- function digRect(length, width, dir, down, fwd, up)
- --create a counter for the width
- local w = width
- --update the 'currCol' variable
- data["currCol"] = w
- --and the dist remain variable
- data["moveDistLeft"] = length
- --and the state to column
- data["state"] = 0
- --update file
- updateFile()
- --while we are not on the last column
- while w > 1 do
- --mine a column
- digColumn(length, down, fwd, up)
- --mine a corner in correct direction
- --on the even columns
- if ((data["width"] - w)%2) == 1 then
- --turn in opposite direction of dir
- digCorner(-dir)
- --on the odd columns
- else --w%2 == 1 then
- --turn in same direction as dir
- digCorner(dir)
- end
- --finished digging the corner
- --update state, moveDist, currCol
- data["state"] = 0
- data["moveDistLeft"] = length
- data["currCol"] = w - 1
- updateFile()
- --update w to show column was mined
- w = w - 1
- end
- --then mine the last column
- --it deals with file changes
- digColumn(length, down, fwd, up)
- end
- --moves the turtle to the next layer
- --which is 'depth' blocks down
- function nextLayer(depth)
- --update state to depth change
- data["state"] = 2
- --update movement left
- data["moveDistLeft"] = depth
- --update orientation
- data["numTurnsReq"] = 2
- --update file
- updateFile()
- --turn 180 for next layer
- turtle.turnLeft()
- --update file
- data["numTurnsReq"] = 1
- updateFile()
- turtle.turnLeft()
- --update file
- data["numTurnsReq"] = 0
- updateFile()
- --move down specified amount
- while depth > 0 do
- safeMove(-1)
- data["currDepth"] = data["currDepth"] - 1
- updateFile()
- depth = depth - 1
- end
- --update the direction
- if (data["width"] % 2) == 0 then
- data["currDir"] = 0 - data["currDir"]
- end
- data["currLayer"] = data["currLayer"] + 1
- updateFile()
- end
- --digs a length by width hole
- --with a depth of depth
- --in direction dir
- function digHole(length, width, dir, depth)
- --update the file as best we can
- data["shouldRecover"] = 1
- data["length"] = length
- data["width"] = width
- data["moveDistLeft"] = length
- data["currCol"] = 1
- data["currDir"] = dir
- data["currDepth"] = depth
- data["state"] = 0
- data["numTurnsReq"] = 0
- data["lastEChestDir"] = 0
- data["currLayer"] = 1 --not really needed
- updateFile()
- local layer = 1
- local depthRem = depth
- --while we are not near the bottom
- while depthRem > 3 do
- --first dig the CORRECT rect
- --nextLayer() deals with dir
- digRect(length, width, data["currDir"], true, true, true)
- --move to the next layer
- nextLayer(3)
- depthRem = depthRem - 3
- layer = layer + 1
- end
- --if there's three layers to bedrock
- if depthRem == 3 then
- --same as before, but go down 2
- digRect(length, width, data["currDir"], true, true, true)
- --digRect done, now go down 2
- nextLayer(2)
- depthRem = depthRem - 2
- layer = layer + 1
- --should move on to the
- --depthRem == 1 if statement
- end
- --if depth remaining == 2
- if depthRem == 2 then
- --same dig rect as before
- --but no more going down
- digRect(length, width, data["currDir"], true, true, true)
- data["shouldRecover"] = 0
- updateFile()
- --all done!
- return true
- end
- --if depth remaining == 1
- if depthRem == 1 then
- --same as 2 except
- --do not dig down on digRect
- digRect(length, width, data["currDir"], false, true, true)
- --all done!
- data["shouldRecover"] = 0
- updateFile()
- return true
- end
- end
- --gets the direction of the next rect
- --only used in recovery function
- function getRectDir()
- --if the width is even
- if (data["width"] % 2) == 0 then
- --if the layer is even
- if (data["currLayer"] % 2) == 0 then
- return (0 - data["currDir"])
- else
- return data["currentDir"]
- end
- else
- return data["currDir"]
- end
- end
- --gets the direction of the current turn
- --mostly used for recover function
- function getTurnDir()
- --if even column
- if (data["width"] - data["currCol"]) % 2 == 1 then
- return (0 - data["currDir"])
- else --odd column
- return data["currDir"]
- end
- end
- --if the turtle was mid way through
- --digging a hole and stopped
- --for whatever reason, this function
- --will carry on from where it stopped
- function recover()
- --load all the variables
- loadFile()
- --if we shouldnt recover, return false
- if data["shouldRecover"] == 0 then
- return false
- --we should recover
- else
- --if we are refuelling
- if isUsingEChest(15) == true then
- --recover the enderchest by:
- --select inventory slot
- turtle.select(15)
- --dig in direction of EChest
- digDirection(data["lastEChestDir"])
- --reselect slot 1
- turtle.select(1)
- --restart refuelling if we need to
- refuel()
- end
- --if we are unloading
- if isUsingEChest(16) == true then
- --dig the enderchest
- turtle.select(16)
- digDirection(data["lastEChestDir"])
- --then restart unloading
- unload()
- end
- --if we are on a column
- if data["state"] == 0 then
- --finish the column
- --if we are at min depth
- if data["currDepth"] == 1 then
- --mine only above and in front
- digColumn(data["moveDistLeft"], false, true, true)
- else --any other depth
- digColumn(data["moveDistLeft"], true, true, true)
- end
- --set up the next stage
- --if this was the last column
- if data["currCol"] == 1 then
- --if we are not near the end
- if data["currDepth"] > 3 then
- --set up vars as if
- --newLayer(3) was called
- data["state"] = 2
- data["moveDistLeft"] = 3
- data["numTurnsReq"] = 2
- updateFile()
- --if we are on layer 3
- elseif data["currDepth"] == 3 then
- --newlayer(2)
- data["state"] = 2
- data["moveDistLeft"] = 2
- data["numTurnsReq"] = 2
- updateFile()
- --if we are on layer 1 or 2
- else
- --finished digging!
- data["shouldRecover"] = 0
- updateFile()
- return true
- end
- --if this is not the last column
- else
- --update Vars to say
- --we are starting a corner
- data["state"] = 1
- data["numTurnsReq"] = 1
- data["moveDistLeft"] = 1
- end
- end
- --if we are on a corner
- if data["state"] == 1 then
- --if we have not moved forwards yet
- if data["moveDistLeft"] == 1 then
- --if we have not turned
- if data["numTurnsReq"] == 1 then
- --turn
- turnDirection(getTurnDir())
- --update the file
- data["numTurnsReq"] = 0
- updateFile()
- end
- --if we have turned
- if data["numTurnsReq"] == 0 then
- safeMove(0) --partially deals with file
- data["numTurnsReq"] = 1
- updateFile()
- end
- end
- --if we have moved forwards already
- if data["moveDistLeft"] == 0 then
- --if we have not turned yet
- if data["numTurnsReq"] == 1 then
- turnDirection(getTurnDir())
- --update File
- data["numTurnsReq"] = 0
- updateFile()
- end
- --if we have turned already
- if data["numTurnsReq"] == 0 then
- --we are on the next column
- data["moveDistleft"] = data["length"]
- data["currCol"] = data["currCol"] - 1
- data["state"] = 0
- updateFile()
- --set up the var's to dig rect
- end
- end
- end
- --if we are moving layers
- if data["state"] == 2 then
- --complete turns (left always?)
- if data["numTurnsReq"] == 2 then
- turtle.turnLeft()
- data["numTurnsReq"] = 1
- updateFile()
- end
- if data["numTurnsReq"] == 1 then
- turtle.turnLeft()
- data["numTurnsReq"] = 0
- updateFile()
- end
- --complete downward movement
- while data["moveDistLeft"] > 0 do
- safeMove(-1) --deals with file stuff
- data["currDepth"] = data["currDepth"] - 1
- updateFile()
- end
- --update file for new layer
- data["currLayer"] = data["currLayer"] + 1
- data["currCol"] = data["width"]
- data["moveDistLeft"] = data["length"]
- if (data["width"] % 2) == 0 then
- data["currDir"] = (0 - data["currDir"])
- end
- data["state"] = 0
- updateFile()
- end
- --we are now definitly at the
- --beginning of a column
- --digRect (remaining width)
- --if we arent at the very bottom
- if data["currDepth"] ~= 1 then
- --finish the current layer
- --by doing
- digRect(data["length"], data["currCol"], getTurnDir(), true, true, true)
- --if we are on the bottom layer
- else
- digRect(data["length"], data["currCol"], getTurnDir(), false, true, true)
- end
- --if this was the last rect to dig
- if data["currDepth"] < 3 then
- data["shouldRecover"] = 0
- updateFile()
- return true
- --if this is the last but one
- --rect to dig
- elseif data["currDepth"] == 3 then
- --move to next layer
- nextLayer(2)
- digRect(data["length"], data["width"], data["currDir"], false, true, true)
- --done!
- data["shouldRecover"] = 0
- updateFile()
- --there are many more rect's to dig
- else
- --move to next layer
- nextLayer(3)
- --continue with dig hole
- digHole(data["length"], data["width"], data["currDir"], data["currDepth"])
- data["shouldRecover"] = 0
- updateFile()
- end
- end
- end
- --actual turtle program
- --checks if it should recover
- --and if it should, it does so
- recover()
- --explanation text
- print("Turtle has completed previous tasks")
- print("and is ready to mine a new hole")
- print("")
- --get length
- print("Please enter the forward distance")
- print("of the hole you want to dig;")
- local inputLength = tonumber(io.read())
- --get width
- print("Now please enter the width of the")
- print("hole you would like to dig")
- local inputWidth = tonumber(io.read())
- --get direction
- print("Please choose which direction you")
- print("would like the turtle to dig in")
- print("Enter -1 for left")
- print("Enter 1 for right")
- local inputDir = tonumber(io.read())
- --get depth
- print("Finally, please enter the y level")
- print("of the turtle")
- local inputDepth = tonumber(io.read())
- --apply all values
- print("Turtle will now start digging")
- sleep(1)
- digHole(inputLength, inputWidth, inputDir, inputDepth)
Advertisement
Add Comment
Please, Sign In to add comment