Advertisement
mydexterid

display_tanks_v2

Jul 29th, 2014
3,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.     display_tanks_v2 v2.0 for OpenPeripheral 0.4.1
  3.     Copyright (C) 2014 DEXTER
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. --]]
  18.  
  19. -- The text scale of your monitor (between 0.5 and 5)
  20. monitor_scale = 1
  21.  
  22. -- The modem channel to receive sensor data
  23. -- This has to be the same channel you set in send_tank_info program
  24. modem_channel = 3
  25.  
  26. -- Whether to show or not the percentage in numberic form above the graphs
  27. -- Note: This will only work if there is enough horizontal space to write the numbers
  28. --       So if the graphs width is less than 4 characters you won't see the numbers
  29. --       even if you set this to true
  30. show_percentage = true
  31.  
  32. -- The colors you want to see your liquids in
  33. -- You can add more liquids, or change the colors here
  34. tank_colors={
  35. {name="Lava", color=colors.red},
  36. {name="Water", color=colors.blue},
  37. {name="Biofuel", color=colors.orange},
  38. {name="item.mobessenceitem", color=colors.green},
  39. {name="Creosote Oil", color=colors.brown},
  40. {name="Fuel", color=colors.yellow},
  41. {name="Oil", color=colors.gray},
  42. {name="Biomass", color=colors.green},
  43. {name="Sludge", color=colors.blue},
  44. {name="Sewage", color=colors.brown},
  45. {name="Liquid DNA", color=colors.pink},
  46. {name="Honey", color=colors.yellow},
  47. {name="Fruit Juice", color=colors.lime},
  48. {name="Milk", color=colors.white},
  49. {name="Seed Oil", color=colors.yellow},
  50. {name="Ethanol", color=colors.orange},
  51. {name="Pink Slime", color=colors.pink},
  52. {name="Molten Iron", color=colors.red},
  53. {name="Molten Gold", color=colors.yellow},
  54. {name="Molten Ardite", color=colors.brown},
  55. {name="Molten Copper", color=colors.orange},
  56. {name="Molten Bronze", color=colors.brown},
  57. {name="Molten Aluminum Brass", color=colors.yellow},
  58. {name="Molten Manyullyn", color=colors.purple},
  59. {name="Molten Alumite", color=colors.pink},
  60. {name="Molten Aluminum", color=colors.pink},
  61. {name="Molten Obsidian", color=colors.purple},
  62. {name="Molten Cobalt", color=colors.blue},
  63. {name="Molten Steel", color=colors.lightGray},
  64. {name="Molten Glass", color=colors.lightBlue},
  65. {name="Seared Stone", color=colors.gray},
  66. {name="Liquified Emerald", color=colors.lime},
  67. {name="Blood", color=colors.red},
  68. {name="Molten Nickel", color=colors.yellow},
  69. {name="Molten Lead", color=colors.purple},
  70. {name="Molten Silver", color=colors.cyan},
  71. {name="fluid.platinum.molten", color=colors.cyan},
  72. {name="Molten Invar", color=colors.lightGray},
  73. {name="Molten Electrum", color=colors.yellow},
  74. {name="Resonant Ender", color=colors.green},
  75. {name="Liquid Blueslime", color=colors.lightBlue},
  76. {name="Pig Iron", color=colors.pink},
  77. {name="Liquid XP", color=colors.lime},
  78. {name="Destabilized Redstone", color=colors.red},
  79. {name="Energized Glowstone", color=colors.yellow},
  80. {name="Blazing Pyrotheum", color=colors.orange},
  81. {name="Gelid Cryotheum", color=colors.cyan},
  82. {name="coal", color=colors.gray},
  83. }
  84.  
  85.  
  86. -----------------------------------------
  87. -- DO NOT MODIFY ANYTHING FROM HERE ON --
  88. -----------------------------------------
  89.  
  90. modem = peripheral.find("modem")
  91. if not modem then
  92.   error("Please attach one wireless modem")
  93. end
  94.  
  95. modem.open(modem_channel)
  96.  
  97. unique_tank_hash = {}
  98.  
  99. function tableLength(T)
  100.   local count = 0
  101.   for _ in pairs(T) do
  102.     count = count + 1
  103.   end
  104.   return count
  105. end
  106.  
  107. function getColor(data)
  108.   for _, v in pairs(tank_colors) do
  109.     if (v.name == data.name) or
  110.        (v.name == data.rawName) then
  111.       return v.color
  112.     end
  113.   end
  114.   return colors.white
  115. end
  116.  
  117. function drawGraph()
  118.   local graph_width=0
  119.   local tanks_num = tableLength(unique_tank_hash)
  120.   local percentage_line = 0
  121.  
  122.   if tanks_num == 0 then
  123.     return
  124.   end
  125.  
  126.   while graph_width <= 0 do
  127.     graph_width = math.floor((width-tanks_num+1) / tanks_num)
  128.     tanks_num = tanks_num - 1
  129.   end
  130.   tanks_num = tanks_num + 1
  131.  
  132.   if (show_percentage) and
  133.      (graph_width > 4) then
  134.     percentage_line = 1
  135.   end
  136.  
  137.   local count=0
  138.   for k, v in pairs(unique_tank_hash) do
  139.     local x = (count * graph_width) + 1 + count
  140.     local x_end = x + graph_width -1
  141.     if v.amount == nil then
  142.       v.amount = 0
  143.     end
  144.     if v.capacity == nil then
  145.       v.capacity = 1
  146.     end
  147.     percent = v.amount / v.capacity
  148.     percentage = (height-percentage_line) * percent
  149.    
  150.     y = (height-percentage_line) - math.floor(percentage + 0.5) + percentage_line
  151.  
  152.     if percentage_line == 1 then
  153.       local round_percent = math.floor((percent*100) + 0.5)
  154.       local text = round_percent.."%"
  155.       local x_pos = x + math.floor((graph_width-string.len(text))/2)
  156.       monitor.setCursorPos(x_pos,1)
  157.       monitor.setBackgroundColor(colors.black)
  158.       monitor.setTextColor(colors.white)
  159.  
  160.       monitor.write(text)
  161.     end
  162.  
  163.     for i=1+percentage_line,height do
  164.       for j=x,x_end do
  165.         local c = colors.black
  166.         if i > y then
  167.           c = getColor(v)
  168.         end
  169.         monitor.setBackgroundColor(c)
  170.         monitor.setCursorPos(j, i)
  171.         monitor.write(" ")
  172.       end
  173.     end
  174.  
  175.     count = count + 1
  176.     if count >= tanks_num then
  177.       return
  178.     end
  179.   end
  180. end
  181.  
  182. function removeTanks(temp_tank_hash, sensor_id)
  183.   local deletables = {}
  184.   local temp_tank_hash_empty = false
  185.   if not next(temp_tank_hash) then
  186.     temp_tank_hash_empty = true
  187.   end
  188.  
  189.   for k, v in pairs(unique_tank_hash) do
  190.     local other_hash = textutils.unserialize(k)
  191.     if other_hash[1] == sensor_id then
  192.       if temp_tank_hash_empty then
  193.         deletables[k] = k
  194.       else
  195.         local found = false
  196.         for i, j in pairs(temp_tank_hash) do
  197.           if k == i then
  198.             found = true
  199.             break
  200.           end
  201.         end
  202.         if not found then
  203.           deletables[k] = k
  204.         end
  205.       end
  206.     end
  207.   end
  208.   for k, v in pairs(deletables) do
  209.     unique_tank_hash[k] = nil
  210.   end
  211. end
  212.  
  213. while true do
  214.   local event, modemSide, senderChannel,
  215.      replyChannel, message, senderDistance = os.pullEvent("modem_message")
  216.   unser_message = textutils.unserialize(message)
  217.  
  218.   tank_id = unser_message.id
  219.   tank_data = unser_message.tanks
  220.  
  221.   local temp_tank_hash = {}
  222.  
  223.   for i, v in ipairs(tank_data) do
  224.     tank=v.data
  225.     tank_position=v.position
  226.  
  227.     local hash={tank_id, tank_position, i}
  228.     unique_tank_hash[textutils.serialize(hash)] = tank
  229.     temp_tank_hash[textutils.serialize(hash)] = tank
  230.   end
  231.  
  232.   removeTanks(temp_tank_hash, tank_id)
  233.  
  234.   monitor = peripheral.find("monitor")
  235.   if monitor then
  236.     monitor.setTextScale(monitor_scale)
  237.     monitor.setBackgroundColor(colors.black)
  238.     monitor.clear()
  239.     width, height = monitor.getSize()
  240.     drawGraph()
  241.   end
  242. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement