Advertisement
Armitige

Turbine Monitor/Control

Jan 19th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.30 KB | None | 0 0
  1. -- Turbine  Monitor
  2.  
  3. local t = peripheral.find("BigReactors-Turbine")
  4. local m = peripheral.find("monitor")
  5.  
  6. function formatNumToString(val, sp, mod)
  7.         local nStr = round(val, mod)
  8.         nStr = tostring(nStr)
  9.         nStr = string.rep(" ", sp - nStr:len())..nStr
  10.         return nStr
  11. end --function formatString(val, sp)
  12.  
  13. function round(num, i)
  14.         local mult = 10^(i or 0)
  15.         return math.floor(num * mult + 0.5) / mult
  16. end --function round(num, i)
  17.  
  18. function rightJustify(w, sLen, mod)
  19.         local x = math.floor(w) - math.ceil(sLen + mod)
  20.         return x
  21. end --funtion rightJustify(w, sLen, mod)
  22.  
  23. local function writeAt(text, x, y, foreCol, backCol)
  24.         if foreCol then term.setTextColour(foreCol) end
  25.         if backCol then term.setBackgroundColour(backCol) end
  26.         if x or y then
  27.                 local curX, curY = term.getCursorPos()
  28.                 term.setCursorPos(x or curX, y or curY)
  29.         end
  30.        
  31.         term.write(text)
  32. end --function writeAt(text, x, y, foreCol, backCol)
  33.  
  34. local function drawScreen()
  35.     local x, y = m.getSize()
  36.     term.redirect(m)
  37.         paintutils.drawLine(1, 1, x, 1, colors.green)
  38.         paintutils.drawLine(1, y, x, y, colors.green)
  39.         paintutils.drawLine(1, 1, 1, y, colors.green)
  40.         paintutils.drawLine(x, 1, x, y, colors.green)
  41.         writeAt("TURBINE 1", 9, 1, colors.black, colors.green)
  42.         writeAt("Turbine:", 2, 3, colors.cyan, colors.black)
  43.         writeAt("Turbine Speed:", 2, 4)
  44.         writeAt("Power Output:", 2, 5)
  45.         writeAt("Power Stored:", 2, 6)
  46.         writeAt("Coil:", 2, 7)
  47.         writeAt("NOT PRESENT", 13, 3, colors.red)
  48.  
  49. end -- function drawScreen()
  50.  
  51. function getTurbineStats()
  52.         local tStats = {} --array matrix for turbine stats.
  53.         tStats[1] = t.getRotorSpeed()
  54.         tStats[2] = t.getEnergyStored()
  55.         tStats[3] = t.getEnergyProducedLastTick()
  56.         tStats[4] = t.getActive()
  57.         tStats[5] = t.getInductorEngaged()
  58.         return tStats --returns the array matrix of stored stats
  59. end --function getTurbineStats()
  60.  
  61. function displayStats()
  62.         local tStats = getTurbineStats() --gets turbine stats
  63.         local w = 0
  64.         local rpm = tStats[1]
  65.         local eStor = tStats[2]
  66.         local eProd = tStats[3]
  67.         local act = tStats[4]
  68.         local cEng = tStats[5]
  69.         rpm = formatNumToString(rpm, 8, 1)
  70.         eStor = formatNumToString(eStor, 8, 0)
  71.         eProd = formatNumToString(eProd, 8, 0)
  72.         term.redirect(m)
  73.         local w = term.getSize()
  74.         if act then -- turbine active/inactive display
  75.                 writeAt("     ACTIVE", 13, 3, colors.lime)    
  76.         else
  77.                 writeAt("   INACTIVE", 13, 3, colors.red)
  78.         end -- if act then
  79.         writeAt(rpm, rightJustify(w, rpm:len(), 1), 4, colors.white)
  80.         writeAt(eProd, rightJustify(w, eProd:len(), 1), 5)
  81.         writeAt(eStor, rightJustify(w, eStor:len(), 1), 6)
  82.         if cEng then --coil engaged/disengaged display
  83.                 writeAt("   ENGAGED", 14, 7, colors.lime, colors.black)
  84.         else
  85.                 writeAt("DISENGAGED", 14, 7, colors.red, colors.black)
  86.         end --if cEng then
  87. end --function displayStats()
  88.  
  89. m.clear()
  90. drawScreen()
  91.  
  92. while true do
  93.     getTurbineStats()
  94.     displayStats()
  95.     os.sleep(1)
  96. end -- while true do
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement