Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- display_tanks v1.0 for OpenCCSensors 0.1.4c
- Copyright (C) 2013 DEXTER
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- --]]
- -- Position of your modem
- modem_position = "left"
- -- Position of your monitor
- monitor_position = "top"
- -- The text scale of your monitor (between 0.5 and 5)
- monitor_scale = 1
- -- The modem channel to receive sensor data
- -- This has to be the same channel you set in send_tank_info program
- modem_channel = 3
- -- Whether to show or not the percentage in numberic form above the graphs
- -- Note: This will only work if there is enough horizontal space to write the numbers
- -- So if the graphs width is less than 4 characters you won't see the numbers
- -- even if you set this to true
- show_percentage = true
- -- The colors you want to see your liquids in
- -- You can add more liquids, or change the colors here
- tank_colors={
- {name="Lava", color=colors.red},
- {name="Water", color=colors.blue},
- {name="Biofuel", color=colors.orange},
- {name="item.mobessenceitem", color=colors.green},
- {name="Creosote Oil", color=colors.brown},
- {name="Fuel", color=colors.yellow},
- {name="Oil", color=colors.gray},
- {name="Biomass", color=colors.green},
- {name="Sludge", color=colors.blue},
- {name="Sewage", color=colors.brown},
- {name="Liquid DNA", color=colors.pink},
- {name="Honey", color=colors.yellow},
- {name="Fruit Juice", color=colors.lime},
- {name="Milk", color=colors.white},
- {name="Seed Oil", color=colors.yellow},
- {name="Ethanol", color=colors.orange},
- {name="Pink Slime", color=colors.pink},
- {name="Molten Iron", color=colors.red},
- {name="Molten Gold", color=colors.yellow},
- {name="Molten Ardite", color=colors.brown},
- {name="Molten Copper", color=colors.orange},
- {name="Molten Bronze", color=colors.brown},
- {name="Molten Aluminum Brass", color=colors.yellow},
- {name="Molten Manyullyn", color=colors.purple},
- {name="Molten Alumite", color=colors.pink},
- {name="Molten Obsidian", color=colors.purple},
- }
- -----------------------------------------
- -- DO NOT MODIFY ANYTHING FROM HERE ON --
- -----------------------------------------
- modem = peripheral.wrap(modem_position)
- monitor = peripheral.wrap(monitor_position)
- monitor.setTextScale(monitor_scale)
- modem.open(modem_channel)
- unique_tank_hash = {}
- function tableLength(T)
- local count = 0
- for _ in pairs(T) do
- count = count + 1
- end
- return count
- end
- function getColor(data)
- for _, v in pairs(tank_colors) do
- if (v.name == data.Name) or
- (v.name == data.RawName) then
- return v.color
- end
- end
- return colors.white
- end
- function drawGraph()
- local graph_width=0
- local tanks_num = tableLength(unique_tank_hash)
- local percentage_line = 0
- if tanks_num == 0 then
- return
- end
- while graph_width <= 0 do
- graph_width = math.floor((width-tanks_num+1) / tanks_num)
- tanks_num = tanks_num - 1
- end
- tanks_num = tanks_num + 1
- if (show_percentage) and
- (graph_width > 4) then
- percentage_line = 1
- end
- local count=0
- for k, v in pairs(unique_tank_hash) do
- local x = (count * graph_width) + 1 + count
- local x_end = x + graph_width -1
- percent = v.Amount / v.Capacity
- percentage = (height-percentage_line) * percent
- y = (height-percentage_line) - math.floor(percentage + 0.5) + percentage_line
- if percentage_line == 1 then
- local round_percent = math.floor((percent*100) + 0.5)
- local text = round_percent.."%"
- local x_pos = x + math.floor((graph_width-string.len(text))/2)
- monitor.setCursorPos(x_pos,1)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- monitor.write(text)
- end
- for i=1+percentage_line,height do
- for j=x,x_end do
- local c = colors.black
- if i > y then
- c = getColor(v)
- end
- monitor.setBackgroundColor(c)
- monitor.setCursorPos(j, i)
- monitor.write(" ")
- end
- end
- count = count + 1
- if count >= tanks_num then
- return
- end
- end
- end
- function removeTanks(temp_tank_hash, sensor_id)
- local deletables = {}
- local temp_tank_hash_empty = false
- if not next(temp_tank_hash) then
- temp_tank_hash_empty = true
- end
- for k, v in pairs(unique_tank_hash) do
- local other_hash = textutils.unserialize(k)
- if other_hash[1] == sensor_id then
- if temp_tank_hash_empty then
- deletables[k] = k
- else
- local found = false
- for i, j in pairs(temp_tank_hash) do
- if k == i then
- found = true
- break
- end
- end
- if not found then
- deletables[k] = k
- end
- end
- end
- end
- for k, v in pairs(deletables) do
- unique_tank_hash[k] = nil
- end
- end
- while true do
- local event, modemSide, senderChannel,
- replyChannel, message, senderDistance = os.pullEvent("modem_message")
- unser_message = textutils.unserialize(message)
- sensor_id = unser_message.id
- sensor_data = unser_message.data
- local temp_tank_hash = {}
- for i, v in ipairs(sensor_data) do
- tanks=v.Tanks
- position=v.Position
- for j, value in ipairs(tanks) do
- tank = value
- local hash={sensor_id, position, j}
- unique_tank_hash[textutils.serialize(hash)] = tank
- temp_tank_hash[textutils.serialize(hash)] = tank
- end
- end
- removeTanks(temp_tank_hash, sensor_id)
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- width, height = monitor.getSize()
- drawGraph()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement