eleeet

чужой копатель

Jul 4th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --//БЕЗ Апгрейда инвентрь.
  2. --//БЕЗ зарядки из ресурсов в инвентре
  3. --//Зарядка от зарядной станции на месте старта.
  4. --// В 1 слот нужно положить сундук.
  5.  
  6.  --//объявляем прочие переменные
  7. local alternate = 0
  8. local done = false
  9. local robot = require("robot");
  10. --//иницианализируем генератор
  11. local component = require("component")
  12. --local gen = component.generator
  13.  
  14. local COMP = require("computer")
  15.  
  16. local depth = 0
  17. local unloaded = 0
  18. local collected = 0
  19.  
  20. local xPos,zPos = 0,0
  21. local xDir,zDir = 0,1
  22.  
  23. --//вводим и проверяем данные: размер и глубину
  24. local size
  25.  
  26. function Collect()
  27.     local bFull = true
  28.     local ISize = robot.inventorySize()
  29.     --local nTotalItems = 0
  30.     for n = 3, ISize do
  31.         if robot.count(n) == 0 then
  32.             bFull = false
  33.         end
  34.  
  35.     end
  36.    
  37.     if bFull then
  38.         print( "No empty slots left." )
  39.         --return false
  40.     end
  41.     --return true
  42.     return not bFull
  43. end
  44.  
  45. function Unload()
  46.     local ISize = robot.inventorySize()
  47.  
  48.     print( "Unloading items..." )
  49.     for n = 1 , ISize do
  50.         robot.select(n)
  51.         while robot.count(n) > 0 do
  52.             robot.drop()
  53.         end
  54.     end
  55.     --collected = 0
  56.     robot.select(3)
  57. end
  58.  
  59. function GoTo( x, y, z, xd, zd )
  60.     while depth > y do
  61.         if robot.up() then
  62.             depth = depth - 1
  63.         elseif robot.swingUp() then
  64.             Collect()
  65.         else
  66.             os.sleep( 0.5 )
  67.         end
  68.     end
  69.  
  70.     if xPos > x then
  71.         while xDir ~= -1 do
  72.             TurnLeft()
  73.         end
  74.         while xPos > x do
  75.             if robot.forward() then
  76.                 xPos = xPos - 1
  77.             elseif robot.swing() then
  78.                 Collect()
  79.             else
  80.                 os.sleep( 0.5 )
  81.             end
  82.         end
  83.     elseif xPos < x then
  84.         while xDir ~= 1 do
  85.             TurnLeft()
  86.         end
  87.         while xPos < x do
  88.             if robot.forward() then
  89.                 xPos = xPos + 1
  90.             elseif robot.swing() then
  91.                 Collect()
  92.             else
  93.                 os.sleep( 0.5 )
  94.             end
  95.         end
  96.     end
  97.    
  98.     if zPos > z then
  99.         while zDir ~= -1 do
  100.             TurnLeft()
  101.         end
  102.         while zPos > z do
  103.             if robot.forward() then
  104.                 zPos = zPos - 1
  105.             elseif robot.swing() then
  106.                 Collect()
  107.             else
  108.                 os.sleep( 0.5 )
  109.             end
  110.         end
  111.     elseif zPos < z then
  112.         while zDir ~= 1 do
  113.             TurnLeft()
  114.         end
  115.         while zPos < z do
  116.             if robot.forward() then
  117.                 zPos = zPos + 1
  118.             elseif robot.swing() then
  119.                 Collect()
  120.             else
  121.                 os.sleep( 0.5 )
  122.             end
  123.         end
  124.     end
  125.    
  126.     while depth < y do
  127.         if robot.down() then
  128.             depth = depth + 1
  129.         elseif robot.swingDown() then
  130.             Collect()
  131.         else
  132.             os.sleep( 0.5 )
  133.         end
  134.     end
  135.    
  136.     while zDir ~= zd or xDir ~= xd do
  137.         TurnLeft()
  138.     end
  139. end
  140.  
  141. local function ReturnStart()
  142.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  143.     print( "Returning to surface...", x,y,z,xd,zd)
  144.     GoTo( 0,0,0,0,-1 )
  145.    
  146.     Unload()
  147.    
  148.     print( "Resuming mining..." )
  149.     GoTo( x,y,z,xd,zd )
  150. end
  151.  
  152. function RobotDown()
  153.     if not Collect() then
  154.         ReturnStart()
  155.         --return false
  156.     end
  157.    
  158.     local i = 1
  159.     repeat
  160.         robot.swingDown()
  161.         i = i + 1
  162.         if i == 30 then
  163.             return false
  164.         end  
  165.     until not robot.detectDown()
  166.  
  167.     robot.down()
  168.  
  169.     depth = depth + 4
  170.     if math.fmod( depth, 10 ) == 0 then
  171.         print( "Descended " .. depth .. " metres." )
  172.     end
  173.     return true
  174. end
  175.  
  176. function RobotForward()
  177.     if not Collect() then
  178.         ReturnStart()
  179.         --return false
  180.     end
  181.  
  182.     local i = 0
  183.  
  184.     if COMP.energy() < 5000 then
  185.         Refuel()
  186.     end
  187.    
  188.     repeat
  189.         robot.swing()
  190.         i = i + 1
  191.         if i == 50 then
  192.             return false
  193.         end        
  194.     until not robot.detect()
  195.     robot.forward()
  196.  
  197.     xPos = xPos + xDir
  198.     zPos = zPos + zDir
  199.    
  200.     return true
  201. end
  202.  
  203. function TurnLeft()
  204.     robot.turnLeft()
  205.     xDir, zDir = -zDir, xDir
  206. end
  207.  
  208. function TurnRight()
  209.     robot.turnRight()
  210.     xDir, zDir = zDir, -xDir
  211. end
  212.  
  213. function Refuel()
  214.     --local selectedSlot = robot.select()
  215.     --begin
  216.     --print("Refuel...")
  217.     --robot.select(16)
  218.     --gen.insert(8)
  219.     --robot.select(selectedSlot)
  220.    
  221.     -- зарядка от "зарядника"
  222.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  223.     print( "Returning to surface...", x,y,z,xd,zd)
  224.     GoTo( 0,0,0,0,-1 )
  225.    
  226.     Unload()
  227.    
  228.     while COMP.energy() < 20200 do
  229.         print("Пробуем зарядиться...")
  230.         os.sleep(60)  
  231.     end
  232.        
  233.     print( "Resuming mining..." )
  234.     GoTo( x,y,z,xd,zd )
  235.    
  236.  end
  237.  
  238. function StartQuarry(size)
  239.     while not done do
  240.        
  241.         if COMP.energy() < 5000 then
  242.             Refuel()
  243.         end
  244.        
  245.         for n = 1, size do
  246.             for m=1, size - 1 do
  247.                 if not RobotForward() then
  248.                     done = true
  249.                     print("Done")
  250.                     break
  251.                 end    
  252.             end
  253.          
  254.             if n < size then
  255.                 if math.fmod(n + alternate,2) == 0 then
  256.                     TurnLeft()
  257.                     if not RobotForward() then
  258.                         done = true
  259.                         break
  260.                     end
  261.                     TurnLeft()
  262.                 else
  263.                     TurnRight()
  264.                     if not RobotForward() then
  265.                         done = true
  266.                         break
  267.                     end
  268.                     TurnRight()
  269.                 end
  270.             end
  271.            
  272.             if done == true then
  273.                 print("Done")
  274.                 break
  275.             end
  276.         end
  277.        
  278.         if size > 1 then
  279.             if math.fmod(size,2) == 0 then
  280.                 TurnRight()
  281.             else
  282.                 if alternate == 0 then
  283.                     TurnLeft()
  284.                 else
  285.                     TurnRight()
  286.                 end
  287.                 alternate = 1 - alternate
  288.             end
  289.     end
  290.    
  291.     print("Level " .. depth)
  292.    
  293.     if done == true then
  294.       print("Done")
  295.     end
  296.    
  297.     if not RobotDown() then
  298.       done = true
  299.       print("Done")
  300.       break
  301.     end
  302.   end
  303. end
  304.  
  305. size = 32
  306.  
  307. if COMP.energy() < 5000 then
  308.     Refuel()
  309. end
  310.  
  311. print( "Excavating..." )
  312. if not RobotDown() then
  313.     print("Done")
  314. else
  315.     robot.select(3)
  316.     StartQuarry(size)
  317. end
  318.  
  319. -- Return to where we started
  320. GoTo( 0,0,0,0,-1 )
  321. Unload()
  322. GoTo( 0,0,0,0,1 )
  323.  
  324.  
  325. done1 = 2
  326.     while done1 == 2 do
  327.         print("Я закончил, выключи меня.")
  328.         os.sleep(6000)
  329.     end
Add Comment
Please, Sign In to add comment