Advertisement
Guest User

app.lua

a guest
Mar 24th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.95 KB | None | 0 0
  1. local component = require("component")
  2. local robot = require("robot")
  3. local computer = require("computer")
  4. local event = require('event')
  5. local term = require('term')
  6.  
  7.  
  8. local magnit = component.tractor_beam
  9. local inventory = component.inventory_controller
  10. local geolyzer = component.geolyzer
  11.  
  12. --Переменные
  13. local maxSlot = 32
  14. local timer_harvest = true
  15. local stop_looting = false
  16. local maxEnergy = computer.maxEnergy()
  17. local energy = computer.energy()
  18. local percentEnergy = math.ceil((100*energy)/maxEnergy)
  19. local need_percent = 100 - percentEnergy;
  20. local charge_tool = ""
  21. local motiga_use = 0
  22. local pila_use = 0
  23. local droped = false
  24. local seeded = false
  25. local felled = false
  26. local charged = false
  27. local chargedTool = false
  28. local position = { x =0,y=0 }
  29. local stop_check_inv = false
  30. local suckLoopI = 0
  31. local suckLoopEnd = true
  32. local movingDrop = false
  33.  
  34.  
  35.  
  36. function checkBlock()
  37.  
  38. local block_name = component.geolyzer.analyze(3).name
  39.  
  40. local index = string.find(block_name, ":")
  41.  
  42. local name = string.sub(block_name, index+1)
  43.  
  44.   return name
  45.  
  46. end
  47.  
  48. --moving function start
  49. function moveFront()
  50.  
  51.   local moved = robot.forward()
  52.  
  53.   if moved == true then
  54.     os.sleep(1)
  55.     return true
  56.   else
  57.     moveFront()
  58.   end
  59.  
  60. end
  61. function moveBack()
  62.  
  63.   local moved = robot.back()
  64.  
  65.   if moved == true then
  66.     os.sleep(1)
  67.     return true
  68.   else
  69.     moveBack()
  70.   end
  71.  
  72. end
  73. function moveUp()
  74.  
  75.   local moved = robot.up()
  76.  
  77.   if moved == true then
  78.     os.sleep(1)
  79.     return true
  80.   else
  81.     moveUp()
  82.   end
  83.  
  84. end
  85. function moveDown()
  86.  
  87.   local moved = robot.down()
  88.  
  89.   if moved == true then
  90.     os.sleep(1)
  91.     return true
  92.   else
  93.     moveDown()
  94.   end
  95.  
  96. end
  97.  
  98. function moveToCharge()
  99.  
  100.   robot.turnAround()
  101.   for i = 1, 4 do
  102.     moveFront()
  103.   end
  104.   robot.turnAround()
  105.   charging()
  106.  
  107.  
  108.  
  109. end
  110.  
  111. function moveToHarvest()
  112.  
  113.   local block_name = component.geolyzer.analyze(0).name
  114.  
  115.   local index = string.find(block_name, ":")
  116.  
  117.   local name = string.sub(block_name, index+1)
  118.  
  119.   if name == "tile.BlockSkyStone" or name == "obsidian" then
  120.     timer_harvest = true
  121.  
  122.     sowing()
  123.   else
  124.     for i = 1, 4 do
  125.       moveFront()
  126.     end
  127.     timer_harvest = true
  128.  
  129.     sowing()
  130.   end
  131.  
  132.  
  133.  
  134.  
  135.  
  136. end
  137.  
  138. function returnToJob()
  139.   movingDrop = false
  140.   if charged == true then
  141.     print("charged")
  142.     for i = 1, 4 do
  143.       moveFront()
  144.     end
  145.     timer_harvest = true
  146.     charged = false
  147.     harvest()
  148.    
  149.    
  150.   end
  151.   if chargedTool == true then
  152.     print("chargedTool")
  153.     chargedTool = false
  154.    
  155.     robot.turnLeft()
  156.     for i = 1, 2 do
  157.       moveFront()
  158.     end
  159.     robot.turnLeft()
  160.     for i = 1, 2 do
  161.       moveFront()
  162.     end
  163.    
  164.     if suckLoopEnd == false then
  165.      
  166.       print(" Дайте инструмент в 1 слот, саженцы во 2, пилу в руки и наберите команду app")
  167.       for i = 1, 4 do
  168.         moveBack()
  169.       end
  170.       robot.select(1)
  171.       inventory.equip()
  172.       print("Берём пилу")
  173.      
  174.       return false
  175.      
  176.     end
  177.    
  178.     if seeded == true then
  179.       robot.select(1)
  180.       inventory.equip() -- берем в руки пилу
  181.       imer_harvest = true
  182.       chargedTool = false
  183.       harvest()
  184.     else
  185.    
  186.       if motiga_use < 4 then
  187.         robot.select(1)
  188.         inventory.equip() -- берем в руки мотыгу
  189.         robot.use()
  190.         robot.use()
  191.         robot.use()
  192.         robot.use()
  193.         motiga_use = 0
  194.         seeded = true
  195.         robot.select(1)
  196.         inventory.equip() -- берем в руки пилу
  197.        
  198.         timer_harvest = true
  199.         chargedTool = false
  200.         harvest()
  201.       end
  202.      
  203.     end
  204.    
  205.   end
  206.  
  207.  
  208.   if droped == true then
  209.     print("droped")
  210.     robot.turnLeft()
  211.     for i=1, 6 do
  212.       moveFront()
  213.     end
  214.     robot.turnLeft()
  215.     moveDown()
  216.    
  217.     droped = false
  218.     timer_harvest = true
  219.     stop_check_inv = false
  220.    
  221.     harvest()
  222.   end
  223.  
  224.  
  225. end
  226. function suckLoop()
  227.   suckLoopI = suckLoopI +1
  228.   print(" suckLoop"..suckLoopI)
  229.   if robot.suck() == false then
  230.     if suckLoopI > 50 then
  231.       suckLoopI = 0
  232.       suckLoopEnd = false
  233.       term.clear()
  234.       return false
  235.     end
  236.     os.sleep(1)
  237.     suckLoop()
  238.   end
  239.  
  240. end
  241.  
  242. function moveToChargeTool(tool)
  243.   charge_tool = tool
  244. -- 1 slot = мотыга
  245. -- equip slot = пила
  246.  
  247.   robot.turnLeft()
  248.   for i = 1, 5 do
  249.     moveFront()
  250.   end
  251.   robot.turnLeft()
  252.   for i = 1, 2 do
  253.     moveUp()
  254.   end
  255.   for i = 1, 3 do
  256.     moveFront()
  257.   end
  258.  
  259.   robot.select(1) -- меняем местами с саженцами
  260.   inventory.equip()
  261.   robot.dropDown()
  262.   os.sleep(10)
  263.   robot.turnLeft()
  264.   for i = 1, 3 do
  265.     moveFront()
  266.   end
  267.   robot.turnLeft()
  268.   moveFront()
  269.   for i = 1, 2 do
  270.     moveDown()
  271.   end
  272.   robot.turnAround()
  273.   if charge_tool == "pila" then
  274.     os.sleep(70)
  275.   else
  276.     os.sleep(10)
  277.   end
  278.   -- забираем заряженый предмет
  279.   if robot.suck() == false then
  280.     print(" Запуск suckLoop")
  281.    
  282.     suckLoop()
  283.     if suckLoopEnd == false then
  284.       print(" Насяльника, я не смог забрать инструмент после зарядки :(")
  285.     end
  286.    
  287.   end
  288.  
  289.   inventory.equip()
  290.  
  291.   if charge_tool == "motiga" then
  292.     robot.select(2)
  293.     inventory.equip()
  294.     robot.select(1)
  295.     robot.transferTo(2)
  296.   end
  297.  
  298.   if charge_tool == "pila" then
  299.     robot.select(1)
  300.     inventory.equip()
  301.   end
  302.  
  303.  
  304.   chargedTool = true
  305.   returnToJob()
  306.  
  307.  
  308. end
  309.  
  310. function backToStart()
  311.  
  312.   local y = position.y
  313.  
  314.   for i = 1, y do
  315.     moveDown()
  316.     position.y = position.y -1
  317.   end
  318.  
  319.   moveBack()
  320.  
  321.   position.x = position.x -1
  322.  
  323.  
  324.  
  325.   sowing()
  326.  
  327. end
  328.  
  329. function movingToDrop()
  330.   if not movingDrop then
  331.     movingDrop = true
  332.     robot.turnLeft()
  333.     for i =1, 6 do
  334.       moveFront()
  335.     end
  336.     moveUp()
  337.     robot.turnLeft()
  338.    
  339.     for i = 4, maxSlot do
  340.       robot.select(i)
  341.       robot.drop(64)
  342.       os.sleep(2)
  343.     end
  344.    
  345.       robot.select(1)
  346.    
  347.     droped = true
  348.     returnToJob()
  349.   end
  350. end
  351.  
  352.  
  353. --moving function end
  354.  
  355. function checkInventory()
  356.  
  357.   if stop_check_inv == false then  
  358.    
  359.     if robot.count(maxSlot) > 0 then
  360.      
  361.       timer_harvest = false
  362.       stop_check_inv = true
  363.      
  364.       return true
  365.     else
  366.       return false
  367.     end
  368.  
  369.   end
  370.  
  371.  
  372. end
  373.  
  374.  
  375.  
  376. function calcEnergy()
  377.  
  378.   maxEnergy = computer.maxEnergy()
  379.   energy = computer.energy()
  380.   percentEnergy = math.ceil((100*energy)/maxEnergy)
  381.   need_percent = 100 - percentEnergy;
  382.  
  383. end
  384.  
  385. function charging()
  386.  
  387.   local fulltime = 60
  388.   calcEnergy()
  389.   local stop_time = math.ceil(fulltime*need_percent)/100
  390.  
  391.   os.sleep(stop_time)
  392.   charged = true
  393.  
  394.   returnToJob()
  395.  
  396.  
  397. end
  398. function checkEnergy()
  399.  
  400.   calcEnergy()
  401.  
  402.   if percentEnergy < 10 then
  403.    
  404.     return false
  405.   else
  406.     return true
  407.   end
  408.  
  409. end
  410.  
  411.  
  412. function checkTool()
  413.  
  414.   local tool = robot.durability()
  415.  
  416.   if tool < 0.1 then
  417.     return false
  418.   else
  419.     return true
  420.   end
  421.  
  422. end
  423.  
  424. function swingUp()
  425.   local checkWood = true
  426.     checkWood = robot.swingUp()
  427.     pila_use = pila_use +1
  428.     if checkTool() == false then
  429.      
  430.       moveToChargeTool("pila")
  431.       return true
  432.     end
  433.     if checkWood == true then
  434.       moveUp()
  435.       position.y = position.y +1
  436.       swingUp()
  437.     else
  438.       -- Дерево срублено
  439.      
  440.       while magnit.suck() do
  441.        
  442.         magnit.suck()
  443.        
  444.       end
  445.      
  446.      
  447.      
  448.      
  449.       felled = true
  450.       backToStart()
  451.     end
  452.  
  453. end
  454.  
  455. function swingFront()
  456.  
  457.   local checkWood = false
  458.   local action = "air"
  459.  
  460.     checkWood,action = robot.swing()
  461.   if checkTool() == false then
  462.    
  463.     moveToChargeTool("pila")
  464.    
  465.     return true
  466.   end
  467.   if action == "block" then
  468.     swingFront()
  469.   else
  470.    
  471.     -- Дерево срублено
  472.      
  473.       os.sleep(2)
  474.      
  475.       while component.tractor_beam.suck() do
  476.        
  477.         component.tractor_beam.suck()
  478.        
  479.       end
  480.      
  481.      
  482.     seeded = false
  483.     felled = true
  484.    
  485.     if checkInventory() then
  486.      
  487.       movingToDrop()
  488.     end
  489.    
  490.     sowing()
  491.    
  492.   end
  493.  
  494. end
  495.  
  496.  
  497. function harvest()
  498.  
  499.  
  500.  
  501.   if checkEnergy() == false then
  502.     timer_harvest = false --Останавливаем сбор
  503.    
  504.     moveToCharge()
  505.     return false
  506.   end
  507.  
  508.  
  509.  
  510.  
  511.   if timer_harvest == true then
  512.    
  513.     swingFront()
  514.    
  515.    
  516.   else
  517.  
  518.    
  519.  
  520.   end
  521.  
  522. end
  523.  
  524. function isBlock()
  525.  
  526.   local block = checkBlock()
  527.   if block == "sapling" then
  528.     robot.swing()
  529.     return true
  530.   end
  531.   if block == "log" then
  532.     robot.swing()
  533.     isBlock()
  534.   end
  535.  
  536. end
  537.  
  538. function sowing()
  539.   if checkEnergy() == false then
  540.     timer_harvest = false --Останавливаем сбор
  541.    
  542.     moveToCharge()
  543.     return false
  544.   end
  545.  
  546.   isBlock()
  547.  
  548.   robot.select(2) -- саженцы
  549.   inventory.equip()
  550.  
  551.  
  552.   local placed,action = robot.use()
  553.  
  554.   if placed == true then
  555.    
  556.     robot.select(1) -- мотыга
  557.     inventory.equip()
  558.     if checkTool() == false then
  559.      
  560.       moveToChargeTool("motiga")--идём заряжать мотыгу
  561.       return true
  562.     end
  563.    
  564.     robot.use()
  565.     motiga_use = motiga_use +1
  566.     if checkTool() == false then
  567.      
  568.       moveToChargeTool("motiga")--идём заряжать мотыгу
  569.       return true
  570.     end
  571.     robot.use()
  572.     motiga_use = motiga_use +1
  573.     if checkTool() == false then
  574.      
  575.       moveToChargeTool("motiga")--идём заряжать мотыгу
  576.       return true
  577.     end
  578.     robot.use()
  579.     motiga_use = motiga_use +1
  580.     if checkTool() == false then
  581.      
  582.       moveToChargeTool("motiga")--идём заряжать мотыгу
  583.       return true
  584.     end
  585.     robot.use()
  586.     motiga_use = motiga_use +1
  587.     if checkTool() == false then
  588.      
  589.       moveToChargeTool("motiga")--идём заряжать мотыгу
  590.       return true
  591.     end
  592.    
  593.     motiga_use = 0
  594.     seeded = true
  595.     felled = false
  596.     robot.select(2) -- берём пилу
  597.     inventory.equip()
  598.     robot.select(1)
  599.     robot.transferTo(2) -- возвращаем порядок слотов
  600.     if checkTool() == false then
  601.      
  602.       moveToChargeTool("pila")--идём заряжать пилу
  603.       return true
  604.     end
  605.    
  606.     harvest()
  607.    
  608.    
  609.   else
  610.     robot.select(2) -- пила
  611.     inventory.equip()
  612.     sowing()
  613.    
  614.   end
  615.  
  616. end
  617.  
  618.  
  619.  
  620. moveToHarvest()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement