Left4Cake2

NoteBlockHero

Feb 20th, 2013
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.45 KB | None | 0 0
  1. --get arguments
  2.  
  3. local tArgs = { ... }
  4. musicplaylist = false
  5. selectedsong = nil
  6. displaynotes = ""
  7. mute = {{ },{ },{ },{ },{ }}
  8. nodes = { }
  9. i = 1
  10. j = 1
  11. selectedi = 1
  12. selectedj = 1
  13. ptoggle = 0
  14. play = 1
  15. debug = false
  16. playerA = 1
  17. score = 0
  18. --attaching ironblocks
  19. ironblock1 = peripheral.wrap("left")
  20. ironblock2 = peripheral.wrap("right")
  21. ironblock3 = peripheral.wrap("back")
  22. ironblock4 = peripheral.wrap("top")
  23. ironblock5 = peripheral.wrap("bottom")
  24.  
  25. function round(num, idp)
  26.   local mult = 10^(idp or 0)
  27.   return math.floor(num * mult + 0.5) / mult
  28. end
  29.  
  30. input = function(evt, arg1, arg2)
  31.     if evt == "key" and arg1 == 2 then
  32.             if line3 == "|O| |-| | |" then
  33.             line3 = "|X| |-| | |"
  34.         elseif line4 == "|O| |-| | |" then
  35.             line4 = "|X| |-| | |"
  36.         elseif line2 == "|O| |-| | |" then
  37.             line2 = "|X| |-| | |"
  38.         else
  39.             score = score - 1
  40.         end
  41.     end
  42.     if evt == "key" and arg1 == 3 then
  43.             if line3 == "| |O|-| | |" then
  44.             line3 = "| |X|-| | |"
  45.         elseif line4 == "| |O|-| | |" then
  46.             line4 = "| |X|-| | |"
  47.         elseif line2 == "| |O|-| | |" then
  48.             line2 = "| |X|-| | |"
  49.         else
  50.             score = score - 1
  51.         end
  52.     end
  53.     if evt == "key" and arg1 == 4 then
  54.             if line3 == "| | |O| | |" then
  55.             line3 = "| | |X| | |"
  56.         elseif line4 == "| | |O| | |" then
  57.             line4 = "| | |X| | |"
  58.         elseif line2 == "| | |O| | |" then
  59.             line2 = "| | |X| | |"
  60.         else
  61.             score = score - 1
  62.         end
  63.     end
  64.     if evt == "key" and arg1 == 5 then
  65.             if line3 == "| | |-|O| |" then
  66.             line3 = "| | |-|X| |"
  67.         elseif line4 == "| | |-|O| |" then
  68.             line4 = "| | |-|X| |"
  69.         elseif line2 == "| | |-|O| |" then
  70.             line2 = "| | |-|X| |"
  71.         else
  72.             score = score - 1
  73.         end
  74.     end
  75.     if evt == "key" and arg1 == 6 then
  76.             if line3 == "| | |-| |O|" then
  77.             line3 = "| | |-| |X|"
  78.         elseif line4 == "| | |-| |O|" then
  79.             line4 = "| | |-| |X|"
  80.         elseif line2 == "| | |-| |O|" then
  81.             line2 = "| | |-| |X|"
  82.         else
  83.             score = score - 1
  84.         end
  85.     end
  86.    
  87. end
  88.  
  89. keepScore = function()
  90.     if line1 == "|O| |-| | |" or line1 == "| |O|-| | |" or line1 == "| | |O| | |" or line1 == "| | |-|O| |" or line1 == "| | |-| |O|" then
  91.         score = score - 3
  92.     elseif line1 == "|X| |-| | |" or line1 == "| |X|-| | |" or line1 == "| | |X| | |" or line1 == "| | |-|X| |" or line1 == "| | |-| |X|" then
  93.         score = score + 5
  94.     end
  95. end
  96.  
  97. playnotes = function(noteblockside, channle, int)
  98. if song[i][channle] then
  99. if song[i][channle][1] and mute[channle][1] ~= 0 then
  100. note = song[i][channle][1]
  101. noteblockside.playNote(int,note)
  102. end
  103. if song[i][channle][2] and mute[channle][2] ~= 0 then
  104. note = song[i][channle][2]
  105. noteblockside.playNote(int,note)
  106. end
  107. if song[i][channle][3] and mute[channle][3] ~= 0 then
  108. note = song[i][channle][3]
  109. noteblockside.playNote(int,note)
  110. end
  111. if song[i][channle][4] and mute[channle][4] ~= 0 then
  112. note = song[i][channle][4]
  113. noteblockside.playNote(int,note)
  114. end
  115. if song[i][channle][5] and mute[channle][5] ~= 0 then
  116. note = song[i][channle][5]
  117. noteblockside.playNote(int,note)
  118. end
  119. end
  120. end
  121.  
  122. -- Song selection stuff
  123. if fs.exists("playlist") and #tArgs < 1 then
  124.   local file = fs.open("playlist", "r")
  125.    playlist = {}
  126.                    toltal = 0
  127.                   while true do
  128.                  local songque = file.readLine()
  129.                  if songque == nil then --if reached end of file
  130.    break end
  131.                    table.insert(playlist, songque)
  132.                    toltal = toltal + 1
  133.                    musicplaylist = true
  134.   end
  135.   selectedsong = playlist[1]
  136. elseif #tArgs < 1 then
  137.    shell.run("clear")
  138.    print( "Use the arrows and enter to chose.  " )
  139.    songlist = fs.list("Music")
  140.    i = 0
  141.    max = -1
  142.    while true do
  143.            i = i + 1
  144.            max = max + 1
  145.                    if songlist[i] == nil then break end
  146.    end
  147.                    pointer = 1
  148.                    term.setCursorPos(1, 10)
  149.   term.clearLine(none)
  150.                    print( "->" .. pointer .. ": " .. songlist[pointer])
  151.           while true do
  152.          
  153.                                         list = 1
  154.                                         while list < 9 do
  155.                                          term.setCursorPos(1, 10 - list)
  156.                                          term.clearLine(none)
  157.                                          if songlist[pointer - (list)] then
  158.                                          write( "  " .. pointer - (list) .. ": " .. songlist[pointer - (list)])  
  159.                                          end
  160.                                          list = list + 1
  161.                                         end                      
  162.                                         term.setCursorPos(1, 10)
  163.                                         term.clearLine(none)
  164.                                         print( "->" .. pointer .. ": " .. songlist[pointer])
  165. list = 1
  166. while list < 10 do
  167.                                          term.setCursorPos(1, 10 + list)
  168.                                          term.clearLine(none)
  169.                                          if songlist[pointer + (list)] then
  170.                                          write( "  " .. pointer + (list) .. ": " .. songlist[pointer + (list)])  
  171.                                          end
  172.                                          list = list + 1
  173.                                         end
  174. local evt, arg1, arg2 = os.pullEvent()
  175.                                         if arg1 == 28 then break end
  176.                                         if arg1 == 200 and pointer > 1 then
  177.                                                         pointer = pointer - 1
  178.                                         elseif arg1 == 208 and pointer < max then
  179.                                                         pointer = pointer + 1
  180.                                         end
  181.                                        
  182.          end
  183.          
  184. selectedsong = songlist[pointer]
  185. else
  186. selectedsong = tArgs[1]
  187. end
  188.  
  189. shell.run("clear")
  190. while true do
  191.     print("What part? 1-5")
  192.     local evt, arg1, arg2 = os.pullEvent()
  193.     if arg1 >= 2 and arg1 <= 6 then
  194.         playerA = arg1 - 1
  195.         break
  196.     end
  197. end
  198. shell.run("clear")
  199. que = 0
  200. while true do
  201. if musicplaylist then
  202. if que >= toltal then
  203. que = 1
  204. else
  205. que = que + 1
  206. end
  207. selectedsong = playlist[que]
  208. end
  209.  
  210. -- Playing The Music
  211.  
  212. --opening file
  213. shell.run( "clear" )
  214. if fs.exists("Instrument/" .. selectedsong) then
  215. shell.run( "Instrument/" .. selectedsong )
  216. else
  217. int1 = 0
  218. int2 = 0
  219. int3 = 0
  220. int4 = 0
  221. int5 = 0
  222. end
  223. file = io.open( "Music/" .. selectedsong )
  224. filedata = file:read()
  225. file:close()
  226. --converting file to table
  227. song = textutils.unserialize(filedata)
  228. allNotes = song["lenght"] / song["delay"]
  229. -- Song info
  230. term.setCursorPos(1,1)
  231. print("Song:" .. song["name"] )
  232. print("Author:" .. song["author"] )
  233. print("Original Author:" .. song["original_author"] )
  234. -- Start playing
  235. i = 0
  236. line1 = ""
  237. line2 = ""
  238. line3 = ""
  239. line4 = ""
  240. line5 = ""
  241. line6 = ""
  242. line7 = ""
  243. line8 = ""
  244. line9 = ""
  245. line10 = ""
  246. while i * song["delay"] < song["lenght"] + 1 and i * song["delay"] > -1 do
  247. term.setCursorPos(1,4)
  248. term.clearLine()
  249. print(i .. "/" .. allNotes .. " Notes" )
  250. term.clearLine()
  251. print("Score:" .. score)
  252. --Note Path
  253. nodes[1] = " "
  254. nodes[2] = " "
  255. nodes[3] = "-"
  256. nodes[4] = " "
  257. nodes[5] = " "
  258. n = 1
  259. rangeS = 1
  260. rangeE = 5
  261. while n <= 5 do
  262. rangeS = rangeS + 5
  263. rangeE = rangeE + 5
  264. if song[i + 8] then
  265.     if song[i + 8][playerA] then
  266.         if song[i + 8][playerA][1] then
  267.             if song[i + 8][playerA][1] >= rangeS and song[i + 8][playerA][1] <= rangeE then
  268.                 nodes[n] = "O"
  269.             end
  270.         elseif song[i + 8][playerA][2] then
  271.             if song[i + 8][playerA][2] >= rangeS and song[i + 8][playerA][2] <= rangeE then
  272.                 nodes[n] = "O"
  273.             end
  274.         elseif song[i + 8][playerA][3] then
  275.             if song[i + 8][playerA][3] >= rangeS and song[i + 8][playerA][3] <= rangeE then
  276.                 nodes[n] = "O"
  277.             end
  278.         elseif song[i + 8][playerA][4] then
  279.             if song[i + 8][playerA][4] >= rangeS and song[i + 8][playerA][4] <= rangeE then
  280.                 nodes[n] = "O"
  281.             end
  282.         elseif song[i + 8][playerA][5] then
  283.             if song[i + 8][playerA][5] >= rangeS and song[i + 8][playerA][5] <= rangeE then
  284.                 nodes[n] = "O"
  285.             end
  286.         end
  287.     end
  288. end
  289. n = n + 1
  290. end
  291.  
  292. -- read notes for up to 5 parts
  293. i = i + play
  294. if song[i] then
  295. if ironblock1 then playnotes(ironblock1, 1, int1) end
  296. if ironblock2 then playnotes(ironblock2, 2, int2) end
  297. if ironblock3 then playnotes(ironblock3, 3, int3) end
  298. if ironblock4 then playnotes(ironblock4, 4, int4) end
  299. if ironblock5 then playnotes(ironblock5, 5, int5) end
  300. end
  301. displaynotes = "|" .. nodes[1] .. "|" .. nodes[2] .. "|" .. nodes[3] .. "|" .. nodes[4] .. "|" .. nodes[5] .. "|"
  302. line1 = line2
  303. line2 = line3
  304. line3 = line4
  305. line4 = line5
  306. line5 = line6
  307. line6 = line7
  308. line7 = line8
  309. line8 = line9
  310. line9 = line10
  311. line10 = displaynotes
  312. term.setCursorPos(1,8)
  313. term.clearLine()
  314. print("  " .. line10 .. "  ")
  315. term.clearLine()
  316. print("  " .. line9 .. "  ")
  317. term.clearLine()
  318. print("  " .. line8 .. "  ")
  319. term.clearLine()
  320. print("  " .. line7 .. "  ")
  321. term.clearLine()
  322. print("  " .. line6 .. "  ")
  323. term.clearLine()
  324. print("  " .. line5 .. "  ")
  325. term.clearLine()
  326. print("  " .. line4 .. "  ")
  327. term.clearLine()
  328. print("->" .. line3 .. "<-")
  329. term.clearLine()
  330. print("  " .. line2 .. "  ")
  331. term.clearLine()
  332. print("  " .. line1 .. "  ")
  333. print("  |1|2|3|4|5|  ")
  334. keepScore()
  335.  
  336. timerID = os.startTimer(song["delay"])
  337. evt, arg1, arg2 = os.pullEvent()
  338. input(evt, arg1, arg2)
  339. if evt ~= "timer" then 
  340.     os.pullEvent("timer")
  341. end
  342.  
  343. end
  344.  
  345.  
  346. score = 0
  347. if musicplaylist == false then break end
  348. end
  349. -- End Playing
Advertisement
Add Comment
Please, Sign In to add comment