home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / compre1a / myagent.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-09-07  |  18.1 KB  |  350 lines

  1. VERSION 5.00
  2. Object = "{F5BE8BC2-7DE6-11D0-91FE-00C04FD701A5}#2.0#0"; "AGENTCTL.DLL"
  3. Begin VB.Form frmMain 
  4.    Caption         =   "Accepting input"
  5.    ClientHeight    =   1695
  6.    ClientLeft      =   60
  7.    ClientTop       =   345
  8.    ClientWidth     =   4680
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   1695
  11.    ScaleWidth      =   4680
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin VB.CommandButton cmdGo 
  14.       Caption         =   "Go"
  15.       Enabled         =   0   'False
  16.       Height          =   615
  17.       Left            =   1500
  18.       TabIndex        =   0
  19.       Top             =   300
  20.       Width           =   1455
  21.    End
  22.    Begin VB.Label Label1 
  23.       Height          =   255
  24.       Left            =   660
  25.       TabIndex        =   1
  26.       Top             =   1080
  27.       Width           =   3075
  28.    End
  29.    Begin AgentObjectsCtl.Agent Agent 
  30.       Left            =   360
  31.       Top             =   300
  32.       _cx             =   847
  33.       _cy             =   847
  34.    End
  35. Attribute VB_Name = "frmMain"
  36. Attribute VB_GlobalNameSpace = False
  37. Attribute VB_Creatable = False
  38. Attribute VB_PredeclaredId = True
  39. Attribute VB_Exposed = False
  40. Option Explicit 'This statement prevents the creation
  41.                 'of Varient variables when the variables
  42.                 'are referenced but not created before
  43.                 'e.g. For n = 1 to 10, if n was never Dim-ed
  44. Dim IntroComplete As Boolean    'When the intro is complete
  45.                                 'this is set to true
  46.                                 'to activate some options
  47.                                 '(well 1 option actually)
  48. Dim LoadRequest(4)
  49.     'These are the LoadRequests, when one is set to a
  50. 'R  'Character command, when the command is completed the
  51. 'E  'Agent control runs extra lines of code, according to
  52. 'A  'which request was used, such as an UnLoad function.
  53. 'D  'This is needed because Agent works with Ques, so when
  54.     'you give Agent a command, that command is put into a
  55. 'T  'que, and your program carries on to the next command
  56. 'H  'in your program, thus freeing up your program to do
  57. 'I  'other things, though it also means if you give an
  58. 'S  'agent a command followed by an UnLoad Me statement,
  59. '!  'the Agent command will be qued, the program carries
  60. '!  'on, and unloads the form, and the Agent control,
  61.     'stopping everything!!
  62.     'It is a rather strange way of programming,
  63.     'but it works, and this is how Microsoft
  64.     'make their scripts too :)
  65.     'This is where many programmers go wrong, and swear at
  66.     'their computers for a few hours!
  67.     '(before kicking it out of a window)
  68. Dim Genie As IAgentCtlCharacter
  69. Dim Merlin As IAgentCtlCharacter
  70.     'To be used in Set commands
  71. Dim Request As IAgentCtlRequest
  72.     'To set to an agent request, so another agent can wait
  73.     'for that request in another Agent's que to complete
  74.     'before continuing the commands in it's own que,
  75.     'well, try going through this later and remove all the
  76.     ''Set Request =' bits, see what happens when you run
  77.     'it ;-)
  78. Private Sub LoadGenie()
  79.     Set LoadRequest(0) = Agent.Characters.Load("Genie", "Genie.acs")
  80.         'This command, Sets LoadRequest(0) as the
  81.         'Agent Load command, which is run, so when the
  82.         'Agent control finishes loading the Genie.acs file
  83.         'it will run through the Agent_RequestComplete
  84.         'Subroutine (see below) and find the finished
  85.         'request equal to LeadRequest(0), and so run the
  86.         'commands in this part of code.  Using this method,
  87.         'you can set commands to only run when an Agent has
  88.         'completed an operation, such as i have used one to
  89.         'only Unload the form when Merlin has finished
  90.         'hiding, this is how you get around the Ques problem
  91.         '(strange huh?!)
  92.     Label1.Caption = "Loading Genie character."
  93. End Sub
  94. Private Sub LoadMerlin()
  95.     Set LoadRequest(1) = Agent.Characters.Load("Merlin", "Merlin.acs")
  96.         'Again, running code after the character is loaded
  97.         'the "Merlin" part is the Key of the agent, or its
  98.         'loaded name, how you can reference to it once it
  99.         'is loaded, and the Merlin.acs part is the path to,
  100.         'and name of the file, if the path is omitted
  101.         '(left out), the default path is used, which is
  102.         'the MS Agent characters directory, in this case
  103.         'C:\WINDOWS\MSAGENT\CHARS\
  104.         
  105.     Label1.Caption = "Loading Merlin character."
  106. End Sub
  107. Private Sub AddMerlinCommands()
  108.     'Add voice commands to Merlin
  109.     Merlin.Commands.Add "ChPoofs", "You want some Cheesy Poofs?", "You want some Cheesy Poofs?", True, True
  110.                         'Ref Name , Caption in the popup menu etc, What it listens for         , Whether it is enabled, whether it is visible in the popupmenu and commands window
  111.     Merlin.Commands.Add "Internet", "Internet?", "Internet?", True, True
  112.     Merlin.Commands.Add "OffChar", "Can I use Office characters with Agent?", "Can I use Office characters with Agent?", True, True
  113.     Merlin.Commands.Add "Goodbye", "Goodbye", "Goodbye", True, True
  114.     Merlin.Commands.Caption = "Merlin"
  115. End Sub
  116. Private Sub Agent_Click(ByVal CharacterID As String, ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Integer, ByVal y As Integer)
  117.     If IntroComplete And Button = vbLeftButton Then
  118.         Merlin.Play "Surprised" 'Play Merlins's Suprised
  119.                                 'animation
  120.         Merlin.Speak "Be careful with that pointer!|Don't touch me!|OUCH!|Don't try to fondle me!|Get back to your programming!"
  121.             'make Merlin speak the text, this can also be from a textbox etc.
  122.             'the '|' character is an OR, so the Agent chooses which text to say randomly, v cool
  123.         Merlin.Play "RestPose"
  124.             'Return to Merlin's Rest Pose
  125.     End If
  126. End Sub
  127. Private Sub Agent_Command(ByVal UserInput As Object)
  128.     Select Case UserInput.Name
  129.         Case "ChPoofs" 'If they said the command with the Key 'ChPoofs' then...
  130.             If Agent.AudioOutput.Enabled Then   'If they have AudioOutput installed..
  131.                 Merlin.Speak "Yeah I want cheesy poofs!", "ChPoofs.lwv"
  132.                 'Say "Yeah I want Cheesy Poofs", and play
  133.                 'the ChPoofs Linguistically Enhanced Wave
  134.                 'Sound file at the same time,
  135.                 'they are basically Wave files with the words
  136.                 'and punctuation written into them
  137.                 'Download the program for it from
  138.                 'http://msdn.microsoft.com/workshop/imedia/agent/default.asp
  139.                 'or wherever it is now, but DON'T ASK ME!
  140.                 'I am NOT going to e-mail a 6mb file to you :)
  141.             Else
  142.                 Merlin.Speak "You do not have the Lernout & Houspie TrueVoice engine installed!  Please download and install it from http://msdn.microsoft.com/workshop/imedia/agent/default.asp"
  143.             End If
  144.         Case "Internet"
  145.             If Agent.AudioOutput.Enabled Then
  146.                 Merlin.Speak ".", "KingOfTheHill.wav"
  147.                     'They can also speak wav files, the
  148.                     '[TEXT] and [URL] can be omitted
  149.                     '(left out), so you can have just text,
  150.                     'just a sound file, or both
  151.                     '(or neither, though that would be useless!)
  152.                 'An hour ago this worked fine without the ".",
  153.                 'though it wont now... try removing it :)
  154.             Else
  155.                 Merlin.Speak "You do not have the Lernout & Houspie TrueVoice engine installed!  Please download and install it from http://msdn.microsoft.com/workshop/imedia/agent/default.asp"
  156.             End If
  157.         Case "OffChar"
  158.             Merlin.Speak "Yes, \pau=200\you can use Office Assistant ACS character files, though they do not have the same animations as proper characters do, and they often cannot speak, only mime.. \pau=700\So I guess you will never hear Clippit's voice.."
  159.                 '\pau=200\ is a 'speech modifier'
  160.                 'see at the end for a definition
  161.         Case "Goodbye"
  162.             'If UserInput.Confidence < 0 Then
  163.             '    Merlin.Speak "Are you sure you want to say goodbye?"
  164.             '    Merlin.Commands.Add "Yes", "Yes", "Yes", True, True
  165.             'Else 'This would be a check of the
  166.                   'confidence level of what you said,
  167.                   'so it doesn't go wrong so easily.
  168.                   'If in doubt it asks you whether you
  169.                   'wanted to say that, adds the command
  170.                   '"YES" to the list (which you can
  171.                   'remove after), etc etc, you'll have to
  172.                   'write this bit yourself if you want it,
  173.                   'though it's only neccessary if you have
  174.                   'a LOT of commands
  175.             Merlin.Speak "From all us Agents, and Geeza, we hope you have lots of fun with MS Agent!"
  176.             Merlin.Play "Greet"
  177.             Merlin.Speak "Goodbye"
  178.             Merlin.Play "RestPose"
  179.             Set LoadRequest(4) = Merlin.Hide
  180.                 'After Merlin's hidden, Unload the form
  181.      End Select
  182. End Sub
  183. Private Sub Agent_RequestComplete(ByVal Request As Object)
  184.     'These are the code bits which can be run after an
  185.     'Agent completes an action, such as speech
  186.     'And often are used to point to other Subroutines to
  187.     'run next, though BE CAREFUL!  Overuse of this technique
  188.     'can make spider-like programs which are very hard to
  189.     'follow, and BAD PROGRAMMING!  (BASIC's flaw)
  190.     'You should see the MS Agent VB Script written by
  191.     'Microsoft I had to start with!  Not easy if you don't
  192.     'know what you're doing :)
  193.     Select Case Request
  194.         Case LoadRequest(0)
  195.             If Request.Status <> 0 Then  'Obvious
  196.                 Label1.Caption = "Genie Character failed to load!"
  197.             Else
  198.                 Label1.Caption = "Genie Character loaded successfully"
  199.                 Set Genie = Agent.Characters("Genie")
  200.             End If
  201.         Case LoadRequest(1)
  202.             If Request.Status <> 0 Then
  203.                 Label1.Caption = "Merlin Character failed to load!"
  204.             Else
  205.                 Label1.Caption = "Merlin Character loaded successfully"
  206.                 Set Merlin = Agent.Characters("Merlin")
  207.                 cmdGo.Enabled = True
  208.                 
  209.                 Call AddMerlinCommands 'Add merlin's voice
  210.                                        'commands
  211.             End If
  212.         
  213.         Case LoadRequest(2)
  214.             Agent.Characters.Unload "Genie"
  215.         Case LoadRequest(3)
  216.             Agent.Characters.Unload "Merlin"
  217.         Case LoadRequest(4)
  218.             Agent.Characters.Unload "Merlin"
  219.             Unload Me   'This is the LoadRequest Referenced
  220.                         'earlier
  221.     End Select
  222. End Sub
  223. Private Sub cmdGo_Click()
  224.     Genie.MoveTo 150, 240  'Move Genie to these (Pixel) coordinates
  225.     Genie.Show 'Show Genie (plays his "Show" animation also)
  226.     Genie.Speak "Hello!  I am Genie, \pau=100\a Microsoft Agent Character"  'Say this text
  227.     Genie.Play "Greet"  'Play Genie's animation "Greet"
  228.     Genie.Speak "And I am at your service"
  229.     Genie.Play "RestPose" 'Return Genie to default position
  230.     Genie.Speak "I hope Geeza's code will help you to create Agent scripts easily too, \pau=100\and get around the problem that we can't pronounce Geeza correctly.  \pau=200\G-eeza, \pau=200\GG eeza... \pau=70\"
  231.     Genie.Play "Explain"
  232.     Genie.Speak "\emp\Oh well..\pau=100\"
  233.     Genie.Play "RestPose"
  234.     Genie.Speak "Microsoft Agent is a very powerful tool, \pau=100\If you can find a use for it\pau=300\"
  235.     Genie.Speak "\pau=100\Agents can give help, \pau=100\play animations, \pau=100\speak text (for example, \pau=50\you can use us to read your e-mail), \pau=100\give presentations, \pau=100\and lots more\pau=100\"
  236.     Genie.Speak "We can even have multiple characters, \pau=100\interacting with you and each other\pau=500\"
  237.     Set Request = Genie.Speak("And to help me explain these features, \pau=100\here's Merlin!\pau=100\")
  238.         'Sets the Request to this, so another Character can
  239.         'be set to wait for this instruction to complete
  240.         'before comtinuing their que of commands
  241.     Genie.Play "LookLeft"
  242.     Merlin.MoveTo 320, 240
  243.     Merlin.Wait Request 'Wait for genie to finish speaking
  244.     Merlin.Show 'then Show Merlin
  245.     Merlin.Play "Wave"
  246.     Merlin.Speak "Hello everyone!"
  247.     Merlin.Play "RestPose"
  248.     Merlin.Speak "I am Merlin; \pau=150\another Microsoft Agent Character\pau=100\"
  249.     Set Request = Merlin.Speak("Though there are even more characters than us, \pau=100\such as Robby the robot,")
  250.     Merlin.Play "RestPose"
  251.     Genie.Wait Request  'Same as before
  252.     Genie.Play "LookLeftReturn"
  253.     Set Request = Genie.Speak("Peedy the Parrot,\pau=100\")
  254.     Genie.Play "LookLeft"
  255.     Merlin.Wait Request
  256.     Merlin.Play "Explain"
  257.     Set Request = Merlin.Speak("and all the Microsoft Office 2000 help Assistants!\pau=100\")
  258.     Merlin.Play "Blink"
  259.     Genie.Wait Request
  260.     Genie.Play "LookLeftReturn"
  261.     Set Request = Genie.Speak("Yes, \pau=200\all the Office 2000 Assistants are MS Agents, \pau=100\though this is not what Agent was originally written for..")
  262.     Genie.Play "LookLeft"
  263.     Merlin.Wait Request
  264.     Set Request = Merlin.Speak("We were originally created for use on the Internet, \pau=100\through web pages, \pau=100\and have many other features.  We can speak, \pau=100\as we are now, \pau=140\speak Wav sound files, \pau=140\speak Linguistically Enhanced Wave sound files, \pau=140\gesture, \pau=140\move, \pau=140\register when the user clicks or drags us, \pau=140\and even accept speech input!")
  265.     Merlin.Play "LookRight"
  266.     'Request is a MUST if you intend to use multiple
  267.     'characters, you may even need seperate request
  268.     'variables for each character, such as MerlinRequest
  269.     'don't believe me? Remove 'Set Request = ' from
  270.     'everywhere you see it, then run this
  271.     Genie.Wait Request
  272.     Genie.Play "LookLeftReturn"
  273.     Set Request = Genie.Speak("Well, \pau=100\that's enough explaining our features, \pau=100\I'm off to the pub with Clippit, \pau=200\why not let Merlin show you some of our features?")
  274.     Merlin.Wait Request
  275.     Merlin.Play "LookRightReturn"
  276.     Merlin.Speak "Later Genie!  \pau=260\Don't get too drunk"
  277.     Genie.Play "Wave"
  278.     Set LoadRequest(2) = Genie.Hide 'After he's hidden, unload him
  279.     Call MerlinSolo 'It's a good idea to separate these
  280.                     'routines, to avoid comfusion and make
  281.                     'debugging easier
  282. End Sub
  283. Private Sub MerlinSolo()
  284.     Merlin.Wait LoadRequest(2)  'wait until Genie is hidden
  285.                                 'and unloaded (you can set
  286.                                 'this to other things than
  287.                                 'Request, you see)
  288.     Merlin.Speak "Well, now Genie has gone, \pau=130\why not try voice commands.\pau=1000\"
  289.     Merlin.MoveTo ScaleX(Screen.Width, vbTwips, vbPixels) - 130, ScaleY(Screen.Height, vbTwips, vbPixels) - 170
  290.     Merlin.Play "GestureDown"
  291.     Merlin.Speak "All visible voice commands for an agent are listed in the Agent Commands Window"
  292.     Merlin.Play "Restpose"
  293.     Merlin.Speak "Just Right-Click my Magic hat icon in your system tray, \pau=130\ or right click me, \pau=130\to make my menu appear,\pau=500\"
  294.     Merlin.Speak "Then either click 'Open Voice Commands Window' to display the commands window, or if you like you can click one of my voice commands directly from the menu to run it, say if you haven't got a microphone or do not have the Speech Input addon installed.\pau=1000\"
  295.     Merlin.Speak "But, \pau=170\if you do have a microphone, hold down the Scroll-Lock key, wait for the tooltip to say I'm ready to listen, then say the command you wish me to execute\pau=750\"
  296.     Merlin.Speak "The power is now in your hands!\pau=800\"
  297.     Merlin.Speak "By the way, I like Cheesy Poofs..."
  298.     Merlin.Play "Acknowledge"
  299.     IntroComplete = True
  300. End Sub
  301. Private Sub Form_Load()
  302.     Call LoadGenie
  303.     Call LoadMerlin 'Load the characters
  304.         'This is handled differently if the show is
  305.         'continuous, see the other form
  306. End Sub
  307. Private Sub Form_Unload(Cancel As Integer)
  308.     'This doesn't work yet, and i'm too tired to make it work
  309.     'it'll be good programming practice for you anyhow :)
  310.     On Error Resume Next 'needed incase one has already been
  311.                          'unloaded
  312.     Set LoadRequest(2) = Genie.Hide 'Hide and unload the
  313.     Set LoadRequest(3) = Merlin.Hide 'characters
  314. End Sub
  315. 'OK, you should be able to see how this works :)  And if
  316. 'not, you'll still be able to use it.
  317. 'Just a few more things:
  318. 'The Speech Modifiers:
  319. '   1.  \emp\       Emphasise the next word
  320. '   2.  \pau = m\   Pause for m milliseconds
  321. '   3.  \pit = p\   Pitch voice to p Hertz (1 - 400)
  322. '   4.  \spd = s\ Set speed to s Words per minute
  323. '                   (50-250)  -Thanks Ric for that one
  324. 'These are the only three I've ever seen and know
  325. 'If you need the components for Agent, whatever they may be
  326. 'DO NOT ask me for them, i wont send them, they're way too
  327. 'big!  Get them on the microsof Agent site
  328. 'it should be at http://msdn.microsoft.com/workshop/imedia/agent/default.asp
  329. 'There is no way of listing all the animations of an Agent
  330. '(other than going through every possible combination or
  331. 'decompiling them)  so to find them out you'll need
  332. 'AllDocs.zip from the microsoft Agent site for the Merlin
  333. 'etc. animations
  334. 'If you are offended by or if i've done something wrong by
  335. 'putting in the example sound files, i am very sorry, and
  336. 'if you want i'll remove the files,  though they are just
  337. 'for a laugh, and hey it's good advertising right? :)
  338. 'Well that's just about Everything to do with Agent! lol
  339. 'i hope this helps!
  340. 'If it did, could you just add a reference to me in your
  341. '"About" box or whatever just to say that i did :)  thanks
  342. 'OK , I'm off to sleep!!
  343. 'Check out my other project on-line so far-
  344. '   Floating Objects on Invisibe Forms
  345. '   And, Killer Button!
  346. 'Goodnight All!
  347. 'GEEZA
  348. '*Of all the things I've lost, I miss my mind the most*
  349. '   -Ozzy Osbourne
  350.