View difference between Paste ID: UqVHTht5 and wk87XK3B
SHOW: | | - or go back to the newest paste.
1
--peripheral config
2-
local sideGateOut = "right"
2+
local sideGateOut = "top"
3-
local sideReactor = "back"
3+
 
4
--output multiplier: set by modpack (default = 1.0), change this to the value of draconic evolution config (!!!not tested yet!!!)
5
local outputMultiplier = 1.0
6
 
7
local reactornumber = "1"
8
9
local tempPinpoint = 0
10
11
--Do not change anything below
12-
local version = "1.0.2" --by electronx3
12+
 
13
--originally made by electronx3
14
--modified by jona23 to work in 1.16+
15
 
16
--constants
17
local val_1div3 = 1.0/3.0
18
 
19
--config
20
 
21-
local maxOvershoot = 200.0		--reactor will shutdown when temperature gets hotter than defaultTemp+maxOvershoot
21+
22
local maxOvershoot = 200.0      --reactor will shutdown when temperature gets hotter than defaultTemp+maxOvershoot
23
local minFuel = 0.05
24
 
25
--default parameters
26-
local defaultField = 0.01
26+
27
local defaultField = 0.09
28-
local maxTempInc = 400.0		--max. temperature increase per computer tick
28+
local restartFix = 100
29-
local maxOutflow = 16000000.0
29+
30
 
31
local maxTempInc = 400.0        --max. temperature increase per computer tick
32-
local shutDownField = 0.1		--field strength while shutdown
32+
local maxOutflow = 30000000.0
33
 
34-
local safeMode = true			--when set to false, the automatic emergency shutdown function is disabled
34+
35
local shutDownField = 0.1       --field strength while shutdown
36
 
37
local safeMode = true           --when set to false, the automatic emergency shutdown function is disabled
38
 
39
 
40
--peripherals
41
local reactor
42
local gateIn
43
local gateOut
44
local mon
45
 
46
--info
47
local info
48
 
49
local currentEmergency = false
50
local currentField
51
local currentFuel
52
 
53
local stableTicks = 0
54
local screenPage = 0
55
local openConfirmDialog = false
56
 
57
local manualStart = false
58
local manualCharge = false
59
local manualStop = false
60
 
61
 
62-
	info = reactor.getReactorInfo()
62+
63-
	
63+
64-
	if info == nil or info == null then
64+
    info = reactor.getReactorInfo()
65-
      error("Reactor has an invalid setup!")
65+
    
66
    if info == nil or info == null then
67-
	
67+
        gClear()
68-
	currentField = info.fieldStrength / info.maxFieldStrength
68+
        gWrite("System Crash!", 1, 1, colors.white, colors.blue)
69-
	currentFuel = 1.0 - (info.fuelConversion / info.maxFuelConversion)
69+
        gWrite("CODE: invalid_core_setup", 1, 2, colors.white, colors.blue)
70
      error("Reactor has an invalid setup")
71
    end
72
    
73-
	currentEmergency = safeMode and not (info.temperature < 2000.0) and (info.status == "running" or info.status == "online" or info.status == "stopping") and (info.temperature > defaultTemp + maxOvershoot or currentField < 0.004 or currentFuel < minFuel)
73+
    currentField = info.fieldStrength / info.maxFieldStrength
74-
	return currentEmergency
74+
    currentFuel = 1.0 - (info.fuelConversion / info.maxFuelConversion)
75
end
76
 
77
function isEmergency()
78-
	return fieldDrainRate / (1.0 - targetStrength)
78+
    currentEmergency = safeMode and not (info.temperature < 2000.0) and (info.status == "running" or info.status == "online" or info.status == "stopping") and (info.temperature > defaultTemp + maxOvershoot or currentField < 0.004 or currentFuel < minFuel)
79
    return currentEmergency
80
end
81
 
82
function calcInflow(targetStrength, fieldDrainRate)
83-
	print("Setup peripherals...")
83+
    return fieldDrainRate / (1.0 - targetStrength)
84-
	reactor = peripheral.wrap(sideReactor)
84+
85-
	gateIn = periphSearch("flux_gate")
85+
 
86-
	gateOut = peripheral.wrap(sideGateOut)
86+
87-
	
87+
88-
	if reactor == null then
88+
    local tmpMon = periphSearch("monitor")
89-
		error("No valid reactor was found!")
89+
    if tmpMon == null then
90-
	end
90+
        mon = null
91-
	if gateIn == null then
91+
        print("WARN: No valid monitor was found!")
92-
		error("No valid input fluxgate was found!")
92+
 
93-
	end	 
93+
    else
94-
	if gateOut == null then
94+
        monX, monY = tmpMon.getSize()
95-
		error("No valid output fluxgate was found!")
95+
        mon = {}
96-
	end
96+
        mon.monitor, mon.x, mon.y = tmpMon, monX, monY
97-
	
97+
98-
	gateIn.setOverrideEnabled(true)
98+
99-
	gateIn.setFlowOverride(0)
99+
    print("Setup peripherals...")
100-
	gateOut.setOverrideEnabled(true)
100+
    reactor = periphSearch("draconic_reactor")
101-
	gateOut.setFlowOverride(0)
101+
    gateIn = periphSearch("flow_gate")
102-
	
102+
    gateOut = peripheral.wrap(sideGateOut)
103-
	local tmpMon = periphSearch("monitor")
103+
    
104-
	if tmpMon == null then
104+
    if reactor == null then
105-
		mon = null
105+
        gClear()
106-
		print("WARN: No valid monitor was found!")
106+
        gWrite("System Crash!", 1, 1, colors.white, colors.blue)
107
        gWrite("CODE: no_valid_core_found", 1, 2, colors.white, colors.blue)
108-
	else
108+
        error("No valid reactor was found!")
109-
		monX, monY = tmpMon.getSize()
109+
110-
		mon = {}
110+
    if gateIn == null then
111-
		mon.monitor, mon.x, mon.y = tmpMon, monX, monY
111+
        gClear()
112-
	end
112+
        gWrite("System Crash!", 1, 1, colors.white, colors.blue)
113-
	
113+
        gWrite("CODE: no_valid_input_gate", 1, 2, colors.white, colors.blue)
114-
	print("Done!")
114+
        error("No valid input fluxgate was found!")
115
    end  
116
    if gateOut == null then
117
        gClear()
118-
	local names = peripheral.getNames()
