Advertisement
mrkmg

BigReactors - TurbineMonitor

Mar 7th, 2015
4,865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.28 KB | None | 0 0
  1. -- TurbineMonitor
  2. --  Made By: MrKMG <mrkmg.com>
  3. --
  4. -- This program will keep your turbines up to speed
  5. -- and turn the coil on and off as needed.
  6. --
  7. -- To Use:
  8. --
  9. -- Attach as many Turbines and Monitors as you
  10. -- would like via wired modems. Monitors will all
  11. -- display the same information for each turbine:
  12. --    Current Speed
  13. --    Current Power Generation,
  14. --    Coil On/Off
  15. --    Flow On/Off
  16. --
  17. -- Monitors can be of any size
  18. --
  19.  
  20. --Edit these to fit your turbines
  21.  
  22. -- Speed to keep rotors between. Recommend putting
  23. -- just above the best your turbine can do.
  24. minSpeed = 1800
  25. maxSpeed = 1820
  26.  
  27. -- Range of RF to keep in turbine. Recommend to
  28. -- keep low to avoid waste.
  29. minEnergy = 1
  30. maxEnergy = 200000
  31.  
  32. -- Colors for the monitor
  33. uiColors = {}
  34. uiColors["background"] = colors.gray
  35. uiColors["border"] = colors.black
  36. uiColors["headerBackground"] = colors.blue
  37. uiColors["headerText"] = colors.black
  38. uiColors["currentSpeedBackground"] = colors.black
  39. uiColors["currentSpeedText"] = colors.white
  40. uiColors["currentPowerGenBackground"] = colors.black
  41. uiColors["currentPowerGenText"] = colors.white
  42. uiColors["coilOnBackground"] = colors.green
  43. uiColors["coilOnText"] = colors.white
  44. uiColors["coilOffBackground"] = colors.red
  45. uiColors["coilOffText"] = colors.white
  46. uiColors["flowOnBackground"] = colors.green
  47. uiColors["flowOnText"] = colors.white
  48. uiColors["flowOffBackground"] = colors.red
  49. uiColors["flowOffText"] = colors.white
  50.  
  51. -- Text for the monitor.
  52. uiText = {}
  53. uiText["name"] = "Turbine "
  54. uiText["rpm"] = " rpm"
  55. uiText["rft"] = " RF/t"
  56. uiText["flowOn"] = "Flow: On"
  57. uiText["flowOff"] = "Flow: Off"
  58. uiText["coilsOn"] = "Coils: On"
  59. uiText["coilsOff"] = "Coils: Off"
  60.  
  61.  
  62.  
  63. -- Edit below at your your own risk
  64. -- !#!#!#!#!#!#!#!#!#!#!#!#!#!#!#!#
  65.  
  66. -- Global Variables
  67.  
  68. t = {} -- Holds turbines
  69. m = {} -- Holds monitor
  70. w = {} -- Holds windows
  71.  
  72. -- Other Functions
  73.  
  74. function round(num)
  75.     return math.floor(num + 0.5)
  76. end
  77.  
  78. -- Program Functions
  79.  
  80. -- Finds all devices. Puts turbines in `t` table
  81. -- and monitors in `m` table
  82. function getDevices()
  83.     for _, name in pairs(peripheral.getNames()) do
  84.         if peripheral.getType(name) == "BigReactors-Turbine" then
  85.             table.insert(t, peripheral.wrap(name))
  86.         end
  87.         if peripheral.getType(name) == "monitor" then
  88.             table.insert(m, peripheral.wrap(name))
  89.         end
  90.     end
  91. end
  92.  
  93. -- Determines if the coil should be turned on
  94. function needCoilOn(i)
  95.     if t[i].getEnergyStored() < minEnergy then
  96.         return true
  97.     else
  98.         return false
  99.     end
  100. end
  101.  
  102. -- Determines if the coil should be turned off
  103. function needCoilOff(i)
  104.     if t[i].getEnergyStored() > maxEnergy then
  105.         return true
  106.     else
  107.         return false
  108.     end
  109. end
  110.  
  111. -- Turns coil on
  112. function coilOn(i)
  113.     t[i].setInductorEngaged(true)
  114. end
  115.  
  116. -- Tuns coil off
  117. function coilOff(i)
  118.     t[i].setInductorEngaged(false)
  119. end
  120.  
  121. -- Checks if coil in on or off
  122. function isCoilOn(i)
  123.     return t[i].getInductorEngaged()
  124. end
  125.  
  126. -- Determines if flow should turn on
  127. function needFlowOn(i)
  128.     if t[i].getRotorSpeed() < minSpeed then
  129.         return true
  130.     else
  131.         return false
  132.     end
  133. end
  134.  
  135. -- Determines if flow should turn off
  136. function needFlowOff(i)
  137.     if t[i].getRotorSpeed() > maxSpeed then
  138.         return true
  139.     else
  140.         return false
  141.     end
  142. end
  143.  
  144. -- Turns flow on
  145. -- (Sets flow to MAX)
  146. function flowOn(i)
  147.     t[i].setFluidFlowRateMax(t[i].getFluidFlowRateMaxMax())
  148. end
  149.  
  150. -- Turns flow off
  151. -- (Sets flow to 0)
  152. function flowOff(i)
  153.     t[i].setFluidFlowRateMax(0)
  154. end
  155.  
  156. -- Checks if flow is on or off
  157. -- (Assumes MAX is on, other is off)
  158. function isFlowOn(i)
  159.     return t[i].getFluidFlowRateMax() == t[i].getFluidFlowRateMaxMax()
  160. end
  161.  
  162. -- Get Rotor Speed
  163. function getRotorSpeed(i)
  164.     return math.floor(t[i].getRotorSpeed())
  165. end
  166.  
  167. -- Get Power Generation
  168. function getPowerGeneration(i)
  169.     return math.floor(t[i].getEnergyProducedLastTick())
  170. end
  171.  
  172. -- Returns text scale for best fit
  173. -- TODO Figure out if there is a better way than
  174. -- brute forcing
  175. function getScaleForBox(baseWidth, baseHeight, fitWidth, fitHeight, count)
  176.     local best = 0.5
  177.  
  178.     for i = best, 5, 0.5 do
  179.         local cWidth = baseWidth / i
  180.         local cHeight = baseHeight / i
  181.  
  182.         local cPerRow = math.floor(cWidth / fitWidth)
  183.         local cPerCol = math.floor(cHeight / fitHeight)
  184.  
  185.         if cPerRow * cPerCol >= count then
  186.             best = i
  187.         else
  188.             break
  189.         end
  190.     end
  191.  
  192.     return best
  193. end
  194.  
  195. -- Determins offset to center the windows in the
  196. -- monitor.
  197. function getOffsetForWindow(monitorWidth, monitorHeight, desiredWidth, desiredHeight, count)
  198.     local columnsNeeded = math.min(math.floor(monitorWidth / desiredWidth), count)
  199.     local rowsNeeded = math.ceil(count / columnsNeeded)
  200.  
  201.     local widthUsed = desiredWidth * columnsNeeded
  202.     local heightUsed = desiredHeight * rowsNeeded
  203.  
  204.     local offsetLeft = math.ceil((monitorWidth - widthUsed) / 2)
  205.     local offsetRight = math.ceil((monitorHeight - heightUsed) / 2)
  206.  
  207.     return offsetLeft, offsetRight
  208. end
  209.  
  210. -- Clears each connected monitor
  211. function clearMonitors()
  212.     for i = 1, #m do
  213.         m[i].setBackgroundColor(uiColors["border"])
  214.         m[i].clear()
  215.     end
  216. end
  217.  
  218. -- Setup each monitor
  219. -- (Put a window in each monitor for each turbine)
  220. function setupMonitors()
  221.     if #t > 0 then
  222.         local monitorWidth
  223.         local monitorHeight
  224.         local desiredW = 14
  225.         local desiredH = 8
  226.         local currentX
  227.         local currentY
  228.  
  229.         for i = 1, #m do
  230.             print("Setting Up " .. i)
  231.  
  232.             table.insert(w, {})
  233.  
  234.             m[i].setTextScale(1)
  235.             monitorWidth, monitorHeight = m[i].getSize()
  236.  
  237.             print("Initial Size: " .. monitorWidth .. " " .. monitorHeight)
  238.  
  239.             scale = getScaleForBox(monitorWidth, monitorHeight, desiredW, desiredH, #t)
  240.  
  241.             print("Scale: " .. scale)
  242.  
  243.             m[i].setTextScale(scale)
  244.             monitorWidth, monitorHeight = m[i].getSize()
  245.  
  246.             offsetX, offsetY = getOffsetForWindow(monitorWidth, monitorHeight, desiredW, desiredH, #t)
  247.  
  248.             print("Offset: " .. offsetX .. " " .. offsetY)
  249.  
  250.             currentX = offsetX
  251.             currentY = offsetY
  252.  
  253.             for ii = 1, #t do
  254.                 print(currentX .. " " .. currentY .. " " .. desiredW .. " " .. desiredH)
  255.                 table.insert(w[i], window.create(m[i], currentX, currentY, desiredW, desiredH))
  256.  
  257.                 currentX = currentX + desiredW
  258.                 if (currentX + desiredW > monitorWidth) then
  259.                     currentX = offsetX
  260.                     currentY = currentY + desiredH
  261.                 end
  262.             end
  263.         end
  264.     end
  265. end
  266.  
  267. -- Write status of turn to window
  268. function writeStatus(turbine, screen)
  269.     screen.setBackgroundColor(uiColors["border"])
  270.     screen.clear()
  271.  
  272.     -- Write Title
  273.     screen.setBackgroundColor(uiColors["headerBackground"])
  274.     screen.setTextColor(uiColors["headerText"])
  275.     screen.setCursorPos(2, 2)
  276.     screen.clearLine()
  277.     screen.write(uiText["name"] .. turbine)
  278.  
  279.     --Filler
  280.     screen.setBackgroundColor(uiColors["background"])
  281.     screen.setCursorPos(1, 3)
  282.     screen.clearLine()
  283.  
  284.     -- Write Rotor Speed
  285.     screen.setBackgroundColor(uiColors["currentSpeedBackground"])
  286.     screen.setTextColor(uiColors["currentSpeedText"])
  287.     screen.setCursorPos(2, 4)
  288.     screen.clearLine()
  289.     screen.write(getRotorSpeed(turbine) .. uiText["rpm"])
  290.  
  291.     -- Write Power Generation
  292.     screen.setBackgroundColor(uiColors["currentPowerGenBackground"])
  293.     screen.setTextColor(uiColors["currentPowerGenText"])
  294.     screen.setCursorPos(2, 5)
  295.     screen.clearLine()
  296.     screen.write(getPowerGeneration(turbine) .. uiText["rft"])
  297.  
  298.     --Filler
  299.     screen.setBackgroundColor(uiColors["background"])
  300.     screen.setCursorPos(1, 6)
  301.     screen.clearLine()
  302.  
  303.     -- Write Coil Status
  304.     if isCoilOn(turbine) then
  305.         screen.setBackgroundColor(uiColors["coilOnBackground"])
  306.         screen.setTextColor(uiColors["coilOnText"])
  307.         screen.setCursorPos(2, 7)
  308.         screen.clearLine()
  309.         screen.write(uiText["coilsOn"])
  310.     else
  311.         screen.setBackgroundColor(uiColors["coilOffBackground"])
  312.         screen.setTextColor(uiColors["coilOffText"])
  313.         screen.setCursorPos(2, 7)
  314.         screen.clearLine()
  315.         screen.write(uiText["coilsOff"])
  316.     end
  317.  
  318.     -- Write Flow Status
  319.     if isFlowOn(turbine) then
  320.         screen.setBackgroundColor(uiColors["flowOnBackground"])
  321.         screen.setTextColor(uiColors["flowOnText"])
  322.         screen.setCursorPos(2, 8)
  323.         screen.clearLine()
  324.         screen.write(uiText["flowOn"])
  325.     else
  326.         screen.setBackgroundColor(uiColors["flowOffBackground"])
  327.         screen.setTextColor(uiColors["flowOffText"])
  328.         screen.setCursorPos(2, 8)
  329.         screen.clearLine()
  330.         screen.write(uiText["flowOff"])
  331.     end
  332.  
  333.     -- Draw Right Border
  334.     screen.setBackgroundColor(uiColors["border"])
  335.     for i = 1, 8 do
  336.         screen.setCursorPos(14, i)
  337.         screen.write(" ")
  338.     end
  339. end
  340.  
  341. -- Write status of each turbine to appropiate windows
  342. function writeStatuses()
  343.     for i = 1, #m do
  344.         for ii = 1, #t do
  345.             writeStatus(ii, w[i][ii])
  346.         end
  347.     end
  348. end
  349.  
  350. -- Setup
  351. function setup()
  352.     w = {}
  353.     m = {}
  354.     t = {}
  355.  
  356.     getDevices()
  357.  
  358.     if (#t == 0) then
  359.         error("No Turbines. You did something wrong")
  360.     end
  361.  
  362.     if (#m > 0) then
  363.         clearMonitors()
  364.         setupMonitors()
  365.     end
  366. end
  367.  
  368. -- Main Loop
  369. function main()
  370.     for i = 1, #t do
  371.         if isCoilOn(i) then
  372.             if needCoilOff(i) then
  373.                 print("turning off " .. i)
  374.                 coilOff(i)
  375.             end
  376.         else
  377.             if needCoilOn(i) then
  378.                 print("turning on " .. i)
  379.                 coilOn(i)
  380.             end
  381.         end
  382.         if isFlowOn(i) then
  383.             if needFlowOff(i) then
  384.                 print("stopping flow " .. i)
  385.                 flowOff(i)
  386.             end
  387.         else
  388.             if needFlowOn(i) then
  389.                 print("starting flow " .. i)
  390.                 flowOn(i)
  391.             end
  392.         end
  393.     end
  394.     writeStatuses()
  395. end
  396.  
  397. -- Program Run
  398. setup()
  399. while true do
  400.     main()
  401.     os.sleep(1)
  402. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement