Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- Control an active cooled reactor from Big reactors (http://big-reactors.com/).
- -- this is an Addon Script for kla_sch turbine script (http://www.computercraft.info/forums2/index.php?/topic/21430-active-cooled-big-reactor-turbine-pid-controller-with-master-control-and-monitoring/)
- --
- -- This script controls a activ cooled reactor to save fuel. It set the rod level automaticly
- -- Run the profiler script before running this script
- --
- -- Author: El_Loko_
- --
- -- Save as "startup"
- --
- --
- --
- -- Constant values (configuration)
- -- ===============================
- -- set the Broadcast channel (make sure it is the same as the turbines)
- local stateRequestChannel = 32768 -- Broadcast channel.
- local loopT = 1
- local rodLevel = 100
- -- set the positon where your computer is conected to the reactor
- -- top, bottom, left, right, back
- local reactor
- local reactor = peripheral.wrap("bottom")
- -- the file with the reactor profile
- local profile = "profile.txt"
- local reactorProfil = {}
- -- Author: kla_sch
- local function sendBroadcastRequest()
- local pList = peripheral.getNames()
- local i, name
- for i, name in pairs(pList) do
- if peripheral.getType(name) == "modem" then
- peripheral.call(name, "open", os.getComputerID())
- peripheral.call(name, "transmit", stateRequestChannel, os.getComputerID(), "BR_turbine_get_state")
- end
- end
- end
- -- Author: unknown
- function split(pString, pPattern)
- local Table = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table,cap)
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end
- -- check if the profile is available
- function file_exists(file)
- local f = io.open(file, "rb")
- if f then f:close() end
- return f ~= nil
- end
- -- get all lines from a file, returns an empty
- -- list/table if the file does not exist
- function lines_from(file)
- if not file_exists(file) then return {} end
- lines = {}
- for line in io.lines(file) do
- lines[#lines + 1] = line
- end
- return lines
- end
- -- tests the functions above
- local lines = lines_from(profile)
- -- load profile
- for k,v in pairs(lines) do
- line = v
- table.insert(reactorProfil, split(line, ";"))
- end
- -- set rod to 100
- reactor.setAllControlRodLevels(rodLevel)
- reactor.setActive(true)
- -- parts of this codes are from kla_sch
- -- some is from me
- while(true) do
- term.clear()
- sendBroadcastRequest()
- local steamNeeded = 0
- local tID = os.startTimer(loopT)
- repeat
- turbineTable = {}
- local evt, p1, p2, p3, p4, p5 = os.pullEvent()
- if evt == "modem_message"
- and p2 == os.getComputerID()
- and p3 == stateRequestChannel
- then
- local rState = textutils.unserialize(tostring(p4))
- if rState ~= nil
- and type(rState) == "table"
- and rState.version ~= nil
- and rState.version == "Turbine V0.1"
- then
- -- seems to be a suitable data structure
- turbineTable[rState.label] = rState
- steamNeeded = rState.fluidFlowRateMax + steamNeeded
- end
- end
- -- search the table reactorProfile for the first value wich is above
- -- the needed steam value and set the rods to this level
- -- very ineficent but working
- for k,v in pairs(reactorProfil) do
- --print(v[3])
- if (tonumber(v[3]) > steamNeeded) then
- reactor.setAllControlRodLevels(tonumber(v[1]))
- break
- end
- end
- term.clear()
- print("Rod Level: " .. reactor.getControlRodLevel(0))
- print("Energy Produced: " .. reactor.getEnergyProducedLastTick())
- print("Energy Requested: " .. steamNeeded)
- until evt == "timer" and p1 == tID
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement