TheGameBoy_95

Untitled

May 21st, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. modem_position = "back"
  2. monitor_position = "right"
  3. monitor_scale = 1
  4. modem_channel = 4
  5. show_percentage = true
  6. sleep_timeout = 0.5
  7.  
  8. tanks={
  9. "1,0,0",
  10. "0,0,-1",
  11. "0,-1,0"
  12. }
  13.  
  14. os.loadAPI("ocs/apis/sensor")
  15.  
  16. sens = sensor.wrap(sensor_position)
  17. modem = peripheral.wrap(modem_position)
  18.  
  19. while true do
  20. local data_table={}
  21. for i, v in ipairs(tanks) do
  22. target_details = sens.getTargetDetails(v)
  23. table.insert(data_table, target_details)
  24. print("a")
  25. end
  26. local send_data={id=os.getComputerID(), data=data_table}
  27.  
  28. serialized_data = textutils.serialize(send_data)
  29.  
  30. modem.transmit(modem_channel,modem_channel,serialized_data)
  31.  
  32. sleep(sleep_timeout)
  33. end
  34.  
  35. tank_colors={
  36. {name="lava", color=colors.orange},
  37. {name="water", color=colors.blue},
  38. {name="biofuel", color=colors.orange},
  39. {name="mobessence", color=colors.green},
  40. {name="creosote", color=colors.brown},
  41. {name="fuel", color=colors.yellow},
  42. {name="oil", color=colors.gray},
  43. {name="biomass", color=colors.green},
  44. {name="sludge", color=colors.blue},
  45. {name="sewage", color=colors.brown},
  46. {name="liquiddna", color=colors.pink},
  47. {name="honey", color=colors.yellow},
  48. {name="milk", color=colors.white},
  49. {name="ethanol", color=colors.orange},
  50. {name="pinkslime", color=colors.pink},
  51. {name="iron.molten", color=colors.red},
  52. {name="gold.molten", color=colors.yellow},
  53. {name="ardite.molten", color=colors.brown},
  54. {name="copper.molten", color=colors.orange},
  55. {name="bronze.molten", color=colors.brown},
  56. {name="aluminumbrass.molten", color=colors.yellow},
  57. {name="manyullyn.molten", color=colors.purple},
  58. {name="alumite.molten", color=colors.pink},
  59. {name="aluminum.molten", color=colors.pink},
  60. {name="obsidian.molten", color=colors.purple},
  61. {name="cobalt.molten", color=colors.blue},
  62. {name="steel.molten", color=colors.lightGray},
  63. {name="glass.molten", color=colors.lightBlue},
  64. {name="stone.seared", color=colors.gray},
  65. {name="emerald.liquid", color=colors.lime},
  66. {name="blood", color=colors.red},
  67. {name="nickel.molten", color=colors.yellow},
  68. {name="lead.molten", color=colors.purple},
  69. {name="silver.molten", color=colors.cyan},
  70. {name="fluid.platinum.molten", color=colors.cyan},
  71. {name="invar.molten", color=colors.lightGray},
  72. {name="electrum.molten", color=colors.yellow},
  73. {name="ender", color=colors.green},
  74. {name="slime.blue", color=colors.lightBlue},
  75. {name="pigiron.molten", color=colors.pink},
  76. {name="immibis.liquidxp", color=colors.lime},
  77. {name="redstone", color=colors.red},
  78. {name="glowstone", color=colors.yellow},
  79. {name="pyrotheum", color=colors.orange},
  80. {name="cryotheum", color=colors.cyan},
  81. {name="coal", color=colors.gray},
  82. {name="shadow.iron.molten", color=colors.lightGray},
  83. }
  84.  
  85. modem = peripheral.wrap("left")
  86. monitor = peripheral.wrap("top")
  87.  
  88. monitor.setTextScale(1)
  89. modem.open(modem_channel)
  90.  
  91. unique_tank_hash = {}
  92. function tableLength(T)
  93. local count = 0
  94. for _ in pairs(T) do
  95. count = count + 1
  96. print("a")
  97. end
  98. return count
  99. end
  100.  
  101. function getColor(data)
  102. for _, v in pairs(tank_colors) do
  103. if (v.name == data.Name) or
  104. (v.name == data.RawName) then
  105. print("b")
  106. return v.color
  107. end
  108. end
  109. return colors.white
  110. end
  111.  
  112. function drawGraph()
  113. local graph_width=0
  114. local tanks_num = table.getn(unique_tank_hash)
  115. local percentage_line = 0
  116.  
  117. if tanks_num == 0 then
  118. print("c_if")
  119. return
  120. end
  121.  
  122. while graph_width <= 0 do
  123. graph_width = math.floor((width-tanks_num+1) / tanks_num)
  124. tanks_num = tanks_num - 1
  125. print("d")
  126. end
  127. tanks_num = tanks_num + 1
  128.  
  129. if (show_percentage) and
  130. (graph_width > 4) then
  131. percentage_line = 1
  132. print("e_if")
  133. end
  134.  
  135. local count=0
  136. for k, v in pairs(unique_tank_hash) do
  137. local x = (count * graph_width) + 1 + count
  138. local x_end = x + graph_width -1
  139. percent = v.Amount / v.Capacity
  140. percentage = (height-percentage_line) * percent
  141.  
  142. y = (height-percentage_line) - math.floor(percentage + 0.5) + percentage_line
  143. print("f")
  144. if percentage_line == 1 then
  145. local round_percent = math.floor((percent*100) + 0.5)
  146. local text = round_percent.."%"
  147. local x_pos = x + math.floor((graph_width-string.len(text))/2)
  148. monitor.setCursorPos(x_pos,1)
  149. monitor.setBackgroundColor(colors.black)
  150. monitor.setTextColor(colors.white)
  151.  
  152. monitor.write(text)
  153. monitor.write("g")
  154. end
  155.  
  156. for i=1+percentage_line,height do
  157. for j=x,x_end do
  158. local c = colors.black
  159. if i > y then
  160. c = getColor(v)
  161. end
  162. monitor.setBackgroundColor(c)
  163. monitor.setCursorPos(j, i)
  164. monitor.write(" ")
  165. end
  166. end
  167.  
  168. count = count + 1
  169. if count >= tanks_num then
  170. return
  171. end
  172. end
  173. end
  174.  
  175. function removeTanks(temp_tank_hash, sensor_id)
  176. local deletables = {}
  177. local temp_tank_hash_empty = false
  178. if not next(temp_tank_hash) then
  179. temp_tank_hash_empty = true
  180. end
  181.  
  182. for k, v in pairs(unique_tank_hash) do
  183. local other_hash = textutils.unserialize(k)
  184. if other_hash[1] == sensor_id then
  185. if temp_tank_hash_empty then
  186. deletables[k] = k
  187. else
  188. local found = false
  189. for i, j in pairs(temp_tank_hash) do
  190. if k == i then
  191. found = true
  192. break
  193. end
  194. end
  195. if not found then
  196. deletables[k] = k
  197. end
  198. end
  199. end
  200. end
  201. for k, v in pairs(deletables) do
  202. unique_tank_hash[k] = nil
  203. end
  204. end
  205.  
  206. while true do
  207. local event, modemSide, senderChannel,
  208. replyChannel, message, senderDistance = os.pullEvent("modem_message")
  209. unser_message = textutils.unserialize(message)
  210.  
  211. sensor_id = unser_message.id
  212. sensor_data = unser_message.data
  213.  
  214. local temp_tank_hash = {}
  215.  
  216. for i, v in ipairs(sensor_data) do
  217. tanks=v.Tanks
  218. position=v.Position
  219.  
  220. for j, value in ipairs(tanks) do
  221. tank = value
  222. local hash={sensor_id, position, j}
  223. unique_tank_hash[textutils.serialize(hash)] = tank
  224. temp_tank_hash[textutils.serialize(hash)] = tank
  225. end
  226. end
  227.  
  228. removeTanks(temp_tank_hash, sensor_id)
  229.  
  230. monitor.setBackgroundColor(colors.black)
  231. monitor.clear()
  232. width, height = monitor.getSize()
  233. drawGraph()
  234. end
Advertisement
Add Comment
Please, Sign In to add comment