Advertisement
fremnet

Jukebox

Feb 24th, 2013
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.81 KB | None | 0 0
  1. -- Configure these
  2. discSide     = "left"
  3. radioSide    = "right"
  4.  
  5. -- Don't touch these
  6. discs = {}
  7. discs["C418 - 11"]      = {duration=75,  title="C418 - 11"}
  8. discs["C418 - 13"]      = {duration=178, title="C418 - 13"}
  9. discs["C418 - blocks"]  = {duration=355, title="C418 - Blocks"}
  10. discs["C418 - cat"]     = {duration=188, title="C418 - Cat"}
  11. discs["C418 - chirp"]   = {duration=186, title="C418 - Chirp"}
  12. discs["C418 - far"]     = {duration=172, title="C418 - Far"}
  13. discs["C418 - mall"]    = {duration=200, title="C418 - Mall"}
  14. discs["C418 - mellohi"] = {duration=99,  title="C418 - Mellohi"}
  15. discs["C418 - stal"]    = {duration=156, title="C418 - Stal"}
  16. discs["C418 - strad"]   = {duration=193, title="C418 - Strand"}
  17. discs["C418 - ward"]    = {duration=251, title="C418 - Ward"}
  18. discs["pg.radioloop"]   = {duration=25,  title="Valve - Radio Loop"}
  19. discs["pg.stillalive"]  = {duration=175, title="Valve - Still Alive"}
  20. discs["pg.wantyougone"] = {duration=135, title="Valve - Want You Gone"}
  21. discs["wait"]           = {duration=235, title="C418 - Wait"}
  22.  
  23. currentSlot  = 0
  24. computerId   = 0
  25. currentTimer = nil
  26. discInfo     = nil
  27. startTime    = 0
  28.  
  29. function incSlot(amount)
  30.     currentSlot = currentSlot + amount
  31.     if currentSlot == 17 then
  32.         currentSlot = 1
  33.     elseif currentSlot == 0 then
  34.         currentSlot = 16
  35.     end
  36.     return currentSlot
  37. end
  38.  
  39. function changeDisk(dir)
  40.     if dir == nil then
  41.         dir = 1
  42.     end
  43.     haveDisk = disk.isPresent(discSide)
  44.  
  45.     -- Turn to face the disk drive
  46.     if discSide == "left" then
  47.         turtle.turnLeft()
  48.     else
  49.         turtle.turnRight()
  50.     end
  51.  
  52.     if haveDisk then
  53.         turtle.suck()
  54.     end
  55.  
  56.     slot = incSlot(dir)
  57.     while turtle.getItemCount(slot) < 1 do
  58.         slot = incSlot(dir)
  59.     end
  60.  
  61.     turtle.select(currentSlot)
  62.     turtle.drop()
  63.  
  64.     -- Turn away from the disk drive
  65.     if discSize == "left" then
  66.         turtle.turnLeft()
  67.     else
  68.         turtle.turnRight()
  69.     end
  70. end
  71.  
  72. function stopPlayback(quiet)
  73.     if disk.isPresent(discSide) and disk.hasAudio(discSide) then
  74.         disk.stopAudio(discSide)
  75.         currentTimer = nil
  76.         startTime = 0
  77.         if not quiet then
  78.             if not(computerId == nil) then
  79.                 rednet.send(computerId, textutils.serialize({type="jukebox", action="stopped"}))
  80.             end
  81.             print("Stopped")
  82.         end
  83.     end
  84. end
  85.  
  86. function playCurrentDisc()
  87.     title = disk.getAudioTitle(discSide)
  88.     discInfo = discs[title]
  89.     if not discInfo then
  90.         print("No disc info found for " .. title)
  91.         changeDisk(1)
  92.     end
  93.     if computerId > 0 then
  94.         rednet.send(computerId, textutils.serialize({type="jukebox", action="playing", discInfo=discInfo, runTime=0}))
  95.     end
  96.     print("Playing " .. discInfo["title"] .. " for " .. discInfo["duration"] .. " seconds")
  97.     disk.playAudio(discSide)
  98.     startTime = os.clock()
  99.     currentTimer = os.startTimer(discInfo["duration"])
  100. end
  101.  
  102. function startPlayback()
  103.     if disk.isPresent(discSide) then
  104.         if disk.hasAudio(discSide) then
  105.             playCurrentDisc()
  106.         end
  107.     else
  108.         changeDisk(1)
  109.         playCurrentDisc()
  110.     end
  111. end
  112.  
  113. if peripheral.isPresent(radioSide) and peripheral.getType(radioSide) == "modem" then
  114.     rednet.open(radioSide)
  115.     rednet.broadcast(textutils.serialize({action="announce", type="jukebox", device="jukebox"}))
  116. end
  117.  
  118. while true do
  119.     ev,p1,p2,p3 = os.pullEvent()
  120.     if ev == "rednet_message" then
  121.         data = textutils.unserialize(p2)
  122.         if data["type"] == "jukebox" then
  123.             if data["action"] == "acknowledge" or (data["action"] == "announce" and data["device"] == "monitor") then
  124.                 print("Found a monitor!")
  125.                 computerId = p1
  126.                 if not(data["action"] == "acknowledge") then
  127.                     rednet.send(computerId, textutils.serialize({type="jukebox", action="acknowledge"}))
  128.                 end
  129.  
  130.                 if currentTimer == nill then
  131.                     rednet.send(computerId, textutils.serialize({type="jukebox", action="stopped"}))
  132.                 else
  133.                     runTime = os.clock() - startTime
  134.                     rednet.send(computerId, textutils.serialize({type="jukebox", action="playing", discInfo=discInfo, runTime=runTime}))
  135.                 end
  136.             elseif data["action"] == "stop" then
  137.                 stopPlayback()
  138.             elseif data["action"] == "play" then
  139.                 startPlayback()
  140.             elseif data["action"] == "next" then
  141.                 stopPlayback(true)
  142.                 changeDisk(1)
  143.                 playCurrentDisc()
  144.             elseif data["action"] == "prev" then
  145.                 stopPlayback(true)
  146.                 changeDisk(-1)
  147.                 playCurrentDisc()
  148.             end
  149.         end
  150.     elseif ev == "timer" and currentTimer == p1 then
  151.         if disk.isPresent(discSide) and disk.hasAudio(discSide) then
  152.             stopPlayback(true)
  153.         end
  154.         changeDisk(1)
  155.         playCurrentDisc()
  156.     elseif ev == "char" then
  157.         if p1 == 'p' then
  158.             if currentTimer == nil then
  159.                 startPlayback()
  160.             else
  161.                 print("Already playing")
  162.             end
  163.         elseif p1 == 'f' then
  164.             stopPlayback(true)
  165.             changeDisk(1)
  166.             playCurrentDisc()
  167.         elseif p1 == 'b' then
  168.             stopPlayback(true)
  169.             changeDisk(-1)
  170.             playCurrentDisc()
  171.         elseif p1 == 's' then
  172.             stopPlayback()
  173.         else
  174.             print("[P]lay, [F]orward, [B]ack, [S]top")
  175.         end
  176.     end
  177. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement