Advertisement
Guest User

Untitled

a guest
May 4th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. --[[
  2. Simple BigReactors Reactor Control program
  3. Usage:
  4. bigreactors-control [-s] [turnOn turnOff]
  5. -s makes the program not print anything to the screen; will automatically enable this option if there is no screen and GPU available
  6. Optional arguments are turnOn and turnOff, allowing you to specify when to turn the reactor on and when to turn it off. Default values are 0.1 and 0.9
  7. Author: Vexatos
  8. ]]
  9.  
  10. --These are the values everyone needs to set for themselves
  11.  
  12. --Default 0.1 - Turn on when equal to or below 10%
  13. local turnOn = 0.1
  14. --Default 0.9 - Turn off when equal to or above 90%
  15. local turnOff = 0.9
  16. --The maximum amount of energy the reactor can store
  17. local maxEnergy = 10000000
  18.  
  19.  
  20. --Code you probably won't need to change starts here
  21.  
  22.  
  23. --Loading the required libraries
  24. local component = require("component")
  25. local keyboard = require("keyboard")
  26. local term = require("term")
  27.  
  28. --This is true if there is no available screen or the option -s is used
  29. local silent = not term.isAvailable()
  30.  
  31. do
  32. local shell = require("shell")
  33. local args, options = shell.parse(...)
  34. if options.s then silent = true end
  35. if #args > 0 then
  36. if #args < 2 then
  37. if silent then
  38. error("invalid number of arguments. needs to be 0 or 2")
  39. else
  40. io.stderr:write("invalid number of arguments, needs to be 0 or 2")
  41. return
  42. end
  43. else
  44. turnOn = tonumber(args[1])
  45. turnOff = tonumber(args[2])
  46. end
  47. end
  48. end
  49.  
  50. if turnOn < 0 or turnOn > 1 or turnOff < 0 or turnOff > 1 then
  51. if silent then
  52. error("turnOn and turnOff both need to be between 0 and 1")
  53. else
  54. io.stderr:write("turnOn and turnOff both need to be between 0 and 1")
  55. return
  56. end
  57. end
  58.  
  59. --Check whether there is a Reactor Computer Port to access
  60. if not component.isAvailable("br_reactor") then
  61. if silent then
  62. error("no connected Reactor Computer Port found.")
  63. else
  64. io.stderr:write("This program requires a connected Reactor Computer Port to run.")
  65. return
  66. end
  67. end
  68.  
  69. --Getting the primary port
  70. local reactor = component.br_reactor
  71.  
  72. --Displays long numbers with commas
  73. local function fancyNumber(n)
  74. return tostring(math.floor(n)):reverse():gsub("(%d%d%d)", "%1,"):gsub("%D$",""):reverse()
  75. end
  76.  
  77. --Displays numbers with a special offset
  78. local function offset(num, d)
  79. if type(num) ~= "string" then
  80. if type(num) == "number" then
  81. return offset(tostring(math.floor(num)), d)
  82. end
  83. return offset(tostring(num), d)
  84. end
  85. if d <= #num then return num end
  86. return string.rep(" ", d - #num) .. num
  87. end
  88.  
  89. if not silent then
  90. component.gpu.setResolution(component.gpu.maxResolution())
  91. term.clear()
  92.  
  93. print("Press Ctrl+W to stop.")
  94. end
  95.  
  96. --Get the current y position of the cursor for the RF display
  97. local _,y = term.getCursor()
  98.  
  99. --The interface offset
  100. local offs = #tostring(maxEnergy) + 5
  101.  
  102. while true do
  103. --Get the current amount of energy stored
  104. local stored = reactor.getEnergyStored()
  105.  
  106. if stored/maxEnergy <= turnOn and not reactor.getActive() then
  107. --The reactor is off, but the power is below the turnOn percentage
  108. reactor.setActive(true)
  109. elseif stored/maxEnergy >= turnOff and reactor.getActive() then
  110. --The reactor is on, but the power is above the turnOff percentage
  111. reactor.setActive(false)
  112. end
  113.  
  114. --Write the reactor state, the currently stored energy, the percentage value and the current production rate to screen
  115. if not silent then
  116. term.setCursor(1, y)
  117. term.clearLine()
  118. local state = reactor.getActive()
  119. if state then
  120. state = "On"
  121. else
  122. state = "Off"
  123. end
  124. term.write("Reactor state: " .. offset(state, offs) .. "\n", false)
  125. term.clearLine()
  126. term.write("Currently stored: " .. offset(fancyNumber(stored), offs) .. " RF\n", false)
  127. term.clearLine()
  128. term.write("Stored percentage: " .. offset(stored / maxEnergy * 100, offs) .. " %\n", false)
  129. term.clearLine()
  130. term.write("Current Production: " .. offset(reactor.getEnergyProducedLastTick(), offs) .. " RF/t", false)
  131. end
  132.  
  133. --Check if the program has been terminated
  134. if keyboard.isKeyDown(keyboard.keys.w) and keyboard.isControlDown() then
  135. --Shut down the reactor, place cursor in a new line and exit
  136. if not silent then
  137. term.write("\nReactor shut down.\n")
  138. end
  139. reactor.setActive(false)
  140. os.exit()
  141. end
  142. os.sleep(1)
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement