Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------Константы----------------------
- direction = {
- ["forw"] = 1,
- ["right"] = 2,
- ["back"] = 3,
- ["left"] = 4,
- }
- dir = direction.forw
- pos = {
- ["x"] = 0,
- ["y"] = 0,
- ["z"] = 0,
- }
- lenght,width,height = 0,0,0
- xt,yt,zt,dt = 0,0,0,direction.forw
- 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
- ------------------ Сохранение позиции ----------------------------
- 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
- 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
- function setStartup()
- if fs.exists("startup") then
- fs.delete("startup")
- end
- name = shell.getRunningProgram()
- local file = fs.open("startup","w")
- file.writeLine("shell.run(\""..name.."\")")
- file.close()
- end
- function delStartup()
- if fs.exists("startup") then
- fs.delete("startup")
- end
- name = shell.getRunningProgram()
- if fs.exists(name..".save") then
- fs.delete(name..".save")
- end
- end
- ------------------ Движение ----------------------------
- function turnRight()
- dir=dir+1
- if dir==5 then dir = direction.forw end
- saveState()
- turtle.turnRight()
- end
- 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
- function down()
- while true do
- if turtle.detectDown() then
- if turtle.digDown() then
- 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
- checkBorder()
- return true
- else
- print("detect unknowing object")
- pos.y = ty
- saveState()
- os.sleep(0.5)
- end
- end
- end
- end
- function up()
- while true do
- if turtle.detectUp() then --обнаружен блок, сломать
- if turtle.digUp() then
- 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
- checkBorder()
- return true
- else
- print("detect unknowing object")
- pos.y = ty
- saveState()
- os.sleep(0.5)
- end
- end
- end
- end
- 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
- 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
- checkBorder()
- return true
- else
- 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
- 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
- local goTo
- 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
- 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
- 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
- main() -- Название основной функции. Заменить main() на свой вариант
- end
- end
- 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() -- Название основной функции. Заменить main() на свой вариант
- end
- ---------------------------Что идёт в саму программу-----------------------------------
- local tArgs = { ... } -- вставить в основную прогу
- local 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
- 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