-
Notifications
You must be signed in to change notification settings - Fork 0
/
smeltery-tank.lua
119 lines (106 loc) · 2.44 KB
/
smeltery-tank.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
--1
local config = {}
local Table = nil
local Term = nil
local port = nil
local function init()
if fs.exists('var/smeltery-tank.conf') then
local file = fs.open('var/smeltery-tank.conf', 'r')
config = textutils.unserialise(file.readAll())
file.close()
end
if not fs.exists('lib/term.lua') then
shell.run('installer lib/term.lua')
end
if not fs.exists('lib/table.lua') then
shell.run('installer lib/table.lua')
end
Term = require('lib/term')
Table = require('lib/table')
if config.portSide == nil then
setPortSide()
end
if config.tankSize == nil then
setTankSize()
end
if config.percentage == nil then
config.percentage = 0
end
if config.invert == nil then
config.invert = false
end
port = peripheral.wrap(config.portSide)
end
function writeConfig()
local file = fs.open('var/smeltery-tank.conf', 'w')
file.write(textutils.serialise(config))
file.close()
end
function setPortSide()
term.clear()
print('Which side is the port on?')
config.portSide = read()
writeConfig()
end
function setTankSize()
term.clear()
print('How big is the smeltery tank (in mB)')
config.tankSize = tonumber(read())
writeConfig()
end
function display()
term.clear()
Term.out(config.percentage..'%', 2,2)
if config.invert then
Term.out('Signal inverted', 4,2)
end
end
function getFillAmount()
return Table.reduce(
port.getController().getMolten(),
function (acc, entry) return acc + entry.amount end,
0
)
end
function xor(a, b)
return (a ~= nil and a ~= false) ~= (b ~= nil and b ~= false)
end
function keyListener()
local running = true
while running do
local _, key = os.pullEvent('key')
if key == keys.up and config.percentage < 90 then
config.percentage = config.percentage + 10
writeConfig()
end
if key == keys.down and config.percentage > 0 then
config.percentage = config.percentage - 10
writeConfig()
end
if key == keys.space then
config.invert = not config.invert
writeConfig()
end
if key == keys.q then
running = false
end
display()
end
term.clear()
term.setCursorPos(1,1)
sleep(0.05)
return
end
function signal()
while true do
local fillRatio = (100 * getFillAmount()) / config.tankSize
rs.setOutput(
config.portSide,
xor(config.invert, fillRatio < config.percentage)
)
sleep(0.05)
end
end
init()
display()
parallel.waitForAny(keyListener, signal)