Advertisement
ivan52

treeFarm(OC width TreeCapitator or TConstruct)

Mar 8th, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.35 KB | None | 0 0
  1. local r = require('robot')
  2. local comp = require('computer')
  3. local term = require('term')
  4. local inv = require('component').inventory_controller
  5. local tractorSuck = require('component').tractor_beam.suck
  6.  
  7. local rast_x = 1
  8. local rast_z = 1
  9.  
  10. local num_of_lines_x = 2
  11. local num_of_blocks_z = 4
  12.  
  13. local sleep = 480
  14. --replace_instrument = false
  15.  
  16. local num_all = num_of_lines_x * num_of_blocks_z - 1
  17. local numRub
  18. sleep = sleep / 10
  19. -- =============================== Н А В И Г  А Ц И Я ============================== --
  20. local x, z, b, dx ,dz  = 0, 0, 0, 0, 1
  21. local function forward(num)
  22.     if not num then num = 1 end
  23.     for i = 1, num do
  24.         while not r.forward() do r.swing() end
  25.     end
  26.     x = x + dx * num
  27.     z = z + dz * num
  28. end
  29. local function turnLeft()
  30.     b = (b + 3)%4
  31.     r.turnLeft()
  32.     if b == 0 then dz = 1; dx = 0
  33.     elseif b == 1 then dx = 1; dz = 0
  34.     elseif b == 2 then dz = -1; dx = 0
  35.     else dx = -1; dz = 0 end
  36. end
  37. local function turnRight()
  38.     b = (b + 1)%4
  39.     r.turnRight()
  40.     if b == 0 then dz = 1; dx = 0
  41.     elseif b == 1 then dx = 1; dz = 0
  42.     elseif b == 2 then dz = -1; dx = 0
  43.     else dx = -1; dz = 0 end
  44. end
  45. local function turnAround()
  46.     b = (b + 2)%4
  47.     r.turnAround()
  48.     dx = dx * (-1)
  49.     dz = dz * (-1)
  50. end
  51. local function turn(side)
  52.     if side == 0 and b == 3 then
  53.         r.turnRight()
  54.         b = side
  55.     elseif side == 3 and b == 0 then
  56.         r.turnLeft()
  57.         b = side
  58.     else
  59.         while (b < side) do
  60.             r.turnRight()
  61.             b = b + 1
  62.         end
  63.         while (b > side) do
  64.             r.turnLeft()
  65.             b = b - 1
  66.         end
  67.     end
  68.     if b == 0 then dz = 1; dx = 0
  69.     elseif b == 1 then dx = 1; dz = 0
  70.     elseif b == 2 then dz = -1; dx = 0
  71.     else dx = -1; dz = 0 end
  72. end
  73.  
  74. -- ============================== Ф У Н К Ц И И ============================= --
  75.  
  76. -- Замена инструмента
  77. --[==[
  78. function zamena()
  79.  
  80.     slotInstr = 2
  81.     while (inv.getStackInInternalSlot(slotInstr) ~= nil) and (slotInstr < allSlots) do
  82.         slotInstr = slotInstr + 1
  83.     end
  84.     r.select(slotInstr)
  85.     inv.equip()
  86.     turnAround()
  87.     r.drop()
  88.     while inv.getStackInSlot(3,1).charge < inv.getStackInSlot(3,1).maxCharge do
  89.         os.sleep(0.2)
  90.     end
  91.     r.suck()
  92.     inv.equip()
  93.     turnAround()
  94.     slotInstr = nil
  95. end
  96. --]==]
  97. -- Возврат домой
  98. local function home(noreturn)
  99.     local x1 = x
  100.     local z1 = z
  101.     local b1 = b
  102.     local item
  103.     local resources = {}
  104.     resources['minecraft:log'] = true
  105.     resources['minecraft:coal'] = true
  106.  
  107.     if x1 > 0 then
  108.         turn(3)
  109.         forward(x1)
  110.     end
  111.     turn(2)
  112.     forward(z1)
  113.     turnLeft()
  114.     r.select(16)
  115.     r.suckDown()
  116.     for slot = 1 , 15 do
  117.         item = inv.getStackInInternalSlot(slot)
  118.         if item then
  119.             if resources[item.name] then
  120.                 r.select(slot)
  121.                 while r.count(slot) > 0 do
  122.                     if not r.drop() then
  123.                         os.sleep(4)
  124.                         term.clear()
  125.                         print('освободите инвентарь для остальных вещей')
  126.                     end
  127.                 end  
  128.             elseif item.name == "minecraft:sapling" then
  129.                 r.select(slot)
  130.                 r.dropDown()
  131.             end
  132.         end
  133.     end
  134.  
  135.  
  136.    
  137.     turnAround()
  138.     for slot = 1 , 15 do
  139.         if r.count(slot) > 0 then
  140.             r.select(slot)
  141.             while r.count(slot) > 0 do
  142.                 if not r.drop() then
  143.                     os.sleep(4)
  144.                     term.clear()
  145.                     print('освободите инвентарь для остальных вещей')
  146.                 end
  147.             end  
  148.         end
  149.     end
  150.     turnRight()
  151.     --[[
  152.     --Починка или подзарядка инструмента
  153.     if r.durability() < 0.3 then
  154.         if not replace_instrument then
  155.             while r.durability() < 0.3 do
  156.                 os.sleep(8)
  157.             end
  158.         else
  159.             zamena()
  160.         end
  161.     end
  162.     --]]
  163.     -- зарядка
  164.     while comp.energy() < 100000 do
  165.         os.sleep(8)
  166.     end
  167.  
  168.     -- возвращаемся если нет флага "в один конец"
  169.     if not noreturn then
  170.         forward(z1)
  171.         if x1 >  0 then
  172.             turn(1)
  173.             forward(x1)
  174.         end
  175.         turn(b1)
  176.     end
  177. end
  178.  
  179. --Рубка дерева
  180. local function axe()
  181.     r.select(16)
  182.     if not r.compare(true) then
  183.         r.swing()
  184.         r.place()
  185.         return 1
  186.     end
  187.     return 0
  188. end
  189.  
  190. -- ======================== Г Л А В Н Ы Й   Ц И К Л ======================== --
  191.  
  192. -- робот движется по полю по кругу, и рубит выросшие деревья
  193. while true do
  194.     -- робот едет на стартовую позицию (левый передний угол поля)
  195.     forward(4)
  196.     -- проход по ферме
  197.     for i = 1, num_all + 1 do
  198.         if i%4 == 0 and  (r.count(15) > 32 or r.count(16) < 8 or comp.energy() < 50000 or r.durability() < 0.3) then
  199.             home(false)
  200.         end
  201.         forward()
  202.         -- проверка дерева слева и справа
  203.         turnLeft()
  204.         numRub = axe()
  205.         turnAround()
  206.         numRub = numRub + axe()
  207.         turnLeft()
  208.         if numRub > 0.5 then
  209.             os.sleep(0.5)
  210.             while tractorSuck() do end
  211.         end
  212.         if i%num_of_blocks_z ~= 0 or i >= num_all then
  213.             forward(rast_z)
  214.         else
  215.             forward()
  216.             if z ~= 4 then
  217.                 turnRight()
  218.                 forward(rast_x + 3)
  219.                 turnRight()
  220.             else
  221.                 turnLeft()
  222.                 forward(rast_x + 3)
  223.                 turnLeft()
  224.             end
  225.         end
  226.         --
  227.     end
  228.  
  229.     -- робот возвращается домой
  230.     -- (флаг true означает - в один конец)
  231.     home(true)
  232.     -- робот спит и видит сны =)
  233.     term.clear()
  234.     print('Progress of sleeping')
  235.     for i=1,10 do
  236.         os.sleep(sleep)
  237.         print(i*10,' %')
  238.     end
  239.     term.clear()
  240.     print('moving')
  241. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement