View difference between Paste ID: jUgXYP6f and CmasA2k4
SHOW: | | - or go back to the newest paste.
1
--[[
2-
    display_tanks_v2 v2.0 for OpenPeripheral 0.4.1
2+
    display_tanks_wired_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
22+
23-
-- This has to be the same channel you set in send_tank_info program
23+
24-
modem_channel = 3
24+
25
--       even if you set this to true
26
show_percentage = true
27
28
-- Monitor refresh frequency (in seconds)
29
sleep_timeout = 0.5
30
31
-- The colors you want to see your liquids in
32
-- You can add more liquids, or change the colors here
33
tank_colors={
34
{name="Lava", color=colors.red},
35
{name="Water", color=colors.blue},
36
{name="Biofuel", color=colors.orange},
37
{name="item.mobessenceitem", color=colors.green},
38
{name="Creosote Oil", color=colors.brown},
39
{name="Fuel", color=colors.yellow},
40
{name="Oil", color=colors.gray},
41
{name="Biomass", color=colors.green},
42
{name="Sludge", color=colors.blue},
43
{name="Sewage", color=colors.brown},
44
{name="Liquid DNA", color=colors.pink},
45
{name="Honey", color=colors.yellow},
46
{name="Fruit Juice", color=colors.lime},
47
{name="Milk", color=colors.white},
48
{name="Seed Oil", color=colors.yellow},
49
{name="Ethanol", color=colors.orange},
50
{name="Pink Slime", color=colors.pink},
51
{name="Molten Iron", color=colors.red},
52
{name="Molten Gold", color=colors.yellow},
53
{name="Molten Ardite", color=colors.brown},
54
{name="Molten Copper", color=colors.orange},
55
{name="Molten Bronze", color=colors.brown},
56
{name="Molten Aluminum Brass", color=colors.yellow},
57
{name="Molten Manyullyn", color=colors.purple},
58
{name="Molten Alumite", color=colors.pink},
59
{name="Molten Aluminum", color=colors.pink},
60
{name="Molten Obsidian", color=colors.purple},
61
{name="Molten Cobalt", color=colors.blue},
62
{name="Molten Steel", color=colors.lightGray},
63
{name="Molten Glass", color=colors.lightBlue},
64
{name="Seared Stone", color=colors.gray},
65
{name="Liquified Emerald", color=colors.lime},
66
{name="Blood", color=colors.red},
67
{name="Molten Nickel", color=colors.yellow},
68
{name="Molten Lead", color=colors.purple},
69
{name="Molten Silver", color=colors.cyan},
70
{name="fluid.platinum.molten", color=colors.cyan},
71
{name="Molten Invar", color=colors.lightGray},
72
{name="Molten Electrum", color=colors.yellow},
73
{name="Resonant Ender", color=colors.green},
74
{name="Liquid Blueslime", color=colors.lightBlue},
75
{name="Pig Iron", color=colors.pink},
76
{name="Liquid XP", color=colors.lime},
77
{name="Destabilized Redstone", color=colors.red},
78
{name="Energized Glowstone", color=colors.yellow},
79
{name="Blazing Pyrotheum", color=colors.orange},
80
{name="Gelid Cryotheum", color=colors.cyan},
81
{name="coal", color=colors.gray},
82
}
83
84
85
-----------------------------------------
86
-- DO NOT MODIFY ANYTHING FROM HERE ON --
87
-----------------------------------------
88
89
function tableLength(T)
90-
modem = peripheral.find("modem")
90+
91-
if not modem then
91+
92-
  error("Please attach one wireless modem")
92+
93
  end
94
  return count
95-
modem.open(modem_channel)
95+
96
97-
unique_tank_hash = {}
97+
98
  for _, v in pairs(tank_colors) do
99
    if (v.name == data.name) or
100
       (v.name == data.rawName) then
101
      return v.color
102
    end
103
  end
104
  return colors.white
105
end
106
107
function drawGraph()
108
  local graph_width=0
109
  local tanks_num = tableLength(unique_tank_hash)
110
  local percentage_line = 0
111
112
  if tanks_num == 0 then
113
    return
114
  end
115
116
  while graph_width <= 0 do
117
    graph_width = math.floor((width-tanks_num+1) / tanks_num)
118
    tanks_num = tanks_num - 1
119
  end
120
  tanks_num = tanks_num + 1
121
122
  if (show_percentage) and
123
     (graph_width > 4) then
124
    percentage_line = 1
125
  end
126
127
  local count=0
128
  for k, v in pairs(unique_tank_hash) do
129
    local x = (count * graph_width) + 1 + count
130
    local x_end = x + graph_width -1
131
    if v.amount == nil then
132
      v.amount = 0
133
    end
134
    if v.capacity == nil then
135
      v.capacity = 1
136
    end
137
    percent = v.amount / v.capacity
138
    percentage = (height-percentage_line) * percent
139
    
140
    y = (height-percentage_line) - math.floor(percentage + 0.5) + percentage_line
141
142
    if percentage_line == 1 then
143
      local round_percent = math.floor((percent*100) + 0.5)
144
      local text = round_percent.."%"
145
      local x_pos = x + math.floor((graph_width-string.len(text))/2)
146
      monitor.setCursorPos(x_pos,1)
147
      monitor.setBackgroundColor(colors.black)
148
      monitor.setTextColor(colors.white)
149
150
      monitor.write(text)
151
    end
152
153
    for i=1+percentage_line,height do
154
      for j=x,x_end do
155
        local c = colors.black
156
        if i > y then
157
          c = getColor(v)
158
        end
159
        monitor.setBackgroundColor(c)
160
        monitor.setCursorPos(j, i)
161
        monitor.write(" ")
162
      end
163
    end
164
165
    count = count + 1
166
    if count >= tanks_num then
167
      return
168
    end
169
  end
170
end
171
172
function getTanks()
173
  tanks={}
174
  for i, name in ipairs(peripheral.getNames()) do
175
    local methods = peripheral.getMethods(name)
176
    if methods then
177
      for k, v in pairs(methods) do
178
        if v == "getTankInfo" then
179
          local p = peripheral.wrap(name)
180
          local info = p.getTankInfo("unknown")
181
          for m,n in ipairs(info) do
182-
function removeTanks(temp_tank_hash, sensor_id)
182+
            local data={position=name, data=n}
183-
  local deletables = {}
183+
            table.insert(tanks, data)
184-
  local temp_tank_hash_empty = false
184+
185-
  if not next(temp_tank_hash) then
185+
186-
    temp_tank_hash_empty = true
186+
187
    end
188
  end
189
190-
    local other_hash = textutils.unserialize(k)
190+
  return tanks
191-
    if other_hash[1] == sensor_id then
191+
192-
      if temp_tank_hash_empty then
192+
193-
        deletables[k] = k
193+
194-
      else
194+
  tank_data = getTanks()
195-
        local found = false
195+
  unique_tank_hash = {}
196-
        for i, j in pairs(temp_tank_hash) do
196+
197-
          if k == i then
197+
198-
            found = true
198+
199-
            break
199+
200
    local hash={tank_position, i}
201
    unique_tank_hash[textutils.serialize(hash)] = tank
202-
        if not found then
202+
203-
          deletables[k] = k
203+
204
  monitor = peripheral.find("monitor")
205
  if monitor then
206
    monitor.setTextScale(monitor_scale)
207
    monitor.setBackgroundColor(colors.black)
208-
  for k, v in pairs(deletables) do
208+
209-
    unique_tank_hash[k] = nil
209+
210
    drawGraph()
211
  end
212
213
  sleep(sleep_timeout)
214-
  local event, modemSide, senderChannel,
214+