Advertisement
Skip_21

Smart Helmet

Nov 23rd, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.79 KB | None | 0 0
  1. -- Program created by Skip_21
  2. -- Require PeripheralsPlusOne
  3. -- Added multiple players handling
  4.  
  5. version = "alpha 1.5"
  6.  
  7. knownprotocols = {"handshake","reactor","matrix","IDSU", "ic2"}
  8.  
  9. icons = {}
  10.  
  11.  
  12. function readablenumber(n)
  13.   if n >= 10000000000 then return ""..(math.floor((n/1000000000)+0.5)).." G" end
  14.   if n >= 1000000000 then return ""..(math.floor((n/100000000)+0.5)/10).." G" end
  15.   if n >= 10000000 then return ""..(math.floor((n/1000000)+0.5)).." M" end
  16.   if n >= 1000000 then return ""..(math.floor((n/100000)+0.5)/10).." M" end
  17.   if n >= 10000 then return ""..(math.floor((n/1000)+0.5)).." k" end
  18.   if n >= 1000 then return ""..(math.floor((n/100)+0.5)/10).." k" end
  19.   if n >= 100 then return ""..math.floor((n)+0.5).." " end
  20.   return ""..(math.floor((n*10)+0.5)/10).." "
  21. end
  22.  
  23.  
  24.  
  25. function guiConfig()
  26.  
  27.     print("Test")
  28.  
  29. end
  30.  
  31.  
  32. function searchlist(searchfor,list)
  33.  
  34.     for i=1,#list do
  35.  
  36.         if list[i]==searchfor then return true end
  37.  
  38.     end
  39.  
  40.     return false
  41.  
  42. end
  43.  
  44.  
  45.  
  46. function mysplit(str, pat)
  47.  
  48.   local t = {} -- NOTE: use {n = 0} in Lua-5.0
  49.   local fpat = "(.-)" .. pat
  50.   local last_end = 1
  51.   local s, e, cap = str:find(fpat, 1)
  52.  
  53.   while s do
  54.  
  55.     if s ~= 1 or cap ~= "" then
  56.  
  57.       table.insert(t,cap)
  58.  
  59.     end
  60.  
  61.     last_end = e+1
  62.     s, e, cap = str:find(fpat, last_end)
  63.  
  64.   end
  65.  
  66.   if last_end <= #str then
  67.  
  68.     cap = str:sub(last_end)
  69.     table.insert(t, cap)
  70.  
  71.   end
  72.  
  73.   return t
  74.  
  75. end
  76.  
  77.  
  78.  
  79. function netevent(p1, p2, p3)
  80.  
  81.     hud.clear()
  82.     hud.sync()
  83.    
  84.     sender = p1
  85.     message = p2
  86.     protocol = p3
  87.     psplit = mysplit(protocol,"/")
  88.     protocol = psplit[1]
  89.     if not searchlist(protocol,knownprotocols) then return end
  90.     pid = psplit[2]
  91.  
  92.     if protocol=="handshake" then
  93.  
  94.         print("Received handshake request from client "..sender)
  95.         return
  96.  
  97.     end
  98.  
  99.     if protocol == "reactor" then
  100.  
  101.         reactor(message)
  102.  
  103.     end
  104.  
  105.     if protocol == "matrix" then
  106.  
  107.         matrix(message)
  108.  
  109.     end
  110.  
  111.     if protocol == "IDSU" then
  112.  
  113.         idsu(message)
  114.  
  115.     end
  116.  
  117.     if protocol == "ic2" then
  118.  
  119.         ic2(message)
  120.    
  121.     end
  122.  
  123. end
  124.  
  125.  
  126. function reactor(n)
  127.  
  128.     draw(5, (y-215), 35, (y-115), green, red)
  129.  
  130.     if n[7] < 100 then
  131.         draw(5, (y-215), 35, (y-115-n[7]), black, black)
  132.     end
  133.  
  134.     writings(("Energy Stored : " .. readablenumber(n[1]) .. "RF"), 40, (y-215))
  135.     writings(("Maximum Capacity : " .. readablenumber(n[2]) .. "RF"), 40, (y-205))
  136.     writings(("Energy Production : " .. readablenumber(n[3]) .. "RF/t"), 40, (y-195))
  137.     writings(("Casing Heat : " .. n[4] .. "°C"), 40, (y-185))
  138.     writings(("Fuel Consumption : " .. n[5] .. "mb/t"), 40, (y-175))
  139.     writings(("Rod Insertion Level : " .. n[6] .. "%"), 40, (y-165))
  140.  
  141.     hud.add()
  142.  
  143. end
  144.  
  145. function matrix(n)
  146.  
  147.     local percent = (n[2]/n[1])*100
  148.     draw(5, (y-105), 35, (y-5), green, red)
  149.    
  150.     if percent < 100 then
  151.         draw(5, (y-105), 35, (y-5-percent), black, black)
  152.     end
  153.  
  154.     local estor = n[2]
  155.     local emax = n[1]
  156.     local ein = n[3]
  157.     local eout = n[4]
  158.  
  159.     writings(("Energy Stored : " .. readablenumber(estor) .. "J"), 40, (y-105))
  160.     writings(("Maximum Capacity : " .. readablenumber(emax) .. "J"), 40, (y-95))
  161.     writings(("Energy Input : " .. readablenumber(ein) .. "J/t"), 40, (y-85))
  162.     writings(("Energy Output : " .. readablenumber(eout) .. "J/t"), 40, (y-75))
  163.  
  164.     hud.add()
  165.  
  166. end
  167.  
  168. function idsu(n)
  169.  
  170.     local percent = (n[2]/n[1])*100
  171.     draw(5, (y-325), 35, (y-225), green, red)
  172.    
  173.     if percent < 100 then
  174.         draw(5, (y-325), 35, (y-225-percent), black, black)
  175.     end
  176.  
  177.     local estor = n[2]
  178.     local emax = n[1]
  179.  
  180.     writings(("Energy Stored : " .. readablenumber(estor) .. "EU"), 40, (y-325))
  181.     writings(("Maximum Capacity : " .. readablenumber(emax) .. "EU"), 40, (y-315))
  182.  
  183.     hud.add()
  184.  
  185. end
  186.  
  187. function ic2(n)
  188.  
  189.     local percent = (n[2]/n[1])*100
  190.     draw(5, (y-325), 35, (y-225), green, red)
  191.    
  192.     if percent < 100 then
  193.         draw(5, (y-325), 35, (y-225-percent), black, black)
  194.     end
  195.  
  196.     local estor = n[2]
  197.     local emax = n[1]
  198.  
  199.     writings(("Energy Stored : " .. readablenumber(estor) .. "EU"), 40, (y-325))
  200.     writings(("Maximum Capacity : " .. readablenumber(emax) .. "EU"), 40, (y-315))
  201.  
  202.     hud.add()
  203.  
  204. end
  205.  
  206.  
  207. function draw(a, b, c, d, e, f)
  208.  
  209.     hud.drawRectangle(a, b, c, d, e, f)
  210.  
  211. end
  212.  
  213.  
  214.  
  215. function writings(txt, x, y)
  216.  
  217.     hud.drawString(txt, x, y)
  218.  
  219. end
  220.  
  221.  
  222.  
  223. function chatcommand(player, command)
  224.  
  225.     for i = 1, #ant.getPlayers() do
  226.    
  227.     if player == ant.getPlayers()[i] then
  228.  
  229.         if command[1] == "open" then
  230.  
  231.             guiConfig()
  232.  
  233.         elseif command[1] == "reboot" then
  234.  
  235.             rednet.broadcast(command, "shserver")
  236.             rednet.close(modemside)
  237.             os.reboot()
  238.  
  239.         elseif command[1] == "br" then
  240.  
  241.             if command[2] == nil then
  242.  
  243.                 chb.tell(players[1], "Correct Usage : br on/off")
  244.  
  245.             elseif command[2] == "on" then
  246.  
  247.                 rednet.broadcast(command, "shserver")
  248.  
  249.             elseif command[2] == "off" then
  250.  
  251.                 rednet.broadcast(command, "shserver")
  252.  
  253.             end
  254.  
  255.         elseif command[1] == "help" then
  256.             chb.tell(player, "List of commands : \nbr on/off - Turns ExtremeReactors on or off \nopen - Turns on your GUI \nreboot - Reboot the SH server and clients")
  257.         end
  258.  
  259.     end
  260.  
  261.     end
  262.  
  263. end
  264.  
  265.  
  266. function max(a,b)
  267.  
  268.     if a>b then
  269.         return a
  270.     else
  271.         return b
  272.     end
  273.  
  274. end
  275.  
  276.  
  277.  
  278. function eventlistener()
  279.    
  280.     local event, p1, p2, p3 = os.pullEvent()
  281.  
  282.     if event == "rednet_message" then netevent(p1, p2, p3)
  283.     elseif event == "command" then
  284.    
  285.         chatcommand(p1, p2)
  286.  
  287.     end
  288.  
  289. end
  290.  
  291.  
  292. -- Init
  293.  
  294. term.clear()
  295. term.setCursorPos(1,1)
  296. print("Skip_21's Smart Helmet "..version.." running !")
  297. if (os.getComputerLabel()==nil) then
  298.   print("Setting computer label to 'SHServer'.")
  299.   os.setComputerLabel("SHServer")
  300. end
  301.  
  302. sides = peripheral.getNames()
  303.  
  304. antennahere = false
  305. chatboxhere = false
  306. modemhere = false
  307.  
  308. for i = 1, #sides do
  309.  
  310.     thistype = peripheral.getType(sides[i])
  311.    
  312.     if thistype == "antenna" then
  313.    
  314.         print("Antenna found on " .. sides[i] .. ", connecting...")
  315.         ant = peripheral.wrap(sides[i])
  316.         antennahere = true
  317.  
  318.     end
  319.  
  320.     if thistype == "chatBox" then
  321.  
  322.         print("ChatBox found on " .. sides[i] .. ", connecting...")
  323.         chb = peripheral.wrap(sides[i])
  324.         chatboxhere = true
  325.  
  326.     end
  327.  
  328.     if thistype == "modem" then
  329.    
  330.         print("Modem found on " .. sides[i] .. ", connecting...")
  331.         rednet.open(sides[i])
  332.         modemside = sides[i]
  333.         modemhere = true
  334.  
  335.     end
  336.  
  337. end
  338.  
  339. if not antennahere then error("Please connect an antenna from PeripheralsPlusOne mod") end
  340. if not chatboxhere then error("Please connect a chatbox from PeripheralsPlusOne mod") end
  341. if not modemhere then error("Please connect a Wireless Modem") end
  342.  
  343. rednet.host("shserver","shserver")
  344. print("Running ...")
  345.  
  346. players = ant.getPlayers()
  347.  
  348. hud = ant.getHUD(players[1])
  349.  
  350. x,y = hud.getResolution()
  351.  
  352. gui = hud.getGUI(players[1])
  353.  
  354. red = hud.getColorFromRGB(255,0,0)
  355. green = hud.getColorFromRGB(0,255,0)
  356. black = hud.getColorFromRGB(0,0,0)
  357.  
  358. hud.clear()
  359.  
  360. -- main loop
  361.  
  362. while true do
  363.  
  364.     eventlistener()
  365.  
  366. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement