This AI is still in early stages of development.
Handling Dialects
- Aubergine
Overview
The quick start guide shows one way of dealing with multiple dialects – using switch statements. However, switch statements can be quite bloaty and it's not particularly easy to hive them off in to separate .js files without needing to add lots more cruft to those files.
An alternative approach is to use an object for each dialect, then use some basic JS API tricks to use the functions in those objects. For example, let's say your AI understands several dialects: "ai" (standad dialect), "nb", "cb" and "nx"...
A home for dialects
First, we need somewhere to store our dialect objects...
include("path/to/APIs/Babel.js"); chat.dialects = {};
Ok, that was easy
Dialect objects
Now let's create our first dialect object, let's do the "nb" (NullBot) dialect – it will define the function for each of the NullBot commands we want to implement...
// create an object for the NullBot commands we are implementing... chat.dialects["nb"] = {}; // now let's implement NullBot's "who" command: chat.dilalects.nb["who"] = function(sender, message, data) { // code here // maybe this command should reply with something? return [ "iAm", // message "nb", // dialect {personality: "mc"} // data ]; } // maybe we should handle the "iAm" command too? // in case a nullbot decides to tell us it's personality... chat.dialects.nb["iAm"] = function(sender, message, data) { chat(sender, "I'll research "+data.personality+" too in that case!"); } // and another one... chat.dialects.nb["res machineguns"] = function(sender, message, data) { // code here }
It's pretty easy to see how the dialect file is structured, just keep adding commands to it until you're done.
A big benefit of putting the commands for a dialect in it's own file is that other AI devs can easily see what commands you support and how you implemented them.
Triggering dialect commands
Now we need to hook up our events...
include("path/to/APIs/Babel.js"); chat.dialects = {}; include("path/to/chat.dialects.nb.js"); // NullBot dialect function eventChat(sender, to, message) { if (chat.isBabel(message)) { eventBabel.apply(this, chat.fromBabel(message)); } else { // normal chat msg handling goes here } } function eventBabel(sender, to, message, dialect, data) { var reply; if (dialect in chat.dialects && message in chat.dialects[dialect]) { // run the command reply = dialects[dialect][message].call(this, sender, message, data); // send a reply? if (reply) chat(sender, chat.toBabel.apply(this, reply)); } }
Re-using commands for human interactions
What happens if we want to make it easy for humans to use the "who" command, and any of the other commands? Well, we could do stuff like this...
const AI = true; const HUMAN = false; // update eventChat... function eventChat(sender, to, message) { if (chat.isBabel(message)) { message = chat.fromBabel(message); message.push(AI); // reply mode eventBabel.apply(this, message); } else { message = message.split("!").pop().toLowerCase(); eventBabel.call(this, sender, message, "nb", null, HUMAN); } } function eventBabel(sender, to, message, dialect, data, replyMode) { var reply; if (dialect in chat.dialects && message in chat.dialects[dialect]) { // run the command reply = dialects[dialect][message].call(this, sender, message, data, replyMode); // send a reply? if (reply) { reutrn (replyMode == AI) ? chat(sender, chat.toBabel.apply(this, reply)); // ai readable : chat(sender, reply) // human readable } } }
And then update the replies sent by commands in the dialect file...
// create an object for the NullBot commands we are implementing... chat.dialects["nb"] = {}; // now let's implement NullBot's "who" command: chat.dilalects.nb["who"] = function(sender, message, data, replyMode) { // code here // send different reply if sender is human return (replyMode == AI) ? ["iAm", "nb", {personality: "mc"}] // ai readable : "I'm researching Machineguns and Cannons"; // human readable } // etc...
Contents
Jump to:
Babel API
Topics:
- Data types — Babel supports most types of data...
- Error Dialect — A specific dialect for reporting errors within Babel...
- Babel on Warzone 3.1 — Babel handles Warzone 3.1 gracefully...
- Position-like Objects — If your data has {x, y} properties, Babel treats it like a Position object...
- chat.toBabel() — Compiles a Babel format message, ready for sending to your AI buddies...
- chat.isBabel() — Quickly determine if a received message is in Babel format...
- chat.fromBabel() — Converts a Babel format message in to an array...
- Handling Dialects — Some ideas on how to handle multiple dialects...