Advertisement
User9684

Untitled

Nov 15th, 2022 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. local proto = "thingforcoded"
  2. rednet.open("left")
  3.  
  4. --[[
  5. Wolfe's Mekanism Induction Matrix Monitor
  6. Usage: Put computer with code near an Induction Port and a monitor (2x3 array should work fine) and run.
  7. Configuration: You can add a file called "config" with the options below, or append them to the command when running it via terminal:
  8. energy_type = 'FE' -- Energy type you want to use
  9. update_frequency = 1 -- Update frequency, in seconds
  10. text_scale = 1 -- The text scale on the monitor, use 0.5 if you want to run on less displays
  11. side_monitor = 'right' -- Hardcodes which side the monitor should be, defaults to auto-detection
  12. side_inductor = 'back' -- Hardcodes which side the Induction Port should be, defaults to auto-detection
  13. ]]
  14.  
  15. --
  16. -- Usage: Put computer near an Induction Port and a monitor , set the sides below and run.
  17.  
  18. -- Default settings
  19. options = {
  20. energy_type = 'FE',
  21. update_frequency = 1,
  22. text_scale = 1,
  23. }
  24.  
  25. -- Loads custom options from file (if available)
  26. if fs.exists('config') then
  27. -- Opens the file for reading
  28. local handle = fs.open('config')
  29.  
  30. -- Reads configs
  31. raw_options = {}
  32. local line = handle.readLine()
  33. while line do
  34. table.insert(raw_options, line)
  35. line = handle.readLine()
  36. end
  37.  
  38. -- Sets custom options
  39. custom_options = string.format('{%s}', table.concat(raw_options, '\n,'))
  40.  
  41. -- Closes the handle properly
  42. handle.close()
  43. end
  44.  
  45. -- Gets custom settings via arguments
  46. args = {...}
  47. if args and #args > 0 then
  48. -- Parses custom settings from args
  49. custom_options = string.format('{%s}', table.concat(args, '\n,'))
  50. end
  51.  
  52. -- Detects custom options
  53. if custom_options then
  54. -- Debug only
  55. print('Running with custom options:')
  56.  
  57. -- Makes sure we're dealing with a table to prevent code injection
  58. if '{' == custom_options:sub(1, 1) then
  59. -- Parses the object
  60. custom_options, err = loadstring(string.format('return %s', custom_options))
  61.  
  62. -- Handles invalid object
  63. if not custom_options then
  64. print('Invalid options:')
  65. print(err)
  66. else
  67. -- Replaces settings
  68. for k, v in pairs(custom_options()) do
  69. print(string.format('%s = %s', k, v))
  70. options[k] = v
  71. end
  72. end
  73. end
  74. end
  75.  
  76. -- Auto-detects sides
  77. for _, side in pairs(peripheral.getNames()) do
  78. -- Auto-detects monitor
  79. if 'monitor' == peripheral.getType(side) and (not options.side_monitor) then
  80. options.side_monitor = side
  81. end
  82.  
  83. -- Auto-detects Induction Port
  84. if 'inductionPort' == peripheral.getType(side) and (not options.side_inductor) then
  85. options.side_inductor = side
  86. end
  87. end
  88.  
  89. -- Connects to Peripherals
  90. monitor = peripheral.wrap(options.side_monitor)
  91. monitor2 = peripheral.wrap('back')
  92. local function pushToAR(str)
  93. monitor2.clear()
  94. local i = 0
  95. for v in string.gmatch(str, "([^\n]+)") do
  96. coroutine.wrap(function()
  97. monitor2.drawString(v, 0, i, 0xFFFFFF)
  98. end)()
  99. i=i+10
  100. end
  101. end
  102.  
  103. -- Queues a new print command to be sent
  104. buffer = {}
  105. function queue (text)
  106. table.insert(buffer, text)
  107. end
  108.  
  109. -- Queues a new print command with string.format
  110. function queuef (fmt, ...)
  111. queue(string.format(fmt, ...))
  112. end
  113.  
  114. -- Flushes (prints) buffer content
  115. function queue_flush ()
  116. -- Clears terminal
  117. term.clear()
  118. term.setCursorPos(1, 1)
  119.  
  120. -- Writes new data
  121. print(table.concat(buffer, '\n'))
  122. pushToAR(table.concat(buffer, '\n'))
  123. buffer = {}
  124. end
  125.  
  126. -- Formats time
  127. function time (secs)
  128. -- Prepare value
  129. secs = math.floor(secs)
  130.  
  131. -- Days
  132. local weeks = math.floor(secs / 604800)
  133. secs = secs - (604800 * weeks)
  134.  
  135. -- Days
  136. local days = math.floor(secs / 86400)
  137. secs = secs - (86400 * days)
  138.  
  139. -- Hours
  140. local hours = math.floor(secs / 3600)
  141. secs = secs - (3600 * hours)
  142.  
  143. -- Minutes
  144. local mins = math.floor(secs / 60)
  145. secs = secs - (60 * mins)
  146.  
  147. -- If we have more than 72h worth of storage, switch to week, day, hour format
  148. if weeks > 0 then
  149. return string.format('%dwk %dd %dh', weeks, days, hours)
  150. elseif days >= 3 then
  151. return string.format('%dd %dh', days, hours)
  152. end
  153.  
  154. -- Formatting to have trailing zeros on H:MM:SS
  155. return string.format('%d:%02d:%02d', hours, mins, secs)
  156. end
  157.  
  158. -- Rounds number
  159. function rnd (val, dec)
  160. local X = math.pow(10, dec)
  161. return math.floor(val * X) / X
  162. end
  163.  
  164. -- Converts to percentage
  165. function pct (val, dec)
  166. return rnd(100 * val, dec or 1) .. '%'
  167. end
  168.  
  169. -- Converts to readable power
  170. function pwr (val, dec)
  171. local pre = ''
  172. local suf = ''
  173.  
  174. local is_neg = false
  175. if val < 0 then
  176. pre = '-'
  177. is_neg = true
  178. val = -val
  179. end
  180.  
  181. val = energy_function(val)
  182.  
  183. if val > 1000 then
  184. suf = 'k'
  185. val = val / 1000
  186. end
  187.  
  188. if val > 1000 then
  189. suf = 'M'
  190. val = val / 1000
  191. end
  192.  
  193. if val > 1000 then
  194. suf = 'G'
  195. val = val / 1000
  196. end
  197.  
  198. if val > 1000 then
  199. suf = 'T'
  200. val = val / 1000
  201. end
  202.  
  203. return string.format('%s%s%s%s', pre, rnd(val, dec or 1), suf, energy_type)
  204. end
  205.  
  206. -- Checks induction port
  207. function check_connection ()
  208. return inductor and inductor.getEnergy and inductor.getLastInput
  209. end
  210.  
  211. -- Detects energy type, sets energy function
  212. energy_type = options.energy_type
  213. energy_function = mekanismEnergyHelper[string.format('joulesTo%s', energy_type)]
  214.  
  215. -- Function not found, use default Joules and a stub
  216. if not energy_function then
  217. energy_type = 'J'
  218. energy_function = function (val) return val end
  219. end
  220.  
  221. -- Starts monitor
  222. term.redirect(monitor)
  223. monitor.setTextScale(options.text_scale)
  224.  
  225. while true do
  226. local _, message = rednet.receive(proto)
  227. local newItem = string.gsub(message, '&', '\n')
  228. queue(newItem)
  229. queue_flush()
  230. os.sleep(options.update_frequency)
  231. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement