SHOW:
|
|
- or go back to the newest paste.
1 | -- Basic little control program for a passively cooled reactor. This program would not be possible, even in the primitive state it is now, without the inspiration and coding support of lolmer and ScatmanJohn. Will passively detect a reactor and a monitor, either through direct proximity or a wired modem. Please ensure the computer and monitors are the advanced version (it's just gold, not exactly rare in Minecraft), and that the monitor is a 3x2. | |
2 | local progVer = "1.06" | |
3 | local progName = "IC_SimpleNuke" | |
4 | local loopTime = 0.5 -- Time between program loops, in seconds. Increase this value if program somehow causes a lot of lag. Which should not happen on any computers made this century | |
5 | - | local baseControlRodLevel = 20 -- Put this as close to what you guess the optimum control rod level will be for your reactor, plus a little more |
5 | + | local baseControlRodLevel = 95 -- Put this as close to what you guess the optimum control rod level will be for your reactor, plus a little more |
6 | local minStoredEnergyPercent = 20 -- Min energy % to store before activating, probably keep it at default (20) | |
7 | - | local maxStoredEnergyPercent = 60 -- Max energy % to store before shutdown, probably keep it at default (70) |
7 | + | local maxStoredEnergyPercent = 90 -- Max energy % to store before shutdown, probably keep it at default (70) |
8 | -- This program doesn't care about heat anymore, only tried to maintain the internal power buffer, following 2 lines only included for legacy | |
9 | local minReactorTemp = 20 -- Minimum reactor temperature (^C) to maintain | |
10 | - | local maxReactorTemp = 400 -- Maximum reactor temperature (^C) to maintain |
10 | + | local maxReactorTemp = 3000 -- Maximum reactor temperature (^C) to maintain |
11 | ||
12 | ---------- | |
13 | ||
14 | -- Basic side determination for wrapping | |
15 | function getDeviceSide(deviceType) | |
16 | deviceType = deviceType:lower() | |
17 | ||
18 | for i, side in pairs(rs.getSides()) do | |
19 | if (peripheral.isPresent(side)) then | |
20 | if (string.lower(peripheral.getType(side)) == deviceType) then | |
21 | return side; | |
22 | end | |
23 | end | |
24 | end | |
25 | ||
26 | return nil; | |
27 | end | |
28 | ||
29 | -- Peripheral wrapping function | |
30 | function wrapThis(thing, f) | |
31 | local wrapped = nil | |
32 | while wrapped == nil and f <= 100 do | |
33 | wrapped = peripheral.wrap(thing.."_"..f) | |
34 | f = f + 1 | |
35 | end | |
36 | ||
37 | if wrapped == nil then | |
38 | side = getDeviceSide(thing) | |
39 | if side ~= nil then | |
40 | return peripheral.wrap(side) | |
41 | else | |
42 | return nil | |
43 | end | |
44 | else | |
45 | return wrapped | |
46 | end | |
47 | end | |
48 | ||
49 | ||
50 | print("Initializing program...",1,1); | |
51 | ||
52 | -- Initialize the monitor | |
53 | -- If it can't find a monitor, will wait 5 seconds then reboot | |
54 | -- This is needed for server restarts when the computer starts up before the modem does | |
55 | local monitor = wrapThis("monitor", 0) | |
56 | if monitor == nil then | |
57 | sleep(5) | |
58 | finished = true | |
59 | os.reboot() | |
60 | end | |
61 | local monitorx, monitory = monitor.getSize() | |
62 | if monitorx ~= 29 or monitory ~= 12 then | |
63 | print("Monitor is the wrong size! Needs to be 3x2.") | |
64 | sleep(5) | |
65 | finished = true | |
66 | os.reboot() | |
67 | end | |
68 | ||
69 | -- Connect to the big reactor peripheral | |
70 | -- If it can't find a reactor, will wait 5 seconds then reboot | |
71 | -- This is needed for server restarts when the computer starts up before the modem does | |
72 | local reactor = wrapThis("BigReactors-Reactor", 0) | |
73 | if reactor == nil then | |
74 | sleep(5) | |
75 | finished = true | |
76 | os.reboot() | |
77 | end | |
78 | ||
79 | -- Easyprint, saves program space elsewhere | |
80 | local function print(str, x, y) | |
81 | term.setCursorPos(x, y) | |
82 | term.write(str) | |
83 | end | |
84 | ||
85 | -- Move stuff to monitor | |
86 | if monitor then | |
87 | sleep(1) | |
88 | term.clear() | |
89 | term.setCursorPos(1,1) | |
90 | term.write("Display redirected to Monitor. Type r to reboot, or") | |
91 | term.setCursorPos(1,2) | |
92 | term.write("q to quit and return control to this terminal.") | |
93 | term.redirect(monitor) | |
94 | end | |
95 | ||
96 | -- Restore functions to computer terminal | |
97 | function restoreNativeTerminal() | |
98 | repeat | |
99 | term.restore() | |
100 | local w, h = term.getSize() | |
101 | until w == 51 and h == 19 | |
102 | end | |
103 | ||
104 | -- Draw some decorative lines | |
105 | function drawLines() | |
106 | term.setBackgroundColor(colors.black) | |
107 | paintutils.drawLine(1, 2, 29, 2, colors.lightBlue) | |
108 | paintutils.drawLine(1, 7, 29, 7, colors.lightBlue) | |
109 | term.setBackgroundColor(colors.black) | |
110 | end | |
111 | ||
112 | -- Just to make sure they're relatively high before the control program starts | |
113 | if reactor then | |
114 | sleep(1) | |
115 | reactor.setAllControlRodLevels(baseControlRodLevel) | |
116 | else | |
117 | sleep(5) | |
118 | finished = true | |
119 | os.reboot() | |
120 | end | |
121 | ||
122 | -- Draw stuff on the monitor | |
123 | local function drawInfo() | |
124 | ||
125 | statusstring = "Reactor status: " | |
126 | tempstring = "Temperature: " | |
127 | tempstring2 = "deg. C" | |
128 | powerstring = "Power Output: " | |
129 | powerstring2 = "RF/t" | |
130 | rodstring = "Control Rods: " | |
131 | rodstring2 = "%" | |
132 | ||
133 | monitor.setTextColor(colors.green) | |
134 | print(progName,5,1) | |
135 | monitor.setTextColor(colors.red) | |
136 | print("v",19,1) | |
137 | monitor.setTextColor(colors.green) | |
138 | print(progVer,20,1) | |
139 | monitor.setTextColor(colors.white) | |
140 | ||
141 | print(statusstring,1,3) | |
142 | if reactor.getActive() then | |
143 | monitor.setTextColor(colors.green) | |
144 | print(" online",23,3) | |
145 | else | |
146 | monitor.setTextColor(colors.red) | |
147 | print("offline",23,3) | |
148 | end | |
149 | ||
150 | monitor.setTextColor(colors.white) | |
151 | --reactortemp = math.floor(reactor.getFuelTemperature()) | |
152 | print(tempstring,1,4) | |
153 | print(reactortemp,16,4) | |
154 | print(tempstring2,24,4) | |
155 | ||
156 | power = math.floor(reactor.getEnergyProducedLastTick()) | |
157 | print(powerstring,1,5) | |
158 | print(power,16,5) | |
159 | print(powerstring2,26,5) | |
160 | ||
161 | rods = reactor.getControlRodLevel(1) | |
162 | print(rodstring,1,6) | |
163 | print(rods,16,6) | |
164 | print(rodstring2,29,6) | |
165 | end | |
166 | ||
167 | -- Draw stuff on the bottom part of the monitor, power bar etc | |
168 | function drawBottomPart() | |
169 | reactivity = math.floor(reactor.getFuelReactivity()) | |
170 | paintutils.drawLine(2, 9, 28, 9, colors.gray) | |
171 | if reactivity > 400 then | |
172 | paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.red) | |
173 | elseif reactivity > 320 then | |
174 | paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.orange) | |
175 | elseif reactivity > 240 then | |
176 | paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.yellow) | |
177 | elseif reactivity > 160 then | |
178 | paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.green) | |
179 | elseif reactivity > 0 then | |
180 | paintutils.drawLine(2, 9, math.floor(26*reactivity/500)+1, 9, colors.blue) | |
181 | end | |
182 | term.setBackgroundColor(colors.black) | |
183 | print("Fuel Reactivity:",1,8) | |
184 | print(reactivity,24,8) | |
185 | print("%",29,8) | |
186 | term.setBackgroundColor(colors.black) | |
187 | ||
188 | energystorage = reactor.getEnergyStored() | |
189 | storagepercent = math.floor(energystorage/10000000*100) | |
190 | paintutils.drawLine(2, 12, 28, 12, colors.gray) | |
191 | if storagepercent > 4 then | |
192 | paintutils.drawLine(2, 12, math.floor(26*storagepercent/100)+2, 12, colors.yellow) | |
193 | elseif storagepercent > 0 then | |
194 | paintutils.drawPixel(2,12,colors.yellow) | |
195 | end | |
196 | term.setBackgroundColor(colors.black) | |
197 | print("Internal buffer: ",1,11) | |
198 | print(storagepercent,24,11) | |
199 | print("%",29,11) | |
200 | term.setBackgroundColor(colors.black) | |
201 | end | |
202 | ||
203 | -- Controls the reactor, keeping it between configured temps and power storage | |
204 | function control() | |
205 | energystorage = reactor.getEnergyStored() | |
206 | reactortemp = math.floor(reactor.getFuelTemperature()) | |
207 | ediff = (maxStoredEnergyPercent - minStoredEnergyPercent)*100000 | |
208 | ediffper = ediff / 100 | |
209 | if energystorage > maxStoredEnergyPercent*100000 then | |
210 | reactor.setActive(false) | |
211 | reactor.setAllControlRodLevels(95) | |
212 | elseif energystorage < minStoredEnergyPercent*100000 then | |
213 | reactor.setActive(true) | |
214 | reactor.setAllControlRodLevels(5) | |
215 | elseif energystorage > minStoredEnergyPercent*100000 then | |
216 | reactor.setActive(true) | |
217 | blarg = energystorage - minStoredEnergyPercent*100000 | |
218 | blarg2 = math.floor(blarg/(ediffper+1)) | |
219 | reactor.setAllControlRodLevels(blarg2) | |
220 | end | |
221 | end | |
222 | ||
223 | -- Main program | |
224 | function main() | |
225 | while not finished do | |
226 | if reactor.getConnected() then | |
227 | drawLines() | |
228 | drawInfo() | |
229 | drawBottomPart() | |
230 | control() | |
231 | sleep(loopTime) | |
232 | end | |
233 | end | |
234 | end | |
235 | ||
236 | -- Event handler for exiting, editting and debugging | |
237 | function eventHandler() | |
238 | while not finished do | |
239 | event, arg1, arg2, arg3 = os.pullEvent() | |
240 | if event == "char" and not inManualMode then | |
241 | local ch = string.lower(arg1) | |
242 | if ch == "q" then | |
243 | finished = true | |
244 | elseif ch == "r" then | |
245 | finished = true | |
246 | os.reboot() | |
247 | end | |
248 | end | |
249 | end | |
250 | end | |
251 | ||
252 | while not finished do | |
253 | parallel.waitForAny(eventHandler, main) | |
254 | sleep(loopTime) | |
255 | end | |
256 | ||
257 | -- Returns control to the terminal if the program has an error or whatever | |
258 | term.clear() | |
259 | term.setCursorPos(1,1) | |
260 | restoreNativeTerminal() | |
261 | term.clear() | |
262 | term.setCursorPos(1,1) |