Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get ASs2sSgC q
- local direction = {
- ["forw"] =1;
- ["right"] =2;
- ["back"] =3;
- ["left"] =4;
- }
- local pos = {
- ["x"]=0;
- ["y"]=0;
- }
- dir=direction.forw;
- local lenght
- local width
- local function saveState()
- name = shell.getRunningProgram()
- local file = fs.open(name..".save","w")
- file.writeLine(pos.x)
- file.writeLine(pos.y)
- file.writeLine(dir)
- file.writeLine(lenght)
- file.writeLine(width)
- 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")
- pos.x = tonumber(file.readLine())
- pos.y = tonumber(file.readLine())
- dir = tonumber(file.readLine())
- lenght = tonumber(file.readLine())
- width = tonumber(file.readLine())
- file.close()
- return true
- 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 forward()
- while true do
- if turtle.detect() then --обнаружен блок, сломать
- if turtle.dig() then
- -- print("detect block")
- else
- print("detect bedrock or unbreaking block")
- return false
- end
- elseif turtle.attack() then -- Впереди нет блока, проверяем есть ли там существо
- print("detect monster or player")
- else -- Передвигаемся вперед
- local ty,tx = pos.y,pos.x
- if dir == direction.forw then
- pos.x=pos.x+1
- elseif dir == direction.right then
- pos.y=pos.y+1
- elseif dir == direction.back then
- pos.x=pos.x-1
- elseif dir == direction.left then
- pos.y=pos.y-1
- end
- saveState()
- print("--")
- print(pos.y)
- if turtle.forward() then
- -- print("it's ok!")
- return true
- else
- -- backup
- if not turtle.detect() then
- print("detect unknowing object")
- end
- pos.y = ty
- pos.x = tx
- saveState()
- print(pos.y)
- os.sleep(0.5)
- end
- end
- end
- end
- local function area2d(lenght, width, func)
- startY=pos.y
- if func then
- func()
- end
- for j =startY, width-1 do
- if dir==direction.right then
- if pos.x == lenght-1 then
- if pos.y%2==0 then
- if forward() then
- if func then
- func()
- end
- end
- end
- turnRight()
- elseif pos.x == 0 then
- if pos.y%2~=0 then
- if forward() then
- if func then
- func()
- end
- end
- end
- turnLeft()
- end
- end
- startX=pos.x
- if dir==direction.back then
- startX=lenght-1-pos.x
- end
- for i=startX,lenght-2 do
- if forward() then
- if func then
- func()
- end
- end
- end
- if pos.x==lenght-1 and dir==direction.forw then
- turnRight()
- end
- if pos.x==0 and dir==direction.back then
- turnLeft()
- end
- end
- end
- local function inventoryEmpty(a, b)
- for n=a,b do
- if turtle.getItemCount(n) ~= 0 then
- return false
- end
- end
- print( "Inventory empty." )
- return true
- end
- local function getFullSlot(a, b)
- for n=a,b do
- if turtle.getItemCount(n) ~= 0 then
- return n
- end
- end
- return 0
- end
- local tArgs = { ... }
- if #tArgs == 1 then
- if tArgs[1] == "recover" then
- readState()
- end
- elseif #tArgs == 2 then
- lenght = tonumber( tArgs[1] )
- width = tonumber( tArgs[2] )
- end
- tx=0
- local function myFunc()
- if tx==0 then
- turtle.turnLeft()
- turtle.select(1)
- turtle.digDown()
- turtle.drop()
- while inventoryEmpty(2,16) do
- os.sleep(1)
- end
- turtle.select(getFullSlot(2,16))
- turtle.placeDown()
- turtle.turnRight()
- end
- tx=tx+1
- if tx==4 then tx=0 end
- end
- slot=1
- local function myFunc2()
- while turtle.getItemCount(slot)==0 do
- slot=slot+1
- if slot>16 then
- slot=1
- os.sleep(1)
- end
- end
- turtle.select(slot)
- turtle.placeDown()
- end
- area2d(lenght, width, myFunc2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement