Advertisement
Gokborg

bigreactorprogram

Sep 8th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. --Better reactor program
  2.  
  3. local reactor = peripheral.wrap("back")
  4. local monitor = peripheral.find("monitor")
  5.  
  6. --Functions at the top are lowest level
  7. --Functions at the bottom are highest level
  8.  
  9. local function clearMonitor()
  10.     monitor.clear()
  11.     monitor.setCursorPos(1,1)
  12.     monitor.setTextColor(colors.white)
  13.     monitor.setBackgroundColor(colors.black)
  14. end
  15.  
  16. local function checkReactor()
  17.     if reactor == nil then
  18.         error("Failed to find the reactor!")
  19.     elseif reactor.getConnected() == false then
  20.         error("Failed to connect to the reactor!")
  21.     end
  22. end
  23.  
  24. local function checkMonitor()
  25.     if monitor == nil then
  26.         error("Failed to find the monitor!")
  27.     end
  28. end
  29.  
  30. local function round(num)
  31.     return math.floor(num + 0.5)
  32. end
  33.  
  34. local function exact_send(x, y, msg)
  35.     monitor.setCursorPos(x,y)
  36.     monitor.write(msg)
  37. end
  38.  
  39. local function f_send(pos, y, msg)
  40.     local length = string.len(msg)
  41.     local width, height = monitor.getSize()
  42.    
  43.     if pos == "RIGHT" then
  44.         exact_send(width-length, y, msg)
  45.     elseif pos == "CENTER" then
  46.         local cx = round(width/2)-round(length/2)
  47.         exact_send(cx, y, msg)
  48.     elseif pos == "LEFT" then
  49.         exact_send(1, y, msg)
  50.     else
  51.         error("Invalid argument: "..pos)
  52.     end
  53. end
  54.  
  55. function c_send(y, msg)
  56.     f_send("CENTER", y, msg)
  57. end
  58.  
  59. --x can be a # or a position ('LEFT', 'RIGHT', 'CENTER')
  60. function send(x, y, msg)
  61.     if type(x) == "string" then
  62.         f_send(x, y, msg)
  63.         return
  64.     end
  65.     exact_send(x, y, msg)
  66. end
  67.  
  68. local function draw_bar(x, y, percent)
  69.     local bar = "["
  70.     local limit = round(percent/10)
  71.     for i=0, limit, 1 do
  72.         bar = bar.."="
  73.     end
  74.     for i=limit, 10, 1 do
  75.         bar = bar.." "
  76.     end
  77.     bar = bar.."]"
  78.    
  79.     send(x, y, bar)
  80. end
  81.  
  82. function main()
  83.    
  84.     checkReactor()
  85.     checkMonitor()
  86.    
  87.     clearMonitor()
  88.    
  89.     local energy, percent_energy, capacity
  90.    
  91.     while true do
  92.        
  93.         energy = reactor.getEnergyStored()
  94.         percent_energy = energy/100000
  95.         capacity = round((100-percent_energy)*1000)/1000
  96.        
  97.         reactor.setAllControlRodLevels(percent_energy)
  98.         c_send(2, "Reactor Energy")
  99.         c_send(3, "--------------")
  100.         c_send(4, percent_energy.."%")
  101.         draw_bar("CENTER", 5, percent_energy)
  102.        
  103.         c_send(7, "Reactor Capacity")
  104.         c_send(8, "----------------")
  105.         c_send(9, capacity.."%")
  106.         draw_bar("CENTER", 10, capacity)
  107.        
  108.         os.sleep(1)
  109.     end
  110. end
  111.  
  112. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement