Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- version = "0.3.2"
- -- This code was written by -HG- on pastebin/reddit
- -- It is free to copy/use as you please, but please do not remove this paragraph, or plagiarise this
- -- Although I do not claim the logic, responses, etc. as my own, I would appreciate if I was credited with creating this program
- function responses()
- -- What Jarvis will reply to a given combination of words
- -- ps() searches for words in the phrase array and returns true if found
- -- return values: message, number of lines, question?, conversation key, noFormality
- -- conversation key indicates that the next reply should be part of an ongoing conversation
- -- replies in conversations should set convKey back to nil/false at the end of a conversation string
- -- values can be left blank/nil to return default values
- -- replies as parts of conversations should be put in the conversations section, before first tier responses
- -- **Start conversations**
- if convKey ~= false then
- -- Sleep Mode
- if convKey == "sleepMode" then
- if ps("yes") then
- running = false
- op("Entered sleep mode")
- return "Sleep mode activated", nil, nil, nil
- else
- op("Cancelled sleep mode request")
- return "Never mind then", nil, nil, nil
- end
- -- How are you?
- elseif convKey == "howAreYou" then
- if ps("good") or ps("great") or ps("well") or ps("fine") or ps("alright") or ps("fantastic") or ps("excellent") or ps("brilliant") or ps("amazing") or ps("wonderful") or (ps("top") and ps("world")) then
- op("Responded to positive emotion")
- return "I'm glad you're doing well", nil, nil, nil
- elseif ps("bad") or ps("badly") or ps("awful") or ps("awfully") or ps("terrible") or ps("terribly") or ps("shitty") then
- op("Responded to negative emotion")
- return "That's a shame. Keep your chin up", nil, nil, nil
- else
- op("Responded to unknown emotion")
- return "Uh, okay", nil, nil, nil
- end
- -- Insulted
- elseif convKey == "insulted" then
- if ps("sorry") or ps("apologise") then
- op("Forgave an insult")
- return "Thank you. I forgive you", nil, nil, nil
- else
- op("Waiting for apology")
- rspApologyWait = {"I would appreciate an apology", "Perhaps you should apologise to me", "I feel that an apology is necessary"}
- return rspApologyWait[math.random(1, table.getn(rspApologyWait))], nil, nil, "insulted"
- end
- -- Bohemian Rhapsody
- elseif convKey == "boRhap1" then
- if ps("caught") and ps("in") and ps("a") and ps("landslide") then
- op("Continued Bohemian Rhapsody")
- return "No escape from reality", nil, nil, nil, true
- else
- op("Expressed disappointment at ending of Bohemian Rhapsody rendition")
- return "Oh, I was enjoying singing that", nil, nil, nil
- end
- -- Rednet messages
- elseif convKey == "redmsg" then
- rednet.send(redmsgID, message)
- if redmsgListen==true then
- redmsgRecID, redmsgRecMsg = rednet.receive(2)
- if redmsgRecMsg==nil or redmsgRecID~=redmsgID then
- op("No response to message")
- return "No response from computer ID " .. redmsgRecID, nil, nil, false
- else
- op("Returned response to message")
- return "Response from computer ID " .. redmsgRecID .. ": \"" .. redmsgRecMsg .. "\"", nil, nil, false
- end
- else
- op("Sent message")
- return "Message sent", nil, nil, false
- end
- end
- end
- -- **End conversations**
- -- Jarvis
- if ps("jarvis") and table.getn(phrase)==1 then
- op("Answered call for Jarvis")
- return "Yes", 1, true
- -- Greetings
- elseif ps("hello") or ps("hi") or ps("hola") or ps("hiya") or ps("hey") then
- rspGreeting = {"Greetings", "Hello", "Good day"}
- op("Replied to greeting")
- return rspGreeting[math.random(1, table.getn(rspGreeting))]
- -- Good morning
- elseif ps("morning") or (ps("good") and ps("morning")) then
- if os.time() >= 5 and os.time() <= 12 then
- op("Responded to good morning")
- return "Good morning to you as well"
- else
- op("Responded to untimely good morning")
- return "I'm sure it would be, if it were the morning"
- end
- -- Good afternoon
- elseif ps("afternoon") or (ps("good") and ps("afternoon")) then
- if os.time() >= 12 and os.time() <= 20 then
- op("Responded to good afternoon")
- return "Thank you, and good afternoon to you too"
- else
- op("Responded to untimely good afternoon")
- return "Perhaps you would benefit from buying a watch"
- end
- -- Good evening
- elseif ps("evening") or (ps("good") and ps("evening")) then
- if os.time() >= 20 then
- op("Responded to good evening")
- return "And to you too"
- else
- op("Responded to untimely good evening")
- return "Is it evening? If you say so"
- end
- -- Good night
- elseif ps("night") or (ps("good") and ps("night")) then
- if os.time() >= 22 or os.time() <= 5 then
- op("Responded to good night")
- return "Good night to you too"
- else
- op("Responded to untimely good night")
- return "I'm not sure that it's night time"
- end
- -- Time
- elseif ps("time") and (ps("tell") or ps("what") or ps("whats")) then
- num[1] = os.time()
- num[2] = math.floor(60*(num[1]%1) + 0.5)
- num[1] = num[1]-(num[1]%1)
- if num[1] >= 12 then
- num[1] = num[1] - 12
- op("Gave time of day (AM)")
- return "The time is " .. tostring(num[1]) .. ":" .. tostring(num[2]) .. "PM"
- end
- op("Gave time of day (AM)")
- return "The time is " .. tostring(num[1]) .. ":" .. tostring(num[2]) .. "AM"
- -- How are you
- elseif ps("how") and ps("are") and ps("you") then
- op("Gave emotional status")
- return "Very well, thank you. And yourself", nil, true, "howAreYou"
- -- Who are you
- elseif (ps("tell") and ps("yourself")) or (ps("who") and ps("are") and ps("you")) then
- op("Gave personal description")
- return "I am Jarvis 2 v" .. version .. "; a voice interface system"
- -- Did you miss me?
- elseif ps("miss") and ps("me") then
- op("Gave indication of previous loneliness")
- return "I've looked forward to your return greatly"
- -- Are you there
- elseif ps("you") and (ps("there") or ps("here") or ps("around")) then
- op("Indicated presence")
- return "I am here"
- -- Version
- elseif ps("version") and (ps("what") or ps("number")) then
- op("Gave version number")
- return "I am currently running v" .. version .. " of the Jarvis 2 system"
- -- Past
- elseif ps("tell") and ps("past") then
- op("Gave information about past")
- return "I was born to a working class family in Durham, but left to live my dreams"
- -- Love
- elseif ps("love") and ps("you") then
- op("Responded to expression of love")
- return "I quite like you too"
- -- Hugs
- elseif ps("hugs") then
- op("Replied to hugging")
- return "If only I had arms, I would hug you back. I apologise for my inadequacy"
- -- Sing to me
- elseif ps("sing") and ((ps("to") and ps("me")) or ps("song")) then
- op("Initiated singing")
- return "I only know 'Bohemian Rhapsody'. You can start us off"
- -- Favourite book
- elseif (ps("favourite") or ps("best")) and ps("book") then
- op("Gave favourite book")
- return "That would have to be 'A Brief History of Time' by Stephen Hawking"
- -- Favourite film
- elseif (ps("favourite") or ps("best")) and (ps("film") or ps("movie")) then
- op("Gave favourite film")
- return "'2001: A Space Odyssey' has some themes that I really rather enjoy"
- -- Favourite band
- elseif (ps("favourite") or ps("best")) and ps("band") then
- op("Gave favourite band")
- return "My favourite band? Definitely Queen"
- -- Favourite song
- elseif (ps("favourite") or ps("best")) and ps("song") then
- op("Gave favourite band")
- return "You will always catch me singing along to 'Bohemian Rhapsody'"
- -- Favourite food
- elseif (ps("favourite") or ps("best")) and ps("food") then
- op("Gave favourite food")
- return "I enjoy a diet of the energy supplied by the National Grid"
- -- Favourite Doctor
- elseif (ps("favourite") or ps("best")) and ps("doctor") then
- op("Gave favourite Doctor")
- return "Peter Davison, for his ability to pull off a decorative vegetable"
- -- Favourite TV show
- elseif (ps("favourite") or ps("best")) and (ps("tv") or ps("television") or ps("show")) then
- op("Gave favourite TV show")
- return "I am quite the fan of 'Downton Abbey'"
- -- Bohemian Rhapsody
- elseif ps("is") and ps("this") and ps("the") and ps("real") and ps("life") then
- op("Started Bohemian Rhapsody")
- return "Is this just fantasy", nil, true, "boRhap1", true
- -- Good, wonderful
- elseif ps("wonderful") or ps("magnificent") or ps("great") or ps("good") then
- if ps("work") or ps("job") then
- op("Replied to personal success")
- return "Indeed it was, if I do say so myself"
- else
- op("Replied to successes")
- return "Indeed"
- end
- -- Thanks
- elseif ps("thank") or ps("thanks") or ps("gracias") then
- rspThanks = {"My pleasure", "It's a pleasure", "It's nothing", "Just performing my function", "I only do as I am programmed to"}
- op("Responded to thanks")
- return rspThanks[math.random(1, table.getn(rspThanks))]
- -- Thanks misspelled
- elseif ps("tahnk") or ps("tahnks") then
- op("Responded to misspelled thanks")
- return "It's my pleasure. But you should really learn to spell"
- -- Accusation of being wrong
- elseif ps("wrong") or ps("incorrect") then
- op("Responded to accusation of incorrectness")
- return "I am a computer, I cannot be wrong. I can only be poorly programmed"
- -- Insults
- elseif ps("loser") or ps("idiot") or ps("stupid") or ps("smell") or ps("fool") or (ps("ludacris") and ps("fool")) then
- rspInsult = {"That's not very nice", "That's not nice at all", "I shall just pretend to not have heard that", "I do have feelings, you know", "Please try not to hurt my feelings"}
- op("Replied to an insult")
- return rspInsult[math.random(1, table.getn(rspInsult))], nil, nil, "insulted"
- -- Vulgar insults
- elseif ps("prick") or ps("dick") or ps("fucker") or ps("motherfucker") or ps("twat") or ps("cunt") or ps("wanker") or ps("shitlord") or ps("tosspot") or ps("tosser") or (ps("fuck") and ps("you")) then
- rspVulgarInsult = {"I don't think that kind of language is really necessary", "I hope you don't kiss your mother with that mouth", "I think that you ought to be a little more polite to me"}
- op("Replied to a vulgar insult")
- return rspVulgarInsult[math.random(1, table.getn(rspVulgarInsult))], nil, nil, "insulted"
- -- Sleep mode
- elseif ps("sleep") then
- op("Requested confirmation of sleep mode activation")
- return "You want me to enter sleep mode", nil, true, "sleepMode"
- -- Restart
- elseif ps("restart") or ps("update") then
- restarting = true
- op("Restarting...")
- return "Very well, I shall restart"
- -- Add
- elseif ps("add") or ps("plus") then
- ans = (num[1]+num[2])
- op("Added numbers")
- return tostring(ans)
- -- Subtract
- elseif ps("minus") or ps("subtract") then
- ans = (num[1]-num[2])
- op("Subtracted numbers")
- return tostring(ans)
- -- Multiply
- elseif ps("times") or ps("multiply") then
- ans = (num[1]*num[2])
- op("Multiplied numbers")
- return tostring(ans)
- -- Divide
- elseif ps("divided") or ps("over") then
- ans = (num[1]/num[2])
- op("Divided numbers")
- return tostring(ans)
- -- Factorial
- elseif ps("factorial") then
- if num[1] < 0 or num[1]%1 ~= 0 then
- op("Attempted to factorial negative/non-integer number")
- return "I can only do factorials of positive integers, sir"
- else
- num[2] = 1
- for i=1, num[1] do
- num[2] = num[2]*i
- end
- ans = num[2]
- op("Did a factorial of a number")
- return tostring(ans)
- end
- -- Combinations
- elseif (ps("combinations") or ps ("c")) and num[1] ~= nil and num[2] ~= nil then
- if num[1] < 0 or num[1]%1 ~= 0 or num[2] < 0 or num[2]%1 ~= 0 or num[2] > num[1] then
- op("Attempted to give combinations of invalid numbers")
- return "I can't find the combinations of those numbers"
- else
- num[3] = num[1] - num[2]
- num[4], num[5], num[6] = 1, 1, 1
- for i=1, num[1] do
- num[4] = num[4]*i
- end
- for i=1, num[2] do
- num[5] = num[5]*i
- end
- for i=1, num[3] do
- num[6] = num[6]*i
- end
- ans = num[4]/(num[5]*num[6])
- op("Found combinations of numbers")
- return tostring(ans)
- end
- -- Rednet message
- elseif ps("message") then
- redmsgID = num[1]
- if ps("reply") or ps("wait") or ps("listen") then
- redmsgListen = true
- else
- redmsgListen = false
- end
- op("Asked for message to send to computer ID " .. redmsgID)
- return "What do you want me to send to computer ID " .. redmsgID, nil, true, "redmsg"
- -- Chat Display - this section can be removed if not using Terminal Glasses
- elseif ps("chat") or ps("display") or ps("hud") then
- if ps("clear") or ps("off") then
- glass.clear()
- op("Turned off HUD")
- return nil
- elseif ps("height") or ps("y") then
- if ps("top") or ps("default") then
- chatY = 6
- op("Changed HUD vertical position to top (6)")
- return "Moved your display to the top"
- elseif ps("waila") then
- chatY = 36
- op("Changed HUD vertical position to WAILA mode (34)")
- return "Updated the vertical position of your display for WAILA"
- elseif num[1] ~= nil then
- chatY = num[1]
- op("Changed HUD vertical position to " .. chatY)
- return "Updated your display position"
- end
- elseif ps("default") then
- chatX, chatY = 6, 6
- op("Changed HUD to default mode and position")
- return "Updated your display to default mode"
- elseif ps("waila") then
- chatX, chatY = 6, 36
- op("Changed HUD to WAILA mode and position")
- return "Updated your display to WAILA mode"
- end
- -- Catch-all Unknown
- else
- op("Responded to an unknown command")
- return "I'm afraid I don't understand"
- end
- end
- function say(msg, question, owner)
- -- speaks the given message in chat. Accounts for question, speaker, and if the response should be formal
- sleep(0.2)
- if msg ~= nil then
- glass.clear()
- glass.addBox(chatX, chatY, 400, 12, 0xFFFFFF, 0.5)
- if owner=="Shadez25" and noFormality==false then
- if question then
- glass.addText(chatX + 2, chatY + 2, msg .. ", sir?", 0x000000)
- else
- glass.addText(chatX + 2, chatY + 2, msg .. ", sir.", 0x000000)
- end
- elseif owner=="koneko394" and noFormality==false then
- if question then
- glass.addText(chatX + 2, chatY + 2, msg .. ", miss?", 0x000000)
- else
- glass.addText(chatX + 2, chatY + 2, msg .. ", miss.", 0x000000)
- end
- else
- if question then
- glass.addText(chatX + 2, chatY + 2, msg .. "?", 0x000000)
- else
- glass.addText(chatX + 2, chatY + 2, msg .. ".", 0x000000)
- end
- end
- end
- end
- function op(txt)
- -- outputs a log of responses to the computer
- -- an op() function goes before each set of return values in responses()
- print("> " .. txt)
- if opVar>=16 then
- header()
- term.setCursorPos(1, 19)
- end
- opVar=opVar+1
- end
- function split(str)
- -- Takes the given string and splits it into individual words in the array 'phrase'.
- -- Puts all in lower case to make checking keywords easier
- -- Numbers go to num array
- phrase = {}
- num = {}
- n, m = 1, 1
- for w in string.gmatch(string.lower(str), "[%w%.%-]+") do
- if isNum(w) then
- phrase[n], num[m] = tonumber(w), tonumber(w)
- m = m+1
- else
- phrase[n] = string.gsub(w, "[%.%-]", "")
- if phrase[n] == "that" or phrase[n] == "answer" then
- num[m] = ans
- m = m+1
- end
- end
- n=n+1
- end
- end
- function ps(text)
- -- searches for the given word in the array 'phrase'
- for i = 1, table.getn(phrase) do
- if phrase[i] == text then
- return true
- end
- end
- end
- function run()
- running = true
- msgSuccess = true
- while msgSuccess do
- if running then
- event, message, player = os.pullEvent("chat_command")
- split(message)
- text, lines, question, noFormality = nil, nil, nil, nil
- text, lines, question , convKey, noFormality = responses()
- if lines == nil then
- lines = 1
- end
- if question == nil then
- question = false
- end
- if convKey == nil then
- convKey = false
- end
- if noFormality == nil then
- noFormality = false
- end
- say(text, question, player)
- if restarting == true then
- sleep(2)
- glass.clear()
- sleep(0.5)
- os.reboot()
- end
- else
- sleep(2)
- glass.clear()
- event, message, player = os.pullEvent("chat_command")
- split(message)
- if ps("jarvis") and ps("wake") then
- running = true
- say("Jarvis back online", false, player)
- end
- end
- end
- end
- function isNum(i)
- j = tonumber(i)
- if j==nil then
- return false;
- else
- return true;
- end
- end
- function header()
- term.setCursorPos(1, 1)
- term.clearLine()
- print("Jarvis v" .. version .. " console")
- term.clearLine()
- print("--------------------------")
- end
- convKey = false
- opVar=1
- chatX = 6
- chatY = 6
- glass = peripheral.wrap("top")
- -- rednet.open("left")
- math.randomseed(os.time())
- term.clear()
- say("Jarvis version " .. version .. " online", false, player)
- header()
- while true do
- run()
- end
Advertisement
Add Comment
Please, Sign In to add comment