Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get xS8Fy4kG quarry
- -- version 1.1.0
- local direction = {
- ["forw"] = 1,
- ["right"] = 2,
- ["back"] = 3,
- ["left"] = 4,
- }
- local dir = direction.forw
- local pos = {
- ["x"] = 0,
- ["y"] = 0,
- ["z"] = 0,
- }
- local name = "quarry"
- local done = false
- globalState = 0
- aState = 0 --состояние автомата
- tmpState = 0
- local lenght,width,height = 0,0,0
- local xt,yt,zt,dt = 0,0,0,direction.forw
- mode = 1
- local function saveState()
- name = shell.getRunningProgram()
- local file = fs.open(name..".save","w")
- file.writeLine(mode)
- file.writeLine(globalState)
- file.writeLine(pos.x)
- file.writeLine(pos.y)
- file.writeLine(pos.z)
- file.writeLine(dir)
- file.writeLine(tmpState)
- file.writeLine(aState)
- file.writeLine(xt)
- file.writeLine(yt)
- file.writeLine(zt)
- file.writeLine(dt)
- file.writeLine(lenght)
- file.writeLine(width)
- file.writeLine(height)
- file.writeLine(done)
- file.close()
- end
- local function readState()
- name = shell.getRunningProgram()
- if not fs.exists(name..".save") then
- return false
- end
- local file = fs.open(name..".save","r")
- mode = tonumber(file.readLine())
- globalState = tonumber(file.readLine())
- pos.x = tonumber(file.readLine())
- pos.y = tonumber(file.readLine())
- pos.z = tonumber(file.readLine())
- dir = tonumber(file.readLine())
- tmpState = tonumber(file.readLine())
- aState = tonumber(file.readLine())
- xt = tonumber(file.readLine())
- yt = tonumber(file.readLine())
- zt = tonumber(file.readLine())
- dt = tonumber(file.readLine())
- lenght = tonumber(file.readLine())
- width = tonumber(file.readLine())
- height = tonumber(file.readLine())
- done = file.readLine()
- if done == "true" then
- done = true
- else
- done = false
- end
- file.close()
- return true
- end
- local function setStartup()
- if fs.exists("startup") then
- -- if fs.exists("startup.old") then
- fs.delete("startup")
- -- else
- -- shell.run("move","startup","startup.old")
- -- end
- end
- name = shell.getRunningProgram()
- local file = fs.open("startup","w")
- file.writeLine("shell.run(\""..name.."\")")
- file.close()
- end
- local function delStartup()
- if fs.exists("startup") then
- fs.delete("startup")
- end
- -- if fs.exists("startup.old") then
- -- shell.run("move","startup.old","startup")
- -- end
- name = shell.getRunningProgram()
- if fs.exists(name..".save") then
- fs.delete(name..".save")
- end
- end
- function refuel(ammount)
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- local needed = 0
- if ammount == nil then
- needed = math.abs(pos.x) + math.abs(pos.y) + math.abs(pos.z) + 3
- else
- needed = ammount
- end
- if turtle.getFuelLevel() < needed then
- for i=1,16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if turtle.refuel(64) then
- print("turtle get fuel")
- while turtle.getItemCount(i) > 0 and turtle.getFuelLevel() < needed do
- print("turtle get fuel")
- turtle.refuel(64)
- end
- if turtle.getFuelLevel() >= needed then
- turtle.select(1)
- return true
- end
- end
- end
- end
- turtle.select(1)
- return false
- end
- return true
- end
- local function inventoryFull()
- local bFull = true
- for n=1,16 do
- if turtle.getItemCount(n) == 0 then
- bFull = false
- end
- end
- if bFull then
- print( "No empty slots left." )
- return true
- end
- return false
- end
- local function unload()
- print( "Unloading items..." )
- local n = 1
- if mode == 1 then
- if not turtle.detect() then
- print("no detect chest")
- while not turtle.detect() do
- os.sleep(1)
- end
- end
- while n <= 16 do
- turtle.select(n)
- if turtle.getItemCount(n) > 0 and not turtle.drop() then
- print("Don't unload to chest")
- os.sleep(5)
- else
- n = n + 1
- end
- end
- else
- if not turtle.detectUp() then
- print("no detect chest")
- while not turtle.detectUp() do
- os.sleep(1)
- end
- end
- n = 2
- while n <= 16 do
- turtle.select(n)
- if turtle.getItemCount(n) > 0 and not turtle.dropUp() then
- print("Don't unload to chest")
- os.sleep(5)
- else
- n = n + 1
- end
- end
- end
- turtle.select(1)
- end
- local goTo
- local function returnToEnderChest()
- if globalState == 1 or globalState == 2 then
- globalState = 5
- saveState()
- end
- if globalState == 5 then
- turtle.select(1)
- while turtle.getItemCount(1) < 0 do
- print("Don't found chest" )
- print("Press any key to continue")
- local input = read()
- end
- while true do
- if turtle.detectUp() then --обнаружен блок, сломать
- if turtle.digUp() then
- --print("detect block")
- else
- print("ERROR! Detect bedrock or unbreaking block.")
- error()
- return
- end
- elseif turtle.attackUp() then -- Сверху нет блока, проверяем есть ли там существо
- print("detect monster or player")
- else -- Ставим сундук
- if turtle.placeUp() then
- -- it's ok!
- globalState = 6
- saveState()
- break
- else
- print("detect unknowing object")
- os.sleep(0.5)
- end
- end
- end
- end
- if globalState == 6 then
- unload()
- turtle.select(1)
- if turtle.getItemCount(1) > 0 then
- turtle.dropUp()
- end
- turtle.digUp()
- globalState = 2
- saveState()
- end
- end
- local function returnToChest(drop)
- if globalState == 1 or globalState == 2 then
- xt = pos.x
- yt = pos.y
- zt = pos.z
- dt = dir
- globalState = 3
- saveState()
- end
- if globalState == 3 then
- print( "Returning to surface..." )
- goTo( 0,0,0,direction.back)
- if drop then
- unload()
- end
- globalState = 4
- saveState()
- end
- if globalState == 4 then
- local fuelNeeded = (math.abs(xt) + math.abs(yt) + math.abs(zt) + 3)*2
- if not refuel( fuelNeeded ) then
- print( "Waiting for fuel" )
- while not refuel( fuelNeeded ) do
- sleep(1)
- end
- end
- print( "Resuming mining..." )
- goTo( xt,yt,zt,dt )
- globalState = 2
- xt = 0
- yt = 0
- zt = 0
- dt = direction.forw
- saveState()
- end
- end
- local function returnSupplies()
- if mode == 1 then
- returnToChest(true)
- else
- returnToEnderChest()
- end
- end
- local function turnRight()
- dir=dir+1
- if dir==5 then dir = direction.forw end
- saveState()
- turtle.turnRight()
- end
- local function turnLeft()
- dir=dir-1
- if dir==0 then dir = direction.left end
- saveState()
- turtle.turnLeft()
- end
- local function checkBorder()
- if (math.abs(pos.x) >= width) or (math.abs(pos.z) >= lenght) then
- print("going beyond a border")
- delStartup()
- error()
- end
- end
- local function down()
- while true do
- if turtle.detectDown() then
- if turtle.digDown() then
- -- print("detect block")
- else
- print("detect bedrock or unbreaking block")
- return false
- end
- elseif turtle.attackDown() then
- print("detect monster or player")
- else
- local ty = pos.y
- pos.y = pos.y - 1
- saveState()
- if turtle.down() then
- -- print("it's ok!")
- checkBorder()
- return true
- else
- print("detect unknowing object")
- pos.y = ty
- saveState()
- os.sleep(0.5)
- end
- end
- end
- end
- local function up()
- while true do
- if turtle.detectUp() then --обнаружен блок, сломать
- if turtle.digUp() then
- -- print("detect block")
- else
- print("detect bedrock or unbreaking block")
- return false
- end
- elseif turtle.attackUp() then -- Сверху нет блока, проверяем есть ли там существо
- print("detect monster or player")
- else -- Передвигаемся вверх
- local ty = pos.y
- pos.y = pos.y + 1
- saveState()
- if turtle.up() then
- -- it's ok!
- -- print("it's ok!")
- checkBorder()
- return true
- else
- -- backup
- print("detect unknowing object")
- pos.y = ty
- saveState()
- os.sleep(0.5)
- end
- end
- end
- end
- local function forward(mined)
- if mined then
- if turtle.detectUp() then
- turtle.digUp()
- if inventoryFull() then
- returnSupplies()
- end
- end
- if turtle.detectDown() then
- turtle.digDown()
- if inventoryFull() then
- returnSupplies()
- end
- end
- end
- while true do
- if turtle.detect() then --обнаружен блок, сломать
- if turtle.dig() then
- -- print("detect block")
- if mined and inventoryFull() then
- returnSupplies()
- end
- else
- print("detect bedrock or unbreaking block")
- return false
- end
- elseif turtle.attack() then -- Впереди нет блока, проверяем есть ли там существо
- print("detect monster or player")
- if mined and inventoryFull() then
- returnSupplies()
- end
- else -- Передвигаемся вперед
- if mined then -- Уберем гравий
- while turtle.detectUp() do
- turtle.digUp()
- os.sleep(0.5)
- end
- end
- local tz,tx = pos.z,pos.x
- if dir == direction.forw then
- pos.z=pos.z+1
- elseif dir == direction.right then
- pos.x=pos.x+1
- elseif dir == direction.back then
- pos.z=pos.z-1
- elseif dir == direction.left then
- pos.x=pos.x-1
- end
- saveState()
- if turtle.forward() then
- -- print("it's ok!")
- checkBorder()
- return true
- else
- -- backup
- if not turtle.detect() then
- print("detect unknowing object")
- end
- pos.z = tz
- pos.x = tx
- saveState()
- os.sleep(0.5)
- end
- end
- end
- end
- local function detectBedrock() --объехать бедрок
- turnLeft()
- turnLeft()
- while not up() do
- if not forward() then
- print("don't going to bedrock!")
- delStartup()
- error()
- return false
- -- не получилось объехать бедрок
- end
- end
- return true
- end
- function goTo( x, y, z, d)
- while pos.y < y do -- идем вверх
- if not up() then
- detectBedrock()
- end
- end
- if pos.x > x then
- while dir ~= direction.left do
- turnLeft()
- end
- while pos.x > x do
- forward(false)
- end
- end
- if pos.x < x then
- while dir ~= direction.right do
- turnRight()
- end
- while pos.x < x do
- forward(false)
- end
- end
- if pos.z > z then
- while dir ~= direction.back do
- turnRight()
- end
- while pos.z > z do
- forward(false)
- end
- end
- if pos.z < z then
- while dir ~= direction.forw do
- turnLeft()
- end
- while pos.z < z do
- forward(false)
- end
- end
- while pos.y > y do -- идем вниз
- down()
- end
- while d ~= dir do
- turnLeft()
- end
- end
- local function nextState()
- if aState == 0 then
- aState = 1
- elseif aState == 1 then
- if pos.z == lenght-1 and pos.x ~= width-1 then
- aState = 2
- elseif pos.z == lenght-1 and pos.x == width-1 then
- aState = 9
- end
- elseif aState == 2 then
- aState = 3
- elseif aState == 3 then
- if pos.z == 0 and pos.x ~= width-1 then
- aState = 4
- elseif pos.z == 0 and pos.x == width-1 then
- aState = 10
- end
- elseif aState == 4 then
- aState = 1
- elseif aState == 5 then
- if pos.z == lenght-1 and pos.x ~= 0 then
- aState = 6
- elseif pos.z == lenght-1 and pos.x == 0 then
- aState = 11
- end
- elseif aState == 6 then
- aState = 7
- elseif aState == 7 then
- if pos.z == 0 and pos.x ~= 0 then
- aState = 8
- elseif pos.z == 0 and pos.x == 0 then
- aState = 12
- end
- elseif aState == 8 then
- aState = 5
- elseif aState == 9 then
- aState = 7
- elseif aState == 10 then
- aState = 5
- elseif aState == 11 then
- aState = 3
- elseif aState == 12 then
- aState = 1
- end
- tmpState = 0
- saveState()
- return
- end
- local function ref()
- if not refuel() then
- print( "Not enough Fuel" )
- if mode == 1 then
- returnSupplies()
- else
- returnToEnderChest()
- returnToChest(false)
- end
- end
- end
- local function lower()
- if turtle.detectUp() then
- turtle.digUp()
- end
- while tmpState ~= 3 do
- ref()
- tmpState = tmpState + 1
- if not down() then
- done = true
- break
- end
- end
- end
- local function main()
- while not done do
- if globalState == 2 then
- if aState == 1 then
- ref()
- if not forward(true) then
- done = true
- break
- end
- elseif aState == 2 then
- while tmpState ~= 3 do
- if tmpState == 0 then
- tmpState = tmpState + 1
- turnRight()
- elseif tmpState == 1 then
- ref()
- tmpState = tmpState + 1
- if not forward(true) then
- done = true
- break
- end
- elseif tmpState == 2 then
- tmpState = tmpState + 1
- turnRight()
- end
- end
- elseif aState == 3 then
- ref()
- if not forward(true) then
- done = true
- break
- end
- elseif aState == 4 then
- while tmpState ~= 3 do
- if tmpState == 0 then
- tmpState = tmpState + 1
- turnLeft()
- elseif tmpState == 1 then
- ref()
- tmpState = tmpState + 1
- if not forward(true) then
- done = true
- break
- end
- elseif tmpState == 2 then
- tmpState = tmpState + 1
- turnLeft()
- end
- end
- elseif aState == 5 then
- ref()
- if not forward(true) then
- done = true
- break
- end
- elseif aState == 6 then
- while tmpState ~= 3 do
- if tmpState == 0 then
- tmpState = tmpState + 1
- turnLeft()
- elseif tmpState == 1 then
- ref()
- tmpState = tmpState + 1
- if not forward(true) then
- done = true
- break
- end
- elseif tmpState == 2 then
- tmpState = tmpState + 1
- turnLeft()
- end
- end
- elseif aState == 7 then
- ref()
- if not forward(true) then
- done = true
- break
- end
- elseif aState == 8 then
- while tmpState ~= 3 do
- if tmpState == 0 then
- tmpState = tmpState + 1
- turnRight()
- elseif tmpState == 1 then
- ref()
- tmpState = tmpState + 1
- if not forward(true) then
- done = true
- break
- end
- elseif tmpState == 2 then
- tmpState = tmpState + 1
- turnRight()
- end
- end
- elseif height+pos.y<3 then
- done = true
- break
- elseif aState == 9 then
- while dir ~= direction.back do
- turnLeft()
- end
- lower()
- elseif aState == 10 then
- while dir ~= direction.forw do
- turnLeft()
- end
- lower()
- elseif aState == 11 then
- while dir ~= direction.back do
- turnLeft()
- end
- lower()
- elseif aState == 12 then
- while dir ~= direction.forw do
- turnLeft()
- end
- lower()
- end
- nextState()
- elseif globalState == 3 or globalState == 4 or globalState == 5 or globalState == 6 then
- returnSupplies()
- end
- end
- saveState()
- print( "Returning to surface..." )
- -- Return to where we started
- goTo( 0,0,0,direction.back )
- if mode == 1 then
- unload()
- else
- returnToEnderChest()
- end
- goTo( 0,0,0,direction.forw )
- delStartup()
- end
- local function recover()
- if not readState() or globalState == 0 then
- print("Programm is not running")
- print( "Usage: quarry <lenght> <width> [<height>]" )
- return
- end
- if globalState == 1 then
- local done = false
- while tmpState ~= 1 do
- ref()
- if not down() then
- done = true
- break
- end
- tmpState=tmpState+1
- saveState()
- end
- globalState = 2
- saveState()
- end
- if globalState >= 2 then
- -- nextState()
- main()
- end
- end
- local function start()
- setStartup()
- globalState = 1 -- Поехали копать
- done = false
- while tmpState < 1 do
- ref()
- if not down() then
- done = true
- break
- end
- tmpState=tmpState+1
- saveState()
- end
- globalState = 2
- saveState()
- nextState()
- main()
- end
- function FuelInfo()
- local fuelLevel = turtle.getFuelLevel()
- local fuelNeed = 0
- local minFuel = lenght*width
- if fuelLevel ~= "unlimited" then
- if height == 256 then
- minFuel = minFuel*64
- else
- minFuel = minFuel*height
- end
- minFuel = math.ceil(minFuel/2)
- fuelNeed = minFuel - turtle.getFuelLevel()
- if fuelNeed < 0 then
- fuelNeed = 0
- end
- end
- print( "Fuel level: "..turtle.getFuelLevel())
- print( "Need fuel: "..fuelNeed)
- end
- local function GetFuel()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return
- end
- local minFuel = lenght*width
- if height == 256 then
- minFuel = minFuel*64
- else
- minFuel = minFuel*height
- end
- minFuel = math.ceil(minFuel/2)
- -- print( "Waiting for fuel level: "..minFuel)
- print( "Fuel level: "..math.ceil((turtle.getFuelLevel()*100)/minFuel).."%")
- local waiting=true
- parallel.waitForAll(
- function()
- while waiting do
- for i = 1,16 do
- if turtle.getItemCount(i)>0 then
- turtle.select(i)
- if turtle.refuel() then
- print( "Fuel level: "..math.ceil((turtle.getFuelLevel()*100)/minFuel).."%")
- end
- end
- end
- os.sleep(0.5)
- end
- end
- ,
- function()
- print("press any key to run quarry")
- local input = read()
- waiting = false
- term.clear()
- term.setCursorPos( 1, 1 )
- turtle.select(1)
- if mode == 2 and turtle.getItemCount(1) < 1 then
- print("Waiting for get chest to first slot")
- while turtle.getItemCount(1) < 1 do
- os.sleep(1)
- end
- end
- -- print( "Fuel level: "..turtle.getFuelLevel())
- print( "Excavating...")
- start()
- return
- end
- )
- end
- local tArgs = { ... }
- if #tArgs == 0 then
- print( "Recovering...")
- recover()
- return
- elseif #tArgs == 2 or #tArgs == 3 or #tArgs == 4 then
- lenght = tonumber( tArgs[1] )
- width = tonumber( tArgs[2] )
- height = 256
- local arg = ""
- if lenght < 1 or width < 1 or lenght > 255 or width > 255 then
- print( "Quarry size must be positive" )
- return
- end
- if #tArgs == 3 then
- arg = tonumber( tArgs[3] )
- if tArgs[3] == "-e" then
- mode = 2
- elseif tArgs[3] == "-f" then
- FuelInfo()
- return
- elseif arg ~= nill then
- height = arg
- if height < 1 or height > 255 then
- print( "Quarry size must be positive" )
- return
- end
- else
- print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
- return
- end
- elseif #tArgs == 4 then
- arg = tonumber( tArgs[3] )
- if arg ~= nill then
- height = arg
- if height < 1 or height > 255 then
- print( "Quarry size must be positive" )
- return
- end
- else
- print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
- return
- end
- if tArgs[4] == "-e" then
- mode = 2
- elseif tArgs[4] == "-f" then
- FuelInfo()
- return
- else
- print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
- return
- end
- end
- --starting
- GetFuel()
- return
- else
- print( "Usage: quarry <lenght> <width> [<height>] [<key>]; key: \"-e\", \"-f\"" )
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement