View difference between Paste ID: xjXG3Qvu and EUCmbEqx
SHOW: | | - or go back to the newest paste.
1
-- modifiable variables
2
local reactorSide = "back"
3
local fluxgateSide = "right"
4
5
local targetStrength = 50
6
local maxTemperature = 8000
7
local safeTemperature = 3000
8
local lowestFieldPercent = 15
9
10
local activateOnCharged = 1
11
12
-- please leave things untouched from here on
13
os.loadAPI("lib/f")
14
15
local version = "0.25"
16
-- toggleable via the monitor, use our algorithm to achieve our target field strength or let the user tweak it
17
local autoInputGate = 1
18
local curInputGate = 222000
19
20
-- monitor 
21
local mon, monitor, monX, monY
22
23
-- peripherals
24
local reactor
25
local fluxgate
26
local inputfluxgate
27
28
-- reactor information
29
local ri
30
31
-- last performed action
32
local action = "None since reboot"
33
local emergencyCharge = false
34
local emergencyTemp = false
35
36
monitor = f.periphSearch("monitor")
37
inputfluxgate = f.periphSearch("flux_gate")
38
fluxgate = peripheral.wrap(fluxgateSide)
39
reactor = peripheral.wrap(reactorSide)
40
41
if monitor == null then
42
	error("No valid monitor was found")
43
end
44
45
if fluxgate == null then
46
	error("No valid fluxgate was found")
47
end
48
49
if reactor == null then
50
	error("No valid reactor was found")
51
end
52
53
if inputfluxgate == null then
54
	error("No valid flux gate was found")
55
end
56
57
monX, monY = monitor.getSize()
58
mon = {}
59
mon.monitor,mon.X, mon.Y = monitor, monX, monY
60
61
--write settings to config file
62
function save_config()
63
  sw = fs.open("config.txt", "w")   
64
  sw.writeLine(version)
65
  sw.writeLine(autoInputGate)
66
  sw.writeLine(curInputGate)
67
  sw.close()
68
end
69
70
--read settings from file
71
function load_config()
72
  sr = fs.open("config.txt", "r")
73
  version = sr.readLine()
74
  autoInputGate = tonumber(sr.readLine())
75
  curInputGate = tonumber(sr.readLine())
76
  sr.close()
77
end
78
79
80
-- 1st time? save our settings, if not, load our settings
81
if fs.exists("config.txt") == false then
82
  save_config()
83
else
84
  load_config()
85
end
86
87
function buttons()
88
89
  while true do
90
    -- button handler
91
    event, side, xPos, yPos = os.pullEvent("monitor_touch")
92
93
    -- reactor control
94
    if yPos >= 1 and yPos <= 3 then
95
      if ri.status == "charging" then
96
        reactor.stopReactor()
97
      elseif ri.status == "offline" then
98
        reactor.chargeReactor()
99
      elseif ri.status == "online" then
100
        reactor.stopReactor()
101
      end
102
    end
103
    
104
    -- output gate controls
105
    -- 2-4 = -1000, 6-9 = -10000, 10-12,8 = -100000
106
    -- 17-19 = +1000, 21-23 = +10000, 25-27 = +100000
107-
    if yPos == 10 then
107+
    if yPos == 7 then
108
      local cFlow = fluxgate.getSignalLowFlow()
109
      if xPos >= 2 and xPos <= 4 then
110
        cFlow = cFlow-1000
111
      elseif xPos >= 6 and xPos <= 9 then
112
        cFlow = cFlow-10000
113
      elseif xPos >= 10 and xPos <= 12 then
114
        cFlow = cFlow-100000
115
      elseif xPos >= 17 and xPos <= 19 then
116
        cFlow = cFlow+100000
117
      elseif xPos >= 21 and xPos <= 23 then
118
        cFlow = cFlow+10000
119
      elseif xPos >= 25 and xPos <= 27 then
120
        cFlow = cFlow+1000
121
      end
122
      fluxgate.setSignalLowFlow(cFlow)
123
    end
124
125
    -- input gate controls
126
    -- 2-4 = -1000, 6-9 = -10000, 10-12,8 = -100000
127
    -- 17-19 = +1000, 21-23 = +10000, 25-27 = +100000
128-
    if yPos == 14 and autoInputGate == 0 and xPos ~= 14 and xPos ~= 15 then
128+
    if yPos == 10 and autoInputGate == 0 and xPos ~= 14 and xPos ~= 15 then
129
      if xPos >= 2 and xPos <= 4 then
130
        curInputGate = curInputGate-1000
131
      elseif xPos >= 6 and xPos <= 9 then
132
        curInputGate = curInputGate-10000
133
      elseif xPos >= 10 and xPos <= 12 then
134
        curInputGate = curInputGate-100000
135
      elseif xPos >= 17 and xPos <= 19 then
136
        curInputGate = curInputGate+100000
137
      elseif xPos >= 21 and xPos <= 23 then
138
        curInputGate = curInputGate+10000
139
      elseif xPos >= 25 and xPos <= 27 then
140
        curInputGate = curInputGate+1000
141
      end
142
      inputfluxgate.setSignalLowFlow(curInputGate)
143
      save_config()
144
    end
145
146
    -- input gate toggle
147-
    if yPos == 14 and ( xPos == 14 or xPos == 15) then
147+
    if yPos == 10 and ( xPos == 14 or xPos == 15) then
148
      if autoInputGate == 1 then
149
        autoInputGate = 0
150
      else
151
        autoInputGate = 1
152
      end
153
      save_config()
154
    end
155
156
  end
157
end
158
159
function drawButtons(y)
160
161
  -- 2-4 = -1000, 6-9 = -10000, 10-12,8 = -100000
162
  -- 17-19 = +1000, 21-23 = +10000, 25-27 = +100000
163
164-
  f.draw_text(mon, 2, y, " < ", colors.white, colors.gray)
164+
  f.draw_text(mon, 2, y, " < ", colors.white, colors.lightBlue)
165-
  f.draw_text(mon, 6, y, " <<", colors.white, colors.gray)
165+
  f.draw_text(mon, 6, y, " <<", colors.white, colors.lightBlue)
166-
  f.draw_text(mon, 10, y, "<<<", colors.white, colors.gray)
166+
  f.draw_text(mon, 10, y, "<<<", colors.white, colors.lightBlue)
167
168-
  f.draw_text(mon, 17, y, ">>>", colors.white, colors.gray)
168+
  f.draw_text(mon, 17, y, ">>>", colors.white, colors.purple)
169-
  f.draw_text(mon, 21, y, ">> ", colors.white, colors.gray)
169+
  f.draw_text(mon, 21, y, ">> ", colors.white, colors.purple)
170-
  f.draw_text(mon, 25, y, " > ", colors.white, colors.gray)
170+
  f.draw_text(mon, 25, y, " > ", colors.white, colors.purple)
171
end
172
173
174
175
function update()
176
  while true do 
177
178
    f.clear(mon)
179
180
    ri = reactor.getReactorInfo()
181
182
    -- print out all the infos from .getReactorInfo() to term
183
184
    if ri == nil then
185
      error("reactor has an invalid setup")
186
    end
187
188
    for k, v in pairs (ri) do
189
      print(k.. ": ".. v)
190
    end
191
    print("Output Gate: ", fluxgate.getSignalLowFlow())
192
    print("Input Gate: ", inputfluxgate.getSignalLowFlow())
193
194
    -- monitor output
195
196
    local statusColor
197
    statusColor = colors.red
198
199
    if ri.status == "online" or ri.status == "charged" then
200
      statusColor = colors.green
201
    elseif ri.status == "offline" then
202
      statusColor = colors.gray
203
    elseif ri.status == "charging" then
204
      statusColor = colors.orange
205
    end
206
207
    f.draw_text_lr(mon, 2, 2, 1, "Reactor Status", string.upper(ri.status), colors.white, statusColor, colors.black)
208
209
    f.draw_text_lr(mon, 2, 4, 1, "Generation", f.format_int(ri.generationRate) .. " rf/t", colors.white, colors.lime, colors.black)
210
211-
    local tempColor = colors.red
211+
    f.draw_text_lr(mon, 2, 6, 1, "Output Gate", f.format_int(fluxgate.getSignalLowFlow()) .. " rf/t", colors.white, colors.blue, colors.black)
212
213
    -- buttons
214-
    f.draw_text_lr(mon, 2, 6, 1, "Temperature", f.format_int(ri.temperature) .. "C", colors.white, tempColor, colors.black)
214+
    drawButtons(7)
215
216-
    f.draw_text_lr(mon, 2, 8, 1, "Output Gate", f.format_int(fluxgate.getSignalLowFlow()) .. " rf/t", colors.white, colors.blue, colors.black)
216+
    f.draw_text_lr(mon, 2, 9, 1, "Input Gate", f.format_int(inputfluxgate.getSignalLowFlow()) .. " rf/t", colors.white, colors.blue, colors.black)
217
218
    if autoInputGate == 1 then
219-
    drawButtons(10)
219+
      f.draw_text(mon, 14, 10, "AU", colors.white, colors.gray)
220
    else
221-
    f.draw_text_lr(mon, 2, 12, 1, "Input Gate", f.format_int(inputfluxgate.getSignalLowFlow()) .. " rf/t", colors.white, colors.blue, colors.black)
221+
      f.draw_text(mon, 14, 10, "MA", colors.white, colors.green)
222
      drawButtons(10)
223
    end
224-
      f.draw_text(mon, 14, 14, "AU", colors.white, colors.gray)
224+
225
    f.draw_text(mon, 0, 12, "                                                      ", colors.white, colors.yellow)
226-
      f.draw_text(mon, 14, 14, "MA", colors.white, colors.gray)
226+
227-
      drawButtons(14)
227+
228
    satPercent = math.ceil(ri.energySaturation / ri.maxEnergySaturation * 10000)*.01
229
    f.draw_text_lr(mon, 2, 14, 1, "Energy Saturation", satPercent .. "%", colors.white, colors.white, colors.black)
230-
    f.draw_text(mon, 0, 16, "                                                      ", colors.white, colors.yellow)
230+
    f.progress_bar(mon, 2, 15, mon.X-2, satPercent, 100, colors.blue, colors.gray)
231
232
    local tempPercent, tempColor
233
    tempPercent = math.ceil(ri.temperature / 8000 * 10000)*.01
234-
    f.draw_text_lr(mon, 2, 18, 1, "Energy Saturation", satPercent .. "%", colors.white, colors.white, colors.black)
234+
235-
    f.progress_bar(mon, 2, 19, mon.X-2, satPercent, 100, colors.blue, colors.gray)
235+
    temperatureColor = colors.red
236
    if ri.temperature <= 5000 then tempColor = colors.green end
237
    if ri.temperature >= 5000 and ri.temperature <= 6500 then tempColor = colors.orange end
238
239
    f.draw_text_lr(mon, 2, 17, 1, "Temperature ", ri.temperature .. "°C", colors.white, tempColor, colors.black)
240
    f.progress_bar(mon, 2, 18, mon.X-2, tempPercent, 100, tempColor, colors.gray)
241
242
    local fieldPercent, fieldColor
243
    fieldPercent = math.ceil(ri.fieldStrength / ri.maxFieldStrength * 10000)*.01
244
245-
      f.draw_text_lr(mon, 2, 21, 1, "Field Strength T:" .. targetStrength, fieldPercent .. "%", colors.white, fieldColor, colors.black)
245+
246
    if fieldPercent >= 50 then fieldColor = colors.green end
247-
      f.draw_text_lr(mon, 2, 21, 1, "Field Strength", fieldPercent .. "%", colors.white, fieldColor, colors.black)
