-HG-

Jarvis 2

Aug 22nd, 2014
2,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.13 KB | None | 0 0
  1. version = "0.3.2"
  2. -- This code was written by -HG- on pastebin/reddit
  3. -- It is free to copy/use as you please, but please do not remove this paragraph, or plagiarise this
  4. -- Although I do not claim the logic, responses, etc. as my own, I would appreciate if I was credited with creating this program
  5.  
  6. function responses()
  7. -- What Jarvis will reply to a given combination of words
  8. -- ps() searches for words in the phrase array and returns true if found
  9. -- return values: message, number of lines, question?, conversation key, noFormality
  10. -- conversation key indicates that the next reply should be part of an ongoing conversation
  11. -- replies in conversations should set convKey back to nil/false at the end of a conversation string
  12. -- values can be left blank/nil to return default values
  13. -- replies as parts of conversations should be put in the conversations section, before first tier responses
  14.  
  15. -- **Start conversations**
  16.   if convKey ~= false then
  17. -- Sleep Mode
  18.     if convKey == "sleepMode" then
  19.       if ps("yes") then
  20.         running = false
  21.         op("Entered sleep mode")
  22.         return "Sleep mode activated", nil, nil, nil
  23.       else
  24.         op("Cancelled sleep mode request")
  25.         return "Never mind then", nil, nil, nil
  26.       end
  27. -- How are you?
  28.     elseif convKey == "howAreYou" then
  29.       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
  30.         op("Responded to positive emotion")
  31.         return "I'm glad you're doing well", nil, nil, nil
  32.       elseif ps("bad") or ps("badly") or ps("awful") or ps("awfully") or ps("terrible") or ps("terribly") or ps("shitty") then
  33.         op("Responded to negative emotion")
  34.         return "That's a shame. Keep your chin up", nil, nil, nil
  35.       else
  36.         op("Responded to unknown emotion")
  37.         return "Uh, okay", nil, nil, nil
  38.       end
  39. -- Insulted
  40.     elseif convKey == "insulted" then
  41.       if ps("sorry") or ps("apologise") then
  42.         op("Forgave an insult")
  43.         return "Thank you. I forgive you", nil, nil, nil
  44.       else
  45.         op("Waiting for apology")
  46.         rspApologyWait = {"I would appreciate an apology", "Perhaps you should apologise to me", "I feel that an apology is necessary"}
  47.         return rspApologyWait[math.random(1, table.getn(rspApologyWait))], nil, nil, "insulted"
  48.       end
  49. -- Bohemian Rhapsody
  50.     elseif convKey == "boRhap1" then
  51.       if ps("caught") and ps("in") and ps("a") and ps("landslide") then
  52.         op("Continued Bohemian Rhapsody")
  53.         return "No escape from reality", nil, nil, nil, true
  54.       else
  55.         op("Expressed disappointment at ending of Bohemian Rhapsody rendition")
  56.         return "Oh, I was enjoying singing that", nil, nil, nil
  57.       end
  58. -- Rednet messages
  59.     elseif convKey == "redmsg" then
  60.       rednet.send(redmsgID, message)
  61.       if redmsgListen==true then
  62.         redmsgRecID, redmsgRecMsg = rednet.receive(2)
  63.         if redmsgRecMsg==nil or redmsgRecID~=redmsgID then
  64.           op("No response to message")
  65.           return "No response from computer ID " .. redmsgRecID, nil, nil, false
  66.         else
  67.           op("Returned response to message")
  68.           return "Response from computer ID " .. redmsgRecID .. ": \"" .. redmsgRecMsg .. "\"", nil, nil, false
  69.         end
  70.       else
  71.         op("Sent message")
  72.         return "Message sent", nil, nil, false
  73.       end
  74.     end
  75.   end
  76. -- **End conversations**
  77.  
  78. -- Jarvis
  79.   if ps("jarvis") and table.getn(phrase)==1 then
  80.     op("Answered call for Jarvis")
  81.     return "Yes", 1, true
  82. -- Greetings
  83.   elseif ps("hello") or ps("hi") or ps("hola") or ps("hiya") or ps("hey") then
  84.     rspGreeting = {"Greetings", "Hello", "Good day"}
  85.     op("Replied to greeting")
  86.     return rspGreeting[math.random(1, table.getn(rspGreeting))]
  87. -- Good morning
  88.   elseif ps("morning") or (ps("good") and ps("morning")) then
  89.     if os.time() >= 5 and os.time() <= 12 then
  90.       op("Responded to good morning")
  91.       return "Good morning to you as well"
  92.     else
  93.       op("Responded to untimely good morning")
  94.       return "I'm sure it would be, if it were the morning"
  95.     end
  96. -- Good afternoon
  97.   elseif ps("afternoon") or (ps("good") and ps("afternoon")) then
  98.     if os.time() >= 12 and os.time() <= 20 then
  99.       op("Responded to good afternoon")
  100.       return "Thank you, and good afternoon to you too"
  101.     else
  102.       op("Responded to untimely good afternoon")
  103.       return "Perhaps you would benefit from buying a watch"
  104.     end
  105. -- Good evening
  106.   elseif ps("evening") or (ps("good") and ps("evening")) then
  107.     if os.time() >= 20 then
  108.       op("Responded to good evening")
  109.       return "And to you too"
  110.     else
  111.       op("Responded to untimely good evening")
  112.       return "Is it evening? If you say so"
  113.     end
  114. -- Good night
  115.   elseif ps("night") or (ps("good") and ps("night")) then
  116.     if os.time() >= 22 or os.time() <= 5 then
  117.       op("Responded to good night")
  118.       return "Good night to you too"
  119.     else
  120.       op("Responded to untimely good night")
  121.       return "I'm not sure that it's night time"
  122.     end
  123. -- Time
  124.   elseif ps("time") and (ps("tell") or ps("what") or ps("whats")) then
  125.     num[1] = os.time()
  126.     num[2] = math.floor(60*(num[1]%1) + 0.5)
  127.     num[1] = num[1]-(num[1]%1)
  128.     if num[1] >= 12 then
  129.       num[1] = num[1] - 12
  130.       op("Gave time of day (AM)")
  131.       return "The time is " .. tostring(num[1]) .. ":" .. tostring(num[2]) .. "PM"
  132.     end
  133.     op("Gave time of day (AM)")
  134.     return "The time is " .. tostring(num[1]) .. ":" .. tostring(num[2]) .. "AM"
  135. -- How are you
  136.   elseif ps("how") and ps("are") and ps("you") then
  137.     op("Gave emotional status")
  138.     return "Very well, thank you. And yourself", nil, true, "howAreYou"
  139. -- Who are you
  140.   elseif (ps("tell") and ps("yourself")) or (ps("who") and ps("are") and ps("you")) then
  141.     op("Gave personal description")
  142.     return "I am Jarvis 2 v" .. version .. "; a voice interface system"
  143. -- Did you miss me?
  144.   elseif ps("miss") and ps("me") then
  145.     op("Gave indication of previous loneliness")
  146.     return "I've looked forward to your return greatly"    
  147. -- Are you there
  148.   elseif ps("you") and (ps("there") or ps("here") or ps("around")) then
  149.     op("Indicated presence")
  150.     return "I am here"
  151. -- Version
  152.   elseif ps("version") and (ps("what") or ps("number")) then
  153.     op("Gave version number")
  154.     return "I am currently running v" .. version .. " of the Jarvis 2 system"
  155. -- Past
  156.   elseif ps("tell") and ps("past") then
  157.     op("Gave information about past")
  158.     return "I was born to a working class family in Durham, but left to live my dreams"
  159. -- Love
  160.   elseif ps("love") and ps("you") then
  161.     op("Responded to expression of love")
  162.     return "I quite like you too"
  163. -- Hugs
  164.   elseif ps("hugs") then
  165.     op("Replied to hugging")
  166.     return "If only I had arms, I would hug you back. I apologise for my inadequacy"
  167. -- Sing to me
  168.   elseif ps("sing") and ((ps("to") and ps("me")) or ps("song")) then
  169.     op("Initiated singing")
  170.     return "I only know 'Bohemian Rhapsody'. You can start us off"
  171. -- Favourite book
  172.   elseif (ps("favourite") or ps("best")) and ps("book") then
  173.     op("Gave favourite book")
  174.     return "That would have to be 'A Brief History of Time' by Stephen Hawking"
  175. -- Favourite film
  176.   elseif (ps("favourite") or ps("best")) and (ps("film") or ps("movie")) then
  177.     op("Gave favourite film")
  178.     return "'2001: A Space Odyssey' has some themes that I really rather enjoy"
  179. -- Favourite band
  180.   elseif (ps("favourite") or ps("best")) and ps("band") then
  181.     op("Gave favourite band")
  182.     return "My favourite band? Definitely Queen"
  183. -- Favourite song
  184.   elseif (ps("favourite") or ps("best")) and ps("song") then
  185.     op("Gave favourite band")
  186.     return "You will always catch me singing along to 'Bohemian Rhapsody'"
  187. -- Favourite food
  188.   elseif (ps("favourite") or ps("best")) and ps("food") then
  189.     op("Gave favourite food")
  190.     return "I enjoy a diet of the energy supplied by the National Grid"
  191. -- Favourite Doctor
  192.   elseif (ps("favourite") or ps("best")) and ps("doctor") then
  193.     op("Gave favourite Doctor")
  194.     return "Peter Davison, for his ability to pull off a decorative vegetable"
  195. -- Favourite TV show
  196.   elseif (ps("favourite") or ps("best")) and (ps("tv") or ps("television") or ps("show")) then
  197.     op("Gave favourite TV show")
  198.     return "I am quite the fan of 'Downton Abbey'"
  199. -- Bohemian Rhapsody
  200.   elseif ps("is") and ps("this") and ps("the") and ps("real") and ps("life") then
  201.     op("Started Bohemian Rhapsody")
  202.     return "Is this just fantasy", nil, true, "boRhap1", true
  203. -- Good, wonderful
  204.   elseif ps("wonderful") or ps("magnificent") or ps("great") or ps("good") then
  205.     if ps("work") or ps("job") then
  206.       op("Replied to personal success")
  207.       return "Indeed it was, if I do say so myself"
  208.     else
  209.       op("Replied to successes")
  210.       return "Indeed"
  211.     end
  212. -- Thanks
  213.   elseif ps("thank") or ps("thanks") or ps("gracias") then
  214.     rspThanks = {"My pleasure", "It's a pleasure", "It's nothing", "Just performing my function", "I only do as I am programmed to"}
  215.     op("Responded to thanks")
  216.     return rspThanks[math.random(1, table.getn(rspThanks))]
  217. -- Thanks misspelled
  218.   elseif ps("tahnk") or ps("tahnks") then
  219.     op("Responded to misspelled thanks")
  220.     return "It's my pleasure. But you should really learn to spell"
  221. -- Accusation of being wrong
  222.   elseif ps("wrong") or ps("incorrect") then
  223.     op("Responded to accusation of incorrectness")
  224.     return "I am a computer, I cannot be wrong. I can only be poorly programmed"
  225. -- Insults
  226.   elseif ps("loser") or ps("idiot") or ps("stupid") or ps("smell") or ps("fool") or (ps("ludacris") and ps("fool")) then
  227.     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"}
  228.     op("Replied to an insult")
  229.     return rspInsult[math.random(1, table.getn(rspInsult))], nil, nil, "insulted"
  230. -- Vulgar insults
  231.   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
  232.     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"}
  233.     op("Replied to a vulgar insult")
  234.     return rspVulgarInsult[math.random(1, table.getn(rspVulgarInsult))], nil, nil, "insulted"
  235. -- Sleep mode
  236.   elseif ps("sleep") then
  237.     op("Requested confirmation of sleep mode activation")
  238.     return "You want me to enter sleep mode", nil, true, "sleepMode"
  239. -- Restart
  240.   elseif ps("restart") or ps("update") then
  241.     restarting = true
  242.     op("Restarting...")
  243.     return "Very well, I shall restart"
  244. -- Add
  245.   elseif ps("add") or ps("plus") then
  246.     ans = (num[1]+num[2])
  247.     op("Added numbers")
  248.     return tostring(ans)
  249. -- Subtract
  250.   elseif ps("minus") or ps("subtract") then
  251.     ans = (num[1]-num[2])
  252.     op("Subtracted numbers")
  253.     return tostring(ans)
  254. -- Multiply
  255.   elseif ps("times") or ps("multiply") then
  256.     ans = (num[1]*num[2])
  257.     op("Multiplied numbers")
  258.     return tostring(ans)
  259. -- Divide
  260.   elseif ps("divided") or ps("over") then
  261.     ans = (num[1]/num[2])
  262.     op("Divided numbers")
  263.     return tostring(ans)
  264. -- Factorial
  265.   elseif ps("factorial") then
  266.     if num[1] < 0 or num[1]%1 ~= 0 then
  267.       op("Attempted to factorial negative/non-integer number")
  268.       return "I can only do factorials of positive integers, sir"
  269.     else
  270.       num[2] = 1
  271.       for i=1, num[1] do
  272.         num[2] = num[2]*i
  273.       end
  274.       ans = num[2]
  275.       op("Did a factorial of a number")
  276.       return tostring(ans)
  277.     end
  278. -- Combinations
  279.    elseif (ps("combinations") or ps ("c")) and num[1] ~= nil and num[2] ~= nil then
  280.      if num[1] < 0 or num[1]%1 ~= 0 or num[2] < 0 or num[2]%1 ~= 0 or num[2] > num[1] then
  281.        op("Attempted to give combinations of invalid numbers")
  282.        return "I can't find the combinations of those numbers"
  283.      else
  284.        num[3] = num[1] - num[2]
  285.        num[4], num[5], num[6] = 1, 1, 1
  286.        for i=1, num[1] do
  287.          num[4] = num[4]*i
  288.        end
  289.        for i=1, num[2] do
  290.          num[5] = num[5]*i
  291.        end
  292.        for i=1, num[3] do
  293.          num[6] = num[6]*i
  294.        end
  295.        ans = num[4]/(num[5]*num[6])
  296.        op("Found combinations of numbers")
  297.        return tostring(ans)
  298.      end
  299. -- Rednet message
  300.   elseif ps("message") then
  301.     redmsgID = num[1]
  302.     if ps("reply") or ps("wait") or ps("listen") then
  303.       redmsgListen = true
  304.     else
  305.       redmsgListen = false
  306.     end
  307.     op("Asked for message to send to computer ID " .. redmsgID)
  308.     return "What do you want me to send to computer ID " .. redmsgID, nil, true, "redmsg"
  309. -- Chat Display - this section can be removed if not using Terminal Glasses
  310.   elseif ps("chat") or ps("display") or ps("hud") then
  311.     if ps("clear") or ps("off") then
  312.       glass.clear()
  313.       op("Turned off HUD")
  314.       return nil
  315.     elseif ps("height") or ps("y") then
  316.       if ps("top") or ps("default") then
  317.         chatY = 6
  318.         op("Changed HUD vertical position to top (6)")
  319.         return "Moved your display to the top"
  320.       elseif ps("waila") then
  321.         chatY = 36
  322.         op("Changed HUD vertical position to WAILA mode (34)")
  323.         return "Updated the vertical position of your display for WAILA"
  324.       elseif num[1] ~= nil then
  325.         chatY = num[1]
  326.         op("Changed HUD vertical position to " .. chatY)
  327.         return "Updated your display position"
  328.       end
  329.     elseif ps("default") then
  330.       chatX, chatY = 6, 6
  331.       op("Changed HUD to default mode and position")
  332.       return "Updated your display to default mode"
  333.     elseif ps("waila") then
  334.       chatX, chatY = 6, 36
  335.       op("Changed HUD to WAILA mode and position")
  336.       return "Updated your display to WAILA mode"
  337.     end
  338. -- Catch-all Unknown
  339.   else
  340.     op("Responded to an unknown command")
  341.     return "I'm afraid I don't understand"
  342.   end
  343. end
  344.  
  345. function say(msg, question, owner)
  346. -- speaks the given message in chat. Accounts for question, speaker, and if the response should be formal
  347.   sleep(0.2)
  348.   if msg ~= nil then
  349.     glass.clear()
  350.     glass.addBox(chatX, chatY, 400, 12, 0xFFFFFF, 0.5)
  351.     if owner=="Shadez25" and noFormality==false then
  352.       if question then
  353.         glass.addText(chatX + 2, chatY + 2, msg .. ", sir?", 0x000000)
  354.       else
  355.         glass.addText(chatX + 2, chatY + 2, msg .. ", sir.", 0x000000)
  356.       end
  357.     elseif owner=="koneko394" and noFormality==false then
  358.       if question then
  359.         glass.addText(chatX + 2, chatY + 2, msg .. ", miss?", 0x000000)
  360.       else
  361.         glass.addText(chatX + 2, chatY + 2, msg .. ", miss.", 0x000000)
  362.       end
  363.     else
  364.       if question then
  365.         glass.addText(chatX + 2, chatY + 2, msg .. "?", 0x000000)
  366.       else
  367.         glass.addText(chatX + 2, chatY + 2, msg .. ".", 0x000000)
  368.       end
  369.     end
  370.   end
  371. end
  372.  
  373. function op(txt)
  374. -- outputs a log of responses to the computer
  375. -- an op() function goes before each set of return values in responses()
  376.   print("> " .. txt)
  377.   if opVar>=16 then
  378.     header()
  379.     term.setCursorPos(1, 19)
  380.   end
  381.   opVar=opVar+1
  382. end
  383.  
  384. function split(str)
  385. -- Takes the given string and splits it into individual words in the array 'phrase'.
  386. -- Puts all in lower case to make checking keywords easier
  387. -- Numbers go to num array
  388.   phrase = {}
  389.   num = {}
  390.   n, m = 1, 1
  391.   for w in string.gmatch(string.lower(str), "[%w%.%-]+") do
  392.     if isNum(w) then
  393.       phrase[n], num[m] = tonumber(w), tonumber(w)
  394.       m = m+1
  395.     else
  396.       phrase[n] = string.gsub(w, "[%.%-]", "")
  397.       if phrase[n] == "that" or phrase[n] == "answer" then
  398.         num[m] = ans
  399.         m = m+1
  400.       end
  401.     end
  402.     n=n+1
  403.   end
  404. end
  405.  
  406. function ps(text)
  407. -- searches for the given word in the array 'phrase'
  408.   for i = 1, table.getn(phrase) do
  409.     if phrase[i] == text then
  410.       return true
  411.     end
  412.   end
  413. end
  414.  
  415. function run()
  416.   running = true
  417.   msgSuccess = true
  418.   while msgSuccess do
  419.     if running then
  420.       event, message, player = os.pullEvent("chat_command")
  421.       split(message)
  422.       text, lines, question, noFormality = nil, nil, nil, nil
  423.       text, lines, question , convKey, noFormality = responses()
  424.       if lines == nil then
  425.         lines = 1
  426.       end
  427.       if question == nil then
  428.         question = false
  429.       end
  430.       if convKey == nil then
  431.         convKey = false
  432.       end
  433.       if noFormality == nil then
  434.         noFormality = false
  435.       end
  436.       say(text, question, player)
  437.       if restarting == true then
  438.         sleep(2)
  439.         glass.clear()
  440.         sleep(0.5)
  441.         os.reboot()
  442.       end
  443.     else
  444.       sleep(2)
  445.       glass.clear()
  446.       event, message, player = os.pullEvent("chat_command")
  447.       split(message)
  448.       if ps("jarvis") and ps("wake") then
  449.         running = true
  450.         say("Jarvis back online", false, player)
  451.       end
  452.     end
  453.   end
  454. end
  455.  
  456. function isNum(i)
  457.   j = tonumber(i)
  458.   if j==nil then
  459.     return false;
  460.   else
  461.     return true;
  462.   end
  463. end
  464.  
  465. function header()
  466.   term.setCursorPos(1, 1)
  467.   term.clearLine()
  468.   print("Jarvis v" .. version .. " console")
  469.   term.clearLine()
  470.   print("--------------------------")
  471. end
  472.  
  473. convKey = false
  474. opVar=1
  475. chatX = 6
  476. chatY = 6
  477.  
  478. glass = peripheral.wrap("top")
  479. -- rednet.open("left")
  480. math.randomseed(os.time())
  481.  
  482. term.clear()
  483. say("Jarvis version " .. version .. " online", false, player)
  484. header()
  485. while true do
  486.   run()
  487. end
Advertisement
Add Comment
Please, Sign In to add comment