Advertisement
User9684

Untitled

Nov 15th, 2022 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. rednet.open('right')
  2.  
  3. --[[
  4. Wolfe's Mekanism Induction Matrix Monitor
  5. Usage: Put computer with code near an Induction Port and a monitor (2x3 array should work fine) and run.
  6. Configuration: You can add a file called "config" with the options below, or append them to the command when running it via terminal:
  7. energy_type = 'FE' -- Energy type you want to use
  8. update_frequency = 1 -- Update frequency, in seconds
  9. text_scale = 1 -- The text scale on the monitor, use 0.5 if you want to run on less displays
  10. side_monitor = 'right' -- Hardcodes which side the monitor should be, defaults to auto-detection
  11. side_inductor = 'back' -- Hardcodes which side the Induction Port should be, defaults to auto-detection
  12. ]]
  13.  
  14. -- Slight mods done :3
  15.  
  16. --
  17. -- Usage: Put computer near an Induction Port and a monitor , set the sides below and run.
  18.  
  19. -- Default settings
  20. options = {
  21. energy_type = 'FE',
  22. update_frequency = 1,
  23. text_scale = 1,
  24. }
  25.  
  26. local proto = "thingforcoded"
  27.  
  28. -- Loads custom options from file (if available)
  29. if fs.exists('config') then
  30. -- Opens the file for reading
  31. local handle = fs.open('config')
  32.  
  33. -- Reads configs
  34. raw_options = {}
  35. local line = handle.readLine()
  36. while line do
  37. table.insert(raw_options, line)
  38. line = handle.readLine()
  39. end
  40.  
  41. -- Sets custom options
  42. custom_options = string.format('{%s}', table.concat(raw_options, '\n,'))
  43.  
  44. -- Closes the handle properly
  45. handle.close()
  46. end
  47.  
  48. -- Gets custom settings via arguments
  49. args = {...}
  50. if args and #args > 0 then
  51. -- Parses custom settings from args
  52. custom_options = string.format('{%s}', table.concat(args, '\n,'))
  53. end
  54.  
  55. -- Detects custom options
  56. if custom_options then
  57. -- Debug only
  58. print('Running with custom options:')
  59.  
  60. -- Makes sure we're dealing with a table to prevent code injection
  61. if '{' == custom_options:sub(1, 1) then
  62. -- Parses the object
  63. custom_options, err = loadstring(string.format('return %s', custom_options))
  64.  
  65. -- Handles invalid object
  66. if not custom_options then
  67. print('Invalid options:')
  68. print(err)
  69. else
  70. -- Replaces settings
  71. for k, v in pairs(custom_options()) do
  72. print(string.format('%s = %s', k, v))
  73. options[k] = v
  74. end
  75. end
  76. end
  77. end
  78.  
  79. -- Auto-detects sides
  80. for _, side in pairs(peripheral.getNames()) do
  81. -- Auto-detects monitor
  82. if 'monitor' == peripheral.getType(side) and (not options.side_monitor) then
  83. options.side_monitor = side
  84. end
  85.  
  86. -- Auto-detects Induction Port
  87. if 'inductionPort' == peripheral.getType(side) and (not options.side_inductor) then
  88. options.side_inductor = side
  89. end
  90. end
  91.  
  92. -- Queues a new print command to be sent
  93. buffer = {}
  94. function queue (text)
  95. table.insert(buffer, text)
  96. end
  97.  
  98. -- Queues a new print command with string.format
  99. function queuef (fmt, ...)
  100. queue(string.format(fmt, ...))
  101. end
  102.  
  103. -- Flushes (prints) buffer content
  104. function queue_flush ()
  105. -- Clears terminal
  106. term.clear()
  107. term.setCursorPos(1, 1)
  108.  
  109. -- Writes new data
  110. print(table.concat(buffer, '\n'))
  111. buffer = {}
  112. end
  113.  
  114. -- Formats time
  115. function time (secs)
  116. -- Prepare value
  117. secs = math.floor(secs)
  118.  
  119. -- Days
  120. local weeks = math.floor(secs / 604800)
  121. secs = secs - (604800 * weeks)
  122.  
  123. -- Days
  124. local days = math.floor(secs / 86400)
  125. secs = secs - (86400 * days)
  126.  
  127. -- Hours
  128. local hours = math.floor(secs / 3600)
  129. secs = secs - (3600 * hours)
  130.  
  131. -- Minutes
  132. local mins = math.floor(secs / 60)
  133. secs = secs - (60 * mins)
  134.  
  135. -- If we have more than 72h worth of storage, switch to week, day, hour format
  136. if weeks > 0 then
  137. return string.format('%dwk %dd %dh', weeks, days, hours)
  138. elseif days >= 3 then
  139. return string.format('%dd %dh', days, hours)
  140. end
  141.  
  142. -- Formatting to have trailing zeros on H:MM:SS
  143. return string.format('%d:%02d:%02d', hours, mins, secs)
  144. end
  145.  
  146. -- Rounds number
  147. function rnd (val, dec)
  148. local X = math.pow(10, dec)
  149. return math.floor(val * X) / X
  150. end
  151.  
  152. -- Converts to percentage
  153. function pct (val, dec)
  154. return rnd(100 * val, dec or 1) .. '%'
  155. end
  156.  
  157. -- Converts to readable power
  158. function pwr (val, dec)
  159. local pre = ''
  160. local suf = ''
  161.  
  162. local is_neg = false
  163. if val < 0 then
  164. pre = '-'
  165. is_neg = true
  166. val = -val
  167. end
  168.  
  169. val = energy_function(val)
  170.  
  171. if val > 1000 then
  172. suf = 'k'
  173. val = val / 1000
  174. end
  175.  
  176. if val > 1000 then
  177. suf = 'M'
  178. val = val / 1000
  179. end
  180.  
  181. if val > 1000 then
  182. suf = 'G'
  183. val = val / 1000
  184. end
  185.  
  186. if val > 1000 then
  187. suf = 'T'
  188. val = val / 1000
  189. end
  190.  
  191. return string.format('%s%s%s%s', pre, rnd(val, dec or 1), suf, energy_type)
  192. end
  193.  
  194. -- Checks induction port
  195. function check_connection ()
  196. return inductor and inductor.getEnergy and inductor.getLastInput
  197. end
  198.  
  199. -- Detects energy type, sets energy function
  200. energy_type = options.energy_type
  201. energy_function = mekanismEnergyHelper[string.format('joulesTo%s', energy_type)]
  202.  
  203. -- Function not found, use default Joules and a stub
  204. if not energy_function then
  205. energy_type = 'J'
  206. energy_function = function (val) return val end
  207. end
  208.  
  209. -- Checks if Inductor Port is missing or multiblock not ready
  210. inductor = peripheral.wrap(options.side_inductor)
  211. while not check_connection() do
  212. -- Wait for next update
  213. os.sleep(options.update_frequency)
  214.  
  215. -- Tries to detect port
  216. if not options.side_inductor then
  217. for _, side in pairs(peripheral.getNames()) do
  218. -- Tries to find an induction port
  219. if 'inductionPort' == peripheral.getType(side) then
  220. options.side_inductor = side
  221. inductor = peripheral.wrap(options.side_inductor)
  222. end
  223. end
  224. else
  225. -- Try again on pre-set port
  226. inductor = peripheral.wrap(options.side_inductor)
  227. end
  228. end
  229.  
  230. -- Initializes balance
  231. balance = inductor.getEnergy()
  232. while true do
  233. local strings = {}
  234. local status, err = pcall(function ()
  235. -- Main script
  236. table.insert(strings, 'Ind.Matrix Monitor')
  237. table.insert(strings, '------------------')
  238. table.insert(strings, '')
  239. table.insert(strings, string.format('Power : %s', pwr(inductor.getEnergy())))
  240. table.insert(strings, string.format('Limit : %s', pwr(inductor.getMaxEnergy())))
  241. table.insert(strings, string.format('Charge: %s', pct(inductor.getEnergyFilledPercentage())))
  242. table.insert(strings, '')
  243. string.format('Input : %s', pwr(inductor.getLastInput()))
  244. string.format('Output: %s', pwr(inductor.getLastOutput()))
  245. string.format('Max IO: %s/t', pwr(inductor.getTransferCap()))
  246. table.insert(strings, '')
  247.  
  248. -- Power balance per second
  249. local balance_last = balance
  250. balance = inductor.getEnergy()
  251. local balance_change = (balance - balance_last) / options.update_frequency
  252.  
  253. -- If we have negative value here, we'll save a character by removing the space so it fits same line
  254. if balance_change < 0 then
  255. string.format('Change:%s/s', pwr(balance_change))
  256. else
  257. string.format('Change: %s/s', pwr(balance_change))
  258. end
  259.  
  260. -- Status (charged/depleted in)
  261. table.insert(strings, 'Status:')
  262. if balance_change > 0 then
  263. -- Charging
  264. local remaining_charge = inductor.getMaxEnergy() - inductor.getEnergy()
  265. local seconds_remaining = remaining_charge / balance_change
  266. table.insert(strings, string.format('Charg. %s', time(seconds_remaining)))
  267. elseif balance_change < 0 then
  268. -- Discharging
  269. local remaining_charge = inductor.getEnergy()
  270. local seconds_remaining = remaining_charge / -balance_change
  271. table.insert(strings, string.format('Disch. %s', time(seconds_remaining)))
  272. else
  273. -- No changes, so we won't be charged or depleted, rare.
  274. table.insert(strings, 'Idle')
  275. end
  276. end)
  277.  
  278. -- Checks for errors (might be disconnected)
  279. if not status then
  280. -- Clears buffer first
  281. buffer = {}
  282.  
  283. -- Shows error message
  284. table.insert(strings, 'Error reading data')
  285. table.insert(strings, 'Check connections.')
  286. table.insert(strings, '------------------')
  287. table.insert(strings, err)
  288. end
  289.  
  290. -- Prints
  291.  
  292. rednet.broadcast(table.concat(strings, '&'), proto)
  293.  
  294. -- Wait for next update
  295. os.sleep(options.update_frequency)
  296. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement