Advertisement
HydrantHunter

Tank Monitor 2

Apr 13th, 2014
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.06 KB | None | 0 0
  1. --[[   Tank Monitor 2  ]]--
  2. --[[       by Dog      ]]--
  3. --[[ aka HydrantHunter ]]--
  4. --[[  based on an AaP  ]]--
  5. --[[ thread started by ]]--
  6. --[[      Zatilla7     ]]--
  7. --[[ pastebin iuKauV9d ]]--
  8.  
  9. --[[
  10.   - Requires:
  11.     - Computer (standard or advanced)
  12.     - 3-wide advanced monitor array (2 tall or better recommended)
  13.     - Tanks to monitor
  14.       - Tanks and monitor array can be connected directly or via modem/network cable
  15.   - MC 1.6.4 or 1.7.10 ... maybe 1.8.9 and beyond (not tested)
  16.   - ComputerCraft 1.58 (untested) -> 1.63+
  17. ]]--
  18.  
  19. --# predeclarations
  20. local tArgs = { ... }                                                      --# declare, localize, and capture any command line arguments with this variable
  21. local mon                                                                  --# declare and localize our monitor wrapping variable
  22. local tankList = { }                                                       --# declare, localize, and set our tank list wrapping variable with an empty table
  23. local validTanks = {                                                       --# declare ,localize, and set the table of tanks we are looking for
  24.   ender_tank = true,                                                       --# Ender tank
  25.   drum = true,                                                             --# Extra Utilities drum - untested
  26.   tileentitycertustank = true,                                             --# Extra Cells certus tank - untested
  27.   tconstruct_lavatank = true,                                              --# Tinkerer's Construct lava tank - untested
  28.   openblocks_tank = true,                                                  --# OpenBlocks tank
  29.   factory2_1 = true,                                                       --# Forestry Raintank
  30.   water_tank = true,                                                       --# Railcraft water tank
  31.   rcirontankvalvetile = true,                                              --# Railcraft iron tank valve
  32.   rcsteeltankvalvetile = true,                                             --# Railcraft steel tank valve
  33.   rcboilertanklowtile = true,                                              --# Railcraft low pressure boiler tank - untested
  34.   rcboilertankhightile = true,                                             --# Railcraft high pressure boiler tank - untested
  35.   cofh_thermalexpansion_tank = true,                                       --# Thermal Expansion portable tank
  36.   net_minecraft_src_buildcraft_factory_tiletank = true,                    --# Buildcraft tank
  37. }
  38. --# look for monitors and tanks (both directly connected and connected via network cable)
  39. for _, side in pairs(rs.getSides()) do                                     --# this generic loop calls redstone.getSides() to look at each side of the computer
  40.   if peripheral.isPresent(side) then                                       --# if we find a perhiperal...
  41.     if validTanks[peripheral.getType(side)] then                           --# we check it against the list of tanks and if we find one...
  42.       tankList[#tankList + 1] = peripheral.wrap(side)                      --# ...we wrap it while simultaneously incrementing our tankList index number
  43.     elseif peripheral.getType(side) == "monitor" then                      --# if we find a monitor...
  44.       mon = peripheral.wrap(side)                                          --# ...we wrap it
  45.     elseif peripheral.getType(side) == "modem" then                        --# if we find a modem, we...
  46.       if not peripheral.call(side,"isWireless") then                       --# ...eliminate wireless modems and...
  47.         for _, device in pairs(peripheral.call(side, "getNamesRemote")) do --# ...look for network attached peripherals using another generic loop
  48.           for tank in pairs(validTanks) do                                 --# we start yet another generic loop that allows us to compare a :sub of our validTanks against the discovered peripheral
  49.             if validTanks[device:sub(1, #tostring(tank))] then             --# we check the peripheral against the list of tanks and if we find one...
  50.               tankList[#tankList + 1] = peripheral.wrap(device)            --# ...we wrap it while simultaneously incrementing our tankList index number then...
  51.               break                                                        --# ...break out of this loop to continue the 'outter loop'
  52.             end
  53.           end
  54.           if peripheral.getType(device) == "monitor" then                  --# if we find a monitor...
  55.             mon = peripheral.wrap(device)                                  --# ...we wrap it
  56.           end
  57.         end
  58.       end
  59.     end
  60.   end
  61. end
  62. term.clear()                                                               --# clear the terminal screen...
  63. term.setCursorPos(2, 2)                                                    --# ...and set the cursor position for any terminal output
  64. --# exit if no tanks are found
  65. if not tankList[1] then
  66.   term.write("No known tanks found.")
  67.   term.setCursorPos(2, 4)
  68.   term.write("Tank Monitor is OFFLINE")
  69.   term.setCursorPos(2, 6)
  70. end
  71. --# prepare display (and exit if no monitor is found)
  72. if not mon then                                                            --# if we don't find a monitor...
  73.   term.write("No monitor found.")                                          --# ...inform the user then...
  74.   term.setCursorPos(2, 3)
  75.   term.write("A monitor is required.")
  76.   term.setCursorPos(2, 5)
  77.   term.write("Tank Monitor is OFFLINE")
  78.   term.setCursorPos(1, 7)                                                  --# ...set cursor position for post-exit and...
  79.   return                                                                   --# ...quit and exit the program
  80. end
  81. mon.setTextScale(1)                                                        --# valid text scales are 0.5 to 5 in increments of 0.5
  82. mon.clear()                                                                --# clear the monitor
  83. local monX, monY = mon.getSize()                                           --# delcare, localize, and set our variables to store monitor width and height
  84. --# Color Definitions
  85. local white = colors.white                                                 --# declare, localize, and set variables for the colors we'll use
  86. local black = colors.black                                                 --#   (so they're easier to type and redefine for b&w)
  87. local silver = colors.lightGray
  88. local orange = colors.orange
  89. local yellow = colors.yellow
  90. local red = colors.red
  91. local green = colors.green
  92. local sky = colors.lightBlue
  93. if not mon.isColor() then                                                  --# if the monitor is not color...
  94.   silver = colors.white                                                    --# ...we set all our not-black colors to white
  95.   orange = colors.white
  96.   yellow = colors.white
  97.   red = colors.white
  98.   green = colors.white
  99.   sky = colors.white
  100. end
  101.  
  102. --# declare and localize function to gather and display tank info
  103. local function monitorTanks()
  104.   while true do
  105.     local yPos = 0                                                         --# declare, localize, and set cursor Y position variable to 0
  106.     for tankNum = 1, #tankList do                                          --# start parsing our list of tanks (1, 2, 3, ...)
  107.       local tankInfo = tankList[tankNum].getTankInfo("unknown")            --# get tank's info
  108.       local tankStats = tankInfo[1]                                        --# take just the table of info we want from the info returned by the tank
  109.       yPos = yPos + 1                                                      --# increment cursor Y position variable
  110.       mon.setCursorPos(1, yPos)                                            --# set the cursor position
  111.       mon.setTextColor(silver)                                             --# set the color for tank #
  112.       mon.write("Tank " .. tostring(tankNum) .. ": ")                      --# display the tank number (1, 2, 3, ...)
  113.       mon.setTextColor(white)                                              --# set the color for tank contents rawName or EMPTY
  114.       if tankStats.rawName or tankStats.contents.rawName then              --# if we have actual tank data (the tank isn't empty) then ...
  115.         mon.write((tankStats.rawName and tankStats.rawName or tankStats.contents.rawName) .. string.rep(" ", monX)) --# ...display rawName of tank contents (this is the 'descriptive' name)
  116.         yPos = yPos + 1                                                    --# increment cursor Y position variable
  117.         mon.setCursorPos(1, yPos)                                          --# set the cursor position
  118.         local tankPercent = ((tankStats.amount and tankStats.amount or tankStats.contents.amount) / tankStats.capacity) * 100 --# declare, localize, and set the variable to hold the tank's fill percentage
  119.         if tankPercent < 10 then                                           --# if our tank is less than 10% full...
  120.           tankPercent = tonumber(tostring(tankPercent):sub(1, 4))          --# ...we take the first 4 digits
  121.         else                                                               --# ...otherwise...
  122.           tankPercent = tonumber(tostring(tankPercent):sub(1, 5))          --# ...we take the first 5 digits
  123.         end
  124.         if tankPercent > 74 then                                           --# if the tank is more than 74% full...
  125.           mon.setTextColor(green)                                          --# ...set the color to green
  126.         elseif tankPercent > 49 and tankPercent < 75 then                  --# if the tank is between 50%-74% full...
  127.           mon.setTextColor(sky)                                            --# ...set the color to light blue
  128.         elseif tankPercent > 24 and tankPercent < 50 then                  --# if the tank is between 25%-49% full...
  129.           mon.setTextColor(yellow)                                         --# ...set the color to yellow
  130.         elseif tankPercent < 25 then                                       --# if the tank is less than 25% full...
  131.           mon.setTextColor(orange)                                         --# ...set the color to orange
  132.         end
  133.         mon.write((tankStats.amount and tankStats.amount or tankStats.contents.amount) .. " of " .. tankStats.capacity .. " (" .. tankPercent .. "%)" .. string.rep(" ", monX)) --# display stored amount and total capacity
  134.       else                                                                 --# if the tank has no contents...
  135.         mon.write("EMPTY")                                                 --# ...display 'EMPTY'
  136.         yPos = yPos + 1                                                    --# increment cursor Y position variable
  137.         mon.setCursorPos(1, yPos)                                          --# set the cursor position
  138.         mon.setTextColor(red)                                              --# set the color for an empty tank
  139.         mon.write("0 of " .. tankStats.capacity .. " (0%)" .. string.rep(monX)) --# display empty amount and total capacity
  140.       end
  141.       yPos = yPos + 1                                                      --# increment cursor Y position variable
  142.     end
  143.     sleep(1)                                                               --# sleep for 1 second before refreshing the tank info
  144.   end
  145. end
  146.  
  147. --# declare and localize function to capture character input
  148. local function keyPress()
  149.   while true do                                                            --# start an infinite loop
  150.     local _, char = os.pullEvent("char")                                   --# declare, localize, and capture the data returned from a "char" event
  151.     if string.lower(char) == "q" then                                      --# if 'q' is pressed then...
  152.       return                                                               --# ...exit this loop, which will result in the program quitting and exiting
  153.     end
  154.   end
  155. end
  156.  
  157. --# declare and localize function to run another program or the CC shell in tandem
  158. local function foregroundShell()
  159.   local tUnpack = unpack or table.unpack                                   --# determine which table unpack routine is available to use
  160.   term.clear()
  161.   term.setCursorPos(1, 1)
  162.   if fs.exists(tArgs[1]) then                                              --# if the file specified by tArgs[1] exists...
  163.       shell.run(tUnpack(tArgs))                                              --# ...run it...
  164.   else                                                                     --# ...otherwise...
  165.     shell.run("shell")                                                     --# ...run the standard shell
  166.   end
  167. end
  168.  
  169. --# start the program
  170. if tArgs[1] then                                                           --# if we are set to run in the background (by command line argument)...
  171.   parallel.waitForAny(monitorTanks, foregroundShell)                       --# ...use the parallel api to wait for activity from either function (monitorTanks() or foregroundShell())
  172. else                                                                       --# otherwise...
  173.   term.write("Monitoring tanks...")                                        --# ...display something on the terminal so the user knows the program is running then...
  174.   term.setCursorPos(2, 4)
  175.   term.write("press 'q' to quit")
  176.   parallel.waitForAny(monitorTanks, keyPress)                              --# ...use the parallel api to wait for activity from either function (monitorTanks() or keyPress())
  177. end
  178. --# do this if the program gracefully exits from a running state
  179. mon.clear()                                                                --# if the shell or program is exited, clear the monitor...
  180. term.clear()                                                               --# ...clear the screen...
  181. term.setCursorPos(1, 1)
  182. term.write("Tank Monitor is OFFLINE")                                      --# ...inform the user and...
  183. term.setCursorPos(1, 3)                                                    --# ...set the cursor position for post-exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement