Advertisement
sanovskiy

Nuclear Reactor Controller

May 29th, 2015
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. os.loadAPI('debug')
  2.  
  3. POSTPONE_TRESHOLD = 80 -- the percentage for the delayed start
  4. TYPE_REACTOR = 'nuclear_reactor'
  5. TYPE_MFSU = 'mfsu'
  6.  
  7. function findPeripherals(type) -- find all peripherals of certain typr
  8. local result = {}
  9. local peripherals = peripheral.getNames()
  10. for i = 1,#peripherals do
  11. print( 'found '.. peripherals[i] .. ' - '..peripheral.getType(peripherals[i]) )
  12. if peripheral.getType(peripherals[i]) == type then
  13. table.insert(result,{ID = peripherals[i],core = peripheral.wrap(peripherals[i])})
  14. end
  15. end
  16. return result
  17. end
  18.  
  19. function cls()
  20. term.clear()
  21. term.setCursorPos(1,1)
  22. end
  23.  
  24. function round(num, idp)
  25. local mult = 10^(idp or 0)
  26. return math.floor(num * mult + 0.5) / mult
  27. end
  28.  
  29. reactor = findPeripherals(TYPE_REACTOR)[1]
  30. mfsu = findPeripherals(TYPE_MFSU)[1]
  31.  
  32. reactor_on = false
  33. postpone_enabling = false
  34. while true do
  35. os.queueEvent("randomEvent")
  36. os.pullEvent()
  37. mfsuEnergy = mfsu.core.getEUStored()
  38. mfsuMax = mfsu.core.getEUCapacity()
  39. mfsuPercentage = round((mfsuEnergy/mfsuMax)*10000)/100;
  40.  
  41. if postpone_enabling and mfsuPercentage<POSTPONE_TRESHOLD then
  42. postpone_enabling = false
  43. end
  44.  
  45. if mfsuEnergy<mfsuMax and not(postpone_enabling) then
  46. reactor_on = true
  47. end
  48.  
  49. redstone.setOutput(reactor.ID,reactor_on)
  50. cls()
  51. print('Reactor status: '..(reactor_on and 'enabled' or 'disabled'))
  52. print('Reactor real status: '..(reactor.core.isActive() and 'enabled' or'disabled'))
  53. print('Reactor generation: '..reactor.core.getEUOutput())
  54. print('Energy storage: '..mfsuEnergy..'/'..mfsuMax..' ('..mfsuPercentage..')')
  55. sleep(1)
  56. end
  57.  
  58. -- debug.dump(reactor)
  59. -- debug.dump(mfsu)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement