Advertisement
MrNaru300

Computercraft Big Reactors controller

Feb 22nd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.38 KB | None | 0 0
  1. --This program was created by MrNaru300
  2. --You can follow me in this link:
  3. --https://www.youtube.com/user/MrNaru300
  4.  
  5. --[[Hint: rename this file to "startup" to]]--
  6. --[[initialize when you reload the world  ]]--
  7.  
  8. --The reactor controller is a program
  9. --that control your reactor (normal or cooler)
  10. --with the the lowest waste of resources
  11.  
  12. --if you have a cooler reactor,
  13. --change the "coolermax" number
  14. --in the configs to get a better performance
  15.  
  16. local configs = {
  17.   lock = 0.75, --the maximum percentage of energy the reactor will produce
  18.   noGraph = false, --don't show the interface on the screen
  19.   waitTime = 1, --waiting time (in seconds)
  20.   coolerLock = 0.9, --the maximum percentage of steam the reactor will produce
  21.   coolerMax = 0.25, --The maximum percentage of "using" of the colling reactor
  22.   direction = nil --the reactor side (nil = automatic)
  23. }
  24. -----------------------------------
  25. function Start ()
  26.   --setting values
  27.   local directions = {"back", "bottom", "top", "left", "right", "front"}
  28.   --getting the reactor direction
  29.   if configs.direction == nil then
  30.     for _, direction in pairs(directions) do
  31.       if peripheral.getType(direction) == "BigReactors-Reactor" then
  32.         configs.direction = direction
  33.         break
  34.       end
  35.     end
  36.   end
  37.   if configs.direction == nil or peripheral.getType(configs.direction) ~= "BigReactors-Reactor" then
  38.     error("reactor not detected!")
  39.   end
  40.  
  41. end
  42. -----------------------------------
  43.  
  44. Start()
  45.  --setting other values
  46.     local Offline = "SYSTEM OFFLINE"
  47.     local reactor = peripheral.wrap(configs.direction)
  48.     local rodsPos = 0
  49.     local decimalPlaces = {"K", "Mi", "B", "T", "q", "Q", "s", "S", "O", "N", "D"}
  50.     local lock = configs.lock
  51.  
  52. -------------------------------------
  53. function reduce (number, places)
  54.   posFix = nil
  55.   i = 0
  56.   while number > 1000 and i < table.getn(decimalPlaces) do
  57.     i = i+1
  58.     posFix = decimalPlaces[i]
  59.     number = number/1000
  60.   end
  61.   if type(places) == "number" then
  62.     number = math.floor(number*math.pow(10, places))/math.pow(10, places)
  63.   end
  64.   if type(posFix) == "nil" then
  65.     return number
  66.   else
  67.     return number..posFix
  68.   end
  69. end
  70. -----------------------------------
  71. function CursorCenter(text, ln)
  72.   term.setCursorPos(x/2-string.len(text)/2, ln)
  73. end
  74. -----------------------------------
  75. function info (ps, col, name, i1, i2, type)
  76.   local WriteTXT = name..": "..reduce(i1, 1).." / "..reduce(i2, 0).." ("..(math.floor((i1)/i2*100)).."%)"
  77.   if type == 1 then WriteTXT = WriteTXT.." +"..reduce(reactor.getEnergyProducedLastTick(), 0) end
  78.   if type == 2 then WriteTXT = WriteTXT.." +"..reduce(reactor.getHotFluidProducedLastTick(), 0) end
  79.   CursorCenter(WriteTXT, ps)
  80.   term.setTextColor(col)
  81.   print(WriteTXT)
  82. end
  83. -----------------------------------
  84. function line (ln, i1, text, col, i2, col2)
  85.   term.setCursorPos(1, ln)
  86.   if type(i2) ~= "number" then i2 = 0 end
  87.   if col == nil then
  88.     col = colors.green
  89.     if i1 > .33 and i1 < .66 then col = colors.yellow end
  90.     if i1 < .33 then col = colors.red end
  91.   end
  92.   term.setBackgroundColor(col)
  93.   for i = 0, x*i1 do
  94.     term.setCursorPos(i, ln)
  95.     print(" ")
  96.   end
  97.   if i2 > 0 then
  98.     term.setBackgroundColor(col2)
  99.     for i = x*i1, x*i1+x*i2+1 do
  100.       term.setCursorPos(i, ln)
  101.       print(" ")
  102.     end
  103.   end
  104.   porc = (math.floor(i1*1000)/10).."%"
  105.   if text ~= nil then
  106.     porc = text..": "..porc
  107.   end
  108.   a = string.len(porc)
  109.   for i = 0, a do
  110.     if (x-a)/2+i <= x*i1 then
  111.       term.setBackgroundColor(col)
  112.     else if (x-a)/2+i <= x*i2 then
  113.       term.setBackgroundColor(col2)
  114.     else
  115.       term.setBackgroundColor(colors.black)
  116.     end
  117.     end
  118.     term.setTextColor(colors.white)
  119.     term.setCursorPos((x-a)/2+i, ln)
  120.     print(string.sub(porc, i+1, i+1))
  121.   end
  122.   term.setBackgroundColor(colors.black)
  123. end
  124. ------------------------------------
  125. function setRods()
  126.   if isCooler then
  127.     rodsPos = steam/MaxSteam/configs.coolerLock
  128.     if rodsPos >= 1 then
  129.         rods(100)
  130.     else if rodsPos <= 1-configs.coolerMax then
  131.         rods(100-configs.coolerMax*100)
  132.     else
  133.         rods(rodsPos*100)
  134.     end
  135.     end
  136.   else
  137.     rodsPos = energy/MaxEnergy/lock
  138.     if rodsPos >= 1 then
  139.         rods(100)
  140.     else
  141.         rods(rodsPos*100)
  142.     end
  143.   end
  144. end
  145. -----------------------------------
  146. local function offAlert()
  147.   if ColorOff == nil then ColorOff = colors.red end
  148.     if ColorOff == colors.black then
  149.       term.setBackgroundColor(colors.red)
  150.       CursorCenter(Offline, 8)
  151.       term.setTextColor(colors.black)
  152.       print(Offline)
  153.       term.setBackgroundColor(colors.black)
  154.       ColorOff = colors.red
  155.       sleep(configs.waitTime*.2)
  156.     else
  157.       term.setBackgroundColor(colors.black)
  158.       CursorCenter(Offline, 8)
  159.       term.setTextColor(colors.red)
  160.       print(Offline)
  161.       term.setBackgroundColor(colors.black)
  162.       ColorOff = colors.black
  163.       sleep(configs.waitTime*.2)
  164.     end
  165. end
  166.  
  167. -----------------------------------
  168. while true do
  169.   x, y = term.getSize()
  170.   energy = reactor.getEnergyStored()
  171.   MaxEnergy = 10000000
  172.  
  173.   fuel = reactor.getFuelAmount()
  174.   MaxFuel = reactor.getFuelAmountMax()
  175.   Activity = reactor.getActive()
  176.   rods = reactor.setAllControlRodLevels
  177.   isCooler = reactor.isActivelyCooled()
  178.   if isCooler then WriLock = "Lock: "..(lock*100).."%"
  179.   else WriLock = "Lock: "..(configs.coolerLock*100).."%" end
  180.   steam = reactor.getHotFluidAmount()
  181.   MaxSteam = reactor.getHotFluidAmountMax()
  182.   getRods = reactor.getControlRodLevel(1)
  183.   -----------------------------------
  184.   setRods()
  185.   -----------------------------------
  186.   if configs.noGraph == false then
  187.     term.clear()
  188.     term.setCursorPos(1,1)
  189.     term.setTextColor(colors.white)
  190.     term.setBackgroundColor(colors.black)
  191.     if isCooler == false then
  192.         info(1, colors.red, "RF", energy,  MaxEnergy, 1)
  193.         line(4, energy/MaxEnergy, "RF", colors.red)
  194.     else
  195.         info(1, colors.red, "Steam", steam, MaxSteam, 2)
  196.         line(4, steam/MaxSteam, "Steam", colors.red)
  197.     end
  198.     CursorCenter(WriLock, 2)
  199.     term.setTextColor(colors.gray)
  200.     print(WriLock)
  201.     term.setCursorPos(1, y-1)
  202.     term.setTextColor(colors.red)
  203.     print('Press "Ctrl + T" to stop the program')
  204.     if Activity then line(3, 1-getRods/100, "Using", colors.gray)
  205.     else line(3, 0, "Using", colors.gray)
  206.     end
  207.     info(5, colors.yellow, "Fuel", fuel, MaxFuel, 2, reactor.getWasteAmount())
  208.     line(6, fuel/MaxFuel, nil, colors.orange, reactor.getWasteAmount()/MaxFuel, colors.blue)
  209.     if Activity == false then
  210.         offAlert()
  211.     end
  212.   end
  213.     -----------------------------------
  214.   sleep(configs.waitTime)
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement