Advertisement
Skip_21

Advanced Peripherals AR Server

May 4th, 2023 (edited)
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.46 KB | None | 0 0
  1. -- Program created by Skip_21
  2. -- Require Advancedperipherals
  3. version = "alpha 0.1"
  4.  
  5. knownprotocols = {"dns", "matrix"}
  6.  
  7. -- Defining resolution values
  8. x = 1600
  9. y = 900
  10.  
  11.  
  12. icons = {}
  13.  
  14. function readablenumber(n)
  15.     if n >= 10000000000000000 then
  16.         return "" .. (math.floor((n / 1000000000000000) + 0.5)) .. " P"
  17.     end
  18.     if n >= 1000000000000000 then
  19.         return "" .. (math.floor((n / 100000000000000) + 0.5) / 10) .. " P"
  20.     end
  21.     if n >= 10000000000000 then
  22.         return "" .. (math.floor((n / 1000000000000) + 0.5)) .. " T"
  23.     end
  24.     if n >= 1000000000000 then
  25.         return "" .. (math.floor((n / 100000000000) + 0.5) / 10) .. " T"
  26.     end
  27.     if n >= 10000000000 then
  28.         return "" .. (math.floor((n / 1000000000) + 0.5)) .. " G"
  29.     end
  30.     if n >= 1000000000 then
  31.         return "" .. (math.floor((n / 100000000) + 0.5) / 10) .. " G"
  32.     end
  33.     if n >= 10000000 then
  34.         return "" .. (math.floor((n / 1000000) + 0.5)) .. " M"
  35.     end
  36.     if n >= 1000000 then
  37.         return "" .. (math.floor((n / 100000) + 0.5) / 10) .. " M"
  38.     end
  39.     if n >= 10000 then
  40.         return "" .. (math.floor((n / 1000) + 0.5)) .. " k"
  41.     end
  42.     if n >= 1000 then
  43.         return "" .. (math.floor((n / 100) + 0.5) / 10) .. " k"
  44.     end
  45.     if n >= 100 then
  46.         return "" .. math.floor((n) + 0.5) .. " "
  47.     end
  48.     return "" .. (math.floor((n * 10) + 0.5) / 10) .. " "
  49. end
  50.  
  51. function searchlist(searchfor, list)
  52.  
  53.     for i = 1, #list do
  54.  
  55.         if list[i] == searchfor then
  56.             return true
  57.         end
  58.  
  59.     end
  60.  
  61.     return false
  62.  
  63. end
  64.  
  65. function netevent(p1, p2, p3)
  66.  
  67.     local sender = p1
  68.     local message = p2
  69.     local protocol = p3
  70.  
  71.     if not searchlist(protocol, knownprotocols) then
  72.         return
  73.     end
  74.  
  75.     if protocol == "dns" then
  76.  
  77.         print("Received handshake request from client " .. sender)
  78.  
  79.     elseif protocol == "matrix" then
  80.  
  81.         local estor = message[2]/2.5
  82.         local emax = message[1]/2.5
  83.         local ein = message[3]/2.5
  84.         local eout = message[4]/2.5
  85.         local percent = message[5]
  86.  
  87.         ant.fillGradientWithId("energybar", 2, 10, 32, 110, 0x00FF00, 0xFF0000)
  88.         ant.fillWithId("energyhide", 2, 10, 32, (110-percent), 0x000000)
  89.  
  90.         ant.drawStringWithId("estor", ("Energy Stored : " .. readablenumber(estor) .. "RF"), 33, 10, 0xFFFFFF)
  91.         ant.drawStringWithId("emax", ("Maximum Capacity : " .. readablenumber(emax) .. "RF"), 33, 20, 0xFFFFFF)
  92.         ant.drawStringWithId("ein", ("Energy Input : " .. readablenumber(ein) .. "RF/t"), 33, 30, 0xFFFFFF)
  93.         ant.drawStringWithId("eout", ("Energy Output : " .. readablenumber(eout) .. "RF/t"), 33, 40, 0xFFFFFF)
  94.     end
  95.  
  96.     return
  97.  
  98. end
  99.  
  100. function max(a, b)
  101.  
  102.     if a > b then
  103.         return a
  104.     else
  105.         return b
  106.     end
  107.  
  108. end
  109.  
  110. function eventlistener()
  111.  
  112.     local event, p1, p2, p3 = os.pullEvent()
  113.  
  114.     if event == "rednet_message" then
  115.         netevent(p1, p2, p3)
  116.     end
  117.  
  118. end
  119.  
  120. -- Init
  121.  
  122. term.clear()
  123. term.setCursorPos(1, 1)
  124. print("Skip_21's Smart Helmet " .. version .. " running !")
  125. print("I'm still working on it so expect a lot of bugs")
  126. if (os.getComputerLabel() == nil) then
  127.     print("Setting computer label to 'ARServer'.")
  128.     os.setComputerLabel("ARServer")
  129. end
  130.  
  131. sides = peripheral.getNames()
  132.  
  133. antennahere = false
  134. chatboxhere = false
  135. modemhere = false
  136.  
  137. for i = 1, #sides do
  138.  
  139.     thistype = peripheral.getType(sides[i])
  140.  
  141.     if thistype == "arController" then
  142.  
  143.         print("AR Controller found on " .. sides[i] .. ", connecting...")
  144.         ant = peripheral.wrap(sides[i])
  145.         antennahere = true
  146.  
  147.     end
  148.  
  149.     if thistype == "chatBox" then
  150.  
  151.         print("ChatBox found on " .. sides[i] .. ", connecting...")
  152.         chb = peripheral.wrap(sides[i])
  153.         chatboxhere = true
  154.  
  155.     end
  156.  
  157.     if thistype == "modem" then
  158.  
  159.         print("Modem found on " .. sides[i] .. ", connecting...")
  160.         rednet.open(sides[i])
  161.         modemside = sides[i]
  162.         modemhere = true
  163.  
  164.     end
  165.  
  166. end
  167.  
  168. if not antennahere then
  169.     error("Please connect an AR Controller from Advanced Peripherals mod")
  170. end
  171. if not chatboxhere then
  172.     error("Please connect a chatbox from Advanced Peripherals mod")
  173. end
  174. if not modemhere then
  175.     error("Please connect a Wireless Modem")
  176. end
  177.  
  178. rednet.host("arserver", "arserver")
  179. print("Running ...")
  180.  
  181. ant.clear()
  182.  
  183. ant.setRelativeMode(true, x, y)
  184.  
  185. -- main loop
  186.  
  187. while true do
  188.  
  189.     eventlistener()
  190.  
  191. end
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement