Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --version 0.1.4
- --fixed if true then statements to be cleaner
- --supports 2 turbines
- --detects if second turbine is present
- --fixed stall on coroutine.yield()
- --improved formatting
- --checks for connections before attempting updateData()
- --now with more reliability!
- function loadPeripherals()
- if peripheral.isPresent("left") then
- local wireless = peripheral.wrap("left")
- --wireless.open(4)
- wireless.open(65535)
- end
- --if peripheral.isPresent("BigReactors-Reactor_1") == false then
- -- term.write("No Reactor Present!")
- --shell.exit()
- --end
- modem = peripheral.wrap("back")
- monitor = peripheral.wrap("monitor_2")
- if peripheral.isPresent("BigReactors-Reactor_1") then
- reactor = peripheral.wrap("BigReactors-Reactor_1")
- reactorPresent = true
- end
- if peripheral.isPresent("BigReactors-Turbine_0") then
- turbine = peripheral.wrap("BigReactors-Turbine_0")
- firstTurbinePresent = true
- end
- if peripheral.isPresent("BigReactors-Turbine_1") then
- secondTurbinePresent = true
- turbine2 = peripheral.wrap("BigReactors-Turbine_1")
- end
- term.clear()
- monitor.setBackgroundColor(32768)
- monitor.clear()
- term.setCursorPos(3,1)
- wirelessSide = "left"
- modemSide = "top"
- rednet.open(wirelessSide)
- end
- function round2(num, places)
- num = tostring(num)
- local inc = false
- if num == nil then
- num = 1
- end --testing this piece out to avoid errors when the reactor doesn't report fuel usage for a tick
- local decimal = string.find(num, "%.")
- if decimal == nil then
- decimal = 1
- end
- if num:len() - decimal <= places then
- return tonumber(num)
- end --already rounded, nothing to do.
- local digit = tonumber(num:sub(decimal + places + 1))
- num = num:sub(1, decimal + places)
- if digit <= 4 then
- return tonumber(num)
- end --no incrementation needed, return truncated number
- local newNum = ""
- for i=num:len(), 1, -1 do
- digit = tonumber(num:sub(i))
- if digit == 9 then
- if i > 1 then
- newNum = "0"..newNum
- else
- newNum = "10"..newNum
- end
- elseif digit == nil then
- newNum = "."..newNum
- else
- if i > 1 then
- newNum = num:sub(1,i-1)..(digit + 1)..newNum
- else
- newNum = (digit + 1)..newNum
- end
- return tonumber(newNum) --No more 9s found, so we are done incrementing. Copy remaining digits, then return number.
- end
- end
- return tonumber(newNum)
- end
- function updateData()
- --declare some reactor variables sucka!
- if reactorPresent then
- fuelTempFloat = reactor.getFuelTemperature()
- if fuelTempFloat == nil then
- fuelTempFloat = 1
- end
- fuelTemp = math.floor(fuelTempFloat)
- reactorIsConnected = reactor.getConnected()
- reactorIsActive = reactor.getActive()
- controlRodCount = reactor.getNumberOfControlRods()
- reactorCasingTempFloat = reactor.getCasingTemperature()
- if reactorCasingTempFloat == nil then
- reactorCasingTempFloat = 0
- end
- reactorCasingTemp = math.floor(reactorCasingTempFloat) --use math.floor to keep it a whole integer otherwise it has too many sigfigs.
- reactorFuelAmount = reactor.getFuelAmount()
- reactorWasteAmount = reactor.getWasteAmount()
- reactorCoolantType = reactor.getCoolantType()
- controlRodDesiredLevel = 0
- reactorFuelUsed = reactor.getFuelConsumedLastTick()
- reactorSteamAmount = reactor.getHotFluidAmount()
- reactorSteamMade = reactor.getHotFluidProducedLastTick()
- controlRodLevelOne = reactor.getControlRodLevel(0) --remember that the index is one less than rod count. find a way to let this adjust for number of control rods automat
- if controlRodLevelOne == nil then
- controlRodLevelOne = 0
- end
- controlRodLevelTwo = reactor.getControlRodLevel(1)
- if controlRodLevelTwo == nil then
- controlRodLevelTwo = 0
- end
- controlRodLevelThree = reactor.getControlRodLevel(2)
- if controlRodLevelThree == nil then
- controlRodLevelThree = 0
- end
- controlRodLevelFour = reactor.getControlRodLevel(3)
- if controlRodLevelFour == nil then
- controlRodLevelFour = 0
- end
- controlRodAverage = ((controlRodLevelOne + controlRodLevelTwo + controlRodLevelThree + controlRodLevelFour)) / (4)
- end
- --turbine variables will go here if this turns out to be beneficial
- if firstTurbinePresent then
- idealRotorRPM = 1800
- turbineIsConnected = turbine.getConnected()
- turbineIsActive = turbine.getActive()
- turbineRotorSpeedFloat = turbine.getRotorSpeed()
- if turbineRotorSpeedFloat == nil then
- turbineRotorSpeedFloat = 0
- end
- turbineRotorSpeed = math.floor(turbineRotorSpeedFloat)
- turbineFluidRate = turbine.getFluidFlowRate()
- turbineEngaged = turbine.getInductorEngaged()
- turbineEnergyFloat = turbine.getEnergyProducedLastTick()
- if turbineEnergyFloat == nil then
- turbineEnergyFloat = 0
- end
- turbineEnergy = math.floor(turbineEnergyFloat)
- end
- --turbine 2
- if secondTurbinePresent then
- turbine2IsConnected = turbine2.getConnected(1)
- idealRotorRPM2 = 1800
- turbine2IsActive = turbine2.getActive()
- turbine2RotorSpeedFloat = turbine2.getRotorSpeed()
- if turbine2RotorSpeedFloat == nil then
- turbine2RotorSpeedFloat = 0
- end
- turbine2RotorSpeed = math.floor(turbine2RotorSpeedFloat)
- turbine2FluidRate = turbine2.getFluidFlowRate()
- turbine2Engaged = turbine2.getInductorEngaged()
- turbine2EnergyFloat = turbine2.getEnergyProducedLastTick()
- if turbine2EnergyFloat == nil then
- turbine2EnergyFloat = 0
- end
- turbine2Energy = math.floor(turbine2EnergyFloat)
- end
- end
- function tabletRequest()
- wirelessSide = "left"
- modemSide = "top"
- local wireless = peripheral.wrap(wirelessSide)
- local modem = peripheral.wrap(modemSide)
- rednet.open(wirelessSide)
- if wirelessSide == nil then
- print("No Modem")
- print("Will shutdown in 3 seconds")
- sleep(3)
- --os.shutdown()
- else
- print("Opened modem on ".. wirelessSide)
- end
- os.sleep(0.2)
- id, message = rednet.receive()
- term.setCursorPos(1,1)
- print ("Device " .. id .. " sent a " .. message .. " ")
- os.sleep(1)
- term.setCursorPos(1,2)
- print ("Sending reactor info...")
- local reactor
- local reactor = peripheral.wrap("BigReactors-Reactor_0")
- if peripheral.wrap("back") then
- term.setCursorPos(1,3)
- print ("Found Reactor")
- end
- active = reactor.getActive(1)
- if active then
- term.setCursorPos(1,5)
- print ("The Reactor is: ")
- term.setTextColor(colors.green)
- print ("ON")
- else term.setTextColor(colors.white)
- print ("The Reactor is: ")
- term.setTextColor(colors.red)
- print ("OFF")
- rednet.send(id, "The Reactor is: OFF")
- sleep(.1)
- rednet.send(id, "The Reactor is: OFF")
- --os.reboot()
- --runProgram()
- --exit() --stops whole program
- --break --only in a loop, at the end
- return --i think this did it
- end
- term.setTextColor(colors.white)
- lvl = reactor.getControlRodLevel(0)
- if lvl then
- print ("Control rod level is " .. lvl .. "% closed")
- end
- turbineEnergy = turbine.getEnergyProducedLastTick()
- tick = turbineEnergy --reactor.getEnergyProducedLastTick(1)
- if tick then
- print (" " .. tick .. " RF/t ")
- end
- rednet.send(id, "The Reactor is: ON")
- os.sleep(0.2)
- rednet.send(id, "Control rod level is " .. lvl .. "% closed")
- os.sleep(0.2)
- rednet.send(id, " Producing " .. tick .. " RF/t ")
- return
- --os.reboot()
- -- The Pocket computer program is at /bkc8M1Bi
- -- Original code by Dukrobber
- -- Updates by blackrabt
- end
- function createDisplayReactor()
- --reactor display stuff
- monitor.setBackgroundColor(32768) --black
- monitor.setTextColor(512) --cyan
- monitor.clear()
- monitor.setCursorPos(4,1)
- --set text color based on reactor status?
- monitor.write("Reactor")
- --term.redirect(monitor)
- --paintutils.drawLine(3,3,7,3,1)
- --term.redirect(term.native())
- monitor.setCursorPos(2,2)
- monitor.write("-----------") --___________
- monitor.setCursorPos(3,3)
- if reactorPresent then
- if reactorIsConnected then
- monitor.setTextColor(8192)
- monitor.write("Connected")
- end
- --skip the rest of the reactor display sections in this event
- monitor.setCursorPos(3,4)
- if reactorIsActive then
- monitor.setTextColor(32)
- monitor.write("Active")
- --if peripheral.isPresent("left") then
- -- testPDA.print(reactorIsActive)
- -- testPDA.print("test")
- --end
- monitor.setTextColor(512)
- monitor.setCursorPos(3,5)
- monitor.setTextColor(16) --yellow
- monitor.write("Casing: "..reactorCasingTemp.."C")
- monitor.setCursorPos(3,6)
- if fuelTemp > 170 and fuelTemp < 230 then
- monitor.setTextColor(32)
- end
- if fuelTemp < 170 and fuelTemp > 100 then
- monitor.setTextColor(16)
- end
- if fuelTemp < 100 then
- monitor.setTextColor(16384)
- end
- if fuelTemp > 230 and fuelTemp < 300 then
- monitor.setTextColor(16)
- end
- if fuelTemp > 3000 then
- monitor.setTextColor(16384)
- monitor.setCursorPos(11,6)
- monitor.write("CAUTION!")
- --while fuelTemp > 300 do --this makes a flashing caution but it halts the whole program
- --monitor.setTextColor(16384)
- --monitor.setCursorPos(3,11)
- --monitor.write("CAUTION!")
- --sleep(0.4)
- --monitor.setTextColor(64)
- --monitor.setCursorPos(3,11)
- --monitor.write("CAUTION!")
- --sleep(0.4)
- --monitor.setTextColor(2)
- --monitor.setCursorPos(3,11)
- --monitor.write("CAUTION!")
- --end
- end
- monitor.write("Core: "..fuelTemp.."C")
- monitor.setCursorPos(3,7)
- monitor.setTextColor(256)
- monitor.write("HFO: "..reactorSteamMade.."mB/t")
- monitor.setCursorPos(3,9)
- controlRods = math.floor(controlRodAverage)
- monitor.write("Avg. Control Rod: "..controlRodAverage.."% ")
- monitor.setCursorPos(3,8)
- --monitor.write("FBR: "..reactorFuelUsed.."mB/t")
- -- f = tostring(reactorFuelUsed)
- --f = reactorFuelUsed
- --fuelFormatted = (string.format("%.4f", reactor.getFuelConsumedLastTick()))
- fuelFormatted = round2(reactorFuelUsed, 2)
- -- monitor.write(fuelFormatted)
- monitor.write("FBR: "..fuelFormatted.."mB/t")
- elseif reactorIsConnected == false then
- monitor.setTextColor(4)
- monitor.write("Disconnected")
- return--skip the rest
- end
- elseif reactorIsActive == false then
- monitor.setCursorPos(3,4)
- monitor.setTextColor(16)
- monitor.write("Inactive")
- --return
- end
- end
- function createDisplayTurbine1()
- --turbine display arranged for a shorter and wider display
- -- monitor.setTextColor(512) --cyan
- -- monitor.setCursorPos(4,11)
- -- monitor.write("Turbine Status 1 2")
- -- monitor.setCursorPos(2,12)
- -- monitor.write("-------------------------------")
- -- --term.redirect(monitor)
- -- --paintutils.drawLine(18,3,25,3,512)
- -- --term.redirect(term.native())
- -- monitor.setCursorPos(1,13)
- -- monitor.setTextColor(64)
- -- monitor.write("Network Status")
- -- monitor.setCursorPos(3,13)
- -- if turbineIsConnected then
- -- monitor.setTextColor(8192)
- -- monitor.write("Connected") --should say Connected
- -- end
- -- if turbineIsConnected == false then
- -- monitor.setTextColor(16384)
- -- monitor.write("Disconnected")
- -- end
- -- monitor.write(turbineIsConnected)
- -- monitor.setCursorPos(1,14)
- -- monitor.setTextColor(64)
- -- monitor.write("Device Status: ")
- -- monitor.setCursorPos(1,15)
- -- monitor.setTextColor(64)
- -- monitor.write("Coil Status: ")
- -- monitor.setCursorPos(1,16)
- -- monitor.setTextColor(64)
- -- monitor.write("Generation: ")
- -- monitor.setCursorPos(1,17)
- -- monitor.setTextColor(64)
- -- monitor.write("Rotor Speed: ")
- monitor.setTextColor(512) --cyan
- monitor.setCursorPos(4,11)
- monitor.write("Turbine 1")
- monitor.setCursorPos(2,12)
- monitor.write("-----------")
- if firstTurbinePresent then
- monitor.setCursorPos(3,13)
- if turbineIsConnected then
- monitor.setTextColor(8192)
- monitor.write("Connected") --should say Connected
- else
- monitor.setTextColor(16384)
- monitor.write("Disconnected")
- end
- end
- monitor.setCursorPos(3,14)
- if turbineIsActive then
- monitor.setTextColor(8192)
- monitor.write("Active")
- end
- if turbineIsActive == false then
- monitor.setTextColor(16)
- monitor.write("Inactive")
- end
- monitor.setCursorPos(3,15)
- if turbineEngaged then
- monitor.setTextColor(32)
- monitor.write("Coils Engaged")
- end
- if turbineEngaged == false then
- monitor.setTextColor(16)
- monitor.write("Not Engaged")
- end
- --monitor.setCursorPos(15,15)
- --monitor.write("15,15")
- monitor.setCursorPos(3,16)
- if turbineEnergy > 1000 and turbineEnergy < 10000 then
- monitor.setTextColor(16)
- end
- if turbineEnergy > 10000 then
- monitor.setTextColor(32)
- end
- if turbineEnergy < 1000 then
- monitor.setTextColor(16384)
- end
- monitor.write("Generating: "..turbineEnergy.."RF")
- monitor.setCursorPos(3,17)
- if turbineRotorSpeed > 0 then
- monitor.setTextColor(32)
- monitor.write("Rotor speed: "..turbineRotorSpeed.."RPM")
- end
- if turbineRotorSpeed <1 then
- monitor.setTextColor(16384)
- monitor.write("Rotor speed: "..turbineRotorSpeed.."RPM")
- end
- end --ends createDisplayTurbine1() don't forget to update runProgram()
- --TURBINE 2 GOES HERE
- function createDisplayTurbine2()
- monitor.setTextColor(512) --cyan
- monitor.setCursorPos(4,20)
- monitor.write("Turbine 2")
- monitor.setCursorPos(2,21)
- monitor.write("-----------")
- monitor.setCursorPos(3,22)
- if secondTurbinePresent then
- if turbine2IsConnected then
- monitor.setTextColor(8192)
- monitor.write("Connected") --should say Connected
- end
- --monitor.write(turbineIsConnected)
- monitor.setCursorPos(3,23)
- if turbine2IsActive then
- monitor.setTextColor(8192)
- monitor.write("Active")
- end
- if turbine2IsActive == false then
- monitor.setTextColor(16)
- monitor.write("Inactive")
- end
- monitor.setCursorPos(3,24)
- if turbineEngaged then
- monitor.setTextColor(32)
- monitor.write("Coils Engaged")
- end
- if turbine2Engaged == false then
- monitor.setTextColor(16)
- monitor.write("Not Engaged")
- end
- --monitor.setCursorPos(15,15)
- --monitor.write("15,15")
- monitor.setCursorPos(3,25)
- if turbine2Energy > 1000 and turbine2Energy < 10000 then
- monitor.setTextColor(16)
- end
- if turbine2Energy > 10000 then
- monitor.setTextColor(32)
- end
- if turbine2Energy < 1000 then
- monitor.setTextColor(16384)
- end
- monitor.write("Generating: "..turbine2Energy.."RF")
- monitor.setCursorPos(3,26)
- if turbine2RotorSpeed > 0 then
- monitor.setTextColor(32)
- monitor.write("Rotor speed: "..turbine2RotorSpeed.."RPM")
- end
- if turbine2RotorSpeed <1 then
- monitor.setTextColor(16384)
- monitor.write("Rotor speed: "..turbine2RotorSpeed.."RPM")
- end
- else
- if turbine2IsConnected == false then
- monitor.setTextColor(16384)
- monitor.write("Disconnected")
- end
- return
- end--end if statement
- end--end function
- function runProgram()
- loadPeripherals()
- updateData()
- createDisplayReactor()
- createDisplayTurbine1()
- createDisplayTurbine2()
- --coroutine.yield()
- --tabletRequest()
- sleep(1.5)
- end
- -- function runAsCoroutine()
- -- runProgram()
- -- end
- -- local theReactorCoroutine = coroutine.create(runProgram)
- -- local theTabletCoroutinr = coroutine.create(tabletRequest)
- -- while true do
- -- coroutine.resume(theReactorCoroutine)
- -- if os.pullEvent(rednet.receive()) then
- -- coroutine.yield(theReactorCoroutine)
- -- coroutine.resume(theTabletCoroutinr)
- -- coroutine.resume(theReactorCoroutine)
- -- end
- -- end
- --[[need to pcall(round2)?]]
- --this is supposed to run the peripheral loading in a protected call, and throw an error if there is a problem. It doesn't want to do that yet.
- --if pcall(loadPeripherals) then
- -- while true do
- -- runProgram()
- -- end
- --runProgram()
- --else
- --there was a problem
- --end
- while true do
- runProgram()
- if rednet.receive(2) then --not nil then
- tabletRequest()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment