gelatine87

reactorWiFisender

Aug 2nd, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.96 KB | None | 0 0
  1. ----------------------------------------------------------
  2. -- Active Cooling Big Reactor Monitor                   --
  3. --                                                      --
  4. -- Minecraft HermitCraft FTB Infinity Episode 28        --
  5. -- https://www.youtube.com/watch?v=cIPxwZJWOiE          --
  6. --                                                      --
  7. -- YouTube Channel http://youtube.com/hypnotizd         --
  8. ----------------------------------------------------------
  9.  
  10. local mon = {}
  11. local hasMon = false
  12. local hasRMon = false
  13. local rMonName = ""
  14. local cPort = {}
  15. local hasCPort = false
  16. local hasRCPort = false
  17. local rCPortName = ""
  18. local directions = {"top", "bottom", "front", "back", "left", "right"}
  19. local minX = 0
  20. local minY = 0
  21. local minZ = 0
  22. local maxX = 0
  23. local maxY = 0
  24. local maxZ = 0
  25. local crPrevCoreTemp = 0
  26. local crCoreTemp = 0
  27. local crCoreChangeWait = 0
  28.  
  29. local table = {}
  30.  
  31. local function findPeripherals()
  32.   for k, v in pairs(peripheral.getNames()) do
  33.     if string.find(v, "Reactor") then
  34.       hasCPort = true
  35.       hasRCPort = true
  36.       rCPortName = v
  37.       print("INFO: Found remote "..v)
  38.       os.sleep(1)
  39.     elseif string.find(v, "monitor") then
  40.       hasMon = true
  41.       hasRMon = true
  42.       rMonName = v
  43.       print("INFO: Found remote "..v)
  44.       os.sleep(1)
  45.     end
  46.   end
  47. end
  48.  
  49. local function findCPort()
  50.   if hasRCPort then return end
  51.   for k, v in pairs(directions) do
  52.     local p = peripheral.wrap(v)
  53.     if p ~= nil then
  54.       if string.find(peripheral.getType(v), "Reactor") then
  55.         cPort = p
  56.         hasCPort = true
  57.         return
  58.       end
  59.     end
  60.   end
  61.   print("ERROR: No computer port detected!")
  62. end
  63.  
  64. local function findMon()
  65.   if hasRMon then return end
  66.   for k, v in pairs(directions) do
  67.     local p = peripheral.wrap(v)
  68.     if p ~= nil then
  69.       if string.find(peripheral.getType(v), "monitor") then
  70.         mon = p
  71.         hasMon = true
  72.         return
  73.       end
  74.     end
  75.   end
  76.   print("WARNING: No monitor detected!")
  77.   os.sleep(5)
  78. end
  79.  
  80. local function monClear()
  81.   if hasMon then
  82.     if hasRMon then
  83.       peripheral.call(rMonName, "setCursorPos", 1, 1)
  84.       peripheral.call(rMonName, "clear")
  85.     else
  86.       mon.setCursorPos(1, 1)
  87.       mon.clear()
  88.     end
  89.   else
  90.     term.clear()
  91.     term.setCursorPos(1, 1)
  92.   end
  93. end
  94.  
  95. local function monWrite(x, y, text)
  96.   if hasMon then
  97.     if hasRMon then
  98.       peripheral.call(rMonName, "setCursorPos", x, y)
  99.       peripheral.call(rMonName, "write", text)
  100.     else
  101.       mon.setCursorPos(x, y)
  102.       mon.write(text)
  103.     end
  104.   else
  105.     term.setCursorPos(x, y)
  106.     term.write(text)
  107.   end
  108. end
  109.  
  110. local function getSize()
  111.   if hasRCPort then
  112.     minX, minY, minZ = peripheral.call(rCPortName, "getMinimumCoordinate")
  113.     maxX, maxY, maxZ = peripheral.call(rCPortName, "getMaximumCoordinate")
  114.   else
  115.     minX, minY, minZ = cPort.getMinimumCoordinate()  
  116.     maxX, maxY, maxZ = cPort.getMaximumCoordinate()
  117.   end
  118. end
  119.  
  120. local function setAllControlRodLevels(x)
  121.   if hasRCPort then
  122.     peripheral.call(rCPortName, "setAllControlRodLevels", x)
  123.   else
  124.     cPort.setAllControlRodLevels(x)
  125.   end
  126. end
  127.  
  128. local function getActive()
  129.   local ret = false
  130.   if hasRCPort then
  131.     ret = peripheral.call(rCPortName, "getActive")
  132.   else
  133.     ret = cPort.getActive()
  134.   end
  135.   return ret
  136. end
  137.  
  138. local function getControlRodLevel()
  139.   local ret = 0
  140.   if hasRCPort then
  141.     ret = peripheral.call(rCPortName, "getControlRodLevel", 0)
  142.   else
  143.     ret = cPort.getControlRodLevel(0)
  144.   end
  145.   return ret
  146. end
  147.  
  148. local function getFuelTemperature()
  149.   local ret
  150.   if hasRCPort then
  151.     ret = peripheral.call(rCPortName, "getFuelTemperature")
  152.   else
  153.     ret = cPort.getFuelTemperature()
  154.   end
  155.   return ret
  156. end
  157.  
  158. local function setControlRods()
  159.   if getActive() then
  160.     if crCoreTemp ~= 0 then
  161.       crPrevCoreTemp = crCoreTemp
  162.       crCoreTemp = getFuelTemperature()
  163.       local ctrlRodLevel = getControlRodLevel()
  164.       local tempDiff = crPrevCoreTemp - crCoreTemp
  165.       if crCoreChangeWait > 0 then
  166.         crCoreChangeWait = crCoreChangeWait - 1
  167.         return
  168.       end
  169.       if crCoreTemp >= 1350 then
  170.         setAllControlRodLevels(ctrlRodLevel+1)
  171.       elseif crCoreTemp >= 1050 then
  172.         setAllControlRodLevels(ctrlRodLevel+1)
  173.         crCoreChangeWait = 1
  174.       elseif crCoreTemp <= 600 then
  175.         setAllControlRodLevels(ctrlRodLevel-1)
  176.       elseif crCoreTemp <= 950 then
  177.         setAllControlRodLevels(ctrlRodLevel-1)
  178.         crCoreChangeWait = 1
  179.       end
  180.     else
  181.       crCoreTemp = getFuelTemperature()
  182.       if getControlRodLevel() == 100 then
  183.         setAllControlRodLevels(99)
  184.       end
  185.       crCoreChangeWait = 1
  186.     end
  187.   end
  188. end
  189.  
  190. local function init()
  191.   term.clear()
  192.   term.setCursorPos(1, 1)
  193.   findPeripherals()
  194.   findCPort()
  195.   findMon()
  196.   getSize()
  197.   monClear()
  198. end
  199.  
  200. local function displayInfo()
  201.   local rods = 0
  202.   local casingTemp = 0
  203.   local coreTemp = 0
  204.   local rodLevel = 0
  205.   local fuelReactivity = 0
  206.   local fuelConsumed = 0
  207.   local steamProduced = 0
  208.  
  209.   if hasRCPort then
  210.     rods = peripheral.call(rCPortName, "getNumberOfControlRods")
  211.     casingTemp = peripheral.call(rCPortName, "getCasingTemperature")
  212.     coreTemp = peripheral.call(rCPortName, "getFuelTemperature")
  213.     rodLevel = peripheral.call(rCPortName, "getControlRodLevel", 0)
  214.     fuelReactivity = peripheral.call(rCPortName, "getFuelReactivity")
  215.     fuelConsumed = peripheral.call(rCPortName, "getFuelConsumedLastTick")
  216.     steamProduced = peripheral.call(rCPortName, "getHotFluidProducedLastTick")
  217.   else
  218.     rods = cPort.getNumberOfControlRods()
  219.     casingTemp = cPort.getCasingTemperature()
  220.     coreTemp = cPort.getFuelTemperature()
  221.     rodLevel = cPort.getControlRodLevel(0)
  222.     fuelReactivity = cPort.getFuelReactivity()
  223.     fuelConsumed = cPort.getFuelConsumedLastTick()
  224.     steamProduced = cPort.getHotFluidProducedLastTick()
  225.   end
  226.  
  227.   local active = "Inactive"
  228.   if getActive() then
  229.     active = "Active"
  230.   end
  231.  
  232.   X = 1+maxX-minX
  233.   Y = 1+maxY-minY
  234.   Z = 1+maxZ-minZ
  235.  
  236.   table[0] = rods
  237.   table[1] = casingTemp
  238.   table[2] = coreTemp
  239.   table[3] = rodLevel
  240.   table[4] = fuelReactivity
  241.   table[5] = fuelConsumed
  242.   table[6] = steamProduced
  243.   table[7] = active
  244.   table[8] = X
  245.   table[9] = Y
  246.   table[10] = Z
  247.  
  248.  
  249.   local msg = textutils.serialize(table)
  250.   rednet.open("back")
  251.   rednet.broadcast(msg)
  252.  
  253.   monClear()
  254.   monWrite(1, 1, "   Reactor Status: "..active)
  255.   monWrite(1, 2, "     Reactor Size: "..X.."x"..Y.."x"..Z)
  256.   monWrite(1, 3, " Num Control Rods: "..rods)
  257.   monWrite(1, 4, "     Casing Temp.: "..casingTemp)
  258.   monWrite(1, 5, "       Core Temp.: "..coreTemp)
  259.   monWrite(1, 6, "Control Rod Level: "..rodLevel)
  260.   monWrite(1, 7, "  Fuel Reactivity: "..fuelReactivity)
  261.   monWrite(1, 8, "  Fuel Usage/tick: "..fuelConsumed)
  262.   monWrite(1, 9, "   Steam Produced: "..steamProduced)
  263. end
  264.  
  265. init()
  266. if hasCPort == false then return end
  267.  
  268. while true do
  269.   setControlRods()
  270.   displayInfo()
  271.   os.sleep(3)
  272. end
Add Comment
Please, Sign In to add comment