Advertisement
denesik

testlib

Sep 15th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. -- pastebin get ASs2sSgC q
  2. local direction = {
  3.     ["forw"]  =1;
  4.     ["right"] =2;
  5.     ["back"]  =3;
  6.     ["left"]  =4;
  7.  
  8. }
  9.  
  10. local pos = {
  11.     ["x"]=0;
  12.     ["y"]=0;
  13. }
  14. dir=direction.forw;
  15.  
  16. local lenght
  17. local width
  18.  
  19. local function saveState()
  20.     name = shell.getRunningProgram()
  21.     local file = fs.open(name..".save","w")
  22.  
  23.     file.writeLine(pos.x)
  24.     file.writeLine(pos.y)
  25.     file.writeLine(dir)
  26.     file.writeLine(lenght)
  27.     file.writeLine(width)
  28.    
  29.     file.close()
  30. end
  31.  
  32. local function readState()
  33.     name = shell.getRunningProgram()
  34.     if not fs.exists(name..".save") then
  35.             return false
  36.     end
  37.     local file = fs.open(name..".save","r")
  38.  
  39.     pos.x =     tonumber(file.readLine())
  40.     pos.y =     tonumber(file.readLine())
  41.     dir =       tonumber(file.readLine())
  42.     lenght =    tonumber(file.readLine())
  43.     width =     tonumber(file.readLine())
  44.  
  45.     file.close()
  46.     return true
  47. end
  48.  
  49. local function turnRight()
  50.     dir=dir+1      
  51.     if dir==5 then dir = direction.forw end
  52.     saveState()
  53.     turtle.turnRight()
  54. end
  55.  
  56. local function turnLeft()
  57.     dir=dir-1      
  58.     if dir==0 then dir = direction.left end
  59.     saveState()
  60.     turtle.turnLeft()
  61. end
  62.  
  63. local function forward()
  64.     while true do
  65.             if turtle.detect() then --обнаружен блок, сломать
  66.                     if turtle.dig() then
  67. --                              print("detect block")
  68.                     else
  69.                             print("detect bedrock or unbreaking block")
  70.                             return false
  71.                     end
  72.             elseif turtle.attack() then -- Впереди нет блока, проверяем есть ли там существо
  73.                     print("detect monster or player")
  74.                    
  75.             else    -- Передвигаемся вперед
  76.                     local ty,tx = pos.y,pos.x
  77.                     if dir == direction.forw then
  78.                             pos.x=pos.x+1
  79.                     elseif dir == direction.right then
  80.                             pos.y=pos.y+1
  81.                     elseif dir == direction.back then
  82.                             pos.x=pos.x-1
  83.                     elseif dir == direction.left then
  84.                             pos.y=pos.y-1
  85.                     end
  86.                     saveState()
  87.                     print("--")
  88.                     print(pos.y)
  89.                     if turtle.forward() then
  90. --                              print("it's ok!")
  91.                             return true
  92.                     else
  93.                             -- backup
  94.                             if not turtle.detect() then    
  95.                                     print("detect unknowing object")
  96.                             end
  97.                             pos.y = ty
  98.                             pos.x = tx
  99.                             saveState()
  100.                             print(pos.y)
  101.                             os.sleep(0.5)
  102.                     end
  103.             end
  104.     end
  105. end
  106.  
  107. local function area2d(lenght, width, func)
  108.     startY=pos.y
  109.     if func then
  110.         func()
  111.     end
  112.     for j =startY, width-1 do
  113.        
  114.         if dir==direction.right then
  115.             if pos.x == lenght-1 then
  116.                 if pos.y%2==0 then
  117.                     if forward() then
  118.                         if func then
  119.                             func()
  120.                         end
  121.                     end
  122.                 end
  123.                 turnRight()
  124.             elseif pos.x == 0 then
  125.                 if pos.y%2~=0 then
  126.                     if forward() then
  127.                         if func then
  128.                             func()
  129.                         end                    
  130.                     end
  131.                 end
  132.                 turnLeft()
  133.             end
  134.         end
  135.        
  136.         startX=pos.x
  137.         if dir==direction.back then
  138.             startX=lenght-1-pos.x
  139.         end
  140.        
  141.         for i=startX,lenght-2 do
  142.             if forward() then
  143.                 if func then
  144.                     func()
  145.                 end            
  146.             end
  147.         end
  148.        
  149.         if pos.x==lenght-1 and dir==direction.forw then
  150.             turnRight()
  151.         end
  152.         if pos.x==0 and dir==direction.back then
  153.             turnLeft()
  154.         end
  155.        
  156.     end
  157. end
  158.  
  159. local function inventoryEmpty(a, b)
  160.     for n=a,b do
  161.         if turtle.getItemCount(n) ~= 0 then
  162.             return false
  163.         end
  164.     end
  165.     print( "Inventory empty." )
  166.     return true
  167. end
  168.  
  169. local function getFullSlot(a, b)
  170.     for n=a,b do
  171.         if turtle.getItemCount(n) ~= 0 then
  172.             return n
  173.         end
  174.     end
  175.     return 0
  176. end
  177.  
  178. local tArgs = { ... }
  179. if #tArgs == 1 then
  180.     if tArgs[1] == "recover" then
  181.         readState()
  182.     end
  183. elseif #tArgs == 2 then
  184.     lenght = tonumber( tArgs[1] )
  185.     width  = tonumber( tArgs[2] )
  186. end
  187.  
  188. tx=0
  189.  
  190. local function myFunc()
  191.     if tx==0 then
  192.         turtle.turnLeft()
  193.         turtle.select(1)
  194.         turtle.digDown()
  195.         turtle.drop()
  196.         while inventoryEmpty(2,16) do
  197.             os.sleep(1)
  198.         end
  199.         turtle.select(getFullSlot(2,16))
  200.         turtle.placeDown()
  201.         turtle.turnRight()
  202.     end
  203.     tx=tx+1
  204.     if tx==4 then tx=0 end
  205. end
  206.  
  207. slot=1
  208. local function myFunc2()
  209.     while turtle.getItemCount(slot)==0 do
  210.         slot=slot+1
  211.         if slot>16 then
  212.             slot=1
  213.             os.sleep(1)
  214.         end    
  215.     end
  216.     turtle.select(slot)
  217.     turtle.placeDown()
  218. end
  219.  
  220. area2d(lenght, width, myFunc2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement