Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.redirect(peripheral.wrap("right"))
- local mTank = peripheral.wrap("back") -- Tank Modem
- local pGlasses = peripheral.wrap("top") -- Bridge
- local updateFreq = 1 -- How often do we update?
- -- Glasses
- local anchorMain = { ["x"] = 10, ["y"] = 50 }
- local glassesBarSize = { ["x"] = 60, ["y"] = 10 }
- local g_bars = {}
- local g_texts = {}
- local bar_y = 0
- -- Tanks
- local tanksPredefData = { [2] = { ["name"] = "Lava", ["color"] = 0xFF0000 } }
- local tanksData = {}
- local tanksEmpty = 0
- function registerRemotePeripherals( modem )
- local remote = modem.getNamesRemote()
- local peripherals = {}
- for k,v in pairs( remote ) do
- peripherals[ k ] = peripheral.wrap( tostring( v ) )
- end
- return peripherals
- end
- local pTanks = registerRemotePeripherals( mTank )
- function getTank( id )
- return pTanks[ id ].getTankInfo( "unknown" )[1]
- end
- function performUpdate()
- -- Update tank values
- tanksEmpty = 0
- for k = 1, #pTanks do
- local tank = getTank( k )
- if( tank.rawName ~= nil ) then
- tanksData[ tank.id ] = { ["amount"] = tank.amount, ["capacity"] = tank.capacity }
- else
- tanksEmpty = tanksEmpty + 1
- end
- end
- end
- function getRoundedPercent( num )
- return math.floor( num * 100 )
- end
- function barExist( id )
- if( g_bars[ id ] ~= nil ) then
- return true
- end
- return false
- end
- function changeBar( id, x, y, percent, color )
- if( barExist( id ) == false ) then
- return
- end
- local d = g_bars[ id ]
- if( color == nil ) then
- color = 0x99FF00
- end
- if( x == nil ) then
- x = d[ "border" ].getX()
- end
- if( y == nil ) then
- y = d[ "border" ].getY()
- end
- d[ "border" ].setX( x )
- d[ "border" ].setY( y )
- d[ "borderbg" ].setX( x + 1 )
- d[ "borderbg" ].setY( y + 1 )
- d[ "bar" ].setX( x + 3 )
- d[ "bar" ].setY( y + 3 )
- local w = d[ "border" ].getWidth()
- local dx = math.ceil( ( w - 6 ) * ( percent / 100 ) )
- d["bar"].setWidth( dx )
- return d
- end
- function createBarAt( id, x, y, percent, color )
- if( color == nil ) then
- color = 0x99FF00
- end
- local d = {}
- local w = glassesBarSize["x"]
- local h = glassesBarSize["y"]
- local dx = math.ceil( ( w - 6 ) * ( percent / 100 ) )
- d["border"] = pGlasses.addBox( x, y, w, h, 0xFFFFFF, 1 )
- d["borderbg"] = pGlasses.addBox( x + 1, y + 1, w - 2, h - 2, 0x000000, 1 )
- d["bar"] = pGlasses.addGradientBox( x + 3, y + 3, dx, h - 6, color, 0.05, 0xFFFFFF, 1, 2 )
- d["border"].setZ( 1 )
- d["borderbg"].setZ( 2 )
- d["bar"].setZ( 3 )
- g_bars[ id ] = d
- return g_bars[ id ]
- end
- function getBar( id, x, y, percent, color )
- if( barExist( id ) ) then
- return changeBar( id, x, y, percent, color )
- end
- return createBarAt( id, x, y, percent, color )
- end
- function textExist( id )
- if( g_texts[ id ] ~= nil ) then
- return true
- end
- return false
- end
- function changeText( id, x, y, text, color )
- if( textExist( id ) == false ) then
- return nil
- end
- if( color == nil ) then
- color = 0xFFFFFF
- end
- local d = g_texts[ id ]
- d.setX( x )
- d.setY( y )
- d.setText( text )
- d.setColor( color )
- return d
- end
- function createTextAt( id, x, y, text, color )
- if( textExist( id ) ) then
- return nil
- end
- if( color == nil ) then
- color = 0xFFFFFF
- end
- local d = pGlasses.addText( x, y, text, color )
- g_texts[ id ] = d
- return g_texts[ id ]
- end
- function getText( id, x, y, text, color )
- if( textExist( id ) ) then
- return changeText( id, x, y, text, color )
- end
- return createTextAt( id, x, y, text, color )
- end
- function handleRow( tid, part, total, color, text )
- local percent = getRoundedPercent( part / total )
- local c = 0xFFFFFF
- -- Bytes
- b = getBar( tid, anchorMain["x"] + 50, bar_y, percent, color )
- if( percent < 11 ) then
- c = 0xFF0000
- end
- x = b[ "border" ].getX() + b[ "border" ].getWidth() + 5
- getText( tid .. "_pre", anchorMain["x"], bar_y + 1, text, 0xFFFFFF )
- getText( tid .. "_post", x, bar_y + 1, percent .. "%", c )
- print( text .. " - " .. percent .. "%" .. "[ " .. part .. " / " .. total .. " ]" )
- bar_y = bar_y + glassesBarSize["y"] + 5
- if( percent < 20 ) then
- redstone.setOutput("left", true)
- sleep(1)
- edstone.setOutput("left", false)
- end
- end
- function updateGlassesDisplay()
- bar_y = anchorMain["y"]
- term.clear()
- term.setCursorPos( 1, 1 )
- -- Tanks
- for k, v in pairs( tanksData ) do
- if tanksPredefData[ k ] == nil then
- print( "missing fluid data for [ " .. k .. " ]" )
- else
- handleRow( "tank_" .. k, v["amount"], v["capacity"], tanksPredefData[ k ][ "color" ], tanksPredefData[ k ][ "name" ] )
- end
- end
- end
- pGlasses.clear()
- while ( true ) do
- performUpdate()
- updateGlassesDisplay()
- sleep( updateFreq )
- end
Advertisement
Add Comment
Please, Sign In to add comment