Advertisement
demongts1998

Ставит схему в реактор

Jan 5th, 2021 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.53 KB | None | 0 0
  1. -- установщик схемы в реактор IC2 by demongts1998
  2.  
  3. local circuits = {
  4.   [360] = {
  5.     5,6,4,5,6,4,5,6,4,
  6.     6,1,6,6,1,6,6,1,6,
  7.     4,6,5,4,6,5,4,6,5,
  8.     5,6,4,5,6,4,5,6,4,
  9.     6,1,6,6,1,6,6,1,6,
  10.     4,6,5,4,6,5,4,6,5
  11.   },
  12.   [420] = {
  13.     1,4,6,3,6,6,4,6,5,
  14.     5,4,6,6,4,6,6,1,6,
  15.     5,6,1,6,6,1,6,6,4,
  16.     4,6,6,4,6,6,4,6,5,
  17.     6,1,6,6,1,6,6,1,6,
  18.     5,6,4,5,6,4,5,6,4
  19.   },
  20.   [1056] = {
  21.     2,7,7,4,4,7,7,2,5,
  22.     7,3,3,7,7,3,3,7,5,
  23.     4,7,7,4,4,7,7,4,5,
  24.     4,7,7,4,4,7,7,4,5,
  25.     7,3,3,7,7,3,3,7,5,
  26.     2,7,7,4,4,7,7,2,5
  27.   },
  28.   [1320] = {
  29.     7,3,7,3,7,3,7,3,7,
  30.     2,7,4,7,4,7,4,7,2,
  31.     7,3,7,3,7,3,7,3,7,
  32.     4,7,4,7,2,7,4,7,4,
  33.     7,3,7,3,7,3,7,3,7,
  34.     2,7,4,7,4,7,4,7,2
  35.   }
  36. }
  37.  
  38. local components = {
  39.   [1] = "IC2:reactorUraniumQuad",
  40.   [2] = "IC2:reactorMOXQuad",
  41.   [3] = "IC2:reactorHeatSwitchSpread",
  42.   [4] = "IC2:reactorVentSpread",
  43.   [5] = "IC2:reactorPlating",
  44.   [6] = "IC2:reactorVentGold",
  45.   [7] = "IC2:reactorVentDiamond"
  46. }
  47.  
  48. local translate = {
  49.   [1] = "счетверённый стержень (УРАН)",
  50.   [2] = "счетверённый стержень (MOX)",
  51.   [3] = "компонентный теплообменник",
  52.   [4] = "компонентный теплоотвод",
  53.   [5] = "реакторная обшивка",
  54.   [6] = "разогнанный теплоотвод",
  55.   [7] = "улучшенный теплоотвод"
  56. }
  57.  
  58. local com = require('component')
  59. local computer = require("computer")
  60. local event = require("event")
  61. local gpu = com.gpu
  62. local w,h = gpu.getViewport()
  63.  
  64. if not com.isAvailable("robot") then
  65.   print("только роботы могут использовать эту программу")
  66.   os.exit()
  67. end
  68. local r = require("robot")
  69. local invsize = r.inventorySize()
  70.  
  71. if not com.isAvailable("inventory_controller") then
  72.   print("нет контроллера инвентаря")
  73.   os.exit()
  74. end
  75. local i_c = com.inventory_controller
  76.  
  77. os.execute("cls")
  78. local a,set = {...}
  79. local atr = tonumber(a[1])
  80. if atr and circuits[atr] then
  81.   set = atr
  82. else
  83.   gpu.set(1,1,"выберите схему :")
  84.   local x = 18
  85.   for i, d in pairs(circuits) do
  86.     gpu.set(x,1,tostring(i))
  87.     x = x + string.len(i) + 2
  88.   end
  89.   print()
  90.   set = tonumber(io.read())
  91.   if not circuits[set] then
  92.     print("схема не найдена")
  93.     os.exit()
  94.   end
  95. end
  96. gpu.fill(1,1,w,2," ")
  97. gpu.set(1,1,"выбрана схема : "..set.." eu/t")
  98. local circuit = {}
  99. for i, d in pairs(circuits) do
  100.   if set == i then
  101.     for a, b in pairs(d) do
  102.       table.insert(circuit, b)
  103.     end
  104.   end
  105. end
  106.  
  107. gpu.set(1,5,"необходимые компоненты")
  108. gpu.set(1,6,string.rep("─",50))
  109. gpu.set(30,5,"всего")
  110.  
  111. local function status(msg)
  112.   gpu.fill(1,3,w,1," ")
  113.   gpu.set(1,3,msg)
  114. end
  115.  
  116. local drop = 0
  117. local scaninv = {}
  118.  
  119. local function scan()
  120.   for slot = 1, invsize do
  121.     if r.count(slot) == 0 then
  122.       r.select(slot)
  123.       i_c.equip()
  124.       break
  125.     end
  126.   end
  127.   status("сканирую инвентарь робота")
  128.   scaninv = {}
  129.   for inv = 1, invsize do
  130.     if r.count(inv) > 0 then
  131.       local item = i_c.getStackInInternalSlot(inv)
  132.       for key, val in ipairs(components) do
  133.         if item.name == components[key] then
  134.           table.insert(scaninv,{inv,val})
  135.           break
  136.         end
  137.       end
  138.     end
  139.   end
  140.   local count,y = 0,6
  141.   for key, val in ipairs(components) do
  142.     count = 0
  143.     for ind, dat in ipairs(circuit) do
  144.       if key == dat then
  145.         count = count + 1
  146.       end
  147.     end
  148.     if count > 0 then
  149.       y = y + 1
  150.       gpu.set(1,y,translate[key])
  151.       gpu.set(32,y,tostring(count))
  152.       count = 0
  153.       for ind, dat in ipairs(circuit) do
  154.         if ind >= drop + 1 and key == dat then
  155.           count = count + 1
  156.         end
  157.       end
  158.       for slot, d in ipairs(scaninv) do
  159.         if scaninv[slot][2] == val then
  160.           count = count - r.count(scaninv[slot][1])
  161.         end
  162.       end
  163.       if count > 0 then
  164.         gpu.set(40,5,"не хватает")
  165.         gpu.set(40,y,tostring(count).." ")
  166.       else
  167.         gpu.set(40,5,"хватает   ")
  168.         gpu.set(40,y,"  ")
  169.       end
  170.     end
  171.   end
  172.   status("выставляю схему в реакторе")
  173. end
  174.  
  175. local time_start = computer.uptime()
  176. scan()
  177. for ind, d in ipairs(circuit) do
  178.   while ind ~= drop do
  179.     for slot, val in ipairs(scaninv) do
  180.       if scaninv[slot][2] == components[d] then
  181.         if r.count(scaninv[slot][1]) > 0 then
  182.           r.select(scaninv[slot][1])
  183.           r.drop(1)
  184.           drop = drop + 1
  185.           if r.count(scaninv[slot][1]) == 0 then
  186.             table.remove(scaninv,slot)
  187.           end
  188.         end
  189.         break
  190.       end
  191.     end
  192.     if ind ~= drop then
  193.       status("требуется - "..translate[d])
  194.       while true do
  195.         local e = {event.pull()}    
  196.         if e[1] == "inventory_changed" and r.count(e[2]) > 0 then
  197.           status("ожидаю компоненты")
  198.           os.sleep(5)
  199.           scan()
  200.           break
  201.         elseif e[1] == "key_down" and e[4] == 29 or e[4] == 157 then
  202.           os.execute("cls")
  203.           os.exit()
  204.         end
  205.       end
  206.     end
  207.   end
  208. end
  209. os.execute("cls")
  210. print("готово "..os.date("%M:%S",computer.uptime()-time_start))
  211.  
  212. for i = 1, invsize do
  213.   if r.count(i) == 0 then
  214.     for j = invsize, 1, -1 do
  215.       if r.count(j) > 0 then
  216.         if j < i then
  217.           break
  218.         end
  219.         r.select(j)
  220.         r.transferTo(i)
  221.         break
  222.       end
  223.     end
  224.   end
  225. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement