Advertisement
ZKoshak

zcarier

Nov 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.16 KB | None | 0 0
  1. -- benchbench (8 8 114) = 768970
  2. r=require("robot")
  3. comp = require("component")
  4. local computer = require("computer")
  5.  
  6. if comp.isAvailable("chunkloader") then comp.chunkloader.setActive(true) end
  7. if not comp.isAvailable("inventory_controller") then
  8.   print("\t Контроллер инвентаря не обнаружен, возможны проблемы и ошибки. Принудительная остановка программы.")
  9.   os.exit()
  10. end
  11. if r.durability() ~= nil then
  12.   ppp = 0
  13. else
  14.   print("\t Инструмент не обнаружен, возможны проблемы и ошибки. Принудительная остановка программы.")
  15.   os.exit()
  16. end
  17.  
  18. inv = comp.inventory_controller
  19.  
  20.  
  21. local tArgs = { ... }
  22. if #tArgs ~= 3 then
  23.   print( "Usage: zcarier <width> <lenght> <height>" )
  24.   return
  25. end
  26.  
  27. local width = tonumber( tArgs[1] )
  28. if width < 1 then
  29.   print( "Quary width must be positive" )
  30.   return
  31. end
  32.  
  33. local lenght = tonumber( tArgs[2] )
  34. if lenght < 1 then
  35.   print( "Quary lenght must be positive" )
  36.   return
  37. end
  38.  
  39. local height = tonumber( tArgs[3] )
  40. if height < 1 then
  41.   print( "Quary height must be positive" )
  42.   return
  43. end
  44.  
  45. -- инициализация констант
  46. maxslot=16
  47. --min = 16, max = 27
  48. inv_size = r.inventorySize()
  49.  
  50. -- направления
  51. forward = 0
  52. upward = 1
  53. downward = 2
  54. backward = 3
  55.  
  56. -- функция перемещения с проверкой на наличие существа или блока по направлению движения
  57. function moveit(direction)
  58.  
  59.   if direction == forward then
  60.     while r.detect() do
  61.       r.swing()
  62. --      os.sleep(0.5)
  63.     end
  64.     while not r.forward() do
  65.       r.swing()
  66. --      os.sleep(0.5)
  67.     end
  68.   end
  69.  
  70.   if direction == upward then
  71.     while r.detectUp() do
  72.       r.swingUp()
  73. --      os.sleep(0.5)
  74.     end
  75.     while not r.up() do
  76.       r.swingUp()
  77. --      os.sleep(0.5)
  78.     end
  79.   end
  80.  
  81.   if direction == downward then
  82.     while r.detectDown() do
  83.       r.swingDown()
  84. --      os.sleep(0.5)
  85.     end
  86.     while not r.down() do
  87.       r.swingDown()
  88. --      os.sleep(0.5)
  89.     end
  90.   end
  91.  
  92.   if direction == backward then
  93.     r.turnAround()
  94.     while r.detect() do
  95.       r.swing()
  96. --      os.sleep(0.5)
  97.     end
  98.     while not r.forward() do
  99.       r.swing()
  100. --      os.sleep(0.5)
  101.     end
  102.     r.turnAround()
  103.   end
  104.  
  105. end
  106.  
  107. -- функция добычи с проверкой блока по направлению движения
  108. function digit(direction)
  109.  
  110.   if direction == forward then
  111.     while r.detect() do
  112.       r.swing()
  113. --      os.sleep(0.5)
  114.     end
  115.   end
  116.  
  117.   if direction == upward then
  118.     while r.detectUp() do
  119.       r.swingUp()
  120. --      os.sleep(0.5)
  121.     end
  122.   end
  123.  
  124.   if direction == downward then
  125.     while r.detectDown() do
  126.       r.swingDown()
  127. --      os.sleep(0.5)
  128.     end
  129.   end
  130.  
  131. end
  132.  
  133. ----
  134.  
  135. -- функция проверки заполнености инвентаря
  136. function emptycheck()
  137.   local inv_check = false
  138.   for slot = 1, inv_size-3 do
  139.     if r.count(slot) == 0 then inv_check = true end
  140.   end
  141.   return inv_check
  142. end
  143.  
  144. ----
  145.  
  146. -- функция разгрузки (позаимствована у artem211 и Doob =)
  147. function dropit()
  148.   while true do
  149.     if r.detect() then
  150.       if inv.getInventorySize(3) ~= nil then
  151.         for i=1,inv_size-2 do
  152.           if inv.getStackInInternalSlot(i) ~= nil then
  153.             r.select(i)
  154.             if not r.drop() then
  155.               print("Сундук переполнен. Освободите место под складирование.")
  156.               while not r.drop() do
  157.                 os.sleep(5)
  158.               end
  159.             end
  160.           end
  161.         end
  162.         r.select(1)
  163.         break
  164.       else
  165.         print("Блок не является сундуком.")
  166.         os.sleep(5)
  167.       end
  168.     else
  169.       print("Установите сундук!")
  170.       os.sleep(5)
  171.     end
  172.   end
  173.   r.select(1)
  174. end
  175.  
  176. ---------------------
  177. --- Основной цикл ---
  178. ---------------------
  179. print ("Excavate quary started...")
  180.  
  181. for h=1, height do
  182.  
  183.   digit(forward)
  184.   moveit(forward)
  185.  
  186.   for w=1, width do
  187.  
  188.     for l=1, lenght-2 do
  189.    
  190.       digit(forward)
  191.       moveit(forward)
  192.    
  193.       if not emptycheck() or r.durability() < 0.16 or (computer.energy()/computer.maxEnergy()) < 0.16 then
  194.    
  195.         if w == 1 then
  196.           r.turnAround()
  197.         else
  198.           if w%2 == 0 then r.turnRight() else r.turnLeft() end
  199.           dxw=w-1
  200.           for bw=1, dxw do moveit(forward) end
  201.           r.turnLeft()
  202.         end
  203.         if w%2 == 0 then
  204.           dxl=lenght-l-1
  205.         else
  206.           dxl=l+1
  207.         end
  208.         for bl=1, dxl do moveit(forward) end
  209.         if h ~= 1 then
  210.           dxh=h-1
  211.           for bh=1, dxh do moveit(upward) end
  212.         end
  213.    
  214.         if not emptycheck() then
  215.           print("...unload...")
  216.           dropit()
  217.           r.select(1)
  218.         end
  219.  
  220.         if r.durability() < 0.16 then
  221.           print("...charge tool...")
  222. --          moveit(upward)
  223.           r.turnRight()
  224.           r.select(inv_size-1)
  225.           inv.equip()
  226.           while inv.getStackInInternalSlot(slot).damage > 1 do
  227.             r.drop(3)
  228.             os.sleep(10)
  229.             r.suck(3)
  230.           end
  231.           inv.equip()
  232.           r.select(1)
  233.           r.turnLeft()
  234. --          moveit(downward)
  235.         end
  236.  
  237.         if (computer.energy()/computer.maxEnergy()) < 0.16 then
  238. --          moveit(downward)
  239.           print('...charge...')
  240.           while (computer.energy()/computer.maxEnergy()) < 0.99 do os.sleep(10) end
  241. --          moveit(upward)
  242.         end
  243.  
  244.         r.turnAround()
  245.         if h ~= 1 then
  246.           for bbh=1, dxh do moveit(downward) end
  247.         end
  248.         for bbl=1, dxl do moveit(forward) end
  249.         if w ~= 1 then
  250.           r.turnRight()
  251.           for bbw=1, dxw do moveit(forward) end
  252.           if w%2 == 0 then r.turnRight() else r.turnLeft() end
  253.         end
  254.  
  255.       end
  256.    
  257.     end
  258.  
  259.     if w ~= width then
  260.       if w%2 == 0 then r.turnLeft() else r.turnRight() end
  261.       digit(forward)
  262.       moveit(forward)
  263.       if w%2 == 0 then r.turnLeft() else r.turnRight() end
  264.     end
  265.  
  266.   end
  267.  
  268.   if width%2 == 0 then
  269.     moveit(forward)
  270.   else
  271.     r.turnAround()
  272.     for l=1, lenght-1 do moveit(forward) end
  273.   end
  274.  
  275.   r.turnRight()
  276.   for w=1, width-1 do
  277.  
  278.     digit(forward)
  279.     moveit(forward)
  280.  
  281.     if not emptycheck() or r.durability() < 0.16 or (computer.energy()/computer.maxEnergy()) < 0.16 then
  282.  
  283.       r.turnRight()
  284.       moveit(forward)
  285.       r.turnLeft()
  286.       dxw=width-w-1
  287.       for bw=1, dxw do moveit(forward) end
  288.       r.turnLeft()
  289.       moveit(forward)
  290.       if h ~= 1 then
  291.         dxh=h-1
  292.         for bh=1, dxh do moveit(upward) end
  293.       end
  294.  
  295.       if not emptycheck() then
  296.         print("...unload...")
  297.         dropit()
  298.         r.select(1)
  299.       end
  300.  
  301.       if r.durability() < 0.16 then
  302.         print("...charge tool...")
  303. --        moveit(upward)
  304.         r.turnRight()
  305.         r.select(inv_size-1)
  306.         inv.equip()
  307.         while inv.getStackInInternalSlot(slot).damage > 1 do
  308.           r.drop(3)
  309.           os.sleep(10)
  310.           r.suck(3)
  311.         end
  312.         inv.equip()
  313.         r.select(1)
  314.         r.turnLeft()
  315. --        moveit(downward)
  316.       end
  317.  
  318.       if (computer.energy()/computer.maxEnergy()) < 0.16 then
  319. --        moveit(downward)
  320.         print('...charge...')
  321.         while (computer.energy()/computer.maxEnergy()) < 0.99 do os.sleep(10) end
  322. --        moveit(upward)
  323.       end
  324.  
  325.       r.turnAround()
  326.       if h ~= 1 then
  327.         for bbh=1, dxh do moveit(downward) end
  328.       end
  329.       moveit(forward)
  330.       r.turnRight()
  331.       for bbw=1, dxw do moveit(forward) end
  332.       r.turnRight()
  333.       moveit(forward)
  334.       r.turnRight()
  335.  
  336.     end
  337.  
  338.   end
  339.  
  340.   if h ~= height then
  341.     r.turnRight()
  342.     digit(downward)
  343.     moveit(downward)
  344.   else
  345.     if h ~= 1 then
  346.       for bh=1, h-1 do moveit(upward) end
  347.       r.turnRight()
  348.     end
  349.   end
  350.  
  351. end
  352.  
  353. print ("Excavating done.")
  354. print ("-----------------------------")
  355. print ("I'm ready. Get me up, papa...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement