NeOzay

IC2 nuclear controle

Aug 31st, 2019
159
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. reactorName = "nuclear_reactor_3"
  2. chestName="ender_chest_0"
  3. chestSize="west"
  4. ecranSize="bottom"
  5. FuelType={"reactorMOXQuad","reactorUraniumQuad"}
  6.  
  7. reactor = peripheral.wrap(reactorName)
  8. ecran = peripheral.wrap(ecranSize)
  9. chest = peripheral.wrap(chestName)
  10.  
  11. function loadSaveTable(file)
  12.     local dossier=fs.open(file,"r")
  13.     local donn=dossier.readAll()
  14.     dossier.close()
  15.     return textutils.unserialize(donn) 
  16. end
  17.  
  18. function saveTable(table,file)
  19.     local file = fs.open(file,"w")
  20.     file.flush()
  21.     file.write(textutils.serialize(table))
  22.     file.close()
  23. end
  24.  
  25. function fusionTable(table1,table2)
  26.     nouvelleTable={}
  27.     for k,v in pairs(table2) do
  28.         table.insert(table1,v)
  29.     end
  30.     return table1
  31. end
  32.  
  33.  
  34. function rechercheInventaire(item,inventaire)
  35.     inv=inventaire
  36.    
  37.     for i=1,inv.getInventorySize() do
  38.         if inv.getStackInSlot(i) ~= nil then
  39.             if string.find(inv.getStackInSlot(i).display_name,item)~=nil then
  40.             return i
  41.             end
  42.         end
  43.     end
  44.  
  45. end
  46.  
  47. function rechercheInventaireFuel(item,inventaire)
  48.     inv=inventaire
  49.     for i,v in ipairs(item) do
  50.         for slot=1,inv.getInventorySize() do
  51.             if inv.getStackInSlot(slot) ~= nil then
  52.                 if inv.getStackInSlot(slot).name == v then
  53.                 return slot
  54.                 end
  55.             end
  56.         end
  57.     end
  58. end
  59.  
  60.  
  61.  
  62.  
  63. function stringColorisation(string,colors)
  64. x=colors
  65. for i=2,#string do
  66. x=x..colors
  67. end
  68. return x
  69. end
  70.  
  71. function statut()
  72.     if reactor.isActive() then
  73.         colorsStatut="5555555"
  74.         return "working"
  75.     else
  76.         colorsStatut="eeeeeee"
  77.         return "stopped"
  78.     end
  79. end
  80.  
  81. function reactorInventoryCheck()
  82.    
  83.     while reactorStatus=="safe" do
  84.             ecran.setCursorPos(1,4)
  85.             ecran.blit("reactorInventoryCheck: ON",stringColorisation("reactorInventoryCheck: ","0").."55",stringColorisation("reactorInventoryCheck: ON","8"))
  86.         for cycle=1,#neutronReflector do
  87.             if string.find(reactor.getStackInSlot(neutronReflector[cycle]).display_name,"Depleted") == nil then    
  88.             chest.pushItemIntoSlot(chestSize,rechercheInventaire("Neutron",chest),1,neutronReflector[cycle])
  89.  
  90.             end
  91.         end
  92.  
  93.  
  94.         for cycle=1,#fuelRod do
  95.             if string.find(reactor.getStackInSlot(fuelRod[cycle]).display_name,"Depleted") ~= nil then     
  96.            
  97.             chest.pullItemIntoSlot(chestSize,fuelRod[cycle],1)
  98.             os.sleep(1)
  99.             chest.pushItemIntoSlot(chestSize,rechercheInventaireFuel(FuelType,chest),1,fuelRod[cycle])
  100.             end
  101.         end
  102.  
  103.         for cycle=1,#coolant do
  104.             if reactor.getStackInSlot(coolant[cycle]).dmg >= 5000 then
  105.                 reactorSwichOff("Coolant Over Heat")
  106.                
  107.             break
  108.             end
  109.         end
  110.     os.sleep(20)
  111.     end
  112. ecran.setCursorPos(1,4)
  113. ecran.blit("reactorInventoryCheck: OFF",stringColorisation("reactorInventoryCheck: ","0").."eee",stringColorisation("reactorInventoryCheck: OFF","7"))
  114. end
  115.  
  116. function reactorSwichOff(raison)
  117.     state =raison
  118. redstone.setOutput("right",false)
  119. reactorStatus="warning"
  120. end
  121.  
  122. function reactorWorkTime()
  123.  
  124. if not fs.exists("clock") then
  125.  
  126. clock = {workTime={day=0,heure=0,min=0},offlineTime={day=0,heure=0,min=0}}
  127. ecran.clear()
  128.  
  129. else
  130. clock=loadSaveTable("clock")
  131.  
  132. end
  133.  
  134. ecran.clear()
  135.  
  136.  
  137.  
  138.  
  139. workTime=clock.workTime
  140.  
  141.  
  142. while reactor.isActive() do
  143.  
  144.  
  145.     if workTime.min == 60 then
  146.     workTime.min = 0
  147.     workTime.heure = workTime.heure+1
  148.    
  149.         if workTime.heure ==24 then
  150.         workTime.heure = 0
  151.         workTime.day = workTime.day+1
  152.         end
  153.     end
  154.         if workTime.min< 10 then
  155.         min="0"..workTime.min
  156.         else
  157.             min=workTime.min
  158.         end
  159.         if workTime.heure<10 then
  160.             heure="0"..workTime.heure
  161.         else
  162.             heure=workTime.heure
  163.         end
  164.         if workTime.day<10 then
  165.             day="00"..workTime.day
  166.             elseif workTime.day<100 then
  167.             day="0"..workTime.day
  168.             else
  169.                 day=workTime.day
  170.            
  171.         end
  172.     clock.workTime=workTime
  173.     saveTable(clock,"clock")
  174.     ecran.setCursorPos(15,1)
  175.     ecran.write("operating time: "..day.."d"..heure.."h"..min)
  176.     os.sleep(60)
  177.     workTime.min=workTime.min+1
  178. end
  179.  
  180. offlineTime=clock.offlineTime
  181.  
  182. while not reactor.isActive() do
  183.  
  184.  
  185.     if offlineTime.min == 60 then
  186.     offlineTime.min = 0
  187.     offlineTime.heure = offlineTime.heure+1
  188.    
  189.         if offlineTime.heure ==24 then
  190.         offlineTime.heure = 0
  191.         offlineTime.day = offlineTime.day+1
  192.         end
  193.     end
  194.         if offlineTime.min< 10 then
  195.         min="0"..offlineTime.min
  196.         else
  197.             min=offlineTime.min
  198.         end
  199.         if offlineTime.heure<10 then
  200.             heure="0"..offlineTime.heure
  201.         else
  202.             heure=offlineTime.heure
  203.         end
  204.         if offlineTime.day<10 then
  205.             day="00"..offlineTime.day
  206.             elseif offlineTime.day<100 then
  207.             day="0"..offlineTime.day
  208.             else
  209.                 day=offlineTime.day
  210.         end
  211.  
  212.     clock.offlineTime=offlineTime
  213.     saveTable(clock,"clock")
  214.     ecran.setCursorPos(17,2)
  215.     ecran.write("offline time: "..day.."d"..heure.."h"..min)
  216.     os.sleep(60)
  217.     offlineTime.min=offlineTime.min+1
  218.  
  219. end
  220.  
  221. end
  222.  
  223. function reactorHeatCore()
  224.     if reactorStatus =="safe" then
  225.        
  226.         while reactorStatus =="safe" do
  227.             if reactor.getHeat()>reactor.getMaxHeat()/2 then
  228.                 reactorSwichOff("Core Over Heat")
  229.             end
  230.         for cycle=1,#coolant do
  231.             if reactor.getStackInSlot(coolant[cycle]).dmg >= 5000  then
  232.                 reactorSwichOff("Coolant Over Heat")
  233.                
  234.             break
  235.             end
  236.         end
  237.         os.sleep(2)
  238.         end
  239.     end
  240. end
  241.  
  242. function display()
  243.  
  244. while true do
  245.     ecran.setCursorPos(1,1)
  246.     ecran.blit("REACTOR 3","111111111","999999999")
  247.     ecran.setCursorPos(1,2)
  248.  
  249.     ecran.blit("Statut: "..statut(),"00000000"..colorsStatut,stringColorisation("Statut: "..colorsStatut,"7"))
  250.  
  251.  
  252.     ecran.setCursorPos(1,3)
  253.     if reactor.isActive() then
  254.     ecran.blit("EU output:"..reactor.getEUOutput()*5,"0000000000"..stringColorisation(tostring(reactor.getEUOutput()*5),"b"),"3333333333"..stringColorisation(tostring(reactor.getEUOutput()*5),"3"))
  255.     else
  256.        
  257.         if reactorStatus == "safe" then
  258.             ecran.blit("reactor integrity: "..state,"0000000000000000000"..stringColorisation(state,"5"),"7777777777777777777"..stringColorisation(state,"7"))
  259.         else
  260.             ecran.blit("reactor integrity: "..state,"0000000000000000000"..stringColorisation(state,"f"),"eeeeeeeeeeeeeeeeeee"..stringColorisation(state,"e"))
  261.         end
  262.     end
  263. os.sleep(5)
  264. end
  265. end
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. if not fs.exists("composant") then
  274.    
  275. elementSort = {vent={},fuelRod = {},coolant = {},neutronReflector={},exchanger={}}
  276. elementName = {vent="Vent",fuelRod="Fuel",coolant="Cell",neutronReflector="Neutron",exchanger="Exchanger"}
  277. for slotNumber = 1 , reactor.getInventorySize() do
  278.     if  reactor.getStackInSlot(slotNumber) ~= nil then
  279.         for k,v in pairs(elementName) do
  280.            
  281.             if string.find(reactor.getStackInSlot(slotNumber).display_name,v) ~= nil then
  282.             table.insert(elementSort[k],slotNumber)
  283.             break
  284.             end
  285.        
  286.         end
  287.     end
  288. end
  289.  
  290. saveTable(elementSort,"composant")
  291.  
  292. os.reboot()
  293.  
  294.    
  295. else
  296. elementSort= loadSaveTable("composant")
  297.  
  298. end
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306. exchanger=elementSort.exchanger
  307. neutronReflector=elementSort.neutronReflector
  308. vent=elementSort.vent
  309. fuelRod=elementSort.fuelRod
  310. coolant=elementSort.coolant
  311. coolant=fusionTable(coolant,vent)
  312. coolant=fusionTable(coolant,exchanger)
  313.  
  314.  
  315. --[[nameDisplay.clear()
  316. nameDisplay.setCursorPos(1,1)
  317. nameDisplay.setTextScale(3.5)
  318. nameDisplay.blit("REACTOR1","eeeeeeee","77777777")
  319. ]]
  320.  
  321. ecran.clear()
  322.  
  323. reactorStatus="safe"
  324. redstone.setOutput("right",true)
  325. os.sleep(1)
  326.  
  327. parallel.waitForAll(reactorWorkTime,reactorInventoryCheck,display,reactorHeatCore)
RAW Paste Data