Advertisement
gelatine87

MekanismPortal

May 7th, 2024 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.63 KB | Gaming | 0 0
  1. -- Load necessary APIs
  2. os.loadAPI("/button")
  3.  
  4. -- Configuration
  5. local config = {
  6.     boxColor = colors.blue,
  7.     buttonsPerRow = 4,
  8.     buttonWidth = 16,
  9.     buttonHeight = 3,
  10.     textScale = 0.5,
  11.     infoBG = colors.red,
  12.     updateTime = 60
  13. }
  14.  
  15. -- ########### ToDo
  16. -- set different portal color after portal switch
  17.  
  18. -- Debug data
  19. local debug = false
  20. local debugArray = {}
  21. local entries = 200
  22.  
  23. -- Populate debugArray
  24. for i = 1, entries do
  25.     table.insert(debugArray, {key = "Portal" .. i, public = true})
  26. end
  27.  
  28. -- Function to ensure the presence of required peripherals (monitor and teleporter).
  29. function checkPeripheral(name, errorMessage)
  30.     local peripheral = peripheral.find(name)
  31.     if not peripheral then
  32.         error(errorMessage)
  33.     end
  34.     return peripheral
  35. end
  36.  
  37. -- Retrieve peripherals
  38. local mon = checkPeripheral("monitor", "Monitor peripheral not found.")
  39. local tp = checkPeripheral("teleporter", "Teleporter peripheral not found.")
  40.  
  41. lastActiveButton = nil
  42.  
  43. -- Determine whether to use debug data or actual teleporter frequencies.
  44. local inputArray = debug and debugArray or tp.getFrequencies()
  45.  
  46. -- Function to display a countdown timer on the monitor when the monitorsize is incorrect.
  47. function showTimer(seconds, centerX, centerY)
  48.     while seconds >= 0 do
  49.         mon.setCursorPos(centerX, centerY)
  50.         mon.setTextColor(colors.white)
  51.         mon.setBackgroundColor(colors.black)
  52.         mon.write("Timer: " .. tostring(seconds) .. "s remaining ")
  53.         os.sleep(1)
  54.         seconds = seconds - 1
  55.     end
  56.     os.reboot()
  57. end
  58.  
  59. -- Function to check and handle monitor size discrepancies.
  60. function checkMonitorSize()
  61.     local w, h = mon.getSize()
  62.     if w ~= 79 or h ~= 38 then
  63.         mon.clear()
  64.         local centerX = math.floor((w - #"ERROR: Monitor size is not correct.") / 2)
  65.         local centerY = math.floor(h / 2)
  66.         mon.setCursorPos(centerX, centerY)
  67.         mon.setTextColor(colors.red)
  68.         mon.write("ERROR: Monitor size is not correct.")
  69.         centerY = centerY + 1
  70.         mon.setCursorPos(centerX, centerY)
  71.         mon.write("Required size: 3 Heigh / 4 Width")
  72.         centerY = centerY + 1
  73.         mon.setCursorPos(centerX, centerY)
  74.         mon.write("Please adjust the monitor size and restart.")
  75.         showTimer(10, centerX, centerY + 2)
  76.         error("Monitor size is not correct.")
  77.     end
  78. end
  79.  
  80. -- Set monitor text properties and initialize variables for pagination.
  81. mon.setTextScale(config.textScale)
  82. mon.setBackgroundColor(colors.black)
  83.  
  84. currentPage = 1
  85. maxButtons = 24
  86. totalPages = math.ceil(#inputArray / maxButtons)
  87.  
  88. -- Function to set the frequency based on the button pressed.
  89. local function setFrequency(freq)
  90.     if not debug then
  91.         tp.setFrequency(freq)      
  92.     end
  93.     button.toggleButton(freq)
  94. end
  95.  
  96.  
  97. -- Function to dynamically fill the button table based on the current page.
  98. function fillTable()
  99.     local w, h = mon.getSize()
  100.     local spacingX = 2
  101.     local spacingY = 1
  102.    
  103.     local maxButtons = maxButtons
  104.     local startIndex = (currentPage - 1) * maxButtons + 1
  105.     local endIndex = math.min(startIndex + maxButtons - 1, #inputArray)
  106.    
  107.     local totalRows = math.ceil(maxButtons / config.buttonsPerRow)
  108.     local totalWidth = config.buttonsPerRow * (config.buttonWidth + spacingX) - spacingX
  109.     local startX = math.floor((w - totalWidth) / 2) + 1
  110.    
  111.     for i = startIndex, endIndex do
  112.         -- Calculate the position of the current button
  113.         local col = (i - 1) % config.buttonsPerRow
  114.         local row = math.floor((i - 1) / config.buttonsPerRow) % totalRows
  115.         local x = startX + col * (config.buttonWidth + spacingX)
  116.         local y = 9 + row * (config.buttonHeight + spacingY)
  117.        
  118.         -- Place the button at the calculated position
  119.         local entry = inputArray[i]
  120.         button.setTable(entry.key, function() setFrequency(entry.key) end, x, x + config.buttonWidth - 1, y, y + config.buttonHeight - 1)
  121.     end
  122.  
  123.     if currentPage  > 1 then
  124.         button.setTable("Previous", prevPage, 2 , 22 , 35 , 37)
  125.     end
  126.  
  127.     button.setTable("Refresh", refresh, 30 , 50 , 35 , 37)
  128.    
  129.     if currentPage < totalPages then
  130.         button.setTable("Next" , nextPage, 58 , 78 , 35 , 37)
  131.     end
  132.    
  133.     button.screen()
  134. end
  135.  
  136. -- Function to display portal information.
  137. function displayInfo()
  138.     mon.setCursorPos(10, 6)
  139.     mon.setBackgroundColor(config.infoBG)
  140.     mon.setTextColor(colors.white)
  141.     mon.write("Active Portal:                           Status:             ")
  142. end
  143.  
  144. -- Function to update portal information continuously.
  145. local function updateInfo()
  146.     while true do
  147.         local afq = tp.getFrequency()
  148.         local sfq = tp.getStatus()
  149.         mon.setCursorPos(25,6)
  150.         mon.setBackgroundColor(config.infoBG)
  151.         mon.setTextColor(colors.white)
  152.         mon.write(string.format("%-25s", afq.key))
  153.         mon.setCursorPos(60,6)
  154.         mon.write(string.format("%-10s", sfq:sub(1, 1):upper() .. sfq:sub(2)))
  155.         mon.setBackgroundColor(colors.black)
  156.         os.sleep(0.25)
  157.     end
  158. end
  159.  
  160. -- Function to initialize the main interface.
  161. local function main()
  162.     button.clearTable()
  163.     checkMonitorSize()
  164.     fillTable()
  165.     button.drawButtonBoxes(config.boxColor)
  166.     button.drawBox(5, 5, 74, 7, config.infoBG)
  167.     button.drawBox(5, 6, 74, 6, config.infoBG)
  168.     button.heading(" Portal Control ", 2, config.infoBG)
  169.     displayInfo()
  170.     local activeFreq = tp.getFrequency()
  171.     if not debug then
  172.         button.toggleButton(activeFreq.key)
  173.     end
  174. end
  175.  
  176. -- Functions to navigate to the next page of portals.
  177. function nextPage()
  178.     currentPage = currentPage + 1
  179.     button.flash("Next")
  180.     main()
  181. end
  182.  
  183. -- Functions to navigate to the previous page of portals.
  184. function prevPage()
  185.     if currentPage > 1 then
  186.         currentPage = currentPage - 1
  187.         button.flash("Previous")
  188.         main()
  189.     end
  190. end
  191.  
  192. -- Function to refresh the portal list.
  193. function refresh()
  194.     button.flash("Refresh")
  195.     inputArray = debug and debugArray or tp.getFrequencies()
  196.     main()
  197. end
  198.  
  199. -- Function to handle monitor touch events.
  200. local function getClick()
  201.     event,side,x,y = os.pullEvent("monitor_touch")
  202.     button.checkxy(x,y)
  203. end
  204.  
  205. -- Function to auto-refresh frequencies.
  206. local function autoRefresh()
  207.     while true do
  208.         os.sleep(config.updateTime)
  209.         inputArray = debug and debugArray or tp.getFrequencies()
  210.         main()
  211.     end
  212. end
  213.  
  214. -- Initialize the main interface.
  215. main()
  216.  
  217. -- Continuously wait for monitor touch events or display portal information.
  218. while true do
  219.     parallel.waitForAny(getClick, updateInfo, autoRefresh)
  220. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement