forked from acidjazz/drmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drmon.lua
324 lines (255 loc) · 9.33 KB
/
drmon.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
-- modifiable variables
local reactorSide = "back"
local fluxgateSide = "right"
local targetStrength = 50
local maxTemperature = 8000
local safeTemperature = 3000
local lowestFieldPercent = 15
local targetTemp = 7000
local activateOnCharged = 1
-- please leave things untouched from here on
os.loadAPI("lib/f")
local version = "0.25"
-- toggleable via the monitor, use our algorithm to achieve our target field strength or let the user tweak it
local auto = 1
local curInputGate = 222000
-- monitor
local mon, monitor, monX, monY
-- peripherals
local reactor
local fluxgate
local inputfluxgate
-- reactor information
local ri
-- last performed action
local action = "None since reboot"
local emergencyCharge = false
local emergencyTemp = false
monitor = f.periphSearch("monitor")
inputfluxgate = f.periphSearch("flux_gate")
fluxgate = peripheral.wrap(fluxgateSide)
reactor = peripheral.wrap(reactorSide)
if monitor == null then
error("No valid monitor was found")
end
if fluxgate == null then
error("No valid fluxgate was found")
end
if reactor == null then
error("No valid reactor was found")
end
if inputfluxgate == null then
error("No valid flux gate was found")
end
monX, monY = monitor.getSize()
mon = {}
mon.monitor,mon.X, mon.Y = monitor, monX, monY
--write settings to config file
function save_config()
sw = fs.open("config.txt", "w")
sw.writeLine(version)
sw.writeLine(auto)
sw.writeLine(curInputGate)
sw.close()
end
--read settings from file
function load_config()
sr = fs.open("config.txt", "r")
version = sr.readLine()
auto = tonumber(sr.readLine())
curInputGate = tonumber(sr.readLine())
sr.close()
end
-- 1st time? save our settings, if not, load our settings
if fs.exists("config.txt") == false then
save_config()
else
load_config()
end
function buttons()
while true do
-- button handler
event, side, xPos, yPos = os.pullEvent("monitor_touch")
-- output gate controls
-- 2-4 = -1000, 6-9 = -10000, 10-12,8 = -100000
-- 17-19 = +1000, 21-23 = +10000, 25-27 = +100000
if yPos == 8 and auto == 0 then
local cFlow = fluxgate.getSignalLowFlow()
if xPos >= 2 and xPos <= 4 then
cFlow = cFlow-1000
elseif xPos >= 6 and xPos <= 9 then
cFlow = cFlow-10000
elseif xPos >= 10 and xPos <= 12 then
cFlow = cFlow-100000
elseif xPos >= 17 and xPos <= 19 then
cFlow = cFlow+100000
elseif xPos >= 21 and xPos <= 23 then
cFlow = cFlow+10000
elseif xPos >= 25 and xPos <= 27 then
cFlow = cFlow+1000
end
fluxgate.setSignalLowFlow(cFlow)
end
-- input gate controls
-- 2-4 = -1000, 6-9 = -10000, 10-12,8 = -100000
-- 17-19 = +1000, 21-23 = +10000, 25-27 = +100000
if yPos == 10 and auto == 0 and xPos ~= 14 and xPos ~= 15 then
if xPos >= 2 and xPos <= 4 then
curInputGate = curInputGate-1000
elseif xPos >= 6 and xPos <= 9 then
curInputGate = curInputGate-10000
elseif xPos >= 10 and xPos <= 12 then
curInputGate = curInputGate-100000
elseif xPos >= 17 and xPos <= 19 then
curInputGate = curInputGate+100000
elseif xPos >= 21 and xPos <= 23 then
curInputGate = curInputGate+10000
elseif xPos >= 25 and xPos <= 27 then
curInputGate = curInputGate+1000
end
inputfluxgate.setSignalLowFlow(curInputGate)
save_config()
end
-- input gate toggle
if yPos == 10 and ( xPos == 14 or xPos == 15) then
if auto == 1 then
auto = 0
else
auto = 1
end
save_config()
end
end
end
function drawButtons(y)
-- 2-4 = -1000, 6-9 = -10000, 10-12,8 = -100000
-- 17-19 = +1000, 21-23 = +10000, 25-27 = +100000
f.draw_text(mon, 2, y, " < ", colors.white, colors.gray)
f.draw_text(mon, 6, y, " <<", colors.white, colors.gray)
f.draw_text(mon, 10, y, "<<<", colors.white, colors.gray)
f.draw_text(mon, 17, y, ">>>", colors.white, colors.gray)
f.draw_text(mon, 21, y, ">> ", colors.white, colors.gray)
f.draw_text(mon, 25, y, " > ", colors.white, colors.gray)
end
function update()
while true do
f.clear(mon)
ri = reactor.getReactorInfo()
-- print out all the infos from .getReactorInfo() to term
if ri == nil then
error("reactor has an invalid setup")
end
for k, v in pairs (ri) do
print(k.. ": ".. v)
end
print("Output Gate: ", fluxgate.getSignalLowFlow())
print("Input Gate: ", inputfluxgate.getSignalLowFlow())
-- monitor output
local statusColor
statusColor = colors.red
if ri.status == "online" or ri.status == "charged" then
statusColor = colors.green
elseif ri.status == "offline" then
statusColor = colors.gray
elseif ri.status == "charging" then
statusColor = colors.orange
end
f.draw_text_lr(mon, 2, 2, 1, "Reactor Status", string.upper(ri.status), colors.white, statusColor, colors.black)
f.draw_text_lr(mon, 2, 4, 1, "Generation", f.format_int(ri.generationRate) .. " rf/t", colors.white, colors.lime, colors.black)
local tempColor = colors.red
if ri.temperature <= 5000 then tempColor = colors.green end
if ri.temperature >= 5000 and ri.temperature <= 6500 then tempColor = colors.orange end
f.draw_text_lr(mon, 2, 6, 1, "Temperature", f.format_int(ri.temperature) .. "C", colors.white, tempColor, colors.black)
f.draw_text_lr(mon, 2, 7, 1, "Output Gate", f.format_int(fluxgate.getSignalLowFlow()) .. " rf/t", colors.white, colors.blue, colors.black)
-- buttons
f.draw_text_lr(mon, 2, 9, 1, "Input Gate", f.format_int(inputfluxgate.getSignalLowFlow()) .. " rf/t", colors.white, colors.blue, colors.black)
if auto == 1 then
f.draw_text(mon, 14, 10, "AU", colors.white, colors.gray)
else
f.draw_text(mon, 14, 10, "MA", colors.white, colors.gray)
drawButtons(10)
drawButtons(8)
end
local satPercent
satPercent = math.ceil(ri.energySaturation / ri.maxEnergySaturation * 10000)*.01
f.draw_text_lr(mon, 2, 11, 1, "Energy Saturation", satPercent .. "%", colors.white, colors.white, colors.black)
f.progress_bar(mon, 2, 12, mon.X-2, satPercent, 100, colors.blue, colors.gray)
local fieldPercent, fieldColor
fieldPercent = math.ceil(ri.fieldStrength / ri.maxFieldStrength * 10000)*.01
fieldColor = colors.red
if fieldPercent >= 50 then fieldColor = colors.green end
if fieldPercent < 50 and fieldPercent > 30 then fieldColor = colors.orange end
if auto == 1 then
f.draw_text_lr(mon, 2, 14, 1, "Field Strength T:" .. targetStrength, fieldPercent .. "%", colors.white, fieldColor, colors.black)
else
f.draw_text_lr(mon, 2, 14, 1, "Field Strength", fieldPercent .. "%", colors.white, fieldColor, colors.black)
end
f.progress_bar(mon, 2, 15, mon.X-2, fieldPercent, 100, fieldColor, colors.gray)
local fuelPercent, fuelColor
fuelPercent = 100 - math.ceil(ri.fuelConversion / ri.maxFuelConversion * 10000)*.01
fuelColor = colors.red
if fuelPercent >= 70 then fuelColor = colors.green end
if fuelPercent < 70 and fuelPercent > 30 then fuelColor = colors.orange end
f.draw_text_lr(mon, 2, 17, 1, "Fuel ", fuelPercent .. "%", colors.white, fuelColor, colors.black)
f.progress_bar(mon, 2, 18, mon.X-2, fuelPercent, 100, fuelColor, colors.gray)
f.draw_text_lr(mon, 2, 19, 1, "Action ", action, colors.gray, colors.gray, colors.black)
-- actual reactor interaction
--
if emergencyCharge == true then
reactor.chargeReactor()
end
-- are we charging? open the floodgates
if ri.status == "charging" then
inputfluxgate.setSignalLowFlow(900000)
emergencyCharge = false
end
-- are we stopping from a shutdown and our temp is better? activate
if emergencyTemp == true and ri.status == "stopping" and ri.temperature < safeTemperature then
reactor.activateReactor()
emergencyTemp = false
end
-- are we charged? lets activate
if ri.status == "charged" and activateOnCharged == 1 then
reactor.activateReactor()
end
-- are we on?
if ri.status == "online" then
--regulate the input fluxgate to our target field strength
-- or set it to our saved setting since we are on manual
if auto == 1 then
fluxval = ri.fieldDrainRate / (1 - (targetStrength/100) )
print("Target Gate: ".. fluxval)
inputfluxgate.setSignalLowFlow(fluxval)
--regulate the output to match a certain target temperature
local limit = fluxgate.getSignalLowFlow()
local generation = ri.generationRate
limit = generation + (targetTemp-ri.temperature)*50
fluxgate.setSignalLowFlow(limit)
else
inputfluxgate.setSignalLowFlow(curInputGate)
end
end
-- safeguards
--
-- out of fuel, kill it
if fuelPercent <= 10 then
reactor.stopReactor()
action = "Fuel below 10%, refuel"
end
-- field strength is too dangerous, kill and it try and charge it before it blows
if fieldPercent <= lowestFieldPercent and ri.status == "online" then
action = "Field Str < " ..lowestFieldPercent.."%"
reactor.stopReactor()
reactor.chargeReactor()
emergencyCharge = true
end
-- temperature too high, kill it and activate it when its cool
if ri.temperature > maxTemperature then
reactor.stopReactor()
action = "Temp > " .. maxTemperature
emergencyTemp = true
end
sleep(0.1)
end
end
parallel.waitForAny(buttons, update)