Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modemSide = "back"
- local bridgeSide = "bottom"
- local glasses = peripheral.wrap(bridgeSide)
- -- Bar Color, BackgroundColor
- local colors = {
- ["energy"] = {0x00FF00, 0x0000AA}
- }
- rednet.open(modemSide)
- glasses.clear()
- rednet.host("glassMonitor", "server")
- local gap = 3
- local mx = 100
- local my = 16
- local offsetY = 160
- local function outlineBox(xx,yy,ww,hh,color)
- if color == nil then
- color = 0x000000
- end
- local outlineTable = {}
- table.insert(outlineTable, glasses.addBox(xx-1,yy-1,ww+2,1,color,0.5))
- table.insert(outlineTable, glasses.addBox(xx-1,yy+hh,ww+2,1,color,0.5))
- table.insert(outlineTable, glasses.addBox(xx-1,yy,1,my,color,0.5))
- table.insert(outlineTable, glasses.addBox(xx+ww,yy,1,my,color,0.5))
- return outlineTable
- end
- local function hAlignText(halign,t)
- t.setObjectAnchor(halign,"top")
- end
- local function hAlignTexts(halign,textTable)
- for i,v in pairs(textTable) do
- v.setObjectAnchor(halign,"top")
- end
- end
- local function readableNumber(n)
- if n >= 10000000000 then return ""..(math.floor((n/1000000000)+0.5)).." G" end
- if n >= 1000000000 then return ""..(math.floor((n/100000000)+0.5)/10).." G" end
- if n >= 10000000 then return ""..(math.floor((n/1000000)+0.5)).." M" end
- if n >= 1000000 then return ""..(math.floor((n/100000)+0.5)/10).." M" end
- if n >= 10000 then return ""..(math.floor((n/1000)+0.5)).." k" end
- if n >= 1000 then return ""..(math.floor((n/100)+0.5)/10).." k" end
- if n >= 100 then return ""..math.floor((n)+0.5).." " end
- return ""..(math.floor((n*10)+0.5)/10).." "
- end
- local function splitString(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- local function fancytext(xx,yy,text,colour,alpha)
- if colour == nil then
- colour = 0xFFFFFF
- end
- if alpha == nil then
- alpha=1
- end
- local t1 = glasses.addText(xx,yy,text,colour)
- t1.setAlpha(alpha)
- t1.setZ(10)
- local t2 = glasses.addText(xx+1,yy+1,text,0x000000)
- t2.setAlpha(alpha/3)
- t2.setZ(5)
- return {t1,t2}
- end
- local function drawTitle()
- local bannerText = "Base Monitor"
- hAlignTexts("middle", fancytext((16+mx+gap*2)/2, offsetY+gap+4,bannerText,0xFFFFFF))
- outlineBox(gap,offsetY+gap,16+mx+gap,my)
- glasses.addGradientBox(gap,offsetY+gap,16+mx+gap,my,0x000000,0.25,0x000000,0.5,1)
- end
- local function percent(minVal, maxVal)
- return math.floor((minVal/maxVal)*100)
- end
- local function drawEnergy(currentEnergy, maxEnergy)
- local x = 1
- local i = 1
- local y = 17
- local text = readableNumber(currentEnergy).."/"..readableNumber(maxEnergy)
- hAlignTexts("middle", fancytext((16+mx+gap*2)/2, offsetY+gap+4+(my),text,0xFFFFFF))
- outlineBox(gap,offsetY+gap+(my), 16+mx+gap,my)
- glasses.addGradientBox(gap, offsetY+gap+(my), 16+mx+gap,my,0x000000,0.25,0x000000,0.5,1)
- local icon = glasses.addIcon(gap,offsetY+gap+my+1,"EnderIO:blockCapacitorBank",3)
- icon.setZ(5)
- local energyPercentage = percent(currentEnergy, maxEnergy)
- local line = glasses.addBox(gap, offsetY+gap+my, (16+gap)+energyPercentage, my, 0x00FF00,0.5)
- line.setZ(2)
- end
- local function chatCommand()
- end
- local function netEvent(sender, message, protocol)
- print("NET EVENT "..sender.." "..message.." "..protocol)
- if protocol == "energy" then
- local energySplit = splitString(message, "|")
- glasses.clear()
- drawTitle()
- drawEnergy(tonumber(energySplit[1]), tonumber(energySplit[2]))
- end
- end
- drawTitle()
- glasses.sync()
- while true do
- local event, p1, p2, p3, p4, p5 = os.pullEvent()
- if event=="modem_message" then
- netEvent(p1,p4["message"], p4["sProtocol"])
- elseif event=="glasses_chat_command" then
- chatCommand(p1,p2,p3,p4)
- end
- glasses.sync()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement