Advertisement
neonerz

rmpv2-turtle-dj

Feb 23rd, 2014
2,409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.37 KB | None | 0 0
  1. -- RAC Media Player v2.0 by neonerZ (turtle-dj)
  2. -- More Info: https://www.youtube.com/watch?v=tfSSdY0jQQk
  3.  
  4. --set variables
  5. chest="top" -- chest position
  6. drive="bottom" -- drive position
  7. modem="right" -- modem position
  8. radio="front" -- **YOU CAN NOT CHANGE THE RADIO POSITION IT MUST BE IN FRONT**
  9. radioCrystal="15254" -- item ID of radio crystal
  10. monitor = 3 -- ID of computer connected to monitor
  11.  
  12. -- Feel free to do whatever you want with this.
  13. -- Edit it, incorporate it into your own project, eat it, I don't care.
  14. -- I only ask if you redistribute this or edit it/incorporate it into
  15. -- your own project you give me a shout out (In the case of including
  16. -- this into something you release. Send me a message, I'd love to
  17. -- check it out). This is only a friendly suggestion, you are
  18. -- welcome to be a jerk and claim this as your own
  19.  
  20. -- don't edit below here
  21. c = peripheral.wrap(chest)
  22. r = peripheral.wrap(radio)
  23. rednet.open(modem)
  24.  
  25.  
  26. function scanTracks()
  27.     data={}
  28.     local slot
  29.     local name
  30.     local slots = c.getAllStacks()
  31.     for i,j in pairs(slots) do
  32.         slot = i
  33.         name = j["name"]
  34.         id = j["id"]
  35.         data[name]=slot..":"..id
  36.     end
  37.    
  38. end
  39.  
  40. function scanRadio()
  41.     data={}
  42.     local slot
  43.     local name
  44.     if peripheral.isPresent(radio) then
  45.         local slots = r.getAllStacks()
  46.         for i,j in pairs(slots) do
  47.             slot = i
  48.             if slot== null then
  49.                 return false
  50.             else
  51.                 return true
  52.             end
  53.         end
  54.     else
  55.         return false
  56.     end
  57.    
  58. end
  59.  
  60. function currentlyPlaying()
  61.     if disk.isPresent(drive) then
  62.         return disk.getAudioTitle(drive)
  63.     elseif scanRadio() then
  64.         local slots = r.getAllStacks()
  65.         for i,j in pairs(slots) do
  66.             slot = i
  67.             name = j["name"]
  68.             id = j["id"]
  69.         end
  70.         return name
  71.     else
  72.         return "none"
  73.     end
  74. end
  75.  
  76. function refreshTracks(id)
  77.     scanTracks()
  78. --  for k,v in pairs(data) do print(k..":"..v) end
  79.     data=textutils.serialize(data)
  80.     rednet.send(id,data)
  81. end
  82.  
  83. function playTrack(name,id)
  84.     local slot
  85.     local item
  86.     local track
  87.     stopPlayback(1)
  88.     stopRadio(1)
  89.     scanTracks()
  90.     track=data[name]
  91.     for k,v in string.gmatch(track, "(%w+):(.+)") do
  92.         slot=k
  93.         item=v
  94.     end
  95.    
  96.     if item==radioCrystal then
  97.         if chest=="bottom" then
  98.             c.pushItem("up", slot, 1,1)
  99.         else
  100.             c.pushItem("down", slot, 1,1)
  101.         end
  102.         turtle.drop()
  103.         redstone.setAnalogOutput(radio,7)
  104.         rednet.send(id,"SUCCESS")
  105.     else
  106.         if chest=="bottom" then
  107.             c.pushItem("up", slot, 1,1)
  108.             turtle.dropUp()
  109.         else
  110.             c.pushItem("down", slot, 1,1)
  111.             turtle.dropDown()
  112.         end
  113.         if disk.hasAudio(drive) then
  114.             disk.playAudio(drive)
  115.             rednet.send(id,"SUCCESS")
  116.         else
  117.             printError("NO DISC IN DRIVE")
  118.             rednet.send(id,"FAILURE")
  119.             stopTrack()
  120.         end
  121.     end
  122. end
  123.  
  124. function play(id)
  125.     if disk.isPresent(drive) then
  126.         disk.playAudio(drive)
  127.         rednet.send(id,"SUCCESS")
  128.     elseif scanRadio() then
  129.         redstone.setAnalogOutput(radio,7)
  130.         rednet.send(id,"SUCCESS")
  131.     else
  132.         printError("NOTHING TO PLAY")
  133.         rednet.send(id,"FAILURE")
  134.     end
  135. end
  136.  
  137. function stopTrack(eject)
  138.     if disk.isPresent(drive) and eject == 1 then
  139.         if drive=="top" then
  140.             turtle.suckUp()
  141.             for i = 1,16 do
  142.                 turtle.select(i)
  143.                 turtle.dropDown()
  144.             end
  145.             turtle.select(1)
  146.         else
  147.             turtle.suckDown()
  148.             for i = 1,16 do
  149.                 turtle.select(i)
  150.                 turtle.dropUp()
  151.             end
  152.             turtle.select(1)
  153.         end
  154.     else
  155.         disk.stopAudio(drive)
  156.         return
  157.     end
  158. end
  159.  
  160. function stopRadio(eject)
  161.     if scanRadio() and eject == 1 then
  162.         turtle.suck()
  163.         if chest=="bottom" then
  164.             for i = 1,16 do
  165.                 turtle.select(i)
  166.                 turtle.dropDown()
  167.             end
  168.         else
  169.             for i = 1,16 do
  170.                 turtle.select(i)
  171.                 turtle.dropUp()
  172.             end
  173.             turtle.select(1)
  174.         end
  175.     else
  176.         redstone.setAnalogOutput(radio,0)
  177.         return
  178.     end
  179. end
  180.  
  181. function stopPlayback(eject)
  182.     --this stop is for the play function to use
  183.     --does not send a redstone message confirming stop
  184.     stopRadio()
  185.     stopTrack(eject)
  186. end
  187.  
  188. function stop(id,eject)
  189.     --this stop will send back a confirmation that
  190.     --the stop was successful
  191.     stopRadio(eject)
  192.     stopTrack(eject)
  193.     rednet.send(id,"SUCCESS")
  194. end
  195.  
  196. function radioVolume(volume,id)
  197.     if redstone.getAnalogOutput(radio) > 0 and volume ==1 and redstone.getAnalogOutput(radio) < 15 then
  198.         redstone.setAnalogOutput(radio,redstone.getAnalogOutput(radio)+1)
  199.         rednet.send(id,"SUCCESS")
  200.     end
  201.    
  202.     if redstone.getAnalogOutput(radio) > 0 and volume ==0 and redstone.getAnalogOutput(radio) > 1 then
  203.         redstone.setAnalogOutput(radio,redstone.getAnalogOutput(radio)-1)
  204.         rednet.send(id,"SUCCESS")
  205.     end
  206. end
  207.  
  208. local tArgs = {...}
  209.  
  210. print("Starting up Turtle DJ")
  211. while true do
  212.     local id, msg, dis = rednet.receive()
  213.     turtle.select(1)
  214.     local command
  215.     local info
  216.     for k,v in string.gmatch(msg, "(%w+):(.+)") do
  217.         command=k
  218.         info=v
  219.     end
  220.     print(command," : ",info)
  221.     if command == "playt" and id==monitor then
  222.         playTrack(info,monitor)
  223.     end
  224.     if command == "play" and id==monitor then
  225.         play(monitor)
  226.     end
  227.     if command == "stopeject" and id==monitor then
  228.         stop(monitor,1)
  229.     end
  230.     if command == "stop" and id==monitor then
  231.         stop(monitor,0)
  232.     end
  233.     if command == "volume" and id==monitor then
  234.         radioVolume(tonumber(info),monitor)
  235.         print("cur:"..redstone.getAnalogOutput(radio))
  236.     end
  237.     if command == "scan" and id==monitor then
  238.         refreshTracks(monitor)
  239.     end
  240.     if command == "alive" and id==monitor then
  241.         rednet.send(monitor,"none")
  242.     end
  243.     if command == "track" and id==monitor then
  244.         rednet.send(monitor,currentlyPlaying())
  245.     end
  246. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement