Advertisement
serafim7

treefarm [OpenComputers]

Jun 1st, 2017
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.35 KB | None | 0 0
  1. --[[ opencomputers Ферма дерева by serafim  pastebin.com/XWmaX9A9
  2.  
  3. Использать с модом который разрушает дерево убрав нижний блок !
  4. Например: treecapitator или Большой топор из tinkers construct
  5.  
  6. Используя Большой топор с улучшением на жарку(лавовый кристалл),
  7. получим ферму древестного угля.
  8.  
  9. требования:
  10. корпус 3-го уровня из за притягивающего луча,
  11. инвентарь, контроллер инвентаря
  12.  
  13. испозование:
  14. Поставить сундук справа от робота, положить саженцы с 1 по 4 слот
  15. в инвентарь робота, дать роботу топор
  16.  
  17. пример запуска:    treefarm 5 5 60
  18. или по умолчанию:  treefarm
  19. ]]--
  20.  
  21. local a = {...}
  22. local grid_x = tonumber(a[1]) or 5   -- Размер сетки по X
  23. local grid_y = tonumber(a[2]) or 5   -- Размер сетки по Y
  24. local delay = tonumber(a[3]) or 60   -- Ожидание в секундах
  25.  
  26. local upstate,final,start,empty = false,false,true,true
  27.  
  28. local computer = require("computer")
  29. local term = require('term')
  30. local com = require("component")
  31.  
  32. if not com.isAvailable("robot") then
  33.   print("Только роботы могут использовать эту программу")
  34.   computer.beep(1000,2)
  35.   os.exit()
  36. end
  37. local r = require("robot")
  38. local invsize = r.inventorySize()
  39.  
  40. if not com.isAvailable("inventory_controller") then
  41.   print("Для работы нужен контроллер инвентаря")
  42.   computer.beep(1000,2)
  43.   os.exit()
  44. end
  45. local ic = com.inventory_controller
  46.  
  47. if not com.isAvailable("tractor_beam") then
  48.   print("Для работы нужен притягивающий луч")
  49.   computer.beep(1000,2)
  50.   os.exit()
  51. end
  52. local tb = com.tractor_beam
  53.  
  54. if require("component").isAvailable("generator") then
  55.   gen = require("component").generator
  56.   print("Доступен генератор")
  57.   geninst = true
  58. end
  59.  
  60. if grid_x < 1 then
  61.   print("Размер сетки по оси X не может быть меньше 1")
  62.   computer.beep(1000,2)
  63.   os.exit()
  64. end
  65.  
  66. if grid_y < 1 then
  67.   print("Размер сетки по оси Y не может быть меньше 1")
  68.   computer.beep(1000,2)
  69.   os.exit()
  70. end
  71.  
  72. if delay < 1 then
  73.   print("Задержка не может быть меньше 1")
  74.   computer.beep(1000,2)
  75.   os.exit()
  76. end
  77.  
  78. print(" ")
  79. print("Размер поля:")
  80. print("Х = "..grid_x)
  81. print("Y = "..grid_y)
  82. print("Ожидание между сбором:")
  83. print(delay.." секунд")
  84.  
  85. local function forward()
  86.   while r.detect() do
  87.     r.swing()
  88.   end
  89.   if not r.forward() then
  90.     forward()
  91.   end
  92. end
  93.  
  94. local function up()
  95.   while r.detectUp() do
  96.     r.swingUp()
  97.   end
  98.   if not r.up() then
  99.     up()
  100.   end
  101. end
  102.  
  103. local function down()
  104.   while r.detectDown() do
  105.     r.swingDown()
  106.   end
  107.   if not r.down() then
  108.     down()
  109.   end
  110. end
  111.  
  112. local function slotsapling(slot)
  113.   if r.count(slot) > 0 then
  114.     if ic.getStackInInternalSlot(slot).name == "minecraft:sapling" then
  115.       return true
  116.     end
  117.   end
  118.   return false
  119. end
  120.  
  121. local function checkforsapling()
  122.   local detect, reason = r.detectDown()
  123.   if not detect and reason == "air" then
  124.     for i = 1, 4 do
  125.       if slotsapling(i) then
  126.         r.select(i)
  127.         ic.equip()
  128.         r.useDown()
  129.         ic.equip()
  130.         empty = true
  131.         break
  132.       end
  133.       if i == 4 and not slotsapling(4) then
  134.         if empty then
  135.           r.setLightColor(0xFF0000)
  136.           term.clear()
  137.           print("Саженцев в слотах 1 - 4 не найдено !")
  138.           empty = false
  139.         end
  140.         computer.beep(1000,10)
  141.         os.sleep(10)
  142.         while tb.suck() do end
  143.         for i = 5, invsize do
  144.           if slotsapling(i) then
  145.             r.select(i)
  146.             r.transferTo(1)
  147.           end
  148.         end
  149.         checkforsapling()
  150.         r.setLightColor(0xFFFFFF)
  151.         term.clear()
  152.         print("Работаю ...")
  153.       end
  154.     end
  155.   elseif detect and reason == "solid" then
  156.     r.swingDown()
  157.     checkforsapling()
  158.   end
  159.   while tb.suck() do end
  160. end
  161.  
  162. local function checkforlog()
  163.   local detect, reason = r.detect()
  164.   if detect and reason == "solid" then --дерево
  165.     if upstate then
  166.       down()
  167.       upstate = false
  168.     end
  169.     r.swing()
  170.     os.sleep(1)
  171.     while tb.suck() do end
  172.   elseif detect and reason == "passable" then --саженец
  173.     up()
  174.     upstate = true
  175.   end
  176.   for i = 1, 2 do
  177.     forward()
  178.   end
  179. end
  180.  
  181. local function unload()
  182.   for i = 1, invsize do
  183.     if slotsapling(i) then
  184.       r.select(i)
  185.       for j = 1, 4 do
  186.         r.transferTo(j)
  187.       end
  188.     else
  189.       if r.count(i) > 0 then
  190.         r.select(i)
  191.         if not r.drop() then
  192.           r.setLightColor(0xFF0000)
  193.           print("В сундуке нет места :(")
  194.           computer.beep(1000,10)
  195.           r.turnLeft()
  196.           os.exit()
  197.         end
  198.       end
  199.     end
  200.   end
  201.   for i = 5, invsize do
  202.     if r.count(i) > 0 then
  203.       r.select(i)
  204.       r.drop()
  205.     end
  206.   end
  207.   r.select(1)
  208. end
  209.  
  210. local function checkfuel()
  211.   if geninst and computer.energy()/computer.maxEnergy() < 0.23 then
  212.     local gencount = gen.count()
  213.     for i = 2, invsize do
  214.       if r.count(i) > 0 and not slotsapling(i) then
  215.         r.select(i)
  216.         gen.insert(32)
  217.       end
  218.     end
  219.     if gen.count() > gencount then
  220.       print("Аварийная заправка зевершена")
  221.       print("Топлива в генераторе = "..gen.count())
  222.       os.sleep(2)
  223.     end
  224.   end
  225. end
  226.  
  227. local function checkenergy()
  228.   local battery = computer.energy()/computer.maxEnergy()
  229.   if battery < 0.25 then
  230.     print("Уровень заряда робота меньше 25%")
  231.     computer.beep(1000,10)
  232.     print("Тест зарядки ...")
  233.     os.sleep(5)
  234.     if battery < computer.energy()/computer.maxEnergy() then
  235.       print("Заряжаюсь до 30% ...")
  236.       local tru = 0
  237.       while computer.energy()/computer.maxEnergy() < 0.3 do
  238.         os.sleep(30)
  239.         tru = tru + 1
  240.         if tru == 6 then
  241.           print("Зарядка слишком долгая !")
  242.           break
  243.         end
  244.         if geninst and gen.count() == 0 then
  245.           print("Закончилось топливо !")
  246.           break
  247.         end
  248.       end
  249.     else
  250.       print("Робот не заряжается !")
  251.     end
  252.   end
  253. end
  254.  
  255. local function axe()
  256.   if r.durability() == nil or r.durability() < 0.03 then
  257.     if empty then
  258.       r.setLightColor(0xFF0000)
  259.       term.clear()
  260.       print("Нет топора, или он сломан !")
  261.       empty = false
  262.     end
  263.     computer.beep(1000,10)
  264.     os.sleep(20)
  265.     axe()
  266.     term.clear()
  267.     empty = true
  268.   end
  269. end
  270.  
  271. local function move_y()
  272.   if start then
  273.     forward()
  274.     start = false
  275.   end
  276.   for i = 1, grid_y do
  277.     checkforlog()
  278.   end
  279.   r.turnAround()
  280.   if not upstate then
  281.     up()
  282.     upstate = true
  283.   end
  284.   for i = 1, grid_y do
  285.     forward()
  286.     checkforsapling()
  287.     forward()
  288.   end
  289.   down()
  290.   upstate = false
  291.   if grid_x == 1 then
  292.     forward()
  293.     r.turnAround()
  294.   else
  295.     if not final then
  296.       r.turnLeft()
  297.       for i = 1, 2 do
  298.         forward()
  299.       end
  300.       r.turnLeft()
  301.     end
  302.   end
  303. end
  304.  
  305. local function move_x()
  306.   local lol3 = grid_x - 1
  307.   for i = 1,lol3 do
  308.     if i == lol3 then
  309.       final = true
  310.     end
  311.     move_y()
  312.   end
  313.   r.turnRight()
  314.   local lol2 = grid_x * 2 - 2
  315.   for i = 1, lol2 do
  316.     forward()
  317.   end
  318.   r.turnLeft()
  319.   forward()
  320.   r.turnLeft()
  321.   checkfuel()
  322.   unload()
  323.   r.turnLeft()
  324. end
  325.  
  326. local function wait()
  327.   term.clear()
  328.   for i = delay, 1, -10 do
  329.     if i == delay then
  330.       term.write("До следующего сбора")
  331.       term.setCursor(1,2)
  332.       term.write("осталось "..delay.." секунд")
  333.     end
  334.     os.sleep(10)
  335.     term.clearLine()
  336.     term.write("осталось "..(i-10).." секунд")
  337.   end
  338.   term.clear()
  339. end
  340.  
  341. while true do
  342.   axe()
  343.   checkenergy()
  344.   r.select(1)
  345.   r.setLightColor(0xFFFFFF)
  346.   print("Работаю ...")
  347.   move_y()
  348.   move_x()
  349.   wait()
  350.   start = true
  351.   final = false
  352. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement