Wiiplay123

KittenAPI

Jun 14th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.36 KB | None | 0 0
  1. function playSound(...)
  2.     local args = {...}
  3.     --Pitch, Length
  4.     side2 = nil
  5.     if (#args == 1) then
  6.         local sides = rs.getSides()
  7.         for i = 1, #sides do
  8.             if (peripheral.getType(sides[i]) == "speaker") then
  9.                 side2 = sides[i]
  10.                 break
  11.             end
  12.         end
  13.     else
  14.         side2 = args[2]
  15.     end
  16.     if (side2 ~= nil) then
  17.         local speaker = peripheral.wrap(side2)
  18.         local sound2 = args[1]
  19.         sound2 = split(sound2,":")
  20.         speaker.shutdown()
  21.         for i = 1, #sound2, 2 do
  22.             speaker.start(1,tonumber(sound2[i]))
  23.             sleep(tonumber(sound2[i + 1]))
  24.         end
  25.         speaker.shutdown()
  26.         return true
  27.     else
  28.         return false
  29.     end
  30. end
  31. function quickRead(file)
  32.     if (fs.exists(file)) then
  33.         local file2 = fs.open(file,"r")
  34.         local file3 = file2.readAll()
  35.         file2.close()
  36.         return file3
  37.     else
  38.         return nil
  39.     end
  40. end
  41. function quickWrite(file,text)
  42.     local file2 = fs.open(file,"w")
  43.     file2.write(text)
  44.     file2.close()
  45. end
  46. function dbGetAll(file)
  47.     if (fs.exists(file)) then
  48.         local db2 = fs.open(file,"r")
  49.         local db = textutils.unserialize(db2.readAll())
  50.         db2.close()
  51.         return db
  52.     end
  53. end
  54. function dbSetValue(file,user,valueName,value)
  55.     local db2 = fs.open(file,"r")
  56.     local db = textutils.unserialize(db2.readAll())
  57.     db2.close()
  58.     db[user][valueName] = value
  59.     local db2 = fs.open(file,"w")
  60.     db2.write(textutils.serialize[db])
  61.     db2.close()
  62. end
  63. function split(pString, pPattern)
  64.     local Table = {}
  65.     local fpat = "(.-)" .. pPattern
  66.     local last_end = 1
  67.     local s, e, cap = pString:find(fpat, 1)
  68.     while s do
  69.         if s ~= 1 or cap ~= "" then
  70.             table.insert(Table,cap)
  71.         end
  72.         last_end = e+1
  73.         s, e, cap = pString:find(fpat, last_end)
  74.     end
  75.     if last_end <= #pString then
  76.     cap = pString:sub(last_end)
  77.     table.insert(Table, cap)
  78. end
  79. return Table
  80. end
  81. function tf(t,c)
  82.     if (t == c) then
  83.         return 2
  84.     else
  85.         return 1
  86.     end
  87. end
  88. function printCenter(str,y,clear)
  89.     if (str ~= nil) then
  90.         local x3, y3 = term.getCursorPos()
  91.         local x2, y2 = term.getSize()
  92.         term.setCursorPos((x2/2)-(string.len(str)/2),y)
  93.         if (clear == nil) then
  94.             term.clearLine()
  95.         end
  96.         write(str)
  97.         term.setCursorPos(x3,y3)
  98.     end
  99. end
  100. function menu(choices,title, bg, tcolor, text, ccolor)
  101.     if (term.isColor() and bg ~= nil) then
  102.         term.setBackgroundColor(bg)
  103.     end
  104.     term.clear()
  105.     choice = 1
  106.     if (term.isColor() and tcolor ~= nil) then
  107.         term.setTextColor(tcolor)
  108.     end
  109.     printCenter(title,1)
  110.     z, c = term.getSize()
  111.     for i = 1, #choices do
  112.         choices[i] = {choices[i],"> "..choices[i].." <"}
  113.         if (term.isColor() and text ~= nil) then
  114.             term.setTextColor(text)
  115.         end
  116.         printCenter(choices[i][1],i + 2)
  117.         if (i == 1 and term.isColor() and ccolor ~= nil) then
  118.             term.setTextColor(ccolor)
  119.         end
  120.         if (i == 1) then
  121.             term.setCursorPos(((z/2) - (string.len(choices[i][1])/2))-2,i + 2)
  122.             write("> ")
  123.             term.setCursorPos(((z/2) + (string.len(choices[i][1])/2)), i + 2)
  124.             write(" <")
  125.         end
  126.     end
  127.     dun = false
  128.     while dun == false do
  129.         event, key = os.pullEvent("key")
  130.         if (key == 28) then
  131.             dun = true
  132.             elseif (key == 208) then
  133.                 if (term.isColor()) then
  134.                     term.setTextColor(text)
  135.                 end
  136.                 printCenter(choices[choice][1],choice + 2)
  137.                 if (choice == #choices) then
  138.                     choice = 1
  139.                 else
  140.                     choice = choice + 1
  141.                 end
  142.                 if (term.isColor()) then
  143.                     term.setTextColor(text)
  144.                 end
  145.                 printCenter(choices[choice][1],choice + 2)
  146.                 if (term.isColor()) then
  147.                     term.setTextColor(ccolor)
  148.                 end
  149.                 term.setCursorPos(((z/2) - (string.len(choices[choice][1])/2)) - 2,choice + 2)
  150.                 write("> ")
  151.                 term.setCursorPos(((z/2) + (string.len(choices[choice][1])/2)),choice + 2)
  152.                 write(" <")
  153.                 elseif (key == 200) then
  154.                     if (term.isColor()) then
  155.                         term.setTextColor(text)
  156.                     end
  157.                     printCenter(choices[choice][1],choice + 2)
  158.                     if (choice == 1) then
  159.                         choice = #choices
  160.                     else
  161.                         choice = choice - 1
  162.                     end
  163.                     printCenter(choices[choice][1],choice + 2)
  164.                     if (term.isColor()) then
  165.                         term.setTextColor(ccolor)
  166.                     end
  167.                     term.setCursorPos(((z/2) - (string.len(choices[choice][1])/2)) - 2, choice + 2)
  168.                     write("> ")
  169.                     term.setCursorPos(((z/2) + (string.len(choices[choice][1])/2)), choice + 2)
  170.                     write(" <")
  171.             end
  172.         end
  173.         term.setBackgroundColor(colors.black)
  174.         term.clear()
  175.         return choice, choices[choice][1]
  176.     end
Advertisement
Add Comment
Please, Sign In to add comment