Advertisement
CoolisTheName007

CoolisAutoReactor

Sep 28th, 2012
1,708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.06 KB | None | 0 0
  1. function everything()
  2.  
  3. print('Waiting for possible chunk loading...')
  4. sleep(2) --wait for chunk loading
  5.  
  6.  
  7. --------configurations
  8.  
  9. --sensors--
  10. AR_Sensor_Prefix=''
  11. side=sensors.getController()
  12.  
  13. --actuators--
  14. BC_side='back'
  15. BCbustransceiver_side='bottom'
  16. ACcolors={c_power=colors.purple,c_ice=colors.blue,c_uran=colors.lime,c_retriever=colors.brown}
  17. mon_side='top' --it is optional to have a monitor
  18.  
  19.  
  20. --id tags--
  21. ucell_name='item.itemCellUran'--replace 0 by number till
  22. uneardepleted_name='item.itemCellUranEmpty'
  23. ureenriched_name='item.itemCellUranEnriched'
  24. ice_name='tile.ice'
  25.  
  26. --------configurations end
  27.  
  28. message={}
  29. function update(arg)
  30.     term.clear()
  31.     if #arg>0 then message=arg end
  32.     for i, v in ipairs(message) do print(v) end
  33.     if power==true then s='On' else s='Off' end
  34.     print('Power '..s)
  35.     print('maxUran:'..tostring(maxUran)..'. To depletion:'..tostring((10000-maxUran-params.Umin)/params.Uspeed/60)..' mn.')
  36.     print('min_iceslots:'..tostring(min_iceslots))
  37.     print('ice_missing:'..tostring(ice_missing))
  38.     print('n_to_withdraw:'..tostring(n_to_withdraw))
  39.     print('n_slots_free:'..tostring(n_slots_free))
  40.     params=getSavedParams()
  41.     updateReactorsState()
  42.     for i,reactor in pairs(reactors) do
  43.         if not compress(reactor.ice_slots_start)==compress(reactor.ice_slots) then
  44.                 error('Ice slots changed in Reactor '..reactor.name..'. Pos:'..reactor.pos)
  45.         end
  46.     end
  47. end
  48.  
  49. function getReactors()
  50.     reactors={}
  51.     for i,sensor in ipairs(sensors.getSensors(side)) do
  52.         if sensor:sub(0,#AR_Sensor_Prefix)==AR_Sensor_Prefix then
  53.         if (not sensors.getAvailableReadings(side, sensor)[1]=='No Readings found') or sensors.getAvailableReadings(side, sensor)[1]=='TargetInfo' then
  54.         if sensors.getSensorInfo(side, sensor).cardType=='IndustrialCraft2 SensorModule' then
  55.             for j, target in ipairs(sensors.getAvailableTargetsforProbe(side,sensor,'ReactorContent')) do
  56.                 name='AR Reactor '..sensor:match('^'..AR_Sensor_Prefix..'(.*)')..' '..tostring(i)..':'..tostring(j)
  57.                 size=6*(3+tonumber(sensors.getSensorReadingAsDict(side,sensor,target,'Reactor').size))
  58.                 pos=target:match('(%d+,%d+,%d+)$')
  59.                 reactor={name=name, target=target, sensor=sensor, size=size, pos=pos}
  60.                 reactors[target]=reactor
  61.             end
  62.         end end end
  63.     end
  64.     new_reactors={}
  65.     for i,v in pairs(reactors) do new_reactors[#new_reactors+1]=v end
  66.     reactors=new_reactors
  67.     if #reactors==0 then print('No reactors found. Check adjacency of sensors, type of card and sensors name prefix:'..AR_Sensor_Prefix) end
  68.     return reactors
  69. end
  70.  
  71.  
  72.  
  73.  
  74. function UpdateReactorState(reactor)
  75.     local n_slots_filled=0
  76.     local ice_missing=0
  77.     local n_to_withdraw=0
  78.     local maxUran=0
  79.     local ice_slots={}
  80.     local min_iceslots=64
  81.    
  82.     for i,v in pairs(sensors.getSensorReadingAsDict(side,reactor.sensor,reactor.target,'ReactorContent')) do
  83.         n_slots_filled=n_slots_filled+1
  84.         if v:sub(-2-#ice_name,-3)==ice_name then
  85.             qty=tonumber(string.match(v,'^(%d*)%*'))
  86.             ice_missing=ice_missing+64-qty
  87.             ice_slots[#ice_slots+1]=i
  88.             min_iceslots=math.min(min_iceslots,qty)
  89.         elseif v:sub(-2-#uneardepleted_name,-3)==uneardepleted_name then
  90.             n_to_withdraw=n_to_withdraw+tonumber(v:match('^(%d+)%*'))
  91.         elseif v:sub(-2-#ureenriched_name,-3)==ureenriched_name then
  92.             n_to_withdraw=n_to_withdraw+tonumber(v:match('^(%d+)%*'))
  93.         elseif v:match(ucell_name..'@') then
  94.             m=v:match('@(%d+)$')
  95.             if m==nil then m=0 end
  96.             maxUran=math.max(m,maxUran)
  97.         end
  98.     end
  99.     table.sort(ice_slots)
  100.     reactor.ice_missing=ice_missing
  101.     reactor.n_to_withdraw=n_to_withdraw
  102.     reactor.n_slots_filled=n_slots_filled
  103.     reactor.maxUran=maxUran
  104.     reactor.ice_slots=ice_slots
  105.     reactor.min_iceslots=min_iceslots
  106. end
  107.  
  108. function aggregateReactors(reactors)
  109.     if #reactors==0 then error('No reactors to aggregate.') end
  110.     t_size=0
  111.     for i, reactor in pairs(reactors) do
  112.             UpdateReactorState(reactor)
  113.             reactor.ice_slots_start=reactor.ice_slots
  114.             t_size=t_size+reactor.size
  115.     end
  116.     print('Reactors aggregated. Total size:'..tostring(t_size))
  117. end
  118.  
  119. function compress(a)
  120.     s=''
  121.     for i=1,#a do
  122.         s=s..a[i]
  123.     end
  124.     return s
  125. end
  126.  
  127. function updateReactorsState()
  128.     ice_missing=0
  129.     n_to_withdraw=0
  130.     n_slots_filled=0
  131.     maxUran=0
  132.     min_iceslots=64
  133.     n_t_ice_slots=0
  134.     for i,reactor in pairs(reactors) do
  135.         UpdateReactorState(reactor)
  136.         ice_missing=ice_missing+reactor.ice_missing
  137.         n_to_withdraw=n_to_withdraw+reactor.n_to_withdraw
  138.         n_slots_filled=n_slots_filled+reactor.n_slots_filled
  139.         maxUran=math.max(reactor.maxUran,maxUran)
  140.         min_iceslots=math.min(min_iceslots,reactor.min_iceslots)
  141.         n_t_ice_slots=n_t_ice_slots+#reactor.ice_slots
  142.     end
  143.     n_slots_free=t_size-n_slots_filled
  144. end
  145.  
  146.  
  147. message={}
  148.  
  149.  
  150. rs.setBundledOutput(BC_side,0)
  151. BCnumbers={'1','2','4','8','16','32','64','128','256','512','1024','2048','4096','8192','16384','32768'}
  152. BCstate={}
  153. for i=1,#BCnumbers do
  154.     BCstate[BCnumbers[i]]=false
  155. end
  156. function setColor(c,Bool)
  157.     BCstate[tostring(c)]=Bool
  158.     BCnumber=0
  159.     for i=1,#BCnumbers do
  160.         if BCstate[BCnumbers[i]]==true then
  161.             BCnumber=BCnumber+tonumber(BCnumbers[i])
  162.         end
  163.     end
  164.     rs.setBundledOutput(BC_side,BCnumber)
  165. end
  166. power=false
  167. function Power(Bool)
  168.     if not (power==Bool) then
  169.         power=Bool
  170.         setColor(ACcolors.c_power,Bool)
  171.         if Bool==true then s='On' else s='Off' end
  172.         print('Power '..s)
  173.     end
  174. end
  175. function Ice(Bool)
  176.     setColor(ACcolors.c_ice,Bool)
  177. end
  178. function Retriever(Bool)
  179.     setColor(ACcolors.c_retriever,Bool)
  180. end
  181. function Uran(Bool)
  182.     setColor(ACcolors.c_uran,Bool)
  183. end
  184. --fill functions--
  185.  
  186. function fillUran()
  187.     update{}
  188.     if n_slots_free > 0 then
  189.         update{'Inserting '..tostring(n_slots_free)..' Uranium cells.'}
  190.         Uran(true)
  191.         while n_slots_free>0 do sleep(params.main_delay) update{} end
  192.         Uran(false)
  193.     end
  194. end
  195.  
  196. function retrieve()
  197.     update{}
  198.     if n_to_withdraw>0 then
  199.         update{'Retrieving '..tostring(n_to_withdraw)..' near depleted and/or re-enriched cells.'}
  200.         Retriever(true)
  201.         while n_to_withdraw>0 do sleep(params.main_delay) update{} end
  202.         Retriever(false)
  203.     end
  204. end
  205.  
  206. function fillIce()
  207.     update{'Filling with ice.'}
  208.     if ice_missing>0 then
  209.         Ice(true)
  210.         while ice_missing>0 do sleep(params.main_delay) update{} end
  211.         Ice(false)
  212.     end
  213. end
  214.  
  215.  
  216. function auto()
  217.     update{}
  218.     while true do
  219.         while 10000-maxUran<=params.Umin do
  220.             Power(false)
  221.             update{'Almost depleted cells detected. Entering depletion cycle'}
  222.             sleep(params.excess_ice_delay)
  223.             retrieve()
  224.             fillUran()
  225.             fillIce()
  226.             if 10000-maxUran<params.Umin then
  227.                 update{}
  228.                 sleep(params.excess_ice_delay)
  229.                 Power(true)
  230.                 ti=os.clock()
  231.                 sleep_duration=(10000-maxUran)/params.Uspeed+params.depletion_delay
  232.                 while os.clock()-ti<sleep_duration do
  233.                     update{}
  234.                     sleep(params.main_delay)
  235.                 end
  236.                 Power(false)
  237.                 update{}
  238.             end
  239.         end
  240.         if (n_slots_free>0 or n_to_withdraw>0) then
  241.             Power(false)
  242.             update{'Empty slots near-depleted/re-enriched cells detected.'}
  243.             sleep(params.excess_ice_delay)
  244.             retrieve()
  245.             fillUran()
  246.         end
  247.         fillIce()
  248.         Ice(true)
  249.         Power(true)
  250.         while (not (10000-maxUran<=params.Umin)) and n_slots_free==0 and n_to_withdraw==0 do
  251.             update{'In main loop.'}
  252.             sleep(params.main_delay)
  253.         end
  254.         Ice(false)
  255.         Power(false)
  256.         sleep(1)
  257.     end
  258. end
  259.  
  260. function saveReactors(reactors)
  261.     file=fs.open('disk/AR_reactors','w')
  262.     for i,reactor in  pairs(reactors) do
  263.         s='Entry{name=\''..reactor.name..'\', target=\''..reactor.target..'\', sensor=\''..reactor.sensor..'\', size=\''..reactor.size..'\', pos=\''..reactor.pos..'\', ice_slots_start=\''..compress(reactor.ice_slots_start)..'\'}'
  264.         file.writeLine(s)
  265.     end
  266.     file.close()
  267. end
  268.  
  269. function getSavedReactors()
  270.     local reactors = {}
  271.     function Entry (b) reactors[#reactors+1] = b  end
  272.     dofile("disk/AR_reactors")
  273.     for i, r in pairs(reactors) do
  274.         r.size=tonumber(r.size)
  275.     end
  276.     return reactors
  277. end
  278.  
  279.  
  280. function saveParams(params)
  281.     file=fs.open('disk/AR_params','w')
  282.     for i,v in  pairs(params) do
  283.         s='Entry{\''..tostring(i)..'\',\''..tostring(v)..'\'}'
  284.         file.writeLine(s)
  285.     end
  286.     file.close()
  287. end
  288.  
  289. function getSavedParams()
  290.     params = {}
  291.     if fs.exists('disk/AR_params') then
  292.         function Entry (b) params[b[1]]=tonumber(b[2])  end
  293.         dofile("disk/AR_params")
  294.     end
  295.     return params
  296. end
  297.  
  298. if fs.exists('disk/AR_reactors') then
  299.     print('Found saved reactors in disk/AR_reactors.\nTo reset and choose reactors, delete the file and reboot.')
  300.     reactors=getSavedReactors()
  301. else
  302.     print('List of reactors not found. Waiting for human interaction, press enter to proceed.')
  303.     read()
  304.     pre_reactors=getReactors()
  305.     reactors={}
  306.     print('Listing reactors, enter \y\+enter to confirm or \'n\'+enter to discard.')
  307.     for i, reactor in pairs(pre_reactors) do
  308.         for n, v in pairs(reactor) do print(n..':'..v) end
  309.         r=0 while not (r=='y' or r=='n') do r=tostring(read()) end
  310.         print('Choice registed')
  311.         if r=='y' then print('Keeping reactor for AR.') reactors[i]=reactor end
  312.     end
  313. end
  314.  
  315. aggregateReactors(reactors)
  316. updateReactorsState()
  317. saveReactors(reactors)
  318. print('Reactors saved to disk/AR_reactors')
  319.  
  320. rs.setOutput(BCbustransceiver_side,true)
  321.  
  322.  
  323. if fs.exists('disk/AR_params') then
  324.     print('Found operational parameters file.\nTo modify, edit disk/AR_params and save.')
  325.     params=getSavedParams()
  326. else
  327.     print('List of parameters not found. Waiting for human interaction, press enter to proceed.')
  328.     read()
  329.     function check_edit(table)
  330.         for i,v in pairs(table) do
  331.             print(i)
  332.             print('set to:'..tostring(v))
  333.             print('Edit?y/n+enter')
  334.             repeat r=read() until (r=='y' or r=='n')
  335.             if tostring(r)=='y' then
  336.             print('Write the desired value, and press enter.')
  337.                 v=tonumber(tostring(read()))
  338.             end
  339.             params[i]=v
  340.         end
  341.     end
  342.     params={}
  343.     aggregateReactors(reactors)
  344.     updateReactorsState()
  345.     check_edit({Umin=20,Uspeed=1,excess_ice_delay=5,depletion_delay=5,main_delay=5})
  346.     saveParams(params)
  347.     print('Parameters saved to disk/AR_params')
  348. end
  349.  
  350. aggregateReactors(reactors)
  351. if peripheral.isPresent('top') then if peripheral.getType('top')=='monitor' then monitor=peripheral.wrap('top') term.redirect(monitor) end end
  352.  
  353. auto()
  354.  
  355. end
  356.  
  357. local status, err = pcall(everything)
  358. if not status then rs.setOutput(BCbustransceiver_side,false) print(err) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement