Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- chat=peripheral.wrap("right")
- Name="HAL"
- dictionary = {}
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- if fs.exists("dictionary") then
- file = fs.open("dictionary","r")
- d = file.readLine()
- repeat
- table.insert(dictionary,d)
- d = file.readLine()
- until d == nil
- end
- function tag(str)
- return "["..str.."] "
- end
- function add(str)
- for i=1,#dictionary
- if dictionary[i] == str then
- return false
- end
- end
- file = fs.open("dictionary","a")
- file.write(str.."\n")
- file.close()
- dictionary[#dictionary+1] = str
- return true
- end
- while true do
- event, side, user, message = os.pullEvent()
- if message:sub(1,5):lower() == "hal, " then
- if #dictionary == 0 then
- add("HAL...")
- end
- length = math.random(1,10)
- text = ""
- for i=1,length do
- text = text.." "..dictionary[math.random(#dictionary)]
- end
- words = split(message:sub(6)," ")
- for i=1,#words do
- add(words[i])
- end
- chat.say("<"..Name.."> "..text)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement