Search the web for "natural language parsers." Many MUDs have very complex input parsers nowadays that let you do things like:
> get the first green sword in the 2nd wood crate
> sell it to bob
Everquest's system is actually very, very simple. It could be easily copied with simple code like the following:
Code:
#include <iostream>
#include <string>
#include <map>
class Responder
{
public:
std::string sayto(std::string msg)
{
std::map<std::string, std::string>::iterator itr = responses.begin();
for( ; itr != responses.end(); ++itr)
{
if(msg.find(itr->first) != std::string::npos)
return itr->second;
}
return "I do not understand.";
}
void add_response(std::string key, std::string resp)
{
responses[key] = resp;
}
private:
std::map<std::string, std::string> responses;
};
int main()
{
Responder r;
r.add_response("hail", "Hello traveller! Hrm, now where did I put those [rusty swords]?");
r.add_response("rusty sword", "Bring me rusty swords and I'll give you a [prize]!");
r.add_response("prize", "Shh! I can't tell you abou that!");
std::cout << "Type 'hail' to start and 'quit' to quit." << std::endl;
std::string s;
do
{
std::cout << "Say what? " << std::flush;
std::getline(std::cin, s);
if(s != "quit")
{
std::string response = r.sayto(s);
std::cout << "Bob says, '" << response << "'" << std::endl;
}
} while(s != "quit");
return 0;
}
And a sample run:
$ ./a.out
Type 'hail' to start and 'quit' to quit.
Say what? hail
Bob says, 'Hello traveller! Hrm, now where did I put those [rusty swords]?'
Say what? What rusty swords are you talking about?
Bob says, 'Bring me rusty swords and I'll give you a [prize]!'
Say what? Wow?! What type of prize?
Bob says, 'Shh! I can't tell you abou that!'
Say what? quit
$
Hope that helps.
-tf