Advertisement
ivan52

reactor.computer

Jun 27th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.40 KB | None | 0 0
  1. local computer = require('computer')
  2. local term = require('term')
  3. local fs = require('filesystem')
  4. local component = require('component')
  5. local rs = component.redstone
  6. local modem = component.modem
  7. local event = require('event')
  8. local serialize = require("serialization").serialize
  9. local floor = math.floor
  10. local transposer
  11.  
  12. term.clear()
  13.  
  14. RCL = {} -- reactor sheme list
  15. RCL[1]  = "lol"-- path to reactor kit
  16.  
  17. local RodList = {}
  18.  
  19. RodList['IC2:reactorUraniumSimple'] = 1
  20. RodList['IC2:reactorUraniumDual'] = 3
  21. RodList['IC2:reactorUraniumQuad'] = 5
  22. RodList['IC2:reactorMOXSimple'] = 2
  23. RodList['IC2:reactorMOXDual'] = 4
  24. RodList['IC2:reactorMOXQuad'] = 6
  25.  
  26. local RodList1 = {}
  27. RodList1[1] = 'IC2:reactorUraniumSimple'
  28. RodList1[3] = 'IC2:reactorUraniumDual'
  29. RodList1[5] = 'IC2:reactorUraniumQuad'
  30. RodList1[2] = 'IC2:reactorMOXSimple'
  31. RodList1[4] = 'IC2:reactorMOXDual'
  32. RodList1[6] = 'IC2:reactorMOXQuad'
  33.  
  34. local function copy(tbl)
  35.   local result = {}
  36.   for k, v in pairs(tbl) do
  37.     result[k] = v
  38.   end
  39.   return result
  40. end
  41.  
  42. local function sort(tbl)
  43.     for i = 1, #tbl do
  44.         for j = i + 1, #tbl do
  45.             if floor(tbl[i] / 10) > floor(tbl[j]) then
  46.                 tbl[i], tbl[j] = tbl[j], tbl[i]
  47.             end
  48.         end
  49.     end
  50. end
  51. ---[[
  52. modem.open(8)
  53. local friend
  54. local fs = io.open("friendAdres", "r")
  55. if not fs then
  56.     local SECRET_KEY_OUTPUT= "lkwejbhvlkwejbkjwerbfjwhvbjkehfvbejbwejb"
  57.     print("нажмите enter, когда включите робота")
  58.     io.read()
  59.     modem.broadcast(8, SECRET_KEY_OUTPUT)
  60.  
  61.     local SECRET_KEY_INPUT = "wqerfwevwegwergverbertgbvertesger"
  62.     while true do
  63.         local _, _, sender, _, _, message = event.pull("modem_message")
  64.         if message == SECRET_KEY_INPUT then
  65.             friend = sender
  66.             break
  67.         end
  68.     end
  69.  
  70.     fs = io.open("friendAdres", "w")
  71.     fs:write(friend)
  72.     fs:close()
  73. else
  74.     friend = fs:read()
  75.     fs:close()
  76.     term.clear()
  77. end
  78.  
  79. --]]
  80. print("какую компоновку вы хотите запустить")
  81. for i = 1, #RCL do
  82.     print(i, ": ", RCL[i])
  83. end
  84. local nameKit = io.read() + 0
  85. term.clear()
  86. local reactorKit
  87. local fs = io.open(RCL[nameKit], "r")
  88. if fs then
  89.     reactorKit = require("serialization").unserialize(fs:read())
  90.     fs:close()
  91. else
  92.     reactorKit = {
  93.         x = {},
  94.         z = {},
  95.         y = {},
  96.         b = {},
  97.         numR = {},
  98.         numChest = {},
  99.         sideChest = {},
  100.         sideT = {},
  101.         sideR = {},
  102.         adrT = {},           -- adres of transposer
  103.         adrR = {},
  104.  
  105.         reactor = {},
  106.         perepheryLoc = {x, z, y, b, y1},
  107.         sbor = {x, z, y}
  108.     }
  109.     term.clear()
  110.     adresRed = {}
  111.     --scaning
  112.  
  113.     print("координаты переферии (x, z, y, b, y1)")
  114.     io.write("x = ");   reactorKit.perepheryLoc.x = io.read() + 0
  115.     io.write("z = ");   reactorKit.perepheryLoc.z = io.read() + 0
  116.     io.write("y = ");   reactorKit.perepheryLoc.y = io.read() + 0
  117.     io.write("b = ");   reactorKit.perepheryLoc.b = io.read() + 0
  118.     io.write("y1 = ");  reactorKit.perepheryLoc.y1 = io.read() + 0 -- z1 высота, на которой нет помех для передвижения робота
  119.     term.clear()
  120.  
  121.     print("координаты места сбора (x, z, y)")
  122.     io.write("x = ");   reactorKit.sbor.x = io.read() + 0
  123.     io.write("z = ");   reactorKit.sbor.z = io.read() + 0
  124.     io.write("y = ");   reactorKit.sbor.y = io.read() + 0
  125.     term.clear()
  126.  
  127.     io.write("Количество сундуков: ") local chestNumber = io.read() + 0
  128.     term.clear()
  129.     for i = 1, chestNumber do
  130.  
  131.         print("координаты точки примыкания к сундуку ", i)
  132.         io.write("x = ");   reactorKit.x[i] = io.read() + 0
  133.         io.write("z = ");   reactorKit.z[i] = io.read() + 0
  134.         io.write("y = ");   reactorKit.y[i] = io.read() + 0
  135.         io.write("b = ");   reactorKit.b[i] = io.read() + 0
  136.  
  137.         term.clear()
  138.     end
  139.  
  140.     local i, iR, i1 , a
  141.     local react
  142.     for adres, name in component.list() do
  143.         if (name == "transposer") then
  144.             --reactorKit.nT = reactorKit.nT + 1
  145.             print(adres)
  146.             i = io.read() + 0
  147.             reactorKit.adrT[i] = adres
  148.             io.write("сторона к сундуку у транспозера: ") local n = io.read() + 0
  149.             reactorKit.sideChest[i] = n
  150.             io.write("номер сундука к транспозеру ", i, ": ")
  151.             reactorKit.numChest[i] =  io.read() + 0
  152.  
  153.             io.write("число реакторов у транспозера: ") n = io.read() + 0
  154.  
  155.             i1 = i * 10
  156.  
  157.             for j = 1, n do
  158.                
  159.                 --react = reactorKit.reactor[i1 + j]
  160.  
  161.                 io.write("(транспозер)сторона к реактору ", j, ": "); local p =  io.read() + 0
  162.                 reactorKit.sideT[i1 + j] = p
  163.                 io.write("номер редстоун блока: ")
  164.                 reactorKit.numR[i1 + j] = io.read() + 0
  165.                 io.write("(редстоун)сторона к реактору (или к проводу) ", j, ": ")
  166.                 reactorKit.sideR[i1 + j] =  io.read() + 0
  167.                
  168.                
  169.                 react = {}
  170.                 local name
  171.                 for k = 1, component.invoke(reactorKit.adrT[i], "getInventorySize", p) do
  172.                     a = component.invoke(reactorKit.adrT[i], "getStackInSlot", p, k)
  173.                     if a and RodList[a.name] then
  174.                         if (react[RodList[a.name]] == nil) then
  175.                             react[RodList[a.name]] = {}
  176.                         end
  177.                         react[RodList[a.name]].name = RodList[a.name]
  178.                         react[RodList[a.name]][#react[RodList[a.name]] + 1] = k
  179.                     end
  180.                 end
  181.                 --уплотнение
  182.                 for k = 1, #RodList1 do
  183.                     if (react[k]) then
  184.                         if (#react + 1 ~= k) then
  185.                             react[#react + 1] = react[k]
  186.                             react[k] = nil
  187.                         end
  188.                     end
  189.                 end
  190.                 -- запись в основную талицу
  191.                 for k = 1, #react do
  192.                     reactorKit.reactor[(i1 + j) * 10 + k] = react[k]
  193.                 end
  194.                 react = nil
  195.             end
  196.         elseif (name == "redstone") then
  197.             --
  198.             adresRed[#adresRed + 1] = adres
  199.         end
  200.         term.clear()
  201.     end
  202.     for j = 1, #adresRed do
  203.         print("укажите номер этого редстоун блока:")
  204.         print(adresRed[j])
  205.         i = io.read() + 0
  206.         reactorKit.adrR[i] = adresRed[j]
  207.     end
  208. --  сохранение в файл
  209.     fs = io.open(RCL[nameKit], "w")
  210.     fs:write(serialize(reactorKit))
  211.     fs:close()
  212. end
  213. react = nil
  214.  
  215.  
  216.  
  217. fs = io.open("robot", "r")
  218. if not fs then
  219.     local rob = {
  220.         x = reactorKit.x,
  221.         z = reactorKit.z,
  222.         y = reactorKit.y,
  223.         b = reactorKit.b,
  224.         numChest = reactorKit.numChest,
  225.         fuelRodName = {},
  226.         fuelRodNum = {},
  227.         perephery = reactorKit.perepheryLoc,
  228.         sbor = reactorKit.sbor
  229.     }
  230.     for i,_ in pairs(reactorKit.reactor) do
  231.         rob.fuelRodName[i] = reactorKit.reactor[i].name
  232.         rob.fuelRodNum[i] = #reactorKit.reactor[i]
  233.     end
  234.     fs = io.open("robot", "w")
  235.     fs:write(serialize(rob))
  236.     fs:close()
  237. else
  238.     rob = require("serialization").unserialize(fs:read())
  239.     fs:close()
  240. end
  241. os.sleep(5)
  242. --отправка данных роботу
  243. modem.send(friend, 8, serialize(rob))
  244. --удаление ненужных данных
  245. rob = nil
  246.  
  247. for i,side in pairs(reactorKit.sideR) do
  248.     component.invoke(reactorKit.adrR[reactorKit.numR[i]], "setOutput", side, 100)
  249. end
  250.  
  251. local iter, sideChes, packet, All, Checking, sender, inChest, react, past_packet, reactIter, robotReady
  252.  
  253.  
  254. robotReady = true
  255. packet = {}
  256. Checking = {}
  257. All = false
  258. past_packet = 1
  259.  
  260. while true do
  261.  
  262.     while true do
  263.         if not robotReady then
  264.             local _, _, sender, _, _, _= event.pull(10, "modem_message")
  265.             if sender == friend then
  266.                 os.sleep(1)
  267.                 modem.send(friend, 8, "true")
  268.                 robotReady = true
  269.                 for _,i in pairs(past_packet) do
  270.                     Checking[i] = nil
  271.                 end
  272.                 break
  273.             end
  274.         else
  275.             if packet[1] then
  276.                 sort(packet)
  277.                 modem.send(friend, 8, serialize(packet))
  278.                 past_packet = copy(packet)
  279.                 packet = {}
  280.                 robotReady = false
  281.             end
  282.         end
  283.         All = false
  284.         --scaning
  285.         if not All then
  286.             All = true
  287.             for i = 10, #reactorKit.adrT * 10, 10 do
  288.                 transposer = component.proxy(reactorKit.adrT[i / 10])
  289.                 i = i + 1
  290.                 while reactorKit.sideT[i] do
  291.                     if not Checking[i] and transposer.getStackInSlot(reactorKit.sideT[i], reactorKit.reactor[i * 10 + 1][1]).name ~= RodList1[reactorKit.reactor[i * 10 + 1].name] then
  292.                         component.invoke(reactorKit.adrR[reactorKit.numR[i]], "setOutput", reactorKit.sideR[i], 0)
  293.                         packet[#packet + 1] = i
  294.                         Checking[i] = true
  295.                         iter = 1
  296.                         while reactorKit.reactor[i*10 + iter] do
  297.  
  298.                             for numSlot,slot in pairs(reactorKit.reactor[i*10 + iter]) do
  299.                                 if numSlot ~= 'name' then
  300.                                     transposer.transferItem(reactorKit.sideT[i], reactorKit.sideChest[(i - i%10) / 10], 1, slot)
  301.                                 end
  302.                             end
  303.                             iter = iter + 1
  304.                         end
  305.                     elseif All then
  306.                         All = false
  307.                     end
  308.                     i = i + 1
  309.                 end
  310.             end
  311.         else
  312.             os.sleep(10)
  313.         end
  314.  
  315.  
  316.  
  317.         for h = 1, #packet do
  318.             print(h, ": ", packet[h])
  319.         end
  320.         io.read()
  321.         term.clear()
  322.     end
  323.     for _,i in pairs(past_packet) do
  324.         iter = 1
  325.         sideChest = reactorKit.sideChest[(i - i%10) / 10]
  326.         while reactorKit.reactor[i*10 + iter] do
  327.             react = reactorKit.reactor[i*10 + iter]
  328.             transposer = component.proxy(reactorKit.adrT[(i - i%10) / 10])
  329.             for slot = 1, transposer.getInventorySize(sideChest) do
  330.                 inChest = transposer.getStackInSlot(sideChest, slot)
  331.                 if inChest and inChest.name  == RodList1[react.name] then
  332.                     for numSlot,j in pairs(react) do
  333.                         if numSlot ~= "name" then
  334.                             transposer.transferItem(sideChest, reactorKit.sideT[i],1,slot, j)
  335.                         end
  336.                     end
  337.                 end
  338.             end
  339.             iter = iter + 1
  340.         end
  341.         component.invoke(reactorKit.adrR[reactorKit.numR[i]], "setOutput", reactorKit.sideR[i], 15)
  342.     end
  343.     past_packet = 1 -- своеобразное удаление прошлого пакета, т.к. он не нужен
  344. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement