MoJoCreatior

Big Reactors Program

Feb 12th, 2016
1,595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. --[[  Big Reactor Monitor Program Written By: 0_Mr_Redstone_0  AKA  MoJoCreatior
  2. This code is considered free for use both commercial and private and can be redistributed so long as it meats following criteria:
  3. 1: 0_Mr_Redstone_0 and MoJoCreatior are credited as the original authors
  4. 2: You do not try to take full ownership of the code/written program
  5. 3: If you modify the code for re-upload you must provide it under the same conditions. as in- Open Source, and credit me as original author]]
  6.  
  7.  
  8. program = "Big Reactor Monitor Program V:1.3"
  9.  
  10. --Functions
  11.   function intro()
  12.     term.setBackgroundColour(colors.white)
  13.     term.clear()
  14.     term.setCursorPos(1,1)
  15.     term.setTextColour(colors.green)
  16.     print(program.." -Written By: 0_Mr_Redstone_0  AKA  MoJoCreatior")
  17.     print(" ")
  18.   end
  19.  
  20.   function centerText(text,line)
  21.     length = string.len(text)
  22.     minus = math.floor(monX-length)
  23.     x = math.floor(minus/2)
  24.     mon.setCursorPos(x+1,line)
  25.     mon.write(text)
  26.   end
  27.  
  28.   function centerTextC(text,line,colorB,colorT)
  29.     mon.setBackgroundColour(colorB)
  30.     mon.setTextColour(colorT)
  31.     length = string.len(text)
  32.     minus = math.floor(monX-length)
  33.     x = math.floor(minus/2)
  34.     mon.setCursorPos(x+1,line)
  35.     mon.write(text)
  36.   end
  37.  
  38.   function clearScreen()
  39.     mon.setBackgroundColour(colors.white)
  40.     mon.clear()
  41.   end
  42.  
  43.   function textScale()
  44.     mon.setTextScale(0.5)
  45.     monX,monY = mon.getSize()
  46.     if monX <= 16 or monY <= 11 then mon.setTextScale(0.5) elseif
  47.       monX <= 37 or monY <= 25 then mon.setTextScale(1) elseif
  48.       monX <= 58 or monY <= 39 then mon.setTextScale(1.5) elseif
  49.       monX <= 80 or monY <= 53 then mon.setTextScale(2) elseif
  50.       monX <= 101 or monY <= 68 then mon.setTextScale(2.5) else
  51.       mon.setTextScale(3)
  52.     end  
  53.   end
  54.  
  55.   function config(name)
  56.     cfg = fs.open("config/"..name,"r")
  57.     name = cfg.readAll()
  58.     cfg.close()
  59.     return name
  60.   end
  61.  
  62.   function statBar(fillColor,emptyColor,line,percent,pColor)
  63.     monX,monY = mon.getSize()
  64.     barEmpty = monX-5
  65.     if percent == 100 then length = 2 elseif percent ==  0 then length = 4 else length = 3 end
  66.     barFill = percent/100
  67.     barFill = math.floor((barFill*barEmpty)+0.5)
  68.     mon.setCursorPos(2,line)
  69.     mon.setBackgroundColour(emptyColor)
  70.     mon.write(string.rep(" ",barEmpty))
  71.     mon.setCursorPos(2,line)
  72.     mon.setBackgroundColour(fillColor)
  73.     mon.write(string.rep(" ",barFill))
  74.     mon.setCursorPos(barEmpty+length,line)
  75.     mon.setBackgroundColour(colors.white)
  76.     mon.setTextColour(pColor)
  77.     mon.write(percent)
  78.     mon.setCursorPos(monX,line)
  79.     mon.write("%")
  80.   end
  81.  
  82.   function percent(current,total)
  83.     return math.floor((current/total*100)+0.5)
  84.   end
  85.  
  86.   function header(colorB,colorT)
  87.     clearScreen()
  88.     mon.setBackgroundColour(colorB)
  89.     mon.setTextColour(colorT)
  90.     mon.setCursorPos(1,1)
  91.     if string.len(title)+2 <= monX-3 then
  92.       centerText(string.rep(" ",string.len(title)+2),1) else
  93.       mon.clearLine() end
  94.     centerText(title,1)
  95.   end
  96.  
  97.  
  98. --Loads Config Files
  99.   title = config("title")
  100.   rate = config("rate")
  101.   dynamic = config("dynamic")
  102.  
  103. --Shows Settings inside control computer
  104.   intro()
  105.   term.setTextColour(colors.red)
  106.   print("Final Settings\n")
  107.   term.setTextColour(colors.blue)
  108.   print("Title: "..title)
  109.   if rate == "s" then print("Update Rate: slow") else print("Update Rate: fast") end
  110.   if dynamic == "y" then print("Dynamic Scaling: enabled") else print("Dynamic Scaling: disabled") end
  111.   term.setTextColour(colors.red)
  112.   print("\nIf settings are incorrect. Run setup again")
  113.   term.setTextColour(colors.blue)
  114.   print("Hold CTRL+T to stop program")
  115.  
  116. --Wraps the Reactor and monitor as peripherals
  117.   mon = peripheral.find("monitor")
  118.   reactor = peripheral.find("BigReactors-Reactor")
  119.  
  120. --Text Scaling
  121.   textScale()
  122.  
  123. while true do
  124. --Main Program Loop
  125.   --Dynamic Scaling Conditional
  126.     if dynamic == "y" then textScale() end
  127.    
  128.   --Basic Variables & Calculations
  129.     monX,monY = mon.getSize()
  130.     controlRod = reactor.getControlRodLevel(0)
  131.     reactorRF = reactor.getEnergyStored()
  132.     percentRF = percent(reactorRF,10000000)
  133.     reactor.setAllControlRodLevels((percentRF*2)-50)
  134.     RFt = math.floor(reactor.getEnergyProducedLastTick()+.05)
  135.     RFs = reactor.getEnergyStored()
  136.     mbC = reactor.getFuelAmount() or 1
  137.     mbM = reactor.getFuelAmountMax() or 1
  138.     fuelPercent = percent(mbC,mbM)
  139.     mbT = (math.floor((reactor.getFuelConsumedLastTick()*1000)+0.05))/1000
  140.     if percentRF <= 40 then reactor.setAllControlRodLevels(0) end
  141.     if percentRF == 100 then reactor.setAllControlRodLevels(100) end
  142.    
  143.   --Render Code
  144.     header(colors.black,colors.white)
  145.     centerTextC(RFt..":RF/t",monY-4,colors.white,colors.green)
  146.     centerTextC(RFs..":RF",monY-3,colors.white,colors.red)
  147.     centerTextC(mbT..":mb/t",3,colors.white,colors.purple)
  148.     statBar(colors.yellow,colors.lightGray,4,fuelPercent,colors.purple)
  149.     statBar(colors.gray,colors.lightGray,monY-1,controlRod,colors.gray)
  150.     statBar(colors.red,colors.lightGray,monY-2,percentRF,colors.red)
  151.    
  152.   --Update Rate
  153.     if rate == "f"
  154.       then os.sleep(1)
  155.     elseif rate == "s"
  156.       then os.sleep(5)
  157.     else os.sleep(1)
  158.     end
  159. end
Advertisement
Add Comment
Please, Sign In to add comment