Advertisement
AkaZombie

DiskReceiver

Feb 14th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.00 KB | None | 0 0
  1. -- DiskReceiver
  2.  
  3. -- function ports from infutil / snet:
  4. do
  5.     local side
  6.     local sides={"right","left","front","back","top","bottom"}
  7.     for l1=1,6 do
  8.         if peripheral.getType(sides[l1])=="modem" then rednet.open(sides[l1]) side=sides[l1] end
  9.     end
  10.     if not side then
  11.         error("No modem attached.")
  12.     end
  13. end
  14. local function compress( ... ) -- serializes all of its arguments into a table and catches errors
  15.     local tArgs={ ... }
  16.     local E,D=pcall(textutils.serialize,tArgs)
  17.     if E then
  18.         return D
  19.     else
  20.         return false
  21.     end
  22. end
  23. function decompress(S) -- convert a serialized table into a normal one and catches errors
  24.     local E,D,C=pcall(textutils.unserialize,S)
  25.     if C or not E then
  26.         return false
  27.     else
  28.         return type(D)=="table" and D
  29.     end
  30. end
  31. -- end ports
  32. if peripheral.call("front","hasAudio") then
  33.     turtle.suck()
  34. end
  35. local slots={}
  36. local disks={}
  37. print("indexing...")
  38. for l1=1,16 do
  39.     turtle.select(l1)
  40.     turtle.drop()
  41.     if peripheral.call("front","hasAudio") then
  42.         slots[peripheral.call("front","getAudioTitle")]=l1
  43.         table.insert(disks,peripheral.call("front","getAudioTitle"))
  44.     end
  45.     turtle.suck()
  46. end
  47. local tDisks={}
  48. for k,v in pairs(disks) do
  49.     tDisks[v]=true
  50. end
  51. print("done!")
  52. print("Hosting: "..os.getComputerID())
  53. rednet.broadcast(compress("gotdisks",disks))
  54. print(table.concat(disks,","))
  55. while true do
  56.     local a,b,c
  57.     while not c do
  58.         a,b=rednet.receive()
  59.         c=decompress(b)
  60.     end
  61.     if c[1]=="diskplay" then
  62.         if tDisks[c[2]] then
  63.             if peripheral.call("front","hasAudio") then
  64.                 turtle.select(slots[peripheral.call("front","getAudioTitle")])
  65.                 turtle.suck()
  66.             end
  67.             if slots[c[2]] then
  68.                 turtle.select(slots[c[2]])
  69.                 turtle.drop()
  70.                 peripheral.call("front","playAudio")
  71.             end
  72.         end
  73.     elseif c[1]=="reqdisks" then
  74.         rednet.send(a,compress("gotdisks",disks))
  75.         print("sending list to "..a)
  76.     elseif c[1]=="diskstop" then
  77.         if peripheral.call("front","hasAudio") then
  78.             turtle.select(slots[peripheral.call("front","getAudioTitle")])
  79.             turtle.suck()
  80.         end
  81.     end
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement