SHOW:
|
|
- or go back to the newest paste.
1 | -- Basic control for BigReactors-Reactor | |
2 | -- BSD3 License | |
3 | -- Emily Backes <lucca@accela.net> | |
4 | ||
5 | -- Uses the first monitor it finds, if any | |
6 | -- May need 3x3 or larger for that | |
7 | -- No log output or printer usage yet | |
8 | -- Will work on adv comps but mouse event handling | |
9 | -- would need to be added below | |
10 | -- Suitable for use in /startup | |
11 | for bullshit=1,60 do | |
12 | print("Computer is sleeping for"..60-bullshit.."more seconds") | |
13 | sleep(1) | |
14 | end | |
15 | ||
16 | -- Max energy in a reactor's internal cell | |
17 | local emax=10000000 | |
18 | ||
19 | -- wrap everything in an exception handler | |
20 | local ok,msg=pcall(function () | |
21 | local r | |
22 | local m | |
23 | local redirected=false | |
24 | local p | |
25 | ||
26 | function findDev (dType) | |
27 | local d | |
28 | for _,d in pairs(peripheral.getNames()) do | |
29 | if (peripheral.getType(d) == dType) then | |
30 | return peripheral.wrap(d) | |
31 | end | |
32 | end | |
33 | return nil, dType..": not found" | |
34 | end | |
35 | ||
36 | function setupDevs() | |
37 | r=assert(findDev("BigReactors-Reactor")) | |
38 | if (not r.getConnected()) then | |
39 | return nil, "Computer port not connected to a valid reactor" | |
40 | end | |
41 | --if (r.getNumberOfControlRods() <1) then | |
42 | -- return nil, "Reactor seems invalid" | |
43 | --end | |
44 | r.getEnergyPercent = function () | |
45 | return math.floor(1000 * r.getEnergyStored() / emax)/10 | |
46 | end | |
47 | if r.nativeEPLT then | |
48 | r.getEnergyProducedLastTick = r.nativeEPLT | |
49 | end | |
50 | r.nativeEPLT = r.getEnergyProducedLastTick | |
51 | r.getEnergyProducedLastTick = function () | |
52 | return math.floor(r.nativeEPLT()*1000)/1000 | |
53 | end | |
54 | ||
55 | if redirected then | |
56 | term.restore() | |
57 | redirected = false | |
58 | end | |
59 | m=findDev("monitor") | |
60 | if m then | |
61 | m.setTextScale(0.5) | |
62 | term.clear() | |
63 | term.setCursorPos(1,1) | |
64 | print("Redirecting to attached monitor") | |
65 | term.redirect(m) | |
66 | redirected = true | |
67 | end | |
68 | ||
69 | term.setCursorBlink(false) | |
70 | p=findDev("printer") | |
71 | end | |
72 | ||
73 | function ft () | |
74 | local d=os.day() | |
75 | local t=os.time() | |
76 | local h=math.floor(t) | |
77 | local m=math.floor((t-h)*60) | |
78 | return string.format("Day %d, %02d:%02d",d,h,m) | |
79 | end | |
80 | ||
81 | function log (msg) | |
82 | local stamp=ft() | |
83 | print (stamp..": "..msg) | |
84 | end | |
85 | ||
86 | function tableWidth(t) | |
87 | local width=0 | |
88 | for _,v in pairs(t) do | |
89 | if #v>width then width=#v end | |
90 | end | |
91 | return width | |
92 | end | |
93 | ||
94 | function ljust(s,w) | |
95 | local pad=w-#s | |
96 | return s .. string.rep(" ",pad) | |
97 | end | |
98 | ||
99 | function rjust(s,w) | |
100 | local pad=w-#s | |
101 | return string.rep(" ",pad) .. s | |
102 | end | |
103 | ||
104 | function display() | |
105 | term.clear() | |
106 | term.setCursorPos(1,1) | |
107 | print("Reactor Status") | |
108 | print(ft()) | |
109 | print("") | |
110 | local funcs={"Connected","Active","NumberOfControlRods","EnergyStored","EnergyPercent","CasingTemperature","FuelTemperature","FuelAmount","WasteAmount","FuelAmountMax","EnergyProducedLastTick"} | |
111 | local units={"","","","RF","%","C","C","mB","mB","mB","RF/t"} | |
112 | local values={} | |
113 | for _,v in pairs(funcs) do | |
114 | values[#values+1] = tostring(r["get"..v]()) | |
115 | end | |
116 | local funcW=tableWidth(funcs) | |
117 | local valW=tableWidth(values) | |
118 | for i,v in pairs(funcs) do | |
119 | print(rjust(v,funcW)..": "..rjust(values[i],valW).." "..units[i]) | |
120 | end | |
121 | end | |
122 | ||
123 | log("Starting") | |
124 | setupDevs() | |
125 | while true do | |
126 | local e=r.getEnergyStored() | |
127 | local p=math.floor(100*e/emax) | |
128 | local a=p<100 | |
129 | local elt=r.getEnergyProducedLastTick() | |
130 | display() | |
131 | r.setAllControlRodLevels(p) | |
132 | r.setActive(a) | |
133 | os.startTimer(0.8333334) | |
134 | local event,p1,p2,p3,p4,p5 = os.pullEvent() | |
135 | if event == "key" then | |
136 | break | |
137 | elseif event == "peripheral_detach" or event == "peripheral" or event == "monitor_resize" then | |
138 | setupDevs() | |
139 | elseif not (event == "timer" or event=="disk" or event=="disk_eject") then | |
140 | error("received "..event) | |
141 | end | |
142 | end | |
143 | ||
144 | end) | |
145 | term.restore() | |
146 | error(msg) |