Advertisement
GravityCube

Radio

Mar 23rd, 2018
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.28 KB | None | 0 0
  1. xo = -2501
  2. yo= 64
  3. zo = 2637
  4.  
  5. os.loadAPI("gcapi")
  6.  
  7. sensor = peripheral.find("openperipheral_sensor")
  8. --------------------------------------------
  9. -->                Sensor                <--
  10. --------------------------------------------
  11. function updateTableWithSensor(dataPlayers)
  12.     --{"name", "x", "y", "z", "world", "updated_at"}
  13.     for i=1,#dataPlayers do
  14.         player_data = dataPlayers[i]
  15.         player_name = player_data["name"]
  16.         sensor_data = sensor.getPlayerData(player_name)
  17.         if sensor_data then
  18.             dataPlayers[i].x = math.floor(xo + sensor_data.position.x)
  19.             dataPlayers[i].y = math.floor(yo + sensor_data.position.y)
  20.             dataPlayers[i].z = math.floor(zo + sensor_data.position.z)
  21.            
  22.             dataPlayers[i].world = "world"
  23.             dataPlayers[i].updated_at = "Updated now"
  24.         end
  25.     end
  26.     return dataPlayers
  27. end
  28. --------------------------------------------------------
  29. -->                       MUSIC                      <--
  30. --------------------------------------------------------
  31. local pitchs = {[0]=0, [1]=0.53, [2]=0.56, [3]=0.6, [4]=0.63, [5]=0.67, [6]=0.7, [7]=0.75,
  32. [8]=0.8,[9]=0.85,[10]=0.9,[11]=0.95,[12]=1,[13]=1.05,[14]=1.1,[15]=1.2,[16]=1.25,[17]=1.32,[18]=1.4,
  33. [19]=1.5,[20]=1.6,[21]=1.7,[22]=1.8,[23]=1.9,[24]=2}
  34.  
  35. local instruments = {[1]="note.harp",[2]="note.bassattack",[3]="note.bd",[4]="note.snare",[5]="note.hat"}
  36.  
  37. local playerPositions = {}
  38. local playersInRadio = {}
  39.  
  40. local vol = 0.7
  41.  
  42. local song = nil
  43.  
  44. local noteblock = peripheral.find("music")
  45.  
  46. function playNote(instrument, pitch)
  47.     for _,playerData in pairs(playerPositions) do
  48.         --if playersInRadio[playerData["name"]] then
  49.             local x = playerData["x"]
  50.             local y = playerData["y"]
  51.             local z = playerData["z"]
  52.             noteblock.playSound(instrument, pitch, vol, x-xo, y-yo+4, z-zo)
  53.         --end
  54.     end
  55. end
  56.  
  57. function getPlayersInRadio()
  58.     local players = {}
  59.     for i, playerData in pairs(playerPositions) do
  60.         local player = playerData["name"]
  61.         if playersInRadio[player] then
  62.             table.insert(players, playerData)
  63.         end
  64.     end
  65.     return players
  66. end
  67.  
  68. function removePlayerFromRadio(player)
  69.     playersInRadio[player] = nil   
  70. end
  71.  
  72. function addPlayerToRadio(player)
  73.     playersInRadio[player] = true
  74. end
  75.  
  76. function addAllPlayers()
  77.     for i, playerData in pairs(playerPositions) do
  78.         local player = playerData["name"]
  79.         playersInRadio[player] = true
  80.     end
  81. end
  82.  
  83. --------------------------------------------------------
  84. -->                     Commands                     <--
  85. --------------------------------------------------------
  86. function hasPermissions(player)
  87.     return gcapi.hasPermissions(player)
  88. end
  89. function executeCommand(player, co, isFromChat)
  90.    
  91.     local co = (co .. "   ")
  92.    
  93.     if isFromChat then
  94.         co = co:sub(2)
  95.     end
  96.    
  97.     local command = gcapi.split(co .. " ", " ")
  98.    
  99.    
  100.     if not command[1] then
  101.         return nil
  102.     end
  103.    
  104.     if command[1] == "music" and hasPermissions(player) then
  105.         if command[2] then
  106.             filePath = "/songs/" .. command[2]
  107.             if fs.exists(filePath) then
  108.                 file = fs.open(filePath, "r")
  109.                 song = textutils.unserialize(file.readAll())
  110.                 file.close()
  111.                 print("Playing " .. command[2] .. "...")
  112.             else
  113.                 print("Song not found!")
  114.             end
  115.         end
  116.         return
  117.     end
  118.    
  119.     if string.lower(command[1]) == "stopmusic" and hasPermissions(player) then
  120.         endSong = true
  121.         return
  122.     end
  123.    
  124.     if string.lower(command[1]) == "addall" and hasPermissions(player) then
  125.         addAllPlayers()
  126.         print("Players added!")
  127.         return
  128.     end
  129.    
  130.     if string.lower(command[1]) == "joinmusic" then
  131.         addPlayerToRadio(player)
  132.         return
  133.     end
  134.    
  135.     if string.lower(command[1]) == "mutemusic" then
  136.         removePlayerFromRadio(player)  
  137.         return
  138.     end
  139.    
  140.     if string.lower(command[1]) == "download" and hasPermissions(player) then
  141.         if command[2] and command[3] then
  142.             local name = "/songs/" .. command[3]
  143.             local code = command[2]
  144.            
  145.             fs.delete(name)
  146.             shell.run("pastebin get " .. code .. " "  .. name)
  147.         end
  148.         return
  149.     end
  150.    
  151.     if string.lower(command[1]) == "addplayer" and command[2] and hasPermissions(player) then
  152.         addPlayerToRadio(command[2])   
  153.         return
  154.     end
  155.    
  156.     if string.lower(command[1]) == "vol" and command[2] and hasPermissions(player) then
  157.         local newvol = tonumber(command[2])
  158.         if newvol and newvol <= 1 and newvol >= 0 then
  159.             vol = newvol
  160.         end
  161.         return
  162.     end
  163.    
  164.     if string.lower(command[1]) == "update" and hasPermissions(player) then
  165.         error()
  166.         return
  167.     end
  168.    
  169.     if not hasPermissions(player) then
  170.         return
  171.     end
  172.    
  173.     local instrument = command[1]
  174.    
  175.     local pitch = 1
  176.     if tonumber(command[2]) then
  177.         pitch = tonumber(command[2])
  178.     end
  179.    
  180.     local vol = 1
  181.     if tonumber(command[3]) then
  182.         vol = tonumber(command[3])
  183.     end
  184.    
  185.     for _,playerData in pairs(playerPositions) do
  186.         local x = playerData["x"]
  187.         local y = playerData["y"]
  188.         local z = playerData["z"]
  189.         noteblock.playSound(instrument, pitch, vol, x-xo, y-yo+4, z-zo)
  190.     end
  191.    
  192.    
  193. end
  194.  
  195.  
  196. --------------------------------------------------------
  197. -->                  Main programs                   <--
  198. --------------------------------------------------------
  199. function updaters()
  200.     parallel.waitForAny(updatePositions, listener)
  201. end
  202.  
  203. function listener()
  204.     sleep(2)
  205.     while true do
  206.         tEvent = {os.pullEvent()}  
  207.         if tEvent[1] == "chatEvent" then
  208.             local player = tEvent[2]
  209.             local co = tEvent[3]
  210.             executeCommand(player, co, true)
  211.             print(player .. ": " .. co)
  212.         end
  213.        
  214.         if tEvent[1] == "chat_command" then
  215.             local co = tostring(tEvent[2])
  216.             local player = tostring(tEvent[3])
  217.             executeCommand(player, co)
  218.             print(player .. ": " .. co)
  219.         end
  220.     end
  221. end
  222.  
  223. function updatePositions()
  224.     while true do
  225.         playerPositions = gcapi.getPosPlayers()
  226.         if sensor then
  227.             playerPositions = updateTableWithSensor(playerPositions)
  228.         end
  229.         sleep(1)
  230.     end
  231. end
  232.  
  233. function updateSong()
  234.     while true do
  235.         if song then
  236.             delay = song["delay"]
  237.            
  238.             for _,tick in ipairs(song) do
  239.                 local players = getPlayersInRadio()
  240.                 for instrumentID, notePitchs in pairs(tick) do
  241.                     for _,pitchID in pairs(notePitchs) do
  242.                         playNote(instruments[instrumentID], pitchs[pitchID])
  243.                     end
  244.                 end
  245.                 if endSong then
  246.                     break
  247.                 end
  248.                 sleep(delay)
  249.             end
  250.            
  251.             song = nil
  252.         end
  253.        
  254.         endSong = false
  255.         sleep(2)
  256.     end
  257. end
  258.  
  259. function startProgram()
  260.     parallel.waitForAny(updaters, updateSong)
  261. end
  262. parallel.waitForAny(gcapi.startChatEventQueue, startProgram)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement