Advertisement
HandieAndy

quantum-beacon

Dec 29th, 2022 (edited)
1,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.43 KB | Gaming | 0 0
  1. --[[
  2.     Quantum Beacon
  3.  
  4. A script that runs on computers connected to an AE quantum ring to ping their
  5. status back to a central computer.
  6. ]]
  7.  
  8. local SEND_CHANNEL = 100
  9. local RECEIVE_CHANNEL = 101
  10. local NODE_NAME = "TMP"
  11. local TRANSMIT_INTERVAL = 15
  12.  
  13. local function drawScreen(online)
  14.     local w, h = term.getSize()
  15.     term.setBackgroundColor(colors.black)
  16.     term.clear()
  17.    
  18.     term.setCursorPos(1, 1)
  19.     term.setBackgroundColor(colors.gray)
  20.     term.setTextColor(colors.white)
  21.     term.clearLine()
  22.     term.write("Quantum Beacon")
  23.  
  24.    
  25.     term.setBackgroundColor(colors.black)
  26.     term.setTextColor(colors.lightGray)
  27.     term.setCursorPos(1, 3)
  28.     term.write("Node:")
  29.     term.setCursorPos(1, 5)
  30.     term.write("Channel:")
  31.     term.setCursorPos(1, 7)
  32.     term.write("Transmit Interval:")
  33.     term.setCursorPos(1, 9)
  34.     term.write("Online Status:")
  35.  
  36.     term.setTextColor(colors.white)
  37.     term.setCursorPos(20, 3)
  38.     term.write(NODE_NAME)
  39.     term.setCursorPos(20, 5)
  40.     term.write(tostring(SEND_CHANNEL))
  41.     term.setCursorPos(20, 7)
  42.     term.write(tostring(TRANSMIT_INTERVAL))
  43.     local str = nil
  44.     if online then
  45.         str = "Online"
  46.         term.setTextColor(colors.lime)
  47.     else
  48.         str = "Offline"
  49.         term.setTextColor(colors.red)
  50.     end
  51.     term.setCursorPos(20, 9)
  52.     term.write(str)
  53. end
  54.  
  55. local function clearMessageLine()
  56.     local _, h = term.getSize()
  57.     term.setBackgroundColor(colors.gray)
  58.     for i = 0, 3 do
  59.         term.setCursorPos(1, h - i)
  60.         term.clearLine()
  61.     end
  62. end
  63.  
  64. local function showError(msg)
  65.     clearMessageLine()
  66.     local _, h = term.getSize()
  67.     term.setCursorPos(1, h - 2)
  68.     term.setBackgroundColor(colors.gray)
  69.     term.setTextColor(colors.red)
  70.     write(msg)
  71. end
  72.  
  73. local function showMessage(msg)
  74.     clearMessageLine()
  75.     local _, h = term.getSize()
  76.     term.setCursorPos(1, h - 2)
  77.     term.setBackgroundColor(colors.gray)
  78.     term.setTextColor(colors.lightBlue)
  79.     write(msg)
  80. end
  81.  
  82. local function getPeripheralOrWait(name)
  83.     local p = nil
  84.     repeat
  85.         p = peripheral.find(name)
  86.         if p == nil then
  87.             showError("Error: Couldn't find an attached peripheral with name \"" .. name .. "\". Please attach one.")
  88.             os.pullEvent("peripheral")
  89.             if p ~= nil then
  90.                 showMessage("Peripheral \"" .. name .. "\" connected. Resuming operations shortly.")
  91.                 os.sleep(3)
  92.             end
  93.         end
  94.     until p ~= nil
  95.     return p
  96. end
  97.  
  98. -- We consider a foreign quantum link to be connected if we detect crafting
  99. -- CPUs on the network, since subnetworks shouldn't ever have these.
  100. local function meSystemConnected(meBridge)
  101.     local craftingCpus = meBridge.getCraftingCPUs()
  102.     return craftingCpus ~= nil and #craftingCpus > 0
  103. end
  104.  
  105.  
  106.  
  107. drawScreen(false)
  108. local lastOnlineStatus = false
  109.  
  110. while true do
  111.     local modem = getPeripheralOrWait("modem")
  112.     local meBridge = getPeripheralOrWait("meBridge")
  113.     local packet = {
  114.         node = NODE_NAME,
  115.         date = os.date("*t"),
  116.         online = meSystemConnected(meBridge)
  117.     }
  118.     modem.transmit(SEND_CHANNEL, RECEIVE_CHANNEL, packet)
  119.     drawScreen(packet.online)
  120.     if lastOnlineStatus == true and packet.online == false then
  121.         showError("The quantum link just went offline.")
  122.         os.sleep(3)
  123.     else
  124.         os.sleep(TRANSMIT_INTERVAL)
  125.     end
  126.     lastOnlineStatus = packet.online
  127. end
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement