Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. --[[
  2. Conservative reactor control program.
  3. Very simple to use, start the program and enjoy, no need to fiddle with configurations or settings.
  4.  
  5. Will display the Reactor state (on or off), reserve percentage, current RF/t production, fuel level, fuel reactivity level, temperatures and fuel rod insertion level.
  6. Does NOT currently work on activly cooled reactors, this program is for passive cooled only.
  7. Author: Joker119
  8. ]]
  9.  
  10.  
  11. --The maximum amount of energy a reactor can store
  12. local maxEnergy = 10000000
  13.  
  14. --Loading the required libraries
  15. local component = require("component")
  16. local keyboard = require("keyboard")
  17. local term = require("term")
  18.  
  19. --This is true if there is no available screen or the option -s is used
  20. local silent = not term.isAvailable()
  21.  
  22. local hasCustomValues, shouldChangeRods = false, false
  23.  
  24. local function serror(msg, msg2)
  25. msg2 = msg2 or msg
  26. if silent then
  27. error(msg, 2)
  28. else
  29. io.stderr:write(msg2)
  30. os.exit()
  31. end
  32. end
  33.  
  34. do
  35. local shell = require("shell")
  36. local args, options = shell.parse(...)
  37. if options.s then silent = true end
  38. if options.b then shouldChangeRods = true end
  39. if #args > 0 then
  40. turnOn = tonumber(args[1])
  41. turnOff = tonumber(args[2])
  42. hasCustomValues = true
  43. end
  44. end
  45.  
  46. --Check whether there is a Reactor Computer Port to access
  47. if not component.isAvailable("br_reactor") then
  48. serror("No connected Reactor Computer Port found.", "This program requires a connected Reactor Computer Port to run.")
  49. end
  50.  
  51. --Getting the primary port
  52. local reactor = component.br_reactor
  53.  
  54. --Displays long numbers with commas
  55. local function fancyNumber(n)
  56. return tostring(math.floor(n)):reverse():gsub("(%d%d%d)", "%1,"):gsub("%D$",""):reverse()
  57. end
  58.  
  59. --Displays numbers with a special offset
  60. local function offset(num, d, ext)
  61. if num == nil then return "" end
  62. if type(num) ~= "string" then
  63. if type(num) == "number" then
  64. if ext then
  65. return offset(tostring(math.floor(num * 100) / 100), d)
  66. else
  67. return offset(tostring(math.floor(num)), d)
  68. end
  69. end
  70. return offset(tostring(num), d)
  71. end
  72. if d <= #num then return num end
  73. return string.rep(" ", d - #num) .. num
  74. end
  75.  
  76. if not silent then
  77. component.gpu.setResolution(component.gpu.maxResolution())
  78. term.clear()
  79.  
  80. print("Press Ctrl+W to shutdown.")
  81. end
  82.  
  83. --Get the current y position of the cursor for the RF display
  84. local y, h
  85. do
  86. local x,w
  87. x,y = term.getCursor()
  88. w,h = component.gpu.getResolution()
  89. end
  90.  
  91. --The interface offset
  92. local offs = #tostring(maxEnergy) + 5
  93.  
  94. local function handleReactor()
  95. --Get the current amount of energy stored
  96. local stored = reactor.getEnergyStored()
  97. local stored_pec = stored / maxEnergy * 100
  98.  
  99. --Set Reactor Control Rod based on energy stored
  100. reactor.setAllControlRodLevels(stored_perc)
  101.  
  102. --Write the reactor state, the currently stored energy, the percentage value and the current production rate to screen
  103. if not silent then
  104. term.setCursor(1, y)
  105. term.clearLine()
  106. local state = reactor.getActive()
  107. if state then
  108. state = "On"
  109. else
  110. state = "Off"
  111. end
  112. term.write("Reactor state: " .. offset(state, offs) .. "\n", false)
  113. term.clearLine()
  114. term.write("Reserve level: " .. offset(stored / maxEnergy * 100, offs) .. " %\n", false)
  115. term.clearLine()
  116. term.write("Current Production: " .. offset(fancyNumber(reactor.getEnergyProducedLastTick()), offs) .. " RF/t\n", false)
  117. term.clearLine()
  118. term.write("Fuel level: " .. offset(fancyNumber(reactor.getFuelAmount() / reactor.getFuelAmountMax() * 100 + 1), offs) .. " %n", false)
  119. term.clearLine()
  120. term.write("Fuel reactivity: " .. offset(fancyNumber(reactor.getFuelReactivity()), offs) .. " %\n", false)
  121. term.clearLine()
  122. term.write("Control Rod Insertion: " .. offset(reactor.getControlRodLevel(1), offs) .. " %\n", false)
  123. term.clearLine()
  124. end
  125. end
  126.  
  127. while true do
  128. handleReactor()
  129.  
  130. --Check if the program has been terminated
  131. if keyboard.isKeyDown(keyboard.keys.w) and keyboard.isControlDown() then
  132. --Shut down the reactor, place cursor in a new line and exit
  133. if not silent then
  134. term.write("\nReactor shut down.\n")
  135. end
  136. reactor.setActive(false)
  137. os.exit()
  138. end
  139. os.sleep(1)
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement