Advertisement
ivan52

treeFarm(OC)

Jan 23rd, 2017
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.42 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. local sqrt = math.abs
  7. local rast_x, rast_z, num_of_lines_x, num_of_blocks_z,
  8.  
  9. rast_x = 1
  10. rast_z = 1
  11.  
  12. num_of_lines_x = 4
  13. num_of_blocks_z = 10
  14.  
  15. replace_instrument = false
  16.  
  17. local num_all = num_of_lines_x * num_of_blocks_z
  18. db = 0
  19.  
  20. -- =============================== Н А В И Г  А Ц И Я ============================== --
  21. x = 0; z = 0; y = 0; b = 0; dx = 0 ; dz = 1;
  22.  
  23. function forward()
  24.     while not r.forward() do r.swing() end
  25.     x = x + dx
  26.     z = z + dz
  27. end
  28. function back()
  29.     if not r.back() then
  30.         r.turnAround()
  31.         while r.detect() or not r.forward() do r.swing() end
  32.         r.turnAround()
  33.     end
  34.     x = x - dx
  35.     z = z - dz
  36. end
  37. function up()
  38.     while r.detectUp() or not r.up() do r.swingUp() end  
  39.     y = y + 1
  40. end
  41. function down()
  42.     while r.detectDown() or not r.down() do r.swingDown() end
  43.     y = y - 1
  44. end
  45. function turnLeft()
  46.     b = (b + 3)%4
  47.     r.turnLeft()
  48.     if b == 0 then dz = 1; dx = 0
  49.     elseif b == 1 then dx = 1; dz = 0
  50.     elseif b == 2 then dz = -1; dx = 0
  51.     else dx = -1; dz = 0 end
  52. end
  53. function turnRight()
  54.     b = (b + 1)%4
  55.     r.turnRight()
  56.     if b == 0 then dz = 1; dx = 0
  57.     elseif b == 1 then dx = 1; dz = 0
  58.     elseif b == 2 then dz = -1; dx = 0
  59.     else dx = -1; dz = 0 end
  60. end
  61. function turnAround()
  62.     b = (b + 2)%4
  63.     r.turnAround()
  64.     dx = dx * (-1)
  65.     dz = dz * (-1)
  66. end
  67. function turn(side)
  68.     if side == 0 and b == 3 then
  69.         r.turnRight()
  70.         b = side
  71.     elseif side == 3 and b == 0 then
  72.         r.turnLeft()
  73.         b = side
  74.     else
  75.         while (b < side) do
  76.             r.turnRight()
  77.             b = b + 1
  78.         end
  79.         while (b > side) do
  80.             r.turnLeft()
  81.             b = b - 1
  82.         end
  83.     end
  84.     if b == 0 then dz = 1; dx = 0
  85.     elseif b == 1 then dx = 1; dz = 0
  86.     elseif b == 2 then dz = -1; dx = 0
  87.     else dx = -1; dz = 0 end
  88. end
  89. function goTo(x1, z1, y1, b1, hod)
  90. local a1, a2, b2, b3
  91.     if x < x1 then
  92.         a1 = 1
  93.     elseif x > x1 then
  94.         a1 = 3
  95.     end
  96.     if z < z1 then
  97.         a2 = 0
  98.     elseif z > z1 then
  99.         a2 = 2  
  100.     end
  101.     if hod then
  102.         b2 = sqrt(z1 - z); b3 = sqrt(x1 - x)
  103.     else
  104.         a1, a2 = a2, a1
  105.         b2 = sqrt(x1 - x); b3 = sqrt(z1 - z)
  106.     end
  107.     while y1 < y do
  108.         while r.detectDown() or not r.down() do r.swingDown() end
  109.         y = y - 1
  110.     end
  111.     while y1 > y do
  112.         while r.detectUp() or not r.up() do r.swingUp() end
  113.         y = y + 1
  114.     end
  115.  
  116.     if a2 then
  117.         turn(a2)
  118.     end
  119.     for i = 1, b2 do
  120.         while r.detect() or not r.forward() do  r.swing() end
  121.         x = x + dx
  122.         z = z + dz
  123.     end
  124.     if a1 then
  125.         turn(a1)
  126.     end
  127.     for i = 1, b3 do
  128.         while r.detect() or not r.forward() do  r.swing() end
  129.         x = x + dx
  130.         z = z + dz
  131.     end
  132.     turn(b1)
  133. end
  134.  
  135. -- ============================== Ф У Н К Ц И И ============================= --
  136. -- Таблица ресурсов
  137. local resources = {}
  138. resources['minecraft:log'] = true
  139. resources['minecraft:coal'] = true
  140. -- Замена инструмента
  141. --[==[
  142. function zamena()
  143.  
  144.     slotInstr = 2
  145.     while (inv.getStackInInternalSlot(slotInstr) ~= nil) and (slotInstr < allSlots) do
  146.         slotInstr = slotInstr + 1
  147.     end
  148.     r.select(slotInstr)
  149.     inv.equip()
  150.     turnLeft()
  151.     r.drop()
  152.     while inv.getStackInSlot(3,1).charge < inv.getStackInSlot(3,1).maxCharge do
  153.         os.sleep(0.2)
  154.     end
  155.     r.suck()
  156.     inv.equip()
  157.     turnRight()
  158.     slotInstr = nil
  159. end
  160. --]==]
  161. -- Возврат домой
  162. function dom(noreturn)
  163.     local b1 = b
  164.     local x1 = x
  165.     local z1 = z
  166.     local item
  167.     local resources = {}
  168.     resources['minecraft:log'] = true
  169.     resources['minecraft:coal'] = true
  170.  
  171.     goTo(0, 0, 0, 2,false)
  172.  
  173.     turnLeft()
  174.     for slot = 1 , 15 do
  175.         item = inv.getStackInInternalSlot(slot)
  176.         if item then
  177.             if resources[item.name] then
  178.                 r.select(slot)
  179.                 while r.count(slot) > 0 do
  180.                     if not r.drop() then
  181.                         os.sleep(4)
  182.                         term.clear()
  183.                         print('освободите инвентарь для остальных вещей')
  184.                     end
  185.                 end  
  186.             elseif item.name == "minecraft:sapling" then
  187.                 r.select(slot)
  188.                 r.dropDown()
  189.             end
  190.         end
  191.     end
  192.  
  193.     r.select(16)
  194.     r.suckDown(64-r.count(16))
  195.    
  196.     turnAround()
  197.     for slot = 1 , 15 do
  198.         if r.count(slot) > 0 then
  199.             r.select(slot)
  200.             while r.count(slot) > 0 do
  201.                 if not r.drop() then
  202.                     os.sleep(4)
  203.                     term.clear()
  204.                     print('освободите инвентарь для остальных вещей')
  205.                 end
  206.             end  
  207.         end
  208.     end
  209.     turnRight()
  210.     if r.durability() < 0.3 then
  211.         if not replace_instrument then
  212.             while r.durability() < 0.5 do
  213.                 os.sleep(8)
  214.             end
  215.         else
  216.             zamena()
  217.         end
  218.     end
  219.    
  220.     -- зарядка
  221.     while comp.energy() < 25000 do
  222.         os.sleep(8)
  223.     end
  224.  
  225.     -- возвращаемся если нет флага "в один конец"
  226.     if not noreturn then
  227.         goTo(x1, z1, 0, b1, true)
  228.     end
  229. end
  230.  
  231. -- Проверка условий, на возврат домой
  232. function usl()
  233.     if r.count(15) > 32 then
  234.         dom(false)
  235.     elseif r.count(16) < 8 then
  236.         dom(false)
  237.     elseif comp.energy() < 11000 then
  238.         dom(false)
  239.     elseif r.durability() < 0.3 then
  240.         dom(false)
  241.     end
  242. end
  243. --Рубка дерева
  244. function axe()
  245.     r.select(16)
  246.     if not r.compare(true) then
  247.         r.swing()
  248.         r.place()
  249.     end
  250. end
  251.  
  252. -- ======================== Г Л А В Н Ы Й   Ц И К Л ======================== --
  253.  
  254. -- робот движется по полю по кругу,
  255. -- и рубит выросшие деревья
  256. while true do
  257.     num_of_turning = 1
  258.     db = 0
  259.     -- робот едет на стартовую позицию
  260.     -- (левый передний угол поля)
  261.     for i = 1, 4 do
  262.         forward()
  263.     end
  264.     -- проход по ферме
  265.     for i=1, num_all do
  266.         -- проверка дерева слева и справа
  267.         usl()
  268.         forward()
  269.  
  270.         turnLeft()
  271.         axe()
  272.         turnAround()
  273.         axe()
  274.         turnLeft()
  275.  
  276.         os.sleep(1)
  277.         while tracktorSuck() do end -- tractorSuck
  278.  
  279.         if rast_z == 1 or i%num_of_blocks_z == 0 then
  280.             forward()
  281.         else
  282.             for zz = 1, rast_z do
  283.                 forward()
  284.             end
  285.         end
  286.  
  287.         if i%num_of_blocks_z == 0 and i<num_all then
  288.             turn(1)
  289.             for j=1, 3 + rast_x do forward() end
  290.             db = (db + 2)%4
  291.             turn(db)
  292.             num_of_turning = num_of_turning + 1
  293.         end
  294.     end
  295.  
  296.     -- робот возвращается домой
  297.     -- (флаг true означает - в один конец)
  298.     dom(true)
  299.  
  300.     -- робот спит и видит сны =)
  301.     term.clear()
  302.     print('Progress of sleeping')
  303.     for i=1,10 do
  304.         os.sleep(72)
  305.         print(i*10,' %')
  306.     end
  307.     term.clear()
  308.     print('moving')
  309. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement