Advertisement
Omsigames

controllers.lua

Dec 25th, 2024 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.70 KB | None | 0 0
  1. local component = require("component")
  2.  
  3. local controllerNames = {}
  4. local controllerAdrs = {}
  5.  
  6. for address, name in component.list("controller", false) do
  7.     table.insert(controllerNames, component.proxy(component.get(address)).getControllerName())
  8.     table.insert(controllerAdrs, component.get(address))
  9. end
  10.  
  11. function table_contains(tbl, x)
  12.     found = false
  13.     for _, v in pairs(tbl) do
  14.         if v == x then
  15.             found = true
  16.         end
  17.     end
  18.     return found
  19. end
  20.  
  21. function findIndexInTable(table, x)
  22.     for i, value in ipairs(table) do
  23.         if value == x then
  24.             return i
  25.         end
  26.     end
  27.     return nil
  28. end
  29.  
  30. local function getAddress(name)
  31.     if table_contains(controllerNames, name) then
  32.         for var = #controllerNames, 1, -1 do
  33.             if (controllerNames[var] == name) then
  34.                 return controllerAdrs[var]
  35.             end
  36.         end
  37.     else
  38.         table.insert(controllerNames, name)
  39.         table.insert(controllerAdrs, "NotConnected")
  40.         return "NotConnected"
  41.     end
  42. end
  43.  
  44. local controllers = {}
  45.  
  46. function controllers.isConnected(name)
  47.     local var = findIndexInTable(controllerNames, name)
  48.     if (controllerAdrs[var] == "NotConnected") then
  49.         return false
  50.     end
  51.     return true
  52. end
  53.  
  54. function controllers.printTable()
  55.     for var = #controllerNames, 1, -1 do
  56.         print(controllerNames[var])
  57.         print(controllerAdrs[var])
  58.     end
  59. end
  60.  
  61. controllers.Signals = component.proxy(getAddress("Signals"))     -- Signals
  62. controllers.Switches = component.proxy(getAddress("Switches"))   -- Switches
  63. controllers.Crossings = component.proxy(getAddress("Crossings")) -- Crossings
  64.  
  65. return controllers
  66.  
Tags: ORMS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement