xo33ab

flux.lua

Nov 25th, 2023 (edited)
137
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.93 KB | None | 0 0
  1. local m = peripheral.find("monitor")
  2. local side_bundle = ("front")
  3.  
  4. local t = {
  5.     purple = {
  6.         desc = "Purple ",
  7.         side = "right",
  8.         flux_level = 0,
  9.         flux_max = 7,
  10.         output = colours.purple, -- used for bundle
  11.         enderio_output = pink
  12.     },
  13.  
  14.     lime = {
  15.         desc = "Lime   ",
  16.         side = "bottom",
  17.         flux_level = 0,
  18.         flux_max = 7,
  19.         output = colours.lime, --
  20.         enderio_output = cyan
  21.     },
  22.  
  23.     orange = {
  24.         desc = "Orange ",
  25.         side = "left",
  26.         flux_level = 0,
  27.         flux_max = 7,
  28.         output = colours.orange, --
  29.         enderio_output = yellow
  30.     },
  31.  
  32.     brown = {
  33.         desc = "Central",
  34.         side = "top",
  35.         flux_level = 0,
  36.         flux_max = 5,
  37.         output = colours.brown, --
  38.         enderio_output = black
  39.     }
  40. }
  41.  
  42. ---------------------------
  43. --Rednet sender, receiver
  44. ---------------------------
  45. peripheral.find("modem",rednet.open)
  46.  
  47. local function sendTable()
  48.     msg = textutils.serialise(t)
  49.     rednet.send(5,msg)  -- 5 id of the computer, type id in the terminal
  50.     print("Rednet send")
  51. end
  52.  
  53. local function receiveTable()
  54.     id, msg = rednet.receive()
  55.     t = textutils.unserialise(msg)
  56. end
  57.  
  58.  
  59. ---------------------------
  60. --Monitor related stuff
  61. ---------------------------
  62. --term.redirect(m)
  63.  
  64. local monitors = {peripheral.find("monitor")}
  65.  
  66.  
  67. m.setCursorPos(0,0)
  68. m.setBackgroundColor(colors.black)
  69. m.setTextColor(colors.white)
  70. m.clear()
  71. m.setTextScale(3)
  72.  
  73.  
  74. ----------------------------
  75. --Prints Table for debugging
  76. ----------------------------
  77. local function printTable(e)
  78.     if type(e) == "table" then
  79.         for k,v in pairs(e) do
  80.             print(k,v)
  81.             printTable(v)       -- recursively repeat the same procedure
  82.             sleep(2)
  83.         end
  84.     else
  85.     end
  86. end
  87.  
  88. ---------------------------
  89. --Updates table
  90. ---------------------------
  91. local function getUpdate()
  92.     for k,v in pairs(t) do
  93.     v.flux_level = rs.getAnalogInput(v.side)
  94.     end
  95. end
  96.  
  97. ---------------------------
  98. --Emits bundled redstone when flux max is exceeded
  99. ---------------------------
  100. local function stopProduction()
  101.     for k,v in pairs(t) do
  102.         sleep(0)
  103.         if v.flux_level >= v.flux_max then
  104.         rs.setBundledOutput(side_bundle,colours.combine(rs.getBundledOutput("front"),v.output))
  105.         rs.setAnalogOutput(v.side,1)
  106.        -- print("Stoped:", v.desc, v.flux_level)
  107.         else
  108.         sleep(0)
  109.         rs.setBundledOutput(side_bundle,colours.subtract(rs.getBundledOutput("front"), v.output))
  110.         rs.setAnalogOutput(v.side,0)
  111.        -- print("Good")
  112.         end
  113.     end
  114. end
  115.  
  116. ---------------------------
  117. --Visual table
  118. ---------------------------
  119. local w = 0
  120. local h = 0
  121.  
  122. local function displayFlux()
  123.     for k,v in pairs(t) do
  124.     h = h + 1
  125.     if h > 4 then h = 1 end
  126.     m.setCursorPos(1,h)
  127.     m.setBackgroundColor(v.output)
  128.     m.write(v.desc)
  129.  
  130.     m.setBackgroundColor(colors.black)
  131.     m.setCursorPos(8,h)
  132.  
  133.     if v.flux_level < v.flux_max * 0.5 then
  134.     m.setBackgroundColor(colors.green)
  135.     m.write(v.flux_level.."/"..v.flux_max)
  136.     elseif v.flux_level < v.flux_max * 0.8 then
  137.     m.setBackgroundColor(colors.yellow)
  138.     m.write(v.flux_level.."/"..v.flux_max)
  139.     else
  140.     m.setBackgroundColor(colors.red)
  141.     m.write(v.flux_level.."/"..v.flux_max)
  142.     end
  143. end
  144. end
  145.  
  146.  
  147. ---------------------------
  148. --Starts the script
  149. ---------------------------
  150. local n = 0
  151. local function start()
  152.     while true do
  153.      -- if os.pullEvent("redstone") then
  154.             getUpdate() -- updates table
  155.             stopProduction() -- emits bundledoutput if flux is high
  156.            -- printTable(t)
  157.            n = n + 1
  158.            print("--------------------------------")
  159.            print("--------------------------------",n)
  160.            displayFlux()
  161.            sendTable()
  162.  
  163.       -- end-- pullevent redstone
  164.     end
  165. end
  166.  
  167. start()
Advertisement
Comments
Add Comment
Please, Sign In to add comment