-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactor_main.lua
148 lines (127 loc) · 4.65 KB
/
reactor_main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
--[[
Conservative reactor control program.
Very simple to use, start the program and enjoy, no need to fiddle with configurations or settings.
Will display the Reactor state (on or off), reserve percentage, current RF/t production, fuel level, fuel reactivity level, temperatures and fuel rod insertion level.
Does NOT currently work on activly cooled reactors, this program is for passive cooled only.
Author: Joker119
]]
--The maximum amount of energy a reactor can store
local maxEnergy = 10000000
local maxProduced = 148000
--Loading the required libraries
local component = require("component")
local keyboard = require("keyboard")
local term = require("term")
--This is true if there is no available screen or the option -s is used
local silent = not term.isAvailable()
local hasCustomValues, shouldChangeRods = false, false
local function serror(msg, msg2)
msg2 = msg2 or msg
if silent then
error(msg, 2)
else
io.stderr:write(msg2)
os.exit()
end
end
do
local shell = require("shell")
local args, options = shell.parse(...)
if options.s then silent = true end
if options.b then shouldChangeRods = true end
if #args > 0 then
turnOn = tonumber(args[1])
turnOff = tonumber(args[2])
hasCustomValues = true
end
end
--Check whether there is a Reactor Computer Port to access
if not component.isAvailable("br_reactor") then
serror("No connected Reactor Computer Port found.", "This program requires a connected Reactor Computer Port to run.")
end
--Getting the primary port
local reactor = component.br_reactor
--Displays long numbers with commas
local function fancyNumber(n)
return tostring(math.floor(n)):reverse():gsub("(%d%d%d)", "%1,"):gsub("%D$",""):reverse()
end
--Displays numbers with a special offset
local function offset(num, d, ext)
if num == nil then return "" end
if type(num) ~= "string" then
if type(num) == "number" then
if ext then
return offset(tostring(math.floor(num * 100) / 100), d)
else
return offset(tostring(math.floor(num)), d)
end
end
return offset(tostring(num), d)
end
if d <= #num then return num end
return string.rep(" ", d - #num) .. num
end
if not silent then
component.gpu.setResolution(component.gpu.maxResolution())
term.clear()
print("Press Ctrl+W to shutdown.")
end
--Get the current y position of the cursor for the RF display
local y, h
do
local x,w
x,y = term.getCursor()
w,h = component.gpu.getResolution()
end
--The interface offset
local offs = #tostring(maxEnergy) + 5
local function handleReactor()
--Get the current amount of energy stored
local stored = reactor.getEnergyStored()
local stored_pec = stored / maxEnergy * 100
--Set Reactor Control Rod based on energy stored
reactor.setAllControlRodLevels(stored_pec)
--Write the reactor state, the currently stored energy, the percentage value and the current production rate to screen
if not silent then
term.setCursor(1, y)
term.clearLine()
local state = reactor.getActive()
if state then
state = "On"
else
state = "Off"
reactor.setActive(true)
end
term.write("Reactor State: " .. offset(state, offs) .. "\n", false)
term.clearLine()
term.write("Reserve Level: " .. offset(stored / maxEnergy * 100, offs) .. " %\n", false)
term.clearLine()
term.write("Current Output: " .. offset(fancyNumber(reactor.getEnergyProducedLastTick()), offs) .. " RF/t\n", false)
term.clearLine()
term.write("Output Capacity: " .. offset(reactor.getEnergyProducedLastTick() / maxProduced * 100, offs) .. " %\n", false)
term.clearLine()
term.write("Casing Temperature: " .. offset(fancyNumber(reactor.getCasingTemperature()), offs) .. " c\n", false)
term.clearLine()
term.write("Fuel Rod Temperature: " .. offset(fancyNumber(reactor.getFuelTemperature()), offs) .. " c\n", false)
term.clearLine()
term.write("Fuel Level: " .. offset(fancyNumber(reactor.getFuelAmount() / reactor.getFuelAmountMax() * 100 + 1), offs) .. " %n", false)
term.clearLine()
term.write("Fuel Reactivity: " .. offset(fancyNumber(reactor.getFuelReactivity()), offs) .. " %\n", false)
term.clearLine()
term.write("Fuel Consumption: " .. offset(reactor.getFuelConsumedLastTick(), offs) .. " mB/t\n", false)
term.clearLine()
end
end
while true do
handleReactor()
--Check if the program has been terminated
if keyboard.isKeyDown(keyboard.keys.w) and keyboard.isControlDown() then
--Shut down the reactor, place cursor in a new line and exit
if not silent then
term.write("\nReactor shut down.\n")
end
reactor.setActive(false)
os.exit()
end
os.sleep(1)
end