118+
        gWrite("System Crash!", 1, 1, colors.white, colors.blue)
119-
	local i, name
119+
        gWrite("CODE: no_valid_output_gate", 1, 2, colors.white, colors.blue)
120-
	for i, name in pairs(names) do
120+
        error("No valid output fluxgate was found!")
121-
		if peripheral.getType(name) == type then
121+
122-
			return peripheral.wrap(name)
122+
    
123-
		end
123+
    gateIn.setOverrideEnabled(true)
124-
	end
124+
    gateIn.setFlowOverride(0)
125-
	return null
125+
    gateOut.setOverrideEnabled(true)
126
    gateOut.setFlowOverride(0)
127
    
128
    print("Done!")
129-
	if mon == null then
129+
130-
		return
130+
 
131-
	end
131+
132-
	
132+
    local names = peripheral.getNames()
133-
	while true do
133+
    local i, name
134-
		event, side, xPos, yPos = os.pullEvent("monitor_touch")
134+
    for i, name in pairs(names) do
135-
		
135+
        if peripheral.getType(name) == type then
136-
		local cFlow = 0 --remove local later
136+
            return peripheral.wrap(name)
137-
		
137+
        end
138-
		if yPos == mon.y and xPos >= 1 and xPos <= 4 then
138+
139-
			openConfirmDialog = false
139+
    return null
140-
			screenPage = (screenPage + 1) % 2
140+
141-
		elseif screenPage == 1 then			
141+
 
142-
			if yPos == 8 then
142+
143-
				local tmpTemp = defaultTemp
143+
    if mon == null then
144-
			
144+
        return
145-
				if xPos >= 2 and xPos <= 3 then
145+
146-
					tmpTemp = tmpTemp - 1.0
146+
    
147-
				elseif xPos >= 5 and xPos <= 6 then
147+
    while true do
148-
					tmpTemp = tmpTemp - 10.0
148+
        event, side, xPos, yPos = os.pullEvent("monitor_touch")
149-
				elseif xPos >= 9 and xPos <= 11 then
149+
        
150-
					tmpTemp = tmpTemp - 100.0
150+
        local cFlow = 0 --remove local later
151-
				elseif xPos >= 14 and xPos <= 16 then
151+
        
152-
					tmpTemp = 8000.0
152+
        if yPos == mon.y and xPos >= 1 and xPos <= 4 then
153-
				elseif xPos >= 19 and xPos <= 21 then
153+
            openConfirmDialog = false
154-
					tmpTemp = tmpTemp + 100.0
154+
            screenPage = (screenPage + 1) % 2
155-
				elseif xPos >= 24 and xPos <= 25 then
155+
        elseif screenPage == 1 then         
156-
					tmpTemp = tmpTemp + 10.0
156+
            if yPos == 8 then
157-
				elseif xPos >= 27 and xPos <= 28 then
157+
                local tmpTemp = defaultTemp
158-
					tmpTemp = tmpTemp + 1.0
158+
            
159-
				end
159+
                if xPos >= 2 and xPos <= 3 then
160-
				
160+
                    tmpTemp = tmpTemp - 1.0
161-
				if tmpTemp < 20.0 then
161+
                elseif xPos >= 5 and xPos <= 6 then
162-
					tmpTemp = 20.0
162+
                    tmpTemp = tmpTemp - 10.0
163-
				elseif tmpTemp > 20000.0 then
163+
                elseif xPos >= 9 and xPos <= 11 then
164-
					tmpTemp = 20000.0
164+
                    tmpTemp = tmpTemp - 100.0
165-
				end
165+
                elseif xPos >= 14 and xPos <= 16 then
166-
				
166+
                    tmpTemp = 8000.0
167-
				defaultTemp = tmpTemp
167+
                elseif xPos >= 19 and xPos <= 21 then
168-
				--print("new default temperature: " .. math.floor(defaultTemp) .. "°C")
168+
                    tmpTemp = tmpTemp + 100.0
169-
			elseif yPos == 12 then
169+
                elseif xPos >= 24 and xPos <= 25 then
170-
				local tmpField = defaultField
170+
                    tmpTemp = tmpTemp + 10.0
171-
				
171+
                elseif xPos >= 27 and xPos <= 28 then
172-
				if xPos >= 2 and xPos <= 3 then
172+
                    tmpTemp = tmpTemp + 1.0
173-
					tmpField = tmpField - 0.001
173+
                end
174-
				elseif xPos >= 5 and xPos <= 6 then
174+
                
175-
					tmpField = tmpField - 0.01
175+
                if tmpTemp < 20.0 then
176-
				elseif xPos >= 9 and xPos <= 11 then
176+
                    tmpTemp = 20.0
177-
					tmpField = tmpField - 0.1
177+
                elseif tmpTemp > 20000.0 then
178-
				elseif xPos >= 14 and xPos <= 16 then
178+
                    tmpTemp = 20000.0
179-
					tmpField = 0.01
179+
                end
180-
				elseif xPos >= 19 and xPos <= 21 then
180+
                
181-
					tmpField = tmpField + 0.1
181+
                defaultTemp = tmpTemp
182-
				elseif xPos >= 24 and xPos <= 25 then
182+
                --print("new default temperature: " .. math.floor(defaultTemp) .. "°C")
183-
					tmpField = tmpField + 0.01
183+
            elseif yPos == 12 then
184-
				elseif xPos >= 27 and xPos <= 28 then
184+
                local tmpField = defaultField
185-
					tmpField = tmpField + 0.001
185+
                
186-
				end
186+
                if xPos >= 2 and xPos <= 3 then
187-
				
187+
                    tmpField = tmpField - 0.001
188-
				if tmpField < 0.005 then
188+
                elseif xPos >= 5 and xPos <= 6 then
189-
					tmpField = 0.005
189+
                    tmpField = tmpField - 0.01
190-
				elseif tmpField > 0.6 then
190+
                elseif xPos >= 9 and xPos <= 11 then
191-
					tmpField = 0.6
191+
                    tmpField = tmpField - 0.1
192-
				end
192+
                elseif xPos >= 14 and xPos <= 16 then
193-
				
193+
                    tmpField = 0.01
194-
				defaultField = tmpField
194+
                elseif xPos >= 19 and xPos <= 21 then
195-
				--print("new default field strength: " .. math.floor(defaultField*1000.0)/10.0 .. "%")
195+
                    tmpField = tmpField + 0.1
196-
			elseif yPos == 15 then				
196+
                elseif xPos >= 24 and xPos <= 25 then
197-
				if openConfirmDialog then
197+
                    tmpField = tmpField + 0.01
198-
					if xPos >= 25 and xPos <= 28 then
198+
                elseif xPos >= 27 and xPos <= 28 then
199-
						openConfirmDialog = false
199+
                    tmpField = tmpField + 0.001
200-
					elseif xPos >= 16 and xPos <= 22 then
200+
                end
201-
						openConfirmDialog = false
201+
                
202-
						safeMode = false
202+
203-
						--print("WARN: Safe mode deactivated!")
203+
                if safeMode then
204-
					end
204+
                if tmpField < 0.005 then
205-
				elseif xPos >= 26 and xPos <= 28 then
205+
                    tmpField = 0.005
206-
					if safeMode then
206+
                elseif tmpField > 0.6 then
207-
						openConfirmDialog = true
207+
                    tmpField = 0.6
208-
					else
208+
                end
209-
						safeMode = true
209+
                else
210-
						--print("Safe mode activated!")
210+
                if tmpField < 0.0001 then
211-
					end
211+
                    tmpField = 0.0001
212-
				end
212+
                elseif tmpField > 0.6 then
213-
			elseif yPos == 17 and info ~= null then
213+
                    tmpField = 0.6
214-
				if not currentEmergency then
214+
                end
215-
					if (info.status == "running" or info.status == "online") and xPos >= 2 and xPos <= 5 then
215+
                end
216-
						manualStop = true
216+
                
217-
					elseif (info.status == "cold" or info.status == "offline" or info.status == "cooling") and xPos >= 2 and xPos <= 7 then
217+
                defaultField = tmpField
218-
						manualCharge = true
218+
                --print("new default field strength: " .. math.floor(defaultField*1000.0)/10.0 .. "%")
219-
					elseif (info.status == "stopping" or info.status == "warming_up" or info.status == "charged") and xPos >= 2 and xPos <= 6 then
219+
            elseif yPos == 15 then              
220-
						manualStart = true
220+
                if openConfirmDialog then
221-
					elseif info.status == "warming_up" and xPos >= 9 and xPos <= 12 then
221+
                    if xPos >= 25 and xPos <= 28 then
222-
						manualStop = true
222+
                        openConfirmDialog = false
223-
					end
223+
                    elseif xPos >= 16 and xPos <= 22 then
224-
				end
224+
                        openConfirmDialog = false
225-
				if info.temperature < 2000.0 and xPos >= 26 and xPos <= 28 then
225+
                        safeMode = false
226-
					print("Program stopped!")
226+
                        --print("WARN: Safe mode deactivated!")
227-
					return
227+
                    end
228-
				end
228+
                elseif xPos >= 26 and xPos <= 28 then
229-
			end
229+
                    if safeMode then
230-
		end
230+
                        openConfirmDialog = true
231-
		saveConfig()
231+
                    else
232-
	end
232+
                        safeMode = true
233
                        --print("Safe mode activated!")
234
                    end
235
                end
236
            elseif yPos == 17 and info ~= null then
237
                if not currentEmergency then
238
                    if (info.status == "running" or info.status == "online") and xPos >= 2 and xPos <= 5 then
239
                        manualStop = true
240
                    elseif (info.status == "cold" or info.status == "offline" or info.status == "cooling") and xPos >= 2 and xPos <= 7 then
241
                        manualCharge = true
242-
	mon.monitor.setCursorPos(x, y)
242+
                    elseif (info.status == "stopping" or info.status == "warming_up" or info.status == "charged") and xPos >= 2 and xPos <= 6 then
243-
	mon.monitor.setTextColor(cl)
243+
                        manualStart = true
244-
	mon.monitor.setBackgroundColor(clBg)
244+
                    elseif info.status == "warming_up" and xPos >= 9 and xPos <= 12 then
245-
	mon.monitor.write(text)
245+
                        manualStop = true
246
                    end
247
                end
248-
	gWrite(text, mon.x - string.len(tostring(text)) - x, y, cl, clBg)
248+
                if info.temperature < 2000.0 and xPos >= 26 and xPos <= 28 then
249
                    print("Program stopped!")
250
                    return
251-
	gWrite(textL, xL, y, clL, clBg)
251+
                end
252-
	gWriteR(textR, xR, y, clR, clBg)
252+
            end
253
        end
254
        saveConfig()
255-
	if length < 0 then
255+
256-
		return
256+
257-
	end
257+
 
258-
	mon.monitor.setCursorPos(x, y)
258+
259
function gClear()
260
  mon.monitor.setBackgroundColor(colors.black)
261
  mon.monitor.clear()
262
  mon.monitor.setCursorPos(1,1)
263-
	if proportion < 0.0 or proportion > 1.0 then
263+
264-
		gDrawLine(x, y, length, cl)
264+
265-
	else
265+
    mon.monitor.setCursorPos(x, y)
266-
		gDrawLine(x, y, length, clBg)
266+
    mon.monitor.setTextColor(cl)
267-
		gDrawLine(x, y, math.floor(proportion*length), cl)
267+
    mon.monitor.setBackgroundColor(clBg)
268-
	end
268+
    mon.monitor.write(text)
269
end
270
function gWriteR(text, x, y, cl, clBg)
271-
	gDrawProgressBar(2, y, mon.x-2, proportion, cl, colors.gray)
271+
    gWrite(text, mon.x - string.len(tostring(text)) - x, y, cl, clBg)
272
end
273
function gWriteLR(textL, textR, xL, xR, y, clL, clR, clBg)
274
    gWrite(textL, xL, y, clL, clBg)
275
    gWriteR(textR, xR, y, clR, clBg)
276-
	local newInflow = 0.0
276+
277-
	local newOutflow = 0.0	
277+
278-
	
278+
    if length < 0 then
279-
	while true do	
279+
        return
280-
		updateInfo()		
280+
281-
		local isStable = false
281+
    mon.monitor.setCursorPos(x, y)
282-
		local tmpShutDownField = math.max(shutDownField, defaultField)
282+
283-
	
283+
284-
		if peripheral.wrap(sideGateOut) == null then
284+
285-
			reactor.stopReactor()
285+
286-
			newInflow = calcInflow(tmpShutDownField, info.fieldDrainRate)
286+
    if proportion < 0.0 or proportion > 1.0 then
287-
			error("Output gate missing!")
287+
        gDrawLine(x, y, length, cl)
288-
		end
288+
    else
289-
		
289+
        gDrawLine(x, y, length, clBg)
290-
		if manualStop then
290+
        gDrawLine(x, y, math.floor(proportion*length), cl)
291-
			manualStart = false
291+
292-
			manualStop = false
292+
293-
			manualCharge = false
293+
294-
			reactor.stopReactor()
294+
    gDrawProgressBar(2, y, mon.x-2, proportion, cl, colors.gray)
295-
		end
295+
296-
		
296+
 
297-
		if isEmergency() == true then
297+
 
298-
			reactor.stopReactor()
298+
299-
			newInflow = calcInflow(0.8, info.fieldDrainRate)
299+
    local newInflow = 0.0
300-
			newOutflow = 0.0
300+
    local newOutflow = 0.0  
301-
			manualStart = false
301+
    
302-
			manualCharge = false
302+
    while true do   
303-
		elseif info.status == "cold" or info.status == "offline" or info.status == "cooling" then
303+
        updateInfo()        
304-
			newInflow = 0.0
304+
        local isStable = true
305-
			newOutflow = 0.0
305+
        local tmpShutDownField = math.max(shutDownField, defaultField)
306-
			if manualCharge then
306+
    
307-
				manualStart = false
307+
        if peripheral.wrap(sideGateOut) == null then
308-
				manualCharge = false
308+
            reactor.stopReactor()
309-
				reactor.chargeReactor()
309+
            newInflow = calcInflow(tmpShutDownField, info.fieldDrainRate)
310-
			end
310+
            gClear()
311-
		elseif info.status == "charging" then
311+
            gWrite("System Crash!", 1, 1, colors.white, colors.blue)
312-
			newInflow = chargeInflow
312+
            gWrite("CODE: output_gate_missing", 1, 2, colors.white, colors.blue)
313-
			newOutflow = 0.0
313+
            error("Output gate missing!")
314-
			manualStart = false
314+
        end
315-
			manualCharge = false
315+
        
316-
		elseif info.status == "warming_up" or info.status == "charged" then
316+
        if manualStop then
317-
			newInflow = chargeInflow
317+
            manualStart = false
318-
			newOutflow = 0.0
318+
            manualStop = false
319-
			if manualStart then
319+
            manualCharge = false
320-
				manualStart = false
320+
            reactor.stopReactor()
321-
				manualCharge = false
321+
        end
322-
				reactor.activateReactor()
322+
        
323-
			end
323+
        if isEmergency() == true then
324-
		elseif info.status == "running" or info.status == "online" then
324+
            reactor.stopReactor()
325-
			manualStart = false
325+
            newInflow = calcInflow(0.8, info.fieldDrainRate)
326-
			manualCharge = false
326+
            newOutflow = 0.0
327-
			local temp = info.temperature
327+
            manualStart = false
328-
			
328+
            manualCharge = false
329-
			if temp > defaultTemp - 6.0 and temp < defaultTemp + 5.0 then
329+
        elseif info.status == "cold" or info.status == "offline" or info.status == "cooling" then
330-
				stableTicks = stableTicks + 1
330+
            newInflow = 0.0
331-
				if stableTicks > 100 then
331+
            newOutflow = 0.0
332-
					isStable = true
332+
            if manualCharge then
333-
				end
333+
                manualStart = false
334-
			else
334+
                manualCharge = false
335-
				stableTicks = 0
335+
                reactor.chargeReactor()
336-
			end
336+
            end
337-
		
337+
        elseif info.status == "charging" then
338-
			if temp < defaultTemp then				
338+
            newInflow = chargeInflow
339-
				local tempInc
339+
            newOutflow = 0.0
340-
				if temp < defaultTemp - 50.0 then
340+
            manualStart = false
341-
					tempInc = math.sqrt(defaultTemp - temp)
341+
            manualCharge = false
342-
				elseif temp < defaultTemp - 0.5 then
342+
        elseif info.status == "warming_up" or info.status == "charged" then
343-
					tempInc = math.sqrt(defaultTemp - temp)/2.0
343+
            newInflow = chargeInflow
344-
				end
344+
            newOutflow = 0.0
345-
			
345+
            if manualStart then
346-
				if tempInc == nil or tempInc < 0.0 then
346+
                manualStart = false
347-
					tempInc = 0
347+
                manualCharge = false
348-
				elseif tempInc > maxTempInc then
348+
                reactor.activateReactor()
349-
					tempInc = maxTempInc
349+
            end
350-
				end
350+
        elseif info.status == "running" or info.status == "online" then
351-
				
351+
            manualStart = false
352-
				
352+
            manualCharge = false
353-
				local t50 = temp/200.0
353+
            local temp = info.temperature
354-
				local convLvl = (info.fuelConversion / info.maxFuelConversion)*1.3 - 0.3
354+
            
355-
				
355+
            if defaultTemp < info.temperature then
356-
				local y = (t50^4)/(100.0-t50)*(1-convLvl) + 1000.0*(tempInc-convLvl) - 444.7
356+
            if tempPinpoint < 10 then
357-
				local dSqrt = math.sqrt((50.0*y)^2 + (y/3.0)^3)
357+
            tempPinpoint = tempPinpoint - 0.01
358-
				
358+
            end
359-
				local x				
359+
            end
360-
				local tmpValRoot = dSqrt - 50.0*y
360+
361-
				if tmpValRoot > 0.0 then
361+
            if defaultTemp > info.temperature then
362-
					x = math.pow(dSqrt + 50.0*y, val_1div3) - math.pow(tmpValRoot, val_1div3)
362+
            if tempPinpoint > -10 then
363-
				else
363+
            tempPinpoint = tempPinpoint + 0.01
364-
					x = math.pow(dSqrt + 50.0*y, val_1div3) + math.pow(0.0-tmpValRoot, val_1div3)
364+
            end
365-
				end
365+
            end
366-
				
366+
            
367-
				newOutflow = info.maxEnergySaturation*x/99.0 + info.energySaturation - info.maxEnergySaturation
367+
            defaultTemp = defaultTemp + tempPinpoint
368-
				if newOutflow > maxOutflow then
368+
369-
					newOutflow = maxOutflow
369+
            if temp > defaultTemp - 6.0 and temp < defaultTemp + 5.0 then
370-
				end
370+
                stableTicks = stableTicks + 1
371-
			else
371+
                if stableTicks > 100 then
372-
				newOutflow = 0.0
372+
                    isStable = true
373-
			end
373+
                end
374-
		
374+
            else
375-
			if isStable == true then
375+
                stableTicks = 0
376-
				if currentField > defaultField + 0.05 then
376+
            end
377-
					newInflow = 0.0
377+
        
378-
				elseif currentField > defaultField*1.2 then
378+
            if temp < defaultTemp then              
379-
					newInflow = calcInflow(defaultField*0.98, info.fieldDrainRate)
379+
                local tempInc
380-
				elseif currentField > defaultField*0.97 then
380+
                if temp < defaultTemp - 50.0 then
381-
					newInflow = calcInflow(defaultField, info.fieldDrainRate)
381+
                    tempInc = math.sqrt(defaultTemp - temp)
382-
				else
382+
                elseif temp < defaultTemp - 0.5 then
383-
					newInflow = calcInflow(defaultField*1.5, info.fieldDrainRate)
383+
                    tempInc = math.sqrt(defaultTemp - temp)/2.0
384-
				end
384+
                end
385-
			else
385+
            
386-
				if currentField > tmpShutDownField then
386+
                if tempInc == nil or tempInc < 0.0 then
387-
					newInflow = calcInflow(tmpShutDownField, info.fieldDrainRate)
387+
                    tempInc = 0
388-
				else
388+
                elseif tempInc > maxTempInc then
389-
					newInflow = calcInflow(tmpShutDownField*1.2, info.fieldDrainRate)
389+
                    tempInc = maxTempInc
390-
				end
390+
                end
391-
			end
391+
392-
		elseif info.status == "stopping" then
392+
                
393-
			if currentField > tmpShutDownField then
393+
                local t50 = temp/200.0
394-
				newInflow = calcInflow(tmpShutDownField, info.fieldDrainRate)
394+
                local convLvl = (info.fuelConversion / info.maxFuelConversion)*1.3 - 0.3
395-
			else
395+
                
396-
				newInflow = calcInflow(tmpShutDownField*1.2, info.fieldDrainRate)
396+
                local y = (t50^4)/(100.0-t50)*(1-convLvl) + 1000.0*(tempInc-convLvl) - 444.7
397-
			end
397+
                local dSqrt = math.sqrt((50.0*y)^2 + (y/3.0)^3)
398-
			newOutflow = 0.0
398+
                
399-
			
399+
                local x             
400-
			if manualStart then
400+
                local tmpValRoot = dSqrt - 50.0*y
401-
				manualStart = false
401+
                if tmpValRoot > 0.0 then
402-
				manualCharge = false
402+
                    x = math.pow(dSqrt + 50.0*y, val_1div3) - math.pow(tmpValRoot, val_1div3)
403-
				reactor.activateReactor()
403+
                else
404-
			end
404+
                    x = math.pow(dSqrt + 50.0*y, val_1div3) + math.pow(0.0-tmpValRoot, val_1div3)
405-
		end
405+
                end
406-
		
406+
                
407-
		if newInflow < 0.0 then
407+
                newOutflow = info.maxEnergySaturation*x/99.0 + info.energySaturation - info.maxEnergySaturation
408-
			newInflow = 0.0
408+
                if newOutflow > maxOutflow then
409-
		end
409+
                    newOutflow = maxOutflow
410-
		if newOutflow < 0.0 then
410+
                end
411-
			newOutflow = 0.0
411+
            else
412-
		end
412+
                newOutflow = 0.0
413-
		
413+
            end
414-
		if newInflow > 0.0 then
414+
        
415-
			newInflow = math.floor(newInflow)
415+
            if isStable == true then
416-
		else
416+
                if currentField > defaultField + 0.05 then
417-
			newInflow = 0
417+
                    newInflow = 0.0
418-
		end
418+
                elseif currentField > defaultField*1.2 then
419-
		local outflowMultiplied = 0
419+
                    newInflow = calcInflow(defaultField*0.98, info.fieldDrainRate)
420-
		if newOutflow > 0.0 then
420+
                elseif currentField > defaultField*0.97 then
421-
			outflowMultiplied = math.floor(newOutflow*outputMultiplier)
421+
                    newInflow = calcInflow(defaultField, info.fieldDrainRate)
422-
		else
422+
                else
423-
			newOutflow = 0.0
423+
                    newInflow = calcInflow(defaultField*1.5, info.fieldDrainRate)
424-
		end
424+
                end
425-
		gateIn.setFlowOverride(newInflow)
425+
            else
426-
		gateOut.setFlowOverride(outflowMultiplied)
426+
                if currentField > tmpShutDownField then
427-
		
427+
                    newInflow = calcInflow(tmpShutDownField, info.fieldDrainRate)
428-
		
428+
                else
429-
		if mon ~= null then
429+
                    newInflow = calcInflow(tmpShutDownField*1.2, info.fieldDrainRate)
430-
			gClear()
430+
                end
431-
			
431+
            end
432-
			if screenPage == 0 then
432+
            defaultTemp = defaultTemp - tempPinpoint
433-
				local clTemp
433+
434-
				if info.temperature < defaultTemp*0.95 then
434+
        elseif info.status == "stopping" then
435-
					clTemp = colors.green
435+
            if currentField > tmpShutDownField then
436-
				elseif info.temperature < defaultTemp + 1.0 then
436+
                newInflow = calcInflow(tmpShutDownField, info.fieldDrainRate)
437-
					clTemp = colors.yellow
437+
            else
438-
				elseif info.temperature < defaultTemp + maxOvershoot then
438+
                newInflow = calcInflow(tmpShutDownField*1.2, info.fieldDrainRate)
439-
					clTemp = colors.orange
439+
            end
440-
				else
440+
            newOutflow = 0.0
441-
					clTemp = colors.red
441+
            
442-
				end
442+
            if manualStart then
443-
				
443+
                manualStart = false
444-
				gWriteLR("temperature:", math.floor(info.temperature*10.0)/10.0 .. "°C", 2, 0, 7, colors.white, clTemp, colors.black)
444+
                manualCharge = false
445-
				gWriteLR("set: " .. math.floor(defaultTemp) .. "°C", "max: " .. math.floor(defaultTemp + maxOvershoot) .. "°C", 2, 0, 8, colors.white, colors.white, colors.black)
445+
                reactor.activateReactor()
446-
				gDrawStandardProgressBar(9, info.temperature/16000.0, clTemp)				
446+
            end
447-
				
447+
        end
448-
				local clField
448+
        
449-
				if currentField > defaultField*1.05 then
449+
        if newInflow < 0.0 then
450-
					clField = colors.green
450+
            newInflow = 0.0
451-
				elseif currentField > defaultField*0.95 then
451+
        end
452-
					clField = colors.yellow
452+
        if newOutflow < 0.0 then
453-
				else
453+
            newOutflow = 0.0
454-
					clField = colors.red
454+
        end
455-
				end
455+
        
456-
				
456+
        if newInflow > 0.0 then
457-
				gWriteLR("field strength:", math.floor(currentField*10000.0)/100.0 .. "%", 2, 0, 11, colors.white, clField, colors.black)
457+
            newInflow = math.floor(newInflow)
458-
				gWriteLR("set: " .. math.floor(defaultField*1000)/10.0 .. "%", "min: 0.4%", 2, 0, 12, colors.white, colors.white, colors.black)
458+
        else
459-
				gDrawStandardProgressBar(13, currentField, clField)
459+
            newInflow = 0
460-
				
460+
        end
461-
				gWriteLR("status", info.status:upper(), 2, 0, 15, colors.white, colors.white, colors.black)
461+
        local outflowMultiplied = 0
462-
				
462+
        if newOutflow > 0.0 then
463-
				local clFuel
463+
            outflowMultiplied = math.floor(newOutflow*outputMultiplier)
464-
				if currentFuel > 0.15 then
464+
        else
465-
					clFuel = colors.green
465+
            newOutflow = 0.0
466-
				elseif currentFuel > 0.05 then
466+
        end
467-
					clFuel = colors.yellow
467+
        if restartFix > 0 then
468-
				elseif currentFuel > 0.01 then
468+
            outflowMultiplied = 0.0
469-
					clFuel = colors.orange
469+
            newInflow = 10000000.0
470-
				else
470+
            restartFix = restartFix - 1.0
471-
					clFuel = colors.red
471+
        end
472-
				end
472+
        gateIn.setFlowOverride(newInflow)
473-
				gWriteLR("fuel:", math.floor(currentFuel*10000.0)/100.0 .. "%", 2, 0, 16, colors.white, clFuel, colors.black)
473+
        gateOut.setFlowOverride(outflowMultiplied)
474-
			elseif screenPage == 1 then
474+
        
475-
				local clTemp
475+
        
476-
				if info.temperature < defaultTemp*0.95 then
476+
        if mon ~= null then
477-
					clTemp = colors.green
477+
            gClear()
478-
				elseif info.temperature < defaultTemp + 1.0 then
478+
            
479-
					clTemp = colors.yellow
479+
            if screenPage == 0 then
480-
				elseif info.temperature < defaultTemp + maxOvershoot then
480+
                local clTemp
481-
					clTemp = colors.orange
481+
                if info.temperature < defaultTemp*0.95 then
482-
				else
482+
                    clTemp = colors.green
483-
					clTemp = colors.red
483+
                elseif info.temperature < defaultTemp + 1.0 then
484-
				end
484+
                    clTemp = colors.yellow
485-
				
485+
                elseif info.temperature < defaultTemp + maxOvershoot then
486-
				gWriteLR("temp: " .. math.floor(defaultTemp) .. " (" .. math.floor(defaultTemp + maxOvershoot) .. ")", math.floor(info.temperature*10.0)/10.0 .. "°C", 2, 0, 7, colors.white, clTemp, colors.black)
486+
                    clTemp = colors.orange
487-
				gDrawStandardProgressBar(9, info.temperature/16000.0, clTemp)
487+
                else
488-
				
488+
                    clTemp = colors.red
489-
				gWrite("<", 2, 8, colors.white, colors.blue)
489+
                end
490-
				gWrite("<<", 5, 8, colors.white, colors.blue)
490+
                
491-
				gWrite("<<<", 9, 8, colors.white, colors.blue)
491+
                gWriteLR("temperature:", math.floor(info.temperature*10.0)/10.0 .. "°C", 2, 0, 7, colors.white, clTemp, colors.black)
492-
				gWrite("res", 14, 8, colors.white, colors.blue)
492+
                gWriteLR("set: " .. math.floor(defaultTemp) .. "°C", "max: " .. math.floor(defaultTemp + maxOvershoot) .. "°C", 2, 0, 8, colors.white, colors.white, colors.black)
493-
				gWrite(">>>", 19, 8, colors.white, colors.blue)
493+
                gDrawStandardProgressBar(9, info.temperature/16000.0, clTemp)               
494-
				gWrite(">>", 24, 8, colors.white, colors.blue)
494+
                
495-
				gWrite(">", 28, 8, colors.white, colors.blue)				
495+
                local clField
496-
				
496+
                if currentField > defaultField*1.05 then
497-
				local clField
497+
                    clField = colors.green
498-
				if currentField > defaultField*1.05 then
498+
                elseif currentField > defaultField*0.95 then
499-
					clField = colors.green
499+
                    clField = colors.yellow
500-
				elseif currentField > defaultField*0.95 then
500+
                else
501-
					clField = colors.yellow
501+
                    clField = colors.red
502-
				else
502+
                end
503-
					clField = colors.red
503+
                
504-
				end
504+
                gWriteLR("field strength:", math.floor(currentField*10000.0)/100.0 .. "%", 2, 0, 11, colors.white, clField, colors.black)
505-
				
505+
                gWriteLR("set: " .. math.floor(defaultField*1000)/10.0 .. "%", "min: 0.4%", 2, 0, 12, colors.white, colors.white, colors.black)
506-
				gWriteLR("field: " .. math.floor(defaultField*1000)/10.0 .. "% (0.4%)", math.floor(currentField*10000.0)/100.0 .. "%", 2, 0, 11, colors.white, clField, colors.black)
506+
                gDrawStandardProgressBar(13, currentField, clField)
507-
				gDrawStandardProgressBar(13, currentField, clField)
507+
                
508-
				
508+
                gWriteLR("status", info.status:upper(), 2, 0, 15, colors.white, colors.white, colors.black)
509-
				gWrite("<", 2, 12, colors.white, colors.blue)
509+
                
510-
				gWrite("<<", 5, 12, colors.white, colors.blue)
510+
                local clFuel
511-
				gWrite("<<<", 9, 12, colors.white, colors.blue)
511+
                if currentFuel > 0.15 then
512-
				gWrite("res", 14, 12, colors.white, colors.blue)
512+
                    clFuel = colors.green
513-
				gWrite(">>>", 19, 12, colors.white, colors.blue)
513+
                elseif currentFuel > 0.05 then
514-
				gWrite(">>", 24, 12, colors.white, colors.blue)
514+
                    clFuel = colors.yellow
515-
				gWrite(">", 28, 12, colors.white, colors.blue)
515+
                elseif currentFuel > 0.01 then
516-
				
516+
                    clFuel = colors.orange
517-
				if safeMode then
517+
                else
518-
					if openConfirmDialog then
518+
                    clFuel = colors.red
519-
						gWrite("DISABLE?", 2, 15, colors.white, colors.black)
519+
                end
520-
						gWrite("CONFIRM", 16, 15, colors.white, colors.green)
520+
                gWriteLR("fuel:", math.floor(currentFuel*10000.0)/100.0 .. "%", 2, 0, 16, colors.white, clFuel, colors.black)
521-
						gWrite("EXIT", 25, 15, colors.white, colors.red)
521+
            elseif screenPage == 1 then
522-
					else
522+
                local clTemp
523-
						gWrite("safe mode:", 2, 15, colors.white, colors.black)
523+
                if info.temperature < defaultTemp*0.95 then
524-
						gWrite("ON", 27, 15, colors.green, colors.black)
524+
                    clTemp = colors.green
525-
					end
525+
                elseif info.temperature < defaultTemp + 1.0 then
526-
				else
526+
                    clTemp = colors.yellow
527-
					gWrite("safe mode:", 2, 15, colors.white, colors.black)
527+
                elseif info.temperature < defaultTemp + maxOvershoot then
528-
					gWrite("OFF", 26, 15, colors.red, colors.black)
528+
                    clTemp = colors.orange
529-
				end
529+
                else
530-
				
530+
                    clTemp = colors.red
531-
				if not currentEmergency then
531+
                end
532-
					if info.status == "running" or info.status == "online" then
532+
                
533-
						gWrite("STOP", 2, 17, colors.white, colors.red)
533+
                gWriteLR("temp: " .. math.floor(defaultTemp) .. " (" .. math.floor(defaultTemp + maxOvershoot) .. ")", math.floor(info.temperature*10.0)/10.0 .. "°C", 2, 0, 7, colors.white, clTemp, colors.black)
534-
					elseif info.status == "cold" or info.status == "offline" or info.status == "cooling" then
534+
                gDrawStandardProgressBar(9, info.temperature/16000.0, clTemp)
535-
						gWrite("CHARGE", 2, 17, colors.white, colors.blue)
535+
                
536-
					elseif info.status == "stopping" or info.status == "charged" then
536+
                gWrite("<", 2, 8, colors.white, colors.blue)
537-
						gWrite("START", 2, 17, colors.white, colors.green)
537+
                gWrite("<<", 5, 8, colors.white, colors.blue)
538-
					elseif info.status == "warming_up" then
538+
                gWrite("<<<", 9, 8, colors.white, colors.blue)
539-
						gWrite("START", 2, 17, colors.white, colors.green)
539+
                gWrite("res", 14, 8, colors.white, colors.blue)
540-
						gWrite("STOP", 9, 17, colors.white, colors.red)
540+
                gWrite(">>>", 19, 8, colors.white, colors.blue)
541-
					end
541+
                gWrite(">>", 24, 8, colors.white, colors.blue)
542-
				end
542+
                gWrite(">", 28, 8, colors.white, colors.blue)               
543-
				
543+
                
544-
				if info.temperature < 2000.0 then
544+
                local clField
545-
					gWrite("END", 26, 17, colors.white, colors.red)
545+
                if currentField > defaultField*1.05 then
546-
				end
546+
                    clField = colors.green
547-
			end
547+
                elseif currentField > defaultField*0.95 then
548-
			
548+
                    clField = colors.yellow
549-
			gWriteLR("Draconic Control", "v" .. version, 2, 0, 1, colors.white, colors.white, colors.black)			
549+
                else
550-
			
550+
                    clField = colors.red
551-
			gWriteLR("inflow:", newInflow .. " RF/t", 2, 0, 3, colors.white, colors.white, colors.black)
551+
                end
552-
			gWriteLR("outflow:", outflowMultiplied .. " RF/t", 2, 0, 4, colors.white, colors.white, colors.black)		
552+
                
553-
			local gain = outflowMultiplied - newInflow
553+
                gWriteLR("field: " .. math.floor(defaultField*1000)/10.0 .. "% (0.4%)", math.floor(currentField*10000.0)/100.0 .. "%", 2, 0, 11, colors.white, clField, colors.black)
554-
			if gain > 0.0 then
554+
                gDrawStandardProgressBar(13, currentField, clField)
555-
				gWriteLR("-> gain:", gain .. " RF/t", 2, 0, 5, colors.white, colors.green, colors.black)
555+
                
556-
			elseif gain < 0.0 then
556+
                gWrite("<", 2, 12, colors.white, colors.blue)
557-
				gWriteLR("-> gain:", gain .. " RF/t", 2, 0, 5, colors.white, colors.red, colors.black)
557+
                gWrite("<<", 5, 12, colors.white, colors.blue)
558-
			elseif gain == 0.0 then
558+
                gWrite("<<<", 9, 12, colors.white, colors.blue)
559-
				gWriteLR("-> gain:", "0 RF/t", 2, 0, 5, colors.white, colors.white, colors.black)
559+
                gWrite("res", 14, 12, colors.white, colors.blue)
560-
			end
560+
                gWrite(">>>", 19, 12, colors.white, colors.blue)
561-
			
561+
                gWrite(">>", 24, 12, colors.white, colors.blue)
562-
			gDrawLine(1, mon.y, 4, colors.gray)
562+
                gWrite(">", 28, 12, colors.white, colors.blue)
563-
		end
563+
                
564-
		
564+
                if safeMode then
565-
		sleep(0.02)
565+
                    if openConfirmDialog then
566-
	end
566+
                        gWrite("DISABLE?", 2, 15, colors.white, colors.black)
567
                        gWrite("CONFIRM", 16, 15, colors.white, colors.green)
568
                        gWrite("EXIT", 25, 15, colors.white, colors.red)
569
                    else
570-
	term.clear()
