ArabianMine

essentia_controller.lua

Jul 11th, 2022 (edited)
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.38 KB | None | 0 0
  1. local component = require("component")
  2. local serialization = require("serialization")
  3. local computer = component.computer
  4. local me = component.me_controller
  5. local io = require("io")
  6. local os = require("os")
  7. local db = component.proxy("da7631fe-9737-4872-921c-fa0aa5e4e76a") -- Database filter with Jars
  8. local interface_to_placer = component.proxy("16be86ff-5248-4ed8-bdb9-a9b98f38ed9b") -- Export Bus to Placer
  9. local rs = component.proxy("6a314dad-8b86-4821-ad83-82ac602fbd50") -- Redstone I/O
  10. local me_interface = component.proxy("ebe2e35d-443c-4aac-81de-a7293db6c228") -- me_interface to Robot
  11. local term = require("term")
  12. local thread = require("thread")
  13. local jar_tmp = component.proxy("bbcaff5a-d326-47b6-9652-8444805ebc5a") -- Temp Jar
  14. local jar_exist = component.type("bbcaff5a-d326-47b6-9652-8444805ebc5a")
  15.  
  16. -- db size: 54
  17. -- side: 5 - сторона dynamist tablet'a
  18. -- side: 2 - сторона toggle bus'a
  19. -- side: 1 - сторона block placer'a
  20.  
  21. zink = {name="zink",total=2,{element="Metallum", count=2}, {element="Sano", count=1}}
  22. antimony = {name="antimony",total=2,{element="Metallum", count=2}, {element="Aqua", count=1}}
  23. nickel = {name="nickel",total=2,{element="Metallum", count=2}, {element="Ignis", count=1}}
  24. cobalt = {name="cobalt",total=2,{element="Metallum", count=2}, {element="Instrumentum", count=1}}
  25. electrum = {name="electrum",total=2,{element="Metallum", count=2}, {element="Lucrum", count=1}}
  26. brass = {name="brass",total=2,{element="Metallum", count=2}, {element="Instrumentum", count=1}}
  27. battery_alloy = {name="battery_alloy",total=3,{element="Metallum", count=2}, {element="Aqua", count=1}, {element="Ordo", count=1}}
  28. soldering_alloy = {name="soldering_alloy",total=3,{element="Metallum", count=2}, {element="Aqua", count=1}, {element="Vitreus", count=1}}
  29. invar = {name="invar",total=2,{element="Metallum", count=2}, {element="Gelum", count=1}}
  30. cupronickel = {name="cupronickel",total=3,{element="Metallum", count=2}, {element="Ignis", count=1}, {element="Permutatio", count=1}}
  31. bismuth = {name="bismuth",total=2,{element="Metallum", count=2}, {element="Instrumentum", count=1}}
  32. aluminium = {name="aluminium",total=4,{element="Metallum", count=2}, {element="Ignis", count=1}, {element="Ordo", count=1}, {element="Volatus", count=1}}
  33.    
  34. function has_value (tab, val)
  35.     if tab[1] then
  36.         for a,b in ipairs(tab) do
  37.             if string.find(b.label, val) then
  38.                 return true
  39.             end
  40.         end
  41.         return false  
  42.     else
  43.         for key, value in pairs(tab) do
  44.             if key == val then
  45.                 return true
  46.             end
  47.         end
  48.  
  49.         return false
  50.     end
  51. end
  52.  
  53.  
  54. function tablelength(T)
  55.   local count = 0
  56.   for _ in pairs(T) do count = count + 1 end
  57.   return count
  58. end
  59.  
  60. -- zink             == Metallum:2, Sano:1
  61. -- antimony         == Metallum:2, Aqua:1
  62. -- nickel           == Metallum:2, Ignis:1
  63. -- cobalt           == Metallum:2, Instrumentum:1
  64.  
  65. -- electrum         == Metallum:2, Lucrum:1
  66. -- brass            == Metallum:2, Instrumentum:1
  67. -- battery_alloy    == Metallum:2, Aqua:1, Ordo:1
  68. -- soldering_alloy  == Metallum:2, Aqua:1, Vitreus:1
  69. -- invar            == Metallum:2, Gelum:1
  70. -- cupronickel      == Metallum:2, Ignis:1, Permutatio:1
  71. -- bismuth          == Metallum:2, Instrumentum:1
  72.  
  73. -- aluminium        == Metallum:2, Ignis:1, Ordo:1, Volatus:1
  74.  
  75. print("====== Choise Ingot =======\n\n==>  zink         <==\n==>  antimony          <==\n==>  nickel        <==\n==>  cobalt        <==\n\n==>  electrum        <==\n==>  brass         <==\n==>  battery_alloy     <==\n==>  soldering_alloy    <==\n==>  invar        <==\n==>  cupronickel   <==\n==>  bismuth           <==\n\n==>  aluminium   <==")
  76.  
  77. function ingot_info(ingot)
  78.     if ingot[4] then
  79.     print("Current Ingot: "..ingot.name.."\nEssentia's for create: "..ingot.total.."\nElements: "..ingot[1].element.." | "..ingot[2].element.." | "..ingot[3].element.." | "..ingot[4].element.."\n")
  80.     elseif not ingot[4] and ingot[3] then
  81.         print("Current Ingot: "..ingot.name.."\nEssentia's for create: "..ingot.total.."\nElements: "..ingot[1].element.." | "..ingot[2].element.." | "..ingot[3].element.."\n")
  82.     elseif not ingot[4] and not ingot[3] then
  83.         print("Current Ingot: "..ingot.name.."\nEssentia's for create: "..ingot.total.."\nElements: "..ingot[1].element.." | "..ingot[2].element.."\n")
  84.     end
  85. end
  86. ingot = "aluminium"
  87. --ingot = io.read()
  88. if ingot == "zink" then
  89.     ingot = zink
  90.     ingot_info(ingot)
  91. elseif ingot == "antimony" then
  92.     ingot = antimony
  93.     ingot_info(ingot)
  94. elseif ingot == "nickel" then
  95.     ingot = nickel
  96.     ingot_info(ingot)
  97. elseif ingot == "cobalt" then
  98.     ingot = cobalt
  99.     ingot_info(ingot)
  100. elseif ingot == "electrum" then
  101.     ingot = electrum
  102.     ingot_info(ingot)
  103. elseif ingot == "brass" then
  104.     ingot = brass
  105.     ingot_info(ingot)
  106. elseif ingot == "battery_alloy" then
  107.     ingot = battery_alloy
  108.     ingot_info(ingot)
  109. elseif ingot == "soldering_alloy" then
  110.     ingot = soldering_alloy
  111.     ingot_info(ingot)
  112. elseif ingot == "invar" then
  113.     ingot = invar
  114.     ingot_info(ingot)
  115. elseif ingot == "cupronickel" then
  116.     ingot = cupronickel
  117.     ingot_info(ingot)
  118. elseif ingot == "bismuth" then
  119.     ingot = bismuth
  120.     ingot_info(ingot)
  121. elseif ingot == "aluminium" then
  122.     ingot = aluminium
  123.     ingot_info(ingot)
  124. else
  125.     print("Error | Put value from list")
  126.     os.exit()
  127. end
  128. os.sleep(3)
  129.  
  130. for component = 1, ingot.total do
  131.     print("Element: "..ingot[component].element.." | "..ingot[component].count)
  132. end
  133.  
  134. essentia = me.getEssentiaInNetwork()
  135. essentia_count = essentia.n
  136. ingot_essentias = {}
  137. for elem = 1, ingot.total do
  138.     table.insert(ingot_essentias, ingot[elem].element)
  139. end
  140.  
  141. --TODO: Надо доделать правильный перебор эссенций ниже
  142.  
  143. print("Проверка в реал тайме...")
  144. os.sleep(2)
  145.  
  146. function jars_in_me(essence_list)
  147.     no_jars = {}
  148.     for k,v in pairs(essence_list) do
  149.         for i=1, items_count do
  150.             if not items[i] then
  151.             elseif items[i].label == "Jar of Essentia" and items[i].aspects[1].name == k and v == false then
  152.                 name = items[i].aspects[1].name
  153.                 size = items[i].size
  154.                 no_jars[name] = size
  155.             end
  156.         end
  157.     end
  158.     return no_jars
  159. end
  160.  
  161.  
  162. function get_amount_essentia(filters)
  163.         --print("checking essence")
  164.         start_count_essentia = {}
  165.         essentia = me.getEssentiaInNetwork()
  166.         essentia_count = essentia.n
  167.         for k,v in pairs(filters) do
  168.             for i=1, essentia_count do
  169.                 if string.find(essentia[i].label, k) then
  170.                     print(k.." : "..essentia[i].amount.." | Id: "..i)
  171.                     start_count_essentia[k] = essentia[i].amount
  172.                 end
  173.             end
  174.             if not start_count_essentia[k] then
  175.                 print(k.." : ".." 0 ")
  176.                 start_count_essentia[k] = 0
  177.             end
  178.         end
  179.         --print("checking essence end")
  180.         return start_count_essentia
  181. end
  182.  
  183. function add_filters(empty_banki)
  184.     temp_banki = {}
  185.     if interface_to_placer.getInterfaceConfiguration(1) ~= nil then
  186.         for i=1,5 do interface_to_placer.setInterfaceConfiguration(i, db.address, 0) end -- Очистить фильтр
  187.         print("Interface filters cleared")
  188.     else end
  189.     for k,v in pairs(empty_banki) do
  190.         for i=1, count_banok do
  191.             for cnt=1, 54 do
  192.             banka_check = db.get(cnt)
  193.                 if banka_check.aspects[1].name == k then
  194.                     DB_NUMBER_JAR = cnt
  195.                     interface_to_placer.setInterfaceConfiguration(i, db.address, DB_NUMBER_JAR)
  196.                     print(db.get(cnt).aspects[1].name.." added to Export Bus")
  197.                     temp_banki[db.get(cnt).aspects[1].name] = DB_NUMBER_JAR
  198.                 end
  199.             end
  200.         end
  201.     end
  202.     return temp_banki
  203. end
  204.  
  205.  
  206. function place_jars()
  207.     -- amount_essence: => {"Ordo" = 5,"Metallum" = 10}
  208.     if not amount_essence then
  209.     else
  210.         --print("checking essence end")
  211.         h = 1
  212.         while h < 11 do
  213.             --print("In while to 10 circle")
  214.             if jar_exist == nil then
  215.                 --print("if jar_exist == nil then")
  216.                 rs.setOutput(1,255) rs.setOutput(1,0) -- Поставить плейсером
  217.                 print("Placed jar")
  218.                 print("Placed jar | h == "..h)
  219.                 os.sleep(1)
  220.                 jar_tmp = component.proxy("bbcaff5a-d326-47b6-9652-8444805ebc5a") -- Temp Jar
  221.                 jar_exist = component.type("bbcaff5a-d326-47b6-9652-8444805ebc5a")
  222.                 h = h + 1
  223.             elseif component.isAvailable(jar_exist) then
  224.                 for k,v in pairs(amount_essence) do
  225.                     if jar_tmp.getAspectCount(k) == 0 then
  226.                         rs.setOutput(5,255) rs.setOutput(5,0)
  227.                         os.sleep(4)
  228.                         rs.setOutput(1,255) rs.setOutput(1,0) -- Поставить плейсером
  229.                         os.sleep(1)
  230.                         h = h + 1
  231.                     end
  232.                 end
  233.             else
  234.                 rs.setOutput(1,255) rs.setOutput(1,0) -- Поставить плейсером
  235.                 print("Сработала ветка else и поставился плейсер")
  236.                 h = h + 1
  237.             end
  238.             os.sleep(0.1)
  239.             --print("Final | h == "..h)
  240.             h = h + 1
  241.         end
  242.         --print("Вышли из Final while h")
  243.         h = 1
  244.     end
  245. end
  246.  
  247. while true do
  248.     print("Working....")
  249.    
  250.     items = me.getItemsInNetwork()
  251.     items_count = items.n
  252.     essentia = me.getEssentiaInNetwork()
  253.     essentia_count = essentia.n
  254.     print("\n")
  255.     check_list_essence = {}
  256.     for component_for_ingot = 1, ingot.total do
  257.         for ess = 1, essentia_count do
  258.             if string.find(essentia[ess].label, ingot[component_for_ingot].element) then
  259.                 print("Now Essentia: "..ingot[component_for_ingot].element.." == "..essentia[ess].amount)
  260.                 check_list_essence[ingot[component_for_ingot].element] = true
  261.             elseif has_value(essentia, ingot[component_for_ingot].element) == false then
  262.                 term.clear()
  263.                 print("Element: "..ingot[component_for_ingot].element.." | ".."Error | Empty")
  264.                 print("\n\nПополните банками с "..ingot[component_for_ingot].element)
  265.                 check_list_essence[ingot[component_for_ingot].element] = false
  266.             end
  267.         end
  268.     end
  269.    
  270.     for component_for_ingot = 1, ingot.total do
  271.         rez = has_value(check_list_essence, ingot[component_for_ingot].element)
  272.         if rez == false then
  273.             check_list_essence[ingot[component_for_ingot].element] = false
  274.             print("Now Essentia: "..ingot[component_for_ingot].element.." | End")
  275.         end
  276.     end
  277.    
  278.     if check_list_essence then
  279.         function main()
  280.             -- DOWN NEW THREAD!
  281.             -- empty_banki: output ===> {Metallum=150.0}
  282.             print("empty_banki") os.sleep(0)
  283.             empty_banki = jars_in_me(check_list_essence)
  284.             print("count_banok")
  285.             count_banok = tablelength(empty_banki)
  286.             print("filters") os.sleep(0)
  287.             filters = add_filters(empty_banki)
  288.             if next(filters) ~= nil then
  289.                 print("amount_essence") os.sleep(0)
  290.                 amount_essence = get_amount_essentia(filters)
  291.                 print("place_jars") os.sleep(0)
  292.                 place_jars()
  293.                
  294.             end
  295.         end
  296.     end
  297.     main()
  298.     print("\n")
  299.     os.sleep(0.1)
  300.     term.clear()
  301. end
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308. --for item = 1, items_count do
  309. --  if not items[item] then
  310. --  else
  311. --      print(items[item].label.." == "..items[item].size)
  312. --  end
  313. --end
  314.  
  315.  
  316.  
  317.  
  318.  
Add Comment
Please, Sign In to add comment