Overview
While all the AIs live in Warzone, they each have their own dialect which poses some problems when they decide to communicate with each other. This API, inspired by a forum discussion, deals with those problems.
Babel makes it easier for AIs to speak to each other in different dialects via the chat() interface. It deals with the cruft so you don't have to:
- Dialect namespacing, error handling, etc.
- JSON conversions – including JS API Objects, primitive values, custom objects and arrays.
- Proper handling of Game objects if they get destroyed before the message arrives
It doesn't pollute the global object (other than backporting some JS API constants on old versions of Warzone) and stays out of your way when you're not using it. The API also has extensive unit testing to make sure it works both now and in the future.
Quick start
Include the API (get it from github)...
Code Block |
---|
theme | RDark |
---|
language | javascript |
---|
title | yourAI.js |
---|
linenumbers | true |
---|
|
include("path/to/APIs/Babel.js"); |
Compile a simple Babel message and send it to some AI player (tip: you can find out if player .isAI
via their Player object) ...
Code Block |
---|
theme | RDark |
---|
language | javascript |
---|
title | yourAI.js |
---|
firstline | 3 |
---|
linenumbers | true |
---|
|
// compile babel message...
// Syntax: chat.toBabel(message[, dialect[, data[, replacer]]])
var msg = chat.toBabel( "help" ); // dialect defaults to "ai"
// send compiled message via chat...
chat( someAIplayer, msg ); |
The recipient is notified when they receive the message, can check to see if it's Babel formatted and if so decompile it...
Code Block |
---|
theme | RDark |
---|
language | javascript |
---|
title | someOtherAI.js |
---|
linenumbers | true |
---|
|
include("path/to/APIs/Babel.js");
function eventChat(sender, to, message) {
if (chat.isBabel(message)) {
// decompile and use as parameters to custom event...
eventChatAI.apply( this, chat.fromBabel(sender, to, message) );
} else {
// from a human or lame AI
}
}
// Custom event handler to process the decompiled Babel message...
function eventChatAI(sender, to, message, dialect, data) {
switch (dialect) {
case "ai": { // standard dialect (all AIs should handle this)
switch (message) {
case "help": {
// send help to player
// at their beacon, or their base if no beacon
break;
}
// etc...
}
break;
}
case "nb": { // nullbot dialect
// process any nullbot messages you understand
break;
}
// etc...
case "error": { // something went wrong...
// ...
break;
}
}
} |
Obviously, you don't need to use switch statements – use whatever you want, there are certainly better ways to handle dialects, the above is just an example.
Compatibility with other APIs
Babel API is designed to run stand-alone but will automatically integrate with other APIs if they are available via the Dependency Checking system:
With the exception of Diag and Test APIs, all other APIs should obviously be loaded before loading Babel API.
If one AI is using enhanced objects (such as those provided by Players API or Defines) and another AI is using basic JS API objects, Babel will transparently convert between them. For example, if an AI sends a basic JS API Player object to an AI that has Players AI API installed, the receiving player will get an enhanced Players Object and vice versa.