Advertisement
Guest User

reactor

a guest
Sep 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.92 KB | None | 0 0
  1. component = require("component")
  2. term = require("term")
  3. gpu = component.gpu
  4. event = require("event")
  5. sides = require("sides")
  6. mfsu = component.ic2_te_mfsu
  7. redstone = component.proxy(component.get("687c5bc1-1a41-4930-9a1c-a7d1af612a5b"))
  8. reactor = component.proxy(component.get("e4b738c2-b758-495f-899e-39e6114ff2b3"))
  9.  
  10. colors = { black = 0x000000, white = 0xf8f8ff, blue = 0x0000ff, lightGray = 0xd9d9d9, red = 0xff0000,
  11. purple = 0x9b30ff, carrot = 0xffa500, magenta = 0xcd00cd, lightBlue = 0x87cefa, yellow = 0xffff00,
  12. lime = 0x32cd32, pink = 0xffc0cb, gray = 0x696969, brown = 0x8b4500, green = 0x006400, cyan = 0x008b8b,
  13. olive = 0x6b8e23, gold = 0x8b6914, orangered = 0xdb4e02, diamond = 0x0fa7c7,crimson = 0xaf002a,fuchsia = 0xfd3f92,
  14. folly = 0xff004f, frenchBlue = 0x0072bb, lilac = 0x86608e, flax = 0xeedc82, darkGray = 0x563c5c,
  15. englishGreen = 0x1b4d3e, eggplant = 0x614051, deepPink  = 0xff1493, ruby = 0x843f5b, orange = 0xf5c71a,
  16. lemon = 0xffd300, darkBlue = 0x002e63, bitterLime = 0xbfff00 }
  17. ------------------------------------------------------------------------------------------------------------------
  18.                                                  --[[ VARIABLES ]]--
  19. ------------------------------------------------------------------------------------------------------------------
  20. Border_bg = colors.diamond
  21. Default_bg = colors.gray
  22. text_col = colors.blue
  23. status_col = colors.black
  24. barBack = colors.black
  25. barFill = colors.white
  26. header = colors.orange
  27. Side = sides.east
  28. -----------------------------------------------------------------------------------------------------------------
  29.                                                  --[[ FUNCTIONS ]]--
  30. ------------------------------------------------------------------------------------------------------------------
  31. function guiBorders(x,y,len,height,str) -- BORDER FUNC FOR GUI
  32.   gpu.setBackground(Border_bg)
  33.   gpu.fill(x,y,len,height,str)
  34.   gpu.setBackground(Default_bg)
  35. end
  36.  
  37. function dot(n)
  38.     local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
  39.     return left..(num:reverse():gsub('(%d%d%d)','%1.'):reverse())..right
  40. end
  41.  
  42. function GUI() -- SETS THE GUI LAYOUT (GRAPHICAL USER INTERFACE)
  43.   gpu.setBackground(Default_bg)
  44.   term.clear()
  45.   w,  h = gpu.getResolution()
  46.   guiBorders(1,1,w,1," ")
  47.   guiBorders(1,5,w,1," ")
  48.     for i = 1,h do
  49.       guiBorders(1,i,1,1," ")
  50.       guiBorders(w,i,1,1," ")
  51.     end
  52.     for i = 5,h do
  53.       guiBorders(7,i,1,5," ")
  54.     end
  55.   for i = 5,h do
  56.     guiBorders(w-6, i,1,5," ")
  57.   end
  58.   guiBorders(1,h,w,1," ")
  59.   gpu.setForeground(header)
  60.   Center(3,"--[[ Nuclear Reactor Control ]]--")
  61. end
  62.  
  63. function key()
  64.  -- KEYS --
  65.   gpu.set(25, 14, "lol")
  66.   gpu.setBackground(colors.red)
  67.   gpu.fill(16,14,8,3," ")
  68.  
  69.   gpu.setBackground(Default_bg)
  70. end
  71.  
  72. function drawBarPos(xOff,yOff,w,h,percentage,text)
  73.   barLen = math.floor(percentage*(h-2) +0.5)
  74.   gpu.setForeground(barBack)
  75.   gpu.fill(xOff,yOff,w,h,"█")
  76.   gpu.setForeground(barFill)
  77.   gpu.fill(xOff+1,yOff+1,w-2,h-2,"█")
  78.  
  79.   if percentage >= 0.75 then
  80.     gpu.setForeground(colors.green)
  81.   elseif percentage >= 0.5 then
  82.     gpu.setForeground(colors.yellow)
  83.   elseif percentage >= 0.25 then
  84.     gpu.setForeground(colors.orange)
  85.   elseif percentage >= 0 then
  86.     gpu.setForeground(colors.red)
  87.   end
  88.  
  89.   gpu.fill(xOff+1,(yOff+h-1)-barLen,w-2,barLen,"█")
  90.  
  91.   gpu.setForeground(colors.white)
  92.   gpu.set(xOff,h+6,"%"..math.floor(percentage*100).." ")
  93.   gpu.set(xOff, h+7, text)
  94. end
  95.  
  96. function drawBarNeg(xOff,yOff,w,h,percentage, text)
  97.   barLen = math.floor(percentage*(h-2) +0.5)
  98.   gpu.setForeground(barBack)
  99.   gpu.fill(xOff,yOff,w,h,"█")
  100.   gpu.setForeground(barFill)
  101.   gpu.fill(xOff+1,yOff+1,w-2,h-2,"█")
  102.  
  103.   if percentage >= 0.75 then
  104.     gpu.setForeground(colors.red)
  105.   elseif percentage >= 0.5 then
  106.     gpu.setForeground(colors.orange)
  107.   elseif percentage >= 0.25 then
  108.     gpu.setForeground(colors.yellow)
  109.   elseif percentage >= 0 then
  110.     gpu.setForeground(colors.green)
  111.   end
  112.  
  113.   gpu.fill(xOff+1,(yOff+h-1)-barLen,w-2,barLen,"█")
  114.  
  115.   gpu.setForeground(colors.white)
  116.   gpu.set(xOff+1,h+6,"%"..math.floor(percentage*100).." ")
  117.   gpu.set(xOff, h+7, text)
  118. end
  119.  
  120. function Center(y, text)
  121.   w, h = gpu.getResolution()
  122.   term.setCursor((w-string.len(text))/2+1, y)
  123.   term.write(text)
  124. end
  125.  
  126. function InfoUpdate(y, text) -- Text for function UPDATE
  127.   w, h = gpu.getResolution()
  128.       place = (w-string.len(text))-7
  129.     gpu.set(place, y, text)
  130. end
  131.  
  132. function Display()
  133. w, h = gpu.getResolution()
  134.   gpu.set(9,6,"Reactor Status:")
  135.   InfoUpdate(6,"   "..tostring(stat))
  136.   gpu.set(9,8, "Heat: ")
  137.   InfoUpdate(8, tostring(heat).." C")
  138.   gpu.set(9,10, "EU/T: ")
  139.   InfoUpdate(10, "   "..tostring(out))
  140.   gpu.set(9,12, "Stored EU: ")
  141.   InfoUpdate(12,"    ".. dot(tostring(math.floor(stored))).."/"..dot(tostring(math.floor(mfsu.getCapacity()))))
  142. end
  143.  
  144. function addButton(x, y, col,text)
  145.   leng = string.len(text)
  146.   gpu.setBackground(col)
  147.   gpu.fill(x, y, (leng + 2), 3," ")
  148.   gpu.setForeground(status_col)
  149.   gpu.set(x+1, y+1, text)
  150.   gpu.setBackground(Default_bg)
  151. end
  152.  
  153. function fail()
  154.   if heatPerc*100 >= 20 then
  155.     redstone.setOutput(Side, 0)
  156.   end
  157. end
  158.  
  159.  
  160. gpu.setResolution(60,20)
  161. GUI()
  162. --key()
  163. addButton(19,16, colors.red,"Offline")
  164. addButton(34,16, colors.lime,"Online")
  165.  
  166. function getP()
  167.     if x~= nil and y ~= nil then
  168.       if x >= 19 and x<=25 and y >= 16 and y <= 18 then
  169.         redstone.setOutput(Side, 0)
  170.       elseif x >= 34 and x <= 42 and y >= 16 and y <= 18 then
  171.         redstone.setOutput(Side, 15)
  172.       end
  173.     end
  174. end
  175.  
  176. while true do
  177.   -- Update
  178.   if reactor.producesEnergy() then
  179.     stat = "Active"
  180.   else
  181.     stat = "Offline"
  182.   end
  183.   --Updates
  184.   stored = mfsu.getEnergy()
  185.   heat = reactor.getHeat()
  186.   out = reactor.getReactorEUOutput()
  187.   EOut = reactor. getReactorEnergyOutput()
  188.   -- Percs
  189.   heatPerc = reactor.getHeat() / reactor.getMaxHeat()
  190.   mfsuPerc = mfsu.getEnergy() / mfsu.getCapacity()
  191.   -- Bars
  192.   drawBarNeg(2,6,5,12, heatPerc, "Heat")
  193.   drawBarPos(55,6,5,12, mfsuPerc,"Mfsu")
  194.   -- Functions
  195.   Display()
  196.   fail()
  197.   _,_,x,y = event.pull(1, "touch")
  198.   getP()
  199. --os.sleep(.2)
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement