ivan52

tree new

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