Advertisement
JamnedZ

Bad Apple!! in CC:Tweaked (updated)

Aug 6th, 2024
1,160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.68 KB | Source Code | 0 0
  1. --Bad Apple!! in Minecraft! Made by JamnedZ
  2. --You'll need at least 1.8 MB to run video only, and 3 MB for video and audio.
  3. --Use monitor 3x2 with scale 0.5! Or 6x4 with scale 1, I guess
  4. --GitHub repo: https://github.com/JamElyZEuS/badapple_cctweaked
  5.  
  6. local video = {}
  7. local no_video = false
  8. local no_audio = false
  9.  
  10. local function download(sUrl) --this code's from rom wget script
  11.     local ok, err = http.checkURL(sUrl)
  12.     if not ok then
  13.         printError(err or "Invalid URL.")
  14.         return
  15.     end
  16.  
  17.     local response = http.get(sUrl , nil , true)
  18.     if not response then
  19.         print("Failed.")
  20.         return nil
  21.     end
  22.  
  23.     local sResponse = response.readAll()
  24.     response.close()
  25.     return sResponse or ""
  26. end
  27.  
  28. local function check_files()
  29.     if not fs.exists('badapple.nfpa') then
  30.         print('Video not found! Downloading...')
  31.         local video_file = download('https://raw.githubusercontent.com/JamElyZEuS/badapple_cctweaked/main/badapple.nfpa')
  32.         if not video_file then
  33.             printError('Couldn\'t download video.')
  34.             no_video = true
  35.         else
  36.             local video_save, err = fs.open('badapple.nfpa', 'w')
  37.             if not video_save then
  38.                 printError('Couldn\'t save video file: ' .. err)
  39.                 no_video = true
  40.             else
  41.                 video_save.write(video_file)
  42.                 video_save.close()
  43.                 print('Video\'s downloaded successfully!')
  44.             end
  45.         end
  46.     end
  47.  
  48.     if not fs.exists('badapple.dfpwm') and not no_audio then
  49.         print('Music not found! Downloading...')
  50.         local audio_file = download('https://raw.githubusercontent.com/JamElyZEuS/badapple_cctweaked/main/badapple.dfpwm')
  51.         if not audio_file then
  52.             printError('Couldn\'t download audio.')
  53.             no_audio = true
  54.         else
  55.             local audio_save, err = fs.open('badapple.dfpwm', 'wb')
  56.             if not audio_save then
  57.                 printError('Couldn\'t save audio file: ' .. err)
  58.                 no_audio = true
  59.             else
  60.                 audio_save.write(audio_file)
  61.                 audio_save.close()
  62.                 print('Audio\'s downloaded successfully!')
  63.             end
  64.         end
  65.     end
  66.  
  67.  
  68. end
  69.  
  70. local function unpack()
  71.     print('Loading video...')
  72.  
  73.     local line_counter = 1
  74.     local frame_counter = 1
  75.     local current_frame = ''
  76.     for line in io.lines("badapple.nfpa") do
  77.         os.queueEvent('cmonnow')
  78.         if line_counter > 24 then
  79.             video[frame_counter] = current_frame
  80.             current_frame = ''
  81.             line_counter = 1
  82.             frame_counter = frame_counter + 1
  83.         end
  84.         if frame_counter > 2192 then break end
  85.  
  86.         current_frame = current_frame .. line .. '\n'
  87.         line_counter = line_counter + 1
  88.         os.pullEvent()
  89.     end
  90. end
  91.  
  92.  
  93.  
  94. local function aud()
  95.     local dfpwm = require "cc.audio.dfpwm"
  96.     local speaker = peripheral.find("speaker")
  97.  
  98.     local decoder = dfpwm.make_decoder()
  99.     for input in io.lines("badapple.dfpwm", 16 * 1024) do
  100.         local decoded = decoder(input)
  101.         while not speaker.playAudio(decoded) do
  102.             os.pullEvent("speaker_audio_empty")
  103.         end
  104.     end
  105. end
  106.  
  107. local function vid()
  108.     local myterm = term.current()
  109.     local buffer = window.create(myterm, 13, 1, 32, 24)
  110.     buffer.setVisible(false)
  111.  
  112.     local timeformat = os.time('local') < 20 and 'local' or 'utc'
  113.     local starttime = os.time(timeformat)
  114.  
  115.     local SEC = 0.000277777777
  116.  
  117.     for i = 1, 2192, 1 do
  118.         if (os.time(timeformat) - starttime) <= ((i - 1) * (SEC / 10)) then
  119.             term.redirect(buffer)
  120.  
  121.             term.clear()
  122.             local frame = paintutils.parseImage(video[i])
  123.             paintutils.drawImage(frame, 1, 1)
  124.  
  125.             term.redirect(myterm)
  126.             buffer.setVisible(true)
  127.             buffer.setVisible(false)
  128.  
  129.             sleep(1/10 + (((i - 1) * (SEC / 10)) - (os.time(timeformat) - starttime)))
  130.         end
  131.     end
  132. end
  133.  
  134.  
  135.  
  136. term.clear()
  137. if not peripheral.find('speaker') then no_audio = true end
  138. check_files()
  139. if not no_audio and not no_video then
  140.     print('Enjoy your full Bad Apple!! experience!')
  141.     unpack()
  142.     term.clear()
  143.     parallel.waitForAll(aud, vid)
  144. elseif no_audio and not no_video then
  145.     print('You will have no audio playback. Connect speakers and download all files for better experience!')
  146.     unpack()
  147.     term.clear()
  148.     vid()
  149. elseif no_video and not no_audio then
  150.     print('You have no video file... Well, at least you\'re able to listen to some classics. Enjoy!')
  151.     aud()
  152. else
  153.     print('Playing failed. Shutting down...')
  154.     return
  155. end
  156.  
  157. sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement