Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local S_PREP = "PREP"
- local S_MINING = "MINING"
- local S_REFUEL = "REFUEL"
- local S_INVENTORY = "INVENTORY"
- local S_FINISH = "FINISH"
- local S_ADVANCE = "ADVANCE"
- -- Chests in slot 1, fuel in slot 2
- local IgnoreSlots = 3
- local chestIndex = 1
- local CHEST = "minecraft:chest"
- local fuelIndex = 2
- local bucketIndex = 3
- local DepthTarget = 100
- local fudgeFactor = 10
- local mov = {}
- local rot = {}
- local dig = {}
- local status = {}
- local statusFileName = "mstatus"
- local function loadTable(name)
- local file = fs.open(name,"r")
- if file == nil then
- return
- end
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- local function saveTable(table,name)
- local file = fs.open(name,"w")
- file.write(textutils.serialize(table))
- file.close()
- end
- local function saveStatus()
- saveTable(status, statusFileName)
- end
- local function loadStatus()
- status = loadTable(statusFileName)
- end
- local function isChest()
- local exists, data = turtle.inspectUp()
- if not exists then return false end
- return string.find(data.name, CHEST) ~= nil
- end
- local DONT_NEED_FUEL = 0
- local GOT_SOME_FUEL = 1
- local OUT_OF_FUEL = -1
- local function Refuel()
- currentFuelRequired = (40 + (DepthTarget-status.progress)*3)/20
- if turtle.getFuelLevel() < currentFuelRequired then
- turtle.select(fuelIndex)
- local r = turtle.refuel(currentFuelRequired)
- if r then
- return GOT_SOME_FUEL
- elseif not r then
- return OUT_OF_FUEL
- end
- else
- return DONT_NEED_FUEL
- end
- end
- local function CheckRefuel()
- if Refuel() == OUT_OF_FUEL then
- print("***Out of fuel***")
- status.status = S_STOP
- return
- end
- local count = 0
- while Refuel() == GOT_SOME_FUEL and count < 16 do
- count = count + 1
- print("refueling")
- end
- end
- mov.forward = function(times)
- local boolean v = false
- for i=1,times do
- if turtle.detect() == false then
- while not turtle.forward() do
- CheckRefuel()
- sleep(1)
- end
- v = true
- else
- v = false
- return v
- end
- end
- return v
- end
- mov.back = function(times)
- local boolean v = false
- for i=1,times do
- while not turtle.back() do
- CheckRefuel()
- sleep(1)
- end
- v = true
- end
- return v
- end
- mov.up = function(times)
- -- print("mov.up")
- -- print("-->")
- local boolean v = false
- for i=1,times do
- if turtle.detectUp() == false then
- local attempts = 0
- while turtle.up() == false and attempts < 10 do
- -- print("failed to move up")
- attempts = attempts + 1
- CheckRefuel()
- sleep(1)
- if attempts > 8 then
- -- print("LOTS of fails move up")
- if status.status == S_FINISH then
- status.status = S_STOP
- else
- status.status = S_FINISH
- end
- return false
- end
- end
- v = true
- else
- v = false
- -- print("<--")
- return v
- end
- end
- -- print("<--")
- return v
- end
- mov.down = function(times)
- -- print("mov.down")
- -- print("-->")
- local boolean v = false
- for i=1,times do
- if turtle.detectDown() == false then
- local attempts = 0
- while turtle.down() == false and attempts < 10 do
- -- print("failed to move down")
- attempts = attempts + 1
- CheckRefuel()
- sleep(1)
- if attempts > 8 then
- -- print("LOTS of fails move down")
- if status.status == S_FINISH then
- status.status = S_STOP
- else
- status.status = S_FINISH
- end
- return false
- end
- end
- v = true
- else
- v = false
- -- print("<--")
- return v
- end
- end
- -- print("<--")
- return v
- end
- mov.place = function()
- while not turtle.place() do
- sleep(1)
- end
- return true
- end
- rot.right = function()
- while not turtle.turnRight() do
- sleep(1)
- end
- return true
- end
- rot.left = function()
- while not turtle.turnLeft() do
- sleep(1)
- end
- return true
- end
- dig.forward = function()
- print("dig.forward")
- -- print("-->")
- if turtle.detect() then
- while not turtle.dig() do
- sleep(1)
- end
- -- print("<--")
- return true
- else
- -- print("No Block to mine forward")
- -- print("<--")
- return false
- end
- end
- dig.up = function()
- print("dig.up")
- -- print("-->")
- if turtle.detectUp() then
- while not turtle.digUp() do
- sleep(1)
- end
- -- print("<--")
- return true
- else
- print("No Block to mine up")
- -- print("<--")
- return false
- end
- end
- dig.down = function()
- print("In dig.down")
- -- print("-->")
- if turtle.detectDown() then
- while not turtle.digDown() do
- sleep(1)
- end
- -- print("<--")
- return true
- else
- print("No Block to mine down")
- -- print("<--")
- return false
- end
- end
- local function DigMoveForward()
- print("In DigMoveForward")
- -- print("-->")
- if mov.forward(1) == false then
- dig.forward()
- sleep(0.5)
- DigMoveForward()
- end
- -- print("<--")
- end
- local function DigMoveUp()
- print("In DigMoveUp")
- -- print("-->")
- local count = 0
- while mov.up(1) == false and count < 10 do
- count = count + 1
- dig.up()
- sleep(0.5)
- end
- if count > 5 then
- -- print("had a hard time moving up")
- end
- -- print("<--")
- end
- local function DigMoveDown()
- print("In DigMoveDown")
- -- print("-->")
- local count = 0
- while mov.down(1) == false and count < 10 do
- dig.down()
- sleep(0.5)
- end
- if count > 5 then
- -- print("had a hard time moving up")
- end
- -- print("<--")
- end
- local function ReturnBack()
- print("In ReturnBack")
- -- print("-->")
- turtle.select(1)
- local count = 0
- while not isChest() and count < DepthTarget + fudgeFactor do
- count = count + 1
- print(count)
- mov.up(1)
- end
- -- print("<--")
- end
- local function DepositChest()
- print("In DepositChest")
- -- print("-->")
- if not isChest then
- print("ERROR: Should be below chest.")
- status.status = S_STOP
- -- print("<--")
- return
- end
- print("Depositing the Load...")
- for i = IgnoreSlots + 1,16 do
- turtle.select(i)
- turtle.dropUp()
- end
- print("Out DepositChest")
- end
- local function InventoryFull()
- print("In InventoryFull")
- -- print("-->")
- SlotsFull = 0
- for i=1,16 do
- if turtle.getItemCount(i) > 0 then
- SlotsFull = SlotsFull + 1
- end
- end
- if SlotsFull == 16 then
- -- print("<--")
- return true;
- else
- -- print("<--")
- return false;
- end
- end
- local function CheckInventory()
- if InventoryFull() then
- print("***Inventory Full***")
- ReturnBack()
- DepositChest()
- for i=1,status.progress do
- DigMoveDown()
- end
- end
- end
- local function Harvest()
- local exist,block = turtle.inspect()
- if not exist then
- return
- end
- if string.find(block.name, "flowing_water") then
- turtle.select(1)
- turtle.place()
- elseif string.find(block.name, "flowing_lava") then
- if bucketIndex > 0 then
- turtle.select(bucketIndex)
- turtle.place()
- end
- turtle.select(1)
- turtle.place()
- end
- while turtle.suck() == true do
- CheckInventory()
- end
- dig.forward()
- end
- local function Progress()
- DigMoveDown()
- status.progress = status.progress + 1
- BlocksTillEnd = DepthTarget - status.progress
- end
- local function Finish()
- print("In Finish")
- -- print("-->")
- ReturnBack()
- DepositChest()
- status.status = S_ADVANCE
- -- print("<--")
- end
- local function NextColumn()
- -- turtle.select(1)
- -- if not turtle.compareUp() then
- -- rot.right()
- -- rot.right()
- -- while not turtle.compareUp() do
- -- DigMoveForward()
- -- end
- -- rot.right()
- -- rot.right()
- -- end
- for i=1,4 do
- DigMoveForward()
- end
- status.status = S_PREP
- end
- local function StartMining()
- print("In StartMining")
- -- print("-->")
- sleep(1)
- BlocksTillEnd = DepthTarget - status.progress
- while BlocksTillEnd > 0 do
- CheckRefuel()
- if status.status == S_STOP then
- return
- end
- CheckInventory()
- Harvest()
- for i=1,4 do
- Progress()
- end
- print("Depth: ")
- print(status.progress)
- end
- Harvest()
- status.status = S_FINISH
- -- print("<--")
- end
- local function Prepare()
- status.previous = ""
- status.progress = 0
- local data = turtle.getItemDetail(chestIndex)
- if data ~= nil and data.name ~= CHEST then
- print("need more chests")
- return
- end
- local FuelCount = turtle.getItemCount(fuelIndex)
- if FuelCount == 0 then
- print("please put fuel in the last slot of the Turtle.")
- return
- else
- if FuelCount < 10 then
- print("Please put at least 10 of the fuel you are using in the Turtle.")
- return
- end
- end
- CheckRefuel()
- if status.status == S_STOP then
- return
- end
- turtle.digUp()
- turtle.select(1)
- local result = turtle.placeUp()
- if not result then
- status.status = S_ADVANCE
- return
- end
- status.status = S_MINING
- end
- function Main()
- while status.status ~= S_STOP do
- saveStatus()
- sleep(1)
- if status.status == S_PREP then
- Prepare()
- elseif status.status == S_MINING then
- StartMining()
- elseif status.status == S_FINISH then
- Finish()
- elseif status.status == S_ADVANCE then
- NextColumn()
- else
- print("Unknown status: "..status.status)
- status.status = S_STOP
- end
- end
- ReturnBack()
- end
- -- ON LAUNCH
- loadStatus()
- if status == nil then
- status = { }
- status.status = S_PREP
- status.previous = ""
- status.progress = 0
- end
- if status.status ~= S_PREP and status.status ~= S_ADVANCE then
- turtle.select(fuelIndex)
- turtle.refuel(2)
- ReturnBack()
- end
- Main()
Advertisement
Add Comment
Please, Sign In to add comment