Advertisement
sanovskiy

PlayerDetect

Sep 25th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.31 KB | None | 0 0
  1. lenQuant=0.1
  2. fileName="nyan.mus"
  3.  
  4. os.loadAPI("ocs/apis/sensor")
  5. proximity = sensor.wrap("right")
  6.  
  7. function isPlayerPresent()
  8.     local targets = proximity.getTargets()
  9.     for k,v in pairs(targets) do
  10.         if v ~= nil then
  11.             return true
  12.         else
  13.             return false
  14.         end
  15.     end
  16. end
  17.  
  18. function strsplit(str)
  19.     local arr = {}
  20.     for i in string.gmatch(str, "%S+") do
  21.       table.insert(arr,i)
  22.     end
  23.     return arr
  24. end
  25.  
  26. note = peripheral.wrap("top")
  27.  
  28. instrument=0
  29. R = {
  30.     A=1,Ab=0,Ad=2,
  31.     B=2,Bb=1,Bd=3,
  32.     H=3,Hb=2,Hd=4,
  33.     C=4,Cb=3,Cd=5,
  34.     D=6,Db=5,Dd=7,
  35.     E=8,Eb=7,Ed=9,
  36.     F=9,Fb=9,Fd=10,
  37.     G=11,Gb=10,G1d=12,
  38.     A1=13,A1b=12,A1d=14,
  39.     B1=14,B1b=13,B1d=15,
  40.     H1=15,H1b=14,H1d=16,
  41.     C1=16,C1b=15,C1d=17,
  42.     D1=18,D1b=17,D1d=19,
  43.     E1=20,E1b=19,E1d=21,
  44.     F1=21,F1b=20,F1d=22,
  45.     G1=23,G1b=22,G1d=24,
  46. }
  47.  
  48. melody = {}
  49. file = io.open(fileName, "r")
  50. while true do
  51.     tmp = file:read("*l")
  52.     if tmp == nil then
  53.         break
  54.     end
  55.     if not(tmp:sub(1,2)=="--") then
  56.         n_ = strsplit(tmp);
  57.         if n_[2]==nil then
  58.             n_[2]=0.1
  59.         end
  60.         table.insert(melody,n_)
  61.     end
  62. end
  63.  
  64. function main()
  65.  
  66.     while true do
  67.         for i=1,table.getn(melody),1 do
  68.             n = melody[i]
  69.             if not(n[1]=="p") and not(n[1]=="P") and isPlayerPresent() then
  70.                 note.playNote(instrument,R[n[1]])
  71.             end
  72.             sleep(tonumber(n[2])*lenQuant)
  73.         end
  74.         sleep(0.001)
  75.     end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement