Advertisement
Epoc

RailStatus : IG Client

Aug 27th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | None | 0 0
  1. --    ___              _       _      ___     _               _                    
  2. --   | _ \   __ _     (_)     | |    / __|   | |_    __ _    | |_    _  _     ___  
  3. --   |   /  / _` |    | |     | |    \__ \   |  _|  / _` |   |  _|  | +| |   (_-<  
  4. --   |_|_\  \__,_|   _|_|_   _|_|_   |___/   _\__|  \__,_|   _\__|   \_,_|   /__/_  
  5. -- _|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|
  6. -- "`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'
  7. --
  8. --                              RailStatus : IG Client
  9. --
  10. -- La seconde génération de RailStatus (http://pastebin.com/js3pN87r), un programme permettant
  11. -- de connaître en temps réel et en jeu (in game) le statut des aiguillages d'un réseau ferroviaire en
  12. -- utilisant RailCraft, ComputerCraft et RedPower
  13. --
  14. -- Utilisation :
  15. -- 1. Dans un ordinateur, importer ce programme depuis Pastebin :
  16. --     pastebin get KsJ9gukb railstatusigc
  17. -- 2. Lancer le programme :
  18. --     railstatusig [côté duquel le bundled câble est connecté] [ID de cet ordinateur] [ID de l'ordinateur serveur]
  19. --     Exemple : railstatusigc left 2 1
  20. --
  21. -- Serveur : http://pastebin.com/KAy0Kqfw
  22. -- Dépôt Github : https://github.com/EaSyCompanyFr/RailStatus
  23.  
  24. term.clear()
  25. term.setCursorPos(1,1)
  26.  
  27. print("RailStatus : IG Client is starting...")
  28.  
  29. -- Arguments passés au programme
  30. args = {...}
  31.  
  32. if table.getn(args) < 2 then
  33.   print("  One or more parameters are missing")
  34.   return
  35. end
  36.  
  37. -- --------------------------------------------------------
  38. -- Constantes
  39.  
  40. -- De quel côté de l'ordinateur arrive le bundled cable ?
  41. CABLE_SIDE = args[1]
  42.  
  43. print("  Cable side : "..CABLE_SIDE)
  44.  
  45. -- Identifiant de l'ordinateur regroupant les aiguillages
  46. COMPUTER_ID = args[2]
  47.  
  48. print("  Computer ID : "..COMPUTER_ID)
  49.  
  50. -- ID réel de l'ordinateur serveur
  51. SERVER_COMPUTER_ID = tonumber(args[3])
  52.  
  53. print("  Server computer ID : "..SERVER_COMPUTER_ID)
  54.  
  55. -- Récupération du premier type de périphérique détecté
  56. function getFirstPeripheralSide(peripheral_type)
  57.   for k,side in pairs(redstone.getSides()) do
  58.     if peripheral.isPresent(side) and peripheral.getType(side) == peripheral_type then
  59.       return side
  60.     end
  61.   end
  62.  
  63.   return false
  64. end
  65.  
  66. -- Modem
  67. MODEM_SIDE = getFirstPeripheralSide("modem")
  68.  
  69. if not MODEM_SIDE then
  70.   print(" Modem not detected on this computer")
  71.   return
  72. end
  73.  
  74. print("  Modem side : "..MODEM_SIDE)
  75.  
  76. print("")
  77. print("To stop RailStatus, hold CTRL+T for some seconds.")
  78.  
  79. -- Tableau de correspondance nom de couleur > identifiant décimal
  80. COLORS_ARRAY = {}
  81.  
  82. COLORS_ARRAY["white"] = colors.white
  83. COLORS_ARRAY["orange"] = colors.orange
  84. COLORS_ARRAY["magenta"] = colors.magenta
  85. COLORS_ARRAY["lightBlue"] = colors.lightBlue
  86. COLORS_ARRAY["yellow"] = colors.yellow
  87. COLORS_ARRAY["lime"] = colors.lime
  88. COLORS_ARRAY["pink"] = colors.pink
  89. COLORS_ARRAY["gray"] = colors.gray
  90. COLORS_ARRAY["lightGray"] = colors.lightGray
  91. COLORS_ARRAY["cyan"] = colors.cyan
  92. COLORS_ARRAY["purple"] = colors.purple
  93. COLORS_ARRAY["blue"] = colors.blue
  94. COLORS_ARRAY["brown"] = colors.brown
  95. COLORS_ARRAY["green"] = colors.green
  96. COLORS_ARRAY["red"] = colors.red
  97. COLORS_ARRAY["black"] = colors.black
  98.  
  99. -- --------------------------------------------------------
  100. -- Fonctions
  101.  
  102. -- Teste un bundled cable avec toutes les couleurs possible, et retourne un tableau indexé contenant
  103. -- en index la couleur et en valeur un booléen (sous forme d'entier) indiquant si le signal redstone est actif ou non
  104. function getBundledInputAsTable(side)
  105.   local ret = {}
  106.  
  107.   for key,value in pairs(COLORS_ARRAY) do
  108.     ret[key] = redstone.testBundledInput(side, value) and 1 or 0 -- Convertit le booléen retourné en entier
  109.   end
  110.  
  111.   return ret
  112. end
  113.  
  114. -- --------------------------------------------------------
  115. -- Démarrage du programme
  116.  
  117. rednet.open(MODEM_SIDE)
  118.  
  119. while true do
  120.   local event, p1, p2, p3 = os.pullEvent()
  121.  
  122.   if event == "rednet_message" then
  123.     local senderId = p1
  124.     local message = p2
  125.     local distance = p3
  126.  
  127.     redstone.setBundledOutput(CABLE_SIDE, tonumber(message))
  128.   elseif event == "redstone" then
  129.     local bundled_cable = getBundledInputAsTable(CABLE_SIDE) -- On chope le statut de chaque câbles de couleur
  130.     rednet.send(SERVER_COMPUTER_ID, COMPUTER_ID.."|"..textutils.serialize(bundled_cable).."|"..os.getComputerID(), true)
  131.   end
  132. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement