Advertisement
Andronio12

TreeFarm [OpenComputers]

Mar 31st, 2017
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Tree Farm by Andronio
  2. local tArgs = {...} -- Данные, вводимые в самом начале
  3. local r = require("robot") -- Робот
  4. local cmp = require("component") -- Компонент
  5. local computer = require("computer") -- Компьютер
  6. local term = require('term')
  7. local gz = cmp.geolyzer -- Геолизёр
  8. local tb = cmp.tractor_beam -- Тянущий луч
  9. local ic = cmp.inventory_controller -- Контроллер Инвентаря
  10. local event = require('event')
  11. local delay = 0 -- В секундах
  12. local grid_x = 0 -- Размер сетки по X
  13. local grid_y = 0 -- Размер сетки по Y
  14. local layers = 0 -- Количесто слоев
  15. local layer_h = 0 -- Высота слоя
  16. local remainlayers = layers -- Осталось слоев. Для мультислойной фермы
  17. local iscoalfarm = false -- Является ли ферма угольной
  18. local upstate = 0
  19.  
  20. term.clear()
  21. if tArgs[1] == 'help' then
  22.   io.write('Запуск:\nwoodman start X Y delay layers layer_h IsCoalFarm\n')
  23.   io.write('Задержка в секундах. Если слой один -\nвысоту не указывать!')
  24.   io.write('\nIsCoalFarm - является ли ферма угольной\nTrue - если да, False - если нет\n')
  25.   io.write('Например:\nwoodman start 10 8 1200 1 0 true\n')
  26.   io.write('\nВ разработке:')
  27.   io.write('\nСохранение в файл:\nwoodman save X Y delay layers layer_h IsCoalFarm\n')
  28.   io.write('Если вы сохранили в файл, то можно запустить так:\nwoodman start saved')
  29.   io.write('Чтобы посмотреть оставшееся время, зажмите C и D\n Чтобы остановить программу, зажмите S и P')
  30.   os.exit()
  31. elseif tArgs[1] == 'start' then
  32.   if #tArgs > 5 then
  33.     grid_x = tonumber(tArgs[2])
  34.     grid_y = tonumber(tArgs[3])
  35.     delay = tonumber(tArgs[4])
  36.     layers = tonumber(tArgs[5])
  37.     if layers > 1 then
  38.       layer_h = tonumber(tArgs[6])
  39.       if tArgs[7] == 'true' then
  40.         iscoalfarm = true
  41.       elseif tArgs[7] == 'false' then
  42.         iscoalfarm = false
  43.       else
  44.         print('Неверный 6-й аргумент!')
  45.         os.exit()
  46.       end
  47.     elseif layers == 1 then
  48.       if tArgs[6] == 'true' then
  49.         iscoalfarm = true
  50.       elseif tArgs[6] == 'false' then
  51.         iscoalfarm = false
  52.       else
  53.         print('Неверный 5-й аргумент!')
  54.         os.exit()
  55.       end
  56.     end
  57.   elseif tArgs[2] == 'saved' then
  58.     -- Когда-то и здесь появится код... А пока что:
  59.     os.exit()
  60.   elseif #tArgs == 1 then
  61.     print('Загляните в woodman help для помощи!')
  62.     os.exit()
  63.   elseif #tArgs < 6 then
  64.     print('Недостаточно аргументов!')
  65.     os.exit()
  66.   end
  67. elseif tArgs[1] == 'save' then
  68.   -- Когда-то и здесь появится код...
  69. elseif #tArgs == 0 then
  70.   print('Загляните в woodman help для помощи!')
  71.   os.exit()
  72. else
  73.   print('Невозможно распознать команду! Воспользуйтесь одной из этих: help, start, save(в разработке). Также загляните в woodman help для помощи!')
  74.   os.exit()
  75. end
  76.  
  77. function report(msg)
  78.   print(msg)
  79.   if cmp.isAvailable('tunnel') then
  80.     cmp.tunnel.send(msg)
  81.   end
  82. end
  83.  
  84. function forward()
  85.   while r.detect() do
  86.     r.swing()
  87.   end
  88.   if not r.forward() then
  89.     forward()
  90.   end
  91. end
  92. function up()
  93.   while r.detectUp() do
  94.     r.swingUp()
  95.   end
  96.   if not r.up() then
  97.     up()
  98.   end
  99. end
  100. function down()
  101.   while r.detectDown() do
  102.     r.swingDown()
  103.   end
  104.   if not r.down() then
  105.     down()
  106.   end
  107. end
  108.  
  109. function checkforlog()
  110.   local item_id = gz.analyze(3).name
  111.   if item_id == 'minecraft:log' then
  112.     if upstate == 1 then
  113.       down()
  114.       upstate = 0
  115.     end
  116.     r.swing()
  117.     if iscoalfarm == true then
  118.       for i=1,40 do
  119.         tb.suck()
  120.       end
  121.     else
  122.       for i=1,16 do
  123.         tb.suck()
  124.       end
  125.     end
  126.     for i=1,2 do
  127.       forward()
  128.     end
  129.   elseif item_id == 'minecraft:sapling' then
  130.     up()
  131.     upstate = 1
  132.     for i=1,2 do
  133.       forward()
  134.     end
  135.   else
  136.     for i=1,2 do
  137.       forward()
  138.     end
  139.   end
  140. end
  141.  
  142. if grid_x < 1 then
  143.   print('Размер сетки по оси X не может быть меньше 1!')
  144.   computer.beep(1000,2.5)
  145.   os.exit()
  146. end
  147. if grid_y < 1 then
  148.   print('Размер сетки по оси Y не может быть меньше 1!')
  149.   computer.beep(1000,2.5)
  150.   os.exit()
  151. end
  152. if delay < 1 then
  153.   print('Задержка не может быть меньше 1!')
  154.   computer.beep(1000,2.5)
  155.   os.exit()
  156. end
  157. if layers < 1 then
  158.   print('Количество слоев не может быть меньше 1!')
  159.   computer.beep(1000,2.5)
  160.   os.exit()
  161. end
  162. if layers > 1 then
  163.   if layer_h < 7 then
  164.     print('Высота одного слоя не может быть меньше 7!')
  165.     computer.beep(1000,2.5)
  166.     os.exit()
  167.   end
  168. end
  169.  
  170. r.select(1)
  171. function checkforsapling()
  172.   local hassapl = false
  173.   local item = gz.analyze(0).name
  174.   if item == 'minecraft:air' then
  175.     for i=1,4 do
  176.       if ic.getStackInInternalSlot(i) then
  177.         if ic.getStackInInternalSlot(i).name == 'minecraft:sapling' then
  178.           r.select(i)
  179.           hassapl = true
  180.           break
  181.         end
  182.       end
  183.     end
  184.     if not hassapl then
  185.       report('Саженцев не найдено, либо они закончились!')
  186.       hassapl = false
  187.       computer.beep(1000,10)
  188.       while not hassapl do
  189.         os.sleep(15)
  190.         for i=1,4 do
  191.           if ic.getStackInInternalSlot(i) then
  192.             if ic.getStackInInternalSlot(i).name == 'minecraft:sapling' then
  193.               r.select(i)
  194.               hassapl = true
  195.               report('Саженцы обнаружены! Продолжаю работу!')
  196.               break
  197.             end
  198.           end
  199.         end
  200.         computer.beep(1000,1)
  201.       end
  202.     end
  203.     ic.equip()
  204.     r.useDown()
  205.     ic.equip()
  206.   end
  207.   for i=1,2 do
  208.     tb.suck()
  209.   end
  210. end
  211.  
  212. function move_y()
  213.   for i=1,grid_y do
  214.     checkforlog()
  215.   end
  216.   r.turnAround()
  217.   if upstate == 0 then
  218.     up()
  219.     upstate = 1
  220.   end
  221.   for i=1,grid_y do
  222.     forward()
  223.     checkforsapling()
  224.     forward()
  225.   end
  226.   down()
  227.   upstate = 0
  228.   if grid_x == 1 then
  229.     forward()
  230.     r.turnAround()
  231.   else
  232.     r.turnLeft()
  233.     for i=1,2 do
  234.       forward()
  235.     end
  236.     r.turnLeft()
  237.   end
  238. end
  239.  
  240. function move_x()
  241.   if grid_x > 1 then
  242.     local lol3 = grid_x - 1
  243.     for i=1,lol3 do
  244.       move_y()
  245.     end
  246.     r.turnLeft()
  247.     local lol2 = grid_x*2
  248.     for i=1,lol2 do
  249.       forward()
  250.     end
  251.     if layers == 1 then
  252.       r.turnLeft()
  253.       forward()
  254.       r.turnAround()
  255.     else
  256.       r.turnRight()
  257.     end
  258.   end
  259. end
  260.  
  261. local is = r.inventorySize()
  262.  
  263. function unload()
  264.   r.turnRight()
  265.   local s1,s2,s3,s4=0,0,0,0
  266.   for i=1,4 do
  267.     if ic.getStackInInternalSlot(i) then
  268.       if i == 1 then
  269.         s1 = ic.getStackInInternalSlot(1).size
  270.       elseif i == 2 then
  271.         s2 = ic.getStackInInternalSlot(2).size
  272.       elseif i == 3 then
  273.         s3 = ic.getStackInInternalSlot(3).size
  274.       elseif i == 4 then
  275.         s4 = ic.getStackInInternalSlot(4).size
  276.       end
  277.     end
  278.   end
  279.   if s1 > 24 then
  280.     r.select(1)
  281.     r.drop(s1 - 24)
  282.   end
  283.   if s2 > 24 then
  284.     r.select(2)
  285.     r.drop(s2 - 24)
  286.   end
  287.   if s3 > 24 then
  288.     r.select(3)
  289.     r.drop(s3 - 24)
  290.   end
  291.   if s4 > 24 then
  292.     r.select(4)
  293.     r.drop(s4 - 24)
  294.   end
  295.   for i=5,is do
  296.     r.select(i)
  297.     r.drop()
  298.   end
  299.   r.turnLeft()
  300.   r.select(1)
  301. end
  302.  
  303. function onelayer()
  304.   move_y()
  305.   move_x()
  306. end
  307.  
  308. function morethanone()
  309.   onelayer()
  310.   remainlayers = layers - 1
  311.   for i=remainlayers,1,-1 do
  312.     for i=1,layer_h do
  313.       up()
  314.     end
  315.     onelayer()
  316.   end
  317.   local godown = (layers - 1) * layer_h
  318.   for i=1,godown do
  319.     down()
  320.   end
  321.   r.turnAround()
  322.   forward()
  323.   r.turnAround()
  324. end
  325.  
  326. while true do
  327.   local hsbs = false
  328.   local pseo = 0
  329.   for p=1,4 do
  330.     if ic.getStackInInternalSlot(p) then
  331.       if ic.getStackInInternalSlot(p).name == 'minecraft:sapling' then
  332.         hsbs = true
  333.         break
  334.       end
  335.     end
  336.   end
  337.   while not hsbs do
  338.     if pseo == 0 and hsbs == false then
  339.       report('Саженцов не найдено! Положите саженцы в первые 4 слота!')
  340.       pseo = 1
  341.     end
  342.     for p=1,4 do
  343.       if ic.getStackInInternalSlot(p) then
  344.         if ic.getStackInInternalSlot(p).name == 'minecraft:sapling' then
  345.           hsbs = true
  346.           pseo = 0
  347.           break
  348.         end
  349.       end
  350.     end
  351.     os.sleep(14)
  352.     computer.beep(1000,1)
  353.   end
  354.   local ica = 0
  355.   local nc = 0
  356.   local icgn = 0
  357.   while computer.energy()/computer.maxEnergy() < 0.25 do
  358.     if gz.analyze(0).name == 'OpenComputers:charger' or gz.analyze(1).name == 'OpenComputers:charger' or gz.analyze(2).name == 'OpenComputers:charger' or gz.analyze(5).name == 'OpenComputers:charger' then
  359.       if ica == 0 then
  360.         report('Уровень зарядки робота меньше 25%. Зарядка...')
  361.         icgn = 1
  362.         ica = 1
  363.       end
  364.     else
  365.       if nc == 0 then
  366.         report('Подключите зарядник!!!')
  367.         nc = 1
  368.       end
  369.       os.sleep(10)
  370.     end
  371.   end
  372.   while icgn == 1 do
  373.     while computer.energy()/computer.maxEnergy() < 0.5 do
  374.       os.sleep(30)
  375.     end
  376.     icgn = 0
  377.     report('Робот достиг 50% заряда! Продолжаю работу...')
  378.   end
  379.   forward()
  380.   if layers == 1 then
  381.     onelayer()
  382.   else
  383.     morethanone()
  384.   end
  385.   unload()
  386.   os.sleep(delay)
  387. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement