Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Basic reactor monitor script for Computercraft
- energyInputAddr = "flow_gate_4" -- Address of injection flux gate
- energyOutputAddr = "flow_gate_3" -- Address of output flux gate
- -- Control
- fieldTarget = 40
- fieldOverfillLimit = 5
- fieldIncreaseMultiplier = 17000
- fieldDecreaseAbsolute = 10000
- injectionRate = 1000000
- column2 = 9
- column3 = 26
- -- !!! DO NOT ALTER BELOW THIS LINE !!! --
- function getPeripheral(type)
- local periNames = peripheral.getNames()
- for i, val in ipairs(periNames) do
- if peripheral.getType(val) == type then
- return peripheral.wrap(val)
- end
- end
- end
- function updateRates()
- i = reac.getReactorInfo()
- fieldDeltaT = i.fieldStrength / i.maxFieldStrength * 100 - currentField
- tempDeltaT = i.temperature - currentTemp
- satDeltaT = i.energySaturation / i.maxEnergySaturation * 100 - currentSat
- fuelDeltaT = (i.maxFuelConversion - i.fuelConversion) / i.maxFuelConversion * 100 - currentFuel
- currentField = i.fieldStrength / i.maxFieldStrength * 100
- currentTemp = i.temperature
- currentSat = i.energySaturation / i.maxEnergySaturation * 100
- currentFuel = (i.maxFuelConversion - i.fuelConversion) / i.maxFuelConversion * 100
- end
- function perTickWrapper()
- while true do
- updateRates()
- updateInjectionRate()
- sleep(0.05)
- end
- end
- function fmtVal(val)
- valStr = string.format("%.4f", val)
- if string.sub(valStr, 1, 1) == "-" then
- return valStr
- else
- return "+" .. valStr
- end
- end
- function writeEmtpyLine(idx)
- mon.setCursorPos(1, idx)
- mon.write(string.rep(" ", monX))
- end
- function writeInfoWRate(idx, label, val, valRate, unit)
- useSeconds = true
- writeEmtpyLine(idx)
- mon.setCursorPos(1, idx)
- mon.write(label)
- mon.setCursorPos(column2, idx)
- mon.write(fmtVal(val) .. " " .. unit)
- mon.setCursorPos(column3, idx)
- if useSeconds then
- valRate = valRate * 20
- end
- mon.write("-> " .. fmtVal(valRate) .. " " .. unit .. "/" .. (useSeconds and "s" or "t"))
- end
- function writeInfo(idx, label, val, unit)
- writeEmtpyLine(idx)
- mon.setCursorPos(1, idx)
- mon.write(label)
- mon.setCursorPos(column2, idx)
- mon.write(fmtVal(val) .. " " .. unit .. "/t")
- end
- function updateInjectionRate()
- -- TODO: !!! Rewrite using i.fieldDrainRate !!!
- if currentField < fieldTarget then
- if fieldDeltaT < 0 then
- injectionRate = injectionRate + math.abs(fieldDeltaT) * fieldIncreaseMultiplier
- elseif fieldTarget - currentField > 10 then
- injectionRate = injectionRate + 2500
- -- elseif fieldDeltaT > 0 and fieldDeltaT < 0.02 then
- -- injectionRate = injectionRate + 100
- end
- elseif currentField - fieldTarget > fieldOverfillLimit then
- if fieldDeltaT > -0.02 then
- injectionRate = injectionRate - fieldDecreaseAbsolute
- end
- end
- gin.setFlowOverride(injectionRate)
- end
- function CLI()
- while true do
- uIn = string.lower(io.read())
- cmd = string.sub(uIn, 1, 1)
- num = tonumber(string.sub(uIn, 2))
- if cmd == "q" then
- return
- elseif cmd == "h" then
- print("Commands:\nq - Quit\nh - Help\nf - Set Field Target\ni - Set Injection Rate\nm - Set Field Increase Multiplier")
- elseif cmd == "i" then
- if num ~= nil then
- injectionRate = num
- gin.setFlowOverride(injectionRate)
- else
- print("Current Injection Rate:\n" .. injectionRate)
- end
- elseif cmd == "f" then
- if num ~= nil then
- fieldTarget = num
- else
- print("Current Field Target:\n" .. fieldTarget)
- end
- elseif cmd == "m" then
- if num ~= nil then
- fieldIncreaseMultiplier = num
- else
- print("Current Field Increase Multiplier:\n" .. fieldIncreaseMultiplier)
- end
- end
- end
- end
- function main()
- while true do
- -- Monitor Exhibit
- idx = 1
- writeInfoWRate(idx, "TEMP", currentTemp, tempDeltaT, "C")
- idx = idx + 1
- writeInfoWRate(idx, "SAT", currentSat, satDeltaT, "%")
- idx = idx + 1
- writeInfoWRate(idx, "FIELD", currentField, fieldDeltaT, "%")
- idx = idx + 1
- writeInfoWRate(idx, "FUEL", currentFuel, fuelDeltaT, "%")
- idx = idx + 1
- writeInfo(idx, "GEN", reac.getReactorInfo().generationRate, "RF")
- idx = idx + 1
- idx = idx + 1
- writeInfo(idx, "IN", gin.getFlow(), "RF")
- idx = idx + 1
- writeInfo(idx, "OUT", gout.getFlow(), "RF")
- idx = idx + 1
- writeInfo(idx, "GAIN", gout.getFlow() - gin.getFlow(), "RF")
- idx = idx + 1
- -- writeInfo(idx, "F-CONV", reac.getReactorInfo().fuelConversionRate, "mB")
- -- idx = idx + 1
- end
- end
- reac = getPeripheral('draconic_reactor')
- mon = getPeripheral('monitor')
- gin = peripheral.wrap(energyInputAddr)
- gout = peripheral.wrap(energyOutputAddr)
- if gin.getFlow() ~= nil and gin.getFlow() ~= 0 then
- injectionRate = gin.getFlow()
- end
- -- For computer controlled input
- gin.setOverrideEnabled(true)
- monX, monY = mon.getSize()
- -- State
- fieldDeltaT = 0
- tempDeltaT = 0
- satDeltaT = 0
- fuelDeltaT = 0
- currentField = 0
- currentTemp = 0
- currentSat = 0
- currentFuel = 0
- parallel.waitForAny(main, perTickWrapper, CLI)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement