Advertisement
maxeebon

growthhelper.lua

May 24th, 2024
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. --   автоматизация фарма дерева
  2. --   конфигурация запуска: робот смотрит в точку гед саженец, робот должен иметь сундук сверху
  3. --   для складывания надамаженного инструмента, снизу сундук где будет починеный инструмент
  4. --   при запуске инструмент должен быть в инвентаре робота
  5. --
  6.  
  7. local r = require("robot")
  8. local component = require("component")
  9. local sides = require("sides")
  10. local inv = component.inventory_controller
  11.  
  12. local toolLabel = "Hoe of Growth"   -- название инструмента
  13. local repairWaitingSleepTime = 4    -- время ожидания повторной проверки починки инструмента (в сек.)
  14. local durThreshold = 0.05           -- процент инструмента для начала починки
  15.  
  16. local rInvSize = r.inventorySize()
  17.  
  18. function criticalError(str)
  19.     print("ERROR: "..str)
  20.     os.exit()
  21. end
  22.  
  23. function findHoe()
  24.     local a
  25.     local found = false
  26.     print("finding hoe...")
  27.    
  28.     if not found and not r.durability() then
  29.         for i = 1,rInvSize do
  30.             a = inv.getStackInInternalSlot(i)
  31.             if a and a.label == toolLabel then
  32.                 found = true
  33.                 r.select(i)
  34.                 break
  35.             end
  36.         end
  37.     end
  38.     if not found then
  39.         r.select(1)
  40.         inv.equip()
  41.         a = inv.getStackInInternalSlot(1)
  42.         if a and a.label == toolLabel then
  43.             found = true
  44.         end
  45.     end
  46.     if not found then
  47.         criticalError("cannot find "..toolLabel)
  48.     end
  49.     r.transferTo(1)
  50.     r.select(1)
  51.     inv.equip()
  52. end
  53.  
  54. function init()
  55.     findHoe()
  56.     if not inv.getInventoryName(sides.bottom) then
  57.         criticalError("cannot find bottom inventory for repaired "..toolLabel)
  58.     end
  59.     if not inv.getInventoryName(sides.top) then
  60.         criticalError("cannot find top inventory for damaged "..toolLabel)
  61.     end
  62. end
  63.  
  64. function isDamaged()
  65.     return r.durability() <= durThreshold
  66. end
  67.  
  68. function placeForRepair()
  69.     inv.equip()
  70.     print("placing "..toolLabel.." for repair")
  71.     if not inv.dropIntoSlot(sides.top,1) then
  72.         criticalError("cannot place "..toolLabel.." into top inventory")
  73.     end
  74. end
  75.  
  76. function waitForRepair()
  77.     local a = nil
  78.     while not a do
  79.         print("waiting for "..toolLabel.." repairing")
  80.         os.sleep(repairWaitingSleepTime)
  81.         a = inv.getStackInSlot(sides.bottom,1)
  82.     end
  83.     if a.label ~= toolLabel then
  84.         criticalError("wrong item in bottom chest")
  85.     end
  86.     if not inv.suckFromSlot(sides.bottom,1) then
  87.         criticalError("cannot suck "..toolLabel.." from bottom inventory")
  88.     end
  89.     inv.equip()
  90. end
  91.  
  92. -- main
  93.  
  94. init()
  95. while true do
  96.     local flag,block = r.detect()
  97.     if block == "solid" then
  98.         print("block is solid")
  99.         os.sleep(0.5)
  100.     else
  101.         print("using "..toolLabel)
  102.         r.use()
  103.         if isDamaged() then
  104.             placeForRepair()
  105.             os.sleep(1)
  106.             waitForRepair()
  107.         end
  108.         os.sleep(0.01)
  109.     end
  110. end
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement