MiStAWaFFlEZZ

genv3

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