serafim7

автокрафт конденсаторов IC2 [OpenComputers]

Jun 3rd, 2019 (edited)
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.43 KB | None | 0 0
  1. --[[opencomputers автокрафт конденсаторов IC2 by serafim
  2.     pastebin.com/WgcCKBFn                update 03.08.20
  3.  
  4. требования:
  5. робот второго уровня, улучшение верстак,
  6. инвентарь, контроллер инвентаря.
  7.  
  8. использование:
  9. поставить робота к сундку (можно передом, сверху, снизу сундкука),
  10. положить в сундук блоки лазурита (для лазуритовх конденсаторов)
  11. или блоки редстоуна (для редстоун конденсаторов).
  12. --]]
  13.  
  14. local com = require("component")
  15. local term = require("term")
  16.  
  17. if not com.isAvailable("robot") then
  18.   print("только роботы могут использовать эту программу")
  19.   os.exit()
  20. end
  21. local r = require("robot")
  22.  
  23. if not com.isAvailable("crafting") then
  24.   print("нет улучшения верстак")
  25.   os.exit()
  26. end
  27. local craft = com.crafting
  28.  
  29. if not com.isAvailable("inventory_controller") then
  30.   print("нет контроллера инвентаря")
  31.   os.exit()
  32. end
  33. local i_c = com.inventory_controller
  34.  
  35. local chestside,side = {3,1,0} --"front","up","down"
  36. for k,n in pairs(chestside) do
  37.   local chest = i_c.getInventorySize(n)
  38.   if chest then
  39.     side = n
  40.     break
  41.   end
  42. end
  43. if side then
  44.   print("сундук в стороне: "..side)
  45.   os.sleep(1)
  46. else
  47.   print("сундук не найден")
  48.   os.exit()
  49. end
  50.  
  51. if side == 3 then
  52.   drop = function() if r.drop() then return true end end
  53. elseif side == 1 then
  54.   drop = function() if r.dropUp() then return true end end
  55. elseif side == 0 then
  56.   drop = function() if r.dropDown() then return true end end
  57. end
  58.  
  59. local sizeInv = i_c.getInventorySize(side)
  60. local dropCount = 0
  61. local slotConden = {}
  62.  
  63. local function unload(all)
  64.   if all then
  65.     print("выгрузка всего инвентаря")
  66.     for i = 1,r.inventorySize() do
  67.       if r.count(i) > 0 then
  68.         r.select(i)
  69.         drop()
  70.       end
  71.     end
  72.     return
  73.   end
  74.   if drop() then
  75.     print("выгрузка")
  76.     r.setLightColor(0x008000)
  77.     dropCount = dropCount + 1
  78.   else
  79.     print("в сундуке нет места :(")
  80.     r.setLightColor(0xFF0000)
  81.   end
  82. end
  83.  
  84. local function find_bloc()
  85.   for slot = 1,sizeInv do
  86.     local item = i_c.getStackInSlot(side, slot)
  87.     if item then
  88.       for j, name in pairs({"lapis","dye","redstone"}) do
  89.         if string.find(item.name,name) then
  90.           print(name.." в слоте: "..slot)
  91.           i_c.suckFromSlot(side, slot)
  92.           return
  93.         end
  94.       end
  95.     end
  96.   end
  97.   if r.count(1) == 0 then
  98.     print("в сундуке нет блоков для крафта")
  99.     r.setLightColor(0xFF0000)
  100.     return
  101.   end
  102. end
  103.  
  104. local function craft_conden()
  105.   for slot,n in pairs(slotConden) do
  106.     print("крафт слота: "..slot)
  107.     if r.count(2) == 0 then
  108.       r.select(2)
  109.       i_c.suckFromSlot(side, slot, 1)
  110.     end
  111.     r.select(4)
  112.     if r.count(4) > 0 then
  113.       unload()
  114.     end
  115.     while true do
  116.       if r.count(1) == 0 or r.count(2) == 0 then
  117.         return
  118.       end
  119.       if craft.craft() then
  120.         r.setLightColor(0xFF00FF)
  121.         local item = i_c.getStackInInternalSlot(4)
  122.         if item then
  123.           per = math.ceil(100*item.damage/item.maxDamage)
  124.           if per > 10 then
  125.             r.transferTo(2)
  126.           else
  127.             unload()
  128.             break
  129.           end
  130.         end
  131.       else
  132.         print("ошибка крафта :(")
  133.         r.setLightColor(0xFF0000)
  134.         unload(true)
  135.         return
  136.       end
  137.     end
  138.   end
  139. end
  140.  
  141. while true do
  142.   r.setLightColor(0xFFFFFF)
  143.   term.clear()
  144.   if r.count(2) > 0 then
  145.     r.select(2)
  146.     drop()
  147.   end
  148.   if r.count(1) == 0 then
  149.     r.select(1)
  150.     find_bloc()
  151.   else
  152.     for slot = 1,sizeInv do
  153.       local item = i_c.getStackInSlot(side, slot)
  154.       if item and string.find(item.name,"ondensator") then
  155.         per = math.ceil(100*item.damage/item.maxDamage)
  156.         if per > 50 then
  157.           print("конденсатор в слоте: "..slot.."  износ: "..per.." %")
  158.           slotConden[slot] = slot
  159.         end
  160.       end
  161.     end
  162.     craft_conden()
  163.     slotConden = {}
  164.   end
  165.   print("всего скрафтил конденсаторов: "..dropCount.."\n".."жду 5 секунд...")
  166.   os.sleep(5)
  167. end
Add Comment
Please, Sign In to add comment