Advertisement
justync7

hal

Jun 26th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. chat=peripheral.wrap("right")
  2. Name="HAL"
  3. dictionary = {}
  4. function split(str, pat)
  5. local t = {} -- NOTE: use {n = 0} in Lua-5.0
  6. local fpat = "(.-)" .. pat
  7. local last_end = 1
  8. local s, e, cap = str:find(fpat, 1)
  9. while s do
  10. if s ~= 1 or cap ~= "" then
  11. table.insert(t,cap)
  12. end
  13. last_end = e+1
  14. s, e, cap = str:find(fpat, last_end)
  15. end
  16. if last_end <= #str then
  17. cap = str:sub(last_end)
  18. table.insert(t, cap)
  19. end
  20. return t
  21. end
  22. if fs.exists("dictionary") then
  23. file = fs.open("dictionary","r")
  24. d = file.readLine()
  25. repeat
  26. table.insert(dictionary,d)
  27. d = file.readLine()
  28. until d == nil
  29. end
  30. function tag(str)
  31. return "["..str.."] "
  32. end
  33. function add(str)
  34. for i=1,#dictionary
  35. if dictionary[i] == str then
  36. return false
  37. end
  38. end
  39. file = fs.open("dictionary","a")
  40. file.write(str.."\n")
  41. file.close()
  42. dictionary[#dictionary+1] = str
  43. return true
  44. end
  45. while true do
  46. event, side, user, message = os.pullEvent()
  47. if message:sub(1,5):lower() == "hal, " then
  48. if #dictionary == 0 then
  49. add("HAL...")
  50. end
  51. length = math.random(1,10)
  52. text = ""
  53. for i=1,length do
  54. text = text.." "..dictionary[math.random(#dictionary)]
  55. end
  56. words = split(message:sub(6)," ")
  57. for i=1,#words do
  58. add(words[i])
  59. end
  60. chat.say("<"..Name.."> "..text)
  61. end
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement