Guest User

gen2

a guest
Apr 25th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.95 KB | None | 0 0
  1. --btn = require("buttonAPI")
  2. local component = require("component")
  3. local keyboard = require("keyboard")
  4. local event = require("event")
  5. local gpu = component.gpu
  6. local sides = require("sides")
  7. local sysinfo = {}
  8.  
  9. local g = component.ie_diesel_generator
  10. local f = component.ie_fermenter
  11. local s = component.ie_squeezer
  12. local r = component.ie_refinery
  13. local red = component.redstone
  14.  
  15. local colours = {black = 0x000000, white = 0xFFFFFF, yellow = 0xFFFF55, blue = 0x0000AA, green = 0x00AA00, grey = 0xAAAAAA, red = 0xAA0000}
  16. local sections = {}
  17. local graphs = {}
  18. local info = {}
  19. local applications = {}
  20.  
  21. local currentDiesel = 0
  22. local currentFEthanol = 0
  23. local currentPotato = 0
  24. local currentSPlantOil = 0
  25. local currnetPumpkin = 0
  26. local currentREthanol=0
  27. local currentRPlantOil = 0
  28. local currentRDiesel = 0
  29.  
  30. gpu.setResolution(132,38)
  31. --gpu.setResolution(98,32)
  32. local w,h = gpu.getResolution() -- lol
  33.  
  34. -- define sections
  35. function setSections()
  36.   sections["tanks"] = { x = 5, y = 26, width = 123, height = 6, title = "[ Tanks ]"}
  37.   sections["status"] = { x = 88, y = 4, width = 40, height = 20, title = "[ Status ]"}
  38.   sections["details"] = { x = 5, y = 4, width = 78, height = 20, title = "[ Details ]"}
  39. end
  40. -- define graphs to display
  41. function setGraphs()
  42.   graphs["diesel"] = { x = 8, y = 29, width = 118, height = 2, title = "Diesel"}
  43. end
  44. -- define info to display
  45. function setInfo()
  46.   info["diesel"]      = { x = 8, y = 6, width = 73, height= 1, title  = "Generator Diesel: ", unit = " mB"}
  47.   info["fethanol"]    = { x = 8, y = 8, width = 73, height = 1, title = "Ethanol: ", unit = " mB"}
  48.   info["potato"]      = { x = 8, y = 10, width = 73, height= 1, title = "Potatoes: ", unit = " potatoes"}
  49.   info["splantoil"]   = { x = 8, y = 12, width = 73, height= 1, title = "Plant Oil: ", unit = " mB"}
  50.   info["pumpkin"]     = { x = 8, y = 14, width = 73, height= 1, title = "Pumpkins: ", unit = " pumpkins"}
  51.   info["rethanol"]    = { x = 8, y = 16, width = 73, height= 1, title = "Refinery Ethanol: ", unit = " mB"}
  52.   info["rplantoil"]   = { x = 8, y = 18, width = 73, height= 1, title = "Refinery Plant Oil: ", unit = " mB"}
  53.   info["rdiesel"]     = { x = 8, y = 20, width = 73, height= 1, title = "Refinery Diesel: ", unit = " mB"}
  54. end
  55. -- define applications
  56. function setApplications()
  57.   applications["main"] = {x = 3, y = 2, width = 128, height = 32, title = "[ Power Generation Control Panel ]"}
  58. end
  59.  
  60.  
  61. ------------------------ Button Stuff -------------------------------------------
  62. local btn = {}
  63. local button = {}
  64. function btn.clearTable()
  65.   button = {}
  66. end
  67.  
  68. function btn.setTable(name, func, xmin, ymin, xmax, ymax, text, colors, active) -- color is an object { on : 0x000000, off 0xAAAAAA}
  69.   button[name] = {}
  70.   button[name]["text"] = text
  71.   button[name]["func"] = func
  72.   button[name]["active"] = active
  73.   button[name]["xmin"] = xmin
  74.   button[name]["ymin"] = ymin
  75.   button[name]["xmax"] = xmax
  76.   button[name]["ymax"] = ymax
  77.   button[name]["colors"] = colors
  78. end
  79.  
  80. function btn.fill(bData)
  81.  
  82.   local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  83.   local xspot = math.floor((bData["xmin"] + bData["xmax"]) /2) - math.floor((string.len(bData["text"])/2))
  84.   local oldColor = gpu.getBackground()
  85.   local curColor = bData["colors"].on
  86.  
  87.   if bData["active"] then
  88.     curColor = bData["colors"].on
  89.   else
  90.     curColor = bData["colors"].off
  91.   end
  92.   gpu.setBackground(curColor)
  93.   gpu.fill(bData["xmin"], bData["ymin"], bData["xmax"] - bData["xmin"] + 1, bData["ymax"] - bData["ymin"] + 1, " ")
  94.   gpu.set(xspot, yspot, bData["text"])
  95.   gpu.setBackground(oldColor)
  96. end
  97.  
  98. function btn.screen()
  99.   for name,data in pairs(button) do
  100.      btn.fill(data)
  101.   end
  102. end
  103.  
  104. function btn.toggleButton(name)
  105.   button[name]["active"] = not button[name]["active"]
  106.   btn.screen() -- not sure about this one here
  107.   return button[name]["active"]
  108. end
  109.  
  110. function btn.flash(name)
  111.   _ = btn.toggleButton(name)
  112.   btn.screen() -- or here
  113.   os.sleep(0.1)
  114.   _ = btn.toggleButton(name)
  115.   btn.screen()
  116. end
  117.  
  118. function btn.checkxy(_, _, x, y, _, _)
  119.   os.sleep(0)
  120.   for name, data in pairs(button) do
  121.     if y >= data["ymin"] and y <= data["ymax"] then
  122.       if x >= data["xmin"] and x <= data["xmax"] then
  123.         data["func"]()
  124.         return true
  125.       end
  126.     end
  127.   end
  128.   return false
  129. end
  130.  
  131. ----------------------------------------------------------------------------------
  132.  
  133. function setButtons()
  134. --  btn.setTable("power", power, 91, 6, 106, 8, "power", {on = colours.green, off = colours.red},false)
  135. --  btn.setTable("test", testBtn, 108, 6, 123, 8, "test", {on = colours.green, off = colours.red},false)
  136.   --btn.setTable("OFF", powerOff, 109, 6, 125, 8,"OFF", {on = colours.red, off = colours.red})
  137.  
  138.   --btn.setTable("lowerMinLimit", lowerMinLimit, 91, 15, 106, 17,"-10", {on = colours.blue, off = colours.blue})
  139.   --btn.setTable("lowerMaxLimit", lowerMaxLimit, 109, 15, 125, 17,"-10", {on = colours.purple, off = colours.purple})
  140.  
  141.   --btn.setTable("augmentMinLimit", augmentMinLimit, 91, 19, 106, 21,"+10", {on = colours.blue, off = colours.blue})
  142.   --btn.setTable("augmentMaxLimit", augmentMaxLimit, 109, 19, 125, 21,"+10", {on = colours.purple, off = colours.purple})
  143. end
  144.  
  145. function printBG()
  146.   gpu.setBackground(colours.black)
  147.   gpu.fill(1,1,w,h," ")
  148.   gpu.setBackground(colours.grey)
  149.   gpu.fill(1,h,w,1," ")
  150.   gpu.setForeground(colours.white)
  151.   local bottomText = "Copyright (c) 1996 - 2009 New DPKR Tech. Inc. All Rights Reserved."
  152.   gpu.set(w/2 - string.len(bottomText)/2,h,bottomText)
  153. end
  154. -- application + borders
  155. function printApplication(appName)
  156.   local a = applications[appName]
  157.  
  158.   --gpu.setBackground(colours.black)
  159.   --gpu.fill(a.x-2,a.y-1,a.width-2,a.height-1, " ")
  160.   gpu.setBackground(colours.blue)
  161.   gpu.fill(a.x,a.y,a.width,a.height," ")
  162.  
  163.   -- fill borders
  164.   gpu.setBackground(colours.blue)
  165.   gpu.setForeground(colours.white)
  166.   gpu.fill(a.x, a.y, a.width, 1, "─")
  167.   gpu.fill(a.x, a.y, 1, a.height, "│")
  168.   gpu.fill(a.x, a.y + a.height, a.width, 1, "─")
  169.   gpu.fill(a.x + a.width, a.y, 1, a.height + 1, "│")
  170.    -- fill corners
  171.   gpu.set(a.x,a.y,"┌")
  172.   gpu.set(a.x + a.width,a.y,"┐")
  173.   gpu.set(a.x,a.y + a.height,"└")
  174.   gpu.set(a.x + a.width,a.y + a.height,"┘")
  175.   -- set title
  176.   gpu.setBackground(colours.blue)
  177.   gpu.setForeground(colours.white)
  178.   gpu.set(a.x + a.width/2 - string.len(a.title)/2, a.y, a.title)
  179.   gpu.setForeground(colours.white)
  180.  
  181. end
  182. -- section borders
  183. function printBorders(sectionName)
  184.   local s = sections[sectionName]
  185.   -- ─ │ ║ ═
  186.   -- ┐ └ ┴ ┬ ├ ─ ┼ │ ┤ ┘ ┌
  187.   -- set border
  188.   gpu.setBackground(colours.blue)
  189.   gpu.fill(s.x, s.y, s.width, 1, "─")
  190.   gpu.fill(s.x, s.y, 1, s.height, "│")
  191.   gpu.fill(s.x, s.y + s.height, s.width, 1, "─")
  192.   gpu.fill(s.x + s.width, s.y, 1, s.height + 1, "│")
  193.  
  194.   gpu.set(s.x,s.y,"┌")
  195.   gpu.set(s.x + s.width,s.y,"┐")
  196.   gpu.set(s.x,s.y + s.height,"└")
  197.   gpu.set(s.x + s.width,s.y + s.height,"┘")
  198.  
  199.   -- set title
  200.   gpu.setBackground(colours.blue)
  201.   gpu.setForeground(colours.white)
  202.   gpu.set(s.x + s.width/2 - string.len(s.title)/2, s.y, s.title)
  203. end
  204.  
  205. function power()
  206.   buttonStatus = btn.toggleButton("power",buttonStatus)
  207.   if buttonStatus == true then
  208.     print("On!")
  209.   else
  210.     print("Off!")
  211.   end
  212. end
  213.  
  214. function testBtn()
  215.   buttonStatus = btn.flash("test")
  216. end
  217.  
  218. function startup()
  219.  -- print("Enabling computer control...")
  220.   g.enableComputerControl(true)
  221.   f.enableComputerControl(true)
  222.   s.enableComputerControl(true)
  223.   r.enableComputerControl(true)
  224.   g.setEnabled(false)
  225.   f.setEnabled(false)
  226.   s.setEnabled(false)
  227.   r.setEnabled(false)
  228. end
  229.  
  230. function getSysInfo()
  231.   -- check how much diesel we have
  232.   dTank = g.getTankInfo()
  233.   -- reset input count
  234.   fInputSize = 0
  235.   sInputSize = 0
  236.   -- get size of each stack in fermenter and squeezer
  237.   for i = 1,6 do
  238.     fInputSize = fInputSize + f.getInputStack(i)["size"]
  239.     sInputSize = sInputSize + s.getInputStack(i)["size"]
  240.   end
  241.   -- fermenter and squeezer fluid output
  242.   fTank = f.getFluid()
  243.   sTank = s.getFluid()
  244.   -- refinery io
  245.   rInTank = r.getInputFluidTanks()
  246.   rOutTank = r.getOutputTank()
  247.  
  248.   sysinfo["diesel"]       = dTank["amount"]
  249.   sysinfo["dieselCap"]    = dTank["capacity"]
  250.   sysinfo["fethanol"]     = fTank["amount"]
  251.   sysinfo["fethanolCap"]  = fTank["capacity"]
  252.   sysinfo["potato"]       = fInputSize
  253.   sysinfo["potatoCap"]    = 384
  254.   sysinfo["splantoil"]    = sTank["amount"]
  255.   sysinfo["splantoilCap"] = sTank["capacity"]
  256.   sysinfo["pumpkin"]      = sInputSize
  257.   sysinfo["pumpkinCap"]   = 384
  258.   sysinfo["rethanol"]     = rInTank["input1"]["amount"]
  259.   sysinfo["rethanolCap"]  = rInTank["input1"]["capacity"]
  260.   sysinfo["rplantoil"]    = rInTank["input2"]["amount"]
  261.   sysinfo["rplantoilCap"] = rInTank["input2"]["capacity"]
  262.   sysinfo["rdiesel"]      = rOutTank["amount"]
  263.   sysinfo["rdieselCap"]   = rOutTank["capacity"]
  264. end
  265.  
  266. function logicCheck()
  267. --  print("checking logic")
  268.   -- if filled by >50% activate the squeezer/fermenter
  269.   if fTank["amount"]/fTank["capacity"] <= 0.5 then
  270.     f.setEnabled(true)
  271.   else
  272.     f.setEnabled(false)
  273.   end
  274.   -- if filled by >50% activate the squeezer/fermenter
  275.   if sTank["amount"]/sTank["capacity"] <= 0.5 then
  276.     s.setEnabled(true)
  277.   else
  278.     s.setEnabled(false)
  279.   end
  280.  
  281.   if rOutTank["amount"]/rOutTank["capacity"] < 0.5 then
  282.     r.setEnabled(true)
  283.     s.setEnabled(true)
  284.     f.setEnabled(true)
  285.   else
  286.     r.setEnabled(false)
  287.   end
  288.  
  289.   if red.getBundledInput(sides.left)[0] > 0 then
  290.   --if dTank["amount"]/dTank["capacity"] > 0.5 then
  291.     g.setEnabled(true)
  292.   else
  293.     g.setEnabled(false)
  294.   end
  295. end
  296.  
  297. -- progress bar bg
  298. function printGraphs(graphName)
  299.   local g = graphs[graphName]
  300.  
  301.   -- set graph
  302.   gpu.setBackground(colours.grey)
  303.   gpu.fill(g.x, g.y, g.width, g.height, " ")
  304.  
  305.   -- set title
  306.   gpu.setBackground(colours.blue)
  307.   gpu.setForeground(colours.yellow)
  308.   gpu.set(g.x, g.y - 1, g.title)
  309. end
  310. -- progressbar
  311. function printGraphBar(activeGraph)
  312.   local g = activeGraph
  313.  
  314.   -- set graph
  315.   gpu.setBackground(colours.green)
  316.   gpu.fill(g.x, g.y, g.width, g.height, " ")
  317.   --gpu.setBackground(colours.black)
  318. end
  319. -- info to display
  320. function printInfo(infoName)
  321.   local maxLength = 15
  322.   local i = info[infoName]
  323.   local spaces = string.rep(" ", maxLength - string.len(sysinfo[infoName] .. i.unit))
  324.   gpu.setForeground(colours.yellow)
  325.   gpu.setBackground(colours.blue)
  326.   gpu.set(i.x, i.y , i.title .. sysinfo[infoName] .. i.unit .. spaces)
  327. end
  328.  
  329. setApplications()
  330. setSections()
  331. setGraphs()
  332. setInfo()
  333. setButtons()
  334.  
  335. printBG()
  336. printApplication("main")
  337. printBorders("status")
  338. printBorders("details")
  339. printBorders("tanks")
  340.  
  341. printGraphs("diesel")
  342.  
  343. function draw()
  344.  
  345.   if currentDiesel ~= sysinfo["diesel"] then
  346.     currentDiesel = sysinfo["diesel"]
  347.     maxDiesel = sysinfo["dieselCap"]
  348.     local dieselFrac = math.ceil(graphs["diesel"].width * (currentDiesel/maxDiesel))
  349.     local dGraphUpdate = {x = graphs["diesel"].x, y = graphs["diesel"].y, width = dieselFrac, height = graphs["diesel"].height}
  350.  
  351.     printGraphs("diesel")
  352.     printGraphBar(dGraphUpdate)
  353.     printInfo("diesel")
  354.   end
  355.   if currentFEthanol ~= sysinfo["fethanol"] then
  356.     currentFEthanol = sysinfo["fethanol"]
  357.     printInfo("fethanol")
  358.   end
  359.   if currentPotato ~= sysinfo["potato"] then
  360.     currentPotato = sysinfo["potato"]
  361.     printInfo("potato")
  362.   end
  363.   if currentSPlantOil ~= sysinfo["splantoil"] then
  364.     currentSPlantOil = sysinfo["splantoil"]
  365.     printInfo("splantoil")
  366.   end
  367.   if currnetPumpkin ~= sysinfo["pumpkin"] then
  368.     currentPumpkin = sysinfo["pumpkin"]
  369.     printInfo("pumpkin")
  370.   end
  371.   if currentREthanol ~= sysinfo["rethanol"] then
  372.     currentREthanol = sysinfo["rethanol"]
  373.     printInfo("rethanol")
  374.   end
  375.   if currentRPlantOil ~= sysinfo["rplantoil"] then
  376.     currentRPlantOil = sysinfo["rplantoil"]
  377.     printInfo("rplantoil")
  378.   end
  379.   if currentRDiesel ~= sysinfo["rdiesel"] then
  380.     currentRDiesel = sysinfo["rdiesel"]
  381.     printInfo("rdiesel")
  382.   end
  383.   os.sleep(0)
  384. end
  385.  
  386. btn.screen()
  387. startup()
  388. while event.pull(0.1, "interrupted") == nil do
  389.   -- get info
  390.   event.listen("touch", btn.checkxy)
  391.   getSysInfo()
  392.   logicCheck()
  393.   draw()
  394.   os.sleep(0)
  395.   local event, address, arg1, arg2, arg3 = event.pull(1)
  396.   if type(address) == "string" and component.isPrimary(address) then
  397.     if event == "key_down" and arg2 == keyboard.keys.q then
  398.       os.exit()
  399.     end
  400.   end
  401. end
Advertisement
Add Comment
Please, Sign In to add comment