247+
248
249-
    f.progress_bar(mon, 2, 22, mon.X-2, fieldPercent, 100, fieldColor, colors.gray)
249+
250
      f.draw_text_lr(mon, 2, 20, 1, "Field Strength T:" .. targetStrength, fieldPercent .. "%", colors.white, fieldColor, colors.black)
251
    else
252
      f.draw_text_lr(mon, 2, 20, 1, "Field Strength", fieldPercent .. "%", colors.white, fieldColor, colors.black)
253
    end
254
    f.progress_bar(mon, 2, 21, mon.X-2, fieldPercent, 100, fieldColor, colors.gray)
255
256
    local fuelPercent, fuelColor
257
258
    fuelPercent = 100 - math.ceil(ri.fuelConversion / ri.maxFuelConversion * 10000)*.01
259
260-
    f.draw_text_lr(mon, 2, 24, 1, "Fuel ", fuelPercent .. "%", colors.white, fuelColor, colors.black)
260+
261-
    f.progress_bar(mon, 2, 25, mon.X-2, fuelPercent, 100, fuelColor, colors.gray)
261+
262
    if fuelPercent >= 70 then fuelColor = colors.green end
263
    if fuelPercent < 70 and fuelPercent > 30 then fuelColor = colors.orange end
264
265
    f.draw_text_lr(mon, 2, 23, 1, "Fuel ", fuelPercent .. "%", colors.white, fuelColor, colors.black)
266
    f.progress_bar(mon, 2, 24, mon.X-2, fuelPercent, 100, fuelColor, colors.gray)
267
268
    f.draw_text_lr(mon, 2, 26, 1, "Action ", action, colors.gray, colors.gray, colors.black)
269
270
    -- actual reactor interaction
271
    --
272
    if emergencyCharge == true then
273
      reactor.chargeReactor()
274
    end
275
    
276
    -- are we charging? open the floodgates
277
    if ri.status == "charging" then
278
      inputfluxgate.setSignalLowFlow(900000)
279
      emergencyCharge = false
280
    end
281
282
    -- are we stopping from a shutdown and our temp is better? activate
283
    if emergencyTemp == true and ri.status == "stopping" and ri.temperature < safeTemperature then
284
      reactor.activateReactor()
285
      emergencyTemp = false
286
    end
287
288
    -- are we charged? lets activate
289
    if ri.status == "charged" and activateOnCharged == 1 then
290
      reactor.activateReactor()
291
    end
292
293
    -- are we on? regulate the input fludgate to our target field strength
294
    -- or set it to our saved setting since we are on manual
295
    if ri.status == "online" then
296
      if autoInputGate == 1 then 
297
        fluxval = ri.fieldDrainRate / (1 - (targetStrength/100) )
298
        print("Target Gate: ".. fluxval)
299
        inputfluxgate.setSignalLowFlow(fluxval)
300
      else
301
        inputfluxgate.setSignalLowFlow(curInputGate)
302
      end
303
    end
304
305
    -- safeguards
306
    --
307
    
308
    -- out of fuel, kill it
309
    if fuelPercent <= 10 then
310
      reactor.stopReactor()
311
      action = "Fuel below 10%, refuel"
312
    end
313
314
    -- field strength is too dangerous, kill and it try and charge it before it blows
315
    if fieldPercent <= lowestFieldPercent and ri.status == "online" then
316
      action = "Field Str < " ..lowestFieldPercent.."%"
317
      reactor.stopReactor()
318
      reactor.chargeReactor()
319
      emergencyCharge = true
320
    end
321
322
    -- temperature too high, kill it and activate it when its cool
323
    if ri.temperature > maxTemperature then
324
      reactor.stopReactor()
325
      action = "Temp > " .. maxTemperature
326
      emergencyTemp = true
327
    end
328
329
    sleep(0.1)
330
  end
331
end
332
333
parallel.waitForAny(buttons, update)