Advertisement
Skip_21

Advanced Peripherals AR Client

May 5th, 2023 (edited)
614
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.55 KB | None | 0 1
  1. -- Program created by Skip_21
  2. -- Require Advanced Peripherals
  3. -- Currently a W.I.P (Work In Progress)
  4. version = "alpha 0.1"
  5.  
  6. fails = 0
  7.  
  8. pblocks = {}
  9.  
  10. knownperipherals = {"inductionPort"}
  11.  
  12. function searchlist(searchfor, list)
  13.     for i = 1, #list do
  14.         if list[i] == searchfor then
  15.             return true
  16.         end
  17.     end
  18.     return false
  19. end
  20.  
  21. function addperipheral(side)
  22.  
  23.     local result = false
  24.     thistype = peripheral.getType(side)
  25.  
  26.     if searchlist(thistype, knownperipherals) then
  27.  
  28.         print("Attaching to the " .. side .. " (" .. thistype .. ")")
  29.         table.insert(pblocks, {peripheral.wrap(side), thistype, side})
  30.         result = true
  31.  
  32.     end
  33.  
  34.     return result
  35.  
  36. end
  37.  
  38. function timedmessages()
  39.  
  40.     local sleepytime = 2
  41.  
  42.     -- Peripherals checking
  43.  
  44.     local i = 1
  45.     while i <= #pblocks do
  46.         local rtype = peripheral.getType(pblocks[i][3])
  47.         if rtype == pblocks[i][2] then
  48.             i = i + 1
  49.  
  50.         else
  51.             print(pblocks[i][2] .. " on " .. pblocks[i][3] .. "has gone missing: removing.")
  52.             table.remove(pblocks, i)
  53.         end
  54.     end
  55.  
  56.     for i = 1, #pblocks do
  57.  
  58.         if pblocks[i][2] == "inductionPort" then
  59.  
  60.             emax = peripheral.wrap(pblocks[i][3]).getMaxEnergy()
  61.             estor = peripheral.wrap(pblocks[i][3]).getEnergy()
  62.             ein = peripheral.wrap(pblocks[i][3]).getLastInput()
  63.             eout = peripheral.wrap(pblocks[i][3]).getLastOutput()
  64.             percent = peripheral.wrap(pblocks[i][3]).getEnergyFilledPercentage()
  65.  
  66.             local message = {emax, estor, ein, eout, percent}
  67.  
  68.             rednet.send(arserver, message, "matrix")
  69.  
  70.         end
  71.  
  72.     end
  73.  
  74.     return sleepytime
  75.  
  76. end
  77.  
  78. function eventlistener()
  79.  
  80.     local event, p1, p2, p3 = os.pullEvent()
  81.  
  82.     if event == "rednet_message" and p3 == "arserver" then
  83.  
  84.         if p2[1] == "reboot" then
  85.             print("Rebooting...")
  86.             sleep(2)
  87.             os.reboot()
  88.         end
  89.         if p2[1] == "br" then
  90.             br(p2)
  91.         end
  92.  
  93.     elseif event == "timer" and p1 == timedmessage then
  94.  
  95.         timedmessage = os.startTimer(timedmessages())
  96.  
  97.     end
  98.  
  99. end
  100.  
  101. -- Init
  102.  
  103. term.clear()
  104. term.setCursorPos(1, 1)
  105. print("Skip_21's Smart Helmet " .. version .. " running !")
  106.  
  107. sleep(2)
  108.  
  109. if (os.getComputerLabel() == nil) then
  110.     print("Setting computer label to 'SHClient'.")
  111.     os.setComputerLabel("SHClient")
  112. end
  113.  
  114. sides = peripheral.getNames()
  115.  
  116. modemhere = false
  117.  
  118. for i = 1, #sides do
  119.  
  120.     thistype = peripheral.getType(sides[i])
  121.  
  122.     if thistype == "modem" then
  123.  
  124.         print("Modem found on " .. sides[i] .. ", connecting...")
  125.         rednet.open(sides[i])
  126.         modemhere = true
  127.  
  128.     end
  129.  
  130. end
  131.  
  132. if not modemhere then
  133.     error("Please connect a Wireless Modem")
  134. end
  135.  
  136. arserver = rednet.lookup("arserver", "arserver")
  137.  
  138. while arserver == nil do
  139.  
  140.     fails = fails + 1
  141.     print("Can't find SHserver. Waiting ... (" .. fails .. ")")
  142.     os.sleep(10)
  143.     arserver = rednet.lookup("arserver", "arserver")
  144.  
  145. end
  146.  
  147. print("Found SHServer! Sending handshake.")
  148.  
  149. rednet.send(arserver, "hello", "handshake")
  150.  
  151. print("Running ...")
  152.  
  153. -- peripherals Lookup
  154.  
  155. local count = 0
  156. for i = 1, #sides do
  157.     if addperipheral(sides[i]) then
  158.         count = count + 1
  159.     end
  160. end
  161.  
  162. if count == 0 then
  163.     error("No recognised peripherals. Aborting.")
  164. else
  165.     print("Attached to " .. count .. " recognised peripherals.")
  166. end
  167.  
  168. -- main loop
  169.  
  170. timedmessage = os.startTimer(5)
  171.  
  172. while true do
  173.  
  174.     eventlistener()
  175.  
  176. end
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement