Crysys

BigReactors - Turbine/Battery Monitor

Nov 19th, 2018
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.86 KB | None | 0 0
  1. local rValue = math.random(1000, 9999) --has no actual effect on the operation of the program
  2.  
  3. while true do --enter our loop
  4.  
  5.   local pList = peripheral.getNames()
  6.   local i, name
  7.  
  8.   --get the full device names
  9.  
  10.   function getMonitorName()
  11.     for i, name in pairs(pList) do
  12.       if peripheral.getType(name) == "monitor" then
  13.         return peripheral.wrap(name)
  14.       end
  15.     end
  16.   end
  17.   function getMatrixName()
  18.     for i, name in pairs(pList) do
  19.       if peripheral.getType(name) == "Induction Matrix" then
  20.          return peripheral.wrap(name)
  21.        end
  22.     end
  23.   end
  24.   function getbCubeName()
  25.     for i, name in pairs(pList) do
  26.       if peripheral.getType(name) == "Basic Energy Cube" then
  27.         return peripheral.wrap(name)
  28.       end
  29.     end
  30.   end
  31.   function getaCubeName()
  32.     for i, name in pairs(pList) do
  33.       if peripheral.getType(name) == "Advanced Energy Cube" then
  34.         return peripheral.wrap(name)
  35.       end
  36.     end
  37.   end
  38.   function geteCubeName()
  39.     for i, name in pairs(pList) do
  40.       if peripheral.getType(name) == "Elite Energy Cube" then
  41.         return peripheral.wrap(name)
  42.       end
  43.     end
  44.   end
  45.   function getuCubeName()
  46.     for i, name in pairs(pList) do
  47.       if peripheral.getType(name) == "Ultimate Energy Cube" then
  48.         return peripheral.wrap(name)
  49.       end
  50.     end
  51.   end
  52.  
  53.   --set the short name aliases
  54.  
  55.   local mon = getMonitorName()
  56.   local matrix = getMatrixName()
  57.   local bCube = getbCubeName()
  58.   local aCube = getaCubeName()
  59.   local eCube = geteCubeName()
  60.   local uCube = getuCubeName()
  61.   local sType = 0 --storage type counter
  62.  
  63.   --quick formatting aliases
  64.  
  65.   function mCursor(x,y) --monitor cursor alias
  66.     mon.setCursorPos(x,y)
  67.   end
  68.   function tCursor(x,y) --terminal cursor alias
  69.     term.setCursorPos(x,y)
  70.   end
  71.   function nText() --nominal text (need to rework later)
  72.     mon.setTextColor(colors.lime)
  73.   end
  74.   function hText() --header text
  75.     mon.setTextColor(colors.white)
  76.   end
  77.   function iText() --info text
  78.     mon.setTextColor(colors.yellow)
  79.   end
  80.   function cText() --caution text
  81.     mon.setTextColor(colors.orange)
  82.   end
  83.   function wText() --warning text
  84.     mon.setTextColor(colors.red)
  85.   end
  86.   function tReset() --reset terminal to initial state
  87.     term.clear()
  88.     tCursor(1,1)
  89.   end
  90.   function mReset() --reset monitor to initial state
  91.     mon.clear()
  92.     mCursor(1,1)
  93.   end
  94.  
  95.   if mon == nil then --detect if a monitor exists, error if it doesn't
  96.     tReset()
  97.     print("Error -1\n")
  98.     print("This system was designed to output to a",
  99.     "monitor, but one couldn't be found. Please",
  100.     "attach a monitor to any side of the terminal",
  101.     "or with a wired modem.")
  102.   else
  103.     tReset()
  104.     print("Process ID:", rValue)
  105.     print("Status: Running...")
  106.   end
  107.  
  108.   if matrix ~= nil then --detect if an Induction Matrix exists, set method aliases if it does
  109.     mEnergy = matrix.getEnergy()
  110.     mMaxEnergy = matrix.getMaxEnergy()
  111.     mInput = matrix.getInput()
  112.     mOutput = matrix.getOutput()
  113.     mXFerCap = matrix.getTransferCap()
  114.     sType = sType+1
  115.   end
  116.  
  117.   if bCube ~= nil then --detect if a Basic Energy Cube exists, set method aliases if it does
  118.     bEnergy = bCube.getEnergy()
  119.     bMaxEnergy = bCube.getMaxEnergy()
  120.     bEnergyNeeded = bCube.getEnergyNeeded()
  121.     bOutput = bCube.getOutput()
  122.     sType = sType+1
  123.   end
  124.  
  125.   if aCube ~= nil then --detect if an Advanced Energy Cube exists, set method aliases if it does
  126.     aEnergy = aCube.getEnergy()
  127.     aMaxEnergy = aCube.getMaxEnergy()
  128.     aEnergyNeeded = aCube.getEnergyNeeded()
  129.     aOutput = aCube.getOutput()
  130.     sType = sType+1
  131.   end
  132.  
  133.   if eCube ~= nil then --detect if an Elite Energy Cube exists, set method aliases if it does
  134.     eEnergy = eCube.getEnergy()
  135.     eMaxEnergy = eCube.getMaxEnergy()
  136.     eEnergyNeeded = eCube.getEnergyNeeded()
  137.     eOutput = eCube.getOutput()
  138.     sType = sType+1
  139.   end
  140.  
  141.   if uCube ~= nil then --detect if an Ultimate Energy Cube exists, set method aliases if it does
  142.     uEnergy = uCube.getEnergy()
  143.     uMaxEnergy = uCube.getMaxEnergy()
  144.     uEnergyNeeded = uCube.getEnergyNeeded()
  145.     uOutput = uCube.getOutput()
  146.     sType = sType+1
  147.   end
  148.  
  149.   function putNumber (number) --round numbers and make them into human readable format
  150.     local round = 0
  151.     local texts = ""
  152.  
  153.     if number >= 1000000000000000000 then
  154.       round = (number / 1000000000000000000)
  155.       texts = string.sub(round, 0, 5) .. " EJ"
  156.     elseif number >= 1000000000000000 then
  157.       round = (number / 1000000000000000)
  158.       texts = string.sub(round, 0, 5) .. " PJ"
  159.     elseif number >= 1000000000000 then
  160.       round = (number / 1000000000000)
  161.       texts = string.sub(round, 0, 5) .. " TJ"
  162.     elseif number >= 1000000000 then
  163.       round = (number / 1000000000)
  164.       texts = string.sub(round, 0, 5) .. " GJ"
  165.     elseif number >= 1000000 then
  166.       round = (number / 1000000)
  167.       texts = string.sub(round, 0, 5) .. " MJ"
  168.     elseif number >= 1000 then
  169.       round = (number / 1000)
  170.       texts = string.sub(round, 0, 5) .. " kJ"
  171.     else
  172.       round = (number)
  173.       texts = string.sub(number, 0, 5) .. " J"
  174.     end
  175.       return texts
  176.   end
  177.  
  178.   mReset()
  179.   mon.write(putNumber(bEnergy))
  180.   mCursor(1,2)
  181.   mon.write(putNumber(aEnergy))
  182.   mCursor(1,3)
  183.   mon.write(putNumber(eEnergy))
  184.   mCursor(1,4)
  185.   mon.write(putNumber(uEnergy))
  186.   mCursor(1,5)
  187.   mon.write(putNumber(mEnergy)) mon.write(putNumber(mMaxEnergy))
  188.  
  189. --[[
  190.   if sType == 0 then
  191.     mReset()
  192.     mon.write("ERROR: No power storage detected.")
  193.     mCursor(1,3)
  194.     mon.write("System TERMINATED.")
  195.     mCursor(1,5)
  196.     mon.write("Please connect power storage")
  197.     tReset()
  198.     break
  199.   elseif sType > 1 then
  200.     mReset()
  201.     mon.write("ERROR: Multi-Type Storage Detected")
  202.     mCursor(1,3)
  203.     mon.write("System TERMINATED")
  204.     tReset()
  205.     break
  206.   end
  207. --]]
  208.   sleep(1) --wait for 1sec
  209. end --exit our loop
Advertisement
Add Comment
Please, Sign In to add comment