570+
                        gWrite("safe mode:", 2, 15, colors.white, colors.black)
571-
	print("Starting program...")	
571+
                        gWrite("ON", 27, 15, colors.green, colors.black)
572-
	
572+
                    end
573-
	if fs.exists("config.txt") then
573+
                else
574-
		print("Loading config...")
574+
                    gWrite("safe mode:", 2, 15, colors.white, colors.black)
575-
		loadConfig()
575+
                    gWrite("OFF", 26, 15, colors.red, colors.black)
576-
		print("Done!")
576+
                end
577-
	else
577+
                
578-
		print("Creating config...")
578+
                if not currentEmergency then
579-
		saveConfig()
579+
                    if info.status == "running" or info.status == "online" then
580-
		print("Done!")
580+
                        gWrite("STOP", 2, 17, colors.white, colors.red)
581-
	end
581+
                    elseif info.status == "cold" or info.status == "offline" or info.status == "cooling" then
582
                        gWrite("CHARGE", 2, 17, colors.white, colors.blue)
583-
	if chargeInflow < 0 then
583+
                    elseif info.status == "stopping" or info.status == "charged" then
584-
		chargeInflow = 0
584+
                        gWrite("START", 2, 17, colors.white, colors.green)
585-
	end
585+
                    elseif info.status == "warming_up" then
586-
	
586+
                        gWrite("START", 2, 17, colors.white, colors.green)
587-
	stableTicks = 0
587+
                        gWrite("STOP", 9, 17, colors.white, colors.red)
588-
	
588+
                    end
589-
	setupPeripherals()
589+
                end
590-
	print("Started!")
590+
                
591
                if info.temperature < 2000.0 then
592
                    gWrite("END", 26, 17, colors.white, colors.red)
593
                end
594-
	local config = fs.open("config.txt", "w")
594+
            end
595-
	config.writeLine(outputMultiplier*1000000.0)
595+
            
596-
	config.writeLine(maxOvershoot)
596+
            gWriteLR("Draconic Reactor Number", reactornumber, 2, 0, 1, colors.white, colors.white, colors.black)         
597-
	config.writeLine(minFuel*100.0)
597+
            
598-
	config.writeLine(defaultTemp)
598+
            gWriteLR("inflow:", newInflow .. " RF/t", 2, 0, 3, colors.white, colors.white, colors.black)
599-
	config.writeLine(defaultField*1000.0)
599+
            gWriteLR("outflow:", outflowMultiplied .. " RF/t", 2, 0, 4, colors.white, colors.white, colors.black)       
600-
	config.writeLine(maxTempInc)
600+
            local gain = outflowMultiplied - newInflow
601-
	config.writeLine(maxOutflow)
601+
            if gain > 0.0 then
602-
	config.writeLine(chargeInflow)
602+
                gWriteLR("-> gain:", gain .. " RF/t", 2, 0, 5, colors.white, colors.green, colors.black)
603-
	config.writeLine(shutDownField*1000.0)
603+
            elseif gain < 0.0 then
604-
	config.close()
604+
                gWriteLR("-> gain:", gain .. " RF/t", 2, 0, 5, colors.white, colors.red, colors.black)
605
            elseif gain == 0.0 then
606
                gWriteLR("-> gain:", "0 RF/t", 2, 0, 5, colors.white, colors.white, colors.black)
607-
	local config = fs.open("config.txt", "r")
607+
            end
608-
	outputMultiplier = tonumber(config.readLine())/1000000.0
608+
            
609-
	maxOvershoot = tonumber(config.readLine())
609+
            gDrawLine(1, mon.y, 4, colors.gray)
610-
	minFuel = tonumber(config.readLine())/100.0
610+
        end
611-
	defaultTemp = tonumber(config.readLine())
611+
        
612-
	defaultField = tonumber(config.readLine())/1000.0
612+
        sleep(0.02)
613-
	maxTempInc = tonumber(config.readLine())
613+
614-
	maxOutflow = tonumber(config.readLine())
614+
615-
	chargeInflow = tonumber(config.readLine())
615+
 
616-
	shutDownField = tonumber(config.readLine())/1000.0
616+
617-
	config.close()
617+
    term.clear()
618
    print("Starting program...")    
619
    
620
    if fs.exists("config.txt") then
621-
	setup()
621+
        print("Loading config...")
622-
	if mon == null then
622+
        loadConfig()
623-
		update()
623+
        print("Done!")
624-
	else
624+
    else
625-
		parallel.waitForAny(update, clickListener)
625+
        print("Creating config...")
626-
		gClear()
626+
        saveConfig()
627-
		gWrite("Program stopped!", 1, 1, colors.white, colors.black)
627+
        print("Done!")
628-
	end
628+
629
 
630
    if chargeInflow < 0 then
631
        chargeInflow = 0
632
    end
633
    
634
    stableTicks = 0
635
    
636
    setupPeripherals()
637
    print("Started!")
638
end
639
function loadConfig()
640
    local config = fs.open("config.txt", "r")
641
    outputMultiplier = tonumber(config.readLine())/1000000.0
642
    maxOvershoot = tonumber(config.readLine())
643
    minFuel = tonumber(config.readLine())/100.0
644
    defaultTemp = tonumber(config.readLine())
645
    defaultField = tonumber(config.readLine())/1000.0
646
    maxTempInc = tonumber(config.readLine())
647
    maxOutflow = tonumber(config.readLine())
648
    chargeInflow = tonumber(config.readLine())
649
    shutDownField = tonumber(config.readLine())/1000.0
650
    config.close()
651
end
652
function saveConfig()
653
    local config = fs.open("config.txt", "w")
654
    config.writeLine(outputMultiplier*1000000.0)
655
    config.writeLine(maxOvershoot)
656
    config.writeLine(minFuel*100.0)
657
    config.writeLine(defaultTemp)
658
    config.writeLine(defaultField*1000.0)
659
    config.writeLine(maxTempInc)
660
    config.writeLine(maxOutflow)
661
    config.writeLine(chargeInflow)
662
    config.writeLine(shutDownField*1000.0)
663
    config.close()
664
end
665
 
666
function main()
667
    setup()
668
    if mon == null then
669
        update()
670
    else
671
        parallel.waitForAny(update, clickListener)
672
        gClear()
673
        gWrite("Program stopped!", 1, 1, colors.white, colors.black)
674
    end
675
end
676
 
677
main()