Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- display_tanks_v2 v2.0 for OpenPeripheral 0.4.1
- Copyright (C) 2014 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/>.
- --]]
- -- 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 Aluminum", color=colors.pink},
- {name="Molten Obsidian", color=colors.purple},
- {name="Molten Cobalt", color=colors.blue},
- {name="Molten Steel", color=colors.lightGray},
- {name="Molten Glass", color=colors.lightBlue},
- {name="Seared Stone", color=colors.gray},
- {name="Liquified Emerald", color=colors.lime},
- {name="Blood", color=colors.red},
- {name="Molten Nickel", color=colors.yellow},
- {name="Molten Lead", color=colors.purple},
- {name="Molten Silver", color=colors.cyan},
- {name="fluid.platinum.molten", color=colors.cyan},
- {name="Molten Invar", color=colors.lightGray},
- {name="Molten Electrum", color=colors.yellow},
- {name="Resonant Ender", color=colors.green},
- {name="Liquid Blueslime", color=colors.lightBlue},
- {name="Pig Iron", color=colors.pink},
- {name="Liquid XP", color=colors.lime},
- {name="Destabilized Redstone", color=colors.red},
- {name="Energized Glowstone", color=colors.yellow},
- {name="Blazing Pyrotheum", color=colors.orange},
- {name="Gelid Cryotheum", color=colors.cyan},
- {name="coal", color=colors.gray},
- }
- -----------------------------------------
- -- DO NOT MODIFY ANYTHING FROM HERE ON --
- -----------------------------------------
- modem = peripheral.find("modem")
- if not modem then
- error("Please attach one wireless modem")
- end
- 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
- if v.amount == nil then
- v.amount = 0
- end
- if v.capacity == nil then
- v.capacity = 1
- end
- 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)
- tank_id = unser_message.id
- tank_data = unser_message.tanks
- local temp_tank_hash = {}
- for i, v in ipairs(tank_data) do
- tank=v.data
- tank_position=v.position
- local hash={tank_id, tank_position, i}
- unique_tank_hash[textutils.serialize(hash)] = tank
- temp_tank_hash[textutils.serialize(hash)] = tank
- end
- removeTanks(temp_tank_hash, tank_id)
- monitor = peripheral.find("monitor")
- if monitor then
- monitor.setTextScale(monitor_scale)
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- width, height = monitor.getSize()
- drawGraph()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement