Advertisement
Guest User

CC Tank Monitor Program for OpenBlocks Tank

a guest
Jul 2nd, 2024
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.46 KB | None | 0 0
  1. local m = peripheral.wrap( "top" )      --monitor
  2.  
  3. -- warp the tanks as peripherals  !!! THE NAMES FOR THE PERIPHERALS NEED TO BE CHANGED TO YOUR HOOKED UP SETUP !!!
  4. local tank4 = peripheral.wrap("openblocks_tank_8")  -- top
  5. local tank3 = peripheral.wrap("openblocks_tank_7")  -- middle top
  6. local tank2 = peripheral.wrap("openblocks_tank_6")  -- middle bottom
  7. local tank1 = peripheral.wrap("openblocks_tank_5")  -- bottom
  8.  
  9. -- setting the dimensions of the tank you have  !!! THESE VALUES NEED TO BE CHANGED ACOORDING TO YOUR TANK SIZE !!!
  10. tankStateTable = {}
  11. tankStateTable.hight = 4
  12. tankStateTable.width = 12
  13. tankStateTable.lenght = 14
  14.  
  15. -- Init the Disply values
  16. tankStateTable.singleTankCapacity = tank1.getTankInfo()[1].capacity --16000
  17. tankStateTable.currentFillstate = 0
  18. tankStateTable.tankCapacity = (tankStateTable.hight * tankStateTable.width * tankStateTable.lenght * tankStateTable.singleTankCapacity) -- h * W * L * Cpacity
  19.  
  20. -- padding the String to the left
  21. local function padLeft(str, w)
  22.     return string.rep(" ", w - #str) .. str
  23. end
  24.  
  25. -- function for number formatting to better read the numbers and group them into three
  26. function formatNumberWithGrouping(number)
  27.     local numberString = tostring(number)
  28.     local parts = {}
  29.    
  30.     -- Loop backwards throug the number
  31.     for i = #numberString, 1, -1 do
  32.         -- add the current value to the table
  33.         table.insert(parts, 1, numberString:sub(i, i))
  34.         -- Put a point after every thrid number
  35.         if ( (#numberString - i + 1) % 3 == 0 ) and (i > 1) then
  36.             table.insert(parts, 1, ".")
  37.         end
  38.     end
  39.    
  40.     -- make the table back to a string
  41.     return table.concat(parts)
  42. end
  43.  
  44.  
  45. -- calculate the current fillsate of the tank
  46. local function calculateTankFillState()
  47.     local tank1current = tank1.getTankInfo()[1].contents.amount
  48.     local tank2current = tank2.getTankInfo()[1].contents.amount
  49.     local tank3current = tank3.getTankInfo()[1].contents.amount
  50.     local tank4current = tank4.getTankInfo()[1].contents.amount
  51.  
  52.     tank1current = tank1current * tankStateTable.width * tankStateTable.lenght
  53.     tank2current = tank2current * tankStateTable.width * tankStateTable.lenght
  54.     tank3current = tank3current * tankStateTable.width * tankStateTable.lenght
  55.     tank4current = tank4current * tankStateTable.width * tankStateTable.lenght
  56.    
  57.     tankStateTable.currentFillstate = (tank1current + tank2current + tank3current + tank4current)
  58. end
  59.  
  60. -- updates the Monitor and displays the current fill state
  61. local function mainUILOOP()
  62.  
  63.     m.setTextScale(0.5)
  64.     local w, h = m.getSize()
  65.     local total = tankStateTable.tankCapacity
  66.  
  67.     --start timer for calculateing and updating every 5 sek
  68.     os.startTimer(5)
  69.     while true do
  70.         local stored = 0
  71.        
  72.         calculateTankFillState()
  73.         stored = tankStateTable.currentFillstate
  74.  
  75.         m.clear()
  76.         m.setCursorPos(1, 2)
  77.         m.setTextColour(colours.orange)
  78.         m.write("Fuel Current: ")
  79.         m.setTextColour(colours.white)
  80.         m.write(padLeft(tostring(formatNumberWithGrouping(stored) .. " mB"), w - 14))
  81.  
  82.         m.setCursorPos(1, 4)
  83.         m.setTextColour(colours.orange)
  84.         m.write("Total Capacity:")
  85.         m.setTextColour(colours.white)
  86.         m.write(padLeft(tostring(formatNumberWithGrouping(total) .. " mB"), w - 15))
  87.  
  88.         m.setCursorPos(1, 6)
  89.         m.setTextColour(colors.orange)
  90.         m.write("%:")
  91.         m.setTextColour(colors.white)
  92.         m.write(padLeft(tostring((stored/total) * 100), w - 3))
  93.  
  94.         local p = (w-2) * stored / total
  95.  
  96.         m.setBackgroundColour(colours.lightGrey)
  97.         m.setCursorPos(2, 8)
  98.         m.write(string.rep(" ", w-2))
  99.         m.setBackgroundColour(colours.red)
  100.         m.setCursorPos(2, 8)
  101.         m.write(string.rep(" ", p))
  102.         m.setBackgroundColour(colours.black)
  103.  
  104.         os.pullEvent("timer")
  105.         os.startTimer(5)
  106.     end
  107. end
  108.  
  109. -- the main funrction start for the programm
  110. local function main()
  111.  
  112.     --Debug stuff----------------------------------------------------------
  113.  
  114.     --local table = tank3.getTankInfo()
  115.     --peripheral.getMethods("openblocks_tank_5")
  116.  
  117.     --print("____________________________________ \n \n")
  118.     --print(table)
  119.  
  120.     --for index, value in pairs(tankFillStateTable) do
  121.     --    print(index, "  ",value)
  122.     --end
  123.     --calculateTankFillState()
  124.     -------------------------------------------------------------
  125.  
  126.     mainUILOOP()
  127. end
  128.  
  129. main()
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement