Robocode Tank Royale Bot API 0.26.0
Overview
This API is used when creating bots for the Robocode Tank Royale programming game. The API handles communication
with a game server behind the scene, so you can focus on the fun part of controlling the bot.
A good way to get started with Robocode Tank Royale is to head over to the general documentation for Tank Royale
to learn about the basics first:
Another good way to get started is to look at the source files for the sample bots.
The bot classes
The first primary class that you know about first is the Bot
class
and perhaps the BaseBot
.
The BaseBot
class provides all the base and minimum functionality
of a bot and deals with the communication with the server.
The Bot
class is based on BaseBot, but provides more convenient
methods like e.g. blocking methods for moving and turning the bot, and firing the gun.
Code example
Here is an example of a simple bot using the Bot API and should run as a regular application.
MyFirstBot.java:
import dev.robocode.tankroyale.botapi.*;
import dev.robocode.tankroyale.botapi.events.*;
public class MyFirstBot extends Bot {
// The main method starts our bot
public static void main(String[] args) {
new MyFirstBot().start();
}
// Constructor, which loads the bot config file
MyFirstBot() {
super(BotInfo.fromFile("MyFirstBot.json"));
}
// Called when a new round is started -> initialize and do some movement
@Override
public void run() {
// Repeat while the bot is running
while (isRunning()) {
forward(100);
turnGunRight(360);
back(100);
turnGunRight(360);
}
}
// We saw another bot -> fire!
@Override
public void onScannedBot(ScannedBotEvent e) {
fire(1);
}
// We were hit by a bullet -> turn perpendicular to the bullet
@Override
public void onHitByBullet(HitByBulletEvent e) {
// Calculate the bearing to the direction of the bullet
double bearing = calcBearing(e.getBullet().getDirection());
// Turn 90 degrees to the bullet direction based on the bearing
turnLeft(90 - bearing);
}
}
The above code describes the behavior of the bot. The main() is the main entry point for all Java applications
to start running the program. Using the bot API, we need to start the bot by calling the
start()
method of the bot API, which will tell the server
that this bot wants to join the battle and also provide the server with the required bot info.
With the bot´s constructor (MyFirstBot()) we call the
BotInfo.fromFile(String)
method provides the bot
info for the server, like e.g. the name of the bot, and its author, etc.
The IBot.run()
method is called when the bot need to start its real
execution to send instructions to the server.
The on-methods (for example, onScannedBot and onHitByBullet) are event handlers with code that
triggers when a specific type of event occurs.
For example, the event handler onScannedBot(ScannedBotEvent)
triggers whenever an opponent bot is scanned by the radar. The ScannedBotEvent
contains the event data for the scanned
bot.
JSON config file
The code in this example is accompanied by a MyFirstBot.json, which is a JSON file containing the config file for the bot, and is used by the booter to start up the bot on a local machine.
MyFirstBot.json:
{
"name": "My First Bot",
"version": "1.0",
"authors": [
"Mathew Nelson",
"Flemming N. Larsen"
],
"description": "A sample bot that is probably the first bot you will learn about.",
"homepage": "",
"countryCodes": [
"us",
"dk"
],
"platform": "JVM",
"programmingLang": "Java 11"
}
You can read more details about the format of this JSON file here.