Advertisement
Guest User

Untitled

a guest
Oct 8th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.89 KB | None | 0 0
  1. --
  2. -- Control an active cooled reactor from Big reactors (http://big-reactors.com/).
  3. -- 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/)
  4. --
  5. -- This script controls a activ cooled reactor to save fuel. It set the rod level automaticly
  6. -- Run the profiler script before running this script
  7. --
  8. -- Author: El_Loko_
  9. --
  10. -- Save as "startup"
  11. --
  12. --
  13. --
  14. -- Constant values (configuration)
  15. -- ===============================
  16.  
  17. -- set the Broadcast channel (make sure it is the same as the turbines)
  18. local stateRequestChannel = 32768 -- Broadcast channel.
  19. local loopT = 1
  20. local rodLevel = 100
  21.  
  22. -- set the positon where your computer is conected to the reactor
  23. -- top, bottom, left, right, back              
  24. local reactor
  25. local reactor = peripheral.wrap("bottom")
  26.  
  27. -- the file with the reactor profile
  28. local profile = "profile.txt"
  29. local reactorProfil = {}
  30.  
  31.  
  32.  
  33. -- Author: kla_sch
  34. local function sendBroadcastRequest()
  35.    local pList = peripheral.getNames()
  36.    local i, name
  37.    for i, name in pairs(pList) do
  38.       if peripheral.getType(name) == "modem" then
  39.          peripheral.call(name, "open", os.getComputerID())
  40.          peripheral.call(name, "transmit", stateRequestChannel, os.getComputerID(), "BR_turbine_get_state")
  41.          
  42.       end
  43.    end
  44. end
  45.  
  46.  
  47. -- Author: unknown
  48. function split(pString, pPattern)
  49.    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  50.    local fpat = "(.-)" .. pPattern
  51.    local last_end = 1
  52.    local s, e, cap = pString:find(fpat, 1)
  53.    while s do
  54.           if s ~= 1 or cap ~= "" then
  55.          table.insert(Table,cap)
  56.           end
  57.           last_end = e+1
  58.           s, e, cap = pString:find(fpat, last_end)
  59.    end
  60.    if last_end <= #pString then
  61.           cap = pString:sub(last_end)
  62.           table.insert(Table, cap)
  63.    end
  64.    return Table
  65. end
  66.  
  67.  
  68. -- check if the profile is available
  69. function file_exists(file)
  70.   local f = io.open(file, "rb")
  71.   if f then f:close() end
  72.   return f ~= nil
  73. end
  74.  
  75. -- get all lines from a file, returns an empty
  76. -- list/table if the file does not exist
  77. function lines_from(file)
  78.   if not file_exists(file) then return {} end
  79.   lines = {}
  80.   for line in io.lines(file) do
  81.     lines[#lines + 1] = line
  82.   end
  83.   return lines
  84. end
  85.  
  86. -- tests the functions above
  87. local lines = lines_from(profile)
  88.  
  89. -- load profile
  90. for k,v in pairs(lines) do
  91.     line = v
  92.     table.insert(reactorProfil, split(line, ";"))
  93. end
  94.  
  95.  
  96. -- set rod to 100
  97. reactor.setAllControlRodLevels(rodLevel)
  98. reactor.setActive(true)
  99.  
  100. -- parts of this codes are from kla_sch
  101. -- some is from me
  102. while(true) do
  103.    
  104.     term.clear()
  105.     sendBroadcastRequest()
  106.     local steamNeeded = 0
  107.     local tID = os.startTimer(loopT)
  108.  
  109.     repeat
  110.        
  111.     turbineTable = {}
  112.     local evt, p1, p2, p3, p4, p5 = os.pullEvent()
  113.     if evt == "modem_message"
  114.      and p2 == os.getComputerID()
  115.      and p3 == stateRequestChannel
  116.     then
  117.         local rState = textutils.unserialize(tostring(p4))
  118.         if rState ~= nil
  119.          and type(rState) == "table"
  120.          and rState.version ~= nil
  121.          and rState.version == "Turbine V0.1"
  122.         then
  123.             -- seems to be a suitable data structure
  124.             turbineTable[rState.label] = rState
  125.             steamNeeded = rState.fluidFlowRateMax + steamNeeded
  126.         end
  127.     end
  128.      
  129.     -- search the table reactorProfile for the first value wich is above
  130.     -- the needed steam value and set the rods to this level
  131.     -- very ineficent but working  
  132.     for k,v in pairs(reactorProfil) do
  133.         --print(v[3])
  134.         if (tonumber(v[3]) > steamNeeded) then
  135.             reactor.setAllControlRodLevels(tonumber(v[1]))
  136.             break
  137.         end
  138.     end
  139.     term.clear()
  140.     print("Rod Level:        " .. reactor.getControlRodLevel(0))
  141.     print("Energy Produced:  " .. reactor.getEnergyProducedLastTick())
  142.     print("Energy Requested: " .. steamNeeded)
  143.     until evt == "timer" and p1 == tID
  144.  
  145. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement