View difference between Paste ID: iTD4rpjq and j2XFBEfZ
SHOW: | | - or go back to the newest paste.
1
term.redirect(peripheral.wrap("right"))
2
3
local mTank			= peripheral.wrap("back")   -- Tank Modem
4
local pGlasses		= peripheral.wrap("top")    -- Bridge
5
local updateFreq 	= 1                         -- How often do we update?
6
7
-- Glasses
8
local anchorMain		= { ["x"] = 10, ["y"] = 50 }
9
local glassesBarSize	= { ["x"] = 60, ["y"] = 10 }
10
local g_bars            = {}
11
local g_texts           = {}
12
local bar_y             = 0
13
14
-- Tanks
15
local tanksPredefData	= { [2] = { ["name"] = "Lava", ["color"] = 0xFF0000 } }
16
local tanksData			= {}
17
local tanksEmpty		= 0
18
19
function registerRemotePeripherals( modem )
20
	local remote = modem.getNamesRemote()
21
	local peripherals = {}
22
	 
23
	for k,v in pairs( remote ) do
24
		peripherals[ k ] = peripheral.wrap( tostring( v ) )
25
	end
26
	
27
	return peripherals
28
end
29
30
local pTanks            = registerRemotePeripherals( mTank )
31
32
function getTank( id )
33
  return pTanks[ id ].getTankInfo( "unknown" )[1]
34
end
35
36
function performUpdate()
37
	-- Update tank values
38
	tanksEmpty = 0
39
	
40
	for k = 1, #pTanks do
41
		local tank = getTank( k )
42
43
		if( tank.rawName ~= nil ) then
44
			tanksData[ tank.id ] = { ["amount"] = tank.amount, ["capacity"] = tank.capacity }
45
		else
46
		  tanksEmpty = tanksEmpty + 1    
47
		end
48
	end
49
end
50
51
function getRoundedPercent( num )
52
	return math.floor( num * 100 )
53
end
54
55
function barExist( id )
56
	if( g_bars[ id ] ~= nil ) then
57
		return true
58
	end
59
	return false
60
end
61
62
function changeBar( id, x, y, percent, color )
63
	if( barExist( id ) == false ) then
64
		return
65
	end
66
	
67
	local d = g_bars[ id ]
68
	
69
	if( color == nil ) then
70
		color = 0x99FF00
71
	end
72
	
73
	if( x == nil ) then
74
		x = d[ "border" ].getX()
75
	end
76
	
77
	if( y == nil ) then
78
		y = d[ "border" ].getY()
79
	end
80
	
81
	d[ "border" ].setX( x )
82
	d[ "border" ].setY( y )
83
	d[ "borderbg" ].setX( x + 1 )
84
	d[ "borderbg" ].setY( y + 1 )
85
	d[ "bar" ].setX( x + 3 )
86
	d[ "bar" ].setY( y + 3 )
87
	
88
	local w	= d[ "border" ].getWidth()
89
	local dx = math.ceil( ( w - 6 ) * ( percent / 100 ) )
90
	
91
	d["bar"].setWidth( dx )
92
	
93
	return d
94
end
95
96
function createBarAt( id, x, y, percent, color )
97
	if( color == nil ) then
98
		color = 0x99FF00
99
	end
100
	
101
	local d = {}
102
	local w = glassesBarSize["x"]
103
	local h = glassesBarSize["y"]
104
	local dx = math.ceil( ( w - 6 ) * ( percent / 100 ) )
105
	
106
	d["border"] 	= pGlasses.addBox( x, y, w, h, 0xFFFFFF, 1 )
107
	d["borderbg"] 	= pGlasses.addBox( x + 1, y + 1, w - 2, h - 2, 0x000000, 1 )
108
	d["bar"] 		= pGlasses.addGradientBox( x + 3, y + 3, dx, h - 6, color, 0.05, 0xFFFFFF, 1, 2 )
109
	
110
	d["border"].setZ( 1 )
111
	d["borderbg"].setZ( 2 )
112
	d["bar"].setZ( 3 )
113
	
114
	g_bars[ id ] = d
115
	
116
	return g_bars[ id ]
117
end
118
119
function getBar( id, x, y, percent, color )
120
	if( barExist( id ) ) then
121
		return changeBar( id, x, y, percent, color )
122
	end
123
	
124
	return createBarAt( id, x, y, percent, color )
125
end
126
127
function textExist( id )
128
	if( g_texts[ id ] ~= nil ) then
129
		return true
130
	end
131
	return false
132
end
133
134
function changeText( id, x, y, text, color )
135
	if( textExist( id ) == false ) then
136
		return nil
137
	end
138
	
139
	if( color == nil ) then
140
		color = 0xFFFFFF
141
	end
142
	
143
	local d = g_texts[ id ]
144
	d.setX( x )
145
	d.setY( y )
146
	d.setText( text )
147
	d.setColor( color )
148
	
149
	return d
150
end
151
152
function createTextAt( id, x, y, text, color )
153
	if( textExist( id ) ) then
154
        return nil
155
    end
156
    
157
    if( color == nil ) then
158
		color = 0xFFFFFF
159
	end
160
	
161
	local d = pGlasses.addText( x, y, text, color )
162
	
163
	g_texts[ id ] = d
164
	
165
	return g_texts[ id ]
166
end
167
168
function getText( id, x, y, text, color )
169
	if( textExist( id ) ) then
170
		return changeText( id, x, y, text, color )
171
	end
172
	
173
	return createTextAt( id, x, y, text, color )
174
end
175
176
function handleRow( tid, part, total, color, text )
177
    local percent = getRoundedPercent( part / total )
178
	local c = 0xFFFFFF
179
    
180
	-- Bytes
181
	b = getBar( tid, anchorMain["x"] + 50, bar_y, percent, color )
182
	
183
	if( percent < 11 ) then
184
		c = 0xFF0000
185
	end
186
	
187
    x = b[ "border" ].getX() + b[ "border" ].getWidth() + 5
188
    
189
	getText( tid .. "_pre", anchorMain["x"], bar_y + 1, text, 0xFFFFFF )
190
	getText( tid .. "_post", x, bar_y + 1, percent .. "%", c )
191
	
192
    print( text .. " - " .. percent .. "%" .. "[ " .. part .. " / " .. total .. " ]" )
193
    
194
	bar_y = bar_y + glassesBarSize["y"] + 5
195
	
196
	if( percent < 20 ) then
197
		redstone.setOutput("left", true)
198
		sleep(1)
199
		edstone.setOutput("left", false)
200
	end
201
end
202
203
function updateGlassesDisplay()
204
	bar_y = anchorMain["y"]
205
	
206
    term.clear()
207
    term.setCursorPos( 1, 1 )
208
	
209
	-- Tanks
210
	for k, v in pairs( tanksData ) do
211
        if tanksPredefData[ k ] == nil then
212
            print( "missing fluid data for [ " .. k .. " ]" )
213
        else
214
            handleRow( "tank_" .. k, v["amount"], v["capacity"], tanksPredefData[ k ][ "color" ], tanksPredefData[ k ][ "name" ] )
215
        end
216
    end
217
end
218
219
pGlasses.clear()
220
221
while ( true ) do
222
	performUpdate()
223
	updateGlassesDisplay()
224
	sleep( updateFreq )
225
end