Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- p = peripheral.wrap("back")
- hasMonitor = peripheral.isPresent("top")
- m = nil
- if hasMonitor then
- m = peripheral.wrap("top")
- m.setTextScale(1)
- end
- function waitReactor()
- a = p.getEnergyProducedLastTick()
- sleep(2)
- b = p.getEnergyProducedLastTick()
- while (math.abs(1-a/b) > 0.01) do
- a = p.getEnergyProducedLastTick()
- sleep(2)
- end
- end
- function calibrate()
- calibration = {}
- calibration["e"] = {}
- maxEnergy = 0
- maxEff = 0
- for i=1,20 do
- print("Calibrating... " .. i .. "/20")
- l = 100-5*i
- p.setAllControlRodLevels(l)
- waitReactor()
- e = p.getEnergyProducedLastTick()
- c = p.getFuelConsumedLastTick()
- if (e > maxEnergy) then
- maxEnergy = e
- end
- if (e/c > maxEff) then
- maxEff = e/c
- end
- calibration["e"][i] = e
- end
- print("Calibration completed!")
- print("Max Output: " .. maxEnergy .. " RF/t")
- print("Max Value: " .. maxEff .. " RF/b")
- calibration["maxEnergy"] = maxEnergy
- calibration["maxEff"] = maxEff
- f = fs.open("calibration", "w")
- f.write(textutils.serialize(calibration))
- f.close()
- end
- function getCTL(energy)
- if energy == 0 then
- return 0
- elseif energy >= maxEnergy then
- return 100
- end
- res=100
- for i=0,19 do
- if i == 0 then
- a = 0
- else
- a = calibration.e[i]
- end
- b = calibration.e[i+1]
- if a <= energy and energy <= b then
- slope = 5/(b-a)
- res = slope*(energy-a)+i*5
- break
- elseif b <= energy and energy <= a then
- slope = 5/(a-b)
- res = slope*(energy-b)+i*5
- break
- end
- end
- return math.ceil(res)
- end
- if not fs.exists("calibration") then
- calibrate()
- end
- f = fs.open("calibration", "r")
- calibration = textutils.unserialize(f.readAll())
- f.close()
- level = 100
- eMax = 10000000
- eTargetPct = 66
- eTarget = eMax*eTargetPct/100
- delay = 30
- ticks = delay * 20
- p.setActive(true)
- maxEff = calibration["maxEff"]
- maxEnergy = calibration["maxEnergy"]
- p.setAllControlRodLevels(0)
- waitReactor()
- prod = p.getEnergyProducedLastTick()
- if (math.abs(1-prod/calibration.e[20]) > 0.1) then
- print("Reactor needs calibration.")
- calibrate()
- end
- while true do
- p.setAllControlRodLevels(100-level)
- s1 = p.getEnergyStored()
- for i=1,delay do
- sleep(1)
- bPct = math.floor(p.getEnergyStored()*100/eMax)
- if hasMonitor then
- m.setCursorPos(1,1)
- m.write("B " .. bPct .. "% ")
- end
- if (math.abs(bPct-eTargetPct) > 3) then
- ticks = i*20
- break
- end
- end
- s2 = p.getEnergyStored()
- prod = p.getEnergyProducedLastTick()
- net = (s2-s1)/ticks-prod
- eBattPwr = eTarget-p.getEnergyStored()
- eOutPwr = 0-net*ticks
- if (eOutPwr < 0) then
- eOutPwr = 0
- end
- eRequested = eBattPwr + eOutPwr
- if (eRequested < 0) then
- eRequested = 0
- end
- eReqPerTick = eRequested/ticks
- level = getCTL(eReqPerTick)
- if (level > 100) then
- level = 100
- end
- bLevel = math.floor(s2*100/eMax)
- gLevel = math.ceil(-net*100/maxEnergy)
- fuelConsumed = p.getFuelConsumedLastTick()
- eff = math.floor(prod/fuelConsumed)
- if fuelConsumed == 0 then
- eff = 0
- end
- eLevel = math.floor(eff*100/maxEff)
- burnPerHour = math.ceil(fuelConsumed*20*36000)/10
- if (gLevel < 0) then
- gLevel = 0
- end
- print("Battery: " .. bLevel .. "%")
- print("Load: " .. gLevel .. "% (" .. math.ceil(-net) .. " RF/t)")
- print("Generation: " .. level .. "% (" .. math.floor(prod) .. " RF/t)")
- print("Eff: " .. eLevel .. "% (" .. eff .. " RF/b)")
- print("Burn: " .. burnPerHour .. " b/Hr")
- print()
- if hasMonitor then
- m.clear()
- m.setCursorPos(1,1)
- m.write("B " .. bLevel .. "%")
- m.setCursorPos(1,2)
- m.write("L " .. gLevel .. "%")
- m.setCursorPos(1,3)
- m.write("G " .. level .. "%")
- m.setCursorPos(1,4)
- m.write("E " .. eLevel .. "%")
- m.setCursorPos(1,5)
- m.write("D " .. burnPerHour)
- end
- ticks = delay*20
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement