Guest User

KittenAPI

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