Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- quarry for Opencomputers !!!in CFG must limitFlightHeight=256
- -- version 1.0.0
- local component = require("component")
- local computer = require("computer")
- local robot = require("robot")
- local shell = require("shell")
- local sides = require("sides")
- local term = require("term")
- local fs = require("filesystem")
- local process = require("process")
- local io = require("io")
- if not component.isAvailable("robot") then
- io.stderr:write("can only run on robots")
- return
- end
- 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 = process.running(1)..".save"
- file = io.open(name,"w")
- file:write(mode.."\n"..globalState.."\n"..pos.x.."\n"..pos.y.."\n"..pos.z.."\n"..dir.."\n"..tmpState.."\n"..aState.."\n"..xt.."\n"..yt.."\n"..zt.."\n")
- file:write(dt.."\n"..lenght.."\n"..width.."\n"..height.."\n"..tostring(done).."\n")
- file:close()
- end
- local function readState()
- name = process.running(1)..".save"
- if not fs.exists(name) then
- return false
- end
- file = io.open(name,"r")
- mode=file:read()+0
- globalState=file:read()+0
- pos.x=file:read()+0
- pos.y=file:read()+0
- pos.z=file:read()+0
- dir=file:read()+0
- tmpState=file:read()+0
- aState=file:read()+0
- xt=file:read()+0
- yt=file:read()+0
- zt=file:read()+0
- dt=file:read()+0
- lenght=file:read()+0
- width=file:read()+0
- height=file:read()+0
- done=file:read()
- file:close()
- if done == "true" then
- done = true
- else
- done = false
- end
- return true
- end
- local function setStartup()
- if fs.exists("startup") then
- -- if fs.exists("startup.old") then
- fs.remove("startup")
- -- else
- -- shell.run("move","startup","startup.old")
- -- end
- end
- name = process.running(1)
- file = io.open("startup","w")
- file:write("shell.run(\""..name.."\")")
- file:close()
- end
- local function delStartup()
- if fs.exists("startup") then
- fs.remove("startup")
- end
- -- if fs.exists("startup.old") then
- -- shell.run("move","startup.old","startup")
- -- end
- name = process.running(1)..".save"
- if fs.exists(name) then
- fs.remove(name)
- end
- end
- local function inventoryFull()
- local bFull = true
- -- for n=1,16 do
- for n=1,8 do
- if robot.count(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 robot.detect() then
- print("no detect chest")
- while not robot.detect() do
- os.sleep(1)
- end
- end
- while n <= 16 do
- robot.select(n)
- if robot.count(n) > 0 and not robot.drop() then
- print("Don't unload to chest")
- os.sleep(5)
- else
- n = n + 1
- end
- end
- else
- if not robot.detectUp() then
- print("no detect chest")
- while not robot.detectUp() do
- os.sleep(1)
- end
- end
- n = 2
- while n <= 16 do
- robot.select(n)
- if robot.count(n) > 0 and not robot.dropUp() then
- print("Don't unload to chest")
- os.sleep(5)
- else
- n = n + 1
- end
- end
- end
- robot.select(1)
- end
- local goTo
- 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()
- robot.turnRight()
- end
- local function turnLeft()
- dir=dir-1
- if dir==0 then dir = direction.left end
- savestate()
- robot.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 robot.detectDown() then
- if robot.swingDown() then
- -- print("detect block")
- else
- print("detect bedrock or unbreaking block")
- return false
- end
- elseif robot.swingDown() then
- print("detect monster or player")
- else
- local ty = pos.y
- pos.y = pos.y - 1
- savestate()
- if robot.down() then
- -- print("it's ok!")
- checkBorder()
- return true
- else
- print("detect unknowing object DOWN or limitFlightHeight=256")
- pos.y = ty
- savestate()
- os.sleep(0.5)
- end
- end
- end
- end
- local function up()
- while true do
- if robot.detectUp() then --обнаружен блок, сломать
- if robot.swingUp() then
- -- print("detect block")
- else
- print("detect bedrock or unbreaking block")
- return false
- end
- elseif robot.swingUp() then -- Сверху нет блока, проверяем есть ли там существо
- print("detect monster or player")
- else -- Передвигаемся вверх
- local ty = pos.y
- pos.y = pos.y + 1
- savestate()
- if robot.up() then
- --it's ok!
- -- print("it's ok!")
- checkBorder()
- return true
- else
- -- backup
- print("detect unknowing object UP or limitFlightHeight=256")
- pos.y = ty
- savestate()
- os.sleep(0.5)
- end
- end
- end
- end
- local function forward(mined)
- if mined then
- if robot.detectUp() then
- robot.swingUp()
- if inventoryFull() then
- returnSupplies()
- end
- end
- if robot.detectDown() then
- robot.swingDown()
- if inventoryFull() then
- returnSupplies()
- end
- end
- end
- while true do
- if robot.detect() then --обнаружен блок, сломать
- if robot.swing() then
- -- print("detect block")
- if mined and inventoryFull() then
- returnSupplies()
- end
- else
- print("detect bedrock or unbreaking block")
- return false
- end
- elseif robot.swing() then -- Впереди нет блока, проверяем есть ли там существо
- print("detect monster or player")
- if mined and inventoryFull() then
- returnSupplies()
- end
- else -- Передвигаемся вперед
- if mined then -- Уберем гравий
- while robot.detectUp() do
- robot.swingUp()
- 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 robot.forward() then
- -- print("it's ok!")
- checkBorder()
- return true
- else
- -- backup
- print("detect unknowing object FORWARD limitFlightHeight=256")
- 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 lower()
- if robot.detectUp() then
- robot.swingUp()
- 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
- local function GetFuel()
- print("press any key to run quarry")
- local input = io.read()
- waiting = false
- term.clear()
- term.setCursor( 1, 1 )
- robot.select(1)
- --if mode == 2 and robot.count(1) < 1 then
- -- print("Waiting for get chest to first slot")
- -- while robot.count(1) < 1 do
- -- os.sleep(1)
- -- end
- --end
- -- print( "Fuel level: "..robot.getFuelLevel())
- print( "Excavating...")
- start()
- return
- end
- local tArgs = { ... }
- if #tArgs == 1 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