Advertisement
mydexterid

display_tanks

Jun 30th, 2013
3,930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.11 KB | None | 0 0
  1. --[[
  2.     display_tanks v1.0 for OpenCCSensors 0.1.4c
  3.     Copyright (C) 2013 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. -- Position of your modem
  20. modem_position = "left"
  21.  
  22. -- Position of your monitor
  23. monitor_position = "top"
  24.  
  25. -- The text scale of your monitor (between 0.5 and 5)
  26. monitor_scale = 1
  27.  
  28. -- The modem channel to receive sensor data
  29. -- This has to be the same channel you set in send_tank_info program
  30. modem_channel = 3
  31.  
  32. -- Whether to show or not the percentage in numberic form above the graphs
  33. -- Note: This will only work if there is enough horizontal space to write the numbers
  34. --       So if the graphs width is less than 4 characters you won't see the numbers
  35. --       even if you set this to true
  36. show_percentage = true
  37.  
  38. -- The colors you want to see your liquids in
  39. -- You can add more liquids, or change the colors here
  40. tank_colors={
  41. {name="Lava", color=colors.red},
  42. {name="Water", color=colors.blue},
  43. {name="Biofuel", color=colors.orange},
  44. {name="item.mobessenceitem", color=colors.green},
  45. {name="Creosote Oil", color=colors.brown},
  46. {name="Fuel", color=colors.yellow},
  47. {name="Oil", color=colors.gray},
  48. {name="Biomass", color=colors.green},
  49. {name="Sludge", color=colors.blue},
  50. {name="Sewage", color=colors.brown},
  51. {name="Liquid DNA", color=colors.pink},
  52. {name="Honey", color=colors.yellow},
  53. {name="Fruit Juice", color=colors.lime},
  54. {name="Milk", color=colors.white},
  55. {name="Seed Oil", color=colors.yellow},
  56. {name="Ethanol", color=colors.orange},
  57. {name="Pink Slime", color=colors.pink},
  58. {name="Molten Iron", color=colors.red},
  59. {name="Molten Gold", color=colors.yellow},
  60. {name="Molten Ardite", color=colors.brown},
  61. {name="Molten Copper", color=colors.orange},
  62. {name="Molten Bronze", color=colors.brown},
  63. {name="Molten Aluminum Brass", color=colors.yellow},
  64. {name="Molten Manyullyn", color=colors.purple},
  65. {name="Molten Alumite", color=colors.pink},
  66. {name="Molten Obsidian", color=colors.purple},
  67. }
  68.  
  69.  
  70. -----------------------------------------
  71. -- DO NOT MODIFY ANYTHING FROM HERE ON --
  72. -----------------------------------------
  73. modem = peripheral.wrap(modem_position)
  74. monitor = peripheral.wrap(monitor_position)
  75.  
  76. monitor.setTextScale(monitor_scale)
  77. modem.open(modem_channel)
  78.  
  79. unique_tank_hash = {}
  80.  
  81. function tableLength(T)
  82.   local count = 0
  83.   for _ in pairs(T) do
  84.     count = count + 1
  85.   end
  86.   return count
  87. end
  88.  
  89. function getColor(data)
  90.   for _, v in pairs(tank_colors) do
  91.     if (v.name == data.Name) or
  92.        (v.name == data.RawName) then
  93.       return v.color
  94.     end
  95.   end
  96.   return colors.white
  97. end
  98.  
  99. function drawGraph()
  100.   local graph_width=0
  101.   local tanks_num = tableLength(unique_tank_hash)
  102.   local percentage_line = 0
  103.  
  104.   if tanks_num == 0 then
  105.     return
  106.   end
  107.  
  108.   while graph_width <= 0 do
  109.     graph_width = math.floor((width-tanks_num+1) / tanks_num)
  110.     tanks_num = tanks_num - 1
  111.   end
  112.   tanks_num = tanks_num + 1
  113.  
  114.   if (show_percentage) and
  115.      (graph_width > 4) then
  116.     percentage_line = 1
  117.   end
  118.  
  119.   local count=0
  120.   for k, v in pairs(unique_tank_hash) do
  121.     local x = (count * graph_width) + 1 + count
  122.     local x_end = x + graph_width -1
  123.     percent = v.Amount / v.Capacity
  124.     percentage = (height-percentage_line) * percent
  125.    
  126.     y = (height-percentage_line) - math.floor(percentage + 0.5) + percentage_line
  127.  
  128.     if percentage_line == 1 then
  129.       local round_percent = math.floor((percent*100) + 0.5)
  130.       local text = round_percent.."%"
  131.       local x_pos = x + math.floor((graph_width-string.len(text))/2)
  132.       monitor.setCursorPos(x_pos,1)
  133.       monitor.setBackgroundColor(colors.black)
  134.       monitor.setTextColor(colors.white)
  135.  
  136.       monitor.write(text)
  137.     end
  138.  
  139.     for i=1+percentage_line,height do
  140.       for j=x,x_end do
  141.         local c = colors.black
  142.         if i > y then
  143.           c = getColor(v)
  144.         end
  145.         monitor.setBackgroundColor(c)
  146.         monitor.setCursorPos(j, i)
  147.         monitor.write(" ")
  148.       end
  149.     end
  150.  
  151.     count = count + 1
  152.     if count >= tanks_num then
  153.       return
  154.     end
  155.   end
  156. end
  157.  
  158. function removeTanks(temp_tank_hash, sensor_id)
  159.   local deletables = {}
  160.   local temp_tank_hash_empty = false
  161.   if not next(temp_tank_hash) then
  162.     temp_tank_hash_empty = true
  163.   end
  164.  
  165.   for k, v in pairs(unique_tank_hash) do
  166.     local other_hash = textutils.unserialize(k)
  167.     if other_hash[1] == sensor_id then
  168.       if temp_tank_hash_empty then
  169.         deletables[k] = k
  170.       else
  171.         local found = false
  172.         for i, j in pairs(temp_tank_hash) do
  173.           if k == i then
  174.             found = true
  175.             break
  176.           end
  177.         end
  178.         if not found then
  179.           deletables[k] = k
  180.         end
  181.       end
  182.     end
  183.   end
  184.   for k, v in pairs(deletables) do
  185.     unique_tank_hash[k] = nil
  186.   end
  187. end
  188.  
  189. while true do
  190.   local event, modemSide, senderChannel,
  191.      replyChannel, message, senderDistance = os.pullEvent("modem_message")
  192.   unser_message = textutils.unserialize(message)
  193.  
  194.   sensor_id = unser_message.id
  195.   sensor_data = unser_message.data
  196.  
  197.   local temp_tank_hash = {}
  198.  
  199.   for i, v in ipairs(sensor_data) do
  200.     tanks=v.Tanks
  201.     position=v.Position
  202.  
  203.     for j, value in ipairs(tanks) do
  204.       tank = value    
  205.       local hash={sensor_id, position, j}
  206.       unique_tank_hash[textutils.serialize(hash)] = tank
  207.       temp_tank_hash[textutils.serialize(hash)] = tank
  208.     end
  209.   end
  210.  
  211.   removeTanks(temp_tank_hash, sensor_id)
  212.  
  213.   monitor.setBackgroundColor(colors.black)
  214.   monitor.clear()
  215.   width, height = monitor.getSize()
  216.   drawGraph()
  217. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement