Advertisement
LBPHacker

Code monster for inspection by Bordo_Bereli51

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