Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Reactor and turbine control script by EternalClickbait
- --Reactor, turbine and monitor must be connected with wired modems to the computer
- --Only works for actively cooled reactors
- local reactors = {}
- local turbines = {}
- local monitors = {}
- -----VARIABLES TO CHANGE FOR YOUR NEEDS-----
- --The maximum temperature for the reactor
- local reactorMaxTemp = 1000
- --The maximum energy in our turbine's buffer. Max a turbine can hold is 1M (1,000,000)
- local turbineMaxEnergy = 0.99*1000000
- --------------------------------------------
- function round(num, numDecimalPlaces)
- local mult = 10^(numDecimalPlaces or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- function logColour(message, textColour, backgroundColour)
- local oldTextCol = term.getTextColor()
- local oldBackCol = term.getBackgroundColor()
- term.setTextColour(textColour)
- term.setBackgroundColour(backgroundColour or colors.black)
- print(message)
- term.setTextColour(oldTextCol)
- term.setBackgroundColour(oldBackCol)
- for _, side in ipairs(monitors) do
- local m = peripheral.wrap(side)
- oldTextCol = term.getTextColor()
- oldBackCol = term.getBackgroundColor()
- term.setTextColour(textColour)
- term.setBackgroundColour(backgroundColour or colors.black)
- m.write(message)
- term.setTextColour(oldTextCol)
- term.setBackgroundColour(oldBackCol)
- --Move down a line
- local x,y = m.getCursorPos()
- m.setCursorPos(1,y+1)
- end
- end
- --Prints a message to all monitors/windows
- function log(message)
- print(message)
- for _, side in ipairs(monitors) do
- local m = peripheral.wrap(side)
- m.write(message)
- --Move down a line
- local x,y = m.getCursorPos()
- m.setCursorPos(1,y+1)
- end
- end
- -- maps a given range from a specific iterator to a new range
- local function map(n, start, stop, newStart, newStop, withinBounds)
- local value = ((n - start) / (stop - start)) * (newStop - newStart) + newStart
- --// Returns basic value
- if not withinBounds then
- return value
- end
- --// Returns values constrained to exact range
- if newStart < newStop then
- return math.max(math.min(value, newStop), newStart)
- else
- return math.max(math.min(value, newStart), newStop)
- end
- end
- --Sets the scales on all monitors for easily visible text
- function setMonitorScales()
- for _, side in ipairs(monitors) do
- local m = peripheral.wrap(side)
- m.setTextScale(2)
- end
- end
- --Clears all monitors/windows
- function clear()
- term.clear()
- for _, side in ipairs(monitors) do
- peripheral.wrap(side).clear()
- peripheral.wrap(side).setCursorPos(1,1)--Reset the cursor position
- end
- term.setCursorPos(1,1)
- end
- function clearTable(t)
- for i=0, #t do table.remove(t, i) end
- end
- --Updates the list of reactors, turbines and monitor
- function updatePeripherals()
- local peripherals = peripheral.getNames() --Get a list of all the peripherals
- --Clear our tables
- clearTable(reactors)
- clearTable(turbines)
- clearTable(monitors)
- for _, pSide in ipairs(peripherals) do
- local pType = peripheral.getType(pSide)
- local pFunctions = peripheral.wrap(pSide)
- if(pType == "BigReactors-Reactor") then --If we found a reactor
- table.insert(reactors, pSide)
- elseif (pType == "BigReactors-Turbine") then
- table.insert(turbines, pSide)
- elseif (pType == "monitor") then
- table.insert(monitors, pSide)
- end
- end
- end
- --Lists all of the peripherals connected
- function listPeripherals()
- log("Reactors:")
- for i, pSide in ipairs(reactors) do
- log("\t" ..i.. ":\t" ..pSide)
- end
- log("Turbines:")
- for i, pSide in ipairs(turbines) do
- log("\t" ..i.. ":\t" ..pSide)
- end
- log("Monitors:")
- for i, pSide in ipairs(monitors) do
- log("\t" ..i.. ":\t" ..pSide)
- end
- end
- --Updates all info on screen
- function update()
- --Clear the window(s)
- clear()
- log ("Turbine max energy: " ..turbineMaxEnergy)
- log ("Reactor max temp: " ..reactorMaxTemp)
- updatePeripherals()
- listPeripherals()
- --Now print out the info for each turbine/reactor, and enable/disable them as needed
- for _,pSide in ipairs(reactors) do
- local r = peripheral.wrap(pSide)
- local temp = r.getFuelTemperature()
- local shouldBeActive = true
- --If the reactor's at or above the max temp, it shouldn't run
- if(temp >= reactorMaxTemp) then shouldBeActive = false end
- --If it has less than 1 bucket of water (coolant), it shoudn't run
- if(r.getCoolantAmount() <= 1000) then shouldBeActive = false end
- --Set the reactor's active state
- r.setActive(shouldBeActive)
- log(pSide.. ":")
- log("\t" ..round(temp).. " C")
- end
- for _,pSide in ipairs(turbines) do
- local t = peripheral.wrap(pSide)
- local energy = t.getEnergyStored()
- local rpm = t.getRotorSpeed()
- --If the turbine's above the amount of energy allowed, disable the coils
- if(energy >= turbineMaxEnergy) then
- t.setInductorEngaged(false)
- else
- t.setInductorEngaged(true)
- end
- log(pSide.. ":")
- log("\t" ..round(energy).. " RF\t" ..round(rpm).. " RPM")
- end
- end
- setMonitorScales()
- while true do
- update()
- sleep(0.1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement