Daniel_I_Am

BaseControl_02.lua

Mar 6th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.91 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. computer = require("computer")
  7. os = require("os")
  8. draco = component.draconic_rf_storage
  9. screen = component.screen
  10. keyboard = require("keyboard")
  11. shell = require("shell")
  12. BA = require("ButtonAPI")
  13.  
  14. ---------- TABLES -----------
  15. colors = { black = 0x000000, white = 0xf8f8ff, blue = 0x0000ff, lightGray = 0xd9d9d9, red = 0xff0000,
  16. purple = 0x9b30ff, carrot = 0xffa500, magenta = 0xcd00cd, lightBlue = 0x87cefa, yellow = 0xffff00,
  17. lime = 0x32cd32, pink = 0xffc0cb, gray = 0x696969, brown = 0x8b4500, green = 0x006400, cyan = 0x008b8b,
  18. olive = 0x6b8e23, gold = 0x8b6914, orangered = 0xdb4e02, diamond = 0x0fa7c7,crimson = 0xaf002a,fuchsia = 0xfd3f92,
  19. folly = 0xff004f, frenchBlue = 0x0072bb, lilac = 0x86608e, flax = 0xeedc82, darkGray = 0x563c5c,
  20. englishGreen = 0x1b4d3e, eggplant = 0x614051, deepPink  = 0xff1493, ruby = 0x843f5b, orange = 0xf5c71a,
  21. lemon = 0xffd300, darkBlue = 0x002e63, bitterLime = 0xbfff00 } --doesn't this look very fucking fancy :3
  22.  
  23. ------- VARIABLES -------------
  24.  
  25. Border_bg = colors.diamond
  26. Default_bg = colors.gray
  27. text_col = colors.blue
  28. status_col = colors.black
  29. barBack = colors.black
  30. barFill = colors.white
  31. header = colors.orange
  32.  
  33. buttonXOff = 10
  34. buttonYOff = 10
  35. buttonWidth = 21
  36. buttonHeight = 3
  37. buttonSpacingX = 5
  38. buttonSpacingY = 3
  39. buttonColorButtonOn = colors.lime
  40. buttonColorButtonOff = colors.red
  41. buttonColorText = colors.black
  42. buttonsColumn = 3
  43.  
  44. listOfComponents = {
  45. --{id, displayName, redstoneOutputSide, isInvertedSignal},
  46.   {"d9eb04c1-7556-4a56-a8c5-ee5cf1116d1a", "Void Miner", sides.north, true},
  47.   {"d9ecdcd0-b816-4da2-a6ad-3428e60955ea", "Applied Energistics", sides.north, false},
  48.   {"d9eb04c1-7556-4a56-a8c5-ee5cf1116d1a", "Resource Miner", sides.east, true}
  49. }
  50.  
  51. oddList = {}
  52. evenList = {}
  53. status = {}
  54.  
  55. -------- FUNCTIONS --------------
  56.  
  57. args, options = shell.parse(...)
  58.  
  59. function init()
  60.   for i = 1, #listOfComponents do
  61.     local canPass = true
  62.     if #listOfComponents[i] < 4 then
  63.       canPass = false
  64.       io.stderr:write("Component array number " .. i .. " is missing arguments, 4 expected, received only " .. #listOfComponents[i] .. ".")
  65.     end
  66.     if not canPass then os.exit() end
  67.   end
  68.   w, h = gpu.getResolution()
  69.   for i = 1, #listOfComponents do
  70.     table.insert(oddList, i * 2 - 1)
  71.     table.insert(evenList, i * 2)
  72.     table.insert(status, true)
  73.   end
  74.   createButtons(listOfComponents)
  75.   if options.c then
  76.     os.execute("/home/ClearLog.lua")
  77.   end
  78.   fileWrite("/etc/output.log", getTime() .. ": " .. "Base Control initialized", false)
  79.   screen.setTouchModeInverted(true)
  80. end
  81.  
  82. function shutdown()
  83.   shouldRep = true
  84.   gpu.setForeground(0xffffff)
  85.   gpu.setBackground(0x000000)
  86.   gpu.setResolution(gpu.maxResolution())
  87.   term.clear()
  88.   screen.setTouchModeInverted(false)
  89.   print("You've terminated Base Control")
  90.   _, _, _, _, player = event.pull("key_up")
  91.   fileWrite("/etc/output.log", getTime() .. ": " .. player .." terminated Base Control", false)
  92. end
  93.  
  94. function getTime()
  95.   internet = require("component").internet
  96.   page = internet.request("http://worldclockapi.com/api/jsonp/cet/now?callback=mycallback")
  97.   repeat
  98.   file = page.read()
  99.   until file ~= ""
  100.   a,b = file:find("%d+%-%d+%-%d..%d+:%d+")
  101.   c = file:sub(a, b):find("T")
  102.   time = "[" .. file:sub(a, a + c - 2) .. " " .. file:sub(a + c, b) .. "]"
  103.   return time
  104. end
  105.  
  106. function guiBorders(x,y,len,height,str) -- BORDER FUNC FOR GUI
  107.   gpu.setBackground(Border_bg)
  108.   gpu.fill(x,y,len,height,str)
  109.   gpu.setBackground(Default_bg)
  110. end
  111.  
  112. function fileWrite(fileLocation, str, isOverwrite)
  113.   if isOverwrite==true then mode = "w" else mode = "a" end
  114.   f = io.open(tostring(fileLocation),mode)
  115.   f:write(tostring(str).."\n")
  116.   f:close()
  117. end
  118.  
  119. function GUI() -- SETS THE GUI LAYOUT (GRAPHICAL USER INTERFACE)
  120.   gpu.setBackground(Default_bg)
  121.   term.clear()
  122.   w,  h = gpu.getResolution()
  123.   guiBorders(1,1,w,1," ")
  124.   guiBorders(1,5,w,1," ")
  125.     for i = 1,h do
  126.       guiBorders(1,i,1,1," ")
  127.       guiBorders(w,i,1,1," ")
  128.     end
  129.     for i = 5,h do
  130.       --guiBorders(7,i,1,5," ")
  131.     end
  132.   for i = 5,h do
  133.     --guiBorders(w-6, i,1,5," ")
  134.   end
  135.   guiBorders(1,h,w,1," ")
  136.   gpu.setForeground(header)
  137.   Center(3,"--[[ Base Control ]]--")
  138. end
  139.  
  140. function comma_value(n) -- credit http://richard.warburton.it
  141.   local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
  142.   return left..(num:reverse():gsub('(%d%d%d)','%1.'):reverse())..right
  143. end
  144.  
  145. function centerText(str, x1, x2, y, FGCol, BGCol)
  146.     if not component.isAvailable("gpu") then return end
  147.     local w,_ = gpu.getResolution()
  148.     x1 = x1 or 1
  149.     x2 = x2 or w
  150.     y = y or 1
  151.     str = str or "text"
  152.     FGCol = FGCol or gpu.getForeground()
  153.     BGCol = BGCol or gpu.getBackground()
  154.     local prevFG = gpu.getForeground()
  155.     local prevBG = gpu.getBackground()
  156.     local l = string.len(tostring(str))
  157.     local center = math.floor((x1+x2)/2)
  158.     local startPos = math.ceil(center - (l-1)/2)
  159.     gpu.setForeground(FGCol)
  160.     gpu.setBackground(BGCol)
  161.     gpu.set(startPos,y,str)
  162.     gpu.setForeground(prevFG)
  163.     gpu.setBackground(prevBG)
  164. end
  165.  
  166. function Center(y, text)
  167.   w, h = gpu.getResolution()
  168.   term.setCursor((w-string.len(text))/2+1, y)
  169.   term.write(text)
  170. end
  171.  
  172. function createButtons(list)
  173.   BA.clear()
  174.   for i,v in ipairs(list) do
  175.     BA.makeButton(buttonXOff + math.floor((i-1)/3) * (buttonWidth + buttonSpacingX), buttonYOff + ((i - 1) % buttonsColumn) * (buttonHeight + buttonSpacingY), buttonWidth, buttonHeight, buttonColorButtonOn, buttonColorText, -1, -1, list[i][2])
  176.     BA.makeButton(buttonXOff + math.floor((i-1)/3) * (buttonWidth + buttonSpacingX), buttonYOff + ((i - 1) % buttonsColumn) * (buttonHeight + buttonSpacingY), buttonWidth, buttonHeight, buttonColorButtonOff, buttonColorText, -1, -1, list[i][2])
  177.   end
  178. end
  179.  
  180. function displayPower()
  181.   power = draco.getEnergyStored()
  182.   maxPower = draco.getMaxEnergyStored()
  183.   transfer = draco.getTransferPerTick()
  184.   powerPCT = math.floor(1000 * power/maxPower + 5)/10
  185.   if transfer < 0 then isNegTransfer = true else isNegTransfer = false end
  186.   if isNegTransfer then transferColor = colors.red else transferColor = colors.lime end
  187.   off = 90
  188.   gpu.set(off, 6, "Power:")  
  189.   gpu.set(off, 7, "    " .. comma_value(tostring(power)) .. " / " .. comma_value(tostring(maxPower)) .. " RF (" .. powerPCT .. "%)    ")
  190.   gpu.set(off, 8, "Usage:")
  191.   temp = gpu.getForeground()
  192.   gpu.setForeground(transferColor)
  193.   gpu.set(off, 9, "    " .. comma_value(tostring(transfer)) .. " RF/t    ")
  194.   gpu.setForeground(temp)
  195. end
  196.  
  197. gpu.setResolution(150,30)
  198. init()
  199. GUI()
  200.  
  201. repeat
  202.   for i = 1, #listOfComponents do
  203.     if status[i] then j = 0 else j = 1 end
  204.     BA.draw({2*i + j - 1})
  205.   end
  206.   startTime = computer.uptime()
  207.    
  208.     --execute main code
  209.     local e,id,x,y,b,p = event.pull(1,"touch")
  210.     if e ~= nil then
  211.       --if we pushed a button
  212.       buttonsPressed = BA.updateAll(e,id,x,y,b,p)
  213.       buttonsPressed = {buttonsPressed[1], buttonsPressed[2]}
  214.       if buttonsPressed[1] ~= nil then
  215.         fileWrite("/etc/output.log", getTime() .. ": " .. p .. " toggled " .. listOfComponents[buttonsPressed[2]/2][2])
  216.         if status[buttonsPressed[2]/2] then
  217.           buttonPressed = buttonsPressed[1]
  218.         else
  219.           buttonPressed = buttonsPressed[2]
  220.         end
  221.         status[buttonsPressed[2]/2] = not status[buttonsPressed[2]/2]
  222.         if status[buttonsPressed[2]/2] then
  223.           out = 15
  224.         else
  225.           out = 0
  226.         end
  227.         if listOfComponents[buttonsPressed[2]/2][4] then out = 15 - out end
  228.         component.proxy(listOfComponents[buttonsPressed[2]/2][1]).setOutput(listOfComponents[buttonsPressed[2]/2][3], out)
  229.       end
  230.     end
  231.   displayPower()
  232.   repeat
  233.     os.sleep(0.05)
  234.   until computer.uptime() >= startTime + 0.5
  235.   shouldRep = not keyboard.isControlDown()
  236. until shouldRep == false
  237. shutdown()
Advertisement
Add Comment
Please, Sign In to add comment